From 2cc84db97978388475504592c88911046bcd2eb6 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 31 Jan 2021 11:12:07 +0100 Subject: urls: initial support for detecting URLs and rendering jump-labels The jump labels work, but is currently hardcoded to use xdg-open --- url-mode.c | 337 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 337 insertions(+) create mode 100644 url-mode.c (limited to 'url-mode.c') diff --git a/url-mode.c b/url-mode.c new file mode 100644 index 00000000..aabb9032 --- /dev/null +++ b/url-mode.c @@ -0,0 +1,337 @@ +#include "url-mode.h" + +#include +#include + +#define LOG_MODULE "url-mode" +#define LOG_ENABLE_DBG 1 +#include "log.h" +#include "grid.h" +#include "spawn.h" +#include "terminal.h" +#include "util.h" +#include "xmalloc.h" + +static bool +execute_binding(struct seat *seat, struct terminal *term, + enum bind_action_url action, uint32_t serial) +{ + switch (action) { + case BIND_ACTION_URL_NONE: + return false; + + case BIND_ACTION_URL_CANCEL: + urls_reset(term); + return true; + + case BIND_ACTION_URL_COUNT: + return false; + + } + return true; +} + +void +urls_input(struct seat *seat, struct terminal *term, uint32_t key, + xkb_keysym_t sym, xkb_mod_mask_t mods, uint32_t serial) +{ + /* Key bindings */ + tll_foreach(seat->kbd.bindings.url, it) { + if (it->item.bind.mods != mods) + continue; + + /* Match symbol */ + if (it->item.bind.sym == sym) { + execute_binding(seat, term, it->item.action, serial); + return; + } + + /* Match raw key code */ + tll_foreach(it->item.bind.key_codes, code) { + if (code->item == key) { + execute_binding(seat, term, it->item.action, serial); + return; + } + } + } + + wchar_t wc = xkb_state_key_get_utf32(seat->kbd.xkb_state, key); + + /* + * Determine if this is a “valid” key. I.e. if there is an URL + * label with a key combo where this key is the next in + * sequence. + */ + + size_t seq_len = wcslen(term->url_keys); + + bool is_valid = false; + const struct url *match = NULL; + + tll_foreach(term->urls, it) { + const struct url *url = &it->item; + const size_t key_len = wcslen(it->item.key); + + if (key_len >= seq_len + 1 && + wcsncmp(url->key, term->url_keys, seq_len) == 0 && + url->key[seq_len] == wc) + { + is_valid = true; + if (key_len == seq_len + 1) { + match = url; + break; + } + } + } + + if (match) { + size_t chars = wcstombs(NULL, match->url, 0); + + char url_utf8[chars + 1]; + wcstombs(url_utf8, match->url, chars); + + spawn(term->reaper, term->cwd, (char *const[]){"xdg-open", url_utf8, NULL}, -1, -1, -1); + urls_reset(term); + } else if (is_valid) { + xassert(seq_len + 1 <= ALEN(term->url_keys)); + term->url_keys[seq_len] = wc; + } +} + +IGNORE_WARNING("-Wpedantic") + +static void +auto_detected(struct terminal *term) +{ + static const wchar_t *const prots[] = { + L"http://", + L"https://", + }; + + size_t max_prot_len = 0; + for (size_t i = 0; i < ALEN(prots); i++) { + size_t len = wcslen(prots[i]); + if (len > max_prot_len) + max_prot_len = len; + } + + wchar_t proto_chars[max_prot_len]; + struct coord proto_start[max_prot_len]; + size_t proto_char_count = 0; + + enum { + STATE_PROTOCOL, + STATE_URL, + } state = STATE_PROTOCOL; + + struct coord start = {-1, -1}; + wchar_t url[term->cols * term->rows + 1]; + size_t len = 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]; + wchar_t wc = towlower(cell->wc); + + 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--; + + proto_chars[proto_char_count] = wc; + proto_start[proto_char_count] = (struct coord){c, r}; + proto_char_count++; + + for (size_t i = 0; i < ALEN(prots); i++) { + size_t prot_len = wcslen(prots[i]); + + if (proto_char_count < prot_len) + continue; + + const wchar_t *proto = &proto_chars[max_prot_len - prot_len]; + + if (wcsncmp(prots[i], proto, prot_len) == 0) { + state = STATE_URL; + start = proto_start[max_prot_len - prot_len]; + + wcsncpy(url, proto, prot_len); + len = prot_len; + break; + } + } + break; + + case STATE_URL: { + // static const wchar_t allowed[] = + // L"abcdefghijklmnopqrstuvwxyz0123456789-._~:/?#[]@!$&'()*+,;="; + // static const wchar_t unwise[] = L"{}|\\^[]`"; + // static const wchar_t reserved[] = L";/?:@&=+$,"; + + switch (wc) { + case L'a'...L'z': + case L'0'...L'9': + case L'-': case L'.': case L'_': case L'~': case L':': + case L'/': case L'?': case L'#': case L'[': case L']': + case L'@': case L'!': case L'$': case L'&': case L'\'': + case L'(': case L')': case L'*': case L'+': case L',': + case L';': case L'=': case L'"': + url[len++] = wc; + break; + + default: { + /* Heuristic to remove trailing characters that + * are valid URL characters, but typically not at + * the end of the URL */ + bool done = false; + struct coord end = {c, r}; + + do { + switch (url[len - 1]) { + case L'.': case L',': case L':': case L';': case L'?': + case L'!': case L'"': case L'\'': + len--; + end.col--; + if (end.col < 0) { + end.row--; + end.col = term->cols - 1; + } + break; + + default: + done = true; + break; + } + } while (!done); + + url[len] = L'\0'; + + tll_push_back( + term->urls, + ((struct url){ + .url = xwcsdup(url), + .text = xwcsdup(L""), + .start = start, + .end = end})); + + state = STATE_PROTOCOL; + len = 0; + break; + } + } + break; + } + } + } + } +} + +UNIGNORE_WARNINGS + +void +urls_collect(struct terminal *term) +{ + xassert(tll_length(term->urls) == 0); + auto_detected(term); + + size_t count = tll_length(term->urls); + + /* Assign key combos */ + + static const wchar_t *const single[] = { + L"f", L"j", L"d", L"k", L"e", L"i", L"c", L"m", L"r", + L"u", L"s", L"l", L"w", L"o", L"x", L"a", L"q", L"p", + }; + + if (count < ALEN(single)) { + size_t idx = 0; + tll_foreach(term->urls, it) { + xassert(wcslen(single[idx]) < ALEN(it->item.key) - 1); + wcscpy(it->item.key, single[idx++]); + } + } else { + LOG_ERR("unimplemented: more URLs than %zu", ALEN(single)); + assert(false); + } + +#if defined(_DEBUG) && LOG_ENABLE_DBG + tll_foreach(term->urls, it) { + char url[1024]; + wcstombs(url, it->item.url, sizeof(url) - 1); + + char key[32]; + wcstombs(key, it->item.key, sizeof(key) - 1); + + LOG_DBG("URL: %s (%s)", url, key); + } +#endif + + struct wl_window *win = term->window; + struct wayland *wayl = term->wl; + + xassert(tll_length(win->urls) == 0); + tll_foreach(win->term->urls, it) { + struct wl_surface *surf = wl_compositor_create_surface(wayl->compositor); + wl_surface_set_user_data(surf, win); + + struct wl_subsurface *sub_surf = NULL; + + if (surf != NULL) { + sub_surf = wl_subcompositor_get_subsurface( + wayl->sub_compositor, surf, win->surface); + + if (sub_surf != NULL) + wl_subsurface_set_sync(sub_surf); + } + + if (surf == NULL || sub_surf == NULL) { + LOG_WARN("failed to create URL (sub)-surface"); + + if (surf != NULL) { + wl_surface_destroy(surf); + surf = NULL; + } + + if (sub_surf != NULL) { + wl_subsurface_destroy(sub_surf); + sub_surf = NULL; + } + } + + struct wl_url url = { + .url = &it->item, + .surf = surf, + .sub_surf = sub_surf, + }; + + tll_push_back(win->urls, url); + } +} + +void +urls_reset(struct terminal *term) +{ + if (term->window != NULL) { + tll_foreach(term->window->urls, it) { + if (it->item.sub_surf != NULL) + wl_subsurface_destroy(it->item.sub_surf); + if (it->item.surf != NULL) + wl_surface_destroy(it->item.surf); + } + tll_free(term->window->urls); + } + + tll_foreach(term->urls, it) { + free(it->item.url); + free(it->item.text); + } + tll_free(term->urls); + + memset(term->url_keys, 0, sizeof(term->url_keys)); +} -- cgit v1.3 From d75688b0bd804b7c5f84178bbc139982a2a0ab39 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 31 Jan 2021 11:38:56 +0100 Subject: url-mode: fix ‘n’ parameter to wcstombs() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- url-mode.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'url-mode.c') diff --git a/url-mode.c b/url-mode.c index aabb9032..0f23845c 100644 --- a/url-mode.c +++ b/url-mode.c @@ -87,12 +87,16 @@ urls_input(struct seat *seat, struct terminal *term, uint32_t key, if (match) { size_t chars = wcstombs(NULL, match->url, 0); - char url_utf8[chars + 1]; - wcstombs(url_utf8, match->url, chars); + if (chars != (size_t)-1) { + char url_utf8[chars + 1]; + wcstombs(url_utf8, match->url, chars + 1); + spawn(term->reaper, term->cwd, (char *const[]){"xdg-open", url_utf8, NULL}, -1, -1, -1); + } - spawn(term->reaper, term->cwd, (char *const[]){"xdg-open", url_utf8, NULL}, -1, -1, -1); urls_reset(term); - } else if (is_valid) { + } + + else if (is_valid) { xassert(seq_len + 1 <= ALEN(term->url_keys)); term->url_keys[seq_len] = wc; } -- cgit v1.3 From 2315aba458b662dcbb2ce4b8ceb3d9bfd14f4970 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 31 Jan 2021 11:39:23 +0100 Subject: url-mode: urls_reset() do an early return if we don’t have any URLs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- url-mode.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'url-mode.c') diff --git a/url-mode.c b/url-mode.c index 0f23845c..170be4c9 100644 --- a/url-mode.c +++ b/url-mode.c @@ -321,6 +321,9 @@ urls_collect(struct terminal *term) void urls_reset(struct terminal *term) { + if (likely(tll_length(term->urls) == 0)) + return; + if (term->window != NULL) { tll_foreach(term->window->urls, it) { if (it->item.sub_surf != NULL) -- cgit v1.3 From 9d8ec857cec426b4f0a3277a63abeb1a420e6107 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 31 Jan 2021 14:43:26 +0100 Subject: url-mode: use ‘url-launch’ from config to open URLs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- url-mode.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'url-mode.c') diff --git a/url-mode.c b/url-mode.c index 170be4c9..a4fea23f 100644 --- a/url-mode.c +++ b/url-mode.c @@ -90,7 +90,22 @@ urls_input(struct seat *seat, struct terminal *term, uint32_t key, if (chars != (size_t)-1) { char url_utf8[chars + 1]; wcstombs(url_utf8, match->url, chars + 1); - spawn(term->reaper, term->cwd, (char *const[]){"xdg-open", url_utf8, NULL}, -1, -1, -1); + + size_t argc; + char **argv; + + if (spawn_expand_template( + &term->conf->url_launch, 1, + (const char *[]){"url"}, + (const char *[]){url_utf8}, + &argc, &argv)) + { + spawn(term->reaper, term->cwd, argv, -1, -1, -1); + + for (size_t i = 0; i < argc; i++) + free(argv[i]); + free(argv); + } } urls_reset(term); -- cgit v1.3 From f61f7c131fc896af23437880778556e506e73018 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Mon, 1 Feb 2021 09:55:18 +0100 Subject: url-mode: auto-detect: heuristics for parenthesis and brackets MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit While parenthesis and brackets _are_ valid URL characters, there are many times when we do *not* want them to be part of the URL. For example, in markdown we write “[text](url)”, or even “[![alt-text](url-1)](url-2)”. Here, the URLs are clearly *not* “url)” or “url-1)](url2)”. --- url-mode.c | 47 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 7 deletions(-) (limited to 'url-mode.c') diff --git a/url-mode.c b/url-mode.c index a4fea23f..fecbaa6b 100644 --- a/url-mode.c +++ b/url-mode.c @@ -147,6 +147,9 @@ auto_detected(struct terminal *term) wchar_t url[term->cols * term->rows + 1]; size_t len = 0; + ssize_t parenthesis = 0; + ssize_t brackets = 0; + for (int r = 0; r < term->rows; r++) { const struct row *row = grid_row_in_view(term->grid, r); @@ -182,6 +185,8 @@ auto_detected(struct terminal *term) wcsncpy(url, proto, prot_len); len = prot_len; + + parenthesis = brackets = 0; break; } } @@ -193,18 +198,47 @@ auto_detected(struct terminal *term) // static const wchar_t unwise[] = L"{}|\\^[]`"; // static const wchar_t reserved[] = L";/?:@&=+$,"; + bool emit_url = false; switch (wc) { case L'a'...L'z': case L'0'...L'9': case L'-': case L'.': case L'_': case L'~': case L':': - case L'/': case L'?': case L'#': case L'[': case L']': - case L'@': case L'!': case L'$': case L'&': case L'\'': - case L'(': case L')': case L'*': case L'+': case L',': - case L';': case L'=': case L'"': + case L'/': case L'?': case L'#': case L'@': case L'!': + case L'$': case L'&': case L'\'': case L'*': case L'+': + case L',': case L';': case L'=': case L'"': + url[len++] = wc; + break; + + case L'(': + parenthesis++; url[len++] = wc; break; - default: { + case L'[': + brackets++; + url[len++] = wc; + break; + + case L')': + if (--parenthesis < 0) + emit_url = true; + else + url[len++] = wc; + break; + + case L']': + if (--brackets < 0) + emit_url = true; + else + url[len++] = wc; + break; + + default: + emit_url = true; + break; + } + + if (emit_url) { /* Heuristic to remove trailing characters that * are valid URL characters, but typically not at * the end of the URL */ @@ -241,8 +275,7 @@ auto_detected(struct terminal *term) state = STATE_PROTOCOL; len = 0; - break; - } + parenthesis = brackets = 0; } break; } -- cgit v1.3 From 65caa33084426cb00df96eda1089768b4251b811 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Mon, 1 Feb 2021 10:40:15 +0100 Subject: url-mode: auto-detect: don’t line-wrap URL is row isn’t line-wrapped MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- url-mode.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'url-mode.c') diff --git a/url-mode.c b/url-mode.c index fecbaa6b..78291685 100644 --- a/url-mode.c +++ b/url-mode.c @@ -238,6 +238,9 @@ auto_detected(struct terminal *term) break; } + if (c >= term->cols - 1 && row->linebreak) + emit_url = true; + if (emit_url) { /* Heuristic to remove trailing characters that * are valid URL characters, but typically not at -- cgit v1.3 From 93181649b32249f572bc4c6892c99155186fc714 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 4 Feb 2021 20:55:08 +0100 Subject: config: add show-urls-copy action This works just like show-urls-launch, except that instead of opening the URL (typically using xdg-open), it is placed in the clipboard when activated. --- config.c | 1 + doc/foot.ini.5.scd | 5 +++++ foot.ini | 1 + input.c | 10 ++++++++-- terminal.h | 2 ++ url-mode.c | 52 ++++++++++++++++++++++++++++++++++------------------ url-mode.h | 2 +- wayland.h | 1 + 8 files changed, 53 insertions(+), 21 deletions(-) (limited to 'url-mode.c') diff --git a/config.c b/config.c index 0ea45b02..74cff1f3 100644 --- a/config.c +++ b/config.c @@ -75,6 +75,7 @@ static const char *const binding_action_map[] = { [BIND_ACTION_PIPE_SCROLLBACK] = "pipe-scrollback", [BIND_ACTION_PIPE_VIEW] = "pipe-visible", [BIND_ACTION_PIPE_SELECTED] = "pipe-selected", + [BIND_ACTION_SHOW_URLS_COPY] = "show-urls-copy", [BIND_ACTION_SHOW_URLS_LAUNCH] = "show-urls-launch", /* Mouse-specific actions */ diff --git a/doc/foot.ini.5.scd b/doc/foot.ini.5.scd index fe158279..727b81f1 100644 --- a/doc/foot.ini.5.scd +++ b/doc/foot.ini.5.scd @@ -500,6 +500,11 @@ e.g. *search-start=none*. a jump label with a key sequence that will open the URL. Default: _Control+Shift+U_. +*show-urls-copy* + Enters URL mode, where all currently visible URLs are tagged with + a jump label with a key sequence that will place the URL in the + clipboard. Default: _none_. + # SECTION: search-bindings diff --git a/foot.ini b/foot.ini index 692eac4f..4ed3d64b 100644 --- a/foot.ini +++ b/foot.ini @@ -98,6 +98,7 @@ # pipe-scrollback=[sh -c "xurls | bemenu | xargs -r firefox"] none # pipe-selected=[xargs -r firefox] none # show-urls-launch=Control+Shift+U +# show-urls-copy=none [search-bindings] # cancel=Control+g Escape diff --git a/input.c b/input.c index dfbf4fa3..65670aa3 100644 --- a/input.c +++ b/input.c @@ -272,12 +272,18 @@ execute_binding(struct seat *seat, struct terminal *term, return true; } - case BIND_ACTION_SHOW_URLS_LAUNCH: + case BIND_ACTION_SHOW_URLS_COPY: + case BIND_ACTION_SHOW_URLS_LAUNCH: { xassert(!urls_mode_is_active(term)); - urls_collect(term); + enum url_action url_action = action == BIND_ACTION_SHOW_URLS_COPY + ? URL_ACTION_COPY + : URL_ACTION_LAUNCH; + + urls_collect(term, url_action); render_refresh_urls(term); return true; + } case BIND_ACTION_SELECT_BEGIN: selection_start( diff --git a/terminal.h b/terminal.h index 24183c7b..b58cd137 100644 --- a/terminal.h +++ b/terminal.h @@ -223,12 +223,14 @@ enum term_surface { typedef tll(struct ptmx_buffer) ptmx_buffer_list_t; +enum url_action { URL_ACTION_COPY, URL_ACTION_LAUNCH }; struct url { wchar_t *url; wchar_t *text; wchar_t key[4]; struct coord start; struct coord end; + enum url_action action; }; struct terminal { diff --git a/url-mode.c b/url-mode.c index 78291685..79c6c17c 100644 --- a/url-mode.c +++ b/url-mode.c @@ -7,6 +7,7 @@ #define LOG_ENABLE_DBG 1 #include "log.h" #include "grid.h" +#include "selection.h" #include "spawn.h" #include "terminal.h" #include "util.h" @@ -88,24 +89,38 @@ urls_input(struct seat *seat, struct terminal *term, uint32_t key, size_t chars = wcstombs(NULL, match->url, 0); if (chars != (size_t)-1) { - char url_utf8[chars + 1]; + char *url_utf8 = malloc(chars + 1); wcstombs(url_utf8, match->url, chars + 1); - size_t argc; - char **argv; - - if (spawn_expand_template( - &term->conf->url_launch, 1, - (const char *[]){"url"}, - (const char *[]){url_utf8}, - &argc, &argv)) - { - spawn(term->reaper, term->cwd, argv, -1, -1, -1); + switch (match->action) { + case URL_ACTION_COPY: + if (text_to_clipboard(seat, term, url_utf8, seat->kbd.serial)) { + /* Now owned by our clipboard “manager” */ + url_utf8 = NULL; + } + break; - for (size_t i = 0; i < argc; i++) - free(argv[i]); - free(argv); + case URL_ACTION_LAUNCH: { + size_t argc; + char **argv; + + if (spawn_expand_template( + &term->conf->url_launch, 1, + (const char *[]){"url"}, + (const char *[]){url_utf8}, + &argc, &argv)) + { + spawn(term->reaper, term->cwd, argv, -1, -1, -1); + + for (size_t i = 0; i < argc; i++) + free(argv[i]); + free(argv); + } + break; + } } + + free(url_utf8); } urls_reset(term); @@ -120,7 +135,7 @@ urls_input(struct seat *seat, struct terminal *term, uint32_t key, IGNORE_WARNING("-Wpedantic") static void -auto_detected(struct terminal *term) +auto_detected(struct terminal *term, enum url_action action) { static const wchar_t *const prots[] = { L"http://", @@ -274,7 +289,8 @@ auto_detected(struct terminal *term) .url = xwcsdup(url), .text = xwcsdup(L""), .start = start, - .end = end})); + .end = end, + .action = action})); state = STATE_PROTOCOL; len = 0; @@ -290,10 +306,10 @@ auto_detected(struct terminal *term) UNIGNORE_WARNINGS void -urls_collect(struct terminal *term) +urls_collect(struct terminal *term, enum url_action action) { xassert(tll_length(term->urls) == 0); - auto_detected(term); + auto_detected(term, action); size_t count = tll_length(term->urls); diff --git a/url-mode.h b/url-mode.h index 150a0222..94c1e675 100644 --- a/url-mode.h +++ b/url-mode.h @@ -11,7 +11,7 @@ static inline bool urls_mode_is_active(const struct terminal *term) return tll_length(term->urls) > 0; } -void urls_collect(struct terminal *term); +void urls_collect(struct terminal *term, enum url_action action); void urls_reset(struct terminal *term); void urls_input(struct seat *seat, struct terminal *term, uint32_t key, diff --git a/wayland.h b/wayland.h index 3ad7b862..3c160b7c 100644 --- a/wayland.h +++ b/wayland.h @@ -49,6 +49,7 @@ enum bind_action_normal { BIND_ACTION_PIPE_SCROLLBACK, BIND_ACTION_PIPE_VIEW, BIND_ACTION_PIPE_SELECTED, + BIND_ACTION_SHOW_URLS_COPY, BIND_ACTION_SHOW_URLS_LAUNCH, /* Mouse specific actions - i.e. they require a mouse coordinate */ -- cgit v1.3 From 6b7003bcc385bda1bcf39aca441a02d122f8ab06 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 4 Feb 2021 20:57:19 +0100 Subject: url-mode: auto-detect: don’t store the lower-cased URL; use original casing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- url-mode.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'url-mode.c') diff --git a/url-mode.c b/url-mode.c index 79c6c17c..8d31908c 100644 --- a/url-mode.c +++ b/url-mode.c @@ -221,31 +221,31 @@ auto_detected(struct terminal *term, enum url_action action) case L'/': case L'?': case L'#': case L'@': case L'!': case L'$': case L'&': case L'\'': case L'*': case L'+': case L',': case L';': case L'=': case L'"': - url[len++] = wc; + url[len++] = cell->wc; break; case L'(': parenthesis++; - url[len++] = wc; + url[len++] = cell->wc; break; case L'[': brackets++; - url[len++] = wc; + url[len++] = cell->wc; break; case L')': if (--parenthesis < 0) emit_url = true; else - url[len++] = wc; + url[len++] = cell->wc; break; case L']': if (--brackets < 0) emit_url = true; else - url[len++] = wc; + url[len++] = cell->wc; break; default: -- cgit v1.3 From 607ee63b77138acd3c366e1a746b6b97e03e0da4 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 4 Feb 2021 21:08:49 +0100 Subject: url-mode: auto-detect: use wcsncasecmp() instead of towlower() When matching the URI scheme, use wcsncasecmp() when comparing the strings, instead of calling towlower() on each cell. --- url-mode.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'url-mode.c') diff --git a/url-mode.c b/url-mode.c index 8d31908c..1b6dc9d8 100644 --- a/url-mode.c +++ b/url-mode.c @@ -170,7 +170,7 @@ auto_detected(struct terminal *term, enum url_action action) for (int c = 0; c < term->cols; c++) { const struct cell *cell = &row->cells[c]; - wchar_t wc = towlower(cell->wc); + wchar_t wc = cell->wc; switch (state) { case STATE_PROTOCOL: @@ -194,7 +194,7 @@ auto_detected(struct terminal *term, enum url_action action) const wchar_t *proto = &proto_chars[max_prot_len - prot_len]; - if (wcsncmp(prots[i], proto, prot_len) == 0) { + if (wcsncasecmp(prots[i], proto, prot_len) == 0) { state = STATE_URL; start = proto_start[max_prot_len - prot_len]; @@ -216,36 +216,37 @@ auto_detected(struct terminal *term, enum url_action action) bool emit_url = false; switch (wc) { case L'a'...L'z': + case L'A'...L'Z': case L'0'...L'9': case L'-': case L'.': case L'_': case L'~': case L':': case L'/': case L'?': case L'#': case L'@': case L'!': case L'$': case L'&': case L'\'': case L'*': case L'+': case L',': case L';': case L'=': case L'"': - url[len++] = cell->wc; + url[len++] = wc; break; case L'(': parenthesis++; - url[len++] = cell->wc; + url[len++] = wc; break; case L'[': brackets++; - url[len++] = cell->wc; + url[len++] = wc; break; case L')': if (--parenthesis < 0) emit_url = true; else - url[len++] = cell->wc; + url[len++] = wc; break; case L']': if (--brackets < 0) emit_url = true; else - url[len++] = cell->wc; + url[len++] = wc; break; default: -- cgit v1.3 From ef3ce530baf48bdf52bf6f1a9a25c59c0fb46bbc Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 4 Feb 2021 21:19:30 +0100 Subject: url-mode: refactor: break out URL activation to a separate function --- url-mode.c | 79 +++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 42 insertions(+), 37 deletions(-) (limited to 'url-mode.c') diff --git a/url-mode.c b/url-mode.c index 1b6dc9d8..f38026b4 100644 --- a/url-mode.c +++ b/url-mode.c @@ -32,6 +32,47 @@ execute_binding(struct seat *seat, struct terminal *term, return true; } +static void +activate_url(struct seat *seat, struct terminal *term, const struct url *url) +{ + size_t chars = wcstombs(NULL, url->url, 0); + + if (chars != (size_t)-1) { + char *url_utf8 = malloc(chars + 1); + wcstombs(url_utf8, url->url, chars + 1); + + switch (url->action) { + case URL_ACTION_COPY: + if (text_to_clipboard(seat, term, url_utf8, seat->kbd.serial)) { + /* Now owned by our clipboard “manager” */ + url_utf8 = NULL; + } + break; + + case URL_ACTION_LAUNCH: { + size_t argc; + char **argv; + + if (spawn_expand_template( + &term->conf->url_launch, 1, + (const char *[]){"url"}, + (const char *[]){url_utf8}, + &argc, &argv)) + { + spawn(term->reaper, term->cwd, argv, -1, -1, -1); + + for (size_t i = 0; i < argc; i++) + free(argv[i]); + free(argv); + } + break; + } + } + + free(url_utf8); + } +} + void urls_input(struct seat *seat, struct terminal *term, uint32_t key, xkb_keysym_t sym, xkb_mod_mask_t mods, uint32_t serial) @@ -86,43 +127,7 @@ urls_input(struct seat *seat, struct terminal *term, uint32_t key, } if (match) { - size_t chars = wcstombs(NULL, match->url, 0); - - if (chars != (size_t)-1) { - char *url_utf8 = malloc(chars + 1); - wcstombs(url_utf8, match->url, chars + 1); - - switch (match->action) { - case URL_ACTION_COPY: - if (text_to_clipboard(seat, term, url_utf8, seat->kbd.serial)) { - /* Now owned by our clipboard “manager” */ - url_utf8 = NULL; - } - break; - - case URL_ACTION_LAUNCH: { - size_t argc; - char **argv; - - if (spawn_expand_template( - &term->conf->url_launch, 1, - (const char *[]){"url"}, - (const char *[]){url_utf8}, - &argc, &argv)) - { - spawn(term->reaper, term->cwd, argv, -1, -1, -1); - - for (size_t i = 0; i < argc; i++) - free(argv[i]); - free(argv); - } - break; - } - } - - free(url_utf8); - } - + activate_url(seat, term, match); urls_reset(term); } -- cgit v1.3 From a578faf494981fdd042dd49e298eeeafdc646499 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 6 Feb 2021 11:45:58 +0100 Subject: url-mode: make the end coordinate *inclusive* --- url-mode.c | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'url-mode.c') diff --git a/url-mode.c b/url-mode.c index f38026b4..4feea2df 100644 --- a/url-mode.c +++ b/url-mode.c @@ -269,6 +269,11 @@ auto_detected(struct terminal *term, enum url_action action) bool done = false; struct coord end = {c, r}; + if (--end.col < 0) { + end.row--; + end.col = term->cols - 1; + } + do { switch (url[len - 1]) { case L'.': case L',': case L':': case L';': case L'?': -- cgit v1.3 From 6726494f4c2836a5ed1c4611c181e4a57a4adbf1 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 6 Feb 2021 11:47:59 +0100 Subject: url-mode: store absolute row numbers in start/end coordinates MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This allows us to update the jump label positions when the viewport changes. This in turn allows us to stay in URL mode while the user is using the mouse to scroll in the scrollback history. Scrolling with the keyboard is currently not possible, since input handling in URL mode does not recognize “regular” key bindings. We _could_ add scrollback up/down bindings to URL mode too, but lets not, for the time being. (Note: an alternative to this patch is to disallow mouse scrolling too. Then we could have kept the URL start/end as viewport local coordinates). --- commands.c | 2 ++ render.c | 32 +++++++++++++++++++++++++++++--- url-mode.c | 3 +++ 3 files changed, 34 insertions(+), 3 deletions(-) (limited to 'url-mode.c') diff --git a/commands.c b/commands.c index ac91ab19..01ad4e1e 100644 --- a/commands.c +++ b/commands.c @@ -83,6 +83,7 @@ cmd_scrollback_up(struct terminal *term, int rows) } else term_damage_view(term); + render_refresh_urls(term); render_refresh(term); } @@ -157,5 +158,6 @@ cmd_scrollback_down(struct terminal *term, int rows) } else term_damage_view(term); + render_refresh_urls(term); render_refresh(term); } diff --git a/render.c b/render.c index d04fd5d4..fff280fc 100644 --- a/render.c +++ b/render.c @@ -2524,6 +2524,15 @@ render_urls(struct terminal *term) struct wl_window *win = term->window; xassert(tll_length(win->urls) > 0); + /* Calculate view start, counted from the *current* scrollback start */ + const int scrollback_end + = (term->grid->offset + term->rows) & (term->grid->num_rows - 1); + const int view_start + = (term->grid->view + - scrollback_end + + term->grid->num_rows) & (term->grid->num_rows - 1); + const int view_end = view_start + term->rows - 1; + tll_foreach(win->urls, it) { const struct url *url = it->item.url; const wchar_t *text = url->text; @@ -2535,6 +2544,18 @@ render_urls(struct terminal *term) if (surf == NULL || sub_surf == NULL) continue; + const struct coord *pos = &url->start; + const int _row + = (pos->row + - scrollback_end + + term->grid->num_rows) & (term->grid->num_rows - 1); + + if (_row < view_start || _row > view_end) { + wl_surface_attach(surf, NULL, 0, 0); + wl_surface_commit(surf); + continue; + } + size_t text_len = wcslen(text); size_t chars = wcslen(key) + (text_len > 0 ? 3 + text_len : 0); @@ -2562,9 +2583,14 @@ render_urls(struct terminal *term) struct buffer *buf = shm_get_buffer( term->wl->shm, width, height, shm_cookie_url(url), false, 1); - const struct coord *pos = &url->start; - int x = pos->col * term->cell_width - 15 * term->cell_width / 10; - int y = pos->row * term->cell_height - 5 * term->cell_height / 10; + int col = pos->col; + int row = pos->row - term->grid->view; + while (row < 0) + row += term->grid->num_rows; + row &= (term->grid->num_rows - 1); + + int x = col * term->cell_width - 15 * term->cell_width / 10; + int y = row * term->cell_height - 5 * term->cell_height / 10; if (x < 0) x = 0; diff --git a/url-mode.c b/url-mode.c index 4feea2df..8f3b867e 100644 --- a/url-mode.c +++ b/url-mode.c @@ -294,6 +294,9 @@ auto_detected(struct terminal *term, enum url_action action) url[len] = L'\0'; + start.row += term->grid->view; + end.row += term->grid->view; + tll_push_back( term->urls, ((struct url){ -- cgit v1.3 From 2c10a147ea0e471f12facfa11913272d42163f76 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 6 Feb 2021 11:51:58 +0100 Subject: url-mode: underline URLs using the color from colors.urls MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is implemented by allocating one of the (few!) remaining bits in the cells’ attribute struct to indicate the cell should be “URL highlighted”. render_cell() looks at this bit and draws an underline using the color from colors.urls (defaults to regular3 - i.e. yellow). A new function, url_tag_cells(), iterates the currently detected URLs and sets the new ‘url’ attribute flag on the affected cells. Note: this is done in a separate function to keep urls_collect() free from as many dependencies as possible. urls_reset() is updated to *clear* the ‘url’ flag (and thus implicitly also results in a grid refresh, _if_ there were any URLs). We now exit URL mode on *any* client application input. This needs to be so since we can’t know if the URLs we previously detected are still valid. --- input.c | 1 + render.c | 9 +++++++++ terminal.c | 2 ++ terminal.h | 3 ++- url-mode.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ url-mode.h | 1 + 6 files changed, 61 insertions(+), 1 deletion(-) (limited to 'url-mode.c') diff --git a/input.c b/input.c index 10507aa8..7ba8e8e0 100644 --- a/input.c +++ b/input.c @@ -281,6 +281,7 @@ execute_binding(struct seat *seat, struct terminal *term, : URL_ACTION_LAUNCH; urls_collect(term, url_action); + urls_tag_cells(term); render_refresh_urls(term); return true; } diff --git a/render.c b/render.c index fff280fc..4e41f48a 100644 --- a/render.c +++ b/render.c @@ -616,6 +616,15 @@ render_cell(struct terminal *term, pixman_image_t *pix, if (cell->attrs.strikethrough) draw_strikeout(term, pix, font, &fg, x, y, cell_cols); + if (unlikely(cell->attrs.url)) { + pixman_color_t url_color = color_hex_to_pixman( + term->conf->colors.use_custom.url + ? term->conf->colors.url + : term->colors.table[3] + ); + draw_underline(term, pix, font, &url_color, x, y, cell_cols); + } + draw_cursor: if (has_cursor && (term->cursor_style != CURSOR_BLOCK || !term->kbd_focus)) draw_cursor(term, cell, font, pix, &fg, &bg, x, y, cell_cols); diff --git a/terminal.c b/terminal.c index c67dda58..91a442ec 100644 --- a/terminal.c +++ b/terminal.c @@ -228,6 +228,8 @@ fdm_ptmx(struct fdm *fdm, int fd, int events, void *data) cursor_blink_rearm_timer(term); } + urls_reset(term); + uint8_t buf[24 * 1024]; ssize_t count = sizeof(buf); diff --git a/terminal.h b/terminal.h index c6a618bf..bb98efc2 100644 --- a/terminal.h +++ b/terminal.h @@ -40,7 +40,8 @@ struct attributes { uint32_t have_fg:1; uint32_t have_bg:1; uint32_t selected:2; - uint32_t reserved:3; + uint32_t url:1; + uint32_t reserved:2; uint32_t bg:24; }; static_assert(sizeof(struct attributes) == 8, "bad size"); diff --git a/url-mode.c b/url-mode.c index 8f3b867e..e418eb7e 100644 --- a/url-mode.c +++ b/url-mode.c @@ -7,6 +7,7 @@ #define LOG_ENABLE_DBG 1 #include "log.h" #include "grid.h" +#include "render.h" #include "selection.h" #include "spawn.h" #include "terminal.h" @@ -399,6 +400,49 @@ urls_collect(struct terminal *term, enum url_action action) } } +static void +tag_cells_for_url(struct terminal *term, const struct url *url, bool value) +{ + const struct coord *start = &url->start; + const struct coord *end = &url->end; + + size_t end_r = end->row & (term->grid->num_rows - 1); + + size_t r = start->row & (term->grid->num_rows - 1); + size_t c = start->col; + + struct row *row = term->grid->rows[r]; + row->dirty = true; + + while (true) { + struct cell *cell = &row->cells[c]; + cell->attrs.url = value; + cell->attrs.clean = 0; + + if (r == end_r && c == end->col) + break; + + if (++c >= term->cols) { + r = (r + 1) & (term->grid->num_rows - 1); + c = 0; + + row = term->grid->rows[r]; + row->dirty = true; + } + } +} + +void +urls_tag_cells(struct terminal *term) +{ + if (unlikely(tll_length(term->urls)) == 0) + return; + + tll_foreach(term->urls, it) + tag_cells_for_url(term, &it->item, true); + render_refresh(term); +} + void urls_reset(struct terminal *term) { @@ -416,10 +460,12 @@ urls_reset(struct terminal *term) } tll_foreach(term->urls, it) { + tag_cells_for_url(term, &it->item, false); free(it->item.url); free(it->item.text); } tll_free(term->urls); memset(term->url_keys, 0, sizeof(term->url_keys)); + render_refresh(term); } diff --git a/url-mode.h b/url-mode.h index 94c1e675..6651e119 100644 --- a/url-mode.h +++ b/url-mode.h @@ -12,6 +12,7 @@ static inline bool urls_mode_is_active(const struct terminal *term) } void urls_collect(struct terminal *term, enum url_action action); +void urls_tag_cells(struct terminal *term); void urls_reset(struct terminal *term); void urls_input(struct seat *seat, struct terminal *term, uint32_t key, -- cgit v1.3 From a988138492ba39d507f644bbb0b6d452a864ca79 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 6 Feb 2021 20:01:52 +0100 Subject: url-mode: urls_collect(): URL list pointer as an argument --- input.c | 6 ++-- terminal.h | 3 +- url-mode.c | 98 ++++++++++++++++++++++++++++++++------------------------------ url-mode.h | 7 +++-- 4 files changed, 60 insertions(+), 54 deletions(-) (limited to 'url-mode.c') diff --git a/input.c b/input.c index 7ba8e8e0..69fce248 100644 --- a/input.c +++ b/input.c @@ -280,9 +280,9 @@ execute_binding(struct seat *seat, struct terminal *term, ? URL_ACTION_COPY : URL_ACTION_LAUNCH; - urls_collect(term, url_action); - urls_tag_cells(term); - render_refresh_urls(term); + urls_collect(term, url_action, &term->urls); + urls_assign_key_combos(&term->urls); + urls_render(term); return true; } diff --git a/terminal.h b/terminal.h index bb98efc2..b0084f63 100644 --- a/terminal.h +++ b/terminal.h @@ -234,6 +234,7 @@ struct url { struct coord end; enum url_action action; }; +typedef tll(struct url) url_list_t; struct terminal { struct fdm *fdm; @@ -513,7 +514,7 @@ struct terminal { unsigned max_height; /* Maximum image height, in pixels */ } sixel; - tll(struct url) urls; + url_list_t urls; wchar_t url_keys[5]; #if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED diff --git a/url-mode.c b/url-mode.c index e418eb7e..e770aa9b 100644 --- a/url-mode.c +++ b/url-mode.c @@ -141,7 +141,7 @@ urls_input(struct seat *seat, struct terminal *term, uint32_t key, IGNORE_WARNING("-Wpedantic") static void -auto_detected(struct terminal *term, enum url_action action) +auto_detected(const struct terminal *term, enum url_action action, url_list_t *urls) { static const wchar_t *const prots[] = { L"http://", @@ -299,7 +299,7 @@ auto_detected(struct terminal *term, enum url_action action) end.row += term->grid->view; tll_push_back( - term->urls, + *urls, ((struct url){ .url = xwcsdup(url), .text = xwcsdup(L""), @@ -321,12 +321,16 @@ auto_detected(struct terminal *term, enum url_action action) UNIGNORE_WARNINGS void -urls_collect(struct terminal *term, enum url_action action) +urls_collect(const struct terminal *term, enum url_action action, url_list_t *urls) { xassert(tll_length(term->urls) == 0); - auto_detected(term, action); + auto_detected(term, action, urls); +} - size_t count = tll_length(term->urls); +void +urls_assign_key_combos(url_list_t *urls) +{ + size_t count = tll_length(*urls); /* Assign key combos */ @@ -337,7 +341,7 @@ urls_collect(struct terminal *term, enum url_action action) if (count < ALEN(single)) { size_t idx = 0; - tll_foreach(term->urls, it) { + tll_foreach(*urls, it) { xassert(wcslen(single[idx]) < ALEN(it->item.key) - 1); wcscpy(it->item.key, single[idx++]); } @@ -347,7 +351,7 @@ urls_collect(struct terminal *term, enum url_action action) } #if defined(_DEBUG) && LOG_ENABLE_DBG - tll_foreach(term->urls, it) { + tll_foreach(*urls, it) { char url[1024]; wcstombs(url, it->item.url, sizeof(url) - 1); @@ -358,6 +362,43 @@ urls_collect(struct terminal *term, enum url_action action) } #endif +} + +static void +tag_cells_for_url(struct terminal *term, const struct url *url, bool value) +{ + const struct coord *start = &url->start; + const struct coord *end = &url->end; + + size_t end_r = end->row & (term->grid->num_rows - 1); + + size_t r = start->row & (term->grid->num_rows - 1); + size_t c = start->col; + + struct row *row = term->grid->rows[r]; + row->dirty = true; + + while (true) { + struct cell *cell = &row->cells[c]; + cell->attrs.url = value; + cell->attrs.clean = 0; + + if (r == end_r && c == end->col) + break; + + if (++c >= term->cols) { + r = (r + 1) & (term->grid->num_rows - 1); + c = 0; + + row = term->grid->rows[r]; + row->dirty = true; + } + } +} + +void +urls_render(struct terminal *term) +{ struct wl_window *win = term->window; struct wayland *wayl = term->wl; @@ -397,49 +438,10 @@ urls_collect(struct terminal *term, enum url_action action) }; tll_push_back(win->urls, url); + tag_cells_for_url(term, &it->item, true); } -} - -static void -tag_cells_for_url(struct terminal *term, const struct url *url, bool value) -{ - const struct coord *start = &url->start; - const struct coord *end = &url->end; - - size_t end_r = end->row & (term->grid->num_rows - 1); - - size_t r = start->row & (term->grid->num_rows - 1); - size_t c = start->col; - - struct row *row = term->grid->rows[r]; - row->dirty = true; - - while (true) { - struct cell *cell = &row->cells[c]; - cell->attrs.url = value; - cell->attrs.clean = 0; - - if (r == end_r && c == end->col) - break; - - if (++c >= term->cols) { - r = (r + 1) & (term->grid->num_rows - 1); - c = 0; - - row = term->grid->rows[r]; - row->dirty = true; - } - } -} -void -urls_tag_cells(struct terminal *term) -{ - if (unlikely(tll_length(term->urls)) == 0) - return; - - tll_foreach(term->urls, it) - tag_cells_for_url(term, &it->item, true); + render_refresh_urls(term); render_refresh(term); } diff --git a/url-mode.h b/url-mode.h index 6651e119..12d20192 100644 --- a/url-mode.h +++ b/url-mode.h @@ -11,8 +11,11 @@ static inline bool urls_mode_is_active(const struct terminal *term) return tll_length(term->urls) > 0; } -void urls_collect(struct terminal *term, enum url_action action); -void urls_tag_cells(struct terminal *term); +void urls_collect( + const struct terminal *term, enum url_action action, url_list_t *urls); +void urls_assign_key_combos(url_list_t *urls); + +void urls_render(struct terminal *term); void urls_reset(struct terminal *term); void urls_input(struct seat *seat, struct terminal *term, uint32_t key, -- cgit v1.3 From e6612927bea46164b823e679b21790e8f1c5b396 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 6 Feb 2021 20:11:31 +0100 Subject: url-mode: add ftp://, ftps://, file://, gemini:// and gopher:// --- url-mode.c | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'url-mode.c') diff --git a/url-mode.c b/url-mode.c index e770aa9b..c631c323 100644 --- a/url-mode.c +++ b/url-mode.c @@ -146,6 +146,11 @@ auto_detected(const struct terminal *term, enum url_action action, url_list_t *u static const wchar_t *const prots[] = { L"http://", L"https://", + L"ftp://", + L"ftps://", + L"file://", + L"gemini://", + L"gopher://", }; size_t max_prot_len = 0; -- cgit v1.3 From ab1224ba913c3c76113c9126b2cf2a688e9de0df Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 6 Feb 2021 20:26:01 +0100 Subject: url-mode: urls_assign_key_combos(): remove URLs when all key combos have been used up --- url-mode.c | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) (limited to 'url-mode.c') diff --git a/url-mode.c b/url-mode.c index c631c323..038ea2e8 100644 --- a/url-mode.c +++ b/url-mode.c @@ -332,27 +332,30 @@ urls_collect(const struct terminal *term, enum url_action action, url_list_t *ur auto_detected(term, action, urls); } +static void url_destroy(struct url *url); + void urls_assign_key_combos(url_list_t *urls) { - size_t count = tll_length(*urls); - - /* Assign key combos */ - - static const wchar_t *const single[] = { + static const wchar_t *const combos[] = { L"f", L"j", L"d", L"k", L"e", L"i", L"c", L"m", L"r", L"u", L"s", L"l", L"w", L"o", L"x", L"a", L"q", L"p", }; - if (count < ALEN(single)) { - size_t idx = 0; - tll_foreach(*urls, it) { - xassert(wcslen(single[idx]) < ALEN(it->item.key) - 1); - wcscpy(it->item.key, single[idx++]); + size_t idx = 0; + tll_foreach(*urls, it) { + if (idx < ALEN(combos)) { + xassert(wcslen(combos[idx]) < ALEN(it->item.key) - 1); + wcscpy(it->item.key, combos[idx]); + } else { + url_destroy(&it->item); + tll_remove(*urls, it); + } + + if (++idx == ALEN(combos)) { + LOG_WARN("not enough key combos (%zu) for %zu URLs", + ALEN(combos), tll_length(*urls)); } - } else { - LOG_ERR("unimplemented: more URLs than %zu", ALEN(single)); - assert(false); } #if defined(_DEBUG) && LOG_ENABLE_DBG @@ -450,6 +453,13 @@ urls_render(struct terminal *term) render_refresh(term); } +static void +url_destroy(struct url *url) +{ + free(url->url); + free(url->text); +} + void urls_reset(struct terminal *term) { @@ -468,8 +478,7 @@ urls_reset(struct terminal *term) tll_foreach(term->urls, it) { tag_cells_for_url(term, &it->item, false); - free(it->item.url); - free(it->item.text); + url_destroy(&it->item); } tll_free(term->urls); -- cgit v1.3 From e9ff8bac1c56cee56e8c8dca8dfdca926ed14343 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 6 Feb 2021 20:53:06 +0100 Subject: url-mode: refresh rendered URLs after accepting a key --- url-mode.c | 1 + 1 file changed, 1 insertion(+) (limited to 'url-mode.c') diff --git a/url-mode.c b/url-mode.c index 038ea2e8..21b11b8d 100644 --- a/url-mode.c +++ b/url-mode.c @@ -135,6 +135,7 @@ urls_input(struct seat *seat, struct terminal *term, uint32_t key, else if (is_valid) { xassert(seq_len + 1 <= ALEN(term->url_keys)); term->url_keys[seq_len] = wc; + render_refresh_urls(term); } } -- cgit v1.3 From 29c86612df087f71c523909988715a96cb0f98b2 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 6 Feb 2021 21:37:26 +0100 Subject: url-mode: generate key combinations using vimium’s algorithm MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- url-mode.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 47 insertions(+), 16 deletions(-) (limited to 'url-mode.c') diff --git a/url-mode.c b/url-mode.c index 21b11b8d..1bb38943 100644 --- a/url-mode.c +++ b/url-mode.c @@ -335,28 +335,57 @@ urls_collect(const struct terminal *term, enum url_action action, url_list_t *ur static void url_destroy(struct url *url); +static void +generate_key_combos(size_t count, wchar_t *combos[static count]) +{ + /* vimium default */ + static const wchar_t alphabet[] = L"sadfjklewcmpgh"; + + tll(wchar_t *) hints = tll_init(); + tll_push_back(hints, xwcsdup(L"")); + + size_t offset = 0; + while (tll_length(hints) - offset < count || tll_length(hints) == 1) { + wchar_t *hint = tll_back(hints); + offset++; + + for (size_t i = 0; i < ALEN(alphabet); i++) { + wchar_t wc = alphabet[i]; + wchar_t *next_hint = malloc((wcslen(hint) + 1 + 1) * sizeof(wchar_t)); + next_hint[0] = wc; + wcscpy(&next_hint[1], hint); + tll_push_back(hints, next_hint); + } + } + + /* Slice the list */ + for (size_t i = 0; i < offset; i++) + free(tll_pop_front(hints)); + + xassert(tll_length(hints) >= count); + + /* Fill in the callers array */ + size_t idx = 0; + tll_foreach(hints, it) { + if (idx >= count) + free(it->item); + else + combos[idx] = it->item; + idx++; + } + tll_free(hints); +} + void urls_assign_key_combos(url_list_t *urls) { - static const wchar_t *const combos[] = { - L"f", L"j", L"d", L"k", L"e", L"i", L"c", L"m", L"r", - L"u", L"s", L"l", L"w", L"o", L"x", L"a", L"q", L"p", - }; + wchar_t *combos[tll_length(*urls)]; + generate_key_combos(tll_length(*urls), combos); size_t idx = 0; tll_foreach(*urls, it) { - if (idx < ALEN(combos)) { - xassert(wcslen(combos[idx]) < ALEN(it->item.key) - 1); - wcscpy(it->item.key, combos[idx]); - } else { - url_destroy(&it->item); - tll_remove(*urls, it); - } - - if (++idx == ALEN(combos)) { - LOG_WARN("not enough key combos (%zu) for %zu URLs", - ALEN(combos), tll_length(*urls)); - } + xassert(wcslen(combos[idx]) < ALEN(it->item.key) - 1); + wcscpy(it->item.key, combos[idx++]); } #if defined(_DEBUG) && LOG_ENABLE_DBG @@ -371,6 +400,8 @@ urls_assign_key_combos(url_list_t *urls) } #endif + for (size_t i = 0; i < tll_length(*urls); i++) + free(combos[i]); } static void -- cgit v1.3 From ddd6f1d944100ab1eb9d01a73d19981aaf2e4332 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 7 Feb 2021 00:01:29 +0100 Subject: url-mode: fix key sequence generation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * We were using the ‘back’ element of the list as prefix for the next iteration of sequences, instead of the element at index ‘offset’ * ALEN() on a wchar_t[] includes the NULL terminator. We don’t want that. --- terminal.h | 2 +- url-mode.c | 84 ++++++++++++++++++++++++++++++++++++++------------------------ 2 files changed, 53 insertions(+), 33 deletions(-) (limited to 'url-mode.c') diff --git a/terminal.h b/terminal.h index b0084f63..43dd8e03 100644 --- a/terminal.h +++ b/terminal.h @@ -229,7 +229,7 @@ enum url_action { URL_ACTION_COPY, URL_ACTION_LAUNCH }; struct url { wchar_t *url; wchar_t *text; - wchar_t key[4]; + wchar_t *key; struct coord start; struct coord end; enum url_action action; diff --git a/url-mode.c b/url-mode.c index 1bb38943..b0203caa 100644 --- a/url-mode.c +++ b/url-mode.c @@ -335,58 +335,80 @@ urls_collect(const struct terminal *term, enum url_action action, url_list_t *ur static void url_destroy(struct url *url); +static int +wcscmp_qsort_wrapper(const void *_a, const void *_b) +{ + const wchar_t *a = *(const wchar_t **)_a; + const wchar_t *b = *(const wchar_t **)_b; + return wcscmp(a, b); +} + static void generate_key_combos(size_t count, wchar_t *combos[static count]) { /* vimium default */ static const wchar_t alphabet[] = L"sadfjklewcmpgh"; + static const size_t alphabet_len = ALEN(alphabet) - 1; + + size_t hints_count = 1; + wchar_t **hints = malloc(hints_count * sizeof(hints[0])); - tll(wchar_t *) hints = tll_init(); - tll_push_back(hints, xwcsdup(L"")); + hints[0] = xwcsdup(L""); size_t offset = 0; - while (tll_length(hints) - offset < count || tll_length(hints) == 1) { - wchar_t *hint = tll_back(hints); - offset++; + while (hints_count - offset < count || hints_count == 1) { + const wchar_t *prefix = hints[offset++]; - for (size_t i = 0; i < ALEN(alphabet); i++) { + hints = realloc(hints, (hints_count + alphabet_len) * sizeof(hints[0])); + + for (size_t i = 0; i < alphabet_len; i++) { wchar_t wc = alphabet[i]; - wchar_t *next_hint = malloc((wcslen(hint) + 1 + 1) * sizeof(wchar_t)); - next_hint[0] = wc; - wcscpy(&next_hint[1], hint); - tll_push_back(hints, next_hint); + wchar_t *hint = malloc((wcslen(prefix) + 1 + 1) * sizeof(wchar_t)); + + /* Will be reversed later */ + hint[0] = wc; + wcscpy(&hint[1], prefix); + hints[hints_count + i] = hint; } + hints_count += alphabet_len; } - /* Slice the list */ - for (size_t i = 0; i < offset; i++) - free(tll_pop_front(hints)); - - xassert(tll_length(hints) >= count); + xassert(hints_count - offset >= count); - /* Fill in the callers array */ - size_t idx = 0; - tll_foreach(hints, it) { - if (idx >= count) - free(it->item); + /* Copy slice of ‘hints’ array to the caller provided array */ + for (size_t i = 0; i < hints_count; i++) { + if (i >= offset && i < offset + count) + combos[i - offset] = hints[i]; else - combos[idx] = it->item; - idx++; + free(hints[i]); + } + free(hints); + + /* Sorting is a kind of shuffle, since we’re sorting on the + * *reversed* strings */ + qsort(combos, count, sizeof(wchar_t *), &wcscmp_qsort_wrapper); + + /* Reverse all strings */ + for (size_t i = 0; i < count; i++) { + const size_t len = wcslen(combos[i]); + for (size_t j = 0; j < len / 2; j++) { + wchar_t tmp = combos[i][j]; + combos[i][j] = combos[i][len - j - 1]; + combos[i][len - j - 1] = tmp; + } } - tll_free(hints); } void urls_assign_key_combos(url_list_t *urls) { - wchar_t *combos[tll_length(*urls)]; - generate_key_combos(tll_length(*urls), combos); + const size_t count = tll_length(*urls); + wchar_t *combos[count]; + generate_key_combos(count, combos); size_t idx = 0; - tll_foreach(*urls, it) { - xassert(wcslen(combos[idx]) < ALEN(it->item.key) - 1); - wcscpy(it->item.key, combos[idx++]); - } + tll_foreach(*urls, it) + it->item.key = combos[idx++]; #if defined(_DEBUG) && LOG_ENABLE_DBG tll_foreach(*urls, it) { @@ -399,9 +421,6 @@ urls_assign_key_combos(url_list_t *urls) LOG_DBG("URL: %s (%s)", url, key); } #endif - - for (size_t i = 0; i < tll_length(*urls); i++) - free(combos[i]); } static void @@ -490,6 +509,7 @@ url_destroy(struct url *url) { free(url->url); free(url->text); + free(url->key); } void -- cgit v1.3 From 1aec534b375a617d1855d4302d886f11fab02172 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 7 Feb 2021 10:32:56 +0100 Subject: url-mode: input: backspace reverts the last key --- url-mode.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'url-mode.c') diff --git a/url-mode.c b/url-mode.c index b0203caa..9d6b83fe 100644 --- a/url-mode.c +++ b/url-mode.c @@ -98,6 +98,17 @@ urls_input(struct seat *seat, struct terminal *term, uint32_t key, } } + size_t seq_len = wcslen(term->url_keys); + + if (sym == XKB_KEY_BackSpace) { + if (seq_len > 0) { + term->url_keys[seq_len - 1] = L'\0'; + render_refresh_urls(term); + } + + return; + } + wchar_t wc = xkb_state_key_get_utf32(seat->kbd.xkb_state, key); /* @@ -106,8 +117,6 @@ urls_input(struct seat *seat, struct terminal *term, uint32_t key, * sequence. */ - size_t seq_len = wcslen(term->url_keys); - bool is_valid = false; const struct url *match = NULL; -- cgit v1.3 From 9066ba87df574a59c4b572bbbb2f1c7962cab1f9 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 7 Feb 2021 14:29:34 +0100 Subject: url-mode: early exit when assigning key combos to empty list --- url-mode.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'url-mode.c') diff --git a/url-mode.c b/url-mode.c index 9d6b83fe..fc710fe6 100644 --- a/url-mode.c +++ b/url-mode.c @@ -412,6 +412,9 @@ void urls_assign_key_combos(url_list_t *urls) { const size_t count = tll_length(*urls); + if (count == 0) + return; + wchar_t *combos[count]; generate_key_combos(count, combos); -- cgit v1.3 From c84e37976763487832b8c80321f0df0fbd793f9e Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 7 Feb 2021 15:10:48 +0100 Subject: url-mode: be consistent; use xmalloc() + xrealloc() --- url-mode.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'url-mode.c') diff --git a/url-mode.c b/url-mode.c index fc710fe6..4d52dab2 100644 --- a/url-mode.c +++ b/url-mode.c @@ -39,7 +39,7 @@ activate_url(struct seat *seat, struct terminal *term, const struct url *url) size_t chars = wcstombs(NULL, url->url, 0); if (chars != (size_t)-1) { - char *url_utf8 = malloc(chars + 1); + char *url_utf8 = xmalloc(chars + 1); wcstombs(url_utf8, url->url, chars + 1); switch (url->action) { @@ -360,7 +360,7 @@ generate_key_combos(size_t count, wchar_t *combos[static count]) static const size_t alphabet_len = ALEN(alphabet) - 1; size_t hints_count = 1; - wchar_t **hints = malloc(hints_count * sizeof(hints[0])); + wchar_t **hints = xmalloc(hints_count * sizeof(hints[0])); hints[0] = xwcsdup(L""); @@ -368,11 +368,11 @@ generate_key_combos(size_t count, wchar_t *combos[static count]) while (hints_count - offset < count || hints_count == 1) { const wchar_t *prefix = hints[offset++]; - hints = realloc(hints, (hints_count + alphabet_len) * sizeof(hints[0])); + hints = xrealloc(hints, (hints_count + alphabet_len) * sizeof(hints[0])); for (size_t i = 0; i < alphabet_len; i++) { wchar_t wc = alphabet[i]; - wchar_t *hint = malloc((wcslen(prefix) + 1 + 1) * sizeof(wchar_t)); + wchar_t *hint = xmalloc((wcslen(prefix) + 1 + 1) * sizeof(wchar_t)); /* Will be reversed later */ hint[0] = wc; -- cgit v1.3 From 9ee392dc8ce678113ecc7827dedb23e0bdae3b84 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 7 Feb 2021 16:03:11 +0100 Subject: url-mode: generate-key-combos: minor efficiency tweaks * Use a do..while loop; this lets us drop the second half of the loop condition. * Call wcslen(prefix) once, *before* iterating the alphabet characters. * Step through the alphabet characters using a pointer, as this avoids an indexed load (with possibly an imul instruction in e.g. -Os builds). --- url-mode.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'url-mode.c') diff --git a/url-mode.c b/url-mode.c index 4d52dab2..2a3ee41d 100644 --- a/url-mode.c +++ b/url-mode.c @@ -365,22 +365,23 @@ generate_key_combos(size_t count, wchar_t *combos[static count]) hints[0] = xwcsdup(L""); size_t offset = 0; - while (hints_count - offset < count || hints_count == 1) { + do { const wchar_t *prefix = hints[offset++]; + const size_t prefix_len = wcslen(prefix); hints = xrealloc(hints, (hints_count + alphabet_len) * sizeof(hints[0])); - for (size_t i = 0; i < alphabet_len; i++) { - wchar_t wc = alphabet[i]; - wchar_t *hint = xmalloc((wcslen(prefix) + 1 + 1) * sizeof(wchar_t)); + const wchar_t *wc = &alphabet[0]; + for (size_t i = 0; i < alphabet_len; i++, wc++) { + wchar_t *hint = xmalloc((prefix_len + 1 + 1) * sizeof(wchar_t)); + hints[hints_count + i] = hint; /* Will be reversed later */ - hint[0] = wc; + hint[0] = *wc; wcscpy(&hint[1], prefix); - hints[hints_count + i] = hint; } hints_count += alphabet_len; - } + } while (hints_count - offset < count); xassert(hints_count - offset >= count); -- cgit v1.3 From 24263412dc121325ca980bd34f2b7ed097c7145b Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 7 Feb 2021 16:05:02 +0100 Subject: url-mode: urls_render(): early exit when URL list is empty --- url-mode.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'url-mode.c') diff --git a/url-mode.c b/url-mode.c index 2a3ee41d..40f3372f 100644 --- a/url-mode.c +++ b/url-mode.c @@ -474,6 +474,9 @@ urls_render(struct terminal *term) struct wl_window *win = term->window; struct wayland *wayl = term->wl; + if (tll_length(win->term->urls) == 0) + return; + xassert(tll_length(win->urls) == 0); tll_foreach(win->term->urls, it) { struct wl_surface *surf = wl_compositor_create_surface(wayl->compositor); -- cgit v1.3