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