diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/Finder.zig | 240 | ||||
| -rw-r--r-- | src/Query.zig | 27 | ||||
| -rw-r--r-- | src/exec.zig | 15 | ||||
| -rw-r--r-- | src/layout.zig | 40 | ||||
| -rw-r--r-- | src/pardes.zig | 18 |
5 files changed, 320 insertions, 20 deletions
diff --git a/src/Finder.zig b/src/Finder.zig new file mode 100644 index 00000000..a2b8f89f --- /dev/null +++ b/src/Finder.zig @@ -0,0 +1,240 @@ +//! The finder column: where query panes go with `SearchPlace column`, the +//! alternative to attaching them under the pane that asked (lifted from +//! the column prototype, cut to its placement). A column with a role at +//! the right edge: the first search opens it and the other columns give it +//! its share once, each in proportion to its weight; after that no pane +//! outside it moves for a search. Its width is a share of the window, not a +//! column weight (layout.compute), so it opens and closes without rounding +//! anyone's width off. Its panes are a recency stack: the newest query pane +//! on top and open, the older ones folded to their tags. A pane opened +//! from a row goes to the column of the pane the query searches, never in +//! here. Its last pane gone, the column goes and every other is as wide as +//! before it came. The role is the column's serial. +const Finder = @This(); + +const std = @import("std"); +const pardes = @import("pardes.zig"); +const layout = @import("layout.zig"); +const config = @import("config.zig"); +const panes = @import("panes.zig"); +const Pardes = pardes.Pardes; + +/// The finder column's serial (layout.columnSerial); 0 while there is none. +serial: u32 = 0, +/// Its width, in thousandths of the window's: kept while it is closed, so +/// it comes back as wide as it went. +share: u16 = 300, + +/// More query panes than this and the oldest closes: each folded one keeps +/// a tag row. ponytail: a fixed count; size it from the column's rows. +pub const max_results = 8; + +const tag_text = "Finder Delcol"; + +/// The finder column's index, while it is there. +pub fn column(f: *const Finder, p: *const Pardes) ?usize { + if (f.serial == 0) return null; + return layout.columnBySerial(p, f.serial); +} + +/// Its width in cells: its share, leaving every other column MINW; the +/// whole window when it is the only column. +pub fn width(f: *const Finder, p: *const Pardes) u16 { + if (p.ncol <= 1) return p.screen_w; + const others: u32 = @as(u32, config.MINW) * @as(u32, @intCast(p.ncol - 1)); + const most: u32 = @as(u32, p.screen_w) -| others; + const wanted: u32 = @as(u32, p.screen_w) * f.share / 1000; + return @intCast(@min(@max(wanted, config.MINW), @max(most, @min(config.MINW, p.screen_w)))); +} + +/// Query pane `id` goes on top of the finder column, opened at the right +/// edge if there is none; the older ones fold. False when the window has +/// no room for the column (the caller attaches it instead). +pub fn place(f: *Finder, p: *Pardes, id: usize) bool { + const pane = p.panes[id] orelse return false; + const c = f.column(p) orelse f.open(p) orelse return false; + layout.insert(p, c, 0, id); + pane.vweight = 1; + pane.collapsed = false; + f.fold(p, c, id); + // The oldest goes once there are too many: the bottom one, as the top + // is always the newest. + while (p.col_n[c] > max_results) { + const oldest = p.col_panes[c][p.col_n[c] - 1]; + p.removePane(oldest, null) catch break; + } + return true; +} + +/// A finder column at the right edge, empty, its tag its own: the other +/// columns give it its share, each in proportion to its weight. Null when +/// the window has no room for one more column, or narrower columns would +/// leave a pane under its tag and 2 rows. +fn open(f: *Finder, p: *Pardes) ?usize { + if (p.ncol >= pardes.MAX_COLS or p.ncol == 0) return null; + if (p.screen_w < @as(u32, config.MINW) * @as(u32, @intCast(p.ncol + 1))) return null; + const c = p.ncol; + p.col_n[c] = 0; + // Not used for its width (layout.compute), only kept positive for the + // code that halves and joins weights. + p.col_weight[c] = layout.column_weight_unit; + p.col_tags[c] = .{ .what = .column, .gpa = p.gpa }; + p.col_tags[c].own = p.gpa.dupe(u8, tag_text) catch null; + p.col_serial[c] = 0; + p.ncol += 1; + f.serial = layout.columnSerial(p, c); + // A narrower column wraps long tags onto more rows: shared out again, + // and where that cannot hold every minimum there is no finder, as + // Newcol is refused. + layout.compute(p); + for (0..p.ncol) |k| if (k != c and !layout.columnAtMinimums(p, k) and !layout.shareColumn(p, k)) { + layout.dropColumnUnannounced(p, c); + f.serial = 0; + layout.compute(p); + return null; + }; + layout.noteColumn(p, "newcol", f.serial); + return c; +} + +/// Fold every pane of finder column `c` but `top` (and what is attached +/// to it, its +Preview) to its tag. +fn fold(f: *const Finder, p: *Pardes, c: usize, top: usize) void { + _ = f; + layout.compute(p); + for (p.col_panes[c][0..p.col_n[c]]) |k| { + const pane = p.panes[k] orelse continue; + if (k == top or layout.groupRoot(p, k) == top) { + pane.collapsed = false; + pane.vweight = 1; + } else if (!pane.collapsed) { + // As Collapse folds it: its weight keeps its height for later. + pane.vweight = @floatFromInt(@max(1, p.rects[k].h)); + pane.collapsed = true; + } + } + layout.compute(p); +} + +/// Query pane `id`, asked again: back on top of the stack, open, the rest +/// folded. Nothing outside the finder column moves. +pub fn raise(f: *Finder, p: *Pardes, id: usize) void { + const c = f.column(p) orelse return; + const at = layout.findPane(p, id) orelse return; + if (at.col != c) return; + if (at.idx != 0) { + // the pane and what is attached to it (its +Preview) move together + const end = layout.groupEnd(p, c, at.idx); + var moved: [pardes.MAX_PANES]usize = undefined; + const n = end - at.idx; + @memcpy(moved[0..n], p.col_panes[c][at.idx..end]); + for (moved[0..n]) |k| layout.removePane(p, k); + for (moved[0..n], 0..) |k, i| layout.insert(p, c, i, k); + } + f.fold(p, c, id); +} + +/// The column a pane asked for from the finder goes in (a row looked at, a +/// command run there): the column of the pane its query searches, else +/// the nearest other. Null when nothing points at the finder. +pub fn away(f: *Finder, p: *Pardes, from: usize, kind: pardes.exec.NewPane) ?usize { + const fc = f.column(p) orelse return null; + const in_finder = if (layout.findPane(p, from)) |at| at.col == fc else false; + const to_last = (kind == .command or kind == .errors) and fc == p.ncol - 1; + if (!in_finder and p.exec_column != fc and !to_last) return null; + if (in_finder) { + const root = layout.groupRoot(p, from); + if (p.panes[root]) |qp| if (qp.query) |q| if (p.paneBySerial(q.owner)) |oid| if (layout.findPane(p, oid)) |at| + if (at.col != fc) return at.col; + } + if (fc > 0) return fc - 1; + if (fc + 1 < p.ncol) return fc + 1; + // The finder is the only column: a document asked from it gets one of + // its own, left of it. + return layout.insertColumn(p, fc, true); +} + +/// The finder column has lost its last pane: it goes, and every other +/// column is as wide as before it came. +pub fn emptied(f: *Finder, p: *Pardes, c: usize) bool { + if (f.column(p) != c or p.col_n[c] != 0) return false; + layout.dropColumn(p, c); + f.serial = 0; + return true; +} + +/// A drag of the border between the finder column and column `other` +/// beside it: the finder's share follows the edge, and `other` takes the +/// difference, every other column keeping its width. +pub fn dragWidth(f: *Finder, p: *Pardes, other: usize, finder_w: u64) void { + const fc = f.column(p) orelse return; + if (p.screen_w == 0) return; + const combined: u64 = p.col_w[fc] + p.col_w[other]; + if (combined < 2 * @as(u64, config.MINW)) return; + const fw = std.math.clamp(finder_w, config.MINW, combined - config.MINW); + f.share = @intCast(@max(1, @min(999, fw * 1000 / p.screen_w))); + var rest_weight: u128 = 0; + var rest_cells: u64 = 0; + for (0..p.ncol) |k| if (k != fc and k != other) { + rest_weight += p.col_weight[k]; + rest_cells += p.col_w[k]; + }; + if (rest_cells > 0) { + const other_cells = combined - fw; + const w = @as(u128, other_cells) * rest_weight / rest_cells; + p.col_weight[other] = @intCast(std.math.clamp(w, 1, layout.max_column_weight)); + } + layout.compute(p); +} + +fn typeKeys(p: *Pardes, text: []const u8) void { + for (text) |c| p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } }); +} + +test "SearchPlace column: query panes stack in a finder column at the right edge, nothing else moves after it opens, and it goes with its last pane" { + const Key = pardes.Key; + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 160, .rows = 40 }); + defer p.deinit(); + p.settings.search_place = .column; + const pane = try p.setTestFile("alpha one\nbeta\nalphabet\n"); + p.newScratchBelow(0); + const other = p.active; + p.active = 0; + layout.compute(p); + const before = p.rects; + const width_before = p.col_w[0]; + typeKeys(p, "/"); + const fc = p.finder.column(p).?; + try std.testing.expectEqual(@as(usize, 1), fc); + const first = p.col_panes[fc][0]; + try std.testing.expect(p.panes[first].?.query != null); + // the first search narrows the column once; its panes keep their rows + try std.testing.expect(p.col_w[0] < width_before); + try std.testing.expectEqual(before[0].h, p.rects[0].h); + try std.testing.expectEqual(before[other].h, p.rects[other].h); + typeKeys(p, "alpha"); + p.update(.{ .key = .{ .cp = Key.enter } }); + try std.testing.expectEqual(@as(usize, 0), p.active); + try std.testing.expect(pane.places.started); + layout.compute(p); + const opened = p.rects; + // a second search, from the other pane: on top, the first folded + p.active = other; + typeKeys(p, "/"); + const second = p.col_panes[fc][0]; + try std.testing.expect(second != first); + try std.testing.expect(p.panes[first].?.collapsed); + layout.compute(p); + try std.testing.expectEqualDeep(opened[0], p.rects[0]); + try std.testing.expectEqualDeep(opened[other], p.rects[other]); + // Esc: the second closes, the first stays + p.update(.{ .key = .{ .cp = Key.escape } }); + try std.testing.expect(p.panes[second] == null); + try std.testing.expectEqual(fc, p.finder.column(p).?); + // the first's Del: the column goes, every other as wide as before it + pardes.exec.runBuiltin(p, .Del, first, "", null); + try std.testing.expect(p.finder.column(p) == null); + layout.compute(p); + try std.testing.expectEqual(width_before, p.col_w[0]); + try std.testing.expectEqualDeep(before[0], p.rects[0]); +} diff --git a/src/Query.zig b/src/Query.zig index 3a75a7f5..6374ce42 100644 --- a/src/Query.zig +++ b/src/Query.zig @@ -257,6 +257,7 @@ pub fn open(p: *Pardes, from: usize, source: Source, arg: []const u8, focus: boo const new = std.mem.concat(p.gpa, u8, &.{ line, "\n" }) catch return; File.setContent(p, f, new); q.reset(p); + if (p.finder.column(p)) |fc| if (layout.findPane(p, id)) |at| if (at.col == fc) p.finder.raise(p, id); if (rp.collapsed) layout.toggleCollapse(p, id); // A source that previews gets its +Preview now, at the key that // asked, not under the typing later. @@ -318,8 +319,11 @@ fn create(p: *Pardes, owner_id: usize, line: []const u8, source: Source) ?usize } } if (p.settings.search_place == .column) { - // The finder column (phase 6): until it exists, under the owner. - if (!layout.attach(p, owner_id, free, want)) exec.placeNew(p, owner_id, owner_id, free, .doc); + // On top of the finder column at the right edge (Finder.zig), the + // older query panes folded under it; its preview under it there. + if (!p.finder.place(p, free) and !layout.attach(p, owner_id, free, want)) exec.placeNew(p, owner_id, owner_id, free, .doc); + if (with_preview) _ = makePreview(p, free, p.rects[free].h / 2, free); + return free; } else if (!layout.attach(p, owner_id, free, want)) { // An owner too short to give a query pane its rows: placed as any // new pane is, the one place a search moves another pane. @@ -1727,8 +1731,11 @@ pub fn cancel(p: *Pardes, id: usize) void { if (owner_id) |oid| p.active = oid; return; } + // Attached, its rows go back to its owner; in the finder column, to its + // own column's. + const attached = layout.ownerOf(p, id) != null; if (owner_id) |oid| { - p.removePane(id, oid) catch {}; + p.removePane(id, if (attached) oid else null) catch {}; p.active = oid; if (layout.findPane(p, oid)) |at| p.setActiveColumn(at.col); } else p.removePane(id, null) catch {}; @@ -1772,7 +1779,9 @@ pub fn key(p: *Pardes, id: usize, k: Key) bool { const q = rp.query orelse return false; if (rp.focus != .body) return false; const t = &rp.body; - if (q.frozen) return false; + // Normal mode, live or frozen: n and N step the rows (the current one + // shown where it points, the keyboard staying), Enter on a hit row lands + // on it, as Enter typing does. if (t.mode == .normal) { if (t.normal.prefix != .none or k.ctrl or k.alt or t.normal.count != 0) return false; if (k.cp == 'n' or k.cp == 'N') { @@ -1780,11 +1789,14 @@ pub fn key(p: *Pardes, id: usize, k: Key) bool { return true; } if (hit(k, config.look_key) and t.cur_row > 0) { - land(p, id, @intCast(t.cur_row - 1)); + const i: usize = @intCast(t.cur_row - 1); + if (i >= q.items.items.len or q.items.items[i].kind != .hit) return false; + land(p, id, i); return true; } return false; } + if (q.frozen) return false; if (t.cur_row != 0 or t.mode != .insert or t.reg_wait) return false; if (hit(k, config.escape)) { cancel(p, id); @@ -2003,6 +2015,11 @@ test "a / query: Enter lands and freezes, the register filled, n and N step sile defer std.testing.allocator.free(frozen); // the rows: one a line (gamma alpha has one), a gutter number each try std.testing.expect(std.mem.indexOf(u8, frozen, " 1 alpha one\n") != null); + // B3 on a row's line number is its address; on its code, no address + var abuf: [256]u8 = undefined; + const addr = rowAddress(qp, 1, 2, &abuf).?; + try std.testing.expect(std.mem.endsWith(u8, addr, ":1:1")); + try std.testing.expect(rowAddress(qp, 1, qp.file.?.location_rows[1].code_start + 1, &abuf) == null); // n and N in the owner step the same set, round the end silently const logged = p.messages.len; try std.testing.expectEqual(@as(?usize, 0), pane.places.current); diff --git a/src/exec.zig b/src/exec.zig index 2bc1a3e5..142dfef6 100644 --- a/src/exec.zig +++ b/src/exec.zig @@ -1197,6 +1197,14 @@ pub fn placeNew(p: *Pardes, from: usize, origin: ?usize, id: usize, kind: NewPan // No column left (the last one Delcol'd): a pane made from the workspace // tag opens one to go in. _ = layout.firstColumn(p); + // Anything asked from the finder column (a row looked at, a command + // there) goes where its query came from, never into the finder; and + // the rules that take the last column take the last that is not it. + const exec_column = p.exec_column; + defer p.exec_column = exec_column; + const away = p.finder.away(p, from, kind); + if (away) |c| p.exec_column = c; + const finder_column = p.finder.column(p); const asked = if (p.exec_column != null) null else origin; layout.compute(p); const before = p.rects; @@ -1206,9 +1214,10 @@ pub fn placeNew(p: *Pardes, from: usize, origin: ?usize, id: usize, kind: NewPan // (look.c:921-923, et->col). .scratch => layout.acmePlace(p, p.exec_column orelse columnOf(p, from), null, id), // +Errors, a command's output: the last column (util.c:94-98); - // a command run from a column's tag, that column. + // a command run from a column's tag, that column. The finder's + // is never the last column for them. .command => layout.acmePlace(p, p.exec_column orelse p.ncol - 1, null, id), - .errors => layout.acmePlace(p, p.ncol - 1, null, id), + .errors => layout.acmePlace(p, away orelse p.ncol - 1, null, id), // makenewwindow(nil) for 9P's new (acme.c:877), else (t). .fs => layout.acmePlace(p, acmeColumn(p, from), null, id), .shell, .doc => layout.acmePlace(p, acmeColumn(p, from), asked, id), @@ -1238,7 +1247,7 @@ pub fn placeNew(p: *Pardes, from: usize, origin: ?usize, id: usize, kind: NewPan var c = p.ncol; while (c > 0) { c -= 1; - if (c == f.col) continue; + if (c == f.col or c == finder_column) continue; for (p.col_panes[c][0..p.col_n[c]]) |k| p.rects[k] = before[k]; layout.snapColWeights(p, c); if (p.col_n[c] == 0) { diff --git a/src/layout.zig b/src/layout.zig index dac0aa7e..8739122c 100644 --- a/src/layout.zig +++ b/src/layout.zig @@ -166,6 +166,11 @@ pub fn columnDrop(p: *const Pardes, source: usize, x: u16, grab: u16) ColumnDrop pub fn setColumnPairWidth(p: *Pardes, left: usize, left_width: u64) void { if (left + 1 >= p.ncol) return; const combined: u64 = p.col_w[left] + p.col_w[left + 1]; + // The finder's edge sets its share (Finder.dragWidth). + if (p.finder.column(p)) |fc| { + if (fc == left + 1) return p.finder.dragWidth(p, left, combined -| left_width); + if (fc == left) return p.finder.dragWidth(p, left + 1, left_width); + } // Too narrow for both to keep MINW: any split would squeeze one away. if (combined < 2 * @as(u64, config.MINW)) return; const width = std.math.clamp(left_width, config.MINW, @max(@as(u64, config.MINW), combined -| config.MINW)); @@ -467,7 +472,12 @@ pub fn dropColumn(p: *Pardes, c: usize) void { pub fn dropColumnUnannounced(p: *Pardes, c: usize) void { std.debug.assert(p.col_n[c] == 0); tagline.exitHeader(p); - if (p.ncol > 1) p.col_weight[if (c > 0) c - 1 else c + 1] +|= p.col_weight[c]; + // The finder's width was its share, no weight: nobody gets one, so the + // others are as wide as before it came. A column whose weight would go + // to the finder gives it to the column on the finder's other side. + const finder = p.finder.column(p); + const heir: ?usize = if (p.ncol <= 1 or finder == c) null else if (c > 0 and finder != c - 1) c - 1 else if (c + 1 < p.ncol and finder != c + 1) c + 1 else if (c > 1) c - 2 else if (c + 2 < p.ncol) c + 2 else null; + if (heir) |h| p.col_weight[h] +|= p.col_weight[c]; p.col_tags[c].deinit(p.gpa); var j = c; while (j + 1 < p.ncol) : (j += 1) { @@ -1187,20 +1197,32 @@ pub fn compute(p: *Pardes) void { if (p.ncol == 0) return; keepGroups(p); for (0..p.ncol) |column| _ = columnSerial(p, column); + // The finder column is as wide as its share says (Finder.width), not + // by weight: the others share what it leaves by theirs, so it comes + // and goes and each of them keeps its proportion, none rounded off. + const finder = p.finder.column(p); + const finder_w: u16 = if (finder != null) p.finder.width(p) else 0; + const rest = p.screen_w -| finder_w; var wsum: u128 = 0; - for (0..p.ncol) |c| wsum += p.col_weight[c]; + var last_plain: usize = p.ncol; + for (0..p.ncol) |c| if (finder != c) { + wsum += p.col_weight[c]; + last_plain = c; + }; if (wsum == 0) wsum = 1; // Round cumulative boundaries so widths still sum to the available screen. var prefix: u128 = 0; + var offset: u16 = 0; for (0..p.ncol) |c| { - const last = c + 1 == p.ncol; - const x = columnBoundary(p.screen_w, prefix, wsum); - prefix += p.col_weight[c]; - const end: u16 = if (last) - p.screen_w - else - columnBoundary(p.screen_w, prefix, wsum); + const x = offset + columnBoundary(rest, prefix, wsum); + const end: u16 = if (finder == c) end: { + offset = finder_w; + break :end x + finder_w; + } else end: { + prefix += p.col_weight[c]; + break :end if (c == last_plain) offset + rest else offset + columnBoundary(rest, prefix, wsum); + }; const cw = end -| x; p.col_x[c] = x; p.col_w[c] = cw; diff --git a/src/pardes.zig b/src/pardes.zig index 3ccfee5f..6f30d6ca 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -26,6 +26,7 @@ pub const Registers = @import("Registers.zig"); pub const Macro = @import("Macro.zig"); pub const Pipe = @import("Pipe.zig"); pub const Stream = @import("Stream.zig"); +pub const Finder = @import("Finder.zig"); pub const colors = @import("colors.zig"); pub const Theme = colors.Theme; pub const themes = colors.themes; @@ -490,6 +491,7 @@ test { _ = @import("Query.zig"); _ = @import("Names.zig"); _ = @import("Stream.zig"); + _ = @import("Finder.zig"); } pub const Builtin = builtins.registry.Builtin(); @@ -4386,6 +4388,9 @@ pub const Pardes = struct { col_tags: [MAX_COLS]Text = @splat(.{ .what = .column }), col_serial: [MAX_COLS]u32 = @splat(0), next_column_serial: u32 = 1, + /// The finder column, where query panes stack with SearchPlace column + /// (Finder.zig). + finder: Finder = .{}, global_tag: Text = .{ .what = .workspace }, // derived each sync rects: [MAX_PANES]Rect = @splat(.{}), @@ -5225,14 +5230,19 @@ pub const Pardes = struct { pub fn newColumn(p: *Pardes, from_id: usize) void { // An empty window (Delcol took the last column): its first column. if (layout.firstColumn(p)) |c| return tagline.enterHeader(p, c); - const col = p.keyboardColumn(from_id) orelse return; + const keyboard_col = p.keyboardColumn(from_id) orelse return; + // Asked from the finder: the new column goes left of it, so the + // finder stays at the edge as wide. + const in_finder = p.finder.column(p) == keyboard_col; + const before_finder = in_finder and keyboard_col == 0; + const col = if (in_finder and keyboard_col > 0) keyboard_col - 1 else keyboard_col; // A trial: laying it out gives it a serial (layout.compute), which a // refusal takes back. Every other column has its own already, so // the counter as it is now is the trial's to return to. for (0..p.ncol) |k| _ = layout.columnSerial(p, k); const unspent = p.next_column_serial; // Said, not silently nothing: "no space" is 9ns's word for ENOSPC. - const c = layout.insertColumnUnannounced(p, col, false) orelse { + const c = layout.insertColumnUnannounced(p, col, before_finder) orelse { var said: [96]u8 = undefined; return p.reportFailure(from_id, if (p.ncol >= MAX_COLS) std.fmt.bufPrint(&said, "Newcol: no space for a column: {d} max", .{MAX_COLS}) catch "Newcol: no space for a column" @@ -7224,9 +7234,11 @@ pub const Pardes = struct { const beside = if (emptied) |c| p.besideColumn(c, p.rects[id]) else null; layout.removePane(p, id); p.panes[id] = null; + // The finder's last pane takes the column with it. + const finder_gone = if (emptied) |c| p.finder.emptied(p, c) else false; if (p.active != id) return; p.active = gainer orelse beside orelse p.anyPane() orelse return p.quitEmpty(); - if (emptied) |c| if (!p.header_focus) tagline.enterHeader(p, c); + if (emptied) |c| if (!finder_gone and !p.header_focus) tagline.enterHeader(p, c); } /// Delcol: close column `col` and every pane in it. |
