Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add set-case-replaced to toggle case-preserving replace on or off
By default, replacing "foo" with "bar" turns "FOO" into "BAR".
With case-replace turned off, "FOO" will turn into "bar".

Signed-off-by: Reyk Flöter <reyk@openbsd.org>
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
  • Loading branch information
reyk authored and troglobit committed Sep 16, 2018
1 parent b7b6ba5 commit 505eb8b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
7 changes: 5 additions & 2 deletions mg.1
@@ -1,7 +1,7 @@
.\" $OpenBSD: mg.1,v 1.106 2017/12/11 07:27:07 jmc Exp $
.\" $OpenBSD: mg.1,v 1.107 2018/08/29 07:50:16 reyk Exp $
.\" This file is in the public domain.
.\"
.Dd $Mdocdate: December 11 2017 $
.Dd $Mdocdate: August 29 2018 $
.Dt MG 1
.Os
.Sh NAME
Expand Down Expand Up @@ -857,6 +857,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.
Expand Down
3 changes: 2 additions & 1 deletion src/def.h
@@ -1,4 +1,4 @@
/* $OpenBSD: def.h,v 1.154 2016/01/02 10:39:19 lum Exp $ */
/* $OpenBSD: def.h,v 1.156 2018/08/29 07:50:16 reyk Exp $ */

/* This file is in the public domain. */

Expand Down Expand Up @@ -474,6 +474,7 @@ int ldelete(RSIZE, int);
int ldelnewline(void);
int lreplace(RSIZE, char *);
char * linetostr(const struct line *);
int setcasereplace(int, int);

/* yank.c X */

Expand Down
3 changes: 2 additions & 1 deletion src/funmap.c
@@ -1,4 +1,4 @@
/* $OpenBSD: funmap.c,v 1.52 2015/12/29 19:44:32 lum Exp $ */
/* $OpenBSD: funmap.c,v 1.54 2018/08/29 07:50:16 reyk Exp $ */

/* This file is in the public domain */

Expand Down Expand Up @@ -194,6 +194,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",},
Expand Down
30 changes: 24 additions & 6 deletions src/line.c
@@ -1,4 +1,4 @@
/* $OpenBSD: line.c,v 1.60 2018/07/12 12:38:56 florian Exp $ */
/* $OpenBSD: line.c,v 1.61 2018/08/29 07:50:16 reyk Exp $ */

/* This file is in the public domain. */

Expand Down Expand Up @@ -26,6 +26,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.
Expand Down Expand Up @@ -515,7 +531,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);
Expand All @@ -530,11 +546,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;
Expand All @@ -555,9 +574,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]);
Expand All @@ -571,6 +587,8 @@ lreplace(RSIZE plen, char *st)
}
}

done:
(void)ldelete(plen, KNONE);
region_put_data(repl, rlen);
lchange(WFFULL);

Expand Down

0 comments on commit 505eb8b

Please sign in to comment.