This commit is contained in:
Linwei 2018-05-21 17:41:40 +00:00 committed by GitHub
commit 64de83722b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 2553 additions and 1 deletions

View File

@ -43,6 +43,7 @@ Set `ZSH_AUTOSUGGEST_STRATEGY` to choose the strategy for generating suggestions
- `default`: Chooses the most recent match. - `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`. - `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 ### Widget Mapping

2327
predefined.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@ -11,7 +11,7 @@ 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 ZSH_AUTOSUGGEST_STRATEGY=${ZSH_AUTOSUGGEST_STRATEGY:-default}
# Widgets that clear the suggestion # Widgets that clear the suggestion
ZSH_AUTOSUGGEST_CLEAR_WIDGETS=( ZSH_AUTOSUGGEST_CLEAR_WIDGETS=(

View File

@ -0,0 +1,112 @@
# zsh-augosuggestion: predefined strategy
# 1. search in history, returns result if it exists
# 2. search in predefined files
#
# configuration:
#
# ZSH_AUTOSUGGEST_PREDEFINE_PATH - user defined files separated by semicolon
#
# "~/.local/share/zsh_autosuggest/predefined" will be generated at the first time
#
# zsh-autosuggestions/predefined.txt is generated from tldr pages and
# will ship with predefined.zsh .
#
_zsh_autosuggest_script_path="${0:A:h}"
_zsh_autosuggest_data_home="${XDG_DATA_HOME:-$HOME/.local/share}/zsh_autosuggest"
_zsh_autosuggest_predefined_generate() {
local pbase="$_zsh_autosuggest_data_home"
local pname="$pbase/predefined"
local ptemp="$pbase/predefined.$[RANDOM]"
local suggests=()
local pwd=$(pwd)
[ ! -d "$pbase" ] && mkdir -p "$pbase" 2> /dev/null
# skip when ~/.zsh_autosuggest exists
[ -f "$pname" ] && return
# echo "autosuggestions is generating: $pname"
# copy builtin predefine database
local txt="${_zsh_autosuggest_script_path}/predefined.txt"
[ -f "$txt" ] && cat "$txt" > "$ptemp"
# enumerate commands in $PATH and add them to ~/.zsh_autosuggest
for p ("${(@s/:/)PATH}"); do
[ ! -d "$p" ] && continue
cd "$p"
local files=($(command ls))
for fn in ${files}; do
if [ -x "$fn" ] && [[ "${fn:l}" != *.dll ]]; then
if [ -f "$fn" ] && [[ "${fn:l}" != *.nls ]]; then
# trim cygwin .exe/.cmd/.bat postfix
if [[ "$fn" == *.exe ]]; then
fn=${fn/%.exe/}
elif [[ "$fn" == *.cmd ]]; then
fn=${fn/%.cmd/}
elif [[ "$fn" == *.bat ]]; then
fn=${fn/%.bat/}
fi
if [[ ${#fn} -gt 1 ]]; then
suggests+=$fn
fi
fi
fi
done
done
cd "${pwd}"
# TODO: generate command parameters from completion database
print -l $suggests >> "$ptemp"
# atomic change file name
mv -f "$ptemp" "$pname"
}
_zsh_autosuggest_strategy_predefined() {
emulate -L zsh
setopt EXTENDED_GLOB
local prefix="${1//(#m)[\\*?[\]<>()|^~#]/\\$MATCH}"
# 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
typeset -g _ZSH_AUTOSUGGEST_PREDEFINE=()
local pbase="$_zsh_autosuggest_data_home"
local pname="$pbase/predefined"
local names="${ZSH_AUTOSUGGEST_PREDEFINE_PATH};$pname"
local array=()
for i ("${(s:;:)names}"); do
if [[ -n "$i" ]] && [[ -f "$i" ]]; then
local temp=(${(f)"$(<$i)"})
array+=($temp)
fi
done
_ZSH_AUTOSUGGEST_PREDEFINE+=($array)
fi
result="${_ZSH_AUTOSUGGEST_PREDEFINE[(r)${prefix}*]}"
fi
typeset -g suggestion="$result"
}
# _zsh_autosuggest_predefined_generate
# vim: set ts=4 sw=4 tw=0 et :

View File

@ -576,6 +576,118 @@ _zsh_autosuggest_strategy_match_prev_cmd() {
# Give back the matched history entry # Give back the matched history entry
typeset -g suggestion="$history[$histkey]" typeset -g suggestion="$history[$histkey]"
} }
# zsh-augosuggestion: predefined strategy
# 1. search in history, returns result if it exists
# 2. search in predefined files
#
# configuration:
#
# ZSH_AUTOSUGGEST_PREDEFINE_PATH - user defined files separated by semicolon
#
# "~/.local/share/zsh_autosuggest/predefined" will be generated at the first time
#
# zsh-autosuggestions/predefined.txt is generated from tldr pages and
# will ship with predefined.zsh .
#
_zsh_autosuggest_script_path="${0:A:h}"
_zsh_autosuggest_data_home="${XDG_DATA_HOME:-$HOME/.local/share}/zsh_autosuggest"
_zsh_autosuggest_predefined_generate() {
local pbase="$_zsh_autosuggest_data_home"
local pname="$pbase/predefined"
local ptemp="$pbase/predefined.$[RANDOM]"
local suggests=()
local pwd=$(pwd)
[ ! -d "$pbase" ] && mkdir -p "$pbase" 2> /dev/null
# skip when ~/.zsh_autosuggest exists
[ -f "$pname" ] && return
# echo "autosuggestions is generating: $pname"
# copy builtin predefine database
local txt="${_zsh_autosuggest_script_path}/predefined.txt"
[ -f "$txt" ] && cat "$txt" > "$ptemp"
# enumerate commands in $PATH and add them to ~/.zsh_autosuggest
for p ("${(@s/:/)PATH}"); do
[ ! -d "$p" ] && continue
cd "$p"
local files=($(command ls))
for fn in ${files}; do
if [ -x "$fn" ] && [[ "${fn:l}" != *.dll ]]; then
if [ -f "$fn" ] && [[ "${fn:l}" != *.nls ]]; then
# trim cygwin .exe/.cmd/.bat postfix
if [[ "$fn" == *.exe ]]; then
fn=${fn/%.exe/}
elif [[ "$fn" == *.cmd ]]; then
fn=${fn/%.cmd/}
elif [[ "$fn" == *.bat ]]; then
fn=${fn/%.bat/}
fi
if [[ ${#fn} -gt 1 ]]; then
suggests+=$fn
fi
fi
fi
done
done
cd "${pwd}"
# TODO: generate command parameters from completion database
print -l $suggests >> "$ptemp"
# atomic change file name
mv -f "$ptemp" "$pname"
}
_zsh_autosuggest_strategy_predefined() {
emulate -L zsh
setopt EXTENDED_GLOB
local prefix="${1//(#m)[\\*?[\]<>()|^~#]/\\$MATCH}"
# 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
typeset -g _ZSH_AUTOSUGGEST_PREDEFINE=()
local pbase="$_zsh_autosuggest_data_home"
local pname="$pbase/predefined"
local names="${ZSH_AUTOSUGGEST_PREDEFINE_PATH};$pname"
local array=()
for i ("${(s:;:)names}"); do
if [[ -n "$i" ]] && [[ -f "$i" ]]; then
local temp=(${(f)"$(<$i)"})
array+=($temp)
fi
done
_ZSH_AUTOSUGGEST_PREDEFINE+=($array)
fi
result="${_ZSH_AUTOSUGGEST_PREDEFINE[(r)${prefix}*]}"
fi
typeset -g suggestion="$result"
}
# _zsh_autosuggest_predefined_generate
# vim: set ts=4 sw=4 tw=0 et :
#--------------------------------------------------------------------# #--------------------------------------------------------------------#
# Async # # Async #