summaryrefslogtreecommitdiff
path: root/src/hunks.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-17 02:07:32 -0300
committerGabriel Schneider <[email protected]>2026-11-17 12:15:01 -0300
commit83b9d45b8665a80bdb70934aa23bbde5cc03d202 (patch)
treec902b557ef624b07cd139659d0a891cd4809e03e /src/hunks.zig
parentb3d32656413734952bc918c2d4cfd12fede6c214 (diff)
downloadpardes-83b9d45b8665a80bdb70934aa23bbde5cc03d202.tar.gz
pardes-83b9d45b8665a80bdb70934aa23bbde5cc03d202.zip
plumber: a look sends what it looks at (docs/next-plumber.md, phase 2): after the pane's own readings and once a name already open in a pane is focused (never sent), lookAt sends the trimmed name, type text, src pardes (term from a raw terminal, a kind's name from its pane), wdir the pane's directory, with what resolveLookTarget found of it (Plumb.Seen: isfile and isdir take it, so no second stat and Look's own readings of a URL or `//` stay no file); a message the rules give back to `edit` as sent is Look's own (Sent.look) and runs lookAt's switch on that target, one nothing takes or a started word declines is Look's own too, whose `.url` arm is now the word's search; the edit port's open of another message runs with look_text, which now means "unplumbed", so the edit port's lookAt never sends again (the recursion trap); the three diff hooks become the default rules of the record's §5.2 (a bare .diff/.patch to `Diff $file`, a commit hash in the editor to `Diff $0`, after the two edit sets), and `Diff` takes a file or a revision, a rule's Diff declining in silence (Pardes.plumb_declined) where it cannot (no hunk, not readable, no .git above): hunks.lookedAt and lookedAtCommit go, their bodies in hunks.word, the `needs` check of the bare form in the word, PaneKind.diff gone; the old one-line `Plumb` word, Core.plumb, look.Port, plumbRuleError, plumbTarget and resolve's rules block go: bare, `Plumb` opens <config>/plumbing as Config opens init (both through builtins.openConfigFile; `include basic` in a new one), an argument is refused in words, Save applies it (host_io.writeFile beside reapplyInitFile, Plumb.saved), and boot reads it (Plumb.boot in applyStartupConfig); a started program now runs detached as Plan 9's plumber's (the coordinator's decision for the user's question 4): Effect.start, every host's host_io.startDetached (double fork, setsid, /dev/null for 0-2, every other fd closed, argv exec'd in wdir with no shell, the child's errno back through a close-on-exec pipe, said as `plumb start: <prog>: <why>`), so exec_dir goes; resolveLookTarget's and Plumb.Send.search's directory walk is one look.Dirs; test: look.zig's Plumb test rewritten onto the rules (a rule's word, an open pane unsent, the hover's resolve, a URL with no web rule searched for) and a hash inside and outside a git work tree; addons.zig's Plumb test onto plumb/rules (a port no kind reads, its kind gone: Look's own); Plumb.zig: the word, the save and boot; replicas.zig gains a URL (speculated whole, confirmed, no rollback) and a hash, each the authority's and a replica's own; every look golden unchanged; the reference quotes src/plumbing whole and says the order, the changelog says so
Diffstat (limited to 'src/hunks.zig')
-rw-r--r--src/hunks.zig110
1 files changed, 64 insertions, 46 deletions
diff --git a/src/hunks.zig b/src/hunks.zig
index afd2ac9d..f316fe48 100644
--- a/src/hunks.zig
+++ b/src/hunks.zig
@@ -601,39 +601,77 @@ fn trimmedWidth(section: []const u8) usize {
// ---- the +Diff in the editor ----
//
-// Its word (Diff), the open and the text it goes back to; and the three
-// hooks the generic code calls, one call each (look.zig's bare look and
-// commit look, ninep/ctl.zig's filled +Pager), which a plumber's rules
-// would replace.
+// Its word (Diff), the open and the text it goes back to; and the hook
+// ninep/ctl.zig's filled +Pager calls, which the pager's plumbing message
+// replaces. A bare look at a .diff and at a commit hash are plumbing
+// rules that start Diff (src/plumbing).
-/// The `Diff` word (builtins.zig registers it): on a +Diff, the diff it
-/// stacks as text; on a diff, its hunks stacked in a +Diff.
+/// The `Diff` word (builtins.zig registers it). Bare, on a +Diff, the diff
+/// it stacks as text; on a diff, its hunks stacked in a +Diff. With a file
+/// (a .diff, a .patch: relative to the pane's directory), its hunks
+/// stacked so; with a revision (7 to 40 lowercase hex digits, a letter and
+/// a digit among them), `git show` of it, paged into a +Pager that stacks
+/// its hunks (ctl.fillPager), run in the pane's git work tree. A plumbing
+/// rule's Diff that cannot (no hunk, no work tree) declines, saying
+/// nothing (Pardes.plumb_declined), and the look looks itself.
pub fn word(c: pardes.builtins.Ctx) void {
- const f = &c.pane.file.?;
- if (diffSource(c.p, f)) |source| return showDiffText(c.p, c.id, source);
- _ = openDiff(c.p, c.id, f.path, f.content) catch |err| switch (err) {
- error.NoHunks => c.p.reportFailure(c.id, "Diff: no hunk in it"),
- else => c.p.reportError(c.id, "hunks", err),
+ const p = c.p;
+ const arg = std.mem.trim(u8, c.arg orelse "", " \t\r\n");
+ if (arg.len == 0) {
+ if (!shows(c.pane)) return p.reportFailure(c.id, "Diff: only a diff or a +Diff pane takes it");
+ const f = &c.pane.file.?;
+ if (diffSource(p, f)) |source| return showDiffText(p, c.id, source);
+ _ = openDiff(p, c.id, f.path, f.content) catch |err| switch (err) {
+ error.NoHunks => p.reportFailure(c.id, "Diff: no hunk in it"),
+ else => p.reportError(c.id, "hunks", err),
+ };
+ return;
+ }
+ if (isCommitHash(arg)) {
+ var dir = pardes.filesystem.localPath(Pardes.paneDir(c.pane)) orelse return declined(p, c.id, "Diff: not in a git work tree");
+ while (true) {
+ var buf: [4096]u8 = undefined;
+ const dot_git = std.fmt.bufPrint(&buf, "{s}/.git", .{dir}) catch return declined(p, c.id, "Diff: not in a git work tree");
+ if (pardes.exec.kindOf(dot_git) != null) break;
+ dir = std.Io.Dir.path.dirname(dir) orelse return declined(p, c.id, "Diff: not in a git work tree");
+ }
+ // ponytail: a hex word that names no commit leaves git's error in a command pane, not a search; check the object first if that bites.
+ var line: [64]u8 = undefined;
+ const dst = pardes.exec.execute(p, c.id, std.fmt.bufPrint(&line, "git show {s}", .{arg}) catch unreachable) orelse return;
+ if (p.panes[dst]) |cp| cp.command_pages = true;
+ return;
+ }
+ var buf: [4096]u8 = undefined;
+ const path = if (arg[0] == '/') arg else std.fmt.bufPrint(&buf, "{s}/{s}", .{ std.mem.trimEnd(u8, Pardes.paneDir(c.pane), "/"), arg }) catch arg;
+ const bytes = pardes.filesystem.read(p, path) catch |err| {
+ if (p.plumb_declined != null) return declined(p, c.id, "");
+ return p.reportError(c.id, path, err);
+ };
+ defer p.gpa.free(bytes);
+ p.view().active = openDiff(p, c.id, path, bytes) catch |err| switch (err) {
+ error.NoHunks => return declined(p, c.id, "Diff: no hunk in it"),
+ else => {
+ if (p.plumb_declined != null) return declined(p, c.id, "");
+ return p.reportError(c.id, "hunks", err);
+ },
};
}
-/// A pane the word works on: a diff (a .diff, a .patch, a +Pager holding
-/// one) or a +Diff.
-pub fn shows(pane: *const Pane) bool {
- const f = pane.file orelse return false;
- return pane.colorAlgo() == .diff or (f.output != null and std.meta.eql(f.output.?.from, Output.Origin{ .cmd = .Diff }));
+/// A Diff that cannot do what it was asked: a plumbing rule's declines,
+/// saying nothing; one typed says `why`.
+fn declined(p: *Pardes, id: usize, why: []const u8) void {
+ if (p.plumb_declined) |*d| {
+ d.* = true;
+ return;
+ }
+ p.reportFailure(id, why);
}
-/// The hook of a bare look at a .diff or a .patch (look.zig's open): its
-/// hunks stacked in a +Diff, which takes the keyboard as the file would;
-/// false, for look to open it as text, when it is no such file, a place in
-/// one, one with no hunk, or the text is what is asked (showDiffText).
-pub fn lookedAt(p: *Pardes, id: usize, path: []const u8, at: pardes.look.Spot) bool {
- if (p.look_text or at.line != 0 or !(std.mem.endsWith(u8, path, ".diff") or std.mem.endsWith(u8, path, ".patch"))) return false;
- const bytes = pardes.filesystem.read(p, path) catch return false;
- defer p.gpa.free(bytes);
- p.view().active = openDiff(p, id, path, bytes) catch return false;
- return true;
+/// A pane the bare word works on: a diff (a .diff, a .patch, a +Pager
+/// holding one) or a +Diff.
+fn shows(pane: *const Pane) bool {
+ const f = pane.file orelse return false;
+ return pane.colorAlgo() == .diff or (f.output != null and std.meta.eql(f.output.?.from, Output.Origin{ .cmd = .Diff }));
}
/// The hook of a +Pager filled with a diff (ninep/ctl.zig's fillPager:
@@ -650,26 +688,6 @@ pub fn paged(p: *Pardes, id: usize) void {
if (p.panes[keyboard] != null) p.view().active = keyboard;
}
-/// The hook of a commit hash (7 to 40 hex digits, a letter and a digit
-/// among them; look.zig's look at a word) looked at inside a git repository: `git show` it, paged into a +Pager,
-/// which stacks its hunks in a +Diff (ctl.fillPager); the command pane
-/// goes when it succeeds (Pane.commandDone).
-// ponytail: a hex word that names no commit leaves git's error in a command pane, not a search; check the object first if that bites.
-pub fn lookedAtCommit(p: *Pardes, id: usize, pane: *Pane, hash: []const u8) bool {
- if (!isCommitHash(hash)) return false;
- var dir = pardes.filesystem.localPath(Pardes.paneDir(pane)) orelse return false;
- while (true) {
- var buf: [4096]u8 = undefined;
- const dot_git = std.fmt.bufPrint(&buf, "{s}/.git", .{dir}) catch return false;
- if (pardes.exec.kindOf(dot_git) != null) break;
- dir = std.Io.Dir.path.dirname(dir) orelse return false;
- }
- var line: [64]u8 = undefined;
- const dst = pardes.exec.execute(p, id, std.fmt.bufPrint(&line, "git show {s}", .{hash}) catch return false) orelse return true;
- if (p.panes[dst]) |c| c.command_pages = true;
- return true;
-}
-
fn isCommitHash(hex: []const u8) bool {
if (hex.len < 7 or hex.len > 40) return false;
var digit = false;