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

Re: Determining the index within the current word during completion



On Fri, Jan 24, 2025 at 11:16 AM Ross Goldberg <ross.goldberg@xxxxxxxxx> wrote:
>
> How can I determine the index of the cursor within the current word
> during completion for all potential inputs, with each character
> between the start of the word & the cursor increasing the index by
> one, regardless if it is part of an escape sequence or not (or any
> other type of character)?

Why?

I ask because I think it's pretty likely you're trying to use
completion for an application that would better be managed by an
"ordinary" widget.

Completion intentionally "understands" as much as possible about the
way the line is going to be interpreted after you accept it, in order
to provide answers that make sense in the final context.  There's some
of this that's very difficult -- it's easy to construct a baroque
quoting context that completion will interpret incorrectly, or will
fail to reconstruct after modifying the buffer -- but it's
completion's job to create strings for comparisons that produce the
same results regardless of how the line or word is "protected".

Anyway ... the backslash will be in $PREFIX if it's meaningful.  That
is, it's removed from \n because n is not special to the shell, but
it's kept for \* because there it's preventing the interpretation of a
glob pattern.

An additional complication is that $PREFIX is influenced by other
settings such as complete_in_word.  Completion may treat the entire
word as being the prefix, even if the cursor is somewhere in the
middle of it when you begin completing, and may adjust (and later
restore) the cursor position along the way.  Once you've entered a
completion widget, you've essentially given up control of the editor
until that widget is done.

Outside of completion (in an ordinary widget) you can use
${${(z)LBUFFER}[-1]} to get the part of (what completion would
consider to be) the current word up to the cursor position.




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