config+url: add support for user-defined regex patterns

Users can now define their own regex patterns, and use them via key
bindings:

    [regex:foo]
    regex=foo(bar)?
    launch=path-to-script-or-application {match}

    [key-bindings]
    regex-launch=[foo] Control+Shift+q
    regex-copy=[foo] Control+Mod1+Shift+q

That is, add a section called 'regex:', followed by an
identifier. Define a regex and a launcher command line.

Add a key-binding, regex-launch and/or regex-copy (similar to
show-urls-launch and show-urls-copy), and connect them to the regex
with the "[regex-name]" syntax (similar to how the pipe-* bindings
work).
This commit is contained in:
Daniel Eklöf 2025-02-03 08:55:47 +01:00
parent f718cb3fb0
commit 051cd6ecfc
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
8 changed files with 310 additions and 48 deletions

View file

@ -67,12 +67,13 @@ spawn_url_launcher_with_token(struct terminal *term,
return false;
}
xassert(term->url_launch != NULL);
bool ret = false;
if (spawn_expand_template(
&term->conf->url.launch, 1,
(const char *[]){"url"},
(const char *[]){url},
term->url_launch, 2,
(const char *[]){"url", "match"},
(const char *[]){url, url},
&argc, &argv))
{
ret = spawn(
@ -84,6 +85,8 @@ spawn_url_launcher_with_token(struct terminal *term,
free(argv);
}
term->url_launch = NULL;
close(dev_null);
return ret;
}
@ -107,6 +110,8 @@ static bool
spawn_url_launcher(struct seat *seat, struct terminal *term, const char *url,
uint32_t serial)
{
xassert(term->url_launch != NULL);
struct spawn_activation_context *ctx = xmalloc(sizeof(*ctx));
*ctx = (struct spawn_activation_context){
.term = term,
@ -300,7 +305,8 @@ struct vline {
};
static void
regex_detected(const struct terminal *term, enum url_action action, url_list_t *urls)
regex_detected(const struct terminal *term, enum url_action action,
const regex_t *preg, url_list_t *urls)
{
/*
* Use regcomp()+regexec() to find patterns.
@ -381,8 +387,6 @@ regex_detected(const struct terminal *term, enum url_action action, url_list_t *
}
}
const regex_t *preg = &term->conf->url.preg;
for (size_t i = 0; i < ALEN(vlines); i++) {
const struct vline *v = &vlines[i];
if (v->utf8 == NULL)
@ -523,11 +527,13 @@ remove_overlapping(url_list_t *urls, int cols)
}
void
urls_collect(const struct terminal *term, enum url_action action, url_list_t *urls)
urls_collect(const struct terminal *term, enum url_action action,
const regex_t *preg, bool osc8, url_list_t *urls)
{
xassert(tll_length(term->urls) == 0);
osc8_uris(term, action, urls);
regex_detected(term, action, urls);
if (osc8)
osc8_uris(term, action, urls);
regex_detected(term, action, preg, urls);
remove_overlapping(urls, term->grid->num_cols);
}
@ -710,7 +716,7 @@ tag_cells_for_url(struct terminal *term, const struct url *url, bool value)
}
void
urls_render(struct terminal *term)
urls_render(struct terminal *term, const struct config_spawn_template *launch)
{
struct wl_window *win = term->window;
@ -745,6 +751,9 @@ urls_render(struct terminal *term)
/* Snapshot the current grid */
term->url_grid_snapshot = grid_snapshot(term->grid);
/* Remember which launcher to use */
term->url_launch = launch;
xassert(tll_length(win->urls) == 0);
tll_foreach(win->term->urls, it) {
struct wl_url url = {.url = &it->item};