From fd3107447031049e44fa7a060f3990d16d69fc3d Mon Sep 17 00:00:00 2001 From: Parker Coates Date: Mon, 16 Mar 2015 08:30:05 -0300 Subject: [PATCH 1/2] Use array reversal instead of array sorting. There's no need to do a full numerical sort here, since we know the keys will be in reverse numerical order, because (R) searches in last- to-first order. Not a big deal, but this is just simpler and presumably more efficient. Also cleaned up a rather confusing comment about (on) being used to remove duplicates, which it definitely doesn't. --- zsh-history-substring-search.zsh | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/zsh-history-substring-search.zsh b/zsh-history-substring-search.zsh index fb381ad..4291614 100644 --- a/zsh-history-substring-search.zsh +++ b/zsh-history-substring-search.zsh @@ -200,12 +200,10 @@ function _history-substring-search-begin() { # # Find all occurrences of the search query in the history file. # - # (k) turns it an array of line numbers. + # (k) returns the "keys" (history index numbers) instead of the values + # (Oa) reverses the order, because (R) returns results reversed. # - # (on) seems to remove duplicates, which are default - # options. They can be turned off by (ON). - # - _history_substring_search_matches=(${(kon)history[(R)(#$HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS)*${_history_substring_search_query_escaped}*]}) + _history_substring_search_matches=(${(kOa)history[(R)(#$HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS)*${_history_substring_search_query_escaped}*]}) # # Define the range of values that $_history_substring_search_match_index From 13033d87a1f9773c55dfa3d71ed661a8a9b3d1f5 Mon Sep 17 00:00:00 2001 From: Parker Coates Date: Mon, 16 Mar 2015 08:35:28 -0300 Subject: [PATCH 2/2] GH-19: Respect the HIST_FIND_NO_DUPS option. This is the best way I've found to remove duplicate entries while keeping the most recent occurence, but I'm not a ZSH expert. There may be a more efficient way. --- zsh-history-substring-search.zsh | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/zsh-history-substring-search.zsh b/zsh-history-substring-search.zsh index 4291614..57cc3a4 100644 --- a/zsh-history-substring-search.zsh +++ b/zsh-history-substring-search.zsh @@ -205,6 +205,15 @@ function _history-substring-search-begin() { # _history_substring_search_matches=(${(kOa)history[(R)(#$HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS)*${_history_substring_search_query_escaped}*]}) + # Remove duplicate entries (keeping on the most recent) if HIST_FIND_NO_DUPS is set. + if [[ -o HIST_FIND_NO_DUPS ]]; then + local -A unique_matches + for n in $_history_substring_search_matches; do + unique_matches[${history[$n]}]="$n" + done + _history_substring_search_matches=(${(@n)unique_matches}) + fi + # # Define the range of values that $_history_substring_search_match_index # can take: [0, $_history_substring_search_matches_count_plus].