##// END OF EJS Templates
Improve bash_completion for patches in MQ
"Mathieu Clabaut " -
r2695:c995d683 default
parent child Browse files
Show More
@@ -1,385 +1,390 b''
1 1 # bash completion for the Mercurial distributed SCM
2 2
3 3 # Docs:
4 4 #
5 5 # If you source this file from your .bashrc, bash should be able to
6 6 # complete a command line that uses hg with all the available commands
7 7 # and options and sometimes even arguments.
8 8 #
9 9 # Mercurial allows you to define additional commands through extensions.
10 10 # Bash should be able to automatically figure out the name of these new
11 11 # commands and their options. If you also want to tell it how to
12 12 # complete non-option arguments, see below for how to define an
13 13 # _hg_cmd_foo function.
14 14 #
15 15 #
16 16 # Notes about completion for specific commands:
17 17 #
18 18 # - the completion function for the email command from the patchbomb
19 19 # extension will try to call _hg_emails to get a list of e-mail
20 20 # addresses. It's up to the user to define this function. For
21 21 # example, put the addresses of the lists that you usually patchbomb
22 22 # in ~/.patchbomb-to and the addresses that you usually use to send
23 23 # the patchbombs in ~/.patchbomb-from and use something like this:
24 24 #
25 25 # _hg_emails()
26 26 # {
27 27 # if [ -r ~/.patchbomb-$1 ]; then
28 28 # cat ~/.patchbomb-$1
29 29 # fi
30 30 # }
31 31 #
32 32 #
33 33 # Writing completion functions for additional commands:
34 34 #
35 35 # If it exists, the function _hg_cmd_foo will be called without
36 36 # arguments to generate the completion candidates for the hg command
37 37 # "foo".
38 38 #
39 39 # In addition to the regular completion variables provided by bash,
40 40 # the following variables are also set:
41 41 # - $hg - the hg program being used (e.g. /usr/bin/hg)
42 42 # - $cmd - the name of the hg command being completed
43 43 # - $cmd_index - the index of $cmd in $COMP_WORDS
44 44 # - $cur - the current argument being completed
45 45 # - $prev - the argument before $cur
46 46 # - $global_args - "|"-separated list of global options that accept
47 47 # an argument (e.g. '--cwd|-R|--repository')
48 48 # - $canonical - 1 if we canonicalized $cmd before calling the function
49 49 # 0 otherwise
50 50 #
51 51
52 52 shopt -s extglob
53 53
54 54 _hg_commands()
55 55 {
56 56 local commands
57 57 commands="$("$hg" debugcomplete "$cur" 2>/dev/null)" || commands=""
58 58 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$commands' -- "$cur"))
59 59 }
60 60
61 61 _hg_paths()
62 62 {
63 63 local paths="$("$hg" paths 2>/dev/null | sed -e 's/ = .*$//')"
64 64 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$paths' -- "$cur"))
65 65 }
66 66
67 67 _hg_repos()
68 68 {
69 69 local i
70 70 for i in $(compgen -d -- "$cur"); do
71 71 test ! -d "$i"/.hg || COMPREPLY=(${COMPREPLY[@]:-} "$i")
72 72 done
73 73 }
74 74
75 75 _hg_status()
76 76 {
77 77 local files="$("$hg" status -n$1 . 2>/dev/null)"
78 78 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$files' -- "$cur"))
79 79 }
80 80
81 81 _hg_tags()
82 82 {
83 83 local tags="$("$hg" tags -q 2>/dev/null)"
84 84 local IFS=$'\n'
85 85 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$tags' -- "$cur"))
86 86 }
87 87
88 88 # this is "kind of" ugly...
89 89 _hg_count_non_option()
90 90 {
91 91 local i count=0
92 92 local filters="$1"
93 93
94 94 for ((i=1; $i<=$COMP_CWORD; i++)); do
95 95 if [[ "${COMP_WORDS[i]}" != -* ]]; then
96 96 if [[ ${COMP_WORDS[i-1]} == @($filters|$global_args) ]]; then
97 97 continue
98 98 fi
99 99 count=$(($count + 1))
100 100 fi
101 101 done
102 102
103 103 echo $(($count - 1))
104 104 }
105 105
106 106 _hg()
107 107 {
108 108 local cur prev cmd cmd_index opts i
109 109 # global options that receive an argument
110 110 local global_args='--cwd|-R|--repository'
111 111 local hg="$1"
112 112
113 113 COMPREPLY=()
114 114 cur="$2"
115 115 prev="$3"
116 116
117 117 # searching for the command
118 118 # (first non-option argument that doesn't follow a global option that
119 119 # receives an argument)
120 120 for ((i=1; $i<=$COMP_CWORD; i++)); do
121 121 if [[ ${COMP_WORDS[i]} != -* ]]; then
122 122 if [[ ${COMP_WORDS[i-1]} != @($global_args) ]]; then
123 123 cmd="${COMP_WORDS[i]}"
124 124 cmd_index=$i
125 125 break
126 126 fi
127 127 fi
128 128 done
129 129
130 130 if [[ "$cur" == -* ]]; then
131 131 opts=$("$hg" debugcomplete --options "$cmd" 2>/dev/null)
132 132
133 133 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$opts' -- "$cur"))
134 134 return
135 135 fi
136 136
137 137 # global options
138 138 case "$prev" in
139 139 -R|--repository)
140 140 _hg_repos
141 141 return
142 142 ;;
143 143 --cwd)
144 144 # Stick with default bash completion
145 145 return
146 146 ;;
147 147 esac
148 148
149 149 if [ -z "$cmd" ] || [ $COMP_CWORD -eq $i ]; then
150 150 _hg_commands
151 151 return
152 152 fi
153 153
154 154 # try to generate completion candidates for whatever command the user typed
155 155 local help
156 156 local canonical=0
157 157 if _hg_command_specific; then
158 158 return
159 159 fi
160 160
161 161 # canonicalize the command name and try again
162 162 help=$("$hg" help "$cmd" 2>/dev/null)
163 163 if [ $? -ne 0 ]; then
164 164 # Probably either the command doesn't exist or it's ambiguous
165 165 return
166 166 fi
167 167 cmd=${help#hg }
168 168 cmd=${cmd%%[$' \n']*}
169 169 canonical=1
170 170 _hg_command_specific
171 171 }
172 172
173 173 _hg_command_specific()
174 174 {
175 175 if [ "$(type -t "_hg_cmd_$cmd")" = function ]; then
176 176 "_hg_cmd_$cmd"
177 177 return 0
178 178 fi
179 179
180 180 if [ "$cmd" != status ] && [ "$prev" = -r ] || [ "$prev" == --rev ]; then
181 181 if [ $canonical = 1 ]; then
182 182 _hg_tags
183 183 return 0
184 184 elif [[ status != "$cmd"* ]]; then
185 185 _hg_tags
186 186 return 0
187 187 else
188 188 return 1
189 189 fi
190 190 fi
191 191
192 192 case "$cmd" in
193 193 help)
194 194 _hg_commands
195 195 ;;
196 196 export|manifest|update)
197 197 _hg_tags
198 198 ;;
199 199 pull|push|outgoing|incoming)
200 200 _hg_paths
201 201 _hg_repos
202 202 ;;
203 203 paths)
204 204 _hg_paths
205 205 ;;
206 206 add)
207 207 _hg_status "u"
208 208 ;;
209 209 commit)
210 210 _hg_status "mar"
211 211 ;;
212 212 remove)
213 213 _hg_status "d"
214 214 ;;
215 215 forget)
216 216 _hg_status "a"
217 217 ;;
218 218 diff)
219 219 _hg_status "mar"
220 220 ;;
221 221 revert)
222 222 _hg_status "mard"
223 223 ;;
224 224 clone)
225 225 local count=$(_hg_count_non_option)
226 226 if [ $count = 1 ]; then
227 227 _hg_paths
228 228 fi
229 229 _hg_repos
230 230 ;;
231 231 debugindex|debugindexdot)
232 232 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -f -X "!*.i" -- "$cur"))
233 233 ;;
234 234 debugdata)
235 235 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -f -X "!*.d" -- "$cur"))
236 236 ;;
237 237 *)
238 238 return 1
239 239 ;;
240 240 esac
241 241
242 242 return 0
243 243 }
244 244
245 245 complete -o bashdefault -o default -F _hg hg 2>/dev/null \
246 246 || complete -o default -F _hg hg
247 247
248 248
249 249 # Completion for commands provided by extensions
250 250
251 251 # mq
252 252 _hg_ext_mq_patchlist()
253 253 {
254 254 local patches=$("$hg" $1 2>/dev/null)
255 255 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$patches' -- "$cur"))
256 256 }
257 257
258 258 _hg_ext_mq_queues()
259 259 {
260 260 local root=$("$hg" root 2>/dev/null)
261 261 local n
262 262 for n in $(cd "$root"/.hg && compgen -d -- "$cur"); do
263 263 # I think we're usually not interested in the regular "patches" queue
264 264 # so just filter it.
265 265 if [ "$n" != patches ] && [ -e "$root/.hg/$n/series" ]; then
266 266 COMPREPLY=(${COMPREPLY[@]:-} "$n")
267 267 fi
268 268 done
269 269 }
270 270
271 271 _hg_cmd_qpop()
272 272 {
273 273 if [[ "$prev" = @(-n|--name) ]]; then
274 274 _hg_ext_mq_queues
275 275 return
276 276 fi
277 277 _hg_ext_mq_patchlist qapplied
278 278 }
279 279
280 280 _hg_cmd_qpush()
281 281 {
282 282 if [[ "$prev" = @(-n|--name) ]]; then
283 283 _hg_ext_mq_queues
284 284 return
285 285 fi
286 286 _hg_ext_mq_patchlist qunapplied
287 287 }
288 288
289 289 _hg_cmd_qdelete()
290 290 {
291 _hg_ext_mq_patchlist qseries
291 _hg_ext_mq_patchlist qunapplied
292 292 }
293 293
294 294 _hg_cmd_qsave()
295 295 {
296 296 if [[ "$prev" = @(-n|--name) ]]; then
297 297 _hg_ext_mq_queues
298 298 return
299 299 fi
300 300 }
301 301
302 302 _hg_cmd_strip()
303 303 {
304 304 _hg_tags
305 305 }
306 306
307 307 _hg_cmd_qcommit()
308 308 {
309 309 local root=$("$hg" root 2>/dev/null)
310 310 # this is run in a sub-shell, so we can't use _hg_status
311 311 local files=$(cd "$root/.hg/patches" 2>/dev/null &&
312 312 "$hg" status -nmar 2>/dev/null)
313 313 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$files' -- "$cur"))
314 314 }
315 315
316 _hg_cmd_export()
317 {
318 _hg_ext_mq_patchlist qapplied
319 }
320
316 321
317 322 # hbisect
318 323 _hg_cmd_bisect()
319 324 {
320 325 local i subcmd
321 326
322 327 # find the sub-command
323 328 for ((i=cmd_index+1; i<=COMP_CWORD; i++)); do
324 329 if [[ ${COMP_WORDS[i]} != -* ]]; then
325 330 if [[ ${COMP_WORDS[i-1]} != @($global_args) ]]; then
326 331 subcmd="${COMP_WORDS[i]}"
327 332 break
328 333 fi
329 334 fi
330 335 done
331 336
332 337 if [ -z "$subcmd" ] || [ $COMP_CWORD -eq $i ] || [ "$subcmd" = help ]; then
333 338 COMPREPLY=(${COMPREPLY[@]:-}
334 339 $(compgen -W 'bad good help init next reset' -- "$cur"))
335 340 return
336 341 fi
337 342
338 343 case "$subcmd" in
339 344 good|bad)
340 345 _hg_tags
341 346 ;;
342 347 esac
343 348
344 349 return
345 350 }
346 351
347 352
348 353 # patchbomb
349 354 _hg_cmd_email()
350 355 {
351 356 case "$prev" in
352 357 -c|--cc|-t|--to|-f|--from)
353 358 # we need an e-mail address. let the user provide a function
354 359 # to get them
355 360 if [ "$(type -t _hg_emails)" = function ]; then
356 361 local arg=to
357 362 if [[ "$prev" == @(-f|--from) ]]; then
358 363 arg=from
359 364 fi
360 365 local addresses=$(_hg_emails $arg)
361 366 COMPREPLY=(${COMPREPLY[@]:-}
362 367 $(compgen -W '$addresses' -- "$cur"))
363 368 fi
364 369 return
365 370 ;;
366 371 -m|--mbox)
367 372 # fallback to standard filename completion
368 373 return
369 374 ;;
370 375 -s|--subject)
371 376 # free form string
372 377 return
373 378 ;;
374 379 esac
375 380
376 381 _hg_tags
377 382 return
378 383 }
379 384
380 385
381 386 # gpg
382 387 _hg_cmd_sign()
383 388 {
384 389 _hg_tags
385 390 }
General Comments 0
You need to be logged in to leave comments. Login now