From 43614f001535fa705c3d9bc5f7698e2fd6800bc4 Mon Sep 17 00:00:00 2001 From: ch1ll321 Date: Thu, 12 Nov 2026 13:20:51 +0800 Subject: [PATCH 1/3] wip(terminal): drop static special-key table, add app-cursor reader The static bindings for arrows/Home/End/PageUp/PageDown/Insert/Delete and F1-F12 can only ever send the unmodified CSI forms, so they would shadow the mode-aware encoder this branch adds; remove them here first. Also add TerminalSession::app_cursor_mode() reading DECCKM through a try-lock. --- crabport-core/src/keybind.rs | 189 +----------------------------- crabport-terminal/src/terminal.rs | 13 ++ 2 files changed, 19 insertions(+), 183 deletions(-) diff --git a/crabport-core/src/keybind.rs b/crabport-core/src/keybind.rs index e689612..86cec38 100644 --- a/crabport-core/src/keybind.rs +++ b/crabport-core/src/keybind.rs @@ -47,11 +47,6 @@ pub fn default_bindings() -> Vec { ..Modifiers::default() }; - let shift = Modifiers { - shift: true, - ..Modifiers::default() - }; - let ctrl_shift = Modifiers { control: true, shift: true, @@ -63,11 +58,6 @@ pub fn default_bindings() -> Vec { ..Modifiers::default() }; - let alt = Modifiers { - alt: true, - ..Modifiers::default() - }; - vec![ // ---- Platform shortcuts (GUI, not sent to PTY) ---- Binding { @@ -128,6 +118,7 @@ pub fn default_bindings() -> Vec { binding_ctrl("y", 0x19), binding_ctrl("z", 0x1a), // Ctrl+other keys + // ---- Ctrl+other keys ---- Binding { modifiers: ctrl, key: "space", @@ -169,179 +160,11 @@ pub fn default_bindings() -> Vec { key: "escape", action: KeyAction::Bytes(vec![0x1b]), }, - // ---- Cursor keys ---- - Binding { - modifiers: Modifiers::default(), - key: "up", - action: KeyAction::Bytes(b"\x1b[A".to_vec()), - }, - Binding { - modifiers: Modifiers::default(), - key: "down", - action: KeyAction::Bytes(b"\x1b[B".to_vec()), - }, - Binding { - modifiers: Modifiers::default(), - key: "right", - action: KeyAction::Bytes(b"\x1b[C".to_vec()), - }, - Binding { - modifiers: Modifiers::default(), - key: "left", - action: KeyAction::Bytes(b"\x1b[D".to_vec()), - }, - Binding { - modifiers: shift, - key: "up", - action: KeyAction::Bytes(b"\x1b[1;2A".to_vec()), - }, - Binding { - modifiers: shift, - key: "down", - action: KeyAction::Bytes(b"\x1b[1;2B".to_vec()), - }, - Binding { - modifiers: shift, - key: "right", - action: KeyAction::Bytes(b"\x1b[1;2C".to_vec()), - }, - Binding { - modifiers: shift, - key: "left", - action: KeyAction::Bytes(b"\x1b[1;2D".to_vec()), - }, - Binding { - modifiers: alt, - key: "up", - action: KeyAction::Bytes(b"\x1b[1;3A".to_vec()), - }, - Binding { - modifiers: alt, - key: "down", - action: KeyAction::Bytes(b"\x1b[1;3B".to_vec()), - }, - Binding { - modifiers: alt, - key: "right", - action: KeyAction::Bytes(b"\x1b[1;3C".to_vec()), - }, - Binding { - modifiers: alt, - key: "left", - action: KeyAction::Bytes(b"\x1b[1;3D".to_vec()), - }, - Binding { - modifiers: ctrl, - key: "up", - action: KeyAction::Bytes(b"\x1b[1;5A".to_vec()), - }, - Binding { - modifiers: ctrl, - key: "down", - action: KeyAction::Bytes(b"\x1b[1;5B".to_vec()), - }, - Binding { - modifiers: ctrl, - key: "right", - action: KeyAction::Bytes(b"\x1b[1;5C".to_vec()), - }, - Binding { - modifiers: ctrl, - key: "left", - action: KeyAction::Bytes(b"\x1b[1;5D".to_vec()), - }, - // ---- Home / End / PageUp / PageDown / Insert / Delete ---- - Binding { - modifiers: Modifiers::default(), - key: "home", - action: KeyAction::Bytes(b"\x1b[H".to_vec()), - }, - Binding { - modifiers: Modifiers::default(), - key: "end", - action: KeyAction::Bytes(b"\x1b[F".to_vec()), - }, - Binding { - modifiers: Modifiers::default(), - key: "pageup", - action: KeyAction::Bytes(b"\x1b[5~".to_vec()), - }, - Binding { - modifiers: Modifiers::default(), - key: "pagedown", - action: KeyAction::Bytes(b"\x1b[6~".to_vec()), - }, - Binding { - modifiers: Modifiers::default(), - key: "insert", - action: KeyAction::Bytes(b"\x1b[2~".to_vec()), - }, - Binding { - modifiers: Modifiers::default(), - key: "delete", - action: KeyAction::Bytes(b"\x1b[3~".to_vec()), - }, - // ---- Function keys ---- - Binding { - modifiers: Modifiers::default(), - key: "f1", - action: KeyAction::Bytes(b"\x1bOP".to_vec()), - }, - Binding { - modifiers: Modifiers::default(), - key: "f2", - action: KeyAction::Bytes(b"\x1bOQ".to_vec()), - }, - Binding { - modifiers: Modifiers::default(), - key: "f3", - action: KeyAction::Bytes(b"\x1bOR".to_vec()), - }, - Binding { - modifiers: Modifiers::default(), - key: "f4", - action: KeyAction::Bytes(b"\x1bOS".to_vec()), - }, - Binding { - modifiers: Modifiers::default(), - key: "f5", - action: KeyAction::Bytes(b"\x1b[15~".to_vec()), - }, - Binding { - modifiers: Modifiers::default(), - key: "f6", - action: KeyAction::Bytes(b"\x1b[17~".to_vec()), - }, - Binding { - modifiers: Modifiers::default(), - key: "f7", - action: KeyAction::Bytes(b"\x1b[18~".to_vec()), - }, - Binding { - modifiers: Modifiers::default(), - key: "f8", - action: KeyAction::Bytes(b"\x1b[19~".to_vec()), - }, - Binding { - modifiers: Modifiers::default(), - key: "f9", - action: KeyAction::Bytes(b"\x1b[20~".to_vec()), - }, - Binding { - modifiers: Modifiers::default(), - key: "f10", - action: KeyAction::Bytes(b"\x1b[21~".to_vec()), - }, - Binding { - modifiers: Modifiers::default(), - key: "f11", - action: KeyAction::Bytes(b"\x1b[23~".to_vec()), - }, - Binding { - modifiers: Modifiers::default(), - key: "f12", - action: KeyAction::Bytes(b"\x1b[24~".to_vec()), - }, + // Cursor keys, Home/End, PageUp/PageDown, Insert/Delete and F1–F12 + // are encoded by [`encode_special_key`] at keypress time, so they can + // follow the program's DECCKM mode and the xterm modifier encoding — + // a static table can only ever send the unmodified CSI forms, which + // is what made the cursor keys wrong inside vim/less. ] } diff --git a/crabport-terminal/src/terminal.rs b/crabport-terminal/src/terminal.rs index 5117e3c..fd6c5d9 100644 --- a/crabport-terminal/src/terminal.rs +++ b/crabport-terminal/src/terminal.rs @@ -1164,6 +1164,19 @@ impl TerminalSession { self.kitty_keyboard.load(Ordering::SeqCst) != 0 } + /// Whether the running program has enabled DECCKM (`\x1b[?1h`), the + /// application-cursor-keys mode that vim/less/nano switch into. Arrow and + /// Home/End keys must then use the SS3 forms (`ESC O A`, `ESC O H`) + /// instead of the CSI forms. + /// + /// Reads through a try-lock: a keypress that lands while the PTY thread + /// holds the term lock encodes the normal CSI form rather than stalling + /// input. + pub fn app_cursor_mode(&self) -> bool { + self.try_with_term_mut(|term| term.mode().contains(TermMode::APP_CURSOR)) + .unwrap_or(false) + } + /// Scan a chunk of *program output* for kitty-keyboard negotiation /// requests and react accordingly. /// From 0b561205a6d638cefc8ff46463b0416a489ab63a Mon Sep 17 00:00:00 2001 From: ch1ll321 Date: Thu, 12 Nov 2026 13:17:49 +0800 Subject: [PATCH 2/3] fix(terminal): encode Ctrl+ as the xterm control byte (#88) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Keys with Ctrl held that no binding covers fell through to nothing: gpui leaves key_char unset whenever Ctrl is held, and reports the shifted character as the key name with shift cleared, so Ctrl+Shift+- arrives as key = "_" — a name no binding matched, and the IME path never sees control keydowns. nano's go-to-line (^_) was unreachable. Map the key name through the xterm control table as a fallback after binding resolution (a-z -> 0x01..0x1a, @ [ \ ] ^ _ -> 0x00..0x1f, ? -> 0x7f); undefined keys (-, /, digits) still fall through unchanged, and an active IME composition is never intercepted. --- crabport-core/src/keybind.rs | 120 ++++++++++++++++++++++++++++++ crabport-ui/src/views/terminal.rs | 23 +++++- 2 files changed, 142 insertions(+), 1 deletion(-) diff --git a/crabport-core/src/keybind.rs b/crabport-core/src/keybind.rs index 86cec38..9a8ada1 100644 --- a/crabport-core/src/keybind.rs +++ b/crabport-core/src/keybind.rs @@ -211,3 +211,123 @@ pub fn resolve<'a>(keystroke: &Keystroke, bindings: &'a [Binding]) -> Option<&'a }) .map(|b| &b.action) } + +#[cfg(test)] +mod tests { + use super::*; + + fn key(key: &str, m: Modifiers) -> Keystroke { + Keystroke { + key: key.to_owned(), + key_char: None, + modifiers: m, + } + } + + fn ctrl() -> Modifiers { + Modifiers { + control: true, + ..Modifiers::default() + } + } + + #[test] + fn letters_map_to_their_control_byte() { + assert_eq!(control_byte(&key("c", ctrl())), Some(0x03)); + assert_eq!(control_byte(&key("u", ctrl())), Some(0x15)); + assert_eq!(control_byte(&key("z", ctrl())), Some(0x1a)); + } + + /// #88: gpui reports `Ctrl+Shift+-` as `key = "_"` with `shift` cleared, + /// so the key name is what has to map, and it must become US (0x1f) — + /// nano's go-to-line. + #[test] + fn shifted_punctuation_maps_by_its_key_name() { + assert_eq!(control_byte(&key("_", ctrl())), Some(0x1f)); + assert_eq!(control_byte(&key("[", ctrl())), Some(0x1b)); + assert_eq!(control_byte(&key("\\", ctrl())), Some(0x1c)); + assert_eq!(control_byte(&key("]", ctrl())), Some(0x1d)); + assert_eq!(control_byte(&key("^", ctrl())), Some(0x1e)); + assert_eq!(control_byte(&key("@", ctrl())), Some(0x00)); + } + + #[test] + fn question_mark_is_del_not_us() { + assert_eq!(control_byte(&key("?", ctrl())), Some(0x7f)); + } + + #[test] + fn undefined_keys_fall_through() { + // `-` without Shift has no control byte (sending CR here would act + // like Enter in the shell). + assert_eq!(control_byte(&key("-", ctrl())), None); + assert_eq!(control_byte(&key("/", ctrl())), None); + assert_eq!(control_byte(&key("1", ctrl())), None); + } + + #[test] + fn other_modifiers_never_map() { + let mut m = ctrl(); + m.platform = true; + assert_eq!(control_byte(&key("_", m)), None); + + let mut m = ctrl(); + m.alt = true; + assert_eq!(control_byte(&key("_", m)), None); + + let mut m = ctrl(); + m.function = true; + assert_eq!(control_byte(&key("_", m)), None); + + // Without Ctrl there is nothing to map. + assert_eq!(control_byte(&key("_", Modifiers::default())), None); + } + + #[test] + fn multi_character_and_named_keys_do_not_map() { + assert_eq!(control_byte(&key("home", ctrl())), None); + assert_eq!(control_byte(&key("ab", ctrl())), None); + } +} + +/// The control byte xterm would send for a key with `Ctrl` held and no ⌘, +/// Option or Fn — the xterm/ECMA-48 control-character table: +/// +/// | key | byte | key | byte | +/// | --- | --- | --- | --- | +/// | `a`–`z` | 0x01–0x1a | `@` `[` `\` `]` `^` `_` | 0x00–0x1f | +/// | `?` | 0x7f (DEL) | | | +/// +/// Keys outside that table (`-`, `/`, digits, …) have no defined control +/// byte and yield `None`, so the key falls through unchanged. +/// +/// This reads `key`, not `key_char`: gpui leaves `key_char` unset whenever +/// Ctrl is held, and reports the shifted character as the key name with +/// `shift` cleared — `Ctrl+Shift+-` arrives as `key = "_"` — so a binding +/// named `ctrl-shift--` could never match and the key was silently dropped +/// (#88). `Ctrl+_` and therefore `Ctrl+Shift+-` now send US (0x1f), which is +/// nano's go-to-line. +pub fn control_byte(keystroke: &Keystroke) -> Option { + let m = &keystroke.modifiers; + if !m.control || m.platform || m.alt || m.function { + return None; + } + let mut chars = keystroke.key.chars(); + let (c, single) = match (chars.next(), chars.next()) { + (Some(c), None) => (c, true), + _ => return None, + }; + if !single { + return None; + } + let lower = c.to_ascii_lowercase() as u8; + match lower { + // Ctrl+A–Z → 0x01–0x1a. + b'a'..=b'z' => Some(lower - b'a' + 0x01), + // Ctrl+@ [ \ ] ^ _ → 0x00–0x1f. + b'@'..=b'_' => Some(lower - b'@'), + // Ctrl+? is DEL, not US: 0x1f already belongs to Ctrl+_. + b'?' => Some(0x7f), + _ => None, + } +} diff --git a/crabport-ui/src/views/terminal.rs b/crabport-ui/src/views/terminal.rs index c67c7c3..c641050 100644 --- a/crabport-ui/src/views/terminal.rs +++ b/crabport-ui/src/views/terminal.rs @@ -2389,7 +2389,28 @@ impl Render for TerminalView { // and we don't want the IME to swallow or echo them. cx.stop_propagation(); } - None => {} + None => { + // Keys with Ctrl held that no binding covers fall + // through to the xterm control-byte table (#88): + // `Ctrl+Shift+-` arrives from gpui as `key = "_"` with + // `shift` cleared and `key_char` unset, so nothing + // above can match it, and the IME path never sees + // control keydowns either. Skipped while an IME + // composition is active so editing keys still reach + // the input context. + let composing = this + .marked_text + .lock() + .as_ref() + .map(|s| !s.is_empty()) + .unwrap_or(false); + if !composing && let Some(byte) = keybind::control_byte(&event.keystroke) { + this.session.write(&[byte]); + this.session.scroll_to_bottom(); + cx.notify(); + cx.stop_propagation(); + } + } } })) .child( From 064944b9990f67ddbcf24434f53142b0ebdcf83e Mon Sep 17 00:00:00 2001 From: ch1ll321 Date: Thu, 12 Nov 2026 13:27:59 +0800 Subject: [PATCH 3/3] feat(terminal): mode-aware special keys for full-screen programs The static table for arrows/Home/End/PageUp/PageDown/Insert/Delete and F1-F12 could only ever send the unmodified CSI forms. Two gaps in full-screen programs (vim, less, htop): - DECCKM: once a program enables application cursor keys (ESC [ ? 1 h), the unmodified cursor keys and Home/End must use the SS3 forms (ESC O A, ESC O H). Everything kept sending CSI, so vim moved by sentences and less scrolled wrong. - Modifiers: Shift/Ctrl/Alt combinations of those keys were dropped entirely; xterm encodes them as CSI 1;{m}X / CSI {n};{m}~. Replace the static table with keybind::encode_special_key(), which takes the program's DECCKM state (TerminalSession::app_cursor_mode(), read through a try-lock) and the xterm modifier parameter (1 + shift + 2*alt + 4*ctrl). F1-F4 use SS3 unmodified and CSI with modifiers; the ~-form keys ignore DECCKM. Cmd-chords stay with the app, matching the kitty-protocol path. --- crabport-core/src/keybind.rs | 240 ++++++++++++++++++++++++++++++ crabport-ui/src/views/terminal.rs | 46 ++++-- 2 files changed, 275 insertions(+), 11 deletions(-) diff --git a/crabport-core/src/keybind.rs b/crabport-core/src/keybind.rs index 9a8ada1..47938d3 100644 --- a/crabport-core/src/keybind.rs +++ b/crabport-core/src/keybind.rs @@ -212,6 +212,92 @@ pub fn resolve<'a>(keystroke: &Keystroke, bindings: &'a [Binding]) -> Option<&'a .map(|b| &b.action) } +/// The xterm modifier parameter for a key event: `1 + shift + 2*alt + 4*ctrl`. +/// `1` is the protocol's "no modifier" value. +fn modifier_parameter(m: &Modifiers) -> u8 { + 1 + (m.shift as u8) + 2 * (m.alt as u8) + 4 * (m.control as u8) +} + +/// Encode a special (non-text) key the way xterm does, honouring the +/// program's DECCKM mode and the xterm modifier encoding: +/// +/// - Arrow, Home and End use the CSI forms (`ESC [ A`, `ESC [ H`) in normal +/// mode and the SS3 forms (`ESC O A`, `ESC O H`) once the program enables +/// DECCKM (`ESC [ ? 1 h`) — what vim/less do at startup. +/// - With a modifier held, xterm switches to the parameterised CSI form +/// (`ESC [ 1;5C` for Ctrl+Right), so DECCKM no longer applies. F1–F4 use +/// SS3 unmodified (`ESC O P`) and CSI with modifiers (`ESC [ 1;2P`). +/// - PageUp/PageDown, Insert/Delete and F5–F12 are `~`-form sequences and are +/// unaffected by DECCKM. +/// +/// Returns `None` for every other key, and when ⌘ is held (⌘ shortcuts stay +/// with the application, as the kitty-protocol path already does). +pub fn encode_special_key(key: &str, mods: &Modifiers, app_cursor: bool) -> Option> { + if mods.platform || mods.function { + return None; + } + let m = modifier_parameter(mods); + + let seq = match key { + "up" => cursor('A', m, app_cursor), + "down" => cursor('B', m, app_cursor), + "right" => cursor('C', m, app_cursor), + "left" => cursor('D', m, app_cursor), + "home" => cursor('H', m, app_cursor), + "end" => cursor('F', m, app_cursor), + "pageup" => tilde(5, m), + "pagedown" => tilde(6, m), + "insert" => tilde(2, m), + "delete" => tilde(3, m), + "f1" => function('P', m), + "f2" => function('Q', m), + "f3" => function('R', m), + "f4" => function('S', m), + "f5" => tilde(15, m), + "f6" => tilde(17, m), + "f7" => tilde(18, m), + "f8" => tilde(19, m), + "f9" => tilde(20, m), + "f10" => tilde(21, m), + "f11" => tilde(23, m), + "f12" => tilde(24, m), + _ => return None, + }; + Some(seq.into_bytes()) +} + +/// One of the cursor-family keys: CSI `X` (or SS3 `X` in application mode) +/// unmodified, parameterised CSI with a modifier held. +fn cursor(final_byte: char, m: u8, app_cursor: bool) -> String { + if m == 1 { + if app_cursor { + format!("\x1bO{final_byte}") + } else { + format!("\x1b[{final_byte}") + } + } else { + format!("\x1b[1;{m}{final_byte}") + } +} + +/// One of the `~`-form keys (PageUp/PageDown, Insert/Delete, F5–F12). +fn tilde(number: u8, m: u8) -> String { + if m == 1 { + format!("\x1b[{number}~") + } else { + format!("\x1b[{number};{m}~") + } +} + +/// F1–F4: SS3 unmodified, parameterised CSI with a modifier held. +fn function(final_byte: char, m: u8) -> String { + if m == 1 { + format!("\x1bO{final_byte}") + } else { + format!("\x1b[1;{m}{final_byte}") + } +} + #[cfg(test)] mod tests { use super::*; @@ -288,6 +374,160 @@ mod tests { assert_eq!(control_byte(&key("home", ctrl())), None); assert_eq!(control_byte(&key("ab", ctrl())), None); } + + fn shift() -> Modifiers { + Modifiers { + shift: true, + ..Modifiers::default() + } + } + + fn alt() -> Modifiers { + Modifiers { + alt: true, + ..Modifiers::default() + } + } + + // ----------------------------------------------------------------- + // encode_special_key + // ----------------------------------------------------------------- + + #[test] + fn arrows_use_csi_outside_application_mode() { + assert_eq!( + encode_special_key("up", &Modifiers::default(), false).as_deref(), + Some(b"\x1b[A".as_slice()) + ); + assert_eq!( + encode_special_key("down", &Modifiers::default(), false).as_deref(), + Some(b"\x1b[B".as_slice()) + ); + assert_eq!( + encode_special_key("right", &Modifiers::default(), false).as_deref(), + Some(b"\x1b[C".as_slice()) + ); + assert_eq!( + encode_special_key("left", &Modifiers::default(), false).as_deref(), + Some(b"\x1b[D".as_slice()) + ); + } + + /// In vim/less (DECCKM on) the unmodified cursor keys must use the SS3 + /// forms, and Home/End follow the same rule. + #[test] + fn application_mode_switches_to_ss3() { + assert_eq!( + encode_special_key("up", &Modifiers::default(), true).as_deref(), + Some(b"\x1bOA".as_slice()) + ); + assert_eq!( + encode_special_key("left", &Modifiers::default(), true).as_deref(), + Some(b"\x1bOD".as_slice()) + ); + assert_eq!( + encode_special_key("home", &Modifiers::default(), true).as_deref(), + Some(b"\x1bOH".as_slice()) + ); + assert_eq!( + encode_special_key("end", &Modifiers::default(), true).as_deref(), + Some(b"\x1bOF".as_slice()) + ); + } + + #[test] + fn modifiers_use_the_parameterised_csi_form_even_in_application_mode() { + // 1 + shift = 2. + assert_eq!( + encode_special_key("up", &shift(), false).as_deref(), + Some(b"\x1b[1;2A".as_slice()) + ); + // 1 + alt = 3. + assert_eq!( + encode_special_key("left", &alt(), true).as_deref(), + Some(b"\x1b[1;3D".as_slice()) + ); + // 1 + ctrl = 5. + assert_eq!( + encode_special_key("right", &ctrl(), false).as_deref(), + Some(b"\x1b[1;5C".as_slice()) + ); + // 1 + shift + ctrl = 6. + let mut m = ctrl(); + m.shift = true; + assert_eq!( + encode_special_key("home", &m, true).as_deref(), + Some(b"\x1b[1;6H".as_slice()) + ); + } + + #[test] + fn home_and_end_use_csi_outside_application_mode() { + assert_eq!( + encode_special_key("home", &Modifiers::default(), false).as_deref(), + Some(b"\x1b[H".as_slice()) + ); + assert_eq!( + encode_special_key("end", &Modifiers::default(), false).as_deref(), + Some(b"\x1b[F".as_slice()) + ); + } + + #[test] + fn tilde_keys_ignore_application_mode() { + assert_eq!( + encode_special_key("pageup", &Modifiers::default(), false).as_deref(), + Some(b"\x1b[5~".as_slice()) + ); + assert_eq!( + encode_special_key("pagedown", &Modifiers::default(), true).as_deref(), + Some(b"\x1b[6~".as_slice()) + ); + assert_eq!( + encode_special_key("insert", &Modifiers::default(), false).as_deref(), + Some(b"\x1b[2~".as_slice()) + ); + assert_eq!( + encode_special_key("delete", &shift(), false).as_deref(), + Some(b"\x1b[3;2~".as_slice()) + ); + } + + #[test] + fn function_keys_use_ss3_then_csi() { + assert_eq!( + encode_special_key("f1", &Modifiers::default(), false).as_deref(), + Some(b"\x1bOP".as_slice()) + ); + assert_eq!( + encode_special_key("f4", &Modifiers::default(), true).as_deref(), + Some(b"\x1bOS".as_slice()) + ); + assert_eq!( + encode_special_key("f1", &shift(), false).as_deref(), + Some(b"\x1b[1;2P".as_slice()) + ); + assert_eq!( + encode_special_key("f5", &Modifiers::default(), false).as_deref(), + Some(b"\x1b[15~".as_slice()) + ); + assert_eq!( + encode_special_key("f12", &ctrl(), false).as_deref(), + Some(b"\x1b[24;5~".as_slice()) + ); + } + + #[test] + fn cmd_shortcuts_and_unknown_keys_are_left_to_the_app() { + let mut m = Modifiers::default(); + m.platform = true; + assert_eq!(encode_special_key("up", &m, false), None); + assert_eq!( + encode_special_key("enter", &Modifiers::default(), false), + None + ); + assert_eq!(encode_special_key("a", &Modifiers::default(), false), None); + } } /// The control byte xterm would send for a key with `Ctrl` held and no ⌘, diff --git a/crabport-ui/src/views/terminal.rs b/crabport-ui/src/views/terminal.rs index c641050..d6b9cce 100644 --- a/crabport-ui/src/views/terminal.rs +++ b/crabport-ui/src/views/terminal.rs @@ -2392,23 +2392,47 @@ impl Render for TerminalView { None => { // Keys with Ctrl held that no binding covers fall // through to the xterm control-byte table (#88): - // `Ctrl+Shift+-` arrives from gpui as `key = "_"` with - // `shift` cleared and `key_char` unset, so nothing - // above can match it, and the IME path never sees - // control keydowns either. Skipped while an IME - // composition is active so editing keys still reach - // the input context. + // Keys no binding covers fall through to the xterm + // encodings: + // + // - `Ctrl+` → its control byte (#88). gpui + // leaves `key_char` unset whenever Ctrl is held, and + // reports the shifted character as the key name with + // `shift` cleared, so `Ctrl+Shift+-` arrives as + // `key = "_"` — a name no binding matched, and the + // IME path never sees control keydowns either. + // - Special keys (arrows, Home/End, PageUp/PageDown, + // Insert/Delete, F1–F12) → the xterm forms that + // follow the program's DECCKM mode and carry + // modifiers, which is what vim/less need. + // + // Skipped while an IME composition is active so + // editing keys still reach the input context. let composing = this .marked_text .lock() .as_ref() .map(|s| !s.is_empty()) .unwrap_or(false); - if !composing && let Some(byte) = keybind::control_byte(&event.keystroke) { - this.session.write(&[byte]); - this.session.scroll_to_bottom(); - cx.notify(); - cx.stop_propagation(); + if !composing { + let bytes = keybind::control_byte(&event.keystroke) + .map(|byte| vec![byte]) + .or_else(|| { + keybind::encode_special_key( + &event.keystroke.key, + &event.keystroke.modifiers, + this.session.app_cursor_mode(), + ) + }); + if let Some(bytes) = bytes { + this.session.write(&bytes); + this.session.scroll_to_bottom(); + cx.notify(); + // Stop propagation so the platform's IME input + // context doesn't also receive this keydown: + // these are control sequences, not text. + cx.stop_propagation(); + } } } }