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]'
29 '--print[Only print the targets instead of performing the operation]'
32 # options for passing to _arguments: options for --upgrade commands
33 _pacman_opts_pkgfile=(
34 '-d[Skip dependency checks]'
35 '-f[Overwrite conflicting files]'
36 '--dbonly[Only remove database entry, do not remove files]'
37 '--needed[Do not reinstall up to date packages]'
38 '--recursive[Reinstall all dependencies of target packages]'
39 '*:package file:_files -g "*.pkg.tar.*(.)"'
42 # options for passing to _arguments: subactions for --query command
43 _pacman_opts_query_actions=(
44 '-g[View all members of a package group]:*:package groups:->query_group'
45 '-o[Query the package that owns a file]:file:_files'
46 '-p[Package file to query]:*:package file:->query_file'
47 '-s[Search package names and descriptions]:*:search text:->query_search'
50 # options for passing to _arguments: options for --query and subcommands
51 _pacman_opts_query_modifiers=(
52 '-c[List package changelog]'
53 '-d[List packages installed as dependencies]'
54 '-e[List packages explicitly installed]'
55 '-i[View package information]'
56 '-ii[View package information including backup files]'
57 '-k[Check package files]'
58 '-l[List package contents]'
59 '-m[List installed packages not found in sync db(s)]'
60 '-t[List packages not required by any package]'
61 '-u[List packages that can be upgraded]'
64 # options for passing to _arguments: options for --remove command
66 '-c[Remove all dependent packages]'
67 '-d[Skip dependency checks]'
68 '-n[Remove protected configuration files]'
69 '-s[Remove dependencies not required by other packages]'
70 '--dbonly[Only remove database entry, do not remove files]'
71 '*:installed package:_pacman_completions_installed_packages'
74 # options for passing to _arguments: options for --sync command
75 _pacman_opts_sync_actions=(
76 '*-c[Remove old packages from cache]:*:clean:->sync_clean'
77 '*-cc[Remove all packages from cache]:*:clean:->sync_clean'
78 '-g[View all members of a package group]:*:package groups:->sync_group'
79 '-s[Search package names and descriptions]:*:search text:->sync_search'
80 '--dbonly[Only remove database entry, do not remove files]'
81 '--needed[Do not reinstall up to date packages]'
82 '--recursive[Reinstall all dependencies of target packages]'
85 # options for passing to _arguments: options for --sync command
86 _pacman_opts_sync_modifiers=(
87 '-d[Skip dependency checks]'
88 '-f[Overwrite conflicting files]'
89 '-i[View package information]'
90 '-l[List all packages in a repository]'
91 '-p[Print download URIs for each package to be installed]'
92 '-u[Upgrade all out-of-date packages]'
93 '-w[Download packages only]'
94 '-y[Download fresh package databases]'
95 '*--ignore[Ignore a package upgrade]:package:
96 _pacman_completions_all_packages'
97 '*--ignoregroup[Ignore a group upgrade]:package group:
98 _pacman_completions_all_groups'
99 '--asdeps[Install packages as non-explicitly installed]'
100 '--asexplicit[Install packages as explicitly installed]'
103 # handles --help subcommand
104 _pacman_action_help() {
106 "$_pacman_opts_commands[@]"
109 # handles cases where no subcommand has yet been given
110 _pacman_action_none() {
112 "$_pacman_opts_commands[@]"
115 # handles --query subcommand
116 _pacman_action_query() {
117 local context state line
121 # "$_pacman_opts_common[@]" \
122 # "$_pacman_opts_query_actions[@]" \
123 # "$_pacman_opts_query_modifiers[@]"
128 "$_pacman_opts_common[@]" \
129 "$_pacman_opts_query_modifiers[@]" \
130 '*:package file:_files -g "*.pkg.tar.*"'
134 "$_pacman_opts_common[@]" \
135 "$_pacman_opts_query_modifiers[@]" \
136 '*:groups:_pacman_completions_installed_groups'
140 "$_pacman_opts_common[@]" \
141 "$_pacman_opts_query_modifiers[@]" \
146 "$_pacman_opts_common[@]" \
147 "$_pacman_opts_query_modifiers[@]" \
152 "$_pacman_opts_common[@]" \
153 "$_pacman_opts_query_actions[@]" \
154 "$_pacman_opts_query_modifiers[@]" \
155 '*:package:_pacman_completions_installed_packages'
160 # handles --remove subcommand
161 _pacman_action_remove() {
163 "$_pacman_opts_common[@]" \
164 "$_pacman_opts_remove[@]"
167 # handles --sync subcommand
168 _pacman_action_sync() {
169 local context state line
173 # "$_pacman_opts_common[@]" \
174 # "$_pacman_opts_sync_actions[@]" #\
175 # #"$_pacman_opts_sync_modifiers[@]"
180 "$_pacman_opts_common[@]" \
181 "$_pacman_opts_sync_modifiers[@]" \
182 '*-c[Remove old packages from cache]' \
186 "$_pacman_opts_common[@]" \
187 "$_pacman_opts_sync_modifiers[@]" \
188 '*:package group:_pacman_completions_all_groups'
192 "$_pacman_opts_common[@]" \
193 "$_pacman_opts_sync_modifiers[@]" \
198 "$_pacman_opts_common[@]" \
199 "$_pacman_opts_sync_modifiers[@]" \
200 '*:package:_pacman_completions_all_packages'
205 # handles --upgrade subcommand
206 _pacman_action_upgrade() {
208 "$_pacman_opts_common[@]" \
209 "$_pacman_opts_pkgfile[@]"
212 # handles --version subcommand
213 _pacman_action_version() {
214 # no further arguments
218 # provides completions for package groups
219 _pacman_completions_all_groups() {
222 groups=( $(_call_program groups $cmd[@] -Sg) )
224 compadd "$@" -a groups
227 # provides completions for packages available from repositories
228 # these can be specified as either 'package' or 'repository/package'
229 _pacman_completions_all_packages() {
230 local -a cmd packages repositories packages_long
233 if compset -P1 '*/*'; then
234 packages=( $(_call_program packages $cmd[@] -Sql ${words[CURRENT]%/*}) )
236 _wanted repo_packages expl "repository/package" compadd ${(@)packages}
238 packages=( $(_call_program packages $cmd[@] -Sql) )
240 _wanted packages expl "packages" compadd - "${(@)packages}"
242 repositories=(${(o)${${${(M)${(f)"$(<@sysconfdir@/pacman.conf)"}:#\[*}/\[/}/\]/}:#options})
243 typeset -U repositories
244 _wanted repo_packages expl "repository/package" compadd -S "/" $repositories
248 # provides completions for package groups
249 _pacman_completions_installed_groups() {
252 groups=(${(o)${(f)"$(_call_program groups $cmd[@] -Qg)"}% *})
254 compadd "$@" -a groups
257 # provides completions for installed packages
258 _pacman_completions_installed_packages() {
259 local -a cmd packages packages_long
260 packages_long=(@localstatedir@/lib/pacman/local/*(/))
261 packages=( ${${packages_long#@localstatedir@/lib/pacman/local/}%-*-*} )
262 compadd "$@" -a packages
265 # provides completions for repository names
266 _pacman_completions_repositories() {
267 local -a cmd repositories
268 repositories=(${(o)${${${(M)${(f)"$(<@sysconfdir@/pacman.conf)"}:#\[*}/\[/}/\]/}:#options})
270 typeset -U repositories
271 compadd "$@" -a repositories
274 # builds command for invoking pacman in a _call_program command - extracts
275 # relevant options already specified (config file, etc)
276 # $cmd must be declared by calling function
277 _pacman_get_command() {
278 # this is mostly nicked from _perforce
281 for (( i = 2; i < CURRENT - 1; i++ )); do
282 if [[ ${words[i]} = "--config" || ${words[i]} = "--root" ]]; then
283 cmd+=( ${words[i,i+1]} )
293 "$_pacman_opts_common[@]" \
294 "$_pacman_opts_query_modifiers[@]" \
295 '*:groups:_pacman_completions_installed_groups'
299 "$_pacman_opts_common[@]" \
300 "$_pacman_opts_query_modifiers[@]" \
301 '*:package file:_files'
303 -Q*p*) # file *.pkg.tar.*
305 "$_pacman_opts_common[@]" \
306 "$_pacman_opts_query_modifiers[@]" \
307 '*:package file:_files -g "*.pkg.tar.*"'
309 -Q*) _pacman_action_query ;;
310 -R*) _pacman_action_remove ;;
311 -S*c*) # no completion
316 "$_pacman_opts_common[@]" \
317 "$_pacman_opts_sync_modifiers[@]" \
318 '*:package repo:_pacman_completions_repositories' \
322 "$_pacman_opts_common[@]" \
323 "$_pacman_opts_sync_modifiers[@]" \
324 '*:package group:_pacman_completions_all_groups'
326 -S*) _pacman_action_sync ;;
327 -U*) _pacman_action_upgrade ;;
328 -V*) _pacman_action_version ;;
329 -h*) _pacman_action_help ;;
330 - ) _pacman_action_none ;;
335 # run the main dispatcher