fix(substitute): subtract number of backslashes later

This commit is contained in:
zeertzjq
2022-06-12 07:48:28 +08:00
parent 502f03fc06
commit 41bb81a2df
3 changed files with 29 additions and 9 deletions

View File

@@ -4142,6 +4142,7 @@ static int do_sub(exarg_T *eap, proftime_T timeout, long cmdpreview_ns, handle_T
// That is Vi compatible. // That is Vi compatible.
for (p1 = new_end; *p1; p1++) { for (p1 = new_end; *p1; p1++) {
if (p1[0] == '\\' && p1[1] != NUL) { // remove backslash if (p1[0] == '\\' && p1[1] != NUL) { // remove backslash
sublen--; // correct the byte counts for extmark_splice()
STRMOVE(p1, p1 + 1); STRMOVE(p1, p1 + 1);
} else if (*p1 == CAR) { } else if (*p1 == CAR) {
if (u_inssub(lnum) == OK) { // prepare for undo if (u_inssub(lnum) == OK) { // prepare for undo

View File

@@ -1737,10 +1737,6 @@ static int vim_regsub_both(char_u *source, typval_T *expr, char_u *dest, int des
static char_u *eval_result = NULL; static char_u *eval_result = NULL;
bool copy = flags & REGSUB_COPY; bool copy = flags & REGSUB_COPY;
// We need to keep track of how many backslashes we escape, so that the byte
// counts for `extmark_splice` are correct.
int num_escaped = 0;
// Be paranoid... // Be paranoid...
if ((source == NULL && expr == NULL) || dest == NULL) { if ((source == NULL && expr == NULL) || dest == NULL) {
emsg(_(e_null)); emsg(_(e_null));
@@ -1928,7 +1924,6 @@ static int vim_regsub_both(char_u *source, typval_T *expr, char_u *dest, int des
// later. Used to insert a literal CR. // later. Used to insert a literal CR.
default: default:
if (flags & REGSUB_BACKSLASH) { if (flags & REGSUB_BACKSLASH) {
num_escaped += 1;
if (copy) { if (copy) {
if (dst + 1 > dest + destlen) { if (dst + 1 > dest + destlen) {
iemsg("vim_regsub_both(): not enough space"); iemsg("vim_regsub_both(): not enough space");
@@ -2096,7 +2091,7 @@ static int vim_regsub_both(char_u *source, typval_T *expr, char_u *dest, int des
} }
exit: exit:
return (int)((dst - dest) + 1 - num_escaped); return (int)((dst - dest) + 1);
} }
/* /*

View File

@@ -612,7 +612,15 @@ describe('lua: nvim_buf_attach on_bytes', function()
} }
feed("<esc>") feed("<esc>")
-- replacing with escaped characters -- replacing with expression register
feed([[:%s/b/\=5+5]])
check_events {
{ "test1", "bytes", 1, 3, 0, 1, 1, 0, 1, 1, 0, 2, 2 };
{ "test1", "bytes", 1, 5, 0, 1, 1, 0, 2, 2, 0, 1, 1 };
}
feed("<esc>")
-- replacing with backslash
feed([[:%s/b/\\]]) feed([[:%s/b/\\]])
check_events { check_events {
{ "test1", "bytes", 1, 3, 0, 1, 1, 0, 1, 1, 0, 1, 1 }; { "test1", "bytes", 1, 3, 0, 1, 1, 0, 1, 1, 0, 1, 1 };
@@ -620,8 +628,24 @@ describe('lua: nvim_buf_attach on_bytes', function()
} }
feed("<esc>") feed("<esc>")
-- replacing with expression register -- replacing with backslash from expression register
feed([[:%s/b/\=5+5]]) feed([[:%s/b/\='\']])
check_events {
{ "test1", "bytes", 1, 3, 0, 1, 1, 0, 1, 1, 0, 1, 1 };
{ "test1", "bytes", 1, 5, 0, 1, 1, 0, 1, 1, 0, 1, 1 };
}
feed("<esc>")
-- replacing with backslash followed by another character
feed([[:%s/b/\\!]])
check_events {
{ "test1", "bytes", 1, 3, 0, 1, 1, 0, 1, 1, 0, 2, 2 };
{ "test1", "bytes", 1, 5, 0, 1, 1, 0, 2, 2, 0, 1, 1 };
}
feed("<esc>")
-- replacing with backslash followed by another character from expression register
feed([[:%s/b/\='\!']])
check_events { check_events {
{ "test1", "bytes", 1, 3, 0, 1, 1, 0, 1, 1, 0, 2, 2 }; { "test1", "bytes", 1, 3, 0, 1, 1, 0, 1, 1, 0, 2, 2 };
{ "test1", "bytes", 1, 5, 0, 1, 1, 0, 2, 2, 0, 1, 1 }; { "test1", "bytes", 1, 5, 0, 1, 1, 0, 2, 2, 0, 1, 1 };