summaryrefslogtreecommitdiff
path: root/src/edit.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/edit.zig')
-rw-r--r--src/edit.zig217
1 files changed, 207 insertions, 10 deletions
diff --git a/src/edit.zig b/src/edit.zig
index 3881020a..5b94c0d8 100644
--- a/src/edit.zig
+++ b/src/edit.zig
@@ -222,7 +222,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);
@@ -260,7 +260,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 +275,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);
@@ -386,7 +386,7 @@ 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(p, pane);
}
pub fn paneByteAtDisplay(p: *Pardes, t: *Text, row: i32, from_raw: i32, display_col: i32) i32 {
@@ -418,6 +418,111 @@ pub fn pinPaneCursor(p: *Pardes, t: *Text) void {
t.cur_col = paneByteAtDisplay(p, 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;
+}
+
+/// 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(p: *Pardes, t: *Text) Error![]const u8 {
switch (t.what) {
.body => {},
@@ -429,8 +534,25 @@ pub fn flatSurface(p: *Pardes, t: *Text) Error![]const u8 {
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(p, pane);
+ // The kept rows' own join when the lines are them (no edit copy), not
+ // made again on every keystroke; a join of them in scratch else.
+ const cache = &p.shell_rows;
+ if (!cache.stale and cache.pane == pane and
+ lines.ptr == cache.rows.ptr and lines.len == cache.rows.len) return cache.text;
+ var total: usize = if (lines.len > 0) lines.len - 1 else 0;
+ for (lines) |line| total += line.len;
+ const text = try p.scratch.allocator().alloc(u8, total);
+ 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 {
@@ -455,7 +577,71 @@ fn editText(p: *Pardes, t: *Text, lo: i32, hi: i32, col: i32) Error!?panes.EditT
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(p, pane);
+ row0 = old.row - @max(0, up);
+ covered = old.rows + @max(0, up) + @max(0, down);
+ const up_len: usize = @intCast(@max(0, up));
+ const down_len: usize = @intCast(@max(0, down));
+ const parts = try p.scratch.allocator().alloc([]const u8, up_len + 1 + down_len);
+ 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(p.gpa, "\n", parts);
+ owned = true;
+ }
+
+ const row: usize = @intCast(@max(0, want_lo - row0));
+ const line_len: i32 = @intCast(modal.lineSlice(text, row).len);
+ if (col > line_len) {
+ const spaces = p.scratch.allocator().alloc(u8, @intCast(col - line_len)) catch {
+ if (owned) p.gpa.free(text);
+ return null;
+ };
+ @memset(spaces, ' ');
+ const padded = modal.insertAt(p.gpa, text, .{ .row = row, .col = @intCast(line_len) }, spaces) catch {
+ if (owned) p.gpa.free(text);
+ return null;
+ };
+ if (owned) p.gpa.free(text);
+ text = padded;
+ owned = true;
+ }
+ if (owned) {
+ if (pane.ovl) |overlay| p.gpa.free(overlay.text);
+ pane.ovl = .{ .row = row0, .rows = covered, .text = text };
+ }
+ // A terminal's text is its program's: edited here it is a copy, to run
+ // with Tab or B2, gone when the program draws again. Said at its first
+ // edit, so it never looks like an edit that was lost.
+ if (fresh) for (p.panes, 0..) |slot, id| if (slot == pane)
+ p.setMessage(id, "a copy of the terminal's text: Tab or B2 runs it; what the program draws next replaces it (Ctrl-b types to the program)");
+ return .{ .text = pane.ovl.?.text, .row0 = pane.ovl.?.row };
}
fn editTextEol(p: *Pardes, t: *Text, b: Bounds) Error!?panes.EditText {
@@ -533,7 +719,14 @@ fn setEditTextSpan(p: *Pardes, t: *Text, new: []u8, known_end: ?modal.Cursor, sp
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);
+ // A terminal's edit copy rewritten: the text it replaces freed.
+ const overlay = if (pane.ovl) |*value| value else return retireEditText(p, new);
+ for (0..pane.sel.len) |button| {
+ pane.clearPointerSelection(button);
+ pane.sel[button].state = .none;
+ }
+ p.gpa.free(overlay.text);
+ overlay.text = new;
}
/// The file whose text `t` edits in place (File.replace), the bytes it puts
@@ -1863,7 +2056,7 @@ pub fn pushUndo(p: *Pardes, t: *Text) error{OutOfMemory}!void {
}
const pane = t.pane() orelse return;
if (pane.file != null) return panes.File.pushUndo(p, pane);
- return panes.terminal.pushUndo(p, pane);
+ return pane.body.remember(p.gpa, pane.ovl);
}
/// Undo (back) or redo one step of the text's history: its file's, its
@@ -1873,7 +2066,11 @@ pub fn undoStep(p: *Pardes, t: *Text, back: bool) error{OutOfMemory}!void {
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);
+ // A terminal's edit copy, as it was.
+ const to = (try pane.body.step(p.gpa, pane.ovl, back)) orelse return;
+ if (pane.ovl) |o| p.gpa.free(o.text);
+ pane.ovl = to.ovl;
+ pane.ensureCursorVisible();
}
/// Undo or redo an edit of a text's own characters; stepping back past its