-_para()
+# Copyright (C) 2007 Andre Noll <maan@tuebingen.mpg.de>
+#
+# Licensed under the GPL v2. For licencing details see COPYING.
+
+_para_complete()
{
-local cur prev sect i manpath tmp
-COMPREPLY=()
-cur=${COMP_WORDS[COMP_CWORD]}
-prev=${COMP_WORDS[COMP_CWORD-1]}
+ local prg="$1" # the program to execute
+ local cur=${COMP_WORDS[$COMP_CWORD]}
+ local line="$COMP_LINE" OLD_IFS="$IFS"
+ local opts n
+
+ if [[ "$COMP_WORDBREAKS" != ' ' ]]; then
+ COMP_WORDBREAKS=' '
+ return 124 # try again with proper value
+ fi
+ # This extracts short and long options from the help output
+ local script='{
+ if ($1 ~ "-[a-zA-Z]," && $2 ~ "--[a-zA-Z]") {
+ print substr($1, 0, 2);
+ gsub("=.*", "", $2)
+ print $2
+ } else if ($1 ~ "--[a-zA-Z]") {
+ gsub("=.*", "", $1)
+ print $1
+ }
+ }'
-#_expand || return 0
+ if [[ "$cur" == -* ]]; then # option
+ # Depending on whether '--' is one of the previous words we
+ # complete either on local options, i.e. those of the program
+ # to execute, or call the program to print possible completions
+ # (to a subcommand).
+ local_opts=true
+ for ((i=0; i < $COMP_CWORD; i++)); do
+ [[ "${COMP_WORDS[$i]}" != '--' ]] && continue
+ local_opts=false
+ break
+ done
+ if [[ "$local_opts" == "true" ]]; then
+ result="-- $($prg --help | awk "$script")"
+ COMPREPLY=($(compgen -W "$result" -- $cur))
+ return
+ fi
+ fi
+ # We need to call the program with --complete to get the possible
+ # completions. Before that, all local options must be discarded.
+ IFS=' '
+ n=0
+ for word in $line; do
+ ((n > 0)) && ! [[ "$word" == -* ]] && break
+ line="${line##*( )}" # remove leading whitespace
+ line="${line##+([^ ])}"
+ line="${line##*( )}"
+ let n++
+ [[ "$word" == '--' ]] && break
+ done
+ IFS="$OLD_IFS"
+ s=$((${#COMP_LINE} - ${#line})) # how many characters have been cut
+ if (($COMP_POINT > $s)); then
+ COMP_POINT=$(($COMP_POINT - $s))
+ else
+ COMP_POINT=0
+ fi
+ COMP_LINE="$line"
+ #echo "line: $COMP_LINE, point: $COMP_POINT"
+ export COMP_LINE COMP_POINT
+ result=($($prg --complete))
-case "${COMP_WORDS[1]}" in
- cs|cs|strdel|strq)
- COMPREPLY=( $( eval para_client streams | grep "^$cur" \
- 2>/dev/null ) )
- return 0
- ;;
- info|la|us|ca|ls|pic|us)
- COMPREPLY=( $( eval para_client ls "$cur%" ) )
- return 0
- ;;
- sa)
- COMPREPLY=( $( eval para_client laa | grep "^$cur" \
- 2>/dev/null ) )
- return 0
-
+ # the last line of the output contains the options for compopt,
+ # prefixed with '-o='.
+ n=${#result[@]}
+ (($n == 0)) && return # oops, $prg did not write any output
+ let n--
+ opts="${result[$n]}"
+ result[$n]=
+ opts="${opts#-o=}"
+ IFS=','
+ compopt +o nospace
+ for opt in $opts; do
+ #echo "opt: $opt"
+ case "$opt" in
+ filenames) compopt -o filenames;;
+ nospace) compopt -o nospace;;
+ esac
+ done
+ IFS="$OLD_IFS"
+ COMPREPLY=(${result[@]})
+}
-
-esac
-# default completion if parameter contains /
-# [[ "$cur" == */* ]] && return 0
-#echo "cur=$cur"
-COMPREPLY=( $( eval para_client help | cut -f 1 | sed 1d | grep ^$cur 2>/dev/null ) )
-# weed out directory path names and paths to man pages
-COMPREPLY=( ${COMPREPLY[@]##*/?(:)} )
-COMPREPLY=( ${COMPREPLY[@]} $( compgen -G $cur\*.[0-9ln] ) )
-return 0
-}
-complete -F _para -o default para
-complete -F _para -o default para_client
+_para_audioc()
+{
+ _para_complete para_audioc
+}
+complete -F _para_audioc para_audioc
+
+_para_client()
+{
+ _para_complete para_client
+}
+complete -o default -o nospace -F _para_client para_client
+complete -o default -o nospace -F _para_client para