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.
This commit is contained in:
Wez Furlong committed 2020-06-10 17:07:30 -07:00
1 parent afe0475ced
commit 9f1303fb8e
5 files changed
+48 -23

No files matched your search

+7 -1
View File
@@ -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 <http://www.leonerd.org.uk/hacks/fixterms/> 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
+7
View File
@@ -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 {
+4
View File
@@ -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();
+4
View File
@@ -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
+26 -22
View File
@@ -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()
}