diff --git a/contrib/bash_completion b/contrib/bash_completion --- a/contrib/bash_completion +++ b/contrib/bash_completion @@ -1,22 +1,53 @@ shopt -s extglob +_hg_command_list() +{ + hg --debug help 2>/dev/null | \ + awk 'function command_line(line) { + gsub(/,/, "", line) + gsub(/:.*/, "", line) + split(line, aliases) + command = aliases[1] + delete aliases[1] + print command + for (i in aliases) + if (index(command, aliases[i]) != 1) + print aliases[i] + } + /^list of commands:/ {commands=1} + commands && /^ debug/ {a[i++] = $0; next;} + commands && /^ [^ ]/ {command_line($0)} + /^global options:/ {exit 0} + END {for (i in a) command_line(a[i])}' + +} + +_hg_option_list() +{ + hg -v help $1 2> /dev/null | \ + awk '/^ *-/ { + for (i = 1; i <= NF; i ++) { + if (index($i, "-") != 1) + break; + print $i; + } + }' +} + + _hg_commands() { local all commands result - all=($(hg --debug help | sed -e '1,/^list of commands:/d' \ - -e '/^global options:/,$d' \ - -e '/^ [^ ]/!d; s/^ //; s/[,:]//g;')) - - commands="${all[*]##debug*}" + all=$(_hg_command_list) + commands=${all%%$'\n'debug*} result=$(compgen -W "$commands" -- "$cur") # hide debug commands from users, but complete them if # there is no other possible command if [ "$result" = "" ]; then local debug - debug=(${all[*]##!(debug*)}) - debug="${debug[*]/g/debug}" + debug=debug${all#*$'\n'debug} result=$(compgen -W "$debug" -- "$cur") fi @@ -90,8 +121,7 @@ shopt -s extglob done if [[ "$cur" == -* ]]; then - # this assumes that there are no commands with spaces in the name - opts=$(hg -v help $cmd | sed -e '/^ *-/!d; s/ [^- ].*//') + opts=$(_hg_option_list $cmd) COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "$opts" -- "$cur") ) return