Refactor to reverse internal enumeration order.
This is a pure refactoring patch and does note change any functionality. We remove an unnecessary reversal of the history match array by swapping the enumeration order of match indices. Older entries now have higher internal indices than younger entries, and hence we start from 1. Also removed one unreachable elif clause each from search-up and search-down.
This commit is contained in:
parent
7a4b54b708
commit
6001e1f0f5
1 changed files with 78 additions and 114 deletions
|
|
@ -201,40 +201,37 @@ _history-substring-search-begin() {
|
||||||
# Find all occurrences of the search query in the history file.
|
# Find all occurrences of the search query in the history file.
|
||||||
#
|
#
|
||||||
# (k) returns the "keys" (history index numbers) instead of the values
|
# (k) returns the "keys" (history index numbers) instead of the values
|
||||||
# (Oa) reverses the order, because (R) returns results reversed.
|
# (R) returns values in reverse older, so the index of the youngest
|
||||||
|
# matching history entry is at the head of the list.
|
||||||
#
|
#
|
||||||
_history_substring_search_matches=(${(kOa)history[(R)(#$HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS)*${_history_substring_search_query_escaped}*]})
|
_history_substring_search_matches=(${(k)history[(R)(#$HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS)*${_history_substring_search_query_escaped}*]})
|
||||||
|
|
||||||
#
|
#
|
||||||
# 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_plus].
|
# 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
|
||||||
_history_substring_search_matches_count_plus=$(( _history_substring_search_matches_count + 1 ))
|
|
||||||
_history_substring_search_matches_count_sans=$(( _history_substring_search_matches_count - 1 ))
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# If $_history_substring_search_match_index is equal to
|
# If $_history_substring_search_match_index is equal to
|
||||||
# $_history_substring_search_matches_count_plus, this indicates that we
|
# $_history_substring_search_matches_count + 1, this indicates that we
|
||||||
# are beyond the beginning 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 end 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
|
# $_history_substring_search_match_index to 0 so that it will be
|
||||||
# $_history_substring_search_matches_count_plus so that it will be
|
# incremented to 1.
|
||||||
# decreased to $_history_substring_search_matches_count.
|
|
||||||
#
|
#
|
||||||
# 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
|
# $_history_substring_search_match_index to 1 so that it will be
|
||||||
# $_history_substring_search_matches_count so that it will be increased to
|
# decremented to 0.
|
||||||
# $_history_substring_search_matches_count_plus.
|
|
||||||
#
|
#
|
||||||
if [[ $WIDGET == history-substring-search-down ]]; then
|
if [[ $WIDGET == history-substring-search-down ]]; then
|
||||||
_history_substring_search_match_index=$_history_substring_search_matches_count
|
_history_substring_search_match_index=1
|
||||||
else
|
else
|
||||||
_history_substring_search_match_index=$_history_substring_search_matches_count_plus
|
_history_substring_search_match_index=0
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
@ -244,7 +241,7 @@ _history-substring-search-end() {
|
||||||
|
|
||||||
_history_substring_search_result=$BUFFER
|
_history_substring_search_result=$BUFFER
|
||||||
|
|
||||||
# the search was succesful so display the result properly by clearing away
|
# the search was successful so display the result properly by clearing away
|
||||||
# existing highlights and moving the cursor to the end of the result buffer
|
# existing highlights and moving the cursor to the end of the result buffer
|
||||||
if [[ $_history_substring_search_refresh_display -eq 1 ]]; then
|
if [[ $_history_substring_search_refresh_display -eq 1 ]]; then
|
||||||
region_highlight=()
|
region_highlight=()
|
||||||
|
|
@ -378,12 +375,30 @@ _history-substring-search-down-history() {
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_history-substring-search-found() {
|
||||||
|
#
|
||||||
|
# A match is available. The index of the match is held in
|
||||||
|
# $_history_substring_search_match_index
|
||||||
|
#
|
||||||
|
# 1. Make $BUFFER equal to the matching history entry.
|
||||||
|
#
|
||||||
|
# 2. Use $HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND
|
||||||
|
# to highlight the current buffer.
|
||||||
|
#
|
||||||
|
BUFFER=$history[$_history_substring_search_matches[$_history_substring_search_match_index]]
|
||||||
|
_history_substring_search_query_highlight=$HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND
|
||||||
|
}
|
||||||
|
|
||||||
_history-substring-search-not-found() {
|
_history-substring-search-not-found() {
|
||||||
#
|
#
|
||||||
# Nothing matched the search query, so put it back into the $BUFFER while
|
# No more matches are available.
|
||||||
# highlighting it accordingly so the user can revise it and search again.
|
#
|
||||||
|
# 1. Make $BUFFER equal to $_history_substring_search_query so the user can
|
||||||
|
# revise it and search again.
|
||||||
|
#
|
||||||
|
# 2. Use $HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND
|
||||||
|
# to highlight the current buffer.
|
||||||
#
|
#
|
||||||
_history_substring_search_old_buffer=$BUFFER
|
|
||||||
BUFFER=$_history_substring_search_query
|
BUFFER=$_history_substring_search_query
|
||||||
_history_substring_search_query_highlight=$HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND
|
_history_substring_search_query_highlight=$HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND
|
||||||
}
|
}
|
||||||
|
|
@ -399,73 +414,48 @@ _history-substring-search-up-search() {
|
||||||
#
|
#
|
||||||
# $_history_substring_search_matches is the current list of matches
|
# $_history_substring_search_matches is the current list of matches
|
||||||
# $_history_substring_search_matches_count is the current number of matches
|
# $_history_substring_search_matches_count is the current number of matches
|
||||||
# $_history_substring_search_matches_count_plus is the current number of matches + 1
|
|
||||||
# $_history_substring_search_matches_count_sans is the current number of matches - 1
|
|
||||||
# $_history_substring_search_match_index is the index of the current match
|
# $_history_substring_search_match_index is the index of the current match
|
||||||
#
|
#
|
||||||
# The range of values that $_history_substring_search_match_index can take
|
# The range of values that $_history_substring_search_match_index can take
|
||||||
# is: [0, $_history_substring_search_matches_count_plus]. A value of 0
|
# is: [0, $_history_substring_search_matches_count + 1]. A value of 0
|
||||||
# indicates that we are beyond the end of
|
# indicates that we are beyond the beginning of
|
||||||
# $_history_substring_search_matches. A value of
|
# $_history_substring_search_matches. A value of
|
||||||
# $_history_substring_search_matches_count_plus indicates that we are beyond
|
# $_history_substring_search_matches_count + 1 indicates that we are beyond
|
||||||
# the beginning of $_history_substring_search_matches.
|
# the end of $_history_substring_search_matches.
|
||||||
#
|
#
|
||||||
# In _history-substring-search-up-search() the initial value of
|
# In _history-substring-search-up-search() the initial value of
|
||||||
# $_history_substring_search_match_index is
|
# $_history_substring_search_match_index is 0. This value is set in
|
||||||
# $_history_substring_search_matches_count_plus. This value is set in
|
# _history-substring-search-begin(). _history-substring-search-up-search()
|
||||||
# _history-substring-search-begin(). _history-substring-search-up-search()
|
# will initially increment it to 1.
|
||||||
# will initially decrease it to $_history_substring_search_matches_count.
|
|
||||||
#
|
#
|
||||||
if [[ $_history_substring_search_match_index -ge 2 ]]; then
|
if [[ $_history_substring_search_match_index -lt $_history_substring_search_matches_count ]]; then
|
||||||
#
|
#
|
||||||
# Highlight the next match:
|
# Pick the next (older) match:
|
||||||
#
|
#
|
||||||
# 1. Decrease the value of $_history_substring_search_match_index.
|
# 1. Increment the value of $_history_substring_search_match_index.
|
||||||
#
|
#
|
||||||
# 2. Use $HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND
|
# 2. Update display (search found)
|
||||||
# to highlight the current buffer.
|
|
||||||
#
|
#
|
||||||
(( _history_substring_search_match_index-- ))
|
(( _history_substring_search_match_index++ ))
|
||||||
BUFFER=$history[$_history_substring_search_matches[$_history_substring_search_match_index]]
|
_history-substring-search-found
|
||||||
_history_substring_search_query_highlight=$HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND
|
|
||||||
|
|
||||||
elif [[ $_history_substring_search_match_index -eq 1 ]]; then
|
elif [[ $_history_substring_search_match_index -eq $_history_substring_search_matches_count ]]; then
|
||||||
#
|
#
|
||||||
# We will move beyond the end of $_history_substring_search_matches:
|
# We will move beyond the end of $_history_substring_search_matches:
|
||||||
#
|
#
|
||||||
# 1. Decrease the value of $_history_substring_search_match_index.
|
# 1. Increment the value of $_history_substring_search_match_index.
|
||||||
#
|
#
|
||||||
# 2. Save the current buffer in $_history_substring_search_old_buffer,
|
# 2. Update display (search not found)
|
||||||
# so that it can be retrieved by
|
|
||||||
# _history-substring-search-down-search() later.
|
|
||||||
#
|
#
|
||||||
# 3. Make $BUFFER equal to $_history_substring_search_query.
|
(( _history_substring_search_match_index++ ))
|
||||||
#
|
|
||||||
# 4. Use $HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND
|
|
||||||
# to highlight the current buffer.
|
|
||||||
#
|
|
||||||
(( _history_substring_search_match_index-- ))
|
|
||||||
_history-substring-search-not-found
|
_history-substring-search-not-found
|
||||||
|
return
|
||||||
elif [[ $_history_substring_search_match_index -eq $_history_substring_search_matches_count_plus ]]; then
|
|
||||||
#
|
|
||||||
# We were beyond the beginning of $_history_substring_search_matches but
|
|
||||||
# UP makes us move back to $_history_substring_search_matches:
|
|
||||||
#
|
|
||||||
# 1. Decrease the value of $_history_substring_search_match_index.
|
|
||||||
#
|
|
||||||
# 2. Restore $BUFFER from $_history_substring_search_old_buffer.
|
|
||||||
#
|
|
||||||
# 3. Use $HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND
|
|
||||||
# to highlight the current buffer.
|
|
||||||
#
|
|
||||||
(( _history_substring_search_match_index-- ))
|
|
||||||
BUFFER=$_history_substring_search_old_buffer
|
|
||||||
_history_substring_search_query_highlight=$HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND
|
|
||||||
|
|
||||||
else
|
else
|
||||||
#
|
#
|
||||||
# We are at the beginning of history and there are no further matches.
|
# We are beyond the end of $_history_substring_search_matches.
|
||||||
|
#
|
||||||
|
# 1. Update display (search not found)
|
||||||
#
|
#
|
||||||
_history-substring-search-not-found
|
_history-substring-search-not-found
|
||||||
return
|
return
|
||||||
|
|
@ -488,81 +478,55 @@ _history-substring-search-down-search() {
|
||||||
_history_substring_search_refresh_display=1
|
_history_substring_search_refresh_display=1
|
||||||
|
|
||||||
#
|
#
|
||||||
# Highlight matches during history-substring-up-search:
|
# Highlight matches during history-substring-down-search:
|
||||||
#
|
#
|
||||||
# The following constants have been initialized in
|
# The following constants have been initialized in
|
||||||
# _history-substring-search-up/down-search():
|
# _history-substring-search-up/down-search():
|
||||||
#
|
#
|
||||||
# $_history_substring_search_matches is the current list of matches
|
# $_history_substring_search_matches is the current list of matches
|
||||||
# $_history_substring_search_matches_count is the current number of matches
|
# $_history_substring_search_matches_count is the current number of matches
|
||||||
# $_history_substring_search_matches_count_plus is the current number of matches + 1
|
|
||||||
# $_history_substring_search_matches_count_sans is the current number of matches - 1
|
|
||||||
# $_history_substring_search_match_index is the index of the current match
|
# $_history_substring_search_match_index is the index of the current match
|
||||||
#
|
#
|
||||||
# The range of values that $_history_substring_search_match_index can take
|
# The range of values that $_history_substring_search_match_index can take
|
||||||
# is: [0, $_history_substring_search_matches_count_plus]. A value of 0
|
# is: [0, $_history_substring_search_matches_count + 1]. A value of 0
|
||||||
# indicates that we are beyond the end of
|
# indicates that we are beyond the beginning of
|
||||||
# $_history_substring_search_matches. A value of
|
# $_history_substring_search_matches. A value of
|
||||||
# $_history_substring_search_matches_count_plus indicates that we are beyond
|
# $_history_substring_search_matches_count + 1 indicates that we are beyond
|
||||||
# the beginning of $_history_substring_search_matches.
|
# the end of $_history_substring_search_matches.
|
||||||
#
|
#
|
||||||
# In _history-substring-search-down-search() the initial value of
|
# In _history-substring-search-down-search() the initial value of
|
||||||
# $_history_substring_search_match_index is
|
# $_history_substring_search_match_index is 1. This value is set in
|
||||||
# $_history_substring_search_matches_count. This value is set in
|
# _history-substring-search-begin(). _history-substring-search-down-search()
|
||||||
# _history-substring-search-begin().
|
# will initially decrement it to 0.
|
||||||
# _history-substring-search-down-search() will initially increase it to
|
|
||||||
# $_history_substring_search_matches_count_plus.
|
|
||||||
#
|
#
|
||||||
if [[ $_history_substring_search_match_index -le $_history_substring_search_matches_count_sans ]]; then
|
if [[ $_history_substring_search_match_index -gt 1 ]]; then
|
||||||
#
|
#
|
||||||
# Highlight the next match:
|
# Highlight the previous (younger) match:
|
||||||
#
|
#
|
||||||
# 1. Increase $_history_substring_search_match_index by 1.
|
# 1. Decrement $_history_substring_search_match_index.
|
||||||
#
|
#
|
||||||
# 2. Use $HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND
|
# 2. Update display (search found)
|
||||||
# to highlight the current buffer.
|
|
||||||
#
|
#
|
||||||
(( _history_substring_search_match_index++ ))
|
(( _history_substring_search_match_index-- ))
|
||||||
BUFFER=$history[$_history_substring_search_matches[$_history_substring_search_match_index]]
|
_history-substring-search-found
|
||||||
_history_substring_search_query_highlight=$HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND
|
|
||||||
|
|
||||||
elif [[ $_history_substring_search_match_index -eq $_history_substring_search_matches_count ]]; then
|
elif [[ $_history_substring_search_match_index -eq 1 ]]; then
|
||||||
#
|
#
|
||||||
# We will move beyond the beginning of $_history_substring_search_matches:
|
# We will move beyond the beginning of $_history_substring_search_matches:
|
||||||
#
|
#
|
||||||
# 1. Increase $_history_substring_search_match_index by 1.
|
# 1. Decrement $_history_substring_search_match_index.
|
||||||
#
|
#
|
||||||
# 2. Save the current buffer in $_history_substring_search_old_buffer, so
|
# 2. Update display (search not found)
|
||||||
# that it can be retrieved by _history-substring-search-up-search()
|
|
||||||
# later.
|
|
||||||
#
|
#
|
||||||
# 3. Make $BUFFER equal to $_history_substring_search_query.
|
(( _history_substring_search_match_index-- ))
|
||||||
#
|
|
||||||
# 4. Use $HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND
|
|
||||||
# to highlight the current buffer.
|
|
||||||
#
|
|
||||||
(( _history_substring_search_match_index++ ))
|
|
||||||
_history-substring-search-not-found
|
_history-substring-search-not-found
|
||||||
|
return
|
||||||
elif [[ $_history_substring_search_match_index -eq 0 ]]; then
|
|
||||||
#
|
|
||||||
# We were beyond the end of $_history_substring_search_matches but DOWN
|
|
||||||
# makes us move back to the $_history_substring_search_matches:
|
|
||||||
#
|
|
||||||
# 1. Increase $_history_substring_search_match_index by 1.
|
|
||||||
#
|
|
||||||
# 2. Restore $BUFFER from $_history_substring_search_old_buffer.
|
|
||||||
#
|
|
||||||
# 3. Use $HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND
|
|
||||||
# to highlight the current buffer.
|
|
||||||
#
|
|
||||||
(( _history_substring_search_match_index++ ))
|
|
||||||
BUFFER=$_history_substring_search_old_buffer
|
|
||||||
_history_substring_search_query_highlight=$HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND
|
|
||||||
|
|
||||||
else
|
else
|
||||||
#
|
#
|
||||||
# We are at the end of history and there are no further matches.
|
# We are beyond the beginning of $_history_substring_search_matches.
|
||||||
|
#
|
||||||
|
# 1. Update display (search not found)
|
||||||
#
|
#
|
||||||
_history-substring-search-not-found
|
_history-substring-search-not-found
|
||||||
return
|
return
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue