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