From f0e94084d1df36009b879b06e9cfd2be946168e8 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Mon, 30 Dec 2019 08:14:20 -0800 Subject: [PATCH] change backspace/delete handling Previously we would try to pass through the Backspace and Delete code points without interference, but that behavior wasn't quite right. With this commit our behavior is now: - At the window layer: Classify a `Backspace` key press as logically `BS` and a `Delete` key press as logically `DEL` unless the `swap_backspace_and_delete` is true (macOS is true by default), in which case `Backspace` is mapped to `Delete` and vice versa. - At the terminal input layer: A `Backspace` input from the Window sends the `DEL` sequence to the pty as that matches the default `VERASE` stty configuration. A `Delete` input from the Window emits `\033[3~`, which matches up to the `TERMINFO` for `xterm-256color` which we claim to be compatible with, and is our default `$TERM` value. The net result of this is that `Backspace` will now start to emit `^?` which should match folks stty verase. Heads up to @fanzeyi! I've tested this only on a linux system so far and will follow up on a macOS system a little later today. Refs: https://github.com/wez/wezterm/issues/88 Refs: https://github.com/wez/wezterm/issues/63 --- term/src/terminalstate.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/term/src/terminalstate.rs b/term/src/terminalstate.rs index 823b40b61..97f8c26ee 100644 --- a/term/src/terminalstate.rs +++ b/term/src/terminalstate.rs @@ -919,6 +919,13 @@ impl TerminalState { _ => mods, }; + // Normalize Backspace and Delete + let key = match key { + Char('\x7f') => Delete, + Char('\x08') => Backspace, + c => c, + }; + let mut buf = String::new(); // TODO: also respect self.application_keypad @@ -959,12 +966,13 @@ impl TerminalState { buf.as_str() } - Enter | Escape | Backspace | Char('\x08') | Delete | Char('\x7f') => { + Enter | Escape | Backspace => { let c = match key { Enter => '\r', Escape => '\x1b', - Char('\x08') | Backspace => '\x08', - Char('\x7f') | Delete => '\x7f', + // Backspace sends the default VERASE which is confusingly + // the DEL ascii codepoint + Backspace => '\x7f', _ => unreachable!(), }; if mods.contains(KeyModifiers::SHIFT) || mods.contains(KeyModifiers::CTRL) { @@ -1058,9 +1066,10 @@ impl TerminalState { "" } - PageUp | PageDown | Insert => { + PageUp | PageDown | Insert | Delete => { let c = match key { Insert => 2, + Delete => 3, PageUp => 5, PageDown => 6, _ => unreachable!(),