zsh-workers
 help / color / mirror / code / Atom feed
From: Oliver Kiddle <opk@zsh.org>
To: Roman Perepelitsa <roman.perepelitsa@gmail.com>
Cc: Zsh Workers <zsh-workers@zsh.org>
Subject: Re: one time in 20 error
Date: Tue, 06 Dec 2022 00:48:11 +0100	[thread overview]
Message-ID: <45486-1670284091.713245@QrNT.Ugrc.e_6U> (raw)
In-Reply-To: <CAN=4vMpCUNgKYgSR+4rmREpA25v_sLHYcS4nfTk8EQ_0Cg5yyw@mail.gmail.com>

On 29 Nov, Roman Perepelitsa wrote:
> I have the following in my .zshrc:
>
>     function skip-csi-sequence() {
>      local key
>      while read -sk key && (( $((#key)) < 0x40 || $((#key)) > 0x7E )); do
>        # empty body
>      done
>     }
>
>     zle -N skip-csi-sequence
>     bindkey '\e[' skip-csi-sequence
>
> With this binding a buggy script that leaks mouse events into zle
> won't have any effect.

This is a nice idea. I've occasionally had unwanted effects from
unintentionally hitting a function key. I thought I'd try this in C.

After testing this, I'm inclined to think it would be better to special
case the CSI sequence in getkeymapcmd() instead. Currently, you would
need to bind this in every local keymap because most local keymaps will
include a binding starting with the \e[ sequence. It is also common for
Alt-[ to generate \e[. Binding that would override the skip-csi-sequence
binding but we could discern between the two based on KEYTIMEOUT. Any
other thoughts on this. Or objections to such an approach? Are there
terminals out there that eschew CSI sequences and for which such special
handling could be problematic? Is anyone especially attached to the
current behaviour where unbound function keys and mouse movements cause
chaos?

I notice that bash has a skip-csi-sequence widget but leaves it
disabled by default. Would be interesting ot know why.

The patch below tries the widget approach. Rather than look for the
termination character, I thought it better to bail out given any
out-of-range parameter byte or KEYTIMEOUT being exceeded. As yet, I'm
not planning to apply this.

Oliver

diff --git a/Src/Zle/iwidgets.list b/Src/Zle/iwidgets.list
index c95c7a491..8d1b64e58 100644
--- a/Src/Zle/iwidgets.list
+++ b/Src/Zle/iwidgets.list
@@ -112,6 +112,7 @@
 "self-insert-unmeta", selfinsertunmeta, ZLE_MENUCMP | ZLE_KEEPSUFFIX
 "send-break", sendbreak, 0
 "set-mark-command", setmarkcommand, ZLE_MENUCMP | ZLE_KEEPSUFFIX | ZLE_LASTCOL
+"skip-csi-sequence", skipcsisequence, ZLE_MENUCMP | ZLE_KEEPSUFFIX | ZLE_LASTCOL
 "split-undo", splitundo, ZLE_MENUCMP | ZLE_KEEPSUFFIX | ZLE_LASTCOL | ZLE_NOTCOMMAND
 "spell-word", spellword, 0
 "set-local-history", setlocalhistory, ZLE_LASTCOL
diff --git a/Src/Zle/zle_keymap.c b/Src/Zle/zle_keymap.c
index d90838f03..737d3cd72 100644
--- a/Src/Zle/zle_keymap.c
+++ b/Src/Zle/zle_keymap.c
@@ -1430,6 +1430,11 @@ default_bindings(void)
     bindkey(emap, "\30\30", refthingy(t_exchangepointandmark), NULL);
     bindkey(emap, "\30=",   refthingy(t_whatcursorposition), NULL);
 
+    /* skip unbound CSI sequence in all keymaps */
+    bindkey(emap, "\33[", refthingy(t_skipcsisequence), NULL);
+    bindkey(vmap, "\33[", refthingy(t_skipcsisequence), NULL);
+    bindkey(amap, "\33[", refthingy(t_skipcsisequence), NULL);
+
     /* bracketed paste applicable to all keymaps */
     bindkey(emap, "\33[200~", refthingy(t_bracketedpaste), NULL);
     bindkey(vmap, "\33[200~", refthingy(t_bracketedpaste), NULL);
diff --git a/Src/Zle/zle_misc.c b/Src/Zle/zle_misc.c
index eba28d1ec..1b996fb8a 100644
--- a/Src/Zle/zle_misc.c
+++ b/Src/Zle/zle_misc.c
@@ -152,6 +152,19 @@ selfinsertunmeta(char **args)
     return selfinsert(args);
 }
 
+
+/**/
+mod_export int
+skipcsisequence(char **args)
+{
+    int next, timeout;
+    do {
+	next = getbyte(1L, &timeout, 1);
+    } while (!timeout && next >= 0x20 && next <= 0x3f);
+
+    return 1;
+}
+
 /**/
 int
 deletechar(char **args)


  parent reply	other threads:[~2022-12-05 23:49 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     ` Oliver Kiddle [this message]
2022-12-06  0:13       ` one time in 20 error 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

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=45486-1670284091.713245@QrNT.Ugrc.e_6U \
    --to=opk@zsh.org \
    --cc=roman.perepelitsa@gmail.com \
    --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).