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

Re: completion within a function

On Fri, Jan 1, 2021 at 4:27 PM Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> Ahh ... because it's zpty, the cursor movements get captured along
> with everything else.

How about this?

ubuntu% complete git --

Earlier, Felipe wrote:
> LISTMAX=1000 # There seems to be a bug in zsh with several thousands

Could this be happening because "zpty -r" will only return up to one
megabyte of output?  If not, what's the symptom of the bug?

# Usage: complete commandline
# where "commandline" is one or more words, of which the last is the
# prefix of the desired completion.  Thus:
#  complete g
#    completes all commands whose name starts with "g"
#  complete git --
#    completes all options of "git" that begin with two hyphens
#  complete ls c
#    completes all files in $PWD having names starting with "c"
# All possible completions are returned, one per line.  There is a
# limit of approximately half a megabyte of total output, as a side-
# effect of vared plus the limit of one megabyte per "zpty -r".

(( $+_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}
  (( ARGC )) || print -nrl -- "~~~${(@)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() {
  (( ARGC )) || return 1
  local REPLY reply=( "$@" )
  zpty complete-tty run-complete
    zpty -r complete-tty REPLY $'*~~~*~~~'
  } always {
    zpty -d complete-tty
  reply=( "${(@)${(f)${${REPLY%~~~}#*~~~}}%$'\r'}" )
  shift $((ARGC-1)) reply
  print -lr -- "${(@)reply}"

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

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