Hi,

the new case-preserving replace is a very nice feature - 
unless you want to replace a string with the exact capitalisation.

>From florian@'s original commit message:
> Use (mostly) the same capitalisation in the replacement string as in
> 
> the replaced string:
> 
> replacing foo with bar turns
> 
> foo
> Foo
> FOO
> 
> into
> 
> bar
> Bar
> BAR
> 
> OK phessler, benno

Now I had the case that I wanted to replace FOO with bar, not BAR.

Emacs has an option "case-replace" to turn it off - it can be set to
nil to not preserve the case of the replaced string.

This adds set-case-replace.

Ok?

Reyk

Index: usr.bin/mg/def.h
===================================================================
RCS file: /cvs/src/usr.bin/mg/def.h,v
retrieving revision 1.155
diff -u -p -u -p -r1.155 def.h
--- usr.bin/mg/def.h    14 Apr 2016 17:05:32 -0000      1.155
+++ usr.bin/mg/def.h    28 Aug 2018 11:59:49 -0000
@@ -391,6 +391,7 @@ int          ldelete(RSIZE, int);
 int             ldelnewline(void);
 int             lreplace(RSIZE, char *);
 char *          linetostr(const struct line *);
+int             setcasereplace(int, int);
 
 /* yank.c X */
 
Index: usr.bin/mg/funmap.c
===================================================================
RCS file: /cvs/src/usr.bin/mg/funmap.c,v
retrieving revision 1.53
diff -u -p -u -p -r1.53 funmap.c
--- usr.bin/mg/funmap.c 14 Apr 2016 17:05:32 -0000      1.53
+++ usr.bin/mg/funmap.c 28 Aug 2018 11:59:49 -0000
@@ -184,6 +184,7 @@ static struct funmap functnames[] = {
 #ifdef REGEX
        {setcasefold, "set-case-fold-search",},
 #endif /* REGEX */
+       {setcasereplace, "set-case-replace",},
        {set_default_mode, "set-default-mode",},
        {setfillcol, "set-fill-column",},
        {setmark, "set-mark-command",},
Index: usr.bin/mg/line.c
===================================================================
RCS file: /cvs/src/usr.bin/mg/line.c,v
retrieving revision 1.60
diff -u -p -u -p -r1.60 line.c
--- usr.bin/mg/line.c   12 Jul 2018 12:38:56 -0000      1.60
+++ usr.bin/mg/line.c   28 Aug 2018 11:59:49 -0000
@@ -27,6 +27,22 @@
 
 #include "def.h"
 
+int    casereplace = TRUE;
+
+/*
+ * Preserve the case of the replaced string.
+ */
+int
+setcasereplace(int f, int n)
+{
+       if (f & FFARG)
+               casereplace = n > 0;
+       else
+               casereplace = !casereplace;
+       ewprintf("Case-replace is %sabled", casereplace ? "en" : "dis");
+       return (TRUE);
+}
+
 /*
  * Allocate a new line of size `used'.  lrealloc() can be called if the line
  * ever needs to grow beyond that.
@@ -516,7 +532,7 @@ lreplace(RSIZE plen, char *st)
        RSIZE n;
        int s, doto, is_query_capitalised = 0, is_query_allcaps = 0;
        int is_replace_alllower = 0;
-       char *repl;
+       char *repl = NULL;
 
        if ((s = checkdirty(curbp)) != TRUE)
                return (s);
@@ -531,11 +547,14 @@ lreplace(RSIZE plen, char *st)
                ewprintf("out of memory");
                return (FALSE);
        }
+       rlen = strlen(repl);
 
        undo_boundary_enable(FFRAND, 0);
-
        (void)backchar(FFARG | FFRAND, (int)plen);
 
+       if (casereplace != TRUE)
+               goto done;
+
        lp = curwp->w_dotp;
        doto = curwp->w_doto;
        n = plen;
@@ -556,9 +575,6 @@ lreplace(RSIZE plen, char *st)
                }
        }
 
-       (void)ldelete(plen, KNONE);
-
-       rlen = strlen(repl);
        for (n = 0, is_replace_alllower = 1; n < rlen && is_replace_alllower;
            n++)
                is_replace_alllower = !isupper((unsigned char)repl[n]);
@@ -572,6 +588,8 @@ lreplace(RSIZE plen, char *st)
                }
        }
 
+ done:
+       (void)ldelete(plen, KNONE);
        region_put_data(repl, rlen);
        lchange(WFFULL);
 
Index: usr.bin/mg/mg.1
===================================================================
RCS file: /cvs/src/usr.bin/mg/mg.1,v
retrieving revision 1.106
diff -u -p -u -p -r1.106 mg.1
--- usr.bin/mg/mg.1     11 Dec 2017 07:27:07 -0000      1.106
+++ usr.bin/mg/mg.1     28 Aug 2018 11:59:49 -0000
@@ -846,6 +846,9 @@ Currently only affects fill-paragraph.
 Set case-fold searching, causing case not to matter
 in regular expression searches.
 This is the default.
+.It set-case-replace
+Preserve the case of the replaced string.
+This is the default.
 .It set-default-mode
 Append the supplied mode to the list of default modes
 used by subsequent buffer creation.

Reply via email to