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

List:       busybox
Subject:    [PATCH 11/12] vi: allow backward search to specify line address
From:       Ron Yorston <rmy () pobox ! com>
Date:       2021-04-15 11:06:11
Message-ID: 60781e23.qyA3c4wyGjp1dBpa%rmy () pobox ! com
[Download RAW message or body]

It should be possible to use a backward search as a line address
in colon commands.

function                                             old     new   delta
colon                                               3661    3701     +40
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/0 up/down: 40/0)               Total: 40 bytes

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

diff --git a/editors/vi.c b/editors/vi.c
index 1d326f454..0866e0fa9 100644
--- a/editors/vi.c
+++ b/editors/vi.c
@@ -2346,9 +2346,9 @@ static char *get_one_address(char *p, int *addr)	// get colon addr, if present
 {
 	int st;
 # if ENABLE_FEATURE_VI_YANKMARK || ENABLE_FEATURE_VI_SEARCH
-	char *q;
+	char *q, c;
 # endif
-	IF_FEATURE_VI_YANKMARK(char c;)
+	IF_FEATURE_VI_SEARCH(int dir;)
 
 	*addr = -1;			// assume no addr
 	if (*p == '.') {	// the current line
@@ -2372,18 +2372,25 @@ static char *get_one_address(char *p, int *addr)	// get colon addr, if present
 	}
 # endif
 # if ENABLE_FEATURE_VI_SEARCH
-	else if (*p == '/') {	// a search pattern
-		q = strchrnul(p + 1, '/');
+	else if (*p == '/' || *p == '?') {	// a search pattern
+		c = *p;
+		q = strchrnul(p + 1, c);
 		if (p + 1 != q) {
 			// save copy of new pattern
 			free(last_search_pattern);
 			last_search_pattern = xstrndup(p, q - p);
 		}
 		p = q;
-		if (*p == '/')
+		if (*p == c)
 			p++;
-		q = char_search(next_line(dot), last_search_pattern + 1,
-						(FORWARD << 1) | FULL);
+		if (c == '/') {
+			q = next_line(dot);
+			dir = (FORWARD << 1) | FULL;
+		} else {
+			q = begin_line(dot);
+			dir = ((unsigned)BACK << 1) | FULL;
+		}
+		q = char_search(q, last_search_pattern + 1, dir);
 		if (q == NULL)
 			return NULL;
 		*addr = count_lines(text, q);
-- 
2.30.2

_______________________________________________
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