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 '-A[Add a package to the system]'
10 '-Q[Query the package database]'
11 '-R[Remove a package from the system]'
12 '-S[Synchronize packages]'
13 '-U[Upgrade a package]'
14 '-V[Display version and exit]'
18 # options for passing to _arguments: options common to all commands
20 '-b[Alternate database location]:database_location:_files -/'
21 '-h[Display syntax for the given operation]'
22 '-r[Set alternate installation root]:installation root:_files -/'
24 '--cachedir[Alternate package cache location]:cache_location:_files -/'
25 '--config[An alternate configuration file]:config file:_files'
26 '--logfile[An alternate log file]:config file:_files'
27 '--noconfirm[Do not ask for confirmation]'
28 '--noprogressbar[Do not show a progress bar when downloading files]'
29 '--noscriptlet[Do not execute the install scriptlet if one exists]'
32 # options for passing to _arguments: options for --add and --update commands
33 _pacman_opts_pkgfile=(
34 '-d[Skip dependency checks]'
35 '-f[Overwrite conflicting files]'
36 '*:package file:_files -g "*.pkg.tar.gz(.)"'
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 '-l[List package contents]'
55 '-m[List installed packages not found in sync db(s)]'
56 '-t[List packages not required by any package]'
57 '-u[List packages that can be upgraded]'
60 # options for passing to _arguments: options for --remove command
62 '-c[Remove all dependent packages]'
63 '-d[Skip dependency checks]'
64 '-k[Only remove database entry, do not remove files]'
65 '-n[Remove protected configuration files]'
66 '-s[Remove dependencies not required by other packages]'
67 '*:installed package:_pacman_completions_installed_packages'
70 # options for passing to _arguments: options for --sync command
71 _pacman_opts_sync_actions=(
72 '*-c[Remove old packages from cache]:*:clean:->sync_clean'
73 '*-cc[Remove all packages from cache]:*:clean:->sync_clean'
74 '-g[View all members of a package group]:*:package groups:->sync_group'
75 '-s[Search package names and descriptions]:*:search text:->sync_search'
78 # options for passing to _arguments: options for --sync command
79 _pacman_opts_sync_modifiers=(
80 '-d[Skip dependency checks]'
81 '-e[Install dependencies only]'
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 --action subcommand
98 _pacman_action_add() {
100 "$_pacman_opts_common[@]" \
101 "$_pacman_opts_pkgfile[@]"
104 # handles --help subcommand
105 _pacman_action_help() {
107 "$_pacman_opts_commands[@]"
110 # handles cases where no subcommand has yet been given
111 _pacman_action_none() {
113 "$_pacman_opts_commands[@]"
116 # handles --query subcommand
117 _pacman_action_query() {
118 local context state line
122 # "$_pacman_opts_common[@]" \
123 # "$_pacman_opts_query_actions[@]" \
124 # "$_pacman_opts_query_modifiers[@]"
129 "$_pacman_opts_common[@]" \
130 "$_pacman_opts_query_modifiers[@]" \
131 '*:package file:_files -g "*.pkg.tar.gz"'
135 "$_pacman_opts_common[@]" \
136 "$_pacman_opts_query_modifiers[@]" \
137 '*:groups:_pacman_completions_installed_groups'
141 "$_pacman_opts_common[@]" \
142 "$_pacman_opts_query_modifiers[@]" \
147 "$_pacman_opts_common[@]" \
148 "$_pacman_opts_query_modifiers[@]" \
153 "$_pacman_opts_common[@]" \
154 "$_pacman_opts_query_actions[@]" \
155 "$_pacman_opts_query_modifiers[@]" \
156 '*:package:_pacman_completions_installed_packages'
161 # handles --remove subcommand
162 _pacman_action_remove() {
164 "$_pacman_opts_common[@]" \
165 "$_pacman_opts_remove[@]"
168 # handles --sync subcommand
169 _pacman_action_sync() {
170 local context state line
174 # "$_pacman_opts_common[@]" \
175 # "$_pacman_opts_sync_actions[@]" #\
176 # #"$_pacman_opts_sync_modifiers[@]"
181 "$_pacman_opts_common[@]" \
182 "$_pacman_opts_sync_modifiers[@]" \
183 '*-c[Remove old packages from cache]' \
187 "$_pacman_opts_common[@]" \
188 "$_pacman_opts_sync_modifiers[@]" \
189 '*:package group:_pacman_completions_all_groups'
193 "$_pacman_opts_common[@]" \
194 "$_pacman_opts_sync_modifiers[@]" \
199 "$_pacman_opts_common[@]" \
200 "$_pacman_opts_sync_modifiers[@]" \
201 '*:package:_pacman_completions_all_packages'
206 # handles --upgrade subcommand
207 _pacman_action_upgrade() {
209 "$_pacman_opts_common[@]" \
210 "$_pacman_opts_pkgfile[@]"
213 # handles --version subcommand
214 _pacman_action_version() {
215 # no further arguments
219 # provides completions for package groups
220 _pacman_completions_all_groups() {
223 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
232 repositories=(${(o)${${${(M)${(f)"$(</etc/pacman.conf)"}:#\[*}/\[/}/\]/}:#options})
233 typeset -U repositories
234 packages_long=(/var/lib/pacman/sync/${^repositories}/*(/))
235 packages=(${(o)${${packages_long/\/var\/lib\/pacman\/sync\//}#*/}%-*-*} )
237 _wanted packages expl "packages" compadd - "${(@)packages}"
238 if [[ $PREFIX != */* ]] ; then
239 repositories=(${(o)${${${(M)${(f)"$(</etc/pacman.conf)"}:#\[*}/\[/}/\]/}:#options})
240 typeset -U repositories
241 _wanted repo_packages expl "repository/package" compadd -S "/" $repositories
244 packages_long=(/var/lib/pacman/sync/$IPREFIX*(/))
245 packages=(${(o)${${packages_long/\/var\/lib\/pacman\/sync\//}#*/}%-*-*} )
247 _wanted repo_packages expl "repository/package" compadd ${(@)packages}
251 # provides completions for package groups
252 _pacman_completions_installed_groups() {
255 groups=(${(o)${(f)"$(pacman -Qg)"}% *})
257 compadd "$@" -a groups
260 # provides completions for installed packages
261 _pacman_completions_installed_packages() {
262 local -a cmd packages packages_long
263 packages_long=(/var/lib/pacman/local/*(/))
264 packages=( ${${packages_long/\/var\/lib\/pacman\/local\//}%-*-*} )
265 compadd "$@" -a packages
268 # provides completions for repository names
269 _pacman_completions_repositories() {
270 local -a cmd repositories
271 repositories=(${(o)${${${(M)${(f)"$(</etc/pacman.conf)"}:#\[*}/\[/}/\]/}:#options})
273 typeset -U repositories
274 compadd "$@" -a repositories
277 # builds command for invoking pacman in a _call_program command - extracts
278 # relevant options already specified (config file, etc)
279 # $cmd must be declared by calling function
280 _pacman_get_command() {
281 # this is mostly nicked from _perforce
284 for (( i = 2; i < CURRENT - 1; i++ )); do
285 if [[ ${words[i]} = "--config" || ${words[i]} = "--root" ]]; then
286 cmd+=( ${words[i,i+1]} )
294 -A*) _pacman_action_add ;;
297 "$_pacman_opts_common[@]" \
298 "$_pacman_opts_query_modifiers[@]" \
299 '*:groups:_pacman_completions_installed_groups'
303 "$_pacman_opts_common[@]" \
304 "$_pacman_opts_query_modifiers[@]" \
305 '*:package file:_files'
307 -Q*p*) # file *.pkg.tar.gz
309 "$_pacman_opts_common[@]" \
310 "$_pacman_opts_query_modifiers[@]" \
311 '*:package file:_files -g "*.pkg.tar.gz"'
313 -Q*) _pacman_action_query ;;
314 -R*) _pacman_action_remove ;;
315 -S*c*) # no completion
320 "$_pacman_opts_common[@]" \
321 "$_pacman_opts_sync_modifiers[@]" \
322 '*:package repo:_pacman_completions_repositories' \
326 "$_pacman_opts_common[@]" \
327 "$_pacman_opts_sync_modifiers[@]" \
328 '*:package group:_pacman_completions_all_groups'
330 -S*) _pacman_action_sync ;;
331 -U*) _pacman_action_upgrade ;;
332 -V*) _pacman_action_version ;;
333 -h*) _pacman_action_help ;;
334 - ) _pacman_action_none ;;
339 # run the main dispatcher