zsh-completion 15 KB

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