bash_completion: be more careful about whitespaces
- use awk to parse the output of hg help.
- print one completion candidate per line
- print the debug commands after regular commands
(this eases the shell side of the parsing)
- don't print aliases that are simple abbreviations
(e.g. up/update, id/identify)
--- a/contrib/bash_completion Thu Jan 26 16:37:31 2006 +0100
+++ b/contrib/bash_completion Fri Jan 27 12:10:35 2006 +0100
@@ -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 @@
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