bash_completion
207 lines
| 3.9 KiB
| application/x-sh
|
BashLexer
/ contrib / bash_completion
TK Soh
|
r1311 | shopt -s extglob | ||
Alexis S. L. Carvalho
|
r1641 | _hg_command_list() | ||
{ | ||||
Thomas Arendsen Hein
|
r1683 | "$hg" --debug help 2>/dev/null | \ | ||
Alexis S. L. Carvalho
|
r1641 | awk 'function command_line(line) { | ||
Thomas Arendsen Hein
|
r1684 | 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])}' | ||||
Alexis S. L. Carvalho
|
r1641 | |||
} | ||||
_hg_option_list() | ||||
{ | ||||
Thomas Arendsen Hein
|
r1684 | "$hg" -v help $1 2>/dev/null | \ | ||
awk '/^ *-/ { | ||||
for (i = 1; i <= NF; i ++) { | ||||
Alexis S. L. Carvalho
|
r1641 | if (index($i, "-") != 1) | ||
Thomas Arendsen Hein
|
r1684 | break; | ||
Alexis S. L. Carvalho
|
r1641 | print $i; | ||
Thomas Arendsen Hein
|
r1684 | } | ||
}' | ||||
Alexis S. L. Carvalho
|
r1641 | } | ||
mpm@selenic.com
|
r916 | _hg_commands() | ||
{ | ||||
Alexis S. L. Carvalho
|
r1555 | local all commands result | ||
Alexis S. L. Carvalho
|
r1641 | all=$(_hg_command_list) | ||
commands=${all%%$'\n'debug*} | ||||
Alexis S. L. Carvalho
|
r1642 | result=$(compgen -W '$commands' -- "$cur") | ||
Thomas Arendsen Hein
|
r1308 | |||
# hide debug commands from users, but complete them if | ||||
Alexis S. L. Carvalho
|
r1555 | # there is no other possible command | ||
if [ "$result" = "" ]; then | ||||
local debug | ||||
Alexis S. L. Carvalho
|
r1641 | debug=debug${all#*$'\n'debug} | ||
Alexis S. L. Carvalho
|
r1642 | result=$(compgen -W '$debug' -- "$cur") | ||
mpm@selenic.com
|
r916 | fi | ||
Alexis S. L. Carvalho
|
r1555 | |||
COMPREPLY=(${COMPREPLY[@]:-} $result) | ||||
mpm@selenic.com
|
r916 | } | ||
_hg_paths() | ||||
{ | ||||
Thomas Arendsen Hein
|
r1684 | local paths="$("$hg" paths 2>/dev/null | sed -e 's/ = .*$//')" | ||
COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$paths' -- "$cur")) | ||||
mpm@selenic.com
|
r916 | } | ||
Daniel Kobras
|
r1587 | _hg_repos() | ||
{ | ||||
local i | ||||
Thomas Arendsen Hein
|
r1684 | for i in $(compgen -d -- "$cur"); do | ||
test ! -d "$i"/.hg || COMPREPLY=(${COMPREPLY[@]:-} "$i") | ||||
Daniel Kobras
|
r1587 | done | ||
} | ||||
mpm@selenic.com
|
r935 | _hg_status() | ||
{ | ||||
Thomas Arendsen Hein
|
r1684 | local files="$("$hg" status -n$1 . 2>/dev/null)" | ||
COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$files' -- "$cur")) | ||||
mpm@selenic.com
|
r935 | } | ||
mpm@selenic.com
|
r916 | _hg_tags() | ||
{ | ||||
Thomas Arendsen Hein
|
r1684 | local tags="$("$hg" tags 2>/dev/null | | ||
sed -e 's/[0-9]*:[a-f0-9]\{40\}$//; s/ *$//')" | ||||
COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$tags' -- "$cur")) | ||||
mpm@selenic.com
|
r916 | } | ||
# this is "kind of" ugly... | ||||
_hg_count_non_option() | ||||
{ | ||||
local i count=0 | ||||
local filters="$1" | ||||
Thomas Arendsen Hein
|
r1684 | for ((i=1; $i<=$COMP_CWORD; i++)); do | ||
mpm@selenic.com
|
r916 | if [[ "${COMP_WORDS[i]}" != -* ]]; then | ||
Alexis S. L. Carvalho
|
r1152 | if [[ ${COMP_WORDS[i-1]} == @($filters|$global_args) ]]; then | ||
continue | ||||
fi | ||||
mpm@selenic.com
|
r916 | count=$(($count + 1)) | ||
fi | ||||
done | ||||
echo $(($count - 1)) | ||||
} | ||||
_hg() | ||||
{ | ||||
local cur prev cmd opts i | ||||
Alexis S. L. Carvalho
|
r1151 | # global options that receive an argument | ||
local global_args='--cwd|-R|--repository' | ||||
Thomas Arendsen Hein
|
r1683 | local hg="$1" | ||
mpm@selenic.com
|
r916 | |||
COMPREPLY=() | ||||
cur="$2" | ||||
prev="$3" | ||||
Thomas Arendsen Hein
|
r1308 | # searching for the command | ||
Alexis S. L. Carvalho
|
r1151 | # (first non-option argument that doesn't follow a global option that | ||
# receives an argument) | ||||
Thomas Arendsen Hein
|
r1684 | for ((i=1; $i<=$COMP_CWORD; i++)); do | ||
Alexis S. L. Carvalho
|
r1151 | if [[ ${COMP_WORDS[i]} != -* ]]; then | ||
if [[ ${COMP_WORDS[i-1]} != @($global_args) ]]; then | ||||
cmd="${COMP_WORDS[i]}" | ||||
break | ||||
fi | ||||
mpm@selenic.com
|
r916 | fi | ||
done | ||||
if [[ "$cur" == -* ]]; then | ||||
Alexis S. L. Carvalho
|
r1641 | opts=$(_hg_option_list $cmd) | ||
mpm@selenic.com
|
r916 | |||
Thomas Arendsen Hein
|
r1684 | COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$opts' -- "$cur")) | ||
mpm@selenic.com
|
r916 | return | ||
fi | ||||
Alexis S. L. Carvalho
|
r1151 | # global options | ||
case "$prev" in | ||||
-R|--repository) | ||||
Daniel Kobras
|
r1587 | _hg_repos | ||
Alexis S. L. Carvalho
|
r1151 | return | ||
;; | ||||
--cwd) | ||||
Daniel Kobras
|
r1587 | # Stick with default bash completion | ||
Alexis S. L. Carvalho
|
r1151 | return | ||
;; | ||||
esac | ||||
mpm@selenic.com
|
r916 | |||
if [ -z "$cmd" ] || [ $COMP_CWORD -eq $i ]; then | ||||
_hg_commands | ||||
return | ||||
fi | ||||
Alexis S. L. Carvalho
|
r1150 | # canonicalize command name | ||
Thomas Arendsen Hein
|
r1684 | cmd=$("$hg" -q help "$cmd" 2>/dev/null | sed -e 's/^hg //; s/ .*//; 1q') | ||
Alexis S. L. Carvalho
|
r1150 | |||
mpm@selenic.com
|
r916 | if [ "$cmd" != status ] && [ "$prev" = -r ] || [ "$prev" = --rev ]; then | ||
_hg_tags | ||||
return | ||||
fi | ||||
case "$cmd" in | ||||
help) | ||||
_hg_commands | ||||
;; | ||||
Alexis S. L. Carvalho
|
r1150 | export|manifest|update) | ||
mpm@selenic.com
|
r916 | _hg_tags | ||
;; | ||||
Alexis S. L. Carvalho
|
r1150 | pull|push|outgoing|incoming) | ||
mpm@selenic.com
|
r916 | _hg_paths | ||
Daniel Kobras
|
r1587 | _hg_repos | ||
mpm@selenic.com
|
r916 | ;; | ||
paths) | ||||
_hg_paths | ||||
;; | ||||
mpm@selenic.com
|
r935 | add) | ||
_hg_status "u" | ||||
;; | ||||
Alexis S. L. Carvalho
|
r1150 | commit) | ||
Thomas Arendsen Hein
|
r1639 | _hg_status "mar" | ||
mpm@selenic.com
|
r935 | ;; | ||
remove) | ||||
Thomas Arendsen Hein
|
r1639 | _hg_status "d" | ||
mpm@selenic.com
|
r935 | ;; | ||
forget) | ||||
_hg_status "a" | ||||
;; | ||||
diff) | ||||
Thomas Arendsen Hein
|
r1639 | _hg_status "mar" | ||
mpm@selenic.com
|
r935 | ;; | ||
revert) | ||||
Thomas Arendsen Hein
|
r1639 | _hg_status "mard" | ||
mpm@selenic.com
|
r935 | ;; | ||
mpm@selenic.com
|
r916 | clone) | ||
local count=$(_hg_count_non_option) | ||||
if [ $count = 1 ]; then | ||||
_hg_paths | ||||
fi | ||||
Thomas Arendsen Hein
|
r1684 | _hg_repos | ||
mpm@selenic.com
|
r916 | ;; | ||
mpm@selenic.com
|
r1115 | debugindex|debugindexdot) | ||
Thomas Arendsen Hein
|
r1684 | COMPREPLY=(${COMPREPLY[@]:-} $(compgen -f -X "!*.i" -- "$cur")) | ||
mpm@selenic.com
|
r1115 | ;; | ||
debugdata) | ||||
Thomas Arendsen Hein
|
r1684 | COMPREPLY=(${COMPREPLY[@]:-} $(compgen -f -X "!*.d" -- "$cur")) | ||
mpm@selenic.com
|
r1115 | ;; | ||
mpm@selenic.com
|
r916 | esac | ||
} | ||||
Thomas Arendsen Hein
|
r1684 | complete -o bashdefault -o default -F _hg hg 2>/dev/null \ | ||
Alexis S. L. Carvalho
|
r1153 | || complete -o default -F _hg hg | ||