##// END OF EJS Templates
bash_completion: better handling of aliases...
Alexis S. L. Carvalho -
r1150:4ee09418 default
parent child Browse files
Show More
@@ -1,158 +1,161 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 for f in $filters; do
44 for f in $filters; do
45 if [[ ${COMP_WORDS[i-1]} == $f ]]; then
45 if [[ ${COMP_WORDS[i-1]} == $f ]]; then
46 continue 2
46 continue 2
47 fi
47 fi
48 done
48 done
49 count=$(($count + 1))
49 count=$(($count + 1))
50 fi
50 fi
51 done
51 done
52
52
53 echo $(($count - 1))
53 echo $(($count - 1))
54 }
54 }
55
55
56 _hg()
56 _hg()
57 {
57 {
58 local cur prev cmd opts i
58 local cur prev cmd opts i
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 -R/--repository)
65 # (first non-option argument that doesn't follow -R/--repository)
66 for (( i=1; $i<=$COMP_CWORD; i++ )); do
66 for (( i=1; $i<=$COMP_CWORD; i++ )); do
67 if [[ ${COMP_WORDS[i]} != -* ]] \
67 if [[ ${COMP_WORDS[i]} != -* ]] \
68 && [ "${COMP_WORDS[i-1]}" != -R ] \
68 && [ "${COMP_WORDS[i-1]}" != -R ] \
69 && [ "${COMP_WORDS[i-1]}" != --repository ]; then
69 && [ "${COMP_WORDS[i-1]}" != --repository ]; then
70 cmd="${COMP_WORDS[i]}"
70 cmd="${COMP_WORDS[i]}"
71 break
71 break
72 fi
72 fi
73 done
73 done
74
74
75 if [[ "$cur" == -* ]]; then
75 if [[ "$cur" == -* ]]; then
76 # this assumes that there are no commands with spaces in the name
76 # this assumes that there are no commands with spaces in the name
77 opts=$(hg -v help $cmd | sed -e '/^ *-/!d; s/ [^- ].*//')
77 opts=$(hg -v help $cmd | sed -e '/^ *-/!d; s/ [^- ].*//')
78
78
79 COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "$opts" -- "$cur") )
79 COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "$opts" -- "$cur") )
80 return
80 return
81 fi
81 fi
82
82
83 if [ "$prev" = -R ] || [ "$prev" = --repository ]; then
83 if [ "$prev" = -R ] || [ "$prev" = --repository ]; then
84 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -d -- "$cur" ))
84 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -d -- "$cur" ))
85 return
85 return
86 fi
86 fi
87
87
88 if [ -z "$cmd" ] || [ $COMP_CWORD -eq $i ]; then
88 if [ -z "$cmd" ] || [ $COMP_CWORD -eq $i ]; then
89 _hg_commands
89 _hg_commands
90 return
90 return
91 fi
91 fi
92
92
93 # canonicalize command name
94 cmd=$(hg -q help "$cmd" | sed -e 's/^hg //; s/ .*//; 1q')
95
93 if [ "$cmd" != status ] && [ "$prev" = -r ] || [ "$prev" = --rev ]; then
96 if [ "$cmd" != status ] && [ "$prev" = -r ] || [ "$prev" = --rev ]; then
94 _hg_tags
97 _hg_tags
95 return
98 return
96 fi
99 fi
97
100
98 case "$cmd" in
101 case "$cmd" in
99 help)
102 help)
100 _hg_commands
103 _hg_commands
101 ;;
104 ;;
102 export|manifest|update|checkout|up|co)
105 export|manifest|update)
103 _hg_tags
106 _hg_tags
104 ;;
107 ;;
105 pull|push|outgoing|incoming|out|in)
108 pull|push|outgoing|incoming)
106 _hg_paths
109 _hg_paths
107 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -d -- "$cur" ))
110 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -d -- "$cur" ))
108 ;;
111 ;;
109 paths)
112 paths)
110 _hg_paths
113 _hg_paths
111 ;;
114 ;;
112 add)
115 add)
113 _hg_status "u"
116 _hg_status "u"
114 ;;
117 ;;
115 commit|ci)
118 commit)
116 _hg_status "mra"
119 _hg_status "mra"
117 ;;
120 ;;
118 remove)
121 remove)
119 _hg_status "r"
122 _hg_status "r"
120 ;;
123 ;;
121 forget)
124 forget)
122 _hg_status "a"
125 _hg_status "a"
123 ;;
126 ;;
124 diff)
127 diff)
125 _hg_status "mra"
128 _hg_status "mra"
126 ;;
129 ;;
127 revert)
130 revert)
128 _hg_status "mra"
131 _hg_status "mra"
129 ;;
132 ;;
130 clone)
133 clone)
131 local count=$(_hg_count_non_option)
134 local count=$(_hg_count_non_option)
132 if [ $count = 1 ]; then
135 if [ $count = 1 ]; then
133 _hg_paths
136 _hg_paths
134 fi
137 fi
135 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -d -- "$cur" ))
138 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -d -- "$cur" ))
136 ;;
139 ;;
137 debugindex|debugindexdot)
140 debugindex|debugindexdot)
138 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -X "!*.i" -- "$cur" ))
141 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -X "!*.i" -- "$cur" ))
139 ;;
142 ;;
140 debugdata)
143 debugdata)
141 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -X "!*.d" -- "$cur" ))
144 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -X "!*.d" -- "$cur" ))
142 ;;
145 ;;
143 cat)
146 cat)
144 local count=$(_hg_count_non_option -o --output)
147 local count=$(_hg_count_non_option -o --output)
145 if [ $count = 2 ]; then
148 if [ $count = 2 ]; then
146 _hg_tags
149 _hg_tags
147 else
150 else
148 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -- "$cur" ))
151 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -- "$cur" ))
149 fi
152 fi
150 ;;
153 ;;
151 *)
154 *)
152 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -- "$cur" ))
155 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -- "$cur" ))
153 ;;
156 ;;
154 esac
157 esac
155
158
156 }
159 }
157
160
158 complete -o default -F _hg hg
161 complete -o default -F _hg hg
General Comments 0
You need to be logged in to leave comments. Login now