From 068cff107e7147ba9dc8fb723a07a6d0a4bd80dd Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Tue, 11 Sep 2018 14:36:30 -0400 Subject: Add urlOpenMatch --- url.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) (limited to 'url.c') diff --git a/url.c b/url.c index 902128a..a398778 100644 --- a/url.c +++ b/url.c @@ -43,7 +43,7 @@ static struct { size_t end; } ring; -static void push(struct Tag tag, const char *url, size_t len) { +static void ringPush(struct Tag tag, const char *url, size_t len) { free(ring.buf[ring.end].url); ring.buf[ring.end].tag = tag.id; ring.buf[ring.end].url = strndup(url, len); @@ -51,13 +51,17 @@ static void push(struct Tag tag, const char *url, size_t len) { ring.end = (ring.end + 1) & (RingLen - 1); } +static struct Entry ringEntry(size_t i) { + return ring.buf[(ring.end + i) & (RingLen - 1)]; +} + void urlScan(struct Tag tag, const char *str) { while (str[0]) { size_t len = 1; for (size_t i = 0; i < SchemesLen; ++i) { if (strncmp(str, Schemes[i], strlen(Schemes[i]))) continue; len = strcspn(str, " >\""); - push(tag, str, len); + ringPush(tag, str, len); } str = &str[len]; } @@ -66,18 +70,29 @@ void urlScan(struct Tag tag, const char *str) { void urlList(struct Tag tag) { uiHide(); for (size_t i = 0; i < RingLen; ++i) { - struct Entry entry = ring.buf[(ring.end + i) & (RingLen - 1)]; + struct Entry entry = ringEntry(i); if (!entry.url || entry.tag != tag.id) continue; printf("%s\n", entry.url); } } -void urlOpen(struct Tag tag, size_t at, size_t to) { +void urlOpenMatch(struct Tag tag, const char *substr) { + for (size_t i = RingLen - 1; i < RingLen; --i) { + struct Entry entry = ringEntry(i); + if (!entry.url || entry.tag != tag.id) continue; + if (!strstr(entry.url, substr)) continue; + char *argv[] = { "open", entry.url, NULL }; + eventPipe(argv); + break; + } +} + +void urlOpenRange(struct Tag tag, size_t at, size_t to) { size_t argc = 1; char *argv[2 + RingLen] = { "open" }; size_t tagIndex = 0; for (size_t i = RingLen - 1; i < RingLen; --i) { - struct Entry entry = ring.buf[(ring.end + i) & (RingLen - 1)]; + struct Entry entry = ringEntry(i); if (!entry.url || entry.tag != tag.id) continue; if (tagIndex >= at && tagIndex < to) argv[argc++] = entry.url; tagIndex++; -- cgit 1.4.1-2-gfad0