Heya, I added the 'g' ex command to vi
The only problem with it that I see is (also a problem in cslpit with /regex/ 
rules) 
that it doesn't read escaped regexp's because 
sscanf(); doesn't work like that and I haven't found a simple solution to 
reading 
escaped strings up until a delimiter (except when that delimiter is escaped)

The only reason this command took so long to implement is because I had some 
errors
that turned out to be caused by uninitialized memory. 

I would write some test cases to go with this command, but I already have a 
pending patch
for other test cases that I have added and I didn't want to have a conflict
between the 2 patches 

Also, In the error message handling, I replaced "sleep(1);" with 
"(void)getchar();".

- Oliver Webb <aquahobby...@proton.me>
From e44eec772ad545a37e6442810cd36f18b282075c Mon Sep 17 00:00:00 2001
From: Oliver Webb <aquahobby...@proton.me>
Date: Mon, 9 Oct 2023 21:52:40 -0500
Subject: [PATCH] vi.c: Added ex 'g' command, Replaced "sleep(1)" with
 getchar() in error handler

---
 toys/pending/vi.c | 24 +++++++++++++++++++++---
 1 file changed, 21 insertions(+), 3 deletions(-)

diff --git a/toys/pending/vi.c b/toys/pending/vi.c
index 2b5777a8..2041f21c 100644
--- a/toys/pending/vi.c
+++ b/toys/pending/vi.c
@@ -16,7 +16,7 @@ config VI
     Visual text editor. Predates keyboards with standardized cursor keys.
     If you don't know how to use it, hit the ESC key, type :q! and press ENTER.
 
-    -s	run SCRIPT of commands on FILE
+    -s  run SCRIPT of commands on FILE
 
     vi mode commands:
 
@@ -547,9 +547,9 @@ static void show_error(char *fmt, ...)
   printf("\e[0m");
   xflush(1);
 
-  // TODO: better integration with status line: remove sleep and keep
+  // TODO: better integration with status line: keep
   // message until next operation.
-  sleep(1);
+  (void)getchar();
 }
 
 static void linelist_unload()
@@ -1371,6 +1371,24 @@ static int run_ex_cmd(char *cmd)
     else if (*(cmd+1) == 'd') {
       run_vi_cmd("dd");
       run_vi_cmd("k");
+    } else if (*(cmd+1) == 'g') {
+      char *rgx = malloc(strlen(cmd));
+      int el = get_endline(), ln = 0;
+      regex_t rgxc;
+      if (!sscanf(cmd, ":g/%[^/]/%[^\ng]", rgx, cmd+1)) return 0;
+      if (regcomp(&rgxc, rgx, 0)) return 0;
+      cmd[0] = ':';
+      
+      for (; ln < el; ln++) {
+        run_vi_cmd("yy");
+        if (!regexec(&rgxc, TT.yank.data, 0, 0, 0)) run_ex_cmd(cmd);
+        run_vi_cmd("j");
+      }
+
+      // Reset Frame
+      ctrl_f();
+      draw_page();
+      ctrl_b();
     }
 
     // Line Ranges
-- 
2.34.1

_______________________________________________
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to