Add urlOpenMatch

master
Curtis McEnroe 2018-09-11 14:36:30 -04:00
parent a0ff0d5c46
commit 068cff107e
No known key found for this signature in database
GPG Key ID: CEA2F97ADCFCD77C
4 changed files with 38 additions and 9 deletions

3
chat.h
View File

@ -158,7 +158,8 @@ void tabReject(void);
void urlScan(struct Tag tag, const char *str); void urlScan(struct Tag tag, const char *str);
void urlList(struct Tag tag); void urlList(struct Tag tag);
void urlOpen(struct Tag tag, size_t at, size_t to); void urlOpenMatch(struct Tag tag, const char *substr);
void urlOpenRange(struct Tag tag, size_t at, size_t to);
void logOpen(const char *path); void logOpen(const char *path);
void logFmt( void logFmt(

View File

@ -131,6 +131,14 @@ The default
.Ar range .Ar range
is 1. is 1.
. .
.It Ic /open Ar substring
Open the most recent URL
appearing in the current view
matching
.Ar substring
with
.Xr open 1 .
.
.It Ic /part .It Ic /part
Leave the current channel. Leave the current channel.
. .

11
input.c
View File

@ -14,6 +14,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <ctype.h>
#include <err.h> #include <err.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
@ -107,9 +108,13 @@ static void inputUrl(struct Tag tag, char *params) {
urlList(tag); urlList(tag);
} }
static void inputOpen(struct Tag tag, char *params) { static void inputOpen(struct Tag tag, char *params) {
size_t at = (params ? strtoul(strsep(&params, "-,"), NULL, 0) : 1); if (params && !isdigit(params[0])) {
size_t to = (params ? strtoul(params, NULL, 0) : at); urlOpenMatch(tag, params);
urlOpen(tag, at - 1, to); } else {
size_t at = (params ? strtoul(strsep(&params, "-,"), NULL, 0) : 1);
size_t to = (params ? strtoul(params, NULL, 0) : at);
urlOpenRange(tag, at - 1, to);
}
} }
static void inputView(struct Tag tag, char *params) { static void inputView(struct Tag tag, char *params) {

25
url.c
View File

@ -43,7 +43,7 @@ static struct {
size_t end; size_t end;
} ring; } 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); free(ring.buf[ring.end].url);
ring.buf[ring.end].tag = tag.id; ring.buf[ring.end].tag = tag.id;
ring.buf[ring.end].url = strndup(url, len); 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); 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) { void urlScan(struct Tag tag, const char *str) {
while (str[0]) { while (str[0]) {
size_t len = 1; size_t len = 1;
for (size_t i = 0; i < SchemesLen; ++i) { for (size_t i = 0; i < SchemesLen; ++i) {
if (strncmp(str, Schemes[i], strlen(Schemes[i]))) continue; if (strncmp(str, Schemes[i], strlen(Schemes[i]))) continue;
len = strcspn(str, " >\""); len = strcspn(str, " >\"");
push(tag, str, len); ringPush(tag, str, len);
} }
str = &str[len]; str = &str[len];
} }
@ -66,18 +70,29 @@ void urlScan(struct Tag tag, const char *str) {
void urlList(struct Tag tag) { void urlList(struct Tag tag) {
uiHide(); uiHide();
for (size_t i = 0; i < RingLen; ++i) { 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; if (!entry.url || entry.tag != tag.id) continue;
printf("%s\n", entry.url); 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; size_t argc = 1;
char *argv[2 + RingLen] = { "open" }; char *argv[2 + RingLen] = { "open" };
size_t tagIndex = 0; size_t tagIndex = 0;
for (size_t i = RingLen - 1; i < RingLen; --i) { 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 (!entry.url || entry.tag != tag.id) continue;
if (tagIndex >= at && tagIndex < to) argv[argc++] = entry.url; if (tagIndex >= at && tagIndex < to) argv[argc++] = entry.url;
tagIndex++; tagIndex++;