1 /* $NetBSD: v_word.c,v 1.2 2013/11/22 15:52:06 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.
14 static const char sccsid
[] = "Id: v_word.c,v 10.6 2001/06/25 15:19:36 skimo Exp (Berkeley) Date: 2001/06/25 15:19:36 ";
17 #include <sys/types.h>
18 #include <sys/queue.h>
21 #include <bitstring.h>
26 #include "../common/common.h"
30 * There are two types of "words". Bigwords are easy -- groups of anything
31 * delimited by whitespace. Normal words are trickier. They are either a
32 * group of characters, numbers and underscores, or a group of anything but,
33 * delimited by whitespace. When for a word, if you're in whitespace, it's
34 * easy, just remove the whitespace and go to the beginning or end of the
35 * word. Otherwise, figure out if the next character is in a different group.
36 * If it is, go to the beginning or end of that group, otherwise, go to the
37 * beginning or end of the current group. The historic version of vi didn't
38 * get this right, so, for example, there were cases where "4e" was not the
39 * same as "eeee" -- in particular, single character words, and commands that
40 * began in whitespace were almost always handled incorrectly. To get it right
41 * you have to resolve the cursor after each search so that the look-ahead to
42 * figure out what type of "word" the cursor is in will be correct.
44 * Empty lines, and lines that consist of only white-space characters count
45 * as a single word, and the beginning and end of the file counts as an
46 * infinite number of words.
48 * Movements associated with commands are different than movement commands.
49 * For example, in "abc def", with the cursor on the 'a', "cw" is from
50 * 'a' to 'c', while "w" is from 'a' to 'd'. In general, trailing white
51 * space is discarded from the change movement. Another example is that,
52 * in the same string, a "cw" on any white space character replaces that
53 * single character, and nothing else. Ain't nothin' in here that's easy.
55 * One historic note -- in the original vi, the 'w', 'W' and 'B' commands
56 * would treat groups of empty lines as individual words, i.e. the command
57 * would move the cursor to each new empty line. The 'e' and 'E' commands
58 * would treat groups of empty lines as a single word, i.e. the first use
59 * would move past the group of lines. The 'b' command would just beep at
60 * you, or, if you did it from the start of the line as part of a motion
61 * command, go absolutely nuts. If the lines contained only white-space
62 * characters, the 'w' and 'W' commands would just beep at you, and the 'B',
63 * 'b', 'E' and 'e' commands would treat the group as a single word, and
64 * the 'B' and 'b' commands will treat the lines as individual words. This
65 * implementation treats all of these cases as a single white-space word.
68 enum which
{BIGWORD
, LITTLEWORD
};
70 static int bword
__P((SCR
*, VICMD
*, enum which
));
71 static int eword
__P((SCR
*, VICMD
*, enum which
));
72 static int fword
__P((SCR
*, VICMD
*, enum which
));
76 * Move forward a bigword at a time.
78 * PUBLIC: int v_wordW __P((SCR *, VICMD *));
81 v_wordW(SCR
*sp
, VICMD
*vp
)
83 return (fword(sp
, vp
, BIGWORD
));
88 * Move forward a word at a time.
90 * PUBLIC: int v_wordw __P((SCR *, VICMD *));
93 v_wordw(SCR
*sp
, VICMD
*vp
)
95 return (fword(sp
, vp
, LITTLEWORD
));
100 * Move forward by words.
103 fword(SCR
*sp
, VICMD
*vp
, enum which type
)
105 enum { INWORD
, NOTWORD
} state
;
109 cnt
= F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1;
110 cs
.cs_lno
= vp
->m_start
.lno
;
111 cs
.cs_cno
= vp
->m_start
.cno
;
112 if (cs_init(sp
, &cs
))
117 * If the count is 1, and it's a change command, we're done.
118 * Else, move to the first non-white-space character, which
119 * counts as a single word move. If it's a motion command,
120 * don't move off the end of the line.
122 if (cs
.cs_flags
== CS_EMP
|| (cs
.cs_flags
== 0 && ISBLANK2(cs
.cs_ch
))) {
123 if (ISMOTION(vp
) && cs
.cs_flags
!= CS_EMP
&& cnt
== 1) {
124 if (ISCMD(vp
->rkp
, 'c'))
126 if (ISCMD(vp
->rkp
, 'd') || ISCMD(vp
->rkp
, 'y')) {
127 if (cs_fspace(sp
, &cs
))
132 if (cs_fblank(sp
, &cs
))
138 * Cyclically move to the next word -- this involves skipping
139 * over word characters and then any trailing non-word characters.
140 * Note, for the 'w' command, the definition of a word keeps
146 if (cs_next(sp
, &cs
))
148 if (cs
.cs_flags
== CS_EOF
)
150 if (cs
.cs_flags
!= 0 || ISBLANK2(cs
.cs_ch
))
154 * If a motion command and we're at the end of the
155 * last word, we're done. Delete and yank eat any
156 * trailing blanks, but we don't move off the end
157 * of the line regardless.
159 if (cnt
== 0 && ISMOTION(vp
)) {
160 if ((ISCMD(vp
->rkp
, 'd') ||
161 ISCMD(vp
->rkp
, 'y')) &&
167 /* Eat whitespace characters. */
168 if (cs_fblank(sp
, &cs
))
170 if (cs
.cs_flags
== CS_EOF
)
175 state
= cs
.cs_flags
== 0 &&
176 inword(cs
.cs_ch
) ? INWORD
: NOTWORD
;
178 if (cs_next(sp
, &cs
))
180 if (cs
.cs_flags
== CS_EOF
)
182 if (cs
.cs_flags
!= 0 || ISBLANK2(cs
.cs_ch
))
184 if (state
== INWORD
) {
185 if (!inword(cs
.cs_ch
))
188 if (inword(cs
.cs_ch
))
191 /* See comment above. */
192 if (cnt
== 0 && ISMOTION(vp
)) {
193 if ((ISCMD(vp
->rkp
, 'd') ||
194 ISCMD(vp
->rkp
, 'y')) &&
200 /* Eat whitespace characters. */
201 if (cs
.cs_flags
!= 0 || ISBLANK2(cs
.cs_ch
))
202 if (cs_fblank(sp
, &cs
))
204 if (cs
.cs_flags
== CS_EOF
)
209 * If we didn't move, we must be at EOF.
212 * That's okay for motion commands, however.
214 ret
: if (!ISMOTION(vp
) &&
215 cs
.cs_lno
== vp
->m_start
.lno
&& cs
.cs_cno
== vp
->m_start
.cno
) {
216 v_eof(sp
, &vp
->m_start
);
220 /* Adjust the end of the range for motion commands. */
221 vp
->m_stop
.lno
= cs
.cs_lno
;
222 vp
->m_stop
.cno
= cs
.cs_cno
;
223 if (ISMOTION(vp
) && cs
.cs_flags
== 0)
227 * Non-motion commands move to the end of the range. Delete
228 * and yank stay at the start, ignore others.
230 vp
->m_final
= ISMOTION(vp
) ? vp
->m_start
: vp
->m_stop
;
235 * v_wordE -- [count]E
236 * Move forward to the end of the bigword.
238 * PUBLIC: int v_wordE __P((SCR *, VICMD *));
241 v_wordE(SCR
*sp
, VICMD
*vp
)
243 return (eword(sp
, vp
, BIGWORD
));
247 * v_worde -- [count]e
248 * Move forward to the end of the word.
250 * PUBLIC: int v_worde __P((SCR *, VICMD *));
253 v_worde(SCR
*sp
, VICMD
*vp
)
255 return (eword(sp
, vp
, LITTLEWORD
));
260 * Move forward to the end of the word.
263 eword(SCR
*sp
, VICMD
*vp
, enum which type
)
265 enum { INWORD
, NOTWORD
} state
;
269 cnt
= F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1;
270 cs
.cs_lno
= vp
->m_start
.lno
;
271 cs
.cs_cno
= vp
->m_start
.cno
;
272 if (cs_init(sp
, &cs
))
277 * If in whitespace, or the next character is whitespace, move past
278 * it. (This doesn't count as a word move.) Stay at the character
279 * past the current one, it sets word "state" for the 'e' command.
281 if (cs
.cs_flags
== 0 && !ISBLANK2(cs
.cs_ch
)) {
282 if (cs_next(sp
, &cs
))
284 if (cs
.cs_flags
== 0 && !ISBLANK2(cs
.cs_ch
))
287 if (cs_fblank(sp
, &cs
))
291 * Cyclically move to the next word -- this involves skipping
292 * over word characters and then any trailing non-word characters.
293 * Note, for the 'e' command, the definition of a word keeps
296 start
: if (type
== BIGWORD
)
299 if (cs_next(sp
, &cs
))
301 if (cs
.cs_flags
== CS_EOF
)
303 if (cs
.cs_flags
!= 0 || ISBLANK2(cs
.cs_ch
))
307 * When we reach the start of the word after the last
308 * word, we're done. If we changed state, back up one
309 * to the end of the previous word.
312 if (cs
.cs_flags
== 0 && cs_prev(sp
, &cs
))
317 /* Eat whitespace characters. */
318 if (cs_fblank(sp
, &cs
))
320 if (cs
.cs_flags
== CS_EOF
)
325 state
= cs
.cs_flags
== 0 &&
326 inword(cs
.cs_ch
) ? INWORD
: NOTWORD
;
328 if (cs_next(sp
, &cs
))
330 if (cs
.cs_flags
== CS_EOF
)
332 if (cs
.cs_flags
!= 0 || ISBLANK2(cs
.cs_ch
))
334 if (state
== INWORD
) {
335 if (!inword(cs
.cs_ch
))
338 if (inword(cs
.cs_ch
))
341 /* See comment above. */
343 if (cs
.cs_flags
== 0 && cs_prev(sp
, &cs
))
348 /* Eat whitespace characters. */
349 if (cs
.cs_flags
!= 0 || ISBLANK2(cs
.cs_ch
))
350 if (cs_fblank(sp
, &cs
))
352 if (cs
.cs_flags
== CS_EOF
)
357 * If we didn't move, we must be at EOF.
360 * That's okay for motion commands, however.
362 ret
: if (!ISMOTION(vp
) &&
363 cs
.cs_lno
== vp
->m_start
.lno
&& cs
.cs_cno
== vp
->m_start
.cno
) {
364 v_eof(sp
, &vp
->m_start
);
368 /* Set the end of the range for motion commands. */
369 vp
->m_stop
.lno
= cs
.cs_lno
;
370 vp
->m_stop
.cno
= cs
.cs_cno
;
373 * Non-motion commands move to the end of the range.
374 * Delete and yank stay at the start, ignore others.
376 vp
->m_final
= ISMOTION(vp
) ? vp
->m_start
: vp
->m_stop
;
381 * v_WordB -- [count]B
382 * Move backward a bigword at a time.
384 * PUBLIC: int v_wordB __P((SCR *, VICMD *));
387 v_wordB(SCR
*sp
, VICMD
*vp
)
389 return (bword(sp
, vp
, BIGWORD
));
393 * v_wordb -- [count]b
394 * Move backward a word at a time.
396 * PUBLIC: int v_wordb __P((SCR *, VICMD *));
399 v_wordb(SCR
*sp
, VICMD
*vp
)
401 return (bword(sp
, vp
, LITTLEWORD
));
406 * Move backward by words.
409 bword(SCR
*sp
, VICMD
*vp
, enum which type
)
411 enum { INWORD
, NOTWORD
} state
;
415 cnt
= F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1;
416 cs
.cs_lno
= vp
->m_start
.lno
;
417 cs
.cs_cno
= vp
->m_start
.cno
;
418 if (cs_init(sp
, &cs
))
423 * If in whitespace, or the previous character is whitespace, move
424 * past it. (This doesn't count as a word move.) Stay at the
425 * character before the current one, it sets word "state" for the
428 if (cs
.cs_flags
== 0 && !ISBLANK2(cs
.cs_ch
)) {
429 if (cs_prev(sp
, &cs
))
431 if (cs
.cs_flags
== 0 && !ISBLANK2(cs
.cs_ch
))
434 if (cs_bblank(sp
, &cs
))
438 * Cyclically move to the beginning of the previous word -- this
439 * involves skipping over word characters and then any trailing
440 * non-word characters. Note, for the 'b' command, the definition
441 * of a word keeps switching.
443 start
: if (type
== BIGWORD
)
446 if (cs_prev(sp
, &cs
))
448 if (cs
.cs_flags
== CS_SOF
)
450 if (cs
.cs_flags
!= 0 || ISBLANK2(cs
.cs_ch
))
454 * When we reach the end of the word before the last
455 * word, we're done. If we changed state, move forward
456 * one to the end of the next word.
459 if (cs
.cs_flags
== 0 && cs_next(sp
, &cs
))
464 /* Eat whitespace characters. */
465 if (cs_bblank(sp
, &cs
))
467 if (cs
.cs_flags
== CS_SOF
)
472 state
= cs
.cs_flags
== 0 &&
473 inword(cs
.cs_ch
) ? INWORD
: NOTWORD
;
475 if (cs_prev(sp
, &cs
))
477 if (cs
.cs_flags
== CS_SOF
)
479 if (cs
.cs_flags
!= 0 || ISBLANK2(cs
.cs_ch
))
481 if (state
== INWORD
) {
482 if (!inword(cs
.cs_ch
))
485 if (inword(cs
.cs_ch
))
488 /* See comment above. */
490 if (cs
.cs_flags
== 0 && cs_next(sp
, &cs
))
495 /* Eat whitespace characters. */
496 if (cs
.cs_flags
!= 0 || ISBLANK2(cs
.cs_ch
))
497 if (cs_bblank(sp
, &cs
))
499 if (cs
.cs_flags
== CS_SOF
)
503 /* If we didn't move, we must be at SOF. */
504 ret
: if (cs
.cs_lno
== vp
->m_start
.lno
&& cs
.cs_cno
== vp
->m_start
.cno
) {
505 v_sof(sp
, &vp
->m_start
);
509 /* Set the end of the range for motion commands. */
510 vp
->m_stop
.lno
= cs
.cs_lno
;
511 vp
->m_stop
.cno
= cs
.cs_cno
;
514 * All commands move to the end of the range. Motion commands
515 * adjust the starting point to the character before the current
519 * The historic vi didn't get this right -- the `yb' command yanked
520 * the right stuff and even updated the cursor value, but the cursor
521 * was not actually updated on the screen.
523 vp
->m_final
= vp
->m_stop
;