##// END OF EJS Templates
bash_completion: qguard
Alexis S. L. Carvalho -
r3486:f699d4eb default
parent child Browse files
Show More
@@ -1,442 +1,479
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 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$files' -- "$cur"))
82 82 }
83 83
84 84 _hg_tags()
85 85 {
86 86 local tags="$("$hg" tags -q 2>/dev/null)"
87 87 local IFS=$'\n'
88 88 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$tags' -- "$cur"))
89 89 }
90 90
91 91 # this is "kind of" ugly...
92 92 _hg_count_non_option()
93 93 {
94 94 local i count=0
95 95 local filters="$1"
96 96
97 97 for ((i=1; $i<=$COMP_CWORD; i++)); do
98 98 if [[ "${COMP_WORDS[i]}" != -* ]]; then
99 99 if [[ ${COMP_WORDS[i-1]} == @($filters|$global_args) ]]; then
100 100 continue
101 101 fi
102 102 count=$(($count + 1))
103 103 fi
104 104 done
105 105
106 106 echo $(($count - 1))
107 107 }
108 108
109 109 _hg()
110 110 {
111 111 local cur prev cmd cmd_index opts i
112 112 # global options that receive an argument
113 113 local global_args='--cwd|-R|--repository'
114 114 local hg="$1"
115 115 local canonical=0
116 116
117 117 COMPREPLY=()
118 118 cur="$2"
119 119 prev="$3"
120 120
121 121 # searching for the command
122 122 # (first non-option argument that doesn't follow a global option that
123 123 # receives an argument)
124 124 for ((i=1; $i<=$COMP_CWORD; i++)); do
125 125 if [[ ${COMP_WORDS[i]} != -* ]]; then
126 126 if [[ ${COMP_WORDS[i-1]} != @($global_args) ]]; then
127 127 cmd="${COMP_WORDS[i]}"
128 128 cmd_index=$i
129 129 break
130 130 fi
131 131 fi
132 132 done
133 133
134 134 if [[ "$cur" == -* ]]; then
135 135 if [ "$(type -t "_hg_opt_$cmd")" = function ] && "_hg_opt_$cmd"; then
136 136 return
137 137 fi
138 138
139 139 opts=$("$hg" debugcomplete --options "$cmd" 2>/dev/null)
140 140
141 141 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$opts' -- "$cur"))
142 142 return
143 143 fi
144 144
145 145 # global options
146 146 case "$prev" in
147 147 -R|--repository)
148 148 _hg_repos
149 149 return
150 150 ;;
151 151 --cwd)
152 152 # Stick with default bash completion
153 153 return
154 154 ;;
155 155 esac
156 156
157 157 if [ -z "$cmd" ] || [ $COMP_CWORD -eq $i ]; then
158 158 _hg_commands
159 159 return
160 160 fi
161 161
162 162 # try to generate completion candidates for whatever command the user typed
163 163 local help
164 164 if _hg_command_specific; then
165 165 return
166 166 fi
167 167
168 168 # canonicalize the command name and try again
169 169 help=$("$hg" help "$cmd" 2>/dev/null)
170 170 if [ $? -ne 0 ]; then
171 171 # Probably either the command doesn't exist or it's ambiguous
172 172 return
173 173 fi
174 174 cmd=${help#hg }
175 175 cmd=${cmd%%[$' \n']*}
176 176 canonical=1
177 177 _hg_command_specific
178 178 }
179 179
180 180 _hg_command_specific()
181 181 {
182 182 if [ "$(type -t "_hg_cmd_$cmd")" = function ]; then
183 183 "_hg_cmd_$cmd"
184 184 return 0
185 185 fi
186 186
187 187 if [ "$cmd" != status ] && [ "$prev" = -r ] || [ "$prev" == --rev ]; then
188 188 if [ $canonical = 1 ]; then
189 189 _hg_tags
190 190 return 0
191 191 elif [[ status != "$cmd"* ]]; then
192 192 _hg_tags
193 193 return 0
194 194 else
195 195 return 1
196 196 fi
197 197 fi
198 198
199 199 case "$cmd" in
200 200 help)
201 201 _hg_commands
202 202 ;;
203 203 export)
204 204 if _hg_ext_mq_patchlist qapplied && [ "${COMPREPLY[*]}" ]; then
205 205 return 0
206 206 fi
207 207 _hg_tags
208 208 ;;
209 209 manifest|update)
210 210 _hg_tags
211 211 ;;
212 212 pull|push|outgoing|incoming)
213 213 _hg_paths
214 214 _hg_repos
215 215 ;;
216 216 paths)
217 217 _hg_paths
218 218 ;;
219 219 add)
220 220 _hg_status "u"
221 221 ;;
222 222 commit)
223 223 _hg_status "mar"
224 224 ;;
225 225 remove)
226 226 _hg_status "d"
227 227 ;;
228 228 forget)
229 229 _hg_status "a"
230 230 ;;
231 231 diff)
232 232 _hg_status "mar"
233 233 ;;
234 234 revert)
235 235 _hg_status "mard"
236 236 ;;
237 237 clone)
238 238 local count=$(_hg_count_non_option)
239 239 if [ $count = 1 ]; then
240 240 _hg_paths
241 241 fi
242 242 _hg_repos
243 243 ;;
244 244 debugindex|debugindexdot)
245 245 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -f -X "!*.i" -- "$cur"))
246 246 ;;
247 247 debugdata)
248 248 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -f -X "!*.d" -- "$cur"))
249 249 ;;
250 250 *)
251 251 return 1
252 252 ;;
253 253 esac
254 254
255 255 return 0
256 256 }
257 257
258 258 complete -o bashdefault -o default -F _hg hg 2>/dev/null \
259 259 || complete -o default -F _hg hg
260 260
261 261
262 262 # Completion for commands provided by extensions
263 263
264 264 # mq
265 265 _hg_ext_mq_patchlist()
266 266 {
267 267 local patches
268 268 patches=$("$hg" $1 2>/dev/null)
269 269 if [ $? -eq 0 ] && [ "$patches" ]; then
270 270 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$patches' -- "$cur"))
271 271 return 0
272 272 fi
273 273 return 1
274 274 }
275 275
276 276 _hg_ext_mq_queues()
277 277 {
278 278 local root=$("$hg" root 2>/dev/null)
279 279 local n
280 280 for n in $(cd "$root"/.hg && compgen -d -- "$cur"); do
281 281 # I think we're usually not interested in the regular "patches" queue
282 282 # so just filter it.
283 283 if [ "$n" != patches ] && [ -e "$root/.hg/$n/series" ]; then
284 284 COMPREPLY=(${COMPREPLY[@]:-} "$n")
285 285 fi
286 286 done
287 287 }
288 288
289 289 _hg_cmd_qpop()
290 290 {
291 291 if [[ "$prev" = @(-n|--name) ]]; then
292 292 _hg_ext_mq_queues
293 293 return
294 294 fi
295 295 _hg_ext_mq_patchlist qapplied
296 296 }
297 297
298 298 _hg_cmd_qpush()
299 299 {
300 300 if [[ "$prev" = @(-n|--name) ]]; then
301 301 _hg_ext_mq_queues
302 302 return
303 303 fi
304 304 _hg_ext_mq_patchlist qunapplied
305 305 }
306 306
307 307 _hg_cmd_qdelete()
308 308 {
309 309 local qcmd=qunapplied
310 310 if [[ "$prev" = @(-r|--rev) ]]; then
311 311 qcmd=qapplied
312 312 fi
313 313 _hg_ext_mq_patchlist $qcmd
314 314 }
315 315
316 316 _hg_cmd_qsave()
317 317 {
318 318 if [[ "$prev" = @(-n|--name) ]]; then
319 319 _hg_ext_mq_queues
320 320 return
321 321 fi
322 322 }
323 323
324 324 _hg_cmd_strip()
325 325 {
326 326 _hg_tags
327 327 }
328 328
329 329 _hg_cmd_qcommit()
330 330 {
331 331 local root=$("$hg" root 2>/dev/null)
332 332 # this is run in a sub-shell, so we can't use _hg_status
333 333 local files=$(cd "$root/.hg/patches" 2>/dev/null &&
334 334 "$hg" status -nmar 2>/dev/null)
335 335 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$files' -- "$cur"))
336 336 }
337 337
338 338 _hg_cmd_qfold()
339 339 {
340 340 _hg_ext_mq_patchlist qunapplied
341 341 }
342 342
343 343 _hg_cmd_qrename()
344 344 {
345 345 _hg_ext_mq_patchlist qseries
346 346 }
347 347
348 348 _hg_cmd_qheader()
349 349 {
350 350 _hg_ext_mq_patchlist qseries
351 351 }
352 352
353 353 _hg_cmd_qclone()
354 354 {
355 355 local count=$(_hg_count_non_option)
356 356 if [ $count = 1 ]; then
357 357 _hg_paths
358 358 fi
359 359 _hg_repos
360 360 }
361 361
362 362 _hg_ext_mq_guards()
363 363 {
364 364 "$hg" qselect --series 2>/dev/null | sed -e 's/^.//'
365 365 }
366 366
367 367 _hg_cmd_qselect()
368 368 {
369 369 local guards=$(_hg_ext_mq_guards)
370 370 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$guards' -- "$cur"))
371 371 }
372 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
408 }
409
373 410
374 411 # hbisect
375 412 _hg_cmd_bisect()
376 413 {
377 414 local i subcmd
378 415
379 416 # find the sub-command
380 417 for ((i=cmd_index+1; i<=COMP_CWORD; i++)); do
381 418 if [[ ${COMP_WORDS[i]} != -* ]]; then
382 419 if [[ ${COMP_WORDS[i-1]} != @($global_args) ]]; then
383 420 subcmd="${COMP_WORDS[i]}"
384 421 break
385 422 fi
386 423 fi
387 424 done
388 425
389 426 if [ -z "$subcmd" ] || [ $COMP_CWORD -eq $i ] || [ "$subcmd" = help ]; then
390 427 COMPREPLY=(${COMPREPLY[@]:-}
391 428 $(compgen -W 'bad good help init next reset' -- "$cur"))
392 429 return
393 430 fi
394 431
395 432 case "$subcmd" in
396 433 good|bad)
397 434 _hg_tags
398 435 ;;
399 436 esac
400 437
401 438 return
402 439 }
403 440
404 441
405 442 # patchbomb
406 443 _hg_cmd_email()
407 444 {
408 445 case "$prev" in
409 446 -c|--cc|-t|--to|-f|--from|--bcc)
410 447 # we need an e-mail address. let the user provide a function
411 448 # to get them
412 449 if [ "$(type -t _hg_emails)" = function ]; then
413 450 local arg=to
414 451 if [[ "$prev" == @(-f|--from) ]]; then
415 452 arg=from
416 453 fi
417 454 local addresses=$(_hg_emails $arg)
418 455 COMPREPLY=(${COMPREPLY[@]:-}
419 456 $(compgen -W '$addresses' -- "$cur"))
420 457 fi
421 458 return
422 459 ;;
423 460 -m|--mbox)
424 461 # fallback to standard filename completion
425 462 return
426 463 ;;
427 464 -s|--subject)
428 465 # free form string
429 466 return
430 467 ;;
431 468 esac
432 469
433 470 _hg_tags
434 471 return
435 472 }
436 473
437 474
438 475 # gpg
439 476 _hg_cmd_sign()
440 477 {
441 478 _hg_tags
442 479 }
General Comments 0
You need to be logged in to leave comments. Login now