From 14e5d94efef3fbb730453e8caa4b7bf8d02de7c9 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 26 Sep 2026 11:08:43 +0530 Subject: [PATCH] Allow specifying regexes for URL matching on hover Fixes #10518 --- docs/changelog.rst | 3 + kitty/options/definition.py | 35 +++++++++- kitty/options/parse.py | 5 ++ kitty/options/to-c-generated.h | 15 +++++ kitty/options/to-c.h | 43 +++++++++++++ kitty/options/types.py | 3 + kitty/screen.c | 113 +++++++++++++++++++++++++++++++++ kitty/state.h | 5 ++ kitty_tests/screen.py | 87 +++++++++++++++++++++++++ 9 files changed, 308 insertions(+), 1 deletion(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 859995c5c..9604f912b 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -202,6 +202,9 @@ Detailed list of changes 0.49.2 [future] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +- A new option :opt:`detect_url_regex` to detect arbitrary text, such as file + references in compiler output, as clickable URLs under the mouse (:iss:`10407`) + - Fix an out-of-bounds memory access when drawing a multi-line text sized character with the cursor below the bottom margin of the scroll region diff --git a/kitty/options/definition.py b/kitty/options/definition.py index c5e3a4f14..6f834f04f 100644 --- a/kitty/options/definition.py +++ b/kitty/options/definition.py @@ -936,7 +936,40 @@ opt( Detect URLs under the mouse. Detected URLs are highlighted with an underline and the mouse cursor becomes a hand over them. Even if this option is disabled, URLs are still clickable. See also the :opt:`underline_hyperlinks` option to control -how hyperlinks (as opposed to plain text URLs) are displayed. +how hyperlinks (as opposed to plain text URLs) are displayed. Use +:opt:`detect_url_regex` to detect arbitrary text, such as file references, as URLs. +""", +) + +opt( + '+detect_url_regex', + '', + option_type='store_multiple', + add_to_default=False, + ctype='!detect_url_regex', + long_text=""" +A regular expression to detect as a URL under the mouse, in addition to the +URLs detected via :opt:`url_prefixes`. Can be specified multiple times, text +under the mouse is detected as a URL if any of the expressions match it. The +expressions are tried in the order specified, and only if no normal URL is +found under the mouse. Matches are highlighted and clickable exactly like +normal detected URLs, see :opt:`detect_urls`. The expressions use the POSIX +extended regular expression syntax, so use :code:`[0-9]` rather than +:code:`\\\\d` and :code:`[[:alnum:]]` rather than :code:`\\\\w`. Text that has +been soft wrapped onto multiple lines is matched as a single line, looking at +most three lines above and below the mouse. For example, to detect file +references of the form :file:`path/to/file.py:42`:: + + detect_url_regex [[:alnum:]_./-]+\\.[[:alnum:]]+:[0-9]+ + +The matched text is used as the URL when clicked. Use :doc:`open_actions` with +the :code:`url` criterion to specify what to do with it. Note that the +:code:`url` criterion uses Python regular expression syntax. For example, to +pass file references to a script of your own that opens them in an editor, add +to :file:`open-actions.conf`:: + + url ^[\\w./-]+\\.\\w+:\\d+$ + action launch --type=background open-file-ref ${URL} """, ) diff --git a/kitty/options/parse.py b/kitty/options/parse.py index 92069195d..9ee0049df 100644 --- a/kitty/options/parse.py +++ b/kitty/options/parse.py @@ -970,6 +970,10 @@ class Parser: choices_for_default_pointer_shape = frozenset(('arrow', 'beam', 'text', 'pointer', 'hand', 'help', 'wait', 'progress', 'crosshair', 'cell', 'vertical-text', 'move', 'e-resize', 'ne-resize', 'nw-resize', 'n-resize', 'se-resize', 'sw-resize', 's-resize', 'w-resize', 'ew-resize', 'ns-resize', 'nesw-resize', 'nwse-resize', 'zoom-in', 'zoom-out', 'alias', 'copy', 'not-allowed', 'no-drop', 'grab', 'grabbing')) + def detect_url_regex(self, val: str, ans: dict[str, typing.Any]) -> None: + for k, v in store_multiple(val, ans["detect_url_regex"]): + ans["detect_url_regex"][k] = v + def detect_urls(self, val: str, ans: dict[str, typing.Any]) -> None: ans['detect_urls'] = to_bool(val) @@ -1638,6 +1642,7 @@ class Parser: def create_result_dict() -> dict[str, typing.Any]: return { 'action_alias': {}, + 'detect_url_regex': {}, 'env': {}, 'exe_search_path': {}, 'filter_notification': {}, diff --git a/kitty/options/to-c-generated.h b/kitty/options/to-c-generated.h index 21a0ffea7..1741319a9 100644 --- a/kitty/options/to-c-generated.h +++ b/kitty/options/to-c-generated.h @@ -615,6 +615,19 @@ convert_from_opts_detect_urls(PyObject *py_opts, Options *opts) { Py_DECREF(ret); } +static void +convert_from_python_detect_url_regex(PyObject *val, Options *opts) { + detect_url_regex(val, opts); +} + +static void +convert_from_opts_detect_url_regex(PyObject *py_opts, Options *opts) { + PyObject *ret = PyObject_GetAttrString(py_opts, "detect_url_regex"); + if (ret == NULL) return; + convert_from_python_detect_url_regex(ret, opts); + Py_DECREF(ret); +} + static void convert_from_python_url_excluded_characters(PyObject *val, Options *opts) { url_excluded_characters(val, opts); @@ -1660,6 +1673,8 @@ convert_opts_from_python_opts(PyObject *py_opts, Options *opts) { if (PyErr_Occurred()) return false; convert_from_opts_detect_urls(py_opts, opts); if (PyErr_Occurred()) return false; + convert_from_opts_detect_url_regex(py_opts, opts); + if (PyErr_Occurred()) return false; convert_from_opts_url_excluded_characters(py_opts, opts); if (PyErr_Occurred()) return false; convert_from_opts_show_hyperlink_targets(py_opts, opts); diff --git a/kitty/options/to-c.h b/kitty/options/to-c.h index 45f9c3864..70d8ddef5 100644 --- a/kitty/options/to-c.h +++ b/kitty/options/to-c.h @@ -476,6 +476,48 @@ url_prefixes(PyObject *up, Options *opts) { } } +static inline void +free_detect_url_regex(Options *opts) { + for (size_t i = 0; i < opts->detect_url_regex.count; i++) regfree(opts->detect_url_regex.items + i); + free(opts->detect_url_regex.items); + opts->detect_url_regex.items = NULL; + opts->detect_url_regex.count = 0; +} + +static inline void +detect_url_regex(PyObject *src, Options *opts) { + if (!PyDict_Check(src)) { + PyErr_SetString(PyExc_TypeError, "detect_url_regex must be a dict"); + return; + } + free_detect_url_regex(opts); + if (!PyDict_GET_SIZE(src)) return; + opts->detect_url_regex.items = calloc(PyDict_GET_SIZE(src), sizeof(regex_t)); + if (!opts->detect_url_regex.items) { + PyErr_NoMemory(); + return; + } + PyObject *key, *value; + Py_ssize_t pos = 0; + while (PyDict_Next(src, &pos, &key, &value)) { + if (!PyUnicode_Check(key)) { + PyErr_SetString(PyExc_TypeError, "detect_url_regex must be strings"); + return; + } + const char *pattern = PyUnicode_AsUTF8(key); + if (!pattern) return; + regex_t *r = opts->detect_url_regex.items + opts->detect_url_regex.count; + int err = regcomp(r, pattern, REG_EXTENDED); + if (err) { + char msg[256]; + regerror(err, r, msg, sizeof(msg)); + log_error("Ignoring invalid detect_url_regex: %s with error: %s", pattern, msg); + continue; + } + opts->detect_url_regex.count++; + } +} + static inline void free_menu_map(Options *opts) { if (opts->global_menu.entries) { @@ -699,6 +741,7 @@ static inline void free_allocs_in_options(Options *opts) { free_menu_map(opts); free_url_prefixes(opts); + free_detect_url_regex(opts); free_font_features(opts); free_background_images(opts); #define F(x) \ diff --git a/kitty/options/types.py b/kitty/options/types.py index e35e9f1b3..6f812963d 100644 --- a/kitty/options/types.py +++ b/kitty/options/types.py @@ -351,6 +351,7 @@ option_names = ( 'cursor_underline_thickness', 'custom_shaders', 'default_pointer_shape', + 'detect_url_regex', 'detect_urls', 'dim_opacity', 'disable_ligatures', @@ -740,6 +741,7 @@ class Options: window_title_bar_min_windows: int = 0 window_title_template: str = '{fmt.fg.red}{bell_symbol}{activity_symbol}{fmt.fg.window}{progress_percent}{title}' action_alias: dict[str, str] = {} + detect_url_regex: dict[str, str] = {} env: dict[str, str] = {} exe_search_path: dict[str, str] = {} filter_notification: dict[str, str] = {} @@ -869,6 +871,7 @@ class Options: defaults = Options() defaults.action_alias = {} +defaults.detect_url_regex = {} defaults.env = {} defaults.exe_search_path = {} defaults.filter_notification = {} diff --git a/kitty/screen.c b/kitty/screen.c index 908961599..6010c35a1 100644 --- a/kitty/screen.c +++ b/kitty/screen.c @@ -4710,6 +4710,117 @@ extend_url( } } +// Detecting URLs using detect_url_regex {{{ +#define DETECT_URL_REGEX_MAX_WRAPPED_LINES 3 + +typedef struct { + index_type x, y; +} RegexCellPos; + +// The text of the line being matched as UTF-8 along with the cell each byte comes from +static struct { + struct { + char *items; + size_t capacity; + } text; + struct { + RegexCellPos *items; + size_t capacity; + } positions; + size_t len; +} regex_line = {0}; + +static void add_url_range(Screen *self, index_type start_x, index_type start_y, index_type end_x, index_type end_y, bool is_hyperlink); + +static void +regex_line_append(const char *bytes, size_t num, index_type x, index_type y) { + ensure_space_for(®ex_line.text, items, char, regex_line.len + num + 1, capacity, 1024, false); + ensure_space_for(®ex_line.positions, items, RegexCellPos, regex_line.len + num, capacity, 1024, false); + for (size_t i = 0; i < num; i++) { + regex_line.text.items[regex_line.len] = bytes[i]; + regex_line.positions.items[regex_line.len++] = (RegexCellPos){.x = x, .y = y}; + } +} + +static void +regex_line_append_row(Line *line, index_type y, ListOfChars *lc) { + index_type limit = line->xnum; + // Ignore trailing empty cells, they are either unused space at the end of + // the line or space left over when a wide character is wrapped onto the next line + while (limit && !line->cpu_cells[limit - 1].ch_and_idx) limit--; + char utf8[8]; + for (index_type x = 0; x < limit; x++) { + const CPUCell *c = line->cpu_cells + x; + if (c->is_multicell && (c->x || c->y)) continue; + text_in_cell(c, line->text_cache, lc); + if (!lc->count || !lc->chars[0]) { + regex_line_append(" ", 1, x, y); + } else if (lc->chars[0] == '\t') { + regex_line_append("\t", 1, x, y); + unsigned num_cells_to_skip_for_tab = lc->count > 1 ? lc->chars[1] : 0; + while (num_cells_to_skip_for_tab && x + 1 < limit && cell_is_char(line->cpu_cells + x + 1, ' ')) { + x++; + num_cells_to_skip_for_tab--; + } + } else { + for (unsigned i = 0; i < lc->count; i++) { + if (lc->chars[i]) regex_line_append(utf8, encode_utf8(lc->chars[i], utf8), x, y); + } + } + } +} + +static bool +detect_url_with_regex(Screen *self, index_type x, index_type y, TextCache *text_cache) { + // Match against the soft wrapped line containing y, looking only a limited number of lines above and below y + index_type top = y, bottom = y; + while (top > 0 && y - top < DETECT_URL_REGEX_MAX_WRAPPED_LINES && visual_line_is_continued(self, top)) top--; + while (bottom + 1 < self->lines && bottom - y < DETECT_URL_REGEX_MAX_WRAPPED_LINES && visual_line_is_continued(self, bottom + 1)) bottom++; + Line line = {.xnum = self->columns, .text_cache = text_cache}; + regex_line.len = 0; + for (index_type r = top; r <= bottom; r++) { + visual_line(self, r, &line); + regex_line_append_row(&line, r, self->lc); + } + size_t mouse_start = regex_line.len, mouse_end = 0; + for (size_t i = 0; i < regex_line.len; i++) { + const RegexCellPos p = regex_line.positions.items[i]; + if (p.x == x && p.y == y) { + if (i < mouse_start) mouse_start = i; + mouse_end = i + 1; + } + } + if (mouse_start >= mouse_end) return false; + const char *text = regex_line.text.items; + regex_line.text.items[regex_line.len] = 0; + for (size_t i = 0; i < OPT(detect_url_regex).count; i++) { + const regex_t *re = OPT(detect_url_regex).items + i; + size_t offset = 0; + int eflags = 0; + regmatch_t m; + while (offset < mouse_end && regexec(re, text + offset, 1, &m, eflags) == 0) { + const size_t start = offset + m.rm_so, end = offset + m.rm_eo; + if (start >= mouse_end) break; + if (end > mouse_start && end > start) { + RegexCellPos s = regex_line.positions.items[start], e = regex_line.positions.items[end - 1]; + visual_line(self, e.y, &line); + const CPUCell *c = line.cpu_cells + e.x; + if (c->is_multicell) e.x = MIN(self->columns - 1, e.x + mcd_x_limit(c) - 1); + self->url_ranges.count = 0; + add_url_range(self, s.x, s.y, e.x, e.y, false); + return true; + } + offset = end > start ? end : start + 1; + // don't start matching in the middle of a UTF-8 encoded character + while (offset < regex_line.len && (text[offset] & 0xc0) == 0x80) offset++; + eflags = REG_NOTBOL; + } + } + return false; +} +#undef DETECT_URL_REGEX_MAX_WRAPPED_LINES +// }}} + int screen_detect_url(Screen *screen, unsigned int x, unsigned int y) { bool has_url = false; @@ -4750,6 +4861,8 @@ screen_detect_url(Screen *screen, unsigned int x, unsigned int y) { index_type y_extended = y; extend_url(screen, line, &url_end, &y_extended, sentinel, newlines_allowed, last_hostname_char_pos, scale); screen_mark_url(screen, url_start, y, url_end, y_extended); + } else if (OPT(detect_url_regex).count && detect_url_with_regex(screen, x, y, line->text_cache)) { + has_url = true; } else { screen_mark_url(screen, 0, 0, 0, 0); } diff --git a/kitty/state.h b/kitty/state.h index 08aafba8b..0fe135670 100644 --- a/kitty/state.h +++ b/kitty/state.h @@ -14,6 +14,7 @@ #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wpedantic" #include +#include #pragma GCC diagnostic pop #define OPT(name) global_state.opts.name @@ -145,6 +146,10 @@ typedef struct Options { } url_prefixes; char_type *url_excluded_characters; bool detect_urls; + struct { + regex_t *items; + size_t count; + } detect_url_regex; bool tab_bar_hidden; double font_size; struct { diff --git a/kitty_tests/screen.py b/kitty_tests/screen.py index 8f82bf346..486c9e2e9 100644 --- a/kitty_tests/screen.py +++ b/kitty_tests/screen.py @@ -1,6 +1,9 @@ #!/usr/bin/env python # License: GPL v3 Copyright: 2016, Kovid Goyal +import os +import tempfile + from kitty.fast_data_types import DECAWM, DECCOLM, DECOM, IRM, VT_PARSER_BUFFER_SIZE, Color, ColorProfile, Cursor from kitty.marks import marker_from_function, marker_from_regex, marker_from_text from kitty.window import pagerhist @@ -1638,6 +1641,90 @@ class TestScreen(BaseTest): detect_url(self) detect_url(self, scale=2) + def test_detect_url_regex(self): + file_ref = r'[[:alnum:]_./-]+\.[[:alnum:]]+:[0-9]+' + + def screen(*patterns, cols=30, lines=5, scrollback=5): + return self.create_screen(cols=cols, lines=lines, scrollback=scrollback, options={'detect_url_regex': {p: p for p in patterns}}) + + def ae(s, expected, x, y=0): + s.detect_url(x, y) + self.ae(expected, ''.join(s.text_for_marked_url())) + + s = screen(file_ref) + s.draw('see src/foo.py:42 here') + for x in range(4, 17): + ae(s, 'src/foo.py:42', x) + for x in (0, 3, 17, 20, 25): + ae(s, '', x) + ae(s, '', 5, 1) + + # multiple expressions, the first one to match under the mouse wins + s = screen(r'[0-9]+', r'JIRA-[0-9]+', r'[A-Z]+-[0-9]+') + s.draw('fix ABC-12 and JIRA-7') + ae(s, 'ABC-12', 5) + ae(s, 'JIRA-7', 16) + ae(s, '12', 9) + + # normal URLs take precedence + s = screen(file_ref) + s.draw('http://moo.com/x.py:1') + ae(s, 'http://moo.com/x.py:1', 16) + + # wide characters before the match must not shift columns + s = screen(file_ref) + s.draw('世界 foo.py:1 x') + ae(s, '', 4) + ae(s, 'foo.py:1', 5) + ae(s, 'foo.py:1', 12) + ae(s, '', 13) + + # matches in soft wrapped lines + s = screen(file_ref, cols=10) + s.draw('xx aaaa/bbbb/cccc.py:12 yy') + for x, y in ((3, 0), (9, 0), (0, 1), (9, 1), (2, 2)): + ae(s, 'aaaa/bbbb/cccc.py:12', x, y) + ae(s, '', 4, 2) + # a wide character wrapped onto the next line leaves an empty cell + s = screen('abcd世x', cols=5) + s.draw('abcd世x') + ae(s, 'abcd世x', 0) + ae(s, 'abcd世x', 2, 1) + # the number of wrapped lines scanned is limited + s = screen('a+', cols=5, lines=12) + s.draw('a' * 50) + ae(s, 'a' * 20, 0) + ae(s, 'a' * 35, 0, 5) + ae(s, 'a' * 20, 0, 9) + # hard line breaks are not joined + s = screen('a+', cols=5) + s.draw('aaaaa'), s.carriage_return(), s.linefeed(), s.draw('aa') + ae(s, 'aaaaa', 0) + ae(s, 'aa', 0, 1) + + # matches in the scrollback when scrolled + s = screen(file_ref, lines=3) + s.draw('see src/foo.py:42') + for i in range(4): + s.carriage_return(), s.linefeed(), s.draw(f'line {i}') + s.scroll(2, True) + ae(s, 'src/foo.py:42', 6) + ae(s, '', 0, 1) + + # invalid expressions are ignored, with an error logged to stderr by C code + with tempfile.TemporaryFile() as f: + orig_stderr = os.dup(2) + os.dup2(f.fileno(), 2) + try: + s = screen('(', 'foo') + finally: + os.dup2(orig_stderr, 2) + os.close(orig_stderr) + f.seek(0) + self.assertIn(b'Ignoring invalid detect_url_regex: (', f.read()) + s.draw('a foo') + ae(s, 'foo', 2) + def test_prompt_marking(self): # ]]]]]]]]]]]]]]]]}}}}}}}}}}}}}}}}))))))))))))))))))))))