Skip to content

feat(document): search the rendered text with /, then n and N - #174

Merged
backnotprop merged 1 commit into
mainfrom
feat/doc-search
Nov 20, 2026
Merged

backnotprop merged 1 commit into
mainfrom
feat/doc-search

Conversation

@backnotprop

@backnotprop backnotprop commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Adds text search to the Markdown document screen.

What it does

  • / opens a one-line prompt in the footer. Each keystroke highlights every match and moves the cursor to the first match at or after where / was pressed. The footer shows the count (3/17).
  • Enter keeps the search and closes the prompt. Esc closes it and restores the view, the cursor, the selected block and any earlier search.
  • n / N go to the next / previous match, wrapping at both ends. The footer says "search wrapped to the top/bottom".
  • Esc peels one layer at a time: a waiting selection, then the search highlights, then quit. An empty search also clears the highlights.
  • Smartcase, plain text (no regex).
  • The cursor lands on the match, so v selects from it.
  • Search moves only the cursor, the selected block and the view. A selection waiting in the toolbar and all annotations stay as they were.
  • In a folder review, search covers the open file only. Opening another file drops the search.

How matching works

The search runs over the rendered lines before wrapping (DocLayout::rendered_lines). A soft wrap never enters the searched text, so a match can run across one. Each match maps back to a source range through the existing source map. The screen's cells carry the same source offsets, so the highlight finds its cells in wrapped rows, wrapped table cells and list items. A cell with no source byte that sits between two cells of the same match is filled in, for example the space a soft break became. A query does not cross a hard line break, a list item or a table row. The index is built once per document, on the first /, and it does not depend on width. A long document that is still being laid out in the background (#166) is finished first, so the search covers all of it. A keystroke costs one search and no relayout.

Keys

New actions search (/), next-match (n) and previous-match (N). They are live only in the document pane, where none of those keys was bound. n/N keep their meanings in the quit and clear-all questions (no) and in the code review (next-file/previous-file), because those contexts never read the same keypress. The new actions are in LATE, so a config that already gave these keys to another action still loads, and the search key it collides with is left unbound. Example: down = ["n", "Down"]. The footer hints name the keys you have bound. The README key table and the crate README list the new actions. The ? help screen exists only in the code review, which has no search, so the new keys are not listed there.

Colours

New [colors] roles search (all matches, dark 24 / light 117) and search-current (the current match, dark 208 / light 214 with black text). They work like cursor and selection. A test checks that neither role uses an annotation tint, the block bar or the cursor background.

Performance (--bench samples/big.md, release, 2.5 MB)

  • index: 16 ms, once per document
  • searches: e 240,156 matches in 5.3 ms, th 1.8 ms, the 1.6 ms, plugin 0.9 ms, no match 0.6 ms
  • in the real binary in tmux, each keystroke showed on screen in 8 to 16 ms, including the capture polling

Known limit: a match in the part of a long code line that is clipped off screen has no cell. It is counted, and the cursor goes to the first row of its block.

Tests: unit tests for matching (smartcase, plain text, markup, soft wrap, soft break, list item, wrapped table cell, decoration only, no match, wraparound, a non-length-preserving lower case). Screen tests in a TestBackend cover the prompt, the count, n/N, the Esc layers, an empty search, no match, a wrap-spanning highlight with v from the match, a waiting selection and an annotation tint left alone, rebinding, folder mode, highlights that follow </> and z reflows, and matches in coloured code (also in a code block nested in a list) that keep their syntax colour under the search background. Key tests cover defaults, other contexts, and old configs.

Closes #137

A one-line prompt in the footer searches the open document as it is
typed: every match is highlighted, the cursor lands on the first at or
after it, and the footer counts them. Enter keeps the search, Esc puts
the view back; n and N step through matches with wraparound, and a
second Esc clears the highlights.

Matching runs over the rendered lines before wrapping, so a match can
cross a soft wrap, and each match maps back to its source range through
the existing source map, which is what the screen's cells carry: the
highlight finds its cells in wrapped rows, table cells and list items.
Smartcase, plain text. The index is built once per document on the
first /, so a keystroke costs one search and no relayout.

New keys search, next-match and previous-match (/, n, N) are live only
in the document and yield to an older config that took those keys. New
colour roles search and search-current keep matches apart from notes.
--bench times the index and a few searches.

Closes #137
@backnotprop
backnotprop added this pull request to the merge queue Nov 20, 2026
Merged via the queue into main with commit c813d09 Nov 20, 2026
3 checks passed
@backnotprop
backnotprop deleted the feat/doc-search branch November 20, 2026 04:36
@backnotprop backnotprop mentioned this pull request Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Search the document text with / (and n / N)

1 participant