Add suggestion "strategy" concept with default strategy

This commit is contained in:
Eric Freese 2016-03-01 12:45:55 -07:00
parent 9df362f783
commit 83f78d0760
5 changed files with 51 additions and 16 deletions

View File

@ -8,6 +8,7 @@ SRC_FILES := \
$(SRC_DIR)/highlight.zsh \
$(SRC_DIR)/widgets.zsh \
$(SRC_DIR)/suggestion.zsh \
$(SRC_DIR)/strategies/*.zsh \
$(SRC_DIR)/start.zsh
HEADER_FILES := \

View File

@ -11,6 +11,8 @@ ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=8'
# Prefix to use when saving original versions of bound widgets
ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX=autosuggest-orig-
ZSH_AUTOSUGGEST_STRATEGY=default
# Widgets that clear the suggestion
ZSH_AUTOSUGGEST_CLEAR_WIDGETS=(
history-search-forward

View File

@ -0,0 +1,17 @@
#--------------------------------------------------------------------#
# Default Suggestion Strategy #
#--------------------------------------------------------------------#
# Suggests the most recent history item that matches the given
# prefix.
#
_zsh_autosuggest_strategy_default() {
local prefix="$(_zsh_autosuggest_escape_command_prefix "$1")"
# Get the hist number of the most recent history item that matches
local histkey="${${(k)history[(R)$prefix*]}[1]}"
# Echo the history entry
echo -E "${history[$histkey]}"
}

View File

@ -3,16 +3,14 @@
# Suggestion #
#--------------------------------------------------------------------#
# Get a suggestion from history that matches a given prefix
# Delegate to the selected strategy to determine a suggestion
_zsh_autosuggest_suggestion() {
local prefix="$(_zsh_autosuggest_escape_command_prefix "$1")"
local prefix="$1"
local strategy_function="_zsh_autosuggest_strategy_$ZSH_AUTOSUGGEST_STRATEGY"
# Get all history items (reversed) that match pattern $prefix*
local history_matches
history_matches=(${(j:\0:s:\0:)history[(R)$prefix*]})
# Echo the first item that matches
echo -E "$history_matches[1]"
if [ -n "$functions[$strategy_function]" ]; then
echo -E "$($strategy_function "$prefix")"
fi
}
_zsh_autosuggest_escape_command_prefix() {

View File

@ -37,6 +37,8 @@ ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=8'
# Prefix to use when saving original versions of bound widgets
ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX=autosuggest-orig-
ZSH_AUTOSUGGEST_STRATEGY=default
# Widgets that clear the suggestion
ZSH_AUTOSUGGEST_CLEAR_WIDGETS=(
history-search-forward
@ -311,16 +313,14 @@ zle -N autosuggest-execute _zsh_autosuggest_widget_execute
# Suggestion #
#--------------------------------------------------------------------#
# Get a suggestion from history that matches a given prefix
# Delegate to the selected strategy to determine a suggestion
_zsh_autosuggest_suggestion() {
local prefix="$(_zsh_autosuggest_escape_command_prefix "$1")"
local prefix="$1"
local strategy_function="_zsh_autosuggest_strategy_$ZSH_AUTOSUGGEST_STRATEGY"
# Get all history items (reversed) that match pattern $prefix*
local history_matches
history_matches=(${(j:\0:s:\0:)history[(R)$prefix*]})
# Echo the first item that matches
echo -E "$history_matches[1]"
if [ -n "$functions[$strategy_function]" ]; then
echo -E "$($strategy_function "$prefix")"
fi
}
_zsh_autosuggest_escape_command_prefix() {
@ -330,6 +330,23 @@ _zsh_autosuggest_escape_command_prefix() {
echo -E "${1//(#m)[\\()\[\]|*?]/\\$MATCH}"
}
#--------------------------------------------------------------------#
# Default Suggestion Strategy #
#--------------------------------------------------------------------#
# Suggests the most recent history item that matches the given
# prefix.
#
_zsh_autosuggest_strategy_default() {
local prefix="$(_zsh_autosuggest_escape_command_prefix "$1")"
# Get the hist number of the most recent history item that matches
local histkey="${${(k)history[(R)$prefix*]}[1]}"
# Echo the history entry
echo -E "${history[$histkey]}"
}
#--------------------------------------------------------------------#
# Start #
#--------------------------------------------------------------------#