From 9f1303fb8eed466d2d883cfd485a190df855cf83 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Wed, 10 Jun 2020 17:02:48 -0700 Subject: [PATCH] term: fixup ctrl+ ambiguous ctrl code representation This was originally intended to be swept in and dealt with as part of adopting CSI-u (refs: https://github.com/wez/wezterm/issues/63) but the default shift-space mapping is super irritating in vim (refs: https://github.com/wez/wezterm/issues/126) so it got partially walked back, but as a consequence we accidentally dropped the modifiers from those keys (refs: https://github.com/wez/wezterm/issues/213 refs: https://github.com/wez/wezterm/issues/214). This commit restores the modifiers for that case. In addition, since we now have a way to plumb configuration directly into the term crate, this adds a config option to enable CSI-u for those that want to use it. --- docs/changelog.markdown | 8 ++++++- src/config/mod.rs | 7 ++++++ src/config/terminal.rs | 4 ++++ term/src/config.rs | 4 ++++ term/src/terminalstate.rs | 48 +++++++++++++++++++++------------------ 5 files changed, 48 insertions(+), 23 deletions(-) diff --git a/docs/changelog.markdown b/docs/changelog.markdown index abe206238..9420557da 100644 --- a/docs/changelog.markdown +++ b/docs/changelog.markdown @@ -10,7 +10,13 @@ daily) from the master branch. It may not be usable and the feature set may change. As features stabilize some brief notes about them may accumulate here. -* Not yet! +* Fixed default mapping of ambiguous ctrl key combinations (`i`, `m`, `[`, `{`, + `@`) so that they emit the old school tab, newline, escape etc. values. + These got broken as part of prototyping CSI-u support a while back. +* Added option to enable CSI-u key encodings. This is a new mapping scheme + defined here that disambiguates + and otherwise enables more key binding combinations. You can enable this + setting using `enable_csi_u_key_encoding = true` in your config file. ### 20200607-144723-74889cd4 diff --git a/src/config/mod.rs b/src/config/mod.rs index 9849435a7..9e75c128b 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -519,6 +519,13 @@ pub struct Config { #[serde(default = "default_update_interval")] pub check_for_updates_interval_seconds: u64, + + /// When set to true, use the CSI-U encoding scheme as described + /// in http://www.leonerd.org.uk/hacks/fixterms/ + /// This is off by default because @wez and @jsgf find the shift-space + /// mapping annoying in vim :-p + #[serde(default)] + pub enable_csi_u_key_encoding: bool, } fn default_update_interval() -> u64 { diff --git a/src/config/terminal.rs b/src/config/terminal.rs index 0fdd61c7a..682a919f1 100644 --- a/src/config/terminal.rs +++ b/src/config/terminal.rs @@ -21,6 +21,10 @@ impl term::TerminalConfiguration for TermConfig { (config.generation(), config.hyperlink_rules.clone()) } + fn enable_csi_u_key_encoding(&self) -> bool { + configuration().enable_csi_u_key_encoding + } + fn color_palette(&self) -> ColorPalette { let config = configuration(); diff --git a/term/src/config.rs b/term/src/config.rs index 0607a5b7a..8ba25b6d9 100644 --- a/term/src/config.rs +++ b/term/src/config.rs @@ -27,6 +27,10 @@ pub trait TerminalConfiguration: std::fmt::Debug { } } + fn enable_csi_u_key_encoding(&self) -> bool { + false + } + // TODO: expose scroll_to_bottom_on_key_input in config file fn scroll_to_bottom_on_key_input(&self) -> bool { true diff --git a/term/src/terminalstate.rs b/term/src/terminalstate.rs index 8cb525b77..34d3de463 100644 --- a/term/src/terminalstate.rs +++ b/term/src/terminalstate.rs @@ -256,23 +256,6 @@ fn encode_modifiers(mods: KeyModifiers) -> u8 { number } -// FIXME: provide an option to enable this, because it is super annoying -// in vim when accidentally pressing shift-space and it emits a sequence -// that undoes some number of commands -const ENABLE_CSI_U: bool = false; - -fn csi_u_encode(buf: &mut String, c: char, mods: KeyModifiers) -> Result<(), Error> { - if ENABLE_CSI_U { - write!(buf, "\x1b[{};{}u", c as u32, 1 + encode_modifiers(mods))?; - } else { - // FIXME: this ignores the modifiers completely. That's sort of - // OK, but eg: CTRL-SPACE should really send a NUL byte in that - // case, so this isn't great - write!(buf, "{}", c)?; - } - Ok(()) -} - /// characters that when masked for CTRL could be an ascii control character /// or could be a key that a user legitimately wants to process in their /// terminal application @@ -602,6 +585,23 @@ impl TerminalState { Ok(()) } + fn csi_u_encode(&self, buf: &mut String, c: char, mods: KeyModifiers) -> Result<(), Error> { + if self.config.enable_csi_u_key_encoding() { + write!(buf, "\x1b[{};{}u", c as u32, 1 + encode_modifiers(mods))?; + } else { + let c = if mods.contains(KeyModifiers::CTRL) { + ((c as u8) & 0x1f) as char + } else { + c + }; + if mods.contains(KeyModifiers::ALT) { + buf.push(0x1b as char); + } + write!(buf, "{}", c)?; + } + Ok(()) + } + /// Processes a key_down event generated by the gui/render layer /// that is embedding the Terminal. This method translates the /// keycode into a sequence of bytes to send to the slave end @@ -635,12 +635,16 @@ impl TerminalState { // TODO: also respect self.application_keypad let to_send = match key { - Char(c) if is_ambiguous_ascii_ctrl(c) && mods.contains(KeyModifiers::CTRL) => { - csi_u_encode(&mut buf, c, mods)?; + Char(c) + if is_ambiguous_ascii_ctrl(c) + && mods.contains(KeyModifiers::CTRL) + && self.config.enable_csi_u_key_encoding() => + { + self.csi_u_encode(&mut buf, c, mods)?; buf.as_str() } Char(c) if c.is_ascii_uppercase() && mods.contains(KeyModifiers::CTRL) => { - csi_u_encode(&mut buf, c, mods)?; + self.csi_u_encode(&mut buf, c, mods)?; buf.as_str() } @@ -680,7 +684,7 @@ impl TerminalState { _ => unreachable!(), }; if mods.contains(KeyModifiers::SHIFT) || mods.contains(KeyModifiers::CTRL) { - csi_u_encode(&mut buf, c, mods)?; + self.csi_u_encode(&mut buf, c, mods)?; } else { if mods.contains(KeyModifiers::ALT) && key != Escape { buf.push(0x1b as char); @@ -711,7 +715,7 @@ impl TerminalState { if mods.is_empty() { buf.push(c); } else { - csi_u_encode(&mut buf, c, mods)?; + self.csi_u_encode(&mut buf, c, mods)?; } buf.as_str() }