##// END OF EJS Templates
bash_completion: try to use bash3 features if they're available...
Alexis S. L. Carvalho -
r1153:fa9ae7df default
parent child Browse files
Show More
@@ -1,169 +1,170 b''
1 #!/bin/bash
1 #!/bin/bash
2
2
3 _hg_commands()
3 _hg_commands()
4 {
4 {
5 local commands="$(hg -v help | sed -e '1,/^list of commands:/d' \
5 local commands="$(hg -v help | sed -e '1,/^list of commands:/d' \
6 -e '/^global options:/,$d' \
6 -e '/^global options:/,$d' \
7 -e '/^ [^ ]/!d; s/[,:]//g;')"
7 -e '/^ [^ ]/!d; s/[,:]//g;')"
8
8
9 # hide debug commands from users, but complete them if
9 # hide debug commands from users, but complete them if
10 # specifically asked for
10 # specifically asked for
11 if [[ "$cur" == de* ]]; then
11 if [[ "$cur" == de* ]]; then
12 commands="$commands debugcheckstate debugstate debugindex"
12 commands="$commands debugcheckstate debugstate debugindex"
13 commands="$commands debugindexdot debugwalk debugdata"
13 commands="$commands debugindexdot debugwalk debugdata"
14 fi
14 fi
15 COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "$commands" -- "$cur") )
15 COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "$commands" -- "$cur") )
16 }
16 }
17
17
18 _hg_paths()
18 _hg_paths()
19 {
19 {
20 local paths="$(hg paths | sed -e 's/ = .*$//')"
20 local paths="$(hg paths | sed -e 's/ = .*$//')"
21 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -W "$paths" -- "$cur" ))
21 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -W "$paths" -- "$cur" ))
22 }
22 }
23
23
24 _hg_status()
24 _hg_status()
25 {
25 {
26 local files="$( hg status -$1 | cut -b 3- )"
26 local files="$( hg status -$1 | cut -b 3- )"
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 | sed -e 's/[0-9]*:[a-f0-9]\{40\}$//; s/ *$//')"
32 local tags="$(hg tags | sed -e 's/[0-9]*:[a-f0-9]\{40\}$//; s/ *$//')"
33 COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "$tags" -- "$cur") )
33 COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "$tags" -- "$cur") )
34 }
34 }
35
35
36 # this is "kind of" ugly...
36 # this is "kind of" ugly...
37 _hg_count_non_option()
37 _hg_count_non_option()
38 {
38 {
39 local i count=0
39 local i count=0
40 local filters="$1"
40 local filters="$1"
41
41
42 for (( i=1; $i<=$COMP_CWORD; i++ )); do
42 for (( i=1; $i<=$COMP_CWORD; i++ )); do
43 if [[ "${COMP_WORDS[i]}" != -* ]]; then
43 if [[ "${COMP_WORDS[i]}" != -* ]]; then
44 if [[ ${COMP_WORDS[i-1]} == @($filters|$global_args) ]]; then
44 if [[ ${COMP_WORDS[i-1]} == @($filters|$global_args) ]]; then
45 continue
45 continue
46 fi
46 fi
47 count=$(($count + 1))
47 count=$(($count + 1))
48 fi
48 fi
49 done
49 done
50
50
51 echo $(($count - 1))
51 echo $(($count - 1))
52 }
52 }
53
53
54 _hg()
54 _hg()
55 {
55 {
56 local cur prev cmd opts i
56 local cur prev cmd opts i
57 # global options that receive an argument
57 # global options that receive an argument
58 local global_args='--cwd|-R|--repository'
58 local global_args='--cwd|-R|--repository'
59
59
60 COMPREPLY=()
60 COMPREPLY=()
61 cur="$2"
61 cur="$2"
62 prev="$3"
62 prev="$3"
63
63
64 # searching for the command
64 # searching for the command
65 # (first non-option argument that doesn't follow a global option that
65 # (first non-option argument that doesn't follow a global option that
66 # receives an argument)
66 # receives an argument)
67 for (( i=1; $i<=$COMP_CWORD; i++ )); do
67 for (( i=1; $i<=$COMP_CWORD; i++ )); do
68 if [[ ${COMP_WORDS[i]} != -* ]]; then
68 if [[ ${COMP_WORDS[i]} != -* ]]; then
69 if [[ ${COMP_WORDS[i-1]} != @($global_args) ]]; then
69 if [[ ${COMP_WORDS[i-1]} != @($global_args) ]]; then
70 cmd="${COMP_WORDS[i]}"
70 cmd="${COMP_WORDS[i]}"
71 break
71 break
72 fi
72 fi
73 fi
73 fi
74 done
74 done
75
75
76 if [[ "$cur" == -* ]]; then
76 if [[ "$cur" == -* ]]; then
77 # this assumes that there are no commands with spaces in the name
77 # this assumes that there are no commands with spaces in the name
78 opts=$(hg -v help $cmd | sed -e '/^ *-/!d; s/ [^- ].*//')
78 opts=$(hg -v help $cmd | sed -e '/^ *-/!d; s/ [^- ].*//')
79
79
80 COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "$opts" -- "$cur") )
80 COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "$opts" -- "$cur") )
81 return
81 return
82 fi
82 fi
83
83
84 # global options
84 # global options
85 case "$prev" in
85 case "$prev" in
86 -R|--repository)
86 -R|--repository)
87 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -d -- "$cur" ))
87 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -d -- "$cur" ))
88 return
88 return
89 ;;
89 ;;
90 --cwd)
90 --cwd)
91 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -d -- "$cur" ))
91 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -d -- "$cur" ))
92 return
92 return
93 ;;
93 ;;
94 esac
94 esac
95
95
96 if [ -z "$cmd" ] || [ $COMP_CWORD -eq $i ]; then
96 if [ -z "$cmd" ] || [ $COMP_CWORD -eq $i ]; then
97 _hg_commands
97 _hg_commands
98 return
98 return
99 fi
99 fi
100
100
101 # canonicalize command name
101 # canonicalize command name
102 cmd=$(hg -q help "$cmd" | sed -e 's/^hg //; s/ .*//; 1q')
102 cmd=$(hg -q help "$cmd" | sed -e 's/^hg //; s/ .*//; 1q')
103
103
104 if [ "$cmd" != status ] && [ "$prev" = -r ] || [ "$prev" = --rev ]; then
104 if [ "$cmd" != status ] && [ "$prev" = -r ] || [ "$prev" = --rev ]; then
105 _hg_tags
105 _hg_tags
106 return
106 return
107 fi
107 fi
108
108
109 case "$cmd" in
109 case "$cmd" in
110 help)
110 help)
111 _hg_commands
111 _hg_commands
112 ;;
112 ;;
113 export|manifest|update)
113 export|manifest|update)
114 _hg_tags
114 _hg_tags
115 ;;
115 ;;
116 pull|push|outgoing|incoming)
116 pull|push|outgoing|incoming)
117 _hg_paths
117 _hg_paths
118 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -d -- "$cur" ))
118 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -d -- "$cur" ))
119 ;;
119 ;;
120 paths)
120 paths)
121 _hg_paths
121 _hg_paths
122 ;;
122 ;;
123 add)
123 add)
124 _hg_status "u"
124 _hg_status "u"
125 ;;
125 ;;
126 commit)
126 commit)
127 _hg_status "mra"
127 _hg_status "mra"
128 ;;
128 ;;
129 remove)
129 remove)
130 _hg_status "r"
130 _hg_status "r"
131 ;;
131 ;;
132 forget)
132 forget)
133 _hg_status "a"
133 _hg_status "a"
134 ;;
134 ;;
135 diff)
135 diff)
136 _hg_status "mra"
136 _hg_status "mra"
137 ;;
137 ;;
138 revert)
138 revert)
139 _hg_status "mra"
139 _hg_status "mra"
140 ;;
140 ;;
141 clone)
141 clone)
142 local count=$(_hg_count_non_option)
142 local count=$(_hg_count_non_option)
143 if [ $count = 1 ]; then
143 if [ $count = 1 ]; then
144 _hg_paths
144 _hg_paths
145 fi
145 fi
146 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -d -- "$cur" ))
146 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -d -- "$cur" ))
147 ;;
147 ;;
148 debugindex|debugindexdot)
148 debugindex|debugindexdot)
149 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -X "!*.i" -- "$cur" ))
149 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -X "!*.i" -- "$cur" ))
150 ;;
150 ;;
151 debugdata)
151 debugdata)
152 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -X "!*.d" -- "$cur" ))
152 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -X "!*.d" -- "$cur" ))
153 ;;
153 ;;
154 cat)
154 cat)
155 local count=$(_hg_count_non_option '-o|--output')
155 local count=$(_hg_count_non_option '-o|--output')
156 if [ $count = 2 ]; then
156 if [ $count = 2 ]; then
157 _hg_tags
157 _hg_tags
158 else
158 else
159 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -- "$cur" ))
159 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -- "$cur" ))
160 fi
160 fi
161 ;;
161 ;;
162 *)
162 *)
163 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -- "$cur" ))
163 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -- "$cur" ))
164 ;;
164 ;;
165 esac
165 esac
166
166
167 }
167 }
168
168
169 complete -o default -F _hg hg
169 complete -o bashdefault -o default -F _hg hg 2> /dev/null \
170 || complete -o default -F _hg hg
General Comments 0
You need to be logged in to leave comments. Login now