Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: completion within a function
- X-seq: zsh-users 26320
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: Felipe Contreras <felipe.contreras@xxxxxxxxx>
- Cc: Zsh Users <zsh-users@xxxxxxx>
- Subject: Re: completion within a function
- Date: Sun, 3 Jan 2021 09:50:55 -0800
- Archived-at: <https://zsh.org/users/26320>
- Archived-at: <http://www.zsh.org/sympa/arcsearch_id/zsh-users/2021-01/CAH%2Bw%3D7bhg0Q_5t3dnKBCJmnq27S4PzRsrE8QY_3mPwTC96_wtw%40mail.gmail.com>
- In-reply-to: <CAMP44s1f7LWrCpcuQhgAiDDUzrLse50S6cNLH4VXjidN=nAbMg@mail.gmail.com>
- List-id: <zsh-users.zsh.org>
- References: <c9416d92-29b1-68e5-e51e-d7152f1fbc4b@eastlink.ca> <CAH+w=7bT5GaGvWih=A8KCyUe4N6k25VxDLcpPA5C1yGn38Zimg@mail.gmail.com> <819ec6fe-5af2-2e97-c5bb-f434b1de86d7@eastlink.ca> <CAH+w=7bSbhdHGC89daK+8o0psLhdw969=GviC4s554t37_zrDw@mail.gmail.com> <520ce98a-4394-726d-3d12-8ef414e881c9@eastlink.ca> <CAN=4vMquijkmnETv2xUdnpGcJoQPaimXT1+i+daHoivLmpK+TA@mail.gmail.com> <f490bbd8-dc96-341d-e11b-a5d957c9268e@eastlink.ca> <CAMP44s13tqgmNgUcuAxUJyJ_LGjDSEE1FJ64k2r12UogbW0r0w@mail.gmail.com> <CAH+w=7a+9_jX8xkvKjCVhQWXQMqQkWvahHyzTj=Lm_DgC=o5kw@mail.gmail.com> <CAMP44s1KT0B-wGmEndZUW2xkh8t-mawKA3y5yaqAH3GjiEXuCA@mail.gmail.com> <CAH+w=7ZqpLfuoqdJ1yRRDR37DHLK5oOQP+878yaWA+M119QdhA@mail.gmail.com> <CAMP44s3qy8OToPuV9DH2fnU=eDYuZYFGmvoDUdmexdCYC_FpSg@mail.gmail.com> <CAH+w=7ZYcLH0DhnDMTGGKE1VFCVT15K0RFpDbQvZH_EvD1ywHg@mail.gmail.com> <CAH+w=7bL1RodoTiNQykLESwK5v3ppGYs_AG-bGcgPqnf-kC_Cw@mail.gmail.com> <CAMP44s3LE2ccjgB9mxjZNifUX0evDhgUSh9oWpNM3GDgM0DwVQ@mail.gmail.com> <CAH+w=7bnFjxfriMt-BVau66A3OZGUdKcbCDXX8G3pD_i=_+kTQ@mail.gmail.com> <CAH+w=7bZXGvVqeftVn4FnPd+G9Y5tm+YU6yV9wkgE1vXTEVLKw@mail.gmail.com> <CAH+w=7adMRpgA97=Y-m_U--HW0APgVH3Bq4Y3=Zjy9uoh_7fzg@mail.gmail.com> <CAMP44s1zOR_zCOYMi_eWGH6ygsm05CC1WNMtMFxZD_qZxejM7g@mail.gmail.com> <CAH+w=7ZTxt+XjXxi-xC2MbDL8E5w-G-WEC2NMZAAD39JaJvdEA@mail.gmail.com> <CAMP44s39S8Nw_e32hnVyaPxZn_ARVKzKM8SUMkK8Dsax4w4Syw@mail.gmail.com> <CAH+w=7bzQEUAg0wwP9rGrbauQ2iebWEZWgy1Jb44=GLtN7f84A@mail.gmail.com> <CAMP44s1f7LWrCpcuQhgAiDDUzrLse50S6cNLH4VXjidN=nAbMg@mail.gmail.com>
On Sat, Jan 2, 2021 at 6:47 PM Felipe Contreras
<felipe.contreras@xxxxxxxxx> wrote:
>
> You do see why I think 'git checkout ' is more friendly than 'git'
> 'checkout' '', right?
Try this one. I think I'm probably done fooling around with this.
#autoload
# Usage: complete commandline
#
# where "commandline" is one or more words, of which the last is the
# prefix of the desired completion. If the commandline is a single
# argument, it is split into words using expansion flag (z) with any
# trailing whitespace treated as delimiting a final empty word.
#
# Thus:
# complete g
# completes all commands whose name starts with "g"
# complete git --
# completes all options of "git" that begin with two hyphens
# complete 'git checkout -- '
# completes all modified files in the current repository
#
# 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".
#
# If the variable CURSOR is defined, that position within commandline
# is used as the start of the completion. This is only useful when
# the COMPLETE_IN_WORD option is enabled. Note, if CURSOR does not
# fall either immediately before, within, or after the last word,
# the results are unpredictable.
#
# Other limitations:
# Only completion is performed, not expansion.
# Splitting the input with (z) collapses any consecutive whitespace,
# which may affect the use of CURSOR.
(( $+_comps )) || { autoload -U compinit; compinit -i -D }
zmodload zsh/zpty
if ! bindkey -M __complete 2>/dev/null
then
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() {
CURSOR=${__cursor:-$CURSOR}
zle -U $'\Cxa\n'
}
zle -N __init_complete
fi
completion-context() {
if (( debug )); then
print -u $debug -C 2 -a -r \
CONTEXT: ":completion:$curcontext" \
STATE: '' "${(@kv)compstate}"
fi
}
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
(( ARGC == 1 )) && argv=( "${(@)${(z):-${1}x}%x}" )
local REPLY reply=( "$@" ) __cursor=$CURSOR
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