This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Patch to make readline parse CSI keycodes properly


When pressing an unbound key or key combination in bash and other
readline programs, somewhat cryptic characters are often inserted into
the command line. For example, you get "~" for Insert, "4~" for F12,
or ";5A" for Ctrl+Up. This can be rather confusing and annoying.

The reason is that readline does not parse control sequences as
defined in section 5.4 of the ECMA-48 standard correctly. VT100
terminals and its numerous descendants use those for many keys or key
combinations that don't have single-character codes. They start with
the control sequence introducer (CSI), normally represented by "\e[",
followed by parameter and intermediate bytes in the range from 0x20 to
0x3F, followed by a final byte in the range from 0x40 to 0x7E.
Readline, at least in its default config, stops parsing these
sequences after the first byte following the "\e[", leaving any
remaining bytes to spill onto the command line.

As far as I can see, there's currently no way to address this short of
explicitly binding all the CSI keycodes to nothing. (Well, actually, a
single binding seems to suffice for codes that differ only in the last
character, but that doesn't help with the F-keys and the likes of
Insert.) That's a rather long list though, which would slow down
startup and lead to an explosion in the number of keymaps allocated
internally by readline.

Hence the attached small patch for the readline source, which avoids
such inefficiencies. It defines a function called 'skip-csi-seq' that
skips over the parameter, intermediate, and final bytes. This is
assigned to '\e['. Generally in readline, bindings for longer
sequences take precedence over shorter ones, which ensures that CSI
sequences that do have functions bound to them continue to work.

Here's the result in the output of 'bind':

$ bind -p | grep e\\[
"\e[D": backward-char
"\e[H": beginning-of-line
"\e[3~": delete-char
"\e[F": end-of-line
"\e[C": forward-char
"\e[B": next-history
"\e[A": previous-history
"\e[": skip-csi-seq

It's working correctly here, including when adding bindings for longer
keycodes: no more funny characters when accidentally hitting the wrong
key. Please consider the patch for inclusion into the readline
package.

Regards,
Andy

Attachment: rl_skip_csi_seq.patch
Description: Binary data

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]