diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-28 17:56:30 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-10-28 21:05:25 -0300 |
| commit | 96f840a055651b88eb1191772bc4b10fcbbb60ce (patch) | |
| tree | 55ccd6e05fc1f7419f1fef3fb2c7116b556c85d2 | |
| parent | f8c27dbfa9afca327189181a668f2191b499bea5 (diff) | |
| download | pardes-96f840a055651b88eb1191772bc4b10fcbbb60ce.tar.gz pardes-96f840a055651b88eb1191772bc4b10fcbbb60ce.zip | |
layout: a new pane takes rows only from the pane it splits, as acme's coladd does: no column shared out evenly after a placement (only a pane the rounding squeezed, and the new one, lifted to their minimum, restoreMinimaFrom), none when there is no pane to halve (takeRoom: its minimum from the panes with the most to spare), none after a refused pane (it held no weight; absorbVWeight gives back and snaps nothing for it), none after Newcol or the finder column (only a pane whose tag now wraps past its rows is lifted, squeezed); a pane dragged short keeps its rows through all of them; test: a pane made from the middle between two short ones, then Newcol
Co-Authored-By: Claude Opus 5.5 <[email protected]>
| -rw-r--r-- | src/Finder.zig | 13 | ||||
| -rw-r--r-- | src/exec.zig | 47 | ||||
| -rw-r--r-- | src/layout.zig | 72 | ||||
| -rw-r--r-- | src/pardes.zig | 65 |
4 files changed, 159 insertions, 38 deletions
diff --git a/src/Finder.zig b/src/Finder.zig index a2b8f89f..e72a8dab 100644 --- a/src/Finder.zig +++ b/src/Finder.zig @@ -73,6 +73,8 @@ pub fn place(f: *Finder, p: *Pardes, id: usize) bool { 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 before_min = layout.minima(p); + const before = p.rects; const c = p.ncol; p.col_n[c] = 0; // Not used for its width (layout.compute), only kept positive for the @@ -83,11 +85,12 @@ fn open(f: *Finder, p: *Pardes) ?usize { 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)) { + // A narrower column wraps long tags onto more rows: those rows back + // from the panes with rows to spare, shared out again only where that + // is not enough, and where nothing holds every minimum there is no + // finder, as Newcol is refused. + layout.restoreMinimaFrom(p, &before, &before_min); + for (0..p.ncol) |k| if (k != c and layout.squeezed(p, k, &before, &before_min) and !layout.shareColumn(p, k)) { layout.dropColumnUnannounced(p, c); f.serial = 0; layout.compute(p); diff --git a/src/exec.zig b/src/exec.zig index 5b0eb204..c14a648c 100644 --- a/src/exec.zig +++ b/src/exec.zig @@ -457,12 +457,9 @@ fn spawnTtyWithMount(p: *Pardes, from: usize, v9fs: bool) ?*Pane { fn refusedPlacement(p: *Pardes, id: usize) bool { if (!p.unplaced.isSet(id)) return false; p.unplaced.unset(id); + // It held no weight (placeNew put every pane's rows back as they + // were): nothing else moves, a pane dragged short included. p.removePane(id, null) catch {}; - // As sync does for a refused pane: none it squeezed stays under its - // minimum. - for (0..p.ncol) |c| if (!layout.columnAtMinimums(p, c)) { - _ = layout.shareColumn(p, c); - }; return true; } @@ -1208,6 +1205,7 @@ pub fn placeNew(p: *Pardes, from: usize, origin: ?usize, id: usize, kind: NewPan const asked = if (p.exec_column != null) null else origin; layout.compute(p); const before = p.rects; + const before_min = layout.minima(p); switch (p.settings.placement) { .acme => switch (kind) { // New: the bottom half of the last pane in its own column @@ -1233,17 +1231,22 @@ pub fn placeNew(p: *Pardes, from: usize, origin: ?usize, id: usize, kind: NewPan for (p.col_panes[f.col][0..p.col_n[f.col]]) |k| p.rects[k] = before[k]; layout.snapColWeights(p, f.col); if (!layout.splitTallest(p, f.col, id) or !layout.placedRoomy(p, id, &before)) { - if (layout.findPane(p, id) == null) layout.insert(p, f.col, p.col_n[f.col], id); - // No pane to halve, but the column holds every pane's minimum - // with the new one's: the rows are shared out again, so what is - // refused is only what the documented arithmetic cannot hold. + if (layout.findPane(p, id) != null) { + layout.removePane(p, id); + for (p.col_panes[f.col][0..p.col_n[f.col]]) |k| p.rects[k] = before[k]; + layout.snapColWeights(p, f.col); + } + // No pane to halve, but the column has the rows for the new + // one's minimum above the others': it takes them from those with + // the most to spare (a pane dragged short keeps its own), so + // what is refused is only what the documented arithmetic cannot + // hold. // +Errors and 9P's pane/new go wherever there is room, last // column first, as acme takes rows from a window that has them // before saying there are none; with none, +Errors's maker says // the text some other way (appendErrors), and pane/new is // refused. - if (layout.shareColumn(p, f.col)) {} else if (kind == .errors or kind == .fs) { - layout.removePane(p, id); + if (layout.takeRoom(p, f.col, id)) {} else if (kind == .errors or kind == .fs) { var c = p.ncol; while (c > 0) { c -= 1; @@ -1259,14 +1262,15 @@ pub fn placeNew(p: *Pardes, from: usize, origin: ?usize, id: usize, kind: NewPan } else { layout.insert(p, f.col, p.col_n[f.col], id); p.unplaced.set(id); + // Refused holding no weight: its column's rows come + // back as they were. + p.panes[id].?.vweight = 0; // No pane anywhere can give it rows: pane/new is refused. - if (kind == .fs) { - p.panes[id].?.vweight = 0; - p.reportError(from, "placement", error.NoPaneRoom); - } + if (kind == .fs) p.reportError(from, "placement", error.NoPaneRoom); return; } } else { + layout.insert(p, f.col, p.col_n[f.col], id); // Refused, it closes at the step's end holding no weight, so // its column's rows come back as they were, none rounded off. p.panes[id].?.vweight = 0; @@ -1276,11 +1280,14 @@ pub fn placeNew(p: *Pardes, from: usize, origin: ?usize, id: usize, kind: NewPan } } }; - // Placed: no pane of its column left under its minimum by the rows - // rounding or an earlier placement gave it; shared out again if so. - if (layout.findPane(p, id)) |f| if (!layout.columnAtMinimums(p, f.col)) { - _ = layout.shareColumn(p, f.col); - }; + // Placed: no pane the rows' rounding took under its minimum stays + // there. Only those: a pane dragged short before keeps its rows, and + // the others keep theirs, as acme's coladd moves only the window it + // splits (cols.c:41-108). The new pane had nothing to keep: it is + // lifted to its minimum too. + var settled = before; + settled[id].h = std.math.maxInt(u16); + layout.restoreMinimaFrom(p, &settled, &before_min); if (kind == .command) return; tagline.exitHeader(p); p.active = id; diff --git a/src/layout.zig b/src/layout.zig index efa79394..f545f60d 100644 --- a/src/layout.zig +++ b/src/layout.zig @@ -681,6 +681,24 @@ pub fn shareColumn(p: *Pardes, c: usize) bool { /// from whichever pane of its column has the most above its own. A pane /// dragged short before keeps what it had. pub fn restoreMinima(p: *Pardes, before: *const [MAX_PANES]Rect) void { + restoreMinimaFrom(p, before, null); +} + +/// Each pane's `minRows` now: what `restoreMinimaFrom` measures a step that +/// grows the minima (a narrower column wrapping tags) against. +pub fn minima(p: *Pardes) [MAX_PANES]u16 { + compute(p); + var mins: [MAX_PANES]u16 = @splat(0); + for (0..p.ncol) |c| for (p.col_panes[c][0..p.col_n[c]]) |k| { + mins[k] = minRows(p, k); + }; + return mins; +} + +/// restoreMinima, a pane's minimum before the step `before_min` (its tag +/// may wrap onto more rows since): one that had that minimum is lifted to +/// its minimum now, one dragged under it before keeps its rows. +pub fn restoreMinimaFrom(p: *Pardes, before: *const [MAX_PANES]Rect, before_min: ?*const [MAX_PANES]u16) void { compute(p); for (0..p.ncol) |c| { const n = p.col_n[c]; @@ -693,11 +711,11 @@ pub fn restoreMinima(p: *Pardes, before: *const [MAX_PANES]Rect) void { if (pane.collapsed) break; hs[i] = p.rects[k].h; mins[i] = minRows(p, k); - if (hs[i] < mins[i] and before[k].h >= mins[i]) short = true; + if (hs[i] < mins[i] and before[k].h >= if (before_min) |bm| bm[k] else mins[i]) short = true; } else { if (!short) continue; for (p.col_panes[c][0..n], 0..) |k, i| { - if (hs[i] >= mins[i] or before[k].h < mins[i]) continue; + if (hs[i] >= mins[i] or before[k].h < if (before_min) |bm| bm[k] else mins[i]) continue; while (hs[i] < mins[i]) { var giver: ?usize = null; var spare: u16 = 0; @@ -716,6 +734,53 @@ pub fn restoreMinima(p: *Pardes, before: *const [MAX_PANES]Rect) void { compute(p); } +/// Whether a pane of column `c` is under its minimum now that had its +/// minimum then (`before`, `before_min`): one a step squeezed, not one +/// dragged short. +pub fn squeezed(p: *Pardes, c: usize, before: *const [MAX_PANES]Rect, before_min: *const [MAX_PANES]u16) bool { + compute(p); + if (p.col_n[c] < 2) return false; + for (p.col_panes[c][0..p.col_n[c]]) |k| { + const pane = p.panes[k] orelse continue; + if (!pane.collapsed and p.rects[k].h < minRows(p, k) and before[k].h >= before_min[k]) return true; + } + return false; +} + +/// Column `c`'s rows for `id`, last in it, at its minimum: a row at a +/// time from whichever pane has the most above its own minimum, so a pane +/// dragged short keeps its rows and the others give only what they must. +/// False, changing nothing, when the column has not the rows. +pub fn takeRoom(p: *Pardes, c: usize, id: usize) bool { + compute(p); + const n = p.col_n[c]; + p.rects[id].w = p.col_w[c]; + var need = minRows(p, id); + var hs: [MAX_PANES]u16 = undefined; + var mins: [MAX_PANES]u16 = undefined; + var spare: u32 = 0; + for (p.col_panes[c][0..n], 0..) |k, i| { + const pane = p.panes[k] orelse return false; + if (pane.collapsed) return false; + hs[i] = p.rects[k].h; + mins[i] = minRows(p, k); + spare += hs[i] -| mins[i]; + } + if (spare < need) return false; + while (need > 0) : (need -= 1) { + var giver: usize = 0; + for (1..n) |i| if (hs[i] -| mins[i] > hs[giver] -| mins[giver]) { + giver = i; + }; + hs[giver] -= 1; + } + for (p.col_panes[c][0..n], 0..) |k, i| p.panes[k].?.vweight = @floatFromInt(@max(1, hs[i])); + insert(p, c, n, id); + p.panes[id].?.vweight = @floatFromInt(minRows(p, id)); + compute(p); + return true; +} + /// After placing `id`: whether it, and every pane of its column that the /// placement shrank, still has its tag and `min_body_rows` (from `before`, /// the rects as they were). A pane alone in its column always has room: the @@ -908,6 +973,9 @@ pub fn snapColWeights(p: *Pardes, c: usize) void { pub fn absorbVWeight(p: *Pardes, id: usize, to: ?usize) ?usize { const f = findPane(p, id) orelse return null; if (p.col_n[f.col] <= 1) return null; + // A refused pane held no weight: the rows the others' rounding lent it + // go back to them as they were, nobody's weight snapped. + if (p.panes[id]) |pane| if (!pane.collapsed and pane.vweight == 0) return null; snapColWeights(p, f.col); var sib = to orelse if (f.idx > 0) p.col_panes[f.col][f.idx - 1] else p.col_panes[f.col][f.idx + 1]; var k = f.idx; diff --git a/src/pardes.zig b/src/pardes.zig index 20415e69..3721cc32 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -1353,6 +1353,48 @@ test "acme placement: under a pane's text when it has many blank rows, else halv try std.testing.expectEqual(was.y + was.h, p.rects[third].y + p.rects[third].h); } +test "acme placement: a pane made from the middle of a column moves only the one it splits, short ones round it kept short" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 60 }); + defer p.deinit(); + const top = p.active; + _ = try p.setTestFile("one\n"); + p.sync(); + p.setActiveColumn(0); + const middle = p.freeSlot().?; + _ = try p.openTutorView(middle); + exec.placeNew(p, top, top, middle, .doc); + const bottom = p.freeSlot().?; + _ = try p.openTutorView(bottom); + exec.placeNew(p, middle, middle, bottom, .doc); + p.sync(); + // A short pane above and below, dragged under a placement's minimum, + // and the middle one full of text, so it is the one halved. + const full = std.mem.asBytes(&@as([80][2]u8, @splat("x\n".*))); + for ([_]usize{ top, middle, bottom }) |id| panes.File.setContent(p, &p.panes[id].?.file.?, try p.gpa.dupe(u8, full)); + try std.testing.expectEqualSlices(usize, &.{ top, middle, bottom }, p.col_panes[0][0..3]); + const total = p.rects[top].h + p.rects[middle].h + p.rects[bottom].h; + p.panes[top].?.vweight = 2; + p.panes[bottom].?.vweight = 2; + p.panes[middle].?.vweight = @floatFromInt(total - 4); + p.sync(); + const was_top = p.rects[top]; + const was_bottom = p.rects[bottom]; + try std.testing.expect(was_top.h < layout.minRows(p, top)); + const made = p.freeSlot().?; + _ = try p.openTutorView(made); + exec.placeNew(p, middle, middle, made, .doc); + p.sync(); + try std.testing.expectEqual(layout.findPane(p, middle).?.idx + 1, layout.findPane(p, made).?.idx); + try std.testing.expectEqual(was_top, p.rects[top]); + try std.testing.expectEqual(was_bottom, p.rects[bottom]); + // A new column narrows this one, and the short panes keep their rows. + try std.testing.expect(p.executeBuiltinLine(made, "Newcol")); + p.sync(); + try std.testing.expectEqual(@as(usize, 2), p.ncol); + try std.testing.expectEqual(was_top.h, p.rects[top].h); + try std.testing.expectEqual(was_bottom.h, p.rects[bottom].h); +} + test "acme placement: New and 9P's new take the bottom half of the last pane" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 40 }); defer p.deinit(); @@ -5273,6 +5315,8 @@ pub const Pardes = struct { // 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; + const before_min = layout.minima(p); + const before = p.rects; // Said, not silently nothing: "no space" is 9ns's word for ENOSPC. const c = layout.insertColumnUnannounced(p, col, before_finder) orelse { var said: [96]u8 = undefined; @@ -5282,10 +5326,13 @@ pub const Pardes = struct { "Newcol: no space for a column: this one is too narrow to split"); }; // A narrower column wraps long tags onto more rows, which can leave - // a pane under its tag and two rows: its column's rows are shared - // out again, and where they cannot hold every minimum there is no - // new column, as a size too small is refused. - for (0..p.ncol) |k| if (!layout.columnAtMinimums(p, k) and !layout.shareColumn(p, k)) { + // a pane under its tag and two rows: it takes them back from the + // panes with rows to spare (a pane dragged short keeps its own), + // its column shared out again only where that is not enough, and + // where nothing holds every minimum there is no new column, as a + // size too small is refused. + layout.restoreMinimaFrom(p, &before, &before_min); + for (0..p.ncol) |k| if (layout.squeezed(p, k, &before, &before_min) and !layout.shareColumn(p, k)) { layout.dropColumnUnannounced(p, c); p.next_column_serial = unspent; layout.compute(p); @@ -7567,18 +7614,14 @@ pub const Pardes = struct { } pub fn sync(p: *Pardes) void { - var refused = false; while (p.unplaced.findFirstSet()) |id| { p.unplaced.unset(id); // Refused, it was never there. Its serial is not handed out - // again: one logged or answered names this pane for good. + // again: one logged or answered names this pane for good. Its + // rows, if it had any, go where a closed pane's go; no other + // pane moves (a pane dragged short keeps its rows). if (p.panes[id] != null) p.removePane(id, null) catch {}; - refused = true; } - // A refused pane gone, no pane it squeezed stays under its minimum. - if (refused) for (0..p.ncol) |c| if (!layout.columnAtMinimums(p, c)) { - _ = layout.shareColumn(p, c); - }; // A file opened or closed this step: the kept list follows. p.saveRecent(); tagline.flattenHeaders(p); |
