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