WIP implementing a historywords suggestion strategy

This commit is contained in:
Eric Freese 2019-06-23 13:39:00 -06:00
parent c80605595c
commit 36dae44064
2 changed files with 56 additions and 0 deletions

View file

@ -0,0 +1,28 @@
#--------------------------------------------------------------------#
# History Words Suggestion Strategy #
#--------------------------------------------------------------------#
# Suggests the most recent history word that matches the given
# prefix.
#
_zsh_autosuggest_strategy_historywords() {
# Reset options to defaults and enable LOCAL_OPTIONS
emulate -L zsh
# Enable globbing flags so that we can use (#m)
setopt EXTENDED_GLOB
local last_word="${${=1}[-1]}"
# Escape backslashes and all of the glob operators so we can use
# this string as a pattern to search the $history associative array.
# - (#m) globbing flag enables setting references for match data
# TODO: Use (b) flag when we can drop support for zsh older than v5.0.8
local prefix="${last_word//(#m)[\\*?[\]<>()|^~#]/\\$MATCH}"
# Get the first history word that matches. Concatenate it together with the
# prefix to form the full suggestion.
# - (r) subscript flag makes the pattern match on values
typeset -g suggestion="${1:0:-$#last_word}${historywords[(r)${prefix}?*]}"
}

View file

@ -611,6 +611,34 @@ _zsh_autosuggest_strategy_completion() {
}
}
#--------------------------------------------------------------------#
# History Words Suggestion Strategy #
#--------------------------------------------------------------------#
# Suggests the most recent history word that matches the given
# prefix.
#
_zsh_autosuggest_strategy_historywords() {
# Reset options to defaults and enable LOCAL_OPTIONS
emulate -L zsh
# Enable globbing flags so that we can use (#m)
setopt EXTENDED_GLOB
local last_word="${${=1}[-1]}"
# Escape backslashes and all of the glob operators so we can use
# this string as a pattern to search the $history associative array.
# - (#m) globbing flag enables setting references for match data
# TODO: Use (b) flag when we can drop support for zsh older than v5.0.8
local prefix="${last_word//(#m)[\\*?[\]<>()|^~#]/\\$MATCH}"
# Get the first history word that matches. Concatenate it together with the
# prefix to form the full suggestion.
# - (r) subscript flag makes the pattern match on values
typeset -g suggestion="${1:0:-$#last_word}${historywords[(r)${prefix}?*]}"
}
#--------------------------------------------------------------------#
# History Suggestion Strategy #
#--------------------------------------------------------------------#