##// 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 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 if [[ ${COMP_WORDS[i-1]} == @($filters|$global_args) ]]; then
45 45 continue
46 46 fi
47 47 count=$(($count + 1))
48 48 fi
49 49 done
50 50
51 51 echo $(($count - 1))
52 52 }
53 53
54 54 _hg()
55 55 {
56 56 local cur prev cmd opts i
57 57 # global options that receive an argument
58 58 local global_args='--cwd|-R|--repository'
59 59
60 60 COMPREPLY=()
61 61 cur="$2"
62 62 prev="$3"
63 63
64 64 # searching for the command
65 65 # (first non-option argument that doesn't follow a global option that
66 66 # receives an argument)
67 67 for (( i=1; $i<=$COMP_CWORD; i++ )); do
68 68 if [[ ${COMP_WORDS[i]} != -* ]]; then
69 69 if [[ ${COMP_WORDS[i-1]} != @($global_args) ]]; then
70 70 cmd="${COMP_WORDS[i]}"
71 71 break
72 72 fi
73 73 fi
74 74 done
75 75
76 76 if [[ "$cur" == -* ]]; then
77 77 # this assumes that there are no commands with spaces in the name
78 78 opts=$(hg -v help $cmd | sed -e '/^ *-/!d; s/ [^- ].*//')
79 79
80 80 COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "$opts" -- "$cur") )
81 81 return
82 82 fi
83 83
84 84 # global options
85 85 case "$prev" in
86 86 -R|--repository)
87 87 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -d -- "$cur" ))
88 88 return
89 89 ;;
90 90 --cwd)
91 91 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -d -- "$cur" ))
92 92 return
93 93 ;;
94 94 esac
95 95
96 96 if [ -z "$cmd" ] || [ $COMP_CWORD -eq $i ]; then
97 97 _hg_commands
98 98 return
99 99 fi
100 100
101 101 # canonicalize command name
102 102 cmd=$(hg -q help "$cmd" | sed -e 's/^hg //; s/ .*//; 1q')
103 103
104 104 if [ "$cmd" != status ] && [ "$prev" = -r ] || [ "$prev" = --rev ]; then
105 105 _hg_tags
106 106 return
107 107 fi
108 108
109 109 case "$cmd" in
110 110 help)
111 111 _hg_commands
112 112 ;;
113 113 export|manifest|update)
114 114 _hg_tags
115 115 ;;
116 116 pull|push|outgoing|incoming)
117 117 _hg_paths
118 118 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -d -- "$cur" ))
119 119 ;;
120 120 paths)
121 121 _hg_paths
122 122 ;;
123 123 add)
124 124 _hg_status "u"
125 125 ;;
126 126 commit)
127 127 _hg_status "mra"
128 128 ;;
129 129 remove)
130 130 _hg_status "r"
131 131 ;;
132 132 forget)
133 133 _hg_status "a"
134 134 ;;
135 135 diff)
136 136 _hg_status "mra"
137 137 ;;
138 138 revert)
139 139 _hg_status "mra"
140 140 ;;
141 141 clone)
142 142 local count=$(_hg_count_non_option)
143 143 if [ $count = 1 ]; then
144 144 _hg_paths
145 145 fi
146 146 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -d -- "$cur" ))
147 147 ;;
148 148 debugindex|debugindexdot)
149 149 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -X "!*.i" -- "$cur" ))
150 150 ;;
151 151 debugdata)
152 152 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -X "!*.d" -- "$cur" ))
153 153 ;;
154 154 cat)
155 155 local count=$(_hg_count_non_option '-o|--output')
156 156 if [ $count = 2 ]; then
157 157 _hg_tags
158 158 else
159 159 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -- "$cur" ))
160 160 fi
161 161 ;;
162 162 *)
163 163 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -- "$cur" ))
164 164 ;;
165 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