-
Notifications
You must be signed in to change notification settings - Fork 5
/
zsh-bash-completions-fallback.plugin.zsh
237 lines (198 loc) · 6.92 KB
/
zsh-bash-completions-fallback.plugin.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
[[ -o interactive ]] || return 0
_bash_completions_getter_path=${0:A:h}/bash-completions-getter.sh
function _bash_completions_fallback_completer {
emulate -L zsh
unsetopt nomatch badpattern
local out=("${(@f)$( \
ZSH_NAME="$name" \
ZSH_BUFFER="$BUFFER" \
ZSH_CURSOR="$CURSOR" \
ZSH_WORDBREAKS="$WORDCHARS" \
ZSH_WORDS="${words[@]}" \
ZSH_CURRENT=$((CURRENT-1)) \
bash -c \
"source ${_bash_completions_getter_path}; get_completions")}");
local -a -U bopts=("${(ps: :)${(@f)out:0:1}}");
local -a -U bactions=("${(ps: :)${(@f)out:1:2}}");
local -a -U bcompletions=("${(@f)out:2}")
local -a -U compoptions=()
if ((${bopts[(Ie)nospace]})); then
compoptions+=(-S '')
fi
if ((${bopts[(Ie)nosort]})); then
compoptions+=(-o 'nosort')
fi
local compadd_opts=("${compoptions[@]}")
if [ -n "$bcompletions" ]; then
compadd_opts+=(-Q)
if ((${bopts[(Ie)filenames]})); then
compset -P '*/' && \
bcompletions=(${bcompletions##*/})
compset -S '/*' && \
bcompletions=(${bcompletions%%/*})
compadd_opts+=(-f)
fi
fi
if [ -z "$bcompletions" ] ||
! compadd "${compadd_opts[@]}" -a bcompletions; then
if ((${bopts[(Ie)default]})) || ((${bopts[(Ie)bashdefault]})); then
_default "${compoptions[@]}"
elif ((${bopts[(Ie)dirnames]})); then
_directories "${compoptions[@]}"
fi
fi
if ((${bopts[(Ie)plusdirs]})); then
_directories "${compoptions[@]}"
fi
return $?
}
function _bash_completions_fetch_supported_commands {
emulate -L zsh
setopt extended_glob typeset_silent no_short_loops
unsetopt nomatch
local bash_completions=${ZSH_BASH_COMPLETIONS_FALLBACK_PATH:-${${(@s/:/)${XDG_DATA_DIRS:-/usr/share}}[1]}/bash-completion}
local -a dirs=(
${BASH_COMPLETION_USER_DIR:-${XDG_DATA_HOME:-$HOME/.local/share}/bash-completion}/completions
)
for dir in ${(@s/:/)${XDG_DATA_DIRS:-/usr/local/share:/usr/share}}; do
dirs+=("$dir/bash-completion/completions")
done
dirs+=("$bash_completions/completions")
for dir in "${dirs[@]}"; do
for c in "$dir"/*; do
[ ! -f "$c" ] && continue
local command=${${${c:t}#_}%.bash};
_bash_completions_commands+=($command)
done
done
if [ -n "${ZSH_BASH_COMPLETIONS_FALLBACK_LOAD_NATIVE_COMPLETIONS-:true}" ]; then
local out=("${(u@f)$( \
bash -c \
"source ${_bash_completions_getter_path}; get_defined_completions")}");
_bash_completions_commands+=($out)
fi
}
function _bash_completions_load {
local bash_completions=${ZSH_BASH_COMPLETIONS_FALLBACK_PATH:-${${(@s/:/)${XDG_DATA_DIRS:-/usr/share}}[1]}/bash-completion}
local reserved_words=(
"do"
"done"
"esac"
"then"
"elif"
"else"
"fi"
"for"
"case"
"if"
"while"
"function"
"repeat"
"time"
"until"
"select"
"coproc"
"nocorrect"
"foreach"
"end"
"declare"
"export"
"float"
"integer"
"local"
"readonly"
"typeset"
)
if ! [ -f /etc/bash_completion ] &&
! [ -f "$bash_completions/bash_completion" ]; then
return 1;
fi
local -a -U _bash_completions_commands=()
_bash_completions_fetch_supported_commands
for completion in $_bash_completions_commands; do
if [ ${#ZSH_BASH_COMPLETIONS_FALLBACK_WHITELIST} -gt 0 ] &&
! ((${ZSH_BASH_COMPLETIONS_FALLBACK_WHITELIST[(I)${completion}]})); then
continue;
fi
if [ -z "$ZSH_BASH_COMPLETIONS_FALLBACK_PRELOAD_ALL" ] &&
! [[ -v commands[$completion] ]] &&
! [[ -v aliases[$completion] ]]; then
continue;
elif ((${reserved_words[(Ie)${completion}]})); then
continue;
fi
if ((${ZSH_BASH_COMPLETIONS_FALLBACK_BLACKLIST[(Ie)${completion}]})); then
continue;
fi
if ((${ZSH_BASH_COMPLETIONS_FALLBACK_REPLACE_LIST[(Ie)${completion}]})) ||
[ -n "$ZSH_BASH_COMPLETIONS_FALLBACK_REPLACE_ALL" ] ||
! [[ -v _comps[$completion] ]]; then
compdef _bash_completions_fallback_completer $completion
fi
done
}
function _bash_completion_get_current_tab_completer()
{
local current_binding=(${$(bindkey '^I')})
echo "${${current_binding[2]}:-expand-or-complete}"
}
typeset -g _bash_completions_loaded=
integer -g _bash_completions_available=0
integer -g _bash_completions_last_checked_timestamp=$EPOCHSECONDS
function _bash-completion-init-and-continue()
{
local update_threshold=${ZSH_BASH_COMPLETIONS_FALLBACK_AUTO_UPDATE_THRESHOLD:-300}
if [ -n "$ZSH_BASH_COMPLETIONS_FALLBACK_LAZYLOAD_AUTO_UPDATE" ] &&
[ -n "$EPOCHSECONDS" ] &&
[ $EPOCHSECONDS -gt \
$((_bash_completions_last_checked_timestamp + update_threshold)) ]; then
local -a -U _bash_completions_commands=()
_bash_completions_fetch_supported_commands
if [ $_bash_completions_available -ne ${#_bash_completions_commands[@]} ]; then
typeset -g _bash_completions_available=${#_bash_completions_commands[@]}
typeset -g _bash_completions_loaded=
fi
fi
if [ -n "$_bash_completions_loaded" ]; then
zle $_bash_completion_previous_binding
return $?
fi
local current_binding=$(_bash_completion_get_current_tab_completer)
(( ${+functions[_bash_completions_lazy_load]} )) && \
unfunction _bash_completions_lazy_load
_bash_completions_load
_bash_completions_loaded=1
if [[ "$current_binding" == "_bash-completion-init-and-continue" ]]; then
bindkey "^I" $_bash_completion_previous_binding
zle $_bash_completion_previous_binding
unset _bash_completion_previous_binding
unfunction _bash-completion-init-and-continue
return
fi
zle $_bash_completion_previous_binding
}
function _bash_completions_lazy_load()
{
local default_binding=$(_bash_completion_get_current_tab_completer)
typeset -g _bash_completion_previous_binding=$default_binding
zle -N _bash-completion-init-and-continue
bindkey "^I" _bash-completion-init-and-continue
}
if [ -n "$ZSH_BASH_COMPLETIONS_FALLBACK_LAZYLOAD_DISABLE" ]; then
_bash_completions_load
if [ -n "$ZSH_BASH_COMPLETIONS_FALLBACK_LAZYLOAD_AUTO_UPDATE" ]; then
_bash_completions_lazy_load
fi
else
_bash_completions_lazy_load
fi
function _bash_completions_fallback_list_handled_completions()
{
local -a our_completions=();
for command completion in ${(kv)_comps}; do
if [ "$completion" = _bash_completions_fallback_completer ]; then
our_completions+=($command)
fi
done
printf "%-32s\n" ${(o)our_completions[@]}
}