contrib/bash_completion
changeset 1641 1ef060ae7966
parent 1639 dbfc04a55607
child 1642 b8d792057e5b
equal deleted inserted replaced
1640:9a5b778f7e2d 1641:1ef060ae7966
     1 shopt -s extglob
     1 shopt -s extglob
     2 
     2 
       
     3 _hg_command_list()
       
     4 {
       
     5     hg --debug help 2>/dev/null | \
       
     6 	awk 'function command_line(line) {
       
     7 		 gsub(/,/, "", line)
       
     8 		 gsub(/:.*/, "", line)
       
     9 		 split(line, aliases)
       
    10 		 command = aliases[1]
       
    11 		 delete aliases[1]
       
    12 		 print command
       
    13 		 for (i in aliases)
       
    14 		     if (index(command, aliases[i]) != 1)
       
    15 			 print aliases[i]
       
    16 	     }
       
    17 	     /^list of commands:/ {commands=1}
       
    18 	     commands && /^ debug/ {a[i++] = $0; next;}
       
    19 	     commands && /^ [^ ]/ {command_line($0)}
       
    20 	     /^global options:/ {exit 0}
       
    21 	     END {for (i in a) command_line(a[i])}'
       
    22 
       
    23 }
       
    24 
       
    25 _hg_option_list()
       
    26 {
       
    27     hg -v help $1 2> /dev/null | \
       
    28         awk '/^ *-/ {
       
    29 		 for (i = 1; i <= NF; i ++) {
       
    30 		    if (index($i, "-") != 1)
       
    31 			 break;
       
    32 		    print $i;
       
    33 		 }
       
    34 	     }'
       
    35 }
       
    36 
       
    37 
     3 _hg_commands()
    38 _hg_commands()
     4 {
    39 {
     5     local all commands result
    40     local all commands result
     6 
    41 
     7     all=($(hg --debug help | sed -e '1,/^list of commands:/d' \
    42     all=$(_hg_command_list)
     8 				 -e '/^global options:/,$d' \
    43     commands=${all%%$'\n'debug*}
     9 				 -e '/^ [^ ]/!d; s/^ //; s/[,:]//g;'))
       
    10 
       
    11     commands="${all[*]##debug*}"
       
    12     result=$(compgen -W "$commands" -- "$cur")
    44     result=$(compgen -W "$commands" -- "$cur")
    13 
    45 
    14     # hide debug commands from users, but complete them if
    46     # hide debug commands from users, but complete them if
    15     # there is no other possible command
    47     # there is no other possible command
    16     if [ "$result" = "" ]; then
    48     if [ "$result" = "" ]; then
    17 	local debug
    49 	local debug
    18 	debug=(${all[*]##!(debug*)})
    50 	debug=debug${all#*$'\n'debug}
    19 	debug="${debug[*]/g/debug}"
       
    20 	result=$(compgen -W "$debug" -- "$cur")
    51 	result=$(compgen -W "$debug" -- "$cur")
    21     fi
    52     fi
    22 
    53 
    23     COMPREPLY=(${COMPREPLY[@]:-} $result)
    54     COMPREPLY=(${COMPREPLY[@]:-} $result)
    24 }
    55 }
    88 	    fi
   119 	    fi
    89 	fi
   120 	fi
    90     done
   121     done
    91 
   122 
    92     if [[ "$cur" == -* ]]; then
   123     if [[ "$cur" == -* ]]; then
    93 	# this assumes that there are no commands with spaces in the name
   124 	opts=$(_hg_option_list $cmd)
    94 	opts=$(hg -v help $cmd | sed -e '/^ *-/!d; s/ [^- ].*//')
       
    95 
   125 
    96 	COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "$opts" -- "$cur") )
   126 	COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "$opts" -- "$cur") )
    97 	return
   127 	return
    98     fi
   128     fi
    99 
   129