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.
12 #include <sys/types.h>
13 #include <sys/queue.h>
16 #include <bitstring.h>
21 #include "../common/common.h"
26 * In historic vi, a sentence was delimited by a '.', '?' or '!' character
27 * followed by TWO spaces or a newline. One or more empty lines was also
28 * treated as a separate sentence. The Berkeley documentation for historical
29 * vi states that any number of ')', ']', '"' and '\'' characters can be
30 * between the delimiter character and the spaces or end of line, however,
31 * the historical implementation did not handle additional '"' characters.
32 * We follow the documentation here, not the implementation.
34 * Once again, historical vi didn't do sentence movements associated with
35 * counts consistently, mostly in the presence of lines containing only
36 * white-space characters.
38 * This implementation also permits a single tab to delimit sentences, and
39 * treats lines containing only white-space characters as empty lines.
40 * Finally, tabs are eaten (along with spaces) when skipping to the start
41 * of the text following a "sentence".
45 * v_sentencef -- [count])
46 * Move forward count sentences.
48 * PUBLIC: int v_sentencef(SCR *, VICMD *);
51 v_sentencef(SCR
*sp
, VICMD
*vp
)
53 enum { BLANK
, NONE
, PERIOD
} state
;
58 cs
.cs_lno
= vp
->m_start
.lno
;
59 cs
.cs_cno
= vp
->m_start
.cno
;
63 cnt
= F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1;
67 * If in white-space, the next start of sentence counts as one.
68 * This may not handle " . " correctly, but it's real unclear
69 * what correctly means in that case.
71 if (cs
.cs_flags
== CS_EMP
|| (cs
.cs_flags
== 0 && isblank(cs
.cs_ch
))) {
72 if (cs_fblank(sp
, &cs
))
75 if (vp
->m_start
.lno
!= cs
.cs_lno
||
76 vp
->m_start
.cno
!= cs
.cs_cno
)
82 for (state
= NONE
;;) {
85 if (cs
.cs_flags
== CS_EOF
)
87 if (cs
.cs_flags
== CS_EOL
) {
88 if ((state
== PERIOD
|| state
== BLANK
) && --cnt
== 0) {
91 if (cs
.cs_flags
== 0 &&
92 isblank(cs
.cs_ch
) && cs_fblank(sp
, &cs
))
99 if (cs
.cs_flags
== CS_EMP
) { /* An EMP is two sentences. */
102 if (cs_fblank(sp
, &cs
))
127 if (state
== PERIOD
) {
131 if (state
== BLANK
&& --cnt
== 0) {
132 if (cs_fblank(sp
, &cs
))
143 /* EOF is a movement sink, but it's an error not to have moved. */
144 if (vp
->m_start
.lno
== cs
.cs_lno
&& vp
->m_start
.cno
== cs
.cs_cno
) {
149 okret
: vp
->m_stop
.lno
= cs
.cs_lno
;
150 vp
->m_stop
.cno
= cs
.cs_cno
;
154 * Historic, uh, features, yeah, that's right, call 'em features.
155 * If the starting and ending cursor positions are at the first
156 * column in their lines, i.e. the movement is cutting entire lines,
157 * the buffer is in line mode, and the ending position is the last
158 * character of the previous line. Note check to make sure that
159 * it's not within a single line.
161 * Non-motion commands move to the end of the range. Delete and
162 * yank stay at the start. Ignore others. Adjust the end of the
163 * range for motion commands.
166 if (vp
->m_start
.cno
== 0 &&
167 (cs
.cs_flags
!= 0 || vp
->m_stop
.cno
== 0)) {
168 if (vp
->m_start
.lno
< vp
->m_stop
.lno
) {
170 --vp
->m_stop
.lno
, DBG_FATAL
, NULL
, &len
))
172 vp
->m_stop
.cno
= len
? len
- 1 : 0;
177 vp
->m_final
= vp
->m_start
;
179 vp
->m_final
= vp
->m_stop
;
184 * v_sentenceb -- [count](
185 * Move backward count sentences.
187 * PUBLIC: int v_sentenceb(SCR *, VICMD *);
190 v_sentenceb(SCR
*sp
, VICMD
*vp
)
200 * Historic vi permitted the user to hit SOF repeatedly.
202 if (vp
->m_start
.lno
== 1 && vp
->m_start
.cno
== 0)
205 cs
.cs_lno
= vp
->m_start
.lno
;
206 cs
.cs_cno
= vp
->m_start
.cno
;
207 if (cs_init(sp
, &cs
))
210 cnt
= F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1;
214 * In empty lines, skip to the previous non-white-space character.
215 * If in text, skip to the prevous white-space character. Believe
216 * it or not, in the paragraph:
219 * if the cursor is on the 'A' or 'B', ( moves to the 'a'. If it
220 * is on the ' ', 'C' or 'D', it moves to the 'A'. Yes, Virginia,
221 * Berkeley was once a major center of drug activity.
223 if (cs
.cs_flags
== CS_EMP
) {
224 if (cs_bblank(sp
, &cs
))
227 if (cs_prev(sp
, &cs
))
229 if (cs
.cs_flags
!= CS_EOL
)
232 } else if (cs
.cs_flags
== 0 && !isblank(cs
.cs_ch
))
234 if (cs_prev(sp
, &cs
))
236 if (cs
.cs_flags
!= 0 || isblank(cs
.cs_ch
))
241 if (cs_prev(sp
, &cs
))
243 if (cs
.cs_flags
== CS_SOF
) /* SOF is a movement sink. */
245 if (cs
.cs_flags
== CS_EOL
) {
249 if (cs
.cs_flags
== CS_EMP
) {
252 if (cs_bblank(sp
, &cs
))
261 if (!last
|| --cnt
!= 0) {
266 ret
: slno
= cs
.cs_lno
;
270 * Move to the start of the sentence, skipping blanks
271 * and special characters.
274 if (cs_next(sp
, &cs
))
276 } while (!cs
.cs_flags
&&
277 (cs
.cs_ch
== ')' || cs
.cs_ch
== ']' ||
278 cs
.cs_ch
== '"' || cs
.cs_ch
== '\''));
279 if ((cs
.cs_flags
|| isblank(cs
.cs_ch
)) &&
284 * If it was ". xyz", with the cursor on the 'x', or
285 * "end. ", with the cursor in the spaces, or the
286 * beginning of a sentence preceded by an empty line,
287 * we can end up where we started. Fix it.
289 if (vp
->m_start
.lno
!= cs
.cs_lno
||
290 vp
->m_start
.cno
> cs
.cs_cno
)
294 * Well, if an empty line preceded possible blanks
295 * and the sentence, it could be a real sentence.
298 if (cs_prev(sp
, &cs
))
300 if (cs
.cs_flags
== CS_EOL
)
302 if (cs
.cs_flags
== 0 && isblank(cs
.cs_ch
))
306 if (cs
.cs_flags
== CS_EMP
)
309 /* But it wasn't; try again. */
320 cs
.cs_flags
== CS_EOL
|| isblank(cs
.cs_ch
) ||
321 cs
.cs_ch
== ')' || cs
.cs_ch
== ']' ||
322 cs
.cs_ch
== '"' || cs
.cs_ch
== '\'' ? 1 : 0;
326 okret
: vp
->m_stop
.lno
= cs
.cs_lno
;
327 vp
->m_stop
.cno
= cs
.cs_cno
;
331 * If the starting and stopping cursor positions are at the first
332 * columns in the line, i.e. the movement is cutting an entire line,
333 * the buffer is in line mode, and the starting position is the last
334 * character of the previous line.
336 * All commands move to the end of the range. Adjust the start of
337 * the range for motion commands.
340 if (vp
->m_start
.cno
== 0 &&
341 (cs
.cs_flags
!= 0 || vp
->m_stop
.cno
== 0)) {
343 --vp
->m_start
.lno
, DBG_FATAL
, NULL
, &len
))
345 vp
->m_start
.cno
= len
? len
- 1 : 0;
349 vp
->m_final
= vp
->m_stop
;