aboutsummaryrefslogtreecommitdiff
path: root/url-mode.c
diff options
context:
space:
mode:
authorDaniel Eklöf <daniel@ekloef.se>2025-02-05 12:47:06 +0000
committerDaniel Eklöf <daniel@ekloef.se>2025-02-05 12:47:06 +0000
commitdd01783f88902b8e33820c838fc4e6aba122b578 (patch)
tree627a273b617d4c7dd1400f45c5e35db08e526830 /url-mode.c
parentbox-drawings: handle architecture with soft-float (diff)
parentchangelog: custom regex's (diff)
Merge branch 'regex-mode'
Closes #1386 Closes #1872
Diffstat (limited to 'url-mode.c')
-rw-r--r--url-mode.c325
1 files changed, 114 insertions, 211 deletions
diff --git a/url-mode.c b/url-mode.c
index 986860af..0101de19 100644
--- a/url-mode.c
+++ b/url-mode.c
@@ -4,6 +4,7 @@
#include <string.h>
#include <wctype.h>
#include <unistd.h>
+#include <regex.h>
#include <sys/stat.h>
#include <fcntl.h>
@@ -66,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(
@@ -83,6 +85,8 @@ spawn_url_launcher_with_token(struct terminal *term,
free(argv);
}
+ term->url_launch = NULL;
+
close(dev_null);
return ret;
}
@@ -106,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,
@@ -291,243 +297,135 @@ urls_input(struct seat *seat, struct terminal *term,
}
}
-static int
-c32cmp_single(const void *_a, const void *_b)
-{
- const char32_t *a = _a;
- const char32_t *b = _b;
- return *a - *b;
-}
+struct vline {
+ char *utf8;
+ size_t len; /* Length of utf8[] */
+ size_t sz; /* utf8[] allocated size */
+ struct coord *map; /* Maps utf8[ofs] to grid coordinates */
+};
static void
-auto_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)
{
- const struct config *conf = term->conf;
-
- const char32_t *uri_characters = conf->url.uri_characters;
- if (uri_characters == NULL)
- return;
-
- const size_t uri_characters_count = c32len(uri_characters);
- if (uri_characters_count == 0)
- return;
-
- size_t max_prot_len = conf->url.max_prot_len;
- char32_t proto_chars[max_prot_len];
- struct coord proto_start[max_prot_len];
- size_t proto_char_count = 0;
+ /*
+ * Use regcomp()+regexec() to find patterns.
+ *
+ * Since we can't feed regexec() one character at a time, and
+ * since it doesn't accept wide characters, we need to build utf8
+ * strings.
+ *
+ * Each string represents a logical line (i.e. handle line-wrap).
+ * To be able to map regex matches back to the grid, we store the
+ * grid coordinates of *each* character, in the line struct as
+ * well. This is offset based; utf8[ofs] has its grid coordinates
+ * in map[ofs.
+ */
- enum {
- STATE_PROTOCOL,
- STATE_URL,
- } state = STATE_PROTOCOL;
+ /* There is *at most* term->rows logical lines */
+ struct vline vlines[term->rows];
+ size_t vline_idx = 0;
- struct coord start = {-1, -1};
- char32_t url[term->cols * term->rows + 1];
- size_t len = 0;
+ memset(vlines, 0, sizeof(vlines));
+ struct vline *vline = &vlines[vline_idx];
- ssize_t parenthesis = 0;
- ssize_t brackets = 0;
- ssize_t ltgts = 0;
+ mbstate_t ps = {0};
for (int r = 0; r < term->rows; r++) {
const struct row *row = grid_row_in_view(term->grid, r);
for (int c = 0; c < term->cols; c++) {
const struct cell *cell = &row->cells[c];
+ const char32_t *wc = &cell->wc;
+ size_t wc_count = 1;
- if (cell->wc >= CELL_SPACER)
- continue;
-
- const char32_t *wcs = NULL;
- size_t wc_count = 0;
+ /* Expand combining characters */
+ if (wc[0] >= CELL_COMB_CHARS_LO && wc[0] <= CELL_COMB_CHARS_HI) {
+ const struct composed *composed =
+ composed_lookup(term->composed, wc[0] - CELL_COMB_CHARS_LO);
+ xassert(composed != NULL);
- if (cell->wc >= CELL_COMB_CHARS_LO && cell->wc <= CELL_COMB_CHARS_HI) {
- struct composed *composed =
- composed_lookup(term->composed, cell->wc - CELL_COMB_CHARS_LO);
- wcs = composed->chars;
+ wc = composed->chars;
wc_count = composed->count;
- } else {
- wcs = &cell->wc;
- wc_count = 1;
}
- for (size_t w_idx = 0; w_idx < wc_count; w_idx++) {
- char32_t wc = wcs[w_idx];
-
- switch (state) {
- case STATE_PROTOCOL:
- for (size_t i = 0; i < max_prot_len - 1; i++) {
- proto_chars[i] = proto_chars[i + 1];
- proto_start[i] = proto_start[i + 1];
- }
-
- if (proto_char_count >= max_prot_len)
- proto_char_count = max_prot_len - 1;
-
- proto_chars[max_prot_len - 1] = wc;
- proto_start[max_prot_len - 1] = (struct coord){c, r};
- proto_char_count++;
-
- for (size_t i = 0; i < conf->url.prot_count; i++) {
- size_t prot_len = c32len(conf->url.protocols[i]);
-
- if (proto_char_count < prot_len)
- continue;
-
- const char32_t *proto =
- &proto_chars[max_prot_len - prot_len];
+ /* Convert wide character to utf8 */
+ for (size_t i = 0; i < wc_count; i++) {
+ char buf[16];
+ size_t char_len = c32rtomb(buf, wc[i], &ps);
- if (c32ncasecmp(conf->url.protocols[i], proto, prot_len) ==
- 0) {
- state = STATE_URL;
- start = proto_start[max_prot_len - prot_len];
+ if (char_len == (size_t)-1)
+ continue;
- c32ncpy(url, proto, prot_len);
- len = prot_len;
+ for (size_t j = 0; j < char_len; j++) {
+ const size_t requires_size = vline->len + char_len;
- parenthesis = brackets = ltgts = 0;
- break;
+ /* Need to grow? Remember to save at least one byte for terminator */
+ if (vline->sz == 0 || requires_size > vline->sz - 1) {
+ const size_t new_size = requires_size * 2;
+ vline->utf8 = xreallocarray(vline->utf8, new_size, 1);
+ vline->map = xreallocarray(vline->map, new_size, sizeof(vline->map[0]));
+ vline->sz = new_size;
}
- }
- break;
- case STATE_URL: {
- const char32_t *match =
- bsearch(&wc, uri_characters, uri_characters_count,
- sizeof(uri_characters[0]), &c32cmp_single);
-
- bool emit_url = false;
-
- if (match == NULL) {
- /*
- * Character is not a valid URI character. Emit
- * the URL we've collected so far, *without*
- * including _this_ character.
- */
- emit_url = true;
- } else {
- xassert(*match == wc);
-
- switch (wc) {
- default:
- url[len++] = wc;
- break;
-
- case U'(':
- parenthesis++;
- url[len++] = wc;
- break;
-
- case U'[':
- brackets++;
- url[len++] = wc;
- break;
-
- case U'<':
- ltgts++;
- url[len++] = wc;
- break;
-
- case U')':
- if (--parenthesis < 0)
- emit_url = true;
- else
- url[len++] = wc;
- break;
-
- case U']':
- if (--brackets < 0)
- emit_url = true;
- else
- url[len++] = wc;
- break;
+ vline->utf8[vline->len + j] = buf[j];
+ vline->map[vline->len + j] = (struct coord){c, term->grid->view + r};
+ }
- case U'>':
- if (--ltgts < 0)
- emit_url = true;
- else
- url[len++] = wc;
- break;
- }
- }
+ vline->len += char_len;
+ }
+ }
- if (c >= term->cols - 1 && row->linebreak) {
- /*
- * Endpoint is inclusive, and we'll be subtracting
- * 1 from the column when emitting the URL.
- */
- c++;
- emit_url = true;
- }
+ if (row->linebreak) {
+ if (vline->len > 0) {
+ vline->utf8[vline->len++] = '\0';
+ ps = (mbstate_t){0};
- if (emit_url) {
- struct coord end = {c, r};
+ vline_idx++;
+ vline = &vlines[vline_idx];
+ }
+ }
+ }
- if (--end.col < 0) {
- end.row--;
- end.col = term->cols - 1;
- }
+ for (size_t i = 0; i < ALEN(vlines); i++) {
+ const struct vline *v = &vlines[i];
+ if (v->utf8 == NULL)
+ continue;;
- /* Heuristic to remove trailing characters that
- * are valid URL characters, but typically not at
- * the end of the URL */
- bool done = false;
- do {
- switch (url[len - 1]) {
- case U'.':
- case U',':
- case U':':
- case U';':
- case U'?':
- case U'!':
- case U'"':
- case U'\'':
- case U'%':
- len--;
- end.col--;
- if (end.col < 0) {
- end.row--;
- end.col = term->cols - 1;
- }
- break;
+ const char *search_string = v->utf8;
+ while (true) {
+ regmatch_t matches[preg->re_nsub + 1];
+ int r = regexec(preg, search_string, preg->re_nsub + 1, matches, 0);
- default:
- done = true;
- break;
- }
- } while (!done);
+ if (r == REG_NOMATCH)
+ break;
- url[len] = U'\0';
+ const size_t mlen = matches[1].rm_eo - matches[1].rm_so;
+ const size_t start = &search_string[matches[1].rm_so] - v->utf8;
+ const size_t end = start + mlen;
- start.row += term->grid->view;
- end.row += term->grid->view;
+ LOG_DBG(
+ "regex match at row %d: %.*srow/col = %dx%d",
+ matches[1].rm_so, (int)mlen, &search_string[matches[1].rm_so],
+ v->map[start].row, v->map[start].col);
- char *url_utf8 = ac32tombs(url);
- if (url_utf8 != NULL) {
- tll_push_back(
- *urls,
- ((struct url){.id = (uint64_t)rand() << 32 | rand(),
- .url = url_utf8,
- .range =
- {
- .start = start,
- .end = end,
- },
- .action = action,
- .osc8 = false}));
- }
+ tll_push_back(
+ *urls,
+ ((struct url){
+ .id = (uint64_t)rand() << 32 | rand(),
+ .url = xstrndup(&v->utf8[start], mlen),
+ .range = {
+ .start = v->map[start],
+ .end = v->map[end - 1], /* Inclusive */
+ },
+ .action = action,
+ .osc8 = false}));
- state = STATE_PROTOCOL;
- len = 0;
- parenthesis = brackets = ltgts = 0;
- }
- break;
- }
- }
- }
+ search_string += matches[0].rm_eo;
}
+
+ free(v->utf8);
+ free(v->map);
}
}
@@ -629,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);
- auto_detected(term, action, urls);
+ if (osc8)
+ osc8_uris(term, action, urls);
+ regex_detected(term, action, preg, urls);
remove_overlapping(urls, term->grid->num_cols);
}
@@ -816,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;
@@ -851,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};