[git commit] vi: improve motion by paragraph

Denys Vlasenko vda.linux at googlemail.com
Mon Mar 29 10:16:21 UTC 2021


commit: https://git.busybox.net/busybox/commit/?id=8b571bd7b507c5dbdf8bb2bd804047ed66ea9809
branch: https://git.busybox.net/busybox/commit/?id=refs/heads/master

When moving by paragraph ('{' and '}'):

- Treat multiple empty lines as a single paragraph separator.

- When no paragraph separator is found move to the start or end of
  the file depending on the direction of motion.

function                                             old     new   delta
do_cmd                                              4821    4900     +79
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/0 up/down: 79/0)               Total: 79 bytes

Signed-off-by: Ron Yorston <rmy at pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 editors/vi.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/editors/vi.c b/editors/vi.c
index e3e0f4b44..5d4b0f20e 100644
--- a/editors/vi.c
+++ b/editors/vi.c
@@ -3515,12 +3515,20 @@ static void do_cmd(int c)
 	case '{':			// {- move backward paragraph
 	case '}':			// }- move forward paragraph
 		do {
-			q = char_search(dot, "\n\n", c == '{' ?
-						((unsigned)BACK << 1) | FULL :
-						(FORWARD << 1) | FULL);
+			dir = c == '}' ? FORWARD : BACK;
+			// skip over consecutive empty lines
+			while ((dir == FORWARD ? dot < end - 1 : dot > text) &&
+						*dot == '\n' && dot[dir] == '\n') {
+				dot += dir;
+			}
+			q = char_search(dot, "\n\n", ((unsigned)dir << 1) | FULL);
 			if (q != NULL) {	// found blank line
 				dot = next_line(q);	// move to next blank line
 			}
+			else {	// blank line not found, move to end of file
+				dot = dir == FORWARD ? end - 1 : text;
+				break;
+			}
 		} while (--cmdcnt > 0);
 		break;
 #endif /* FEATURE_VI_SEARCH */


More information about the busybox-cvs mailing list