From 7cfc6297bcc3c13511b6b54d7416f09cb89f7e3e Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Tue, 6 Aug 2024 10:32:41 +0200 Subject: [PATCH] Hack to make alt-{left,right} work again in iTerm2 iTerm2 deviates from protocol, so back out c3c832761 (Stop using stack for kitty progressive enhancement, 2024-08-03) in that case. Note that we use several ways of detecting iTerm2 (ITERM_PROFILE, TERM_PROGRAM=iTerm.app, ITERM_SESSION_ID). LC_TERMINAL seems superior because it works over ssh. This new one should hopefully go away eventually. --- src/input_common.rs | 33 +++++++++++++++++++++------------ src/reader.rs | 7 +++++++ 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/input_common.rs b/src/input_common.rs index d9b8305c6..fc9ece828 100644 --- a/src/input_common.rs +++ b/src/input_common.rs @@ -435,6 +435,7 @@ static TERMINAL_PROTOCOLS: MainThread>> = pub(crate) static IS_TMUX: RelaxedAtomicBool = RelaxedAtomicBool::new(false); pub(crate) static IN_MIDNIGHT_COMMANDER: RelaxedAtomicBool = RelaxedAtomicBool::new(false); +pub(crate) static IN_ITERM: RelaxedAtomicBool = RelaxedAtomicBool::new(false); pub fn terminal_protocols_enable_ifn() { if IN_MIDNIGHT_COMMANDER.load() { @@ -461,12 +462,16 @@ struct TerminalProtocols {} impl TerminalProtocols { fn new() -> Self { - let sequences = concat!( - "\x1b[?2004h", // Bracketed paste - "\x1b[>4;1m", // XTerm's modifyOtherKeys - "\x1b[=5u", // CSI u with kitty progressive enhancement - "\x1b=", // set application keypad mode, so the keypad keys send unique codes - ); + let sequences = if IN_ITERM.load() { + concat!("\x1b[?2004h", "\x1b[>4;1m", "\x1b[>5u", "\x1b=",) + } else { + concat!( + "\x1b[?2004h", // Bracketed paste + "\x1b[>4;1m", // XTerm's modifyOtherKeys + "\x1b[=5u", // CSI u with kitty progressive enhancement + "\x1b=", // set application keypad mode, so the keypad keys send unique codes + ) + }; FLOG!( term_protocols, format!( @@ -485,12 +490,16 @@ impl TerminalProtocols { impl Drop for TerminalProtocols { fn drop(&mut self) { - let sequences = concat!( - "\x1b[?2004l", // Bracketed paste - "\x1b[>4;0m", // XTerm's modifyOtherKeys - "\x1b[=0u", // CSI u with kitty progressive enhancement - "\x1b>", // application keypad mode - ); + let sequences = if IN_ITERM.load() { + concat!("\x1b[?2004l", "\x1b[>4;0m", "\x1b[<1u", "\x1b>",) + } else { + concat!( + "\x1b[?2004l", // Bracketed paste + "\x1b[>4;0m", // XTerm's modifyOtherKeys + "\x1b[=0u", // CSI u with kitty progressive enhancement + "\x1b>", // application keypad mode + ) + }; FLOG!( term_protocols, format!( diff --git a/src/reader.rs b/src/reader.rs index 7046f63c3..a36a362b2 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -70,6 +70,7 @@ use crate::history::{ SearchType, }; use crate::input::init_input; +use crate::input_common::IN_ITERM; use crate::input_common::IN_MIDNIGHT_COMMANDER; use crate::input_common::{ terminal_protocols_disable_ifn, terminal_protocols_enable_ifn, CharEvent, CharInputStyle, @@ -3851,6 +3852,12 @@ fn reader_interactive_init(parser: &Parser) { IS_TMUX.store(parser.vars().get_unless_empty(L!("TMUX")).is_some()); IN_MIDNIGHT_COMMANDER.store(parser.vars().get_unless_empty(L!("MC_TMPDIR")).is_some()); + IN_ITERM.store( + parser + .vars() + .get(L!("LC_TERMINAL")) + .is_some_and(|term| term.as_list() == &[L!("iTerm2")]), + ); } /// Destroy data for interactive use.