zsh-completion 17 KB

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