1 /* $NetBSD: v_word.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_word.c,v 10.6 2001/06/25 15:19:36 skimo Exp (Berkeley) Date: 2001/06/25 15:19:36 ";
19 __RCSID("$NetBSD: v_word.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"
35 * There are two types of "words". Bigwords are easy -- groups of anything
36 * delimited by whitespace. Normal words are trickier. They are either a
37 * group of characters, numbers and underscores, or a group of anything but,
38 * delimited by whitespace. When for a word, if you're in whitespace, it's
39 * easy, just remove the whitespace and go to the beginning or end of the
40 * word. Otherwise, figure out if the next character is in a different group.
41 * If it is, go to the beginning or end of that group, otherwise, go to the
42 * beginning or end of the current group. The historic version of vi didn't
43 * get this right, so, for example, there were cases where "4e" was not the
44 * same as "eeee" -- in particular, single character words, and commands that
45 * began in whitespace were almost always handled incorrectly. To get it right
46 * you have to resolve the cursor after each search so that the look-ahead to
47 * figure out what type of "word" the cursor is in will be correct.
49 * Empty lines, and lines that consist of only white-space characters count
50 * as a single word, and the beginning and end of the file counts as an
51 * infinite number of words.
53 * Movements associated with commands are different than movement commands.
54 * For example, in "abc def", with the cursor on the 'a', "cw" is from
55 * 'a' to 'c', while "w" is from 'a' to 'd'. In general, trailing white
56 * space is discarded from the change movement. Another example is that,
57 * in the same string, a "cw" on any white space character replaces that
58 * single character, and nothing else. Ain't nothin' in here that's easy.
60 * One historic note -- in the original vi, the 'w', 'W' and 'B' commands
61 * would treat groups of empty lines as individual words, i.e. the command
62 * would move the cursor to each new empty line. The 'e' and 'E' commands
63 * would treat groups of empty lines as a single word, i.e. the first use
64 * would move past the group of lines. The 'b' command would just beep at
65 * you, or, if you did it from the start of the line as part of a motion
66 * command, go absolutely nuts. If the lines contained only white-space
67 * characters, the 'w' and 'W' commands would just beep at you, and the 'B',
68 * 'b', 'E' and 'e' commands would treat the group as a single word, and
69 * the 'B' and 'b' commands will treat the lines as individual words. This
70 * implementation treats all of these cases as a single white-space word.
73 enum which
{BIGWORD
, LITTLEWORD
};
75 static int bword
__P((SCR
*, VICMD
*, enum which
));
76 static int eword
__P((SCR
*, VICMD
*, enum which
));
77 static int fword
__P((SCR
*, VICMD
*, enum which
));
81 * Move forward a bigword at a time.
83 * PUBLIC: int v_wordW __P((SCR *, VICMD *));
86 v_wordW(SCR
*sp
, VICMD
*vp
)
88 return (fword(sp
, vp
, BIGWORD
));
93 * Move forward a word at a time.
95 * PUBLIC: int v_wordw __P((SCR *, VICMD *));
98 v_wordw(SCR
*sp
, VICMD
*vp
)
100 return (fword(sp
, vp
, LITTLEWORD
));
105 * Move forward by words.
108 fword(SCR
*sp
, VICMD
*vp
, enum which type
)
110 enum { INWORD
, NOTWORD
} state
;
114 cnt
= F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1;
115 cs
.cs_lno
= vp
->m_start
.lno
;
116 cs
.cs_cno
= vp
->m_start
.cno
;
117 if (cs_init(sp
, &cs
))
122 * If the count is 1, and it's a change command, we're done.
123 * Else, move to the first non-white-space character, which
124 * counts as a single word move. If it's a motion command,
125 * don't move off the end of the line.
127 if (cs
.cs_flags
== CS_EMP
|| (cs
.cs_flags
== 0 && ISBLANK2(cs
.cs_ch
))) {
128 if (ISMOTION(vp
) && cs
.cs_flags
!= CS_EMP
&& cnt
== 1) {
129 if (ISCMD(vp
->rkp
, 'c'))
131 if (ISCMD(vp
->rkp
, 'd') || ISCMD(vp
->rkp
, 'y')) {
132 if (cs_fspace(sp
, &cs
))
137 if (cs_fblank(sp
, &cs
))
143 * Cyclically move to the next word -- this involves skipping
144 * over word characters and then any trailing non-word characters.
145 * Note, for the 'w' command, the definition of a word keeps
151 if (cs_next(sp
, &cs
))
153 if (cs
.cs_flags
== CS_EOF
)
155 if (cs
.cs_flags
!= 0 || ISBLANK2(cs
.cs_ch
))
159 * If a motion command and we're at the end of the
160 * last word, we're done. Delete and yank eat any
161 * trailing blanks, but we don't move off the end
162 * of the line regardless.
164 if (cnt
== 0 && ISMOTION(vp
)) {
165 if ((ISCMD(vp
->rkp
, 'd') ||
166 ISCMD(vp
->rkp
, 'y')) &&
172 /* Eat whitespace characters. */
173 if (cs_fblank(sp
, &cs
))
175 if (cs
.cs_flags
== CS_EOF
)
180 state
= cs
.cs_flags
== 0 &&
181 inword(cs
.cs_ch
) ? INWORD
: NOTWORD
;
183 if (cs_next(sp
, &cs
))
185 if (cs
.cs_flags
== CS_EOF
)
187 if (cs
.cs_flags
!= 0 || ISBLANK2(cs
.cs_ch
))
189 if (state
== INWORD
) {
190 if (!inword(cs
.cs_ch
))
193 if (inword(cs
.cs_ch
))
196 /* See comment above. */
197 if (cnt
== 0 && ISMOTION(vp
)) {
198 if ((ISCMD(vp
->rkp
, 'd') ||
199 ISCMD(vp
->rkp
, 'y')) &&
205 /* Eat whitespace characters. */
206 if (cs
.cs_flags
!= 0 || ISBLANK2(cs
.cs_ch
))
207 if (cs_fblank(sp
, &cs
))
209 if (cs
.cs_flags
== CS_EOF
)
214 * If we didn't move, we must be at EOF.
217 * That's okay for motion commands, however.
219 ret
: if (!ISMOTION(vp
) &&
220 cs
.cs_lno
== vp
->m_start
.lno
&& cs
.cs_cno
== vp
->m_start
.cno
) {
221 v_eof(sp
, &vp
->m_start
);
225 /* Adjust the end of the range for motion commands. */
226 vp
->m_stop
.lno
= cs
.cs_lno
;
227 vp
->m_stop
.cno
= cs
.cs_cno
;
228 if (ISMOTION(vp
) && cs
.cs_flags
== 0)
232 * Non-motion commands move to the end of the range. Delete
233 * and yank stay at the start, ignore others.
235 vp
->m_final
= ISMOTION(vp
) ? vp
->m_start
: vp
->m_stop
;
240 * v_wordE -- [count]E
241 * Move forward to the end of the bigword.
243 * PUBLIC: int v_wordE __P((SCR *, VICMD *));
246 v_wordE(SCR
*sp
, VICMD
*vp
)
248 return (eword(sp
, vp
, BIGWORD
));
252 * v_worde -- [count]e
253 * Move forward to the end of the word.
255 * PUBLIC: int v_worde __P((SCR *, VICMD *));
258 v_worde(SCR
*sp
, VICMD
*vp
)
260 return (eword(sp
, vp
, LITTLEWORD
));
265 * Move forward to the end of the word.
268 eword(SCR
*sp
, VICMD
*vp
, enum which type
)
270 enum { INWORD
, NOTWORD
} state
;
274 cnt
= F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1;
275 cs
.cs_lno
= vp
->m_start
.lno
;
276 cs
.cs_cno
= vp
->m_start
.cno
;
277 if (cs_init(sp
, &cs
))
282 * If in whitespace, or the next character is whitespace, move past
283 * it. (This doesn't count as a word move.) Stay at the character
284 * past the current one, it sets word "state" for the 'e' command.
286 if (cs
.cs_flags
== 0 && !ISBLANK2(cs
.cs_ch
)) {
287 if (cs_next(sp
, &cs
))
289 if (cs
.cs_flags
== 0 && !ISBLANK2(cs
.cs_ch
))
292 if (cs_fblank(sp
, &cs
))
296 * Cyclically move to the next word -- this involves skipping
297 * over word characters and then any trailing non-word characters.
298 * Note, for the 'e' command, the definition of a word keeps
301 start
: if (type
== BIGWORD
)
304 if (cs_next(sp
, &cs
))
306 if (cs
.cs_flags
== CS_EOF
)
308 if (cs
.cs_flags
!= 0 || ISBLANK2(cs
.cs_ch
))
312 * When we reach the start of the word after the last
313 * word, we're done. If we changed state, back up one
314 * to the end of the previous word.
317 if (cs
.cs_flags
== 0 && cs_prev(sp
, &cs
))
322 /* Eat whitespace characters. */
323 if (cs_fblank(sp
, &cs
))
325 if (cs
.cs_flags
== CS_EOF
)
330 state
= cs
.cs_flags
== 0 &&
331 inword(cs
.cs_ch
) ? INWORD
: NOTWORD
;
333 if (cs_next(sp
, &cs
))
335 if (cs
.cs_flags
== CS_EOF
)
337 if (cs
.cs_flags
!= 0 || ISBLANK2(cs
.cs_ch
))
339 if (state
== INWORD
) {
340 if (!inword(cs
.cs_ch
))
343 if (inword(cs
.cs_ch
))
346 /* See comment above. */
348 if (cs
.cs_flags
== 0 && cs_prev(sp
, &cs
))
353 /* Eat whitespace characters. */
354 if (cs
.cs_flags
!= 0 || ISBLANK2(cs
.cs_ch
))
355 if (cs_fblank(sp
, &cs
))
357 if (cs
.cs_flags
== CS_EOF
)
362 * If we didn't move, we must be at EOF.
365 * That's okay for motion commands, however.
367 ret
: if (!ISMOTION(vp
) &&
368 cs
.cs_lno
== vp
->m_start
.lno
&& cs
.cs_cno
== vp
->m_start
.cno
) {
369 v_eof(sp
, &vp
->m_start
);
373 /* Set the end of the range for motion commands. */
374 vp
->m_stop
.lno
= cs
.cs_lno
;
375 vp
->m_stop
.cno
= cs
.cs_cno
;
378 * Non-motion commands move to the end of the range.
379 * Delete and yank stay at the start, ignore others.
381 vp
->m_final
= ISMOTION(vp
) ? vp
->m_start
: vp
->m_stop
;
386 * v_WordB -- [count]B
387 * Move backward a bigword at a time.
389 * PUBLIC: int v_wordB __P((SCR *, VICMD *));
392 v_wordB(SCR
*sp
, VICMD
*vp
)
394 return (bword(sp
, vp
, BIGWORD
));
398 * v_wordb -- [count]b
399 * Move backward a word at a time.
401 * PUBLIC: int v_wordb __P((SCR *, VICMD *));
404 v_wordb(SCR
*sp
, VICMD
*vp
)
406 return (bword(sp
, vp
, LITTLEWORD
));
411 * Move backward by words.
414 bword(SCR
*sp
, VICMD
*vp
, enum which type
)
416 enum { INWORD
, NOTWORD
} state
;
420 cnt
= F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1;
421 cs
.cs_lno
= vp
->m_start
.lno
;
422 cs
.cs_cno
= vp
->m_start
.cno
;
423 if (cs_init(sp
, &cs
))
428 * If in whitespace, or the previous character is whitespace, move
429 * past it. (This doesn't count as a word move.) Stay at the
430 * character before the current one, it sets word "state" for the
433 if (cs
.cs_flags
== 0 && !ISBLANK2(cs
.cs_ch
)) {
434 if (cs_prev(sp
, &cs
))
436 if (cs
.cs_flags
== 0 && !ISBLANK2(cs
.cs_ch
))
439 if (cs_bblank(sp
, &cs
))
443 * Cyclically move to the beginning of the previous word -- this
444 * involves skipping over word characters and then any trailing
445 * non-word characters. Note, for the 'b' command, the definition
446 * of a word keeps switching.
448 start
: if (type
== BIGWORD
)
451 if (cs_prev(sp
, &cs
))
453 if (cs
.cs_flags
== CS_SOF
)
455 if (cs
.cs_flags
!= 0 || ISBLANK2(cs
.cs_ch
))
459 * When we reach the end of the word before the last
460 * word, we're done. If we changed state, move forward
461 * one to the end of the next word.
464 if (cs
.cs_flags
== 0 && cs_next(sp
, &cs
))
469 /* Eat whitespace characters. */
470 if (cs_bblank(sp
, &cs
))
472 if (cs
.cs_flags
== CS_SOF
)
477 state
= cs
.cs_flags
== 0 &&
478 inword(cs
.cs_ch
) ? INWORD
: NOTWORD
;
480 if (cs_prev(sp
, &cs
))
482 if (cs
.cs_flags
== CS_SOF
)
484 if (cs
.cs_flags
!= 0 || ISBLANK2(cs
.cs_ch
))
486 if (state
== INWORD
) {
487 if (!inword(cs
.cs_ch
))
490 if (inword(cs
.cs_ch
))
493 /* See comment above. */
495 if (cs
.cs_flags
== 0 && cs_next(sp
, &cs
))
500 /* Eat whitespace characters. */
501 if (cs
.cs_flags
!= 0 || ISBLANK2(cs
.cs_ch
))
502 if (cs_bblank(sp
, &cs
))
504 if (cs
.cs_flags
== CS_SOF
)
508 /* If we didn't move, we must be at SOF. */
509 ret
: if (cs
.cs_lno
== vp
->m_start
.lno
&& cs
.cs_cno
== vp
->m_start
.cno
) {
510 v_sof(sp
, &vp
->m_start
);
514 /* Set the end of the range for motion commands. */
515 vp
->m_stop
.lno
= cs
.cs_lno
;
516 vp
->m_stop
.cno
= cs
.cs_cno
;
519 * All commands move to the end of the range. Motion commands
520 * adjust the starting point to the character before the current
524 * The historic vi didn't get this right -- the `yb' command yanked
525 * the right stuff and even updated the cursor value, but the cursor
526 * was not actually updated on the screen.
528 vp
->m_final
= vp
->m_stop
;