summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-17 05:45:38 -0300
committerGabriel Schneider <[email protected]>2026-11-17 12:15:01 -0300
commit779ff8cc27014b8c2f2f078985ab2ca9cf285740 (patch)
tree8b05d0404544b9efd92e88c7b727d3ba8f6a37f5 /src
parent55dbff5aced5c62f47be39fbc01276b6e36d7dd5 (diff)
downloadpardes-779ff8cc27014b8c2f2f078985ab2ca9cf285740.tar.gz
pardes-779ff8cc27014b8c2f2f078985ab2ca9cf285740.zip
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
Diffstat (limited to 'src')
-rw-r--r--src/Core.zig4
-rw-r--r--src/Editor.zig54
-rw-r--r--src/edit.zig55
-rw-r--r--src/exec.zig2
-rw-r--r--src/keys.zig2
-rw-r--r--src/lsp/Ask.zig2
-rw-r--r--src/mouse.zig2
-rw-r--r--src/normal.zig7
-rw-r--r--src/pardes.zig1
9 files changed, 95 insertions, 34 deletions
diff --git a/src/Core.zig b/src/Core.zig
index ecfd8fa7..c57818ee 100644
--- a/src/Core.zig
+++ b/src/Core.zig
@@ -438,6 +438,10 @@ pub const setMessage = Messages.setMessage;
pub const reportError = Messages.reportError;
pub const reportFailure = Messages.reportFailure;
+/// What an edit of a text runs with (Editor.zig), gathered from this core:
+/// called from everywhere as `edit.f(p.editor(), t, ...)`.
+pub const editor = pardes.Editor.of;
+
/// Running a builtin line, called from everywhere as
/// `p.executeBuiltinLine(...)`; it lives with the rest of exec in exec.zig.
pub const executeBuiltinLine = exec.executeBuiltinLine;
diff --git a/src/Editor.zig b/src/Editor.zig
new file mode 100644
index 00000000..559ea196
--- /dev/null
+++ b/src/Editor.zig
@@ -0,0 +1,54 @@
+//! What an edit of a text runs with (normal.zig, edit.zig): the allocators,
+//! the registers, the register the running command names, the replay of a
+//! key across the ranges, the text an edit made that nothing kept, and the
+//! core the text's pane is in. Gathered for each call from where pardes
+//! keeps them (`Pardes.editor`); the library gathers it from the value a
+//! sketch stores (src/lib_editor.zig). Nothing here is kept between calls.
+const std = @import("std");
+const Registers = @import("Registers.zig");
+const Pardes = @import("Core.zig");
+
+const Editor = @This();
+
+gpa: std.mem.Allocator,
+/// What an edit makes and drops: the update's scratch.
+scratch: std.mem.Allocator,
+registers: *Registers,
+/// The register the running normal command names (`"<reg>`); 0 for the
+/// default one.
+register: *u21,
+/// While a key is replayed across the ranges (normal.replaySels): whether
+/// this is the first pass, whether a pass stopped the replay, which range
+/// this pass is, in document order, and how many there are; outside a
+/// replay, the one range and its first pass. A register value belongs to
+/// its range by `multi_index`.
+multi_first: *bool,
+multi_stop: *bool,
+multi_index: *usize,
+multi_count: *usize,
+/// An edited text nothing kept (a tag's shown text): see
+/// edit.retireEditText.
+retired_edit: *?[]u8,
+/// The core the text's pane is in: what an edit reaches past its text with.
+p: *Pardes,
+
+pub fn of(p: *Pardes) Editor {
+ return .{
+ .gpa = p.gpa,
+ .scratch = p.scratch.allocator(),
+ .registers = &p.registers,
+ .register = &p.view().register,
+ .multi_first = &p.multi_first,
+ .multi_stop = &p.multi_stop,
+ .multi_index = &p.multi_index,
+ .multi_count = &p.multi_count,
+ .retired_edit = &p.retired_edit,
+ .p = p,
+ };
+}
+
+/// An edit that failed, said on the message row of the pane the keyboard
+/// is in.
+pub fn reportError(e: Editor, err: anyerror) void {
+ e.p.reportError(e.p.view().active, "edit", err);
+}
diff --git a/src/edit.zig b/src/edit.zig
index 23c223d9..2dd613ac 100644
--- a/src/edit.zig
+++ b/src/edit.zig
@@ -29,6 +29,7 @@ const sel_slot = pardes.sel_slot;
const Effect = pardes.Effect;
const Pardes = pardes.Pardes;
const Registers = pardes.Registers;
+const Editor = pardes.Editor;
/// An edit's failure, memory or a terminal's rows not read, said by its key.
pub const Error = error{ OutOfMemory, WriteFailed, UnknownPoint };
@@ -36,28 +37,28 @@ pub const Error = error{ OutOfMemory, WriteFailed, UnknownPoint };
/// names (`"<reg>`), the default one otherwise. `_` swallows it, and the
/// registers computed from the editor (`#`, `.`, `%`) cannot be written.
/// `+` and `*` are the system clipboard: the value is sent there too.
-pub fn setYank(p: *Pardes, text: []const u8) error{OutOfMemory}!void {
- var name: u21 = if (p.view().register != 0) p.view().register else Registers.default;
+pub fn setYank(e: Editor, text: []const u8) error{OutOfMemory}!void {
+ var name: u21 = if (e.register.* != 0) e.register.* else Registers.default;
switch (name) {
'_', '#', '.', '%' => return,
'*' => name = '+',
else => {},
}
- try p.registers.put(p.gpa, name, text, p.multi_index, p.multi_count, p.multi_first);
- if (name == '+' and p.multi_first) p.emit(.{ .set_clipboard = {} });
+ try e.registers.put(e.gpa, name, text, e.multi_index.*, e.multi_count.*, e.multi_first.*);
+ if (name == '+' and e.multi_first.*) e.p.emit(.{ .set_clipboard = {} });
}
/// Value `i` of register `name` as the command at range `i` reads it, the
/// computed registers included: `#` is the range's number from 1, `.` its
/// text, `%` the file's name, `_` nothing. Scratch- or register-owned.
-pub fn registerValue(p: *Pardes, t: *Text, name: u21) ?[]const u8 {
- const i = p.multi_index;
+pub fn registerValue(e: Editor, t: *Text, name: u21) ?[]const u8 {
+ const i = e.multi_index.*;
return switch (name) {
'_' => null,
- '#' => std.fmt.allocPrint(p.scratch.allocator(), "{d}", .{i + 1}) catch null,
- '.' => selText(p, t) catch null,
+ '#' => std.fmt.allocPrint(e.scratch, "{d}", .{i + 1}) catch null,
+ '.' => selText(e.p, t) catch null,
'%' => if (t.what == .body) if (t.pane()) |pane| (if (pane.file) |f| f.path else null) else null else null,
- else => p.registers.value(name, i),
+ else => e.registers.value(name, i),
};
}
@@ -79,11 +80,11 @@ pub const Paste = enum { after, before, replace };
pub fn pasteRegister(p: *Pardes, t: *Text, how: Paste, count: usize) Error!void {
const name: u21 = if (p.view().register != 0) p.view().register else Registers.default;
if (name == '+' or name == '*') {
- if (!normal.multiOnce(p)) return;
+ if (!normal.multiOnce(p.editor())) return;
const pane = t.pane() orelse return;
return clipRequest(p, panes.paneIdOf(p, pane) orelse return, how);
}
- const y = registerValue(p, t, name) orelse return;
+ const y = registerValue(p.editor(), t, name) orelse return;
switch (how) {
.after => try pasteText(p, t, y, false, count),
.before => try pasteText(p, t, y, true, count),
@@ -182,7 +183,7 @@ pub fn clipYank(p: *Pardes, pane: *Pane, main_only: bool) void {
defer v.register = named;
// A kind's pane: what it has selected (Kind.Slot.selection).
if (pane.addon) |k| {
- if (k.selection.len > 0) setYank(p, k.selection) catch |err| p.reportError(p.view().active, "yank", err);
+ if (k.selection.len > 0) setYank(p.editor(), k.selection) catch |err| p.reportError(p.view().active, "yank", err);
return;
}
if (pane.body.nsel > 0 and !main_only) {
@@ -674,7 +675,7 @@ fn setEditTextSpan(p: *Pardes, t: *Text, new: []u8, known_end: ?modal.Cursor, sp
// 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, new);
+ defer retireEditText(p.editor(), new);
const pane = t.pane() orelse return;
const prefix = try tagline.pathPrefix(p, p.scratch.allocator(), pane);
if (!std.mem.startsWith(u8, new, prefix)) {
@@ -691,14 +692,14 @@ fn setEditTextSpan(p: *Pardes, t: *Text, new: []u8, known_end: ?modal.Cursor, sp
t.own = new;
return;
}
- const pane = t.pane() orelse return retireEditText(p, new);
+ const pane = t.pane() orelse return retireEditText(p.editor(), 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, new);
- if (kept.ptr != new.ptr) retireEditText(p, new);
+ 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 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;
@@ -720,7 +721,7 @@ fn setEditTextSpan(p: *Pardes, t: *Text, new: []u8, known_end: ?modal.Cursor, sp
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);
// A terminal's edit copy rewritten: the text it replaces freed.
- const overlay = if (pane.ovl) |*value| value else return retireEditText(p, new);
+ const overlay = if (pane.ovl) |*value| value else return retireEditText(p.editor(), new);
for (0..pane.sel.len) |button| {
pane.clearPointerSelection(button);
pane.sel[button].state = .none;
@@ -742,9 +743,9 @@ fn bodyFile(t: *Text) ?*panes.File.State {
/// Take a text an edit made that nothing keeps, holding it until the next
/// one: the edit's caller still reads it to place the cursor.
-pub fn retireEditText(p: *Pardes, new: []u8) void {
- if (p.retired_edit) |old| p.gpa.free(old);
- p.retired_edit = new;
+pub fn retireEditText(e: Editor, new: []u8) void {
+ if (e.retired_edit.*) |old| e.gpa.free(old);
+ e.retired_edit.* = new;
}
/// helix's indent for a line opened after byte `at` of `row` (indent.zig),
@@ -913,7 +914,7 @@ pub fn insertKey(p: *Pardes, t: *Text, key: Key) Error!void {
if (t.reg_wait) {
t.reg_wait = false;
if (key.ctrl or key.alt or key.cp >= 0xF0000) return;
- const y = registerValue(p, t, key.cp) orelse return;
+ const y = registerValue(p.editor(), t, key.cp) orelse return;
if (y.len > 0) try insertKey(p, t, .{ .cp = key.cp, .text = y });
return;
}
@@ -1296,7 +1297,7 @@ 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, try vselText(p, t)); // read the range before the move
+ 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 {
t.vsel.active = false;
@@ -1312,10 +1313,10 @@ pub fn normalDelete(p: *Pardes, t: *Text, yank: bool) Error!void {
pub fn normalYank(p: *Pardes, t: *Text) Error!void {
t.select = false;
if (t.what == .body) if (t.pane()) |pane| if (pane.rawPointerText(pane.sel[sel_slot])) |text| {
- try setYank(p, text);
+ try setYank(p.editor(), text);
return;
};
- try setYank(p, try if (t.vsel.active) vselText(p, t) else selText(p, t));
+ try setYank(p.editor(), try if (t.vsel.active) vselText(p, t) else selText(p, t));
}
pub fn pasteText(p: *Pardes, t: *Text, y0: []const u8, before: bool, count: usize) Error!void {
@@ -1391,7 +1392,7 @@ pub fn normalChange(p: *Pardes, t: *Text) Error!void {
const eb = (try editTextEol(p, 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, try vselText(p, t));
+ try setYank(p.editor(), try vselText(p, t));
try pushUndo(p, 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);
@@ -1411,7 +1412,7 @@ pub fn normalChange(p: *Pardes, t: *Text) Error!void {
t.show();
return;
}
- try setYank(p, try vselText(p, t));
+ try setYank(p.editor(), try vselText(p, t));
try pushUndo(p, t);
const new = try deleteSelection(p, t, eb);
try setEditText(p, t, new);
@@ -1425,7 +1426,7 @@ pub fn normalChange(p: *Pardes, t: *Text) Error!void {
try pushUndo(p, t);
const eb = (try editTextEol(p, t, selRows(t))) orelse return;
const r = t.primaryRange(eb.text, eb.row0);
- try setYank(p, eb.text[r.anchor..r.head]);
+ 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);
const llen = modal.lineSlice(new, @intCast(@max(0, t.cur_row - eb.row0))).len;
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)) {
diff --git a/src/keys.zig b/src/keys.zig
index 8b67a9d1..413818ed 100644
--- a/src/keys.zig
+++ b/src/keys.zig
@@ -771,7 +771,7 @@ test "raw tty forwards Ctrl letters and former global shortcuts to the child" {
defer p.deinit();
var buf: [256]u8 = undefined;
_ = test_api.ptyBytes(p, null, &buf);
- try edit.setYank(p, "register text");
+ try edit.setYank(p.editor(), "register text");
for ('a'..'z' + 1) |letter| {
if (letter == p.tty_toggle) continue; // editor/TTY mode shortcut
if (letter == 'v') continue; // the window's paste, asserted just below
diff --git a/src/lsp/Ask.zig b/src/lsp/Ask.zig
index 250d6dba..879c1f5d 100644
--- a/src/lsp/Ask.zig
+++ b/src/lsp/Ask.zig
@@ -40,7 +40,7 @@ const TAG_TEXT_INSET = pardes.TAG_TEXT_INSET;
pub fn lspRequest(p: *Pardes, asked: usize, kind: lsp.Kind, arg_in: []const u8) void {
var arg = arg_in;
- if (!normal.multiOnce(p)) return; // one question per keystroke, from the primary
+ if (!normal.multiOnce(p.editor())) return; // one question per keystroke, from the primary
// Lspwhy narrates the last query where it was asked -- that pane's
// file, place and argument -- from wherever it is run.
const last_pane: ?usize = if (kind == .explain) (if (p.lsp_ask.last) |last| p.paneBySerial(last.serial) else null) else null;
diff --git a/src/mouse.zig b/src/mouse.zig
index 8867982c..9ca45ad3 100644
--- a/src/mouse.zig
+++ b/src/mouse.zig
@@ -1107,7 +1107,7 @@ fn mirrorTtySelection(p: *Pardes, pane: *Pane) void {
pane.body.restore_cursor = false;
pane.body.sticky_col = -1;
pane.body.normal.clear();
- edit.setYank(p, text) catch |err| p.reportError(p.view().active, "yank", err);
+ edit.setYank(p.editor(), text) catch |err| p.reportError(p.view().active, "yank", err);
}
/// acme's double-click (text.c:881 textselect, plan9port text.c:1407
diff --git a/src/normal.zig b/src/normal.zig
index dbd35fb5..d35dd60d 100644
--- a/src/normal.zig
+++ b/src/normal.zig
@@ -21,14 +21,15 @@ const hit = pardes.hit;
const sel_slot = pardes.sel_slot;
const Pardes = pardes.Pardes;
const Macro = pardes.Macro;
+const Editor = pardes.Editor;
fn isPrefix(key: Key, cp: u21) bool {
return key.cp == cp and !key.ctrl and !key.alt;
}
-pub fn multiOnce(p: *Pardes) bool {
- p.multi_stop = true;
- return p.multi_first;
+pub fn multiOnce(e: Editor) bool {
+ e.multi_stop.* = true;
+ return e.multi_first.*;
}
fn regexMarker(mode: modal.Normal.RegexMode) []const u8 {
diff --git a/src/pardes.zig b/src/pardes.zig
index 07e612e5..48c87bf6 100644
--- a/src/pardes.zig
+++ b/src/pardes.zig
@@ -31,6 +31,7 @@ pub const draw = @import("draw.zig");
pub const Messages = @import("Messages.zig");
pub const Registers = @import("Registers.zig");
pub const Macro = @import("Macro.zig");
+pub const Editor = @import("Editor.zig");
pub const Pipe = @import("Pipe.zig");
pub const Stream = @import("Stream.zig");
pub const Handover = @import("Handover.zig");