From: "Peter Slížik" <peter.slizik@gmail.com>
To: Daniel Shahaf <d.s@daniel.shahaf.name>
Cc: zsh-users@zsh.org
Subject: Re: How to avoid infinite recursion in ZLE widgets
Date: Wed, 23 Sep 2020 13:32:54 +0200 [thread overview]
Message-ID: <CAC-uhUCcuybwB5UtKiXoKwf8wwpZmVPJn2Mq99F2wh_WWXTymA@mail.gmail.com> (raw)
In-Reply-To: <20200923112945.578227b9@tarpaulin.shahaf.local2>
[-- Attachment #1: Type: text/plain, Size: 2345 bytes --]
Thanks, Daniel!
Peter
сре, 23. сеп 2020. у 13:29 Daniel Shahaf <d.s@daniel.shahaf.name> је
написао/ла:
> Peter Slížik wrote on Wed, 23 Sep 2020 10:34 +0200:
> > Hello,
> >
> > I'd like to replicate the functionality found in some text editors -
> namely
> > that if you press a single or double quote, the editor inserts two of
> them
> > and places the cursor inside the pair.
> >
> > With some necessary checks for word boundaries, etc. left out, the
> solution
> > looks rather trivial:
> >
> > function insert-single-quotes() {
> > zle self-insert "''" # that's "_'_'_"
> > zle backward-char
> > }
> >
> > zle -N insert-single-quotes
> > bindkey "'" insert-single-quotes # that's "_'_"
> >
> > However, this solution creates infinite recursion (a single quote bound
> to
> > insert a single quote).
>
> No, it doesn't. I tried in «zsh -f» and it inserts a single quote
> without moving the cursor.
>
> It inserts _one_ quote, rather than two, because self-insert ignores
> its positional arguments and the widget was bound to «'».
>
> > 1. How to prevent the recursion?
>
> Always open a new shell for testing.
>
> > Is self-insert the right widget for this task?
>
> You could also use «zle .self-insert», or even modify $LBUFFER and
> $RBUFFER directly («LBUFFER+=\'; RBUFFER=\'$RBUFFER»).
>
> (Incidentally, I guess you may also want to check whether ${RBUFFER}
> starts with a single quote, but that's no longer a zsh question but
> a business logic question.)
>
> > 2. I played with zle -U. What are the use cases for zle self-insert and
> zle
> > -U?
>
> «zle -U foo» subjects the «f», «o», and «o» to bindkey mappings. For
> instance, «bindkey -s x y» followed by «zle -U x» would insert «y».
>
> «self-insert» appends one character to the buffer.
>
> > 3. I tried to avoid the recursion by using "zle -K .safe -U text", but it
> > ended with "too many arguments for -K". How is zle -K expected to be
> used?
>
> As «zle -K foo» without further arguments. You can do something like this:
>
> {
> readonly save_KEYMAP=$KEYMAP
> zle -K .safe
> ⋮
> } always {
> zle -K $save_KEYMAP
> }
>
> But see above about $LBUFFER.
>
> Cheers,
>
> Daniel
>
[-- Attachment #2: Type: text/html, Size: 3020 bytes --]
next prev parent reply other threads:[~2020-09-23 11:33 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-09-23 8:34 Peter Slížik
2020-09-23 11:21 ` Peter Stephenson
2020-09-23 11:24 ` Peter Slížik
2020-09-23 11:29 ` Daniel Shahaf
2020-09-23 11:32 ` Peter Slížik [this message]
2020-09-23 18:25 ` zsugabubus
2020-09-24 9:55 ` Peter Slížik
2020-09-25 14:19 ` Peter Slížik
2020-09-25 14:43 ` Peter Stephenson
2020-09-26 9:27 ` Daniel Shahaf
2020-09-26 9:40 ` Roman Perepelitsa
2020-09-26 10:24 ` Daniel Shahaf
2020-09-26 10:31 ` Roman Perepelitsa
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=CAC-uhUCcuybwB5UtKiXoKwf8wwpZmVPJn2Mq99F2wh_WWXTymA@mail.gmail.com \
--to=peter.slizik@gmail.com \
--cc=d.s@daniel.shahaf.name \
--cc=zsh-users@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).