diff options
Diffstat (limited to 'src/keys.zig')
| -rw-r--r-- | src/keys.zig | 64 |
1 files changed, 54 insertions, 10 deletions
diff --git a/src/keys.zig b/src/keys.zig index 49ed00c0..8b67a9d1 100644 --- a/src/keys.zig +++ b/src/keys.zig @@ -46,11 +46,52 @@ pub fn cycleMode(p: *Pardes, id: usize) void { .normal => edit.enterInsert(p, &pane.body, .at, 1) catch |err| p.reportError(id, "edit", err), .insert => { edit.exitInsert(p, &pane.body); - if (pane.isTerminal()) panes.terminal.enterTty(p, id); + if (pane.isTerminal()) enterTty(p, id); }, } } +/// Raw tty mode, the keys the program's: the shell's cursor first moved to +/// where the edit cursor was on its prompt line (cursor keys, as a click +/// there would), unless the pointer holds a selection to keep. +pub fn enterTty(p: *Pardes, id: usize) void { + const pane = p.panes[id] orelse return; + const selected = pane.rawPointerText(pane.sel[@backingInt(config.select_button)]) != null; + if (!selected and pane.body.cur_pinned) if (pane.terminal) |state| { + const row = pane.gridRow(pane.body.cur_row) - @as(i32, @intCast(panes.Term.grid(state).offset)); + if (panes.Term.promptMoves(state, row, pane.body.cur_col)) |moves| { + for (0..moves.left) |_| p.emitWrite(id, "\x1b[D"); + for (0..moves.right) |_| p.emitWrite(id, "\x1b[C"); + } + }; + pane.body.mode = .tty; + pane.followOutput(); // entering raw mode is a request for the live shell + pane.body.vsel.active = false; + pane.body.nsel = 0; + // The raw renderer follows the live cursor. A captured mouse selection + // keeps its modal endpoint for the next return to editor mode. + pane.body.cur_pinned = selected; + pane.body.select = false; + pane.body.restore_cursor = false; + pane.body.sticky_col = -1; + pane.body.normal.clear(); +} + +/// One key for the program that owns a raw terminal, its pty write queued. +pub fn forwardKey(p: *Pardes, id: usize, key: Key) void { + const pane = p.panes[id] orelse return; + const state = pane.terminal orelse return; + // A key's text has no bound of its own (an input method's commit, a + // paste as one key): past the stack's room the encoding grows on the + // heap, and emitWrite cuts it into writes. + var room: [256]u8 = undefined; + var first: std.heap.BufferFirstAllocator = .init(&room, p.gpa); + var out: std.Io.Writer.Allocating = .init(first.allocator()); + defer out.deinit(); + panes.Term.key(state, key, &out.writer) catch |err| return p.reportError(id, "key", err); + if (out.written().len > 0) p.emitWrite(id, out.written()); +} + /// One mode transition for the terminal tag command and the Ctrl-B chord. pub fn toggleTty(p: *Pardes, id: usize) void { const pane = p.panes[id] orelse return; @@ -62,7 +103,7 @@ pub fn toggleTty(p: *Pardes, id: usize) void { if (pane.body.mode == .tty) { pane.body.mode = .normal; pane.body.normal.clear(); - } else panes.terminal.enterTty(p, id); + } else enterTty(p, id); } /// Type `keys` again (a macro, `.`): through the modal handling as @@ -176,10 +217,10 @@ pub fn handleKey(p: *Pardes, key: Key) void { tagline.focusedHeader(p) == null; if (raw_esc and key.shift) { p.view().prefix = .none; - panes.terminal.followOutput(pane); + pane.followOutput(); var plain = key; plain.shift = false; - return panes.terminal.forwardKey(p, p.view().active, plain); + return forwardKey(p, p.view().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 @@ -210,7 +251,7 @@ pub fn handleKey(p: *Pardes, key: Key) void { p.view().prefix = .none; if (key.ctrl and !key.alt and key.cp == p.tty_toggle) return toggleTty(p, p.view().active); - panes.terminal.followOutput(pane); // typing snaps back to live output + pane.followOutput(); // typing snaps back to live output // Paste stays the window's, the one exception to forwarding a raw // tty's keys: Ctrl-V types the register at the program and // Ctrl-Shift-V asks the desktop for its clipboard first. Forwarded, @@ -225,7 +266,7 @@ pub fn handleKey(p: *Pardes, key: Key) void { // Ctrl-V a black hole in every full-screen application. if (p.registers.text(p.gpa, Registers.default)) |text| return edit.typeToTty(p, p.view().active, pane, text); } - return panes.terminal.forwardKey(p, p.view().active, key); + return forwardKey(p, p.view().active, key); } // A kind's pane that takes keys takes them all, as a raw terminal // does; Esc too, first, when its kind is of 2.1 (a PDF's drops its @@ -406,7 +447,7 @@ pub fn handleKey(p: *Pardes, key: Key) void { edit.exitInsert(p, t) else edit.handleInsert(p, t, key), - .tty => panes.terminal.forwardKey(p, p.view().active, key), + .tty => forwardKey(p, p.view().active, key), } // `.`'s log: the keys of the normal command that entered insert // mode, then everything typed until it is left @@ -482,6 +523,7 @@ fn leaderKey(p: *Pardes, key: Key) void { } test "Esc back into a tty leaves its view at the prompt" { + if (pardes.Term.ops == null) return error.SkipZigTest; // a shell, opened by a key const gpa = std.testing.allocator; const p = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }); defer p.deinit(); @@ -508,7 +550,7 @@ test "Esc back into a tty leaves its view at the prompt" { p.update(.{ .output = .{ .pane = @intCast(shell), .bytes = std.fmt.bufPrint(&buf, "line {d}\r\n", .{i}) catch unreachable } }); } p.sync(); - const live = sp.terminal.?.vt.screens.active.pages.scrollbar().offset; + const live = pardes.Term.grid(sp.terminal).offset; try std.testing.expect(live > 0); p.update(.{ .key = .{ .cp = Key.escape } }); // shell-prompt heuristic: out to the doc @@ -517,7 +559,7 @@ test "Esc back into a tty leaves its view at the prompt" { p.sync(); try std.testing.expectEqual(shell, p.view().active); - try std.testing.expectEqual(live, sp.terminal.?.vt.screens.active.pages.scrollbar().offset); + try std.testing.expectEqual(live, pardes.Term.grid(sp.terminal).offset); } test "Del from a key asks which neighbour takes the rows; nothing else asks" { @@ -637,6 +679,7 @@ test "Del from a key asks which neighbour takes the rows; nothing else asks" { } test "raw tty Esc backs out and Shift-Esc is the program's Esc" { + if (pardes.Term.ops == null) return error.SkipZigTest; // a shell, opened by a key const p = try Pardes.init(std.testing.allocator, .{ .cols = 80, .rows = 24, .file = "mise.toml" }); defer p.deinit(); _ = exec.spawnTty(p, 0); @@ -723,6 +766,7 @@ test "raw tty Ctrl-B switches to editor mode without sending child input" { } test "raw tty forwards Ctrl letters and former global shortcuts to the child" { + if (pardes.Term.ops == null) return error.SkipZigTest; // a shell const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true }); defer p.deinit(); var buf: [256]u8 = undefined; @@ -762,7 +806,7 @@ test "raw tty forwards Ctrl letters and former global shortcuts to the child" { } p.update(.{ .key = .{ .cp = Key.up } }); try std.testing.expectEqualStrings("\x1b[A", test_api.ptyBytes(p, null, &buf)); - p.panes[0].?.terminal.?.vt.modes.set(.cursor_keys, true); + _ = pardes.Term.feed(p.panes[0].?.terminal.?, "\x1b[?1h", &.{}, &.{}, &.{}); // DECCKM p.update(.{ .key = .{ .cp = Key.up } }); try std.testing.expectEqualStrings("\x1bOA", test_api.ptyBytes(p, null, &buf)); p.update(.{ .key = .{ .cp = Key.left, .ctrl = true } }); |
