Default strategy now tries history first and falls back to completion

This commit is contained in:
Eric Freese 2018-05-16 15:52:46 -06:00
parent 82b08e2dc8
commit c5551daabc
3 changed files with 50 additions and 16 deletions

View file

@ -2,24 +2,16 @@
#--------------------------------------------------------------------# #--------------------------------------------------------------------#
# Default Suggestion Strategy # # Default Suggestion Strategy #
#--------------------------------------------------------------------# #--------------------------------------------------------------------#
# Suggests the most recent history item that matches the given # Will provide suggestions from your history. If no matches are found
# prefix. # in history, will provide a suggestion from the completion engine.
# #
_zsh_autosuggest_strategy_default() { _zsh_autosuggest_strategy_default() {
# Reset options to defaults and enable LOCAL_OPTIONS typeset -g suggestion
emulate -L zsh
# Enable globbing flags so that we can use (#m) _zsh_autosuggest_strategy_history "$1"
setopt EXTENDED_GLOB
# Escape backslashes and all of the glob operators so we can use if [[ -z "$suggestion" ]]; then
# this string as a pattern to search the $history associative array. _zsh_autosuggest_strategy_completion "$1"
# - (#m) globbing flag enables setting references for match data fi
# TODO: Use (b) flag when we can drop support for zsh older than v5.0.8
local prefix="${1//(#m)[\\*?[\]<>()|^~#]/\\$MATCH}"
# Get the history items that match
# - (r) subscript flag makes the pattern match on values
typeset -g suggestion="${history[(r)${prefix}*]}"
} }

View file

@ -0,0 +1,25 @@
#--------------------------------------------------------------------#
# History Suggestion Strategy #
#--------------------------------------------------------------------#
# Suggests the most recent history item that matches the given
# prefix.
#
_zsh_autosuggest_strategy_history() {
# Reset options to defaults and enable LOCAL_OPTIONS
emulate -L zsh
# Enable globbing flags so that we can use (#m)
setopt EXTENDED_GLOB
# 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="${1//(#m)[\\*?[\]<>()|^~#]/\\$MATCH}"
# Get the history items that match
# - (r) subscript flag makes the pattern match on values
typeset -g suggestion="${history[(r)${prefix}*]}"
}

View file

@ -631,11 +631,28 @@ _zsh_autosuggest_strategy_completion() {
#--------------------------------------------------------------------# #--------------------------------------------------------------------#
# Default Suggestion Strategy # # Default Suggestion Strategy #
#--------------------------------------------------------------------# #--------------------------------------------------------------------#
# Will provide suggestions from your history. If no matches are found
# in history, will provide a suggestion from the completion engine.
#
_zsh_autosuggest_strategy_default() {
typeset -g suggestion
_zsh_autosuggest_strategy_history "$1"
if [[ -z "$suggestion" ]]; then
_zsh_autosuggest_strategy_completion "$1"
fi
}
#--------------------------------------------------------------------#
# History Suggestion Strategy #
#--------------------------------------------------------------------#
# Suggests the most recent history item that matches the given # Suggests the most recent history item that matches the given
# prefix. # prefix.
# #
_zsh_autosuggest_strategy_default() { _zsh_autosuggest_strategy_history() {
# Reset options to defaults and enable LOCAL_OPTIONS # Reset options to defaults and enable LOCAL_OPTIONS
emulate -L zsh emulate -L zsh