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 '*:package file:_files -g "*.pkg.tar.*(.)"'
39 # options for passing to _arguments: subactions for --query command
40 _pacman_opts_query_actions=(
41 '-g[View all members of a package group]:*:package groups:->query_group'
42 '-o[Query the package that owns a file]:file:_files'
43 '-p[Package file to query]:*:package file:->query_file'
44 '-s[Search package names and descriptions]:*:search text:->query_search'
47 # options for passing to _arguments: options for --query and subcommands
48 _pacman_opts_query_modifiers=(
49 '-c[List package changelog]'
50 '-d[List packages installed as dependencies]'
51 '-e[List packages explicitly installed]'
52 '-i[View package information]'
53 '-ii[View package information including backup files]'
54 '-k[Check package files]'
55 '-l[List package contents]'
56 '-m[List installed packages not found in sync db(s)]'
57 '-t[List packages not required by any package]'
58 '-u[List packages that can be upgraded]'
61 # options for passing to _arguments: options for --remove command
63 '-c[Remove all dependent packages]'
64 '-d[Skip dependency checks]'
65 '-k[Only remove database entry, do not remove files]'
66 '-n[Remove protected configuration files]'
67 '-s[Remove dependencies not required by other packages]'
68 '*:installed package:_pacman_completions_installed_packages'
71 # options for passing to _arguments: options for --sync command
72 _pacman_opts_sync_actions=(
73 '*-c[Remove old packages from cache]:*:clean:->sync_clean'
74 '*-cc[Remove all packages from cache]:*:clean:->sync_clean'
75 '-g[View all members of a package group]:*:package groups:->sync_group'
76 '-s[Search package names and descriptions]:*:search text:->sync_search'
79 # options for passing to _arguments: options for --sync command
80 _pacman_opts_sync_modifiers=(
81 '-d[Skip dependency checks]'
82 '-f[Overwrite conflicting files]'
83 '-i[View package information]'
84 '-l[List all packages in a repository]'
85 '-p[Print download URIs for each package to be installed]'
86 '-u[Upgrade all out-of-date packages]'
87 '-w[Download packages only]'
88 '-y[Download fresh package databases]'
89 '*--ignore[Ignore a package upgrade]:package:
90 _pacman_completions_all_packages'
91 '*--ignoregroup[Ignore a group upgrade]:package group:
92 _pacman_completions_all_groups'
93 '--asdeps[Install packages as non-explicitly installed]'
94 '--asexplicit[Install packages as explicitly installed]'
97 # handles --help subcommand
98 _pacman_action_help() {
100 "$_pacman_opts_commands[@]"
103 # handles cases where no subcommand has yet been given
104 _pacman_action_none() {
106 "$_pacman_opts_commands[@]"
109 # handles --query subcommand
110 _pacman_action_query() {
111 local context state line
115 # "$_pacman_opts_common[@]" \
116 # "$_pacman_opts_query_actions[@]" \
117 # "$_pacman_opts_query_modifiers[@]"
122 "$_pacman_opts_common[@]" \
123 "$_pacman_opts_query_modifiers[@]" \
124 '*:package file:_files -g "*.pkg.tar.*"'
128 "$_pacman_opts_common[@]" \
129 "$_pacman_opts_query_modifiers[@]" \
130 '*:groups:_pacman_completions_installed_groups'
134 "$_pacman_opts_common[@]" \
135 "$_pacman_opts_query_modifiers[@]" \
140 "$_pacman_opts_common[@]" \
141 "$_pacman_opts_query_modifiers[@]" \
146 "$_pacman_opts_common[@]" \
147 "$_pacman_opts_query_actions[@]" \
148 "$_pacman_opts_query_modifiers[@]" \
149 '*:package:_pacman_completions_installed_packages'
154 # handles --remove subcommand
155 _pacman_action_remove() {
157 "$_pacman_opts_common[@]" \
158 "$_pacman_opts_remove[@]"
161 # handles --sync subcommand
162 _pacman_action_sync() {
163 local context state line
167 # "$_pacman_opts_common[@]" \
168 # "$_pacman_opts_sync_actions[@]" #\
169 # #"$_pacman_opts_sync_modifiers[@]"
174 "$_pacman_opts_common[@]" \
175 "$_pacman_opts_sync_modifiers[@]" \
176 '*-c[Remove old packages from cache]' \
180 "$_pacman_opts_common[@]" \
181 "$_pacman_opts_sync_modifiers[@]" \
182 '*:package group:_pacman_completions_all_groups'
186 "$_pacman_opts_common[@]" \
187 "$_pacman_opts_sync_modifiers[@]" \
192 "$_pacman_opts_common[@]" \
193 "$_pacman_opts_sync_modifiers[@]" \
194 '*:package:_pacman_completions_all_packages'
199 # handles --upgrade subcommand
200 _pacman_action_upgrade() {
202 "$_pacman_opts_common[@]" \
203 "$_pacman_opts_pkgfile[@]"
206 # handles --version subcommand
207 _pacman_action_version() {
208 # no further arguments
212 # provides completions for package groups
213 _pacman_completions_all_groups() {
216 groups=( $(_call_program groups $cmd[@] -Sg) )
218 compadd "$@" -a groups
221 # provides completions for packages available from repositories
222 # these can be specified as either 'package' or 'repository/package'
223 _pacman_completions_all_packages() {
224 local -a cmd packages repositories packages_long
227 if compset -P1 '*/*'; then
228 packages=( $(_call_program packages $cmd[@] -Sql ${words[CURRENT]%/*}) )
230 _wanted repo_packages expl "repository/package" compadd ${(@)packages}
232 packages=( $(_call_program packages $cmd[@] -Sql) )
234 _wanted packages expl "packages" compadd - "${(@)packages}"
236 repositories=(${(o)${${${(M)${(f)"$(<@sysconfdir@/pacman.conf)"}:#\[*}/\[/}/\]/}:#options})
237 typeset -U repositories
238 _wanted repo_packages expl "repository/package" compadd -S "/" $repositories
242 # provides completions for package groups
243 _pacman_completions_installed_groups() {
246 groups=(${(o)${(f)"$(_call_program groups $cmd[@] -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=(@localstatedir@/lib/pacman/local/*(/))
255 packages=( ${${packages_long#@localstatedir@/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)"$(<@sysconfdir@/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.*
299 "$_pacman_opts_common[@]" \
300 "$_pacman_opts_query_modifiers[@]" \
301 '*:package file:_files -g "*.pkg.tar.*"'
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