comparison contrib/bash_completion @ 1680:c21b54f7f7b8

Merge with crew
author Benoit Boissinot <benoit.boissinot@ens-lyon.org>
date Wed, 01 Feb 2006 19:18:15 +0100
parents 747c8d03bd29
children 063e04831a09
comparison
equal deleted inserted replaced
1679:675ca845c2f8 1680:c21b54f7f7b8
1 shopt -s extglob 1 shopt -s extglob
2 2
3 _hg_command_list()
4 {
5 hg --debug help 2>/dev/null | \
6 awk 'function command_line(line) {
7 gsub(/,/, "", line)
8 gsub(/:.*/, "", line)
9 split(line, aliases)
10 command = aliases[1]
11 delete aliases[1]
12 print command
13 for (i in aliases)
14 if (index(command, aliases[i]) != 1)
15 print aliases[i]
16 }
17 /^list of commands:/ {commands=1}
18 commands && /^ debug/ {a[i++] = $0; next;}
19 commands && /^ [^ ]/ {command_line($0)}
20 /^global options:/ {exit 0}
21 END {for (i in a) command_line(a[i])}'
22
23 }
24
25 _hg_option_list()
26 {
27 hg -v help $1 2> /dev/null | \
28 awk '/^ *-/ {
29 for (i = 1; i <= NF; i ++) {
30 if (index($i, "-") != 1)
31 break;
32 print $i;
33 }
34 }'
35 }
36
37
3 _hg_commands() 38 _hg_commands()
4 { 39 {
5 local all commands result 40 local all commands result
6 41
7 all=($(hg --debug help | sed -e '1,/^list of commands:/d' \ 42 all=$(_hg_command_list)
8 -e '/^global options:/,$d' \ 43 commands=${all%%$'\n'debug*}
9 -e '/^ [^ ]/!d; s/^ //; s/[,:]//g;')) 44 result=$(compgen -W '$commands' -- "$cur")
10
11 commands="${all[*]##debug*}"
12 result=$(compgen -W "${commands[*]}" -- "$cur")
13 45
14 # hide debug commands from users, but complete them if 46 # hide debug commands from users, but complete them if
15 # there is no other possible command 47 # there is no other possible command
16 if [ "$result" = "" ]; then 48 if [ "$result" = "" ]; then
17 local debug 49 local debug
18 debug=(${all[*]##!(debug*)}) 50 debug=debug${all#*$'\n'debug}
19 debug="${debug[*]/g/debug}" 51 result=$(compgen -W '$debug' -- "$cur")
20 result=$(compgen -W "$debug" -- "$cur")
21 fi 52 fi
22 53
23 COMPREPLY=(${COMPREPLY[@]:-} $result) 54 COMPREPLY=(${COMPREPLY[@]:-} $result)
24 } 55 }
25 56
26 _hg_paths() 57 _hg_paths()
27 { 58 {
28 local paths="$(hg paths | sed -e 's/ = .*$//')" 59 local paths="$(hg paths 2> /dev/null | sed -e 's/ = .*$//')"
29 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -W "$paths" -- "$cur" )) 60 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -W '$paths' -- "$cur" ))
30 } 61 }
31 62
32 _hg_repos() 63 _hg_repos()
33 { 64 {
34 local i 65 local i
37 done 68 done
38 } 69 }
39 70
40 _hg_status() 71 _hg_status()
41 { 72 {
42 local files="$( hg status -$1 | cut -b 3- )" 73 local files="$( hg status -n$1 . 2> /dev/null)"
43 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -W "$files" -- "$cur" )) 74 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -W '$files' -- "$cur" ))
44 } 75 }
45 76
46 _hg_tags() 77 _hg_tags()
47 { 78 {
48 local tags="$(hg tags | sed -e 's/[0-9]*:[a-f0-9]\{40\}$//; s/ *$//')" 79 local tags="$(hg tags 2> /dev/null |
49 COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "$tags" -- "$cur") ) 80 sed -e 's/[0-9]*:[a-f0-9]\{40\}$//; s/ *$//')"
81 COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W '$tags' -- "$cur") )
50 } 82 }
51 83
52 # this is "kind of" ugly... 84 # this is "kind of" ugly...
53 _hg_count_non_option() 85 _hg_count_non_option()
54 { 86 {
88 fi 120 fi
89 fi 121 fi
90 done 122 done
91 123
92 if [[ "$cur" == -* ]]; then 124 if [[ "$cur" == -* ]]; then
93 # this assumes that there are no commands with spaces in the name 125 opts=$(_hg_option_list $cmd)
94 opts=$(hg -v help $cmd | sed -e '/^ *-/!d; s/ [^- ].*//') 126
95 127 COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W '$opts' -- "$cur") )
96 COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "$opts" -- "$cur") )
97 return 128 return
98 fi 129 fi
99 130
100 # global options 131 # global options
101 case "$prev" in 132 case "$prev" in
113 _hg_commands 144 _hg_commands
114 return 145 return
115 fi 146 fi
116 147
117 # canonicalize command name 148 # canonicalize command name
118 cmd=$(hg -q help "$cmd" | sed -e 's/^hg //; s/ .*//; 1q') 149 cmd=$(hg -q help "$cmd" 2> /dev/null | sed -e 's/^hg //; s/ .*//; 1q')
119 150
120 if [ "$cmd" != status ] && [ "$prev" = -r ] || [ "$prev" = --rev ]; then 151 if [ "$cmd" != status ] && [ "$prev" = -r ] || [ "$prev" = --rev ]; then
121 _hg_tags 152 _hg_tags
122 return 153 return
123 fi 154 fi
138 ;; 169 ;;
139 add) 170 add)
140 _hg_status "u" 171 _hg_status "u"
141 ;; 172 ;;
142 commit) 173 commit)
143 _hg_status "mra" 174 _hg_status "mar"
144 ;; 175 ;;
145 remove) 176 remove)
146 _hg_status "r" 177 _hg_status "d"
147 ;; 178 ;;
148 forget) 179 forget)
149 _hg_status "a" 180 _hg_status "a"
150 ;; 181 ;;
151 diff) 182 diff)
152 _hg_status "mra" 183 _hg_status "mar"
153 ;; 184 ;;
154 revert) 185 revert)
155 _hg_status "mra" 186 _hg_status "mard"
156 ;; 187 ;;
157 clone) 188 clone)
158 local count=$(_hg_count_non_option) 189 local count=$(_hg_count_non_option)
159 if [ $count = 1 ]; then 190 if [ $count = 1 ]; then
160 _hg_paths 191 _hg_paths
165 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -X "!*.i" -- "$cur" )) 196 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -X "!*.i" -- "$cur" ))
166 ;; 197 ;;
167 debugdata) 198 debugdata)
168 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -X "!*.d" -- "$cur" )) 199 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -X "!*.d" -- "$cur" ))
169 ;; 200 ;;
170 cat)
171 local count=$(_hg_count_non_option '-o|--output')
172 if [ $count = 2 ]; then
173 _hg_tags
174 else
175 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -- "$cur" ))
176 fi
177 ;;
178 *)
179 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -- "$cur" ))
180 ;;
181 esac 201 esac
182 202
183 } 203 }
184 204
185 complete -o bashdefault -o default -F _hg hg 2> /dev/null \ 205 complete -o bashdefault -o default -F _hg hg 2> /dev/null \