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