##// END OF EJS Templates
bash_completion is not an executable...
Vincent Danjean -
r1339:f351d1a0 default
parent child Browse files
Show More
@@ -1,173 +1,171 b''
1 #!/bin/bash
2
3 1 shopt -s extglob
4 2
5 3 _hg_commands()
6 4 {
7 5 local commands="$(hg -v help | sed -e '1,/^list of commands:/d' \
8 6 -e '/^global options:/,$d' \
9 7 -e '/^ [^ ]/!d; s/[,:]//g;')"
10 8
11 9 # hide debug commands from users, but complete them if
12 10 # specifically asked for
13 11 if [[ "$cur" == de* ]]; then
14 12 commands="$commands debugcheckstate debugstate debugindex"
15 13 commands="$commands debugindexdot debugwalk debugdata"
16 14 commands="$commands debugancestor debugconfig debugrename"
17 15 fi
18 16 COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "$commands" -- "$cur") )
19 17 }
20 18
21 19 _hg_paths()
22 20 {
23 21 local paths="$(hg paths | sed -e 's/ = .*$//')"
24 22 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -W "$paths" -- "$cur" ))
25 23 }
26 24
27 25 _hg_status()
28 26 {
29 27 local files="$( hg status -$1 | cut -b 3- )"
30 28 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -W "$files" -- "$cur" ))
31 29 }
32 30
33 31 _hg_tags()
34 32 {
35 33 local tags="$(hg tags | sed -e 's/[0-9]*:[a-f0-9]\{40\}$//; s/ *$//')"
36 34 COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "$tags" -- "$cur") )
37 35 }
38 36
39 37 # this is "kind of" ugly...
40 38 _hg_count_non_option()
41 39 {
42 40 local i count=0
43 41 local filters="$1"
44 42
45 43 for (( i=1; $i<=$COMP_CWORD; i++ )); do
46 44 if [[ "${COMP_WORDS[i]}" != -* ]]; then
47 45 if [[ ${COMP_WORDS[i-1]} == @($filters|$global_args) ]]; then
48 46 continue
49 47 fi
50 48 count=$(($count + 1))
51 49 fi
52 50 done
53 51
54 52 echo $(($count - 1))
55 53 }
56 54
57 55 _hg()
58 56 {
59 57 local cur prev cmd opts i
60 58 # global options that receive an argument
61 59 local global_args='--cwd|-R|--repository'
62 60
63 61 COMPREPLY=()
64 62 cur="$2"
65 63 prev="$3"
66 64
67 65 # searching for the command
68 66 # (first non-option argument that doesn't follow a global option that
69 67 # receives an argument)
70 68 for (( i=1; $i<=$COMP_CWORD; i++ )); do
71 69 if [[ ${COMP_WORDS[i]} != -* ]]; then
72 70 if [[ ${COMP_WORDS[i-1]} != @($global_args) ]]; then
73 71 cmd="${COMP_WORDS[i]}"
74 72 break
75 73 fi
76 74 fi
77 75 done
78 76
79 77 if [[ "$cur" == -* ]]; then
80 78 # this assumes that there are no commands with spaces in the name
81 79 opts=$(hg -v help $cmd | sed -e '/^ *-/!d; s/ [^- ].*//')
82 80
83 81 COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "$opts" -- "$cur") )
84 82 return
85 83 fi
86 84
87 85 # global options
88 86 case "$prev" in
89 87 -R|--repository)
90 88 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -d -- "$cur" ))
91 89 return
92 90 ;;
93 91 --cwd)
94 92 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -d -- "$cur" ))
95 93 return
96 94 ;;
97 95 esac
98 96
99 97 if [ -z "$cmd" ] || [ $COMP_CWORD -eq $i ]; then
100 98 _hg_commands
101 99 return
102 100 fi
103 101
104 102 # canonicalize command name
105 103 cmd=$(hg -q help "$cmd" | sed -e 's/^hg //; s/ .*//; 1q')
106 104
107 105 if [ "$cmd" != status ] && [ "$prev" = -r ] || [ "$prev" = --rev ]; then
108 106 _hg_tags
109 107 return
110 108 fi
111 109
112 110 case "$cmd" in
113 111 help)
114 112 _hg_commands
115 113 ;;
116 114 export|manifest|update)
117 115 _hg_tags
118 116 ;;
119 117 pull|push|outgoing|incoming)
120 118 _hg_paths
121 119 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -d -- "$cur" ))
122 120 ;;
123 121 paths)
124 122 _hg_paths
125 123 ;;
126 124 add)
127 125 _hg_status "u"
128 126 ;;
129 127 commit)
130 128 _hg_status "mra"
131 129 ;;
132 130 remove)
133 131 _hg_status "r"
134 132 ;;
135 133 forget)
136 134 _hg_status "a"
137 135 ;;
138 136 diff)
139 137 _hg_status "mra"
140 138 ;;
141 139 revert)
142 140 _hg_status "mra"
143 141 ;;
144 142 clone)
145 143 local count=$(_hg_count_non_option)
146 144 if [ $count = 1 ]; then
147 145 _hg_paths
148 146 fi
149 147 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -d -- "$cur" ))
150 148 ;;
151 149 debugindex|debugindexdot)
152 150 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -X "!*.i" -- "$cur" ))
153 151 ;;
154 152 debugdata)
155 153 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -X "!*.d" -- "$cur" ))
156 154 ;;
157 155 cat)
158 156 local count=$(_hg_count_non_option '-o|--output')
159 157 if [ $count = 2 ]; then
160 158 _hg_tags
161 159 else
162 160 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -- "$cur" ))
163 161 fi
164 162 ;;
165 163 *)
166 164 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -- "$cur" ))
167 165 ;;
168 166 esac
169 167
170 168 }
171 169
172 170 complete -o bashdefault -o default -F _hg hg 2> /dev/null \
173 171 || complete -o default -F _hg hg
General Comments 0
You need to be logged in to leave comments. Login now