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