summaryrefslogtreecommitdiff
path: root/contrib/bash_completion.in
blob: 06963c4214e35ded19656f49356dea7638c98c73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# This file is in the public domain.

_arch_compgen() {
  local i r
  COMPREPLY=($(compgen -W '$*' -- "$cur"))
  for ((i=1; i < ${#COMP_WORDS[@]}-1; i++)); do
    for r in ${!COMPREPLY[@]}; do
      if [[ ${COMP_WORDS[i]} = ${COMPREPLY[r]} ]]; then
        unset 'COMPREPLY[r]'; break
      fi
    done
  done
}

_arch_ptr2comp() {
  local list= x y
  for x; do
    for y in '0 --' '1 -'; do
      eval 'set -- ${'$x'[${y% *}]}'
      list+=\ ${@/#/${y#* }}
    done
  done
  _arch_compgen $list
}

_arch_incomp() {
  local r="\s-(-${1#* }\s|\w*${1% *})"; [[ $COMP_LINE =~ $r ]]
}

_pacman_keyids() {
  \pacman-key --list-keys 2>/dev/null | awk '
    $1 == "pub" {
      # key id
      split($2, a, "/"); print a[2]
    }
    $1 == "uid" {
      # email
      if (match($NF, /<[^>]+>/))
        print substr($NF, RSTART + 1, RLENGTH - 2)
    }'
}

_pacman_key() {
  local o cur opts prev wantfiles
  COMPREPLY=()
  _get_comp_words_by_ref cur prev
  opts=('add config delete edit-key export finger gpgdir
         help import import-trustdb init keyserver list-keys list-sigs
         lsign-key nocolor populate recv-keys refresh-keys updatedb
         verify version'
        'a d e f h l r u v V')

  # operations for which we want to complete keyids
  for o in 'd delete' 'e export' 'f finger' 'l list-keys' 'r recv-keys' \
      'edit-key' 'list-sigs' 'lsign-key' 'refresh-keys'; do
    _arch_incomp "$o" && break
    unset o
  done

  # options for which we want file completion
  wantfiles='-@(c|-config|g|-gpgdir)'

  if [[ $prev = 'pacman-key' || ( $cur = -* && $prev != $wantfiles ) ]]; then
    _arch_ptr2comp opts
  elif [[ $prev = @(-k|--keyserver) ]]; then
    return
  elif [[ $prev != $wantfiles && $o ]]; then
    COMPREPLY=($(compgen -W '$(_pacman_keyids)' -- "$cur"))
  fi
  true
}

_makepkg() {
  local cur opts prev
  COMPREPLY=()
  _get_comp_words_by_ref cur prev
  if [[ $cur = -* && ! $prev =~ ^-(-(config|help|key|version)$|\w*[Vhp]) ]]; then
    opts=('allsource asdeps check clean cleanbuild config force geninteg help
           holdver ignorearch install key log needed noarchive nobuild nocheck
           nocolor noconfirm nodeps noextract noprepare noprogressbar nosign
           packagelist printsrcinfo repackage rmdeps sign skipchecksums
           skipinteg skippgpcheck source syncdeps verifysource version'
          'A C L R S c d e f g h i m o p r s')
    _arch_ptr2comp opts
  fi
  true
}

_pacman_pkg() {
  _arch_compgen "$(
    if [[ $2 ]]; then
      \pacman -$1 2>/dev/null | \cut -d' ' -f1 | \sort -u
    else
      \pacman -$1 2>/dev/null
    fi
  )"
}

_pacman() {
  local common core cur database files prev query remove sync upgrade o
  COMPREPLY=()
  _get_comp_words_by_ref cur prev
  database=('asdeps asexplicit')
  files=('list machinereadable owns search refresh regex' 'l o s x y')
  query=('changelog check deps explicit file foreign groups info list owns
          search unrequired upgrades' 'c e g i k l m o p s t u')
  remove=('cascade dbonly nodeps assume-installed nosave print recursive unneeded' 'c n p s u')
  sync=('asdeps asexplicit clean dbonly downloadonly force groups ignore ignoregroup
         info list needed nodeps assume-installed print refresh recursive search sysupgrade'
        'c g i l p s u w y')
  upgrade=('asdeps asexplicit force needed nodeps assume-installed print recursive' 'p')
  common=('arch cachedir color config confirm dbpath debug gpgdir help hookdir logfile
           noconfirm noprogressbar noscriptlet quiet root verbose' 'b d h q r v')
  core=('database files help query remove sync upgrade version' 'D F Q R S U V h')

  for o in 'D database' 'F files' 'Q query' 'R remove' 'S sync' 'U upgrade'; do
    _arch_incomp "$o" && break
  done

  if [[ $? != 0 ]]; then
    _arch_ptr2comp core
  elif [[ ! $prev =~ ^-\w*[Vbhr] &&
    ! $prev = --@(cachedir|color|config|dbpath|help|hookdir|gpgdir|logfile|root|version) ]]
  then
    [[ $cur = -* ]] && _arch_ptr2comp ${o#* } common ||
      case ${o% *} in
      D|R)
          _pacman_pkg Qq;;
      F)
          _arch_incomp 'l list'   && _pacman_pkg Slq;
          ;;
      Q)
        { _arch_incomp 'g groups' && _pacman_pkg Qg sort; }    ||
        { _arch_incomp 'p file'   && _pacman_file; }           ||
          _arch_incomp 'o owns'   || _arch_incomp 'u upgrades' ||
          _pacman_pkg Qq;;
      S)
        { _arch_incomp 'g groups' && _pacman_pkg Sg; }      ||
        { _arch_incomp 'l list'   && _pacman_pkg Sl sort; } ||
          _pacman_pkg Slq;;
      U)
          _pacman_file;;
      esac
  fi
  true
}

_pacman_file() {
  compopt -o filenames; _filedir 'pkg.tar*'
}

complete -F _pacman -o default pacman
complete -F _makepkg -o default makepkg
complete -F _pacman_key -o default pacman-key

# ex:et ts=2 sw=2 ft=sh