##// END OF EJS Templates
Add missing bash_completion for qfinish
Thomas Arendsen Hein -
r9840:18d5935c default
parent child Browse files
Show More
@@ -1,565 +1,573 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. See below for how to define _hg_opt_foo
12 12 # and _hg_cmd_foo functions to fine-tune the completion for option and
13 13 # non-option arguments, respectively.
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". If the command receives some arguments that aren't options
38 38 # even though they start with a "-", you can define a function called
39 39 # _hg_opt_foo to generate the completion candidates. If _hg_opt_foo
40 40 # doesn't return 0, regular completion for options is attempted.
41 41 #
42 42 # In addition to the regular completion variables provided by bash,
43 43 # the following variables are also set:
44 44 # - $hg - the hg program being used (e.g. /usr/bin/hg)
45 45 # - $cmd - the name of the hg command being completed
46 46 # - $cmd_index - the index of $cmd in $COMP_WORDS
47 47 # - $cur - the current argument being completed
48 48 # - $prev - the argument before $cur
49 49 # - $global_args - "|"-separated list of global options that accept
50 50 # an argument (e.g. '--cwd|-R|--repository')
51 51 # - $canonical - 1 if we canonicalized $cmd before calling the function
52 52 # 0 otherwise
53 53 #
54 54
55 55 shopt -s extglob
56 56
57 57 _hg_commands()
58 58 {
59 59 local commands
60 60 commands="$("$hg" debugcomplete "$cur" 2>/dev/null)" || commands=""
61 61 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$commands' -- "$cur"))
62 62 }
63 63
64 64 _hg_paths()
65 65 {
66 66 local paths="$("$hg" paths 2>/dev/null | sed -e 's/ = .*$//')"
67 67 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$paths' -- "$cur"))
68 68 }
69 69
70 70 _hg_repos()
71 71 {
72 72 local i
73 73 for i in $(compgen -d -- "$cur"); do
74 74 test ! -d "$i"/.hg || COMPREPLY=(${COMPREPLY[@]:-} "$i")
75 75 done
76 76 }
77 77
78 78 _hg_status()
79 79 {
80 80 local files="$("$hg" status -n$1 . 2>/dev/null)"
81 81 local IFS=$'\n'
82 82 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$files' -- "$cur"))
83 83 }
84 84
85 85 _hg_tags()
86 86 {
87 87 local tags="$("$hg" tags -q 2>/dev/null)"
88 88 local IFS=$'\n'
89 89 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$tags' -- "$cur"))
90 90 }
91 91
92 92 _hg_branches()
93 93 {
94 94 local branches="$("$hg" branches -q 2>/dev/null)"
95 95 local IFS=$'\n'
96 96 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$branches' -- "$cur"))
97 97 }
98 98
99 99 # this is "kind of" ugly...
100 100 _hg_count_non_option()
101 101 {
102 102 local i count=0
103 103 local filters="$1"
104 104
105 105 for ((i=1; $i<=$COMP_CWORD; i++)); do
106 106 if [[ "${COMP_WORDS[i]}" != -* ]]; then
107 107 if [[ ${COMP_WORDS[i-1]} == @($filters|$global_args) ]]; then
108 108 continue
109 109 fi
110 110 count=$(($count + 1))
111 111 fi
112 112 done
113 113
114 114 echo $(($count - 1))
115 115 }
116 116
117 117 _hg()
118 118 {
119 119 local cur prev cmd cmd_index opts i
120 120 # global options that receive an argument
121 121 local global_args='--cwd|-R|--repository'
122 122 local hg="$1"
123 123 local canonical=0
124 124
125 125 COMPREPLY=()
126 126 cur="$2"
127 127 prev="$3"
128 128
129 129 # searching for the command
130 130 # (first non-option argument that doesn't follow a global option that
131 131 # receives an argument)
132 132 for ((i=1; $i<=$COMP_CWORD; i++)); do
133 133 if [[ ${COMP_WORDS[i]} != -* ]]; then
134 134 if [[ ${COMP_WORDS[i-1]} != @($global_args) ]]; then
135 135 cmd="${COMP_WORDS[i]}"
136 136 cmd_index=$i
137 137 break
138 138 fi
139 139 fi
140 140 done
141 141
142 142 if [[ "$cur" == -* ]]; then
143 143 if [ "$(type -t "_hg_opt_$cmd")" = function ] && "_hg_opt_$cmd"; then
144 144 return
145 145 fi
146 146
147 147 opts=$("$hg" debugcomplete --options "$cmd" 2>/dev/null)
148 148
149 149 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$opts' -- "$cur"))
150 150 return
151 151 fi
152 152
153 153 # global options
154 154 case "$prev" in
155 155 -R|--repository)
156 156 _hg_paths
157 157 _hg_repos
158 158 return
159 159 ;;
160 160 --cwd)
161 161 # Stick with default bash completion
162 162 return
163 163 ;;
164 164 esac
165 165
166 166 if [ -z "$cmd" ] || [ $COMP_CWORD -eq $i ]; then
167 167 _hg_commands
168 168 return
169 169 fi
170 170
171 171 # try to generate completion candidates for whatever command the user typed
172 172 local help
173 173 if _hg_command_specific; then
174 174 return
175 175 fi
176 176
177 177 # canonicalize the command name and try again
178 178 help=$("$hg" help "$cmd" 2>/dev/null)
179 179 if [ $? -ne 0 ]; then
180 180 # Probably either the command doesn't exist or it's ambiguous
181 181 return
182 182 fi
183 183 cmd=${help#hg }
184 184 cmd=${cmd%%[$' \n']*}
185 185 canonical=1
186 186 _hg_command_specific
187 187 }
188 188
189 189 _hg_command_specific()
190 190 {
191 191 if [ "$(type -t "_hg_cmd_$cmd")" = function ]; then
192 192 "_hg_cmd_$cmd"
193 193 return 0
194 194 fi
195 195
196 196 if [ "$cmd" != status ] && [ "$prev" = -r ] || [ "$prev" == --rev ]; then
197 197 if [ $canonical = 1 ]; then
198 198 _hg_tags
199 199 _hg_branches
200 200 return 0
201 201 elif [[ status != "$cmd"* ]]; then
202 202 _hg_tags
203 203 _hg_branches
204 204 return 0
205 205 else
206 206 return 1
207 207 fi
208 208 fi
209 209
210 210 case "$cmd" in
211 211 help)
212 212 _hg_commands
213 213 ;;
214 214 export)
215 215 if _hg_ext_mq_patchlist qapplied && [ "${COMPREPLY[*]}" ]; then
216 216 return 0
217 217 fi
218 218 _hg_tags
219 219 _hg_branches
220 220 ;;
221 221 manifest|update)
222 222 _hg_tags
223 223 _hg_branches
224 224 ;;
225 225 pull|push|outgoing|incoming)
226 226 _hg_paths
227 227 _hg_repos
228 228 ;;
229 229 paths)
230 230 _hg_paths
231 231 ;;
232 232 add)
233 233 _hg_status "u"
234 234 ;;
235 235 merge)
236 236 _hg_tags
237 237 _hg_branches
238 238 ;;
239 239 commit)
240 240 _hg_status "mar"
241 241 ;;
242 242 remove)
243 243 _hg_status "d"
244 244 ;;
245 245 forget)
246 246 _hg_status "a"
247 247 ;;
248 248 diff)
249 249 _hg_status "mar"
250 250 ;;
251 251 revert)
252 252 _hg_status "mard"
253 253 ;;
254 254 clone)
255 255 local count=$(_hg_count_non_option)
256 256 if [ $count = 1 ]; then
257 257 _hg_paths
258 258 fi
259 259 _hg_repos
260 260 ;;
261 261 debugindex|debugindexdot)
262 262 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -f -X "!*.i" -- "$cur"))
263 263 ;;
264 264 debugdata)
265 265 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -f -X "!*.d" -- "$cur"))
266 266 ;;
267 267 *)
268 268 return 1
269 269 ;;
270 270 esac
271 271
272 272 return 0
273 273 }
274 274
275 275 complete -o bashdefault -o default -F _hg hg 2>/dev/null \
276 276 || complete -o default -F _hg hg
277 277
278 278
279 279 # Completion for commands provided by extensions
280 280
281 281 # bookmarks
282 282 _hg_bookmarks()
283 283 {
284 284 local bookmarks="$("$hg" bookmarks --quiet 2>/dev/null )"
285 285 local IFS=$'\n'
286 286 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$bookmarks' -- "$cur"))
287 287 }
288 288
289 289 _hg_cmd_bookmarks()
290 290 {
291 291 if [[ "$prev" = @(-d|--delete|-m|--rename) ]]; then
292 292 _hg_bookmarks
293 293 return
294 294 fi
295 295 }
296 296
297 297 # mq
298 298 _hg_ext_mq_patchlist()
299 299 {
300 300 local patches
301 301 patches=$("$hg" $1 2>/dev/null)
302 302 if [ $? -eq 0 ] && [ "$patches" ]; then
303 303 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$patches' -- "$cur"))
304 304 return 0
305 305 fi
306 306 return 1
307 307 }
308 308
309 309 _hg_ext_mq_queues()
310 310 {
311 311 local root=$("$hg" root 2>/dev/null)
312 312 local n
313 313 for n in $(cd "$root"/.hg && compgen -d -- "$cur"); do
314 314 # I think we're usually not interested in the regular "patches" queue
315 315 # so just filter it.
316 316 if [ "$n" != patches ] && [ -e "$root/.hg/$n/series" ]; then
317 317 COMPREPLY=(${COMPREPLY[@]:-} "$n")
318 318 fi
319 319 done
320 320 }
321 321
322 322 _hg_cmd_qpop()
323 323 {
324 324 if [[ "$prev" = @(-n|--name) ]]; then
325 325 _hg_ext_mq_queues
326 326 return
327 327 fi
328 328 _hg_ext_mq_patchlist qapplied
329 329 }
330 330
331 331 _hg_cmd_qpush()
332 332 {
333 333 if [[ "$prev" = @(-n|--name) ]]; then
334 334 _hg_ext_mq_queues
335 335 return
336 336 fi
337 337 _hg_ext_mq_patchlist qunapplied
338 338 }
339 339
340 340 _hg_cmd_qgoto()
341 341 {
342 342 if [[ "$prev" = @(-n|--name) ]]; then
343 343 _hg_ext_mq_queues
344 344 return
345 345 fi
346 346 _hg_ext_mq_patchlist qseries
347 347 }
348 348
349 349 _hg_cmd_qdelete()
350 350 {
351 351 local qcmd=qunapplied
352 352 if [[ "$prev" = @(-r|--rev) ]]; then
353 353 qcmd=qapplied
354 354 fi
355 355 _hg_ext_mq_patchlist $qcmd
356 356 }
357 357
358 _hg_cmd_qfinish()
359 {
360 if [[ "$prev" = @(-a|--applied) ]]; then
361 return
362 fi
363 _hg_ext_mq_patchlist qapplied
364 }
365
358 366 _hg_cmd_qsave()
359 367 {
360 368 if [[ "$prev" = @(-n|--name) ]]; then
361 369 _hg_ext_mq_queues
362 370 return
363 371 fi
364 372 }
365 373
366 374 _hg_cmd_strip()
367 375 {
368 376 _hg_tags
369 377 _hg_branches
370 378 }
371 379
372 380 _hg_cmd_qcommit()
373 381 {
374 382 local root=$("$hg" root 2>/dev/null)
375 383 # this is run in a sub-shell, so we can't use _hg_status
376 384 local files=$(cd "$root/.hg/patches" 2>/dev/null &&
377 385 "$hg" status -nmar 2>/dev/null)
378 386 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$files' -- "$cur"))
379 387 }
380 388
381 389 _hg_cmd_qfold()
382 390 {
383 391 _hg_ext_mq_patchlist qunapplied
384 392 }
385 393
386 394 _hg_cmd_qrename()
387 395 {
388 396 _hg_ext_mq_patchlist qseries
389 397 }
390 398
391 399 _hg_cmd_qheader()
392 400 {
393 401 _hg_ext_mq_patchlist qseries
394 402 }
395 403
396 404 _hg_cmd_qclone()
397 405 {
398 406 local count=$(_hg_count_non_option)
399 407 if [ $count = 1 ]; then
400 408 _hg_paths
401 409 fi
402 410 _hg_repos
403 411 }
404 412
405 413 _hg_ext_mq_guards()
406 414 {
407 415 "$hg" qselect --series 2>/dev/null | sed -e 's/^.//'
408 416 }
409 417
410 418 _hg_cmd_qselect()
411 419 {
412 420 local guards=$(_hg_ext_mq_guards)
413 421 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$guards' -- "$cur"))
414 422 }
415 423
416 424 _hg_cmd_qguard()
417 425 {
418 426 local prefix=''
419 427
420 428 if [[ "$cur" == +* ]]; then
421 429 prefix=+
422 430 elif [[ "$cur" == -* ]]; then
423 431 prefix=-
424 432 fi
425 433 local ncur=${cur#[-+]}
426 434
427 435 if ! [ "$prefix" ]; then
428 436 _hg_ext_mq_patchlist qseries
429 437 return
430 438 fi
431 439
432 440 local guards=$(_hg_ext_mq_guards)
433 441 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -P $prefix -W '$guards' -- "$ncur"))
434 442 }
435 443
436 444 _hg_opt_qguard()
437 445 {
438 446 local i
439 447 for ((i=cmd_index+1; i<=COMP_CWORD; i++)); do
440 448 if [[ ${COMP_WORDS[i]} != -* ]]; then
441 449 if [[ ${COMP_WORDS[i-1]} != @($global_args) ]]; then
442 450 _hg_cmd_qguard
443 451 return 0
444 452 fi
445 453 elif [ "${COMP_WORDS[i]}" = -- ]; then
446 454 _hg_cmd_qguard
447 455 return 0
448 456 fi
449 457 done
450 458 return 1
451 459 }
452 460
453 461
454 462 # hbisect
455 463 _hg_cmd_bisect()
456 464 {
457 465 local i subcmd
458 466
459 467 # find the sub-command
460 468 for ((i=cmd_index+1; i<=COMP_CWORD; i++)); do
461 469 if [[ ${COMP_WORDS[i]} != -* ]]; then
462 470 if [[ ${COMP_WORDS[i-1]} != @($global_args) ]]; then
463 471 subcmd="${COMP_WORDS[i]}"
464 472 break
465 473 fi
466 474 fi
467 475 done
468 476
469 477 if [ -z "$subcmd" ] || [ $COMP_CWORD -eq $i ] || [ "$subcmd" = help ]; then
470 478 COMPREPLY=(${COMPREPLY[@]:-}
471 479 $(compgen -W 'bad good help init next reset' -- "$cur"))
472 480 return
473 481 fi
474 482
475 483 case "$subcmd" in
476 484 good|bad)
477 485 _hg_tags
478 486 _hg_branches
479 487 ;;
480 488 esac
481 489
482 490 return
483 491 }
484 492
485 493
486 494 # patchbomb
487 495 _hg_cmd_email()
488 496 {
489 497 case "$prev" in
490 498 -c|--cc|-t|--to|-f|--from|--bcc)
491 499 # we need an e-mail address. let the user provide a function
492 500 # to get them
493 501 if [ "$(type -t _hg_emails)" = function ]; then
494 502 local arg=to
495 503 if [[ "$prev" == @(-f|--from) ]]; then
496 504 arg=from
497 505 fi
498 506 local addresses=$(_hg_emails $arg)
499 507 COMPREPLY=(${COMPREPLY[@]:-}
500 508 $(compgen -W '$addresses' -- "$cur"))
501 509 fi
502 510 return
503 511 ;;
504 512 -m|--mbox)
505 513 # fallback to standard filename completion
506 514 return
507 515 ;;
508 516 -s|--subject)
509 517 # free form string
510 518 return
511 519 ;;
512 520 esac
513 521
514 522 _hg_tags
515 523 _hg_branches
516 524 return
517 525 }
518 526
519 527
520 528 # gpg
521 529 _hg_cmd_sign()
522 530 {
523 531 _hg_tags
524 532 _hg_branches
525 533 }
526 534
527 535
528 536 # transplant
529 537 _hg_cmd_transplant()
530 538 {
531 539 case "$prev" in
532 540 -s|--source)
533 541 _hg_paths
534 542 _hg_repos
535 543 return
536 544 ;;
537 545 --filter)
538 546 # standard filename completion
539 547 return
540 548 ;;
541 549 esac
542 550
543 551 # all other transplant options values and command parameters are revisions
544 552 _hg_tags
545 553 _hg_branches
546 554 return
547 555 }
548 556
549 557 # shelve
550 558 _hg_shelves()
551 559 {
552 560 local shelves="$("$hg" unshelve -l . 2>/dev/null)"
553 561 local IFS=$'\n'
554 562 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$shelves' -- "$cur"))
555 563 }
556 564
557 565 _hg_cmd_shelve()
558 566 {
559 567 _hg_status "mard"
560 568 }
561 569
562 570 _hg_cmd_unshelve()
563 571 {
564 572 _hg_shelves
565 573 }
General Comments 0
You need to be logged in to leave comments. Login now