zsh 18 KB

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