Skip to content

Commit

Permalink
Make the use of private compatible w/ zcompile
Browse files Browse the repository at this point in the history
Fixes #335.
  • Loading branch information
marlonrichert committed Oct 6, 2021
1 parent 6f01370 commit b76e0b1
Show file tree
Hide file tree
Showing 18 changed files with 146 additions and 154 deletions.
10 changes: 5 additions & 5 deletions functions/completion/_autocomplete.ancestor_dirs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@
[[ $_comp_tags == *( | local-)'directories '* ]] ||
return

private tag=ancestor-directories
local -P tag=ancestor-directories
_tags $tag
_tags ||
return
_requested $tag ||
return

private -a ancestors=()
private parent=$PWD:h
local -Pa ancestors=()
local -P parent=$PWD:h
while (( $#parent > 1 )); do
ancestors+=( $parent )
parent=$parent:h
done

local displ= expl=
private -i ret=1
private a=
local -Pi ret=1
local -P a=
while _next_label -V $tag expl 'ancestor directory'; do
for a in ${(aO)ancestors[@]}; do
displ=( "$a" )
Expand Down
2 changes: 1 addition & 1 deletion functions/completion/_autocomplete.command
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#compdef -command-
private ret=1
local -P ret=1
_expand_alias "$@" &&
ret=0
_autocd "$@" &&
Expand Down
2 changes: 1 addition & 1 deletion functions/completion/_autocomplete.compadd_opts_len
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#autoload

private -i len=$(( ${@[(i)(-|--)]} - 1 ))
local -Pi len=$(( ${@[(i)(-|--)]} - 1 ))
(( len < $# )) &&
return len

Expand Down
36 changes: 18 additions & 18 deletions functions/completion/_autocomplete.history_lines
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
#autoload

_autocomplete.history_lines() {
private tag=history-lines
local -P tag=history-lines
_tags $tag
_tags ||
return 1
_requested $tag ||
return 1

private -A events=()
private -i min_lines= max_lines=
local -PA events=()
local -Pi min_lines= max_lines=
builtin zstyle -s ":autocomplete:${curcontext}:" list-lines min_lines ||
min_lines=16
(( max_lines = min( min_lines, HISTNO - 1, LINES - BUFFERLINES ) ))

if [[ -z $BUFFER ]]; then
if [[ $curcontext == *-forward:* ]]; then
private -i inc=1 histno=1
local -Pi inc=1 histno=1
else
private -i inc=-1 histno=$(( HISTNO - 1 ))
local -Pi inc=-1 histno=$(( HISTNO - 1 ))
fi
if [[ -o histfindnodups ]]; then
while (( $#events[@] < max_lines && 0 < histno && histno < HISTNO )); do
Expand All @@ -35,38 +35,38 @@ _autocomplete.history_lines() {
done
fi
else
private word=$IPREFIX$PREFIX$SUFFIX$ISUFFIX
private -aU err=( '(#ia'{0..$(( $#words[CURRENT] / 3 ))}')' )
local -P word=$IPREFIX$PREFIX$SUFFIX$ISUFFIX
local -PaU err=( '(#ia'{0..$(( $#words[CURRENT] / 3 ))}')' )
if [[ $WIDGET == (|*-)search(|-*) ]]; then
private -aU queries=( \*$^err${(j:*:)${(0)words[CURRENT]}}\* )
local -PaU queries=( \*$^err${(j:*:)${(0)words[CURRENT]}}\* )
else
private lbuffer="${(j.[[:blank:]]##.)${(@b)words[1,CURRENT-1]}}"
private rbuffer="${(j.[[:blank:]]##.)${(@b)words[CURRENT+1,-1]}}"
local -P lbuffer="${(j.[[:blank:]]##.)${(@b)words[1,CURRENT-1]}}"
local -P rbuffer="${(j.[[:blank:]]##.)${(@b)words[CURRENT+1,-1]}}"
[[ -n $lbuffer ]] &&
lbuffer="${lbuffer}[[:blank:]]##"
[[ -n $rbuffer ]] &&
rbuffer="[[:blank:]]##${rbuffer}"
lbuffer="$lbuffer${(b)QIPREFIX}"
rbuffer="${(b)QISUFFIX}$rbuffer"
private -aU queries=( $lbuffer\*\($^err${words[CURRENT]}\)\*$rbuffer )
local -PaU queries=( $lbuffer\*\($^err${words[CURRENT]}\)\*$rbuffer )
fi
if [[ -o histfindnodups ]]; then
private -a numbers=()
private q=
local -Pa numbers=()
local -P q=
for q in $queries[@]; do
numbers=( "${(@k)history[(R)$~q]}" )
(( $#numbers[@] )) &&
break
done
private -a lines=( "${(@v)history[(I)(${(~j:|:)numbers})]}" )
local -Pa lines=( "${(@v)history[(I)(${(~j:|:)numbers})]}" )
if [[ $curcontext != *-forward:* ]]; then
numbers=( "${(@Oa)numbers}" )
lines=( "${(@Oa)lines}" )
fi
events=( "${(@)lines:^numbers}" )
events=( "${(@Oa)${(@kv)events}}" )
else
private q=
local -P q=
for q in $queries[@]; do
events=( "${(@kv)history[(R)$~q]}" )
(( $#events[@] )) &&
Expand All @@ -82,8 +82,8 @@ _autocomplete.history_lines() {
printf -v displays "%s:%s" "${(@kv)events}"
displays=( "$displays[@]" )

private -i diff=$(( $#displays[@] - max_lines ))
private suf=
local -Pi diff=$(( $#displays[@] - max_lines ))
local -P suf=
if [[ $WIDGET == (|*-)search(|-*) ]]; then
displays=( ${(@n)displays} )
(( diff > 0 )) &&
Expand All @@ -104,7 +104,7 @@ if [[ $WIDGET == (|*-)search(|-*) ]]; then
[[ $WIDGET != (|*-)search(|-*) ]] &&
matches=( "${(@)${(@)matches[@]##$~lbuffer}%%$~rbuffer}" )

private -a match=() mbegin=() mend=()
local -Pa match=() mbegin=() mend=()
displays=(
"${(@r:COLUMNS-1:)displays[@]/%(#b)(<->):(*)/${(l:$#HISTNO:)match[1]} ${(V)${(Z+C+)match[2]}}}"
)
Expand Down
10 changes: 5 additions & 5 deletions functions/completion/_autocomplete.recent_paths
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ _autocomplete.recent_paths() {
(( compstate[nmatches] > 1 )) &&
return 1

private -aU dirs=()
private -i ret=1
private tag=
local -PaU dirs=()
local -Pi ret=1
local -P tag=
_tags recent-directories recent-files
while _tags; do

Expand Down Expand Up @@ -45,7 +45,7 @@ _autocomplete.recent_paths() {

.autocomplete.recent_paths.trim

private -i ret=1
local -Pi ret=1

# Work around `setopt autonamedirs` by not assigning absolute paths to scalars.
while (( $#matches )); do
Expand All @@ -61,7 +61,7 @@ _autocomplete.recent_paths() {

# Workaround to prevent the prompt from jumping
.autocomplete.recent_paths.trim() {
private total="${(j:/ :)displ}/ "
local -P total="${(j:/ :)displ}/ "
while (( $#total > COLUMNS )); do
shift -p -- displ matches
total="${(j:/ :)displ}/ "
Expand Down
4 changes: 2 additions & 2 deletions functions/completion/_autocomplete.requoted
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#autoload

private tag=requoted
local -P tag=requoted
_tags $tag
_tags && _requested $tag ||
return 1
Expand All @@ -12,7 +12,7 @@ _autocomplete.is_glob &&
[[ $_completer == expand ]] ||
return 1

private match=${(Q)compstate[unambiguous]}
local -P match=${(Q)compstate[unambiguous]}

[[ $match == ($words[CURRENT]|$compstate[unambiguous]) ]] &&
return 1
Expand Down
6 changes: 3 additions & 3 deletions functions/completion/_autocomplete.unambiguous
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
$IPREFIX$PREFIX$SUFFIX$ISUFFIX == *${compstate[unambiguous]:#?}* ]] &&
return 1

private tag=unambiguous
local -P tag=unambiguous
_tags $tag
_tags ||
return
Expand All @@ -20,8 +20,8 @@ _requested $tag ||
typeset -gH _autocomplete__unambiguous="$compstate[unambiguous]"

if builtin zstyle -t :autocomplete:tab: insert-unambiguous; then
private -i nxt= prv=1
private subs= cols=
local -Pi nxt= prv=1
local -P subs= cols=
for nxt in ${(s.:.)compstate[unambiguous_positions]}; do
subs+='('${(b)compstate[unambiguous][prv,nxt]}')*'
cols+='=30;103' # black foreground, bright yellow background
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/zsh
zmodload -F zsh/terminfo p:terminfo

private key_name=
local -P key_name=
case $KEYS in
( $'\t' ) key_name=tab ;;
( $terminfo[kcbt] ) key_name=shift-tab ;;
Expand Down
7 changes: 3 additions & 4 deletions functions/widget/.autocomplete.complete-word.post
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#autoload
builtin autoload -Uz is-at-least

unset MENUSELECT 'compstate[list]'
compstate[insert]=

private key_name=
local -P key_name=
case $KEYS in
( $'\t' ) key_name=tab ;;
( $terminfo[kcbt] ) key_name=shift-tab ;;
Expand All @@ -22,7 +21,7 @@ if [[ -n $_autocomplete__unambiguous ]] &&
return
fi

private -i nmatches=0
local -Pi nmatches=0
if [[ $compstate[old_list] == keep ]]; then
nmatches=$_lastcomp[nmatches]
else
Expand Down Expand Up @@ -57,7 +56,7 @@ if [[ $RBUFFER != [[:space:]]* ]]; then
local -a spacetags=()
builtin zstyle -a :autocomplete: add-space spacetags ||
spacetags=( executables aliases functions builtins reserved-words commands )
private -a comptags=()
local -Pa comptags=()
if [[ $compstate[old_list] == keep ]]; then
comptags=( $=_lastcomp[tags] )
else
Expand Down
4 changes: 2 additions & 2 deletions functions/widget/.autocomplete.history-search.zle-widget
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ ${0}.context() {
}

${0}() {
private lbuffer="$LBUFFER" rbuffer="$RBUFFER"
private keymap_menuselect="$( builtin bindkey -M menuselect -L )"
local -P lbuffer="$LBUFFER" rbuffer="$RBUFFER"
local -P keymap_menuselect="$( builtin bindkey -M menuselect -L )"
typeset -g ZLE_REMOVE_SUFFIX_CHARS=${ZLE_REMOVE_SUFFIX_CHARS/ }
{
builtin bindkey -M menuselect -s \
Expand Down
63 changes: 45 additions & 18 deletions scripts/.autocomplete.__init__
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,36 @@ zmodload -F zsh/parameter p:functions
zmodload -F zsh/system p:sysparams
zmodload -F zsh/zleparameter p:widgets
zmodload -Fa zsh/zutil b:zstyle
builtin autoload -Uz add-zsh-hook zmathfunc
builtin autoload +X -Uz add-zsh-hook zmathfunc
zmathfunc

typeset -gHa _autocomplete__func_opts=(
localoptions extendedglob clobber
NO_aliases localloops pipefail NO_shortloops NO_unset warncreateglobal
)
setopt $_autocomplete__func_opts[@]

typeset -gHa _autocomplete__comp_opts=( localoptions NO_banghist NO_completeinword )
typeset -gHa _autocomplete__ctxt_opts=( completealiases completeinword )

.autocomplete.__init__.precmd() {
# Workaround for https://github.com/zdharma/zinit/issues/366
# NOTE: Needs to come before _everything_ else!
[[ -v functions[.zinit-shade-off] ]] &&
.zinit-shade-off "${___mode:-load}"
.zinit-shade-off "${___mode:-load}"
[[ -v functions[.zinit-tmp-subst-off] ]] &&
.zinit-tmp-subst-off "${___mode:-load}"
.zinit-tmp-subst-off "${___mode:-load}"

() {
emulate -L zsh
setopt $_autocomplete__func_opts[@]

# Workaround for https://github.com/zdharma/zinit/issues/366
[[ -v functions[.zinit-shade-off] ]] &&
.zinit-shade-off "${___mode:-load}"

add-zsh-hook -d precmd .autocomplete.__init__.precmd
unfunction .autocomplete.__init__.precmd

# Workaround for https://www.zsh.org/mla/workers/2021/msg01310.html
if builtin zstyle -L zle-hook types > /dev/null; then
private hook=
local -P hook=
for hook in \
zle-{isearch-{exit,update},line-{pre-redraw,init,finish},history-line-set,keymap-select}
do
Expand All @@ -39,29 +44,49 @@ zmathfunc
fi
}

private mod=
local -P mod=
for mod in compinit config widget key-binding recent-dirs async; do
mod=.autocomplete.$mod.precmd
if [[ -v functions[$mod] ]]; then
$mod
unfunction $mod
fi
done

true
}

private xdg_data_home=${XDG_DATA_HOME:-$HOME/.local/share}
local -P basedir=${${(%):-%x}:P:h:h}
if ! [[ -n $basedir && -d $basedir ]]; then
print -u2 -- 'zsh-autocomplete: Failed to find base dir. Aborting.'
return 66
fi
hash -d zsh-autocomplete=$basedir
typeset -gU FPATH fpath=( ~zsh-autocomplete/functions/completion $fpath[@] )

local -Pa funcs=(
~zsh-autocomplete/functions{,/widget}/.autocomplete.*~*.zwc(N-.:P)
)
if ! (( $#funcs )); then
print -u2 -- 'zsh-autocomplete: Failed to find functions. Aborting.'
return 66
fi
unfunction $funcs[@]:t 2> /dev/null
builtin autoload -UWz $funcs[@]

private zsh_data_dir=$xdg_data_home/zsh
local -P xdg_data_home=${XDG_DATA_HOME:-$HOME/.local/share}

local -P zsh_data_dir=$xdg_data_home/zsh
[[ -d $zsh_data_dir ]] ||
zf_mkdir -pm 0700 $zsh_data_dir

private logdir=$xdg_data_home/zsh-autocomplete/log
local -P logdir=$xdg_data_home/zsh-autocomplete/log
[[ -d $logdir ]] ||
zf_mkdir -p -- $logdir
private -a older_than_a_week=( $logdir/*(Nmd+7) )
local -Pa older_than_a_week=( $logdir/*(Nmd+7) )
(( $#older_than_a_week[@] )) &&
zf_rm -f -- $older_than_a_week[@]
private date=${(%):-%D{%F}}
local -P date=${(%):-%D{%F}}
typeset -gH _autocomplete__log=$logdir/$date.log
typeset -gH _autocomplete__log_async=$logdir/$date.async.log
typeset -gH _autocomplete__log_pty=$logdir/$date.pty.log
Expand All @@ -73,14 +98,14 @@ typeset -gH _autocomplete__ps4=\
'${_autocomplete__prev::=${(%)_autocomplete__next}}${(%)_autocomplete__time}}'\
$'%I%(?,\t, ->%?\t)%(1_,%_ ,)'

private zsh_cache_dir=${XDG_CACHE_HOME:-$HOME/.cache}/zsh
local -P zsh_cache_dir=${XDG_CACHE_HOME:-$HOME/.cache}/zsh
[[ -d $zsh_cache_dir ]] ||
zf_mkdir -pm 0700 $zsh_cache_dir

private mod=
local -P mod=
for mod in compinit config widget key-binding recent-dirs async; do
if builtin zstyle -T ':autocomplete:' $mod; then
builtin autoload -Uz ~zsh-autocomplete/scripts/.autocomplete.$mod
builtin autoload +X -Uz ~zsh-autocomplete/scripts/.autocomplete.$mod
{
.autocomplete.$mod "$@"
} always {
Expand All @@ -91,8 +116,10 @@ done

add-zsh-hook precmd .autocomplete.__init__.precmd

# Ensure we run before zsh-autosuggestions.
# Ensure our precmd runs before zsh-autosuggestions'.
[[ -v precmd_functions && $precmd_functions[(I)_zsh_autosuggest_start] != 0 ]] &&
typeset -gHaU precmd_functions=(
${precmd_functions[@]:#_zsh_autosuggest_start} _zsh_autosuggest_start
)

true
Loading

0 comments on commit b76e0b1

Please sign in to comment.