vis

a vi-like editor based on Plan 9's structural regular expressions

git clone https://9o.is/git/vis.git

commit 40e227ba930e3f45eaac0152d3892c950be6f948
parent ae3c2432e4e280b30e946b2124ce2bfb62ad8892
Author: Marc André Tanner <mat@brain-dump.org>
Date:   Thu, 11 Feb 2021 10:47:27 +0100

sam: tweak handling of zero length matches in y commands

In sam(1) a command like x/[a-z]+/ y/-?/ matches every character
individually, whereas in vis it would produce a zero length match before
each character as is correctly the case for the x counter part.

Diffstat:
Msam.c | 13+++++++++----
1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/sam.c b/sam.c @@ -1394,7 +1394,8 @@ static int extract(Vis *vis, Win *win, Command *cmd, const char *argv[], Selecti if (cmd->regex) { bool trailing_match = false; - size_t start = range->start, end = range->end, last_start = EPOS; + size_t start = range->start, end = range->end; + size_t last_start = argv[0][0] == 'x' ? EPOS : start; size_t nsub = 1 + text_regex_nsub(cmd->regex); if (nsub > MAX_REGEX_SUB) nsub = MAX_REGEX_SUB; @@ -1413,7 +1414,7 @@ static int extract(Vis *vis, Win *win, Command *cmd, const char *argv[], Selecti if (argv[0][0] == 'x') r = text_range_new(match[0].start, match[0].end); else - r = text_range_new(start, match[0].start); + r = text_range_new(last_start, match[0].start); if (match[0].start == match[0].end) { if (last_start == match[0].start) { start++; @@ -1428,8 +1429,10 @@ static int extract(Vis *vis, Win *win, Command *cmd, const char *argv[], Selecti if (end == match[0].start && start > range->start && text_byte_get(txt, end-1, &c) && c == '\n') break; + start = match[0].end + 1; + } else { + start = match[0].end; } - start = match[0].end; trailing_match = start == end; } else { if (argv[0][0] == 'y') @@ -1443,12 +1446,14 @@ static int extract(Vis *vis, Win *win, Command *cmd, const char *argv[], Selecti Register *reg = &vis->registers[VIS_REG_AMPERSAND+i]; register_put_range(vis, reg, txt, &match[i]); } + last_start = match[0].end; + } else { + last_start = start; } if (simulate) count++; else ret &= sam_execute(vis, win, cmd->cmd, NULL, &r); - last_start = start; } } } else {