//! docs/typ/reference.typ, Flags and undo . const std = @import("std"); const Session = @import("session").Session; test "flags: dirty, mark and scroll read and write the state acme kept behind ctl verbs, 1 or 0" { var s = try Session.initFile(std.testing.allocator, "one\ntwo\n"); defer s.deinit(); const n = try s.focused(); const dirty = s.at("/pane/{d}/dirty", .{n}); const mark = s.at("/pane/{d}/mark", .{n}); const scroll = s.at("/pane/{d}/scroll", .{n}); for ([_][]const u8{ dirty, mark, scroll }) |flag| { try std.testing.expectEqual(@as(u64, 2), (try s.stat(flag)).size); for ([_][]const u8{ "", "yes", "2", "0 1", "true" }) |bad| { try std.testing.expectEqual(@as(u16, 22), try s.refused(flag, bad)); try std.testing.expectEqualStrings("bad flag: takes 1 or 0", s.said); } } try s.expect(dirty, "0\n"); try s.expect(mark, "1\n"); try s.expect(scroll, "1\n"); // dirty as /index shows it. try s.write(dirty, "1\n"); try s.expect(dirty, "1\n"); try std.testing.expect(std.mem.indexOf(u8, try s.read("/index"), s.at("{d} text 1 ", .{n})) != null); try s.write(dirty, "0"); try std.testing.expect(std.mem.indexOf(u8, try s.read("/index"), s.at("{d} text 0 ", .{n})) != null); try s.write(mark, "0"); try s.write(scroll, "0"); try s.expect(mark, "0\n"); try s.expect(scroll, "0\n"); try s.write(mark, "1"); try s.write(scroll, "1"); try s.expect(mark, "1\n"); try s.expect(scroll, "1\n"); // A terminal has no file, so its dirty flag is readable but not writable. var t = try Session.initTerm(std.testing.allocator); defer t.deinit(); const term_dirty = t.at("/pane/{d}/dirty", .{try t.focused()}); try t.expect(term_dirty, "0\n"); try std.testing.expectEqual(@as(u16, 22), try t.refused(term_dirty, "1")); } fn has(hay: []const u8, needle: []const u8) bool { return std.mem.indexOf(u8, hay, needle) != null; } test "flags: Undo and Redo on a pane's ctl step its body through its edits, and say when there is none, counted" { var s = try Session.initFile(std.testing.allocator, "one\n"); defer s.deinit(); const n = try s.focused(); const body = s.at("/pane/{d}/body", .{n}); const ctl = s.at("/pane/{d}/ctl", .{n}); try s.write(body, "two\n"); try s.expect(body, "one\ntwo\n"); try s.write(ctl, "Undo"); try s.expect(body, "one\n"); try s.write(ctl, "Redo"); try s.expect(body, "one\ntwo\n"); // Nothing left: said, and the write still succeeds, as acme's is silent. try s.write(ctl, "Redo"); try std.testing.expect(has(try s.read("/log"), "Redo: nothing to redo")); try s.write(ctl, "Undo"); try s.write(ctl, "Undo"); try std.testing.expect(has(try s.read("/log"), "Undo: nothing to undo")); // Said again word for word, it is that line counted, as err's are. for (0..3) |_| try s.write(ctl, "Undo"); try std.testing.expect(has(try s.read("/log"), "Undo: nothing to undo (x4)\n")); } test "flags: dirty goes back to 0 when the text is the saved text again" { var s = try Session.initFile(std.testing.allocator, "one\n"); defer s.deinit(); const n = try s.focused(); const dirty = s.at("/pane/{d}/dirty", .{n}); const ctl = s.at("/pane/{d}/ctl", .{n}); try s.expect(dirty, "0\n"); try s.write(s.at("/pane/{d}/body", .{n}), "two\n"); try s.expect(dirty, "1\n"); try s.write(ctl, "Undo"); try s.expect(dirty, "0\n"); try s.write(ctl, "Redo"); try s.expect(dirty, "1\n"); } test "flags: a > into an empty body is one undo step, as into any other" { var s = try Session.initFile(std.testing.allocator, ""); defer s.deinit(); const n = try s.focused(); const body = s.at("/pane/{d}/body", .{n}); try s.replace(body, "fresh\n"); try s.expect(body, "fresh\n"); try s.write(s.at("/pane/{d}/ctl", .{n}), "Undo\n"); try s.expect(body, ""); } test "flags: a write that changes nothing, an empty range deleted or a range replaced by itself, is no undo step and no new version" { var s = try Session.initFile(std.testing.allocator, "abc\ndef\n"); defer s.deinit(); const n = try s.focused(); const body = s.at("/pane/{d}/body", .{n}); const addr = s.at("/pane/{d}/addr", .{n}); const data = s.at("/pane/{d}/data", .{n}); const ctl = s.at("/pane/{d}/ctl", .{n}); // One real change, so a step made after it would be the one undone. try s.write(addr, "1"); try s.write(data, "abd\n"); const version = (try s.stat(body)).version; // Each as a shell does it: `: > data` after an empty address, `echo // abd > data` over the line it holds, and Edit's `s/d/d/`. try s.write(addr, "#2,#2"); const h = try s.hold(data, 1); try s.truncate(h); s.clunk(h); try s.write(addr, "1"); try s.write(data, "abd\n"); try s.write(ctl, "Edit ,s/d/d/\n"); try s.expect(body, "abd\ndef\n"); try std.testing.expectEqual(version, (try s.stat(body)).version); try s.write(ctl, "Undo\n"); try s.expect(body, "abc\ndef\n"); } test "flags: an open's writes in a row go in as one edit, seen by the next request" { var s = try Session.initFile(std.testing.allocator, "one\n"); defer s.deinit(); const n = try s.focused(); const body = s.at("/pane/{d}/body", .{n}); const version = (try s.stat(body)).version; const h = try s.hold(body, 1); for (0..100) |_| try std.testing.expectEqual(@as(u16, 0), try s.writeFid(h, "0123456789\n")); try std.testing.expectEqual(@as(usize, 4 + 1100), (try s.read(body)).len); try std.testing.expectEqual(version + 1, (try s.stat(body)).version); s.clunk(h); try s.write(s.at("/pane/{d}/ctl", .{n}), "Undo\n"); try s.expect(body, "one\n"); // data inserts at its address go on the same way. try s.write(s.at("/pane/{d}/addr", .{n}), "#0"); const d = try s.hold(s.at("/pane/{d}/data", .{n}), 1); _ = try s.writeFid(d, "a"); _ = try s.writeFid(d, "b"); s.clunk(d); try s.expect(body, "abone\n"); } test "flags: undo keeps the last 256 steps of a long session, back to where they begin, and redo takes them all again" { var s = try Session.initFile(std.testing.allocator, ""); defer s.deinit(); const n = try s.focused(); const body = s.at("/pane/{d}/body", .{n}); const ctl = s.at("/pane/{d}/ctl", .{n}); // Each write its own open, so its own step: 300 of them. for (0..300) |i| try s.write(body, s.at("line {d}\n", .{i})); const all = try s.read(body); for (0..256) |_| try s.write(ctl, "Undo\n"); // The first 44 are older than the ring: their text stays. var kept: std.ArrayList(u8) = .empty; defer kept.deinit(std.testing.allocator); for (0..44) |i| try kept.print(std.testing.allocator, "line {d}\n", .{i}); try s.expect(body, kept.items); try s.write(ctl, "Undo\n"); try s.expect(body, kept.items); try std.testing.expect(has(try s.read("/log"), "Undo: nothing to undo")); for (0..256) |_| try s.write(ctl, "Redo\n"); try s.expect(body, all); } test "flags: the writes after mark 0 are one undo step until mark 1, as acme's nomark makes them" { var s = try Session.initFile(std.testing.allocator, "one\ntwo\nthree\n"); defer s.deinit(); const n = try s.focused(); const body = s.at("/pane/{d}/body", .{n}); const addr = s.at("/pane/{d}/addr", .{n}); const data = s.at("/pane/{d}/data", .{n}); const ctl = s.at("/pane/{d}/ctl", .{n}); try s.write(s.at("/pane/{d}/mark", .{n}), "0"); try s.write(addr, "1"); try s.write(data, "ONE\n"); try s.write(addr, "3"); try s.write(data, "THREE\n"); try s.write(body, "four\n"); try s.write(s.at("/pane/{d}/mark", .{n}), "1"); try s.write(addr, "2"); try s.write(data, "TWO\n"); try s.expect(body, "ONE\nTWO\nTHREE\nfour\n"); try s.write(ctl, "Undo\n"); try s.expect(body, "ONE\ntwo\nTHREE\nfour\n"); try s.write(ctl, "Undo\n"); try s.expect(body, "one\ntwo\nthree\n"); try s.write(ctl, "Redo\n"); try s.expect(body, "ONE\ntwo\nTHREE\nfour\n"); } test "flags: an open's data writes are one undo step, the next open's another" { var s = try Session.initFile(std.testing.allocator, "one\n"); defer s.deinit(); const n = try s.focused(); const body = s.at("/pane/{d}/body", .{n}); const addr = s.at("/pane/{d}/addr", .{n}); const data = s.at("/pane/{d}/data", .{n}); const ctl = s.at("/pane/{d}/ctl", .{n}); try s.write(addr, "#0"); const d = try s.hold(data, 1); for ([_][]const u8{ "a", "b", "c\n" }) |piece| try std.testing.expectEqual(@as(u16, 0), try s.writeFid(d, piece)); s.clunk(d); try s.write(addr, "$"); try s.write(data, "end\n"); try s.expect(body, "abc\none\nend\n"); try s.write(ctl, "Undo\n"); try s.expect(body, "abc\none\n"); try s.write(ctl, "Undo\n"); try s.expect(body, "one\n"); } test "flags: a key typed at three ranges is one undo step, and its undo puts the three back" { var s = try Session.initFile(std.testing.allocator, "a\na\na\n"); defer s.deinit(); const n = try s.focused(); const body = s.at("/pane/{d}/body", .{n}); // C twice: a range on each of the three lines; then X typed at each. try s.keys("CCiX\x1b"); try s.expect(body, "Xa\nXa\nXa\n"); try s.keys("u"); try s.expect(body, "a\na\na\n"); try s.keys("U"); try s.expect(body, "Xa\nXa\nXa\n"); }