Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: PATCH: query terminal properties on ZLE startup
- X-seq: zsh-workers 53394
- From: Oliver Kiddle <opk@xxxxxxx>
- To: zsh-workers@xxxxxxx
- Subject: Re: PATCH: query terminal properties on ZLE startup
- Date: Fri, 28 Feb 2025 03:25:36 +0100
- Archived-at: <https://zsh.org/workers/53394>
- In-reply-to: <73847-1740613975.581764@cI3H.WJge.amo7>
- List-id: <zsh-workers.zsh.org>
- References: <42572-1740099942.836692@_3BM.v5Iz.JqiS> <F07C8421-2663-440A-82AD-0748D39511B1@kba.biglobe.ne.jp> <CAH+w=7a9hye-2BxQUjkHw-=zFo5T7vXjVwQRnKnKEoZ3W0-_ug@mail.gmail.com> <40483-1740433325.086036@2QZo.3hhS.-9FG> <CAH+w=7Zf3vL6BYg0t61uskLmDD_gOwE7XhuwxB9SSwv+z0qOKw@mail.gmail.com> <69272-1740474756.308875@TCCj.PrGv.2vsI> <CAH+w=7ZJNRy2zV7t4wRhzcnW1jn2LgRKbMByCOrtbr9VVM2emw@mail.gmail.com> <5355-1740527169.384113@fufd.0HLy.otw9> <CAN=4vMpHQyTmXBScGZDn+OTc=cDWKSt7vJ_2+hmTe1zViQyU8w@mail.gmail.com> <73847-1740613975.581764@cI3H.WJge.amo7>
I wrote:
> I'll look into whether we can make zsh recognise and drop the junk
> input.
Following is a patch for that. This is useful completely independent of
the earlier patches, especially for any users who do terminal queries
from shell code.
OSC and DCS sequences have a well-known structure. To my knowledge they
shouldn't be used for key sequences but this won't stop you binding
them. Unlike the existing code for CSI sequences, this discards them
outright rather than returning the undefined-key widget. This is because
an undefined-key will typically beep which is not helpful for a late
arriving terminal query response.
The middle block makes a special case for the two terminal queries the
original patch makes that do generate a CSI sequence response - also
discarding them rather than returning undefined-key. This could be
extended to recognise other responses such as cursor position. Or we
could flip it to only check for those CSI sequences that are typically
associated with key sequences. I think that'd be any ending in any of
ABCDFHPZu~ but perhaps someone more familiar with the specs has better
knowledge of that?
If you now try, e.g. print "\e]11;?\a" the input is no longer messed up
but there's a high chance that the terminal response is printed to the
terminal (depending on how fast the response comes back). This is due to
the terminal's input echoing. That's unavoidable without also breaking the
ability to see typed-ahead characters.
Unlike the query patch, this doesn't handle the urxvt 9.31 incorrect ST
bug.
Oliver
diff --git a/Src/Zle/zle_keymap.c b/Src/Zle/zle_keymap.c
index 5012917f5..ce52abe66 100644
--- a/Src/Zle/zle_keymap.c
+++ b/Src/Zle/zle_keymap.c
@@ -1580,7 +1580,7 @@ getkeymapcmd(Keymap km, Thingy *funcp, char **strp)
Thingy func = t_undefinedkey;
char *str = NULL;
int lastlen = 0, lastc = lastchar;
- int timeout = 0, csi = 0;
+ int timeout = 0, csi = 0, oscdcs = 0;
keybuflen = 0;
keybuf[0] = 0;
@@ -1647,14 +1647,38 @@ getkeymapcmd(Keymap km, Thingy *funcp, char **strp)
if (keybuf[keybuflen - 1] >= 0x40 &&
keybuf[keybuflen - 1] <= 0x7e && lastlen > csi - 2 &&
lastlen <= csi) {
+ if (keybuf[csi] == '?' && (keybuf[keybuflen - 1] == 'c' ||
+ keybuf[keybuflen - 1] == 'u'))
+ { /* is a terminal query response - discard */
+ keybuflen = csi - 2;
+ timeout = csi = 0;
+ continue;
+ }
func = t_undefinedkey;
lastlen = keybuflen;
}
csi = 0;
}
}
+ /* An OSC or DCS sequence is likely a late arriving terminal query
+ * response. Keep looping; if we reach an ST, discard the sequence
+ * - unless we first match a keybinding or a keytimeout elapses. */
+ if (oscdcs) {
+ if (keybuf[keybuflen - 1] == '\007' || /* BEL sometimes used */
+ (keybuf[keybuflen - 2] == '\033' &&
+ keybuf[keybuflen - 1] == '\\') ||
+ (keybuf[keybuflen - 2] == Meta && /* ST can be 0x9b */
+ (unsigned char) keybuf[keybuflen - 1] == (0x9b ^ 32)))
+ {
+ keybuflen = oscdcs - 2; /* discard */
+ timeout = oscdcs = 0;
+ continue;
+ }
+ } else if (keybuflen >= 2 && keybuf[keybuflen - 2] == '\033' &&
+ (keybuf[keybuflen - 1] == ']' || keybuf[keybuflen - 1] == 'P'))
+ oscdcs = keybuflen;
- if (!ispfx && !csi)
+ if (!ispfx && !csi && !oscdcs)
break;
}
if(!lastlen && keybuflen)
Messages sorted by:
Reverse Date,
Date,
Thread,
Author