From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.4 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FROM,HTML_MESSAGE,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 21194 invoked from network); 23 Sep 2020 11:33:38 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 23 Sep 2020 11:33:38 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20200801; t=1600860818; b=wxdUiGZ9qRk9jdGL4XitJvhbHPGcQZMxjQ0gRCZUyPMbrDPrggavxx6HASUZuQz+JISnnGQ76R 84GXeJf12UE0NTRze0DWEPsQVy2aJQpLTETZ2dYON6QV9F9F0aT0jAO5PK+cD+LVmChozLwQQ/ RitR8wWda0T6IaPqQv88bSbYwfjuacc+0SfTd2v2K1m0TpgfrLp/IDKslsax1ZNJHIblhYFEMh z85UxaNuXCKJoCoxq1F9rYV8HP5tqqTc33CjcPwZWaEYxhzeFsh6+r7n2bcq9vI40j08oNNjPX rKPGnvwV0jBpnuaQtQc1EI7DDMsicgJuLJ/vLUawAs2R9Q==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (mail-ej1-f42.google.com) smtp.remote-ip=209.85.218.42; dkim=pass header.d=gmail.com header.s=20161025 header.a=rsa-sha256; dmarc=pass header.from=gmail.com; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20200801; t=1600860818; bh=2lMtwGQfK+WCBa8R8pvVea+ywPT6K0FQcocIPQLdJFc=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:Content-Type:Cc:To:Subject:Message-ID:Date:From:In-Reply-To: References:MIME-Version:DKIM-Signature:DKIM-Signature; b=FZ2o4EqziozHPDcBBcxICEQhkn+KvLQgkLw3cdgu2KJqCPS+bxOE+LASX8IyXafJtHSVVcVVbd 8TQZYaNs781erqECHOyrh0oBNOn3JLEykwDD1NZBx6EffTwc7xwXL2bodmxK9cNYjhvPwG0P1R rns7fmYxWbm6OTTXg9OtyNGnrrKe+C12NPiP8PiIhi6SMqSBxsmUuIzCsgeoPoP7gxyjh5EWck F6OLBMqJbB562CmY1NMZtWvm8oy/SJ6idcChNO4ltHlqH0splylUgtm9hdwPzZa9oAIR1yuO0X 8F6ExrKhmJ61HQul13cNnJIPsGd6eHtzI/gWmBO+3xPOOA==; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20200801; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:Content-Type:Cc:To:Subject:Message-ID :Date:From:In-Reply-To:References:MIME-Version:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=XnR6HGeLn5gdFm8ZANQNY4CgIVDC8G6o+Kbr3fXEfVs=; b=mawZM2/VuVGQfHfTxtU8AOwOfG MdoigGLM5Bk9qT9zy9ovHjZpRsS91plPZjX17FJEeHD0cBEJwWurbNGnVqqWrL4cRPHwtri5p/Pi9 ARoSrIKXNIZi9V8oGECqlvhwblRFlH0dI7+8MBSphK5/nrDgK7H8/ABZbrm+HVAGm5XQlz+qHfwOo wN2y+0LxI/in58VhASIxaurBU1SON+0+XhbznSwPGYz1HnA8pBPJOuQotdEmIt0mtlJWZvcCrccjP Zxm+Fdmd5a/PY9+YCTydulBKBYE309PiKj8sd8NYTxTH/jU1+skwFRF59FyVUwjBZrsKLwvccF4ED dK0qXqJg==; Received: from authenticated user by zero.zsh.org with local id 1kL31l-000EEt-I1; Wed, 23 Sep 2020 11:33:37 +0000 Authentication-Results: zsh.org; iprev=pass (mail-ej1-f42.google.com) smtp.remote-ip=209.85.218.42; dkim=pass header.d=gmail.com header.s=20161025 header.a=rsa-sha256; dmarc=pass header.from=gmail.com; arc=none Received: from mail-ej1-f42.google.com ([209.85.218.42]:35001) by zero.zsh.org with esmtps (TLS1.3:TLS_AES_128_GCM_SHA256:128) id 1kL31L-000DrM-7U; Wed, 23 Sep 2020 11:33:11 +0000 Received: by mail-ej1-f42.google.com with SMTP id u21so27312309eja.2 for ; Wed, 23 Sep 2020 04:33:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=XnR6HGeLn5gdFm8ZANQNY4CgIVDC8G6o+Kbr3fXEfVs=; b=UsMJSA+3Oc9QFR5F34oN9WkjV/I212TPHAcB1va+rc8K3EmoYliqn4coCgryTjv0y2 5YXevaZXHvH1cAikypubOK4gXihxqYTCDpCbkC2fLV3SfnX1hnpduxKo+E5o30Z8SQef KAKvuj6+AlUmemRDKYr1Ghb2SMmNPqfjtzP0ocPRcH9bq9HJ9PNU+Mn4hzZuT+/dFdc6 5TTzhRDT9sMB4KXT0aizAiM/5NrTPNP1ke1abiHZKRjWLjJDlNCPGCd0Nw5AVY61dr9Q dR2jUHmX2XhEza+3bpE/CW9ru/8XZWoK7eCJ+nUAnq6Mb8zq32wDEBg1VADmkb928edH kCcA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=XnR6HGeLn5gdFm8ZANQNY4CgIVDC8G6o+Kbr3fXEfVs=; b=Pf5gax73ytmDQzy8bcBu8EE1C2sAL/4o8LzwFpP7esKpqJU8vIU0XbVjLFMKWU01Dx tP7vLtxed2vpwLhEjxQyFnLSILSk0+1MlA+YUKF6yMC5zjwFLtytdw1JYajbS3gNQ5lK EjfMV6UYWpbuvGG8fSsHmJqLiSDP3d6QfZzyt3I3juD1dSwQ+DbcftvkfcJTsaI0ppjH ADsG0za7bbN4clAnr+mqvR3ng4v5PjmBwQLeaT22g+Qu/LMhfAX0dPMka5NgviF8io0D I/4Yy6IQUomzVKLm5uD484UyzJgc1reaiX6oS+D1S8rS9HR4gdaG0refvOA36nlImUpG M+BQ== X-Gm-Message-State: AOAM530ajGQp8uag3NCifhszK13VKwMKcCYOGYPkwVHL0vFBNICPIQWA Ii+DJ3c3pYTRuXTGzOh3EZ3/VjHhrpk5YBBAA78EHrjWBDg/xg7h X-Google-Smtp-Source: ABdhPJykyZqNwiQboTdPpjyNdH3EuTCS8L52zmMhknOCo74IJZ5RCZ4vUWDq3h6Q4av0RPvLXWuu3TCJJ5mrKk3b2f4= X-Received: by 2002:a17:906:b4e:: with SMTP id v14mr1711911ejg.179.1600860790962; Wed, 23 Sep 2020 04:33:10 -0700 (PDT) MIME-Version: 1.0 References: <20200923112945.578227b9@tarpaulin.shahaf.local2> In-Reply-To: <20200923112945.578227b9@tarpaulin.shahaf.local2> From: =?UTF-8?B?UGV0ZXIgU2zDrcW+aWs=?= Date: Wed, 23 Sep 2020 13:32:54 +0200 Message-ID: Subject: Re: How to avoid infinite recursion in ZLE widgets To: Daniel Shahaf Cc: zsh-users@zsh.org Content-Type: multipart/alternative; boundary="000000000000c7bc4905aff97226" X-Seq: 26077 Archived-At: X-Loop: zsh-users@zsh.org Errors-To: zsh-users-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-users-request@zsh.org X-no-archive: yes List-Id: List-Help: List-Subscribe: List-Unsubscribe: List-Post: List-Owner: List-Archive: Archived-At: --000000000000c7bc4905aff97226 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Thanks, Daniel! Peter =D1=81=D1=80=D0=B5, 23. =D1=81=D0=B5=D0=BF 2020. =D1=83 13:29 Daniel Shahaf= =D1=98=D0=B5 =D0=BD=D0=B0=D0=BF=D0=B8=D1=81=D0=B0=D0=BE/=D0=BB=D0=B0: > Peter Sl=C3=AD=C5=BEik 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 =C2=ABzsh -f=C2=BB and it inserts a single qu= ote > without moving the cursor. > > It inserts _one_ quote, rather than two, because self-insert ignores > its positional arguments and the widget was bound to =C2=AB'=C2=BB. > > > 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 =C2=ABzle .self-insert=C2=BB, or even modify $LBUFFER = and > $RBUFFER directly (=C2=ABLBUFFER+=3D\'; RBUFFER=3D\'$RBUFFER=C2=BB). > > (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? > > =C2=ABzle -U foo=C2=BB subjects the =C2=ABf=C2=BB, =C2=ABo=C2=BB, and =C2= =ABo=C2=BB to bindkey mappings. For > instance, =C2=ABbindkey -s x y=C2=BB followed by =C2=ABzle -U x=C2=BB wou= ld insert =C2=ABy=C2=BB. > > =C2=ABself-insert=C2=BB 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 =C2=ABzle -K foo=C2=BB without further arguments. You can do somethin= g like this: > > { > readonly save_KEYMAP=3D$KEYMAP > zle -K .safe > =E2=8B=AE > } always { > zle -K $save_KEYMAP > } > > But see above about $LBUFFER. > > Cheers, > > Daniel > --000000000000c7bc4905aff97226 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Thanks, Daniel!

Peter

=D1=81=D1=80=D0=B5, 23. =D1=81=D0=B5=D0=BF 2020. =D1=83 13:29 Daniel Sha= haf <d.s@daniel.shahaf.name> =D1=98=D0=B5 =D0=BD=D0=B0=D0=BF=D0=B8=D1=81=D0=B0=D0=BE/=D0=BB=D0=B0:=
Peter Sl=C3=AD= =C5=BEik 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 sol= ution
> looks rather trivial:
>
> function insert-single-quotes() {
>=C2=A0 =C2=A0 =C2=A0zle self-insert "''"=C2=A0 =C2=A0= # that's "_'_'_"
>=C2=A0 =C2=A0 =C2=A0zle backward-char
> }=C2=A0
>
> zle -N insert-single-quotes
> bindkey "'" insert-single-quotes=C2=A0 =C2=A0 # that'= ;s "_'_"
>
> However, this solution creates infinite recursion (a single quote boun= d to
> insert a single quote).

No, it doesn't.=C2=A0 I tried in =C2=ABzsh -f=C2=BB and it inserts a si= ngle 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 =C2=AB'=C2=BB.

> 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 =C2=ABzle .self-insert=C2=BB, or even modify $LBUFFER an= d
$RBUFFER directly (=C2=ABLBUFFER+=3D\'; RBUFFER=3D\'$RBUFFER=C2=BB)= .

(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 an= d zle
> -U?

=C2=ABzle -U foo=C2=BB subjects the =C2=ABf=C2=BB, =C2=ABo=C2=BB, and =C2= =ABo=C2=BB to bindkey mappings.=C2=A0 For
instance, =C2=ABbindkey -s x y=C2=BB followed by =C2=ABzle -U x=C2=BB would= insert =C2=ABy=C2=BB.

=C2=ABself-insert=C2=BB appends one character to the buffer.

> 3. I tried to avoid the recursion by using "zle -K .safe -U text&= quot;, but it
> ended with "too many arguments for -K". How is zle -K expect= ed to be used?

As =C2=ABzle -K foo=C2=BB without further arguments.=C2=A0 You can do somet= hing like this:

=C2=A0 =C2=A0 {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 readonly save_KEYMAP=3D$KEYMAP
=C2=A0 =C2=A0 =C2=A0 =C2=A0 zle -K .safe
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =E2=8B=AE
=C2=A0 =C2=A0 } always {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 zle -K $save_KEYMAP
=C2=A0 =C2=A0 }

But see above about $LBUFFER.

Cheers,

Daniel
--000000000000c7bc4905aff97226--