//! docs/typ/reference.typ, Addresses and data . const std = @import("std"); const Session = @import("session").Session; const expectEqual = std.testing.expectEqual; test "addresses: data and xdata read from addr, data moving it, and a write goes through it" { var s = try Session.initFile(std.testing.allocator, "one\ntwo\n"); defer s.deinit(); const n = try s.focused(); const addr = s.at("/pane/{d}/addr", .{n}); const data = s.at("/pane/{d}/data", .{n}); try s.write(addr, "#0"); try std.testing.expectEqualStrings("one", try s.readAt(data, 0, 3)); try expectEqual(.{ @as(u32, 3), @as(u32, 3) }, try s.range(addr)); try s.write(addr, "1"); try s.expect(s.at("/pane/{d}/xdata", .{n}), "one\n"); try s.write(addr, "1"); try s.expect(data, "one\ntwo\n"); try s.write(addr, "1"); try s.write(data, "ONE\n"); try s.expect(s.at("/pane/{d}/body", .{n}), "ONE\ntwo\n"); try expectEqual(@as(u32, 4), (try s.range(addr))[0]); } test "addresses: sel reads the selection, and a write to sel replaces it and is selected" { var s = try Session.initFile(std.testing.allocator, "one\ntwo\n"); defer s.deinit(); const n = try s.focused(); const addr = s.at("/pane/{d}/addr", .{n}); const dot = s.at("/pane/{d}/dot", .{n}); const sel = s.at("/pane/{d}/sel", .{n}); const body = s.at("/pane/{d}/body", .{n}); // Copying addr onto dot is the whole of acme's `dot=addr`, and back again // of `addr=dot`; each range file reads exactly what the other takes. try s.write(addr, "#0,#3"); try s.write(dot, " 0 3 "); try s.expect(dot, " 0 3 "); try expectEqual(@as(u64, 24), (try s.stat(dot)).size); try s.expect(sel, "one"); try expectEqual(@as(u64, 3), (try s.stat(sel)).size); try s.write(addr, "#0"); try s.write(addr, " 0 3 "); try expectEqual(.{ @as(u32, 0), @as(u32, 3) }, try s.range(addr)); // A truncation empties dot once its open goes, nothing written after // it; an address expression addresses it. const h = try s.hold(dot, 1); try s.truncate(h); try s.expect(sel, "one"); s.clunk(h); try s.expect(sel, ""); try s.write(dot, "#0,#3"); try s.write(sel, "ONE"); try s.expect(body, "ONE\ntwo\n"); // What was written is selected, as acme's wrsel leaves it. try s.expect(sel, "ONE"); // A write of another open replaces it; one open's writes run on. try s.write(sel, "!"); try s.expect(body, "!\ntwo\n"); const w = try s.hold(sel, 1); try expectEqual(@as(u16, 0), try s.writeFid(w, "a")); try expectEqual(@as(u16, 0), try s.writeFid(w, "b")); s.clunk(w); try s.expect(body, "ab\ntwo\n"); try s.expect(sel, "ab"); } test "addresses: a malformed address is bad address syntax; on a pane with no text any address is refused as nothing to address" { var s = try Session.initFile(std.testing.allocator, "one\n"); defer s.deinit(); const addr = s.at("/pane/{d}/addr", .{try s.focused()}); for ([_][]const u8{ "1x", "/(/", ",,x" }) |expr| { try expectEqual(@as(u16, 22), try s.refused(addr, expr)); try std.testing.expect(s.said.len > 0); } _ = try s.refused(addr, "1x"); try std.testing.expectEqualStrings("bad address syntax", s.said); // A terminal (as an image or a PDF): addr, dot and limit alike. var t = try Session.initTerm(std.testing.allocator); defer t.deinit(); const term = try t.focused(); for ([_][]const u8{ "addr", "dot", "limit" }) |file| for ([_][]const u8{ "1", "1x" }) |expr| { try expectEqual(@as(u16, 22), try t.refused(t.at("/pane/{d}/{s}", .{ term, file }), expr)); try std.testing.expectEqualStrings("invalid address: this pane has no text to address", t.said); }; } test "addresses: a 9P write moves dot only as acme's textinsert does" { // dot is `cd` of `abcdef\n` (2 4); two bytes go in at `at`. for ([_][3]u32{ .{ 0, 4, 6 }, // before: shifted .{ 2, 2, 6 }, // at its start: grown .{ 3, 2, 6 }, // inside: grown .{ 4, 2, 4 }, // at its end: left .{ 6, 2, 4 }, // after: left }) |c| { var s = try Session.initFile(std.testing.allocator, "abcdef\n"); defer s.deinit(); const n = try s.focused(); const dot = s.at("/pane/{d}/dot", .{n}); try s.write(dot, "2 4"); try s.write(s.at("/pane/{d}/addr", .{n}), s.at("#{d}", .{c[0]})); try s.write(s.at("/pane/{d}/data", .{n}), "XX"); try expectEqual(.{ c[1], c[2] }, try s.range(dot)); } // A body append leaves dot, and a sel write after it replaces dot, not // the text appended. var s = try Session.initFile(std.testing.allocator, "abcdef\n"); defer s.deinit(); const n = try s.focused(); const dot = s.at("/pane/{d}/dot", .{n}); try s.write(dot, "2 4"); try s.write(s.at("/pane/{d}/body", .{n}), "more\n"); try expectEqual(.{ @as(u32, 2), @as(u32, 4) }, try s.range(dot)); try s.write(s.at("/pane/{d}/sel", .{n}), "YY"); try s.expect(s.at("/pane/{d}/body", .{n}), "abYYef\nmore\n"); } test "addresses: a pair out of order or out of range is refused as the address form is, not clamped" { var s = try Session.initFile(std.testing.allocator, "abcdef\n"); defer s.deinit(); const addr = s.at("/pane/{d}/addr", .{try s.focused()}); for ([_][2][]const u8{ .{ "5 2", "addresses out of order" }, .{ "#5,#2", "addresses out of order" }, .{ "2 99", "address out of range" }, .{ "#2,#99", "address out of range" }, }) |c| { try expectEqual(@as(u16, 22), try s.refused(addr, c[0])); try std.testing.expectEqualStrings(c[1], s.said); } try s.write(addr, "2 4"); try expectEqual(.{ @as(u32, 2), @as(u32, 4) }, try s.range(addr)); } test "addresses: every address lands on a rune, and a combining mark and a lone \\r are places of their own" { // e, U+0301 (two bytes, 1-2), x, \r, \n var s = try Session.initFile(std.testing.allocator, "e\u{0301}x\r\n"); defer s.deinit(); const n = try s.focused(); const addr = s.at("/pane/{d}/addr", .{n}); for ([_]struct { []const u8, u32, u32 }{ .{ "#1,#3", 1, 3 }, // the mark alone .{ "#2", 1, 1 }, // inside it: back to its start .{ "#4,#5", 4, 5 }, // the \r alone .{ "1:2", 1, 1 }, // line:col the same .{ "1:3", 1, 1 }, .{ "0/\xCC/", 1, 3 }, // a match inside a rune covers it }) |c| { try s.write(addr, c[0]); try expectEqual(.{ c[1], c[2] }, try s.range(addr)); } // Copied to dot, it reads back the same. const dot = s.at("/pane/{d}/dot", .{n}); try s.write(addr, "#1,#3"); try s.write(dot, " 1 3 "); try s.expect(dot, " 1 3 "); try s.write(addr, "#4,#5"); try s.expect(s.at("/pane/{d}/xdata", .{n}), "\r"); } test "addresses: truncating data or xdata deletes only the addressed range, so > data replaces it" { var s = try Session.initFile(std.testing.allocator, ""); defer s.deinit(); const n = try s.focused(); const body = s.at("/pane/{d}/body", .{n}); for ([_][]const u8{ "data", "xdata" }) |file| { try s.replace(body, "hello world\nsecond line\n"); try s.write(s.at("/pane/{d}/addr", .{n}), "#0,#5"); const h = try s.hold(s.at("/pane/{d}/{s}", .{ n, file }), 1); try s.truncate(h); try s.expect(body, " world\nsecond line\n"); try expectEqual(@as(u16, 0), try s.writeFid(h, "HOWDY")); s.clunk(h); try s.expect(body, "HOWDY world\nsecond line\n"); } }