update predefined strategies

This commit is contained in:
skywind3000 2018-03-07 16:48:14 +08:00
parent 050143d539
commit 04a8736841
2 changed files with 84 additions and 84 deletions

View file

@ -16,55 +16,55 @@ _zsh_autosuggest_script_path="${0:A:h}"
_zsh_autosuggest_data_home="${XDG_DATA_HOME:-$HOME/.local/share}/zsh_autosuggest" _zsh_autosuggest_data_home="${XDG_DATA_HOME:-$HOME/.local/share}/zsh_autosuggest"
_zsh_autosuggest_predefined_generate() { _zsh_autosuggest_predefined_generate() {
local pbase="$_zsh_autosuggest_data_home" local pbase="$_zsh_autosuggest_data_home"
local pname="$pbase/predefined" local pname="$pbase/predefined"
local ptemp="$pbase/predefined.$[RANDOM]" local ptemp="$pbase/predefined.$[RANDOM]"
local suggests=() local suggests=()
local pwd=$(pwd) local pwd=$(pwd)
[ ! -d "$pbase" ] && mkdir -p "$pbase" 2> /dev/null [ ! -d "$pbase" ] && mkdir -p "$pbase" 2> /dev/null
# skip when ~/.zsh_autosuggest exists # skip when ~/.zsh_autosuggest exists
[ -f "$pname" ] && return [ -f "$pname" ] && return
echo "autosuggestions is generating: $pname" echo "autosuggestions is generating: $pname"
# copy builtin predefine database # copy builtin predefine database
local txt="${_zsh_autosuggest_script_path}/predefined.txt" local txt="${_zsh_autosuggest_script_path}/predefined.txt"
[ -f "$txt" ] && cat "$txt" > "$ptemp" [ -f "$txt" ] && cat "$txt" > "$ptemp"
# enumerate commands in $PATH and add them to ~/.zsh_autosuggest # enumerate commands in $PATH and add them to ~/.zsh_autosuggest
for p ("${(@s/:/)PATH}"); do for p ("${(@s/:/)PATH}"); do
[ ! -d "$p" ] && continue [ ! -d "$p" ] && continue
cd "$p" cd "$p"
local files=("${(@f)$(ls -la | awk -F ' ' '{print $9}')}") local files=("${(@f)$(ls -la | awk -F ' ' '{print $9}')}")
for fn in ${files}; do for fn in ${files}; do
if [ -x "$fn" ] && [[ "${fn:l}" != *.dll ]]; then if [ -x "$fn" ] && [[ "${fn:l}" != *.dll ]]; then
if [ -f "$fn" ] && [[ "${fn:l}" != *.nls ]]; then if [ -f "$fn" ] && [[ "${fn:l}" != *.nls ]]; then
# trim cygwin .exe/.cmd/.bat postfix # trim cygwin .exe/.cmd/.bat postfix
if [[ "$fn" == *.exe ]]; then if [[ "$fn" == *.exe ]]; then
fn=${fn/%.exe/} fn=${fn/%.exe/}
elif [[ "$fn" == *.cmd ]]; then elif [[ "$fn" == *.cmd ]]; then
fn=${fn/%.cmd/} fn=${fn/%.cmd/}
elif [[ "$fn" == *.bat ]]; then elif [[ "$fn" == *.bat ]]; then
fn=${fn/%.bat/} fn=${fn/%.bat/}
fi fi
if [[ ${#fn} -gt 1 ]]; then if [[ ${#fn} -gt 1 ]]; then
suggests+=$fn suggests+=$fn
fi fi
fi fi
fi fi
done done
done done
cd "${pwd}" cd "${pwd}"
# TODO: generate command parameters from completion database # TODO: generate command parameters from completion database
print -l $suggests >> "$ptemp" print -l $suggests >> "$ptemp"
# atomic change file name # atomic change file name
mv -f "$ptemp" "$pname" mv -f "$ptemp" "$pname"
} }
@ -73,16 +73,16 @@ _zsh_autosuggest_strategy_predefined() {
setopt EXTENDED_GLOB setopt EXTENDED_GLOB
local prefix="${1//(#m)[\\*?[\]<>()|^~#]/\\$MATCH}" local prefix="${1//(#m)[\\*?[\]<>()|^~#]/\\$MATCH}"
# search the history at first # search the history at first
local result="${history[(r)${prefix}*]}" local result="${history[(r)${prefix}*]}"
# search the predefine files if nothing found in history # search the predefine files if nothing found in history
if [[ -z "$result" ]]; then if [[ -z "$result" ]]; then
if (( ! ${+_ZSH_AUTOSUGGEST_PREDEFINE} )); then if (( ! ${+_ZSH_AUTOSUGGEST_PREDEFINE} )); then
# _zsh_autosuggest_predefined_generate # _zsh_autosuggest_predefined_generate
typeset -g _ZSH_AUTOSUGGEST_PREDEFINE=() typeset -g _ZSH_AUTOSUGGEST_PREDEFINE=()
local pbase="$_zsh_autosuggest_data_home" local pbase="$_zsh_autosuggest_data_home"
local pname="$pbase/predefined" local pname="$pbase/predefined"
local names="${ZSH_AUTOSUGGEST_PREDEFINE_PATH};$pname" local names="${ZSH_AUTOSUGGEST_PREDEFINE_PATH};$pname"
local array=() local array=()
for i ("${(s:;:)names}"); do for i ("${(s:;:)names}"); do
@ -102,6 +102,6 @@ _zsh_autosuggest_strategy_predefined() {
_zsh_autosuggest_predefined_generate _zsh_autosuggest_predefined_generate
# vim: set ts=4 sw=4 tw=0 noet : # vim: set ts=4 sw=4 tw=0 et :

View file

@ -582,55 +582,55 @@ _zsh_autosuggest_script_path="${0:A:h}"
_zsh_autosuggest_data_home="${XDG_DATA_HOME:-$HOME/.local/share}/zsh_autosuggest" _zsh_autosuggest_data_home="${XDG_DATA_HOME:-$HOME/.local/share}/zsh_autosuggest"
_zsh_autosuggest_predefined_generate() { _zsh_autosuggest_predefined_generate() {
local pbase="$_zsh_autosuggest_data_home" local pbase="$_zsh_autosuggest_data_home"
local pname="$pbase/predefined" local pname="$pbase/predefined"
local ptemp="$pbase/predefined.$[RANDOM]" local ptemp="$pbase/predefined.$[RANDOM]"
local suggests=() local suggests=()
local pwd=$(pwd) local pwd=$(pwd)
[ ! -d "$pbase" ] && mkdir -p "$pbase" 2> /dev/null [ ! -d "$pbase" ] && mkdir -p "$pbase" 2> /dev/null
# skip when ~/.zsh_autosuggest exists # skip when ~/.zsh_autosuggest exists
[ -f "$pname" ] && return [ -f "$pname" ] && return
echo "autosuggestions is generating: $pname" echo "autosuggestions is generating: $pname"
# copy builtin predefine database # copy builtin predefine database
local txt="${_zsh_autosuggest_script_path}/predefined.txt" local txt="${_zsh_autosuggest_script_path}/predefined.txt"
[ -f "$txt" ] && cat "$txt" > "$ptemp" [ -f "$txt" ] && cat "$txt" > "$ptemp"
# enumerate commands in $PATH and add them to ~/.zsh_autosuggest # enumerate commands in $PATH and add them to ~/.zsh_autosuggest
for p ("${(@s/:/)PATH}"); do for p ("${(@s/:/)PATH}"); do
[ ! -d "$p" ] && continue [ ! -d "$p" ] && continue
cd "$p" cd "$p"
local files=("${(@f)$(ls -la | awk -F ' ' '{print $9}')}") local files=("${(@f)$(ls -la | awk -F ' ' '{print $9}')}")
for fn in ${files}; do for fn in ${files}; do
if [ -x "$fn" ] && [[ "${fn:l}" != *.dll ]]; then if [ -x "$fn" ] && [[ "${fn:l}" != *.dll ]]; then
if [ -f "$fn" ] && [[ "${fn:l}" != *.nls ]]; then if [ -f "$fn" ] && [[ "${fn:l}" != *.nls ]]; then
# trim cygwin .exe/.cmd/.bat postfix # trim cygwin .exe/.cmd/.bat postfix
if [[ "$fn" == *.exe ]]; then if [[ "$fn" == *.exe ]]; then
fn=${fn/%.exe/} fn=${fn/%.exe/}
elif [[ "$fn" == *.cmd ]]; then elif [[ "$fn" == *.cmd ]]; then
fn=${fn/%.cmd/} fn=${fn/%.cmd/}
elif [[ "$fn" == *.bat ]]; then elif [[ "$fn" == *.bat ]]; then
fn=${fn/%.bat/} fn=${fn/%.bat/}
fi fi
if [[ ${#fn} -gt 1 ]]; then if [[ ${#fn} -gt 1 ]]; then
suggests+=$fn suggests+=$fn
fi fi
fi fi
fi fi
done done
done done
cd "${pwd}" cd "${pwd}"
# TODO: generate command parameters from completion database # TODO: generate command parameters from completion database
print -l $suggests >> "$ptemp" print -l $suggests >> "$ptemp"
# atomic change file name # atomic change file name
mv -f "$ptemp" "$pname" mv -f "$ptemp" "$pname"
} }
@ -639,16 +639,16 @@ _zsh_autosuggest_strategy_predefined() {
setopt EXTENDED_GLOB setopt EXTENDED_GLOB
local prefix="${1//(#m)[\\*?[\]<>()|^~#]/\\$MATCH}" local prefix="${1//(#m)[\\*?[\]<>()|^~#]/\\$MATCH}"
# search the history at first # search the history at first
local result="${history[(r)${prefix}*]}" local result="${history[(r)${prefix}*]}"
# search the predefine files if nothing found in history # search the predefine files if nothing found in history
if [[ -z "$result" ]]; then if [[ -z "$result" ]]; then
if (( ! ${+_ZSH_AUTOSUGGEST_PREDEFINE} )); then if (( ! ${+_ZSH_AUTOSUGGEST_PREDEFINE} )); then
# _zsh_autosuggest_predefined_generate # _zsh_autosuggest_predefined_generate
typeset -g _ZSH_AUTOSUGGEST_PREDEFINE=() typeset -g _ZSH_AUTOSUGGEST_PREDEFINE=()
local pbase="$_zsh_autosuggest_data_home" local pbase="$_zsh_autosuggest_data_home"
local pname="$pbase/predefined" local pname="$pbase/predefined"
local names="${ZSH_AUTOSUGGEST_PREDEFINE_PATH};$pname" local names="${ZSH_AUTOSUGGEST_PREDEFINE_PATH};$pname"
local array=() local array=()
for i ("${(s:;:)names}"); do for i ("${(s:;:)names}"); do
@ -668,7 +668,7 @@ _zsh_autosuggest_strategy_predefined() {
_zsh_autosuggest_predefined_generate _zsh_autosuggest_predefined_generate
# vim: set ts=4 sw=4 tw=0 noet : # vim: set ts=4 sw=4 tw=0 et :