Commit fd534eb9 authored by Marc Cornellà's avatar Marc Cornellà
Browse files

history-substring-search: update to upstream version 2019-05-12

Updates OMZ's copy to commit 0f80b8eb3368b46e5e573c1d91ae69eb095db3fb from zsh-users/zsh-history-substring-search
parent 2a3a940a
zsh-history-substring-search # zsh-history-substring-search
==============================================================================
This is a clean-room implementation of the [Fish shell][1]'s history search This is a clean-room implementation of the [Fish shell][1]'s history search
feature, where you can type in any part of any previously entered command feature, where you can type in any part of any command from history and then
and press the UP and DOWN arrow keys to cycle through the matching commands. press chosen keys, such as the UP and DOWN arrows, to cycle through matches.
You can also use K and J in VI mode or ^P and ^N in EMACS mode for the same.
[1]: http://fishshell.com
[1]: https://fishshell.com [2]: http://www.zsh.org/mla/users/2009/msg00818.html
[2]: https://www.zsh.org/mla/users/2009/msg00818.html [3]: http://sourceforge.net/projects/fizsh/
[3]: https://sourceforge.net/projects/fizsh/ [4]: https://github.com/robbyrussell/oh-my-zsh/pull/215
[4]: https://github.com/ohmyzsh/ohmyzsh/pull/215
[5]: https://github.com/zsh-users/zsh-history-substring-search [5]: https://github.com/zsh-users/zsh-history-substring-search
[6]: https://github.com/zsh-users/zsh-syntax-highlighting [6]: https://github.com/zsh-users/zsh-syntax-highlighting
------------------------------------------------------------------------------
Requirements Requirements
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
* [ZSH](http://zsh.sourceforge.net) 4.3 or newer * [ZSH](http://zsh.sourceforge.net) 4.3 or newer
Install
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
Using the [Homebrew]( https://brew.sh ) package manager:
brew install zsh-history-substring-search
echo 'source /usr/local/share/zsh-history-substring-search/zsh-history-substring-search.zsh' >> ~/.zshrc
Using [Oh-my-zsh](https://github.com/robbyrussell/oh-my-zsh):
1. Clone this repository in oh-my-zsh's plugins directory:
git clone https://github.com/zsh-users/zsh-history-substring-search ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-history-substring-search
2. Activate the plugin in `~/.zshrc`:
plugins=( [plugins...] history-substring-search)
3. Source `~/.zshrc` to take changes into account:
source ~/.zshrc
Usage Usage
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
...@@ -33,117 +52,147 @@ Usage ...@@ -33,117 +52,147 @@ Usage
% source zsh-syntax-highlighting.zsh % source zsh-syntax-highlighting.zsh
% source zsh-history-substring-search.zsh % source zsh-history-substring-search.zsh
2. Bind keyboard shortcuts to this script's functions: 2. Bind keyboard shortcuts to this script's functions.
Users typically bind their UP and DOWN arrow keys to this script, thus:
* Run `cat -v` in your favorite terminal emulator to observe key codes.
     (**NOTE:** In some cases, `cat -v` shows the wrong key codes. If the
key codes shown by `cat -v` don't work for you, press `<C-v><UP>` and
`<C-v><DOWN>` at your ZSH command line prompt for correct key codes.)
* Press the UP arrow key and observe what is printed in your terminal.
* Press the DOWN arrow key and observe what is printed in your terminal.
* Press the Control and C keys simultaneously to terminate the `cat -v`.
* Use your observations from the previous steps to create key bindings.
For example, if you observed `^[[A` for UP and `^[[B` for DOWN, then:
bindkey '^[[A' history-substring-search-up
bindkey '^[[B' history-substring-search-down
# bind UP and DOWN arrow keys However, if the observed values don't work, you can try using terminfo:
zmodload zsh/terminfo
bindkey "$terminfo[kcuu1]" history-substring-search-up
bindkey "$terminfo[kcud1]" history-substring-search-down
# bind UP and DOWN arrow keys (compatibility fallback bindkey "$terminfo[kcuu1]" history-substring-search-up
# for Ubuntu 12.04, Fedora 21, and MacOSX 10.9 users) bindkey "$terminfo[kcud1]" history-substring-search-down
bindkey '^[[A' history-substring-search-up
bindkey '^[[B' history-substring-search-down
# bind P and N for EMACS mode You might also want to bind the Control-P/N keys for use in EMACS mode:
bindkey -M emacs '^P' history-substring-search-up
bindkey -M emacs '^N' history-substring-search-down
# bind k and j for VI mode bindkey -M emacs '^P' history-substring-search-up
bindkey -M vicmd 'k' history-substring-search-up bindkey -M emacs '^N' history-substring-search-down
bindkey -M vicmd 'j' history-substring-search-down
You might also want to bind the `k` and `j` keys for use in VI mode:
bindkey -M vicmd 'k' history-substring-search-up
bindkey -M vicmd 'j' history-substring-search-down
3. Type any part of any previous command and then: 3. Type any part of any previous command and then:
* Press the UP arrow key to select the nearest command that (1) contains * Press the `history-substring-search-up` key, which was configured in
your query and (2) is older than the current command in the command step 2 above, to select the nearest command that (1) contains your query
history. and (2) is also older than the current command in your command history.
* Press the DOWN arrow key to select the nearest command that (1) * Press the `history-substring-search-down` key, which was configured in
contains your query and (2) is newer than the current command in the step 2 above, to select the nearest command that (1) contains your query
command history. and (2) is also newer than the current command in your command history.
* Press ^U (the Control and U keys simultaneously) to abort the search. * Press `^U` the Control and U keys simultaneously to abort the search.
4. If a matching command spans more than one line of text, press the LEFT 4. If a matching command spans more than one line of text, press the LEFT
arrow key to move the cursor away from the end of the command, and then: arrow key to move the cursor away from the end of the command, and then:
* Press the UP arrow key to move the cursor to the line above. When the * Press the `history-substring-search-up` key, which was configured in
cursor reaches the first line of the command, pressing the UP arrow step 2 above, to move the cursor to the line above the cursored line.
key again will cause this script to perform another search. When the cursor reaches the first line of the command, pressing the
`history-substring-search-up` key again will cause this script to
perform another search.
* Press the `history-substring-search-down` key, which was configured in
step 2 above, to move the cursor to the line below the cursored line.
When the cursor reaches the last line of the command, pressing the
`history-substring-search-down` key, which was configured in step 2
above, again will cause this script to perform another search.
* Press the DOWN arrow key to move the cursor to the line below. When
the cursor reaches the last line of the command, pressing the DOWN
arrow key again will cause this script to perform another search.
------------------------------------------------------------------------------
Configuration Configuration
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
This script defines the following global variables. You may override their This script defines the following global variables. You may override their
default values only after having loaded this script into your ZSH session. default values only after having loaded this script into your ZSH session.
* HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND is a global variable that defines * `HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND` is a global variable that defines
how the query should be highlighted inside a matching command. Its default how the query should be highlighted inside a matching command. Its default
value causes this script to highlight using bold, white text on a magenta value causes this script to highlight using bold, white text on a magenta
background. See the "Character Highlighting" section in the zshzle(1) man background. See the "Character Highlighting" section in the zshzle(1) man
page to learn about the kinds of values you may assign to this variable. page to learn about the kinds of values you may assign to this variable.
* HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND is a global variable that * `HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND` is a global variable that
defines how the query should be highlighted when no commands in the defines how the query should be highlighted when no commands in the
history match it. Its default value causes this script to highlight using history match it. Its default value causes this script to highlight using
bold, white text on a red background. See the "Character Highlighting" bold, white text on a red background. See the "Character Highlighting"
section in the zshzle(1) man page to learn about the kinds of values you section in the zshzle(1) man page to learn about the kinds of values you
may assign to this variable. may assign to this variable.
* HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS is a global variable that defines * `HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS` is a global variable that defines
how the command history will be searched for your query. Its default value how the command history will be searched for your query. Its default value
causes this script to perform a case-insensitive search. See the "Globbing causes this script to perform a case-insensitive search. See the "Globbing
Flags" section in the zshexpn(1) man page to learn about the kinds of Flags" section in the zshexpn(1) man page to learn about the kinds of
values you may assign to this variable. values you may assign to this variable.
To always receive _unique_ search results, use `setopt HIST_IGNORE_ALL_DUPS`. * `HISTORY_SUBSTRING_SEARCH_FUZZY` is a global variable that defines
Alternatively, use `setopt HIST_FIND_NO_DUPS` which makes this plugin skip how the command history will be searched for your query. If set to a non-empty
duplicate _adjacent_ search results as you cycle through them---however, this value, causes this script to perform a fuzzy search by words, matching in
does not guarantee that search results are unique: if your search results were given order e.g. `ab c` will match `*ab*c*`
"Dog", "Dog", "HotDog", "Dog", then cycling them gives "Dog", "HotDog", "Dog".
Notice that the "Dog" search result appeared twice as you cycled through them! * `HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE` is a global variable that defines
If you wish to avoid this limitation, then use `setopt HIST_IGNORE_ALL_DUPS`. whether all search results returned are _unique_. If set to a non-empty
value, then only unique search results are presented. This behaviour is off
by default. An alternative way to ensure that search results are unique is
to use `setopt HIST_IGNORE_ALL_DUPS`. If this configuration variable is off
and `setopt HIST_IGNORE_ALL_DUPS` is unset, then `setopt HIST_FIND_NO_DUPS`
is still respected and it makes this script skip duplicate _adjacent_ search
results as you cycle through them, but this does not guarantee that search
results are unique: if your search results were "Dog", "Dog", "HotDog",
"Dog", then cycling them gives "Dog", "HotDog", "Dog". Notice that the "Dog"
search result appeared twice as you cycled through them. If you wish to
receive globally unique search results only once, then use this
configuration variable, or use `setopt HIST_IGNORE_ALL_DUPS`.
------------------------------------------------------------------------------
History History
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
This script was originally written by [Peter Stephenson][2], who published it * September 2009: [Peter Stephenson][2] originally wrote this script and it
to the ZSH users mailing list (thereby making it public domain) in September published to the zsh-users mailing list.
2009. It was later revised by Guido van Steen and released under the BSD
license (see below) as part of [the fizsh project][3] in January 2011. * January 2011: Guido van Steen (@guidovansteen) revised this script and
released it under the 3-clause BSD license as part of [fizsh][3], the
Friendly Interactive ZSHell.
* January 2011: Suraj N. Kurapati (@sunaku) extracted this script from
[fizsh][3] 1.0.1, refactored it heavily, and finally repackaged it as an
[oh-my-zsh plugin][4] and as an independently loadable [ZSH script][5].
It was later extracted from fizsh release 1.0.1, refactored heavily, and * July 2011: Guido van Steen, Suraj N. Kurapati, and Sorin Ionescu
repackaged as both an [oh-my-zsh plugin][4] and as an independently loadable (@sorin-ionescu) [further developed it][4] with Vincent Guerci (@vguerci).
[ZSH script][5] by Suraj N. Kurapati in 2011.
It was [further developed][4] by Guido van Steen, Suraj N. Kurapati, Sorin * March 2016: Geza Lore (@gezalore) greatly refactored it in pull request #55.
Ionescu, and Vincent Guerci in 2011.
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
Oh My Zsh Distribution Notes Oh My Zsh Distribution Notes
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
What you are looking at now is Oh My Zsh's repackaging of zsh-history-substring-search What you are looking at now is Oh My Zsh's repackaging of zsh-history-substring-search
as an OMZ module inside the Oh My Zsh distribution. as an OMZ module inside the Oh My Zsh distribution.
The upstream repo, zsh-users/zsh-history-substring-search, can be found on GitHub at The upstream repo, zsh-users/zsh-history-substring-search, can be found on GitHub at
https://github.com/zsh-users/zsh-history-substring-search. https://github.com/zsh-users/zsh-history-substring-search.
This downstream copy was last updated from the following upstream commit: This downstream copy was last updated from the following upstream commit:
SHA: 2c295432175990c1bb4e90bc13f609daa67a25d6 SHA: 0f80b8eb3368b46e5e573c1d91ae69eb095db3fb
Commit date: 2015-09-28 10:47:34 -0700 Commit date: 2019-05-12 17:35:54 -0700
Everything above this section is a copy of the original upstream's README, so things Everything above this section is a copy of the original upstream's README, so things
may differ slightly when you're using this inside OMZ. In particular, you do not may differ slightly when you're using this inside OMZ. In particular, you do not
need to set up key bindings for the up and down arrows yourself in `~/.zshrc`; the OMZ need to set up key bindings for the up and down arrows yourself in `~/.zshrc`; the OMZ
plugin does that for you. You may still want to set up additional emacs- or vi-specific plugin does that for you. You may still want to set up additional emacs- or vi-specific
bindings as mentioned above. bindings as mentioned above.
# This file integrates the zsh-history-substring-search script into oh-my-zsh. 0=${(%):-%N}
source ${0:A:h}/zsh-history-substring-search.zsh
source "${0:r:r}.zsh"
if test "$CASE_SENSITIVE" = true; then
unset HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS
fi
if test "$DISABLE_COLOR" = true; then
unset HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND
unset HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND
fi
# Bind terminal-specific up and down keys # Bind terminal-specific up and down keys
# Bind in both emacs and vi modes so it works in both, and is not
# sensitive to whether this is loaded before or after the vi-mode plugin
if [[ -n "$terminfo[kcuu1]" ]]; then if [[ -n "$terminfo[kcuu1]" ]]; then
bindkey -M emacs "$terminfo[kcuu1]" history-substring-search-up bindkey -M emacs "$terminfo[kcuu1]" history-substring-search-up
bindkey -M viins "$terminfo[kcuu1]" history-substring-search-up bindkey -M viins "$terminfo[kcuu1]" history-substring-search-up
......
...@@ -6,6 +6,8 @@ ...@@ -6,6 +6,8 @@
# Copyright (c) 2011 Suraj N. Kurapati # Copyright (c) 2011 Suraj N. Kurapati
# Copyright (c) 2011 Sorin Ionescu # Copyright (c) 2011 Sorin Ionescu
# Copyright (c) 2011 Vincent Guerci # Copyright (c) 2011 Vincent Guerci
# Copyright (c) 2016 Geza Lore
# Copyright (c) 2017 Bengt Brodersen
# All rights reserved. # All rights reserved.
# #
# Redistribution and use in source and binary forms, with or without # Redistribution and use in source and binary forms, with or without
...@@ -38,12 +40,30 @@ ...@@ -38,12 +40,30 @@
############################################################################## ##############################################################################
#----------------------------------------------------------------------------- #-----------------------------------------------------------------------------
# configuration variables # declare global configuration variables
#----------------------------------------------------------------------------- #-----------------------------------------------------------------------------
HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND='bg=magenta,fg=white,bold' typeset -g HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND='bg=magenta,fg=white,bold'
HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND='bg=red,fg=white,bold' typeset -g HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND='bg=red,fg=white,bold'
HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS='i' typeset -g HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS='i'
typeset -g HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE=''
typeset -g HISTORY_SUBSTRING_SEARCH_FUZZY=''
#-----------------------------------------------------------------------------
# declare internal global variables
#-----------------------------------------------------------------------------
typeset -g BUFFER MATCH MBEGIN MEND CURSOR
typeset -g _history_substring_search_refresh_display
typeset -g _history_substring_search_query_highlight
typeset -g _history_substring_search_result
typeset -g _history_substring_search_query
typeset -g -a _history_substring_search_query_parts
typeset -g -a _history_substring_search_raw_matches
typeset -g -i _history_substring_search_raw_match_index
typeset -g -a _history_substring_search_matches
typeset -g -i _history_substring_search_match_index
typeset -g -A _history_substring_search_unique_filter
#----------------------------------------------------------------------------- #-----------------------------------------------------------------------------
# the main ZLE widgets # the main ZLE widgets
...@@ -180,62 +200,104 @@ _history-substring-search-begin() { ...@@ -180,62 +200,104 @@ _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;
# For the purpose of highlighting we will also keep fi
# a version without doubly-escaped meta characters.
#
_history_substring_search_query=$BUFFER
#
# Clear the previous result.
#
_history_substring_search_result=''
if [[ -z $BUFFER ]]; then
# #
# $BUFFER contains the text that is in the command-line currently. # If the buffer is empty, we will just act like up-history/down-history
# we put an extra "\\" before meta characters such as "\(" and "\)", # in ZSH, so we do not need to actually search the history. This should
# so that they become "\\\(" and "\\\)". # speed things up a little.
# #
_history_substring_search_query_escaped=${BUFFER//(#m)[\][()|\\*?#<>~^]/\\$MATCH} _history_substring_search_query=
_history_substring_search_query_parts=()
_history_substring_search_raw_matches=()
else
# #
# Find all occurrences of the search query in the history file. # For the purpose of highlighting we keep a copy of the original
# # query string.
# (k) returns the "keys" (history index numbers) instead of the values
# (Oa) reverses the order, because (R) returns results reversed.
# #
_history_substring_search_matches=(${(kOa)history[(R)(#$HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS)*${_history_substring_search_query_escaped}*]}) _history_substring_search_query=$BUFFER
# #
# Define the range of values that $_history_substring_search_match_index # compose search pattern
# can take: [0, $_history_substring_search_matches_count_plus].
# #
_history_substring_search_matches_count=$#_history_substring_search_matches if [[ -n $HISTORY_SUBSTRING_SEARCH_FUZZY ]]; then
_history_substring_search_matches_count_plus=$(( _history_substring_search_matches_count + 1 )) #
_history_substring_search_matches_count_sans=$(( _history_substring_search_matches_count - 1 )) # `=` split string in arguments
#
_history_substring_search_query_parts=(${=_history_substring_search_query})
else
_history_substring_search_query_parts=(${==_history_substring_search_query})
fi
# #
# If $_history_substring_search_match_index is equal to # Escape and join query parts with wildcard character '*' as seperator
# $_history_substring_search_matches_count_plus, this indicates that we # `(j:CHAR:)` join array to string with CHAR as seperator
# are beyond the beginning of $_history_substring_search_matches.
# #
# If $_history_substring_search_match_index is equal to 0, this indicates local search_pattern="*${(j:*:)_history_substring_search_query_parts[@]//(#m)[\][()|\\*?#<>~^]/\\$MATCH}*"
# that we are beyond the end of $_history_substring_search_matches.
# #
# If we have initially pressed "up" we have to initialize # Find all occurrences of the search pattern in the history file.
# $_history_substring_search_match_index to
# $_history_substring_search_matches_count_plus so that it will be
# decreased to $_history_substring_search_matches_count.
# #
# If we have initially pressed "down" we have to initialize # (k) returns the "keys" (history index numbers) instead of the values
# $_history_substring_search_match_index to # (R) returns values in reverse older, so the index of the youngest
# $_history_substring_search_matches_count so that it will be increased to # matching history entry is at the head of the list.
# $_history_substring_search_matches_count_plus.
# #
if [[ $WIDGET == history-substring-search-down ]]; then _history_substring_search_raw_matches=(${(k)history[(R)(#$HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS)${search_pattern}]})
_history_substring_search_match_index=$_history_substring_search_matches_count fi
else
_history_substring_search_match_index=$_history_substring_search_matches_count_plus #
fi # In order to stay as responsive as possible, we will process the raw
# matches lazily (when the user requests the next match) to choose items
# that need to be displayed to the user.
# _history_substring_search_raw_match_index holds the index of the last
# unprocessed entry in _history_substring_search_raw_matches. Any items
# that need to be displayed will be added to
# _history_substring_search_matches.
#
# We use an associative array (_history_substring_search_unique_filter) as
# a 'set' data structure to ensure uniqueness of the results if desired.
# If an entry (key) is in the set (non-empty value), then we have already
# added that entry to _history_substring_search_matches.
#
_history_substring_search_raw_match_index=0
_history_substring_search_matches=()
_history_substring_search_unique_filter=()
#
# If $_history_substring_search_match_index is equal to
# $#_history_substring_search_matches + 1, this indicates that we
# are beyond the end of $_history_substring_search_matches and that we
# have also processed all entries in
# _history_substring_search_raw_matches.
#
# If $#_history_substring_search_match_index is equal to 0, this indicates
# that we are beyond the beginning of $_history_substring_search_matches.
#
# If we have initially pressed "up" we have to initialize
# $_history_substring_search_match_index to 0 so that it will be
# incremented to 1.
#
# If we have initially pressed "down" we have to initialize
# $_history_substring_search_match_index to 1 so that it will be
# decremented to 0.
#
if [[ $WIDGET == history-substring-search-down ]]; then
_history_substring_search_match_index=1
else
_history_substring_search_match_index=0
fi fi
} }
...@@ -255,16 +317,21 @@ _history-substring-search-end() { ...@@ -255,16 +317,21 @@ _history-substring-search-end() {
_zsh_highlight _zsh_highlight
# highlight the search query inside the command line # highlight the search query inside the command line
if [[ -n $_history_substring_search_query_highlight && -n $_history_substring_search_query ]]; then if [[ -n $_history_substring_search_query_highlight ]]; then
# # highlight first matching query parts
# The following expression yields a variable $MBEGIN, which local highlight_start_index=0
# indicates the begin position + 1 of the first occurrence local highlight_end_index=0
# of _history_substring_search_query_escaped in $BUFFER. local query_part
# for query_part in $_history_substring_search_query_parts; do
: ${(S)BUFFER##(#m$HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS)($_history_substring_search_query##)} local escaped_query_part=${query_part//(#m)[\][()|\\*?#<>~^]/\\$MATCH}
local begin=$(( MBEGIN - 1 )) # (i) get index of pattern
local end=$(( begin + $#_history_substring_search_query )) local query_part_match_index="${${BUFFER:$highlight_start_index}[(i)(#$HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS)${escaped_query_part}]}"
region_highlight+=("$begin $end $_history_substring_search_query_highlight") if [[ $query_part_match_index -le ${#BUFFER:$highlight_start_index} ]]; then
highlight_start_index=$(( $highlight_start_index + $query_part_match_index ))
highlight_end_index=$(( $highlight_start_index + ${#query_part} ))
region_highlight+=("$(($highlight_start_index - 1)) $(($highlight_end_index - 1)) $_history_substring_search_query_highlight")
fi
done
fi fi
# For debugging purposes: # For debugging purposes:
...@@ -378,12 +445,143 @@ _history-substring-search-down-history() { ...@@ -378,12 +445,143 @@ _history-substring-search-down-history() {
return 1 return 1
} }
_history_substring_search_process_raw_matches() {
#
# Process more outstanding raw matches and append any matches that need to
# be displayed to the user to _history_substring_search_matches.
# Return whether there were any more results appended.
#
#
# While we have more raw matches. Process them to see if there are any more
# matches that need to be displayed to the user.
#
while [[ $_history_substring_search_raw_match_index -lt $#_history_substring_search_raw_matches ]]; do
#
# Move on to the next raw entry and get its history index.
#
_history_substring_search_raw_match_index+=1
local index=${_history_substring_search_raw_matches[$_history_substring_search_raw_match_index]}
#
# If HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE is set to a non-empty value,
# then ensure that only unique matches are presented to the user.
# When HIST_IGNORE_ALL_DUPS is set, ZSH already ensures a unique history,
# so in this case we do not need to do anything.
#
if [[ ! -o HIST_IGNORE_ALL_DUPS && -n $HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE ]]; then
#
# Get the actual history entry at the new index, and check if we have
# already added it to _history_substring_search_matches.
#
local entry=${history[$index]}
if [[ -z ${_history_substring_search_unique_filter[$entry]} ]]; then
#
# This is a new unique entry. Add it to the filter and append the
# index to _history_substring_search_matches.
#
_history_substring_search_unique_filter[$entry]=1
_history_substring_search_matches+=($index)
#
# Indicate that we did find a match.
#
return 0
fi
else
#
# Just append the new history index to the processed matches.
#
_history_substring_search_matches+=($index)
#
# Indicate that we did find a match.
#
return 0
fi
done
#
# We are beyond the end of the list of raw matches. Indicate that no
# more matches are available.
#
return 1
}
_history-substring-search-has-next() {
#
# Predicate function that returns whether any more older matches are
# available.
#
if [[ $_history_substring_search_match_index -lt $#_history_substring_search_matches ]]; then
#
# We did not reach the end of the processed list, so we do have further
# matches.
#
return 0
else
#
# We are at the end of the processed list. Try to process further
# unprocessed matches. _history_substring_search_process_raw_matches
# returns whether any more matches were available, so just return
# that result.
#
_history_substring_search_process_raw_matches
return $?
fi
}
_history-substring-search-has-prev() {
#
# Predicate function that returns whether any more younger matches are
# available.
#
if [[ $_history_substring_search_match_index -gt 1 ]]; then
#
# We did not reach the beginning of the processed list, so we do have
# further matches.
#
return 0
else
#
# We are at the beginning of the processed list. We do not have any more
# matches.
#
return 1
fi
}
_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
} }
...@@ -392,91 +590,84 @@ _history-substring-search-up-search() { ...@@ -392,91 +590,84 @@ _history-substring-search-up-search() {
_history_substring_search_refresh_display=1 _history_substring_search_refresh_display=1
# #
# Highlight matches during history-substring-up-search: # Select history entry during history-substring-down-search:
# #
# The following constants have been initialized in # The following variables 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 that
# $_history_substring_search_matches_count is the current number of matches # need to be displayed to the user.
# $_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
# that is being displayed to the user.
# #
# 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 + 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 + 1 indicates that we are beyond
# the beginning of $_history_substring_search_matches. # the end of $_history_substring_search_matches and that we have also
# processed all entries in _history_substring_search_raw_matches.
#
# If $_history_substring_search_match_index equals
# $#_history_substring_search_matches and
# $_history_substring_search_raw_match_index is not greater than
# $#_history_substring_search_raw_matches, then we need to further process
# $_history_substring_search_raw_matches to see if there are any more
# entries that need to be displayed to the user.
# #
# 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
#
# Highlight the next match:
#
# 1. Decrease the value of $_history_substring_search_match_index.
#
# 2. Use $HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND
# to highlight the current buffer.
#
(( _history_substring_search_match_index-- ))
BUFFER=$history[$_history_substring_search_matches[$_history_substring_search_match_index]]
_history_substring_search_query_highlight=$HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND
elif [[ $_history_substring_search_match_index -eq 1 ]]; then if [[ $_history_substring_search_match_index -gt $#_history_substring_search_matches ]]; then
#
# We will move beyond the end of $_history_substring_search_matches:
#
# 1. Decrease the value of $_history_substring_search_match_index.
#
# 2. Save the current buffer in $_history_substring_search_old_buffer,
# so that it can be retrieved by
# _history-substring-search-down-search() later.
# #
# 3. Make $BUFFER equal to $_history_substring_search_query. # We are beyond the end of $_history_substring_search_matches. This
# can only happen if we have also exhausted the unprocessed matches in
# _history_substring_search_raw_matches.
# #
# 4. Use $HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND # 1. Update display to indicate search not found.
# to highlight the current buffer.
# #
(( _history_substring_search_match_index-- ))
_history-substring-search-not-found _history-substring-search-not-found
return
fi
elif [[ $_history_substring_search_match_index -eq $_history_substring_search_matches_count_plus ]]; then if _history-substring-search-has-next; 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. # We do have older matches.
# #
# 3. Use $HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND # 1. Move index to point to the next match.
# to highlight the current buffer. # 2. Update display to indicate search found.
# #
(( _history_substring_search_match_index-- )) _history_substring_search_match_index+=1
BUFFER=$_history_substring_search_old_buffer _history-substring-search-found
_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 do not have older matches.
# #
# 1. Move the index beyond the end of
# _history_substring_search_matches.
# 2. Update display to indicate search not found.
#
_history_substring_search_match_index+=1
_history-substring-search-not-found _history-substring-search-not-found
return
fi fi
# #
# When HIST_FIND_NO_DUPS is set, meaning that only unique command lines from # When HIST_FIND_NO_DUPS is set, meaning that only unique command lines from
# history should be matched, make sure the new and old results are different. # history should be matched, make sure the new and old results are different.
# But when HIST_IGNORE_ALL_DUPS is set, ZSH already ensures a unique history.
# #
if [[ ! -o HIST_IGNORE_ALL_DUPS && -o HIST_FIND_NO_DUPS && $BUFFER == $_history_substring_search_result ]]; then # However, if the HIST_IGNORE_ALL_DUPS shell option, or
# HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE is set, then we already have a
# unique history, so in this case we do not need to do anything.
#
if [[ -o HIST_IGNORE_ALL_DUPS || -n $HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE ]]; then
return
fi
if [[ -o HIST_FIND_NO_DUPS && $BUFFER == $_history_substring_search_result ]]; then
# #
# Repeat the current search so that a different (unique) match is found. # Repeat the current search so that a different (unique) match is found.
# #
...@@ -488,92 +679,75 @@ _history-substring-search-down-search() { ...@@ -488,92 +679,75 @@ _history-substring-search-down-search() {
_history_substring_search_refresh_display=1 _history_substring_search_refresh_display=1
# #
# Highlight matches during history-substring-up-search: # Select history entry during history-substring-down-search:
# #
# The following constants have been initialized in # The following variables 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 that
# $_history_substring_search_matches_count is the current number of matches # need to be displayed to the user.
# $_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
# that is being displayed to the user.
# #
# 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 + 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 + 1 indicates that we are beyond
# the beginning of $_history_substring_search_matches. # the end of $_history_substring_search_matches and that we have also
# processed all entries in _history_substring_search_raw_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
#
# Highlight the next match:
#
# 1. Increase $_history_substring_search_match_index by 1.
#
# 2. Use $HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND
# to highlight the current buffer.
#
(( _history_substring_search_match_index++ ))
BUFFER=$history[$_history_substring_search_matches[$_history_substring_search_match_index]]
_history_substring_search_query_highlight=$HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND
elif [[ $_history_substring_search_match_index -eq $_history_substring_search_matches_count ]]; then if [[ $_history_substring_search_match_index -lt 1 ]]; then
#
# We will move beyond the beginning of $_history_substring_search_matches:
#
# 1. Increase $_history_substring_search_match_index by 1.
# #
# 2. Save the current buffer in $_history_substring_search_old_buffer, so # We are beyond the beginning of $_history_substring_search_matches.
# that it can be retrieved by _history-substring-search-up-search()
# later.
# #
# 3. Make $BUFFER equal to $_history_substring_search_query. # 1. Update display to indicate search not found.
# #
# 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
fi
elif [[ $_history_substring_search_match_index -eq 0 ]]; then if _history-substring-search-has-prev; 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. # We do have younger matches.
# #
# 2. Restore $BUFFER from $_history_substring_search_old_buffer. # 1. Move index to point to the previous match.
# 2. Update display to indicate search found.
# #
# 3. Use $HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND _history_substring_search_match_index+=-1
# to highlight the current buffer. _history-substring-search-found
#
(( _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 do not have younger matches.
#
# 1. Move the index beyond the beginning of
# _history_substring_search_matches.
# 2. Update display to indicate search not found.
# #
_history_substring_search_match_index+=-1
_history-substring-search-not-found _history-substring-search-not-found
return
fi fi
# #
# When HIST_FIND_NO_DUPS is set, meaning that only unique command lines from # When HIST_FIND_NO_DUPS is set, meaning that only unique command lines from
# history should be matched, make sure the new and old results are different. # history should be matched, make sure the new and old results are different.
# But when HIST_IGNORE_ALL_DUPS is set, ZSH already ensures a unique history.
# #
if [[ ! -o HIST_IGNORE_ALL_DUPS && -o HIST_FIND_NO_DUPS && $BUFFER == $_history_substring_search_result ]]; then # However, if the HIST_IGNORE_ALL_DUPS shell option, or
# HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE is set, then we already have a
# unique history, so in this case we do not need to do anything.
#
if [[ -o HIST_IGNORE_ALL_DUPS || -n $HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE ]]; then
return
fi
if [[ -o HIST_FIND_NO_DUPS && $BUFFER == $_history_substring_search_result ]]; then
# #
# Repeat the current search so that a different (unique) match is found. # Repeat the current search so that a different (unique) match is found.
# #
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment