From 4df542f590c81757a9a35fc8ed4887dd8198e71f Mon Sep 17 00:00:00 2001 From: BlackAngelTVdev Date: Thu, 3 Dec 2026 10:06:04 +0100 Subject: [PATCH] feat(search): highlight the current match in its own color Every match of the in-place search used the same mark color, so after pressing n or N the only hint of the current match was the counter in the window title, which is easy to miss on a short query or a dense screen. The marker now colors the current match with the second mark color and keeps the rest on the first one, rebuilding the marker whenever the index changes. Fixes #51 --- docs/basic.rst | 4 +++- docs/changelog.rst | 2 ++ kitty/marks.py | 20 +++++++++++++++++++- kitty/scrollback_search.py | 25 +++++++++++++++++++++++++ kitty/window.py | 21 +++++++++++++++++---- kitty_tests/scrollback_search.py | 25 +++++++++++++++++++++++++ 6 files changed, 91 insertions(+), 6 deletions(-) diff --git a/docs/basic.rst b/docs/basic.rst index 255cc08..e5c78d2 100644 --- a/docs/basic.rst +++ b/docs/basic.rst @@ -35,7 +35,9 @@ to use a regular expression; both modifiers can be combined. Write a doubled backslash (``\\c``, ``\\r``) to search for those prefixes literally. While the search is active, :kbd:`n` jumps to the next match, :kbd:`N` to the previous one and :kbd:`Escape` ends it. The ``[current/total]`` counter is shown in the window -title and matches use the first marker color (see :opt:`mark1_foreground`). +title, matches use the first mark color (see :opt:`mark1_foreground`) and the +current match uses the second one (:opt:`mark2_foreground`), so it stands out +from the other matches. The :ac:`scrollback_search_next`, :ac:`scrollback_search_prev` and :ac:`clear_scrollback_search` actions can also be mapped directly. diff --git a/docs/changelog.rst b/docs/changelog.rst index b30692b..77bd55a 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -279,6 +279,8 @@ Detailed list of changes with :kbd:`n` and :kbd:`N`. Prefix the query with :code:`\c` for a case sensitive search and :code:`\r` for a regular expression; write a doubled backslash (:code:`\\c`, :code:`\\r`) to search for those prefixes literally. + The current match is drawn with the second mark color, so the match reached + with :kbd:`n` and :kbd:`N` is visible without looking at the window title. See :doc:`basic` for the details. - A configurable :opt:`leader` key now opens a which-key panel listing every diff --git a/kitty/marks.py b/kitty/marks.py index 88a0c1e..55d351c 100644 --- a/kitty/marks.py +++ b/kitty/marks.py @@ -2,7 +2,7 @@ # License: GPLv3 Copyright: 2020, Kovid Goyal import re -from collections.abc import Callable, Generator, Iterable, Sequence +from collections.abc import Callable, Generator, Iterable, Mapping, Sequence from re import Pattern from typing import Union @@ -29,6 +29,24 @@ def marker_from_regex(expression: Union[str, 'Pattern[str]'], color: int, flags: return marker +def marker_from_spans(spans: Mapping[str, Sequence[tuple[int, int, int]]]) -> MarkerFunc: + """A marker built from pre-computed spans, keyed by the text of their line. + + Each span is (start, end, color), where color is a mark number. Marker + functions receive one line at a time without its number, hence the lookup + by text. + """ + + def marker(text: str, left_address: int, right_address: int, color_address: int) -> Generator[None, None, None]: + for start, end, color in spans.get(text, ()): + set_uint_at_address(left_address, start) + set_uint_at_address(right_address, end - 1) + set_uint_at_address(color_address, max(1, min(color, 3))) + yield + + return marker + + def marker_from_multiple_regex(regexes: Iterable[tuple[int, str]], flags: int = re.UNICODE) -> MarkerFunc: expr = '' color_map = {} diff --git a/kitty/scrollback_search.py b/kitty/scrollback_search.py index 8b1ef8f..afb2da5 100644 --- a/kitty/scrollback_search.py +++ b/kitty/scrollback_search.py @@ -13,11 +13,17 @@ modifier syntax, so that the two prefixes can be searched for literally. from __future__ import annotations import re +from collections.abc import Sequence from dataclasses import dataclass, field CASE_SENSITIVE_MODIFIER = 'c' REGEX_MODIFIER = 'r' +# Mark numbers (see :opt:`mark1_background`) used to highlight the matches. The +# current match gets its own color so that it stands out from the other ones. +MATCH_COLOR = 1 +CURRENT_MATCH_COLOR = 2 + class InvalidQuery(ValueError): pass @@ -52,6 +58,25 @@ class ScrollbackSearch: def __len__(self) -> int: return len(self.matches) + @property + def current_match(self) -> Match | None: + return self.matches[self.index] if self.matches else None + + def marker_spans(self, lines: Sequence[str]) -> dict[str, tuple[tuple[int, int, int], ...]]: + """Spans to highlight, keyed by the text of the line holding them. + + Marker functions are called once per line with only its text, never with + its number, so the spans are grouped by line text. Lines that are + identical to the current one are therefore highlighted alike, which the + user cannot tell apart since their text is the same. + """ + current = self.current_match + spans: dict[str, list[tuple[int, int, int]]] = {} + for match in self.matches: + color = CURRENT_MATCH_COLOR if match == current else MATCH_COLOR + spans.setdefault(lines[match.line], []).append((match.start, match.end, color)) + return {text: tuple(found) for text, found in spans.items()} + def parse_query(raw: str) -> Query: text = (raw or '').strip() diff --git a/kitty/window.py b/kitty/window.py index e7283aa..4fd687b 100644 --- a/kitty/window.py +++ b/kitty/window.py @@ -827,6 +827,7 @@ class Window: self.actions_on_removal: list[Callable[['Window'], None]] = [] self.current_marker_spec: tuple[str, str | tuple[tuple[int, str], ...]] | None = None self.scrollback_search: ScrollbackSearch | None = None + self.scrollback_search_spans: dict[str, tuple[tuple[int, int, int], ...]] = {} self.scrollback_search_mode: str = '' self.scrollback_search_saved_marker: tuple[str, str | tuple[tuple[int, str], ...]] | None = None self.scrollback_search_saved_title: str | None = None @@ -2678,7 +2679,8 @@ class Window: Search the scrollback in place, highlighting every match. Prefix the query with \\c for a case sensitive search and with \\r for a regular expression. To search for those two strings literally, write a double backslash instead (``\\\\c`` and ``\\\\r``). Matches are navigated with n and N and - the search ends with Escape. The [current/total] counter is shown in the window title. + the search ends with Escape. The [current/total] counter is shown in the window title and the + current match uses the second mark color. """, ) def search_scrollback_prompt(self) -> bool | None: @@ -2728,12 +2730,12 @@ class Window: ring_bell(self.os_window_id) return self.clear_scrollback_search() - from .marks import marker_from_regex - self.scrollback_search = ScrollbackSearch(query, pattern, matches, total_lines=len(lines)) + search = ScrollbackSearch(query, pattern, matches, total_lines=len(lines)) + self.scrollback_search = search + self.scrollback_search_spans = search.marker_spans(lines) self.scrollback_search_saved_marker = self.current_marker_spec self.scrollback_search_saved_title = self.override_title - self.screen.set_marker(marker_from_regex(pattern, 1)) self._push_scrollback_search_mode() self._goto_scrollback_match(0) @@ -2742,6 +2744,7 @@ class Window: if self.scrollback_search is None: return self.scrollback_search = None + self.scrollback_search_spans = {} self.screen.set_marker() self.current_marker_spec = None saved = self.scrollback_search_saved_marker @@ -2785,8 +2788,18 @@ class Window: search.index = index % len(search.matches) match = search.matches[search.index] self.set_title(f'[{search.index + 1}/{len(search.matches)}] {search.query.text}') + self._refresh_scrollback_search_marker() self.screen.scroll_to_absolute(scroll_offset_for(match, search.total_lines, self.screen.lines)) + def _refresh_scrollback_search_marker(self) -> None: + from .marks import marker_from_spans + + # Clear first, as set_marker() only releases the previous callable when + # it is called without one. Marking every line again is needed anyway to + # recolor the match that was current until now. + self.screen.set_marker() + self.screen.set_marker(marker_from_spans(self.scrollback_search_spans)) + def _push_scrollback_search_mode(self) -> None: from .fast_data_types import GLFW_FKEY_ESCAPE, GLFW_MOD_SHIFT from .options.utils import KeyboardMode, KeyDefinition, SingleKey diff --git a/kitty_tests/scrollback_search.py b/kitty_tests/scrollback_search.py index 51a65bb..d6fb011 100644 --- a/kitty_tests/scrollback_search.py +++ b/kitty_tests/scrollback_search.py @@ -5,8 +5,11 @@ import re import time from kitty.scrollback_search import ( + CURRENT_MATCH_COLOR, + MATCH_COLOR, InvalidQuery, Match, + ScrollbackSearch, compile_query, find_matches, parse_query, @@ -83,6 +86,28 @@ class TestScrollbackSearch(BaseTest): # Empty matches are skipped and do not loop forever self.ae(find_matches(['abc'], parse_query('\\r x*')), []) + def test_current_match(self): + lines = ['one 17 here', 'nothing', '17 and 17'] + query = parse_query('17') + matches = find_matches(lines, query) + search = ScrollbackSearch(query, compile_query(query), matches, total_lines=len(lines)) + self.ae(len(search), 3) + self.ae(search.current_match, Match(0, 4, 6)) + search.index = 2 + self.ae(search.current_match, Match(2, 7, 9)) + + # The current match is the only one with its own color, and navigation + # moves that color along + spans = search.marker_spans(lines) + self.ae(spans['one 17 here'], ((4, 6, MATCH_COLOR),)) + self.ae(spans['17 and 17'], ((0, 2, MATCH_COLOR), (7, 9, CURRENT_MATCH_COLOR))) + search.index = 1 + self.ae(search.marker_spans(lines)['17 and 17'], ((0, 2, CURRENT_MATCH_COLOR), (7, 9, MATCH_COLOR))) + + # Wrapping the index keeps exactly one current match selected + search.index = 4 % len(search.matches) + self.ae(sum(1 for found in search.marker_spans(lines).values() for span in found if span[2] == CURRENT_MATCH_COLOR), 1) + def test_scroll_offset(self): self.ae(scroll_offset_for(Match(0, 0, 1), 100, 10), 90) self.ae(scroll_offset_for(Match(50, 0, 1), 100, 10), 43)