Automatically generate cache file

This makes updating cache files easier; you just delete the old one and try completing again.
This commit is contained in:
vapniks 2016-10-02 19:54:10 +01:00 committed by GitHub
parent be69f52b69
commit 5282d2882e

View file

@ -2,19 +2,21 @@
# zsh completion for beets music library manager and MusicBrainz tagger: http://beets.radbox.org/
# NOTE: it will be very slow the first time you try to complete in a zsh shell (especially if you've enable many plugins)
# You can make it faster in future by creating a cached version:
# 1) perform a query completion with this file (_beet), e.g. do: beet list artist:"<TAB>
# to create the completion function (takes a few seconds)
# 2) save a copy of the completion function: which _beet > _beet_cached
# 3) save a copy of the query completion function: which _beet_query > _beet_query_cached
# 4) copy the contents of _beet_query_cached to the top of _beet_cached
# 5) copy and paste the _beet_field_values function from _beet to the top of _beet_cached
# 6) add the following line to the top of _beet_cached: #compdef beet
# 7) add the following line to the bottom of _beet_cached: _beet "$@"
# 8) save _beet_cached to your completions directory (e.g. /usr/share/zsh/functions/Completion)
# 9) add the following line to your .zshrc file: compdef _beet_cached beet
# You will need to repeat this proceedure each time you enable new plugins if you want them to complete properly.
# IMPORTANT: You need to change the $completionsdir variable defined below to point to the directory where your completion files
# are stored.
# It will be very slow the first time you try to complete in a zsh shell (especially if you've enable many plugins),
# but a cache file (_beet_cached) will be created after the first completion which will make subsequent calls much
# faster. If you enable new plugins or fields you should update the cache file by deleting it and then performing
# another completion in a new shell.
# If cache file exists, use that
local completionsdir="$HOME/.oh-my-zsh/custom/completions"
local cachefile=$completionsdir/_beet_cached
if [[ -r "$cachefile" ]]; then
source "$cachefile"
_beet "$@"
return
fi
# useful: argument to _regex_arguments for matching any word
local matchany=/$'[^\0]##\0'/
@ -68,8 +70,7 @@ queryelem="_values -S : 'query field (add an extra : to match by regexp)' '::' $
# store call to _values function for completing modify terms (no need to complete field values)
modifyelem="_values -S = 'modify field (replace = with ! to remove field)' $(echo "'${^fields[@]}:: '")"
# Create completion function for queries
_regex_arguments _beet_query "$matchany" \# \( "$matchquery" ":query:query string:$queryelem" \) \
\( "$matchquery" ":query:query string:$queryelem" \) \#
_regex_arguments _beet_query "$matchany" \# \( "$matchquery" ":query:query string:$queryelem" \) \( "$matchquery" ":query:query string:$queryelem" \) \#
# store regexps for completing lists of queries and modifications
local -a query modify
query=( \( "$matchquery" ":query:query string:{_beet_query}" \) \( "$matchquery" ":query:query string:{_beet_query}" \) \# )
@ -261,6 +262,17 @@ _regex_arguments _beet "$matchany" \( "${globalopts[@]}" \# \) "${subcmds[@]}"
# Set tag-order so that options are completed separately from arguments
zstyle ":completion:${curcontext}:" tag-order '! options'
# Save completion functions to _beet_cached
echo "# Cached completion file for beet\n# Note: this was created from _beet, and should be regenerated when new fields or plugins are added.\n# To regenerate this file just delete it, open a new zshell and try completing beet.\n\n" > $cachefile
which _beet_field_values >> $cachefile
echo "\n\n" >> $cachefile
which _beet_query >> $cachefile
echo "\n\n" >> $cachefile
which _beet >> $cachefile
echo "\n\n" >> $cachefile
echo zstyle \":completion:\${curcontext}:\" tag-order \'! options\' >> $cachefile
echo "\n\n_beet \"\$@\"\n" >> $cachefile
# Execute the completion function
_beet "$@"