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_word.c,v 10.6 2001/06/25 15:19:36 skimo Exp $ (Berkeley) $Date: 2001/06/25 15:19:36 $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
20 #include <bitstring.h>
25 #include "../common/common.h"
29 * There are two types of "words". Bigwords are easy -- groups of anything
30 * delimited by whitespace. Normal words are trickier. They are either a
31 * group of characters, numbers and underscores, or a group of anything but,
32 * delimited by whitespace. When for a word, if you're in whitespace, it's
33 * easy, just remove the whitespace and go to the beginning or end of the
34 * word. Otherwise, figure out if the next character is in a different group.
35 * If it is, go to the beginning or end of that group, otherwise, go to the
36 * beginning or end of the current group. The historic version of vi didn't
37 * get this right, so, for example, there were cases where "4e" was not the
38 * same as "eeee" -- in particular, single character words, and commands that
39 * began in whitespace were almost always handled incorrectly. To get it right
40 * you have to resolve the cursor after each search so that the look-ahead to
41 * figure out what type of "word" the cursor is in will be correct.
43 * Empty lines, and lines that consist of only white-space characters count
44 * as a single word, and the beginning and end of the file counts as an
45 * infinite number of words.
47 * Movements associated with commands are different than movement commands.
48 * For example, in "abc def", with the cursor on the 'a', "cw" is from
49 * 'a' to 'c', while "w" is from 'a' to 'd'. In general, trailing white
50 * space is discarded from the change movement. Another example is that,
51 * in the same string, a "cw" on any white space character replaces that
52 * single character, and nothing else. Ain't nothin' in here that's easy.
54 * One historic note -- in the original vi, the 'w', 'W' and 'B' commands
55 * would treat groups of empty lines as individual words, i.e. the command
56 * would move the cursor to each new empty line. The 'e' and 'E' commands
57 * would treat groups of empty lines as a single word, i.e. the first use
58 * would move past the group of lines. The 'b' command would just beep at
59 * you, or, if you did it from the start of the line as part of a motion
60 * command, go absolutely nuts. If the lines contained only white-space
61 * characters, the 'w' and 'W' commands would just beep at you, and the 'B',
62 * 'b', 'E' and 'e' commands would treat the group as a single word, and
63 * the 'B' and 'b' commands will treat the lines as individual words. This
64 * implementation treats all of these cases as a single white-space word.
67 enum which
{BIGWORD
, LITTLEWORD
};
69 static int bword
__P((SCR
*, VICMD
*, enum which
));
70 static int eword
__P((SCR
*, VICMD
*, enum which
));
71 static int fword
__P((SCR
*, VICMD
*, enum which
));
75 * Move forward a bigword at a time.
77 * PUBLIC: int v_wordW __P((SCR *, VICMD *));
80 v_wordW(SCR
*sp
, VICMD
*vp
)
82 return (fword(sp
, vp
, BIGWORD
));
87 * Move forward a word at a time.
89 * PUBLIC: int v_wordw __P((SCR *, VICMD *));
92 v_wordw(SCR
*sp
, VICMD
*vp
)
94 return (fword(sp
, vp
, LITTLEWORD
));
99 * Move forward by words.
102 fword(SCR
*sp
, VICMD
*vp
, enum which type
)
104 enum { INWORD
, NOTWORD
} state
;
108 cnt
= F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1;
109 cs
.cs_lno
= vp
->m_start
.lno
;
110 cs
.cs_cno
= vp
->m_start
.cno
;
111 if (cs_init(sp
, &cs
))
116 * If the count is 1, and it's a change command, we're done.
117 * Else, move to the first non-white-space character, which
118 * counts as a single word move. If it's a motion command,
119 * don't move off the end of the line.
121 if (cs
.cs_flags
== CS_EMP
|| cs
.cs_flags
== 0 && isblank(cs
.cs_ch
)) {
122 if (ISMOTION(vp
) && cs
.cs_flags
!= CS_EMP
&& cnt
== 1) {
123 if (ISCMD(vp
->rkp
, 'c'))
125 if (ISCMD(vp
->rkp
, 'd') || ISCMD(vp
->rkp
, 'y')) {
126 if (cs_fspace(sp
, &cs
))
131 if (cs_fblank(sp
, &cs
))
137 * Cyclically move to the next word -- this involves skipping
138 * over word characters and then any trailing non-word characters.
139 * Note, for the 'w' command, the definition of a word keeps
145 if (cs_next(sp
, &cs
))
147 if (cs
.cs_flags
== CS_EOF
)
149 if (cs
.cs_flags
!= 0 || isblank(cs
.cs_ch
))
153 * If a motion command and we're at the end of the
154 * last word, we're done. Delete and yank eat any
155 * trailing blanks, but we don't move off the end
156 * of the line regardless.
158 if (cnt
== 0 && ISMOTION(vp
)) {
159 if ((ISCMD(vp
->rkp
, 'd') ||
160 ISCMD(vp
->rkp
, 'y')) &&
166 /* Eat whitespace characters. */
167 if (cs_fblank(sp
, &cs
))
169 if (cs
.cs_flags
== CS_EOF
)
174 state
= cs
.cs_flags
== 0 &&
175 inword(cs
.cs_ch
) ? INWORD
: NOTWORD
;
177 if (cs_next(sp
, &cs
))
179 if (cs
.cs_flags
== CS_EOF
)
181 if (cs
.cs_flags
!= 0 || isblank(cs
.cs_ch
))
183 if (state
== INWORD
) {
184 if (!inword(cs
.cs_ch
))
187 if (inword(cs
.cs_ch
))
190 /* See comment above. */
191 if (cnt
== 0 && ISMOTION(vp
)) {
192 if ((ISCMD(vp
->rkp
, 'd') ||
193 ISCMD(vp
->rkp
, 'y')) &&
199 /* Eat whitespace characters. */
200 if (cs
.cs_flags
!= 0 || isblank(cs
.cs_ch
))
201 if (cs_fblank(sp
, &cs
))
203 if (cs
.cs_flags
== CS_EOF
)
208 * If we didn't move, we must be at EOF.
211 * That's okay for motion commands, however.
213 ret
: if (!ISMOTION(vp
) &&
214 cs
.cs_lno
== vp
->m_start
.lno
&& cs
.cs_cno
== vp
->m_start
.cno
) {
215 v_eof(sp
, &vp
->m_start
);
219 /* Adjust the end of the range for motion commands. */
220 vp
->m_stop
.lno
= cs
.cs_lno
;
221 vp
->m_stop
.cno
= cs
.cs_cno
;
222 if (ISMOTION(vp
) && cs
.cs_flags
== 0)
226 * Non-motion commands move to the end of the range. Delete
227 * and yank stay at the start, ignore others.
229 vp
->m_final
= ISMOTION(vp
) ? vp
->m_start
: vp
->m_stop
;
234 * v_wordE -- [count]E
235 * Move forward to the end of the bigword.
237 * PUBLIC: int v_wordE __P((SCR *, VICMD *));
240 v_wordE(SCR
*sp
, VICMD
*vp
)
242 return (eword(sp
, vp
, BIGWORD
));
246 * v_worde -- [count]e
247 * Move forward to the end of the word.
249 * PUBLIC: int v_worde __P((SCR *, VICMD *));
252 v_worde(SCR
*sp
, VICMD
*vp
)
254 return (eword(sp
, vp
, LITTLEWORD
));
259 * Move forward to the end of the word.
262 eword(SCR
*sp
, VICMD
*vp
, enum which type
)
264 enum { INWORD
, NOTWORD
} state
;
268 cnt
= F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1;
269 cs
.cs_lno
= vp
->m_start
.lno
;
270 cs
.cs_cno
= vp
->m_start
.cno
;
271 if (cs_init(sp
, &cs
))
276 * If in whitespace, or the next character is whitespace, move past
277 * it. (This doesn't count as a word move.) Stay at the character
278 * past the current one, it sets word "state" for the 'e' command.
280 if (cs
.cs_flags
== 0 && !isblank(cs
.cs_ch
)) {
281 if (cs_next(sp
, &cs
))
283 if (cs
.cs_flags
== 0 && !isblank(cs
.cs_ch
))
286 if (cs_fblank(sp
, &cs
))
290 * Cyclically move to the next word -- this involves skipping
291 * over word characters and then any trailing non-word characters.
292 * Note, for the 'e' command, the definition of a word keeps
295 start
: if (type
== BIGWORD
)
298 if (cs_next(sp
, &cs
))
300 if (cs
.cs_flags
== CS_EOF
)
302 if (cs
.cs_flags
!= 0 || isblank(cs
.cs_ch
))
306 * When we reach the start of the word after the last
307 * word, we're done. If we changed state, back up one
308 * to the end of the previous word.
311 if (cs
.cs_flags
== 0 && cs_prev(sp
, &cs
))
316 /* Eat whitespace characters. */
317 if (cs_fblank(sp
, &cs
))
319 if (cs
.cs_flags
== CS_EOF
)
324 state
= cs
.cs_flags
== 0 &&
325 inword(cs
.cs_ch
) ? INWORD
: NOTWORD
;
327 if (cs_next(sp
, &cs
))
329 if (cs
.cs_flags
== CS_EOF
)
331 if (cs
.cs_flags
!= 0 || isblank(cs
.cs_ch
))
333 if (state
== INWORD
) {
334 if (!inword(cs
.cs_ch
))
337 if (inword(cs
.cs_ch
))
340 /* See comment above. */
342 if (cs
.cs_flags
== 0 && cs_prev(sp
, &cs
))
347 /* Eat whitespace characters. */
348 if (cs
.cs_flags
!= 0 || isblank(cs
.cs_ch
))
349 if (cs_fblank(sp
, &cs
))
351 if (cs
.cs_flags
== CS_EOF
)
356 * If we didn't move, we must be at EOF.
359 * That's okay for motion commands, however.
361 ret
: if (!ISMOTION(vp
) &&
362 cs
.cs_lno
== vp
->m_start
.lno
&& cs
.cs_cno
== vp
->m_start
.cno
) {
363 v_eof(sp
, &vp
->m_start
);
367 /* Set the end of the range for motion commands. */
368 vp
->m_stop
.lno
= cs
.cs_lno
;
369 vp
->m_stop
.cno
= cs
.cs_cno
;
372 * Non-motion commands move to the end of the range.
373 * Delete and yank stay at the start, ignore others.
375 vp
->m_final
= ISMOTION(vp
) ? vp
->m_start
: vp
->m_stop
;
380 * v_WordB -- [count]B
381 * Move backward a bigword at a time.
383 * PUBLIC: int v_wordB __P((SCR *, VICMD *));
386 v_wordB(SCR
*sp
, VICMD
*vp
)
388 return (bword(sp
, vp
, BIGWORD
));
392 * v_wordb -- [count]b
393 * Move backward a word at a time.
395 * PUBLIC: int v_wordb __P((SCR *, VICMD *));
398 v_wordb(SCR
*sp
, VICMD
*vp
)
400 return (bword(sp
, vp
, LITTLEWORD
));
405 * Move backward by words.
408 bword(SCR
*sp
, VICMD
*vp
, enum which type
)
410 enum { INWORD
, NOTWORD
} state
;
414 cnt
= F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1;
415 cs
.cs_lno
= vp
->m_start
.lno
;
416 cs
.cs_cno
= vp
->m_start
.cno
;
417 if (cs_init(sp
, &cs
))
422 * If in whitespace, or the previous character is whitespace, move
423 * past it. (This doesn't count as a word move.) Stay at the
424 * character before the current one, it sets word "state" for the
427 if (cs
.cs_flags
== 0 && !isblank(cs
.cs_ch
)) {
428 if (cs_prev(sp
, &cs
))
430 if (cs
.cs_flags
== 0 && !isblank(cs
.cs_ch
))
433 if (cs_bblank(sp
, &cs
))
437 * Cyclically move to the beginning of the previous word -- this
438 * involves skipping over word characters and then any trailing
439 * non-word characters. Note, for the 'b' command, the definition
440 * of a word keeps switching.
442 start
: if (type
== BIGWORD
)
445 if (cs_prev(sp
, &cs
))
447 if (cs
.cs_flags
== CS_SOF
)
449 if (cs
.cs_flags
!= 0 || isblank(cs
.cs_ch
))
453 * When we reach the end of the word before the last
454 * word, we're done. If we changed state, move forward
455 * one to the end of the next word.
458 if (cs
.cs_flags
== 0 && cs_next(sp
, &cs
))
463 /* Eat whitespace characters. */
464 if (cs_bblank(sp
, &cs
))
466 if (cs
.cs_flags
== CS_SOF
)
471 state
= cs
.cs_flags
== 0 &&
472 inword(cs
.cs_ch
) ? INWORD
: NOTWORD
;
474 if (cs_prev(sp
, &cs
))
476 if (cs
.cs_flags
== CS_SOF
)
478 if (cs
.cs_flags
!= 0 || isblank(cs
.cs_ch
))
480 if (state
== INWORD
) {
481 if (!inword(cs
.cs_ch
))
484 if (inword(cs
.cs_ch
))
487 /* See comment above. */
489 if (cs
.cs_flags
== 0 && cs_next(sp
, &cs
))
494 /* Eat whitespace characters. */
495 if (cs
.cs_flags
!= 0 || isblank(cs
.cs_ch
))
496 if (cs_bblank(sp
, &cs
))
498 if (cs
.cs_flags
== CS_SOF
)
502 /* If we didn't move, we must be at SOF. */
503 ret
: if (cs
.cs_lno
== vp
->m_start
.lno
&& cs
.cs_cno
== vp
->m_start
.cno
) {
504 v_sof(sp
, &vp
->m_start
);
508 /* Set the end of the range for motion commands. */
509 vp
->m_stop
.lno
= cs
.cs_lno
;
510 vp
->m_stop
.cno
= cs
.cs_cno
;
513 * All commands move to the end of the range. Motion commands
514 * adjust the starting point to the character before the current
518 * The historic vi didn't get this right -- the `yb' command yanked
519 * the right stuff and even updated the cursor value, but the cursor
520 * was not actually updated on the screen.
522 vp
->m_final
= vp
->m_stop
;