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 ++++++ 1 file changed, 6 insertions(+) (limited to 'render.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; -- 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 'render.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 'render.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