From e1c2a4e50ccbc2bcb2fba08e8b2aebe7864d9015 Mon Sep 17 00:00:00 2001 From: Eddie Lebow Date: Sun, 15 Jan 2023 00:41:19 -0500 Subject: [PATCH] Include subsequence matches in history-pager If a `contains` search yields no results, try again with `contains_subsequence`. (cherry picked from commit 00692bcdfeed150edaf145bfe64bec9b6c9ccce8) --- src/history.cpp | 3 +++ src/history.h | 2 ++ src/reader.cpp | 12 +++++++++++- src/wcstringutil.cpp | 2 +- src/wcstringutil.h | 3 +++ 5 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/history.cpp b/src/history.cpp index 9ab51723c..6ce463b0e 100644 --- a/src/history.cpp +++ b/src/history.cpp @@ -195,6 +195,9 @@ bool history_item_t::matches_search(const wcstring &term, enum history_search_ty if (wcpattern2.back() != ANY_STRING) wcpattern2.push_back(ANY_STRING); return wildcard_match(content_to_match, wcpattern2); } + case history_search_type_t::contains_subsequence: { + return subsequence_in_string(term, content_to_match); + } case history_search_type_t::match_everything: { return true; } diff --git a/src/history.h b/src/history.h index 61fba0429..0f649486d 100644 --- a/src/history.h +++ b/src/history.h @@ -57,6 +57,8 @@ enum class history_search_type_t { contains_glob, /// Search for commands starting with the given glob pattern. prefix_glob, + /// Search for commands containing the given string as a subsequence + contains_subsequence, /// Matches everything. match_everything, }; diff --git a/src/reader.cpp b/src/reader.cpp index 036d2096b..384eb3a88 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -1295,11 +1295,21 @@ static history_pager_result_t history_pager_search(const std::shared_ptr haystack.size()) { return false; diff --git a/src/wcstringutil.h b/src/wcstringutil.h index 566b3f931..1ede27fba 100644 --- a/src/wcstringutil.h +++ b/src/wcstringutil.h @@ -34,6 +34,9 @@ bool string_suffixes_string_case_insensitive(const wcstring &proposed_suffix, bool string_prefixes_string_case_insensitive(const wcstring &proposed_prefix, const wcstring &value); +/// Test if a string matches a subsequence of another. +bool subsequence_in_string(const wcstring &needle, const wcstring &haystack); + /// Case-insensitive string search, modeled after std::string::find(). /// \param fuzzy indicates this is being used for fuzzy matching and case insensitivity is /// expanded to include symbolic characters (#3584).