comparison contrib/bash_completion @ 916:fe094cca9915

Add bash_completion to contrib Contributed by "Alexis S. L. Carvalho" <alexis@cecm.usp.br> Attached is a file that implements bash completion for hg. Just reading it from your .bashrc should be enough to use it - I think: I'm using the /etc/bash_completion from debian and I'm not sure whether it sets some important option. It gets the list of commands, aliases and options from the output of hg help and then adds some specific stuff - e.g. completing update with tags; pull and push with path aliases and directories, etc.
author mpm@selenic.com
date Tue, 16 Aug 2005 14:17:27 -0800
parents
children f4c7ad186983
comparison
equal deleted inserted replaced
915:24a31f46fa13 916:fe094cca9915
1 _hg_commands()
2 {
3 local commands="$(hg -v help | sed -e '1,/^list of commands:/d' \
4 -e '/^global options:/Q' \
5 -e '/^ [^ ]/!d; s/[,:]//g;')"
6
7 # hide debug commands from users, but complete them if
8 # specifically asked for
9 if [[ "$cur" == de* ]]; then
10 commands="$commands debugcheckstate debugstate debugindex"
11 commands="$commands debugindexdot debugwalk"
12 fi
13 COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "$commands" -- "$cur") )
14 }
15
16 _hg_paths()
17 {
18 local paths="$(hg paths | sed -e 's/ = .*$//')"
19 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -W "$paths" -- "$cur" ))
20 }
21
22 _hg_tags()
23 {
24 local tags="$(hg tags | sed -e 's/[0-9]*:[a-f0-9]\{40\}$//; s/ *$//')"
25 COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "$tags" -- "$cur") )
26 }
27
28 # this is "kind of" ugly...
29 _hg_count_non_option()
30 {
31 local i count=0
32 local filters="$1"
33
34 for (( i=1; $i<=$COMP_CWORD; i++ )); do
35 if [[ "${COMP_WORDS[i]}" != -* ]]; then
36 for f in $filters; do
37 if [[ ${COMP_WORDS[i-1]} == $f ]]; then
38 continue 2
39 fi
40 done
41 count=$(($count + 1))
42 fi
43 done
44
45 echo $(($count - 1))
46 }
47
48 _hg()
49 {
50 local cur prev cmd opts i
51
52 COMPREPLY=()
53 cur="$2"
54 prev="$3"
55
56 # searching for the command
57 # (first non-option argument that doesn't follow -R/--repository)
58 for (( i=1; $i<=$COMP_CWORD; i++ )); do
59 if [[ ${COMP_WORDS[i]} != -* ]] \
60 && [ "${COMP_WORDS[i-1]}" != -R ] \
61 && [ "${COMP_WORDS[i-1]}" != --repository ]; then
62 cmd="${COMP_WORDS[i]}"
63 break
64 fi
65 done
66
67 if [[ "$cur" == -* ]]; then
68 opts="$(hg -v help | sed -e '1,/^global options/d; /^ -/!d')"
69
70 if [ -n "$cmd" ]; then
71 opts="$opts $(hg help "$cmd" | sed -e '/^ -/!d; s/ [^-][^ ]*//')"
72 fi
73
74 COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "$opts" -- "$cur") )
75 return
76 fi
77
78 if [ "$prev" = -R ] || [ "$prev" = --repository ]; then
79 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -d -- "$cur" ))
80 return
81 fi
82
83 if [ -z "$cmd" ] || [ $COMP_CWORD -eq $i ]; then
84 _hg_commands
85 return
86 fi
87
88 if [ "$cmd" != status ] && [ "$prev" = -r ] || [ "$prev" = --rev ]; then
89 _hg_tags
90 return
91 fi
92
93 case "$cmd" in
94 help)
95 _hg_commands
96 ;;
97 export|manifest|update|checkout|up|co)
98 _hg_tags
99 ;;
100 pull|push)
101 _hg_paths
102 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -d -- "$cur" ))
103 ;;
104 paths)
105 _hg_paths
106 ;;
107 clone)
108 local count=$(_hg_count_non_option)
109 if [ $count = 1 ]; then
110 _hg_paths
111 fi
112 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -d -- "$cur" ))
113 ;;
114 cat)
115 local count=$(_hg_count_non_option -o --output)
116 if [ $count = 2 ]; then
117 _hg_tags
118 else
119 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -- "$cur" ))
120 fi
121 ;;
122 *)
123 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -- "$cur" ))
124 ;;
125 esac
126
127 }
128
129 complete -o filenames -F _hg hg