predefined update

This commit is contained in:
skywind3000 2018-03-07 16:32:28 +08:00
parent 491aff3a51
commit 942662d12d
2 changed files with 34 additions and 20 deletions

View file

@ -4,32 +4,35 @@
# #
# configuration: # configuration:
# #
# ZSH_AUTOSUGGEST_PREDEFINE_NAME - auto generated predefine file name
# ZSH_AUTOSUGGEST_PREDEFINE_PATH - user defined files separated by semicolon # ZSH_AUTOSUGGEST_PREDEFINE_PATH - user defined files separated by semicolon
# #
# "$HOME/.zsh_autosuggest" will be generated at the first time # "~/.local/share/zsh_autosuggest/predefined" will be generated at the first time
# #
# zsh-autosuggestions/predefined.txt is generated from tldr pages and # zsh-autosuggestions/predefined.txt is generated from tldr pages and
# will ship with predefined.zsh . # will ship with predefined.zsh .
# #
_zsh_autosuggest_script_path="${0:A:h}" _zsh_autosuggest_script_path="${0:A:h}"
_zsh_autosuggest_data_home="${XDG_DATA_HOME:-$HOME/.local/share}/zsh_autosuggest"
_zsh_autosuggest_predefined_generate() { _zsh_autosuggest_predefined_generate() {
local pname="$HOME/.zsh_autosuggest" local pbase="$_zsh_autosuggest_data_home"
pname="${ZSH_AUTOSUGGEST_PREDEFINE_NAME:-$pname}" local pname="$pbase/predefined"
local ptemp="$pbase/predefined.$[RANDOM]"
local suggests=() local suggests=()
local pwd=$(pwd) local pwd=$(pwd)
[ ! -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" > "$pname" [ -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
@ -57,7 +60,10 @@ _zsh_autosuggest_predefined_generate() {
# TODO: generate command parameters from completion database # TODO: generate command parameters from completion database
print -l $suggests >> "$pname" print -l $suggests >> "$ptemp"
# atomic change file name
mv -f "$ptemp" "$pname"
} }
@ -72,9 +78,10 @@ _zsh_autosuggest_strategy_predefined() {
# 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
typeset -g _ZSH_AUTOSUGGEST_PREDEFINE=() typeset -g _ZSH_AUTOSUGGEST_PREDEFINE=()
local pname="$HOME/.zsh_autosuggest" local pbase="$_zsh_autosuggest_data_home"
pname="${ZSH_AUTOSUGGEST_PREDEFINE_NAME:-$pname}" 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
@ -92,7 +99,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 noet :

View file

@ -570,32 +570,35 @@ _zsh_autosuggest_strategy_match_prev_cmd() {
# #
# configuration: # configuration:
# #
# ZSH_AUTOSUGGEST_PREDEFINE_NAME - auto generated predefine file name
# ZSH_AUTOSUGGEST_PREDEFINE_PATH - user defined files separated by semicolon # ZSH_AUTOSUGGEST_PREDEFINE_PATH - user defined files separated by semicolon
# #
# "$HOME/.zsh_autosuggest" will be generated at the first time # "~/.local/share/zsh_autosuggest/predefined" will be generated at the first time
# #
# zsh-autosuggestions/predefined.txt is generated from tldr pages and # zsh-autosuggestions/predefined.txt is generated from tldr pages and
# will ship with predefined.zsh . # will ship with predefined.zsh .
# #
_zsh_autosuggest_script_path="${0:A:h}" _zsh_autosuggest_script_path="${0:A:h}"
_zsh_autosuggest_data_home="${XDG_DATA_HOME:-$HOME/.local/share}/zsh_autosuggest"
_zsh_autosuggest_predefined_generate() { _zsh_autosuggest_predefined_generate() {
local pname="$HOME/.zsh_autosuggest" local pbase="$_zsh_autosuggest_data_home"
pname="${ZSH_AUTOSUGGEST_PREDEFINE_NAME:-$pname}" local pname="$pbase/predefined"
local ptemp="$pbase/predefined.$[RANDOM]"
local suggests=() local suggests=()
local pwd=$(pwd) local pwd=$(pwd)
[ ! -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" > "$pname" [ -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
@ -623,7 +626,10 @@ _zsh_autosuggest_predefined_generate() {
# TODO: generate command parameters from completion database # TODO: generate command parameters from completion database
print -l $suggests >> "$pname" print -l $suggests >> "$ptemp"
# atomic change file name
mv -f "$ptemp" "$pname"
} }
@ -638,9 +644,10 @@ _zsh_autosuggest_strategy_predefined() {
# 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
typeset -g _ZSH_AUTOSUGGEST_PREDEFINE=() typeset -g _ZSH_AUTOSUGGEST_PREDEFINE=()
local pname="$HOME/.zsh_autosuggest" local pbase="$_zsh_autosuggest_data_home"
pname="${ZSH_AUTOSUGGEST_PREDEFINE_NAME:-$pname}" 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
@ -658,7 +665,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 noet :