bash_completion: Get rid of weird retry logic.
[paraslash.git] / bash_completion
1 # Copyright (C) 2007 Andre Noll <maan@tuebingen.mpg.de>
2 #
3 # Licensed under the GPL v2. For licencing details see COPYING.
4
5 _para_complete()
6 {
7 local prg="$1" # the program to execute
8 local cur=${COMP_WORDS[$COMP_CWORD]}
9 local line="$COMP_LINE" OLD_IFS="$IFS"
10 local opts n
11
12 # This extracts short and long options from the help output
13 local script='{
14 if ($1 ~ "-[a-zA-Z]," && $2 ~ "--[a-zA-Z]") {
15 print substr($1, 0, 2);
16 gsub("=.*", "", $2)
17 print $2
18 } else if ($1 ~ "--[a-zA-Z]") {
19 gsub("=.*", "", $1)
20 print $1
21 }
22 }'
23
24 if [[ "$cur" == -* ]]; then # option
25 # Depending on whether '--' is one of the previous words we
26 # complete either on local options, i.e. those of the program
27 # to execute, or call the program to print possible completions
28 # (to a subcommand).
29 local_opts=true
30 for ((i=0; i < $COMP_CWORD; i++)); do
31 [[ "${COMP_WORDS[$i]}" != '--' ]] && continue
32 local_opts=false
33 break
34 done
35 if [[ "$local_opts" == "true" ]]; then
36 result="-- $($prg --help | awk "$script")"
37 COMPREPLY=($(compgen -W "$result" -- $cur))
38 return
39 fi
40 fi
41 # We need to call the program with --complete to get the possible
42 # completions. Before that, all local options must be discarded.
43 IFS=' '
44 n=0
45 for word in $line; do
46 ((n > 0)) && ! [[ "$word" == -* ]] && break
47 line="${line##*( )}" # remove leading whitespace
48 line="${line##+([^ ])}"
49 line="${line##*( )}"
50 let n++
51 [[ "$word" == '--' ]] && break
52 done
53 IFS="$OLD_IFS"
54 s=$((${#COMP_LINE} - ${#line})) # how many characters have been cut
55 if (($COMP_POINT > $s)); then
56 COMP_POINT=$(($COMP_POINT - $s))
57 else
58 COMP_POINT=0
59 fi
60 COMP_LINE="$line"
61 #echo "line: $COMP_LINE, point: $COMP_POINT"
62 export COMP_LINE COMP_POINT
63 result=($($prg --complete))
64
65 # the last line of the output contains the options for compopt,
66 # prefixed with '-o='.
67 n=${#result[@]}
68 (($n == 0)) && return # oops, $prg did not write any output
69 let n--
70 opts="${result[$n]}"
71 result[$n]=
72 opts="${opts#-o=}"
73 IFS=','
74 compopt +o nospace
75 for opt in $opts; do
76 #echo "opt: $opt"
77 case "$opt" in
78 filenames) compopt -o filenames;;
79 nospace) compopt -o nospace;;
80 esac
81 done
82 IFS="$OLD_IFS"
83 COMPREPLY=(${result[@]})
84 }
85
86 _para_audioc()
87 {
88 _para_complete para_audioc
89 }
90 complete -F _para_audioc para_audioc
91
92 _para_client()
93 {
94 _para_complete para_client
95 }
96 complete -o default -o nospace -F _para_client para_client
97 complete -o default -o nospace -F _para_client para