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