diff options
| -rw-r--r-- | docs/typ/builtins-gui.json | 2 | ||||
| -rw-r--r-- | docs/typ/builtins-tui.json | 2 | ||||
| -rw-r--r-- | docs/typ/guide.typ | 6 | ||||
| -rw-r--r-- | docs/typ/reference.typ | 24 | ||||
| -rw-r--r-- | src/CHANGELOG.md | 6 | ||||
| -rw-r--r-- | src/File.zig | 142 | ||||
| -rw-r--r-- | src/builtins.zig | 51 | ||||
| -rw-r--r-- | src/config.zig | 2 | ||||
| -rw-r--r-- | src/panes.zig | 12 | ||||
| -rw-r--r-- | src/views.zig | 3 | ||||
| -rw-r--r-- | test/behaviour.zig | 1 | ||||
| -rw-r--r-- | test/behaviour/follow.zig | 204 | ||||
| -rw-r--r-- | test/snapshots/builtins.golden | 8 | ||||
| -rw-r--r-- | test/snapshots/follow.golden | 61 | ||||
| -rw-r--r-- | test/snapshots/follow.snap | 20 | ||||
| -rw-r--r-- | test/snapshots/leader.golden | 7 | ||||
| -rw-r--r-- | test/snapshots/ttyhelp.golden | 12 |
17 files changed, 546 insertions, 17 deletions
diff --git a/docs/typ/builtins-gui.json b/docs/typ/builtins-gui.json index 37b8e1ba..09517de3 100644 --- a/docs/typ/builtins-gui.json +++ b/docs/typ/builtins-gui.json @@ -37,6 +37,7 @@ {"name": "Exit", "leader": "SPC q", "also": ["topbar"], "arg": false, "scope": "root", "setting": false, "choices": null, "doc": "Quit the editor, refusing once while a pane holds unsaved text: an Exit with nothing edited since discards it."}, {"name": "Filter", "leader": null, "also": [], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "Recolour a terminal's or a +Pager's program colours to the theme, or show them as sent, flipping between them."}, {"name": "Find", "leader": "SPC f f", "also": ["topbar"], "arg": true, "scope": "pane", "setting": false, "choices": null, "doc": "List the files below this pane's directory whose names hold its argument, in a +Search."}, + {"name": "Follow", "leader": "SPC c f", "also": [], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "Emacs's follow-mode: one text across a chain of panes as consecutive pages, each going on where the one before ends, scrolled as one."}, {"name": "Font", "leader": null, "also": [], "arg": true, "scope": "root", "setting": true, "choices": null, "doc": "The body font, by name or path, and its size."}, {"name": "Fonts", "leader": "SPC t f", "also": [], "arg": false, "scope": "root", "setting": false, "choices": null, "doc": "List the fonts in a pane, each a word to click."}, {"name": "Forward", "leader": "SPC j i", "also": ["C-i"], "arg": false, "scope": "root", "setting": false, "choices": null, "doc": "Ctrl-i: one step forward again, up to wherever Back started."}, @@ -101,6 +102,7 @@ {"name": "Tutor", "leader": "SPC h t", "also": ["topbar"], "arg": false, "scope": "root", "setting": false, "choices": null, "doc": "Open the tutor, a document to work through."}, {"name": "Unattach", "leader": null, "also": [], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "Unattach a pane from the pane it answers for: it stays where it is, a pane of its own, and the next search from that pane opens another under it."}, {"name": "Undo", "leader": null, "also": [], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "acme's Undo and Redo: step the pane's body back or forward through its edits, from a tag, a pane ctl or exec."}, + {"name": "Unfollow", "leader": null, "also": [], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "Take this pane out of its Follow chain: it stays, a Zerox twin with the scroll it has, and the chain closes up behind it."}, {"name": "Unmount", "leader": null, "also": [], "arg": true, "scope": "root", "setting": false, "choices": null, "doc": "Unmount the 9P tree mounted under /n/<name>."}, {"name": "Up", "leader": null, "also": [], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "Move the keyboard to the pane above, and from a column's top pane to its tag."}, {"name": "Upgrade", "leader": "SPC s U", "also": [], "arg": false, "scope": "root", "setting": false, "choices": null, "doc": "Move this session onto the pardes installed since it started, in place: its text, undo, views, terminals with their running shells, and its terminals and windows come along."}, diff --git a/docs/typ/builtins-tui.json b/docs/typ/builtins-tui.json index e307b3f8..edc401f6 100644 --- a/docs/typ/builtins-tui.json +++ b/docs/typ/builtins-tui.json @@ -36,6 +36,7 @@ {"name": "Exit", "leader": "SPC q", "also": ["topbar"], "arg": false, "scope": "root", "setting": false, "choices": null, "doc": "Quit the editor, refusing once while a pane holds unsaved text: an Exit with nothing edited since discards it."}, {"name": "Filter", "leader": null, "also": [], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "Recolour a terminal's or a +Pager's program colours to the theme, or show them as sent, flipping between them."}, {"name": "Find", "leader": "SPC f f", "also": ["topbar"], "arg": true, "scope": "pane", "setting": false, "choices": null, "doc": "List the files below this pane's directory whose names hold its argument, in a +Search."}, + {"name": "Follow", "leader": "SPC c f", "also": [], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "Emacs's follow-mode: one text across a chain of panes as consecutive pages, each going on where the one before ends, scrolled as one."}, {"name": "Forward", "leader": "SPC j i", "also": ["C-i"], "arg": false, "scope": "root", "setting": false, "choices": null, "doc": "Ctrl-i: one step forward again, up to wherever Back started."}, {"name": "Get", "leader": null, "also": [], "arg": true, "scope": "pane", "setting": false, "choices": null, "doc": "acme's Get: the pane's file read again, its unsaved edits refused once as Del refuses them."}, {"name": "Grep", "leader": "SPC f g", "also": ["topbar"], "arg": true, "scope": "pane", "setting": false, "choices": null, "doc": "Find's sibling: Find matches file NAMES under this pane's directory, Grep matches the LINES of those files, on the disk engine's worker."}, @@ -95,6 +96,7 @@ {"name": "Tutor", "leader": "SPC h t", "also": ["topbar"], "arg": false, "scope": "root", "setting": false, "choices": null, "doc": "Open the tutor, a document to work through."}, {"name": "Unattach", "leader": null, "also": [], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "Unattach a pane from the pane it answers for: it stays where it is, a pane of its own, and the next search from that pane opens another under it."}, {"name": "Undo", "leader": null, "also": [], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "acme's Undo and Redo: step the pane's body back or forward through its edits, from a tag, a pane ctl or exec."}, + {"name": "Unfollow", "leader": null, "also": [], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "Take this pane out of its Follow chain: it stays, a Zerox twin with the scroll it has, and the chain closes up behind it."}, {"name": "Unmount", "leader": null, "also": [], "arg": true, "scope": "root", "setting": false, "choices": null, "doc": "Unmount the 9P tree mounted under /n/<name>."}, {"name": "Up", "leader": null, "also": [], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "Move the keyboard to the pane above, and from a column's top pane to its tag."}, {"name": "Upgrade", "leader": "SPC s U", "also": [], "arg": false, "scope": "root", "setting": false, "choices": null, "doc": "Move this session onto the pardes installed since it started, in place: its text, undo, views, terminals with their running shells, and its terminals and windows come along."}, diff --git a/docs/typ/guide.typ b/docs/typ/guide.typ index 2128f0cc..51f29fb8 100644 --- a/docs/typ/guide.typ +++ b/docs/typ/guide.typ @@ -146,6 +146,12 @@ To open beside: #word("Newcol"), then #word("Look") a file name typed in its tag opens it there; #key("Alt-c") moves a pane into a new column. #clip("columns", wide: true) +A file longer than its pane can go on in the next column, as Emacs's +follow-mode shows it: #word("Follow") (#key("SPC c f")) adds a page right of +it, and each page starts where the one before ends; scrolling any of them, or +moving the cursor past a page's edge, moves through all of them as one. +#word("Unfollow") on a page's tag takes it out of the chain. + Closing a column's last pane leaves it empty; #word("Delcol") and #word("Joincol") take columns away. Closing the session's last pane quits pardes. diff --git a/docs/typ/reference.typ b/docs/typ/reference.typ index db090d24..7ffbd3ea 100644 --- a/docs/typ/reference.typ +++ b/docs/typ/reference.typ @@ -612,7 +612,8 @@ takes: its tag; #word("Window") (#key("Ctrl-w"), #key("SPC w")) picks the pane first: #keys("h", "j", "k", "l") move an outline, #key("Enter") or #key("Esc") or a pane's letter focuses, #key("d") is #word("Del") and - #key("c") #word("Collapse") on the outlined pane. + #key("c") #word("Collapse") on the outlined pane. #word("Follow") + (#key("SPC c f")) and #word("Unfollow"), below. - `get`: reload from the file (refused once over unsaved edits, `<name>: Modified (get again to discard)`). It is an undo step, so #word("Undo") brings back what was there: the sure way back to the file on disk. - `lock`/`unlock` (acme's). The lock binds only clients that take it and belongs to the open that wrote it: `exec 3>$pane/ctl; echo lock >&3; ...; exec 3>&-`. A `lock` another open holds fails at once, `file in use` @@ -629,6 +630,27 @@ A file changed on disk reloads by itself only when the buffer has no unsaved edits; otherwise it stays dirty, says `<name> changed on disk (get reloads it, Save overwrites it)`, logs `changed <serial>`, and its next #word("Save") warns once. A file whose disk text is what the pane was read or saved as is no change: an #word("Undo") back across #word("Get") `file` or `e` to unsaved edits says nothing changed, and #word("Save") writes it. +== Follow <follow> + +#word("Follow") (#key("SPC c f")) is Emacs's follow-mode: one text across a +chain of panes as consecutive pages. On a file pane it adds a page, a +#word("Zerox") twin, at the end of the column right of the chain's last +page, or alone in a new column there when there is none; the keyboard stays +where it was. #word("Follow") on any page adds one after the last; on a twin +outside the chain it is refused (`Follow: this text has a chain: Follow on +one of its panes`). The pages, in column order, each start on the line after +the one before ends (a line wrapped on past a page's bottom starts the next +again), and scroll as one: the wheel, a page key, a jump or a #word("Look") +in any page moves the others with it. Each page keeps its own cursor; one +moved past its page's last row (or above its first) takes the keyboard to +the page showing its line, scrolling nothing, and past the chain's ends the +chain scrolls as one pane would. A page the chain made has #word("Unfollow") +on its tag: it takes the pane out, a plain twin with the scroll it has, and +the chain closes up. #word("Del") of a page leaves the chain too; the first +page left is the head, whose tag has no #word("Unfollow"), and a chain of +one is none. The chain is kept across #word("Dump") and #word("Restore"); +each window or terminal on the session scrolls its own. + == Addresses and data <addresses> #file("addr"), #file("dot") and #file("limit") read the pair of byte diff --git a/src/CHANGELOG.md b/src/CHANGELOG.md index 8bb5216c..2dca1ac0 100644 --- a/src/CHANGELOG.md +++ b/src/CHANGELOG.md @@ -2,6 +2,12 @@ ## Unreleased +- `Follow` (`SPC c f`), Emacs's follow-mode: one text across a chain of + panes as consecutive pages, each a Zerox twin in the column to the right + (a new column when there is none). The pages scroll as one, and the + cursor moved past a page's edge goes on in the next. `Unfollow` on a + follower's tag takes it out; the chain is kept in a Dump. + - A theme may give the tagline (the workspace row) colours of its own, `tagline_bg` and `tagline_fg`, an editor's status bar; dark_plus has VS Code's blue. Without them the row keeps the tag's colours, as before. diff --git a/src/File.zig b/src/File.zig index 1e553bc9..e387abab 100644 --- a/src/File.zig +++ b/src/File.zig @@ -200,6 +200,9 @@ pub const State = struct { /// and (while it is the one index) `line_starts`, each with its own /// scroll, cursor and syntax; 0, none. See `zerox`. twin: u32 = 0, + /// In its twin group's Follow chain (builtins.Follow), whose panes + /// show the text as consecutive pages (followChain). + follow: bool = false, }; pub const NodeStep = struct { ranges: []modal.Selection, primary: usize }; @@ -1020,7 +1023,10 @@ fn edit(p: *Pardes, f: *State, off: usize, len: usize, bytes_in: []const u8, who const cursor = positionIn(p.gpa, tf, moved[i].cursor); t.body.cur_row = @intCast(cursor.row); t.body.cur_col = @intCast(cursor.col); + const was = tf.scroll; tf.scroll = positionIn(p.gpa, tf, moved[i].scroll).row; + // Moved by the edit, not scrolled: a Follow chain reflows from its head. + t.follow_scroll +%= tf.scroll -% was; } return true; } @@ -1094,7 +1100,9 @@ const ParkedPlaces = struct { v.body.cur_col = @intCast(cursor.col); v.body.vsel.active = false; v.body.nsel = 0; + const was = v.scroll; v.scroll = positionIn(p.gpa, tf, @min(a.scroll, tf.content.len)).row; + v.follow_scroll +%= v.scroll -% was; } } }; @@ -1232,6 +1240,7 @@ pub fn leaveTwins(p: *Pardes, f: *State) void { f.history = History.create(p.gpa) catch f.history; // The one left is no twin now (syncTwins drops the group). if (n == 1) p.panes[twins[0]].?.file.?.twin = 0; + if (n == 1) unchain(p.panes[twins[0]].?); } pub const TwinGroup = struct { @@ -1265,6 +1274,7 @@ pub fn syncTwins(p: *Pardes) void { } if (n < 2) { if (n == 1) members[0].twin = 0; + if (n == 1) unchain(member_panes[0]); p.gpa.free(group.path); _ = p.twin_groups.swapRemove(g); continue; @@ -1305,10 +1315,142 @@ pub fn syncTwins(p: *Pardes) void { std.debug.assert(other.content.ptr == members[0].content.ptr and other.content.len == members[0].content.len); std.debug.assert(other.revision == members[0].revision); }; + // Only a group with a chain pays for one. + for (members[0..n]) |f| if (f.follow) { + followChain(p, group.id); + break; + }; g += 1; } } +/// Follow's chain in twin group `twin`, as the live view lays it out: its +/// panes in column order show the text as consecutive pages, each starting +/// on the line after the one before ends. A pane scrolled (by anything: +/// the wheel, a page key, a jump, the cursor past a chain's end) moves the +/// others with it; the cursor going past a page's edge takes the keyboard +/// to the page that shows its line (ensureCursorVisible leaves the +/// keyboard's page's inner edges alone, `follow_edges`). A chain of one is +/// no chain. +fn followChain(p: *Pardes, twin: u32) void { + const v = p.view(); + var chain: [pardes.MAX_PANES]*Pane = undefined; + var ids: [pardes.MAX_PANES]usize = undefined; + var n: usize = 0; + for (v.cols[0..v.ncol]) |col| for (col.panes[0..col.n]) |id| { + const pane = p.panes[id] orelse continue; + const f = if (pane.file) |*file| file else continue; + if (f.twin != twin or !f.follow) continue; + chain[n] = pane; + ids[n] = id; + n += 1; + }; + if (n < 2) { + for (chain[0..n]) |pane| unchain(pane); + return; + } + // The head is the text's first page, never a follower of another. + dropUnfollow(chain[0]); + // The cursor moved past its page's edge: the keyboard goes to the page + // showing its line, else to the chain's end, which scrolls to it. + const active = for (ids[0..n], 0..) |id, k| { + if (id == v.active) break k; + } else null; + if (active) |k| if (v.header == null and chain[k].focus == .body) hop: { + const m = chain[k]; + const row: usize = @intCast(@max(0, m.body.cur_row)); + if (m.follow_crossed and (row < m.file.?.scroll or row >= pageEnd(m))) { + const j = for (chain[0..n], 0..) |o, i| { + if (row >= o.file.?.scroll and row < pageEnd(o)) break i; + } else if (row < m.file.?.scroll) 0 else n - 1; + if (j == k) break :hop; + const to = chain[j]; + to.body.cur_row = m.body.cur_row; + to.body.cur_col = m.body.cur_col; + to.body.cur_pinned = true; + to.body.vsel.active = false; + to.body.nsel = 0; + to.body.mode = m.body.mode; + m.body.mode = .normal; + v.active = ids[j]; + to.follow_edges = @as(u2, @intFromBool(j > 0)) | @as(u2, @intFromBool(j + 1 < n)) << 1; + to.ensureCursorVisible(); + } + }; + // The page that moved (the keyboard's first), else the head, sets the + // others: back to the head, then on from it. + var anchor: usize = 0; + for (chain[0..n], 0..) |o, k| if (o.file.?.scroll != o.follow_scroll) { + anchor = k; + if (ids[k] == v.active) break; + }; + var k = anchor; + while (k > 0) : (k -= 1) { + const prev = chain[k - 1]; + const want = chain[k].file.?.scroll; + // The furthest scroll whose page ends at `want` or before. + var s = want -| prev.rows; + setPageScroll(prev, s); + while (s < want) : (s += 1) { + setPageScroll(prev, s + 1); + if (pageEnd(prev) <= want) continue; + setPageScroll(prev, s); + break; + } + } + for (chain[1..n], 0..) |o, i| setPageScroll(o, pageEnd(chain[i])); + // Only the keyboard's page crosses its edges: a dot written to another + // scrolls it there, and the chain with it. + for (chain[0..n], ids[0..n], 0..) |o, id, i| { + o.follow_scroll = o.file.?.scroll; + o.follow_crossed = false; + o.follow_edges = if (id != v.active) 0 else @as(u2, @intFromBool(i > 0)) | @as(u2, @intFromBool(i + 1 < n)) << 1; + } +} + +/// The line a page after `pane`'s starts on: the one after its last row's, +/// or that line again when the pane wraps it on past its bottom. +fn pageEnd(pane: *Pane) usize { + const f = &pane.file.?; + if (pane.collapsed) return f.scroll; + geometry(pane); + if (pane.wrap_n == 0) return f.scroll + pane.rows; + const last = (if (pane.body_visible_rows > 0) @min(pane.wrap_n, pane.body_visible_rows) else pane.wrap_n) - 1; + const line: usize = @intCast(pane.wrap_line[last]); + if (!pane.body_wrapped or line >= nlines(pane.gpa, f)) return line + 1; + const text = sourceLine(pane, @intCast(line)); + const width = wrapWidth(pane, pane.view.wrap) -| pane.wrap_pad[last]; + return if (fitEnd(text, @intCast(pane.wrap_col[last]), width) < text.len) @max(line, f.scroll + 1) else line + 1; +} + +fn setPageScroll(pane: *Pane, line: usize) void { + const f = &pane.file.?; + const to = @min(line, nlines(pane.gpa, f) -| 1); + if (to == f.scroll) return; + f.scroll = to; + f.syntax_dirty = true; +} + +/// `pane` out of its Follow chain (Unfollow, or a chain down to it): a +/// plain twin with the scroll it has. +pub fn unchain(pane: *Pane) void { + pane.file.?.follow = false; + pane.follow_edges = 0; + pane.follow_crossed = false; + dropUnfollow(pane); +} + +/// The Unfollow word Follow gave a pane's tag (builtins.Follow), gone. +fn dropUnfollow(pane: *Pane) void { + const own = pane.tag.own orelse return; + const at = std.mem.indexOf(u8, own, " Unfollow") orelse return; + const end = at + " Unfollow".len; + if (end < own.len and std.mem.indexOfScalar(u8, " \t\n", own[end]) == null) return; + const cut = std.mem.concat(pane.gpa, u8, &.{ own[0..at], own[end..] }) catch return; + pane.gpa.free(own); + pane.tag.own = cut; +} + /// Whether `head`, a text's first bytes, holds a diff, as a command pane's /// rows are told one (diff.looksLikeDiff, its first 200 lines). pub fn looksLikeDiff(head: []const u8) bool { diff --git a/src/builtins.zig b/src/builtins.zig index 15319d6e..c41b3e1b 100644 --- a/src/builtins.zig +++ b/src/builtins.zig @@ -1785,6 +1785,57 @@ pub const Right = struct { } }; +/// Emacs's follow-mode: one text across a chain of panes as consecutive +/// pages, each going on where the one before ends, scrolled as one. Adds a +/// page, a Zerox twin, at the end of the column right of the chain's last, +/// or in a new column there; its tag has Unfollow. +pub const Follow = struct { + pub fn run(c: Ctx) void { + if (@import("edit_cmd.zig").busyOn(c.p, c.id)) return c.p.reportFailure(c.id, "Follow: " ++ @import("edit_cmd.zig").e_busy); + const f = if (c.pane.file) |*file| file else return c.p.reportFailure(c.id, "Follow: only a file pane takes it"); + if (f.output != null or f.listing != null or f.mini != null) return c.p.reportFailure(c.id, "Follow: only a file pane takes it"); + // One chain a text: the new page goes after its last, in column order. + const v = c.p.view(); + var last = c.id; + for (v.cols[0..v.ncol]) |col| for (col.panes[0..col.n]) |id| { + const other = if (c.p.panes[id].?.file) |*file| file else continue; + if (f.twin != 0 and other.twin == f.twin and other.follow) last = id; + }; + if (!f.follow and last != c.id) return c.p.reportFailure(c.id, "Follow: this text has a chain: Follow on one of its panes"); + const col = (layout.findPane(c.p, last) orelse return c.p.reportFailure(c.id, "Follow: the pane is not laid out")).col; + const free = c.p.freeSlot() orelse return c.p.reportError(c.id, "Follow", error.NoPaneSlots); + const made = panes.File.zerox(c.p, c.id, free) catch |err| return c.p.reportError(c.id, "Follow", err); + const keep = v.active; + if (col + 1 < v.ncol) { + c.p.exec_column = col + 1; + defer c.p.exec_column = null; + exec.placeNew(c.p, c.id, null, free, .scratch); + if (c.p.unplaced.isSet(free)) return; + } else if (!layout.splitColumn(c.p, last, free, false)) { + c.p.unplaced.set(free); + return c.p.reportFailure(c.id, "Follow: no space for a column: this one is too narrow to split"); + } + // The keyboard stays on the page it was on. + v.active = keep; + if (layout.findPane(c.p, keep)) |at| c.p.setActiveColumn(at.col); + f.follow = true; + made.file.?.follow = true; + c.pane.follow_scroll = f.scroll; + made.follow_scroll = made.file.?.scroll; + made.appendTag(" Unfollow") catch |err| c.p.reportError(c.id, "Follow", err); + } +}; + +/// Take this pane out of its Follow chain: it stays, a Zerox twin with the +/// scroll it has, and the chain closes up behind it. +pub const Unfollow = struct { + pub fn run(c: Ctx) void { + const f = if (c.pane.file) |*file| file else return c.p.reportFailure(c.id, "Unfollow: this pane is in no Follow chain"); + if (!f.follow) return c.p.reportFailure(c.id, "Unfollow: this pane is in no Follow chain"); + panes.File.unchain(c.pane); + } +}; + /// Window mode: h/j/k/l move an outline between the panes; Enter or Esc /// focuses the outlined pane, `:` its tag, the letter shown on a pane that one; d is Del on it (then k or j between two), c Collapse. pub const Window = struct { diff --git a/src/config.zig b/src/config.zig index ee7d76f7..3f9403a8 100644 --- a/src/config.zig +++ b/src/config.zig @@ -135,6 +135,8 @@ pub const leader_path = paths: { .Up = null, .Right = null, .Window = "w", + .Follow = "cf", + .Unfollow = null, .Back = "jo", .Forward = "ji", .Last = "jj", diff --git a/src/panes.zig b/src/panes.zig index a90613d0..6b6f621a 100644 --- a/src/panes.zig +++ b/src/panes.zig @@ -260,6 +260,13 @@ pub const Pane = struct { tag_lead: usize = 0, cwd: Cwd = .none, hscroll: i32 = 0, + /// A Follow chain's page (File.followChain): the scroll the chain last + /// gave it, each view's; on the keyboard's page, whether a page comes + /// before (1) and after (2), whose edge the cursor crosses rather than + /// scrolling, and whether it crossed one this step. + follow_scroll: usize = 0, + follow_edges: u2 = 0, + follow_crossed: bool = false, // Visible rows map to source lines and raw/display column origins. wrap_line: [limits.wrap_rows]i32 = undefined, wrap_col: [limits.wrap_rows]i32 = undefined, @@ -624,9 +631,10 @@ pub const Pane = struct { margin = @min(margin, @divTrunc(@max(0, lines_shown), 2)); } if (!in_context) { + if (pane.follow_edges != 0 and (pane.body.cur_row < off or pane.body.cur_row > last)) pane.follow_crossed = true; if (pane.body.cur_row < off + margin) { - pane.scrollBy(pane.body.cur_row - margin - off); - } else if (pane.body.cur_row > last - margin) { + if (pane.follow_edges & 1 == 0) pane.scrollBy(pane.body.cur_row - margin - off); + } else if (pane.body.cur_row > last - margin and pane.follow_edges & 2 == 0) { var to = pane.body.cur_row + margin; if (pane.file) |*f| { // The margin only reveals text: a pane its whole text diff --git a/src/views.zig b/src/views.zig index 354ae0d0..953b39a4 100644 --- a/src/views.zig +++ b/src/views.zig @@ -33,7 +33,7 @@ pub const max = 4; /// A pane's fields that are each view's own: where it is in the view's /// layout, which of its texts has the keyboard, how far it is scrolled, and /// what the view's step measured of it (its tag's rows, its bands). -const pane_fields = [_][:0]const u8{ "focus", "vweight", "collapsed", "attached_to", "hscroll", "tag_scroll", "tag_top", "tag_rows", "view", "notices", "pan" }; +const pane_fields = [_][:0]const u8{ "focus", "vweight", "collapsed", "attached_to", "hscroll", "tag_scroll", "tag_top", "tag_rows", "view", "notices", "pan", "follow_scroll" }; /// The body's: its cursor, selections and mode. Its undo stays the pane's. const body_fields = [_][:0]const u8{ "mode", "cur_pinned", "cur_row", "cur_col", "vsel", "sels", "nsel", "select", "sticky_col", "restore_cursor", "last_edit", "reg_wait", "normal", "find_op", "find_ch" }; @@ -51,6 +51,7 @@ pub const PaneView = struct { view: Pane.SessionView = .{}, notices: Pane.Notices = .{}, pan: u16 = 0, + follow_scroll: usize = 0, body: Part(Text, &body_fields) = .{}, /// A document's scroll and size; a terminal's size is its pty's, which /// every view shows (sized by its last typist, `Pardes.sync`). diff --git a/test/behaviour.zig b/test/behaviour.zig index de0cb83c..5b31b6d9 100644 --- a/test/behaviour.zig +++ b/test/behaviour.zig @@ -12,6 +12,7 @@ test { _ = @import("behaviour/edit.zig"); _ = @import("behaviour/event.zig"); _ = @import("behaviour/flags.zig"); + _ = @import("behaviour/follow.zig"); _ = @import("behaviour/from_acme.zig"); _ = @import("behaviour/keys.zig"); _ = @import("behaviour/language_servers.zig"); diff --git a/test/behaviour/follow.zig b/test/behaviour/follow.zig new file mode 100644 index 00000000..5f2acb02 --- /dev/null +++ b/test/behaviour/follow.zig @@ -0,0 +1,204 @@ +//! docs/typ/reference.typ, Follow <follow>. +const std = @import("std"); +const Session = @import("session").Session; + +fn has(hay: []const u8, needle: []const u8) bool { + return std.mem.indexOf(u8, hay, needle) != null; +} + +/// A session on a 300-line file, `L001` to `L300`, in a window wide enough +/// for three columns' tags on one row each. +fn init() !Session { + var text: [300 * 5]u8 = undefined; + for (0..300) |i| _ = try std.fmt.bufPrint(text[i * 5 ..][0..5], "L{d:0>3}\n", .{i + 1}); + var s = try Session.initFile(std.testing.allocator, &text); + s.detach(); + try s.write("/ctl", "size 200 30\n"); + return s; +} + +const Page = struct { x: usize, first: usize, last: usize, rows: usize }; + +/// Each pane's lines as /screen draws them, left to right: the labels +/// drawn at one column of cells are one pane's. +fn pages(s: *Session) ![]Page { + var out: std.ArrayList(Page) = .empty; + var rows = std.mem.splitScalar(u8, try s.screen(), '\n'); + while (rows.next()) |row| { + var i: usize = 0; + while (std.mem.indexOfScalarPos(u8, row, i, 'L')) |at| : (i = at + 1) { + if (at + 4 > row.len) break; + const n = std.fmt.parseInt(usize, row[at + 1 .. at + 4], 10) catch continue; + const x = try std.unicode.utf8CountCodepoints(row[0..at]); + for (out.items) |*p| { + if (p.x != x) continue; + p.first = @min(p.first, n); + p.last = @max(p.last, n); + p.rows += 1; + break; + } else try out.append(s.arena.allocator(), .{ .x = x, .first = n, .last = n, .rows = 1 }); + } + } + std.mem.sort(Page, out.items, {}, struct { + fn lt(_: void, a: Page, b: Page) bool { + return a.x < b.x; + } + }.lt); + return out.items; +} + +/// The pages go on one from another: each starts the line after the one +/// before it ends, every line drawn once. +fn expectConsecutive(ps: []const Page) !void { + for (ps) |p| try std.testing.expectEqual(p.last - p.first + 1, p.rows); + for (ps[1..], ps[0 .. ps.len - 1]) |p, before| try std.testing.expectEqual(before.last + 1, p.first); +} + +/// The pane the last Follow made: the newest serial in the index. +fn newest(s: *Session) !u32 { + var max: u32 = 0; + var lines = std.mem.tokenizeScalar(u8, try s.read("/index"), '\n'); + while (lines.next()) |l| max = @max(max, try std.fmt.parseInt(u32, std.mem.sliceTo(l, ' '), 10)); + return max; +} + +/// The first line of the cursor's selection, 0-based (a label is 5 bytes). +fn line(s: *Session, pane: u32) !usize { + return (try s.range(s.at("/pane/{d}/dot", .{pane})))[0] / 5; +} + +test "follow: Follow puts the next page right of the chain, in a new column or at the end of the one there, Unfollow on its tag alone" { + var s = try init(); + defer s.deinit(); + const head = try s.focused(); + try s.write(s.at("/pane/{d}/exec", .{head}), "Follow\n"); + const second = try newest(&s); + // No column to the right: a new one; the keyboard stays on the head. + try std.testing.expectEqualStrings(s.at("{d}:{d} {d}:{d}", .{ try s.column(0), head, try s.column(1), second }), try s.columns()); + try std.testing.expectEqual(head, try s.focused()); + try std.testing.expect(has(try s.read(s.at("/pane/{d}/tag", .{second})), " Unfollow")); + try std.testing.expect(!has(try s.read(s.at("/pane/{d}/tag", .{head})), "Unfollow")); + try expectConsecutive(try pages(&s)); + // A column right of the last page: the next goes at its end, from + // any page of the chain. + try s.write(s.at("/pane/{d}/exec", .{second}), "Newcol\n"); + const other = try s.newPane(); + try s.write(s.at("/pane/{d}/exec", .{head}), "Follow\n"); + const third = try newest(&s); + try std.testing.expect(has(try s.columns(), s.at(":{d},{d}", .{ other, third }))); + // A twin out of the chain is refused one of its own. + try s.write(s.at("/pane/{d}/exec", .{head}), "Zerox\n"); + try std.testing.expect(try s.refused(s.at("/pane/{d}/exec", .{try newest(&s)}), "Follow\n") != 0); + try std.testing.expect(has(s.said, "Follow: this text has a chain")); +} + +test "follow: two and three pages show consecutive lines; the wheel in any page moves them all" { + var s = try init(); + defer s.deinit(); + const head = try s.focused(); + try s.write(s.at("/pane/{d}/exec", .{head}), "Follow\n"); + var ps = try pages(&s); + try std.testing.expectEqual(@as(usize, 2), ps.len); + try std.testing.expectEqual(@as(usize, 1), ps[0].first); + try expectConsecutive(ps); + try s.write(s.at("/pane/{d}/exec", .{head}), "Follow\n"); + ps = try pages(&s); + try std.testing.expectEqual(@as(usize, 3), ps.len); + try expectConsecutive(ps); + // Three notches down in the second page: the first follows. + for (0..3) |_| try s.write("/mousein", s.at("m {d} 10 16\nm {d} 10 0\n", .{ ps[1].x + 2, ps[1].x + 2 })); + const scrolled = try pages(&s); + try std.testing.expectEqual(@as(usize, 4), scrolled[0].first); + try expectConsecutive(scrolled); + // And up again in the last. + for (0..3) |_| try s.write("/mousein", s.at("m {d} 10 8\nm {d} 10 0\n", .{ ps[2].x + 2, ps[2].x + 2 })); + try std.testing.expectEqual(@as(usize, 1), (try pages(&s))[0].first); + try expectConsecutive(try pages(&s)); +} + +test "follow: the cursor past a page's last row takes the keyboard to the next page, and back above its first" { + var s = try init(); + defer s.deinit(); + const head = try s.focused(); + try s.write(s.at("/pane/{d}/exec", .{head}), "Follow\n"); + const second = try newest(&s); + const ps = try pages(&s); + const rows = ps[0].rows; + try s.keys(s.at("{d}j", .{rows - 1})); + try std.testing.expectEqual(head, try s.focused()); + try std.testing.expectEqual(rows - 1, try line(&s, head)); + try s.keys("j"); + try std.testing.expectEqual(second, try s.focused()); + try std.testing.expectEqual(rows, try line(&s, second)); + // Nothing scrolled: the pages are where they were. + try std.testing.expectEqualSlices(Page, ps, try pages(&s)); + try s.keys("k"); + try std.testing.expectEqual(head, try s.focused()); + try std.testing.expectEqual(rows - 1, try line(&s, head)); + // Past the chain's end the chain scrolls as one pane. + try s.keys(s.at("{d}j", .{2 * rows + 2})); + try std.testing.expectEqual(second, try s.focused()); + const moved = try pages(&s); + try std.testing.expect(moved[0].first > 1); + try expectConsecutive(moved); + // A dot written to the other page scrolls it there, the chain with it. + try s.write(s.at("/pane/{d}/dot", .{head}), "745 745"); + const shown = try pages(&s); + try std.testing.expect(shown[0].first <= 150 and 150 <= shown[0].last); + try expectConsecutive(shown); +} + +test "follow: Unfollow leaves a twin with its own scroll and closes the chain up; Del of the head makes the next one head" { + var s = try init(); + defer s.deinit(); + const head = try s.focused(); + try s.write(s.at("/pane/{d}/exec", .{head}), "Follow\n"); + const second = try newest(&s); + try s.write(s.at("/pane/{d}/exec", .{head}), "Follow\n"); + const third = try newest(&s); + const was = try pages(&s); + try s.write(s.at("/pane/{d}/exec", .{second}), "Unfollow\n"); + try std.testing.expect(!has(try s.read(s.at("/pane/{d}/tag", .{second})), "Unfollow")); + var ps = try pages(&s); + // The twin keeps its lines; the third follows the head now. + try std.testing.expectEqual(was[1], ps[1]); + try std.testing.expectEqual(was[0].last + 1, ps[2].first); + // A wheel in the twin moves it alone, as Zerox's does. + try s.write("/mousein", s.at("m {d} 10 16\nm {d} 10 0\n", .{ ps[1].x + 2, ps[1].x + 2 })); + const after = try pages(&s); + try std.testing.expectEqual(ps[0], after[0]); + try std.testing.expectEqual(ps[2], after[2]); + try std.testing.expectEqual(ps[1].first + 1, after[1].first); + try std.testing.expect(try s.refused(s.at("/pane/{d}/exec", .{second}), "Unfollow\n") != 0); + // Del of the head: the third is a chain of one, which is no chain. + try s.write(s.at("/pane/{d}/ctl", .{second}), "Del\n"); + try s.write(s.at("/pane/{d}/exec", .{third}), "Follow\n"); + const fourth = try newest(&s); + try s.write(s.at("/pane/{d}/ctl", .{head}), "Del\n"); + try std.testing.expect(!has(try s.read(s.at("/pane/{d}/tag", .{third})), "Unfollow")); + try std.testing.expect(has(try s.read(s.at("/pane/{d}/tag", .{fourth})), "Unfollow")); + ps = try pages(&s); + try std.testing.expectEqual(@as(usize, 2), ps.len); + try expectConsecutive(ps); + try s.write(s.at("/pane/{d}/ctl", .{third}), "Del\n"); + try std.testing.expect(!has(try s.read(s.at("/pane/{d}/tag", .{fourth})), "Unfollow")); +} + +test "follow: a Dump and Restore keep the chain, still one" { + var s = try init(); + defer s.deinit(); + const head = try s.focused(); + try s.write(s.at("/pane/{d}/exec", .{head}), "Follow\n"); + try s.write(s.at("/pane/{d}/exec", .{head}), "Follow\n"); + try s.write("/ctl", "Dump\n"); + const label = try s.arena.allocator().dupe(u8, std.mem.sliceTo(try s.read("/dumps"), ' ')); + try s.write(s.at("/pane/{d}/exec", .{head}), "Unfollow\n"); + try s.write("/ctl", s.at("Restore {s}\n", .{label})); + var ps = try pages(&s); + try std.testing.expectEqual(@as(usize, 3), ps.len); + try expectConsecutive(ps); + for (0..2) |_| try s.write("/mousein", s.at("m {d} 10 16\nm {d} 10 0\n", .{ ps[2].x + 2, ps[2].x + 2 })); + ps = try pages(&s); + try std.testing.expectEqual(@as(usize, 3), ps[0].first); + try expectConsecutive(ps); +} diff --git a/test/snapshots/builtins.golden b/test/snapshots/builtins.golden index fd60bc74..7230f9b8 100644 --- a/test/snapshots/builtins.golden +++ b/test/snapshots/builtins.golden @@ -14,16 +14,16 @@ |10: 1 pardes builtins under SPC c |11: 2 |12: 3 SPC c d Delcol -|13: 4 SPC c j Joincol topbar -|14: 5 SPC c n Newcol topbar -|15: 6 +|13: 4 SPC c f Follow +|14: 5 SPC c j Joincol topbar +|15: 6 SPC c n Newcol topbar +|16: 7 == snap docs-group grid=120x67 cursor=7,10 |10: 1 pardes builtins under SPC h |12: 3 SPC h b Build |13: 4 SPC h c Changelog topbar |14: 5 SPC h m Messages |15: 6 SPC h t Tutor topbar -|16: 7 == snap tutor-from-index grid=120x67 cursor=7,17 |10: 1 pardes builtins under SPC h Tutor |16: /tmp/pardes-snap/builtins/cwd/+Tutor Save Term Collapse Del diff --git a/test/snapshots/follow.golden b/test/snapshots/follow.golden new file mode 100644 index 00000000..99ef20b9 --- /dev/null +++ b/test/snapshots/follow.golden @@ -0,0 +1,61 @@ +== snap three-pages grid=150x30 cursor=7,3 +| Newcol Joincol Find Grep Help Changelog Tutor Dump Themes Config Debug Exit +| New Term Find Grep Joincol Delcol New Term Find Grep Joincol Delcol New Term Find Grep Joincol Delcol +| /tmp/pardes-snap/follow/cwd/big.txt Save Term Collapse Del /tmp/pardes-snap/follow/cwd/big.txt /tmp/pardes-snap/follow/cwd/big.txt +| 1 line 1 Follow Save Term Collapse Del Unfollow Save Term Collapse Del Unfollow +| 2 line 2 28 line 28 54 line 54 +| 3 line 3 29 line 29 55 line 55 +| 4 line 4 30 line 30 56 line 56 +| 5 line 5 31 line 31 57 line 57 +| 6 line 6 32 line 32 58 line 58 +| 7 line 7 33 line 33 59 line 59 +| 8 line 8 34 line 34 60 line 60 +| 9 line 9 35 line 35 61 line 61 +| 10 line 10 36 line 36 62 line 62 +| 11 line 11 37 line 37 63 line 63 +| 12 line 12 38 line 38 64 line 64 +| 13 line 13 39 line 39 65 line 65 +| 14 line 14 40 line 40 66 line 66 +| 15 line 15 41 line 41 67 line 67 +| 16 line 16 42 line 42 68 line 68 +| 17 line 17 43 line 43 69 line 69 +| 18 line 18 44 line 44 70 line 70 +| 19 line 19 45 line 45 71 line 71 +| 20 line 20 46 line 46 72 line 72 +| 21 line 21 47 line 47 73 line 73 +| 22 line 22 48 line 48 74 line 74 +| 23 line 23 49 line 49 75 line 75 +| 24 line 24 50 line 50 76 line 76 +| 25 line 25 51 line 51 77 line 77 +| 26 line 26 52 line 52 78 line 78 +| 27 line 27 53 line 53 79 line 79 +== snap hop grid=150x30 cursor=82,4 +|3: 1 line 1 Save Term Collapse Del Unfollow Save Term Collapse Del Unfollow +== snap wheel grid=150x30 cursor=82,4 +|3: 2 line 2 Save Term Collapse Del Unfollow Save Term Collapse Del Unfollow +|4: 3 line 3 29 line 29 55 line 55 +|5: 4 line 4 30 line 30 56 line 56 +|6: 5 line 5 31 line 31 57 line 57 +|7: 6 line 6 32 line 32 58 line 58 +|8: 7 line 7 33 line 33 59 line 59 +|9: 8 line 8 34 line 34 60 line 60 +|10: 9 line 9 35 line 35 61 line 61 +|11: 10 line 10 36 line 36 62 line 62 +|12: 11 line 11 37 line 37 63 line 63 +|13: 12 line 12 38 line 38 64 line 64 +|14: 13 line 13 39 line 39 65 line 65 +|15: 14 line 14 40 line 40 66 line 66 +|16: 15 line 15 41 line 41 67 line 67 +|17: 16 line 16 42 line 42 68 line 68 +|18: 17 line 17 43 line 43 69 line 69 +|19: 18 line 18 44 line 44 70 line 70 +|20: 19 line 19 45 line 45 71 line 71 +|21: 20 line 20 46 line 46 72 line 72 +|22: 21 line 21 47 line 47 73 line 73 +|23: 22 line 22 48 line 48 74 line 74 +|24: 23 line 23 49 line 49 75 line 75 +|25: 24 line 24 50 line 50 76 line 76 +|26: 25 line 25 51 line 51 77 line 77 +|27: 26 line 26 52 line 52 78 line 78 +|28: 27 line 27 53 line 53 79 line 79 +|29: 28 line 28 54 line 54 80 line 80 diff --git a/test/snapshots/follow.snap b/test/snapshots/follow.snap new file mode 100644 index 00000000..0e5d23a2 --- /dev/null +++ b/test/snapshots/follow.snap @@ -0,0 +1,20 @@ +# Follow (SPC c f) twice on a 300-line file: three pages in three columns, +# each going on the line after the one before ends; Unfollow on the two +# followers' tags only. Then j past the first page's last row takes the +# keyboard to the second page without scrolling, and a wheel notch in the +# last page moves all three. +lines big.txt 300 +start 30 150 big.txt +wait 8000 big.txt +stable 500 15000 +key space c f +stable 500 8000 +key space c f +stable 500 8000 +snap three-pages +text 27j +stable 500 8000 +snap hop +wheel down 120 12 +stable 500 8000 +snap wheel diff --git a/test/snapshots/leader.golden b/test/snapshots/leader.golden index 932e8a95..1895a833 100644 --- a/test/snapshots/leader.golden +++ b/test/snapshots/leader.golden @@ -90,9 +90,10 @@ |6: 1 pardes builtins under SPC c |7: 2 |8: 3 SPC c d Delcol -|9: 4 SPC c j Joincol topbar -|10: 5 SPC c n Newcol topbar -|11: 6 +|9: 4 SPC c f Follow +|10: 5 SPC c j Joincol topbar +|11: 6 SPC c n Newcol topbar +|12: 7 |34: |35: |36: diff --git a/test/snapshots/ttyhelp.golden b/test/snapshots/ttyhelp.golden index d0b90887..c3b4d57e 100644 --- a/test/snapshots/ttyhelp.golden +++ b/test/snapshots/ttyhelp.golden @@ -34,11 +34,11 @@ |12: 7 SPC R ClipReplace |13: 8 SPC Y ClipYankMain |14: 9 SPC c d Delcol -|15: 10 SPC c j Joincol topbar -|16: 11 SPC c n Newcol topbar -|17: 12 SPC d Del -|18: 13 SPC f c Config topbar -|19: 14 SPC f f Find topbar -|20: 15 SPC f g Grep topbar +|15: 10 SPC c f Follow +|16: 11 SPC c j Joincol topbar +|17: 12 SPC c n Newcol topbar +|18: 13 SPC d Del +|19: 14 SPC f c Config topbar +|20: 15 SPC f f Find topbar == snap leaderworks grid=100x31 cursor=7,6 |3: $ |
