zsh 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. #compdef yay
  2. # vim:fdm=marker foldlevel=0 tabstop=2 shiftwidth=2 filetype=zsh
  3. typeset -A opt_args
  4. setopt extendedglob
  5. # options for passing to _arguments: main pacman commands
  6. _pacman_opts_commands=(
  7. {-D,--database}'[Modify database]'
  8. {-F,--files}'[Query the files database]'
  9. {-G,--getpkgbuild}'[Get PKGBUILD from ABS or AUR]'
  10. {-Q,--query}'[Query the package database]'
  11. {-R,--remove}'[Remove a package from the system]'
  12. {-P,--print}'[Print yay information]'
  13. {-S,--sync}'[Synchronize packages]'
  14. {-T,--deptest}'[Check if dependencies are installed]'
  15. {-U,--upgrade}'[Upgrade a package]'
  16. {-Y,--yay}'[Yay specific options]'
  17. {-V,--version}'[Display version and exit]'
  18. '(-h --help)'{-h,--help}'[Display usage]'
  19. )
  20. # options for passing to _arguments: options common to all commands
  21. _pacman_opts_common=(
  22. '--arch[Set an alternate architecture]'
  23. {-b,--dbpath}'[Alternate database location]:database_location:_files -/'
  24. '--color[colorize the output]:color options:(always never auto)'
  25. {-h,--help}'[Display syntax for the given operation]'
  26. {-r,--root}'[Set alternate installation root]:installation root:_files -/'
  27. {-v,--verbose}'[Be more verbose]'
  28. '--cachedir[Alternate package cache location]:cache_location:_files -/'
  29. '--config[An alternate configuration file]:config file:_files'
  30. '--confirm[Always ask for confirmation]'
  31. '--debug[Display debug messages]'
  32. '--gpgdir[Set an alternate directory for GnuPG (instead of /etc/pacman.d/gnupg)]: :_files -/'
  33. '--hookdir[Set an alternate hook location]: :_files -/'
  34. '--logfile[An alternate log file]:config file:_files'
  35. '--noconfirm[Do not ask for confirmation]'
  36. '--noprogressbar[Do not show a progress bar when downloading files]'
  37. '--noscriptlet[Do not execute the install scriptlet if one exists]'
  38. '--save[Causes config options to be saved back to the config file]'
  39. '--builddir[Directory to use for building AUR Packages]:build dir:_files -/'
  40. '--editor[Editor to use when editing PKGBUILDs]:editor:_files'
  41. '--makepkg[makepkg command to use]:makepkg:_files'
  42. '--pacman[pacman command to use]:pacman:_files'
  43. '--tar[bsdtar command to use]:tar:_files'
  44. '--git[git command to use]:git:_files'
  45. '--gpg[gpg command to use]:gpg:_files'
  46. '--bottomup[Show AUR packages first]'
  47. '--topdown[Show repository packages first]'
  48. '--devel[Check -git/-svn/-hg development version]'
  49. '--nodevel[Disable development version checking]'
  50. '--afterclean[Clean package sources after successful build]'
  51. '--noafterclean[Disable package sources cleaning after successful build]'
  52. '--timeupdate[Check packages modification date and version]'
  53. '--notimeupdate[Check only package version change]'
  54. '--redownload[Always download pkgbuilds of targets]'
  55. '--redownloadall[Always download pkgbuilds of all AUR packages]'
  56. '--noredownload[Skip pkgbuild download if in cache and up to date]'
  57. '--mflags[Pass arguments to makepkg]:mflags'
  58. '--gpgflags[Pass arguments to gpg]:gpgflags'
  59. '--sudoloop[Loop sudo calls in the backgroud to avoid timeout]'
  60. '--nosudoloop[Do not loop sudo calls in the backgrount]'
  61. )
  62. # options for passing to _arguments: options for --upgrade commands
  63. _pacman_opts_pkgfile=(
  64. '*-d[Skip dependency checks]'
  65. '*--nodeps[Skip dependency checks]'
  66. '*--assume-installed[Add virtual package to satisfy dependencies]'
  67. '--dbonly[Only remove database entry, do not remove files]'
  68. '--force[Overwrite conflicting files]'
  69. '--needed[Do not reinstall up to date packages]'
  70. '--asdeps[mark packages as non-explicitly installed]'
  71. '--asexplicit[mark packages as explicitly installed]'
  72. {-p,--print}'[Only print the targets instead of performing the operation]'
  73. '*--ignore[Ignore a package upgrade]:package: _pacman_completions_all_packages'
  74. '*--ignoregroup[Ignore a group upgrade]:package group:_pacman_completions_all_groups'
  75. '--print-format[Specify how the targets should be printed]'
  76. '*:package file:_files -g "*.pkg.tar*~*.sig(.,@)"'
  77. )
  78. # options for passing to _arguments: subactions for --query command
  79. _pacman_opts_query_actions=(
  80. '(-Q --query)'{-Q,--query}
  81. {-g,--groups}'[View all members of a package group]:*:package groups:->query_group'
  82. {-o,--owns}'[Query the package that owns a file]:file:_files'
  83. {-p,--file}'[Package file to query]:*:package file:->query_file'
  84. {-s,--search}'[Search package names and descriptions]:*:search text:->query_search'
  85. )
  86. # options for passing to _arguments: options for --query and subcommands
  87. _pacman_opts_query_modifiers=(
  88. {-c,--changelog}'[List package changelog]'
  89. {-d,--deps}'[List packages installed as dependencies]'
  90. {-e,--explicit}'[List packages explicitly installed]'
  91. {\*-i,\*--info}'[View package information]'
  92. {\*-k,\*--check}'[Check package files]'
  93. {-l,--list}'[List package contents]'
  94. {-m,--foreign}'[List installed packages not found in sync db(s)]'
  95. {-n,--native}'[List installed packages found in sync db(s)]'
  96. {-q,--quiet}'[Show less information for query and search]'
  97. {-t,--unrequired}'[List packages not required by any package]'
  98. {-u,--upgrades}'[List packages that can be upgraded]'
  99. )
  100. # -Y
  101. _pacman_opts_yay_modifiers=(
  102. {-c,--clean}'[Remove unneeded dependencies]'
  103. '--gendb[Generates development package DB used for updating]'
  104. )
  105. # -P
  106. _pacman_opts_print_modifiers=(
  107. {-c,--complete}'[Used for completions]'
  108. {-d,--defaultconfig}'[Print default yay configuration]'
  109. {-g,--config}'[Print current yay configuration]'
  110. {-n,--numberupgrades}'[Print number of updates]'
  111. {-s,--stats}'[Display system package statistics]'
  112. {-u,--upgrades}'[Print update list]'
  113. )
  114. # options for passing to _arguments: options for --remove command
  115. _pacman_opts_remove=(
  116. {-c,--cascade}'[Remove all dependent packages]'
  117. {-d,--nodeps}'[Skip dependency checks]'
  118. '*--assume-installed[Add virtual package to satisfy dependencies]'
  119. {-n,--nosave}'[Remove protected configuration files]'
  120. {-p,--print}'[Only print the targets instead of performing the operation]'
  121. {\*-s,\*--recursive}'[Remove dependencies not required by other packages]'
  122. {-u,--unneeded}'[Remove unneeded packages]'
  123. '--dbonly[Only remove database entry, do not remove files]'
  124. '--print-format[Specify how the targets should be printed]'
  125. '*:installed package:_pacman_completions_installed_packages'
  126. )
  127. _pacman_opts_database=(
  128. '--asdeps[mark packages as non-explicitly installed]'
  129. '--asexplicit[mark packages as explicitly installed]'
  130. '*:installed package:_pacman_completions_installed_packages'
  131. )
  132. _pacman_opts_files=(
  133. {-l,--list}'[List the files owned by the queried package]:package:_pacman_completions_all_packages'
  134. {-o,--owns}'[Query the package that owns]:files:_files'
  135. {-s,--search}'[Search package file names for matching strings]:files:_files'
  136. {-x,--regex}'[Enable searching using regluar expressions]:regex:'
  137. {-y,--refresh}'[Download fresh files databases from the server]'
  138. '--machinereadable[Produce machine-readable output]'
  139. {-q,--quiet}'[Show less information for query and search]'
  140. )
  141. # options for passing to _arguments: options for --sync command
  142. _pacman_opts_sync_actions=(
  143. '(-S --sync)'{-S,--sync}
  144. {\*-c,\*--clean}'[Remove old packages from cache]:\*:clean:->sync_clean'
  145. {-g,--groups}'[View all members of a package group]:*:package groups:->sync_group'
  146. {-s,--search}'[Search package names and descriptions]:*:search text:->sync_search'
  147. '--dbonly[Only remove database entry, do not remove files]'
  148. '--needed[Do not reinstall up to date packages]'
  149. '--recursive[Reinstall all dependencies of target packages]'
  150. )
  151. # options for passing to _arguments: options for --sync command
  152. _pacman_opts_sync_modifiers=(
  153. {\*-d,\*--nodeps}'[Skip dependency checks]'
  154. '*--assume-installed[Add virtual package to satisfy dependencies]'
  155. {\*-i,\*--info}'[View package information]'
  156. {-l,--list}'[List all packages in a repository]'
  157. {-p,--print}'[Print download URIs for each package to be installed]'
  158. {-q,--quiet}'[Show less information for query and search]'
  159. {\*-u,\*--sysupgrade}'[Upgrade all out-of-date packages]'
  160. {-w,--downloadonly}'[Download packages only]'
  161. {\*-y,\*--refresh}'[Download fresh package databases]'
  162. '*--ignore[Ignore a package upgrade]:package: _pacman_completions_all_packages'
  163. '*--ignoregroup[Ignore a group upgrade]:package group:_pacman_completions_all_groups'
  164. '--asdeps[Install packages as non-explicitly installed]'
  165. '--asexplicit[Install packages as explicitly installed]'
  166. '--force[Overwrite conflicting files]'
  167. '--print-format[Specify how the targets should be printed]'
  168. )
  169. # handles --help subcommand
  170. _pacman_action_help() {
  171. _arguments -s : "$_pacman_opts_commands[@]"
  172. }
  173. # handles cases where no subcommand has yet been given
  174. _pacman_action_none() {
  175. _arguments -s : "$_pacman_opts_commands[@]"
  176. }
  177. # handles --query subcommand
  178. _pacman_action_query() {
  179. local context state line
  180. typeset -A opt_args
  181. case $state in
  182. query_file)
  183. _arguments -s : "$_pacman_opts_common[@]" "$_pacman_opts_query_modifiers[@]" '*:package file:_files -g "*.pkg.tar*~*.sig(.,@)"'
  184. query_group)
  185. _arguments -s : "$_pacman_opts_common[@]" "$_pacman_opts_query_modifiers[@]" '*:groups:_pacman_completions_installed_groups'
  186. query_owner)
  187. _arguments -s : "$_pacman_opts_common[@]" "$_pacman_opts_query_modifiers[@]" '*:file:_files'
  188. query_search)
  189. _arguments -s : "$_pacman_opts_common[@]" "$_pacman_opts_query_modifiers[@]" '*:search text: '
  190. *)
  191. _arguments -s : "$_pacman_opts_common[@]" "$_pacman_opts_query_actions[@]" "$_pacman_opts_query_modifiers[@]" '*:package:_pacman_completions_installed_packages'
  192. esac
  193. }
  194. # handles --remove subcommand
  195. _pacman_action_remove() {
  196. _arguments -s : '(--remove -R)'{-R,--remove} "$_pacman_opts_common[@]" "$_pacman_opts_remove[@]"
  197. }
  198. # handles --database subcommand
  199. _pacman_action_database() {
  200. _arguments -s : '(--database -D)'{-D,--database} "$_pacman_opts_common[@]" "$_pacman_opts_database[@]"
  201. }
  202. # handles --files subcommand
  203. _pacman_action_files() {
  204. _arguments -s : '(--files -F)'{-F,--files} "$_pacman_opts_common[@]" "$_pacman_opts_files[@]"
  205. }
  206. _pacman_action_deptest () {
  207. _arguments -s : '(--deptest)-T' "$_pacman_opts_common[@]" ":packages:_pacman_all_packages"
  208. }
  209. # handles --sync subcommand
  210. _pacman_action_sync() {
  211. local context state line
  212. typeset -A opt_args
  213. if (( $+words[(r)--clean] ))
  214. then
  215. state=sync_clean
  216. elif (( $+words[(r)--groups] ))
  217. then
  218. state=sync_group
  219. elif (( $+words[(r)--search] ))
  220. then
  221. state=sync_search
  222. fi
  223. case $state in
  224. sync_clean)
  225. _arguments -s : {\*-c,\*--clean}'[Remove old packages from cache]' "$_pacman_opts_common[@]" "$_pacman_opts_sync_modifiers[@]"
  226. sync_group)
  227. _arguments -s : "$_pacman_opts_common[@]" "$_pacman_opts_sync_modifiers[@]" '(-g --group)'{-g,--groups} '*:package group:_pacman_completions_all_groups'
  228. sync_search)
  229. _arguments -s : "$_pacman_opts_common[@]" "$_pacman_opts_sync_modifiers[@]" '*:search text: '
  230. *)
  231. _arguments -s : "$_pacman_opts_common[@]" "$_pacman_opts_sync_actions[@]" "$_pacman_opts_sync_modifiers[@]" '*:package:_pacman_completions_all_packages'
  232. esac
  233. }
  234. # handles --upgrade subcommand
  235. _pacman_action_upgrade() {
  236. _arguments -s : '(-U --upgrade)'{-U,--upgrade} "$_pacman_opts_common[@]" "$_pacman_opts_pkgfile[@]"
  237. }
  238. # handles --version subcommand
  239. _pacman_action_version() {
  240. # no further arguments
  241. return 0
  242. }
  243. # provides completions for package groups
  244. _pacman_completions_all_groups() {
  245. local -a cmd groups
  246. _pacman_get_command
  247. groups=( $(_call_program groups $cmd[@] -Sg) )
  248. typeset -U groups
  249. if [[ ${words[CURRENT-1]} == '--ignoregroup' ]]
  250. then
  251. _sequence compadd -S ',' "$@" -a groups
  252. else
  253. compadd "$@" -a groups
  254. fi
  255. }
  256. # provides completions for packages available from repositories
  257. # these can be specified as either 'package' or 'repository/package'
  258. _pacman_completions_all_packages() {
  259. local -a seq sep cmd packages repositories packages_long
  260. if [[ ${words[CURRENT-1]} == '--ignore' ]]
  261. then
  262. seq='_sequence'
  263. sep=(-S ',')
  264. else
  265. seq=
  266. sep=()
  267. fi
  268. if compset -P1 '*/*'
  269. then
  270. packages=( $(_call_program packages yay -Pc ${words[CURRENT]%/*}) )
  271. typeset -U packages
  272. ${seq} _wanted repo_packages expl "repository/package" compadd ${sep[@]} ${(@)packages}
  273. else
  274. packages=( $(_call_program packages yay -Pc ) )
  275. typeset -U packages
  276. ${seq} _wanted packages expl "packages" compadd ${sep[@]} - "${(@)packages}"
  277. repositories=(${(o)${${${(M)${(f)"$(</etc/pacman.conf)"}:#\[*}/\[/}/\]/}:#options})
  278. typeset -U repositories
  279. _wanted repo_packages expl "repository/package" compadd -S "/" $repositories
  280. fi
  281. }
  282. # provides completions for package groups
  283. _pacman_completions_installed_groups() {
  284. local -a cmd groups
  285. _pacman_get_command
  286. groups=(${(o)${(f)"$(_call_program groups $cmd[@] -Qg)"}% *})
  287. typeset -U groups
  288. compadd "$@" -a groups
  289. }
  290. # provides completions for installed packages
  291. _pacman_completions_installed_packages() {
  292. local -a cmd packages packages_long
  293. packages_long=(/var/lib/pacman/local/*(/))
  294. packages=( ${${packages_long#/var/lib/pacman/local/}%-*-*} )
  295. compadd "$@" -a packages
  296. }
  297. _pacman_all_packages() {
  298. _alternative : 'localpkgs:local packages:_pacman_completions_installed_packages' 'repopkgs:repository packages:_pacman_completions_all_packages'
  299. }
  300. # provides completions for repository names
  301. _pacman_completions_repositories() {
  302. local -a cmd repositories
  303. repositories=(${(o)${${${(M)${(f)"$(</etc/pacman.conf)"}:#\[*}/\[/}/\]/}:#options})
  304. # Uniq the array
  305. typeset -U repositories
  306. compadd "$@" -a repositories
  307. }
  308. # builds command for invoking pacman in a _call_program command - extracts
  309. # relevant options already specified (config file, etc)
  310. # $cmd must be declared by calling function
  311. _pacman_get_command() {
  312. # this is mostly nicked from _perforce
  313. cmd=( "pacman" "2>/dev/null")
  314. integer i
  315. for (( i = 2; i < CURRENT - 1; i++ ))
  316. ; do
  317. if [[ ${words[i]} = "--config" |
  318. | ${words[i]} = "--root" ]]
  319. then
  320. cmd+=( ${words[i,i+1]} )
  321. fi
  322. done
  323. }
  324. # main dispatcher
  325. _pacman_zsh_comp() {
  326. local -a args cmds
  327. local tmp
  328. args=( ${${${(M)words:#-*}#-}:#-*} )
  329. for tmp in $words
  330. do
  331. cmds+=("${${_pacman_opts_commands[(r)*$tmp\[*]%%\[*}#*\)}")
  332. done
  333. case $args in #$words[2] in
  334. h*)
  335. if (( ${(c)#args} <= 1 && ${(w)#cmds} <= 1 ))
  336. then
  337. _pacman_action_help
  338. else
  339. _message "no more arguments"
  340. fi
  341. *h*)
  342. _message "no more arguments"
  343. D*)
  344. _pacman_action_database
  345. F*)
  346. _pacman_action_files
  347. Q*g*) # ipkg groups
  348. _arguments -s : "$_pacman_opts_common[@]" "$_pacman_opts_query_modifiers[@]" '*:groups:_pacman_completions_installed_groups'
  349. Q*o*) # file
  350. _arguments -s : "$_pacman_opts_common[@]" "$_pacman_opts_query_modifiers[@]" '*:package file:_files'
  351. Q*p*) # file *.pkg.tar*
  352. _arguments -s : "$_pacman_opts_common[@]" "$_pacman_opts_query_modifiers[@]" '*:package file:_files -g "*.pkg.tar*~*.sig(.,@)"'
  353. T*)
  354. _pacman_action_deptest
  355. Q*)
  356. _pacman_action_query
  357. P*)
  358. _arguments -s : "$_pacman_opts_print_modifiers[@]"
  359. R*)
  360. _pacman_action_remove
  361. S*c*) # no completion
  362. _arguments -s : '(-c --clean)'{\*-c,\*--clean}'[Remove all files from the cache]' "$_pacman_opts_common[@]"
  363. S*l*) # repos
  364. _arguments -s : "$_pacman_opts_common[@]" "$_pacman_opts_sync_modifiers[@]" '*:package repo:_pacman_completions_repositories'
  365. S*g*) # pkg groups
  366. _arguments -s : "$_pacman_opts_common[@]" "$_pacman_opts_sync_modifiers[@]" '*:package group:_pacman_completions_all_groups'
  367. S*s*)
  368. _arguments -s : "$_pacman_opts_common[@]" "$_pacman_opts_sync_modifiers[@]" '*:search text: '
  369. S*)
  370. _pacman_action_sync
  371. T*)
  372. _arguments -s : '-T' "$_pacman_opts_common[@]" ":packages:_pacman_all_packages"
  373. U*)
  374. _pacman_action_upgrade
  375. V*)
  376. _pacman_action_version
  377. Y*)
  378. _arguments -s : "$_pacman_opts_yay_modifiers[@]"
  379. *)
  380. case ${(M)words:#--*} in
  381. *--help*)
  382. if (( ${(w)#cmds} == 1 ))
  383. then
  384. _pacman_action_help
  385. else
  386. return 0
  387. fi
  388. *--sync*)
  389. _pacman_action_sync
  390. *--query*)
  391. _pacman_action_query
  392. *--remove*)
  393. _pacman_action_remove
  394. *--deptest*)
  395. _pacman_action_deptest
  396. *--database*)
  397. _pacman_action_database
  398. *--files*)
  399. _pacman_action_files
  400. *--version*)
  401. _pacman_action_version
  402. *--upgrade*)
  403. _pacman_action_upgrade
  404. *)
  405. _pacman_action_none
  406. esac
  407. esac
  408. }
  409. _pacman_comp() {
  410. case "$service" in
  411. yay)
  412. _pacman_zsh_comp "$@"
  413. *)
  414. _message "Error"
  415. esac
  416. }
  417. _pacman_comp "$@"