From 779ff8cc27014b8c2f2f078985ab2ca9cf285740 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 05:45:38 -0300 Subject: Editor: what an edit of a text runs with, out of Pardes (docs/next-editor.md, R1): src/Editor.zig is a context of the allocators and pointers to the registers, the register the running command names, the replay's multi_* and the retired text, and the core (`p`), gathered per call by `p.editor()` from where they are, so no field of Pardes or its view moves and a dump crosses builds as before (an embedded `ed: Editor` would have renamed `registers` in the dump, matched by name, and lost them at an Upgrade); setYank, registerValue, retireEditText and normal.multiOnce take it, their callers hand `p.editor()`; core-test 810 of 949, shell-test 989 of 992, hxdiff 658 / parity 738 / hxgolf 124 with 0 mismatches, replicas pass --- src/exec.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/exec.zig') diff --git a/src/exec.zig b/src/exec.zig index 5f5a2206..4ab5a37f 100644 --- a/src/exec.zig +++ b/src/exec.zig @@ -958,7 +958,7 @@ pub fn runBuiltin(p: *Pardes, b: Builtin, id: usize, txt: []const u8, arg: ?[]co /// `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)) return; // a builtin is per-keystroke, never per-cursor + 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)) { -- cgit v1.3 From 2b04f877c29cf1e95c2fc8154de1da322c7299a3 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 05:49:29 -0300 Subject: edit: the routing of a text's characters takes the editing context (docs/next-editor.md, R2, first slice): flatSurface, editText, editTextEol, setEditText, setEditTextEnd, setEditTextSpan, paneCursorLines, paneByteAtDisplay, pinPaneCursor, paneWrapWidth, treeIndent, pushUndo, undoStep, stepOwn, selText and vselText take `e: Editor` where they took `*Pardes`, the pane they route to reached through `e.p`; 149 calls in 16 files hand `p.editor()`, nothing else changes; core-test 810 of 949, shell-test 989 of 992, hxdiff 658 / parity 738 / hxgolf 124 with 0 mismatches, replicas pass --- src/Core.zig | 2 +- src/Pipe.zig | 10 +- src/body_layer.zig | 2 +- src/builtins.zig | 4 +- src/draw.zig | 2 +- src/edit.zig | 348 ++++++++++++++++++++++++++--------------------------- src/exec.zig | 4 +- src/keys.zig | 6 +- src/look.zig | 16 +-- src/lsp/Ask.zig | 2 +- src/mouse.zig | 20 +-- src/normal.zig | 26 ++-- src/tagline.zig | 6 +- test/hxdiff.zig | 2 +- test/panes.zig | 2 +- test/perf.zig | 2 +- 16 files changed, 227 insertions(+), 227 deletions(-) (limited to 'src/exec.zig') diff --git a/src/Core.zig b/src/Core.zig index c57818ee..9ba448be 100644 --- a/src/Core.zig +++ b/src/Core.zig @@ -2922,7 +2922,7 @@ test "a REPL bound for a language takes the exec made on a body in it; tags, Exe try std.testing.expectEqualStrings("def f():\r return 1\r\r", Sent.said(1)); // Several selections executed at once: each is sent, not the first. { - const text = try edit.flatSurface(p, &file.body); + const text = try edit.flatSurface(p.editor(), &file.body); file.body.setRanges(text, &.{ .{ .anchor = 0, .head = 8 }, .{ .anchor = 9, .head = 11 } }, &.{}, 0, true); try std.testing.expect(exec.chordEachSel(p, file, .Exec)); Sent.drain(p); diff --git a/src/Pipe.zig b/src/Pipe.zig index 10967a3c..9b913f1e 100644 --- a/src/Pipe.zig +++ b/src/Pipe.zig @@ -95,7 +95,7 @@ pub fn submitPipe(p: *Pardes, id: usize) void { const f = pane.file orelse return; if (!panes.Output.fileTraits(f.output).saves) return; } - const content = edit.flatSurface(p, t) catch |err| return p.reportError(id, "pipe", err); + const content = edit.flatSurface(p.editor(), t) catch |err| return p.reportError(id, "pipe", err); const how = switch (pane.prompt) { .pipe => |pipe| pipe, else => return, @@ -259,7 +259,7 @@ pub fn pipeResponse( .workspace => &p.global_tag, .input => return, }; - const content = edit.flatSurface(p, t) catch |err| return p.reportError(wait.pane, "pipe", err); + const content = edit.flatSurface(p.editor(), t) catch |err| return p.reportError(wait.pane, "pipe", err); const changed = if (t.what == .body) changed: { const f = pane.file orelse return; if (!panes.Output.fileTraits(f.output).saves) return; @@ -314,12 +314,12 @@ pub fn pipeResponse( } // One async request is one history transaction, even at 64 cursors. - edit.pushUndo(p, t) catch |err| { + edit.pushUndo(p.editor(), t) catch |err| { p.gpa.free(replacement); return p.reportError(wait.pane, "pipe", err); }; - edit.setEditText(p, t, replacement) catch |err| return p.reportError(wait.pane, "pipe", err); - const now = edit.flatSurface(p, t) catch |err| return p.reportError(wait.pane, "pipe", err); + edit.setEditText(p.editor(), t, replacement) catch |err| return p.reportError(wait.pane, "pipe", err); + const now = edit.flatSurface(p.editor(), t) catch |err| return p.reportError(wait.pane, "pipe", err); t.setRanges(now, new_ranges[0..n], &.{}, wait.primary, wait.explicit); t.select = false; t.show(); diff --git a/src/body_layer.zig b/src/body_layer.zig index 26513b93..6748c161 100644 --- a/src/body_layer.zig +++ b/src/body_layer.zig @@ -466,7 +466,7 @@ pub fn renderBody(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane // `gw`'s labels, two letters over the start of each word if (pane.jump.items.len > 0) labels: { - const flat = edit.flatSurface(p, &pane.body) catch break :labels; + const flat = edit.flatSurface(p.editor(), &pane.body) catch break :labels; const letters = config.jump_label_alphabet; const style: CellStyle = .{ .fg = .{ .rgb = th.sel_fg }, .bg = .{ .rgb = th.sel_bg }, .bold = true }; for (pane.jump.items, 0..) |word, i| { diff --git a/src/builtins.zig b/src/builtins.zig index e9c91fd8..0e8610ae 100644 --- a/src/builtins.zig +++ b/src/builtins.zig @@ -1111,7 +1111,7 @@ pub const Undo = struct { pub fn run(c: Ctx) void { if (@import("edit_cmd.zig").busyOn(c.p, c.id)) return c.p.reportFailure(c.id, "Undo: " ++ @import("edit_cmd.zig").e_busy); if (c.pane.file) |f| if (f.history.journal.done == 0) return c.p.setMessage(c.id, "Undo: nothing to undo"); - edit.undoStep(c.p, &c.pane.body, true) catch |err| c.p.reportError(c.id, "Undo", err); + edit.undoStep(c.p.editor(), &c.pane.body, true) catch |err| c.p.reportError(c.id, "Undo", err); } }; @@ -1120,7 +1120,7 @@ pub const Redo = struct { pub fn run(c: Ctx) void { if (@import("edit_cmd.zig").busyOn(c.p, c.id)) return c.p.reportFailure(c.id, "Redo: " ++ @import("edit_cmd.zig").e_busy); if (c.pane.file) |f| if (f.history.journal.done == f.history.journal.steps.items.len) return c.p.setMessage(c.id, "Redo: nothing to redo"); - edit.undoStep(c.p, &c.pane.body, false) catch |err| c.p.reportError(c.id, "Redo", err); + edit.undoStep(c.p.editor(), &c.pane.body, false) catch |err| c.p.reportError(c.id, "Redo", err); } }; diff --git a/src/draw.zig b/src/draw.zig index 92bcc592..10556ac9 100644 --- a/src/draw.zig +++ b/src/draw.zig @@ -645,7 +645,7 @@ fn renderHeaderLayer(p: *Pardes, layer: *pardes.Layer, kind: TagKind, id: u16, v }; } if (editing) |t| { - if (tagline.headerSelection(t, try edit.flatSurface(p, t), line + v.header_top)) |span| { + if (tagline.headerSelection(t, try edit.flatSurface(p.editor(), t), line + v.header_top)) |span| { var col = panes.File.rawDisplayCol(shown, span.lo) -| scroll; const end = panes.File.rawDisplayCol(shown, span.hi) -| scroll; while (col < end and col < columns) : (col += 1) { diff --git a/src/edit.zig b/src/edit.zig index 2dd613ac..e8b5aab2 100644 --- a/src/edit.zig +++ b/src/edit.zig @@ -56,7 +56,7 @@ pub fn registerValue(e: Editor, t: *Text, name: u21) ?[]const u8 { return switch (name) { '_' => null, '#' => std.fmt.allocPrint(e.scratch, "{d}", .{i + 1}) catch null, - '.' => selText(e.p, t) catch null, + '.' => selText(e, t) catch null, '%' => if (t.what == .body) if (t.pane()) |pane| (if (pane.file) |f| f.path else null) else null else null, else => e.registers.value(name, i), }; @@ -64,11 +64,11 @@ pub fn registerValue(e: Editor, t: *Text, name: u21) ?[]const u8 { /// the text of the range under the cursor: the selection, or the cursor's /// own character -fn selText(p: *Pardes, t: *Text) Error![]const u8 { +fn selText(e: Editor, t: *Text) Error![]const u8 { const save = t.vsel; defer t.vsel = save; if (!t.vsel.active) t.vsel = .{ .active = true, .row = t.cur_row, .col = t.cur_col, .explicit = false }; - return vselText(p, t); + return vselText(e, t); } /// `p` / `P` / `R` from the register the command names: this range's value @@ -351,7 +351,7 @@ pub fn sweepEnds(sl: Pane.Sel) struct { [2]i32, [2]i32 } { pub fn currentSelText(p: *Pardes, pane: *Pane) ?[]const u8 { if (pane.addon) |k| if (k.selection.len > 0) return k.selection; if (!pane.body.vsel.active) return null; - return vselText(p, &pane.body) catch null; + return vselText(p.editor(), &pane.body) catch null; } pub fn exitInsert(p: *Pardes, t: *Text) void { @@ -363,7 +363,7 @@ pub fn exitInsert(p: *Pardes, t: *Text) void { // helix enter_normal_mode: every range, from its start to the char // before its end when it points forward, else to its end const expl = t.vsel.active and t.vsel.explicit; - const text = flatSurface(p, t) catch |err| return p.reportError(p.view().active, "edit", err); + const text = flatSurface(p.editor(), t) catch |err| return p.reportError(p.view().active, "edit", err); var rs: [Text.max_selections]modal.Selection = undefined; const got = t.ranges(text, 0, &rs); for (rs[0..got.n]) |*r| { @@ -375,10 +375,10 @@ pub fn exitInsert(p: *Pardes, t: *Text) void { t.setRanges(text, rs[0..got.n], &.{}, got.pri, expl); } -pub fn paneCursorLines(p: *Pardes, t: *Text) Error![]const []const u8 { - const arena = p.scratch.allocator(); +pub fn paneCursorLines(e: Editor, t: *Text) Error![]const []const u8 { + const arena = e.scratch; if (t.what != .body) { - const text = try flatSurface(p, t); + const text = try flatSurface(e, t); const lines = try arena.alloc([]const u8, std.mem.count(u8, text, "\n") + 1); var it = std.mem.splitScalar(u8, text, '\n'); for (lines) |*line| line.* = it.next().?; @@ -387,13 +387,13 @@ pub fn paneCursorLines(p: *Pardes, t: *Text) Error![]const []const u8 { const pane = t.pane() orelse return &.{}; if (pane.file) |*f| return panes.File.cursorLines(arena, pane, f); if (pane.addon != null) return &.{}; - return terminalLines(p, pane); + return terminalLines(e.p, pane); } -pub fn paneByteAtDisplay(p: *Pardes, t: *Text, row: i32, from_raw: i32, display_col: i32) i32 { +pub fn paneByteAtDisplay(e: Editor, t: *Text, row: i32, from_raw: i32, display_col: i32) i32 { if (t.what == .body) if (t.pane()) |pane| if (pane.file != null) return panes.File.byteAtRowDisplay(pane, row, from_raw, display_col); - const lines = paneCursorLines(p, t) catch return @max(0, from_raw + display_col); + const lines = paneCursorLines(e, t) catch return @max(0, from_raw + display_col); const local = row; if (local < 0 or @as(usize, @intCast(local)) >= lines.len) return @max(0, from_raw + display_col); @@ -406,7 +406,7 @@ pub fn paneByteAtDisplay(p: *Pardes, t: *Text, row: i32, from_raw: i32, display_ /// Freeze the live terminal cursor into the modal coordinate space. The /// emulator reports screen cells; editing state stores UTF-8 byte offsets. -pub fn pinPaneCursor(p: *Pardes, t: *Text) void { +pub fn pinPaneCursor(e: Editor, t: *Text) void { if (t.cur_pinned) return; // Only a body has a live cursor of someone else's to freeze. if (t.what != .body) { @@ -416,7 +416,7 @@ pub fn pinPaneCursor(p: *Pardes, t: *Text) void { const pane = t.pane() orelse return; pane.pinCursor(); if (pane.file == null and pane.addon == null) - t.cur_col = paneByteAtDisplay(p, t, t.cur_row, 0, t.cur_col); + t.cur_col = paneByteAtDisplay(e, t, t.cur_row, 0, t.cur_col); } // ---- a terminal's rows, and the copy of them an edit makes ---- @@ -524,26 +524,26 @@ pub fn terminalLines(p: *Pardes, pane: *Pane) ![]const []const u8 { return lines[0..n]; } -pub fn flatSurface(p: *Pardes, t: *Text) Error![]const u8 { +pub fn flatSurface(e: Editor, t: *Text) Error![]const u8 { switch (t.what) { .body => {}, // the tag as it is shown: the keyboard reaches its prefix too - .tag => return if (t.pane()) |pane| try tagline.tagText(p, p.scratch.allocator(), pane) else "", + .tag => return if (t.pane()) |pane| try tagline.tagText(e.p, e.scratch, pane) else "", .input => return t.own orelse "", - .column, .workspace => return t.own orelse tagline.headerDefault(p, t.what), + .column, .workspace => return t.own orelse tagline.headerDefault(e.p, t.what), } const pane = t.pane() orelse return ""; if (pane.file) |f| return f.content; if (pane.addon != null) return ""; - const lines = try terminalLines(p, pane); + const lines = try terminalLines(e.p, pane); // The kept rows' own join when the lines are them (no edit copy), not // made again on every keystroke; a join of them in scratch else. - const cache = &p.shell_rows; + const cache = &e.p.shell_rows; if (!cache.stale and cache.pane == pane and lines.ptr == cache.rows.ptr and lines.len == cache.rows.len) return cache.text; var total: usize = if (lines.len > 0) lines.len - 1 else 0; for (lines) |line| total += line.len; - const text = try p.scratch.allocator().alloc(u8, total); + const text = try e.scratch.alloc(u8, total); var at: usize = 0; for (lines, 0..) |line, i| { if (i > 0) { @@ -556,24 +556,24 @@ pub fn flatSurface(p: *Pardes, t: *Text) Error![]const u8 { return text; } -pub fn paneWrapWidth(p: *const Pardes, t: *Text) usize { +pub fn paneWrapWidth(e: Editor, t: *Text) usize { if (t.what != .body) return 0; const pane = t.pane() orelse return 0; if (pane.file == null) return 0; - return panes.File.wrapWidth(pane, p.settings.wrap); + return panes.File.wrapWidth(pane, e.p.settings.wrap); } /// Route shared edit operations to the characters `t` edits: its own, or /// for a body its file's content or its terminal's overlay. -fn editText(p: *Pardes, t: *Text, lo: i32, hi: i32, col: i32) Error!?panes.EditText { +fn editText(e: Editor, t: *Text, lo: i32, hi: i32, col: i32) Error!?panes.EditText { // A tag or header nobody has changed shows its default; the first edit // makes that default its own text. if (t.what == .tag) { const pane = t.pane() orelse return null; - return .{ .text = try tagline.tagText(p, p.scratch.allocator(), pane), .row0 = 0 }; + return .{ .text = try tagline.tagText(e.p, e.scratch, pane), .row0 = 0 }; } if (t.what != .body and t.what != .input and t.own == null) - t.own = try p.gpa.dupe(u8, try flatSurface(p, t)); + t.own = try e.gpa.dupe(u8, try flatSurface(e, t)); if (t.what != .body) return .{ .text = t.own orelse &.{}, .row0 = 0 }; const pane = t.pane() orelse return null; if (pane.file) |f| return .{ .text = f.content, .row0 = 0 }; @@ -594,12 +594,12 @@ fn editText(p: *Pardes, t: *Text, lo: i32, hi: i32, col: i32) Error!?panes.EditT var text = old.text; var owned = false; if (extending) { - const rows = try shellRows(p, pane); + const rows = try shellRows(e.p, pane); row0 = old.row - @max(0, up); covered = old.rows + @max(0, up) + @max(0, down); const up_len: usize = @intCast(@max(0, up)); const down_len: usize = @intCast(@max(0, down)); - const parts = try p.scratch.allocator().alloc([]const u8, up_len + 1 + down_len); + const parts = try e.scratch.alloc([]const u8, up_len + 1 + down_len); for (parts[0..up_len], 0..) |*part, i| { const src = @as(usize, @intCast(row0)) + i; part.* = if (src < rows.len) rows[src] else ""; @@ -613,100 +613,100 @@ fn editText(p: *Pardes, t: *Text, lo: i32, hi: i32, col: i32) Error!?panes.EditT const src = @as(usize, @intCast(old.row + old.rows)) + i; part.* = if (src < rows.len) rows[src] else ""; } - text = try std.mem.join(p.gpa, "\n", parts); + text = try std.mem.join(e.gpa, "\n", parts); owned = true; } const row: usize = @intCast(@max(0, want_lo - row0)); const line_len: i32 = @intCast(modal.lineSlice(text, row).len); if (col > line_len) { - const spaces = p.scratch.allocator().alloc(u8, @intCast(col - line_len)) catch { - if (owned) p.gpa.free(text); + const spaces = e.scratch.alloc(u8, @intCast(col - line_len)) catch { + if (owned) e.gpa.free(text); return null; }; @memset(spaces, ' '); - const padded = modal.insertAt(p.gpa, text, .{ .row = row, .col = @intCast(line_len) }, spaces) catch { - if (owned) p.gpa.free(text); + const padded = modal.insertAt(e.gpa, text, .{ .row = row, .col = @intCast(line_len) }, spaces) catch { + if (owned) e.gpa.free(text); return null; }; - if (owned) p.gpa.free(text); + if (owned) e.gpa.free(text); text = padded; owned = true; } if (owned) { - if (pane.ovl) |overlay| p.gpa.free(overlay.text); + if (pane.ovl) |overlay| e.gpa.free(overlay.text); pane.ovl = .{ .row = row0, .rows = covered, .text = text }; } // A terminal's text is its program's: edited here it is a copy, to run // with Tab or B2, gone when the program draws again. Said at its first // edit, so it never looks like an edit that was lost. - if (fresh) for (p.panes, 0..) |slot, id| if (slot == pane) - p.setMessage(id, "a copy of the terminal's text: Tab or B2 runs it; what the program draws next replaces it (Ctrl-b types to the program)"); + if (fresh) for (e.p.panes, 0..) |slot, id| if (slot == pane) + e.p.setMessage(id, "a copy of the terminal's text: Tab or B2 runs it; what the program draws next replaces it (Ctrl-b types to the program)"); return .{ .text = pane.ovl.?.text, .row0 = pane.ovl.?.row }; } -fn editTextEol(p: *Pardes, t: *Text, b: Bounds) Error!?panes.EditText { - const eb = (try editText(p, t, b.lo_row, b.hi_row, -1)) orelse return null; +fn editTextEol(e: Editor, t: *Text, b: Bounds) Error!?panes.EditText { + const eb = (try editText(e, t, b.lo_row, b.hi_row, -1)) orelse return null; const r: usize = @intCast(@max(0, b.hi_row - eb.row0)); // A file's own line count and line, from its index. if (r + 1 < panes.File.textLineCount(t, eb.text)) return eb; if (b.hi_col < @as(i32, @intCast(panes.File.textLine(t, eb.text, r).len))) return eb; - return editText(p, t, b.lo_row, b.hi_row + 1, -1); + return editText(e, t, b.lo_row, b.hi_row + 1, -1); } /// install a rewritten editable text (frees the old one) -pub fn setEditText(p: *Pardes, t: *Text, new: []u8) Error!void { - return setEditTextEnd(p, t, new, null); +pub fn setEditText(e: Editor, t: *Text, new: []u8) Error!void { + return setEditTextEnd(e, t, new, null); } /// `setEditText` with where the edit ended in `new` (in the edit text's /// rows) when the caller knows it, as typing does: then nothing scans the /// whole text to find it. Null, it is found by comparing the texts. -pub fn setEditTextEnd(p: *Pardes, t: *Text, new: []u8, known_end: ?modal.Cursor) Error!void { - return setEditTextSpan(p, t, new, known_end, null); +pub fn setEditTextEnd(e: Editor, t: *Text, new: []u8, known_end: ?modal.Cursor) Error!void { + return setEditTextSpan(e, t, new, known_end, null); } /// `setEditTextEnd` with what a file's splice left alone at either end, when /// the caller knows it (typing): the line index is then not found again by /// comparing the whole old and new texts, which a keystroke in a 7 MB file did. -fn setEditTextSpan(p: *Pardes, t: *Text, new: []u8, known_end: ?modal.Cursor, span: ?panes.File.Span) Error!void { +fn setEditTextSpan(e: Editor, t: *Text, new: []u8, known_end: ?modal.Cursor, span: ?panes.File.Span) Error!void { if (t.what == .tag) { // A tag is edited as it is shown, but only what follows its // prefix is its own: an edit that changed the prefix is refused. // The shown text stays alive until the next such edit, since every // caller reads the new text back, as a body's is kept. - defer retireEditText(p.editor(), new); + defer retireEditText(e, new); const pane = t.pane() orelse return; - const prefix = try tagline.pathPrefix(p, p.scratch.allocator(), pane); + const prefix = try tagline.pathPrefix(e.p, e.scratch, pane); if (!std.mem.startsWith(u8, new, prefix)) { t.refused = true; return; } - const tail = try p.gpa.dupe(u8, new[prefix.len..]); - if (t.own) |old| p.gpa.free(old); + const tail = try e.gpa.dupe(u8, new[prefix.len..]); + if (t.own) |old| e.gpa.free(old); t.own = tail; return; } if (t.what != .body) { - if (t.own) |old| p.gpa.free(old); + if (t.own) |old| e.gpa.free(old); t.own = new; return; } - const pane = t.pane() orelse return retireEditText(p.editor(), new); + const pane = t.pane() orelse return retireEditText(e, new); // A live query pane's rows are its line 1's (Query.zig): an edit there // is refused, as a tag's prefix refuses one, and one from line 1 into // them is cut at its end. The rows' metadata stays theirs across an // edit of the line, which moves no row. if (pane.file) |*f| if (pane.query) |q| if (!q.frozen) { - const kept = panes.Query.clampEdit(p, t, f.content, new) orelse return retireEditText(p.editor(), new); - if (kept.ptr != new.ptr) retireEditText(p.editor(), new); + const kept = panes.Query.clampEdit(e.p, t, f.content, new) orelse return retireEditText(e, new); + if (kept.ptr != new.ptr) retireEditText(e, new); const end = modal.positionAt(kept, kept.len - panes.File.spanOf(f.content, kept).tail); t.last_edit = .{ .row = @intCast(end.row), .col = @intCast(end.col) }; const rows = f.location_rows; const spans = f.pager_spans; f.location_rows = &.{}; f.pager_spans = &.{}; - panes.File.setContentSpan(p, f, kept, null); + panes.File.setContentSpan(e.p, f, kept, null); f.location_rows = rows; f.pager_spans = spans; return; @@ -719,14 +719,14 @@ fn setEditTextSpan(p: *Pardes, t: *Text, new: []u8, known_end: ?modal.Cursor, sp const found = span orelse if (known_end == null or pane.file != null) panes.File.spanOf(old, new) else null; const end = known_end orelse modal.positionAt(new, new.len - found.?.tail); t.last_edit = .{ .row = @as(i32, @intCast(end.row)) + row0, .col = @intCast(end.col) }; - if (pane.file) |*f| return panes.File.setContentSpan(p, f, new, found); + if (pane.file) |*f| return panes.File.setContentSpan(e.p, f, new, found); // A terminal's edit copy rewritten: the text it replaces freed. - const overlay = if (pane.ovl) |*value| value else return retireEditText(p.editor(), new); + const overlay = if (pane.ovl) |*value| value else return retireEditText(e, new); for (0..pane.sel.len) |button| { pane.clearPointerSelection(button); pane.sel[button].state = .none; } - p.gpa.free(overlay.text); + e.gpa.free(overlay.text); overlay.text = new; } @@ -750,11 +750,11 @@ pub fn retireEditText(e: Editor, new: []u8) void { /// helix's indent for a line opened after byte `at` of `row` (indent.zig), /// from the file's tree, where its grammar has an indent query. -fn treeIndent(p: *Pardes, t: *Text, text: []const u8, row: usize, at: usize) Error!?[]const u8 { +fn treeIndent(e: Editor, t: *Text, text: []const u8, row: usize, at: usize) Error!?[]const u8 { const f = bodyFile(t) orelse return null; const l = indents.lang(f.path) orelse return null; - const tree = normal.fileTree(p, f) orelse return null; - return indents.forNewline(p.scratch.allocator(), l, tree, text, row, at); + const tree = normal.fileTree(e.p, f) orelse return null; + return indents.forNewline(e.scratch, l, tree, text, row, at); } /// helix's default auto-pairs (auto_pairs.rs DEFAULT_PAIRS): `o` opens one @@ -785,15 +785,15 @@ pub fn clampFileCursor(t: *Text) bool { pub fn enterInsert(p: *Pardes, t: *Text, where: InsertAt, cnt: usize) Error!void { if (t.what == .body) if (t.pane()) |pane| if (pane.addon != null) return; - pinPaneCursor(p, t); + pinPaneCursor(p.editor(), t); _ = clampFileCursor(t); // snapshot once per insert session (WITH the pre-insert selection) so // `u` undoes the whole session and restores what was selected - try pushUndo(p, t); + try pushUndo(p.editor(), t); t.select = false; t.restore_cursor = false; t.sticky_col = -1; - const text = flatSurface(p, t) catch { + const text = flatSurface(p.editor(), t) catch { t.mode = .insert; t.vsel.active = false; t.normal.clear(); @@ -826,7 +826,7 @@ pub fn enterInsert(p: *Pardes, t: *Text, where: InsertAt, cnt: usize) Error!void var now = text; if (hi < text.len and gap >= text.len and !terminal) { const grown = try std.mem.concat(p.gpa, u8, &.{ text, "\n" }); - try setEditText(p, t, grown); + try setEditText(p.editor(), t, grown); now = grown; // what setEditText keeps; `text` may be gone } const gc = modal.positionAt(now, gap); @@ -850,13 +850,13 @@ pub fn enterInsert(p: *Pardes, t: *Text, where: InsertAt, cnt: usize) Error!void t.vsel.active = false; const below = where == .open_below; const abs: i32 = if (b) |bb| (if (below) bb.hi_row else bb.lo_row) else t.cur_row; - const eb = (try editText(p, t, abs, abs, -1)) orelse return; + const eb = (try editText(p.editor(), t, abs, abs, -1)) orelse return; const row: usize = @intCast(@max(0, abs - eb.row0)); // helix open_below/open_above: the tree's indent for a line // after the one above the new one, else acme's copy of this one const above = if (below) row else row -| 1; const after = if (!below and row == 0) 0 else panes.File.textLineStart(t, eb.text, above) + panes.File.textLine(t, eb.text, above).len; - const ind = (try treeIndent(p, t, eb.text, above, after)) orelse modal.indentText(modal.lineSlice(eb.text, row)); + const ind = (try treeIndent(p.editor(), t, eb.text, above, after)) orelse modal.indentText(modal.lineSlice(eb.text, row)); const arena = p.scratch.allocator(); const block_len = std.math.mul(usize, cnt, ind.len + 1) catch return error.OutOfMemory; const block = try arena.alloc(u8, block_len); @@ -878,12 +878,12 @@ pub fn enterInsert(p: *Pardes, t: *Text, where: InsertAt, cnt: usize) Error!void else .{ .row = row, .col = 0 }; const new = try modal.insertAt(p.gpa, eb.text, at, block); - try setEditText(p, t, new); + try setEditText(p.editor(), t, new); t.cur_row = @as(i32, @intCast(if (below) row + 1 else row)) + eb.row0; t.cur_col = @intCast(ind.len); t.cur_pinned = true; if (cnt > 1 and p.multi_count == 1) opened: { - const t2 = flatSurface(p, t) catch break :opened; + const t2 = flatSurface(p.editor(), t) catch break :opened; var rs: [Text.max_selections]modal.Selection = undefined; const m = @min(cnt, Text.max_selections); for (0..m) |k| { @@ -923,12 +923,12 @@ pub fn insertKey(p: *Pardes, t: *Text, key: Key) Error!void { return; } // helix commit_undo_checkpoint: `u` after the session goes back to here - if (hit(key, config.insert_checkpoint)) return pushUndo(p, t); + if (hit(key, config.insert_checkpoint)) return pushUndo(p.editor(), t); // helix maps every range through each insert-mode edit (Range::map): // the edits below work on the bare cursor, and the range the cursor // heads is carried through what they did to the text afterwards if (!t.vsel.active) return insertEdit(p, t, key); - const old = flatSurface(p, t) catch return insertEdit(p, t, key); + const old = flatSurface(p.editor(), t) catch return insertEdit(p, t, key); const r = t.primaryRange(old, 0); const c = modal.selectionCursor(old, r); const old_len = old.len; @@ -936,7 +936,7 @@ pub fn insertKey(p: *Pardes, t: *Text, key: Key) Error!void { t.vsel.active = false; try insertEdit(p, t, key); if (t.mode != .insert) return; - const new = try flatSurface(p, t); + const new = try flatSurface(p.editor(), t); const c2 = modal.offsetAt(new, .{ .row = @intCast(@max(0, t.cur_row)), .col = @intCast(@max(0, t.cur_col)) }); // every insert-mode edit happens at the cursor: an insertion there, or // a deletion that ends there (backward) or starts there (forward) @@ -991,11 +991,11 @@ fn mapThroughEdit(pos: usize, at: usize, del: usize, ins: usize, before: bool) u fn insertEdit(p: *Pardes, t: *Text, key: Key) Error!void { t.vsel.active = false; - if (!t.cur_pinned) pinPaneCursor(p, t); + if (!t.cur_pinned) pinPaneCursor(p.editor(), t); _ = clampFileCursor(t); switch (key.cp) { Key.left, Key.right, Key.up, Key.down => { - const lines = try paneCursorLines(p, t); + const lines = try paneCursorLines(p.editor(), t); const cur0 = t.toModalCursor(); // In a pane's tag, Up on its first line collapses it and Down // on its last expands it, where they would move nothing; acme's @@ -1024,7 +1024,7 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) Error!void { Key.page_up, Key.page_down => { // helix binds insert pageup/pagedown to the same view // scroll + cursor snap as normal mode - const flat = try flatSurface(p, t); + const flat = try flatSurface(p.editor(), t); const range = t.primaryRange(flat, 0); if (t.what != .body) return; const step: i32 = @intCast(@max(1, (t.pane() orelse return).rows)); @@ -1039,7 +1039,7 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) Error!void { }, Key.end => { // helix insert End: past the last char (goto_line_end_newline) - const text = try flatSurface(p, t); + const text = try flatSurface(p.editor(), t); const cur0 = t.toModalCursor(); t.cur_col = @intCast(panes.File.textLine(t, text, cur0.row).len); t.cur_pinned = true; @@ -1048,7 +1048,7 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) Error!void { }, else => {}, } - const eb = (try editText(p, t, t.cur_row, t.cur_row, t.cur_col)) orelse return; + const eb = (try editText(p.editor(), t, t.cur_row, t.cur_row, t.cur_col)) orelse return; const text = eb.text; const c = modal.Cursor{ .row = @intCast(@max(0, t.cur_row - eb.row0)), .col = @intCast(@max(0, t.cur_col)) }; if (key.text.len > 0 and !key.ctrl and !key.alt) { @@ -1065,7 +1065,7 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) Error!void { t.last_edit = .{ .row = @intCast(end.row), .col = @intCast(end.col) }; } else { const new = try modal.spliceAlloc(p.gpa, text, at, at, key.text); - try setEditTextSpan(p, t, new, end, .{ .head = at, .tail = text.len - at }); + try setEditTextSpan(p.editor(), t, new, end, .{ .head = at, .tail = text.len - at }); } t.cur_row = @as(i32, @intCast(end.row)) + eb.row0; t.cur_col = @intCast(end.col); @@ -1076,7 +1076,7 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) Error!void { // helix insert-mode kills (word/line; deleteSpan is exclusive) if (hit(key, config.delete_word_backward)) { const e2 = if (c.row == 0 and c.col == 0) - ((try editText(p, t, t.cur_row - 1, t.cur_row, 0)) orelse return) + ((try editText(p.editor(), t, t.cur_row - 1, t.cur_row, 0)) orelse return) else eb; const c2 = modal.Cursor{ .row = @intCast(@max(0, t.cur_row - e2.row0)), .col = c.col }; @@ -1086,7 +1086,7 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) Error!void { const from = @min(wr.anchor, wr.head); const fc = modal.positionAt(e2.text, from); const new = try modal.deleteSpan(p.gpa, e2.text, fc, c2); - try setEditTextEnd(p, t, new, fc); + try setEditTextEnd(p.editor(), t, new, fc); t.cur_row = @as(i32, @intCast(fc.row)) + e2.row0; t.cur_col = @intCast(fc.col); t.cur_pinned = true; @@ -1095,7 +1095,7 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) Error!void { } if (hit(key, config.delete_word_forward)) { const e2 = if (c.col >= modal.lineSlice(text, c.row).len and c.row + 1 >= modal.lineCount(text)) - ((try editText(p, t, t.cur_row, t.cur_row + 1, t.cur_col)) orelse return) + ((try editText(p.editor(), t, t.cur_row, t.cur_row + 1, t.cur_col)) orelse return) else eb; const c2 = modal.Cursor{ .row = @intCast(@max(0, t.cur_row - e2.row0)), .col = c.col }; @@ -1104,13 +1104,13 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) Error!void { const to = @max(wr.anchor, wr.head); if (to <= g) return; const new = try modal.deleteSpan(p.gpa, e2.text, c2, modal.positionAt(e2.text, to)); - try setEditTextEnd(p, t, new, c2); + try setEditTextEnd(p.editor(), t, new, c2); t.cur_pinned = true; return; } if (hit(key, config.kill_to_line_start)) { const new = try modal.deleteSpan(p.gpa, text, .{ .row = c.row, .col = 0 }, c); - try setEditTextEnd(p, t, new, .{ .row = c.row, .col = 0 }); + try setEditTextEnd(p.editor(), t, new, .{ .row = c.row, .col = 0 }); t.cur_col = 0; t.cur_pinned = true; t.show(); @@ -1119,7 +1119,7 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) Error!void { if (hit(key, config.kill_to_line_end)) { const llen = modal.lineSlice(text, c.row).len; const new = try modal.deleteSpan(p.gpa, text, c, .{ .row = c.row, .col = llen }); - try setEditTextEnd(p, t, new, c); + try setEditTextEnd(p.editor(), t, new, c); t.cur_pinned = true; return; } @@ -1132,7 +1132,7 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) Error!void { // cursor go and the new line takes the indent if (std.mem.indexOfNone(u8, before, " \t") == null) { const new = try modal.insertAt(p.gpa, text, .{ .row = c.row, .col = 0 }, "\n"); - try setEditTextEnd(p, t, new, .{ .row = c.row + 1, .col = 0 }); + try setEditTextEnd(p.editor(), t, new, .{ .row = c.row + 1, .col = 0 }); t.cur_row += 1; t.cur_pinned = true; t.show(); @@ -1146,7 +1146,7 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) Error!void { // indent, the cursor to one a level in. const arena = p.scratch.allocator(); const lead = modal.indentText(line); - const tree = try treeIndent(p, t, text, c.row, panes.File.textLineStart(t, text, c.row) + before.len); + const tree = try treeIndent(p.editor(), t, text, c.row, panes.File.textLineStart(t, text, c.row) + before.len); const pair = bodyFile(t) != null and c.col < line.len and opensPair(before[before.len - 1], line[c.col]); const more = modal.newlineIndentWidth(line, c.col) > lead.len; const base = tree orelse if (pair or !more) lead else try std.mem.concat(arena, u8, &.{ lead, indents.unit(null, lead) }); @@ -1158,7 +1158,7 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) Error!void { const cut = try modal.deleteSpan(p.gpa, text, .{ .row = c.row, .col = kept }, c); defer p.gpa.free(cut); const new = try modal.insertAt(p.gpa, cut, .{ .row = c.row, .col = kept }, block); - try setEditTextEnd(p, t, new, if (pair) .{ .row = c.row + 2, .col = base.len } else .{ .row = c.row + 1, .col = mid.len }); + try setEditTextEnd(p.editor(), t, new, if (pair) .{ .row = c.row + 2, .col = base.len } else .{ .row = c.row + 1, .col = mid.len }); t.cur_row += 1; t.cur_col = @intCast(mid.len); t.cur_pinned = true; @@ -1191,7 +1191,7 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) Error!void { } else { const new = if (e > s) try modal.spliceAlloc(p.gpa, text, s, e, "") else try p.gpa.dupe(u8, text); const span: panes.File.Span = if (e > s) .{ .head = s, .tail = text.len - e } else .{ .head = text.len, .tail = 0 }; - try setEditTextSpan(p, t, new, .{ .row = c.row, .col = prev }, span); + try setEditTextSpan(p.editor(), t, new, .{ .row = c.row, .col = prev }, span); } t.cur_col = @intCast(prev); t.cur_pinned = true; @@ -1201,12 +1201,12 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) Error!void { // at col 0: eat the newline above (join up) — a terminal grows // its buffer over the row it is joining with if (t.cur_row <= 0) return; - const e2 = (try editText(p, t, t.cur_row - 1, t.cur_row, 0)) orelse return; + const e2 = (try editText(p.editor(), t, t.cur_row - 1, t.cur_row, 0)) orelse return; const row: usize = @intCast(@max(0, t.cur_row - e2.row0)); if (row == 0) return; const prevlen = modal.lineSlice(e2.text, row - 1).len; const new = try modal.deleteSpan(p.gpa, e2.text, .{ .row = row - 1, .col = prevlen }, .{ .row = row, .col = 0 }); - try setEditTextEnd(p, t, new, .{ .row = row - 1, .col = prevlen }); + try setEditTextEnd(p.editor(), t, new, .{ .row = row - 1, .col = prevlen }); t.cur_row -= 1; t.cur_col = @intCast(prevlen); t.cur_pinned = true; @@ -1219,13 +1219,13 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) Error!void { if (c.col < line.len) { const at = modal.offsetAt(text, c); const new = try modal.spliceAlloc(p.gpa, text, at, modal.nextGrapheme(text, at), ""); - try setEditTextEnd(p, t, new, c); + try setEditTextEnd(p.editor(), t, new, c); } else { - const e2 = (try editText(p, t, t.cur_row, t.cur_row + 1, t.cur_col)) orelse return; + const e2 = (try editText(p.editor(), t, t.cur_row, t.cur_row + 1, t.cur_col)) orelse return; const row: usize = @intCast(@max(0, t.cur_row - e2.row0)); if (row + 1 >= modal.lineCount(e2.text)) return; const new = try modal.deleteSpan(p.gpa, e2.text, .{ .row = row, .col = c.col }, .{ .row = row + 1, .col = 0 }); - try setEditTextEnd(p, t, new, .{ .row = row, .col = c.col }); + try setEditTextEnd(p.editor(), t, new, .{ .row = row, .col = c.col }); } t.cur_pinned = true; }, @@ -1257,14 +1257,14 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) Error!void { } pub fn insertTab(p: *Pardes, t: *Text) Error!void { - const eb = (try editText(p, t, t.cur_row, t.cur_row, t.cur_col)) orelse return; + const eb = (try editText(p.editor(), t, t.cur_row, t.cur_row, t.cur_col)) orelse return; const c: modal.Cursor = .{ .row = @intCast(@max(0, t.cur_row - eb.row0)), .col = @intCast(@max(0, t.cur_col)), }; const pad = modal.INDENT_W - (c.col % modal.INDENT_W); const new = try modal.insertAt(p.gpa, eb.text, c, " "[0..pad]); - try setEditTextEnd(p, t, new, .{ .row = c.row, .col = c.col + pad }); + try setEditTextEnd(p.editor(), t, new, .{ .row = c.row, .col = c.col + pad }); t.cur_col += @intCast(pad); t.cur_pinned = true; t.show(); @@ -1284,10 +1284,10 @@ fn vselBounds(t: *Text) Bounds { return cellBounds(.{ .row = t.cur_row, .col = t.cur_col, .arow = t.vsel.row, .acol = t.vsel.col }); } -fn vselText(p: *Pardes, t: *Text) Error![]const u8 { - const text = try flatSurface(p, t); +fn vselText(e: Editor, t: *Text) Error![]const u8 { + const text = try flatSurface(e, t); const r = t.primaryRange(text, 0); - return p.scratch.allocator().dupe(u8, text[@min(r.anchor, r.head)..@max(r.anchor, r.head)]); + return e.scratch.dupe(u8, text[@min(r.anchor, r.head)..@max(r.anchor, r.head)]); } pub fn normalDelete(p: *Pardes, t: *Text, yank: bool) Error!void { @@ -1297,14 +1297,14 @@ pub fn normalDelete(p: *Pardes, t: *Text, yank: bool) Error!void { // cell too, where the delete joins the lines. if (!t.vsel.active) t.vsel = .{ .active = true, .row = t.cur_row, .col = t.cur_col, .explicit = false }; const b = vselBounds(t); - if (yank) try setYank(p.editor(), try vselText(p, t)); // read the range before the move - try pushUndo(p, t); - const eb = (try editTextEol(p, t, b)) orelse { + if (yank) try setYank(p.editor(), try vselText(p.editor(), t)); // read the range before the move + try pushUndo(p.editor(), t); + const eb = (try editTextEol(p.editor(), t, b)) orelse { t.vsel.active = false; return; }; const new = try deleteSelection(p, t, eb); - try setEditText(p, t, new); + try setEditText(p.editor(), t, new); t.cur_row = b.lo_row; t.cur_col = b.lo_col; t.clampCursor(new, eb.row0); @@ -1316,12 +1316,12 @@ pub fn normalYank(p: *Pardes, t: *Text) Error!void { try setYank(p.editor(), text); return; }; - try setYank(p.editor(), try if (t.vsel.active) vselText(p, t) else selText(p, t)); + try setYank(p.editor(), try if (t.vsel.active) vselText(p.editor(), t) else selText(p.editor(), t)); } pub fn pasteText(p: *Pardes, t: *Text, y0: []const u8, before: bool, count: usize) Error!void { if (y0.len == 0) return; - try pushUndo(p, t); + try pushUndo(p.editor(), t); t.select = false; t.sticky_col = -1; const arena = p.scratch.allocator(); @@ -1336,7 +1336,7 @@ pub fn pasteText(p: *Pardes, t: *Text, y0: []const u8, before: bool, count: usiz vselBounds(t) else .{ .lo_row = t.cur_row, .lo_col = t.cur_col, .hi_row = t.cur_row, .hi_col = t.cur_col }; - const eb = (try editText(p, t, b.lo_row, b.hi_row, -1)) orelse return; + const eb = (try editText(p.editor(), t, b.lo_row, b.hi_row, -1)) orelse return; const row0 = eb.row0; if (y[y.len - 1] == '\n') { const block_text = y[0 .. y.len - 1]; @@ -1352,7 +1352,7 @@ pub fn pasteText(p: *Pardes, t: *Text, y0: []const u8, before: bool, count: usiz out = try modal.pasteLineBelow(p.gpa, eb.text, row, block_text); t.cur_row = @as(i32, @intCast(row + n)) + row0; } - try setEditText(p, t, out); + try setEditText(p.editor(), t, out); t.vsel = .{ .active = true, .row = t.cur_row - @as(i32, @intCast(n - 1)), .col = 0, .explicit = false }; // cursor ON the last pasted line's '\n' (helix) const llen = modal.lineSlice(out, @intCast(@max(0, t.cur_row - row0))).len; @@ -1369,7 +1369,7 @@ pub fn pasteText(p: *Pardes, t: *Text, y0: []const u8, before: bool, count: usiz break :blk .{ .row = hrow, .col = gcol }; }; const out = try modal.insertAt(p.gpa, eb.text, at, y); - try setEditText(p, t, out); + try setEditText(p.editor(), t, out); t.vsel = .{ .active = modal.nextGrapheme(y, 0) < y.len, .row = @as(i32, @intCast(at.row)) + row0, .col = @intCast(at.col), .explicit = false }; const end = modal.advanceBy(at, y); if (end.col > 0) { @@ -1389,20 +1389,20 @@ pub fn normalChange(p: *Pardes, t: *Text) Error!void { t.sticky_col = -1; if (t.vsel.active) { const b = vselBounds(t); - const eb = (try editTextEol(p, t, b)) orelse return; + const eb = (try editTextEol(p.editor(), t, b)) orelse return; const hi_len = modal.lineSlice(eb.text, @intCast(@max(0, b.hi_row - eb.row0))).len; if (b.lo_col == 0 and @as(usize, @intCast(@max(0, b.hi_col))) >= hi_len) { - try setYank(p.editor(), try vselText(p, t)); - try pushUndo(p, t); + try setYank(p.editor(), try vselText(p.editor(), t)); + try pushUndo(p.editor(), t); const d = try modal.deleteLines(p.gpa, eb.text, @intCast(@max(0, b.lo_row - eb.row0)), @intCast(@max(0, b.hi_row - eb.row0))); - try setEditText(p, t, d); + try setEditText(p.editor(), t, d); const n = modal.lineCount(d); const row: usize = @min(@as(usize, @intCast(@max(0, b.lo_row - eb.row0))), if (n == 0) 0 else n - 1); const ind = modal.indentText(modal.lineSlice(d, row)); const arena = p.scratch.allocator(); const block = try std.fmt.allocPrint(arena, "{s}\n", .{ind}); const new = try modal.insertAt(p.gpa, d, .{ .row = row, .col = 0 }, block); - try setEditText(p, t, new); + try setEditText(p.editor(), t, new); t.cur_row = @as(i32, @intCast(row)) + eb.row0; t.cur_col = @intCast(ind.len); t.vsel.active = false; @@ -1412,10 +1412,10 @@ pub fn normalChange(p: *Pardes, t: *Text) Error!void { t.show(); return; } - try setYank(p.editor(), try vselText(p, t)); - try pushUndo(p, t); + try setYank(p.editor(), try vselText(p.editor(), t)); + try pushUndo(p.editor(), t); const new = try deleteSelection(p, t, eb); - try setEditText(p, t, new); + try setEditText(p.editor(), t, new); t.cur_row = b.lo_row; t.cur_col = b.lo_col; t.clampCursor(new, eb.row0); @@ -1423,12 +1423,12 @@ pub fn normalChange(p: *Pardes, t: *Text) Error!void { t.normal.clear(); return; } - try pushUndo(p, t); - const eb = (try editTextEol(p, t, selRows(t))) orelse return; + try pushUndo(p.editor(), t); + const eb = (try editTextEol(p.editor(), t, selRows(t))) orelse return; const r = t.primaryRange(eb.text, eb.row0); try setYank(p.editor(), eb.text[r.anchor..r.head]); const new = try modal.spliceAlloc(p.gpa, eb.text, r.anchor, r.head, ""); - try setEditText(p, t, new); + try setEditText(p.editor(), t, new); const llen = modal.lineSlice(new, @intCast(@max(0, t.cur_row - eb.row0))).len; t.cur_col = @min(t.cur_col, @as(i32, @intCast(llen))); t.mode = .insert; @@ -1467,8 +1467,8 @@ fn setEditCells(p: *Pardes, t: *Text, new: []u8, row0: i32, cells: []const Text. const ac = modal.positionAt(new, c.anc); s.* = .{ .row = @as(i32, @intCast(cc.row)) + row0, .col = @intCast(cc.col), .arow = @as(i32, @intCast(ac.row)) + row0, .acol = @intCast(ac.col) }; } - try setEditText(p, t, new); - const text = try flatSurface(p, t); + try setEditText(p.editor(), t, new); + const text = try flatSurface(p.editor(), t); var rs: [Text.max_selections]modal.Selection = undefined; for (sels[0..cells.len], rs[0..cells.len]) |s, *r| r.* = Text.cellRange(text, s.arow, s.acol, s.row, s.col); t.setRanges(text, rs[0..cells.len], &.{}, pri, expl); @@ -1478,7 +1478,7 @@ fn setEditCells(p: *Pardes, t: *Text, new: []u8, row0: i32, cells: []const Text. /// newlines included (helix), so `xrz` joins the selected lines pub fn normalReplaceChar(p: *Pardes, t: *Text, ch: u21) Error!void { t.select = false; - const eb = (try editTextEol(p, t, selRows(t))) orelse return; + const eb = (try editTextEol(p.editor(), t, selRows(t))) orelse return; const before = t.primaryRange(eb.text, eb.row0); const lo = @min(before.anchor, before.head); const hi = @max(before.anchor, before.head); @@ -1489,9 +1489,9 @@ pub fn normalReplaceChar(p: *Pardes, t: *Text, ch: u21) Error!void { const encoded_len = std.unicode.utf8Encode(ch, &encoded) catch return; // a surrogate half types nothing const rep = try p.scratch.allocator().alloc(u8, graphemes * encoded_len); for (0..graphemes) |i| @memcpy(rep[i * encoded_len ..][0..encoded_len], encoded[0..encoded_len]); - try pushUndo(p, t); + try pushUndo(p.editor(), t); const new = try modal.spliceAlloc(p.gpa, eb.text, lo, hi, rep); - try setEditText(p, t, new); + try setEditText(p.editor(), t, new); const end = lo + rep.len; const mapped: modal.Selection = if (before.head < before.anchor) .{ .anchor = end, .head = lo } @@ -1513,12 +1513,12 @@ pub fn chordReplace(p: *Pardes, t: *Text) Error!void { fn replaceWithText(p: *Pardes, t: *Text, y: []const u8) Error!void { if (y.len == 0) return; t.select = false; - const eb = (try editTextEol(p, t, selRows(t))) orelse return; + const eb = (try editTextEol(p.editor(), t, selRows(t))) orelse return; const r = t.primaryRange(eb.text, eb.row0); const lo = @min(r.anchor, r.head); - try pushUndo(p, t); + try pushUndo(p.editor(), t); const new = try modal.spliceAlloc(p.gpa, eb.text, lo, @max(r.anchor, r.head), y); - try setEditText(p, t, new); + try setEditText(p.editor(), t, new); const end = lo + y.len; t.setRange(new, eb.row0, if (r.head < r.anchor) .{ .anchor = end, .head = lo } else .{ .anchor = lo, .head = end }, false); } @@ -1527,11 +1527,11 @@ fn replaceWithText(p: *Pardes, t: *Text, y: []const u8) Error!void { /// keeping the selection (helix) pub fn normalCase(p: *Pardes, t: *Text, op: modal.CaseOp) Error!void { t.select = false; - const eb = (try editTextEol(p, t, selRows(t))) orelse return; + const eb = (try editTextEol(p.editor(), t, selRows(t))) orelse return; const r = t.primaryRange(eb.text, eb.row0); - try pushUndo(p, t); + try pushUndo(p.editor(), t); const new = try modal.changeCase(p.gpa, eb.text, @min(r.anchor, r.head), @max(r.anchor, r.head), op); - try setEditText(p, t, new); + try setEditText(p.editor(), t, new); } /// Where `pos` lands after `chs`, which are ascending and disjoint. @@ -1559,7 +1559,7 @@ pub fn normalJoin(p: *Pardes, t: *Text, select_space: bool) Error!void { // a join always eats the newline of its last line, so the buffer has // to reach one row PAST every range const b = allSelRows(t); - const eb = (try editText(p, t, b.lo_row, b.hi_row + 1, -1)) orelse return; + const eb = (try editText(p.editor(), t, b.lo_row, b.hi_row + 1, -1)) orelse return; const text = eb.text; var rs: [Text.max_selections]modal.Selection = undefined; const got = t.ranges(text, eb.row0, &rs); @@ -1590,7 +1590,7 @@ pub fn normalJoin(p: *Pardes, t: *Text, select_space: bool) Error!void { const chs = changes.items; const chs_len = chs.len; if (chs_len == 0) return; - try pushUndo(p, t); + try pushUndo(p.editor(), t); const new = try sam.apply(p.gpa, text, chs[0..chs_len]); // every range's cells ride through the edit; Alt-J replaces them with // the spaces, when it put any in @@ -1625,7 +1625,7 @@ pub fn normalJoin(p: *Pardes, t: *Text, select_space: bool) Error!void { /// and the primary moves with the text it had. pub fn normalRotateContents(p: *Pardes, t: *Text, forward: bool, cnt: usize) Error!void { const expl = t.vsel.active and t.vsel.explicit; - const eb = (try editTextEol(p, t, allSelRows(t))) orelse return; + const eb = (try editTextEol(p.editor(), t, allSelRows(t))) orelse return; const text = eb.text; var rs: [Text.max_selections]modal.Selection = undefined; const got = t.ranges(text, eb.row0, &rs); @@ -1641,7 +1641,7 @@ pub fn normalRotateContents(p: *Pardes, t: *Text, forward: bool, cnt: usize) Err .text = text[@min(src.anchor, src.head)..@max(src.anchor, src.head)], }; } - try pushUndo(p, t); + try pushUndo(p.editor(), t); const new = try sam.apply(p.gpa, text, chs[0..n]); var cells: [Text.max_selections]Text.Cells = undefined; var delta: i64 = 0; @@ -1660,7 +1660,7 @@ pub fn normalRotateContents(p: *Pardes, t: *Text, forward: bool, cnt: usize) Err /// in display cells. A range over several lines aligns nothing. pub fn normalAlign(p: *Pardes, t: *Text) Error!void { const expl = t.vsel.active and t.vsel.explicit; - const eb = (try editTextEol(p, t, allSelRows(t))) orelse return; + const eb = (try editTextEol(p.editor(), t, allSelRows(t))) orelse return; const text = eb.text; var rs: [Text.max_selections]modal.Selection = undefined; const got = t.ranges(text, eb.row0, &rs); @@ -1709,7 +1709,7 @@ pub fn normalAlign(p: *Pardes, t: *Text) Error!void { m += 1; } if (m == 0) return; - try pushUndo(p, t); + try pushUndo(p.editor(), t); const new = try sam.apply(p.gpa, text, chs[0..m]); var cells: [Text.max_selections]Text.Cells = undefined; for (rs[0..n], cells[0..n]) |r, *cell| { @@ -1722,7 +1722,7 @@ pub fn normalAlign(p: *Pardes, t: *Text) Error!void { pub fn normalIndent(p: *Pardes, t: *Text, cnt: usize, add: bool) Error!void { t.select = false; const sr = selRows(t); - const eb = (try editText(p, t, sr.lo_row, sr.hi_row, -1)) orelse return; + const eb = (try editText(p.editor(), t, sr.lo_row, sr.hi_row, -1)) orelse return; const text = eb.text; const range = t.primaryRange(text, eb.row0); const span = Text.rangeLineSpan(text, range); @@ -1763,7 +1763,7 @@ pub fn normalIndent(p: *Pardes, t: *Text, cnt: usize, add: bool) Error!void { } } if (chs_len == 0) return; - try pushUndo(p, t); + try pushUndo(p.editor(), t); const cur_off = modal.offsetAt(text, .{ .row = @intCast(@max(0, t.cur_row - eb.row0)), .col = @intCast(@max(0, t.cur_col)) }); const anc_off = if (t.vsel.active) modal.offsetAt(text, .{ .row = @intCast(@max(0, t.vsel.row - eb.row0)), .col = @intCast(@max(0, t.vsel.col)) }) @@ -1772,7 +1772,7 @@ pub fn normalIndent(p: *Pardes, t: *Text, cnt: usize, add: bool) Error!void { const new = try sam.apply(p.gpa, text, chs[0..chs_len]); const nc = mapThroughChanges(chs[0..chs_len], cur_off); const na = mapThroughChanges(chs[0..chs_len], anc_off); - try setEditText(p, t, new); + try setEditText(p.editor(), t, new); const cc = modal.positionAt(new, nc); const ac = modal.positionAt(new, na); t.cur_row = @as(i32, @intCast(cc.row)) + eb.row0; @@ -1791,9 +1791,9 @@ pub fn normalToggleComment(p: *Pardes, t: *Text) Error!void { t.select = false; // helix exit_select_mode // the buffer must cover every cursor's rows, not just the primary's const b = allSelRows(t); - const surface = try flatSurface(p, t); + const surface = try flatSurface(p.editor(), t); const last_row = @as(i32, @intCast(panes.File.textLineCount(t, surface))) - 1; - const eb = (try editText(p, t, b.lo_row, @min(b.hi_row + 1, last_row), -1)) orelse return; + const eb = (try editText(p.editor(), t, b.lo_row, @min(b.hi_row + 1, last_row), -1)) orelse return; const text = eb.text; var rs: [Text.max_selections]modal.Selection = undefined; const got = t.ranges(text, eb.row0, &rs); @@ -1854,7 +1854,7 @@ pub fn normalToggleComment(p: *Pardes, t: *Text) Error!void { .{ .q0 = @intCast(at), .q1 = @intCast(at), .text = ins }; chs_len += 1; } - try pushUndo(p, t); + try pushUndo(p.editor(), t); // one edit, and the WHOLE selection rides through it (helix maps the // selection with the transaction) var cells: [Text.max_selections]Text.Cells = undefined; @@ -1867,15 +1867,15 @@ pub fn normalToggleComment(p: *Pardes, t: *Text) Error!void { } pub fn normalAdjustNumber(p: *Pardes, t: *Text, delta: i64) Error!void { - const eb = (try editTextEol(p, t, selRows(t))) orelse return; + const eb = (try editTextEol(p.editor(), t, selRows(t))) orelse return; const r = t.primaryRange(eb.text, eb.row0); const lo = @min(r.anchor, r.head); const hi = @max(r.anchor, r.head); const rep = (modal.incrementDecimal(p.scratch.allocator(), eb.text[lo..hi], delta) catch null) orelse return; - try pushUndo(p, t); + try pushUndo(p.editor(), t); t.select = false; const new = try modal.spliceAlloc(p.gpa, eb.text, lo, hi, rep); - try setEditText(p, t, new); + try setEditText(p.editor(), t, new); t.setRange(new, eb.row0, .{ .anchor = lo, .head = lo + rep.len }, false); } @@ -1885,15 +1885,15 @@ pub fn addNewline(p: *Pardes, t: *Text, below: bool, cnt: usize) Error!void { // above the selection's first line, below its last (helix line_range) const sr = selRows(t); const at = if (below) sr.hi_row else sr.lo_row; - const eb = (try editText(p, t, at, at, -1)) orelse return; - try pushUndo(p, t); + const eb = (try editText(p.editor(), t, at, at, -1)) orelse return; + try pushUndo(p.editor(), t); const row: usize = @intCast(@max(0, at - eb.row0)); const arena = p.scratch.allocator(); const nl = try arena.alloc(u8, cnt); @memset(nl, '\n'); const llen = modal.lineSlice(eb.text, row).len; const new = try modal.insertAt(p.gpa, eb.text, .{ .row = row, .col = if (below) llen else 0 }, nl); - try setEditText(p, t, new); + try setEditText(p.editor(), t, new); if (!below) { // the whole selection moved down; stay on it const n: i32 = @intCast(cnt); @@ -1948,7 +1948,7 @@ pub fn textobjectSelect(p: *Pardes, t: *Text, text: []const u8, obj: u21, around }, else => {}, } - const lines = try paneCursorLines(p, t); + const lines = try paneCursorLines(p.editor(), t); const cur = modal.positionAt(text, modal.selectionCursor(text, range)); const pair: ?modal.Range = switch (obj) { '\'', '"', '`' => modal.enclosingQuote(lines, cur, @intCast(obj)), @@ -1975,15 +1975,15 @@ pub fn textobjectSelect(p: *Pardes, t: *Text, text: []const u8, obj: u21, around pub fn surroundAdd(p: *Pardes, t: *Text, ch: u21) Error!void { const pr = pairFor(ch) orelse return; t.select = false; - const eb = (try editTextEol(p, t, selRows(t))) orelse return; + const eb = (try editTextEol(p.editor(), t, selRows(t))) orelse return; const r = t.primaryRange(eb.text, eb.row0); const lo = @min(r.anchor, r.head); const hi = @max(r.anchor, r.head); - try pushUndo(p, t); + try pushUndo(p.editor(), t); const open = [1]u8{pr.o}; const close = [1]u8{pr.c}; const new = try sam.apply(p.gpa, eb.text, &.{ .{ .q0 = @intCast(lo), .q1 = @intCast(lo), .text = &open }, .{ .q0 = @intCast(hi), .q1 = @intCast(hi), .text = &close } }); - try setEditText(p, t, new); + try setEditText(p.editor(), t, new); // the wrapped range keeps the direction it had (helix with_direction) t.setRange(new, eb.row0, if (r.head < r.anchor) .{ .anchor = hi + 2, .head = lo } else .{ .anchor = lo, .head = hi + 2 }, false); } @@ -1991,7 +1991,7 @@ pub fn surroundAdd(p: *Pardes, t: *Text, ch: u21) Error!void { /// `md`: delete the enclosing pair's chars; the cursor maps through pub fn surroundDelete(p: *Pardes, t: *Text, ch: u21) Error!void { const pr = pairFor(ch) orelse return; - const lines = try paneCursorLines(p, t); + const lines = try paneCursorLines(p.editor(), t); const cur = t.toModalCursor(); const r = (if (pr.o == pr.c) modal.enclosingQuote(lines, cur, pr.o) @@ -1999,12 +1999,12 @@ pub fn surroundDelete(p: *Pardes, t: *Text, ch: u21) Error!void { modal.enclosingPair(lines, cur, pr.o, pr.c)) orelse return; const lo: i32 = @intCast(@min(r.a.row, cur.row)); const hi: i32 = @intCast(@max(r.b.row, cur.row)); - const eb = (try editText(p, t, lo, hi, -1)) orelse return; + const eb = (try editText(p.editor(), t, lo, hi, -1)) orelse return; const text = eb.text; const drow = -eb.row0; // surface row -> buffer row const ra: modal.Cursor = .{ .row = @intCast(@as(i32, @intCast(r.a.row)) + drow), .col = r.a.col }; const rb: modal.Cursor = .{ .row = @intCast(@as(i32, @intCast(r.b.row)) + drow), .col = r.b.col }; - try pushUndo(p, t); + try pushUndo(p.editor(), t); const a_off = modal.offsetAt(text, ra); const b_off = modal.offsetAt(text, rb); const cur_off = modal.offsetAt(text, .{ .row = @intCast(@as(i32, @intCast(cur.row)) + drow), .col = cur.col }); @@ -2012,7 +2012,7 @@ pub fn surroundDelete(p: *Pardes, t: *Text, ch: u21) Error!void { .{ .q0 = @intCast(a_off), .q1 = @intCast(modal.nextGrapheme(text, a_off)), .text = "" }, .{ .q0 = @intCast(b_off), .q1 = @intCast(modal.nextGrapheme(text, b_off)), .text = "" }, }); - try setEditText(p, t, new); + try setEditText(p.editor(), t, new); var nc = cur_off; if (nc > b_off) nc -= 1; if (nc > a_off) nc -= 1; @@ -2028,57 +2028,57 @@ pub fn surroundDelete(p: *Pardes, t: *Text, ch: u21) Error!void { pub fn surroundReplace(p: *Pardes, t: *Text, from: u21, to: u21) Error!void { const fp = pairFor(from) orelse return; const tp = pairFor(to) orelse return; - const lines = try paneCursorLines(p, t); + const lines = try paneCursorLines(p.editor(), t); const cur = t.toModalCursor(); const r = (if (fp.o == fp.c) modal.enclosingQuote(lines, cur, fp.o) else modal.enclosingPair(lines, cur, fp.o, fp.c)) orelse return; - const eb = (try editText(p, t, @as(i32, @intCast(r.a.row)), @as(i32, @intCast(r.b.row)), -1)) orelse return; + const eb = (try editText(p.editor(), t, @as(i32, @intCast(r.a.row)), @as(i32, @intCast(r.b.row)), -1)) orelse return; const drow = -eb.row0; // surface row -> buffer row const ar: usize = @intCast(@as(i32, @intCast(r.a.row)) + drow); const br: usize = @intCast(@as(i32, @intCast(r.b.row)) + drow); - try pushUndo(p, t); + try pushUndo(p.editor(), t); const out = try p.gpa.dupe(u8, eb.text); out[modal.lineStartOffset(out, ar) + r.a.col] = tp.o; out[modal.lineStartOffset(out, br) + r.b.col] = tp.c; - try setEditText(p, t, out); + try setEditText(p.editor(), t, out); } -pub fn pushUndo(p: *Pardes, t: *Text) error{OutOfMemory}!void { +pub fn pushUndo(e: Editor, t: *Text) error{OutOfMemory}!void { // one keystroke, one undo step — even when it edited at ten cursors. - if (!p.multi_first) return; + if (!e.multi_first.*) return; switch (t.what) { // A B1 chord took its one undo point when it began (mouse.zig). - .body => if (p.view().chord_hold) return, - .tag, .column, .workspace => return t.remember(p.gpa, if (t.own) |own| .{ .text = own } else null), + .body => if (e.p.view().chord_hold) return, + .tag, .column, .workspace => return t.remember(e.gpa, if (t.own) |own| .{ .text = own } else null), // a prompt's answer is typed once and gone; it keeps no history .input => return, } const pane = t.pane() orelse return; - if (pane.file != null) return panes.File.pushUndo(p, pane); - return pane.body.remember(p.gpa, pane.ovl); + if (pane.file != null) return panes.File.pushUndo(e.p, pane); + return pane.body.remember(e.gpa, pane.ovl); } /// Undo (back) or redo one step of the text's history: its file's, its /// terminal overlay's, or a tag's or header's own. -pub fn undoStep(p: *Pardes, t: *Text, back: bool) error{OutOfMemory}!void { - if (t.what == .tag or t.what == .column or t.what == .workspace) return stepOwn(p, t, back); +pub fn undoStep(e: Editor, t: *Text, back: bool) error{OutOfMemory}!void { + if (t.what == .tag or t.what == .column or t.what == .workspace) return stepOwn(e, t, back); if (t.what != .body) return; const pane = t.pane() orelse return; - if (pane.file != null) return panes.File.undoStep(p, pane, back); + if (pane.file != null) return panes.File.undoStep(e.p, pane, back); // A terminal's edit copy, as it was. - const to = (try pane.body.step(p.gpa, pane.ovl, back)) orelse return; - if (pane.ovl) |o| p.gpa.free(o.text); + const to = (try pane.body.step(e.gpa, pane.ovl, back)) orelse return; + if (pane.ovl) |o| e.gpa.free(o.text); pane.ovl = to.ovl; pane.ensureCursorVisible(); } /// Undo or redo an edit of a text's own characters; stepping back past its /// first edit gives a tag its default again. -fn stepOwn(p: *Pardes, t: *Text, back: bool) error{OutOfMemory}!void { - const to = (try t.step(p.gpa, if (t.own) |own| .{ .text = own } else null, back)) orelse return; - if (t.own) |own| p.gpa.free(own); +fn stepOwn(e: Editor, t: *Text, back: bool) error{OutOfMemory}!void { + const to = (try t.step(e.gpa, if (t.own) |own| .{ .text = own } else null, back)) orelse return; + if (t.own) |own| e.gpa.free(own); t.own = if (to.ovl) |buffer| buffer.text else null; } @@ -2330,10 +2330,10 @@ test "flat text replacement preserves a terminal fragment origin and direction" try std.testing.expectEqual(@as(i32, 3), pane.body.vsel.row); try std.testing.expectEqual(@as(i32, 2), pane.body.vsel.col); try std.testing.expect(pane.body.vsel.active and pane.body.vsel.explicit); - const lines = try paneCursorLines(p, &pane.body); + const lines = try paneCursorLines(p.editor(), &pane.body); try std.testing.expectEqualStrings("first", lines[0]); try std.testing.expectEqualStrings("λλ", lines[3]); - const flat = try flatSurface(p, &pane.body); + const flat = try flatSurface(p.editor(), &pane.body); try std.testing.expect(std.mem.startsWith(u8, flat, "first\nsecond\nthird\nλλ\n")); } @@ -2430,10 +2430,10 @@ test "a typed edit's known end is where the texts' comparison would find it" { const base = try p.gpa.dupe(u8, pane.file.?.content); defer p.gpa.free(base); const scanned = try modal.insertAt(p.gpa, base, c.at, c.s); - try setEditTextEnd(p, t, scanned, null); + try setEditTextEnd(p.editor(), t, scanned, null); const by_scan = t.last_edit.?; const known = try modal.insertAt(p.gpa, base, c.at, c.s); - try setEditTextEnd(p, t, known, modal.advanceBy(c.at, c.s)); + try setEditTextEnd(p.editor(), t, known, modal.advanceBy(c.at, c.s)); try std.testing.expectEqual(by_scan.row, t.last_edit.?.row); try std.testing.expectEqual(by_scan.col, t.last_edit.?.col); } diff --git a/src/exec.zig b/src/exec.zig index 4ab5a37f..f2fb30d4 100644 --- a/src/exec.zig +++ b/src/exec.zig @@ -32,7 +32,7 @@ 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, &pane.body) catch 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; @@ -118,7 +118,7 @@ pub fn operandAt(p: *Pardes, pane: *Pane, clicked: Pane.Sel, borrow: bool, for_l @as(i32, @intCast(panes.File.rawAtDisplay(raw_line, @intCast(@max(0, clicked.c0))))) else if (clicked.r0 >= pane.tag_rows) edit.paneByteAtDisplay( - p, + p.editor(), &pane.body, wrapped.line, wrapped.at, diff --git a/src/keys.zig b/src/keys.zig index 413818ed..8a9006dc 100644 --- a/src/keys.zig +++ b/src/keys.zig @@ -381,7 +381,7 @@ pub fn handleKey(p: *Pardes, key: Key) void { if (t.what != .body) { // An explicit selection, else the word under the cursor in the // text as it is shown: a tag's prefix is part of what a word is. - const shown = p.scratch.allocator().dupe(u8, edit.flatSurface(p, t) catch |err| return p.reportError(p.view().active, "exec", err)) catch |err| return p.reportError(p.view().active, "exec", err); + const shown = p.scratch.allocator().dupe(u8, edit.flatSurface(p.editor(), t) catch |err| return p.reportError(p.view().active, "exec", err)) catch |err| return p.reportError(p.view().active, "exec", err); const range = t.primaryRange(shown, 0); const lo = @min(range.anchor, range.head); const hi = @max(range.anchor, range.head); @@ -408,7 +408,7 @@ pub fn handleKey(p: *Pardes, key: Key) void { exec.runBuiltin(p, cmd, p.view().active, "", txt); return; } - edit.pinPaneCursor(p, &pane.body); + edit.pinPaneCursor(p.editor(), &pane.body); const explicit = (if (pane.addon) |k| k.selection.len > 0 else false) or (pane.body.vsel.active and pane.body.vsel.explicit); if (explicit) { @@ -826,7 +826,7 @@ test "s in a header selects in the header's own text" { p.update(.{ .key = .{ .cp = Key.enter } }); try std.testing.expect(p.view().header != null and pane.prompt == .none); var ranges: [Text.max_selections]modal.Selection = undefined; - const text = try edit.flatSurface(p, &p.global_tag); + const text = try edit.flatSurface(p.editor(), &p.global_tag); const got = p.global_tag.ranges(text, 0, &ranges); try std.testing.expectEqual(@as(usize, 2), got.n); // Newcol, Joincol try std.testing.expectEqual(@as(u16, 0), pane.body.nsel); diff --git a/src/look.zig b/src/look.zig index 136a7a9c..62f6a7da 100644 --- a/src/look.zig +++ b/src/look.zig @@ -296,7 +296,7 @@ pub fn cursorWordSel(p: *Pardes, pane: *Pane) Pane.Sel { const vcol = if (pane.file != null) w.pad + panes.File.displayOffset(pane, pane.body.cur_row, w.at, pane.body.cur_col) + @as(i32, panes.File.gutterWidth(pane)) else blk: { - const lines = edit.paneCursorLines(p, &pane.body) catch break :blk pane.body.cur_col; + const lines = edit.paneCursorLines(p.editor(), &pane.body) catch break :blk pane.body.cur_col; const local = pane.body.cur_row; if (local < 0 or @as(usize, @intCast(local)) >= lines.len) break :blk pane.body.cur_col; break :blk panes.File.lineDisplayOffset(lines[@intCast(local)], @intCast(@max(0, w.at)), @intCast(@max(0, pane.body.cur_col))); @@ -372,7 +372,7 @@ pub fn runSearch(p: *Pardes, id: usize, pat_raw: []const u8, start: SearchStart, @memcpy(out[0..out_len], rows[0..out_len]); nrows = std.mem.count(u8, rows, "\n"); } else { - const lines = try edit.paneCursorLines(p, &pane.body); + const lines = try edit.paneCursorLines(p.editor(), &pane.body); // a real file names itself; a terminal or an output buffer has no path const has_path = if (pane.file) |f| f.output == null else false; var idbuf: [16]u8 = undefined; @@ -547,7 +547,7 @@ pub fn wordSpan(p: ?*pardes.Pardes, word: []const u8, cwd: []const u8, realbuf: } fn lookSpanIn(p: *Pardes, pane: *Pane, from: LookFrom, delta: i32, budget: *usize) ?Pane.LookSpot { - const lines = edit.paneCursorLines(p, &pane.body) catch return null; + const lines = edit.paneCursorLines(p.editor(), &pane.body) catch return null; const nrows: i32 = @intCast(lines.len); if (nrows == 0) return null; const grain: panes.Output.Grain = if (pane.file) |*f| panes.Output.grain(f.output) else .word; @@ -636,7 +636,7 @@ pub fn lookWalk(p: *Pardes, delta: i32) void { } fn landLookSpot(p: *Pardes, id: usize, pane: *Pane, spot: Pane.LookSpot) void { - edit.pinPaneCursor(p, &pane.body); // fresh out of tty mode the cursor still tracks the shell + edit.pinPaneCursor(p.editor(), &pane.body); // fresh out of tty mode the cursor still tracks the shell pane.body.vsel = .{ .active = true, .row = spot.row, .col = spot.col1, .explicit = true }; pane.body.nsel = 0; pane.body.cur_row = spot.row; @@ -751,7 +751,7 @@ pub fn focusPaneLine(p: *Pardes, id: usize, at: Spot, landing: enum { center, ke } fn selectSpan(p: *Pardes, pane: *Pane, at: Spot) void { - const text = edit.flatSurface(p, &pane.body) catch |err| return p.reportError(p.view().active, "look", err); + const text = edit.flatSurface(p.editor(), &pane.body) catch |err| return p.reportError(p.view().active, "look", err); // saturating, not `- 1`: `f.zig:0-5` is a legal thing to type and a // 1-based zero is the same nothing an absent number is const acol: i32 = @intCast(at.col -| 1); @@ -1185,7 +1185,7 @@ fn closeAndMiss(p: *Pardes, id: usize, opened: usize, name: []const u8, why: []c fn lookTerminalAddress(p: *Pardes, id: usize, target: usize, expr: []const u8) void { const Addr = @import("ninep/addr.zig").Addr; const pane = p.panes[target].?; - const lines = edit.paneCursorLines(p, &pane.body) catch return missed(p, id, "{s}", .{"out of memory"}); + const lines = edit.paneCursorLines(p.editor(), &pane.body) catch return missed(p, id, "{s}", .{"out of memory"}); var text: std.ArrayList(u8) = .empty; const arena = p.scratch.allocator(); const row: usize = @intCast(@max(0, pane.body.cur_row)); @@ -1490,7 +1490,7 @@ pub fn lookAt(p: *Pardes, id: usize, operand_raw: []const u8) void { if (found == .pane and found.pane.id < MAX_PANES) if (p.panes[found.pane.id]) |shown| { // A terminal's lines are its logical lines, as a +Search lists them. const n = if (shown.file) |*f| panes.File.nlines(p.gpa, f) else if (shown.isTerminal()) - if (edit.paneCursorLines(p, &shown.body)) |ls| ls.len else |_| std.math.maxInt(usize) + if (edit.paneCursorLines(p.editor(), &shown.body)) |ls| ls.len else |_| std.math.maxInt(usize) else std.math.maxInt(usize); if (pl.at.line > n) return missedLine(p, id, trimmed, pl.at.line); @@ -2515,7 +2515,7 @@ test "LookNext in a command pane walks its own file:line places, whoever walked exec.runBuiltin(p, .LookNext, p.view().active, "", null); try std.testing.expectEqual(dst, p.view().active); const first = cmd.look_at orelse return error.NoSpot; - const lines = try edit.paneCursorLines(p, &cmd.body); + const lines = try edit.paneCursorLines(p.editor(), &cmd.body); try std.testing.expect(std.mem.startsWith(u8, lines[@intCast(first.row)][@intCast(first.col0)..], "x.c:3:1")); exec.runBuiltin(p, .LookNext, p.view().active, "", null); try std.testing.expectEqual(dst, p.view().active); diff --git a/src/lsp/Ask.zig b/src/lsp/Ask.zig index 879c1f5d..da03c021 100644 --- a/src/lsp/Ask.zig +++ b/src/lsp/Ask.zig @@ -224,7 +224,7 @@ fn applyLspEdits(p: *Pardes, pane: *Pane, revision: u32, edits: []const LspEdit) } @memcpy(replacement[write_at..], f.content[read_at..]); - edit.pushUndo(p, &pane.body) catch { + edit.pushUndo(p.editor(), &pane.body) catch { p.gpa.free(replacement); return false; }; diff --git a/src/mouse.zig b/src/mouse.zig index 9ca45ad3..590f6806 100644 --- a/src/mouse.zig +++ b/src/mouse.zig @@ -91,7 +91,7 @@ fn headerSweepRun(p: *Pardes, sweep: HeaderSweep) void { const v = p.view(); const column = sweep.column(p) catch return; // error.Gone: its column closed under the sweep const t = if (column) |c| &v.col_tags[c] else &p.global_tag; - const text = edit.flatSurface(p, t) catch |err| return p.reportError(v.active, "edit", err); + const text = edit.flatSurface(p.editor(), t) catch |err| return p.reportError(v.active, "edit", err); const swept = sweep.span(text.len); const selected = if (swept.lo < swept.hi) text[swept.lo..swept.hi] else click: { const line = std.mem.count(u8, text[0..swept.lo], "\n"); @@ -364,7 +364,7 @@ fn headerPoint(p: *Pardes, column: ?usize, mcol: u16, mrow: u16, clamp: bool) ?s const t = if (column) |c| &v.col_tags[c] else &p.global_tag; const x = if (column) |c| v.col_x[c] + TAG_TEXT_INSET else TAG_TEXT_INSET; const focused = v.header != null and p.headerColumn() == column; - const text = edit.flatSurface(p, t) catch return null; + const text = edit.flatSurface(p.editor(), t) catch return null; const base = if (column != null) p.topBarHeight() else 0; const rows = if (column != null) layout.columnBarHeight(p) else p.topBarHeight(); const row: u16 = if (clamp) @min(mrow -| base, rows -| 1) else mrow -| base; @@ -444,7 +444,7 @@ fn chordCutPaste(p: *Pardes, cut: bool) edit.Error!void { // Everything done while B1 stays down is one undo step, as acme // marks the file once per B1 hold (text.c:881 textselect, `state` // and its one filemark): B1-B2 then B1-B3 is a copy. - try edit.pushUndo(p, &pane.body); + try edit.pushUndo(p.editor(), &pane.body); p.view().chord_hold = true; const sl = pane.sel[sel_slot]; pane.sel[sel_slot].state = .none; @@ -455,8 +455,8 @@ fn chordCutPaste(p: *Pardes, cut: bool) edit.Error!void { const w1 = pane.wrapAt(@max(0, sl.r1 - @as(i32, pane.tag_rows))); const row0 = w0.line; const row1 = w1.line; - const col0 = edit.paneByteAtDisplay(p, &pane.body, w0.line, w0.at, sl.c0 - pfx - w0.pad); - const col1 = edit.paneByteAtDisplay(p, &pane.body, w1.line, w1.at, sl.c1 - pfx - w1.pad); + const col0 = edit.paneByteAtDisplay(p.editor(), &pane.body, w0.line, w0.at, sl.c0 - pfx - w0.pad); + const col1 = edit.paneByteAtDisplay(p.editor(), &pane.body, w1.line, w1.at, sl.c1 - pfx - w1.pad); pane.body.cur_row = row1; pane.body.cur_col = col1; pane.body.cur_pinned = true; @@ -475,7 +475,7 @@ fn chordCutPaste(p: *Pardes, cut: bool) edit.Error!void { const y = p.registers.text(p.gpa, pardes.Registers.default) orelse return; if (y.len == 0) return; const at = modal.Cursor{ .row = @intCast(@max(0, pane.body.cur_row)), .col = @intCast(@max(0, pane.body.cur_col)) }; - try edit.pushUndo(p, &pane.body); + try edit.pushUndo(p.editor(), &pane.body); const new = try modal.insertAt(p.gpa, f.content, at, y); panes.File.setContent(p, f, new); pane.body.vsel = .{ .active = modal.nextGrapheme(y, 0) < y.len, .row = @intCast(at.row), .col = @intCast(at.col), .explicit = false }; @@ -1042,7 +1042,7 @@ fn gestureRange(p: *Pardes, id: usize, text: []const u8, on_tag: bool, operand: const top = lead[0] - @as(i32, pane.tag_rows); if (top < 0) break :lead null; const w = pane.wrapAt(top); - const col = edit.paneByteAtDisplay(p, &pane.body, w.line, w.at, lead[1] - panes.File.gutterWidth(pane) - w.pad); + const col = edit.paneByteAtDisplay(p.editor(), &pane.body, w.line, w.at, lead[1] - panes.File.gutterWidth(pane) - w.pad); break :lead .{ .row = @intCast(@max(0, w.line)), .col = @intCast(@max(0, col)), @@ -1124,7 +1124,7 @@ fn doubleClick(p: *Pardes, id: usize, t: *panes.Text, on_tag: bool, row: i32, co const c = prev orelse return; if (c.id != id or c.on_tag != on_tag or c.row != row or c.col != col or now - c.ms > 500) return; v.last_click = null; - const text = edit.flatSurface(p, t) catch |err| return p.reportError(v.active, "edit", err); + const text = edit.flatSurface(p.editor(), t) catch |err| return p.reportError(v.active, "edit", err); const q = modal.offsetAt(text, .{ .row = @intCast(@max(0, t.cur_row)), .col = @intCast(@max(0, t.cur_col)) }); const r = edit.doubleClickRange(text, q); if (r[1] <= r[0]) return; @@ -1160,7 +1160,7 @@ fn sweepPoint(p: *Pardes, pane: *panes.Pane, r: i32, c: i32) ?modal.Cursor { if (visible < 0) return null; const w = pane.wrapAt(visible); if (w.line < 0) return null; - const col = edit.paneByteAtDisplay(p, &pane.body, w.line, w.at, c - w.pad - (if (pane.file != null) @as(i32, panes.File.gutterWidth(pane)) else 0)); + const col = edit.paneByteAtDisplay(p.editor(), &pane.body, w.line, w.at, c - w.pad - (if (pane.file != null) @as(i32, panes.File.gutterWidth(pane)) else 0)); return .{ .row = @intCast(w.line), .col = @intCast(@max(0, col)) }; } @@ -1220,7 +1220,7 @@ fn dragRelease(p: *Pardes, button: Mouse.Button) void { const w = pane.wrapAt(body_vis); pane.body.cur_row = w.line; pane.body.cur_col = edit.paneByteAtDisplay( - p, + p.editor(), &pane.body, w.line, w.at, diff --git a/src/normal.zig b/src/normal.zig index d35dd60d..a344269c 100644 --- a/src/normal.zig +++ b/src/normal.zig @@ -55,7 +55,7 @@ pub fn selRegexArmed(pane: *Pane) ?struct { pat: []const u8, mode: modal.Normal. /// had when s/S/K/Alt-K was pressed; the prompt's snapshot of them lives on /// `pane`. pub fn applySelRegex(p: *Pardes, pane: *Pane, t: *Text, pat: []const u8, mode: modal.Normal.RegexMode) void { - const text = edit.flatSurface(p, t) catch |err| return p.reportError(p.view().active, "edit", err); + const text = edit.flatSurface(p.editor(), t) catch |err| return p.reportError(p.view().active, "edit", err); const snap = pane.sel_snap[0..pane.nsel_snap]; var out: [Text.max_selections]modal.Selection = undefined; var m: usize = 0; @@ -317,7 +317,7 @@ pub fn nodeSelect(p: *Pardes, pane: *Pane, t: *Text, walk: modal.Normal.NodeWalk /// with two letters each; the next two keys choose one (`jumpKey`). fn startJump(p: *Pardes, pane: *Pane, t: *Text) edit.Error!void { if (t.what != .body) return; - const text = try edit.flatSurface(p, t); + const text = try edit.flatSurface(p.editor(), t); const cursor = modal.selectionCursor(text, t.primaryRange(text, 0)); const top: usize = @intCast(@max(0, pane.scroll())); const start = @min(modal.lineStartOffset(text, top), text.len); @@ -343,7 +343,7 @@ pub fn jumpKey(p: *Pardes, pane: *Pane, t: *Text, key: Key) void { defer pane.jump.clearRetainingCapacity(); if (chosen >= pane.jump.items.len) return; const r = pane.jump.items[chosen]; - const text = edit.flatSurface(p, t) catch |err| return p.reportError(p.view().active, "edit", err); + const text = edit.flatSurface(p.editor(), t) catch |err| return p.reportError(p.view().active, "edit", err); if (!t.select) return t.setRange(text, 0, .{ .anchor = @min(r.anchor, r.head), .head = @max(r.anchor, r.head) }, true); // select mode stretches the primary to the word (helix Movement::Extend) const pr = t.primaryRange(text, 0); @@ -467,7 +467,7 @@ pub fn replaySels(p: *Pardes, t: *Text, what: Replay) void { after = KeyState.of(t); after_expl = t.vsel.explicit; } - const text = edit.flatSurface(p, t) catch { + const text = edit.flatSurface(p.editor(), t) catch { p.multi_stop = true; break; }; @@ -490,7 +490,7 @@ pub fn replaySels(p: *Pardes, t: *Text, what: Replay) void { t.nsel = 0; return; } - const text = edit.flatSurface(p, t) catch |err| return p.reportError(p.view().active, "edit", err); + const text = edit.flatSurface(p.editor(), t) catch |err| return p.reportError(p.view().active, "edit", err); var rs: [Text.max_selections]modal.Selection = undefined; var st: [Text.max_selections]i32 = undefined; for (res[0..n], 0..) |r, k| { @@ -539,7 +539,7 @@ pub fn handleNormal(p: *Pardes, t: *Text, key: Key) void { } fn startSelRegex(p: *Pardes, pane: *Pane, t: *Text, mode: modal.Normal.RegexMode) edit.Error!void { - const text = try edit.flatSurface(p, t); + const text = try edit.flatSurface(p.editor(), t); const got = t.ranges(text, 0, &pane.sel_snap); pane.nsel_snap = @intCast(got.n); pane.sel_snap_pri = @intCast(got.pri); @@ -957,8 +957,8 @@ fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) edit.Error! pane.sel[sel_slot].state = .none; }; if (raw_selection and a.role == .yank) return edit.normalYank(p, t); - edit.pinPaneCursor(p, t); - const text = try edit.flatSurface(p, t); + edit.pinPaneCursor(p.editor(), t); + const text = try edit.flatSurface(p.editor(), t); const range = t.primaryRange(text, 0); const cur = modal.selectionCursor(text, range); @@ -991,8 +991,8 @@ fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) edit.Error! if (nw == slice.len) return; return pointMove(t, text, range, ls + nw); }, - .goto_line_down => return verticalMove(t, text, range, true, a.count, edit.paneWrapWidth(p, t)), - .goto_line_up => return verticalMove(t, text, range, false, a.count, edit.paneWrapWidth(p, t)), + .goto_line_down => return verticalMove(t, text, range, true, a.count, edit.paneWrapWidth(p.editor(), t)), + .goto_line_up => return verticalMove(t, text, range, false, a.count, edit.paneWrapWidth(p.editor(), t)), .goto_column => { const line = modal.lineAtOffset(text, cur); const ls = modal.lineStartOffset(text, line); @@ -1041,7 +1041,7 @@ fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) edit.Error! .replace_prefix => return edit.normalReplaceChar(p, t, a.char), // `mm` .match_prefix => { - const lines = try edit.paneCursorLines(p, t); + const lines = try edit.paneCursorLines(p.editor(), t); const mc = modal.matchBracket(lines, modal.positionAt(text, cur)) orelse return; return pointMove(t, text, range, modal.offsetAt(text, mc)); }, @@ -1155,8 +1155,8 @@ fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) edit.Error! .indent => return edit.normalIndent(p, t, a.count, true), .unindent => return edit.normalIndent(p, t, a.count, false), .comment_toggle => return edit.normalToggleComment(p, t), - .undo => return edit.undoStep(p, t, true), - .redo => return edit.undoStep(p, t, false), + .undo => return edit.undoStep(p.editor(), t, true), + .redo => return edit.undoStep(p.editor(), t, false), // Language shortcuts run the same builtins as tags and leader commands. .goto_definition => return exec.runBuiltin(p, .Def, v.active, "", null), .goto_references => return exec.runBuiltin(p, .Refs, v.active, "", null), diff --git a/src/tagline.zig b/src/tagline.zig index 1bb4dc85..84cb3173 100644 --- a/src/tagline.zig +++ b/src/tagline.zig @@ -90,7 +90,7 @@ pub fn revealHeader(p: *Pardes) void { v.header_top = 0; return; }; - const text = edit.flatSurface(p, t) catch |err| return p.reportError(v.active, "tag", err); + const text = edit.flatSurface(p.editor(), t) catch |err| return p.reportError(v.active, "tag", err); const cur: usize = @intCast(@max(0, t.cur_row)); const rows: usize = if (p.headerColumn() != null) layout.columnBarHeight(p) else p.topBarHeight(); if (cur < v.header_top) v.header_top = cur; @@ -424,7 +424,7 @@ pub fn enterHeader(p: *Pardes, column: ?usize) void { p.view().header = if (column) |c| .{ .column = c } else .workspace; if (column) |c| p.setActiveColumn(c); const t = focusedHeader(p) orelse return exitHeader(p); - takeKeyboard(t, edit.flatSurface(p, t) catch |err| return p.reportError(p.view().active, "tag", err)); + takeKeyboard(t, edit.flatSurface(p.editor(), t) catch |err| return p.reportError(p.view().active, "tag", err)); } /// The span a header's selection covers on line `row` of its text, in @@ -452,7 +452,7 @@ pub fn workspaceBlank(p: *Pardes, col: u16, row: u16, tag_hit: ?pardes.TagHit) b const v = p.view(); if (row >= p.topBarHeight() or col >= v.screen_w) return false; if (col < TAG_TEXT_INSET) return true; - const text = edit.flatSurface(p, &p.global_tag) catch return false; + const text = edit.flatSurface(p.editor(), &p.global_tag) catch return false; const scroll: usize = if (v.header != null and p.headerColumn() == null) v.header_scroll else 0; const at = @as(usize, Layer.columnAt(p, .workspace, 0, tag_hit, false, 0) orelse (col - TAG_TEXT_INSET)) + scroll; return at >= panes.File.displayWidth(modal.lineSlice(text, 0)) + 2; diff --git a/test/hxdiff.zig b/test/hxdiff.zig index 010cc674..ae73863e 100644 --- a/test/hxdiff.zig +++ b/test/hxdiff.zig @@ -433,7 +433,7 @@ fn runCase(arena: std.mem.Allocator, case: Case, pane_kind: []const u8) !Result // the cursor's empty row below the output (see Pardes.shellRows), so // its line list is exactly a file content's — join it and you have the // same bytes, trailing newline included, with nothing to patch up. - const lines = try pardes.edit.paneCursorLines(core, &pane.body); + const lines = try pardes.edit.paneCursorLines(core.editor(), &pane.body); var text_len: usize = if (lines.len == 0) 0 else lines.len - 1; for (lines) |ln| text_len = std.math.add(usize, text_len, ln.len) catch return error.OutOfMemory; const joined = try arena.alloc(u8, text_len); diff --git a/test/panes.zig b/test/panes.zig index 04769ccf..fb973ea2 100644 --- a/test/panes.zig +++ b/test/panes.zig @@ -1153,7 +1153,7 @@ test "terminal edits and undo survive partial history clearing and whole-screen try std.testing.expectEqualStrings("edit one\nedit two", snapshot.ovl.?.text); } try std.testing.expectEqual(@as(usize, 2), pardes.Term.emulatorOf(pane.terminal.?).vt.screens.active.pages.countTrackedPins()); - try pardes.edit.undoStep(p, &pane.body, true); + try pardes.edit.undoStep(p.editor(), &pane.body, true); try std.testing.expectEqualStrings("edit one\nedit two", pane.ovl.?.text); try std.testing.expectEqual(expected, pane.ovl.?.row); } diff --git a/test/perf.zig b/test/perf.zig index 0f5466ad..85cc2ca3 100644 --- a/test/perf.zig +++ b/test/perf.zig @@ -695,7 +695,7 @@ fn measureTerm(op: TermOp, fx: TermFixture, reps: usize) !Cell { pump(core); } _ = try frame(core); - const motion_rows = if (op == .key_down) (try pardes.edit.paneCursorLines(core, &pane.body)).len else 0; + const motion_rows = if (op == .key_down) (try pardes.edit.paneCursorLines(core.editor(), &pane.body)).len else 0; if (op == .key_down and motion_rows < 3) return error.EmptyTerminalFixture; var cols = screen_cols; -- cgit v1.3