[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    [PATCH 2/3] vi: up/down motion beyond end of file should fail
From:       Ron Yorston <rmy () pobox ! com>
Date:       2021-06-16 13:46:01
Message-ID: 60ca0099.UDdQ63mfHimeQIyv%rmy () pobox ! com
[Download RAW message or body]

In traditional vi and vim line motion commands ('+'/'-'/'j'/'k')
fail if the movement would exceed the bounds of the file.  BusyBox vi
allowed such commands to succeed, leaving the cursor on the first or
last character of the file.

Make BusyBox vi work like vi/vim.

For the 'G'/'H'/'L' commands traditional vi treats an out of bounds
result as an error, vim doesn't.  BusyBox vi behaves like vim, both
before and after this patch.

function                                             old     new   delta
do_cmd                                              4785    4851     +66
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/0 up/down: 66/0)               Total: 66 bytes

Signed-off-by: Ron Yorston <rmy@pobox.com>
---
 editors/vi.c | 63 +++++++++++++++++++++++++++++++---------------------
 1 file changed, 38 insertions(+), 25 deletions(-)

diff --git a/editors/vi.c b/editors/vi.c
index 3daa0756b..231c62809 100644
--- a/editors/vi.c
+++ b/editors/vi.c
@@ -3635,24 +3635,30 @@ static void do_cmd(int c)
 	case 10:			// Newline ^J
 	case 'j':			// j- goto next line, same col
 	case KEYCODE_DOWN:	// cursor key Down
+	case 13:			// Carriage Return ^M
+	case '+':			// +- goto next line
+		q = dot;
 		do {
-			dot_next();		// go to next B-o-l
+			p = next_line(q);
+			if (p == end_line(q)) {
+				indicate_error();
+				goto dc1;
+			}
+			q = p;
 		} while (--cmdcnt > 0);
-		// try to stay in saved column
-		dot = cindex == C_END ? end_line(dot) : move_to_col(dot, cindex);
-		keep_index = TRUE;
+		dot = q;
+		if (c == 13 || c == '+') {
+			dot_skip_over_ws();
+		} else {
+			// try to stay in saved column
+			dot = cindex == C_END ? end_line(dot) : move_to_col(dot, cindex);
+			keep_index = TRUE;
+		}
 		break;
 	case 12:			// ctrl-L  force redraw whole screen
 	case 18:			// ctrl-R  force redraw
 		redraw(TRUE);	// this will redraw the entire display
 		break;
-	case 13:			// Carriage Return ^M
-	case '+':			// +- goto next line
-		do {
-			dot_next();
-		} while (--cmdcnt > 0);
-		dot_skip_over_ws();
-		break;
 	case 21:			// ctrl-U  scroll up half screen
 		dot_scroll((rows - 2) / 2, -1);
 		break;
@@ -3818,12 +3824,6 @@ static void do_cmd(int c)
 	case ',':           // ,- repeat latest search in opposite direction
 		dot_to_char(c != ',' ? last_search_cmd : last_search_cmd ^ 0x20);
 		break;
-	case '-':			// -- goto prev line
-		do {
-			dot_prev();
-		} while (--cmdcnt > 0);
-		dot_skip_over_ws();
-		break;
 #if ENABLE_FEATURE_VI_DOT_CMD
 	case '.':			// .- repeat the last modifying command
 		// Stuff the last_modifying_cmd back into stdin
@@ -4029,9 +4029,10 @@ static void do_cmd(int c)
 		if (cmdcnt > (rows - 1)) {
 			cmdcnt = (rows - 1);
 		}
-		if (--cmdcnt > 0) {
-			do_cmd('+');
+		while (--cmdcnt > 0) {
+			dot_next();
 		}
+		dot_begin();
 		dot_skip_over_ws();
 		break;
 	case 'I':			// I- insert before first non-blank
@@ -4068,8 +4069,8 @@ static void do_cmd(int c)
 		if (cmdcnt > (rows - 1)) {
 			cmdcnt = (rows - 1);
 		}
-		if (--cmdcnt > 0) {
-			do_cmd('-');
+		while (--cmdcnt > 0) {
+			dot_prev();
 		}
 		dot_begin();
 		dot_skip_over_ws();
@@ -4234,12 +4235,24 @@ static void do_cmd(int c)
 	}
 	case 'k':			// k- goto prev line, same col
 	case KEYCODE_UP:		// cursor key Up
+	case '-':			// -- goto prev line
+		q = dot;
 		do {
-			dot_prev();
+			p = prev_line(q);
+			if (p == begin_line(q)) {
+				indicate_error();
+				goto dc1;
+			}
+			q = p;
 		} while (--cmdcnt > 0);
-		// try to stay in saved column
-		dot = cindex == C_END ? end_line(dot) : move_to_col(dot, cindex);
-		keep_index = TRUE;
+		dot = q;
+		if (c == '-') {
+			dot_skip_over_ws();
+		} else {
+			// try to stay in saved column
+			dot = cindex == C_END ? end_line(dot) : move_to_col(dot, cindex);
+			keep_index = TRUE;
+		}
 		break;
 	case 'r':			// r- replace the current char with user input
 		c1 = get_one_char();	// get the replacement char
-- 
2.31.1

_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic