##// END OF EJS Templates
Really fix bash_completion on Solaris. Maybe. Hopefully....
Alexis S. L. Carvalho -
r1820:7e10518b default
parent child Browse files
Show More
@@ -1,207 +1,210
1 shopt -s extglob
1 shopt -s extglob
2
2
3 _hg_command_list()
3 _hg_command_list()
4 {
4 {
5 "$hg" --debug help 2>/dev/null | \
5 "$hg" --debug help 2>/dev/null | \
6 awk -F', ' '/^list of commands:/ {commands=1}
6 awk -F', ' '/^list of commands:/ {commands=1}
7 commands && /^ [^ ]/ {
7 commands==1 && /^ [^ ]/ {
8 sub(/ /, "")
8 line = substr($0, 2)
9 sub(/:.*/, "")
9 colon = index(line, ":")
10 command = $1
10 if (colon > 0)
11 line = substr(line, 1, colon-1)
12 n = split(line, aliases)
13 command = aliases[1]
11 if (index(command, "debug") == 1) {
14 if (index(command, "debug") == 1) {
12 for (i=1; i<=NF; i++)
15 for (i=1; i<=n; i++)
13 debug[j++] = $i
16 debug[j++] = aliases[i]
14 next
17 next
15 }
18 }
16 print command
19 print command
17 for (i=2; i<=NF; i++)
20 for (i=2; i<=n; i++)
18 if (index(command, $i) != 1)
21 if (index(command, aliases[i]) != 1)
19 print $i
22 print aliases[i]
20 }
23 }
21 /^global options:/ {exit 0}
24 /^global options:/ {exit 0}
22 END {for (i in debug) print debug[i]}'
25 END {for (i in debug) print debug[i]}'
23 }
26 }
24
27
25 _hg_option_list()
28 _hg_option_list()
26 {
29 {
27 "$hg" -v help $1 2>/dev/null | \
30 "$hg" -v help $1 2>/dev/null | \
28 awk '/^ *-/ {
31 awk '/^ *-/ {
29 for (i = 1; i <= NF; i ++) {
32 for (i = 1; i <= NF; i ++) {
30 if (index($i, "-") != 1)
33 if (index($i, "-") != 1)
31 break;
34 break;
32 print $i;
35 print $i;
33 }
36 }
34 }'
37 }'
35 }
38 }
36
39
37
40
38 _hg_commands()
41 _hg_commands()
39 {
42 {
40 local all commands result
43 local all commands result
41
44
42 all=$(_hg_command_list)
45 all=$(_hg_command_list)
43 commands=${all%%$'\n'debug*}
46 commands=${all%%$'\n'debug*}
44 result=$(compgen -W '$commands' -- "$cur")
47 result=$(compgen -W '$commands' -- "$cur")
45
48
46 # hide debug commands from users, but complete them if
49 # hide debug commands from users, but complete them if
47 # there is no other possible command
50 # there is no other possible command
48 if [ "$result" = "" ]; then
51 if [ "$result" = "" ]; then
49 local debug
52 local debug
50 debug=debug${all#*$'\n'debug}
53 debug=debug${all#*$'\n'debug}
51 result=$(compgen -W '$debug' -- "$cur")
54 result=$(compgen -W '$debug' -- "$cur")
52 fi
55 fi
53
56
54 COMPREPLY=(${COMPREPLY[@]:-} $result)
57 COMPREPLY=(${COMPREPLY[@]:-} $result)
55 }
58 }
56
59
57 _hg_paths()
60 _hg_paths()
58 {
61 {
59 local paths="$("$hg" paths 2>/dev/null | sed -e 's/ = .*$//')"
62 local paths="$("$hg" paths 2>/dev/null | sed -e 's/ = .*$//')"
60 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$paths' -- "$cur"))
63 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$paths' -- "$cur"))
61 }
64 }
62
65
63 _hg_repos()
66 _hg_repos()
64 {
67 {
65 local i
68 local i
66 for i in $(compgen -d -- "$cur"); do
69 for i in $(compgen -d -- "$cur"); do
67 test ! -d "$i"/.hg || COMPREPLY=(${COMPREPLY[@]:-} "$i")
70 test ! -d "$i"/.hg || COMPREPLY=(${COMPREPLY[@]:-} "$i")
68 done
71 done
69 }
72 }
70
73
71 _hg_status()
74 _hg_status()
72 {
75 {
73 local files="$("$hg" status -n$1 . 2>/dev/null)"
76 local files="$("$hg" status -n$1 . 2>/dev/null)"
74 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$files' -- "$cur"))
77 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$files' -- "$cur"))
75 }
78 }
76
79
77 _hg_tags()
80 _hg_tags()
78 {
81 {
79 local tags="$("$hg" tags 2>/dev/null |
82 local tags="$("$hg" tags 2>/dev/null |
80 sed -e 's/[0-9]*:[a-f0-9]\{40\}$//; s/ *$//')"
83 sed -e 's/[0-9]*:[a-f0-9]\{40\}$//; s/ *$//')"
81 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$tags' -- "$cur"))
84 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$tags' -- "$cur"))
82 }
85 }
83
86
84 # this is "kind of" ugly...
87 # this is "kind of" ugly...
85 _hg_count_non_option()
88 _hg_count_non_option()
86 {
89 {
87 local i count=0
90 local i count=0
88 local filters="$1"
91 local filters="$1"
89
92
90 for ((i=1; $i<=$COMP_CWORD; i++)); do
93 for ((i=1; $i<=$COMP_CWORD; i++)); do
91 if [[ "${COMP_WORDS[i]}" != -* ]]; then
94 if [[ "${COMP_WORDS[i]}" != -* ]]; then
92 if [[ ${COMP_WORDS[i-1]} == @($filters|$global_args) ]]; then
95 if [[ ${COMP_WORDS[i-1]} == @($filters|$global_args) ]]; then
93 continue
96 continue
94 fi
97 fi
95 count=$(($count + 1))
98 count=$(($count + 1))
96 fi
99 fi
97 done
100 done
98
101
99 echo $(($count - 1))
102 echo $(($count - 1))
100 }
103 }
101
104
102 _hg()
105 _hg()
103 {
106 {
104 local cur prev cmd opts i
107 local cur prev cmd opts i
105 # global options that receive an argument
108 # global options that receive an argument
106 local global_args='--cwd|-R|--repository'
109 local global_args='--cwd|-R|--repository'
107 local hg="$1"
110 local hg="$1"
108
111
109 COMPREPLY=()
112 COMPREPLY=()
110 cur="$2"
113 cur="$2"
111 prev="$3"
114 prev="$3"
112
115
113 # searching for the command
116 # searching for the command
114 # (first non-option argument that doesn't follow a global option that
117 # (first non-option argument that doesn't follow a global option that
115 # receives an argument)
118 # receives an argument)
116 for ((i=1; $i<=$COMP_CWORD; i++)); do
119 for ((i=1; $i<=$COMP_CWORD; i++)); do
117 if [[ ${COMP_WORDS[i]} != -* ]]; then
120 if [[ ${COMP_WORDS[i]} != -* ]]; then
118 if [[ ${COMP_WORDS[i-1]} != @($global_args) ]]; then
121 if [[ ${COMP_WORDS[i-1]} != @($global_args) ]]; then
119 cmd="${COMP_WORDS[i]}"
122 cmd="${COMP_WORDS[i]}"
120 break
123 break
121 fi
124 fi
122 fi
125 fi
123 done
126 done
124
127
125 if [[ "$cur" == -* ]]; then
128 if [[ "$cur" == -* ]]; then
126 opts=$(_hg_option_list $cmd)
129 opts=$(_hg_option_list $cmd)
127
130
128 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$opts' -- "$cur"))
131 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$opts' -- "$cur"))
129 return
132 return
130 fi
133 fi
131
134
132 # global options
135 # global options
133 case "$prev" in
136 case "$prev" in
134 -R|--repository)
137 -R|--repository)
135 _hg_repos
138 _hg_repos
136 return
139 return
137 ;;
140 ;;
138 --cwd)
141 --cwd)
139 # Stick with default bash completion
142 # Stick with default bash completion
140 return
143 return
141 ;;
144 ;;
142 esac
145 esac
143
146
144 if [ -z "$cmd" ] || [ $COMP_CWORD -eq $i ]; then
147 if [ -z "$cmd" ] || [ $COMP_CWORD -eq $i ]; then
145 _hg_commands
148 _hg_commands
146 return
149 return
147 fi
150 fi
148
151
149 # canonicalize command name
152 # canonicalize command name
150 cmd=$("$hg" -q help "$cmd" 2>/dev/null | sed -e 's/^hg //; s/ .*//; 1q')
153 cmd=$("$hg" -q help "$cmd" 2>/dev/null | sed -e 's/^hg //; s/ .*//; 1q')
151
154
152 if [ "$cmd" != status ] && [ "$prev" = -r ] || [ "$prev" = --rev ]; then
155 if [ "$cmd" != status ] && [ "$prev" = -r ] || [ "$prev" = --rev ]; then
153 _hg_tags
156 _hg_tags
154 return
157 return
155 fi
158 fi
156
159
157 case "$cmd" in
160 case "$cmd" in
158 help)
161 help)
159 _hg_commands
162 _hg_commands
160 ;;
163 ;;
161 export|manifest|update)
164 export|manifest|update)
162 _hg_tags
165 _hg_tags
163 ;;
166 ;;
164 pull|push|outgoing|incoming)
167 pull|push|outgoing|incoming)
165 _hg_paths
168 _hg_paths
166 _hg_repos
169 _hg_repos
167 ;;
170 ;;
168 paths)
171 paths)
169 _hg_paths
172 _hg_paths
170 ;;
173 ;;
171 add)
174 add)
172 _hg_status "u"
175 _hg_status "u"
173 ;;
176 ;;
174 commit)
177 commit)
175 _hg_status "mar"
178 _hg_status "mar"
176 ;;
179 ;;
177 remove)
180 remove)
178 _hg_status "d"
181 _hg_status "d"
179 ;;
182 ;;
180 forget)
183 forget)
181 _hg_status "a"
184 _hg_status "a"
182 ;;
185 ;;
183 diff)
186 diff)
184 _hg_status "mar"
187 _hg_status "mar"
185 ;;
188 ;;
186 revert)
189 revert)
187 _hg_status "mard"
190 _hg_status "mard"
188 ;;
191 ;;
189 clone)
192 clone)
190 local count=$(_hg_count_non_option)
193 local count=$(_hg_count_non_option)
191 if [ $count = 1 ]; then
194 if [ $count = 1 ]; then
192 _hg_paths
195 _hg_paths
193 fi
196 fi
194 _hg_repos
197 _hg_repos
195 ;;
198 ;;
196 debugindex|debugindexdot)
199 debugindex|debugindexdot)
197 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -f -X "!*.i" -- "$cur"))
200 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -f -X "!*.i" -- "$cur"))
198 ;;
201 ;;
199 debugdata)
202 debugdata)
200 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -f -X "!*.d" -- "$cur"))
203 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -f -X "!*.d" -- "$cur"))
201 ;;
204 ;;
202 esac
205 esac
203
206
204 }
207 }
205
208
206 complete -o bashdefault -o default -F _hg hg 2>/dev/null \
209 complete -o bashdefault -o default -F _hg hg 2>/dev/null \
207 || complete -o default -F _hg hg
210 || complete -o default -F _hg hg
General Comments 0
You need to be logged in to leave comments. Login now