Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: completion within a function

On Wed, Dec 30, 2020 at 10:45 AM Ray Andrews <rayandrews@xxxxxxxxxxx> wrote:

> So the idea of invoking completion within a function is now a matter of
> curiosity  only.  Still I'm surprised it can't be done.  It's one of
> those things where zsh smartness is hard to turn off.

I can be done, it's just not straight-forward.

After removing all the unnecessary stuff from the tests, I came up
with this script that does list completions:

  ./complete "git --"


zmodload zsh/zpty

dir=$(mktemp -d)

cat > "$dir/.zshrc" <<'EOF'
setopt zle
setopt list_rows_first

fpath=($ZDOTDIR $fpath)
LISTMAX=1000 # There seems to be a bug in zsh with several thousands

autoload -U compinit && compinit -u

zstyle ":completion:*:default" list-colors "no=<NO>" "fi=<NO>"
"di=<NO>" "sp=<SP>" "lc=<LC>" "rc=<RC>" "ec=<EC>\n"
zstyle ':completion:*' verbose no

zle_complete () {
zle list-choices
zle kill-whole-line
print "<END-CHOICES>"
zle -N zle_complete
bindkey "^I" zle_complete

zpty zsh "ZDOTDIR=$dir zsh"
zpty -n -w zsh "$1"$'\t'
zpty -r zsh log '*<END-CHOICES>'

for x in ${(M)${(f)log}:#*'<LC><NO>'*}; do
print -- "${${x%'<EC>'*}#*'<RC>'}"

rm -r "$dir"

Felipe Contreras

Messages sorted by: Reverse Date, Date, Thread, Author