summaryrefslogtreecommitdiff
path: root/usr.bin
diff options
context:
space:
mode:
authorChad Loder <cloder@cvs.openbsd.org>2005-05-27 08:08:19 +0000
committerChad Loder <cloder@cvs.openbsd.org>2005-05-27 08:08:19 +0000
commit0d2991c584ff46d1969268d0d449da3d41460aac (patch)
treee7c445cd7b0d2889c1e10cee5772d8585f201856 /usr.bin
parent01b5e624bc03f48e6b93c9542088caa94ee45e8d (diff)
Add emacs-style replace-string function that does not prompt you
to confirm replacements. OK jason, deraadt
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/mg/def.h3
-rw-r--r--usr.bin/mg/funmap.c3
-rw-r--r--usr.bin/mg/search.c40
3 files changed, 43 insertions, 3 deletions
diff --git a/usr.bin/mg/def.h b/usr.bin/mg/def.h
index 5604c2e5abf..987f968214a 100644
--- a/usr.bin/mg/def.h
+++ b/usr.bin/mg/def.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: def.h,v 1.60 2005/04/03 02:09:28 db Exp $ */
+/* $OpenBSD: def.h,v 1.61 2005/05/27 08:08:18 cloder Exp $ */
#include <sys/queue.h>
@@ -577,6 +577,7 @@ int re_forwsearch(int, int);
int re_backsearch(int, int);
int re_searchagain(int, int);
int re_queryrepl(int, int);
+int replstr(int, int);
int setcasefold(int, int);
int delmatchlines(int, int);
int delnonmatchlines(int, int);
diff --git a/usr.bin/mg/funmap.c b/usr.bin/mg/funmap.c
index 72a7fa66016..07857e7f7ee 100644
--- a/usr.bin/mg/funmap.c
+++ b/usr.bin/mg/funmap.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: funmap.c,v 1.12 2005/04/28 07:23:56 otto Exp $ */
+/* $OpenBSD: funmap.c,v 1.13 2005/05/27 08:08:18 cloder Exp $ */
/*
* Copyright (c) 2001 Artur Grabowski <art@openbsd.org>. All rights reserved.
*
@@ -175,6 +175,7 @@ static struct funmap functnames[] = {
{showcwdir, "pwd",},
#endif /* !NO_DIR */
{queryrepl, "query-replace",},
+ {replstr, "replace-string",},
#ifdef REGEX
{re_queryrepl, "query-replace-regexp",},
#endif /* REGEX */
diff --git a/usr.bin/mg/search.c b/usr.bin/mg/search.c
index b15f5b53b2c..1564b74d95e 100644
--- a/usr.bin/mg/search.c
+++ b/usr.bin/mg/search.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: search.c,v 1.16 2005/05/27 07:22:52 cloder Exp $ */
+/* $OpenBSD: search.c,v 1.17 2005/05/27 08:08:18 cloder Exp $ */
/*
* Search commands.
@@ -553,6 +553,44 @@ stopsearch:
}
/*
+ * Replace string globally without individual prompting.
+ */
+/* ARGSUSED */
+int
+replstr(int f, int n)
+{
+ char news[NPAT];
+ int s, plen, rcnt = 0;
+ char *r;
+
+ if ((s = readpattern("Replace string")) != TRUE)
+ return s;
+
+ r = ereply("Replace string %s with: ", news, NPAT, pat);
+ if (r == NULL)
+ return (ABORT);
+
+ plen = strlen(pat);
+ while (forwsrch() == TRUE) {
+ update();
+ if (lreplace((RSIZE)plen, news, f) == FALSE)
+ return (FALSE);
+
+ rcnt++;
+ }
+
+ curwp->w_flag |= WFHARD;
+ update();
+
+ if (rcnt == 1)
+ ewprintf("(1 replacement done)");
+ else
+ ewprintf("(%d replacements done)", rcnt);
+
+ return (TRUE);
+}
+
+/*
* This routine does the real work of a forward search. The pattern is sitting
* in the external variable "pat". If found, dot is updated, the window system
* is notified of the change, and TRUE is returned. If the string isn't found,