summaryrefslogtreecommitdiff
path: root/core/exec.zig
diff options
context:
space:
mode:
Diffstat (limited to 'core/exec.zig')
-rw-r--r--core/exec.zig1152
1 files changed, 1152 insertions, 0 deletions
diff --git a/core/exec.zig b/core/exec.zig
new file mode 100644
index 00000000..601d43dc
--- /dev/null
+++ b/core/exec.zig
@@ -0,0 +1,1152 @@
+//! Executing a word (acme's exec.c): a builtin line or a command typed into
+//! a terminal, the builtin dispatch and settings, the operand a click or
+//! chord hands over, the terminals commands run in, placing a document, and
+//! saving a pane.
+const pardes = @import("root.zig");
+const tagline = @import("tagline.zig");
+const std = @import("std");
+const layout = @import("layout.zig");
+const modal = @import("modal.zig");
+const look = @import("look.zig");
+const panes = @import("panes.zig");
+const normal = @import("normal.zig");
+const edit = @import("edit.zig");
+const body_layer = @import("body_layer.zig");
+const colors = @import("colors.zig");
+const builtins = @import("builtins.zig");
+const config = @import("config.zig");
+const grammars = @import("grammar_manifest.zig").all;
+const python_lang = blk: {
+ @setEvalBranchQuota(20_000);
+ break :blk @import("grammar_manifest.zig").byName("python").?;
+};
+const font_picker = pardes.font_picker;
+const fonts = pardes.fonts;
+const Pane = panes.Pane;
+const MAX_COLS = pardes.MAX_COLS;
+const Builtin = pardes.Builtin;
+const Key = pardes.Key;
+const sel_slot = pardes.sel_slot;
+const Pardes = pardes.Pardes;
+
+pub fn chordEachSel(p: *Pardes, pane: *Pane, cmd: Builtin) bool {
+ const v = p.view();
+ if (pane.body.nsel == 0) return false;
+ const text = edit.flatSurface(p.editor(), &pane.body) catch return false;
+ var ranges: [panes.Text.max_selections]modal.Selection = undefined;
+ const got = pane.body.ranges(text, 0, &ranges);
+ if (got.n < 2) return false;
+
+ var texts: [panes.Text.max_selections][]u8 = undefined;
+ var made: usize = 0;
+ defer for (texts[0..made]) |t| p.gpa.free(t);
+ for (ranges[0..got.n]) |range| {
+ const lo = @min(range.anchor, range.head);
+ const hi = @max(range.anchor, range.head);
+ if (hi > text.len) break;
+ texts[made] = p.gpa.dupe(u8, text[lo..hi]) catch break;
+ made += 1;
+ }
+ // All or nothing, like the pipe: half a chord is not a chord.
+ if (made != got.n) return false;
+
+ const id = v.active;
+ const serial = pane.serial;
+ pane.body.vsel.active = false;
+ pane.body.select = false;
+ pane.body.nsel = 0; // the chord consumed them, exactly as it consumes one
+
+ var primary_active: ?usize = null;
+ for (texts[0..made], 0..) |txt, i| {
+ // Every selection is the body's, each one sent as the first is.
+ runBuiltinFrom(p, cmd, id, "", txt, true);
+ if (i == got.pri) primary_active = v.active;
+ const still = p.panes[id] orelse break;
+ if (still.serial != serial) break;
+ }
+ if (primary_active) |a| if (p.panes[a] != null) {
+ v.active = a;
+ };
+ return true;
+}
+
+pub const PointerOperand = struct {
+ /// Absolute body position corresponding to the pointed screen cell.
+ row: i32,
+ col: i32,
+ /// Exactly what a no-drag middle/right click will dispatch.
+ text: ?[]const u8 = null,
+ /// What hover paints. Null names the pane's live modal selection.
+ preview: ?Pane.Sel = null,
+ /// File words stay logical so a soft-wrapped operand is not cut into
+ /// unrelated rendered fragments. Null for tags and non-file panes.
+ file_word: ?look.FileWordSpan = null,
+ /// A newly expanded word is installed in the gesture's button slot;
+ /// an existing selection is only borrowed and must not replace it.
+ expanded: ?Pane.Sel = null,
+};
+
+/// The body's explicit selection, else what a look at its cursor takes.
+pub fn currentOperand(p: *Pardes, pane: *Pane) ?[]const u8 {
+ if (pane.file == null and !pane.isTerminal()) return null;
+ if (pane.body.vsel.active and pane.body.vsel.explicit) return edit.currentSelText(p, pane);
+ return operandAt(p, pane, look.cursorWordSel(p, pane), false, true).text;
+}
+
+fn spanHas(r: i32, c: i32, ar: i32, ac: i32, br: i32, bc: i32) bool {
+ const fwd = ar < br or (ar == br and ac <= bc);
+ const sr, const sc = if (fwd) .{ ar, ac } else .{ br, bc };
+ const er, const ec = if (fwd) .{ br, bc } else .{ ar, ac };
+ return (r > sr or (r == sr and c >= sc)) and (r < er or (r == er and c <= ec));
+}
+
+/// What a look or an exec at `clicked` takes: the ONE expansion every
+/// entry point uses (a B2/B3 click, Enter and Tab in normal mode, a bare
+/// Look), so a diff line, a hunk line's code word and a file word expand
+/// alike however they are reached (look.zig's "every entry point" test).
+/// `borrow`: a gesture inside a kept or modal selection takes it; the
+/// keyboard has said which selection it means already. `for_look`: only a
+/// look takes a diff line's address; an exec there takes the word, as
+/// anywhere else.
+pub fn operandAt(p: *Pardes, pane: *Pane, clicked: Pane.Sel, borrow: bool, for_look: bool) PointerOperand {
+ const visible = clicked.r0 - @as(i32, pane.tag_rows);
+ const wrapped = pane.wrapAt(visible);
+ const raw = pane.isTerminal() and pane.body.mode == .tty and clicked.r0 >= pane.tag_rows;
+ const row = if (raw) @as(i32, @intCast(panes.Term.grid(pane.terminal).offset)) + visible else wrapped.line;
+ const raw_line = if (raw) modal.lineSlice(body_layer.bodyText(p.scratch.allocator(), pane) catch "", @intCast(@max(0, visible))) else "";
+ const col = if (raw)
+ @as(i32, @intCast(panes.File.rawAtDisplay(raw_line, @intCast(@max(0, clicked.c0)))))
+ else if (clicked.r0 >= pane.tag_rows)
+ edit.paneByteAtDisplay(
+ p.editor(),
+ &pane.body,
+ wrapped.line,
+ wrapped.at,
+ clicked.c0 - wrapped.pad - (if (pane.file != null) @as(i32, panes.File.gutterWidth(pane)) else 0),
+ )
+ else
+ clicked.c0;
+ var result: PointerOperand = .{ .row = row, .col = col };
+ if (borrow) {
+ const kept = pane.sel[sel_slot];
+ const inside_kept = if (pane.pointerSelection(kept)) |rows| inside: {
+ if (clicked.r0 < pane.tag_rows) break :inside false;
+ if (pane.file != null and clicked.c0 < panes.File.gutterWidth(pane)) break :inside false;
+ const source_col: usize = @intCast(@max(0, col));
+ for (rows) |span| {
+ const target = if (span.raw_terminal)
+ (if (raw) span.row else pane.surfRow(span.row))
+ else
+ (if (raw) pane.gridRow(span.row) else span.row);
+ var lo = span.lo;
+ var hi = span.hi;
+ if (span.raw_terminal and !raw) {
+ lo -|= span.prompt_bytes;
+ hi -|= span.prompt_bytes;
+ } else if (!span.raw_terminal and raw) {
+ const prefix = panes.Term.promptPrefix(pane.terminal, row, raw_line);
+ lo += prefix;
+ hi += prefix;
+ }
+ if (target == row and source_col >= lo and source_col < hi)
+ break :inside true;
+ }
+ break :inside false;
+ } else spanHas(clicked.r0, clicked.c0, kept.r0, kept.c0, kept.r1, kept.c1);
+ if (kept.state == .done and inside_kept) {
+ result.text = edit.selectionText(p, pane, kept) catch null;
+ result.preview = kept;
+ return result;
+ }
+ if (pane.body.vsel.active and pane.body.vsel.explicit and
+ spanHas(row, col, pane.body.vsel.row, pane.body.vsel.col, pane.body.cur_row, pane.body.cur_col))
+ {
+ result.text = edit.currentSelText(p, pane);
+ return result;
+ }
+ }
+ // A row that names an address (look.rowAddressAt), pointed at on it: a
+ // query pane's row on its address (its gutter), a diff's file or hunk
+ // header anywhere, a hunk line on its `+`/`-`/` ` prefix. It expands
+ // to the address's text (the hover shows it) and the look is of the
+ // address, a regular look from there. A row's code is words, looked at
+ // as ever: the code's, the prefix never one of them. A click left of a
+ // query pane's text is no part of its row.
+ var word_from: usize = 0;
+ if (for_look and clicked.r0 >= pane.tag_rows and clicked.r0 == clicked.r1 and clicked.c0 == clicked.c1 and row >= 0 and (col >= 0 or pane.query == null)) row_address: {
+ var address_buf: [4096 + 32]u8 = undefined;
+ const got = look.rowAddressAt(p, pane, @intCast(row), @intCast(@max(0, col)), &address_buf) orelse break :row_address;
+ const address = got.address orelse {
+ word_from = got.word_from;
+ break :row_address;
+ };
+ result.text = p.scratch.allocator().dupe(u8, address) catch break :row_address;
+ const hi = std.math.cast(i32, got.end) orelse return result;
+ if (hi == 0) return result;
+ var expanded = clicked;
+ if (pane.file != null) {
+ result.file_word = .{ .row = row, .lo = 0, .hi = hi };
+ expanded = fileWordCells(pane, clicked, row, 0, hi) orelse return result;
+ } else {
+ const shown = if (raw) raw_line else got.line;
+ expanded.c0 = 0;
+ expanded.c1 = @as(i32, @intCast(panes.File.rawDisplayCol(shown, shown.len))) - 1;
+ }
+ result.preview = expanded;
+ result.expanded = expanded;
+ return result;
+ }
+ if (pane.file != null and clicked.r0 >= pane.tag_rows and clicked.r0 == clicked.r1 and clicked.c0 == clicked.c1) {
+ const line = panes.File.sourceLine(pane, row);
+ const source_col: usize = @intCast(@max(0, col));
+ if (source_col < word_from or word_from > line.len) return result;
+ const word = look.expandedWord(line[word_from..], source_col - word_from) orelse return result;
+ var b: look.ExpandedWord = .{ .lo = word.lo + word_from, .hi = word.hi + word_from };
+ // A +History line's `Restore` is that line's: a click on it (an
+ // exec, or a look) takes the label beside it, as the line reads.
+ if (look.isHistoryList(pane) and std.mem.eql(u8, line[b.lo..b.hi], "Restore")) {
+ const rest = std.mem.trimStart(u8, line[b.hi..], " ");
+ if (std.mem.startsWith(u8, rest, "pardes-")) b.hi = (line.len - rest.len) + (std.mem.indexOfAny(u8, rest, " \t") orelse rest.len);
+ }
+ const lo = std.math.cast(i32, b.lo) orelse return result;
+ const hi = std.math.cast(i32, b.hi) orelse return result;
+ result.text = line[b.lo..b.hi];
+ result.file_word = .{ .row = row, .lo = lo, .hi = hi };
+ const expanded = fileWordCells(pane, clicked, row, lo, hi) orelse clicked;
+ result.preview = expanded;
+ result.expanded = expanded;
+ return result;
+ }
+
+ const expanded = look.expandedSel(p, pane, clicked) orelse return result;
+ result.text = edit.selectionText(p, pane, expanded) catch null;
+ if (result.text == null or result.text.?.len == 0) return result;
+ result.preview = expanded;
+ result.expanded = expanded;
+ return result;
+}
+
+/// The cells bytes `lo..hi` of a file pane's line `row` take on screen,
+/// soft-wrapped or not, as `clicked` reshaped; null when scrolled off.
+fn fileWordCells(pane: *Pane, clicked: Pane.Sel, row: i32, lo: i32, hi: i32) ?Pane.Sel {
+ const first = pane.wrapRow(row, lo);
+ const last = pane.wrapRow(row, hi - 1);
+ if (first.row < 0 or last.row < 0) return null;
+ var cells = clicked;
+ cells.r0 = first.row + @as(i32, pane.tag_rows);
+ cells.c0 = @as(i32, panes.File.gutterWidth(pane)) + first.pad + panes.File.displayOffset(pane, row, first.at, lo);
+ cells.r1 = last.row + @as(i32, pane.tag_rows);
+ cells.c1 = @as(i32, panes.File.gutterWidth(pane)) + last.pad + panes.File.displayEndOffset(pane, row, last.at, hi - 1);
+ return cells;
+}
+
+/// Where a held selection is looked for, step `k` of `p.panes.len + 2`:
+/// `first`, then the active pane, then the rest; null for one seen.
+fn heldPane(p: *Pardes, first: usize, k: usize) ?*Pane {
+ const i = switch (k) {
+ 0 => first,
+ 1 => p.view().active,
+ else => k - 2,
+ };
+ if (k > 0 and (i == first or (k > 1 and i == p.view().active))) return null;
+ return p.panes[i];
+}
+
+pub fn heldSelection(p: *Pardes, first: usize) ?[]const u8 {
+ for (0..p.panes.len + 2) |k| {
+ const t = heldPane(p, first, k) orelse continue;
+ if (t.addon) |kind| if (kind.selection.len > 0) return kind.selection;
+ if (t.sel[sel_slot].state == .done) {
+ if (edit.selectionText(p, t, t.sel[sel_slot]) catch null) |tx| return tx;
+ } else if (t.body.vsel.active and t.body.vsel.explicit) {
+ if (edit.currentSelText(p, t)) |tx| return tx;
+ }
+ }
+ return null;
+}
+
+/// Where the selection `heldSelection` would give is, as acme's argument
+/// location (exec.c:162, getbytearg): `path:#q0,#q1` of a file pane's dot.
+pub fn heldSelectionPlace(p: *Pardes, first: usize, buf: []u8) ?[]const u8 {
+ for (0..p.panes.len + 2) |k| {
+ const t = heldPane(p, first, k) orelse continue;
+ if (t.sel[sel_slot].state == .done) return null; // a pointer selection: no byte range kept
+ if (t.body.vsel.active and t.body.vsel.explicit) {
+ const f = t.file orelse return null;
+ const d = @import("ninep/pane.zig").dotOf(t);
+ return std.fmt.bufPrint(buf, "{s}:#{d},#{d}", .{ f.path, d.q0, d.q1 }) catch null;
+ }
+ }
+ return null;
+}
+
+/// A clicked word and the selection held for it, as one command line.
+pub fn withArg(p: *Pardes, txt: []const u8, arg: ?[]const u8) []const u8 {
+ const a = std.mem.trim(u8, arg orelse return txt, " \t\r\n");
+ if (a.len == 0) return txt;
+ const t = std.mem.trim(u8, txt, " \t\r\n");
+ // A switch (Verbose, Wrap) clicked with some other text
+ // selected flips, as it does bare: only `on` or `off` is an argument
+ // for it. What a ctl write gives it is checked strictly instead.
+ if (config.Runtime.find(commandText(t))) |setting| switch (setting.action) {
+ .toggle => if (!std.mem.eql(u8, a, "on") and !std.mem.eql(u8, a, "off")) return txt,
+ else => {},
+ };
+ return std.fmt.allocPrint(p.scratch.allocator(), "{s} {s}", .{ t, a }) catch txt;
+}
+
+/// Enter on a save input: the path is everything past the marker.
+pub fn submitSave(p: *Pardes, id: usize) void {
+ const pane = p.panes[id] orelse return;
+ if (pane.prompt != .save) return;
+ const armed = pane.promptText() orelse return;
+ if (!std.mem.startsWith(u8, armed, config.save_marker)) return;
+ const path = std.mem.trim(u8, armed[config.save_marker.len..], " \t\r\n");
+ if (path.len == 0) return;
+ saveTo(p, id, path);
+}
+
+/// Write the file this pane already has: acme's Put, and the one Save that
+/// needs no argument because the path was never a question.
+pub fn saveFile(p: *Pardes, id: usize) void {
+ const pane = p.panes[id] orelse return;
+ const f = if (pane.file) |*file| file else return;
+ if (f.output != null) return; // nothing behind it yet: saveTo, with a path
+ // Its file changed on disk under these edits: once, as acme's Put says
+ // "modified since last read" (exec.c:577), and the next Save writes.
+ if (f.disk_newer != null) {
+ if (!f.disk_newer_warned) {
+ f.disk_newer_warned = true;
+ var said: pardes.Kind.Buf(Pardes.SavePath.cap + 64) = .{};
+ return p.reportFailure(id, said.print("{s} modified on disk since read (Save again to overwrite)", .{f.path[0..@min(f.path.len, Pardes.SavePath.cap)]}));
+ }
+ f.disk_newer = null;
+ }
+ p.emit(.{ .save_file = .{ .pane = @intCast(id) } });
+}
+
+pub fn saveTo(p: *Pardes, id: usize, path: []const u8) void {
+ const pane = p.panes[id] orelse return;
+ if (path.len == 0 or path[path.len - 1] == '/' or
+ std.mem.indexOfScalar(u8, path, '\n') != null)
+ return p.reportError(id, "save", error.NoFilename);
+ var home_buf: [4096]u8 = undefined;
+ const full = std.Io.Dir.path.resolvePosix(
+ p.scratch.allocator(),
+ &.{ Pardes.paneDir(pane), @import("fs.zig").expandHome(path, &home_buf) },
+ ) catch |err| return p.reportError(id, "save", err);
+ if (!std.Io.Dir.path.isAbsolute(full)) return p.reportError(id, "save", error.NoDirectory);
+ if (full.len > Pardes.SavePath.cap) return p.reportError(id, "save", error.PathTooLong);
+ const f = if (pane.file) |*file| file else {
+ // a terminal writes its scrollback; an image and a PDF have nothing
+ // of their own that is unwritten, so they have nothing to write
+ if (pane.isTerminal()) askWrite(p, id, pane.serial, full);
+ return;
+ };
+ // A scratch takes the name once the write is done (promoteSaved): a
+ // Save that fails changes nothing, the name included.
+ if (f.output != null and panes.Output.fileTraits(f.output).saves) {
+ p.emit(.{ .save_text = .{
+ .pane = @intCast(id),
+ .serial = pane.serial,
+ .path = Pardes.SavePath.from(full),
+ .promote = true,
+ } });
+ return;
+ }
+ // its own path, spelled out: the in-place write, so the pane comes clean
+ if (f.output == null and std.mem.eql(u8, f.path, full)) return saveFile(p, id);
+ askWrite(p, id, pane.serial, full);
+}
+
+fn askWrite(p: *Pardes, id: usize, serial: u32, path: []const u8) void {
+ p.emit(.{ .save_text = .{
+ .pane = @intCast(id),
+ .serial = serial,
+ .path = Pardes.SavePath.from(path),
+ } });
+}
+
+/// Term: a shell in the caller's directory, raw from the first frame,
+/// stacked below the caller like Alt-n's new shell.
+pub fn spawnTty(p: *Pardes, from: usize) ?*Pane {
+ // No pane asking (an empty window's tag): the session's directory, as
+ // New's scratch is.
+ // A directory that is not there makes no terminal: its shell could not
+ // start in it, and a dead pane would answer no run. Asked only of a host
+ // that starts shells (none, as in the core's tests, starts none).
+ const dir = p.execDir(from);
+ if (missingDir(p, dir)) {
+ var said: pardes.Kind.Buf(pardes.memory.limits.host_path_cap + 48) = .{};
+ p.reportFailure(from, said.print("Term: {s}: no such directory", .{dir[0..@min(dir.len, pardes.memory.limits.host_path_cap)]}));
+ return null;
+ }
+ const free = p.freeSlot() orelse {
+ p.reportError(from, "Term", error.NoPaneSlots);
+ return null;
+ };
+ const nt = p.newShell(free, dir) catch |err| {
+ if (err == error.PathTooLong) {
+ p.reportFailure(from, std.fmt.comptimePrint("Term: its directory is longer than a path may be ({d} bytes)", .{pardes.memory.limits.host_path_cap}));
+ return null;
+ }
+ p.reportError(from, "Term", err);
+ return null;
+ };
+ nt.greet = true;
+ nt.body.mode = .tty;
+ layout.placeNew(p, from, from, free, .shell);
+ if (refusedPlacement(p, free)) {
+ p.reportError(from, "Term", error.NoPaneRoom);
+ return null;
+ }
+ return nt;
+}
+
+/// A pane layout.placeNew found no room for: taken back now, before anything is
+/// logged of it or answered with its serial (it was never announced), so
+/// no `new`/`del` pair and no serial a script was told of is left behind.
+fn refusedPlacement(p: *Pardes, id: usize) bool {
+ if (!p.unplaced.isSet(id)) return false;
+ p.unplaced.unset(id);
+ // It held no weight (layout.placeNew put every pane's rows back as they
+ // were): nothing else moves, a pane dragged short included.
+ p.removePane(id, null) catch |err| p.reportError(id, "open", err);
+ return true;
+}
+
+/// Whether a shell would be started in `dir` and `dir` is not there: asked
+/// only of a host that starts shells, and only of a local path.
+pub fn missingDir(p: *Pardes, dir: []const u8) bool {
+ if (p.host.vtable.spawn == null) return false;
+ // A peer's directory (`/n/peer/x`) is no directory on this host, where
+ // the shell would start: no command or terminal runs there, and none
+ // is made to say `exit 127`.
+ const local = @import("fs.zig").localPath(dir) orelse return std.mem.startsWith(u8, dir, "/n/");
+ return kindOf(local) != .directory;
+}
+
+/// What `path` is on this host, null when it is not there; the turn out
+/// while it asks (it may be inside a mount this editor serves).
+pub fn kindOf(path: []const u8) ?std.Io.File.Kind {
+ var buf: [4096]u8 = undefined;
+ const z = std.fmt.bufPrintSentinel(&buf, "{s}", .{path}, 0) catch return null;
+ return pardes.tape.site(.kind, null, kindHost, .{z});
+}
+
+fn kindHost(z: [:0]const u8) ?std.Io.File.Kind {
+ const stat = @import("fs.zig").statPath(std.Io.Threaded.global_single_threaded.io(), z, .{}) catch return null;
+ return stat.kind;
+}
+
+// ---- REPLs ----
+//
+// A terminal is bound as a language's REPL by `Repl python` (builtins.zig).
+// An exec made by a gesture on the body of a file in that language -- or on
+// the REPL's own body -- is sent to it as input rather than run: all of it,
+// a word like `ls` too. The tag's words, `Exec` by name and a command word
+// `@`...`` still run as commands.
+
+/// A bound REPL whose program is there to take input: a command pane's
+/// while its command runs; an interactive terminal's while a program, not
+/// the shell, has its tty (the host's `tty_taken`) -- a python3 left with
+/// Ctrl-D leaves the shell, which would run `import os` as ImageMagick's.
+pub fn replLive(p: *const Pardes, id: usize) bool {
+ const pane = p.panes[id] orelse return false;
+ if (pane.repl == null) return false;
+ if (pane.command != null) return !pane.command_done;
+ return p.hostTtyTaken(id);
+}
+
+/// `python-a`.
+pub fn replId(buf: *[48]u8, pane: *const Pane) []const u8 {
+ const r = pane.repl orelse return "";
+ return std.fmt.bufPrint(buf, "{s}-{c}", .{ grammars[r.lang].name, r.letter }) catch buf; // cut at 48
+}
+
+const Routed = union(enum) { none, sent: usize, asked };
+
+/// The REPL bound here has no program running to take the text: said,
+/// and nothing sent or run.
+pub fn replGone(p: *Pardes, from: usize, repl: *const Pane) Routed {
+ var idbuf: [48]u8 = undefined;
+ var said: pardes.Kind.Buf(128) = .{};
+ p.setMessage(from, said.print("Repl: {s} has no program running; nothing sent", .{replId(&idbuf, repl)}));
+ return .asked;
+}
+
+/// Where an exec from `from`'s body goes when a REPL is bound for it: the
+/// one there is, or a question when there are several.
+fn routeToRepl(p: *Pardes, from: usize, text: []const u8) Routed {
+ const src = p.panes[from] orelse return .none;
+ if (src.repl != null) {
+ if (!replLive(p, from)) return replGone(p, from, src);
+ sendToRepl(p, from, from, text);
+ return .{ .sent = from };
+ }
+ // The file's language, by its grammar_manifest index.
+ const lang: u8 = @intCast(@import("grammar_manifest.zig").byPath((src.file orelse return .none).path) orelse return .none);
+ // Every REPL bound for the language whose program still runs: one
+ // whose program exited is skipped, in the question and in the choice.
+ var bound: [26]usize = undefined;
+ var n: usize = 0;
+ var exited: ?usize = null;
+ for (p.panes, 0..) |slot, i| if (slot) |pane| if (pane.repl) |r| if (r.lang == lang and n < bound.len) {
+ if (!replLive(p, i)) {
+ exited = i;
+ continue;
+ }
+ bound[n] = i;
+ n += 1;
+ };
+ // Bound but none running: said, never run as a command instead.
+ if (n == 0) return if (exited) |i| replGone(p, from, p.panes[i].?) else .none;
+ if (n == 1) {
+ if (!replLive(p, bound[0])) return replGone(p, from, p.panes[bound[0]].?);
+ sendToRepl(p, from, bound[0], text);
+ return .{ .sent = bound[0] };
+ }
+ // Several: always asked on the pane's notice band, as Del asks, one key
+ // answering; nothing is remembered of the answer.
+ const owned = p.gpa.dupe(u8, text) catch return .none;
+ if (p.repl_ask) |old| p.gpa.free(old.text);
+ p.repl_ask = .{ .from = src.serial, .lang = lang, .text = owned };
+ for (bound[0..n]) |i| p.repl_ask.?.offered[p.panes[i].?.repl.?.letter - 'a'] = p.panes[i].?.serial;
+ var w = std.Io.Writer.fixed(&src.question);
+ w.print("send to {s}:", .{grammars[lang].name}) catch {};
+ for (bound[0..n]) |i| w.print(" {c}", .{p.panes[i].?.repl.?.letter}) catch {};
+ w.writeAll(" (Esc: none)") catch {};
+ src.question_len = @intCast(w.buffered().len);
+ src.prompt = .repl_choice;
+ p.view().active = from;
+ var rec: [96]u8 = undefined;
+ var rw = std.Io.Writer.fixed(&rec);
+ rw.print("{d} repl", .{src.serial}) catch unreachable; // a u32 and bound's 26 letters fit 96
+ for (bound[0..n]) |i| rw.print(" {c}", .{p.panes[i].?.repl.?.letter}) catch unreachable;
+ pardes.ctlfs.events.notePath(p, "ask", rw.buffered());
+ return .asked;
+}
+
+const repl_enter_wait_ns: u64 = 250 * std.time.ns_per_ms;
+/// Python echoes a long paste over several reads: its Enter waits for the
+/// echo to go quiet this long, not for the first of it.
+const repl_quiet_ns: u64 = 100 * std.time.ns_per_ms;
+
+/// The host's clock now; core time (`now_ns`, `stepped_ns`) is as old as
+/// the last frame, seconds in an idle session.
+pub fn clockNow(p: *Pardes) u64 {
+ return if (p.host.vtable.now) |clock| clock(p.host.ctx) else p.clock.now_ns;
+}
+
+/// Sends the Enters a paste into pane `id`'s REPL is owed: when the REPL
+/// has answered it (`answered`), or once the wait is up. Python's answer
+/// only restarts a short wait, so the Enter goes once its echo of the
+/// whole paste is in (a multi-line paste read in part would leave it at
+/// `...`).
+pub fn flushReplEnter(p: *Pardes, id: usize, answered: bool) void {
+ const pane = p.panes[id] orelse return;
+ if (pane.repl_enter == 0) return;
+ if (answered and (if (pane.repl) |r| r.lang == python_lang else false)) {
+ pane.repl_enter_due_ns = clockNow(p) + repl_quiet_ns;
+ return;
+ }
+ if (!answered and p.clock.now_ns < pane.repl_enter_due_ns) return;
+ for (0..pane.repl_enter) |_| p.emitWrite(id, "\r");
+ pane.repl_enter = 0;
+}
+
+fn pythonBlockOpen(text: []const u8) bool {
+ const trimmed = std.mem.trimEnd(u8, text, " \t\n");
+ const start = if (std.mem.lastIndexOfScalar(u8, trimmed, '\n')) |nl| nl + 1 else 0;
+ const last = trimmed[start..];
+ return last.len > 0 and (last[0] == ' ' or last[0] == '\t' or last[last.len - 1] == ':');
+}
+
+/// Types `text` into the REPL in `to`, bracketed if its program asked for
+/// that, then Enter; says so on `from`'s message row and in the log.
+pub fn sendToRepl(p: *Pardes, from: usize, to: usize, text: []const u8) void {
+ const dst = p.panes[to] orelse return;
+ const src = p.panes[from] orelse return;
+ var idbuf: [48]u8 = undefined;
+ const id = replId(&idbuf, dst);
+ // Lines end in \n alone (a \r\n would give Python a blank line that
+ // ends its block), and no ESC or other control byte but a tab goes: an
+ // ESC[201~ in the text would end a bracketed paste early.
+ const clean = p.scratch.allocator().alloc(u8, text.len) catch |err| return p.reportError(from, "Repl", err);
+ var n: usize = 0;
+ for (text, 0..) |c, i| {
+ if (c == '\r') {
+ if (i + 1 < text.len and text[i + 1] == '\n') continue;
+ clean[n] = '\n';
+ } else if (c < ' ' and c != '\t' and c != '\n' or c == 0x7f) continue else clean[n] = c;
+ n += 1;
+ }
+ // Sent whole or not at all: a send cut short would leave the REPL in a
+ // paste, or with half a block.
+ if (n + 16 > p.writeRoom(to)) {
+ var said: pardes.Kind.Buf(128) = .{};
+ return p.setMessage(from, said.print("Repl: {d} bytes is more than can be sent at once; send less", .{n}));
+ }
+ const pasted = panes.Term.modes(dst.terminal).bracketed_paste;
+ edit.typeToTty(p, to, dst, clean[0..n]);
+ // A Python block whose last line is indented, or a bare header, is not
+ // over at one Enter, pasted into 3.13's REPL or typed into the old one:
+ // the blank line a second Enter gives ends it.
+ // So is a paste of more than one line: 3.13's REPL takes it as one
+ // input, which one Enter leaves at `...`.
+ const python = if (dst.repl) |r| r.lang == python_lang else false;
+ const lines = std.mem.count(u8, std.mem.trim(u8, clean[0..n], " \t\n"), "\n") + 1;
+ const enters: u8 = if (python and (pythonBlockOpen(clean[0..n]) or (pasted and lines > 1))) 2 else 1;
+ if (pasted) {
+ // An Enter read with the paste is part of it: 3.13's REPL takes it
+ // as a newline in the pasted text, even a one-line one, and waits.
+ // It goes once the REPL has answered the paste (its next output),
+ // or after a moment if nothing comes.
+ dst.repl_enter = enters;
+ // From the clock now, not core time: idle (a detached session
+ // nobody watches), `stepped_ns` and `now_ns` are as old as the last
+ // frame, and a wait counted from there would be over already, the
+ // Enter going out with the paste.
+ dst.repl_enter_due_ns = clockNow(p) + repl_enter_wait_ns;
+ } else for (0..enters) |_| p.emitWrite(to, "\r");
+ // Line by line, a blank line inside a Python block ends the block.
+ if (!panes.Term.modes(dst.terminal).bracketed_paste and !dst.repl_warned and (if (dst.repl) |r| r.lang == python_lang else false) and
+ std.mem.indexOfScalar(u8, text, '\n') != null)
+ {
+ dst.repl_warned = true;
+ var warn: pardes.Kind.Buf(128) = .{};
+ p.setMessage(from, warn.print("Repl: {s} takes no bracketed paste; a blank line in a block ends it", .{id}));
+ }
+ var said: pardes.Kind.Buf(64) = .{};
+ p.setMessage(from, said.print("\u{2192} {s}", .{id}));
+ src.msg_sent = true;
+ var rec: [96]u8 = undefined;
+ pardes.ctlfs.events.notePath(p, "send", std.fmt.bufPrint(&rec, "{d} {d} {s}", .{ src.serial, dst.serial, id }) catch return);
+}
+
+/// The longest line a command pane runs.
+pub const command_max = 1024;
+
+const max_exec_depth = 8;
+
+/// A word no builtin knows (acme's exec.c run()). Clicked in an interactive
+/// terminal, it is typed into that shell, whose state the click can see;
+/// from anywhere else it runs as its own command pane, a terminal whose
+/// child is `$SHELL -c` the line, which shows its output and `exit N` when
+/// done, and which Kill signals. Answers the pane it went to.
+pub fn execute(p: *Pardes, id: usize, txt: []const u8) ?usize {
+ return executeFrom(p, id, txt, false);
+}
+
+/// A line that starts with `#` is a comment, as in a shell: it runs as
+/// nothing, silently, wherever a line runs (exec, a ctl, the init file),
+/// so DumpConfig's report reads back as configuration whole.
+pub fn isComment(line: []const u8) bool {
+ const t = std.mem.trimStart(u8, line, " \t");
+ return t.len > 0 and t[0] == '#';
+}
+
+pub fn takesCommandLine(p: *const Pardes, id: usize) bool {
+ const pane = p.panes[id] orelse return false;
+ // A command pane's child is its command, never a shell to type at.
+ if (!pane.isTerminal() or pane.command != null) return false;
+ // Someone's line at the shell's prompt, typed and not sent: typing a
+ // command there would run it joined to theirs (`echo TYPEDls`). Only an
+ // idle, EMPTY prompt is typed at; otherwise a new pane runs the line.
+ // (Text in pardes's own edit buffer is not the shell's yet: an exec of
+ // it, or of anything, types only what it runs.)
+ const at = panes.Term.prompt(pane.terminal);
+ if (at.input_ready and !at.input_empty) return false;
+ return !p.hostTtyTaken(id);
+}
+
+/// `execute` for text a gesture took from `id`'s body when `from_body`:
+/// such text may be a bound REPL's. Run by name (`Exec ...`, whose line
+/// comes back through `execute`) or written as a command word, never.
+pub fn executeFrom(p: *Pardes, id: usize, txt: []const u8, from_body: bool) ?usize {
+ if (isComment(commandText(txt))) return null;
+ // An empty window runs its session words, and a command line in the
+ // session's directory (execDir), as its workspace tag would.
+ const pane = p.panes[id] orelse {
+ const cmd = commandText(txt);
+ if (cmd.len == 0 or executeBuiltinLine(p, id, cmd)) return null;
+ return runCommand(p, id, cmd);
+ };
+ const gesture = from_body and config.commandWord(std.mem.trim(u8, txt, " \t\r\n")) == null;
+ const cmd = commandText(txt);
+ if (cmd.len == 0) return null;
+ if (p.exec_depth == 0) p.noteHaptic(.exec);
+ if (executeBuiltinLine(p, id, cmd)) return null;
+ if (p.exec_depth >= max_exec_depth) return null;
+ p.exec_depth += 1;
+ defer p.exec_depth -= 1;
+ if (gesture) switch (routeToRepl(p, id, cmd)) {
+ .none => {},
+ .sent => |to| return to,
+ .asked => return null,
+ };
+ // A builtin or setting this build lacks (Fonts on a terminal) is said,
+ // never run as a shell command.
+ const word = cmd[0 .. std.mem.indexOfAny(u8, cmd, " \t") orelse cmd.len];
+ if (builtins.absentWhy(word) orelse if (config.Runtime.find(word) != null) @as([]const u8, "GUI-only") else null) |why| {
+ var said: pardes.Kind.Buf(96) = .{};
+ p.reportFailure(id, said.print("{s} is {s}, invalid here", .{ word, why }));
+ return null;
+ }
+ // A command from a column's or the workspace's tag is that tag's: a
+ // command pane in the session's directory, never typed into whichever
+ // terminal has the keyboard.
+ if (p.exec_header or !takesCommandLine(p, id)) return runCommand(p, id, cmd);
+ // At a prompt with typed text below it: the output area padded with
+ // newlines so the command's output does not overwrite the buffer.
+ if (pane.ovl) |o| if (pane.isTerminal()) if (pane.terminal) |state| if (panes.Term.prompt(state).at_prompt) {
+ // the buffer's LAST surface row: its lines may outnumber the shell
+ // rows it covers, and it is the bottom one output must clear
+ const max_row = o.row + @as(i32, @intCast(modal.lineCount(o.text))) - 1;
+ const grid = panes.Term.grid(state);
+ const cursor_abs = pane.surfRow(@as(i32, @intCast(grid.offset)) + @as(i32, grid.cursor_y));
+ const pad = std.math.clamp(max_row - cursor_abs, 0, @as(i32, pane.rows));
+ for (0..@intCast(pad)) |_| p.emitWrite(id, "\r");
+ };
+ pane.noteCommand(cmd);
+ if (pane.queuePendingCommand(cmd) catch |err| {
+ p.reportError(id, "queue command", err);
+ return id;
+ }) return id;
+ p.emitWrite(id, cmd);
+ p.emitWrite(id, "\r");
+ return id;
+}
+
+/// Runs `line` in a command pane for `from`'s directory: `from` itself if
+/// it is one whose command is done, else the directory's command pane that
+/// is done, else a new one, placed as layout.placeNew places a command pane.
+/// A pane reused keeps what it showed: the new command runs below a line
+/// naming it, as acme appends to +Errors and never clears it (util.c:213).
+pub fn runCommand(p: *Pardes, from: usize, line: []const u8) ?usize {
+ if (line.len > command_max) {
+ p.reportFailure(from, "exec: a command line is at most 1024 bytes");
+ return null;
+ }
+ // None asking (an empty window): the session's directory, no pane of
+ // its own to reuse.
+ const src = p.panes[from];
+ const dir = p.execDir(from);
+ // A directory that is not there runs nothing, as Term makes nothing
+ // there: no pane, and the write fails ENOENT.
+ if (missingDir(p, dir)) {
+ var said: pardes.Kind.Buf(pardes.memory.limits.host_path_cap + 48) = .{};
+ p.reportFailure(from, said.print("exec: {s}: no such directory", .{dir[0..@min(dir.len, pardes.memory.limits.host_path_cap)]}));
+ return null;
+ }
+ // Not one whose pty a job it left behind still prints to: reusing it
+ // would hang that job up.
+ // From a column's tag, only a finished pane in that column is reused,
+ // as the new one goes in that column (layout.placeNew).
+ // Nor one leased: an open that was answered its serial and is still
+ // open reads that pane's output, which another client's command must
+ // not replace; a new pane runs it instead.
+ const reuse: ?usize = if (!p.exec_header and p.exec_column == null and src != null and src.?.command != null and src.?.command_done and !src.?.command_pty and !leased(p, src.?.serial)) from else for (p.panes, 0..) |slot, i| {
+ const other = slot orelse continue;
+ if (p.exec_column) |c| if (if (layout.findPane(p, i)) |f| f.col != c else true) continue;
+ if (other.command != null and other.command_done and !other.command_pty and std.mem.eql(u8, other.cwdSlice(), dir) and !leased(p, other.serial)) break i;
+ } else null;
+ if (reuse) |id| {
+ const pane = p.panes[id].?;
+ const owned = p.gpa.dupe(u8, line) catch |err| {
+ p.reportError(from, "exec", err);
+ return null;
+ };
+ p.gpa.free(pane.command.?);
+ pane.command = owned;
+ p.diff_output.drop(p.gpa, pane.serial);
+ pane.command_done = false;
+ pane.command_status = null;
+ pane.command_pty = true;
+ pane.body.mode = .tty;
+ // What the last program left the emulator in goes first: the
+ // alternate screen left (only if it is there: leaving restores a
+ // saved cursor), mouse reports and bracketed paste off, the cursor
+ // shown, colours reset. (Not DECSTR: the emulator's stream does not
+ // implement it.)
+ if (panes.Term.modes(pane.terminal).alternate) pane.feedOutput(p, "\x1b[?1049l");
+ pane.feedOutput(p, "\x1b[?1000l\x1b[?1002l\x1b[?1003l\x1b[?1006l\x1b[?2004l\x1b[?25h\x1b[0m");
+ echoCommand(p, pane, line);
+ pane.command_from = winid(p, src);
+ p.emitSpawn(id, pane.serial, pane.cwdSlice());
+ pane.noteRun(p, "run", line);
+ return id;
+ }
+ const free = p.freeSlot() orelse {
+ p.reportError(from, "exec", error.NoPaneSlots);
+ return null;
+ };
+ const pane = panes.newCommand(p, free, dir, line) catch |err| {
+ p.reportError(from, "exec", err);
+ return null;
+ };
+ pane.command_from = winid(p, src);
+ echoCommand(p, pane, line);
+ layout.placeNew(p, from, from, free, .command);
+ if (refusedPlacement(p, free)) {
+ p.reportError(from, "exec", error.NoPaneRoom);
+ return null;
+ }
+ pane.noteRun(p, "run", line);
+ return free;
+}
+
+/// Whether an open of a look or exec file holds `serial` from its own write
+/// (tree.Open.results): its client is reading that pane.
+fn leased(p: *const Pardes, serial: u32) bool {
+ for (p.fs.opens) |o| if (o.node != 0 and o.results.wrote) {
+ if (std.mem.indexOfScalar(u32, o.results.list[0..o.results.len], serial) != null) return true;
+ };
+ return false;
+}
+
+/// The serial a command's `$winid` names: the pane it was run from, none
+/// (0) from a column's or the workspace's tag.
+fn winid(p: *const Pardes, src: ?*Pane) u32 {
+ if (p.exec_header) return 0;
+ return if (src) |s| s.serial else 0;
+}
+
+/// `% line` above a command's output, every command, as a shell echoes
+/// what it was given; its newlines the terminal's, so a multi-line command
+/// does not staircase.
+fn echoCommand(p: *Pardes, pane: *Pane, line: []const u8) void {
+ var buf: [2 * command_max + 8]u8 = undefined;
+ var w = std.Io.Writer.fixed(&buf);
+ // A line past the room is cut at its end.
+ echo: {
+ w.writeAll(if (panes.Term.grid(pane.terminal).cursor_x != 0) "\r\n% " else "% ") catch break :echo;
+ for (line) |c| (if (c == '\n') w.writeAll("\r\n") else w.writeByte(c)) catch break :echo;
+ w.writeAll("\r\n") catch break :echo;
+ }
+ pane.feedOutput(p, w.buffered());
+}
+
+/// Trim and unwrap clickable command-word notation once for every command
+/// consumer, so startup lines and interactive Exec speak the same syntax.
+pub fn commandText(txt: []const u8) []const u8 {
+ var cmd = std.mem.trim(u8, txt, " \t\r\n");
+ while (config.commandWord(cmd)) |inner| cmd = std.mem.trim(u8, inner, " \t\r\n");
+ return cmd;
+}
+
+/// The builtin a command line's first word names, as acme's lookup reads
+/// only the first word (editors/acme/exec.c, lookup): what an event's
+/// flag 1 says.
+pub fn namesBuiltin(txt: []const u8) bool {
+ const cmd = commandText(txt);
+ const end = std.mem.indexOfAny(u8, cmd, " \t") orelse cmd.len;
+ return std.meta.stringToEnum(Builtin, cmd[0..end]) != null;
+}
+
+/// acme's words, run by their pardes names (Put is Save; Delete is a Del
+/// that does not ask), or refused, never run as a shell command: a Put that
+/// exited 127 looked like a save that worked. Tty, Term's old name, as
+/// saved dumps and users' tags still hold it, is here too.
+pub const acme_words = [_]struct { acme: []const u8, pardes: ?[]const u8, hint: []const u8 = "" }{
+ .{ .acme = "Put", .pardes = "Save" },
+ .{ .acme = "Tty", .pardes = "Term" },
+ .{ .acme = "Delete", .pardes = "Del" },
+ .{ .acme = "Snarf", .pardes = null, .hint = ": y yanks the selection" },
+ .{ .acme = "Cut", .pardes = null, .hint = ": d deletes the selection into a register" },
+ .{ .acme = "Paste", .pardes = null, .hint = ": p puts a register" },
+ .{ .acme = "Sort", .pardes = null },
+ .{ .acme = "Load", .pardes = "Restore" },
+ .{ .acme = "ID", .pardes = null },
+ .{ .acme = "Send", .pardes = null },
+ .{ .acme = "Indent", .pardes = null, .hint = ": Enter, o and O keep the indent always" },
+ .{ .acme = "Local", .pardes = null },
+ .{ .acme = "Abort", .pardes = null, .hint = ": a debugging word that crashes acme" },
+};
+
+pub fn executeBuiltinLine(p: *Pardes, id: usize, txt: []const u8) bool {
+ const raw = commandText(txt);
+ if (raw.len == 0 or p.exec_depth >= max_exec_depth) return false;
+ if (isComment(raw)) return true;
+ // `+` too, so an old `Tty+fish` is renamed as `Tty fish` is.
+ const word_end = std.mem.indexOfAny(u8, raw, " \t+") orelse raw.len;
+ var renamed: [1024 + 16]u8 = undefined;
+ const cmd = for (acme_words) |w| {
+ if (!std.mem.eql(u8, raw[0..word_end], w.acme)) continue;
+ const now = w.pardes orelse {
+ var said: pardes.Kind.Buf(128) = .{};
+ p.reportFailure(id, said.print("invalid: acme's {s} is not a pardes builtin{s}", .{ w.acme, w.hint }));
+ return true;
+ };
+ // Delete: Del, the text thrown away unasked.
+ if (std.mem.eql(u8, w.acme, "Delete")) if (p.panes[id]) |pane| if (pane.file) |f| {
+ pane.discard_warned = .{ .revision = f.revision, .by = .Del };
+ };
+ break std.fmt.bufPrint(&renamed, "{s}{s}", .{ now, raw[word_end..] }) catch raw;
+ } else raw;
+ // `Word arg`, or for a word that says so, `Word+arg` (`Term+fish`).
+ const plus = std.mem.indexOfScalar(u8, cmd, '+');
+ var sp = std.mem.indexOfAny(u8, cmd, " \t");
+ if (sp == null) if (plus) |at| if (std.meta.stringToEnum(Builtin, cmd[0..at])) |head| {
+ if (builtins.registry.plusArg(head)) sp = at;
+ };
+ const bi: ?Builtin = std.meta.stringToEnum(Builtin, cmd) orelse blk: {
+ const head = std.meta.stringToEnum(Builtin, cmd[0 .. sp orelse break :blk null]) orelse break :blk null;
+ break :blk if (builtins.registry.takesArg(head)) head else null;
+ };
+ const b = bi orelse return runLine(p, id, cmd);
+ p.exec_depth += 1;
+ defer p.exec_depth -= 1;
+ const typed = if (sp) |s| std.mem.trim(u8, cmd[s + 1 ..], " \t") else "";
+ runBuiltin(p, b, id, cmd, if (typed.len > 0) typed else null);
+ return true;
+}
+
+pub fn applyStartupConfig(p: *Pardes) void {
+ @import("Plumb.zig").boot(p);
+ applyInitText(p, p.opts.startup_config orelse return, false);
+}
+
+/// The init file saved in a pane: its settings applied again. Only its
+/// setting lines: a command in it (`Newcol`) is the session's start, and
+/// would not be right twice.
+pub fn reapplyInitFile(p: *Pardes, path: []const u8, text: []const u8) void {
+ const init_path = p.startupPath() orelse return;
+ if (!std.mem.eql(u8, init_path, path)) return;
+ applyInitText(p, text, true);
+}
+
+fn applyInitText(p: *Pardes, text: []const u8, settings_only: bool) void {
+ p.announce = false;
+ defer p.announce = true;
+ // A UTF-8 BOM an editor left at its start is no part of its first word.
+ const body = if (std.mem.startsWith(u8, text, "\xEF\xBB\xBF")) text[3..] else text;
+ var lines = std.mem.splitScalar(u8, body, '\n');
+ var number: usize = 0;
+ while (lines.next()) |line| {
+ number += 1;
+ const word = std.mem.trim(u8, line, " \t\r");
+ // `#` starts a comment only at a line's start: a value may hold one
+ // (`#ff0000`).
+ if (word.len == 0 or isComment(word)) continue;
+ const setting = config.Runtime.find(word[0 .. std.mem.indexOfAny(u8, word, " \t") orelse word.len]);
+ // Saved in a pane, its settings run again and its commands do not:
+ // a word that is neither (a typo) is still said, as at startup.
+ if (settings_only and setting == null and std.meta.stringToEnum(Builtin, word[0 .. std.mem.indexOfAny(u8, word, " \t+") orelse word.len]) != null) continue;
+ // One init file serves the window and the terminal: a setting only
+ // the other has (Font, GripWidth in a terminal) is no failure here.
+ if (setting) |s| if (!s.enabled(builtins.viewCapabilities(p))) continue;
+ const failures = p.fs.failures;
+ const known = executeBuiltinLine(p, p.view().active, line);
+ if (known and p.fs.failures == failures) continue;
+ // Never silent: a word it has not, or one that failed, is a notice
+ // on the message row and an err in the log, with its line.
+ var why_buf: pardes.Kind.Buf(320) = .{};
+ const why: []const u8 = if (!known)
+ why_buf.print("unknown word \"{s}\"", .{word[0..@min(word.len, 64)]})
+ else if (p.fs.said_len > 0) p.fs.said[0..p.fs.said_len] else "failed";
+ var said: pardes.Kind.Buf(400) = .{};
+ const line_said = said.print("init file line {d}: {s}", .{ number, why });
+ if (!known) p.reportFailure(p.view().active, line_said);
+ pardes.ctlfs.events.notePath(p, "err -", line_said);
+ }
+}
+
+pub fn runBuiltin(p: *Pardes, b: Builtin, id: usize, txt: []const u8, arg: ?[]const u8) void {
+ runBuiltinFrom(p, b, id, txt, arg, false);
+}
+
+/// `runBuiltin` saying whether a gesture on the pane's body made it.
+pub fn runBuiltinFrom(p: *Pardes, b: Builtin, id: usize, txt: []const u8, arg: ?[]const u8, from_body: bool) void {
+ if (!normal.multiOnce(p.editor())) return; // a builtin is per-keystroke, never per-cursor
+ // A window's own word in a terminal's view (Fonts, Lift): said, as a
+ // word a build lacks is, and never run.
+ if (!builtins.registry.here(p, b)) {
+ var said: pardes.Kind.Buf(96) = .{};
+ return p.reportFailure(id, said.print("{s} is GUI-only, invalid here", .{@tagName(b)}));
+ }
+ // No pane at all (the window Delcol emptied): a word runs all the same,
+ // with a stand-in that is no pane (New, Newcol, Exit are what make sense).
+ const pane = p.panes[id] orelse (if (builtins.registry.scope(b) == .session or panes.anyPane(p) == null) panes.standIn(p) orelse return else return);
+ // A builtin says its own name before it runs, so a tagline click is
+ // legible even when what it did is invisible. Whatever the builtin
+ // then says for itself replaces the announcement. It is the message
+ // row's only, never /log's: a msg record is something said.
+ if (p.announce and p.settings.verbose and builtins.registry.announces(b)) {
+ p.fs.unlogged = true;
+ p.setMessage(id, @tagName(b));
+ p.fs.unlogged = false;
+ pane.msg_announcement = true;
+ }
+ // A word for one kind of pane, on another: refused with why, not a
+ // silent nothing (Palette on a file, Filter on a PDF).
+ if (builtins.registry.needs(b)) |kind| if (p.panes[id] == null or !kind.has(pane)) {
+ var said: pardes.Kind.Buf(96) = .{};
+ return p.reportFailure(id, said.print("{s}: only {s} pane takes it", .{ @tagName(b), kind.noun() }));
+ };
+ const c: builtins.Ctx = .{ .p = p, .pane = pane, .id = id, .txt = txt, .arg = arg, .from_body = from_body };
+ builtins.registry.dispatch(b, c);
+}
+
+pub fn applySettingBuiltin(p: *Pardes, setting: config.Runtime.Setting, arg: ?[]const u8) void {
+ const v = p.view();
+ const previous_tagline_percent = p.settings.font.tagline_percent;
+ switch (setting.action) {
+ .theme => {
+ const want = std.mem.trim(u8, arg orelse return, " \t\r\n");
+ if (colors.themeIndex(want)) |i| return colors.setThemeIndex(p, i);
+ // A failure is said, the init file's too (applyStartupConfig
+ // logs it with its line): never silent.
+ // All of it within the 128 bytes a 9P error carries, so a write
+ // failing with it is cut nowhere: the names get what is left.
+ const shown = want[0..@min(want.len, 40)];
+ const room = 128 -| ("Theme: no theme \"\"; like it: ; Themes lists all".len + shown.len);
+ var near: [128]u8 = undefined;
+ var text: pardes.Kind.Buf(256) = .{};
+ return p.reportFailure(v.active, text.print("Theme: no theme \"{s}\"; like it: {s}; Themes lists all", .{ shown, colors.themesNear(near[0..@min(room, near.len)], want) }));
+ },
+ .font => {
+ if (comptime !font_picker) return;
+ const spec = config.Runtime.FontSpec.parse(arg orelse return) orelse return;
+ const matches = fonts.list(p.scratch.allocator(), spec.name);
+ if (matches.len == 0) return;
+ if (!p.settings.requestFont(matches[0].path, matches[0].name, spec.size_hundredths)) return;
+ p.font_request_taken = false;
+ },
+ // A shell that is no executable is refused, not taken and quietly
+ // replaced at the next spawn; bare, it goes back to the default.
+ .shell => {
+ const want = std.mem.trim(u8, arg orelse "", " \t\r\n");
+ if (want.len == 0) {
+ p.settings.shell.requested.clear();
+ p.settings.shell.pending = true;
+ if (!p.announce) return;
+ var said: pardes.Kind.Buf(320) = .{};
+ return p.setMessage(v.active, said.print("Shell: the default again, {s} ($SHELL, else /bin/sh)", .{config.defaultShell()[0..@min(config.defaultShell().len, 256)]}));
+ }
+ var why: [320]u8 = undefined;
+ if (pardes.tape.value(.shell, @import("dev").host_io.Shell.refusal, .{ want[0..@min(want.len, 200)], &why })) |refused| {
+ var text: pardes.Kind.Buf(340) = .{};
+ return p.reportFailure(v.active, text.print("Shell: {s}", .{refused}));
+ }
+ if (!p.settings.apply(setting, want)) p.reportFailure(v.active, "Shell: takes " ++ comptime config.Runtime.takes(.shell).?);
+ },
+ // Bare, each says its value, as acme's Tab says the width.
+ inline .tab, .callstack_depth => |_, tag| if (arg == null) {
+ var said: pardes.Kind.Buf(40) = .{};
+ return p.setMessage(v.active, said.print("{s} {d}", .{ setting.word, @field(p.settings, @tagName(tag)) }));
+ } else if (!p.settings.apply(setting, arg)) {
+ var said: pardes.Kind.Buf(96) = .{};
+ p.reportFailure(v.active, said.print("{s}{s}: takes {s}", .{ setting.word, if (tag == .tab) ": invalid tab width" else "", comptime config.Runtime.takes(tag).? }));
+ } else if (tag == .tab) {
+ config.tab_now = p.settings.tab;
+ },
+ // Said with what it takes: its words, its range.
+ else => if (!p.settings.apply(setting, arg)) {
+ var buf: [160]u8 = undefined;
+ var text: std.Io.Writer = .fixed(&buf);
+ // A list of values too long for the row is cut.
+ (if (config.Runtime.takes(setting.action)) |values| text.print("{s}: takes {s}", .{ setting.word, values }) else text.print("{s}: does not take that value", .{setting.word})) catch {};
+ p.reportFailure(v.active, text.buffered());
+ },
+ }
+ if (setting.action == .tab) {
+ for (p.panes, 0..) |slot, id| {
+ p.surface.body_layers[id].rows = 0;
+ const pane = slot orelse continue;
+ pane.geometry_key = 0;
+ }
+ look.cancelLookHover(p);
+ }
+ if (setting.action == .tagline_size and p.settings.font.tagline_percent != previous_tagline_percent)
+ look.cancelLookHover(p);
+}
+
+// ---- the words addons bring (Kind.Registry's) ----
+
+/// A line whose first word is an addon's: run, and true; false for one
+/// that is no addon's word.
+fn runLine(p: *Pardes, id: usize, line: []const u8) bool {
+ const end = std.mem.indexOfAny(u8, line, " \t") orelse line.len;
+ const w = p.kinds.word(line[0..end]) orelse return false;
+ runWord(p, id, w, line, std.mem.trim(u8, line[end..], " \t"));
+ return true;
+}
+
+/// Word `w` from pane `id`: one that opens its kind opens a pane of it
+/// (`arg` its path), any other goes to the pane's kind, which must be
+/// the word's. Its argument is checked first.
+pub fn runWord(p: *Pardes, id: usize, w: *const pardes.Kind.Word, line: []const u8, arg: []const u8) void {
+ const name = w.name.slice();
+ var said: [256]u8 = undefined;
+ if (w.arg == pardes.Kind.c.PA_ARG_NONE and arg.len > 0) return p.reportFailure(id, std.fmt.bufPrint(&said, "{s} takes no argument", .{name}) catch name);
+ if (w.arg == pardes.Kind.c.PA_ARG_REQUIRED and arg.len == 0) return p.reportFailure(id, std.fmt.bufPrint(&said, "{s} needs an argument", .{name}) catch name);
+ if (w.opens != 0) {
+ const free = p.freeSlot() orelse return p.reportError(id, name, error.NoPaneSlots);
+ var path_buf: [4096]u8 = undefined;
+ const path = if (arg.len == 0 or std.Io.Dir.path.isAbsolute(arg)) arg else std.fmt.bufPrint(&path_buf, "{s}/{s}", .{ std.mem.trimEnd(u8, if (p.panes[id]) |pane| Pardes.paneDir(pane) else p.sessionDir(), "/"), arg }) catch arg;
+ const made = (if (path.len > 0) pardes.Kind.openFile(p, free, w.kind, path) else pardes.Kind.open(p, free, w.kind, null, null)) catch |err| return p.reportError(id, name, err);
+ if (made.addon.?.refused.len > 0) {
+ made.vweight = 0;
+ p.unplaced.set(free);
+ return p.reportFailure(id, std.fmt.bufPrint(&said, "{s}: {s}", .{ name, made.addon.?.refused.slice() }) catch name);
+ }
+ return layout.placeNew(p, id, id, free, .doc);
+ }
+ const kind = p.kinds.kinds[w.kind].nameOf();
+ const here = if (p.panes[id]) |pane| if (pane.addon) |s| s.live and s.is(kind) else false else false;
+ if (!here) return p.reportFailure(id, std.fmt.bufPrint(&said, "{s} is a {s} pane's word: run it in one", .{ name, kind }) catch name);
+ var reply: [160]u8 = undefined;
+ if (pardes.Kind.ctl(p, id, line, &reply)) |why| return p.reportFailure(id, std.fmt.bufPrint(&said, "{s}: {s}", .{ name, why }) catch name);
+ if (arg.len == 0) return;
+ // Its last value, one line a word, which a kind with no record is
+ // given again when its pane comes back.
+ const s = p.panes[id].?.addon.?;
+ var next: pardes.Kind.Buf(256) = .{};
+ var out: std.Io.Writer = .fixed(&next.bytes);
+ var lines = std.mem.tokenizeScalar(u8, s.values.slice(), '\n');
+ const full = std.fmt.bufPrint(&said, "{s}: the pane's values are past {d} bytes: not kept", .{ name, next.bytes.len }) catch name;
+ while (lines.next()) |l| if (!std.mem.eql(u8, l[0 .. std.mem.indexOfScalar(u8, l, ' ') orelse l.len], name)) out.print("{s}\n", .{l}) catch return p.reportFailure(id, full);
+ out.print("{s} {s}\n", .{ name, arg }) catch return p.reportFailure(id, full);
+ next.len = @intCast(out.buffered().len);
+ s.values = next;
+}
+
+test "the acme chords act once per selection, not once on the primary" {
+ const gpa = std.testing.allocator;
+ const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 100, .rows = 30 });
+ defer p.deinit();
+ while (p.nextEffect()) |_| {}
+ // Two selections, each naming a DIFFERENT builtin, so what ran is visible
+ // in the layout rather than in a shell nobody can read from a test.
+ const pane = try p.setTestFile("Newcol\nNewcol\n");
+ const ranges = [_]modal.Selection{
+ .{ .anchor = 0, .head = 6 },
+ .{ .anchor = 7, .head = 13 },
+ };
+ pane.body.setRanges(pane.file.?.content, &ranges, &.{}, 0, true);
+ try std.testing.expectEqual(@as(u16, 1), pane.body.nsel);
+
+ const before = p.view().ncol;
+ p.update(.{ .key = .{ .cp = Key.tab } }); // config.exec_key: Exec
+ // BOTH ran. Before this the chord read the primary range and dropped the
+ // other cursor, so one keystroke over two cursors made one column.
+ try std.testing.expectEqual(before + 2, p.view().ncol);
+ // ...and the chord consumed the selection exactly as it does with one.
+ try std.testing.expectEqual(@as(u16, 0), p.panes[0].?.body.nsel);
+}
+
+test "a switch clicked with a selection held flips unless the selection says on or off" {
+ const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 24 });
+ defer p.deinit();
+ while (p.nextEffect()) |_| {}
+ const before = p.settings.verbose;
+ _ = execute(p, p.view().active, withArg(p, "Verbose", "some selected text"));
+ try std.testing.expectEqual(!before, p.settings.verbose);
+ _ = execute(p, p.view().active, withArg(p, "Verbose", "on"));
+ try std.testing.expect(p.settings.verbose);
+ _ = execute(p, p.view().active, withArg(p, "Verbose", " off\n"));
+ try std.testing.expect(!p.settings.verbose);
+ // A word that takes any argument still gets the selection.
+ try std.testing.expectEqualStrings("Msg some text", withArg(p, "Msg", "some text"));
+}