| 1 |
#compdef beet |
| 2 |
|
| 3 |
# zsh completion for beets music library manager and MusicBrainz tagger: https://beets.io/ |
| 4 |
|
| 5 |
# Default values for BEETS_LIBRARY & BEETS_CONFIG needed for the cache checking function. |
| 6 |
# They will be updated under the assumption that the config file is in the same directory as the library. |
| 7 |
local BEETS_LIBRARY=~/.config/beets/library.db |
| 8 |
local BEETS_CONFIG=~/.config/beets/config.yaml |
| 9 |
# Use separate caches for file locations, command completions, and query completions. |
| 10 |
# This allows the use of different rules for when to update each one. |
| 11 |
zstyle ":completion:${curcontext%:*}:*" cache-policy _beet_check_cache |
| 12 |
zstyle ":completion:${curcontext%:*}:*" use-cache true |
| 13 |
_beet_check_cache () { |
| 14 |
local cachefile="$(basename ${1})" |
| 15 |
if [[ ! -a "${1}" ]] || [[ "${1}" -ot =beet ]]; then |
| 16 |
# always update the cache if it doesn't exist, or if the beet executable changes |
| 17 |
return 0 |
| 18 |
fi |
| 19 |
case cachefile; in |
| 20 |
(beetslibrary) |
| 21 |
if [[ ! -a "${~BEETS_LIBRARY}" ]] || [[ "${1}" -ot "${~BEETS_CONFIG}" ]]; then |
| 22 |
return 0 |
| 23 |
fi |
| 24 |
;; |
| 25 |
(beetscmds) |
| 26 |
_retrieve_cache beetslibrary |
| 27 |
if [[ "${1}" -ot "${~BEETS_CONFIG}" ]]; then |
| 28 |
return 0 |
| 29 |
fi |
| 30 |
;; |
| 31 |
esac |
| 32 |
return 1 |
| 33 |
} |
| 34 |
|
| 35 |
# useful: argument to _regex_arguments for matching any word |
| 36 |
local matchany=/$'[^\0]##\0'/ |
| 37 |
# arguments to _regex_arguments for completing files and directories |
| 38 |
local -a files dirs |
| 39 |
files=("$matchany" ':file:file:_files') |
| 40 |
dirs=("$matchany" ':dir:directory:_dirs') |
| 41 |
|
| 42 |
# Retrieve or update caches |
| 43 |
if ! _retrieve_cache beetslibrary || _cache_invalid beetslibrary; then |
| 44 |
local BEETS_LIBRARY="${$(beet config|grep library|cut -f 2 -d ' '):-${BEETS_LIBRARY}}" |
| 45 |
local BEETS_CONFIG="${$(beet config -p):-${BEETS_CONFIG}}" |
| 46 |
_store_cache beetslibrary BEETS_LIBRARY BEETS_CONFIG |
| 47 |
fi |
| 48 |
|
| 49 |
if ! _retrieve_cache beetscmds || _cache_invalid beetscmds; then |
| 50 |
local -a subcommands fields beets_regex_words_subcmds beets_regex_words_help query modify |
| 51 |
local subcmd cmddesc matchquery matchmodify field fieldargs queryelem modifyelem |
| 52 |
# Useful function for joining grouped lines of output into single lines (taken from _completion_helpers) |
| 53 |
_join_lines() { |
| 54 |
awk -v SEP="$1" -v ARG2="$2" -v START="$3" -v END2="$4" 'BEGIN {if(START==""){f=1}{f=0}; |
| 55 |
if(ARG2 ~ "^[0-9]+"){LINE1 = "^[[:space:]]{0,"ARG2"}[^[:space:]]"}else{LINE1 = ARG2}} |
| 56 |
($0 ~ END2 && f>0 && END2!="") {exit} |
| 57 |
($0 ~ START && f<1) {f=1; if(length(START)!=0){next}} |
| 58 |
($0 ~ LINE1 && f>0) {if(f<2){f=2; printf("%s",$0)}else{printf("\n%s",$0)}; next} |
| 59 |
(f>1) {gsub(/^[[:space:]]+|[[:space:]]+$/,"",$0); printf("%s%s",SEP, $0); next} |
| 60 |
END {print ""}' |
| 61 |
} |
| 62 |
# Variables used for completing subcommands and queries |
| 63 |
subcommands=(${${(f)"$(beet help | _join_lines ' ' 3 'Commands:')"}[@]}) |
| 64 |
fields=($(beet fields | grep -G '^ ' | sort -u | colrm 1 2)) |
| 65 |
for field in "${fields[@]}" |
| 66 |
do |
| 67 |
fieldargs="$fieldargs '$field:::{_beet_field_values $field}'" |
| 68 |
done |
| 69 |
queryelem="_values -S : 'query field (add an extra : to match by regexp)' '::' $fieldargs" |
| 70 |
modifyelem="_values -S = 'modify field (replace = with ! to remove field)' $(echo "'${^fields[@]}:: '")" |
| 71 |
# regexps for matching query and modify terms on the command line |
| 72 |
matchquery=/"(${(j/|/)fields[@]})"$':[^\0]##\0'/ |
| 73 |
matchmodify=/"(${(j/|/)fields[@]})"$'(=[^\0]##|!)\0'/ |
| 74 |
# create completion function for queries |
| 75 |
_regex_arguments _beet_query "$matchany" \# \( "$matchquery" ":query:query string:$queryelem" \) \# |
| 76 |
local "beets_query"="$(which _beet_query)" |
| 77 |
# arguments for _regex_arguments for completing lists of queries and modifications |
| 78 |
beets_query_args=( \( "$matchquery" ":query:query string:{_beet_query}" \) \# ) |
| 79 |
beets_modify_args=( \( "$matchmodify" ":modify:modify string:$modifyelem" \) \# ) |
| 80 |
# now build arguments for _beet and _beet_help completion functions |
| 81 |
beets_regex_words_subcmds=('(') |
| 82 |
for i in ${subcommands[@]}; do |
| 83 |
subcmd="${i[(w)1]}" |
| 84 |
# remove first word and parenthesised alias, replace : with -, [ with (, ] with ), and remove single quotes |
| 85 |
cmddesc="${${${${${i[(w)2,-1]##\(*\) #}//:/-}//\[/(}//\]/)}//\'/}" |
| 86 |
# update arguments needed for creating _beet |
| 87 |
beets_regex_words_subcmds+=(/"${subcmd}"$'\0'/ ":subcmds:subcommands:((${subcmd}:${cmddesc// /\ }))") |
| 88 |
beets_regex_words_subcmds+=(\( "${matchany}" ":option:option:{_beet_subcmd ${subcmd}}" \) \# \|) |
| 89 |
# update arguments needed for creating _beet_help |
| 90 |
beets_regex_words_help+=("${subcmd}:${cmddesc}") |
| 91 |
done |
| 92 |
beets_regex_words_subcmds[-1]=')' |
| 93 |
_store_cache beetscmds beets_regex_words_subcmds beets_regex_words_help beets_query_args beets_modify_args beets_query |
| 94 |
else |
| 95 |
# Evaluate the variable containing the query completer function |
| 96 |
eval "${beets_query}" |
| 97 |
fi |
| 98 |
|
| 99 |
# Function for getting unique values for field from database (you may need to change the path to the database). |
| 100 |
_beet_field_values() { |
| 101 |
local -a output fieldvals |
| 102 |
local sqlcmd="select distinct $1 from items;" |
| 103 |
_retrieve_cache beetslibrary |
| 104 |
case ${1} |
| 105 |
in |
| 106 |
lyrics) |
| 107 |
fieldvals= |
| 108 |
;; |
| 109 |
*) |
| 110 |
if [[ "$(sqlite3 ${~BEETS_LIBRARY} ${sqlcmd} 2>&1)" =~ "no such column" ]]; then |
| 111 |
sqlcmd="select distinct value from item_attributes where key=='$1' and value!='';" |
| 112 |
fi |
| 113 |
output="$(sqlite3 -list -noheader ${~BEETS_LIBRARY} ${sqlcmd} 2>/dev/null)" |
| 114 |
fieldvals=("${(f)output[@]}") |
| 115 |
;; |
| 116 |
esac |
| 117 |
compadd -P \" -S \" -M 'm:{[:lower:][:upper:]}={[:upper:][:lower:]}' -Q -a fieldvals |
| 118 |
} |
| 119 |
|
| 120 |
# This function takes a beet subcommand as its first argument, and then uses _regex_words to set ${reply[@]} |
| 121 |
# to an array containing arguments for the _regex_arguments function. |
| 122 |
_beet_subcmd_options() { |
| 123 |
local shortopt optarg optdesc |
| 124 |
local matchany=/$'[^\0]##\0'/ |
| 125 |
local -a regex_words |
| 126 |
regex_words=() |
| 127 |
for i in ${${(f)"$(beet help $1 | awk '/^ +-/{if(x)print x;x=$0;next}/^ *$/{if(x) exit}{if(x) x=x$0}END{print x}')"}[@]} |
| 128 |
do |
| 129 |
opt="${i[(w)1]/,/}" |
| 130 |
optarg="${${${i## #[-a-zA-Z]# }##[- ]##*}%%[, ]*}" |
| 131 |
optdesc="${${${${${i[(w)2,-1]/[A-Z, ]#--[-a-z]##[=A-Z]# #/}//:/-}//\[/(}//\]/)}//\'/}" |
| 132 |
case $optarg; in |
| 133 |
("") |
| 134 |
if [[ "$1" == "import" && "$opt" == "-L" ]]; then |
| 135 |
regex_words+=("$opt:$optdesc:\${beets_query_args[@]}") |
| 136 |
else |
| 137 |
regex_words+=("$opt:$optdesc") |
| 138 |
fi |
| 139 |
;; |
| 140 |
(LOG) |
| 141 |
local -a files |
| 142 |
files=("$matchany" ':file:file:_files') |
| 143 |
regex_words+=("$opt:$optdesc:\$files") |
| 144 |
;; |
| 145 |
(CONFIG) |
| 146 |
local -a configfile |
| 147 |
configfile=("$matchany" ':file:config file:{_files -g *.yaml}') |
| 148 |
regex_words+=("$opt:$optdesc:\$configfile") |
| 149 |
;; |
| 150 |
(LIB|LIBRARY) |
| 151 |
local -a libfile |
| 152 |
libfile=("$matchany" ':file:database file:{_files -g *.db}') |
| 153 |
regex_words+=("$opt:$optdesc:\$libfile") |
| 154 |
;; |
| 155 |
(DIR|DIRECTORY|DEST) |
| 156 |
local -a dirs |
| 157 |
dirs=("$matchany" ':dir:directory:_dirs') |
| 158 |
regex_words+=("$opt:$optdesc:\$dirs") |
| 159 |
;; |
| 160 |
(SOURCE) |
| 161 |
if [[ "${1}" -eq lastgenre ]]; then |
| 162 |
local -a lastgenresource |
| 163 |
lastgenresource=(/$'(artist|album|track)\0'/ ':source:genre source:(artist album track)') |
| 164 |
regex_words+=("$opt:$optdesc:\$lastgenresource") |
| 165 |
else |
| 166 |
regex_words+=("$opt:$optdesc:\$matchany") |
| 167 |
fi |
| 168 |
;; |
| 169 |
(*) |
| 170 |
regex_words+=("$opt:$optdesc:\$matchany") |
| 171 |
;; |
| 172 |
esac |
| 173 |
done |
| 174 |
_regex_words options "$1 options" "${regex_words[@]}" |
| 175 |
} |
| 176 |
|
| 177 |
## Function for completing subcommands. It calls another completion function which is first created if it doesn't already exist. |
| 178 |
_beet_subcmd() { |
| 179 |
local -a options |
| 180 |
local subcmd="${1}" |
| 181 |
if [[ ! $(type _beet_${subcmd} | grep function) =~ function ]]; then |
| 182 |
if ! _retrieve_cache "beets${subcmd}" || _cache_invalid "beets${subcmd}"; then |
| 183 |
local matchany=/$'[^\0]##\0'/ |
| 184 |
local -a files |
| 185 |
files=("$matchany" ':file:file:_files') |
| 186 |
# get arguments for completing subcommand options |
| 187 |
_beet_subcmd_options "$subcmd" |
| 188 |
options=("${reply[@]}" \#) |
| 189 |
_retrieve_cache beetscmds |
| 190 |
case ${subcmd}; in |
| 191 |
(import) |
| 192 |
_regex_arguments _beet_import "${matchany}" /"${subcmd}"$'\0'/ "${options[@]}" "${files[@]}" \# |
| 193 |
;; |
| 194 |
(modify) |
| 195 |
_regex_arguments _beet_modify "${matchany}" /"${subcmd}"$'\0'/ "${options[@]}" \ |
| 196 |
"${beets_query_args[@]}" "${beets_modify_args[@]}" |
| 197 |
;; |
| 198 |
(fields|migrate|version|config) |
| 199 |
_regex_arguments _beet_${subcmd} "${matchany}" /"${subcmd}"$'\0'/ "${options[@]}" |
| 200 |
;; |
| 201 |
(help) |
| 202 |
_regex_words subcmds "subcommands" "${beets_regex_words_help[@]}" |
| 203 |
_regex_arguments _beet_help "${matchany}" /$'help\0'/ "${options[@]}" "${reply[@]}" |
| 204 |
;; |
| 205 |
(*) # Other commands have options followed by a query |
| 206 |
_regex_arguments _beet_${subcmd} "${matchany}" /"${subcmd}"$'\0'/ "${options[@]}" "${beets_query_args[@]}" |
| 207 |
;; |
| 208 |
esac |
| 209 |
# Store completion function in a cache file |
| 210 |
local "beets_${subcmd}"="$(which _beet_${subcmd})" |
| 211 |
_store_cache "beets${subcmd}" "beets_${subcmd}" |
| 212 |
else |
| 213 |
# Evaluate the function which is stored in $beets_${subcmd} |
| 214 |
local var="beets_${subcmd}" |
| 215 |
eval "${(P)var}" |
| 216 |
fi |
| 217 |
fi |
| 218 |
_beet_${subcmd} |
| 219 |
} |
| 220 |
|
| 221 |
# Global options |
| 222 |
local -a globalopts |
| 223 |
_regex_words options "global options" '-c:path to configuration file:$files' '-v:print debugging information' \ |
| 224 |
'-l:library database file to use:$files' '-h:show this help message and exit' '-d:destination music directory:$dirs' |
| 225 |
globalopts=("${reply[@]}") |
| 226 |
|
| 227 |
# Create main completion function |
| 228 |
_regex_arguments _beet "$matchany" \( "${globalopts[@]}" \# \) "${beets_regex_words_subcmds[@]}" |
| 229 |
|
| 230 |
# Set tag-order so that options are completed separately from arguments |
| 231 |
zstyle ":completion:${curcontext}:" tag-order '! options' |
| 232 |
|
| 233 |
# Execute the completion function |
| 234 |
_beet "$@" |
| 235 |
|
| 236 |
# Local Variables: |
| 237 |
# mode:shell-script |
| 238 |
# End: |