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