zsh 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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 backgroud 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. )
  125. # options for passing to _arguments: options for --remove command
  126. _pacman_opts_remove=(
  127. {-c,--cascade}'[Remove all dependent packages]'
  128. {-d,--nodeps}'[Skip dependency checks]'
  129. '*--assume-installed[Add virtual package to satisfy dependencies]'
  130. {-n,--nosave}'[Remove protected configuration files]'
  131. {-p,--print}'[Only print the targets instead of performing the operation]'
  132. {\*-s,\*--recursive}'[Remove dependencies not required by other packages]'
  133. {-u,--unneeded}'[Remove unneeded packages]'
  134. '--dbonly[Only remove database entry, do not remove files]'
  135. '--print-format[Specify how the targets should be printed]'
  136. '*:installed package:_pacman_completions_installed_packages'
  137. )
  138. _pacman_opts_database=(
  139. '--asdeps[mark packages as non-explicitly installed]'
  140. '--asexplicit[mark packages as explicitly installed]'
  141. '*:installed package:_pacman_completions_installed_packages'
  142. )
  143. _pacman_opts_files=(
  144. {-l,--list}'[List the files owned by the queried package]:package:_pacman_completions_all_packages'
  145. {-o,--owns}'[Query the package that owns]:files:_files'
  146. {-s,--search}'[Search package file names for matching strings]:files:_files'
  147. {-x,--regex}'[Enable searching using regluar expressions]:regex:'
  148. {-y,--refresh}'[Download fresh files databases from the server]'
  149. '--machinereadable[Produce machine-readable output]'
  150. {-q,--quiet}'[Show less information for query and search]'
  151. )
  152. # options for passing to _arguments: options for --sync command
  153. _pacman_opts_sync_actions=(
  154. '(-S --sync)'{-S,--sync}
  155. {\*-c,\*--clean}'[Remove old packages from cache]:\*:clean:->sync_clean'
  156. {-g,--groups}'[View all members of a package group]:*:package groups:->sync_group'
  157. {-s,--search}'[Search package names and descriptions]:*:search text:->sync_search'
  158. '--dbonly[Only remove database entry, do not remove files]'
  159. '--needed[Do not reinstall up to date packages]'
  160. '--recursive[Reinstall all dependencies of target packages]'
  161. )
  162. # options for passing to _arguments: options for --sync command
  163. _pacman_opts_sync_modifiers=(
  164. {\*-d,\*--nodeps}'[Skip dependency checks]'
  165. '*--assume-installed[Add virtual package to satisfy dependencies]'
  166. {\*-i,\*--info}'[View package information]'
  167. {-l,--list}'[List all packages in a repository]'
  168. {-p,--print}'[Print download URIs for each package to be installed]'
  169. {-q,--quiet}'[Show less information for query and search]'
  170. {\*-u,\*--sysupgrade}'[Upgrade all out-of-date packages]'
  171. {-w,--downloadonly}'[Download packages only]'
  172. {\*-y,\*--refresh}'[Download fresh package databases]'
  173. '*--ignore[Ignore a package upgrade]:package: _pacman_completions_all_packages'
  174. '*--ignoregroup[Ignore a group upgrade]:package group:_pacman_completions_all_groups'
  175. '--asdeps[Install packages as non-explicitly installed]'
  176. '--asexplicit[Install packages as explicitly installed]'
  177. '--force[Overwrite conflicting files]'
  178. '--print-format[Specify how the targets should be printed]'
  179. )
  180. # handles --help subcommand
  181. _pacman_action_help() {
  182. _arguments -s : \
  183. "$_pacman_opts_commands[@]"
  184. }
  185. # handles cases where no subcommand has yet been given
  186. _pacman_action_none() {
  187. _arguments -s : \
  188. "$_pacman_opts_commands[@]"
  189. }
  190. # handles --query subcommand
  191. _pacman_action_query() {
  192. local context state line
  193. typeset -A opt_args
  194. case $state in
  195. query_file)
  196. _arguments -s : \
  197. "$_pacman_opts_common[@]" \
  198. "$_pacman_opts_query_modifiers[@]" \
  199. '*:package file:_files -g "*.pkg.tar*~*.sig(.,@)"'
  200. ;;
  201. query_group)
  202. _arguments -s : \
  203. "$_pacman_opts_common[@]" \
  204. "$_pacman_opts_query_modifiers[@]" \
  205. '*:groups:_pacman_completions_installed_groups'
  206. ;;
  207. query_owner)
  208. _arguments -s : \
  209. "$_pacman_opts_common[@]" \
  210. "$_pacman_opts_query_modifiers[@]" \
  211. '*:file:_files'
  212. ;;
  213. query_search)
  214. _arguments -s : \
  215. "$_pacman_opts_common[@]" \
  216. "$_pacman_opts_query_modifiers[@]" \
  217. '*:search text: '
  218. ;;
  219. *)
  220. _arguments -s : \
  221. "$_pacman_opts_common[@]" \
  222. "$_pacman_opts_query_actions[@]" \
  223. "$_pacman_opts_query_modifiers[@]" \
  224. '*:package:_pacman_completions_installed_packages'
  225. ;;
  226. esac
  227. }
  228. # handles --remove subcommand
  229. _pacman_action_remove() {
  230. _arguments -s : \
  231. '(--remove -R)'{-R,--remove} \
  232. "$_pacman_opts_common[@]" \
  233. "$_pacman_opts_remove[@]"
  234. }
  235. # handles --database subcommand
  236. _pacman_action_database() {
  237. _arguments -s : \
  238. '(--database -D)'{-D,--database} \
  239. "$_pacman_opts_common[@]" \
  240. "$_pacman_opts_database[@]"
  241. }
  242. # handles --files subcommand
  243. _pacman_action_files() {
  244. _arguments -s : \
  245. '(--files -F)'{-F,--files} \
  246. "$_pacman_opts_common[@]" \
  247. "$_pacman_opts_files[@]"
  248. }
  249. _pacman_action_deptest () {
  250. _arguments -s : \
  251. '(--deptest)-T' \
  252. "$_pacman_opts_common[@]" \
  253. ":packages:_pacman_all_packages"
  254. }
  255. # handles --sync subcommand
  256. _pacman_action_sync() {
  257. local context state line
  258. typeset -A opt_args
  259. if (( $+words[(r)--clean] )); then
  260. state=sync_clean
  261. elif (( $+words[(r)--groups] )); then
  262. state=sync_group
  263. elif (( $+words[(r)--search] )); then
  264. state=sync_search
  265. fi
  266. case $state in
  267. sync_clean)
  268. _arguments -s : \
  269. {\*-c,\*--clean}'[Remove old packages from cache]' \
  270. "$_pacman_opts_common[@]" \
  271. "$_pacman_opts_sync_modifiers[@]"
  272. ;;
  273. sync_group)
  274. _arguments -s : \
  275. "$_pacman_opts_common[@]" \
  276. "$_pacman_opts_sync_modifiers[@]" \
  277. '(-g --group)'{-g,--groups} \
  278. '*:package group:_pacman_completions_all_groups'
  279. ;;
  280. sync_search)
  281. _arguments -s : \
  282. "$_pacman_opts_common[@]" \
  283. "$_pacman_opts_sync_modifiers[@]" \
  284. '*:search text: '
  285. ;;
  286. *)
  287. _arguments -s : \
  288. "$_pacman_opts_common[@]" \
  289. "$_pacman_opts_sync_actions[@]" \
  290. "$_pacman_opts_sync_modifiers[@]" \
  291. '*:package:_pacman_completions_all_packages'
  292. ;;
  293. esac
  294. }
  295. # handles --upgrade subcommand
  296. _pacman_action_upgrade() {
  297. _arguments -s : \
  298. '(-U --upgrade)'{-U,--upgrade} \
  299. "$_pacman_opts_common[@]" \
  300. "$_pacman_opts_pkgfile[@]"
  301. }
  302. # handles --version subcommand
  303. _pacman_action_version() {
  304. # no further arguments
  305. return 0
  306. }
  307. # provides completions for package groups
  308. _pacman_completions_all_groups() {
  309. local -a cmd groups
  310. _pacman_get_command
  311. groups=( $(_call_program groups $cmd[@] -Sg) )
  312. typeset -U groups
  313. if [[ ${words[CURRENT-1]} == '--ignoregroup' ]]; then
  314. _sequence compadd -S ',' "$@" -a groups
  315. else
  316. compadd "$@" -a groups
  317. fi
  318. }
  319. # provides completions for packages available from repositories
  320. # these can be specified as either 'package' or 'repository/package'
  321. _pacman_completions_all_packages() {
  322. local -a seq sep cmd packages repositories packages_long
  323. if [[ ${words[CURRENT-1]} == '--ignore' ]]; then
  324. seq='_sequence'
  325. sep=(-S ',')
  326. else
  327. seq=
  328. sep=()
  329. fi
  330. if compset -P1 '*/*'; then
  331. packages=( $(_call_program packages yay -Pc ${words[CURRENT]%/*}) )
  332. typeset -U packages
  333. ${seq} _wanted repo_packages expl "repository/package" compadd ${sep[@]} ${(@)packages}
  334. else
  335. packages=( $(_call_program packages yay -Pc ) )
  336. typeset -U packages
  337. ${seq} _wanted packages expl "packages" compadd ${sep[@]} - "${(@)packages}"
  338. repositories=(${(o)${${${(M)${(f)"$(</etc/pacman.conf)"}:#\[*}/\[/}/\]/}:#options})
  339. typeset -U repositories
  340. _wanted repo_packages expl "repository/package" compadd -S "/" $repositories
  341. fi
  342. }
  343. # provides completions for package groups
  344. _pacman_completions_installed_groups() {
  345. local -a cmd groups
  346. _pacman_get_command
  347. groups=(${(o)${(f)"$(_call_program groups $cmd[@] -Qg)"}% *})
  348. typeset -U groups
  349. compadd "$@" -a groups
  350. }
  351. # provides completions for installed packages
  352. _pacman_completions_installed_packages() {
  353. local -a cmd packages packages_long
  354. packages_long=(/var/lib/pacman/local/*(/))
  355. packages=( ${${packages_long#/var/lib/pacman/local/}%-*-*} )
  356. compadd "$@" -a packages
  357. }
  358. _pacman_all_packages() {
  359. _alternative : \
  360. 'localpkgs:local packages:_pacman_completions_installed_packages' \
  361. 'repopkgs:repository packages:_pacman_completions_all_packages'
  362. }
  363. # provides completions for repository names
  364. _pacman_completions_repositories() {
  365. local -a cmd repositories
  366. repositories=(${(o)${${${(M)${(f)"$(</etc/pacman.conf)"}:#\[*}/\[/}/\]/}:#options})
  367. # Uniq the array
  368. typeset -U repositories
  369. compadd "$@" -a repositories
  370. }
  371. # builds command for invoking pacman in a _call_program command - extracts
  372. # relevant options already specified (config file, etc)
  373. # $cmd must be declared by calling function
  374. _pacman_get_command() {
  375. # this is mostly nicked from _perforce
  376. cmd=( "pacman" "2>/dev/null")
  377. integer i
  378. for (( i = 2; i < CURRENT - 1; i++ )); do
  379. if [[ ${words[i]} = "--config" || ${words[i]} = "--root" ]]; then
  380. cmd+=( ${words[i,i+1]} )
  381. fi
  382. done
  383. }
  384. # main dispatcher
  385. _pacman_zsh_comp() {
  386. local -a args cmds;
  387. local tmp
  388. args=( ${${${(M)words:#-*}#-}:#-*} )
  389. for tmp in $words; do
  390. cmds+=("${${_pacman_opts_commands[(r)*$tmp\[*]%%\[*}#*\)}")
  391. done
  392. case $args in #$words[2] in
  393. h*)
  394. if (( ${(c)#args} <= 1 && ${(w)#cmds} <= 1 )); then
  395. _pacman_action_help
  396. else
  397. _message "no more arguments"
  398. fi
  399. ;;
  400. *h*)
  401. _message "no more arguments"
  402. ;;
  403. D*)
  404. _pacman_action_database
  405. ;;
  406. F*)
  407. _pacman_action_files
  408. ;;
  409. Q*g*) # ipkg groups
  410. _arguments -s : \
  411. "$_pacman_opts_common[@]" \
  412. "$_pacman_opts_query_modifiers[@]" \
  413. '*:groups:_pacman_completions_installed_groups'
  414. ;;
  415. Q*o*) # file
  416. _arguments -s : \
  417. "$_pacman_opts_common[@]" \
  418. "$_pacman_opts_query_modifiers[@]" \
  419. '*:package file:_files'
  420. ;;
  421. Q*p*) # file *.pkg.tar*
  422. _arguments -s : \
  423. "$_pacman_opts_common[@]" \
  424. "$_pacman_opts_query_modifiers[@]" \
  425. '*:package file:_files -g "*.pkg.tar*~*.sig(.,@)"'
  426. ;;
  427. T*)
  428. _pacman_action_deptest
  429. ;;
  430. Q*)
  431. _pacman_action_query
  432. ;;
  433. P*)
  434. _arguments -s : \
  435. "$_pacman_opts_print_modifiers[@]"
  436. ;;
  437. R*)
  438. _pacman_action_remove
  439. ;;
  440. S*c*) # no completion
  441. _arguments -s : \
  442. '(-c --clean)'{\*-c,\*--clean}'[Remove all files from the cache]' \
  443. "$_pacman_opts_common[@]"
  444. ;;
  445. S*l*) # repos
  446. _arguments -s : \
  447. "$_pacman_opts_common[@]" \
  448. "$_pacman_opts_sync_modifiers[@]" \
  449. '*:package repo:_pacman_completions_repositories' \
  450. ;;
  451. S*g*) # pkg groups
  452. _arguments -s : \
  453. "$_pacman_opts_common[@]" \
  454. "$_pacman_opts_sync_modifiers[@]" \
  455. '*:package group:_pacman_completions_all_groups'
  456. ;;
  457. S*s*)
  458. _arguments -s : \
  459. "$_pacman_opts_common[@]" \
  460. "$_pacman_opts_sync_modifiers[@]" \
  461. '*:search text: '
  462. ;;
  463. S*)
  464. _pacman_action_sync
  465. ;;
  466. T*)
  467. _arguments -s : \
  468. '-T' \
  469. "$_pacman_opts_common[@]" \
  470. ":packages:_pacman_all_packages"
  471. ;;
  472. U*)
  473. _pacman_action_upgrade
  474. ;;
  475. V*)
  476. _pacman_action_version
  477. ;;
  478. Y*)
  479. _arguments -s : \
  480. "$_pacman_opts_yay_modifiers[@]"
  481. ;;
  482. *)
  483. case ${(M)words:#--*} in
  484. *--help*)
  485. if (( ${(w)#cmds} == 1 )); then
  486. _pacman_action_help
  487. else
  488. return 0;
  489. fi
  490. ;;
  491. *--sync*)
  492. _pacman_action_sync
  493. ;;
  494. *--query*)
  495. _pacman_action_query
  496. ;;
  497. *--remove*)
  498. _pacman_action_remove
  499. ;;
  500. *--deptest*)
  501. _pacman_action_deptest
  502. ;;
  503. *--database*)
  504. _pacman_action_database
  505. ;;
  506. *--files*)
  507. _pacman_action_files
  508. ;;
  509. *--version*)
  510. _pacman_action_version
  511. ;;
  512. *--upgrade*)
  513. _pacman_action_upgrade
  514. ;;
  515. *)
  516. _pacman_action_none
  517. ;;
  518. esac
  519. ;;
  520. esac
  521. }
  522. _pacman_comp() {
  523. case "$service" in
  524. yay)
  525. _pacman_zsh_comp "$@"
  526. ;;
  527. *)
  528. _message "Error"
  529. ;;
  530. esac
  531. }
  532. _pacman_comp "$@"