[PATCH 03/11] vi: more fixes to range selection by word

Ron Yorston rmy at pobox.com
Tue Apr 6 12:41:01 UTC 2021


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 at pobox.com>
---
 editors/vi.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/editors/vi.c b/editors/vi.c
index ecffbacf2..7ede8f2ff 100644
--- a/editors/vi.c
+++ b/editors/vi.c
@@ -3094,8 +3094,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
@@ -3104,7 +3106,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
-- 
2.30.2



More information about the busybox mailing list