26763: fix problem on failed cd -s to relative path
[zsh.git] / Completion / bashcompinit
blobcba436a55193753366d3597b5616ca541ede3dbb
1 #autoload
3 _bash_complete() {
4   local ret=1
5   local -a suf matches
6   local COMP_POINT COMP_CWORD
7   local -a COMP_WORDS COMPREPLY BASH_VERSINFO
8   local COMP_LINE="$words"
9   local -A savejobstates savejobtexts
11   (( COMP_POINT = 1 + ${#${(j. .)words[1,CURRENT-1]}} + $#QIPREFIX + $#IPREFIX + $#PREFIX ))
12   (( COMP_CWORD = CURRENT - 1))
13   COMP_WORDS=( $words )
14   BASH_VERSINFO=( 2 05b 0 1 release )
15   
16   savejobstates=( ${(kv)jobstates} )
17   savejobtexts=( ${(kv)jobtexts} )
18   
19   [[ ${argv[${argv[(I)nospace]:-0}-1]} = -o ]] && suf=( -S '' )
21   matches=( ${(f)"$(compgen $@)"} )
22   
23   if [[ -n $matches ]]; then
24     if [[ ${argv[${argv[(I)filenames]:-0}-1]} = -o ]]; then
25       compset -P '*/' && matches=( ${matches##*/} )
26       compset -S '/*' && matches=( ${matches%%/*} )
27       compadd -f "${suf[@]}" -a matches && ret=0
28     else
29       compadd "${suf[@]}" -a matches && ret=0
30     fi
31   fi
33   if (( ret )); then
34     if [[ ${argv[${argv[(I)default]:-0}-1]} = -o ]]; then
35       _default "${suf[@]}" && ret=0
36     elif [[ ${argv[${argv[(I)dirnames]:-0}-1]} = -o ]]; then
37       _directories "${suf[@]}" && ret=0
38     fi
39   fi
41   return ret
44 compgen() {
45   local opts prefix suffix job OPTARG OPTIND ret=1 
46   local -a name res results jids
47   local -A shortopts
48   
49   emulate -L sh
50   setopt kshglob noshglob braceexpand nokshautoload
52   shortopts=(
53     a alias b builtin c command d directory e export f file
54     g group j job k keyword u user v variable
55   )
57   while getopts "o:A:G:C:F:P:S:W:X:abcdefgjkuv" name; do
58     case $name in
59       [abcdefgjkuv]) OPTARG="${shortopts[$name]}" ;&
60       A)
61         case $OPTARG in
62           alias) results+=( "${(k)aliases[@]}" ) ;;
63           arrayvar) results+=( "${(k@)parameters[(R)array*]}" ) ;;
64           binding) results+=( "${(k)widgets[@]}" ) ;;
65           builtin) results+=( "${(k)builtins[@]}" "${(k)dis_builtins[@]}" ) ;;
66           command)
67             results+=(
68               "${(k)commands[@]}" "${(k)aliases[@]}" "${(k)builtins[@]}"
69               "${(k)functions[@]}" "${(k)reswords[@]}"
70             )
71           ;;
72           directory)
73             setopt bareglobqual
74             results+=( ${IPREFIX}${PREFIX}*${SUFFIX}${ISUFFIX}(N-/) )
75             setopt nobareglobqual
76           ;;
77           disabled) results+=( "${(k)dis_builtins[@]}" ) ;;
78           enabled) results+=( "${(k)builtins[@]}" ) ;;
79           export) results+=( "${(k)parameters[(R)*export*]}" ) ;;
80           file)
81             setopt bareglobqual
82             results+=( ${IPREFIX}${PREFIX}*${SUFFIX}${ISUFFIX}(N) )
83             setopt nobareglobqual
84           ;;
85           function) results+=( "${(k)functions[@]}" ) ;;
86           group)
87             emulate zsh
88             _groups -U -O res
89             emulate sh
90             setopt kshglob noshglob braceexpand
91             results+=( "${res[@]}" )
92           ;;
93           hostname)
94             emulate zsh
95             _hosts -U -O res
96             emulate sh
97             setopt kshglob noshglob braceexpand
98             results+=( "${res[@]}" )
99           ;;
100           job) results+=( "${savejobtexts[@]%% *}" );;
101           keyword) results+=( "${(k)reswords[@]}" ) ;;
102           running)
103             jids=( "${(@k)savejobstates[(R)running*]}" )
104             for job in "${jids[@]}"; do
105               results+=( ${savejobtexts[$job]%% *} )
106             done
107           ;;
108           stopped)
109             jids=( "${(@k)savejobstates[(R)suspended*]}" )
110             for job in "${jids[@]}"; do
111               results+=( ${savejobtexts[$job]%% *} )
112             done
113           ;;
114           setopt|shopt) results+=( "${(k)options[@]}" ) ;;
115           signal) results+=( "SIG${^signals[@]}" ) ;;
116           user) results+=( "${(k)userdirs[@]}" ) ;;
117           variable) results+=( "${(k)parameters[@]}" ) ;;
118           helptopic) ;;
119         esac
120       ;;
121       F)
122         COMPREPLY=()
123         $OPTARG "${words[0]}" "${words[CURRENT-1]}" "${words[CURRENT-2]}"
124         results+=( "${COMPREPLY[@]}" )
125       ;;
126       G)
127         setopt nullglob
128         results+=( ${~OPTARG} )
129         unsetopt nullglob
130       ;;
131       W) eval "results+=( $OPTARG )" ;;
132       C) results+=( $(eval $OPTARG) ) ;;
133       P) prefix="$OPTARG" ;;
134       S) suffix="$OPTARG" ;;
135       X)
136         if [[ ${OPTARG[0]} = '!' ]]; then
137           results=( "${(M)results[@]:#${OPTARG#?}}" )
138         else
139           results=( "${results[@]:#$OPTARG}" )
140         fi
141       ;;
142     esac
143   done
144   
145   # support for the last, `word' option to compgen. Zsh's matching does a
146   # better job but if you need to, comment this in and use compadd -U
147   #shift $(( OPTIND - 1 ))
148   #(( $# )) && results=( "${(M)results[@]:#$1*}" )
150   print -l -- "$prefix${^results[@]}$suffix"
153 complete() {
154   emulate -L zsh
155   local args void cmd print remove
156   args=( "$@" )
157   zparseopts -D -a void o: A: G: W: C: F: P: S: X: a b c d e f g j k u v \
158       p=print r=remove
159   if [[ -n $print ]]; then
160     printf 'complete %2$s %1$s\n' "${(@kv)_comps[(R)_bash*]#* }"
161   elif [[ -n $remove ]]; then
162     for cmd; do
163       unset "_comps[$cmd]"
164     done
165   else
166     compdef _bash_complete\ ${(j. .)${(q)args[1,-1-$#]}} "$@"
167   fi
170 unfunction bashcompinit
171 autoload -Uz bashcompinit
172 return 0