* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
static char sccsid
[] = "@(#)v_word.c 8.10 (Berkeley) 10/26/93";
* There are two types of "words". Bigwords are easy -- groups of anything
* delimited by whitespace. Normal words are trickier. They are either a
* group of characters, numbers and underscores, or a group of anything but,
* delimited by whitespace. When for a word, if you're in whitespace, it's
* easy, just remove the whitespace and go to the beginning or end of the
* word. Otherwise, figure out if the next character is in a different group.
* If it is, go to the beginning or end of that group, otherwise, go to the
* beginning or end of the current group. The historic version of vi didn't
* get this right, so, for example, there were cases where "4e" was not the
* same as "eeee". To get it right you have to resolve the cursor after each
* search so that the look-ahead to figure out what type of "word" the cursor
* Empty lines, and lines that consist of only white-space characters count
* as a single word, and the beginning and end of the file counts as an
* infinite number of words.
* Movements associated with commands are different than movement commands.
* For example, in "abc def", with the cursor on the 'a', "cw" is from
* 'a' to 'c', while "w" is from 'a' to 'd'. In general, trailing white
* space is discarded from the change movement. Another example is that,
* in the same string, a "cw" on any white space character replaces that
* single character, and nothing else. Ain't nothin' in here that's easy.
* One historic note -- in the original vi, the 'w', 'W' and 'B' commands
* would treat groups of empty lines as individual words, i.e. the command
* would move the cursor to each new empty line. The 'e' and 'E' commands
* would treat groups of empty lines as a single word, i.e. the first use
* would move past the group of lines. The 'b' command would just beep at
* you. If the lines contained only white-space characters, the 'w' and 'W'
* commands will just beep at you, and the 'B', 'b', 'E' and 'e' commands
* will treat the group as a single word, and the 'B' and 'b' commands will
* treat the lines as individual words. This implementation treats both
* cases as a single white-space word.
#define FW(test) for (; len && (test); --len, ++p)
#define BW(test) for (; len && (test); --len, --p)
enum which
{BIGWORD
, LITTLEWORD
};
static int bword
__P((SCR
*, EXF
*, VICMDARG
*, MARK
*, MARK
*, int));
static int eword
__P((SCR
*, EXF
*, VICMDARG
*, MARK
*, MARK
*, int));
static int fword
__P((SCR
*, EXF
*, VICMDARG
*, MARK
*, MARK
*, enum which
));
* Move forward a word at a time.
v_wordw(sp
, ep
, vp
, fm
, tm
, rp
)
return (fword(sp
, ep
, vp
, fm
, rp
, LITTLEWORD
));
* Move forward a bigword at a time.
v_wordW(sp
, ep
, vp
, fm
, tm
, rp
)
return (fword(sp
, ep
, vp
, fm
, rp
, BIGWORD
));
fword(sp
, ep
, vp
, fm
, rp
, type
)
enum { INWORD
, NOTWORD
} state
;
if (cs_init(sp
, ep
, &cs
))
cnt
= F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1;
* If the count is 1, and it's a change command, we're done.
* Else, move to the first non-white-space character, which
* counts as a single word move. If it's a motion command,
* don't move off the end of the line.
if (cs
.cs_flags
== CS_EMP
|| cs
.cs_flags
== 0 && isblank(cs
.cs_ch
)) {
if (cs
.cs_flags
!= CS_EMP
&& cnt
== 1) {
if (F_ISSET(vp
, VC_D
| VC_Y
)) {
if (cs_fspace(sp
, ep
, &cs
))
if (cs_fblank(sp
, ep
, &cs
))
* Cyclically move to the next word -- this involves skipping
* over word characters and then any trailing non-word characters.
* Note, for the 'w' command, the definition of a word keeps
if (cs_next(sp
, ep
, &cs
))
if (cs
.cs_flags
== CS_EOF
)
if (cs
.cs_flags
!= 0 || isblank(cs
.cs_ch
))
* If a motion command and we're at the end of the
* last word, we're done. Delete and yank eat any
* trailing blanks, but we don't move off the end
* of the line regardless.
if (cnt
== 0 && F_ISSET(vp
, VC_C
| VC_D
| VC_Y
)) {
if (F_ISSET(vp
, VC_D
| VC_Y
) &&
/* Eat whitespace characters. */
if (cs_fblank(sp
, ep
, &cs
))
if (cs
.cs_flags
== CS_EOF
)
state
= cs
.cs_flags
== 0 &&
inword(cs
.cs_ch
) ? INWORD
: NOTWORD
;
if (cs_next(sp
, ep
, &cs
))
if (cs
.cs_flags
== CS_EOF
)
if (cs
.cs_flags
!= 0 || isblank(cs
.cs_ch
))
if (cnt
== 0 && F_ISSET(vp
, VC_C
| VC_D
| VC_Y
)) {
if (F_ISSET(vp
, VC_D
| VC_Y
) &&
/* Eat whitespace characters. */
if (cs
.cs_flags
!= 0 || isblank(cs
.cs_ch
))
if (cs_fblank(sp
, ep
, &cs
))
if (cs
.cs_flags
== CS_EOF
)
* If a motion command, and eating the trailing non-word would
* move us off this line, don't do it. Move the return cursor
* to one past the EOL instead.
ret1
: if (F_ISSET(vp
, VC_C
| VC_D
| VC_Y
) && cs
.cs_flags
== CS_EOL
)
/* If we didn't move, we must be at EOF. */
ret2
: if (cs
.cs_lno
== fm
->lno
&& cs
.cs_cno
== fm
->cno
) {
* If at EOF, and it's a motion command, move the return cursor
if (F_ISSET(vp
, VC_C
| VC_D
| VC_Y
) && cs
.cs_flags
== CS_EOF
)
ret3
: rp
->lno
= cs
.cs_lno
;
* Move backward a word at a time.
v_wordb(sp
, ep
, vp
, fm
, tm
, rp
)
return (bword(sp
, ep
, vp
, fm
, rp
, 0));
* Move backward a bigword at a time.
v_wordB(sp
, ep
, vp
, fm
, tm
, rp
)
return (bword(sp
, ep
, vp
, fm
, rp
, 1));
* Move backward by words.
bword(sp
, ep
, vp
, fm
, rp
, spaceonly
)
/* Check for start of file. */
if (lno
== 1 && cno
== 0) {
if ((p
= file_gline(sp
, ep
, lno
, &len
)) == NULL
) {
if (file_lline(sp
, ep
, &lno
))
cnt
= F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1;
* Reset the length to the number of characters in the line; the
* first character is the current cursor position.
for (startp
= p
, p
+= cno
; cnt
--;) {
BW(!isblank(*p
) && !inword(*p
));
/* If we hit SOF, stay there (historic practice). */
* Get the line. If the line is empty, decrement
* count and get another one.
if ((p
= file_gline(sp
, ep
, --lno
, &len
)) == NULL
) {
if (cnt
== 0 || --cnt
== 0) {
* Set the cursor to the end of the line. If the word
* at the end of this line has only a single character,
* we've already skipped over it.
if (cnt
&& len
> 1 && !isblank(p
[0]))
} else if (!isblank(p
[-1]) &&
* Move forward to the end of the word.
v_worde(sp
, ep
, vp
, fm
, tm
, rp
)
return (eword(sp
, ep
, vp
, fm
, rp
, 0));
* Move forward to the end of the bigword.
v_wordE(sp
, ep
, vp
, fm
, tm
, rp
)
return (eword(sp
, ep
, vp
, fm
, rp
, 1));
* Move forward to the end of the word.
eword(sp
, ep
, vp
, fm
, rp
, spaceonly
)
if ((p
= file_gline(sp
, ep
, lno
, &llen
)) == NULL
) {
if (file_lline(sp
, ep
, &lno
))
cnt
= F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1;
* Reset the length; the first character is the current cursor
* position. If no more characters in this line, may already
if (empty
= llen
== 0 || llen
== cno
+ 1)
for (startp
= p
+= cno
; cnt
--; empty
= 0) {
FW(!isblank(*p
) && !inword(*p
));
/* If we hit EOF, stay there (historic practice). */
line
: if ((p
= file_gline(sp
, ep
, ++lno
, &llen
)) == NULL
) {
* If already at eof, complain, unless it's
* a change command or a delete command and
* there's something to delete.
F_ISSET(vp
, VC_D
) && llen
!= 0) {
rp
->cno
= llen
? llen
: 1;
file_gline(sp
, ep
, --lno
, &llen
)) == NULL
) {
rp
->cno
= llen
? llen
- 1 : 0;
/* The 'c', 'd' and 'y' need one more space. */
if (F_ISSET(vp
, VC_C
| VC_D
| VC_Y
))
rp
->cno
= cno
+ (p
- startp
);
/* The 'c', 'd' and 'y' need one more space. */
if (F_ISSET(vp
, VC_C
| VC_D
| VC_Y
))