diff options
Diffstat (limited to 'src')
| -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 |
6 files changed, 213 insertions, 3 deletions
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`). |
