vis

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

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

commit 631e10790f10e82e4aa35590dc40879cb1d78563
parent 0add2ec84cce3b650039da7e6b94144541f5eebf
Author: Marc André Tanner <mat@brain-dump.org>
Date:   Wed, 12 Apr 2017 18:30:50 +0200

sam: support %n count specifier matching every n-th selection

Diffstat:
Msam.c | 15+++++++++++++++
1 file changed, 15 insertions(+), 0 deletions(-)

diff --git a/sam.c b/sam.c @@ -62,6 +62,7 @@ struct Address { typedef struct { int start, end; /* interval [n,m] */ + bool mod; /* % every n-th match, implies n == m */ } Count; struct Command { @@ -649,6 +650,18 @@ static int parse_number(const char **s) { } static enum SamError parse_count(const char **s, Count *count) { + count->mod = **s == '%'; + + if (count->mod) { + (*s)++; + int n = parse_number(s); + if (!n) + return SAM_ERR_COUNT; + count->start = n; + count->end = n; + return SAM_ERR_OK; + } + const char *before = *s; if (!(count->start = parse_number(s)) && *s != before) return SAM_ERR_COUNT; @@ -1066,6 +1079,8 @@ static Filerange address_evaluate(Address *addr, File *file, Filerange *range, i static bool count_evaluate(Command *cmd) { Count *count = &cmd->count; + if (count->mod) + return count->start ? cmd->iteration % count->start == 0 : true; return count->start <= cmd->iteration && cmd->iteration <= count->end; }