Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: How to avoid infinite recursion in ZLE widgets
- X-seq: zsh-users 26076
- From: Daniel Shahaf <d.s@xxxxxxxxxxxxxxxxxx>
- To: Peter Slížik <peter.slizik@xxxxxxxxx>
- Subject: Re: How to avoid infinite recursion in ZLE widgets
- Date: Wed, 23 Sep 2020 11:29:45 +0000
- Archived-at: <https://zsh.org/users/26076>
- Archived-at: <http://www.zsh.org/sympa/arcsearch_id/zsh-users/2020-09/20200923112945.578227b9%40tarpaulin.shahaf.local2>
- Authentication-results: zsh.org; iprev=pass (out5-smtp.messagingengine.com) smtp.remote-ip=66.111.4.29; dkim=pass header.d=daniel.shahaf.name header.s=fm1 header.a=rsa-sha256; dkim=pass header.d=messagingengine.com header.s=fm3 header.a=rsa-sha256; dmarc=none header.from=daniel.shahaf.name; arc=none
- Cc: zsh-users@xxxxxxx
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= daniel.shahaf.name; h=date:from:to:cc:subject:message-id :in-reply-to:references:mime-version:content-type :content-transfer-encoding; s=fm1; bh=KT1Ipe98AJP7CjBw97Jqm6V2Kn 0Ljzt7QG0VBrOalP4=; b=SJwEIKsEcOWUO67YKiA+n0sBD9iKrFUjgwhM3aKD64 FvC53tPpOM2SHMisJGMCXFmfXNgeIZD5bMwjcaB50dbTO/mxkUbI8Gxd633wWC7v 4wO0LLr/La4ab4riHNXeKH0up2gqKAQ17oSSEVNdHRkw0MwzyGhe2dXO/Z2Dk78f RziCnL23LQs+WMo3kA3PpqhQfSCgTD11pxsGOLSQSY+pP7YBOUyB6Coy+ZWyL4Ob et86N/UGGlWn99irJn1xF/z4Oe86eC0b2wor6pT42KuiaL41vdY3BxWV2lOykfHQ QLzmhplLZT9h+mP17/vIg6SiYWYf6B24czqJ93bVWf9w==
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; bh=KT1Ipe98AJP7CjBw97Jqm6V2Kn0Ljzt7QG0VBrOal P4=; b=a5af0Bjzcv6DBTDO/0cORGASijRpCEulJvSYlSvF1oJJ2cZLzfmxyCbnA d0YXE5Kklnf4wE6gEVPROYAi3REHCdUI1MmAkXcrYEQ4k8Hv8vVqqhctuEuBAj5u 5/w5ywFqcMDusLuUsPKlQbgigIxnPDnWwCOIu4ZNdgGqOA/zpQep7DIzp9Qax0q/ 5Qvfd59H6g6XzFrJE8RBmbis6NHeoKCYC36mII1O41ckLWgS0YunZWxEx0HgzwLe fy2sHFHH1pMXKedPhU1op8AuC182UG93fs17fyTZNeOw/tspyZm8pcCN75oB0QI0 6ANscuptlzeGzFKDOn5dDdYls+2ow==
- In-reply-to: <CAC-uhUD6-t3F0ZQuz1A_OWsFOJVj7L0dibNwbnDJurpq4-mZ+Q@mail.gmail.com>
- List-archive: <http://www.zsh.org/sympa/arc/zsh-users>
- List-help: <mailto:sympa@zsh.org?subject=help>
- List-id: <zsh-users.zsh.org>
- List-owner: <mailto:zsh-users-request@zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- List-subscribe: <mailto:sympa@zsh.org?subject=subscribe%20zsh-users>
- List-unsubscribe: <mailto:sympa@zsh.org?subject=unsubscribe%20zsh-users>
- References: <CAC-uhUD6-t3F0ZQuz1A_OWsFOJVj7L0dibNwbnDJurpq4-mZ+Q@mail.gmail.com>
- Sender: zsh-users-request@xxxxxxx
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
Messages sorted by:
Reverse Date,
Date,
Thread,
Author