From 16d220d3f97737bd59520646365c525fd732cce4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Fri, 17 Jun 2022 18:45:28 +0200 Subject: [PATCH] =?UTF-8?q?config:=20add=20new=20mouse=20binding=20?= =?UTF-8?q?=E2=80=98launch-url=E2=80=99,=20bind=20to=20alt+BTN=5FLEFT?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This adds a new mouse binding, launch-url (bound to Alt+BTN_LEFT by default). When executed, the URL under the mouse pointer is launched (with url.launcher, as usual). This is done by calling urls_collect(), to get all currently visible URLs (both auto-detected and OSC-8 ones), and then iterating them all until we find one that spans the cell the mouse pointer is on. --- config.c | 2 ++ input.c | 35 +++++++++++++++++++++++++++++++++++ key-binding.h | 3 ++- 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/config.c b/config.c index ce0e27b8..ba2fc347 100644 --- a/config.c +++ b/config.c @@ -126,6 +126,7 @@ static const char *const binding_action_map[] = { [BIND_ACTION_SELECT_WORD] = "select-word", [BIND_ACTION_SELECT_WORD_WS] = "select-word-whitespace", [BIND_ACTION_SELECT_ROW] = "select-row", + [BIND_ACTION_LAUNCH_URL] = "launch-url", }; static const char *const search_binding_action_map[] = { @@ -2745,6 +2746,7 @@ add_default_mouse_bindings(struct config *conf) {BIND_ACTION_SELECT_WORD, m_none, {.m = {BTN_LEFT, 2}}}, {BIND_ACTION_SELECT_WORD_WS, m_ctrl, {.m = {BTN_LEFT, 2}}}, {BIND_ACTION_SELECT_ROW, m_none, {.m = {BTN_LEFT, 3}}}, + {BIND_ACTION_LAUNCH_URL, m_alt, {.m = {BTN_LEFT, 1}}}, }; conf->bindings.mouse.count = ALEN(bindings); diff --git a/input.c b/input.c index fca46050..8d652e7f 100644 --- a/input.c +++ b/input.c @@ -454,6 +454,41 @@ execute_binding(struct seat *seat, struct terminal *term, term, seat->mouse.col, seat->mouse.row, SELECTION_LINE_WISE, false); return true; + case BIND_ACTION_LAUNCH_URL: { + const int m_row = seat->mouse.row; + const int m_col = seat->mouse.col; + + if (m_col >= 0 && m_row >= 0) { + url_list_t urls = tll_init(); + urls_collect(term, URL_ACTION_LAUNCH, &urls); + + tll_foreach(urls, it) { + const struct url *url = &it->item; + const int start_row = url->range.start.row; + const int start_col = url->range.start.col; + const int end_row = url->range.end.row; + const int end_col = url->range.end.col; + + bool start_is_before_point = + start_row < m_row || + (start_row == m_row && start_col <= m_col); + + bool end_is_after_point = + end_row > m_row || + (end_row == m_row && end_col >= m_col); + + if (start_is_before_point && end_is_after_point) { + url_activate(seat, term, url, serial); + break; + } + } + + url_list_destroy(&urls); + } + + return true; + } + case BIND_ACTION_COUNT: BUG("Invalid action type"); return false; diff --git a/key-binding.h b/key-binding.h index 1c0e2a99..c853b2a1 100644 --- a/key-binding.h +++ b/key-binding.h @@ -47,9 +47,10 @@ enum bind_action_normal { BIND_ACTION_SELECT_WORD, BIND_ACTION_SELECT_WORD_WS, BIND_ACTION_SELECT_ROW, + BIND_ACTION_LAUNCH_URL, BIND_ACTION_KEY_COUNT = BIND_ACTION_PROMPT_NEXT + 1, - BIND_ACTION_COUNT = BIND_ACTION_SELECT_ROW + 1, + BIND_ACTION_COUNT = BIND_ACTION_LAUNCH_URL + 1, }; enum bind_action_search {