aboutsummaryrefslogtreecommitdiff
path: root/sam.c
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2018-02-19 16:55:22 +0100
committerMarc André Tanner <mat@brain-dump.org>2018-02-19 18:14:58 +0100
commitcbbf9286124ce60097177050c6ebff9801b34909 (patch)
treecd242c715e6e5a5e57aac3e5536581872e028a11 /sam.c
parent08c98fb36aa6fc7448b66ade6589c5a1a07c4b98 (diff)
downloadvis-cbbf9286124ce60097177050c6ebff9801b34909.tar.gz
vis-cbbf9286124ce60097177050c6ebff9801b34909.tar.xz
sam: fix g/^$/
With POSIX ERE the pattern ^$ matches strings ending with a new line because an empty match is reported after the trailing newline at the very end of the input. This is undesirable for use cases like x g/^$/ d which is supposed to delete all empty lines of a file. As a fix we disregard empty matches at the end of the given range.
Diffstat (limited to 'sam.c')
-rw-r--r--sam.c9
1 files changed, 7 insertions, 2 deletions
diff --git a/sam.c b/sam.c
index a0a48ec..d0d53c4 100644
--- a/sam.c
+++ b/sam.c
@@ -1343,8 +1343,13 @@ static bool cmd_delete(Vis *vis, Win *win, Command *cmd, const char *argv[], Sel
static bool cmd_guard(Vis *vis, Win *win, Command *cmd, const char *argv[], Selection *sel, Filerange *range) {
if (!win)
return false;
- bool match = !cmd->regex || !text_search_range_forward(win->file->text, range->start,
- text_range_size(range), cmd->regex, 0, NULL, 0);
+ bool match = false;
+ RegexMatch captures[1];
+ size_t len = text_range_size(range);
+ if (!cmd->regex)
+ match = true;
+ else if (!text_search_range_forward(win->file->text, range->start, len, cmd->regex, 1, captures, 0))
+ match = captures[0].start < range->end;
if ((count_evaluate(cmd) && match) ^ (argv[0][0] == 'v'))
return sam_execute(vis, win, cmd->cmd, sel, range);
view_selections_dispose_force(sel);