zsh-workers
 help / color / mirror / code / Atom feed
From: Felipe Contreras <felipe.contreras@gmail.com>
To: Zsh Users <zsh-users@zsh.org>,
	Zsh hackers list <zsh-workers@zsh.org>,
	 Bart Schaefer <schaefer@brasslantern.com>
Subject: Get completions from a script
Date: Thu, 8 Sep 2022 21:59:47 -0500	[thread overview]
Message-ID: <CAMP44s2YGfh3Dmy77akyV56fV8jjoC84sLsFFDks0o6LH9s7-w@mail.gmail.com> (raw)

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


                 reply	other threads:[~2022-09-09  3:03 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAMP44s2YGfh3Dmy77akyV56fV8jjoC84sLsFFDks0o6LH9s7-w@mail.gmail.com \
    --to=felipe.contreras@gmail.com \
    --cc=schaefer@brasslantern.com \
    --cc=zsh-users@zsh.org \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).