New --asexplicit option
[pacman-ng.git] / contrib / zsh_completion
blobe5ad92b72c5b502b7dab292c891022bfdcf280c4
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         '-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]'
15         '-h[Display usage]'
18 # options for passing to _arguments: options common to all commands
19 _pacman_opts_common=(
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 -/'
23         '-v[Be more verbose]'
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
61 _pacman_opts_remove=(
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() {
99         _arguments -s : \
100                 "$_pacman_opts_common[@]" \
101                 "$_pacman_opts_pkgfile[@]"
104 # handles --help subcommand
105 _pacman_action_help() {
106         _arguments -s : \
107                 "$_pacman_opts_commands[@]"
110 # handles cases where no subcommand has yet been given
111 _pacman_action_none() {
112         _arguments -s : \
113                 "$_pacman_opts_commands[@]"
116 # handles --query subcommand
117 _pacman_action_query() {
118         local context state line
119         typeset -A opt_args
120         
121 #       _arguments -s : \
122 #               "$_pacman_opts_common[@]" \
123 #               "$_pacman_opts_query_actions[@]" \
124 #               "$_pacman_opts_query_modifiers[@]"
125                 
126         case $state in
127                 query_file)
128                         _arguments -s : \
129                                 "$_pacman_opts_common[@]" \
130                                 "$_pacman_opts_query_modifiers[@]" \
131                                 '*:package file:_files -g "*.pkg.tar.gz"'
132                                 ;;
133                 query_group)
134                         _arguments -s : \
135                                 "$_pacman_opts_common[@]" \
136                                 "$_pacman_opts_query_modifiers[@]" \
137                                 '*:groups:_pacman_completions_installed_groups'
138                                 ;;
139                 query_owner)
140                         _arguments -s : \
141                                 "$_pacman_opts_common[@]" \
142                                 "$_pacman_opts_query_modifiers[@]" \
143                                 '*:file:_files'
144                                 ;;
145                 query_search)
146                         _arguments -s : \
147                                 "$_pacman_opts_common[@]" \
148                                 "$_pacman_opts_query_modifiers[@]" \
149                                 '*:search text: '
150                                 ;;
151                 *)
152                         _arguments -s : \
153                                 "$_pacman_opts_common[@]" \
154                                 "$_pacman_opts_query_actions[@]" \
155                                 "$_pacman_opts_query_modifiers[@]" \
156                                 '*:package:_pacman_completions_installed_packages'
157                                 ;;
158         esac
161 # handles --remove subcommand
162 _pacman_action_remove() {
163         _arguments -s : \
164                 "$_pacman_opts_common[@]" \
165                 "$_pacman_opts_remove[@]"
168 # handles --sync subcommand
169 _pacman_action_sync() {
170         local context state line
171         typeset -A opt_args
172         
173 #       _arguments -s : \
174 #               "$_pacman_opts_common[@]" \
175 #               "$_pacman_opts_sync_actions[@]" #\
176 #               #"$_pacman_opts_sync_modifiers[@]"
177                 
178         case $state in
179                 sync_clean)
180                         _arguments -s : \
181                                 "$_pacman_opts_common[@]" \
182                                 "$_pacman_opts_sync_modifiers[@]" \
183                                 '*-c[Remove old packages from cache]' \
184                                 ;;
185                 sync_group)
186                         _arguments -s : \
187                                 "$_pacman_opts_common[@]" \
188                                 "$_pacman_opts_sync_modifiers[@]" \
189                                 '*:package group:_pacman_completions_all_groups'
190                                 ;;
191                 sync_search)
192                         _arguments -s : \
193                                 "$_pacman_opts_common[@]" \
194                                 "$_pacman_opts_sync_modifiers[@]" \
195                                 '*:search text: '
196                                 ;;
197                 *)
198                         _arguments -s : \
199                                 "$_pacman_opts_common[@]" \
200                                 "$_pacman_opts_sync_modifiers[@]" \
201                                 '*:package:_pacman_completions_all_packages'
202                                 ;;
203                 esac
206 # handles --upgrade subcommand
207 _pacman_action_upgrade() {
208         _arguments -s : \
209                 "$_pacman_opts_common[@]" \
210                 "$_pacman_opts_pkgfile[@]"
213 # handles --version subcommand
214 _pacman_action_version() {
215         # no further arguments
216         return 0
219 # provides completions for package groups
220 _pacman_completions_all_groups() {
221         local -a cmd groups
222         _pacman_get_command
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\//}#*/}%-*-*} )
236         typeset -U packages
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
242         else
243                 compset -P '*/'
244                 packages_long=(/var/lib/pacman/sync/$IPREFIX*(/))
245                 packages=(${(o)${${packages_long/\/var\/lib\/pacman\/sync\//}#*/}%-*-*} )
246                 typeset -U packages
247                 _wanted repo_packages expl "repository/package" compadd ${(@)packages}
248         fi
251 # provides completions for package groups
252 _pacman_completions_installed_groups() {
253         local -a cmd groups
254         _pacman_get_command
255         groups=(${(o)${(f)"$(pacman -Qg)"}% *})
256         typeset -U groups
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})
272         # Uniq the array
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
282         cmd=( "pacman" )
283         integer i
284         for (( i = 2; i < CURRENT - 1; i++ )); do
285                 if [[ ${words[i]} = "--config" || ${words[i]} = "--root" ]]; then
286                         cmd+=( ${words[i,i+1]} )
287                 fi
288         done
291 # main dispatcher
292 _pacman() {
293         case $words[2] in
294                 -A*)  _pacman_action_add      ;;
295                 -Q*g*) # ipkg groups
296                         _arguments -s : \
297                                 "$_pacman_opts_common[@]" \
298                                 "$_pacman_opts_query_modifiers[@]" \
299                                 '*:groups:_pacman_completions_installed_groups'
300                                 ;;
301                 -Q*o*) # file
302                         _arguments -s : \
303                                 "$_pacman_opts_common[@]" \
304                                 "$_pacman_opts_query_modifiers[@]" \
305                                 '*:package file:_files'
306                                 ;;
307                 -Q*p*) # file *.pkg.tar.gz
308                         _arguments -s : \
309                                 "$_pacman_opts_common[@]" \
310                                 "$_pacman_opts_query_modifiers[@]" \
311                                 '*:package file:_files -g "*.pkg.tar.gz"'
312                                 ;;
313                 -Q*)  _pacman_action_query    ;;
314                 -R*)  _pacman_action_remove   ;;
315                 -S*c*) # no completion
316                         return 0
317                         ;;
318                 -S*l*) # repos
319                         _arguments -s : \
320                                 "$_pacman_opts_common[@]" \
321                                 "$_pacman_opts_sync_modifiers[@]" \
322                                 '*:package repo:_pacman_completions_repositories' \
323                                 ;;
324                 -S*g*) # pkg groups
325                         _arguments -s : \
326                                 "$_pacman_opts_common[@]" \
327                                 "$_pacman_opts_sync_modifiers[@]" \
328                                 '*:package group:_pacman_completions_all_groups'
329                                 ;;
330                 -S*)  _pacman_action_sync     ;;
331                 -U*)  _pacman_action_upgrade  ;;
332                 -V*)  _pacman_action_version  ;;
333                 -h*)  _pacman_action_help     ;;
334                 -  )  _pacman_action_none     ;;
335                 *  )  return 1                ;;
336         esac
339 # run the main dispatcher
340 _pacman "$@"