From 3339915d204d1e6ccfe948e9df050e6b16a5a84b Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 13 Feb 2021 12:28:53 +0100 Subject: url-mode: store URL in UTF-8, not UTF-32 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The only time the URL is actually in UTF-32 is when we’re collecting it (auto-detecting it) from the grid, since cells store their character(s) in UTF-32. Everything *after* that prefers the URL in UTF-8. So, do the conversion while collecting the URL. This patch also changes the URL activation code to strip the ‘file://user@host/’ prefix from file URIs that refer to files on the *local* computer. --- url-mode.c | 103 +++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 60 insertions(+), 43 deletions(-) (limited to 'url-mode.c') diff --git a/url-mode.c b/url-mode.c index ac379c8e..b6025b57 100644 --- a/url-mode.c +++ b/url-mode.c @@ -11,6 +11,7 @@ #include "selection.h" #include "spawn.h" #include "terminal.h" +#include "uri.h" #include "util.h" #include "xmalloc.h" @@ -36,42 +37,55 @@ execute_binding(struct seat *seat, struct terminal *term, static void activate_url(struct seat *seat, struct terminal *term, const struct url *url) { - size_t chars = wcstombs(NULL, url->url, 0); + char *url_string = NULL; + + char *scheme, *host, *path; + if (uri_parse(url->url, strlen(url->url), &scheme, NULL, NULL, + &host, NULL, &path, NULL, NULL) && + strcmp(scheme, "file") == 0 && + hostname_is_localhost(host)) + { + /* + * This is a file in *this* computer. Pass only the + * filename to the URL-launcher. + * + * I.e. strip the ‘file://user@host/’ prefix. + */ + url_string = path; + free(scheme); + free(host); + } else + url_string = xstrdup(url->url); + + switch (url->action) { + case URL_ACTION_COPY: + if (text_to_clipboard(seat, term, url_string, seat->kbd.serial)) { + /* Now owned by our clipboard “manager” */ + url_string = NULL; + } + break; - if (chars != (size_t)-1) { - char *url_utf8 = xmalloc(chars + 1); - wcstombs(url_utf8, url->url, chars + 1); + case URL_ACTION_LAUNCH: { + size_t argc; + char **argv; - 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; + if (spawn_expand_template( + &term->conf->url_launch, 1, + (const char *[]){"url"}, + (const char *[]){url_string}, + &argc, &argv)) + { + spawn(term->reaper, term->cwd, argv, -1, -1, -1); - 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; + for (size_t i = 0; i < argc; i++) + free(argv[i]); + free(argv); } - } - - free(url_utf8); + break; + } } + + free(url_string); } void @@ -313,14 +327,20 @@ auto_detected(const struct terminal *term, enum url_action action, url_list_t *u start.row += term->grid->view; end.row += term->grid->view; - tll_push_back( - *urls, - ((struct url){ - .url = xwcsdup(url), - .text = xwcsdup(L""), - .start = start, - .end = end, - .action = action})); + size_t chars = wcstombs(NULL, url, 0); + if (chars != (size_t)-1) { + char *url_utf8 = xmalloc((chars + 1) * sizeof(wchar_t)); + wcstombs(url_utf8, url, chars + 1); + + tll_push_back( + *urls, + ((struct url){ + .url = url_utf8, + .text = xwcsdup(L""), + .start = start, + .end = end, + .action = action})); + } state = STATE_PROTOCOL; len = 0; @@ -425,13 +445,10 @@ urls_assign_key_combos(const struct config *conf, url_list_t *urls) #if defined(_DEBUG) && LOG_ENABLE_DBG tll_foreach(*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); + LOG_DBG("URL: %s (%s)", it->item.url, key); } #endif } -- cgit v1.3 From 39c5057d4963b292651cb23adfd0e1127cf89188 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 13 Feb 2021 12:42:35 +0100 Subject: url-mode: initial support for OSC-8 URLs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Things left to do: since OSC-8 URLs are stored as ranges in the per-row ‘extra’ data member, we currently do not handle line-wrapping URLs very well; they will be considered two separate URLs, and assigned two different key sequences. --- url-mode.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'url-mode.c') diff --git a/url-mode.c b/url-mode.c index b6025b57..83421abf 100644 --- a/url-mode.c +++ b/url-mode.c @@ -355,11 +355,42 @@ auto_detected(const struct terminal *term, enum url_action action, url_list_t *u UNIGNORE_WARNINGS +static void +osc8_uris(const struct terminal *term, enum url_action action, url_list_t *urls) +{ + for (int r = 0; r < term->rows; r++) { + const struct row *row = grid_row_in_view(term->grid, r); + + if (row->extra == NULL) + continue; + + tll_foreach(row->extra->uri_ranges, it) { + struct coord start = { + .col = it->item.start, + .row = r + term->grid->view, + }; + struct coord end = { + .col = it->item.end, + .row = r + term->grid->view, + }; + tll_push_back( + *urls, + ((struct url){ + .url = xstrdup(it->item.uri), + .text = xwcsdup(L""), + .start = start, + .end = end, + .action = action})); + } + } +} + void urls_collect(const struct terminal *term, enum url_action action, url_list_t *urls) { xassert(tll_length(term->urls) == 0); auto_detected(term, action, urls); + osc8_uris(term, action, urls); } static void url_destroy(struct url *url); -- cgit v1.3 From fb9e9513a59126831862a59ce9db2d2896299441 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 13 Feb 2021 13:45:59 +0100 Subject: url-mode: multiple URL (parts) with the same ID is assigned a single key sequence In case an URL is split up into multiple parts, those parts are now treated as a single URL when it comes to key assignment. Only the *first* URL part is actually assigned a key combo. The other parts are ignored. We still highlight them, but for all other purposes they are ignored. --- render.c | 6 ++++++ terminal.h | 1 + url-mode.c | 33 ++++++++++++++++++++++++++++++--- 3 files changed, 37 insertions(+), 3 deletions(-) (limited to 'url-mode.c') diff --git a/render.c b/render.c index aaa106a3..0f23fc4e 100644 --- a/render.c +++ b/render.c @@ -2531,6 +2531,12 @@ render_urls(struct terminal *term) const wchar_t *key = url->key; const size_t entered_key_len = wcslen(term->url_keys); + if (key == NULL) { + /* TODO: if we decide to use the .text field, we cannot + * just skip the entire jump label like this */ + continue; + } + struct wl_surface *surf = it->item.surf.surf; struct wl_subsurface *sub_surf = it->item.surf.sub; diff --git a/terminal.h b/terminal.h index 69c7c2ea..284dafa9 100644 --- a/terminal.h +++ b/terminal.h @@ -250,6 +250,7 @@ typedef tll(struct ptmx_buffer) ptmx_buffer_list_t; enum url_action { URL_ACTION_COPY, URL_ACTION_LAUNCH }; struct url { + uint64_t id; char *url; wchar_t *text; wchar_t *key; diff --git a/url-mode.c b/url-mode.c index 83421abf..57291e87 100644 --- a/url-mode.c +++ b/url-mode.c @@ -1,5 +1,6 @@ #include "url-mode.h" +#include #include #include @@ -135,6 +136,9 @@ urls_input(struct seat *seat, struct terminal *term, uint32_t key, const struct url *match = NULL; tll_foreach(term->urls, it) { + if (it->item.key == NULL) + continue; + const struct url *url = &it->item; const size_t key_len = wcslen(it->item.key); @@ -165,7 +169,8 @@ urls_input(struct seat *seat, struct terminal *term, uint32_t key, IGNORE_WARNING("-Wpedantic") static void -auto_detected(const struct terminal *term, enum url_action action, url_list_t *urls) +auto_detected(const struct terminal *term, enum url_action action, + url_list_t *urls) { static const wchar_t *const prots[] = { L"http://", @@ -335,6 +340,7 @@ auto_detected(const struct terminal *term, enum url_action action, url_list_t *u tll_push_back( *urls, ((struct url){ + .id = (uint64_t)rand() << 32 | rand(), .url = url_utf8, .text = xwcsdup(L""), .start = start, @@ -376,6 +382,7 @@ osc8_uris(const struct terminal *term, enum url_action action, url_list_t *urls) tll_push_back( *urls, ((struct url){ + .id = it->item.id, .url = xstrdup(it->item.uri), .text = xwcsdup(L""), .start = start, @@ -467,18 +474,38 @@ urls_assign_key_combos(const struct config *conf, url_list_t *urls) if (count == 0) return; + uint64_t seen_ids[count]; wchar_t *combos[count]; generate_key_combos(conf, count, combos); size_t idx = 0; - tll_foreach(*urls, it) + tll_foreach(*urls, it) { + bool id_already_seen = false; + for (size_t i = 0; i < idx; i++) { + if (it->item.id == seen_ids[i]) { + id_already_seen = true; + break; + } + } + + if (id_already_seen) + continue; + + seen_ids[idx] = it->item.id; it->item.key = combos[idx++]; + } + + /* Free combos we didn’t use up */ + for (size_t i = idx; i < count; i++) + free(combos[i]); #if defined(_DEBUG) && LOG_ENABLE_DBG tll_foreach(*urls, it) { + if (it->item.key == NULL) + continue; + char key[32]; wcstombs(key, it->item.key, sizeof(key) - 1); - LOG_DBG("URL: %s (%s)", it->item.url, key); } #endif -- cgit v1.3 From a99f9c9341d3e9e5133fb76006c9be54af4cbace Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 13 Feb 2021 13:58:16 +0100 Subject: url-mode: activate_url(): ensure ‘scheme’, ‘host’ and ‘path’ are free:d MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit uri_parse() may succeed. But if the scheme isn’t “file”, or if the hostname isn’t localhost, then we failed to free ‘scheme’, ‘host’ and ‘path’. --- url-mode.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'url-mode.c') diff --git a/url-mode.c b/url-mode.c index 57291e87..0733a457 100644 --- a/url-mode.c +++ b/url-mode.c @@ -42,20 +42,24 @@ activate_url(struct seat *seat, struct terminal *term, const struct url *url) char *scheme, *host, *path; if (uri_parse(url->url, strlen(url->url), &scheme, NULL, NULL, - &host, NULL, &path, NULL, NULL) && - strcmp(scheme, "file") == 0 && - hostname_is_localhost(host)) + &host, NULL, &path, NULL, NULL)) { - /* - * This is a file in *this* computer. Pass only the - * filename to the URL-launcher. - * - * I.e. strip the ‘file://user@host/’ prefix. - */ - url_string = path; + if (strcmp(scheme, "file") == 0 && hostname_is_localhost(host)) { + /* + * This is a file in *this* computer. Pass only the + * filename to the URL-launcher. + * + * I.e. strip the ‘file://user@host/’ prefix. + */ + url_string = path; + } else + free(path); + free(scheme); free(host); - } else + } + + if (url_string == NULL) url_string = xstrdup(url->url); switch (url->action) { -- cgit v1.3 From cf651d361f382bb572ff0ee60807a17058d4b365 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 14 Feb 2021 13:36:07 +0100 Subject: url-mode: remove duplicate URLs Remove URLs with the same start and end coordinates. Such duplicate URLs can be created by emitting an OSC-8 URL with matching grid content: \E]8;;http://foo\E\\http://foo\E]8;;\E\\ --- url-mode.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'url-mode.c') diff --git a/url-mode.c b/url-mode.c index 0733a457..7568b922 100644 --- a/url-mode.c +++ b/url-mode.c @@ -396,12 +396,35 @@ osc8_uris(const struct terminal *term, enum url_action action, url_list_t *urls) } } +static void +remove_duplicates(url_list_t *urls) +{ + tll_foreach(*urls, outer) { + tll_foreach(*urls, inner) { + if (outer == inner) + continue; + + if (outer->item.start.row == inner->item.start.row && + outer->item.start.col == inner->item.start.col && + outer->item.end.row == inner->item.end.row && + outer->item.end.col == inner->item.end.col) + { + free(inner->item.url); + free(inner->item.text); + free(inner->item.key); + tll_remove(*urls, inner); + } + } + } +} + void urls_collect(const struct terminal *term, enum url_action action, url_list_t *urls) { xassert(tll_length(term->urls) == 0); - auto_detected(term, action, urls); osc8_uris(term, action, urls); + auto_detected(term, action, urls); + remove_duplicates(urls); } static void url_destroy(struct url *url); -- cgit v1.3 From cc43c1b7048d2cf1cedb48ca63ecde40e2d0dbf5 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 14 Feb 2021 13:42:37 +0100 Subject: urls: remove free-form ‘text’ member from URL struct MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- render.c | 14 ++------------ terminal.h | 1 - url-mode.c | 11 +++-------- 3 files changed, 5 insertions(+), 21 deletions(-) (limited to 'url-mode.c') diff --git a/render.c b/render.c index 0f23fc4e..c9dfc1fc 100644 --- a/render.c +++ b/render.c @@ -2527,7 +2527,6 @@ render_urls(struct terminal *term) tll_foreach(win->urls, it) { const struct url *url = it->item.url; - const wchar_t *text = url->text; const wchar_t *key = url->key; const size_t entered_key_len = wcslen(term->url_keys); @@ -2563,22 +2562,13 @@ render_urls(struct terminal *term) continue; } - size_t text_len = wcslen(text); - size_t chars = wcslen(key) + (text_len > 0 ? 3 + text_len : 0); + size_t chars = wcslen(key); const size_t max_chars = 50; chars = min(chars, max_chars); wchar_t label[chars + 2]; - if (text_len == 0) - wcscpy(label, key); - else { - int count = swprintf(label, chars + 1, L"%ls - %ls", key, text); - if (count >= max_chars) { - label[max_chars] = L'…'; - label[max_chars + 1] = L'\0'; - } - } + wcscpy(label, key); for (size_t i = 0; i < wcslen(key); i++) label[i] = towupper(label[i]); diff --git a/terminal.h b/terminal.h index 284dafa9..cc37bcac 100644 --- a/terminal.h +++ b/terminal.h @@ -252,7 +252,6 @@ enum url_action { URL_ACTION_COPY, URL_ACTION_LAUNCH }; struct url { uint64_t id; char *url; - wchar_t *text; wchar_t *key; struct coord start; struct coord end; diff --git a/url-mode.c b/url-mode.c index 7568b922..44f9bb19 100644 --- a/url-mode.c +++ b/url-mode.c @@ -16,6 +16,8 @@ #include "util.h" #include "xmalloc.h" +static void url_destroy(struct url *url); + static bool execute_binding(struct seat *seat, struct terminal *term, enum bind_action_url action, uint32_t serial) @@ -346,7 +348,6 @@ auto_detected(const struct terminal *term, enum url_action action, ((struct url){ .id = (uint64_t)rand() << 32 | rand(), .url = url_utf8, - .text = xwcsdup(L""), .start = start, .end = end, .action = action})); @@ -388,7 +389,6 @@ osc8_uris(const struct terminal *term, enum url_action action, url_list_t *urls) ((struct url){ .id = it->item.id, .url = xstrdup(it->item.uri), - .text = xwcsdup(L""), .start = start, .end = end, .action = action})); @@ -409,9 +409,7 @@ remove_duplicates(url_list_t *urls) outer->item.end.row == inner->item.end.row && outer->item.end.col == inner->item.end.col) { - free(inner->item.url); - free(inner->item.text); - free(inner->item.key); + url_destroy(&inner->item); tll_remove(*urls, inner); } } @@ -427,8 +425,6 @@ urls_collect(const struct terminal *term, enum url_action action, url_list_t *ur remove_duplicates(urls); } -static void url_destroy(struct url *url); - static int wcscmp_qsort_wrapper(const void *_a, const void *_b) { @@ -595,7 +591,6 @@ static void url_destroy(struct url *url) { free(url->url); - free(url->text); free(url->key); } -- cgit v1.3 From 06a9ffa7632abf201b0ea0270bb5058af4fc4a6b Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 14 Feb 2021 14:18:11 +0100 Subject: urls: add key binding that toggles whether URLs are displayed on jump-label MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit By default, the URL isn’t shown on the jump-label. For auto-detect URLs, doing so is virtually always useless, as the URL is already visible in the grid. For OSC-8 URLs however, the URL is often _not_ visible in the grid. Many times, seeing the URL is still not needed (if you’re doing ‘ls --hyperlink’, you already know what the URIs are). But it is still useful to have a way to show the URLs. This patch adds a new key binding action that can be used in url-mode to toggle the URL on and off in the jump label. It is bound to ctrl+t by default. --- config.c | 2 ++ doc/foot.ini.5.scd | 14 ++++++++++++++ foot.ini | 1 + render.c | 21 +++++++++++++++++++-- terminal.h | 1 + url-mode.c | 6 ++++++ wayland.h | 1 + 7 files changed, 44 insertions(+), 2 deletions(-) (limited to 'url-mode.c') diff --git a/config.c b/config.c index 9ec6960f..3b09b882 100644 --- a/config.c +++ b/config.c @@ -119,6 +119,7 @@ static_assert(ALEN(search_binding_action_map) == BIND_ACTION_SEARCH_COUNT, static const char *const url_binding_action_map[] = { [BIND_ACTION_URL_NONE] = NULL, [BIND_ACTION_URL_CANCEL] = "cancel", + [BIND_ACTION_URL_TOGGLE_URL_ON_JUMP_LABEL] = "toggle-url-visible", }; static_assert(ALEN(url_binding_action_map) == BIND_ACTION_URL_COUNT, @@ -2107,6 +2108,7 @@ add_default_url_bindings(struct config *conf) add_binding(BIND_ACTION_URL_CANCEL, ctrl, XKB_KEY_g); add_binding(BIND_ACTION_URL_CANCEL, ctrl, XKB_KEY_d); add_binding(BIND_ACTION_URL_CANCEL, none, XKB_KEY_Escape); + add_binding(BIND_ACTION_URL_TOGGLE_URL_ON_JUMP_LABEL, ctrl, XKB_KEY_t); #undef add_binding } diff --git a/doc/foot.ini.5.scd b/doc/foot.ini.5.scd index 0d25fe12..6c1938a2 100644 --- a/doc/foot.ini.5.scd +++ b/doc/foot.ini.5.scd @@ -610,6 +610,20 @@ mode. The syntax is exactly the same as the regular **key-bindings**. Exits URL mode without opening an URL. Default: _Control+g Control+d Escape_. +*toggle-url-visible* + By default, the jump label only shows the key sequence required to + activate it. This is fine as long as the URL is visible in the + original text. + + But with e.g. OSC-8 URLs (the terminal version of HTML anchors, + i.e. "links"), the text on the screen can be something completey + different than the URL. + + This action toggles between showing and hiding the URL on the jump + label. + + Default: _Control+t_ + # SECTION: mouse-bindings diff --git a/foot.ini b/foot.ini index 5a5256c4..216ef45d 100644 --- a/foot.ini +++ b/foot.ini @@ -125,6 +125,7 @@ [url-bindings] # cancel=Control+g Control+d Escape +# toggle-url-visible=Control+t [mouse-bindings] # primary-paste=BTN_MIDDLE diff --git a/render.c b/render.c index c9dfc1fc..1db813bc 100644 --- a/render.c +++ b/render.c @@ -2525,6 +2525,8 @@ render_urls(struct terminal *term) + term->grid->num_rows) & (term->grid->num_rows - 1); const int view_end = view_start + term->rows - 1; + const bool show_url = term->urls_show_uri_on_jump_label; + tll_foreach(win->urls, it) { const struct url *url = it->item.url; const wchar_t *key = url->key; @@ -2562,13 +2564,28 @@ render_urls(struct terminal *term) continue; } - size_t chars = wcslen(key); + const size_t key_len = wcslen(key); + + size_t url_len = mbstowcs(NULL, url->url, 0); + if (url_len == (size_t)-1) + url_len = 0; + + wchar_t url_wchars[url_len + 1]; + mbstowcs(url_wchars, url->url, url_len + 1); + + size_t chars = key_len + (show_url ? (2 + url_len) : 0); const size_t max_chars = 50; chars = min(chars, max_chars); wchar_t label[chars + 2]; - wcscpy(label, key); + label[chars] = L'…'; + label[chars + 1] = L'\0'; + + if (show_url) + swprintf(label, chars + 1, L"%ls: %ls", key, url_wchars); + else + wcsncpy(label, key, chars + 1); for (size_t i = 0; i < wcslen(key); i++) label[i] = towupper(label[i]); diff --git a/terminal.h b/terminal.h index cc37bcac..7d6d0616 100644 --- a/terminal.h +++ b/terminal.h @@ -539,6 +539,7 @@ struct terminal { url_list_t urls; wchar_t url_keys[5]; + bool urls_show_uri_on_jump_label; #if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED struct { diff --git a/url-mode.c b/url-mode.c index 44f9bb19..09e170e6 100644 --- a/url-mode.c +++ b/url-mode.c @@ -30,6 +30,11 @@ execute_binding(struct seat *seat, struct terminal *term, urls_reset(term); return true; + case BIND_ACTION_URL_TOGGLE_URL_ON_JUMP_LABEL: + term->urls_show_uri_on_jump_label = !term->urls_show_uri_on_jump_label; + render_refresh_urls(term); + return true; + case BIND_ACTION_URL_COUNT: return false; @@ -613,6 +618,7 @@ urls_reset(struct terminal *term) tll_remove(term->urls, it); } + term->urls_show_uri_on_jump_label = false; memset(term->url_keys, 0, sizeof(term->url_keys)); render_refresh(term); } diff --git a/wayland.h b/wayland.h index b2d7c64f..7aecaee0 100644 --- a/wayland.h +++ b/wayland.h @@ -82,6 +82,7 @@ enum bind_action_search { enum bind_action_url { BIND_ACTION_URL_NONE, BIND_ACTION_URL_CANCEL, + BIND_ACTION_URL_TOGGLE_URL_ON_JUMP_LABEL, BIND_ACTION_URL_COUNT, }; -- cgit v1.3 From 2074f8b6563b7a6331c94a9231cd5b5af8bd7ade Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 14 Feb 2021 21:29:22 +0100 Subject: urls: OSC-8 URLs can now optionally be underlined outside of url-mode MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch adds a new configuration option, ‘osc8-underline=url-mode|always’. When set to ‘url-mode’, OSC-8 URLs are only highlighted (i.e. underlined) in url-mode, just like auto-detected URLs. When set to ‘always’, they are always underlined, regardless of mode, and regardless of their other attributes. This is implemented by tagging collected URLs with a boolean, instructing urls_render() and urls_reset() whether they should update the cells’ ‘url’ attribute or not. The OSC-8 collecter sets this based on the value of ‘osc8-underline’. Finally, when closing an OSC-8 URL, the cells are immediately tagged with the ‘url’ attribute if ‘osc8-underline’ is set to ‘always’. --- config.c | 15 +++++++++++++++ config.h | 5 +++++ doc/foot.ini.5.scd | 14 +++++++++++++- foot.ini | 1 + terminal.c | 14 +++++++++++++- terminal.h | 1 + url-mode.c | 18 +++++++++++++++++- 7 files changed, 65 insertions(+), 3 deletions(-) (limited to 'url-mode.c') diff --git a/config.c b/config.c index c2eaf382..d7d2c3a1 100644 --- a/config.c +++ b/config.c @@ -791,6 +791,19 @@ parse_section_main(const char *key, const char *value, struct config *conf, return false; } + else if (strcmp(key, "osc8-underline") == 0) { + if (strcmp(value, "url-mode") == 0) + conf->osc8_underline = OSC8_UNDERLINE_URL_MODE; + else if (strcmp(value, "always") == 0) + conf->osc8_underline = OSC8_UNDERLINE_URL_MODE; + else { + LOG_AND_NOTIFY_ERR( + "%s:%u: [default]: %s: invalid 'osc8-underline'; " + "must be one of 'url-mode', or 'always'", path, lineno, value); + return false; + } + } + else { LOG_AND_NOTIFY_ERR("%s:%u: [default]: %s: invalid key", path, lineno, key); return false; @@ -2243,6 +2256,8 @@ config_load(struct config *conf, const char *conf_path, .argv = NULL, }, + .osc8_underline = OSC8_UNDERLINE_ALWAYS, + .tweak = { .fcft_filter = FCFT_SCALING_FILTER_LANCZOS3, .allow_overflowing_double_width_glyphs = true, diff --git a/config.h b/config.h index 69930ec0..e8d15859 100644 --- a/config.h +++ b/config.h @@ -205,6 +205,11 @@ struct config { struct config_spawn_template notify; struct config_spawn_template url_launch; + enum { + OSC8_UNDERLINE_URL_MODE, + OSC8_UNDERLINE_ALWAYS, + } osc8_underline; + struct { enum fcft_scaling_filter fcft_filter; bool allow_overflowing_double_width_glyphs; diff --git a/doc/foot.ini.5.scd b/doc/foot.ini.5.scd index cdcdcfaa..3108c750 100644 --- a/doc/foot.ini.5.scd +++ b/doc/foot.ini.5.scd @@ -241,13 +241,25 @@ in this order: Clipboard target to automatically copy selected text to. One of *none*, *primary*, *clipboard* or *both*. Default: _primary_. - *workers* Number of threads to use for rendering. Set to 0 to disable multithreading. Default: the number of available logical CPUs (including SMT). Note that this is not always the best value. In some cases, the number of physical _cores_ is better. +*osc8-underline* + When to underline OSC-8 URLs. Possible values are *url-mode* and + *always*. + + When set to *url-mode*, OSC-8 URLs are only highlighted in URL + mode, just like auto-detected URLs. + + When set to *always*, OSC-8 URLs are always highlighted, + regardless of their other attributes (bold, italic etc). Note that + this does _not_ make them clickable. + + Default: _url-mode_ + # SECTION: scrollback diff --git a/foot.ini b/foot.ini index 1b309210..8ea060c9 100644 --- a/foot.ini +++ b/foot.ini @@ -29,6 +29,7 @@ # jump-label-letters=sadfjklewcmpgh # selection-target=primary # workers= +# osc8-underline=url-mode [scrollback] # lines=1000 diff --git a/terminal.c b/terminal.c index ce311c3d..3710f005 100644 --- a/terminal.c +++ b/terminal.c @@ -3049,13 +3049,25 @@ term_osc8_close(struct terminal *term) do { int end_col = r == end.row ? end.col : term->cols - 1; + struct row *row = term->grid->rows[r]; + + switch (term->conf->osc8_underline) { + case OSC8_UNDERLINE_ALWAYS: + for (int c = start_col; c <= end_col; c++) + row->cells[c].attrs.url = true; + break; + + case OSC8_UNDERLINE_URL_MODE: + break; + } + struct row_uri_range range = { .start = start_col, .end = end_col, .id = term->vt.osc8.id, .uri = xstrdup(term->vt.osc8.uri), }; - grid_row_add_uri_range(term->grid->rows[r], range); + grid_row_add_uri_range(row, range); start_col = 0; } while (r++ != end.row); diff --git a/terminal.h b/terminal.h index 7d6d0616..acff9d06 100644 --- a/terminal.h +++ b/terminal.h @@ -256,6 +256,7 @@ struct url { struct coord start; struct coord end; enum url_action action; + bool url_mode_dont_change_url_attr; /* Entering/exiting URL mode doesn’t touch the cells’ attr.url */ }; typedef tll(struct url) url_list_t; diff --git a/url-mode.c b/url-mode.c index 09e170e6..a441aa86 100644 --- a/url-mode.c +++ b/url-mode.c @@ -374,6 +374,18 @@ UNIGNORE_WARNINGS static void osc8_uris(const struct terminal *term, enum url_action action, url_list_t *urls) { + bool dont_touch_url_attr = false; + + switch (term->conf->osc8_underline) { + case OSC8_UNDERLINE_URL_MODE: + dont_touch_url_attr = false; + break; + + case OSC8_UNDERLINE_ALWAYS: + dont_touch_url_attr = true; + break; + } + for (int r = 0; r < term->rows; r++) { const struct row *row = grid_row_in_view(term->grid, r); @@ -396,7 +408,8 @@ osc8_uris(const struct terminal *term, enum url_action action, url_list_t *urls) .url = xstrdup(it->item.uri), .start = start, .end = end, - .action = action})); + .action = action, + .url_mode_dont_change_url_attr = dont_touch_url_attr})); } } } @@ -542,6 +555,9 @@ urls_assign_key_combos(const struct config *conf, url_list_t *urls) static void tag_cells_for_url(struct terminal *term, const struct url *url, bool value) { + if (url->url_mode_dont_change_url_attr) + return; + const struct coord *start = &url->start; const struct coord *end = &url->end; -- cgit v1.3 From 11464a65de4d96f04b4abd5c32e6c092a81a7a76 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 21 Feb 2021 20:10:24 +0100 Subject: url-mode: use the same key combo for all occurrences of an URL --- url-mode.c | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) (limited to 'url-mode.c') diff --git a/url-mode.c b/url-mode.c index a441aa86..554c17bf 100644 --- a/url-mode.c +++ b/url-mode.c @@ -519,10 +519,13 @@ urls_assign_key_combos(const struct config *conf, url_list_t *urls) wchar_t *combos[count]; generate_key_combos(conf, count, combos); - size_t idx = 0; + size_t combo_idx = 0; + size_t id_idx = 0; + tll_foreach(*urls, it) { bool id_already_seen = false; - for (size_t i = 0; i < idx; i++) { + + for (size_t i = 0; i < id_idx; i++) { if (it->item.id == seen_ids[i]) { id_already_seen = true; break; @@ -531,13 +534,30 @@ urls_assign_key_combos(const struct config *conf, url_list_t *urls) if (id_already_seen) continue; + seen_ids[id_idx++] = it->item.id; + + /* + * Scan previous URLs, and check if *this* URL matches any of + * them; if so, re-use the *same* key combo. + */ + bool url_already_seen = false; + tll_foreach(*urls, it2) { + if (&it->item == &it2->item) + break; + + if (strcmp(it->item.url, it2->item.url) == 0) { + it->item.key = xwcsdup(it2->item.key); + url_already_seen = true; + break; + } + } - seen_ids[idx] = it->item.id; - it->item.key = combos[idx++]; + if (!url_already_seen) + it->item.key = combos[combo_idx++]; } /* Free combos we didn’t use up */ - for (size_t i = idx; i < count; i++) + for (size_t i = combo_idx; i < count; i++) free(combos[i]); #if defined(_DEBUG) && LOG_ENABLE_DBG -- cgit v1.3