diff options
Diffstat (limited to 'src/edit.zig')
| -rw-r--r-- | src/edit.zig | 889 |
1 files changed, 563 insertions, 326 deletions
diff --git a/src/edit.zig b/src/edit.zig index c5466531..fe644541 100644 --- a/src/edit.zig +++ b/src/edit.zig @@ -17,18 +17,19 @@ const syntax = @import("syntax.zig"); const indents = @import("indent.zig"); const platform = pardes.platform; const Pane = panes.Pane; -const Text = panes.Text; +const Text = @import("Text.zig"); const Layer = @import("Layer.zig"); const TagHit = pardes.TagHit; const BOX_H = pardes.BOX_H; const TAG_TEXT_INSET = pardes.TAG_TEXT_INSET; const Mouse = pardes.Mouse; -const Key = pardes.Key; -const hit = pardes.hit; +const Key = @import("plain").Key; +const hit = @import("Event.zig").hit; const sel_slot = pardes.sel_slot; const Effect = pardes.Effect; const Pardes = pardes.Pardes; -const Registers = pardes.Registers; +const Registers = @import("Registers.zig"); +const Editor = @import("Editor.zig"); /// An edit's failure, memory or a terminal's rows not read, said by its key. pub const Error = error{ OutOfMemory, WriteFailed, UnknownPoint }; @@ -36,38 +37,39 @@ 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.*); + // the library's `+` is a register its caller fills and reads + if (comptime !Editor.lib) 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, 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), }; } /// 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 @@ -76,18 +78,18 @@ fn selText(p: *Pardes, t: *Text) Error![]const u8 { /// Where a paste goes: after the selection, before it, or over it. 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; +pub fn pasteRegister(e: Editor, t: *Text, how: Paste, count: usize) Error!void { + const name: u21 = if (e.register.* != 0) e.register.* else Registers.default; if (name == '+' or name == '*') { - if (!normal.multiOnce(p)) return; + if (!normal.multiOnce(e)) return; const pane = t.pane() orelse return; - return clipRequest(p, panes.paneIdOf(p, pane) orelse return, how); + return clipRequest(e.p, panes.paneIdOf(e.p, pane) orelse return, how); } - const y = registerValue(p, t, name) orelse return; + const y = registerValue(e, t, name) orelse return; switch (how) { - .after => try pasteText(p, t, y, false, count), - .before => try pasteText(p, t, y, true, count), - .replace => try replaceWithText(p, t, y), + .after => try pasteText(e, t, y, false, count), + .before => try pasteText(e, t, y, true, count), + .replace => try replaceWithText(e, t, y), } } @@ -105,11 +107,11 @@ pub fn clipRequest(p: *Pardes, id: usize, mode: Paste) void { } /// Text pasted, yanked or sent into the program at `id`, as xterm pastes -/// (terminal.encodePaste). +/// (Term.paste). pub fn typeToTty(p: *Pardes, id: usize, pane: *const Pane, text: []const u8) void { if (text.len == 0) return; const cp = p.scratch.allocator().dupe(u8, text) catch |err| return p.reportError(id, "paste", err); - for (panes.terminal.encodePaste(pane, cp)) |part| if (part.len > 0) p.emitWrite(id, part); + for (panes.Term.paste(pane.terminal, cp)) |part| if (part.len > 0) p.emitWrite(id, part); } pub fn applyPaste(p: *Pardes, whole: []const u8) Error!void { @@ -143,16 +145,16 @@ pub fn applyPaste(p: *Pardes, whole: []const u8) Error!void { line[n] = byte; n += 1; }; - if (n > 0) try insertKey(p, &pane.input, .{ .cp = line[0], .text = line[0..n] }); + if (n > 0) try insertKey(p.editor(), &pane.input, .{ .cp = line[0], .text = line[0..n] }); if (normal.selRegexArmed(pane)) |a| if (panes.promptTarget(p, pane)) |t| normal.applySelRegex(p, pane, t, a.pat, a.mode); return; } if (pane.focus == .tag) return pasteInto(p, &pane.tag, bytes); if (pane.isTerminal() and pane.body.mode == .tty) return typeToTty(p, id, pane, bytes); switch (if (req) |r| r.mode else .after) { - .after => try pasteText(p, &pane.body, bytes, false, 1), - .before => try pasteText(p, &pane.body, bytes, true, 1), - .replace => try replaceWithText(p, &pane.body, bytes), + .after => try pasteText(p.editor(), &pane.body, bytes, false, 1), + .before => try pasteText(p.editor(), &pane.body, bytes, true, 1), + .replace => try replaceWithText(p.editor(), &pane.body, bytes), } } @@ -167,7 +169,7 @@ fn pasteInto(p: *Pardes, t: *Text, bytes: []const u8) Error!void { const kept_n = t.nsel; @memcpy(kept_sels[0..kept_n], t.sels[0..kept_n]); t.refused = false; - if (t.mode == .insert) try insertKey(p, t, typed) else try pasteText(p, t, bytes, false, 1); + if (t.mode == .insert) try insertKey(p.editor(), t, typed) else try pasteText(p.editor(), t, bytes, false, 1); if (t.refused) { t.cur_row, t.cur_col, t.vsel = kept; t.restoreSels(kept_sels[0..kept_n]); @@ -182,15 +184,15 @@ 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) { - normal.replaySels(p, &pane.body, .{ .normal = .{ .role = .yank } }); + normal.replaySels(p.editor(), &pane.body, .{ .normal = .{ .role = .yank } }); } else { const others = pane.body.nsel; pane.body.nsel = 0; - normalYank(p, &pane.body) catch |err| p.reportError(p.view().active, "edit", err); + normalYank(p.editor(), &pane.body) catch |err| p.reportError(p.view().active, "edit", err); pane.body.nsel = others; } } @@ -222,7 +224,7 @@ pub fn capturePointerSelection(p: *Pardes, pane: *Pane, slot: usize) !void { // Refresh the source map before translating the rendered rectangle. const raw = pane.isTerminal() and pane.body.mode == .tty; const body = try body_layer.bodyText(p.scratch.allocator(), pane); - const terminal_lines = if (pane.file == null and !raw) try panes.terminal.cursorLines(p, pane) else &.{}; + const terminal_lines = if (pane.file == null and !raw) try terminalLines(p, pane) else &.{}; const first = @max(0, @min(gesture.r0, gesture.r1) - pane.tag_rows); const logical_rows = if (pane.body_rows > 0) pane.body_rows else pane.rows; const last = @min(@as(i32, logical_rows) - 1, @max(gesture.r0, gesture.r1) - pane.tag_rows); @@ -239,7 +241,7 @@ pub fn capturePointerSelection(p: *Pardes, pane: *Pane, slot: usize) !void { const left: i32 = if (cols.first) @max(cols.lo, prefix) else prefix; const right: i32 = cols.hi orelse std.math.maxInt(i32) / 2; var source = pane.wrapAt(visible); - if (raw) source = .{ .line = panes.terminal.gridOffset(pane) + visible, .at = 0 }; + if (raw) source = .{ .line = @as(i32, @intCast(panes.Term.grid(pane.terminal).offset)) + visible, .at = 0 }; const line = if (raw) modal.lineSlice(body, @intCast(visible)) else pointerSourceLine(pane, terminal_lines, source.line); var next = pane.wrapAt(visible + 1); if (raw) next = .{ .line = source.line + 1, .at = 0 }; @@ -260,7 +262,7 @@ pub fn capturePointerSelection(p: *Pardes, pane: *Pane, slot: usize) !void { .hi = if (right < prefix) lo else if (cols.hi == null) end else @min(end, modal.nextGrapheme(line, at)), .to_edge = cols.hi == null, .raw_terminal = raw, - .prompt_bytes = if (raw) panes.terminal.promptPrefixBytes(pane, source.line, line) else 0, + .prompt_bytes = if (raw) panes.Term.promptPrefix(pane.terminal, source.line, line) else 0, }; } pane.next_pointer_selection += 1; @@ -275,7 +277,7 @@ pub fn selectionText(p: *Pardes, pane: *Pane, sl: Pane.Sel) ![]const u8 { const arena = p.scratch.allocator(); if (pane.rawPointerText(sl)) |text| return arena.dupe(u8, text); if (pane.pointerSelection(sl)) |rows| { - const terminal_lines = if (pane.file == null) try panes.terminal.cursorLines(p, pane) else &.{}; + const terminal_lines = if (pane.file == null) try terminalLines(p, pane) else &.{}; var total: usize = rows.len -| 1; for (rows) |row| { const line = pointerSourceLine(pane, terminal_lines, row.row); @@ -350,10 +352,10 @@ 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 { +pub fn exitInsert(e: Editor, t: *Text) void { t.mode = .normal; t.reg_wait = false; t.normal.clear(); @@ -362,7 +364,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(e, t) catch |err| return e.reportError(err); var rs: [Text.max_selections]modal.Selection = undefined; const got = t.ranges(text, 0, &rs); for (rs[0..got.n]) |*r| { @@ -374,10 +376,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(); - if (t.what != .body) { - const text = try flatSurface(p, t); +pub fn paneCursorLines(e: Editor, t: *Text) Error![]const []const u8 { + const arena = e.scratch; + if (t.what != .body or comptime Editor.lib) { + 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().?; @@ -386,13 +388,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 panes.terminal.cursorLines(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); @@ -405,7 +407,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) { @@ -415,111 +417,308 @@ 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 ---- + +/// Set while a frame is drawn: what it reads is no step's. +pub var drawing = false; + +/// A terminal's rows as text (`Term.rows`), kept for one pane until its +/// grid moves. Returned slices remain valid until the next cache rebuild +/// or reset. +pub const RowsCache = struct { + /// whose grid this describes; null = the slot is free + pane: ?*const Pane = null, + /// the rows joined by '\n' — `flatSurface` hands this back verbatim + /// instead of rebuilding the join on every keystroke + text: []const u8 = &.{}, + /// slices INTO `text`, absolute grid rows from 0 + rows: [][]const u8 = &.{}, + /// `text` is a prefix of this: blanking a prompt row shortens the join, + /// and the slack is not worth a second allocation to reclaim + text_alloc: []u8 = &.{}, + stale: bool = false, + + pub fn reset(c: *RowsCache, gpa: std.mem.Allocator) void { + if (c.text_alloc.len > 0) gpa.free(c.text_alloc); + if (c.rows.len > 0) gpa.free(c.rows); + c.* = .{}; + } + + /// Free a stale entry. Called at the top of `update`, and nowhere else. + pub fn sweep(c: *RowsCache, gpa: std.mem.Allocator) void { + if (c.stale) c.reset(gpa); + } + + /// `pane`'s grid moved: the entry no longer describes it. + pub fn markStale(c: *RowsCache, pane: *const Pane) void { + if (c.pane == pane) c.stale = true; + } + + pub fn dropPane(c: *RowsCache, pane: *const Pane) void { + if (c.pane != pane) return; + c.pane = null; + c.stale = true; + } +}; + +const empty_grid = [1][]const u8{""}; + +/// A terminal pane's rows from the top of its history (`Term.rows`), kept +/// in `Pardes.shell_rows` while its grid holds still. +pub fn shellRows(p: *Pardes, pane: *Pane) ![]const []const u8 { + const state = pane.terminal orelse return &empty_grid; + pardes.tape.touchTerminal(); + // A step's read whether the rows are kept or made: a client that came + // since holds them as holes, and is sent them (Screens.fill). + if (!drawing) panes.Term.markRead(state); + const c = &p.shell_rows; + if (!c.stale and c.pane == pane) return c.rows; + // Another pane's rows, or a dropped pane's still waiting for the step's + // sweep (dropPane keeps them, as they may still be borrowed): this + // step's rows are scratch, and nothing in the slot is written over. + if (c.pane != null or c.text_alloc.len > 0 or c.rows.len > 0) { + c.stale = true; + return (try panes.Term.textRows(state, p.scratch.allocator(), p.scratch.allocator())).rows; + } + const built = try panes.Term.textRows(state, p.gpa, p.scratch.allocator()); + c.* = .{ + .pane = pane, + .text = built.text, + .rows = built.rows, + .text_alloc = built.text_alloc, + }; + return c.rows; } -pub fn flatSurface(p: *Pardes, t: *Text) Error![]const u8 { +/// The full modal motion surface: shell history with the live edit overlay +/// spliced into the rows it covers. +pub fn terminalLines(p: *Pardes, pane: *Pane) ![]const []const u8 { + const rows = try shellRows(p, pane); + if (pane.ovl == null) return rows; + var last = rows.len; + if (pane.ovl) |overlay| + last = @max(last, @as(usize, @intCast(@max(0, overlay.row + overlay.rows)))); + var count = last; + if (pane.ovl) |overlay| { + if (overlay.row >= 0 and @as(usize, @intCast(overlay.row)) < last) + count = count - @min( + @as(usize, @intCast(overlay.rows)), + last - @as(usize, @intCast(overlay.row)), + ) + @max(1, modal.lineCount(overlay.text)); + } + const lines = try p.scratch.allocator().alloc([]const u8, count); + var n: usize = 0; + var grid_row: usize = 0; + while (grid_row < last) : (grid_row += 1) { + if (pane.ovl) |overlay| if (overlay.row >= 0 and grid_row == @as(usize, @intCast(overlay.row))) { + var overlay_lines = std.mem.splitScalar(u8, overlay.text, '\n'); + while (overlay_lines.next()) |line| : (n += 1) lines[n] = line; + grid_row += @intCast(overlay.rows - 1); + continue; + }; + lines[n] = if (grid_row < rows.len) rows[grid_row] else ""; + n += 1; + } + return lines[0..n]; +} + +pub fn flatSurface(e: Editor, t: *Text) Error![]const u8 { switch (t.what) { - .body => {}, + // the library's text has no pane: its characters are its own + .body => if (comptime Editor.lib) return t.own orelse "", // 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 if (comptime Editor.lib) "" else 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 panes.terminal.cursorLines(p, pane); - return panes.terminal.flatSurface(p, pane, lines); + 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 = &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 e.scratch.alloc(u8, total); + var at: usize = 0; + for (lines, 0..) |line, i| { + if (i > 0) { + text[at] = '\n'; + at += 1; + } + @memcpy(text[at..][0..line.len], line); + at += line.len; + } + 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)); - if (t.what != .body) return .{ .text = t.own orelse &.{}, .row0 = 0 }; + t.own = try e.gpa.dupe(u8, try flatSurface(e, t)); + if (t.what != .body or comptime Editor.lib) return .{ .text = t.own orelse &.{}, .row0 = 0 }; const pane = t.pane() orelse return null; if (pane.file) |f| return .{ .text = f.content, .row0 = 0 }; if (pane.addon != null) return null; - return panes.terminal.editText(p, pane, lo, hi, col); + // A terminal's: its edit copy (`Pane.ovl`), made from the rows `lo` to + // `hi` or widened over them, with one exact allocation; the rows are + // scratch-owned or borrowed, only the joined text is kept. + const want_lo = @max(0, @min(lo, hi)); + const want_hi = @max(want_lo, @max(lo, hi)); + const fresh = pane.ovl == null; + const old: Text.EditBuffer = pane.ovl orelse .{ .row = want_lo, .rows = 1, .text = &.{} }; + const lines: i32 = if (fresh) 1 else @intCast(modal.lineCount(old.text)); + const up = old.row - want_lo; + const down = want_hi - (old.row + lines - 1); + const extending = fresh or up > 0 or down > 0; + var row0 = old.row; + var covered = old.rows; + var text = old.text; + var owned = false; + if (extending) { + 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 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 ""; + } + const middle: usize = @intCast(old.row); + parts[up_len] = if (fresh) + (if (middle < rows.len) rows[middle] else "") + else + old.text; + for (parts[up_len + 1 ..], 0..) |*part, i| { + const src = @as(usize, @intCast(old.row + old.rows)) + i; + part.* = if (src < rows.len) rows[src] else ""; + } + 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 = e.scratch.alloc(u8, @intCast(col - line_len)) catch { + if (owned) e.gpa.free(text); + return null; + }; + @memset(spaces, ' '); + 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) e.gpa.free(text); + text = padded; + owned = true; + } + if (owned) { + 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 (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, 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, new); + if (comptime Editor.lib) { + // the library's text, its own, where the edit ended found as a + // file's is (below) + const found = span orelse panes.File.spanOf(t.own orelse "", new); + const end = known_end orelse modal.positionAt(new, new.len - found.tail); + t.last_edit = .{ .row = @intCast(end.row), .col = @intCast(end.col) }; + if (t.own) |old| e.gpa.free(old); + t.own = new; + return; + } + 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, new); - if (kept.ptr != new.ptr) retireEditText(p, 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; @@ -532,14 +731,22 @@ 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); - panes.terminal.setEditText(p, pane, new); + 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(e, new); + for (0..pane.sel.len) |button| { + pane.clearPointerSelection(button); + pane.sel[button].state = .none; + } + e.gpa.free(overlay.text); + overlay.text = new; } /// The file whose text `t` edits in place (File.replace), the bytes it puts /// in and no copy of the text: a file's body, not a live query's (whose /// edits `setEditTextSpan` cuts to its line 1). -fn bodyFile(t: *Text) ?*panes.File.State { +inline fn bodyFile(t: *Text) ?*panes.File.State { + if (comptime Editor.lib) return null; if (t.what != .body) return null; const pane = t.pane() orelse return null; const f = if (pane.file) |*file| file else return null; @@ -549,18 +756,23 @@ 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), -/// 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 { +/// from the file's tree, where its grammar has an indent query; in the +/// library, from the parse handed in (normal.nodeTree), its `Language`'s. +fn treeIndent(e: Editor, t: *Text, text: []const u8, row: usize, at: usize) Error!?[]const u8 { + if (comptime Editor.lib) { + const tree = normal.nodeTree(e, t, text) orelse return null; + return indents.forNewline(e.scratch, indents.lang(tree.path) orelse return null, tree.root, text, row, at); + } 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 @@ -589,17 +801,17 @@ pub fn clampFileCursor(t: *Text) bool { return old_row != t.cur_row or old_col != t.cur_col; } -pub fn enterInsert(p: *Pardes, t: *Text, where: InsertAt, cnt: usize) Error!void { +pub fn enterInsert(e: Editor, 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(e, 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(e, t); t.select = false; t.restore_cursor = false; t.sticky_col = -1; - const text = flatSurface(p, t) catch { + const text = flatSurface(e, t) catch { t.mode = .insert; t.vsel.active = false; t.normal.clear(); @@ -631,8 +843,8 @@ pub fn enterInsert(p: *Pardes, t: *Text, where: InsertAt, cnt: usize) Error!void const terminal = t.what == .body and if (t.pane()) |pane| pane.isTerminal() else false; 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); + const grown = try std.mem.concat(e.gpa, u8, &.{ text, "\n" }); + try setEditText(e, t, grown); now = grown; // what setEditText keeps; `text` may be gone } const gc = modal.positionAt(now, gap); @@ -656,14 +868,14 @@ 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(e, 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 arena = p.scratch.allocator(); + const ind = (try treeIndent(e, t, eb.text, above, after)) orelse modal.indentText(modal.lineSlice(eb.text, row)); + const arena = e.scratch; const block_len = std.math.mul(usize, cnt, ind.len + 1) catch return error.OutOfMemory; const block = try arena.alloc(u8, block_len); var block_at: usize = 0; @@ -683,13 +895,13 @@ pub fn enterInsert(p: *Pardes, t: *Text, where: InsertAt, cnt: usize) Error!void .{ .row = row, .col = modal.lineSlice(eb.text, row).len } else .{ .row = row, .col = 0 }; - const new = try modal.insertAt(p.gpa, eb.text, at, block); - try setEditText(p, t, new); + const new = try modal.insertAt(e.gpa, eb.text, at, block); + try setEditText(e, 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; + if (cnt > 1 and e.multi_count.* == 1) opened: { + const t2 = flatSurface(e, t) catch break :opened; var rs: [Text.max_selections]modal.Selection = undefined; const m = @min(cnt, Text.max_selections); for (0..m) |k| { @@ -705,23 +917,23 @@ pub fn enterInsert(p: *Pardes, t: *Text, where: InsertAt, cnt: usize) Error!void t.show(); } -pub fn handleInsert(p: *Pardes, t: *Text, key: Key) void { - if (t.nsel == 0) return insertKey(p, t, key) catch |err| p.reportError(p.view().active, "edit", err); - normal.replaySels(p, t, .{ .insert = key }); +pub fn handleInsert(e: Editor, t: *Text, key: Key) void { + if (t.nsel == 0) return insertKey(e, t, key) catch |err| e.reportError(err); + normal.replaySels(e, t, .{ .insert = key }); } -pub fn insertKey(p: *Pardes, t: *Text, key: Key) Error!void { +pub fn insertKey(e: Editor, t: *Text, key: Key) Error!void { // helix aliases: normalize to the base key and re-dispatch - if (hit(key, config.insert_backspace_alias)) return insertKey(p, t, .{ .cp = Key.backspace }); - if (hit(key, config.insert_enter_alias)) return insertKey(p, t, .{ .cp = Key.enter }); - if (hit(key, config.insert_delete_alias)) return insertKey(p, t, .{ .cp = Key.delete }); + if (hit(key, config.insert_backspace_alias)) return insertKey(e, t, .{ .cp = Key.backspace }); + if (hit(key, config.insert_enter_alias)) return insertKey(e, t, .{ .cp = Key.enter }); + if (hit(key, config.insert_delete_alias)) return insertKey(e, t, .{ .cp = Key.delete }); // Ctrl-r <reg>: type this range's value of the register (helix // insert_register) 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; - if (y.len > 0) try insertKey(p, t, .{ .cp = key.cp, .text = y }); + const y = registerValue(e, t, key.cp) orelse return; + if (y.len > 0) try insertKey(e, t, .{ .cp = key.cp, .text = y }); return; } if (hit(key, config.insert_register)) { @@ -729,20 +941,20 @@ 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(e, 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); + if (!t.vsel.active) return insertEdit(e, t, key); + const old = flatSurface(e, t) catch return insertEdit(e, t, key); const r = t.primaryRange(old, 0); const c = modal.selectionCursor(old, r); const old_len = old.len; const explicit = t.vsel.explicit; t.vsel.active = false; - try insertEdit(p, t, key); + try insertEdit(e, t, key); if (t.mode != .insert) return; - const new = try flatSurface(p, t); + const new = try flatSurface(e, 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) @@ -795,13 +1007,13 @@ fn mapThroughEdit(pos: usize, at: usize, del: usize, ins: usize, before: bool) u return pos + ins; } -fn insertEdit(p: *Pardes, t: *Text, key: Key) Error!void { +fn insertEdit(e: Editor, t: *Text, key: Key) Error!void { t.vsel.active = false; - if (!t.cur_pinned) pinPaneCursor(p, t); + if (!t.cur_pinned) pinPaneCursor(e, t); _ = clampFileCursor(t); switch (key.cp) { Key.left, Key.right, Key.up, Key.down => { - const lines = try paneCursorLines(p, t); + const lines = try paneCursorLines(e, 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 @@ -830,7 +1042,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(e, t); const range = t.primaryRange(flat, 0); if (t.what != .body) return; const step: i32 = @intCast(@max(1, (t.pane() orelse return).rows)); @@ -845,7 +1057,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(e, t); const cur0 = t.toModalCursor(); t.cur_col = @intCast(panes.File.textLine(t, text, cur0.row).len); t.cur_pinned = true; @@ -854,7 +1066,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(e, 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) { @@ -867,11 +1079,11 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) Error!void { modal.insertOffset(text, c); const end = modal.advanceBy(c, key.text); if (bodyFile(t)) |f| { - if (!panes.File.replace(p, f, at, 0, key.text)) return; + if (!panes.File.replace(e.p, f, at, 0, key.text)) return; 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 }); + const new = try modal.spliceAlloc(e.gpa, text, at, at, key.text); + try setEditTextSpan(e, 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); @@ -882,7 +1094,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(e, 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 }; @@ -891,8 +1103,8 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) Error!void { const wr = modal.moveWord(e2.text, .{ .anchor = g, .head = g }, 1, .prev_word_start); 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); + const new = try modal.deleteSpan(e.gpa, e2.text, fc, c2); + try setEditTextEnd(e, t, new, fc); t.cur_row = @as(i32, @intCast(fc.row)) + e2.row0; t.cur_col = @intCast(fc.col); t.cur_pinned = true; @@ -901,7 +1113,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(e, 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 }; @@ -909,14 +1121,14 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) Error!void { const wr = modal.moveWord(e2.text, .{ .anchor = g, .head = g }, 1, .next_word_end); 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); + const new = try modal.deleteSpan(e.gpa, e2.text, c2, modal.positionAt(e2.text, to)); + try setEditTextEnd(e, 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 }); + const new = try modal.deleteSpan(e.gpa, text, .{ .row = c.row, .col = 0 }, c); + try setEditTextEnd(e, t, new, .{ .row = c.row, .col = 0 }); t.cur_col = 0; t.cur_pinned = true; t.show(); @@ -924,8 +1136,8 @@ 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); + const new = try modal.deleteSpan(e.gpa, text, c, .{ .row = c.row, .col = llen }); + try setEditTextEnd(e, t, new, c); t.cur_pinned = true; return; } @@ -937,8 +1149,8 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) Error!void { // itself moves down, blanks and all; else the blanks before the // 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 }); + const new = try modal.insertAt(e.gpa, text, .{ .row = c.row, .col = 0 }, "\n"); + try setEditTextEnd(e, t, new, .{ .row = c.row + 1, .col = 0 }); t.cur_row += 1; t.cur_pinned = true; t.show(); @@ -950,9 +1162,9 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) Error!void { // delimiter. Between a pair (helix insert_newline's // on_auto_pair) the closer goes to a line of its own at that // indent, the cursor to one a level in. - const arena = p.scratch.allocator(); + const arena = e.scratch; 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(e, 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) }); @@ -961,10 +1173,10 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) Error!void { // a tag keeps its blanks: they part its words, and the last // may be its read-only prefix's const kept = if (t.what == .body) std.mem.trimEnd(u8, before, " \t").len else c.col; - 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 }); + const cut = try modal.deleteSpan(e.gpa, text, .{ .row = c.row, .col = kept }, c); + defer e.gpa.free(cut); + const new = try modal.insertAt(e.gpa, cut, .{ .row = c.row, .col = kept }, block); + try setEditTextEnd(e, 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; @@ -988,16 +1200,16 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) Error!void { } // deleteSpan's splice, the line found once. const s = start + @min(prev, line.len); - const e = start + @min(c.col, line.len); + const stop = start + @min(c.col, line.len); if (bodyFile(t)) |f| { // As the whole text put back would be: a no-op edit // when there is nothing before the cursor to take. - if (!(if (e > s) panes.File.replace(p, f, s, e - s, "") else panes.File.replace(p, f, text.len, 0, ""))) return; + if (!(if (stop > s) panes.File.replace(e.p, f, s, stop - s, "") else panes.File.replace(e.p, f, text.len, 0, ""))) return; t.last_edit = .{ .row = @intCast(c.row), .col = @intCast(prev) }; } 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); + const new = if (stop > s) try modal.spliceAlloc(e.gpa, text, s, stop, "") else try e.gpa.dupe(u8, text); + const span: panes.File.Span = if (stop > s) .{ .head = s, .tail = text.len - stop } else .{ .head = text.len, .tail = 0 }; + try setEditTextSpan(e, t, new, .{ .row = c.row, .col = prev }, span); } t.cur_col = @intCast(prev); t.cur_pinned = true; @@ -1007,12 +1219,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(e, 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 }); + const new = try modal.deleteSpan(e.gpa, e2.text, .{ .row = row - 1, .col = prevlen }, .{ .row = row, .col = 0 }); + try setEditTextEnd(e, t, new, .{ .row = row - 1, .col = prevlen }); t.cur_row -= 1; t.cur_col = @intCast(prevlen); t.cur_pinned = true; @@ -1024,26 +1236,26 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) Error!void { const line = modal.lineSlice(text, c.row); 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); + const new = try modal.spliceAlloc(e.gpa, text, at, modal.nextGrapheme(text, at), ""); + try setEditTextEnd(e, 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(e, 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 }); + const new = try modal.deleteSpan(e.gpa, e2.text, .{ .row = row, .col = c.col }, .{ .row = row + 1, .col = 0 }); + try setEditTextEnd(e, t, new, .{ .row = row, .col = c.col }); } t.cur_pinned = true; }, Key.tab => { const ln = modal.lineSlice(text, c.row); - if (p.multi_count == 1 and c.col > 0 and c.col <= ln.len and ln[c.col - 1] == '.') dot: { + if (e.multi_count.* == 1 and c.col > 0 and c.col <= ln.len and ln[c.col - 1] == '.') dot: { if (t.what != .body) break :dot; const f = (t.pane() orelse break :dot).file orelse break :dot; if (f.output != null or !lsp.speaks(f.path)) break :dot; - const seq = p.lsp_ask.seq; - p.lspRequest(p.view().active, .completion, ""); - if (p.lsp_ask.seq != seq) return; + const seq = e.p.lsp_ask.seq; + e.p.lspRequest(e.p.view().active, .completion, ""); + if (e.p.lsp_ask.seq != seq) return; } // helix smart_tab: after text, Tab leaves the syntax node the // cursor is in; only where the file has a grammar, since the @@ -1054,23 +1266,23 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) Error!void { const pane = t.pane() orelse break :smart; const f = pane.file orelse break :smart; if (!syntax.supportsPath(f.path)) break :smart; - return normal.nodeSelect(p, pane, t, .parent_node_end); + return normal.nodeSelect(e, t, f.content, .parent_node_end); } - try insertTab(p, t); + try insertTab(e, t); }, else => {}, } } -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; +pub fn insertTab(e: Editor, t: *Text) Error!void { + const eb = (try editText(e, 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 }); + const new = try modal.insertAt(e.gpa, eb.text, c, " "[0..pad]); + try setEditTextEnd(e, t, new, .{ .row = c.row, .col = c.col + pad }); t.cur_col += @intCast(pad); t.cur_pinned = true; t.show(); @@ -1090,47 +1302,47 @@ 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 { +pub fn normalDelete(e: Editor, t: *Text, yank: bool) Error!void { t.select = false; // helix delete exits select mode t.sticky_col = -1; // A bare cursor is the 1-wide selection on its cell (helix), a '\n' // 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 - try pushUndo(p, t); - const eb = (try editTextEol(p, t, b)) orelse { + if (yank) try setYank(e, try vselText(e, t)); // read the range before the move + try pushUndo(e, t); + const eb = (try editTextEol(e, t, b)) orelse { t.vsel.active = false; return; }; - const new = try deleteSelection(p, t, eb); - try setEditText(p, t, new); + const new = try deleteSelection(e, t, eb); + try setEditText(e, t, new); t.cur_row = b.lo_row; t.cur_col = b.lo_col; t.clampCursor(new, eb.row0); } -pub fn normalYank(p: *Pardes, t: *Text) Error!void { +pub fn normalYank(e: Editor, 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(e, text); return; }; - try setYank(p, try if (t.vsel.active) vselText(p, t) else selText(p, t)); + try setYank(e, try if (t.vsel.active) vselText(e, t) else selText(e, t)); } -pub fn pasteText(p: *Pardes, t: *Text, y0: []const u8, before: bool, count: usize) Error!void { +pub fn pasteText(e: Editor, t: *Text, y0: []const u8, before: bool, count: usize) Error!void { if (y0.len == 0) return; - try pushUndo(p, t); + try pushUndo(e, t); t.select = false; t.sticky_col = -1; - const arena = p.scratch.allocator(); + const arena = e.scratch; var y: []const u8 = y0; if (count > 1) { const total = std.math.mul(usize, count, y0.len) catch return error.OutOfMemory; @@ -1142,7 +1354,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(e, 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]; @@ -1151,14 +1363,14 @@ pub fn pasteText(p: *Pardes, t: *Text, y0: []const u8, before: bool, count: usiz if (before) { const row: usize = @intCast(@max(0, b.lo_row - row0)); const block = try std.fmt.allocPrint(arena, "{s}\n", .{block_text}); - out = try modal.insertAt(p.gpa, eb.text, .{ .row = row, .col = 0 }, block); + out = try modal.insertAt(e.gpa, eb.text, .{ .row = row, .col = 0 }, block); t.cur_row = @as(i32, @intCast(row + n - 1)) + row0; } else { const row: usize = @intCast(@max(0, b.hi_row - row0)); - out = try modal.pasteLineBelow(p.gpa, eb.text, row, block_text); + out = try modal.pasteLineBelow(e.gpa, eb.text, row, block_text); t.cur_row = @as(i32, @intCast(row + n)) + row0; } - try setEditText(p, t, out); + try setEditText(e, 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; @@ -1174,8 +1386,8 @@ pub fn pasteText(p: *Pardes, t: *Text, y0: []const u8, before: bool, count: usiz const gcol = modal.nextGrapheme(high_line, hi_col); break :blk .{ .row = hrow, .col = gcol }; }; - const out = try modal.insertAt(p.gpa, eb.text, at, y); - try setEditText(p, t, out); + const out = try modal.insertAt(e.gpa, eb.text, at, y); + try setEditText(e, 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) { @@ -1190,25 +1402,25 @@ pub fn pasteText(p: *Pardes, t: *Text, y0: []const u8, before: bool, count: usiz t.show(); } -pub fn normalChange(p: *Pardes, t: *Text) Error!void { +pub fn normalChange(e: Editor, t: *Text) Error!void { t.select = false; 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(e, 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 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); + try setYank(e, try vselText(e, t)); + try pushUndo(e, t); + const d = try modal.deleteLines(e.gpa, eb.text, @intCast(@max(0, b.lo_row - eb.row0)), @intCast(@max(0, b.hi_row - eb.row0))); + try setEditText(e, 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 arena = e.scratch; 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); + const new = try modal.insertAt(e.gpa, d, .{ .row = row, .col = 0 }, block); + try setEditText(e, t, new); t.cur_row = @as(i32, @intCast(row)) + eb.row0; t.cur_col = @intCast(ind.len); t.vsel.active = false; @@ -1218,10 +1430,10 @@ pub fn normalChange(p: *Pardes, t: *Text) Error!void { t.show(); return; } - try setYank(p, try vselText(p, t)); - try pushUndo(p, t); - const new = try deleteSelection(p, t, eb); - try setEditText(p, t, new); + try setYank(e, try vselText(e, t)); + try pushUndo(e, t); + const new = try deleteSelection(e, t, eb); + try setEditText(e, t, new); t.cur_row = b.lo_row; t.cur_col = b.lo_col; t.clampCursor(new, eb.row0); @@ -1229,12 +1441,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(e, t); + const eb = (try editTextEol(e, t, selRows(t))) orelse return; const r = t.primaryRange(eb.text, eb.row0); - try setYank(p, 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 setYank(e, eb.text[r.anchor..r.head]); + const new = try modal.spliceAlloc(e.gpa, eb.text, r.anchor, r.head, ""); + try setEditText(e, 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; @@ -1242,9 +1454,9 @@ pub fn normalChange(p: *Pardes, t: *Text) Error!void { } /// `eb` without the primary range. -fn deleteSelection(p: *Pardes, t: *Text, eb: panes.EditText) Error![]u8 { +fn deleteSelection(e: Editor, t: *Text, eb: panes.EditText) Error![]u8 { const r = t.primaryRange(eb.text, eb.row0); - return modal.spliceAlloc(p.gpa, eb.text, @min(r.anchor, r.head), @max(r.anchor, r.head), ""); + return modal.spliceAlloc(e.gpa, eb.text, @min(r.anchor, r.head), @max(r.anchor, r.head), ""); } /// the rows an op's selection spans, absolute (what editText must cover) @@ -1266,15 +1478,15 @@ fn allSelRows(t: *Text) Bounds { /// Put in `new`, an edit of the buffer whose first line is surface row /// `row0`, and select `cells`, each range's cursor and anchor cells in it. -fn setEditCells(p: *Pardes, t: *Text, new: []u8, row0: i32, cells: []const Text.Cells, pri: usize, expl: bool) Error!void { +fn setEditCells(e: Editor, t: *Text, new: []u8, row0: i32, cells: []const Text.Cells, pri: usize, expl: bool) Error!void { var sels: [Text.max_selections]Text.SelRange = undefined; for (cells, sels[0..cells.len]) |c, *s| { const cc = modal.positionAt(new, c.cur); 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(e, t, new); + const text = try flatSurface(e, 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); @@ -1282,9 +1494,9 @@ fn setEditCells(p: *Pardes, t: *Text, new: []u8, row0: i32, cells: []const Text. /// `r<ch>`: overwrite the selection (or the cursor char) with ch — /// newlines included (helix), so `xrz` joins the selected lines -pub fn normalReplaceChar(p: *Pardes, t: *Text, ch: u21) Error!void { +pub fn normalReplaceChar(e: Editor, t: *Text, ch: u21) Error!void { t.select = false; - const eb = (try editTextEol(p, t, selRows(t))) orelse return; + const eb = (try editTextEol(e, 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); @@ -1293,11 +1505,11 @@ pub fn normalReplaceChar(p: *Pardes, t: *Text, ch: u21) Error!void { while (at < hi) : (graphemes += 1) at = modal.nextGrapheme(eb.text, at); var encoded: [4]u8 = undefined; 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); + const rep = try e.scratch.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); - const new = try modal.spliceAlloc(p.gpa, eb.text, lo, hi, rep); - try setEditText(p, t, new); + try pushUndo(e, t); + const new = try modal.spliceAlloc(e.gpa, eb.text, lo, hi, rep); + try setEditText(e, t, new); const end = lo + rep.len; const mapped: modal.Selection = if (before.head < before.anchor) .{ .anchor = end, .head = lo } @@ -1309,35 +1521,35 @@ pub fn normalReplaceChar(p: *Pardes, t: *Text, ch: u21) Error!void { /// `R`: replace the selection (or the cursor char) with the DEFAULT /// register. `SPC R` is the same verb over the system clipboard. /// The acme chords' replace: the default register as one text. -pub fn chordReplace(p: *Pardes, t: *Text) Error!void { - return replaceWithText(p, t, p.registers.text(p.gpa, Registers.default) orelse return); +pub fn chordReplace(e: Editor, t: *Text) Error!void { + return replaceWithText(e, t, e.registers.text(e.gpa, Registers.default) orelse return); } /// The pasted text becomes the selection, in the direction the replaced one /// had: a forward head lands on its last char (which for a trailing-newline /// `y` is the '\n' cell of the last full line), a backward one on its first. -fn replaceWithText(p: *Pardes, t: *Text, y: []const u8) Error!void { +fn replaceWithText(e: Editor, 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(e, t, selRows(t))) orelse return; const r = t.primaryRange(eb.text, eb.row0); const lo = @min(r.anchor, r.head); - try pushUndo(p, t); - const new = try modal.spliceAlloc(p.gpa, eb.text, lo, @max(r.anchor, r.head), y); - try setEditText(p, t, new); + try pushUndo(e, t); + const new = try modal.spliceAlloc(e.gpa, eb.text, lo, @max(r.anchor, r.head), y); + try setEditText(e, 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); } /// `~` / `` ` `` / ``Alt-` ``: case-map the selection (or the cursor char), /// keeping the selection (helix) -pub fn normalCase(p: *Pardes, t: *Text, op: modal.CaseOp) Error!void { +pub fn normalCase(e: Editor, t: *Text, op: modal.CaseOp) Error!void { t.select = false; - const eb = (try editTextEol(p, t, selRows(t))) orelse return; + const eb = (try editTextEol(e, t, selRows(t))) orelse return; const r = t.primaryRange(eb.text, eb.row0); - try pushUndo(p, 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 pushUndo(e, t); + const new = try modal.changeCase(e.gpa, eb.text, @min(r.anchor, r.head), @max(r.anchor, r.head), op); + try setEditText(e, t, new); } /// Where `pos` lands after `chs`, which are ascending and disjoint. @@ -1356,23 +1568,41 @@ fn mapThroughChanges(chs: []const sam.Op, pos: usize) usize { return @intCast(@as(i64, @intCast(pos)) + delta); } +/// The bytes from `from` to `to` replaced with `bytes` (the library's +/// `replace`), every range following the edit as `normalAlign`'s do; a step +/// to undo of its own when `step`, else part of the one before it. +pub fn replaceBytes(e: Editor, t: *Text, from: usize, to: usize, bytes: []const u8, step: bool) Error!void { + const expl = t.vsel.active and t.vsel.explicit; + const text = try flatSurface(e, t); + var rs: [Text.max_selections]modal.Selection = undefined; + const got = t.ranges(text, 0, &rs); + const ch = [1]sam.Op{.{ .q0 = @intCast(from), .q1 = @intCast(to), .text = bytes }}; + var cells: [Text.max_selections]Text.Cells = undefined; + for (rs[0..got.n], cells[0..got.n]) |r, *cell| { + const c = Text.rangeCells(text, r); + cell.* = .{ .cur = mapThroughChanges(&ch, c.cur), .anc = mapThroughChanges(&ch, c.anc) }; + } + if (step) try pushUndo(e, t); + try setEditCells(e, t, try sam.apply(e.gpa, text, &ch), 0, cells[0..got.n], got.pri, expl); +} + /// `J` / `Alt-J`: join every range's lines with the next, in ONE edit /// (helix join_selections): the newline and the next line's leading blanks /// become one space, or nothing when that line is blank, and a line two /// ranges share is joined once. `Alt-J` then selects the spaces it put in. -pub fn normalJoin(p: *Pardes, t: *Text, select_space: bool) Error!void { +pub fn normalJoin(e: Editor, t: *Text, select_space: bool) Error!void { const expl = t.vsel.active and t.vsel.explicit; // 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(e, 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); // Lines, their count and rows from the file's index when it has one: // counted from the top, a J in a 7 MB file was 8 ms. const nlines = panes.File.textLineCount(t, text); - const arena = p.scratch.allocator(); + const arena = e.scratch; var changes: std.ArrayList(sam.Op) = .empty; var next: usize = 0; // lines below this one are joined already for (rs[0..got.n]) |r| { @@ -1396,8 +1626,8 @@ 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); - const new = try sam.apply(p.gpa, text, chs[0..chs_len]); + try pushUndo(e, t); + const new = try sam.apply(e.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 var cells: [Text.max_selections]Text.Cells = undefined; @@ -1422,16 +1652,16 @@ pub fn normalJoin(p: *Pardes, t: *Text, select_space: bool) Error!void { n = got.n; pri = got.pri; } - try setEditCells(p, t, new, eb.row0, cells[0..n], pri, expl); + try setEditCells(e, t, new, eb.row0, cells[0..n], pri, expl); } /// `Alt-)` / `Alt-(`: rotate the TEXT of the selections forward / back by /// one range (count ranges), in one edit (helix reorder_selection_contents). /// Each range comes back over the text it now holds, in its own direction, /// and the primary moves with the text it had. -pub fn normalRotateContents(p: *Pardes, t: *Text, forward: bool, cnt: usize) Error!void { +pub fn normalRotateContents(e: Editor, 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(e, 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); @@ -1447,8 +1677,8 @@ 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); - const new = try sam.apply(p.gpa, text, chs[0..n]); + try pushUndo(e, t); + const new = try sam.apply(e.gpa, text, chs[0..n]); var cells: [Text.max_selections]Text.Cells = undefined; var delta: i64 = 0; for (chs[0..n], rs[0..n], cells[0..n]) |ch, r, *cell| { @@ -1457,16 +1687,16 @@ pub fn normalRotateContents(p: *Pardes, t: *Text, forward: bool, cnt: usize) Err const to = from + ch.text.len; cell.* = Text.rangeCells(new, if (r.anchor > r.head) .{ .anchor = to, .head = from } else .{ .anchor = from, .head = to }); } - try setEditCells(p, t, new, eb.row0, cells[0..n], if (forward) (got.pri + by) % n else (got.pri + n - by) % n, expl); + try setEditCells(e, t, new, eb.row0, cells[0..n], if (forward) (got.pri + by) % n else (got.pri + n - by) % n, expl); } /// `&`: pad the selections into columns, in one edit (helix /// align_selections). The k-th range of every line is a column; spaces go in /// before each range so its head lands on the column's widest head, measured /// in display cells. A range over several lines aligns nothing. -pub fn normalAlign(p: *Pardes, t: *Text) Error!void { +pub fn normalAlign(e: Editor, 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(e, 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); @@ -1494,7 +1724,7 @@ pub fn normalAlign(p: *Pardes, t: *Text) Error!void { t.select = false; // helix exit_select_mode // each column's head position, left to right for (1..n) |i| widths[i] += widths[i - 1]; - const arena = p.scratch.allocator(); + const arena = e.scratch; var chs: [Text.max_selections]sam.Op = undefined; var m: usize = 0; line = std.math.maxInt(usize); @@ -1515,24 +1745,24 @@ pub fn normalAlign(p: *Pardes, t: *Text) Error!void { m += 1; } if (m == 0) return; - try pushUndo(p, t); - const new = try sam.apply(p.gpa, text, chs[0..m]); + try pushUndo(e, t); + const new = try sam.apply(e.gpa, text, chs[0..m]); var cells: [Text.max_selections]Text.Cells = undefined; for (rs[0..n], cells[0..n]) |r, *cell| { const c = Text.rangeCells(text, r); cell.* = .{ .cur = mapThroughChanges(chs[0..m], c.cur), .anc = mapThroughChanges(chs[0..m], c.anc) }; } - try setEditCells(p, t, new, eb.row0, cells[0..n], got.pri, expl); + try setEditCells(e, t, new, eb.row0, cells[0..n], got.pri, expl); } -pub fn normalIndent(p: *Pardes, t: *Text, cnt: usize, add: bool) Error!void { +pub fn normalIndent(e: Editor, 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(e, 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); - const arena = p.scratch.allocator(); + const arena = e.scratch; // one run of spaces, sliced per line: `>` never inserts more than this const pad = try arena.alloc(u8, modal.INDENT_W * cnt); @memset(pad, ' '); @@ -1569,16 +1799,16 @@ pub fn normalIndent(p: *Pardes, t: *Text, cnt: usize, add: bool) Error!void { } } if (chs_len == 0) return; - try pushUndo(p, t); + try pushUndo(e, 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)) }) else cur_off; - const new = try sam.apply(p.gpa, text, chs[0..chs_len]); + const new = try sam.apply(e.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(e, t, new); const cc = modal.positionAt(new, nc); const ac = modal.positionAt(new, na); t.cur_row = @as(i32, @intCast(cc.row)) + eb.row0; @@ -1592,19 +1822,19 @@ pub fn normalIndent(p: *Pardes, t: *Text, cnt: usize, add: bool) Error!void { t.show(); } -pub fn normalToggleComment(p: *Pardes, t: *Text) Error!void { +pub fn normalToggleComment(e: Editor, t: *Text) Error!void { const expl = t.vsel.active and t.vsel.explicit; 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(e, 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(e, 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); const nlines = modal.cursorLineCount(text); - const arena = p.scratch.allocator(); + const arena = e.scratch; // the lines the ranges cover, each ONE ONCE and in order (helix's // min_next_line: two cursors on one line comment it once) const lines = try arena.alloc(usize, nlines); @@ -1620,11 +1850,11 @@ pub fn normalToggleComment(p: *Pardes, t: *Text) Error!void { } next = @max(next, end); } - const file = if (t.what != .body) null else if (t.pane()) |pane| pane.file else null; + const file = if (Editor.lib or t.what != .body) null else if (t.pane()) |pane| pane.file else null; const ext = if (file) |f| std.Io.Dir.path.extension(f.path) else ""; var token: []const u8 = config.comment_token_default; lang: for (config.comment_tokens) |row| { - for (row.exts) |e| if (std.ascii.eqlIgnoreCase(ext, e)) { + for (row.exts) |x| if (std.ascii.eqlIgnoreCase(ext, x)) { token = row.token; break :lang; }; @@ -1660,46 +1890,46 @@ 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(e, 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; - const new = try sam.apply(p.gpa, text, chs[0..chs_len]); + const new = try sam.apply(e.gpa, text, chs[0..chs_len]); for (rs[0..got.n], cells[0..got.n]) |r, *cell| cell.* = Text.rangeCells(new, .{ .anchor = mapThroughChanges(chs[0..chs_len], r.anchor), .head = mapThroughChanges(chs[0..chs_len], r.head), }); - try setEditCells(p, t, new, eb.row0, cells[0..got.n], got.pri, expl); + try setEditCells(e, t, new, eb.row0, cells[0..got.n], got.pri, expl); } -pub fn normalAdjustNumber(p: *Pardes, t: *Text, delta: i64) Error!void { - const eb = (try editTextEol(p, t, selRows(t))) orelse return; +pub fn normalAdjustNumber(e: Editor, t: *Text, delta: i64) Error!void { + const eb = (try editTextEol(e, 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); + const rep = (modal.incrementDecimal(e.scratch, eb.text[lo..hi], delta) catch null) orelse return; + try pushUndo(e, t); t.select = false; - const new = try modal.spliceAlloc(p.gpa, eb.text, lo, hi, rep); - try setEditText(p, t, new); + const new = try modal.spliceAlloc(e.gpa, eb.text, lo, hi, rep); + try setEditText(e, t, new); t.setRange(new, eb.row0, .{ .anchor = lo, .head = lo + rep.len }, false); } /// `]Space` / `[Space`: add blank lines below/above, the cursor staying /// on its text line (the selection rides along) -pub fn addNewline(p: *Pardes, t: *Text, below: bool, cnt: usize) Error!void { +pub fn addNewline(e: Editor, 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(e, t, at, at, -1)) orelse return; + try pushUndo(e, t); const row: usize = @intCast(@max(0, at - eb.row0)); - const arena = p.scratch.allocator(); + const arena = e.scratch; 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); + const new = try modal.insertAt(e.gpa, eb.text, .{ .row = row, .col = if (below) llen else 0 }, nl); + try setEditText(e, t, new); if (!below) { // the whole selection moved down; stay on it const n: i32 = @intCast(cnt); @@ -1722,7 +1952,7 @@ fn pairFor(ch: u21) ?struct { o: u8, c: u8 } { }; } -pub fn textobjectSelect(p: *Pardes, t: *Text, text: []const u8, obj: u21, around: bool) Error!void { +pub fn textobjectSelect(e: Editor, t: *Text, text: []const u8, obj: u21, around: bool) Error!void { const range = t.primaryRange(text, 0); // helix's tree-sitter objects, from the grammar's textobjects query const object: ?modal.Normal.Object = switch (obj) { @@ -1736,11 +1966,8 @@ pub fn textobjectSelect(p: *Pardes, t: *Text, text: []const u8, obj: u21, around else => null, }; if (object) |o| { - if (t.what != .body) return; - const pane = t.pane() orelse return; - const f = if (pane.file) |*file| file else return; - const tree = normal.fileTree(p, f) orelse return; - const s = syntax.objectAt(f.path, tree, text, o, around, modal.selectionCursor(text, range)) orelse return; + const tree = normal.nodeTree(e, t, text) orelse return; + const s = syntax.objectAt(tree.path, tree.root, text, o, around, modal.selectionCursor(text, range)) orelse return; return t.setRange(text, 0, .{ .anchor = s.from, .head = s.to }, false); } switch (obj) { @@ -1754,7 +1981,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(e, t); const cur = modal.positionAt(text, modal.selectionCursor(text, range)); const pair: ?modal.Range = switch (obj) { '\'', '"', '`' => modal.enclosingQuote(lines, cur, @intCast(obj)), @@ -1778,26 +2005,26 @@ pub fn textobjectSelect(p: *Pardes, t: *Text, text: []const u8, obj: u21, around /// `ms<ch>`: wrap the selection (or the cursor char) in a pair; the wrap /// including the pair becomes the selection -pub fn surroundAdd(p: *Pardes, t: *Text, ch: u21) Error!void { +pub fn surroundAdd(e: Editor, 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(e, 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(e, 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); + const new = try sam.apply(e.gpa, eb.text, &.{ .{ .q0 = @intCast(lo), .q1 = @intCast(lo), .text = &open }, .{ .q0 = @intCast(hi), .q1 = @intCast(hi), .text = &close } }); + try setEditText(e, 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); } /// `md<ch>`: delete the enclosing pair's chars; the cursor maps through -pub fn surroundDelete(p: *Pardes, t: *Text, ch: u21) Error!void { +pub fn surroundDelete(e: Editor, t: *Text, ch: u21) Error!void { const pr = pairFor(ch) orelse return; - const lines = try paneCursorLines(p, t); + const lines = try paneCursorLines(e, t); const cur = t.toModalCursor(); const r = (if (pr.o == pr.c) modal.enclosingQuote(lines, cur, pr.o) @@ -1805,20 +2032,20 @@ 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(e, 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(e, 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 }); - const new = try sam.apply(p.gpa, text, &.{ + const new = try sam.apply(e.gpa, text, &.{ .{ .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(e, t, new); var nc = cur_off; if (nc > b_off) nc -= 1; if (nc > a_off) nc -= 1; @@ -1831,56 +2058,64 @@ pub fn surroundDelete(p: *Pardes, t: *Text, ch: u21) Error!void { } /// `mr<from><to>`: swap the enclosing <from> pair's chars for <to>'s -pub fn surroundReplace(p: *Pardes, t: *Text, from: u21, to: u21) Error!void { +pub fn surroundReplace(e: Editor, 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(e, 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(e, 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); - const out = try p.gpa.dupe(u8, eb.text); + try pushUndo(e, t); + const out = try e.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(e, 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), + // A B1 chord took its one undo point when it began (mouse.zig); + // the library's text keeps its own undo, as a tag does. + .body => if (comptime Editor.lib) + return t.remember(e.gpa, if (t.own) |own| .{ .text = own } else null) + else 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 panes.terminal.pushUndo(p, pane); + 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 (comptime Editor.lib) return stepOwn(e, t, back); + 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); - return panes.terminal.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(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; } @@ -2078,28 +2313,28 @@ test "insert and normal modes edit complete Unicode graphemes" { pane.body.mode = .insert; pane.body.cur_col = 22; // on z, after the CJK grapheme - try insertKey(p, &pane.body, .{ .cp = Key.left }); + try insertKey(p.editor(), &pane.body, .{ .cp = Key.left }); try std.testing.expectEqual(@as(i32, 19), pane.body.cur_col); - try insertKey(p, &pane.body, .{ .cp = Key.left }); + try insertKey(p.editor(), &pane.body, .{ .cp = Key.left }); try std.testing.expectEqual(@as(i32, 4), pane.body.cur_col); - try insertKey(p, &pane.body, .{ .cp = Key.right }); + try insertKey(p.editor(), &pane.body, .{ .cp = Key.right }); try std.testing.expectEqual(@as(i32, 19), pane.body.cur_col); - try insertKey(p, &pane.body, .{ .cp = Key.backspace }); + try insertKey(p.editor(), &pane.body, .{ .cp = Key.backspace }); try std.testing.expectEqualStrings("ae\u{301}界z", pane.file.?.content); try std.testing.expectEqual(@as(i32, 4), pane.body.cur_col); pane.body.mode = .normal; pane.body.cur_col = 1; - try normalDelete(p, &pane.body, false); + try normalDelete(p.editor(), &pane.body, false); try std.testing.expectEqualStrings("a界z", pane.file.?.content); try std.testing.expectEqual(@as(i32, 1), pane.body.cur_col); - try normalReplaceChar(p, &pane.body, 'λ'); + try normalReplaceChar(p.editor(), &pane.body, 'λ'); try std.testing.expectEqualStrings("aλz", pane.file.?.content); panes.File.setContent(p, &pane.file.?, try gpa.dupe(u8, "界a")); pane.body.cur_col = 3; pane.body.vsel = .{ .active = true, .row = 0, .col = 0, .explicit = true }; - try normalReplaceChar(p, &pane.body, 'λ'); + try normalReplaceChar(p.editor(), &pane.body, 'λ'); try std.testing.expectEqualStrings("λλ", pane.file.?.content); try std.testing.expectEqual(@as(i32, 2), pane.body.cur_col); try std.testing.expectEqual(@as(i32, 0), pane.body.vsel.col); @@ -2107,13 +2342,14 @@ test "insert and normal modes edit complete Unicode graphemes" { panes.File.setContent(p, &pane.file.?, try gpa.dupe(u8, "界a")); pane.body.cur_col = 0; pane.body.vsel = .{ .active = true, .row = 0, .col = 3, .explicit = true }; - try normalReplaceChar(p, &pane.body, 'λ'); + try normalReplaceChar(p.editor(), &pane.body, 'λ'); try std.testing.expectEqualStrings("λλ", pane.file.?.content); try std.testing.expectEqual(@as(i32, 0), pane.body.cur_col); try std.testing.expectEqual(@as(i32, 2), pane.body.vsel.col); } test "flat text replacement preserves a terminal fragment origin and direction" { + if (pardes.Term.ops == null) return error.SkipZigTest; // a shell const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 60, .rows = 12 }); defer p.deinit(); p.update(.{ .output = .{ .pane = 0, .bytes = "first\r\nsecond\r\nthird\r\n界a\r\n" } }); @@ -2123,7 +2359,7 @@ test "flat text replacement preserves a terminal fragment origin and direction" pane.body.cur_row = 3; pane.body.cur_col = 0; pane.body.vsel = .{ .active = true, .row = 3, .col = 3, .explicit = true }; - try normalReplaceChar(p, &pane.body, 'λ'); + try normalReplaceChar(p.editor(), &pane.body, 'λ'); try std.testing.expectEqual(@as(i32, 3), pane.ovl.?.row); try std.testing.expectEqualStrings("λλ", pane.ovl.?.text); try std.testing.expectEqual(@as(i32, 3), pane.body.cur_row); @@ -2131,10 +2367,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")); } @@ -2231,16 +2467,17 @@ 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); } } test "a paste into a terminal is xterm's: bracketed when the program asked, control bytes spaces, so no ESC[201~ ends the brackets early" { + if (pardes.Term.ops == null) return error.SkipZigTest; // a shell const gpa = std.testing.allocator; const p = try Pardes.init(gpa, .{ .cols = 60, .rows = 16, .tty_only = true }); defer p.deinit(); @@ -2299,9 +2536,9 @@ test "typing and backspace through the line index keep the text and the index as pane.body.cur_col = @intCast(random.uintLessThan(usize, line.len + 1)); pane.body.cur_pinned = true; if (random.boolean()) { - try insertKey(p, &pane.body, .{ .cp = 'x', .text = "x" }); + try insertKey(p.editor(), &pane.body, .{ .cp = 'x', .text = "x" }); } else { - try insertKey(p, &pane.body, .{ .cp = Key.backspace }); + try insertKey(p.editor(), &pane.body, .{ .cp = Key.backspace }); } // the index the edits kept, against one built from the text const kept = try gpa.dupe(usize, try panes.File.lineIndex(gpa, &pane.file.?)); @@ -2330,7 +2567,7 @@ test "J joins a line with the next, its indent dropped, a space between unless t pane.body.cur_row = case.row; pane.body.cur_col = 0; if (case.rows > 0) pane.body.vsel = .{ .active = true, .row = case.row + case.rows, .col = 0, .explicit = true }; - try normalJoin(p, &pane.body, false); + try normalJoin(p.editor(), &pane.body, false); try std.testing.expectEqualStrings(case.want, pane.file.?.content); } } |
