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