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

Re: replacing current word in user-defined widget?



On Jun 6,  6:41pm, Adam Spiers wrote:
}
} I was trying to write a make-path-absolute type of widget that
} replaces the pathname which is the current word with an absolute path,

That sounds like a job for a completion widget.  As you say:

} Completion widgets seem to have it a lot easier, since they're given
} the $words array to mess around with.

Something like:

	#compdef -k complete-word ^X^F
	compadd -U -S '' $PWD/$words[CURRENT]

Might be a good idea to test $compstate[quote] etc. but you get the idea.

However ...

} but I got stuck trying to write code which figures out where the
} current word starts and ends.  Presumably it involves looking at
} $BUFFER and $CURSOR, and maybe doing word splitting with ${(z)BUFFER}
} or similar, but beyond that I don't have much of a clue :-)

I messed with this while trying to write an improved transpose-words and
came to the conclusion that it's nearly impossible.  As you noticed, one
can split the command line into words with ${(z)BUFFER} but as that has
the side-effect of removing an unknown amount of whitespace between the
words, you can't map from CURSOR to an index into the resulting array.

You can try taking the last word from ${(z)LBUFFER} and the first word
from ${(z)RBUFFER) and gluing them together, but I'm not sure that'll
work right if the cursor is in the middle of a quoted string, so it's
best to let the completion code deal with it.

Unfortunately transpose-words doesn't have that option, because it has
to deal with two words, not one.  So I eventually gave up.

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com

Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net   



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