diff options
Diffstat (limited to 'core')
| -rw-r--r-- | core/Pick.zig | 145 | ||||
| -rw-r--r-- | core/Trail.zig | 29 | ||||
| -rw-r--r-- | core/exec.zig | 2 | ||||
| -rw-r--r-- | core/layout.zig | 307 | ||||
| -rw-r--r-- | core/look.zig | 18 |
5 files changed, 460 insertions, 41 deletions
diff --git a/core/Pick.zig b/core/Pick.zig index b76020ca..2bcaf76f 100644 --- a/core/Pick.zig +++ b/core/Pick.zig @@ -233,9 +233,11 @@ fn make(p: *Pardes, source: Source) !*Pick { } /// Picker `pk` asked from pane `from`: the pane it lands in is `from` when -/// it reads; its reader when it is held and has one; else a pane made under -/// it as its reader would be, empty until the preview fills it. The -/// keyboard stays where it was: the landing pane is the tag's host. +/// it reads; its reader when it is held and has one; else an untouched +/// pane, `from` itself first (UX J1), which previews as a reading pane does +/// and is given back as it was on Esc; else a pane made where its reader +/// would go, empty until the preview fills it. The keyboard stays where it +/// was: the landing pane is the tag's host. fn hostIn(p: *Pardes, pk: *Pick, from: usize) !void { const asker = p.panes[from].?; pk.owner = asker.serial; @@ -247,13 +249,17 @@ fn hostIn(p: *Pardes, pk: *Pick, from: usize) !void { p.view().active = from; return; } - const reader = if (Trail.reading(p, asker)) from else Trail.readerOf(p, asker); + var reader = if (Trail.reading(p, asker)) from else Trail.readerOf(p, asker); + if (reader == null) if (layout.freeSpace(p, from, .reader)) |free| if (free == .pane) { + reader = free.pane; + }; pk.landing = p.panes[reader orelse try landingFor(p, from)].?.serial; pk.made = reader == null; p.view().active = from; } -/// A pane made under pane `from` for a picker, where its reader goes. +/// A pane made for a picker where pane `from`'s reader goes: under it, or +/// an empty column (layout.freeSpace). fn landingFor(p: *Pardes, from: usize) !usize { const asker = p.panes[from].?; const free = p.freeSlot() orelse return error.NoPaneSlots; @@ -1340,8 +1346,10 @@ const How = enum { walk, below, column, queue }; /// Enter: the current row, or the marked ones, looked at from the asking /// pane (look.open, the walk's way: a reading pane steps, a held one's /// reader does), the first as `how` says, the others each in a pane below. -/// The list rolls into the landing pane's trail as its word (item 4). -fn land(p: *Pardes, how: How) void { +/// The list rolls into the landing pane's trail as its word (item 4). From +/// an untouched pane S-Enter and C-v are Enter: it is the free space a new +/// pane would take (UX J1). +fn land(p: *Pardes, wanted: How) void { const pk = live(p) orelse return; var arena = std.heap.ArenaAllocator.init(p.gpa); defer arena.deinit(); @@ -1352,7 +1360,7 @@ fn land(p: *Pardes, how: How) void { pk.queue.clearRetainingCapacity(); pk.queue_bytes.clearRetainingCapacity(); pk.queue_at = 0; - if (how == .queue) for (picked[1..]) |t| switch (t) { + if (wanted == .queue) for (picked[1..]) |t| switch (t) { .place => |at| { const q: Queued = .{ .path = @intCast(pk.queue_bytes.items.len), .len = @intCast(@min(at.path.len, std.math.maxInt(u16))), .line = std.math.lossyCast(u32, at.at.line), .col = std.math.lossyCast(u32, at.at.col) }; pk.queue_bytes.appendSlice(p.gpa, at.path[0..q.len]) catch break; @@ -1360,13 +1368,16 @@ fn land(p: *Pardes, how: How) void { }, else => {}, }; - // A step from the pane it lands in when that reads (its owner's reader - // or itself), else from its owner, whose reader the place opens as. - const landing = if (pk.made) null else p.paneBySerial(pk.landing); TagState.toRest(p); const v = p.view(); const owner = p.paneBySerial(pk.owner) orelse v.active; - const from = if (how == .walk or how == .queue) landing orelse owner else owner; + const how: How = if ((wanted == .below or wanted == .column) and Trail.untouched(p, p.panes[owner].?)) .walk else wanted; + // A step from the pane it lands in when that reads (its owner's reader + // or itself), else from its owner, whose reader the place opens as: + // where the pane made for the picker was, or in the untouched pane it + // previewed in (UX J1), given back now. + const landing = p.paneBySerial(pk.landing) orelse owner; + const from = if ((how == .walk or how == .queue) and Trail.reading(p, p.panes[landing].?)) landing else owner; v.active = from; v.jumps.force = true; for (picked, 0..) |t, k| switch (t) { @@ -1383,9 +1394,11 @@ fn land(p: *Pardes, how: How) void { else => listed(p, pk), } } else if (k == 0 and how == .column) { - layout.newColumn(p, from); - const c = layout.activeColumn(p) orelse continue; - p.exec_column = c; + // A column made only where there is no free space (UX J1). + if (layout.freeSpace(p, from, .doc) == null) { + layout.newColumn(p, from); + p.exec_column = layout.activeColumn(p) orelse continue; + } defer p.exec_column = null; pardes.tagline.exitHeader(p); _ = look.open(p, from, at.path, at.at, .below); @@ -1978,6 +1991,7 @@ test "Enter lands as a walk step in the reading pane; S-Enter opens below; from const b_path = try testFile(p, &tmp, "beta.zig", "one\ntwo\nthree\n"); defer p.gpa.free(b_path); look.lookAt(p, p.view().active, a_path); + p.sync(); const id = p.view().active; const npanes = paneCount(p); open(p, id, .list, "Refs", "", "beta.zig:3:1 three\n"); @@ -2193,6 +2207,7 @@ test "a picker from a reading pane unrolls in that pane, its own text set aside const b_path = try testFile(p, &tmp, "beta.zig", "one\ntwo\nthree\n"); defer p.gpa.free(b_path); look.lookAt(p, p.view().active, a_path); + p.sync(); const id = p.view().active; try std.testing.expect(Trail.reading(p, p.panes[id].?)); const npanes = paneCount(p); @@ -2620,3 +2635,103 @@ test "Backspace on a Messages picker's empty field goes back to the level it was p.sync(); _ = try p.render(frame.allocator()); } + +test "a picker started in an untouched +New previews in it (UX J1): no pane made, Esc gives it back empty, Enter lands in it, the same pane by its number, reading, its trail starting at the list word; S-Enter there is Enter" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 140, .rows = 50 }); + defer p.deinit(); + p.settings.jumps = .walk; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + const b_path = try testFile(p, &tmp, "beta.zig", "one\ntwo\nthree\n"); + defer p.gpa.free(b_path); + const rows = try std.fmt.allocPrint(p.gpa, "{s}:3:1 three\n", .{b_path}); + defer p.gpa.free(rows); + _ = try p.setTestFile("first\n"); + for ([_]bool{ false, true }) |shift| { + exec.runBuiltin(p, .New, 0, "New", null); + p.sync(); + const scratch = p.view().active; + const serial = p.panes[scratch].?.serial; + const top = p.view().rects[scratch].y; + const npanes = paneCount(p); + try std.testing.expect(Trail.untouched(p, p.panes[scratch].?)); + open(p, scratch, .list, "Refs", "", rows); + settle(p); + p.update(.tick); + try std.testing.expect(!live(p).?.made); + try std.testing.expectEqual(serial, live(p).?.landing); + try std.testing.expectEqual(npanes, paneCount(p)); + try std.testing.expectEqualStrings(b_path, p.panes[scratch].?.file.?.path); + // Esc: the +New as it was, still free space. + p.update(.{ .key = .{ .cp = Key.escape } }); + try std.testing.expectEqual(npanes, paneCount(p)); + try std.testing.expect(Trail.untouched(p, p.panes[scratch].?)); + try std.testing.expectEqual(serial, p.panes[scratch].?.serial); + // Enter (or S-Enter): the file in it. + open(p, scratch, .list, "Refs", "", rows); + settle(p); + p.update(.{ .key = .{ .cp = Key.enter, .shift = shift } }); + p.sync(); + try std.testing.expect(live(p) == null); + const here = p.panes[p.view().active].?; + try std.testing.expectEqual(serial, here.serial); + try std.testing.expectEqualStrings(b_path, here.file.?.path); + try std.testing.expectEqual(@as(i32, 2), here.body.cur_row); + try std.testing.expectEqual(npanes, paneCount(p)); + try std.testing.expectEqual(top, p.view().rects[p.view().active].y); + try std.testing.expect(Trail.reading(p, here)); + try std.testing.expectEqual(@as(u8, 2), here.trail.n); + try std.testing.expect(here.trail.steps[0] == .list and here.trail.steps[1] == .place); + } +} + +test "a picker from a held pane previews in an untouched +New (UX J1): Esc gives it back empty, Enter makes it the held pane's reader by its number; C-v lands in an empty column rather than making one" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 140, .rows = 50 }); + defer p.deinit(); + p.settings.jumps = .walk; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + const b_path = try testFile(p, &tmp, "beta.zig", "one\ntwo\nthree\n"); + defer p.gpa.free(b_path); + const rows = try std.fmt.allocPrint(p.gpa, "{s}:3:1 three\n", .{b_path}); + defer p.gpa.free(rows); + _ = try p.setTestFile("held\n"); + const held = p.view().active; + Trail.keep(p.panes[held].?); + exec.runBuiltin(p, .New, held, "New", null); + p.sync(); + const scratch = p.view().active; + const serial = p.panes[scratch].?.serial; + p.view().active = held; + const npanes = paneCount(p); + open(p, held, .list, "Refs", "", rows); + settle(p); + p.update(.tick); + try std.testing.expect(!live(p).?.made); + try std.testing.expectEqual(serial, live(p).?.landing); + try std.testing.expectEqualStrings(b_path, p.panes[scratch].?.file.?.path); + p.update(.{ .key = .{ .cp = Key.escape } }); + try std.testing.expectEqual(npanes, paneCount(p)); + try std.testing.expect(Trail.untouched(p, p.panes[scratch].?)); + open(p, held, .list, "Refs", "", rows); + settle(p); + p.update(.{ .key = .{ .cp = Key.enter } }); + p.sync(); + const reader = Trail.readerOf(p, p.panes[held].?).?; + try std.testing.expectEqual(serial, p.panes[reader].?.serial); + try std.testing.expectEqualStrings(b_path, p.panes[reader].?.file.?.path); + try std.testing.expectEqual(p.panes[held].?.serial, p.panes[reader].?.trail.steps[0].owner); + try std.testing.expectEqual(npanes, paneCount(p)); + // C-v with an empty column there: the place goes in it. + Trail.keep(p.panes[reader].?); + const c = layout.insertColumn(p, layout.findPane(p, held).?.col, false).?; + p.sync(); + const ncol = p.view().ncol; + open(p, held, .list, "Refs", "", rows); + settle(p); + p.update(.{ .key = .{ .cp = 'v', .ctrl = true } }); + p.sync(); + try std.testing.expectEqual(ncol, p.view().ncol); + try std.testing.expectEqual(c, layout.findPane(p, p.view().active).?.col); + try std.testing.expectEqualStrings(b_path, p.panes[p.view().active].?.file.?.path); +} diff --git a/core/Trail.zig b/core/Trail.zig index 96d505ae..3003a7df 100644 --- a/core/Trail.zig +++ b/core/Trail.zig @@ -79,6 +79,25 @@ pub fn reading(p: *const Pardes, pane: *const Pane) bool { return p.settings.jumps == .walk and isFile(pane) and !pane.trail.held; } +/// An untouched pane (UX J1): a `+New` never typed into (no undo +/// history), its body empty, still so named, not kept, not folded, and no +/// 9P open holding it: a look or exec open answered with it (exec.leased), +/// or an open of one of its own files (a script's look or exec at it, an +/// event helper's event), so that a script's pane/new stays its own. It is +/// free space: the next pane made takes its place (layout.freeSpace), a +/// jump from it among them. +pub fn untouched(p: *const Pardes, pane: *const Pane) bool { + const f = pane.file orelse return false; + const o = f.output orelse return false; + if (o.from != .cmd or o.from.cmd != .New or f.pager) return false; + if (f.content.len != 0 or f.history.journal.steps.items.len != 0 or pane.trail.held or pane.collapsed) return false; + if (!std.mem.eql(u8, std.Io.Dir.path.basename(f.path), config.scratch_buffer) or @import("exec.zig").leased(p, pane.serial)) return false; + for (p.fs.opens[0..p.fs.opens_len]) |open| if (open.node != 0) if (pardes.ctlfs.Node.target(open.node)) |t| { + if (t == .pane and t.pane.serial == pane.serial) return false; + }; + return true; +} + /// The reading pane that is held pane `owner`'s reader, if it has one. pub fn readerOf(p: *const Pardes, owner: *const Pane) ?usize { for (p.panes, 0..) |slot, id| { @@ -89,12 +108,14 @@ pub fn readerOf(p: *const Pardes, owner: *const Pane) ?usize { return null; } -/// A new reader's trail: its owner, then where it opened. -pub fn reader(owner: u32, path: []const u8, at: Spot) Trail { +/// A new reader's trail: its owner, then where it opened. With no owner, +/// a jump's from an untouched pane, whose place it takes (UX J1): its +/// trail starts at the file. +pub fn reader(owner: ?u32, path: []const u8, at: Spot) Trail { var t: Trail = .{}; - push(&t, .{ .owner = owner }); + if (owner) |o| push(&t, .{ .owner = o }); if (placeOf(path, at.line, at.col)) |s| push(&t, s); - t.cur = t.n - 1; + t.cur = t.n -| 1; return t; } diff --git a/core/exec.zig b/core/exec.zig index 5a5ff725..a56c17f9 100644 --- a/core/exec.zig +++ b/core/exec.zig @@ -802,7 +802,7 @@ pub fn runCommand(p: *Pardes, from: usize, line: []const u8) ?usize { /// Whether an open of a look or exec file holds `serial` from its own write /// (tree.Open.results): its client is reading that pane. -fn leased(p: *const Pardes, serial: u32) bool { +pub fn leased(p: *const Pardes, serial: u32) bool { for (p.fs.opens[0..p.fs.opens_len]) |o| if (o.node != 0 and o.results.wrote) { if (std.mem.indexOfScalar(u32, o.results.list[0..o.results.len], serial) != null) return true; }; diff --git a/core/layout.zig b/core/layout.zig index 4b5b7d2c..89d206b9 100644 --- a/core/layout.zig +++ b/core/layout.zig @@ -11,6 +11,7 @@ const TOPBAR_H = pardes.TOPBAR_H; const COLBAR_H = pardes.COLBAR_H; const MAX_TAG_ROWS = pardes.MAX_TAG_ROWS; const Pane = pardes.Pane; +const Trail = @import("Trail.zig"); pub const Rect = struct { x: u16 = 0, y: u16 = 0, w: u16 = 0, h: u16 = 0 }; const MAX_COLS = pardes.MAX_COLS; const MAX_PANES = pardes.MAX_PANES; @@ -946,9 +947,9 @@ pub fn replacePane(p: *Pardes, old: usize, new: usize) void { } } -/// Put new pane `id` in the layout: every new pane comes through here, as -/// acme's makenewwindow places it, and every one but a command pane takes -/// the keyboard. `from` is the pane that asked, `origin` the text it asked +/// Put new pane `id` in the layout: every new pane comes through here, +/// into free space first (freeSpace), else as acme's makenewwindow places +/// it, and every one but a command pane takes the keyboard. `from` is the pane that asked, `origin` the text it asked /// from: null when a column tag, the workspace tag or 9P asked (acme's /// t->w == nil). pub fn placeNew(p: *Pardes, from: usize, origin: ?usize, id: usize, kind: NewPane) void { @@ -960,24 +961,32 @@ pub fn placeNew(p: *Pardes, from: usize, origin: ?usize, id: usize, kind: NewPan compute(p); const before = v.rects; const before_min = minima(p); - switch (kind) { - // New: the bottom half of the last pane in its own column - // (look.c:921-923, et->col). - .scratch => 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. - .command => acmePlace(p, p.exec_column orelse v.ncol - 1, null, id), - .errors => acmePlace(p, v.ncol - 1, null, id), - // makenewwindow(nil) for 9P's new (acme.c:877), else (t). - .fs => acmePlace(p, acmeColumn(p, from), null, id), - .shell, .doc => acmePlace(p, acmeColumn(p, from), asked, id), + // Free space first (UX J1): an untouched pane is taken, an empty + // column filled, before any pane is split; an untouched pane only where + // its rows hold the new one's tag at that width and two rows, as every + // placement leaves a pane. + const free = if (freeSpace(p, from, kind)) |space| switch (space) { + .pane => |k| holds: { + v.rects[id].w = v.rects[k].w; + break :holds if (v.rects[k].h >= minRows(p, id)) space else null; + }, + .column => space, + } else null; + if (free) |space| switch (space) { + .pane => |k| takePane(p, k, id), + .column => |c| acmePlace(p, c, null, id), + } else switch (kind) { .reader => readerPlace(p, from, id), + // makenewwindow(t) asked from a pane's text. + .shell, .doc => acmePlace(p, columnFor(p, from, kind), asked, id), + .scratch, .fs, .command, .errors => acmePlace(p, columnFor(p, from, kind), null, id), } // No pane is made shorter than its tag and two rows: where the place // chosen has not the room, the column's tallest pane gives it, and // where none can, the pane is refused, and closed at the step's end - // (Pardes.unplaced) once whatever made it is done with it. - if (!placedRoomy(p, id, &before)) if (findPane(p, id)) |f| { + // (Pardes.unplaced) once whatever made it is done with it. Free space + // splits no pane: a pane taken gives its rows whatever they are. + if (free == null and !placedRoomy(p, id, &before)) if (findPane(p, id)) |f| { removePane(p, id); for (v.cols[f.col].panes[0..p.view().cols[f.col].n]) |k| v.rects[k] = before[k]; snapColWeights(p, f.col); @@ -1045,6 +1054,76 @@ pub fn placeNew(p: *Pardes, from: usize, origin: ?usize, id: usize, kind: NewPan if (findPane(p, id)) |f| p.setActiveColumn(f.col); } +/// The column a new pane of `kind` asked from pane `from` goes in. +fn columnFor(p: *Pardes, from: usize, kind: NewPane) usize { + return switch (kind) { + // New: the bottom half of the last pane in its own column + // (look.c:921-923, et->col). + .scratch => p.exec_column orelse columnOf(p, from), + // +Errors, a command's output: the last column (util.c:94-98); a + // command run from a column's tag, that column. + .command => p.exec_column orelse p.view().ncol - 1, + .errors => p.view().ncol - 1, + // makenewwindow(nil) for 9P's new (acme.c:877), else (t). + .fs, .shell, .doc => acmeColumn(p, from), + // Its held pane's (readerPlace). + .reader => columnOf(p, from), + }; +} + +/// Free space (UX J1): an untouched pane (Trail.untouched) or an empty +/// column. +pub const Free = union(enum) { pane: usize, column: usize }; + +/// The free space a new pane of `kind` asked from pane `from` goes into +/// before it splits a pane (UX J1): `from` itself when it is untouched; +/// else the nearest to where it would go, its column first (below `from`, +/// nearest first, then above it; another column from its top), then the +/// columns to its right, nearest first, then those to its left. Asked from +/// a column's tag, that column alone, as acme puts the pane there; from +/// the workspace's, no pane is the asker. None for New and 9P's pane/new, +/// which make it. +pub fn freeSpace(p: *Pardes, from: usize, kind: NewPane) ?Free { + const v = p.view(); + if (kind == .scratch or kind == .fs or v.ncol == 0) return null; + const at = findPane(p, from); + if (at != null and !p.exec_header) if (p.panes[from]) |asker| if (Trail.untouched(p, asker)) return .{ .pane = from }; + const c0 = columnFor(p, from, kind); + for (0..if (p.exec_column != null) 1 else v.ncol) |i| { + const c = if (c0 + i < v.ncol) c0 + i else v.ncol - 1 - i; + const col = v.cols[c].panes[0..v.cols[c].n]; + if (col.len == 0) return .{ .column = c }; + const below = if (at) |f| (if (f.col == c) f.idx + 1 else 0) else 0; + for (0..col.len) |j| { + const k = col[if (j < col.len - below) below + j else col.len - 1 - j]; + const pane = p.panes[k] orelse continue; + if (Trail.untouched(p, pane)) return .{ .pane = k }; + } + } + return null; +} + +/// New pane `id` in untouched pane `k`'s place (UX J1), with its rows and +/// weight; `k` closes at the step's end (Pardes.unplaced), once whatever +/// made `id` is done with it. A file takes its number too, `@pN` and the +/// 9P state its scripts hold, as a walk step's file does (Trail.show): `k` +/// then closes never logged, and the file is a revision on from its text, +/// so that nothing a worker was asked of it lands on this one. Any other +/// pane keeps its own: a shell's spawn and a kind's jobs went to the host +/// by it as the pane was made; a Zerox twin's revision is its text's. +fn takePane(p: *Pardes, k: usize, id: usize) void { + const was = p.panes[k].?; + const now = p.panes[id].?; + replacePane(p, k, id); + p.unplaced.set(k); + if (!Trail.isFile(now) or now.file.?.twin != 0) return; + std.mem.swap(u32, &was.serial, &now.serial); + std.mem.swap(@TypeOf(was.fs), &was.fs, &now.fs); + now.file.?.revision = was.file.?.revision +% 1; + now.file.?.saved_revision = now.file.?.revision; + pardes.ctlfs.events.noteLog(p, .rename, now); +} + /// acme's makenewwindow column (util.c:456-467): the column a command came /// from (its tag's), else the active column, else the keyboard's pane's, /// else the asking pane's. @@ -2301,3 +2380,199 @@ test "a new pane goes below the rows a pane's text takes, a wrapped line's each try std.testing.expect(p.view().active != a); try std.testing.expect(p.view().rects[a].h -| pane.tag_rows >= used); } + +/// UX J1's sessions: a directory of files of 100 lines, a.zig to g.zig, +/// the session's first pane `/test.txt`, jumps walking. +const FreeFixture = struct { + p: *Pardes, + tmp: std.testing.TmpDir, + dir_buf: [std.Io.Dir.max_path_bytes]u8 = undefined, + dir: []const u8 = "", + + fn init(f: *FreeFixture, cols: u16, rows: u16) !void { + f.tmp = std.testing.tmpDir(.{}); + errdefer f.tmp.cleanup(); + var text: [100 * 9]u8 = undefined; + var w: std.Io.Writer = .fixed(&text); + for (1..101) |n| try w.print("line {d}\n", .{n}); + for ("abcdefg") |name| try f.tmp.dir.writeFile(std.testing.io, .{ .sub_path = &.{ name, '.', 'z', 'i', 'g' }, .data = w.buffered() }); + f.dir = f.dir_buf[0..try f.tmp.dir.realPath(std.testing.io, &f.dir_buf)]; + f.p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = cols, .rows = rows }); + errdefer f.p.deinit(); + _ = try f.p.setTestFile("first\n"); + f.p.settings.jumps = .walk; + } + + fn deinit(f: *FreeFixture) void { + f.p.deinit(); + f.tmp.cleanup(); + } + + /// A pane made at `idx` of column `c` and laid there by hand: a +New + /// (`held` false), or a held file of the directory's. + fn paneAt(f: *FreeFixture, c: usize, idx: usize, held: bool) !usize { + const p = f.p; + const id = p.freeSlot().?; + if (held) { + const pane = try p.newDocPane(id); + pane.file = .{ .path = try std.Io.Dir.path.join(p.gpa, &.{ f.dir, "held.txt" }), .content = try p.gpa.dupe(u8, "held\n"), .history = try panes.File.History.create(p.gpa) }; + Trail.keep(pane); + } else _ = try panes.newScratch(p, 0, id, .scratch); + insert(p, c, idx, id); + p.sync(); + return id; + } + + /// A Look from pane `id` at `name`.zig:`line`; the pane it landed in. + fn look(f: *FreeFixture, id: usize, name: u8, line: usize) usize { + var buf: [std.Io.Dir.max_path_bytes + 16]u8 = undefined; + pardes.look.lookAt(f.p, id, std.fmt.bufPrint(&buf, "{s}/{c}.zig:{d}", .{ f.dir, name, line }) catch unreachable); + f.p.sync(); + return f.p.view().active; + } + + fn count(f: *const FreeFixture) usize { + var n: usize = 0; + for (f.p.panes) |slot| n += @intFromBool(slot != null); + return n; + } +}; + +test "free space first (UX J1): a held pane's reader takes the untouched +New nearest it, in its column below then above, then the columns to its right, then to its left, each keeping its number and rows; with none it splits a pane as before; an empty column is taken whole before a split" { + var f: FreeFixture = .{ .p = undefined, .tmp = undefined }; + try f.init(150, 45); + defer f.deinit(); + const p = f.p; + _ = insertColumn(p, 0, false).?; + _ = insertColumn(p, 1, false).?; + // Column 0: the first pane, a +New; column 1: a +New, the held pane, + // a +New; column 2: a +New. + const left = try f.paneAt(0, 1, false); + const above = try f.paneAt(1, 0, false); + const held = try f.paneAt(1, 1, true); + const below = try f.paneAt(1, 2, false); + const right = try f.paneAt(2, 0, false); + const n = f.count(); + for ([_]usize{ below, above, right, left }, 0..) |want, i| { + const serial = p.panes[want].?.serial; + const rect = p.view().rects[want]; + const reader = f.look(held, "abcd"[i], 10); + // In its place, its number, its rows; the +New gone. + try std.testing.expect(p.panes[want] == null); + try std.testing.expectEqual(serial, p.panes[reader].?.serial); + try std.testing.expectEqual(rect.y, p.view().rects[reader].y); + try std.testing.expectEqual(rect.x, p.view().rects[reader].x); + try std.testing.expectEqual(n, f.count()); + try std.testing.expectEqual(p.panes[held].?.serial, p.panes[reader].?.trail.steps[0].owner); + try std.testing.expectEqual("abcd"[i], std.Io.Dir.path.basename(p.panes[reader].?.file.?.path)[0]); + // Kept: the next jump makes a new reader. + Trail.keep(p.panes[reader].?); + } + // No free space: placed as before (readerPlace), the held pane too + // short to halve: at the bottom of the next column, splitting it. + const split = f.look(held, 'e', 20); + try std.testing.expectEqual(n + 1, f.count()); + try std.testing.expectEqual(@as(usize, 2), findPane(p, split).?.col); + try std.testing.expectEqual(@as(usize, 2), p.view().cols[2].n); + // An empty column, right of them all: the next reader takes it whole, + // the held pane keeping its rows. + Trail.keep(p.panes[split].?); + const c = insertColumn(p, 2, false).?; + p.sync(); + const kept = p.view().rects[held]; + const whole = f.look(held, 'f', 30); + try std.testing.expectEqual(c, findPane(p, whole).?.col); + try std.testing.expectEqual(@as(usize, 1), p.view().cols[c].n); + try std.testing.expectEqual(kept, p.view().rects[held]); +} + +test "free space first (UX J1): a jump from an untouched +New reads in it, the same pane by its number, its trail starting at the file; under Jumps new-pane it opens in it the same way; a +New typed into and emptied, kept, or held by a 9P open (answered with it, or one of its files open) is never free space" { + var f: FreeFixture = .{ .p = undefined, .tmp = undefined }; + try f.init(120, 40); + defer f.deinit(); + const p = f.p; + // Walking: the +New is the file now, as a reading pane's step makes it. + const scratch = try f.paneAt(0, 1, false); + const serial = p.panes[scratch].?.serial; + const n = f.count(); + const into = f.look(scratch, 'a', 12); + try std.testing.expectEqual(serial, p.panes[into].?.serial); + try std.testing.expectEqual(n, f.count()); + try std.testing.expect(Trail.reading(p, p.panes[into].?)); + try std.testing.expectEqual(@as(u8, 1), p.panes[into].?.trail.n); + try std.testing.expectEqual(@as(u32, 12), p.panes[into].?.trail.steps[0].place.line); + // Under Jumps new-pane: the same, placement not a walk. + p.settings.jumps = .@"new-pane"; + const other = try f.paneAt(0, 2, false); + const other_serial = p.panes[other].?.serial; + pardes.exec.runBuiltin(p, .Keep, into, "Keep", null); + const opened = f.look(other, 'b', 3); + try std.testing.expectEqual(other_serial, p.panes[opened].?.serial); + try std.testing.expectEqual(n + 1, f.count()); + try std.testing.expectEqualStrings("b.zig", std.Io.Dir.path.basename(p.panes[opened].?.file.?.path)); + p.settings.jumps = .walk; + // Never free space: typed into and emptied again, kept, leased. + const typed = try f.paneAt(0, 3, false); + p.view().active = typed; + for ("ix") |ch| p.update(.{ .key = .{ .cp = ch, .text = &[_]u8{ch} } }); + p.update(.{ .key = .{ .cp = pardes.Key.escape } }); + p.update(.{ .key = .{ .cp = 'u', .text = "u" } }); + try std.testing.expectEqual(@as(usize, 0), p.panes[typed].?.file.?.content.len); + const kept = try f.paneAt(0, 4, false); + Trail.keep(p.panes[kept].?); + const leased = try f.paneAt(0, 5, false); + p.fs.opens[0] = .{ .node = 1 }; + p.fs.opens[0].results.list[0] = p.panes[leased].?.serial; + p.fs.opens[0].results.len = 1; + p.fs.opens[0].results.wrote = true; + // ...and one whose own file a 9P client holds open (a script's look + // at it): a script's pane/new stays its own. + const held_open = try f.paneAt(0, 6, false); + p.fs.opens[1] = .{ .node = pardes.ctlfs.Node.of(p.panes[held_open].?.serial, .look) }; + p.fs.opens_len = 2; + defer p.fs.opens[0] = .{}; + defer p.fs.opens[1] = .{}; + for ([_]usize{ typed, kept, leased, held_open }) |k| try std.testing.expect(!Trail.untouched(p, p.panes[k].?)); + try std.testing.expect(freeSpace(p, 0, .doc) == null); + try std.testing.expect(freeSpace(p, 0, .reader) == null); +} + +test "free space first (UX J1): a command's output and a Term take an untouched +New's place, the asking one first, each by a number of its own; New and 9P's pane/new never do" { + if (pardes.Term.ops == null) return error.SkipZigTest; // a command pane's terminal + var f: FreeFixture = .{ .p = undefined, .tmp = undefined }; + try f.init(120, 40); + defer f.deinit(); + const p = f.p; + _ = insertColumn(p, 0, false).?; + // A +New in the last column, where a command's output goes. + const last = try f.paneAt(1, 0, false); + const rect = p.view().rects[last]; + const out = pardes.exec.runCommand(p, 0, "true").?; + p.sync(); + try std.testing.expect(p.panes[last] == null); + try std.testing.expect(p.panes[out].?.command != null); + try std.testing.expectEqual(rect, p.view().rects[out]); + // A Term run from an untouched +New is in its place. + const asker = try f.paneAt(0, 1, false); + const at = p.view().rects[asker]; + const n = f.count(); + pardes.exec.runBuiltin(p, .Term, asker, "Term", null); + p.sync(); + try std.testing.expectEqual(n, f.count()); + try std.testing.expect(p.panes[asker] == null); + try std.testing.expect(p.panes[p.view().active].?.isTerminal()); + try std.testing.expectEqual(at, p.view().rects[p.view().active]); + // New makes free space, and 9P's pane/new: neither takes it. + const before = f.count(); + pardes.exec.runBuiltin(p, .New, 0, "New", null); + p.sync(); + const scratch = p.view().active; + try std.testing.expect(Trail.untouched(p, p.panes[scratch].?)); + pardes.exec.runBuiltin(p, .New, scratch, "New", null); + p.sync(); + try std.testing.expectEqual(before + 2, f.count()); + panes.newScratchFor(p, scratch, .fs); + p.sync(); + try std.testing.expectEqual(before + 3, f.count()); + try std.testing.expect(p.panes[scratch] != null); +} diff --git a/core/look.zig b/core/look.zig index f9dcfbc3..41c59cf3 100644 --- a/core/look.zig +++ b/core/look.zig @@ -433,12 +433,15 @@ pub fn runSearch(p: *Pardes, id: usize, pat_raw: []const u8, start: SearchStart, // a builtin's argument comes trimmed already. const pat = std.mem.trimEnd(u8, pat_raw, "\r\n"); if (std.mem.trim(u8, pat, " \t").len == 0) return 0; - const arena = p.scratch.allocator(); // where the pane lives: a file's directory, a shell's cwd — the // directory the results buffer is named in. From a column's or the // workspace's tag, the session's. const dir = if (p.exec_header) p.sessionDir() else Pardes.paneDir(pane); - const out = try @import("memory.zig").unfilled(arena, search_text_output_bytes); + // The gpa's, not the step's arena: 9P requests come between steps, + // and a word looked at twice in one burst of them asked that arena for + // a node of 75 MB (core/fuzz.zig's 9P seed 610). + const out = try @import("memory.zig").unfilled(p.gpa, search_text_output_bytes); + defer p.gpa.free(out); var out_len: usize = 0; var nrows: usize = 0; var anchor: ?usize = null; @@ -1514,7 +1517,8 @@ pub const Landed = union(enum) { none, shown, opened: usize }; /// SPEC §6.1). With `Jumps walk`: a held pane showing the file is focused /// and nothing replaced; the asking pane showing it moves in it (a step /// when it is reading); a reading pane steps in place, a held one steps -/// its reader (Trail.zig), made under it on its first jump. With +/// its reader (Trail.zig), made on its first jump in free space or under +/// it (layout.placeNew), an untouched pane's in its place. With /// `Jumps new-pane` as before the walk: a pane showing the file focused, /// else a new one. pub fn open(p: *Pardes, from: usize, path: []const u8, at: Spot, how: How) Landed { @@ -1568,8 +1572,12 @@ fn openNew(p: *Pardes, from: usize, path: []const u8, at: Spot, kind: layout.New }; nt.body.cur_col = columnOn(nt, nt.body.cur_row, at.col); // A reader's walk (its owner's word, its place) is in its tag before - // it is placed: the rows it is given are for the tag it shows. - if (kind == .reader) nt.trail = Trail.reader(p.panes[from].?.serial, path, at); + // it is placed: the rows it is given are for the tag it shows. From an + // untouched pane, whose place it takes (UX J1), its place alone. + if (kind == .reader) { + const asker = p.panes[from].?; + nt.trail = Trail.reader(if (Trail.untouched(p, asker)) null else asker.serial, path, at); + } const failures = p.fs.failures; layout.placeNew(p, from, from, free, kind); // Refused a place: that is the look's one failure, not a miss of its |
