Rehash efficiently
[pacman-ng.git] / contrib / zsh_completion.in
blob16919134bdc49dfb3d1527e8189926c1743d5d78
1 #compdef pacman pacman.static=pacman
3 # copy this file to /usr/share/zsh/site-functions/_pacman
5 typeset -A opt_args
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]'
14         '-h[Display usage]'
17 # options for passing to _arguments: options common to all commands
18 _pacman_opts_common=(
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 -/'
22         '-v[Be more verbose]'
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
62 _pacman_opts_remove=(
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() {
99         _arguments -s : \
100                 "$_pacman_opts_commands[@]"
103 # handles cases where no subcommand has yet been given
104 _pacman_action_none() {
105         _arguments -s : \
106                 "$_pacman_opts_commands[@]"
109 # handles --query subcommand
110 _pacman_action_query() {
111         local context state line
112         typeset -A opt_args
113         
114 #       _arguments -s : \
115 #               "$_pacman_opts_common[@]" \
116 #               "$_pacman_opts_query_actions[@]" \
117 #               "$_pacman_opts_query_modifiers[@]"
118                 
119         case $state in
120                 query_file)
121                         _arguments -s : \
122                                 "$_pacman_opts_common[@]" \
123                                 "$_pacman_opts_query_modifiers[@]" \
124                                 '*:package file:_files -g "*.pkg.tar.*"'
125                                 ;;
126                 query_group)
127                         _arguments -s : \
128                                 "$_pacman_opts_common[@]" \
129                                 "$_pacman_opts_query_modifiers[@]" \
130                                 '*:groups:_pacman_completions_installed_groups'
131                                 ;;
132                 query_owner)
133                         _arguments -s : \
134                                 "$_pacman_opts_common[@]" \
135                                 "$_pacman_opts_query_modifiers[@]" \
136                                 '*:file:_files'
137                                 ;;
138                 query_search)
139                         _arguments -s : \
140                                 "$_pacman_opts_common[@]" \
141                                 "$_pacman_opts_query_modifiers[@]" \
142                                 '*:search text: '
143                                 ;;
144                 *)
145                         _arguments -s : \
146                                 "$_pacman_opts_common[@]" \
147                                 "$_pacman_opts_query_actions[@]" \
148                                 "$_pacman_opts_query_modifiers[@]" \
149                                 '*:package:_pacman_completions_installed_packages'
150                                 ;;
151         esac
154 # handles --remove subcommand
155 _pacman_action_remove() {
156         _arguments -s : \
157                 "$_pacman_opts_common[@]" \
158                 "$_pacman_opts_remove[@]"
161 # handles --sync subcommand
162 _pacman_action_sync() {
163         local context state line
164         typeset -A opt_args
165         
166 #       _arguments -s : \
167 #               "$_pacman_opts_common[@]" \
168 #               "$_pacman_opts_sync_actions[@]" #\
169 #               #"$_pacman_opts_sync_modifiers[@]"
170                 
171         case $state in
172                 sync_clean)
173                         _arguments -s : \
174                                 "$_pacman_opts_common[@]" \
175                                 "$_pacman_opts_sync_modifiers[@]" \
176                                 '*-c[Remove old packages from cache]' \
177                                 ;;
178                 sync_group)
179                         _arguments -s : \
180                                 "$_pacman_opts_common[@]" \
181                                 "$_pacman_opts_sync_modifiers[@]" \
182                                 '*:package group:_pacman_completions_all_groups'
183                                 ;;
184                 sync_search)
185                         _arguments -s : \
186                                 "$_pacman_opts_common[@]" \
187                                 "$_pacman_opts_sync_modifiers[@]" \
188                                 '*:search text: '
189                                 ;;
190                 *)
191                         _arguments -s : \
192                                 "$_pacman_opts_common[@]" \
193                                 "$_pacman_opts_sync_modifiers[@]" \
194                                 '*:package:_pacman_completions_all_packages'
195                                 ;;
196                 esac
199 # handles --upgrade subcommand
200 _pacman_action_upgrade() {
201         _arguments -s : \
202                 "$_pacman_opts_common[@]" \
203                 "$_pacman_opts_pkgfile[@]"
206 # handles --version subcommand
207 _pacman_action_version() {
208         # no further arguments
209         return 0
212 # provides completions for package groups
213 _pacman_completions_all_groups() {
214         local -a cmd groups
215         _pacman_get_command
216         groups=( $(_call_program groups $cmd[@] -Sg) )
217         compadd "$@" -a groups
220 # provides completions for packages available from repositories
221 # these can be specified as either 'package' or 'repository/package'
222 _pacman_completions_all_packages() {
223         local -a cmd packages repositories packages_long
225         repositories=(${(o)${${${(M)${(f)"$(<@sysconfdir@/pacman.conf)"}:#\[*}/\[/}/\]/}:#options})
226         typeset -U repositories
227         packages_long=(@localstatedir@/lib/pacman/sync/${^repositories}/*(/))
228         packages=(${(o)${${packages_long#@localstatedir@/lib/pacman/sync/}#*/}%-*-*} )
229         typeset -U packages
230         _wanted packages expl "packages" compadd - "${(@)packages}"
231         if [[ $PREFIX != */* ]] ; then
232                 repositories=(${(o)${${${(M)${(f)"$(<@sysconfdir@/pacman.conf)"}:#\[*}/\[/}/\]/}:#options})
233                 typeset -U repositories
234                 _wanted repo_packages expl "repository/package" compadd -S "/" $repositories
235         else
236                 compset -P '*/'
237                 packages_long=(@localstatedir@/lib/pacman/sync/$IPREFIX*(/))
238                 packages=(${(o)${${packages_long#@localstatedir@/lib/pacman/sync/}#*/}%-*-*} )
239                 typeset -U packages
240                 _wanted repo_packages expl "repository/package" compadd ${(@)packages}
241         fi
244 # provides completions for package groups
245 _pacman_completions_installed_groups() {
246         local -a cmd groups
247         _pacman_get_command
248         groups=(${(o)${(f)"$(pacman -Qg)"}% *})
249         typeset -U groups
250         compadd "$@" -a groups
253 # provides completions for installed packages
254 _pacman_completions_installed_packages() {
255         local -a cmd packages packages_long
256         packages_long=(@localstatedir@/lib/pacman/local/*(/))
257         packages=( ${${packages_long#@localstatedir@/lib/pacman/local/}%-*-*} )
258         compadd "$@" -a packages
261 # provides completions for repository names
262 _pacman_completions_repositories() {
263         local -a cmd repositories
264         repositories=(${(o)${${${(M)${(f)"$(<@sysconfdir@/pacman.conf)"}:#\[*}/\[/}/\]/}:#options})
265         # Uniq the array
266         typeset -U repositories
267         compadd "$@" -a repositories
270 # builds command for invoking pacman in a _call_program command - extracts
271 # relevant options already specified (config file, etc)
272 # $cmd must be declared by calling function
273 _pacman_get_command() {
274         # this is mostly nicked from _perforce
275         cmd=( "pacman" )
276         integer i
277         for (( i = 2; i < CURRENT - 1; i++ )); do
278                 if [[ ${words[i]} = "--config" || ${words[i]} = "--root" ]]; then
279                         cmd+=( ${words[i,i+1]} )
280                 fi
281         done
284 # main dispatcher
285 _pacman() {
286         case $words[2] in
287                 -Q*g*) # ipkg groups
288                         _arguments -s : \
289                                 "$_pacman_opts_common[@]" \
290                                 "$_pacman_opts_query_modifiers[@]" \
291                                 '*:groups:_pacman_completions_installed_groups'
292                                 ;;
293                 -Q*o*) # file
294                         _arguments -s : \
295                                 "$_pacman_opts_common[@]" \
296                                 "$_pacman_opts_query_modifiers[@]" \
297                                 '*:package file:_files'
298                                 ;;
299                 -Q*p*) # file *.pkg.tar.*
300                         _arguments -s : \
301                                 "$_pacman_opts_common[@]" \
302                                 "$_pacman_opts_query_modifiers[@]" \
303                                 '*:package file:_files -g "*.pkg.tar.*"'
304                                 ;;
305                 -Q*)  _pacman_action_query    ;;
306                 -R*)  _pacman_action_remove   ;;
307                 -S*c*) # no completion
308                         return 0
309                         ;;
310                 -S*l*) # repos
311                         _arguments -s : \
312                                 "$_pacman_opts_common[@]" \
313                                 "$_pacman_opts_sync_modifiers[@]" \
314                                 '*:package repo:_pacman_completions_repositories' \
315                                 ;;
316                 -S*g*) # pkg groups
317                         _arguments -s : \
318                                 "$_pacman_opts_common[@]" \
319                                 "$_pacman_opts_sync_modifiers[@]" \
320                                 '*:package group:_pacman_completions_all_groups'
321                                 ;;
322                 -S*)  _pacman_action_sync     ;;
323                 -U*)  _pacman_action_upgrade  ;;
324                 -V*)  _pacman_action_version  ;;
325                 -h*)  _pacman_action_help     ;;
326                 -  )  _pacman_action_none     ;;
327                 *  )  return 1                ;;
328         esac
331 # run the main dispatcher
332 _pacman "$@"