zsh-workers
 help / color / mirror / code / Atom feed
From: Oliver Kiddle <opk@zsh.org>
To: Zsh Workers <zsh-workers@zsh.org>
Subject: Re: one time in 20 error
Date: Thu, 15 Dec 2022 02:32:06 +0100	[thread overview]
Message-ID: <54567-1671067926.542452@5dFf.Unco.iZC5> (raw)
In-Reply-To: <11286-1670634245.024361@Cn3a.dvUs.EJET>

Below is an update to the CSI key sequence patch I posted in 51160.

It adds a couple of test cases. There's also a new test case for the
related code that ensures it loops for vi widgets that need to wait for
an operator.

After thinking about it, I changed the condition when it finds a
complete CSI sequence. It will now keep a key binding for the first part
of the CSI sequence if that binding extends into the parameters. There
may be some use for this I haven't forseen and there isn't much gained
in preventing it. A binding for the CSI prefix (escape or escape
bracket) is still dropped in favour of undefined-key and consuming the
full CSI sequence from the input buffer.

I also added a comment to that condition.

Oliver

diff --git a/Src/Zle/zle_keymap.c b/Src/Zle/zle_keymap.c
index d90838f03..48691e8d0 100644
--- a/Src/Zle/zle_keymap.c
+++ b/Src/Zle/zle_keymap.c
@@ -1586,7 +1586,7 @@ getkeymapcmd(Keymap km, Thingy *funcp, char **strp)
     Thingy func = t_undefinedkey;
     char *str = NULL;
     int lastlen = 0, lastc = lastchar;
-    int timeout = 0;
+    int timeout = 0, csi = 0, startcsi;
 
     keybuflen = 0;
     keybuf[0] = 0;
@@ -1636,7 +1636,30 @@ getkeymapcmd(Keymap km, Thingy *funcp, char **strp)
 	    }
 #endif
 	}
-	if (!ispfx)
+
+	/* CSI key sequences have a well defined structure so if we currently
+	 * have an incomplete one, loop so the rest of it will be included in
+	 * the key sequence if that arrives within the timeout. */
+	if (keybuflen >= 3 && !csi) {
+	    startcsi = keybuflen - 3;
+	    csi = keybuf[startcsi] == '\033' && keybuf[keybuflen - 2] == '[';
+	}
+	if (csi) {
+	    csi = keybuf[keybuflen - 2] != Meta && keybuf[keybuflen - 1] >= 0x20
+		&& keybuf[keybuflen - 1] <= 0x3f;
+	    /* If we reach the end of a valid CSI sequence and the matched key
+	     * binding is for part of the CSI introduction, select instead the
+	     * undefined-key widget and consume the full sequence from the
+	     * input buffer. */
+	    if (!csi && keybuf[keybuflen - 1] >= 0x40 &&
+		    keybuf[keybuflen - 1] <= 0x7e && lastlen > startcsi &&
+		    lastlen <= startcsi + 2) {
+		func = t_undefinedkey;
+		lastlen = keybuflen;
+	    }
+	}
+
+	if (!ispfx && !csi)
 	    break;
     }
     if(!lastlen && keybuflen)
diff --git a/Test/X02zlevi.ztst b/Test/X02zlevi.ztst
index 203c13c32..ccfb7b1c6 100644
--- a/Test/X02zlevi.ztst
+++ b/Test/X02zlevi.ztst
@@ -596,6 +596,13 @@
 >BUFFER: 1ls `2`  $(3) "4" $'5' ${6}
 >CURSOR: 0
 
+  zpty_run 'bindkey -s -a "cw" "dwi"'
+  zletest $'one two\e0cwyksi'
+  zpty_run 'bindkey -r -a "cw"'
+0:for a vi command, wait to allow a longer binding to be used
+>BUFFER: yksitwo
+>CURSOR: 4
+
 %clean
 
   zmodload -ui zsh/zpty
diff --git a/Test/X03zlebindkey.ztst b/Test/X03zlebindkey.ztst
index 5277332a7..1b63b3920 100644
--- a/Test/X03zlebindkey.ztst
+++ b/Test/X03zlebindkey.ztst
@@ -37,6 +37,28 @@
 >"^Xy" "bar"
 >"^Xy" undefined-key
 
+  zpty_run 'bindkey -s "\e[" altbracket'
+  zletest $'$\C-A\e[17~'
+  zpty_run 'bindkey -r "\e["'
+0:binding to CSI introduction is not used if a full sequence arrives
+>BUFFER: $
+>CURSOR: 0
+
+  zpty_run 'bindkey -s "\e[1" altbracketone'
+  zletest $'$\C-A\e[17~'
+  zpty_run 'bindkey -r "\e[1"'
+0:binding to longer prefix of a CSI sequence is used
+# we assume the user knows what they're doing
+>BUFFER: altbracketone7~$
+>CURSOR: 15
+
+  zpty_run 'bindkey -s "\e[" altbracket'
+  zletest $'$\C-A\e[177'
+  zpty_run 'bindkey -r "\e["'
+0:use prefix binding where we don't have a CSI sequence
+>BUFFER: altbracket177$
+>CURSOR: 13
+
 # As we're only looking at definitions here, we don't
 # bother using the pseudo-terminal; just test in the normal fashion.
   bindkey -e


      parent reply	other threads:[~2022-12-15  1:32 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <f29162cd-28d1-85ed-6a3c-9bec1fb2e13a@eastlink.ca>
     [not found] ` <CAA-Ti-hhbwZD3-aeMvy_tm5f7SKHw7XN4muJn8ymez1q7rAE_A@mail.gmail.com>
     [not found]   ` <CAN=4vMpCUNgKYgSR+4rmREpA25v_sLHYcS4nfTk8EQ_0Cg5yyw@mail.gmail.com>
     [not found]     ` <5d0c4e22-80b0-2fd2-ee75-6902da52d121@eastlink.ca>
     [not found]       ` <CAH+w=7aeERdOcpPzTmKVQFK7HLduh2s1J9VFzumCh6W0SJFqpg@mail.gmail.com>
     [not found]         ` <57e8e248-bb1a-663a-8557-e3fc13f671d4@eastlink.ca>
     [not found]           ` <CAH+w=7aW9meLuEKSGsKiZMXoAd7KBc9fgakXZnB_t2iphq=BPQ@mail.gmail.com>
     [not found]             ` <e35c2f14-abda-93d7-bf2c-6823d6d3215d@eastlink.ca>
     [not found]               ` <CAN=4vMrZzxYVjc63DwU=Scks39FhzmK+E57XerOwusmd64QOjw@mail.gmail.com>
     [not found]                 ` <c2f348d1-8982-d4a4-2c78-a0dd67319b8c@eastlink.ca>
     [not found]                   ` <CAGdYchv9T6ByD7meADqWzdJwAF2SG2YXhasT0=+AQvV+08ZRrA@mail.gmail.com>
2022-12-01 16:30                     ` ERR_RETURN doc Bart Schaefer
2022-12-01 19:30                       ` Philippe Altherr
2022-12-01 20:05                         ` Bart Schaefer
2022-12-01 22:52                           ` Philippe Altherr
2022-12-10 11:33                         ` Daniel Shahaf
2022-12-10 14:06                           ` Philippe Altherr
2022-12-11  1:24                             ` Bart Schaefer
2022-12-12 23:35                               ` Philippe Altherr
2022-12-05 23:48     ` one time in 20 error Oliver Kiddle
2022-12-06  0:13       ` Bart Schaefer
2022-12-06 13:21       ` Roman Perepelitsa
2022-12-10  1:04         ` Oliver Kiddle
2022-12-10  9:23           ` Roman Perepelitsa
2022-12-15  1:32           ` Oliver Kiddle [this message]

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=54567-1671067926.542452@5dFf.Unco.iZC5 \
    --to=opk@zsh.org \
    --cc=zsh-workers@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).