# Copyright (C) 2007-2011 Andre Noll # # Licensed under the GPL v2. For licencing details see COPYING. _para_complete() { 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 } }' 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)) # 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[@]}) } _para_audioc() { _para_complete ~maan/para/para_audioc } complete -F _para_audioc para_audioc _para_client() { _para_complete ~maan/para/para_client } complete -o default -o nospace -F _para_client para_client complete -o default -o nospace -F _para_client para