#compdef packer packer.static=packer
# copy this file to /usr/share/zsh/site-functions/_packer
typeset -A opt_args
# options for passing to _arguments: main packer commands
_packer_opts_commands=(
'-Ss[Search for a package.]:*:search text:->sync_search'
'-S[Install a package.]'
'-Syu[Sync with repositories.]'
'-Si[Show information for a package.]'
'-G[Just download and extract AUR tarball]'
'-h[Show packer usage.]'
)
# options for passing to _arguments: options common to all commands
_packer_opts_common=(
'--ignore[Ignore packages listed here.]:package:_packer_completions_all_packages'
'--noconfirm[Do not ask user for confirmation.]'
'--noedit[Do not ask user if he wants to edit files.]'
'--auronly[Only perform commands for the AUR.]'
'--devel[Update development packages. (cvs, git, ...)]'
'--skipinteg[Skip the integrity checks.]'
)
# handles cases where no subcommand has yet been given
_packer_action_none() {
_arguments -s : \
"$_packer_opts_commands[@]"
}
# handles --sync subcommand
_packer_action_sync() {
local context state line
typeset -A opt_args
# _arguments -s : \
# "$_packer_opts_common[@]" \
# "$_packer_opts_sync_actions[@]" #\
# #"$_packer_opts_sync_modifiers[@]"
case $state in
sync_search)
_arguments -s : \
"$_packer_opts_common[@]" \
'*:search text: '
;;
*)
_arguments -s : \
"$_packer_opts_common[@]" \
'*:package:_packer_completions_all_packages'
;;
esac
}
# provides completions for package groups
_packer_completions_all_groups() {
local -a cmd groups
_packer_get_command
groups=( $(_call_program groups $cmd[@] -Sg) )
typeset -U groups
compadd "$@" -a groups
}
# provides completions for packages available from repositories
# these can be specified as either 'package' or 'repository/package'
_packer_completions_all_packages() {
local -a cmd packages repositories packages_long
_packer_get_command
if compset -P1 '*/*'; then
packages=( $(_call_program packages $cmd[@] -Sql ${words[CURRENT]%/*}) )
typeset -U packages
_wanted repo_packages expl "repository/package" compadd ${(@)packages}
else
packages=( $(_call_program packages $cmd[@] -Sql) )
typeset -U packages
_wanted packages expl "packages" compadd - "${(@)packages}"
repositories=(${(o)${${${(M)${(f)"$(</etc/pacman.conf)"}:#\[*}/\[/}/\]/}:#options})
typeset -U repositories
_wanted repo_packages expl "repository/package" compadd -S "/" $repositories
fi
}
# provides completions for package groups
_packer_completions_installed_groups() {
local -a cmd groups
_packer_get_command
groups=(${(o)${(f)"$(_call_program groups $cmd[@] -Qg)"}% *})
typeset -U groups
compadd "$@" -a groups
}
# provides completions for installed packages
_packer_completions_installed_packages() {
local -a cmd packages packages_long
packages_long=(/var/lib/pacman/local/*(/))
packages=( ${${packages_long#/var/lib/pacman/local/}%-*-*} )
compadd "$@" -a packages
}
# provides completions for repository names
_packer_completions_repositories() {
local -a cmd repositories
repositories=(${(o)${${${(M)${(f)"$(</etc/pacman.conf)"}:#\[*}/\[/}/\]/}:#options})
# Uniq the array
typeset -U repositories
compadd "$@" -a repositories
}
# builds command for invoking pacman in a _call_program command - extracts
# relevant options already specified (config file, etc)
# $cmd must be declared by calling function
_packer_get_command() {
# this is mostly nicked from _perforce
cmd=( "pacman" )
integer i
for (( i = 2; i < CURRENT - 1; i++ )); do
if [[ ${words[i]} = "--config" || ${words[i]} = "--root" ]]; then
cmd+=( ${words[i,i+1]} )
fi
done
}
# main dispatcher
_packer() {
case $words[2] in
-S*) _packer_action_sync ;;
-G*) _packer_action_sync ;;
- ) _packer_action_none ;;
* ) return 1 ;;
esac
}
# run the main dispatcher
_packer "$@"