vi: more fixes to range selection by word

An example in my vi book presents different ways to fix the spelling
of the last word in a line:

   ... anyweigh.

With the cursor on the 'e' the command 'cway' should do the job.
Since commit 776b56d77, though, 'cw' incorrectly includes the full
stop in the range if we're on the last line of the file.

(Prior to commit 776b56d77 BusyBox vi got 'cw' right in this case but
'cW' wrong:  it *didn't* delete the full stop.)

Reinstate some of the bloat removed by the earlier commit to fix this.

Also, commit 7b4c2276a (vi: fix word operations across line boundaries)
incorrectly ignores whitespace after a single character word.  Adjust
the condition to avoid this.

function                                             old     new   delta
find_range                                           707     737     +30
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/0 up/down: 30/0)               Total: 30 bytes

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Ron Yorston 2021-04-06 13:41:01 +01:00 committed by Denys Vlasenko
parent b7b1119d9f
commit e577afca7c

View File

@ -3117,8 +3117,10 @@ static int find_range(char **start, char **stop, int cmd)
} else if (strchr("wW", c)) {
buftype = MULTI;
do_cmd(c); // execute movement cmd
// step back one char, but not if we're at end of file
if (dot > p && !at_eof(dot))
// step back one char, but not if we're at end of file,
// or if we are at EOF and search was for 'w' and we're at
// the start of a 'W' word.
if (dot > p && (!at_eof(dot) || (c == 'w' && ispunct(*dot))))
dot--;
t = dot;
// don't include trailing WS as part of word
@ -3127,7 +3129,7 @@ static int find_range(char **start, char **stop, int cmd)
t = dot;
}
// for non-change operations WS after NL is not part of word
if (cmd != 'c' && dot != p && *dot != '\n')
if (cmd != 'c' && dot != t && *dot != '\n')
dot = t;
} else if (strchr("GHL+-jk'\r\n", c)) {
// these operate on whole lines