-/* $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. */
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);
-/* $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 */
{reposition, "recenter", 0},
{redraw, "redraw-display", 0},
#ifdef REGEX
+ {re_repl, "replace-regexp", 2},
{replstr, "replace-string", 2},
#endif /* REGEX */
{revertbuffer, "revert-buffer", 0},
-.\" $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
.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
-/* $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. */
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.