diff --git a/src/strategies/default.zsh b/src/strategies/default.zsh index 0e85fb5..68617ff 100644 --- a/src/strategies/default.zsh +++ b/src/strategies/default.zsh @@ -2,24 +2,16 @@ #--------------------------------------------------------------------# # Default Suggestion Strategy # #--------------------------------------------------------------------# -# Suggests the most recent history item that matches the given -# prefix. +# 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() { - # Reset options to defaults and enable LOCAL_OPTIONS - emulate -L zsh + typeset -g suggestion - # Enable globbing flags so that we can use (#m) - setopt EXTENDED_GLOB + _zsh_autosuggest_strategy_history "$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="${1//(#m)[\\*?[\]<>()|^~#]/\\$MATCH}" - - # Get the history items that match - # - (r) subscript flag makes the pattern match on values - typeset -g suggestion="${history[(r)${prefix}*]}" + if [[ -z "$suggestion" ]]; then + _zsh_autosuggest_strategy_completion "$1" + fi } diff --git a/src/strategies/history.zsh b/src/strategies/history.zsh new file mode 100644 index 0000000..a2755a5 --- /dev/null +++ b/src/strategies/history.zsh @@ -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}*]}" +} diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh index edd6d84..41336a7 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -631,11 +631,28 @@ _zsh_autosuggest_strategy_completion() { #--------------------------------------------------------------------# # 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 # prefix. # -_zsh_autosuggest_strategy_default() { +_zsh_autosuggest_strategy_history() { # Reset options to defaults and enable LOCAL_OPTIONS emulate -L zsh