1 /* $NetBSD: v_sentence.c,v 1.3 2014/01/26 21:43:45 christos Exp $ */
3 * Copyright (c) 1992, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1992, 1993, 1994, 1995, 1996
6 * Keith Bostic. All rights reserved.
8 * See the LICENSE file for redistribution information.
13 #include <sys/cdefs.h>
16 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 ";
19 __RCSID("$NetBSD: v_sentence.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
22 #include <sys/types.h>
23 #include <sys/queue.h>
26 #include <bitstring.h>
31 #include "../common/common.h"
36 * In historic vi, a sentence was delimited by a '.', '?' or '!' character
37 * followed by TWO spaces or a newline. One or more empty lines was also
38 * treated as a separate sentence. The Berkeley documentation for historical
39 * vi states that any number of ')', ']', '"' and '\'' characters can be
40 * between the delimiter character and the spaces or end of line, however,
41 * the historical implementation did not handle additional '"' characters.
42 * We follow the documentation here, not the implementation.
44 * Once again, historical vi didn't do sentence movements associated with
45 * counts consistently, mostly in the presence of lines containing only
46 * white-space characters.
48 * This implementation also permits a single tab to delimit sentences, and
49 * treats lines containing only white-space characters as empty lines.
50 * Finally, tabs are eaten (along with spaces) when skipping to the start
51 * of the text following a "sentence".
55 * v_sentencef -- [count])
56 * Move forward count sentences.
58 * PUBLIC: int v_sentencef __P((SCR *, VICMD *));
61 v_sentencef(SCR
*sp
, VICMD
*vp
)
63 enum { BLANK
, NONE
, PERIOD
} state
;
68 cs
.cs_lno
= vp
->m_start
.lno
;
69 cs
.cs_cno
= vp
->m_start
.cno
;
73 cnt
= F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1;
77 * If in white-space, the next start of sentence counts as one.
78 * This may not handle " . " correctly, but it's real unclear
79 * what correctly means in that case.
81 if (cs
.cs_flags
== CS_EMP
|| (cs
.cs_flags
== 0 && ISBLANK2(cs
.cs_ch
))) {
82 if (cs_fblank(sp
, &cs
))
85 if (vp
->m_start
.lno
!= cs
.cs_lno
||
86 vp
->m_start
.cno
!= cs
.cs_cno
)
92 for (state
= NONE
;;) {
95 if (cs
.cs_flags
== CS_EOF
)
97 if (cs
.cs_flags
== CS_EOL
) {
98 if ((state
== PERIOD
|| state
== BLANK
) && --cnt
== 0) {
101 if (cs
.cs_flags
== 0 &&
102 ISBLANK2(cs
.cs_ch
) && cs_fblank(sp
, &cs
))
109 if (cs
.cs_flags
== CS_EMP
) { /* An EMP is two sentences. */
112 if (cs_fblank(sp
, &cs
))
137 if (state
== PERIOD
) {
141 if (state
== BLANK
&& --cnt
== 0) {
142 if (cs_fblank(sp
, &cs
))
153 /* EOF is a movement sink, but it's an error not to have moved. */
154 if (vp
->m_start
.lno
== cs
.cs_lno
&& vp
->m_start
.cno
== cs
.cs_cno
) {
159 okret
: vp
->m_stop
.lno
= cs
.cs_lno
;
160 vp
->m_stop
.cno
= cs
.cs_cno
;
164 * Historic, uh, features, yeah, that's right, call 'em features.
165 * If the starting and ending cursor positions are at the first
166 * column in their lines, i.e. the movement is cutting entire lines,
167 * the buffer is in line mode, and the ending position is the last
168 * character of the previous line. Note check to make sure that
169 * it's not within a single line.
171 * Non-motion commands move to the end of the range. Delete and
172 * yank stay at the start. Ignore others. Adjust the end of the
173 * range for motion commands.
176 if (vp
->m_start
.cno
== 0 &&
177 (cs
.cs_flags
!= 0 || vp
->m_stop
.cno
== 0)) {
178 if (vp
->m_start
.lno
< vp
->m_stop
.lno
) {
180 --vp
->m_stop
.lno
, DBG_FATAL
, NULL
, &len
))
182 vp
->m_stop
.cno
= len
? len
- 1 : 0;
187 vp
->m_final
= vp
->m_start
;
189 vp
->m_final
= vp
->m_stop
;
194 * v_sentenceb -- [count](
195 * Move backward count sentences.
197 * PUBLIC: int v_sentenceb __P((SCR *, VICMD *));
200 v_sentenceb(SCR
*sp
, VICMD
*vp
)
210 * Historic vi permitted the user to hit SOF repeatedly.
212 if (vp
->m_start
.lno
== 1 && vp
->m_start
.cno
== 0)
215 cs
.cs_lno
= vp
->m_start
.lno
;
216 cs
.cs_cno
= vp
->m_start
.cno
;
217 if (cs_init(sp
, &cs
))
220 cnt
= F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1;
224 * In empty lines, skip to the previous non-white-space character.
225 * If in text, skip to the prevous white-space character. Believe
226 * it or not, in the paragraph:
229 * if the cursor is on the 'A' or 'B', ( moves to the 'a'. If it
230 * is on the ' ', 'C' or 'D', it moves to the 'A'. Yes, Virginia,
231 * Berkeley was once a major center of drug activity.
233 if (cs
.cs_flags
== CS_EMP
) {
234 if (cs_bblank(sp
, &cs
))
237 if (cs_prev(sp
, &cs
))
239 if (cs
.cs_flags
!= CS_EOL
)
242 } else if (cs
.cs_flags
== 0 && !ISBLANK2(cs
.cs_ch
))
244 if (cs_prev(sp
, &cs
))
246 if (cs
.cs_flags
!= 0 || ISBLANK2(cs
.cs_ch
))
251 if (cs_prev(sp
, &cs
))
253 if (cs
.cs_flags
== CS_SOF
) /* SOF is a movement sink. */
255 if (cs
.cs_flags
== CS_EOL
) {
259 if (cs
.cs_flags
== CS_EMP
) {
262 if (cs_bblank(sp
, &cs
))
271 if (!last
|| --cnt
!= 0) {
276 ret
: slno
= cs
.cs_lno
;
280 * Move to the start of the sentence, skipping blanks
281 * and special characters.
284 if (cs_next(sp
, &cs
))
286 } while (!cs
.cs_flags
&&
287 (cs
.cs_ch
== ')' || cs
.cs_ch
== ']' ||
288 cs
.cs_ch
== '"' || cs
.cs_ch
== '\''));
289 if ((cs
.cs_flags
|| ISBLANK2(cs
.cs_ch
)) &&
294 * If it was ". xyz", with the cursor on the 'x', or
295 * "end. ", with the cursor in the spaces, or the
296 * beginning of a sentence preceded by an empty line,
297 * we can end up where we started. Fix it.
299 if (vp
->m_start
.lno
!= cs
.cs_lno
||
300 vp
->m_start
.cno
!= cs
.cs_cno
)
304 * Well, if an empty line preceded possible blanks
305 * and the sentence, it could be a real sentence.
308 if (cs_prev(sp
, &cs
))
310 if (cs
.cs_flags
== CS_EOL
)
312 if (cs
.cs_flags
== 0 && ISBLANK2(cs
.cs_ch
))
316 if (cs
.cs_flags
== CS_EMP
)
319 /* But it wasn't; try again. */
330 cs
.cs_flags
== CS_EOL
|| ISBLANK2(cs
.cs_ch
) ||
331 cs
.cs_ch
== ')' || cs
.cs_ch
== ']' ||
332 cs
.cs_ch
== '"' || cs
.cs_ch
== '\'' ? 1 : 0;
336 okret
: vp
->m_stop
.lno
= cs
.cs_lno
;
337 vp
->m_stop
.cno
= cs
.cs_cno
;
341 * If the starting and stopping cursor positions are at the first
342 * columns in the line, i.e. the movement is cutting an entire line,
343 * the buffer is in line mode, and the starting position is the last
344 * character of the previous line.
346 * All commands move to the end of the range. Adjust the start of
347 * the range for motion commands.
350 if (vp
->m_start
.cno
== 0 &&
351 (cs
.cs_flags
!= 0 || vp
->m_stop
.cno
== 0)) {
353 --vp
->m_start
.lno
, DBG_FATAL
, NULL
, &len
))
355 vp
->m_start
.cno
= len
? len
- 1 : 0;
360 vp
->m_final
= vp
->m_stop
;