diff options
author | Mark Lumsden <lum@cvs.openbsd.org> | 2021-04-22 19:50:56 +0000 |
---|---|---|
committer | Mark Lumsden <lum@cvs.openbsd.org> | 2021-04-22 19:50:56 +0000 |
commit | 436d9e11a871cc61c332d4ed0828b0f4f246ca1f (patch) | |
tree | 07b2e3d6fb092b9ae8e1fb055ad26a20d14f050b /usr.bin/mg | |
parent | b9ecd1063263ccc7343f56e4c67eccabf06a96dd (diff) |
Add a non-interactive version of query-replace-regexp function called
replace-regexp. Unfortunately query-replace-regexp can't be used in a
startup file.
Diffstat (limited to 'usr.bin/mg')
-rw-r--r-- | usr.bin/mg/def.h | 3 | ||||
-rw-r--r-- | usr.bin/mg/funmap.c | 3 | ||||
-rw-r--r-- | usr.bin/mg/mg.1 | 6 | ||||
-rw-r--r-- | usr.bin/mg/re_search.c | 30 |
4 files changed, 37 insertions, 5 deletions
diff --git a/usr.bin/mg/def.h b/usr.bin/mg/def.h index f5102b840ac..ee32c06f127 100644 --- a/usr.bin/mg/def.h +++ b/usr.bin/mg/def.h @@ -1,4 +1,4 @@ -/* $OpenBSD: def.h,v 1.172 2021/04/20 10:02:50 lum Exp $ */ +/* $OpenBSD: def.h,v 1.173 2021/04/22 19:50:55 lum Exp $ */ /* This file is in the public domain. */ @@ -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); diff --git a/usr.bin/mg/funmap.c b/usr.bin/mg/funmap.c index c6c59272ed8..36a88e4573d 100644 --- a/usr.bin/mg/funmap.c +++ b/usr.bin/mg/funmap.c @@ -1,4 +1,4 @@ -/* $OpenBSD: funmap.c,v 1.62 2021/04/20 16:34:20 lum Exp $ */ +/* $OpenBSD: funmap.c,v 1.63 2021/04/22 19:50:55 lum Exp $ */ /* This file is in the public domain */ @@ -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}, diff --git a/usr.bin/mg/mg.1 b/usr.bin/mg/mg.1 index e43d851b513..c13a7c15366 100644 --- a/usr.bin/mg/mg.1 +++ b/usr.bin/mg/mg.1 @@ -1,7 +1,7 @@ -.\" $OpenBSD: mg.1,v 1.123 2021/04/20 10:02:50 lum Exp $ +.\" $OpenBSD: mg.1,v 1.124 2021/04/22 19:50:55 lum Exp $ .\" This file is in the public domain. .\" -.Dd $Mdocdate: April 20 2021 $ +.Dd $Mdocdate: April 22 2021 $ .Dt MG 1 .Os .Sh NAME @@ -778,6 +778,8 @@ Display current (global) working directory in the status area. .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 diff --git a/usr.bin/mg/re_search.c b/usr.bin/mg/re_search.c index 95349dd3951..eba9b03c3a6 100644 --- a/usr.bin/mg/re_search.c +++ b/usr.bin/mg/re_search.c @@ -1,4 +1,4 @@ -/* $OpenBSD: re_search.c,v 1.35 2020/07/22 13:29:05 tb Exp $ */ +/* $OpenBSD: re_search.c,v 1.36 2021/04/22 19:50:55 lum Exp $ */ /* This file is in the public domain. */ @@ -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. |