1
0
dotfiles/.config/zsh/lib/completion.zsh

143 lines
3.4 KiB
Bash
Raw Normal View History

2021-03-21 18:25:10 +03:00
#!/bin/zsh
2024-02-18 15:10:19 +03:00
typeset -gA ZSHU_COMP_FORCE
2021-03-21 18:25:10 +03:00
ZSHU[f_compdump]="${ZSHU[d_cache]}/compdump"
2024-02-23 21:46:04 +03:00
ZSHU[d_completion]="${ZSHU[d_cache]}/completion"
ZSHU[d_compzwc]="${ZSHU[d_cache]}/compzwc"
2021-03-21 18:25:10 +03:00
ZSHU[d_compcache]="${ZSHU[d_cache]}/compcache"
2024-02-23 21:46:04 +03:00
typeset -a ZSHU_SYS_FPATH=( ${fpath} )
fpath=( "${ZSHU[d_compzwc]}" "${ZSHU[d_completion]}" ${fpath} )
2021-03-21 18:25:10 +03:00
__z_compdump_print() { printf '#zshu %s %s\n' "$1" "${(P)1}" ; }
__z_compdump_invalidate() {
2024-01-27 00:10:58 +03:00
rm -f "${ZSHU[f_compdump]}"
2022-09-05 10:35:13 +03:00
find "${ZSHU[d_compcache]}/" -xdev -type f '!' -name '.keep' -delete
2021-03-21 18:25:10 +03:00
ZSHU[compdump_refresh]=1
}
__z_compdump_verify() {
2022-07-19 02:41:45 +03:00
local i s
2024-01-16 02:55:39 +03:00
unset 'ZSHU[compdump_refresh]'
2021-03-21 18:25:10 +03:00
ZSHU[compdump_meta]='ZSH_VERSION ZSH_PATCHLEVEL FPATH PATH'
for i ( ${(s: :)ZSHU[compdump_meta]} ) ; do
s=$(__z_compdump_print "$i")
command grep -Fx -e "$s" "${ZSHU[f_compdump]}" &>/dev/null && continue
__z_compdump_invalidate
break
done
}
__z_compdump_finalize() {
local i
2022-07-19 02:41:45 +03:00
2021-03-21 18:25:10 +03:00
if (( ${+ZSHU[compdump_refresh]} )) ; then
{
echo
for i ( ${(s: :)ZSHU[compdump_meta]} ) ; do
__z_compdump_print "$i"
done
} | tee -a "${ZSHU[f_compdump]}" &>/dev/null
2024-01-16 02:55:39 +03:00
unset 'ZSHU[compdump_refresh]'
2021-03-21 18:25:10 +03:00
fi
2024-01-16 02:55:39 +03:00
unset 'ZSHU[compdump_meta]'
2021-03-21 18:25:10 +03:00
}
2024-02-18 02:32:08 +03:00
## TODO: refactor (e.g. buildah completion is a "bit" broken)
2021-03-21 18:25:10 +03:00
__z_comp_bash() {
2022-07-19 02:41:45 +03:00
local f p x
2021-03-21 18:25:10 +03:00
(( ${+commands[$1]} )) || return 1
(( ${+_comps[$1]} )) && return 2
(( ${+ZSHU[compdump_bash]} )) || return 3
(( ${+2} )) && return 0
2022-07-19 02:41:45 +03:00
2021-03-21 18:25:10 +03:00
f=0
for p ( /usr/share/bash-completion/completions ) ; do
2021-03-22 22:44:26 +03:00
x="_$1" ; [ -s "$p/$x" ] && f=1 && break
x="$1" ; [ -s "$p/$x" ] && f=1 && break
2021-03-21 18:25:10 +03:00
done
2021-03-22 22:44:26 +03:00
[ "$f" = 0 ] && return 4
complete -C "$x" "$1"
2022-07-19 02:41:45 +03:00
2021-03-21 18:25:10 +03:00
return 0
}
2021-03-22 22:57:36 +03:00
__z_comp_external() {
2024-02-18 02:32:08 +03:00
local c f
c="$1" ; shift
2022-07-19 02:41:45 +03:00
2024-02-18 02:32:08 +03:00
[ $# -gt 0 ] || return 1
2022-07-19 02:41:45 +03:00
2024-02-18 02:32:08 +03:00
(( ${+commands[$c]} )) || return 2
if ! (( ${+ZSHU_COMP_FORCE[$c]} )) ; then
2024-02-23 21:46:04 +03:00
(( ${+_comps[$c]} )) && return 0
2024-02-18 02:32:08 +03:00
fi
2024-02-23 21:46:04 +03:00
f="${ZSHU[d_completion]}/_$c"
2021-03-22 22:57:36 +03:00
if ! [ -s "$f" ] ; then
2024-02-18 02:32:08 +03:00
if ! "$@" > "$f" ; then
rm -f "$f"
return 3
fi
2021-03-22 22:57:36 +03:00
fi
2024-02-23 21:46:04 +03:00
# zcompile -zR "$f"
# mv -f "$f.zwc" "${ZSHU[d_compzwc]}/$c.zwc"
# emulate zsh -c "autoload -Uz _$c"
2024-02-18 02:32:08 +03:00
autoload -Uz "_$c"
2022-07-19 02:41:45 +03:00
2021-03-21 18:25:10 +03:00
return 0
}
2021-10-01 10:15:45 +03:00
2022-05-18 11:13:35 +03:00
__z_comp_system() {
2022-07-19 02:41:45 +03:00
local d
2022-05-18 11:13:35 +03:00
(( ${+commands[$1]} )) || return 1
(( ${+_comps[$1]} )) && return 2
2022-07-19 02:41:45 +03:00
2024-02-23 21:46:04 +03:00
(( ${+ZSHU_COMP_FORCE[$c]} )) && return 0
local -a _fpath
_fpath=( ${fpath} )
fpath=( ${ZSHU_SYS_FPATH} )
for d ( ${fpath} ) ; do
2022-05-18 11:13:35 +03:00
[ -s "$d/_$1" ] || continue
2024-02-23 21:46:04 +03:00
# emulate zsh -c "autoload -Uz _$1"
2022-05-18 11:13:35 +03:00
autoload -Uz "_$1"
2024-02-23 21:46:04 +03:00
fpath=( ${_fpath} )
2022-05-18 11:13:35 +03:00
return 0
2022-07-19 02:41:45 +03:00
done
2024-02-23 21:46:04 +03:00
fpath=( ${_fpath} )
2022-05-18 11:13:35 +03:00
return 3
}
2021-10-01 10:15:45 +03:00
## reload or new session are required to regenerate compcache
z-comp-invalidate() {
[ -n "$1" ] || return 1
2022-07-19 02:41:45 +03:00
2024-02-23 21:46:04 +03:00
# rm -f "${ZSHU[d_completion]}/_$1" "${ZSHU[d_compzwc]}/_$1.zwc" "${ZSHU[d_compzwc]}/$1.zwc"
rm -f "${ZSHU[d_completion]}/_$1"
2021-10-01 10:15:45 +03:00
}
2024-02-18 15:10:19 +03:00
## reload or new session are required to regenerate completions
z-comp-flush() {
2024-02-23 21:46:04 +03:00
find "${ZSHU[d_completion]}/" "${ZSHU[d_compzwc]}/" -xdev -type f '!' -name '.keep' -delete
2024-02-18 15:10:19 +03:00
}
z-comp-auto() {
local c f
for c ( ${(k)ZSHU_COMP_EXTERNAL} ) ; do
__z_comp_external "$c" "${(@s: :)ZSHU_COMP_EXTERNAL[$c]}" && unset "ZSHU_COMP_EXTERNAL[$c]"
done
for f ( ${functions[(I)__z_comp_ext__*]} ) ; do
c=${f#__z_comp_ext__}
__z_comp_external $c $f && unset -f "$f"
done
}