From 270d988ae68efeeb5862c9a9e8d3cf269be3d68c Mon Sep 17 00:00:00 2001 From: skywind3000 Date: Wed, 7 Mar 2018 22:12:02 +0800 Subject: [PATCH] update predefined strategy and allow user set ZSH_AUTOSUGGEST_STRATEGY before plugin initializing --- README.md | 1 + src/config.zsh | 2 +- src/strategies/predefined.zsh | 11 ++++++++--- zsh-autosuggestions.zsh | 11 ++++++++--- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 6a6ae1e..ddfdc1d 100644 --- a/README.md +++ b/README.md @@ -107,6 +107,7 @@ Set `ZSH_AUTOSUGGEST_STRATEGY` to choose the strategy for generating suggestions - `default`: Chooses the most recent match. - `match_prev_cmd`: Chooses the most recent match whose preceding history item matches the most recently executed command ([more info](src/strategies/match_prev_cmd.zsh)). Note that this strategy won't work as expected with ZSH options that don't preserve the history order such as `HIST_IGNORE_ALL_DUPS` or `HIST_EXPIRE_DUPS_FIRST`. +- `predefined`: Chooses the most recent match in history and predefined database which is generated from available commands in $PATH and their help at the first time `predefined` strategy is used. ### Widget Mapping diff --git a/src/config.zsh b/src/config.zsh index ba694c0..2a8cb15 100644 --- a/src/config.zsh +++ b/src/config.zsh @@ -11,7 +11,7 @@ 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 +ZSH_AUTOSUGGEST_STRATEGY=${ZSH_AUTOSUGGEST_STRATEGY:-default} # Widgets that clear the suggestion ZSH_AUTOSUGGEST_CLEAR_WIDGETS=( diff --git a/src/strategies/predefined.zsh b/src/strategies/predefined.zsh index 87173ad..6efd0e6 100644 --- a/src/strategies/predefined.zsh +++ b/src/strategies/predefined.zsh @@ -27,7 +27,7 @@ _zsh_autosuggest_predefined_generate() { # skip when ~/.zsh_autosuggest exists [ -f "$pname" ] && return - echo "autosuggestions is generating: $pname" + # echo "autosuggestions is generating: $pname" # copy builtin predefine database local txt="${_zsh_autosuggest_script_path}/predefined.txt" @@ -76,10 +76,15 @@ _zsh_autosuggest_strategy_predefined() { # search the history at first local result="${history[(r)${prefix}*]}" + # check if necessary to generate predefine database + if (( ! ${+_ZSH_AUTOSUGGEST_GENERATED} )); then + _ZSH_AUTOSUGGEST_GENERATED=1 + _zsh_autosuggest_predefined_generate + fi + # search the predefine files if nothing found in history if [[ -z "$result" ]]; then if (( ! ${+_ZSH_AUTOSUGGEST_PREDEFINE} )); then - # _zsh_autosuggest_predefined_generate typeset -g _ZSH_AUTOSUGGEST_PREDEFINE=() local pbase="$_zsh_autosuggest_data_home" local pname="$pbase/predefined" @@ -100,7 +105,7 @@ _zsh_autosuggest_strategy_predefined() { } -_zsh_autosuggest_predefined_generate +# _zsh_autosuggest_predefined_generate # vim: set ts=4 sw=4 tw=0 et : diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh index fc5ef94..07a0614 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -593,7 +593,7 @@ _zsh_autosuggest_predefined_generate() { # skip when ~/.zsh_autosuggest exists [ -f "$pname" ] && return - echo "autosuggestions is generating: $pname" + # echo "autosuggestions is generating: $pname" # copy builtin predefine database local txt="${_zsh_autosuggest_script_path}/predefined.txt" @@ -642,10 +642,15 @@ _zsh_autosuggest_strategy_predefined() { # search the history at first local result="${history[(r)${prefix}*]}" + # check if necessary to generate predefine database + if (( ! ${+_ZSH_AUTOSUGGEST_GENERATED} )); then + _ZSH_AUTOSUGGEST_GENERATED=1 + _zsh_autosuggest_predefined_generate + fi + # search the predefine files if nothing found in history if [[ -z "$result" ]]; then if (( ! ${+_ZSH_AUTOSUGGEST_PREDEFINE} )); then - # _zsh_autosuggest_predefined_generate typeset -g _ZSH_AUTOSUGGEST_PREDEFINE=() local pbase="$_zsh_autosuggest_data_home" local pname="$pbase/predefined" @@ -666,7 +671,7 @@ _zsh_autosuggest_strategy_predefined() { } -_zsh_autosuggest_predefined_generate +# _zsh_autosuggest_predefined_generate # vim: set ts=4 sw=4 tw=0 et :