From 4a384aa27275db509b4395ccb5cd6398fb0011f4 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 13 Aug 2023 15:12:13 -0700 Subject: [PATCH] parse and respect mode 1036 --- src/Surface.zig | 22 +++++++++++++++++++--- src/apprt/glfw.zig | 9 +++------ src/terminal/Terminal.zig | 1 + src/terminal/ansi.zig | 3 +++ src/termio/Exec.zig | 2 +- 5 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/Surface.zig b/src/Surface.zig index 0d08901d7..3462df8f0 100644 --- a/src/Surface.zig +++ b/src/Surface.zig @@ -1006,7 +1006,9 @@ pub fn charCallback( } // Critical area - { + const critical: struct { + alt_esc_prefix: bool, + } = critical: { self.renderer_state.mutex.lock(); defer self.renderer_state.mutex.unlock(); @@ -1019,17 +1021,31 @@ pub fn charCallback( // We want to scroll to the bottom // TODO: detect if we're at the bottom to avoid the render call here. try self.io.terminal.scrollViewport(.{ .bottom = {} }); - } + + break :critical .{ + .alt_esc_prefix = self.io.terminal.modes.alt_esc_prefix, + }; + }; var data: termio.Message.WriteReq.Small.Array = undefined; // Prefix our data with ESC if we have alt pressed. var i: u8 = 0; if (mods.alt) alt: { + // If the terminal explicitly disabled this feature using mode 1036, + // then we don't send the prefix. + if (!critical.alt_esc_prefix) { + log.debug("alt_esc_prefix disabled with mode, not sending esc prefix", .{}); + break :alt; + } + // On macOS, we have to opt-in to using alt because option // by default is a unicode character sequence. if (comptime builtin.target.isDarwin()) { - if (!self.config.macos_option_as_alt) break :alt; + if (!self.config.macos_option_as_alt) { + log.debug("macos_option_as_alt disabled, not sending esc prefix", .{}); + break :alt; + } } data[i] = 0x1b; diff --git a/src/apprt/glfw.zig b/src/apprt/glfw.zig index 44467cb9b..195f67646 100644 --- a/src/apprt/glfw.zig +++ b/src/apprt/glfw.zig @@ -611,16 +611,12 @@ pub const Surface = struct { glfw_action: glfw.Action, glfw_mods: glfw.Mods, ) void { + const tracy = trace(@src()); + defer tracy.end(); _ = scancode; const core_win = window.getUserPointer(CoreSurface) orelse return; - // Reset our consumption state - core_win.rt_surface.key_consumed = false; - - const tracy = trace(@src()); - defer tracy.end(); - // Convert our glfw types into our input types const mods: input.Mods = @bitCast(glfw_mods); const action: input.Action = switch (glfw_action) { @@ -756,6 +752,7 @@ pub const Surface = struct { // TODO: we need to do mapped keybindings + core_win.rt_surface.key_mods = mods; core_win.rt_surface.key_consumed = core_win.keyCallback( action, key, diff --git a/src/terminal/Terminal.zig b/src/terminal/Terminal.zig index 7972d696b..ff9b0c8f3 100644 --- a/src/terminal/Terminal.zig +++ b/src/terminal/Terminal.zig @@ -91,6 +91,7 @@ modes: packed struct { deccolm: bool = false, // 3, deccolm_supported: bool = false, // 40 keypad_keys: bool = false, // 66 + alt_esc_prefix: bool = true, // 1036 focus_event: bool = false, // 1004 mouse_alternate_scroll: bool = true, // 1007 diff --git a/src/terminal/ansi.zig b/src/terminal/ansi.zig index 2429210e0..377007bda 100644 --- a/src/terminal/ansi.zig +++ b/src/terminal/ansi.zig @@ -121,6 +121,9 @@ pub const Mode = enum(u16) { /// Report mouse position in the SGR format as pixels, instead of cells. mouse_format_sgr_pixels = 1016, + /// The alt key sends esc as a prefix before any character. On by default. + alt_esc_prefix = 1036, + /// Alternate screen mode with save cursor and clear on enter. alt_screen_save_cursor_clear_enter = 1049, diff --git a/src/termio/Exec.zig b/src/termio/Exec.zig index 8f7d05568..1aec3f269 100644 --- a/src/termio/Exec.zig +++ b/src/termio/Exec.zig @@ -1274,8 +1274,8 @@ const StreamHandler = struct { .mouse_format_sgr_pixels => self.terminal.modes.mouse_format = if (enabled) .sgr_pixels else .x10, .mouse_alternate_scroll => self.terminal.modes.mouse_alternate_scroll = enabled, - .focus_event => self.terminal.modes.focus_event = enabled, + .alt_esc_prefix => self.terminal.modes.alt_esc_prefix = enabled, else => if (enabled) log.warn("unimplemented mode: {}", .{mode}), }