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

Re: completion within a function

On Wed, Dec 30, 2020 at 5:04 PM Felipe Contreras
<felipe.contreras@xxxxxxxxx> wrote:
> With that I do see the output. There's some garbage in it, but it works.

I'm curious what the garbage is?  Maybe something needs redirection.

Attached (to prevent gmail line wrapping) is a still-fragmentary and
undocumented source-able implementation.

* It works better to discard compstate[vared] than to muck with _comps[-vared-]
* Uses a restricted keymap to avoid messing with the main keymap.
* Some names "protected" with double-underscore as in zed.
* Included a debug function although it's a bit knobbly.

At an interactive command-line, you can type e.g.
  run-complete git --
to see what the completion might be.  I wonder about attaching this to
something ala run-help.

However, that doesn't work if output is redirected anywhere.  If you
want to pipe it or put it in a command substitution, you have to use
  complete git -- | less
  guesses=( $(complete git --) )

Also, each word has to be individually quoted, you won't get useful results from
  complete "git --"

(( $+_comps )) || { autoload -U compinit; compinit -i -D }
zmodload zsh/zpty

if ! bindkey -M __complete 2>/dev/null
  bindkey -N __complete
  zle -C __complete_all complete-word _generic
  zstyle ':completion:__complete_all::::' completer _all_matches _complete
  zstyle ':completion:__complete_all:*' insert true

  bindkey -M __complete '^Xa' __complete_all
  # bindkey -M __complete '^X?' _complete_debug
  bindkey -M __complete $'\n' .accept-line
  # bindkey -M __complete '^G' .send-break

  __init_complete() { zle -U $'\Cxa\n' }
  zle -N __init_complete

completion-context() {
  if (( debug )); then
    print -u $debug -C 2 -a -r \
    CONTEXT: ":completion:$curcontext" \
    STATE: '' "${(@kv)compstate}"
hide-vared() { compstate[vared]='' }

run-complete () {
  local -a compprefuncs=(hide-vared "${(@)compprefuncs}")
  vared -M __complete -i __init_complete ${${argv:+argv}:-reply}

debug-complete() {
  local -i debug
  local -a compprefuncs=(completion-context "${(@)compprefuncs}")
  local -a comppostfuncs=("${(@)comppostfuncs}" completion-context)
  exec {debug}>&2
  complete "$@"
  exec {debug}>&-

complete() {
  local -a reply=( "$@" )
  zpty complete-tty run-complete
  { zpty -r complete-tty } always { zpty -d complete-tty }

(( ARGC )) && complete "$@"

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