zsh-completion 17 KB

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