From 4e89db185fa4e3bfe4358d2da3b76fad0cc7e547 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 21 Oct 2026 21:30:24 -0300 Subject: Esc in a raw terminal is pardes's; Shift-Esc goes to the program (ProgramEsc) --- src/pardes.zig | 47 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-) (limited to 'src/pardes.zig') diff --git a/src/pardes.zig b/src/pardes.zig index 72470490..c885acc2 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -6374,10 +6374,23 @@ pub const Pardes = struct { } const pane = p.panes[p.active] orelse return; p.noteTyped(); + // In a raw terminal the two Escs swap unless ProgramEsc is on: Esc + // backs out, and Shift-Esc is the program's, sent as a plain Esc. + const raw_esc = key.cp == Key.escape and !key.ctrl and !key.alt and !p.settings.program_esc and + pane.isTerminal() and pane.body.mode == .tty and pane.focus == .body and pane.prompt == .none and + tagline.focusedHeader(p) == null; + if (raw_esc and key.shift) { + p.leader_on = false; + p.ctrl_w_pending = false; + panes.terminal.followOutput(pane); + var plain = key; + plain.shift = false; + return panes.terminal.forwardKey(p, p.active, plain); + } // Shift-Esc backs out of whatever the pane is in -- a prompt, insert // mode, a pending count, a raw program -- to the previous pane, as // Esc does from normal mode. Nothing under it sees the key. - if (hit(key, config.back_out)) { + if (raw_esc or hit(key, config.back_out)) { p.leader_on = false; p.ctrl_w_pending = false; if (pane.prompt != .none) { @@ -6405,8 +6418,8 @@ pub const Pardes = struct { p.ctrl_w_pending = false; if (key.ctrl and !key.alt and key.cp == p.opts.tty_toggle) return p.toggleTty(p.active); - // Bare Escape leaves only at a shell prompt (Shift-Esc, above, - // always). + // With ProgramEsc on, bare Escape leaves only at a shell prompt + // (Shift-Esc, above, always). if (key.cp == Key.escape and !key.ctrl and !key.alt and !key.shift and exec.takesCommandLine(p, p.active)) return exec.runBuiltin(p, .Last, p.active, "", null); panes.terminal.followOutput(pane); // typing snaps back to live output @@ -8442,10 +8455,36 @@ test "Esc back into a file leaves its view where it was" { try std.testing.expectEqual(@as(usize, @intCast(row)) -| @as(usize, dp.rows) / 2, dp.file.?.scroll); } -test "raw tty Escape only leaves at a shell prompt" { +test "raw tty Esc backs out and Shift-Esc is the program's Esc" { + if (platform == .web) return; + const p = try Pardes.init(std.testing.allocator, .{ .cols = 80, .rows = 24, .file = "mise.toml" }); + defer p.deinit(); + _ = exec.spawnTty(p, 0); + const shell = p.active; + const pane = p.panes[shell].?; + var buf: [256]u8 = undefined; + _ = drainWrites(p, &buf); + + p.update(.{ .key = .{ .cp = 'x', .text = "x" } }); + _ = drainWrites(p, &buf); + p.update(.{ .key = .{ .cp = Key.escape } }); // past typed text too + try std.testing.expectEqual(@as(usize, 0), p.active); + try std.testing.expectEqualStrings("", drainWrites(p, &buf)); + try std.testing.expectEqual(Text.Mode.tty, pane.body.mode); + + p.update(.{ .key = .{ .cp = Key.escape } }); // back into the shell + try std.testing.expectEqual(shell, p.active); + _ = drainWrites(p, &buf); + p.update(.{ .key = .{ .cp = Key.escape, .shift = true } }); + try std.testing.expectEqual(shell, p.active); + try std.testing.expectEqualStrings("\x1b", drainWrites(p, &buf)); +} + +test "with ProgramEsc raw tty Escape only leaves at a shell prompt" { if (platform == .web) return; const p = try Pardes.init(std.testing.allocator, .{ .cols = 80, .rows = 24, .file = "mise.toml" }); defer p.deinit(); + p.settings.program_esc = true; _ = exec.spawnTty(p, 0); const shell = p.active; const pane = p.panes[shell].?; -- cgit v1.3