mirror of
https://github.com/zsh-users/zsh-autosuggestions.git
synced 2024-12-30 00:03:56 +08:00
Add suggestion "strategy" concept with default strategy
This commit is contained in:
parent
9df362f783
commit
83f78d0760
1
Makefile
1
Makefile
|
@ -8,6 +8,7 @@ SRC_FILES := \
|
||||||
$(SRC_DIR)/highlight.zsh \
|
$(SRC_DIR)/highlight.zsh \
|
||||||
$(SRC_DIR)/widgets.zsh \
|
$(SRC_DIR)/widgets.zsh \
|
||||||
$(SRC_DIR)/suggestion.zsh \
|
$(SRC_DIR)/suggestion.zsh \
|
||||||
|
$(SRC_DIR)/strategies/*.zsh \
|
||||||
$(SRC_DIR)/start.zsh
|
$(SRC_DIR)/start.zsh
|
||||||
|
|
||||||
HEADER_FILES := \
|
HEADER_FILES := \
|
||||||
|
|
|
@ -11,6 +11,8 @@ ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=8'
|
||||||
# Prefix to use when saving original versions of bound widgets
|
# Prefix to use when saving original versions of bound widgets
|
||||||
ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX=autosuggest-orig-
|
ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX=autosuggest-orig-
|
||||||
|
|
||||||
|
ZSH_AUTOSUGGEST_STRATEGY=default
|
||||||
|
|
||||||
# Widgets that clear the suggestion
|
# Widgets that clear the suggestion
|
||||||
ZSH_AUTOSUGGEST_CLEAR_WIDGETS=(
|
ZSH_AUTOSUGGEST_CLEAR_WIDGETS=(
|
||||||
history-search-forward
|
history-search-forward
|
||||||
|
|
17
src/strategies/default.zsh
Normal file
17
src/strategies/default.zsh
Normal 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]}"
|
||||||
|
}
|
|
@ -3,16 +3,14 @@
|
||||||
# Suggestion #
|
# Suggestion #
|
||||||
#--------------------------------------------------------------------#
|
#--------------------------------------------------------------------#
|
||||||
|
|
||||||
# Get a suggestion from history that matches a given prefix
|
# Delegate to the selected strategy to determine a suggestion
|
||||||
_zsh_autosuggest_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*
|
if [ -n "$functions[$strategy_function]" ]; then
|
||||||
local history_matches
|
echo -E "$($strategy_function "$prefix")"
|
||||||
history_matches=(${(j:\0:s:\0:)history[(R)$prefix*]})
|
fi
|
||||||
|
|
||||||
# Echo the first item that matches
|
|
||||||
echo -E "$history_matches[1]"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_zsh_autosuggest_escape_command_prefix() {
|
_zsh_autosuggest_escape_command_prefix() {
|
||||||
|
|
|
@ -37,6 +37,8 @@ ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=8'
|
||||||
# Prefix to use when saving original versions of bound widgets
|
# Prefix to use when saving original versions of bound widgets
|
||||||
ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX=autosuggest-orig-
|
ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX=autosuggest-orig-
|
||||||
|
|
||||||
|
ZSH_AUTOSUGGEST_STRATEGY=default
|
||||||
|
|
||||||
# Widgets that clear the suggestion
|
# Widgets that clear the suggestion
|
||||||
ZSH_AUTOSUGGEST_CLEAR_WIDGETS=(
|
ZSH_AUTOSUGGEST_CLEAR_WIDGETS=(
|
||||||
history-search-forward
|
history-search-forward
|
||||||
|
@ -311,16 +313,14 @@ zle -N autosuggest-execute _zsh_autosuggest_widget_execute
|
||||||
# Suggestion #
|
# Suggestion #
|
||||||
#--------------------------------------------------------------------#
|
#--------------------------------------------------------------------#
|
||||||
|
|
||||||
# Get a suggestion from history that matches a given prefix
|
# Delegate to the selected strategy to determine a suggestion
|
||||||
_zsh_autosuggest_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*
|
if [ -n "$functions[$strategy_function]" ]; then
|
||||||
local history_matches
|
echo -E "$($strategy_function "$prefix")"
|
||||||
history_matches=(${(j:\0:s:\0:)history[(R)$prefix*]})
|
fi
|
||||||
|
|
||||||
# Echo the first item that matches
|
|
||||||
echo -E "$history_matches[1]"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_zsh_autosuggest_escape_command_prefix() {
|
_zsh_autosuggest_escape_command_prefix() {
|
||||||
|
@ -330,6 +330,23 @@ _zsh_autosuggest_escape_command_prefix() {
|
||||||
echo -E "${1//(#m)[\\()\[\]|*?]/\\$MATCH}"
|
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 #
|
# Start #
|
||||||
#--------------------------------------------------------------------#
|
#--------------------------------------------------------------------#
|
||||||
|
|
Loading…
Reference in New Issue
Block a user