aboutsummaryrefslogtreecommitdiff
path: root/sam.c
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2016-04-13 17:20:18 +0200
committerMarc André Tanner <mat@brain-dump.org>2016-04-13 17:20:18 +0200
commit1a158268c7693b00bf43c7e81034816d8d00358c (patch)
treee3fc48430d5e62efaa1ebeff52532bd4a2662e66 /sam.c
parent6c2b9033cfb1a82275871b2800c5ef1e4cf877a5 (diff)
downloadvis-1a158268c7693b00bf43c7e81034816d8d00358c.tar.gz
vis-1a158268c7693b00bf43c7e81034816d8d00358c.tar.xz
sam: improve ^ matching start of line
There are some nasty differences between the meaning of ^ in Plan 9's regexp library and POSIX when using REG_NEWLINE. The former only matches at the beginning of a line wheras the latter matches the zero-length string immediately after a newline character \n. As a result this also matches after the very last newline at the end of the file. This is undesired behavior for a command like :x/^/c/#/ Hence we try to filter out this last match. Close #264
Diffstat (limited to 'sam.c')
-rw-r--r--sam.c14
1 files changed, 10 insertions, 4 deletions
diff --git a/sam.c b/sam.c
index be0b14a..3331567 100644
--- a/sam.c
+++ b/sam.c
@@ -833,7 +833,8 @@ static bool cmd_extract(Vis *vis, Win *win, Command *cmd, const char *argv[], Cu
RegexMatch match[1];
while (start < end) {
bool found = text_search_range_forward(txt, start,
- end - start, cmd->regex, 1, match, 0) == 0;
+ end - start, cmd->regex, 1, match,
+ start > range->start ? REG_NOTBOL : 0) == 0;
Filerange r = text_range_empty();
if (found) {
if (argv[0][0] == 'x')
@@ -845,10 +846,15 @@ static bool cmd_extract(Vis *vis, Win *win, Command *cmd, const char *argv[], Cu
start++;
continue;
}
- start = match[0].end+1;
- } else {
- start = match[0].end;
+ /* in Plan 9's regexp library ^ matches the beginning
+ * of a line, however in POSIX with REG_NEWLINE ^
+ * matches the zero-length string immediately after a
+ * newline. Try filtering out the last such match at EOF.
+ */
+ if (end == match[0].start && start > range->start)
+ break;
}
+ start = match[0].end;
} else {
if (argv[0][0] == 'y')
r = text_range_new(start, end);