This diff adds a non-interactive version of mg's query-replace-regexp function called replace-regexp. Unfortunately query-replace-regexp can't be used in a startup file.

Ok?

Index: def.h
===================================================================
RCS file: /cvs/src/usr.bin/mg/def.h,v
retrieving revision 1.172
diff -u -p -u -p -r1.172 def.h
--- def.h       20 Apr 2021 10:02:50 -0000      1.172
+++ def.h       20 Apr 2021 16:12:50 -0000
@@ -673,6 +673,7 @@ int          re_forwsearch(int, int);
 int             re_backsearch(int, int);
 int             re_searchagain(int, int);
 int             re_queryrepl(int, int);
+int             re_repl(int, int);
 int             replstr(int, int);
 int             setcasefold(int, int);
 int             delmatchlines(int, int);
Index: funmap.c
===================================================================
RCS file: /cvs/src/usr.bin/mg/funmap.c,v
retrieving revision 1.61
diff -u -p -u -p -r1.61 funmap.c
--- funmap.c    20 Apr 2021 10:02:50 -0000      1.61
+++ funmap.c    20 Apr 2021 16:12:50 -0000
@@ -181,6 +181,7 @@ static struct funmap functnames[] = {
        {reposition, "recenter", 0},
        {redraw, "redraw-display", 0},
 #ifdef REGEX
+       {re_repl, "replace-regexp", 2},
        {replstr, "replace-string", 2},
 #endif /* REGEX */
        {revertbuffer, "revert-buffer", 0},
Index: mg.1
===================================================================
RCS file: /cvs/src/usr.bin/mg/mg.1,v
retrieving revision 1.123
diff -u -p -u -p -r1.123 mg.1
--- mg.1        20 Apr 2021 10:02:50 -0000      1.123
+++ mg.1        20 Apr 2021 16:12:50 -0000
@@ -778,6 +778,8 @@ Display current (global) working directo
 .It query-replace
 Query Replace.
 Search and replace strings selectively, prompting after each match.
+.It replace-regexp
+Replace regular expression globally without individual prompting.
 .It replace-string
 Replace string globally without individual prompting.
 .It query-replace-regexp
Index: re_search.c
===================================================================
RCS file: /cvs/src/usr.bin/mg/re_search.c,v
retrieving revision 1.35
diff -u -p -u -p -r1.35 re_search.c
--- re_search.c 22 Jul 2020 13:29:05 -0000      1.35
+++ re_search.c 20 Apr 2021 16:12:50 -0000
@@ -211,6 +211,34 @@ stopsearch:
        return (TRUE);
 }

+int
+re_repl(int f, int n)
+{
+        int     rcnt = 0;               /* replacements made so far     */
+        int     plen, s;                /* length of found string       */
+        char    news[NPAT];             /* replacement string           */
+
+        if ((s = re_readpattern("RE Replace")) != TRUE)
+                return (s);
+        if (eread("Replace %s with: ", news, NPAT,
+            EFNUL | EFNEW | EFCR, re_pat) == NULL)
+                return (ABORT);
+
+       while (re_forwsrch() == TRUE) {
+               plen = regex_match[0].rm_eo - regex_match[0].rm_so;
+               if (re_doreplace((RSIZE)plen, news) == FALSE)
+                       return (FALSE);
+               rcnt++;
+       }
+ + curwp->w_rflag |= WFFULL;
+       update(CMODE);
+       if (!inmacro)
+               ewprintf("(%d replacement(s) done)", rcnt);
+ + return(TRUE);
+}
+
 /*
  * Routine re_doreplace calls lreplace to make replacements needed by
  * re_query replace.  Its reason for existence is to deal with \1, \2. etc.

Reply via email to