From e948741864636b04aa0159ea02bcc3109dfbd43c Mon Sep 17 00:00:00 2001 From: Přemysl Eric Janouch Date: Sat, 17 Jul 2021 08:15:07 +0200 Subject: Enable pushing the search in a certain direction We want to make it possible to iterate all current matches. --- sdn.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/sdn.cpp b/sdn.cpp index b9aec88..0e81a98 100644 --- a/sdn.cpp +++ b/sdn.cpp @@ -989,10 +989,11 @@ fun show_help () { fclose (contents); } -fun search (const wstring &needle) -> int { - int best = g.cursor, best_n = 0, matches = 0; - for (int i = 0; i < int (g.entries.size ()); i++) { - auto o = (i + g.cursor) % g.entries.size (); +/// Stays on the current match when there are no better ones, unless it's pushed +fun search (const wstring &needle, int push) -> int { + int best = g.cursor, best_n = 0, matches = 0, step = push != 0 ? push : 1; + for (int i = 0, count = g.entries.size (); i < count; i++) { + auto o = (g.cursor + (count + i * step) + (count + push)) % count; int n = prefix_length (to_wide (g.entries[o].filename), needle); matches += n == needle.size (); if (n > best_n) { @@ -1060,7 +1061,7 @@ fun pop_levels (const string& old_cwd) { fix_cursor_and_offset (); if (!anchor.empty () && at_cursor ().filename != anchor) - search (to_wide (anchor)); + search (to_wide (anchor), 0); } fun explode_path (const string &path, vector &out) { @@ -1375,7 +1376,7 @@ fun handle (wint_t c) -> bool { case ACTION_SEARCH: g.editor = L"search"; g.editor_on_change = [] { - search (g.editor_line); + search (g.editor_line, 0); }; g.editor_on_confirm = [] { choose (at_cursor ()); -- cgit v1.2.3-70-g09d2