autoclean.sh: abide by POSIX shebang
[pacman-ng.git] / contrib / zsh_completion.in
blobb30e9600da9261f11d234c1174fb915edc3defbb
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         '--dbonly[Only remove database entry, do not remove files]'
36         '--force[Overwrite conflicting 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
65 _pacman_opts_remove=(
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         '-i[View package information]'
89         '-l[List all packages in a repository]'
90         '-p[Print download URIs for each package to be installed]'
91         '-u[Upgrade all out-of-date packages]'
92         '-w[Download packages only]'
93         '-y[Download fresh package databases]'
94         '*--ignore[Ignore a package upgrade]:package:
95                 _pacman_completions_all_packages'
96         '*--ignoregroup[Ignore a group upgrade]:package group:
97                 _pacman_completions_all_groups'
98         '--asdeps[Install packages as non-explicitly installed]'
99         '--asexplicit[Install packages as explicitly installed]'
100         '--force[Overwrite conflicting files]'
103 # handles --help subcommand
104 _pacman_action_help() {
105         _arguments -s : \
106                 "$_pacman_opts_commands[@]"
109 # handles cases where no subcommand has yet been given
110 _pacman_action_none() {
111         _arguments -s : \
112                 "$_pacman_opts_commands[@]"
115 # handles --query subcommand
116 _pacman_action_query() {
117         local context state line
118         typeset -A opt_args
120 #       _arguments -s : \
121 #               "$_pacman_opts_common[@]" \
122 #               "$_pacman_opts_query_actions[@]" \
123 #               "$_pacman_opts_query_modifiers[@]"
125         case $state in
126                 query_file)
127                         _arguments -s : \
128                                 "$_pacman_opts_common[@]" \
129                                 "$_pacman_opts_query_modifiers[@]" \
130                                 '*:package file:_files -g "*.pkg.tar*"'
131                                 ;;
132                 query_group)
133                         _arguments -s : \
134                                 "$_pacman_opts_common[@]" \
135                                 "$_pacman_opts_query_modifiers[@]" \
136                                 '*:groups:_pacman_completions_installed_groups'
137                                 ;;
138                 query_owner)
139                         _arguments -s : \
140                                 "$_pacman_opts_common[@]" \
141                                 "$_pacman_opts_query_modifiers[@]" \
142                                 '*:file:_files'
143                                 ;;
144                 query_search)
145                         _arguments -s : \
146                                 "$_pacman_opts_common[@]" \
147                                 "$_pacman_opts_query_modifiers[@]" \
148                                 '*:search text: '
149                                 ;;
150                 *)
151                         _arguments -s : \
152                                 "$_pacman_opts_common[@]" \
153                                 "$_pacman_opts_query_actions[@]" \
154                                 "$_pacman_opts_query_modifiers[@]" \
155                                 '*:package:_pacman_completions_installed_packages'
156                                 ;;
157         esac
160 # handles --remove subcommand
161 _pacman_action_remove() {
162         _arguments -s : \
163                 "$_pacman_opts_common[@]" \
164                 "$_pacman_opts_remove[@]"
167 # handles --sync subcommand
168 _pacman_action_sync() {
169         local context state line
170         typeset -A opt_args
172 #       _arguments -s : \
173 #               "$_pacman_opts_common[@]" \
174 #               "$_pacman_opts_sync_actions[@]" #\
175 #               #"$_pacman_opts_sync_modifiers[@]"
177         case $state in
178                 sync_clean)
179                         _arguments -s : \
180                                 "$_pacman_opts_common[@]" \
181                                 "$_pacman_opts_sync_modifiers[@]" \
182                                 '*-c[Remove old packages from cache]' \
183                                 ;;
184                 sync_group)
185                         _arguments -s : \
186                                 "$_pacman_opts_common[@]" \
187                                 "$_pacman_opts_sync_modifiers[@]" \
188                                 '*:package group:_pacman_completions_all_groups'
189                                 ;;
190                 sync_search)
191                         _arguments -s : \
192                                 "$_pacman_opts_common[@]" \
193                                 "$_pacman_opts_sync_modifiers[@]" \
194                                 '*:search text: '
195                                 ;;
196                 *)
197                         _arguments -s : \
198                                 "$_pacman_opts_common[@]" \
199                                 "$_pacman_opts_sync_modifiers[@]" \
200                                 '*:package:_pacman_completions_all_packages'
201                                 ;;
202                 esac
205 # handles --upgrade subcommand
206 _pacman_action_upgrade() {
207         _arguments -s : \
208                 "$_pacman_opts_common[@]" \
209                 "$_pacman_opts_pkgfile[@]"
212 # handles --version subcommand
213 _pacman_action_version() {
214         # no further arguments
215         return 0
218 # provides completions for package groups
219 _pacman_completions_all_groups() {
220         local -a cmd groups
221         _pacman_get_command
222         groups=( $(_call_program groups $cmd[@] -Sg) )
223         typeset -U groups
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
231         _pacman_get_command
233         if compset -P1 '*/*'; then
234                 packages=( $(_call_program packages $cmd[@] -Sql ${words[CURRENT]%/*}) )
235                 typeset -U packages
236                 _wanted repo_packages expl "repository/package" compadd ${(@)packages}
237         else
238                 packages=( $(_call_program packages $cmd[@] -Sql) )
239                 typeset -U packages
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
245         fi
248 # provides completions for package groups
249 _pacman_completions_installed_groups() {
250         local -a cmd groups
251         _pacman_get_command
252         groups=(${(o)${(f)"$(_call_program groups $cmd[@] -Qg)"}% *})
253         typeset -U groups
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})
269         # Uniq the array
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
279         cmd=( "pacman" "2>/dev/null")
280         integer i
281         for (( i = 2; i < CURRENT - 1; i++ )); do
282                 if [[ ${words[i]} = "--config" || ${words[i]} = "--root" ]]; then
283                         cmd+=( ${words[i,i+1]} )
284                 fi
285         done
288 # main dispatcher
289 _pacman() {
290         case $words[2] in
291                 -Q*g*) # ipkg groups
292                         _arguments -s : \
293                                 "$_pacman_opts_common[@]" \
294                                 "$_pacman_opts_query_modifiers[@]" \
295                                 '*:groups:_pacman_completions_installed_groups'
296                                 ;;
297                 -Q*o*) # file
298                         _arguments -s : \
299                                 "$_pacman_opts_common[@]" \
300                                 "$_pacman_opts_query_modifiers[@]" \
301                                 '*:package file:_files'
302                                 ;;
303                 -Q*p*) # file *.pkg.tar*
304                         _arguments -s : \
305                                 "$_pacman_opts_common[@]" \
306                                 "$_pacman_opts_query_modifiers[@]" \
307                                 '*:package file:_files -g "*.pkg.tar*"'
308                                 ;;
309                 -Q*)  _pacman_action_query    ;;
310                 -R*)  _pacman_action_remove   ;;
311                 -S*c*) # no completion
312                         return 0
313                         ;;
314                 -S*l*) # repos
315                         _arguments -s : \
316                                 "$_pacman_opts_common[@]" \
317                                 "$_pacman_opts_sync_modifiers[@]" \
318                                 '*:package repo:_pacman_completions_repositories' \
319                                 ;;
320                 -S*g*) # pkg groups
321                         _arguments -s : \
322                                 "$_pacman_opts_common[@]" \
323                                 "$_pacman_opts_sync_modifiers[@]" \
324                                 '*:package group:_pacman_completions_all_groups'
325                                 ;;
326                 -S*)  _pacman_action_sync     ;;
327                 -U*)  _pacman_action_upgrade  ;;
328                 -V*)  _pacman_action_version  ;;
329                 -h*)  _pacman_action_help     ;;
330                 -  )  _pacman_action_none     ;;
331                 *  )  return 1                ;;
332         esac
335 # run the main dispatcher
336 _pacman "$@"