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