Mercurial > hg > mercurial-crew-with-dirclash
comparison contrib/bash_completion @ 3499:ceaa3fefc10c
Merge with crew
author | Matt Mackall <mpm@selenic.com> |
---|---|
date | Tue, 24 Oct 2006 13:46:04 -0500 |
parents | f699d4eb25d9 |
children | d86ab4ba5ae6 |
comparison
equal
deleted
inserted
replaced
3498:ff06fe0703ef | 3499:ceaa3fefc10c |
---|---|
6 # complete a command line that uses hg with all the available commands | 6 # complete a command line that uses hg with all the available commands |
7 # and options and sometimes even arguments. | 7 # and options and sometimes even arguments. |
8 # | 8 # |
9 # Mercurial allows you to define additional commands through extensions. | 9 # Mercurial allows you to define additional commands through extensions. |
10 # Bash should be able to automatically figure out the name of these new | 10 # Bash should be able to automatically figure out the name of these new |
11 # commands and their options. If you also want to tell it how to | 11 # commands and their options. See below for how to define _hg_opt_foo |
12 # complete non-option arguments, see below for how to define an | 12 # and _hg_cmd_foo functions to fine-tune the completion for option and |
13 # _hg_cmd_foo function. | 13 # non-option arguments, respectively. |
14 # | 14 # |
15 # | 15 # |
16 # Notes about completion for specific commands: | 16 # Notes about completion for specific commands: |
17 # | 17 # |
18 # - the completion function for the email command from the patchbomb | 18 # - the completion function for the email command from the patchbomb |
32 # | 32 # |
33 # Writing completion functions for additional commands: | 33 # Writing completion functions for additional commands: |
34 # | 34 # |
35 # If it exists, the function _hg_cmd_foo will be called without | 35 # If it exists, the function _hg_cmd_foo will be called without |
36 # arguments to generate the completion candidates for the hg command | 36 # arguments to generate the completion candidates for the hg command |
37 # "foo". | 37 # "foo". If the command receives some arguments that aren't options |
38 # even though they start with a "-", you can define a function called | |
39 # _hg_opt_foo to generate the completion candidates. If _hg_opt_foo | |
40 # doesn't return 0, regular completion for options is attempted. | |
38 # | 41 # |
39 # In addition to the regular completion variables provided by bash, | 42 # In addition to the regular completion variables provided by bash, |
40 # the following variables are also set: | 43 # the following variables are also set: |
41 # - $hg - the hg program being used (e.g. /usr/bin/hg) | 44 # - $hg - the hg program being used (e.g. /usr/bin/hg) |
42 # - $cmd - the name of the hg command being completed | 45 # - $cmd - the name of the hg command being completed |
107 { | 110 { |
108 local cur prev cmd cmd_index opts i | 111 local cur prev cmd cmd_index opts i |
109 # global options that receive an argument | 112 # global options that receive an argument |
110 local global_args='--cwd|-R|--repository' | 113 local global_args='--cwd|-R|--repository' |
111 local hg="$1" | 114 local hg="$1" |
115 local canonical=0 | |
112 | 116 |
113 COMPREPLY=() | 117 COMPREPLY=() |
114 cur="$2" | 118 cur="$2" |
115 prev="$3" | 119 prev="$3" |
116 | 120 |
126 fi | 130 fi |
127 fi | 131 fi |
128 done | 132 done |
129 | 133 |
130 if [[ "$cur" == -* ]]; then | 134 if [[ "$cur" == -* ]]; then |
135 if [ "$(type -t "_hg_opt_$cmd")" = function ] && "_hg_opt_$cmd"; then | |
136 return | |
137 fi | |
138 | |
131 opts=$("$hg" debugcomplete --options "$cmd" 2>/dev/null) | 139 opts=$("$hg" debugcomplete --options "$cmd" 2>/dev/null) |
132 | 140 |
133 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$opts' -- "$cur")) | 141 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$opts' -- "$cur")) |
134 return | 142 return |
135 fi | 143 fi |
151 return | 159 return |
152 fi | 160 fi |
153 | 161 |
154 # try to generate completion candidates for whatever command the user typed | 162 # try to generate completion candidates for whatever command the user typed |
155 local help | 163 local help |
156 local canonical=0 | |
157 if _hg_command_specific; then | 164 if _hg_command_specific; then |
158 return | 165 return |
159 fi | 166 fi |
160 | 167 |
161 # canonicalize the command name and try again | 168 # canonicalize the command name and try again |
191 | 198 |
192 case "$cmd" in | 199 case "$cmd" in |
193 help) | 200 help) |
194 _hg_commands | 201 _hg_commands |
195 ;; | 202 ;; |
196 export|manifest|update) | 203 export) |
204 if _hg_ext_mq_patchlist qapplied && [ "${COMPREPLY[*]}" ]; then | |
205 return 0 | |
206 fi | |
207 _hg_tags | |
208 ;; | |
209 manifest|update) | |
197 _hg_tags | 210 _hg_tags |
198 ;; | 211 ;; |
199 pull|push|outgoing|incoming) | 212 pull|push|outgoing|incoming) |
200 _hg_paths | 213 _hg_paths |
201 _hg_repos | 214 _hg_repos |
249 # Completion for commands provided by extensions | 262 # Completion for commands provided by extensions |
250 | 263 |
251 # mq | 264 # mq |
252 _hg_ext_mq_patchlist() | 265 _hg_ext_mq_patchlist() |
253 { | 266 { |
254 local patches=$("$hg" $1 2>/dev/null) | 267 local patches |
255 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$patches' -- "$cur")) | 268 patches=$("$hg" $1 2>/dev/null) |
269 if [ $? -eq 0 ] && [ "$patches" ]; then | |
270 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$patches' -- "$cur")) | |
271 return 0 | |
272 fi | |
273 return 1 | |
256 } | 274 } |
257 | 275 |
258 _hg_ext_mq_queues() | 276 _hg_ext_mq_queues() |
259 { | 277 { |
260 local root=$("$hg" root 2>/dev/null) | 278 local root=$("$hg" root 2>/dev/null) |
286 _hg_ext_mq_patchlist qunapplied | 304 _hg_ext_mq_patchlist qunapplied |
287 } | 305 } |
288 | 306 |
289 _hg_cmd_qdelete() | 307 _hg_cmd_qdelete() |
290 { | 308 { |
291 _hg_ext_mq_patchlist qunapplied | 309 local qcmd=qunapplied |
310 if [[ "$prev" = @(-r|--rev) ]]; then | |
311 qcmd=qapplied | |
312 fi | |
313 _hg_ext_mq_patchlist $qcmd | |
292 } | 314 } |
293 | 315 |
294 _hg_cmd_qsave() | 316 _hg_cmd_qsave() |
295 { | 317 { |
296 if [[ "$prev" = @(-n|--name) ]]; then | 318 if [[ "$prev" = @(-n|--name) ]]; then |
311 local files=$(cd "$root/.hg/patches" 2>/dev/null && | 333 local files=$(cd "$root/.hg/patches" 2>/dev/null && |
312 "$hg" status -nmar 2>/dev/null) | 334 "$hg" status -nmar 2>/dev/null) |
313 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$files' -- "$cur")) | 335 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$files' -- "$cur")) |
314 } | 336 } |
315 | 337 |
316 _hg_cmd_export() | 338 _hg_cmd_qfold() |
317 { | 339 { |
318 _hg_ext_mq_patchlist qapplied | 340 _hg_ext_mq_patchlist qunapplied |
341 } | |
342 | |
343 _hg_cmd_qrename() | |
344 { | |
345 _hg_ext_mq_patchlist qseries | |
346 } | |
347 | |
348 _hg_cmd_qheader() | |
349 { | |
350 _hg_ext_mq_patchlist qseries | |
351 } | |
352 | |
353 _hg_cmd_qclone() | |
354 { | |
355 local count=$(_hg_count_non_option) | |
356 if [ $count = 1 ]; then | |
357 _hg_paths | |
358 fi | |
359 _hg_repos | |
360 } | |
361 | |
362 _hg_ext_mq_guards() | |
363 { | |
364 "$hg" qselect --series 2>/dev/null | sed -e 's/^.//' | |
365 } | |
366 | |
367 _hg_cmd_qselect() | |
368 { | |
369 local guards=$(_hg_ext_mq_guards) | |
370 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$guards' -- "$cur")) | |
371 } | |
372 | |
373 _hg_cmd_qguard() | |
374 { | |
375 local prefix='' | |
376 | |
377 if [[ "$cur" == +* ]]; then | |
378 prefix=+ | |
379 elif [[ "$cur" == -* ]]; then | |
380 prefix=- | |
381 fi | |
382 local ncur=${cur#[-+]} | |
383 | |
384 if ! [ "$prefix" ]; then | |
385 _hg_ext_mq_patchlist qseries | |
386 return | |
387 fi | |
388 | |
389 local guards=$(_hg_ext_mq_guards) | |
390 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -P $prefix -W '$guards' -- "$ncur")) | |
391 } | |
392 | |
393 _hg_opt_qguard() | |
394 { | |
395 local i | |
396 for ((i=cmd_index+1; i<=COMP_CWORD; i++)); do | |
397 if [[ ${COMP_WORDS[i]} != -* ]]; then | |
398 if [[ ${COMP_WORDS[i-1]} != @($global_args) ]]; then | |
399 _hg_cmd_qguard | |
400 return 0 | |
401 fi | |
402 elif [ "${COMP_WORDS[i]}" = -- ]; then | |
403 _hg_cmd_qguard | |
404 return 0 | |
405 fi | |
406 done | |
407 return 1 | |
319 } | 408 } |
320 | 409 |
321 | 410 |
322 # hbisect | 411 # hbisect |
323 _hg_cmd_bisect() | 412 _hg_cmd_bisect() |
352 | 441 |
353 # patchbomb | 442 # patchbomb |
354 _hg_cmd_email() | 443 _hg_cmd_email() |
355 { | 444 { |
356 case "$prev" in | 445 case "$prev" in |
357 -c|--cc|-t|--to|-f|--from) | 446 -c|--cc|-t|--to|-f|--from|--bcc) |
358 # we need an e-mail address. let the user provide a function | 447 # we need an e-mail address. let the user provide a function |
359 # to get them | 448 # to get them |
360 if [ "$(type -t _hg_emails)" = function ]; then | 449 if [ "$(type -t _hg_emails)" = function ]; then |
361 local arg=to | 450 local arg=to |
362 if [[ "$prev" == @(-f|--from) ]]; then | 451 if [[ "$prev" == @(-f|--from) ]]; then |