##// END OF EJS Templates
Improve bash_completion for patches in MQ
"Mathieu Clabaut " -
r2695:c995d683 default
parent child Browse files
Show More
@@ -1,385 +1,390 b''
1 # bash completion for the Mercurial distributed SCM
1 # bash completion for the Mercurial distributed SCM
2
2
3 # Docs:
3 # Docs:
4 #
4 #
5 # If you source this file from your .bashrc, bash should be able to
5 # If you source this file from your .bashrc, bash should be able to
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. If you also want to tell it how to
12 # complete non-option arguments, see below for how to define an
12 # complete non-option arguments, see below for how to define an
13 # _hg_cmd_foo function.
13 # _hg_cmd_foo function.
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
19 # extension will try to call _hg_emails to get a list of e-mail
19 # extension will try to call _hg_emails to get a list of e-mail
20 # addresses. It's up to the user to define this function. For
20 # addresses. It's up to the user to define this function. For
21 # example, put the addresses of the lists that you usually patchbomb
21 # example, put the addresses of the lists that you usually patchbomb
22 # in ~/.patchbomb-to and the addresses that you usually use to send
22 # in ~/.patchbomb-to and the addresses that you usually use to send
23 # the patchbombs in ~/.patchbomb-from and use something like this:
23 # the patchbombs in ~/.patchbomb-from and use something like this:
24 #
24 #
25 # _hg_emails()
25 # _hg_emails()
26 # {
26 # {
27 # if [ -r ~/.patchbomb-$1 ]; then
27 # if [ -r ~/.patchbomb-$1 ]; then
28 # cat ~/.patchbomb-$1
28 # cat ~/.patchbomb-$1
29 # fi
29 # fi
30 # }
30 # }
31 #
31 #
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".
38 #
38 #
39 # In addition to the regular completion variables provided by bash,
39 # In addition to the regular completion variables provided by bash,
40 # the following variables are also set:
40 # the following variables are also set:
41 # - $hg - the hg program being used (e.g. /usr/bin/hg)
41 # - $hg - the hg program being used (e.g. /usr/bin/hg)
42 # - $cmd - the name of the hg command being completed
42 # - $cmd - the name of the hg command being completed
43 # - $cmd_index - the index of $cmd in $COMP_WORDS
43 # - $cmd_index - the index of $cmd in $COMP_WORDS
44 # - $cur - the current argument being completed
44 # - $cur - the current argument being completed
45 # - $prev - the argument before $cur
45 # - $prev - the argument before $cur
46 # - $global_args - "|"-separated list of global options that accept
46 # - $global_args - "|"-separated list of global options that accept
47 # an argument (e.g. '--cwd|-R|--repository')
47 # an argument (e.g. '--cwd|-R|--repository')
48 # - $canonical - 1 if we canonicalized $cmd before calling the function
48 # - $canonical - 1 if we canonicalized $cmd before calling the function
49 # 0 otherwise
49 # 0 otherwise
50 #
50 #
51
51
52 shopt -s extglob
52 shopt -s extglob
53
53
54 _hg_commands()
54 _hg_commands()
55 {
55 {
56 local commands
56 local commands
57 commands="$("$hg" debugcomplete "$cur" 2>/dev/null)" || commands=""
57 commands="$("$hg" debugcomplete "$cur" 2>/dev/null)" || commands=""
58 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$commands' -- "$cur"))
58 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$commands' -- "$cur"))
59 }
59 }
60
60
61 _hg_paths()
61 _hg_paths()
62 {
62 {
63 local paths="$("$hg" paths 2>/dev/null | sed -e 's/ = .*$//')"
63 local paths="$("$hg" paths 2>/dev/null | sed -e 's/ = .*$//')"
64 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$paths' -- "$cur"))
64 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$paths' -- "$cur"))
65 }
65 }
66
66
67 _hg_repos()
67 _hg_repos()
68 {
68 {
69 local i
69 local i
70 for i in $(compgen -d -- "$cur"); do
70 for i in $(compgen -d -- "$cur"); do
71 test ! -d "$i"/.hg || COMPREPLY=(${COMPREPLY[@]:-} "$i")
71 test ! -d "$i"/.hg || COMPREPLY=(${COMPREPLY[@]:-} "$i")
72 done
72 done
73 }
73 }
74
74
75 _hg_status()
75 _hg_status()
76 {
76 {
77 local files="$("$hg" status -n$1 . 2>/dev/null)"
77 local files="$("$hg" status -n$1 . 2>/dev/null)"
78 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$files' -- "$cur"))
78 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$files' -- "$cur"))
79 }
79 }
80
80
81 _hg_tags()
81 _hg_tags()
82 {
82 {
83 local tags="$("$hg" tags -q 2>/dev/null)"
83 local tags="$("$hg" tags -q 2>/dev/null)"
84 local IFS=$'\n'
84 local IFS=$'\n'
85 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$tags' -- "$cur"))
85 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$tags' -- "$cur"))
86 }
86 }
87
87
88 # this is "kind of" ugly...
88 # this is "kind of" ugly...
89 _hg_count_non_option()
89 _hg_count_non_option()
90 {
90 {
91 local i count=0
91 local i count=0
92 local filters="$1"
92 local filters="$1"
93
93
94 for ((i=1; $i<=$COMP_CWORD; i++)); do
94 for ((i=1; $i<=$COMP_CWORD; i++)); do
95 if [[ "${COMP_WORDS[i]}" != -* ]]; then
95 if [[ "${COMP_WORDS[i]}" != -* ]]; then
96 if [[ ${COMP_WORDS[i-1]} == @($filters|$global_args) ]]; then
96 if [[ ${COMP_WORDS[i-1]} == @($filters|$global_args) ]]; then
97 continue
97 continue
98 fi
98 fi
99 count=$(($count + 1))
99 count=$(($count + 1))
100 fi
100 fi
101 done
101 done
102
102
103 echo $(($count - 1))
103 echo $(($count - 1))
104 }
104 }
105
105
106 _hg()
106 _hg()
107 {
107 {
108 local cur prev cmd cmd_index opts i
108 local cur prev cmd cmd_index opts i
109 # global options that receive an argument
109 # global options that receive an argument
110 local global_args='--cwd|-R|--repository'
110 local global_args='--cwd|-R|--repository'
111 local hg="$1"
111 local hg="$1"
112
112
113 COMPREPLY=()
113 COMPREPLY=()
114 cur="$2"
114 cur="$2"
115 prev="$3"
115 prev="$3"
116
116
117 # searching for the command
117 # searching for the command
118 # (first non-option argument that doesn't follow a global option that
118 # (first non-option argument that doesn't follow a global option that
119 # receives an argument)
119 # receives an argument)
120 for ((i=1; $i<=$COMP_CWORD; i++)); do
120 for ((i=1; $i<=$COMP_CWORD; i++)); do
121 if [[ ${COMP_WORDS[i]} != -* ]]; then
121 if [[ ${COMP_WORDS[i]} != -* ]]; then
122 if [[ ${COMP_WORDS[i-1]} != @($global_args) ]]; then
122 if [[ ${COMP_WORDS[i-1]} != @($global_args) ]]; then
123 cmd="${COMP_WORDS[i]}"
123 cmd="${COMP_WORDS[i]}"
124 cmd_index=$i
124 cmd_index=$i
125 break
125 break
126 fi
126 fi
127 fi
127 fi
128 done
128 done
129
129
130 if [[ "$cur" == -* ]]; then
130 if [[ "$cur" == -* ]]; then
131 opts=$("$hg" debugcomplete --options "$cmd" 2>/dev/null)
131 opts=$("$hg" debugcomplete --options "$cmd" 2>/dev/null)
132
132
133 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$opts' -- "$cur"))
133 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$opts' -- "$cur"))
134 return
134 return
135 fi
135 fi
136
136
137 # global options
137 # global options
138 case "$prev" in
138 case "$prev" in
139 -R|--repository)
139 -R|--repository)
140 _hg_repos
140 _hg_repos
141 return
141 return
142 ;;
142 ;;
143 --cwd)
143 --cwd)
144 # Stick with default bash completion
144 # Stick with default bash completion
145 return
145 return
146 ;;
146 ;;
147 esac
147 esac
148
148
149 if [ -z "$cmd" ] || [ $COMP_CWORD -eq $i ]; then
149 if [ -z "$cmd" ] || [ $COMP_CWORD -eq $i ]; then
150 _hg_commands
150 _hg_commands
151 return
151 return
152 fi
152 fi
153
153
154 # try to generate completion candidates for whatever command the user typed
154 # try to generate completion candidates for whatever command the user typed
155 local help
155 local help
156 local canonical=0
156 local canonical=0
157 if _hg_command_specific; then
157 if _hg_command_specific; then
158 return
158 return
159 fi
159 fi
160
160
161 # canonicalize the command name and try again
161 # canonicalize the command name and try again
162 help=$("$hg" help "$cmd" 2>/dev/null)
162 help=$("$hg" help "$cmd" 2>/dev/null)
163 if [ $? -ne 0 ]; then
163 if [ $? -ne 0 ]; then
164 # Probably either the command doesn't exist or it's ambiguous
164 # Probably either the command doesn't exist or it's ambiguous
165 return
165 return
166 fi
166 fi
167 cmd=${help#hg }
167 cmd=${help#hg }
168 cmd=${cmd%%[$' \n']*}
168 cmd=${cmd%%[$' \n']*}
169 canonical=1
169 canonical=1
170 _hg_command_specific
170 _hg_command_specific
171 }
171 }
172
172
173 _hg_command_specific()
173 _hg_command_specific()
174 {
174 {
175 if [ "$(type -t "_hg_cmd_$cmd")" = function ]; then
175 if [ "$(type -t "_hg_cmd_$cmd")" = function ]; then
176 "_hg_cmd_$cmd"
176 "_hg_cmd_$cmd"
177 return 0
177 return 0
178 fi
178 fi
179
179
180 if [ "$cmd" != status ] && [ "$prev" = -r ] || [ "$prev" == --rev ]; then
180 if [ "$cmd" != status ] && [ "$prev" = -r ] || [ "$prev" == --rev ]; then
181 if [ $canonical = 1 ]; then
181 if [ $canonical = 1 ]; then
182 _hg_tags
182 _hg_tags
183 return 0
183 return 0
184 elif [[ status != "$cmd"* ]]; then
184 elif [[ status != "$cmd"* ]]; then
185 _hg_tags
185 _hg_tags
186 return 0
186 return 0
187 else
187 else
188 return 1
188 return 1
189 fi
189 fi
190 fi
190 fi
191
191
192 case "$cmd" in
192 case "$cmd" in
193 help)
193 help)
194 _hg_commands
194 _hg_commands
195 ;;
195 ;;
196 export|manifest|update)
196 export|manifest|update)
197 _hg_tags
197 _hg_tags
198 ;;
198 ;;
199 pull|push|outgoing|incoming)
199 pull|push|outgoing|incoming)
200 _hg_paths
200 _hg_paths
201 _hg_repos
201 _hg_repos
202 ;;
202 ;;
203 paths)
203 paths)
204 _hg_paths
204 _hg_paths
205 ;;
205 ;;
206 add)
206 add)
207 _hg_status "u"
207 _hg_status "u"
208 ;;
208 ;;
209 commit)
209 commit)
210 _hg_status "mar"
210 _hg_status "mar"
211 ;;
211 ;;
212 remove)
212 remove)
213 _hg_status "d"
213 _hg_status "d"
214 ;;
214 ;;
215 forget)
215 forget)
216 _hg_status "a"
216 _hg_status "a"
217 ;;
217 ;;
218 diff)
218 diff)
219 _hg_status "mar"
219 _hg_status "mar"
220 ;;
220 ;;
221 revert)
221 revert)
222 _hg_status "mard"
222 _hg_status "mard"
223 ;;
223 ;;
224 clone)
224 clone)
225 local count=$(_hg_count_non_option)
225 local count=$(_hg_count_non_option)
226 if [ $count = 1 ]; then
226 if [ $count = 1 ]; then
227 _hg_paths
227 _hg_paths
228 fi
228 fi
229 _hg_repos
229 _hg_repos
230 ;;
230 ;;
231 debugindex|debugindexdot)
231 debugindex|debugindexdot)
232 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -f -X "!*.i" -- "$cur"))
232 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -f -X "!*.i" -- "$cur"))
233 ;;
233 ;;
234 debugdata)
234 debugdata)
235 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -f -X "!*.d" -- "$cur"))
235 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -f -X "!*.d" -- "$cur"))
236 ;;
236 ;;
237 *)
237 *)
238 return 1
238 return 1
239 ;;
239 ;;
240 esac
240 esac
241
241
242 return 0
242 return 0
243 }
243 }
244
244
245 complete -o bashdefault -o default -F _hg hg 2>/dev/null \
245 complete -o bashdefault -o default -F _hg hg 2>/dev/null \
246 || complete -o default -F _hg hg
246 || complete -o default -F _hg hg
247
247
248
248
249 # Completion for commands provided by extensions
249 # Completion for commands provided by extensions
250
250
251 # mq
251 # mq
252 _hg_ext_mq_patchlist()
252 _hg_ext_mq_patchlist()
253 {
253 {
254 local patches=$("$hg" $1 2>/dev/null)
254 local patches=$("$hg" $1 2>/dev/null)
255 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$patches' -- "$cur"))
255 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$patches' -- "$cur"))
256 }
256 }
257
257
258 _hg_ext_mq_queues()
258 _hg_ext_mq_queues()
259 {
259 {
260 local root=$("$hg" root 2>/dev/null)
260 local root=$("$hg" root 2>/dev/null)
261 local n
261 local n
262 for n in $(cd "$root"/.hg && compgen -d -- "$cur"); do
262 for n in $(cd "$root"/.hg && compgen -d -- "$cur"); do
263 # I think we're usually not interested in the regular "patches" queue
263 # I think we're usually not interested in the regular "patches" queue
264 # so just filter it.
264 # so just filter it.
265 if [ "$n" != patches ] && [ -e "$root/.hg/$n/series" ]; then
265 if [ "$n" != patches ] && [ -e "$root/.hg/$n/series" ]; then
266 COMPREPLY=(${COMPREPLY[@]:-} "$n")
266 COMPREPLY=(${COMPREPLY[@]:-} "$n")
267 fi
267 fi
268 done
268 done
269 }
269 }
270
270
271 _hg_cmd_qpop()
271 _hg_cmd_qpop()
272 {
272 {
273 if [[ "$prev" = @(-n|--name) ]]; then
273 if [[ "$prev" = @(-n|--name) ]]; then
274 _hg_ext_mq_queues
274 _hg_ext_mq_queues
275 return
275 return
276 fi
276 fi
277 _hg_ext_mq_patchlist qapplied
277 _hg_ext_mq_patchlist qapplied
278 }
278 }
279
279
280 _hg_cmd_qpush()
280 _hg_cmd_qpush()
281 {
281 {
282 if [[ "$prev" = @(-n|--name) ]]; then
282 if [[ "$prev" = @(-n|--name) ]]; then
283 _hg_ext_mq_queues
283 _hg_ext_mq_queues
284 return
284 return
285 fi
285 fi
286 _hg_ext_mq_patchlist qunapplied
286 _hg_ext_mq_patchlist qunapplied
287 }
287 }
288
288
289 _hg_cmd_qdelete()
289 _hg_cmd_qdelete()
290 {
290 {
291 _hg_ext_mq_patchlist qseries
291 _hg_ext_mq_patchlist qunapplied
292 }
292 }
293
293
294 _hg_cmd_qsave()
294 _hg_cmd_qsave()
295 {
295 {
296 if [[ "$prev" = @(-n|--name) ]]; then
296 if [[ "$prev" = @(-n|--name) ]]; then
297 _hg_ext_mq_queues
297 _hg_ext_mq_queues
298 return
298 return
299 fi
299 fi
300 }
300 }
301
301
302 _hg_cmd_strip()
302 _hg_cmd_strip()
303 {
303 {
304 _hg_tags
304 _hg_tags
305 }
305 }
306
306
307 _hg_cmd_qcommit()
307 _hg_cmd_qcommit()
308 {
308 {
309 local root=$("$hg" root 2>/dev/null)
309 local root=$("$hg" root 2>/dev/null)
310 # this is run in a sub-shell, so we can't use _hg_status
310 # this is run in a sub-shell, so we can't use _hg_status
311 local files=$(cd "$root/.hg/patches" 2>/dev/null &&
311 local files=$(cd "$root/.hg/patches" 2>/dev/null &&
312 "$hg" status -nmar 2>/dev/null)
312 "$hg" status -nmar 2>/dev/null)
313 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$files' -- "$cur"))
313 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$files' -- "$cur"))
314 }
314 }
315
315
316 _hg_cmd_export()
317 {
318 _hg_ext_mq_patchlist qapplied
319 }
320
316
321
317 # hbisect
322 # hbisect
318 _hg_cmd_bisect()
323 _hg_cmd_bisect()
319 {
324 {
320 local i subcmd
325 local i subcmd
321
326
322 # find the sub-command
327 # find the sub-command
323 for ((i=cmd_index+1; i<=COMP_CWORD; i++)); do
328 for ((i=cmd_index+1; i<=COMP_CWORD; i++)); do
324 if [[ ${COMP_WORDS[i]} != -* ]]; then
329 if [[ ${COMP_WORDS[i]} != -* ]]; then
325 if [[ ${COMP_WORDS[i-1]} != @($global_args) ]]; then
330 if [[ ${COMP_WORDS[i-1]} != @($global_args) ]]; then
326 subcmd="${COMP_WORDS[i]}"
331 subcmd="${COMP_WORDS[i]}"
327 break
332 break
328 fi
333 fi
329 fi
334 fi
330 done
335 done
331
336
332 if [ -z "$subcmd" ] || [ $COMP_CWORD -eq $i ] || [ "$subcmd" = help ]; then
337 if [ -z "$subcmd" ] || [ $COMP_CWORD -eq $i ] || [ "$subcmd" = help ]; then
333 COMPREPLY=(${COMPREPLY[@]:-}
338 COMPREPLY=(${COMPREPLY[@]:-}
334 $(compgen -W 'bad good help init next reset' -- "$cur"))
339 $(compgen -W 'bad good help init next reset' -- "$cur"))
335 return
340 return
336 fi
341 fi
337
342
338 case "$subcmd" in
343 case "$subcmd" in
339 good|bad)
344 good|bad)
340 _hg_tags
345 _hg_tags
341 ;;
346 ;;
342 esac
347 esac
343
348
344 return
349 return
345 }
350 }
346
351
347
352
348 # patchbomb
353 # patchbomb
349 _hg_cmd_email()
354 _hg_cmd_email()
350 {
355 {
351 case "$prev" in
356 case "$prev" in
352 -c|--cc|-t|--to|-f|--from)
357 -c|--cc|-t|--to|-f|--from)
353 # we need an e-mail address. let the user provide a function
358 # we need an e-mail address. let the user provide a function
354 # to get them
359 # to get them
355 if [ "$(type -t _hg_emails)" = function ]; then
360 if [ "$(type -t _hg_emails)" = function ]; then
356 local arg=to
361 local arg=to
357 if [[ "$prev" == @(-f|--from) ]]; then
362 if [[ "$prev" == @(-f|--from) ]]; then
358 arg=from
363 arg=from
359 fi
364 fi
360 local addresses=$(_hg_emails $arg)
365 local addresses=$(_hg_emails $arg)
361 COMPREPLY=(${COMPREPLY[@]:-}
366 COMPREPLY=(${COMPREPLY[@]:-}
362 $(compgen -W '$addresses' -- "$cur"))
367 $(compgen -W '$addresses' -- "$cur"))
363 fi
368 fi
364 return
369 return
365 ;;
370 ;;
366 -m|--mbox)
371 -m|--mbox)
367 # fallback to standard filename completion
372 # fallback to standard filename completion
368 return
373 return
369 ;;
374 ;;
370 -s|--subject)
375 -s|--subject)
371 # free form string
376 # free form string
372 return
377 return
373 ;;
378 ;;
374 esac
379 esac
375
380
376 _hg_tags
381 _hg_tags
377 return
382 return
378 }
383 }
379
384
380
385
381 # gpg
386 # gpg
382 _hg_cmd_sign()
387 _hg_cmd_sign()
383 {
388 {
384 _hg_tags
389 _hg_tags
385 }
390 }
General Comments 0
You need to be logged in to leave comments. Login now