summaryrefslogtreecommitdiff
path: root/core/ninep/pane.zig
diff options
context:
space:
mode:
Diffstat (limited to 'core/ninep/pane.zig')
-rw-r--r--core/ninep/pane.zig137
1 files changed, 95 insertions, 42 deletions
diff --git a/core/ninep/pane.zig b/core/ninep/pane.zig
index d47b637b..2616c735 100644
--- a/core/ninep/pane.zig
+++ b/core/ninep/pane.zig
@@ -146,11 +146,28 @@ pub const State = struct {
// ---- what a pane is ----
-pub fn bodyOf(pane: *const Pane) []const u8 {
+/// A pane's text as 9P serves it: a file's text (File.fileText: the
+/// buffer less its read-only lines, the `Changes` layer's, which are no
+/// text of the file's). Every address, `dot` and range over 9P is of this
+/// text (`fileDot`); a write to it is the file's (`spliceBody`).
+pub fn bodyOf(p: *Pardes, pane: *const Pane) []const u8 {
+ if (pane.file) |*f| return panes.File.fileText(f, p.scratch.allocator()) catch "";
+ return "";
+}
+
+/// The buffer the editor edits (with the layer's lines): what the
+/// editor's own dot is of (`dotOf`, `setDot`).
+fn bufferOf(pane: *const Pane) []const u8 {
if (pane.file) |*f| return f.content;
return "";
}
+/// Whether 9P reads of the pane are a copy of its file's text, not a
+/// region of its buffer (`fsPayload` reads the buffer).
+fn layered(pane: *const Pane) bool {
+ return if (pane.file) |*f| f.read_only.len > 0 else false;
+}
+
pub fn fileOf(pane: *Pane) ?*panes.File.State {
return if (pane.file) |*f| f else null;
}
@@ -240,7 +257,7 @@ fn positionAt(pane: *const Pane, text: []const u8, off: usize) modal.Cursor {
}
pub fn dotOf(pane: *Pane) State.Range {
- const text = bodyOf(pane);
+ const text = bufferOf(pane);
// In runes, as every address is (modal.runeStart).
const head = runeOffsetAt(pane, text, .{ .row = @intCast(@max(0, pane.body.cur_row)), .col = @intCast(@max(0, pane.body.cur_col)) });
if (!pane.body.vsel.active) return .{ .q0 = clip(head), .q1 = clip(head) };
@@ -251,7 +268,7 @@ pub fn dotOf(pane: *Pane) State.Range {
}
pub fn setDot(tab: u16, pane: *Pane, r: State.Range) void {
- const text = bodyOf(pane);
+ const text = bufferOf(pane);
const q0 = @min(@as(usize, r.q0), text.len);
const q1 = @max(q0, @min(@as(usize, r.q1), text.len));
const a = runePositionAt(pane, text, q0);
@@ -268,7 +285,7 @@ pub fn setDot(tab: u16, pane: *Pane, r: State.Range) void {
/// a write shown is not a write that moves dot (acme's textinsert leaves
/// dot where its rule puts it; only the view follows the text).
pub fn showOffset(tab: u16, pane: *Pane, off: usize) void {
- const text = bodyOf(pane);
+ const text = bufferOf(pane);
const c = positionAt(pane, text, @min(off, text.len));
// Where dot is now, kept: pinned, so the view moving under it does not
// move it to the view's top (pinCursor).
@@ -282,6 +299,26 @@ pub fn showOffset(tab: u16, pane: *Pane, off: usize) void {
pane.body.cur_col = col;
}
+/// `dotOf` in the file's text's offsets, as 9P serves it (`bodyOf`): a dot
+/// in a read-only line is after the file line above it.
+pub fn fileDot(pane: *Pane) State.Range {
+ const d = dotOf(pane);
+ const f = if (pane.file) |*file| file else return d;
+ return .{ .q0 = clip(panes.File.fileOffset(f, d.q0)), .q1 = clip(panes.File.fileOffset(f, d.q1)) };
+}
+
+/// `setDot` from the file's text's offsets (`fileDot`).
+pub fn setFileDot(tab: u16, pane: *Pane, r: State.Range) void {
+ const f = if (pane.file) |*file| file else return setDot(tab, pane, r);
+ setDot(tab, pane, .{ .q0 = clip(panes.File.bufferOffset(f, r.q0, false)), .q1 = clip(panes.File.bufferOffset(f, r.q1, r.q1 > r.q0)) });
+}
+
+/// `showOffset` of a file's text's offset.
+pub fn showFileOffset(tab: u16, pane: *Pane, off: usize) void {
+ const f = if (pane.file) |*file| file else return showOffset(tab, pane, off);
+ showOffset(tab, pane, panes.File.bufferOffset(f, off, false));
+}
+
pub fn clampAddr(pf: *State, len: usize) void {
const n = clip(len);
pf.addr.q0 = @min(pf.addr.q0, n);
@@ -314,18 +351,21 @@ pub fn wholeUtf8(data: []const u8) usize {
return data.len;
}
+/// `bytes` over `q0`..`q1` of the file's text (`bodyOf`'s offsets): the
+/// file's edit (File.replaceFile), never refused for the layer's lines.
pub fn spliceBody(p: *Pardes, pane: *Pane, q0: usize, q1: usize, bytes: []const u8) ?usize {
const f = fileOf(pane) orelse return null;
const take = if (bytes.len == 0) 0 else wholeUtf8(bytes);
- const lo = @min(q0, f.content.len);
- const hi = @max(lo, @min(q1, f.content.len));
+ const text = bodyOf(p, pane);
+ const lo = @min(q0, text.len);
+ const hi = @max(lo, @min(q1, text.len));
// An empty range deleted, or a range replaced by what it holds: no
// edit, so no undo step and no new revision.
- if (std.mem.eql(u8, f.content[lo..hi], bytes[0..take])) return take;
+ if (std.mem.eql(u8, text[lo..hi], bytes[0..take])) return take;
const join = pane.fs.joined == f.revision;
pane.fs.joined = null;
if (!pane.fs.nomark and !join) panes.File.pushUndo(p, pane) catch return null;
- if (!panes.File.replace(p, f, lo, hi - lo, bytes[0..take])) return null;
+ if (!panes.File.replaceFile(p, f, lo, hi - lo, bytes[0..take])) return null;
return take;
}
@@ -340,7 +380,7 @@ pub fn fileSize(p: *Pardes, id: usize, f: PaneFile) u64 {
const pf = &pane.fs;
return switch (f) {
// A terminal's body reads its screen and history, as text.
- .body => if (pane.file == null and pane.isTerminal()) pardes.Term.scrollbackLen(pane.terminal) else if (kindText(p, pane)) |t| t.len else bodyOf(pane).len,
+ .body => if (pane.file == null and pane.isTerminal()) pardes.Term.scrollbackLen(pane.terminal) else if (kindText(p, pane)) |t| t.len else bodyOf(p, pane).len,
// What addr selects, generated by each read: no length, as acme's
// data and xdata have none; and rows, made by each read.
.data, .xdata, .rows => 0,
@@ -351,7 +391,7 @@ pub fn fileSize(p: *Pardes, id: usize, f: PaneFile) u64 {
break :ctl ctl.paneText(p, pane, &buf).len;
},
.sel => sel: {
- const d = dotOf(pane);
+ const d = fileDot(pane);
break :sel @max(d.q0, d.q1) - d.q0;
},
.addr => if (pf.addr_failed) 0 else range_len,
@@ -407,14 +447,14 @@ pub fn read(p: *Pardes, req: Req, id: usize, pane: *Pane, file: PaneFile) Reply
// A failed address reads empty, as an unset limit does: there is
// none. data and xdata are what refuse.
.addr => readRange(p, req, if (pf.addr_failed) null else addr: {
- clampAddr(pf, bodyOf(pane).len);
+ clampAddr(pf, bodyOf(p, pane).len);
break :addr pf.addr;
}),
- .dot => readRange(p, req, dotOf(pane)),
+ .dot => readRange(p, req, fileDot(pane)),
.limit => readRange(p, req, pf.limit),
- .data => readData(req, id, pane, pf, false),
- .xdata => readData(req, id, pane, pf, true),
- .sel => readSel(req, id, pane),
+ .data => readData(p, req, id, pane, pf, false),
+ .xdata => readData(p, req, id, pane, pf, true),
+ .sel => readSel(p, req, id, pane),
.dirty => readFlag(p, req, dirtyOf(pane)),
.mark => readFlag(p, req, !pf.nomark),
.scroll => readFlag(p, req, !pf.noscroll),
@@ -457,7 +497,9 @@ fn kindText(p: *Pardes, pane: *Pane) ?[]const u8 {
fn readBody(p: *Pardes, req: Req, id: usize, pane: *Pane) Reply {
if (pane.file != null) {
- const text = bodyOf(pane);
+ const text = bodyOf(p, pane);
+ // The layer's lines in the buffer: a copy of the file's text.
+ if (layered(pane)) return tree.window(p, req, text);
const off = @min(req.off, text.len);
const n = @min(text.len - off, req.size);
return .{ .tag = req.tag, .payload = .{ .region = .{
@@ -476,9 +518,9 @@ fn readBody(p: *Pardes, req: Req, id: usize, pane: *Pane) Reply {
return tree.window(p, req, text);
}
-fn readData(req: Req, id: usize, pane: *Pane, pf: *State, stop_at_end: bool) Reply {
+fn readData(p: *Pardes, req: Req, id: usize, pane: *Pane, pf: *State, stop_at_end: bool) Reply {
if (pf.addr_failed) return tree.failText(req.tag, E.INVAL, e_addr_failed);
- const text = bodyOf(pane);
+ const text = bodyOf(p, pane);
clampAddr(pf, text.len);
const q0: usize = pf.addr.q0;
const hi: usize = if (stop_at_end) @max(q0, @as(usize, pf.addr.q1)) else text.len;
@@ -487,6 +529,11 @@ fn readData(req: Req, id: usize, pane: *Pane, pf: *State, stop_at_end: bool) Rep
pf.addr.q0 = clip(end);
if (!stop_at_end) pf.addr.q1 = clip(end);
if (pane.file == null) return .{ .tag = req.tag };
+ if (layered(pane)) {
+ var whole = req;
+ whole.off = 0;
+ return tree.window(p, whole, text[q0..end]);
+ }
return .{ .tag = req.tag, .payload = .{ .region = .{
.pane = @intCast(id),
.serial = pane.serial,
@@ -495,14 +542,19 @@ fn readData(req: Req, id: usize, pane: *Pane, pf: *State, stop_at_end: bool) Rep
} } };
}
-fn readSel(req: Req, id: usize, pane: *Pane) Reply {
+fn readSel(p: *Pardes, req: Req, id: usize, pane: *Pane) Reply {
if (pane.file == null) return .{ .tag = req.tag };
- const text = bodyOf(pane);
- const d = dotOf(pane);
+ const text = bodyOf(p, pane);
+ const d = fileDot(pane);
const lo = @min(@as(usize, d.q0), text.len);
const hi = @max(lo, @min(@as(usize, d.q1), text.len));
const off = @min(req.off, hi - lo);
const n = @min(hi - lo - off, req.size);
+ if (layered(pane)) {
+ var whole = req;
+ whole.off = 0;
+ return tree.window(p, whole, text[lo + off ..][0..n]);
+ }
return .{ .tag = req.tag, .payload = .{ .region = .{
.pane = @intCast(id),
.serial = pane.serial,
@@ -519,7 +571,7 @@ pub fn write(p: *Pardes, req: Req, id: usize, pane: *Pane, file: PaneFile) Reply
.body => writeBody(p, req, id, pane),
.tag => writeTag(p, req, pane),
.ctl => ctl.writePane(p, req, pane),
- .addr, .dot, .limit => writeRange(p.settings.tab, req, pane, file),
+ .addr, .dot, .limit => writeRange(p, req, pane, file),
.data, .xdata => writeData(p, req, pane),
.sel => writeSel(p, req, pane),
.dirty, .mark, .scroll => writeFlag(p, req, pane, file),
@@ -603,13 +655,13 @@ pub fn flushBatch(p: *Pardes) void {
const id = p.paneBySerial(b.serial) orelse return;
const pane = p.panes[id].?;
if (fileOf(pane) == null) return;
- const before = dotOf(pane);
+ const before = fileDot(pane);
const take = spliceJoined(p, pane, b.at, b.at, b.bytes.items, b.handle) orelse return;
- setDot(p.settings.tab, pane, .{
+ setFileDot(p.settings.tab, pane, .{
.q0 = shiftOne(before.q0, clip(b.at), 0, clip(take)),
.q1 = shiftOne(before.q1, clip(b.at), 0, clip(take)),
});
- if (!pane.fs.noscroll) showOffset(p.settings.tab, pane, b.at + take);
+ if (!pane.fs.noscroll) showFileOffset(p.settings.tab, pane, b.at + take);
}
/// How many bytes this open's write holds back (`fs.batch`) for its pane.
@@ -645,11 +697,11 @@ fn writeBody(p: *Pardes, req: Req, id: usize, pane: *Pane) Reply {
p.emitWrite(id, req.data[0..take]);
return .{ .tag = req.tag, .written = @intCast(take) };
}
- const at = bodyOf(pane).len + heldBytes(p, req, pane);
+ const at = bodyOf(p, pane).len + heldBytes(p, req, pane);
if (holdWrite(p, req, pane, at) catch return Reply.fail(req.tag, E.NOMEM)) |take| return .{ .tag = req.tag, .written = @intCast(take) };
const take = spliceJoined(p, pane, at, at, req.data, req.handle) orelse
return Reply.fail(req.tag, E.NOMEM);
- if (!pane.fs.noscroll) showOffset(p.settings.tab, pane, at + take);
+ if (!pane.fs.noscroll) showFileOffset(p.settings.tab, pane, at + take);
return .{ .tag = req.tag, .written = @intCast(take) };
}
@@ -738,7 +790,7 @@ fn writeData(p: *Pardes, req: Req, pane: *Pane) Reply {
if (fileOf(pane) == null) return tree.failText(req.tag, E.INVAL, e_no_text);
const pf = &pane.fs;
if (pf.addr_failed) return tree.failText(req.tag, E.INVAL, e_addr_failed);
- clampAddr(pf, bodyOf(pane).len + heldBytes(p, req, pane));
+ clampAddr(pf, bodyOf(p, pane).len + heldBytes(p, req, pane));
const q0: usize = pf.addr.q0;
const q1: usize = @max(q0, @as(usize, pf.addr.q1));
// An insert (an empty address) goes on from the open's held bytes.
@@ -747,15 +799,15 @@ fn writeData(p: *Pardes, req: Req, pane: *Pane) Reply {
return .{ .tag = req.tag, .written = @intCast(take) };
};
flushBatch(p);
- const before = dotOf(pane);
+ const before = fileDot(pane);
const take = spliceJoined(p, pane, q0, q1, req.data, req.handle) orelse
return Reply.fail(req.tag, E.NOMEM);
- setDot(p.settings.tab, pane, .{
+ setFileDot(p.settings.tab, pane, .{
.q0 = shiftOne(before.q0, clip(q0), clip(q1 - q0), clip(take)),
.q1 = shiftOne(before.q1, clip(q0), clip(q1 - q0), clip(take)),
});
pf.addr = .{ .q0 = clip(q0 + take), .q1 = clip(q0 + take) };
- if (!pf.noscroll) showOffset(p.settings.tab, pane, q0 + take);
+ if (!pf.noscroll) showFileOffset(p.settings.tab, pane, q0 + take);
return .{ .tag = req.tag, .written = @intCast(take) };
}
@@ -764,7 +816,7 @@ fn writeSel(p: *Pardes, req: Req, pane: *Pane) Reply {
const pf = &pane.fs;
// The same open's next write follows its last; any other replaces dot.
const going_on = req.handle != 0 and pf.wrsel_by == req.handle;
- const d = if (going_on) State.Range{ .q0 = pf.wrsel.q1, .q1 = pf.wrsel.q1 } else dotOf(pane);
+ const d = if (going_on) State.Range{ .q0 = pf.wrsel.q1, .q1 = pf.wrsel.q1 } else fileDot(pane);
const q0: usize = d.q0;
const q1: usize = @max(q0, @as(usize, d.q1));
const take = spliceBody(p, pane, q0, q1, req.data) orelse
@@ -774,7 +826,7 @@ fn writeSel(p: *Pardes, req: Req, pane: *Pane) Reply {
const start = if (going_on) pf.wrsel.q0 else clip(q0);
pf.wrsel = .{ .q0 = start, .q1 = clip(q0 + take) };
pf.wrsel_by = req.handle;
- setDot(p.settings.tab, pane, pf.wrsel);
+ setFileDot(p.settings.tab, pane, pf.wrsel);
return .{ .tag = req.tag, .written = @intCast(take) };
}
@@ -796,12 +848,13 @@ pub const e_addr_failed = "no address: the last one written to addr failed";
/// that does not evaluate says why: `bad address syntax`, `no match for
/// regexp`, `address out of range`, `bad regular expression`, `addresses
/// out of order`.
-fn writeRange(tab: u16, req: Req, pane: *Pane, file: PaneFile) Reply {
+fn writeRange(p: *Pardes, req: Req, pane: *Pane, file: PaneFile) Reply {
+ const tab = p.settings.tab;
// A terminal, an image or a PDF has no text an address names: refused
// alike for addr, dot and limit, before the expression is read.
if (fileOf(pane) == null) return tree.failText(req.tag, E.INVAL, e_no_text_address);
const pf = &pane.fs;
- const text = bodyOf(pane);
+ const text = bodyOf(p, pane);
clampAddr(pf, text.len);
const expr = std.mem.trimEnd(u8, req.data, "\n");
// One that goes from the address (`.`, `+`, `-`) has none to go from
@@ -842,7 +895,7 @@ fn writeRange(tab: u16, req: Req, pane: *Pane, file: PaneFile) Reply {
},
.limit => pf.limit = range,
// Setting dot scrolls to it, which is the whole of acme's `show`.
- .dot => setDot(tab, pane, range),
+ .dot => setFileDot(tab, pane, range),
else => unreachable,
}
return .{ .tag = req.tag, .written = @intCast(req.data.len) };
@@ -1011,25 +1064,25 @@ pub fn truncate(p: *Pardes, pane: *Pane, file: PaneFile) tree.Status {
.body => if (fileOf(pane) != null) {
pf.joined = null;
const was = fileOf(pane).?.revision;
- _ = spliceBody(p, pane, 0, bodyOf(pane).len, "") orelse return .err;
+ _ = spliceBody(p, pane, 0, bodyOf(p, pane).len, "") orelse return .err;
// An empty body truncated is no edit: the write after it makes
// the step, rather than join one that was never pushed.
pf.joined = if (fileOf(pane).?.revision != was) fileOf(pane).?.revision else null;
pf.joined_by = 0;
pf.addr = .{};
- setDot(p.settings.tab, pane, .{});
+ setFileDot(p.settings.tab, pane, .{});
},
.data, .xdata => if (fileOf(pane) != null) {
- clampAddr(pf, bodyOf(pane).len);
+ clampAddr(pf, bodyOf(p, pane).len);
const q0 = pf.addr.q0;
const q1 = @max(q0, pf.addr.q1);
- const before = dotOf(pane);
+ const before = fileDot(pane);
pf.joined = null;
const was = fileOf(pane).?.revision;
_ = spliceBody(p, pane, q0, q1, "") orelse return .err;
pf.joined = if (fileOf(pane).?.revision != was) fileOf(pane).?.revision else null;
pf.joined_by = 0;
- setDot(p.settings.tab, pane, .{ .q0 = shiftOne(before.q0, q0, q1 - q0, 0), .q1 = shiftOne(before.q1, q0, q1 - q0, 0) });
+ setFileDot(p.settings.tab, pane, .{ .q0 = shiftOne(before.q0, q0, q1 - q0, 0), .q1 = shiftOne(before.q1, q0, q1 - q0, 0) });
pf.addr = .{ .q0 = q0, .q1 = q0 };
},
// Done with the write after it, which may yet be refused.
@@ -1067,7 +1120,7 @@ test "rows through the line index are the ones counting newlines gives, at every
const pane = p.panes[0].?;
const f = &pane.file.?;
_ = try panes.File.lineIndex(p.gpa, f);
- const text = bodyOf(pane);
+ const text = bufferOf(pane);
try testing.expect(lineIndex(pane, text) != null);
for (0..text.len + 2) |off| {
try testing.expectEqual(modal.positionAt(text, off), positionAt(pane, text, off));