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