Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jul 25th, 2011  |  syntax: None  |  size: 4.42 KB  |  views: 98  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #compdef packer packer.static=packer
  2.  
  3. # copy this file to /usr/share/zsh/site-functions/_packer
  4.  
  5. typeset -A opt_args
  6.  
  7. # options for passing to _arguments: main packer commands
  8. _packer_opts_commands=(
  9.         '-Ss[Search for a package.]:*:search text:->sync_search'
  10.         '-S[Install a package.]'
  11.         '-Syu[Sync with repositories.]'
  12.         '-Si[Show information for a package.]'
  13.     '-G[Just download and extract AUR tarball]'
  14.         '-h[Show packer usage.]'
  15. )
  16.  
  17. # options for passing to _arguments: options common to all commands
  18. _packer_opts_common=(
  19.         '--ignore[Ignore packages listed here.]:package:_packer_completions_all_packages'
  20.         '--noconfirm[Do not ask user for confirmation.]'
  21.         '--noedit[Do not ask user if he wants to edit files.]'
  22.         '--auronly[Only perform commands for the AUR.]'
  23.         '--devel[Update development packages. (cvs, git, ...)]'
  24.         '--skipinteg[Skip the integrity checks.]'
  25. )
  26.  
  27. # handles cases where no subcommand has yet been given
  28. _packer_action_none() {
  29.     _arguments -s : \
  30.         "$_packer_opts_commands[@]"
  31. }
  32.  
  33. # handles -h subcommand
  34. _packer_action_help() {
  35.     _arguments -s : \
  36.         "$_packer_opts_commands[@]"
  37. }
  38.  
  39. # handles --sync subcommand
  40. _packer_action_sync() {
  41.     local context state line
  42.     typeset -A opt_args
  43.    
  44. #   _arguments -s : \
  45. #       "$_packer_opts_common[@]" \
  46. #       "$_packer_opts_sync_actions[@]" #\
  47. #       #"$_packer_opts_sync_modifiers[@]"
  48.        
  49.     case $state in
  50.         sync_search)
  51.             _arguments -s : \
  52.                 "$_packer_opts_common[@]" \
  53.                 '*:search text: '
  54.                 ;;
  55.         *)
  56.             _arguments -s : \
  57.                 "$_packer_opts_common[@]" \
  58.                 '*:package:_packer_completions_all_packages'
  59.                 ;;
  60.         esac
  61. }
  62.  
  63. # provides completions for package groups
  64. _packer_completions_all_groups() {
  65.     local -a cmd groups
  66.     _packer_get_command
  67.     groups=( $(_call_program groups $cmd[@] -Sg) )
  68.     typeset -U groups
  69.     compadd "$@" -a groups
  70. }
  71.  
  72. # provides completions for packages available from repositories
  73. # these can be specified as either 'package' or 'repository/package'
  74. _packer_completions_all_packages() {
  75.     local -a cmd packages repositories packages_long
  76.     _packer_get_command
  77.  
  78.     if compset -P1 '*/*'; then
  79.         packages=( $(_call_program packages $cmd[@] -Sql ${words[CURRENT]%/*}) )
  80.         typeset -U packages
  81.         _wanted repo_packages expl "repository/package" compadd ${(@)packages}
  82.     else
  83.         packages=( $(_call_program packages $cmd[@] -Sql) )
  84.         typeset -U packages
  85.         _wanted packages expl "packages" compadd - "${(@)packages}"
  86.  
  87.         repositories=(${(o)${${${(M)${(f)"$(</etc/pacman.conf)"}:#\[*}/\[/}/\]/}:#options})
  88.         typeset -U repositories
  89.         _wanted repo_packages expl "repository/package" compadd -S "/" $repositories
  90.     fi
  91. }
  92.  
  93. # provides completions for package groups
  94. _packer_completions_installed_groups() {
  95.     local -a cmd groups
  96.     _packer_get_command
  97.     groups=(${(o)${(f)"$(_call_program groups $cmd[@] -Qg)"}% *})
  98.     typeset -U groups
  99.     compadd "$@" -a groups
  100. }
  101.  
  102. # provides completions for installed packages
  103. _packer_completions_installed_packages() {
  104.     local -a cmd packages packages_long
  105.     packages_long=(/var/lib/pacman/local/*(/))
  106.     packages=( ${${packages_long#/var/lib/pacman/local/}%-*-*} )
  107.     compadd "$@" -a packages
  108. }
  109.  
  110. # provides completions for repository names
  111. _packer_completions_repositories() {
  112.     local -a cmd repositories
  113.     repositories=(${(o)${${${(M)${(f)"$(</etc/pacman.conf)"}:#\[*}/\[/}/\]/}:#options})
  114.     # Uniq the array
  115.     typeset -U repositories
  116.     compadd "$@" -a repositories
  117. }
  118.  
  119. # builds command for invoking pacman in a _call_program command - extracts
  120. # relevant options already specified (config file, etc)
  121. # $cmd must be declared by calling function
  122. _packer_get_command() {
  123.     # this is mostly nicked from _perforce
  124.     cmd=( "pacman" )
  125.     integer i
  126.     for (( i = 2; i < CURRENT - 1; i++ )); do
  127.         if [[ ${words[i]} = "--config" || ${words[i]} = "--root" ]]; then
  128.             cmd+=( ${words[i,i+1]} )
  129.         fi
  130.     done
  131. }
  132.  
  133. # main dispatcher
  134. _packer() {
  135.     case $words[2] in
  136.         -S*)  _packer_action_sync     ;;
  137.         -G*)  _packer_action_sync     ;;
  138.         -h*)  _packer_action_help     ;;
  139.         -  )  _packer_action_none     ;;
  140.         *  )  return 1                ;;
  141.     esac
  142. }
  143.  
  144. # run the main dispatcher
  145. _packer "$@"