tex: support regular expression groups in :s replacement - neatvi - [fork] simple vi-type editor with UTF-8 support
(HTM) git clone git://src.adamsgaard.dk/neatvi
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
---
(DIR) commit 9cc9b8bb918f8a581ed57c730a42d4fe85b3152c
(DIR) parent df9f5a424f9e869ce1367f3c9ee3ceee84683f9f
(HTM) Author: Ali Gholami Rudi <ali@rudi.ir>
Date: Sat, 7 May 2016 21:35:01 +0430
ex: support regular expression groups in :s replacement
Diffstat:
M ex.c | 21 ++++++++++++++++++++-
1 file changed, 20 insertions(+), 1 deletion(-)
---
(DIR) diff --git a/ex.c b/ex.c
t@@ -660,6 +660,25 @@ static int ec_mark(char *ec)
return 0;
}
+static void replace(struct sbuf *dst, char *rep, char *ln, int *offs)
+{
+ while (rep[0]) {
+ if (rep[0] == '\\' && rep[1]) {
+ if (rep[1] >= '0' && rep[1] <= '9') {
+ int grp = (rep[1] - '0') * 2;
+ int len = offs[grp + 1] - offs[grp];
+ sbuf_mem(dst, ln + offs[grp], len);
+ } else {
+ sbuf_chr(dst, (unsigned char) rep[1]);
+ }
+ rep++;
+ } else {
+ sbuf_chr(dst, (unsigned char) rep[0]);
+ }
+ rep++;
+ }
+}
+
static int ec_substitute(char *ec)
{
char loc[EXLEN];
t@@ -697,7 +716,7 @@ static int ec_substitute(char *ec)
struct sbuf *r = sbuf_make();
while (rset_find(re, ln, LEN(offs) / 2, offs, 0) >= 0) {
sbuf_mem(r, ln, offs[0]);
- sbuf_str(r, rep);
+ replace(r, rep, ln, offs);
ln += offs[1];
if (!strchr(s, 'g'))
break;