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