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.8 2000/04/21 19:00:41 skimo Exp $ (Berkeley) $Date: 2000/04/21 19:00:41 $";
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 *));
59 enum { BLANK
, NONE
, PERIOD
} state
;
64 cs
.cs_lno
= vp
->m_start
.lno
;
65 cs
.cs_cno
= vp
->m_start
.cno
;
69 cnt
= F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1;
73 * If in white-space, the next start of sentence counts as one.
74 * This may not handle " . " correctly, but it's real unclear
75 * what correctly means in that case.
77 if (cs
.cs_flags
== CS_EMP
|| cs
.cs_flags
== 0 && isblank(cs
.cs_ch
)) {
78 if (cs_fblank(sp
, &cs
))
81 if (vp
->m_start
.lno
!= cs
.cs_lno
||
82 vp
->m_start
.cno
!= cs
.cs_cno
)
88 for (state
= NONE
;;) {
91 if (cs
.cs_flags
== CS_EOF
)
93 if (cs
.cs_flags
== CS_EOL
) {
94 if ((state
== PERIOD
|| state
== BLANK
) && --cnt
== 0) {
97 if (cs
.cs_flags
== 0 &&
98 isblank(cs
.cs_ch
) && cs_fblank(sp
, &cs
))
105 if (cs
.cs_flags
== CS_EMP
) { /* An EMP is two sentences. */
108 if (cs_fblank(sp
, &cs
))
133 if (state
== PERIOD
) {
137 if (state
== BLANK
&& --cnt
== 0) {
138 if (cs_fblank(sp
, &cs
))
149 /* EOF is a movement sink, but it's an error not to have moved. */
150 if (vp
->m_start
.lno
== cs
.cs_lno
&& vp
->m_start
.cno
== cs
.cs_cno
) {
155 okret
: vp
->m_stop
.lno
= cs
.cs_lno
;
156 vp
->m_stop
.cno
= cs
.cs_cno
;
160 * Historic, uh, features, yeah, that's right, call 'em features.
161 * If the starting and ending cursor positions are at the first
162 * column in their lines, i.e. the movement is cutting entire lines,
163 * the buffer is in line mode, and the ending position is the last
164 * character of the previous line. Note check to make sure that
165 * it's not within a single line.
167 * Non-motion commands move to the end of the range. Delete and
168 * yank stay at the start. Ignore others. Adjust the end of the
169 * range for motion commands.
172 if (vp
->m_start
.cno
== 0 &&
173 (cs
.cs_flags
!= 0 || vp
->m_stop
.cno
== 0)) {
174 if (vp
->m_start
.lno
< vp
->m_stop
.lno
) {
176 --vp
->m_stop
.lno
, DBG_FATAL
, NULL
, &len
))
178 vp
->m_stop
.cno
= len
? len
- 1 : 0;
183 vp
->m_final
= vp
->m_start
;
185 vp
->m_final
= vp
->m_stop
;
190 * v_sentenceb -- [count](
191 * Move backward count sentences.
193 * PUBLIC: int v_sentenceb __P((SCR *, VICMD *));
208 * Historic vi permitted the user to hit SOF repeatedly.
210 if (vp
->m_start
.lno
== 1 && vp
->m_start
.cno
== 0)
213 cs
.cs_lno
= vp
->m_start
.lno
;
214 cs
.cs_cno
= vp
->m_start
.cno
;
215 if (cs_init(sp
, &cs
))
218 cnt
= F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1;
222 * In empty lines, skip to the previous non-white-space character.
223 * If in text, skip to the prevous white-space character. Believe
224 * it or not, in the paragraph:
227 * if the cursor is on the 'A' or 'B', ( moves to the 'a'. If it
228 * is on the ' ', 'C' or 'D', it moves to the 'A'. Yes, Virginia,
229 * Berkeley was once a major center of drug activity.
231 if (cs
.cs_flags
== CS_EMP
) {
232 if (cs_bblank(sp
, &cs
))
235 if (cs_prev(sp
, &cs
))
237 if (cs
.cs_flags
!= CS_EOL
)
240 } else if (cs
.cs_flags
== 0 && !isblank(cs
.cs_ch
))
242 if (cs_prev(sp
, &cs
))
244 if (cs
.cs_flags
!= 0 || isblank(cs
.cs_ch
))
249 if (cs_prev(sp
, &cs
))
251 if (cs
.cs_flags
== CS_SOF
) /* SOF is a movement sink. */
253 if (cs
.cs_flags
== CS_EOL
) {
257 if (cs
.cs_flags
== CS_EMP
) {
260 if (cs_bblank(sp
, &cs
))
269 if (!last
|| --cnt
!= 0) {
274 ret
: slno
= cs
.cs_lno
;
278 * Move to the start of the sentence, skipping blanks
279 * and special characters.
282 if (cs_next(sp
, &cs
))
284 } while (!cs
.cs_flags
&&
285 (cs
.cs_ch
== ')' || cs
.cs_ch
== ']' ||
286 cs
.cs_ch
== '"' || cs
.cs_ch
== '\''));
287 if ((cs
.cs_flags
|| isblank(cs
.cs_ch
)) &&
292 * If it was ". xyz", with the cursor on the 'x', or
293 * "end. ", with the cursor in the spaces, or the
294 * beginning of a sentence preceded by an empty line,
295 * we can end up where we started. Fix it.
297 if (vp
->m_start
.lno
!= cs
.cs_lno
||
298 vp
->m_start
.cno
!= cs
.cs_cno
)
302 * Well, if an empty line preceded possible blanks
303 * and the sentence, it could be a real sentence.
306 if (cs_prev(sp
, &cs
))
308 if (cs
.cs_flags
== CS_EOL
)
310 if (cs
.cs_flags
== 0 && isblank(cs
.cs_ch
))
314 if (cs
.cs_flags
== CS_EMP
)
317 /* But it wasn't; try again. */
328 cs
.cs_flags
== CS_EOL
|| isblank(cs
.cs_ch
) ||
329 cs
.cs_ch
== ')' || cs
.cs_ch
== ']' ||
330 cs
.cs_ch
== '"' || cs
.cs_ch
== '\'' ? 1 : 0;
334 okret
: vp
->m_stop
.lno
= cs
.cs_lno
;
335 vp
->m_stop
.cno
= cs
.cs_cno
;
339 * If the starting and stopping cursor positions are at the first
340 * columns in the line, i.e. the movement is cutting an entire line,
341 * the buffer is in line mode, and the starting position is the last
342 * character of the previous line.
344 * All commands move to the end of the range. Adjust the start of
345 * the range for motion commands.
348 if (vp
->m_start
.cno
== 0 &&
349 (cs
.cs_flags
!= 0 || vp
->m_stop
.cno
== 0)) {
351 --vp
->m_start
.lno
, DBG_FATAL
, NULL
, &len
))
353 vp
->m_start
.cno
= len
? len
- 1 : 0;
357 vp
->m_final
= vp
->m_stop
;