summaryrefslogtreecommitdiff
path: root/src/keys.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-17 00:25:48 -0300
committerGabriel Schneider <[email protected]>2026-11-17 12:15:01 -0300
commit9c2fc736b9ccd7b7eea11e7f544ed540d2d2a6c2 (patch)
treeac4270b4e90867228534c01647e8a0e755b58d4a /src/keys.zig
parent5354090b6122faf0d18e246a55e8a4ead0955394 (diff)
downloadpardes-9c2fc736b9ccd7b7eea11e7f544ed540d2d2a6c2.tar.gz
pardes-9c2fc736b9ccd7b7eea11e7f544ed540d2d2a6c2.zip
Term: input through the interface: Term.key writes a key's bytes as its program's keyboard protocol and modes ask, Term.mouse and Term.wheel a mouse event's and a wheel turn's, Term.paste a paste as xterm makes one, Term.modes is the plain record of the modes the core decides by (mouse, all motion, the wheel, focus, bracketed paste, the alternate screen) and Term.promptMoves how far a click on the prompt line moves the shell's cursor; the mouse's records (MouseMods, MouseButton, MouseAction) are the interface's; keys.forwardKey (the bytes queued, an error reported) and keys.enterTty (raw mode, the cursor handed to the shell) are the core's, in keys.zig, pardes.keys for the tests; bracketedPaste, reportsMouse, reportsFocus, ownsWheel, tracksAllMotion and onAlternateScreen go into the record; the tests that called them follow; nothing else changes
Diffstat (limited to 'src/keys.zig')
-rw-r--r--src/keys.zig51
1 files changed, 46 insertions, 5 deletions
diff --git a/src/keys.zig b/src/keys.zig
index 7703d573..c4651be7 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
@@ -179,7 +220,7 @@ pub fn handleKey(p: *Pardes, key: Key) void {
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
@@ -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