zsh 18 KB

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