Update Italian translation
[pacman-ng.git] / contrib / zsh_completion
blobe1273184c1f95fc28d537af5687ecad62c2fde9e
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]'
31 # options for passing to _arguments: options for --upgrade commands
32 _pacman_opts_pkgfile=(
33         '-d[Skip dependency checks]'
34         '-f[Overwrite conflicting files]'
35         '*:package file:_files -g "*.pkg.tar.gz(.)"'
38 # options for passing to _arguments: subactions for --query command
39 _pacman_opts_query_actions=(
40         '-g[View all members of a package group]:*:package groups:->query_group'
41         '-o[Query the package that owns a file]:file:_files'
42         '-p[Package file to query]:*:package file:->query_file'
43         '-s[Search package names and descriptions]:*:search text:->query_search'
46 # options for passing to _arguments: options for --query and subcommands
47 _pacman_opts_query_modifiers=(
48         '-c[List package changelog]'
49         '-d[List packages installed as dependencies]'
50         '-e[List packages explicitly installed]'
51         '-i[View package information]'
52         '-ii[View package information including backup files]'
53         '-l[List package contents]'
54         '-m[List installed packages not found in sync db(s)]'
55         '-t[List packages not required by any package]'
56         '-u[List packages that can be upgraded]'
59 # options for passing to _arguments: options for --remove command
60 _pacman_opts_remove=(
61         '-c[Remove all dependent packages]'
62         '-d[Skip dependency checks]'
63         '-k[Only remove database entry, do not remove files]'
64         '-n[Remove protected configuration files]'
65         '-s[Remove dependencies not required by other packages]'
66         '*:installed package:_pacman_completions_installed_packages'
69 # options for passing to _arguments: options for --sync command
70 _pacman_opts_sync_actions=(
71         '*-c[Remove old packages from cache]:*:clean:->sync_clean'
72         '*-cc[Remove all packages from cache]:*:clean:->sync_clean'
73         '-g[View all members of a package group]:*:package groups:->sync_group'
74         '-s[Search package names and descriptions]:*:search text:->sync_search'
77 # options for passing to _arguments: options for --sync command
78 _pacman_opts_sync_modifiers=(
79         '-d[Skip dependency checks]'
80         '-f[Overwrite conflicting files]'
81         '-i[View package information]'
82         '-l[List all packages in a repository]'
83         '-p[Print download URIs for each package to be installed]'
84         '-u[Upgrade all out-of-date packages]'
85         '-w[Download packages only]'
86         '-y[Download fresh package databases]'
87         '*--ignore[Ignore a package upgrade]:package:
88                 _pacman_completions_all_packages'
89         '*--ignoregroup[Ignore a group upgrade]:package group:
90                 _pacman_completions_all_groups'
91         '--asdeps[Install packages as non-explicitly installed]'
92         '--asexplicit[Install packages as explicitly installed]'
95 # handles --help subcommand
96 _pacman_action_help() {
97         _arguments -s : \
98                 "$_pacman_opts_commands[@]"
101 # handles cases where no subcommand has yet been given
102 _pacman_action_none() {
103         _arguments -s : \
104                 "$_pacman_opts_commands[@]"
107 # handles --query subcommand
108 _pacman_action_query() {
109         local context state line
110         typeset -A opt_args
111         
112 #       _arguments -s : \
113 #               "$_pacman_opts_common[@]" \
114 #               "$_pacman_opts_query_actions[@]" \
115 #               "$_pacman_opts_query_modifiers[@]"
116                 
117         case $state in
118                 query_file)
119                         _arguments -s : \
120                                 "$_pacman_opts_common[@]" \
121                                 "$_pacman_opts_query_modifiers[@]" \
122                                 '*:package file:_files -g "*.pkg.tar.gz"'
123                                 ;;
124                 query_group)
125                         _arguments -s : \
126                                 "$_pacman_opts_common[@]" \
127                                 "$_pacman_opts_query_modifiers[@]" \
128                                 '*:groups:_pacman_completions_installed_groups'
129                                 ;;
130                 query_owner)
131                         _arguments -s : \
132                                 "$_pacman_opts_common[@]" \
133                                 "$_pacman_opts_query_modifiers[@]" \
134                                 '*:file:_files'
135                                 ;;
136                 query_search)
137                         _arguments -s : \
138                                 "$_pacman_opts_common[@]" \
139                                 "$_pacman_opts_query_modifiers[@]" \
140                                 '*:search text: '
141                                 ;;
142                 *)
143                         _arguments -s : \
144                                 "$_pacman_opts_common[@]" \
145                                 "$_pacman_opts_query_actions[@]" \
146                                 "$_pacman_opts_query_modifiers[@]" \
147                                 '*:package:_pacman_completions_installed_packages'
148                                 ;;
149         esac
152 # handles --remove subcommand
153 _pacman_action_remove() {
154         _arguments -s : \
155                 "$_pacman_opts_common[@]" \
156                 "$_pacman_opts_remove[@]"
159 # handles --sync subcommand
160 _pacman_action_sync() {
161         local context state line
162         typeset -A opt_args
163         
164 #       _arguments -s : \
165 #               "$_pacman_opts_common[@]" \
166 #               "$_pacman_opts_sync_actions[@]" #\
167 #               #"$_pacman_opts_sync_modifiers[@]"
168                 
169         case $state in
170                 sync_clean)
171                         _arguments -s : \
172                                 "$_pacman_opts_common[@]" \
173                                 "$_pacman_opts_sync_modifiers[@]" \
174                                 '*-c[Remove old packages from cache]' \
175                                 ;;
176                 sync_group)
177                         _arguments -s : \
178                                 "$_pacman_opts_common[@]" \
179                                 "$_pacman_opts_sync_modifiers[@]" \
180                                 '*:package group:_pacman_completions_all_groups'
181                                 ;;
182                 sync_search)
183                         _arguments -s : \
184                                 "$_pacman_opts_common[@]" \
185                                 "$_pacman_opts_sync_modifiers[@]" \
186                                 '*:search text: '
187                                 ;;
188                 *)
189                         _arguments -s : \
190                                 "$_pacman_opts_common[@]" \
191                                 "$_pacman_opts_sync_modifiers[@]" \
192                                 '*:package:_pacman_completions_all_packages'
193                                 ;;
194                 esac
197 # handles --upgrade subcommand
198 _pacman_action_upgrade() {
199         _arguments -s : \
200                 "$_pacman_opts_common[@]" \
201                 "$_pacman_opts_pkgfile[@]"
204 # handles --version subcommand
205 _pacman_action_version() {
206         # no further arguments
207         return 0
210 # provides completions for package groups
211 _pacman_completions_all_groups() {
212         local -a cmd groups
213         _pacman_get_command
214         groups=( $(_call_program groups $cmd[@] -Sg) )
215         compadd "$@" -a groups
218 # provides completions for packages available from repositories
219 # these can be specified as either 'package' or 'repository/package'
220 _pacman_completions_all_packages() {
221         local -a cmd packages repositories packages_long
223         repositories=(${(o)${${${(M)${(f)"$(</etc/pacman.conf)"}:#\[*}/\[/}/\]/}:#options})
224         typeset -U repositories
225         packages_long=(/var/lib/pacman/sync/${^repositories}/*(/))
226         packages=(${(o)${${packages_long/\/var\/lib\/pacman\/sync\//}#*/}%-*-*} )
227         typeset -U packages
228         _wanted packages expl "packages" compadd - "${(@)packages}"
229         if [[ $PREFIX != */* ]] ; then
230                 repositories=(${(o)${${${(M)${(f)"$(</etc/pacman.conf)"}:#\[*}/\[/}/\]/}:#options})
231                 typeset -U repositories
232                 _wanted repo_packages expl "repository/package" compadd -S "/" $repositories
233         else
234                 compset -P '*/'
235                 packages_long=(/var/lib/pacman/sync/$IPREFIX*(/))
236                 packages=(${(o)${${packages_long/\/var\/lib\/pacman\/sync\//}#*/}%-*-*} )
237                 typeset -U packages
238                 _wanted repo_packages expl "repository/package" compadd ${(@)packages}
239         fi
242 # provides completions for package groups
243 _pacman_completions_installed_groups() {
244         local -a cmd groups
245         _pacman_get_command
246         groups=(${(o)${(f)"$(pacman -Qg)"}% *})
247         typeset -U groups
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=(/var/lib/pacman/local/*(/))
255         packages=( ${${packages_long/\/var\/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)"$(</etc/pacman.conf)"}:#\[*}/\[/}/\]/}:#options})
263         # Uniq the array
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
273         cmd=( "pacman" )
274         integer i
275         for (( i = 2; i < CURRENT - 1; i++ )); do
276                 if [[ ${words[i]} = "--config" || ${words[i]} = "--root" ]]; then
277                         cmd+=( ${words[i,i+1]} )
278                 fi
279         done
282 # main dispatcher
283 _pacman() {
284         case $words[2] in
285                 -Q*g*) # ipkg groups
286                         _arguments -s : \
287                                 "$_pacman_opts_common[@]" \
288                                 "$_pacman_opts_query_modifiers[@]" \
289                                 '*:groups:_pacman_completions_installed_groups'
290                                 ;;
291                 -Q*o*) # file
292                         _arguments -s : \
293                                 "$_pacman_opts_common[@]" \
294                                 "$_pacman_opts_query_modifiers[@]" \
295                                 '*:package file:_files'
296                                 ;;
297                 -Q*p*) # file *.pkg.tar.gz
298                         _arguments -s : \
299                                 "$_pacman_opts_common[@]" \
300                                 "$_pacman_opts_query_modifiers[@]" \
301                                 '*:package file:_files -g "*.pkg.tar.gz"'
302                                 ;;
303                 -Q*)  _pacman_action_query    ;;
304                 -R*)  _pacman_action_remove   ;;
305                 -S*c*) # no completion
306                         return 0
307                         ;;
308                 -S*l*) # repos
309                         _arguments -s : \
310                                 "$_pacman_opts_common[@]" \
311                                 "$_pacman_opts_sync_modifiers[@]" \
312                                 '*:package repo:_pacman_completions_repositories' \
313                                 ;;
314                 -S*g*) # pkg groups
315                         _arguments -s : \
316                                 "$_pacman_opts_common[@]" \
317                                 "$_pacman_opts_sync_modifiers[@]" \
318                                 '*:package group:_pacman_completions_all_groups'
319                                 ;;
320                 -S*)  _pacman_action_sync     ;;
321                 -U*)  _pacman_action_upgrade  ;;
322                 -V*)  _pacman_action_version  ;;
323                 -h*)  _pacman_action_help     ;;
324                 -  )  _pacman_action_none     ;;
325                 *  )  return 1                ;;
326         esac
329 # run the main dispatcher
330 _pacman "$@"