diff --git a/Makefile b/Makefile index 9d3ae6c..564fc95 100644 --- a/Makefile +++ b/Makefile @@ -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 := \ diff --git a/src/config.zsh b/src/config.zsh index ae34f82..8a83e4d 100644 --- a/src/config.zsh +++ b/src/config.zsh @@ -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 diff --git a/src/strategies/default.zsh b/src/strategies/default.zsh new file mode 100644 index 0000000..feee14e --- /dev/null +++ b/src/strategies/default.zsh @@ -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]}" +} diff --git a/src/suggestion.zsh b/src/suggestion.zsh index f8e5459..b2b8f5a 100644 --- a/src/suggestion.zsh +++ b/src/suggestion.zsh @@ -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() { diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh index cd8ad5a..a3b0d51 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -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 # #--------------------------------------------------------------------#