2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
13 static const char sccsid
[] = "$Id: v_sentence.c,v 10.9 2001/06/25 15:19:35 skimo Exp $ (Berkeley) $Date: 2001/06/25 15:19:35 $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
20 #include <bitstring.h>
25 #include "../common/common.h"
30 * In historic vi, a sentence was delimited by a '.', '?' or '!' character
31 * followed by TWO spaces or a newline. One or more empty lines was also
32 * treated as a separate sentence. The Berkeley documentation for historical
33 * vi states that any number of ')', ']', '"' and '\'' characters can be
34 * between the delimiter character and the spaces or end of line, however,
35 * the historical implementation did not handle additional '"' characters.
36 * We follow the documentation here, not the implementation.
38 * Once again, historical vi didn't do sentence movements associated with
39 * counts consistently, mostly in the presence of lines containing only
40 * white-space characters.
42 * This implementation also permits a single tab to delimit sentences, and
43 * treats lines containing only white-space characters as empty lines.
44 * Finally, tabs are eaten (along with spaces) when skipping to the start
45 * of the text following a "sentence".
49 * v_sentencef -- [count])
50 * Move forward count sentences.
52 * PUBLIC: int v_sentencef __P((SCR *, VICMD *));
55 v_sentencef(SCR
*sp
, VICMD
*vp
)
57 enum { BLANK
, NONE
, PERIOD
} state
;
62 cs
.cs_lno
= vp
->m_start
.lno
;
63 cs
.cs_cno
= vp
->m_start
.cno
;
67 cnt
= F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1;
71 * If in white-space, the next start of sentence counts as one.
72 * This may not handle " . " correctly, but it's real unclear
73 * what correctly means in that case.
75 if (cs
.cs_flags
== CS_EMP
|| cs
.cs_flags
== 0 && isblank(cs
.cs_ch
)) {
76 if (cs_fblank(sp
, &cs
))
79 if (vp
->m_start
.lno
!= cs
.cs_lno
||
80 vp
->m_start
.cno
!= cs
.cs_cno
)
86 for (state
= NONE
;;) {
89 if (cs
.cs_flags
== CS_EOF
)
91 if (cs
.cs_flags
== CS_EOL
) {
92 if ((state
== PERIOD
|| state
== BLANK
) && --cnt
== 0) {
95 if (cs
.cs_flags
== 0 &&
96 isblank(cs
.cs_ch
) && cs_fblank(sp
, &cs
))
103 if (cs
.cs_flags
== CS_EMP
) { /* An EMP is two sentences. */
106 if (cs_fblank(sp
, &cs
))
131 if (state
== PERIOD
) {
135 if (state
== BLANK
&& --cnt
== 0) {
136 if (cs_fblank(sp
, &cs
))
147 /* EOF is a movement sink, but it's an error not to have moved. */
148 if (vp
->m_start
.lno
== cs
.cs_lno
&& vp
->m_start
.cno
== cs
.cs_cno
) {
153 okret
: vp
->m_stop
.lno
= cs
.cs_lno
;
154 vp
->m_stop
.cno
= cs
.cs_cno
;
158 * Historic, uh, features, yeah, that's right, call 'em features.
159 * If the starting and ending cursor positions are at the first
160 * column in their lines, i.e. the movement is cutting entire lines,
161 * the buffer is in line mode, and the ending position is the last
162 * character of the previous line. Note check to make sure that
163 * it's not within a single line.
165 * Non-motion commands move to the end of the range. Delete and
166 * yank stay at the start. Ignore others. Adjust the end of the
167 * range for motion commands.
170 if (vp
->m_start
.cno
== 0 &&
171 (cs
.cs_flags
!= 0 || vp
->m_stop
.cno
== 0)) {
172 if (vp
->m_start
.lno
< vp
->m_stop
.lno
) {
174 --vp
->m_stop
.lno
, DBG_FATAL
, NULL
, &len
))
176 vp
->m_stop
.cno
= len
? len
- 1 : 0;
181 vp
->m_final
= vp
->m_start
;
183 vp
->m_final
= vp
->m_stop
;
188 * v_sentenceb -- [count](
189 * Move backward count sentences.
191 * PUBLIC: int v_sentenceb __P((SCR *, VICMD *));
194 v_sentenceb(SCR
*sp
, VICMD
*vp
)
204 * Historic vi permitted the user to hit SOF repeatedly.
206 if (vp
->m_start
.lno
== 1 && vp
->m_start
.cno
== 0)
209 cs
.cs_lno
= vp
->m_start
.lno
;
210 cs
.cs_cno
= vp
->m_start
.cno
;
211 if (cs_init(sp
, &cs
))
214 cnt
= F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1;
218 * In empty lines, skip to the previous non-white-space character.
219 * If in text, skip to the prevous white-space character. Believe
220 * it or not, in the paragraph:
223 * if the cursor is on the 'A' or 'B', ( moves to the 'a'. If it
224 * is on the ' ', 'C' or 'D', it moves to the 'A'. Yes, Virginia,
225 * Berkeley was once a major center of drug activity.
227 if (cs
.cs_flags
== CS_EMP
) {
228 if (cs_bblank(sp
, &cs
))
231 if (cs_prev(sp
, &cs
))
233 if (cs
.cs_flags
!= CS_EOL
)
236 } else if (cs
.cs_flags
== 0 && !isblank(cs
.cs_ch
))
238 if (cs_prev(sp
, &cs
))
240 if (cs
.cs_flags
!= 0 || isblank(cs
.cs_ch
))
245 if (cs_prev(sp
, &cs
))
247 if (cs
.cs_flags
== CS_SOF
) /* SOF is a movement sink. */
249 if (cs
.cs_flags
== CS_EOL
) {
253 if (cs
.cs_flags
== CS_EMP
) {
256 if (cs_bblank(sp
, &cs
))
265 if (!last
|| --cnt
!= 0) {
270 ret
: slno
= cs
.cs_lno
;
274 * Move to the start of the sentence, skipping blanks
275 * and special characters.
278 if (cs_next(sp
, &cs
))
280 } while (!cs
.cs_flags
&&
281 (cs
.cs_ch
== ')' || cs
.cs_ch
== ']' ||
282 cs
.cs_ch
== '"' || cs
.cs_ch
== '\''));
283 if ((cs
.cs_flags
|| isblank(cs
.cs_ch
)) &&
288 * If it was ". xyz", with the cursor on the 'x', or
289 * "end. ", with the cursor in the spaces, or the
290 * beginning of a sentence preceded by an empty line,
291 * we can end up where we started. Fix it.
293 if (vp
->m_start
.lno
!= cs
.cs_lno
||
294 vp
->m_start
.cno
!= cs
.cs_cno
)
298 * Well, if an empty line preceded possible blanks
299 * and the sentence, it could be a real sentence.
302 if (cs_prev(sp
, &cs
))
304 if (cs
.cs_flags
== CS_EOL
)
306 if (cs
.cs_flags
== 0 && isblank(cs
.cs_ch
))
310 if (cs
.cs_flags
== CS_EMP
)
313 /* But it wasn't; try again. */
324 cs
.cs_flags
== CS_EOL
|| isblank(cs
.cs_ch
) ||
325 cs
.cs_ch
== ')' || cs
.cs_ch
== ']' ||
326 cs
.cs_ch
== '"' || cs
.cs_ch
== '\'' ? 1 : 0;
330 okret
: vp
->m_stop
.lno
= cs
.cs_lno
;
331 vp
->m_stop
.cno
= cs
.cs_cno
;
335 * If the starting and stopping cursor positions are at the first
336 * columns in the line, i.e. the movement is cutting an entire line,
337 * the buffer is in line mode, and the starting position is the last
338 * character of the previous line.
340 * All commands move to the end of the range. Adjust the start of
341 * the range for motion commands.
344 if (vp
->m_start
.cno
== 0 &&
345 (cs
.cs_flags
!= 0 || vp
->m_stop
.cno
== 0)) {
347 --vp
->m_start
.lno
, DBG_FATAL
, NULL
, &len
))
349 vp
->m_start
.cno
= len
? len
- 1 : 0;
353 vp
->m_final
= vp
->m_stop
;