summaryrefslogtreecommitdiff
path: root/contrib/bash_completion
blob: 9c378d63b0e90a115ac7d930093f393d9a2b0479 (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# vim: set ft=sh:
# file: /etc/bash_completion.d/pacman

# Bash completion for ArchLinux
# Original: v.1.1 Manolis Tzanidakis <mtzanidakis@freemail.gr>
#
# Distributed under the terms of the GNU General Public License, v2 or later.
#

## Changelog ####################################################
#                                                               #
# * 1.1 (20040117)                                              #
#   - Code cleanup                                              #
#   - Updated to pacman 2.7.2-2                                 #
# * 20040216 (orelien)                                          #
#   - Improved available_{pkgs,groups) functions                #
#   - Added support for querying groups                         #
#                                                               #
#################################################################

## initial functions
#

rem_selected ()
{
  # (Adapted from bash_completion by Ian Macdonald <ian@caliban.org>)
  # This removes any options from the list of completions that have
  # already been specified on the command line.
  COMPREPLY=($(echo "${COMP_WORDS[@]}" | \
    (while read -d ' ' i; do
      [ "${i}" == "" ] && continue
      # flatten array with spaces on either side,
      # otherwise we cannot grep on word boundaries of
      # first and last word
      COMPREPLY=" ${COMPREPLY[@]} "
      # remove word from list of completions
      COMPREPLY=(${COMPREPLY/ ${i%% *} / })
    done
  echo ${COMPREPLY[@]})))
  return 0
}

_available_repos ()
{
  COMPREPLY=( $( compgen -W "$(grep '\[' /etc/pacman.conf | grep -v -e 'options' -e '^#' | tr -d '[]' )" -- $cur ) )
}

_installed_pkgs ()
{
  local installed_pkgs
  installed_pkgs=$( ls /var/lib/pacman/local/ )
  COMPREPLY=( $( compgen -W "$( for i in $installed_pkgs; do echo ${i%-*-*}; done )" -- $cur ) )
}

_available_pkgs ()
{
  #find balks easilly on a find /foo/*/* type dir, especially one like
  #   /var/lib/pacman/*/*
  # This little change-up removes the find *and* only uses enabled repos
  local available_pkgs
  local enabled_repos
  enabled_repos=$( grep '\[' /etc/pacman.conf | grep -v -e 'options' -e '^#' | tr -d '[]' )
  available_pkgs=$( for r in $enabled_repos; do echo /var/lib/pacman/$r/*; done )
  COMPREPLY=( $( compgen -W "$( for i in $available_pkgs; do j=${i##*/}; echo ${j%-*-*}; done )" -- $cur ) )
}

_installed_groups ()
{
  local installed_groups
  installed_groups=$( find /var/lib/pacman/local -name desc -exec sed -ne '/%GROUPS%/,/^$/{//d; p}' {} \; | sort -u )
  COMPREPLY=( $( compgen -W "$( for i in $installed_groups; do echo ${i%-*-*}; done )" -- $cur ) )
}

_available_groups ()
{
  #find balks easilly on a find /foo/*/* type dir, especially one like
  #   /var/lib/pacman/*/*
  # This little change-up removes the find *and* only uses enabled repos
  local available_groups
  local enabled_repos
  enabled_repos=$( grep '\[' /etc/pacman.conf | grep -v -e 'options' -e '^#' | tr -d '[]' )
  available_groups=$( for r in $enabled_repos; do sed '/%GROUPS%/,/^$/{//d; p}' /var/lib/pacman/$r/*/desc | sort -u; done )
  COMPREPLY=( $( compgen -W "$( for i in $available_groups; do echo ${i%-*-*}; done )" -- $cur ) )
}

## init (/etc/rc.d) script completion (quick 'n' dirty ;-)
#

complete -W "start stop restart" \
  $(for i in /etc/rc.d/*; do echo ${i##*/}; done)

## makepkg completion
#

_makepkg ()
{
  local cur prev
  COMPREPLY=()
  cur=${COMP_WORDS[COMP_CWORD]}
  prev=${COMP_WORDS[COMP_CWORD-1]}

  case "$prev" in
    -@(p|w))
      _filedir
      return 0
    ;;
    --help|--cleancache|--genmd5)
      COMPREPLY=''
      return 0
    ;;
  esac

  if [[ "$cur" == -* ]]; then
    COMPREPLY=( $( compgen -W '-b --builddeps \
      -c --clean \
      -C --cleancache \
      -d --nodeps \
      -f --force \
      -g --genmd5 \
      -h --help \
      -i --install \
      -m --nocolor \
      -n --nostrip \
      -r --rmdeps \
      -s --syncdeps \
      -p -w' -- $cur ) )
  fi

  rem_selected
}
complete -o default -F _makepkg makepkg

## pacman completion
#

_instring ()
{
    str="${1}"
    shift 1
    for c in "${@}"; do
        if [ $(expr index "${str}" "${c}") -gt 0 ]; then
            return 0
        fi
    done
    return 1
}

_pacman ()
{
  local a arg toparse op mod cur
  COMPREPLY=()

  # This argument parsing is done so we can check for flag existance later
  #  right now it's a tad crappy, but does the job
  for (( i=1; i < ${#COMP_WORDS[@]}-1; i++ )); do
        a=${COMP_WORDS[i]}
    arg="${a:0:2}"
    toparse="${a:2}"

    case "${arg}" in
      -@(A|U|F|R|S|Q|h|V))
        op="${arg/-}"
        mod="${mod}${a:2}"
        ;;
      --)
        arg="${a:2}"
        case "${arg}" in
          add) op="A" ;;
          remove) op="R" ;;
          upgrade) op="U" ;;
          freshen) op="F" ;;
          query) op="Q" ;;
          sync) op="S" ;;
          help) op="h" ;;
          version) op="V" ;;
          verbose) mod="${mod}v" ;;
          root) mod="${mod}r" ;;
          dbpath) mod="${mod}b" ;;
          nodeps) mod="${mod}d" ;;
          force) mod="${mod}f" ;;
          groups) mod="${mod}g" ;;
          info) mod="${mod}i" ;;
          list) mod="${mod}l" ;;
          print-uris) mod="${mod}p" ;;
          search) mod="${mod}s" ;;
          sysupgrade) mod="${mod}u" ;;
          downloadonly) mod="${mod}w" ;;
          refresh) mod="${mod}y" ;;
          orphans) mod="${mod}e" ;;
          foreign) mod="${mod}m" ;;
          owns) mod="${mod}o" ;;
          file) mod="${mod}p" ;;
          search) mod="${mod}s" ;;
          cascade) mod="${mod}c" ;;
          nodeps) mod="${mod}d" ;;
          dbonly) mod="${mod}k" ;;
          nosave) mod="${mod}n" ;;
          recursive) mod="${mod}s" ;;
        esac ;;
      *) toparse="${a}" ;;
    esac

    arglen=$(( ${#toparse}-1 ))
    for c in $(seq 0 "${arglen}"); do
      arg=${toparse:$c:1}
      [ "${arg}" != "-" ] && mod="${mod}${arg}"
    done
  done

  cur=${COMP_WORDS[COMP_CWORD]}

  if [ $COMP_CWORD -eq 1 ] && [[ "$cur" == -* ]]; then
        COMPREPLY=( $( compgen -W '\
            -A --add \
            -F --freshen \
            -h --help \
            -Q --query \
            -R --remove \
            -S --sync \
            -U --upgrade \
            -V --version \
            ' -- $cur ) )
        rem_selected
        return 0
  fi

  if [[ "$cur" == -* ]]; then
    case "${op}" in
      A|U|F)
        COMPREPLY=( $( compgen -W '\
           -d --nodeps \
           -f --force \
           -h --help \
           --config \
           --noconfirm \
           --noprogressbar \
           -v --verbose \
           -r --root \
           -b --dbpath \
           ' -- $cur ) )
        return 0
      ;;
      R)
        COMPREPLY=( $( compgen -W '\
          -c --cascade \
          -d --nodeps \
          -f --force \
          -h --help \
          -k --dbonly \
          -n --nosave \
          -s --recursive \
           --config \
           --noconfirm \
           --noprogressbar \
          -v --verbose \
          -r --root \
          -b --dbpath \
          ' -- $cur ) )
        return 0
      ;;
      S)
        COMPREPLY=( $( compgen -W '\
          -c --clean \
          -d --nodeps \
          -f --force \
          -g --groups \
          -h --help \
          -i --info \
          -l --list \
          -p --print-uris \
          -s --search \
          -u --sysupgrade \
          -w --downloadonly \
          -y --refresh \
           --config \
           --noconfirm \
           --noprogressbar \
          -v --verbose \
          -r --root \
          -b --dbpath \
          ' -- $cur ) )
        return 0
      ;;
      Q)
        COMPREPLY=( $( compgen -W '\
          -e --orphans \
          -g --groups \
          -h --help \
          -i --info \
          -l --list \
          -m --foreign \
          -o --owns \
          -p --file \
          -s --search \
           --config \
           --noconfirm \
           --noprogressbar \
          -v --verbose \
          -r --root \
          -b --dbpath \
          ' -- $cur ) )
        return 0
      ;;
    esac
    rem_selected
  else
    case "${op}" in
      A|U)
                COMPREPLY=( $( compgen -d -- "$cur" ) \
                    $( compgen -f -X '!*.pkg.tar.gz' -- "$cur" ) )
        return 0
      ;;
      h|V)
        COMPREPLY=''
        return 0
      ;;
      Q)
        if _instring $mod g; then
            _installed_groups
        elif _instring $mod o; then
                        COMPREPLY=( $( compgen -d -- "$cur" ) \
                        $( compgen -f -- "$cur" ) )
        elif _instring $mod p; then
                        COMPREPLY=( $( compgen -d -- "$cur" ) \
                        $( compgen -f -X '!*.pkg.tar.gz' -- "$cur" ) )
        else
            _installed_pkgs
        fi
        return 0
      ;;
      R)
        _installed_pkgs
        return 0
      ;;
      S)
        if _instring $mod l; then
            _available_repos
        else
            _available_pkgs
        fi
        return 0
      ;;
    esac
  fi

  rem_selected
}
complete -o filenames -F _pacman pacman