diff --git a/README.md b/README.md index 61e4eb4..8cb8e27 100644 --- a/README.md +++ b/README.md @@ -166,10 +166,10 @@ run `psyterm --shutdown` on the remote host, for example through SSH. | Increase terminal text size | Cmd+Plus or Cmd+= | Ctrl+Plus or Ctrl+= | | Decrease terminal text size | Cmd+Minus | Ctrl+Minus | | Open or dismiss the command palette | Cmd+Shift+P | Ctrl+Shift+P | -| Create a tab | Cmd+T | Ctrl+Shift+T | +| Create a tab | Cmd+T or Option+Shift+Left / Right | Ctrl+Shift+T | | Add a pane to the right | Cmd+D | Ctrl+Shift+D or Alt+\ | | Insert a stacked pane above / below | Option+Shift+Up / Cmd+Shift+D | Alt+Shift+Up / Down | -| Focus the adjacent pane or tab | Cmd+Option+Left / Right | Alt+Left / Right | +| Focus the adjacent pane or tab | Option+Left / Right or Cmd+Option+Left / Right | Alt+Left / Right | | Switch tabs | Cmd+Shift+[ / ] | Alt+Shift+[ / ] | | Focus the previous / next stacked pane | Cmd+Option+Up / Down | Alt+Up / Down | | Cycle through panes | Cmd+[ / ] | Ctrl+Shift+Left / Right | @@ -189,10 +189,9 @@ Choose **Rename tab** in the palette to edit the selected tab's name. When renaming a pane or tab, press Enter to save or Escape to cancel. Saving a blank name restores automatic naming. -Both platforms also support the Alt shortcuts from Zellij's `shared` map and -Alt+\ from its `locked` map. On macOS, Alt is the Option key. -Alt+Shift+N and Alt+Shift+Right also create a tab. Plain Ctrl shortcuts such -as Ctrl+C and Ctrl+D reach the terminal. +On Linux, the Alt shortcuts follow Zellij’s `shared` map, with Alt+\ from its +`locked` map. Alt+Shift+N and Alt+Shift+Right also create a tab. On macOS, +Option+Up/Down also focus stacked panes, and Option+Shift+Up/Down insert them. The command palette searches command names and keywords as you type. Arrow keys or Tab select a result; Enter runs it or opens a submenu. Escape returns to the @@ -201,8 +200,11 @@ live workspace lists, including collapsed panes. Commands without shortcuts, such as restoring automatic pane names, are available here too. Palette input never reaches the shell. -Typing returns to the live screen. Ctrl+C, Ctrl+D and unbound Alt combinations, -arrows, navigation keys, and function keys reach the terminal application. +Typing returns to the live screen. Unbound Alt/Option character keys send Meta. +On a Mac keyboard, Option+Delete (⌥⌫) sends Meta+Backspace, which deletes +the preceding word in Bash and Zsh’s Emacs keymaps. Option+B/F move by word. +Shift+Left/Right carry their xterm Shift modifier so a CLI can bind them. +Plain Ctrl shortcuts such as Ctrl+C and Ctrl+D reach the terminal. Alternate-screen applications receive shifted navigation; the wheel sends arrow keys when the application enables alternate scrolling. diff --git a/src/bindings.rs b/src/bindings.rs index 1414897..874ca35 100644 --- a/src/bindings.rs +++ b/src/bindings.rs @@ -67,29 +67,24 @@ pub fn action_for_key(key: &Keystroke) -> Option { if !modifiers.alt || modifiers.control || modifiers.platform { return None; } - if modifiers.shift { - // Wayland keyboard layouts can report the shifted symbol for brackets. - Some(match name.as_str() { - "[" | "{" => CommandAction::PreviousTab, - "]" | "}" => CommandAction::NextTab, - "n" | "right" => CommandAction::NewTab, - "up" => CommandAction::StackAbove, - "down" => CommandAction::StackBelow, - _ => return None, - }) - } else { - Some(match name.as_str() { - // GPUI consumes Shift for punctuation, leaving Alt+{ / Alt+}. - "{" => CommandAction::PreviousTab, - "}" => CommandAction::NextTab, - "left" => CommandAction::FocusLeft, - "right" => CommandAction::FocusRight, - "up" => CommandAction::FocusAbove, - "down" => CommandAction::FocusBelow, - "\\" => CommandAction::SplitPane, - _ => return None, - }) - } + Some(match (modifiers.shift, name.as_str()) { + (false, "left") => CommandAction::FocusLeft, + (false, "right") => CommandAction::FocusRight, + (false, "up") => CommandAction::FocusAbove, + (false, "down") => CommandAction::FocusBelow, + (true, "right") => CommandAction::NewTab, + #[cfg(target_os = "macos")] + (true, "left") => CommandAction::NewTab, + (true, "up") => CommandAction::StackAbove, + (true, "down") => CommandAction::StackBelow, + _ if cfg!(target_os = "macos") => return None, + // Linux Zellij aliases. GPUI can fold Shift into a brace. + (true, "[") | (_, "{") => CommandAction::PreviousTab, + (true, "]") | (_, "}") => CommandAction::NextTab, + (true, "n") => CommandAction::NewTab, + (false, "\\") => CommandAction::SplitPane, + _ => return None, + }) } #[cfg(test)] @@ -101,6 +96,7 @@ mod tests { } #[test] + #[cfg(not(target_os = "macos"))] fn zellij_tab_bindings_accept_shifted_and_unshifted_brackets() { for key in ["alt-shift-[", "alt-shift-{", "alt-{"] { assert!(matches!(action(key), Some(CommandAction::PreviousTab))); @@ -108,19 +104,47 @@ mod tests { for key in ["alt-shift-]", "alt-shift-}", "alt-}"] { assert!(matches!(action(key), Some(CommandAction::NextTab))); } - for key in ["alt-shift-n", "alt-shift-right"] { - assert!(matches!(action(key), Some(CommandAction::NewTab))); - } + assert!(matches!(action("alt-shift-n"), Some(CommandAction::NewTab))); + assert!(matches!(action("alt-\\"), Some(CommandAction::SplitPane))); } #[test] + #[cfg(not(target_os = "macos"))] fn zellij_horizontal_focus_and_existing_shortcuts_coexist() { assert!(matches!(action("alt-left"), Some(CommandAction::FocusLeft))); assert!(matches!( action("alt-right"), Some(CommandAction::FocusRight) )); - assert!(matches!(action("alt-\\"), Some(CommandAction::SplitPane))); + assert!(matches!( + action("alt-shift-right"), + Some(CommandAction::NewTab) + )); + } + + #[test] + #[cfg(target_os = "macos")] + fn mac_option_arrows_navigate_and_editing_chords_reach_the_terminal() { + for key in [ + "alt-backspace", + "alt-shift-n", + "alt-shift-[", + "alt-{", + "alt-shift-]", + "alt-}", + "alt-\\", + ] { + assert!(action(key).is_none(), "{key}"); + } + for key in ["alt-left", "cmd-alt-left"] { + assert!(matches!(action(key), Some(CommandAction::FocusLeft))); + } + for key in ["alt-right", "cmd-alt-right"] { + assert!(matches!(action(key), Some(CommandAction::FocusRight))); + } + for key in ["alt-shift-left", "alt-shift-right"] { + assert!(matches!(action(key), Some(CommandAction::NewTab)), "{key}"); + } } #[test] @@ -229,6 +253,7 @@ mod tests { "alt-h", "alt-ctrl-left", "left", + "shift-left", "shift-right", ] { assert!(action(key).is_none(), "{key} must reach the terminal"); diff --git a/src/commands.rs b/src/commands.rs index e965997..c4fe330 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -204,7 +204,7 @@ pub fn root_items(snapshot: Option<&Snapshot>) -> Vec { "focus-left", "Focus pane or tab left", "navigate previous horizontal", - shortcut("Cmd+Option+Left", "Alt+Left"), + shortcut("Option+Left", "Alt+Left"), FocusLeft, ) .available(pane), @@ -212,7 +212,7 @@ pub fn root_items(snapshot: Option<&Snapshot>) -> Vec { "focus-right", "Focus pane or tab right", "navigate next horizontal", - shortcut("Cmd+Option+Right", "Alt+Right"), + shortcut("Option+Right", "Alt+Right"), FocusRight, ) .available(pane), diff --git a/src/input.rs b/src/input.rs index 730503b..50c9410 100644 --- a/src/input.rs +++ b/src/input.rs @@ -115,6 +115,17 @@ pub fn encode_key(key: &Keystroke, mode: TermMode) -> Option> { } "tab" => vec![b'\t'], _ if modifiers.control => vec![control_byte(&key.key)?], + // AppKit's key_char includes Option's symbol or accent. GPUI's + // logical key keeps the underlying character and folds in shifted + // punctuation, but leaves ASCII letters lowercase with Shift set. + #[cfg(target_os = "macos")] + name if modifiers.alt && !modifiers.function && name.chars().count() == 1 => { + let mut bytes = name.as_bytes().to_vec(); + if modifiers.shift { + bytes.make_ascii_uppercase(); + } + bytes + } // Do not fall back to `key`: a missing key_char can mean that a dead // key or compose sequence is still waiting for its next character. _ => key.key_char.as_ref()?.as_bytes().to_vec(), @@ -185,7 +196,11 @@ mod tests { ("ctrl-6", b"\x1e"), ("ctrl-/", b"\x1f"), ("alt-ctrl-c", b"\x1b\x03"), + ("alt-b->b", b"\x1bb"), + ("alt-f->f", b"\x1bf"), + ("alt-space-> ", b"\x1b "), ("backspace", b"\x7f"), + ("alt-backspace", b"\x1b\x7f"), ("ctrl-backspace", b"\x08"), ("enter", b"\r"), ("shift-tab", b"\x1b[Z"), @@ -195,6 +210,22 @@ mod tests { assert_eq!(encode("enter", TermMode::LINE_FEED_NEW_LINE), b"\r\n"); } + #[cfg(target_os = "macos")] + #[test] + fn option_sends_meta_for_letters_symbols_and_dead_keys() { + for (key, expected) in [ + ("alt-b->∫", b"\x1bb".as_slice()), + ("alt-f->ƒ", b"\x1bf"), + ("alt-shift-b->ı", b"\x1bB"), + ("alt-/->÷", b"\x1b/"), + ("alt-?->¿", b"\x1b?"), + ("alt-e->´", b"\x1be"), + ("alt-e", b"\x1be"), + ] { + assert_eq!(encode(key, TermMode::empty()), expected, "{key}"); + } + } + #[test] fn application_cursor_and_modified_navigation() { assert_eq!(encode("up", TermMode::empty()), b"\x1b[A"); @@ -206,6 +237,31 @@ mod tests { assert_eq!(encode("pagedown", TermMode::empty()), b"\x1b[6~"); } + #[test] + fn modified_arrows_preserve_direction_and_modifiers() { + for (mods, parameter) in [ + ("shift", 2), + ("alt", 3), + ("alt-shift", 4), + ("ctrl", 5), + ("ctrl-shift", 6), + ("ctrl-alt", 7), + ("ctrl-alt-shift", 8), + ] { + for (key, end) in [("up", 'A'), ("down", 'B'), ("right", 'C'), ("left", 'D')] { + let binding = format!("{mods}-{key}"); + let csi = format!("\x1b[1;{parameter}{end}"); + for mode in [ + TermMode::empty(), + TermMode::APP_CURSOR, + TermMode::ALT_SCREEN, + ] { + assert_eq!(encode(&binding, mode), csi.as_bytes(), "{binding} {mode:?}"); + } + } + } + } + #[test] fn function_keys_use_xterm_sequences() { assert_eq!(encode("f1", TermMode::empty()), b"\x1bOP");