comparison contrib/bash_completion @ 1697:7b4128f689bd

Merge with crew
author Matt Mackall <mpm@selenic.com>
date Sun, 05 Feb 2006 22:18:38 -0600
parents cf930b2452d3
children 2af98c4b2587
comparison
equal deleted inserted replaced
1668:586b50294ea8 1697:7b4128f689bd
1 shopt -s extglob 1 shopt -s extglob
2 2
3 _hg_command_list() 3 _hg_command_list()
4 { 4 {
5 hg --debug help 2>/dev/null | \ 5 "$hg" --debug help 2>/dev/null | \
6 awk 'function command_line(line) { 6 awk 'function command_line(line) {
7 gsub(/,/, "", line) 7 gsub(/,/, "", line)
8 gsub(/:.*/, "", line) 8 gsub(/:.*/, "", line)
9 split(line, aliases) 9 split(line, aliases)
10 command = aliases[1] 10 command = aliases[1]
11 delete aliases[1] 11 delete aliases[1]
12 print command 12 print command
13 for (i in aliases) 13 for (i in aliases)
14 if (index(command, aliases[i]) != 1) 14 if (index(command, aliases[i]) != 1)
15 print aliases[i] 15 print aliases[i]
16 } 16 }
17 /^list of commands:/ {commands=1} 17 /^list of commands:/ {commands=1}
18 commands && /^ debug/ {a[i++] = $0; next;} 18 commands && /^ debug/ {a[i++] = $0; next;}
19 commands && /^ [^ ]/ {command_line($0)} 19 commands && /^ [^ ]/ {command_line($0)}
20 /^global options:/ {exit 0} 20 /^global options:/ {exit 0}
21 END {for (i in a) command_line(a[i])}' 21 END {for (i in a) command_line(a[i])}'
22 22
23 } 23 }
24 24
25 _hg_option_list() 25 _hg_option_list()
26 { 26 {
27 hg -v help $1 2> /dev/null | \ 27 "$hg" -v help $1 2>/dev/null | \
28 awk '/^ *-/ { 28 awk '/^ *-/ {
29 for (i = 1; i <= NF; i ++) { 29 for (i = 1; i <= NF; i ++) {
30 if (index($i, "-") != 1) 30 if (index($i, "-") != 1)
31 break; 31 break;
32 print $i; 32 print $i;
33 } 33 }
34 }' 34 }'
35 } 35 }
36 36
37 37
38 _hg_commands() 38 _hg_commands()
39 { 39 {
54 COMPREPLY=(${COMPREPLY[@]:-} $result) 54 COMPREPLY=(${COMPREPLY[@]:-} $result)
55 } 55 }
56 56
57 _hg_paths() 57 _hg_paths()
58 { 58 {
59 local paths="$(hg paths 2> /dev/null | sed -e 's/ = .*$//')" 59 local paths="$("$hg" paths 2>/dev/null | sed -e 's/ = .*$//')"
60 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -W '$paths' -- "$cur" )) 60 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$paths' -- "$cur"))
61 } 61 }
62 62
63 _hg_repos() 63 _hg_repos()
64 { 64 {
65 local i 65 local i
66 for i in $( compgen -d -- "$cur" ); do 66 for i in $(compgen -d -- "$cur"); do
67 test ! -d "$i"/.hg || COMPREPLY=(${COMPREPLY[@]:-} "$i") 67 test ! -d "$i"/.hg || COMPREPLY=(${COMPREPLY[@]:-} "$i")
68 done 68 done
69 } 69 }
70 70
71 _hg_status() 71 _hg_status()
72 { 72 {
73 local files="$( hg status -n$1 . 2> /dev/null)" 73 local files="$("$hg" status -n$1 . 2>/dev/null)"
74 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -W '$files' -- "$cur" )) 74 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$files' -- "$cur"))
75 } 75 }
76 76
77 _hg_tags() 77 _hg_tags()
78 { 78 {
79 local tags="$(hg tags 2> /dev/null | 79 local tags="$("$hg" tags 2>/dev/null |
80 sed -e 's/[0-9]*:[a-f0-9]\{40\}$//; s/ *$//')" 80 sed -e 's/[0-9]*:[a-f0-9]\{40\}$//; s/ *$//')"
81 COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W '$tags' -- "$cur") ) 81 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$tags' -- "$cur"))
82 } 82 }
83 83
84 # this is "kind of" ugly... 84 # this is "kind of" ugly...
85 _hg_count_non_option() 85 _hg_count_non_option()
86 { 86 {
87 local i count=0 87 local i count=0
88 local filters="$1" 88 local filters="$1"
89 89
90 for (( i=1; $i<=$COMP_CWORD; i++ )); do 90 for ((i=1; $i<=$COMP_CWORD; i++)); do
91 if [[ "${COMP_WORDS[i]}" != -* ]]; then 91 if [[ "${COMP_WORDS[i]}" != -* ]]; then
92 if [[ ${COMP_WORDS[i-1]} == @($filters|$global_args) ]]; then 92 if [[ ${COMP_WORDS[i-1]} == @($filters|$global_args) ]]; then
93 continue 93 continue
94 fi 94 fi
95 count=$(($count + 1)) 95 count=$(($count + 1))
102 _hg() 102 _hg()
103 { 103 {
104 local cur prev cmd opts i 104 local cur prev cmd opts i
105 # global options that receive an argument 105 # global options that receive an argument
106 local global_args='--cwd|-R|--repository' 106 local global_args='--cwd|-R|--repository'
107 local hg="$1"
107 108
108 COMPREPLY=() 109 COMPREPLY=()
109 cur="$2" 110 cur="$2"
110 prev="$3" 111 prev="$3"
111 112
112 # searching for the command 113 # searching for the command
113 # (first non-option argument that doesn't follow a global option that 114 # (first non-option argument that doesn't follow a global option that
114 # receives an argument) 115 # receives an argument)
115 for (( i=1; $i<=$COMP_CWORD; i++ )); do 116 for ((i=1; $i<=$COMP_CWORD; i++)); do
116 if [[ ${COMP_WORDS[i]} != -* ]]; then 117 if [[ ${COMP_WORDS[i]} != -* ]]; then
117 if [[ ${COMP_WORDS[i-1]} != @($global_args) ]]; then 118 if [[ ${COMP_WORDS[i-1]} != @($global_args) ]]; then
118 cmd="${COMP_WORDS[i]}" 119 cmd="${COMP_WORDS[i]}"
119 break 120 break
120 fi 121 fi
122 done 123 done
123 124
124 if [[ "$cur" == -* ]]; then 125 if [[ "$cur" == -* ]]; then
125 opts=$(_hg_option_list $cmd) 126 opts=$(_hg_option_list $cmd)
126 127
127 COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W '$opts' -- "$cur") ) 128 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$opts' -- "$cur"))
128 return 129 return
129 fi 130 fi
130 131
131 # global options 132 # global options
132 case "$prev" in 133 case "$prev" in
144 _hg_commands 145 _hg_commands
145 return 146 return
146 fi 147 fi
147 148
148 # canonicalize command name 149 # canonicalize command name
149 cmd=$(hg -q help "$cmd" 2> /dev/null | sed -e 's/^hg //; s/ .*//; 1q') 150 cmd=$("$hg" -q help "$cmd" 2>/dev/null | sed -e 's/^hg //; s/ .*//; 1q')
150 151
151 if [ "$cmd" != status ] && [ "$prev" = -r ] || [ "$prev" = --rev ]; then 152 if [ "$cmd" != status ] && [ "$prev" = -r ] || [ "$prev" = --rev ]; then
152 _hg_tags 153 _hg_tags
153 return 154 return
154 fi 155 fi
188 clone) 189 clone)
189 local count=$(_hg_count_non_option) 190 local count=$(_hg_count_non_option)
190 if [ $count = 1 ]; then 191 if [ $count = 1 ]; then
191 _hg_paths 192 _hg_paths
192 fi 193 fi
193 _hg_repos 194 _hg_repos
194 ;; 195 ;;
195 debugindex|debugindexdot) 196 debugindex|debugindexdot)
196 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -X "!*.i" -- "$cur" )) 197 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -f -X "!*.i" -- "$cur"))
197 ;; 198 ;;
198 debugdata) 199 debugdata)
199 COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -X "!*.d" -- "$cur" )) 200 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -f -X "!*.d" -- "$cur"))
200 ;; 201 ;;
201 esac 202 esac
202 203
203 } 204 }
204 205
205 complete -o bashdefault -o default -F _hg hg 2> /dev/null \ 206 complete -o bashdefault -o default -F _hg hg 2>/dev/null \
206 || complete -o default -F _hg hg 207 || complete -o default -F _hg hg