1 #compdef pacman pacman.static=pacman
3 # copy this file to /usr/share/zsh/site-functions/_pacman
7 # options for passing to _arguments: main pacman commands
8 _pacman_opts_commands=(
9 '-Q[Query the package database]'
10 '-R[Remove a package from the system]'
11 '-S[Synchronize packages]'
12 '-U[Upgrade a package]'
13 '-V[Display version and exit]'
17 # options for passing to _arguments: options common to all commands
19 '-b[Alternate database location]:database_location:_files -/'
20 '-h[Display syntax for the given operation]'
21 '-r[Set alternate installation root]:installation root:_files -/'
23 '--cachedir[Alternate package cache location]:cache_location:_files -/'
24 '--config[An alternate configuration file]:config file:_files'
25 '--logfile[An alternate log file]:config file:_files'
26 '--noconfirm[Do not ask for confirmation]'
27 '--noprogressbar[Do not show a progress bar when downloading files]'
28 '--noscriptlet[Do not execute the install scriptlet if one exists]'
31 # options for passing to _arguments: options for --upgrade commands
32 _pacman_opts_pkgfile=(
33 '-d[Skip dependency checks]'
34 '-f[Overwrite conflicting files]'
35 '*:package file:_files -g "*.pkg.tar.gz(.)"'
38 # options for passing to _arguments: subactions for --query command
39 _pacman_opts_query_actions=(
40 '-g[View all members of a package group]:*:package groups:->query_group'
41 '-o[Query the package that owns a file]:file:_files'
42 '-p[Package file to query]:*:package file:->query_file'
43 '-s[Search package names and descriptions]:*:search text:->query_search'
46 # options for passing to _arguments: options for --query and subcommands
47 _pacman_opts_query_modifiers=(
48 '-c[List package changelog]'
49 '-d[List packages installed as dependencies]'
50 '-e[List packages explicitly installed]'
51 '-i[View package information]'
52 '-ii[View package information including backup files]'
53 '-l[List package contents]'
54 '-m[List installed packages not found in sync db(s)]'
55 '-t[List packages not required by any package]'
56 '-u[List packages that can be upgraded]'
59 # options for passing to _arguments: options for --remove command
61 '-c[Remove all dependent packages]'
62 '-d[Skip dependency checks]'
63 '-k[Only remove database entry, do not remove files]'
64 '-n[Remove protected configuration files]'
65 '-s[Remove dependencies not required by other packages]'
66 '*:installed package:_pacman_completions_installed_packages'
69 # options for passing to _arguments: options for --sync command
70 _pacman_opts_sync_actions=(
71 '*-c[Remove old packages from cache]:*:clean:->sync_clean'
72 '*-cc[Remove all packages from cache]:*:clean:->sync_clean'
73 '-g[View all members of a package group]:*:package groups:->sync_group'
74 '-s[Search package names and descriptions]:*:search text:->sync_search'
77 # options for passing to _arguments: options for --sync command
78 _pacman_opts_sync_modifiers=(
79 '-d[Skip dependency checks]'
80 '-f[Overwrite conflicting files]'
81 '-i[View package information]'
82 '-l[List all packages in a repository]'
83 '-p[Print download URIs for each package to be installed]'
84 '-u[Upgrade all out-of-date packages]'
85 '-w[Download packages only]'
86 '-y[Download fresh package databases]'
87 '*--ignore[Ignore a package upgrade]:package:
88 _pacman_completions_all_packages'
89 '*--ignoregroup[Ignore a group upgrade]:package group:
90 _pacman_completions_all_groups'
91 '--asdeps[Install packages as non-explicitly installed]'
92 '--asexplicit[Install packages as explicitly installed]'
95 # handles --help subcommand
96 _pacman_action_help() {
98 "$_pacman_opts_commands[@]"
101 # handles cases where no subcommand has yet been given
102 _pacman_action_none() {
104 "$_pacman_opts_commands[@]"
107 # handles --query subcommand
108 _pacman_action_query() {
109 local context state line
113 # "$_pacman_opts_common[@]" \
114 # "$_pacman_opts_query_actions[@]" \
115 # "$_pacman_opts_query_modifiers[@]"
120 "$_pacman_opts_common[@]" \
121 "$_pacman_opts_query_modifiers[@]" \
122 '*:package file:_files -g "*.pkg.tar.gz"'
126 "$_pacman_opts_common[@]" \
127 "$_pacman_opts_query_modifiers[@]" \
128 '*:groups:_pacman_completions_installed_groups'
132 "$_pacman_opts_common[@]" \
133 "$_pacman_opts_query_modifiers[@]" \
138 "$_pacman_opts_common[@]" \
139 "$_pacman_opts_query_modifiers[@]" \
144 "$_pacman_opts_common[@]" \
145 "$_pacman_opts_query_actions[@]" \
146 "$_pacman_opts_query_modifiers[@]" \
147 '*:package:_pacman_completions_installed_packages'
152 # handles --remove subcommand
153 _pacman_action_remove() {
155 "$_pacman_opts_common[@]" \
156 "$_pacman_opts_remove[@]"
159 # handles --sync subcommand
160 _pacman_action_sync() {
161 local context state line
165 # "$_pacman_opts_common[@]" \
166 # "$_pacman_opts_sync_actions[@]" #\
167 # #"$_pacman_opts_sync_modifiers[@]"
172 "$_pacman_opts_common[@]" \
173 "$_pacman_opts_sync_modifiers[@]" \
174 '*-c[Remove old packages from cache]' \
178 "$_pacman_opts_common[@]" \
179 "$_pacman_opts_sync_modifiers[@]" \
180 '*:package group:_pacman_completions_all_groups'
184 "$_pacman_opts_common[@]" \
185 "$_pacman_opts_sync_modifiers[@]" \
190 "$_pacman_opts_common[@]" \
191 "$_pacman_opts_sync_modifiers[@]" \
192 '*:package:_pacman_completions_all_packages'
197 # handles --upgrade subcommand
198 _pacman_action_upgrade() {
200 "$_pacman_opts_common[@]" \
201 "$_pacman_opts_pkgfile[@]"
204 # handles --version subcommand
205 _pacman_action_version() {
206 # no further arguments
210 # provides completions for package groups
211 _pacman_completions_all_groups() {
214 groups=( $(_call_program groups $cmd[@] -Sg) )
215 compadd "$@" -a groups
218 # provides completions for packages available from repositories
219 # these can be specified as either 'package' or 'repository/package'
220 _pacman_completions_all_packages() {
221 local -a cmd packages repositories packages_long
223 repositories=(${(o)${${${(M)${(f)"$(</etc/pacman.conf)"}:#\[*}/\[/}/\]/}:#options})
224 typeset -U repositories
225 packages_long=(/var/lib/pacman/sync/${^repositories}/*(/))
226 packages=(${(o)${${packages_long/\/var\/lib\/pacman\/sync\//}#*/}%-*-*} )
228 _wanted packages expl "packages" compadd - "${(@)packages}"
229 if [[ $PREFIX != */* ]] ; then
230 repositories=(${(o)${${${(M)${(f)"$(</etc/pacman.conf)"}:#\[*}/\[/}/\]/}:#options})
231 typeset -U repositories
232 _wanted repo_packages expl "repository/package" compadd -S "/" $repositories
235 packages_long=(/var/lib/pacman/sync/$IPREFIX*(/))
236 packages=(${(o)${${packages_long/\/var\/lib\/pacman\/sync\//}#*/}%-*-*} )
238 _wanted repo_packages expl "repository/package" compadd ${(@)packages}
242 # provides completions for package groups
243 _pacman_completions_installed_groups() {
246 groups=(${(o)${(f)"$(pacman -Qg)"}% *})
248 compadd "$@" -a groups
251 # provides completions for installed packages
252 _pacman_completions_installed_packages() {
253 local -a cmd packages packages_long
254 packages_long=(/var/lib/pacman/local/*(/))
255 packages=( ${${packages_long/\/var\/lib\/pacman\/local\//}%-*-*} )
256 compadd "$@" -a packages
259 # provides completions for repository names
260 _pacman_completions_repositories() {
261 local -a cmd repositories
262 repositories=(${(o)${${${(M)${(f)"$(</etc/pacman.conf)"}:#\[*}/\[/}/\]/}:#options})
264 typeset -U repositories
265 compadd "$@" -a repositories
268 # builds command for invoking pacman in a _call_program command - extracts
269 # relevant options already specified (config file, etc)
270 # $cmd must be declared by calling function
271 _pacman_get_command() {
272 # this is mostly nicked from _perforce
275 for (( i = 2; i < CURRENT - 1; i++ )); do
276 if [[ ${words[i]} = "--config" || ${words[i]} = "--root" ]]; then
277 cmd+=( ${words[i,i+1]} )
287 "$_pacman_opts_common[@]" \
288 "$_pacman_opts_query_modifiers[@]" \
289 '*:groups:_pacman_completions_installed_groups'
293 "$_pacman_opts_common[@]" \
294 "$_pacman_opts_query_modifiers[@]" \
295 '*:package file:_files'
297 -Q*p*) # file *.pkg.tar.gz
299 "$_pacman_opts_common[@]" \
300 "$_pacman_opts_query_modifiers[@]" \
301 '*:package file:_files -g "*.pkg.tar.gz"'
303 -Q*) _pacman_action_query ;;
304 -R*) _pacman_action_remove ;;
305 -S*c*) # no completion
310 "$_pacman_opts_common[@]" \
311 "$_pacman_opts_sync_modifiers[@]" \
312 '*:package repo:_pacman_completions_repositories' \
316 "$_pacman_opts_common[@]" \
317 "$_pacman_opts_sync_modifiers[@]" \
318 '*:package group:_pacman_completions_all_groups'
320 -S*) _pacman_action_sync ;;
321 -U*) _pacman_action_upgrade ;;
322 -V*) _pacman_action_version ;;
323 -h*) _pacman_action_help ;;
324 - ) _pacman_action_none ;;
329 # run the main dispatcher