31 Commits
Author SHA1 Message Date
Guido van SteenandGitHub 400e58a87f Merge pull request #135 from jirutka/redrawhook
Fix compatibility with latest zsh-syntax-highlighting (zle-line-pre-redraw) and use memo= feature
2023-01-03 20:25:44 +01:00
Jakub Jirutka fcbdb89754 Use new zsh 'memo=' feature to improve interoperability with others
See https://github.com/zsh-users/zsh-syntax-highlighting/commit/810c2dcedebcfef5458c0bbd5e2e01d63adc7059

I'm not sure if it should be used even in our dummy _zsh_highlight or
not and, to be honest, I couldn't figure out why is this dummy
_zsh_highlight needed at all, so I left it unchanged.
2022-08-07 17:19:08 +02:00
Jakub Jirutka b4fb93de0e Fix compatibility with latest zsh-syntax-highlighting
zsh-syntax-highlighting started using zle-line-pre-redraw hook instead
of the legacy "bind all widgets" if 1) zsh has the memo= feature (added
in version 5.9) and 2) add-zle-hook-widget is available.

Now when zsh-history-substring-search is loaded before
zsh-syntax-highlighting, it causes error:

    _zsh_highlight_widget_zle-line-pre-redraw: job table full or recursion limit exceeded

See https://github.com/zsh-users/zsh-syntax-highlighting/pull/749
2022-08-07 17:08:21 +02:00
Jakub Jirutka 563a551165 Use SPDX snippet tags instead of copy-pasting full license text
Based on https://github.com/spdx/spdx-spec/pull/719
2022-08-07 14:39:08 +02:00
Suraj N. Kurapati 4abed97b6e Merge pull request #97 from partcyborg/master
Also rename `HISTORY_SUBSTRING_SEARCH_PREFIX` variable by adding "ED"
suffix so that it reads more like a special mode of operation rather
than an instruction to prepend a specified prefix to matched commands.
2021-08-02 23:19:13 -07:00
Suraj N. KurapatiandGitHub e310a75a52 Merge pull request #117 from ivopauly/patch-1
README: Update plugin name in Oh-My-Zsh activation instructions.
2021-08-02 22:55:40 -07:00
Suraj N. KurapatiandGitHub c765a4a17c Merge pull request #108 from ericbn/dont-overwrite-config
Don't overwrite config with default values
2021-08-02 22:45:42 -07:00
Eric Nielsen 4ee70abb6f Don't overwrite config with default values
otherwise users are obliged to set the config values *after* sourcing
the plugin. They're not able to do it before. Also, re-sourcing the
plugin will reset the values to the defaults again.

A similar change was done in zsh-autosuggestions in the past:
https://github.com/zsh-users/zsh-autosuggestions/commit/9e4d3c3
2021-08-02 15:23:57 -05:00
4f2f17cc46 add HISTORY_SUBSTRING_SEARCH_PREFIX (#112,#115)
* `HISTORY_SUBSTRING_SEARCH_PREFIX` is a global variable that defines
  how the command history will be searched for your query. If set to a non-empty
  value, only history prefixed by your query will be matched. For example,
  if this variable is empty, `ls` will match `ls -l` and `echo ls`; if it is
  non-empty, `ls` will only match `ls -l`.

Co-authored-by: Xue Qianming <qianmingxue@microsoft.com>
2021-08-02 10:20:14 -07:00
Ivo PaulyandGitHub 56dc8c2ec4 Update README.md
Plugin name isn't correct in instructions
2020-12-24 10:07:20 +01:00
Part Cyborg f48193bcd9 Declare HISTORY_SUBSTRING_SEARCH_ANCHORED global 2020-07-03 15:57:16 -07:00
Suraj N. Kurapati 0f80b8eb33 GH-92: also try bindkey with $terminfo for arrows 2019-05-12 17:35:54 -07:00
Suraj N. KurapatiandGitHub 2c36e4dd3a Merge pull request #98 from FranciscoBorges/master
Declare `query_part` as local to avoid warnings due to `WARN_CREATE_GLOBAL`
2019-05-12 17:18:38 -07:00
Suraj N. KurapatiandGitHub 79d3619704 Merge pull request #99 from d10n/fix-zsh-4.3-regressions
Fix zsh 4.3 regressions
2019-05-12 17:17:45 -07:00
d10n 19cb0eb8be Quote variable to highlight right words on zsh 4.3
When the variable is not quoted, there seems to be some difference in
evaluating the line between zsh 4.3 and 5.6
2018-12-26 19:43:49 -05:00
d10n 36505c46c7 Explicitly disable word splitting when not fuzzy
This prevents shell configuration from breaking fuzzy search
configuration
2018-12-26 19:42:36 -05:00
Francisco Borges 8d4a409c89 Declare query_part as local to avoid warnings due to WARN_CREATE_GLOBAL 2018-12-11 10:21:28 +01:00
Part Cyborg 3f8e85f3f8 Support anchoring the substring to search for
Add a new config variable that will anchor the history search to the beginning of the command.

Note that the default behavior does not change with this
2018-10-01 05:15:02 -07:00
Bastian HeistandSuraj N. Kurapati 47a7d416c6 Fix typo in README.md (#91)
The plugin line needed to contain "history-substring-search" (not zsh-history-substring-search) to work for me.
2018-02-21 10:15:09 -08:00
David SuileaandSuraj N. Kurapati d44159b5e8 support for installing as Oh-My-Zsh plugin (#87) 2017-08-03 11:15:30 -07:00
Suraj N. KurapatiandGitHub 4594acaa4f Merge pull request #86 from iroedius/master
also ignore files ending in .zwc.old
2017-08-02 11:20:53 -07:00
iroedius c35b4d1e8c also ignore files ending in .zwc.old 2017-07-20 14:52:31 +02:00
VithonandGeza Lore 933733e867 Update README.md
With Homebrew/homebrew-core#13567 script is now installed in a different location. updating README.md to reflect
2017-05-20 20:40:29 +01:00
Geza Lore be0fe1fca9 Clean up global variable declarations
- Remove duplicate declarations
- Separate internal variables from configuration variables
2017-05-16 20:07:06 +01:00
Bengt BrodersenandGeza Lore fd27842cad feat: add fuzzy search 2017-05-16 19:49:29 +01:00
Bengt BrodersenandSuraj N. Kurapati 563c88c4ca README: add installation instructions for Homebrew 2017-05-15 22:42:16 -07:00
Geza LoreandGitHub c750a17757 Merge pull request #67 from gezalore/master
Fix breakage with setopt ERR_RETURN (Issue #60)
2017-05-05 21:45:55 +01:00
Geza Lore 07011f632b Fix breakage with setopt ERR_RETURN (Issue #60) 2017-05-05 19:20:48 +01:00
Suraj N. KurapatiandGitHub 2b6451de03 README: upgrade to CommonMark; move <C-v> note up 2017-05-05 03:34:00 -07:00
Suraj N. KurapatiandGitHub 21c025eb33 Merge pull request #78 from RuRo/patch-1
Updated README.md keybinding instructions.
2017-05-05 03:24:21 -07:00
RuRoandGitHub 34fdd6b1b2 Updated README.md keybinding instructions.
The solution I propose to add to the README was found in issue https://github.com/zsh-users/zsh-history-substring-search/issues/64#issuecomment-289208209 .
2017-05-03 00:50:18 +03:00
4 changed files with 242 additions and 117 deletions
+2 -2
View File
@@ -1,2 +1,2 @@
# zsh word code files (zcompile)
*.zwc
# zsh word code files (zcompile and zrecompile)
*.zwc*
+49 -9
View File
@@ -17,6 +17,27 @@ Requirements
* [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...] zsh-history-substring-search)
3. Source `~/.zshrc` to take changes into account:
source ~/.zshrc
Usage
------------------------------------------------------------------------------
@@ -35,24 +56,32 @@ Usage
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
bindkey '^[[A' history-substring-search-up
bindkey '^[[B' history-substring-search-down
You might also want to bind the Control-P/N keys for use in EMACS mode:
However, if the observed values don't work, you can try using terminfo:
bindkey -M emacs '^P' history-substring-search-up
bindkey -M emacs '^N' history-substring-search-down
bindkey "$terminfo[kcuu1]" history-substring-search-up
bindkey "$terminfo[kcud1]" history-substring-search-down
You might also want to bind the `k` and `j` keys for use in VI mode:
You might also want to bind the Control-P/N keys for use in EMACS mode:
bindkey -M vicmd 'k' history-substring-search-up
bindkey -M vicmd 'j' history-substring-search-down
bindkey -M emacs '^P' history-substring-search-up
bindkey -M emacs '^N' 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:
@@ -86,7 +115,7 @@ Configuration
------------------------------------------------------------------------------
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.
* `HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND` is a global variable that defines
how the query should be highlighted inside a matching command. Its default
@@ -107,6 +136,17 @@ default values only after having loaded this script into your ZSH session.
Flags" section in the zshexpn(1) man page to learn about the kinds of
values you may assign to this variable.
* `HISTORY_SUBSTRING_SEARCH_FUZZY` is a global variable that defines
how the command history will be searched for your query. If set to a non-empty
value, causes this script to perform a fuzzy search by words, matching in
given order e.g. `ab c` will match `*ab*c*`
* `HISTORY_SUBSTRING_SEARCH_PREFIXED` is a global variable that defines how
the command history will be searched for your query. If set to a non-empty
value, your query will be matched against the start of each history entry.
For example, if this variable is empty, `ls` will match `ls -l` and `echo
ls`; if it is non-empty, `ls` will only match `ls -l`.
* `HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE` is a global variable that defines
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
+2
View File
@@ -0,0 +1,2 @@
0=${(%):-%N}
source ${0:A:h}/zsh-history-substring-search.zsh
+189 -106
View File
@@ -7,6 +7,7 @@
# Copyright (c) 2011 Sorin Ionescu
# Copyright (c) 2011 Vincent Guerci
# Copyright (c) 2016 Geza Lore
# Copyright (c) 2017 Bengt Brodersen
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
@@ -39,34 +40,32 @@
##############################################################################
#-----------------------------------------------------------------------------
# declare global variables
# declare global configuration variables
#-----------------------------------------------------------------------------
: ${HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND='bg=magenta,fg=white,bold'}
: ${HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND='bg=red,fg=white,bold'}
: ${HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS='i'}
: ${HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE=''}
: ${HISTORY_SUBSTRING_SEARCH_FUZZY=''}
: ${HISTORY_SUBSTRING_SEARCH_PREFIXED=''}
#-----------------------------------------------------------------------------
# declare internal global variables
#-----------------------------------------------------------------------------
typeset -g BUFFER MATCH MBEGIN MEND CURSOR
typeset -g HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND
typeset -g HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND
typeset -g HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS
typeset -g HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE
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_raw_matches
typeset -g _history_substring_search_raw_match_index
typeset -g -A _history_substring_search_matches
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
typeset -g _history_substring_search_match_index
#-----------------------------------------------------------------------------
# configuration variables
#-----------------------------------------------------------------------------
typeset -g HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND='bg=magenta,fg=white,bold'
typeset -g HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND='bg=red,fg=white,bold'
typeset -g HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS='i'
typeset -g HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE=''
typeset -g _history_substring_search_{refresh_display,query_highlight,result,query,match_index,raw_match_index}
typeset -ga _history_substring_search{,_raw}_matches
typeset -g -i _history_substring_search_zsh_5_9
#-----------------------------------------------------------------------------
# the main ZLE widgets
@@ -100,6 +99,11 @@ zle -N history-substring-search-down
#-----------------------------------------------------------------------------
zmodload -F zsh/parameter
autoload -Uz is-at-least
if is-at-least 5.9 $ZSH_VERSION; then
_history_substring_search_zsh_5_9=1
fi
#
# We have to "override" some keys and widgets if the
@@ -120,80 +124,125 @@ if [[ $+functions[_zsh_highlight] -eq 0 ]]; then
}
#
# The following snippet was taken from the zsh-syntax-highlighting project:
# Check if $1 denotes the name of a callable function, i.e. it is fully
# defined or it is marked for autoloading and autoloading it at the first
# call to it will succeed. In particular, if $1 has been marked for
# autoloading but is not available in $fpath, then it will return 1 (false).
#
# https://github.com/zsh-users/zsh-syntax-highlighting/blob/56b134f5d62ae3d4e66c7f52bd0cc2595f9b305b/zsh-syntax-highlighting.zsh#L126-161
# This is based on the zsh-syntax-highlighting plugin.
#
# Copyright (c) 2010-2011 zsh-syntax-highlighting contributors
# All rights reserved.
_history-substring-search-function-callable() {
if (( ${+functions[$1]} )) && ! [[ "$functions[$1]" == *"builtin autoload -X"* ]]; then
return 0 # already fully loaded
else
# "$1" is either an autoload stub, or not a function at all.
# We expect 'autoload +X' to return non-zero if it fails to fully load
# the function.
( autoload -U +X -- "$1" 2>/dev/null )
return $?
fi
}
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
# The zsh-syntax-highlighting plugin uses zle-line-pre-redraw hook instead
# of the legacy "bind all widgets" if 1) zsh has the memo= feature (added in
# version 5.9) and 2) add-zle-hook-widget is available.
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# * Neither the name of the zsh-syntax-highlighting contributors nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#--------------8<-------------------8<-------------------8<-----------------
# Rebind all ZLE widgets to make them invoke _zsh_highlights.
_zsh_highlight_bind_widgets()
{
# Load ZSH module zsh/zleparameter, needed to override user defined widgets.
zmodload zsh/zleparameter 2>/dev/null || {
echo 'zsh-syntax-highlighting: failed loading zsh/zleparameter.' >&2
return 1
if [[ $_history_substring_search_zsh_5_9 -eq 1 ]] && _history-substring-search-function-callable add-zle-hook-widget; then
#
# The following code is based on the zsh-syntax-highlighting plugin.
#
autoload -U add-zle-hook-widget
_history-substring-search-zle-line-finish() {
#
# Reset $WIDGET since the 'main' highlighter depends on it.
#
# Since $WIDGET is declared by zle as read-only in this function's scope,
# a nested function is required in order to shadow its built-in value;
# see "User-defined widgets" in zshall.
#
() {
local -h -r WIDGET=zle-line-finish
_zsh_highlight
}
}
# Override ZLE widgets to make them invoke _zsh_highlight.
local cur_widget
for cur_widget in ${${(f)"$(builtin zle -la)"}:#(.*|_*|orig-*|run-help|which-command|beep|yank*)}; do
case $widgets[$cur_widget] in
_history-substring-search-zle-line-pre-redraw() {
#
# If the zsh-syntax-highlighting plugin has been loaded (after our plugin
# plugin, otherwise this hook wouldn't be called), remove our hooks.
#
if [[ $+ZSH_HIGHLIGHT_VERSION -eq 1 ]]; then
autoload -U add-zle-hook-widget
add-zle-hook-widget -d zle-line-pre-redraw _history-substring-search-zle-line-pre-redraw
add-zle-hook-widget -d zle-line-finish _history-substring-search-zle-line-finish
return 0
fi
#
# Set $? to 0 for _zsh_highlight. Without this, subsequent
# zle-line-pre-redraw hooks won't run, since add-zle-hook-widget happens to
# call us with $? == 1 in the common case.
#
true && _zsh_highlight "$@"
}
# Already rebound event: do nothing.
user:$cur_widget|user:_zsh_highlight_widget_*);;
if [[ -o zle ]]; then
add-zle-hook-widget zle-line-pre-redraw _history-substring-search-zle-line-pre-redraw
add-zle-hook-widget zle-line-finish _history-substring-search-zle-line-finish
fi
else
#
# The following snippet was taken from the zsh-syntax-highlighting project:
# https://github.com/zsh-users/zsh-syntax-highlighting/blob/56b134f5d62ae3d4e66c7f52bd0cc2595f9b305b/zsh-syntax-highlighting.zsh#L126-161
#
# SPDX-SnippetBegin
# SPDX-License-Identifier: BSD-3-Clause
# SPDX-SnippetCopyrightText: 2010-2011 zsh-syntax-highlighting contributors
#--------------8<-------------------8<-------------------8<-----------------
# Rebind all ZLE widgets to make them invoke _zsh_highlights.
_zsh_highlight_bind_widgets()
{
# Load ZSH module zsh/zleparameter, needed to override user defined widgets.
zmodload zsh/zleparameter 2>/dev/null || {
echo 'zsh-syntax-highlighting: failed loading zsh/zleparameter.' >&2
return 1
}
# User defined widget: override and rebind old one with prefix "orig-".
user:*) eval "zle -N orig-$cur_widget ${widgets[$cur_widget]#*:}; \
_zsh_highlight_widget_$cur_widget() { builtin zle orig-$cur_widget -- \"\$@\" && _zsh_highlight }; \
zle -N $cur_widget _zsh_highlight_widget_$cur_widget";;
# Override ZLE widgets to make them invoke _zsh_highlight.
local cur_widget
for cur_widget in ${${(f)"$(builtin zle -la)"}:#(.*|_*|orig-*|run-help|which-command|beep|yank*)}; do
case $widgets[$cur_widget] in
# Completion widget: override and rebind old one with prefix "orig-".
completion:*) eval "zle -C orig-$cur_widget ${${widgets[$cur_widget]#*:}/:/ }; \
_zsh_highlight_widget_$cur_widget() { builtin zle orig-$cur_widget -- \"\$@\" && _zsh_highlight }; \
zle -N $cur_widget _zsh_highlight_widget_$cur_widget";;
# Already rebound event: do nothing.
user:$cur_widget|user:_zsh_highlight_widget_*);;
# Builtin widget: override and make it call the builtin ".widget".
builtin) eval "_zsh_highlight_widget_$cur_widget() { builtin zle .$cur_widget -- \"\$@\" && _zsh_highlight }; \
zle -N $cur_widget _zsh_highlight_widget_$cur_widget";;
# User defined widget: override and rebind old one with prefix "orig-".
user:*) eval "zle -N orig-$cur_widget ${widgets[$cur_widget]#*:}; \
_zsh_highlight_widget_$cur_widget() { builtin zle orig-$cur_widget -- \"\$@\" && _zsh_highlight }; \
zle -N $cur_widget _zsh_highlight_widget_$cur_widget";;
# Default: unhandled case.
*) echo "zsh-syntax-highlighting: unhandled ZLE widget '$cur_widget'" >&2 ;;
esac
done
}
#-------------->8------------------->8------------------->8-----------------
# Completion widget: override and rebind old one with prefix "orig-".
completion:*) eval "zle -C orig-$cur_widget ${${widgets[$cur_widget]#*:}/:/ }; \
_zsh_highlight_widget_$cur_widget() { builtin zle orig-$cur_widget -- \"\$@\" && _zsh_highlight }; \
zle -N $cur_widget _zsh_highlight_widget_$cur_widget";;
_zsh_highlight_bind_widgets
# Builtin widget: override and make it call the builtin ".widget".
builtin) eval "_zsh_highlight_widget_$cur_widget() { builtin zle .$cur_widget -- \"\$@\" && _zsh_highlight }; \
zle -N $cur_widget _zsh_highlight_widget_$cur_widget";;
# Default: unhandled case.
*) echo "zsh-syntax-highlighting: unhandled ZLE widget '$cur_widget'" >&2 ;;
esac
done
}
#-------------->8------------------->8------------------->8-----------------
# SPDX-SnippetEnd
_zsh_highlight_bind_widgets
fi
unfunction _history-substring-search-function-callable
fi
_history-substring-search-begin() {
@@ -223,6 +272,7 @@ _history-substring-search-begin() {
# speed things up a little.
#
_history_substring_search_query=
_history_substring_search_query_parts=()
_history_substring_search_raw_matches=()
else
@@ -233,20 +283,38 @@ _history-substring-search-begin() {
_history_substring_search_query=$BUFFER
#
# $BUFFER contains the text that is in the command-line currently.
# we put an extra "\\" before meta characters such as "\(" and "\)",
# so that they become "\\\(" and "\\\)".
# compose search pattern
#
local escaped_query=${BUFFER//(#m)[\][()|\\*?#<>~^]/\\$MATCH}
if [[ -n $HISTORY_SUBSTRING_SEARCH_FUZZY ]]; then
#
# `=` split string in arguments
#
_history_substring_search_query_parts=(${=_history_substring_search_query})
else
_history_substring_search_query_parts=(${==_history_substring_search_query})
fi
#
# Find all occurrences of the search query in the history file.
# Escape and join query parts with wildcard character '*' as seperator
# `(j:CHAR:)` join array to string with CHAR as seperator
#
local search_pattern="${(j:*:)_history_substring_search_query_parts[@]//(#m)[\][()|\\*?#<>~^]/\\$MATCH}*"
#
# Support anchoring history search to the beginning of the command
#
if [[ -z $HISTORY_SUBSTRING_SEARCH_PREFIXED ]]; then
search_pattern="*${search_pattern}"
fi
#
# Find all occurrences of the search pattern in the history file.
#
# (k) returns the "keys" (history index numbers) instead of the values
# (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_raw_matches=(${(k)history[(R)(#$HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS)*${escaped_query}*]})
_history_substring_search_raw_matches=(${(k)history[(R)(#$HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS)${search_pattern}]})
fi
#
@@ -265,8 +333,7 @@ _history-substring-search-begin() {
#
_history_substring_search_raw_match_index=0
_history_substring_search_matches=()
unset _history_substring_search_unique_filter
typeset -A -g _history_substring_search_unique_filter
_history_substring_search_unique_filter=()
#
# If $_history_substring_search_match_index is equal to
@@ -296,12 +363,21 @@ _history-substring-search-begin() {
_history-substring-search-end() {
setopt localoptions extendedglob
local highlight_memo=
_history_substring_search_result=$BUFFER
if [[ $_history_substring_search_zsh_5_9 -eq 1 ]]; then
highlight_memo='memo=history-substring-search'
fi
# 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
if [[ $_history_substring_search_refresh_display -eq 1 ]]; then
region_highlight=()
if [[ -n $highlight_memo ]]; then
region_highlight=( "${(@)region_highlight:#*${highlight_memo}*}" )
else
region_highlight=()
fi
CURSOR=${#BUFFER}
fi
@@ -309,16 +385,23 @@ _history-substring-search-end() {
_zsh_highlight
# highlight the search query inside the command line
if [[ -n $_history_substring_search_query_highlight && -n $_history_substring_search_query ]]; then
#
# The following expression yields a variable $MBEGIN, which
# indicates the begin position + 1 of the first occurrence
# of _history_substring_search_query in $BUFFER.
#
: ${(S)BUFFER##(#m$HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS)($_history_substring_search_query##)}
local begin=$(( MBEGIN - 1 ))
local end=$(( begin + $#_history_substring_search_query ))
region_highlight+=("$begin $end $_history_substring_search_query_highlight")
if [[ -n $_history_substring_search_query_highlight ]]; then
# highlight first matching query parts
local highlight_start_index=0
local highlight_end_index=0
local query_part
for query_part in $_history_substring_search_query_parts; do
local escaped_query_part=${query_part//(#m)[\][()|\\*?#<>~^]/\\$MATCH}
# (i) get index of pattern
local query_part_match_index="${${BUFFER:$highlight_start_index}[(i)(#$HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS)${escaped_query_part}]}"
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}${highlight_memo:+,$highlight_memo}"
)
fi
done
fi
# For debugging purposes:
@@ -447,7 +530,7 @@ _history_substring_search_process_raw_matches() {
#
# Move on to the next raw entry and get its history index.
#
(( _history_substring_search_raw_match_index++ ))
_history_substring_search_raw_match_index+=1
local index=${_history_substring_search_raw_matches[$_history_substring_search_raw_match_index]}
#
@@ -627,7 +710,7 @@ _history-substring-search-up-search() {
# 1. Move index to point to the next match.
# 2. Update display to indicate search found.
#
(( _history_substring_search_match_index++ ))
_history_substring_search_match_index+=1
_history-substring-search-found
else
@@ -638,7 +721,7 @@ _history-substring-search-up-search() {
# _history_substring_search_matches.
# 2. Update display to indicate search not found.
#
(( _history_substring_search_match_index++ ))
_history_substring_search_match_index+=1
_history-substring-search-not-found
fi
@@ -707,7 +790,7 @@ _history-substring-search-down-search() {
# 1. Move index to point to the previous match.
# 2. Update display to indicate search found.
#
(( _history_substring_search_match_index-- ))
_history_substring_search_match_index+=-1
_history-substring-search-found
else
@@ -718,7 +801,7 @@ _history-substring-search-down-search() {
# _history_substring_search_matches.
# 2. Update display to indicate search not found.
#
(( _history_substring_search_match_index-- ))
_history_substring_search_match_index+=-1
_history-substring-search-not-found
fi