diff --git a/crabport-core/src/keybind.rs b/crabport-core/src/keybind.rs index c89f260..47938d3 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. ] } @@ -389,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::*; @@ -465,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-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. /// diff --git a/crabport-ui/src/views/terminal.rs b/crabport-ui/src/views/terminal.rs index c641050..396ec2e 100644 --- a/crabport-ui/src/views/terminal.rs +++ b/crabport-ui/src/views/terminal.rs @@ -2392,6 +2392,22 @@ impl Render for TerminalView { None => { // Keys with Ctrl held that no binding covers fall // through to the xterm control-byte table (#88): + // 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. // `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 @@ -2404,6 +2420,25 @@ impl Render for TerminalView { .as_ref() .map(|s| !s.is_empty()) .unwrap_or(false); + 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(); + } if !composing && let Some(byte) = keybind::control_byte(&event.keystroke) { this.session.write(&[byte]); this.session.scroll_to_bottom();