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

Get completions from a script



Hi,

I finally finished working on the script to generate completions based
on some hypothetically typed characters.

For example:

  complete 'git s'

This comes from a discussion in zsh-users [1] in 2021 where different
methods were suggested.

The problem with using the complete-word approach is that it generates
prefixes that I don't want, for example given this:

  _foobar() {
    compset -P '*[=:]'
    compadd octopus ours recursive resolve subtree
  }
  compdef _foobar foobar

I would expect `complete 'git -s=re'` to only return "recursive", not
"-s=recursive".

The idea to use vared did actually work, and with that I'm able to get
rid of the separate file and to put everything in a single file of 29
lines.

Here it is:

  zmodload zsh/zpty
  autoload -U compinit && compinit -u
  setopt list_rows_first
  LISTMAX=1000

  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

  hide-vared() { compstate[vared]=''; }
  run_complete() {
  local -a compprefuncs=(hide-vared "${(@)compprefuncs}")
  local cmd="$1"
  vared -i zle_complete cmd
  }

  zpty c "run_complete \"$1\""
  zpty -r c log '*<END-CHOICES>'
  zpty -d c

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

Cheers.

[1] https://www.zsh.org/users/26334

-- 
Felipe Contreras




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