Do not search history when query is empty.

We used to do a history search when the query string was empty, which
resulted in all history indices being returned, but they are never
actually used as _history-substring-search-up-history and
_history-substring-search-down-history will just act like ZSH up-history
and down-history if the query is empty. Removing this unnecessary lookup
should speed up the empty query case, especially if you have a long
history.
This commit is contained in:
Geza Lore 2016-03-05 15:21:24 +00:00
parent 6001e1f0f5
commit 7444ca5783

View file

@ -180,13 +180,27 @@ _history-substring-search-begin() {
_history_substring_search_query_highlight= _history_substring_search_query_highlight=
# #
# Continue using the previous $_history_substring_search_result by default, # If the buffer is the same as the previously displayed history substring
# unless the current query was cleared or a new/different query was entered. # search result, then just keep stepping through the match list. Otherwise
# start a new search.
# #
if [[ -z $BUFFER || $BUFFER != $_history_substring_search_result ]]; then if [[ -n $BUFFER && $BUFFER == $_history_substring_search_result ]]; then
return;
fi
if [[ -z $BUFFER ]]; then
# #
# For the purpose of highlighting we will also keep # If the buffer is empty, we will just act like up-history/down-history
# a version without doubly-escaped meta characters. # in ZSH, so we do not need to actually search the history. This should
# speed things up a little.
#
_history_substring_search_query=
_history_substring_search_matches=()
else
#
# For the purpose of highlighting we keep a copy of the original
# query string.
# #
_history_substring_search_query=$BUFFER _history_substring_search_query=$BUFFER
@ -195,7 +209,7 @@ _history-substring-search-begin() {
# we put an extra "\\" before meta characters such as "\(" and "\)", # we put an extra "\\" before meta characters such as "\(" and "\)",
# so that they become "\\\(" and "\\\)". # so that they become "\\\(" and "\\\)".
# #
_history_substring_search_query_escaped=${BUFFER//(#m)[\][()|\\*?#<>~^]/\\$MATCH} local escaped_query=${BUFFER//(#m)[\][()|\\*?#<>~^]/\\$MATCH}
# #
# Find all occurrences of the search query in the history file. # Find all occurrences of the search query in the history file.
@ -204,35 +218,35 @@ _history-substring-search-begin() {
# (R) returns values in reverse older, so the index of the youngest # (R) returns values in reverse older, so the index of the youngest
# matching history entry is at the head of the list. # matching history entry is at the head of the list.
# #
_history_substring_search_matches=(${(k)history[(R)(#$HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS)*${_history_substring_search_query_escaped}*]}) _history_substring_search_matches=(${(k)history[(R)(#$HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS)*${escaped_query}*]})
fi
# #
# Define the range of values that $_history_substring_search_match_index # Define the range of values that $_history_substring_search_match_index
# can take: [0, $_history_substring_search_matches_count + 1]. # can take: [0, $_history_substring_search_matches_count + 1].
# #
_history_substring_search_matches_count=$#_history_substring_search_matches _history_substring_search_matches_count=$#_history_substring_search_matches
# #
# If $_history_substring_search_match_index is equal to # If $_history_substring_search_match_index is equal to
# $_history_substring_search_matches_count + 1, this indicates that we # $_history_substring_search_matches_count + 1, this indicates that we
# are beyond the end of $_history_substring_search_matches. # are beyond the end of $_history_substring_search_matches.
# #
# If $_history_substring_search_match_index is equal to 0, this indicates # If $_history_substring_search_match_index is equal to 0, this indicates
# that we are beyond the beginning of $_history_substring_search_matches. # that we are beyond the beginning of $_history_substring_search_matches.
# #
# If we have initially pressed "up" we have to initialize # If we have initially pressed "up" we have to initialize
# $_history_substring_search_match_index to 0 so that it will be # $_history_substring_search_match_index to 0 so that it will be
# incremented to 1. # incremented to 1.
# #
# If we have initially pressed "down" we have to initialize # If we have initially pressed "down" we have to initialize
# $_history_substring_search_match_index to 1 so that it will be # $_history_substring_search_match_index to 1 so that it will be
# decremented to 0. # decremented to 0.
# #
if [[ $WIDGET == history-substring-search-down ]]; then if [[ $WIDGET == history-substring-search-down ]]; then
_history_substring_search_match_index=1 _history_substring_search_match_index=1
else else
_history_substring_search_match_index=0 _history_substring_search_match_index=0
fi
fi fi
} }
@ -256,7 +270,7 @@ _history-substring-search-end() {
# #
# The following expression yields a variable $MBEGIN, which # The following expression yields a variable $MBEGIN, which
# indicates the begin position + 1 of the first occurrence # indicates the begin position + 1 of the first occurrence
# of _history_substring_search_query_escaped in $BUFFER. # of _history_substring_search_query in $BUFFER.
# #
: ${(S)BUFFER##(#m$HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS)($_history_substring_search_query##)} : ${(S)BUFFER##(#m$HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS)($_history_substring_search_query##)}
local begin=$(( MBEGIN - 1 )) local begin=$(( MBEGIN - 1 ))