From 5c938585963818743139439bfc5db6a15c90e10b Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 24 Nov 2026 23:20:24 -0300 Subject: Messages, body_layer, edit, mouse, panes: a click lands on what is drawn under it while a message's detail is unrolled under the caret's line, and the detail's rows are read-only text for the mouse (walk UI G item 2d, the user's "everything a pane shows is regular text"): C's detail rows (body_layer.zig messageGhosts) pushed the rows under the caret's line down in the drawing only, so the mouse, which maps a body row through the row map (Pane.wrapAt), landed a click below them that many rows off (C's report: 'to fix'), and a click on a detail row landed on a line under it; THE FIX: where the detail goes is the step's, as the notice bands are: Messages.detailUnder (the first message naming the caret's line, its lines that fit under that line's last row, in the pane with the keyboard, its body) is made in Messages.collectNotices (the top of update, presented, sync, place) into Pane.ghost_at and ghost_rows (derived: root.zig's kept_out says so), messageGhosts draws from them (it no longer finds the detail itself), and the mouse maps a drawn body row to the row map with Pane.textRow (edit.pointerTextSelection, both the grid's and a body hit's; mouse.dragUpdate's sweep end): a row under the detail is ghost_rows higher, a row of the detail is the line it explains (B1 there puts the caret on that line, the detail kept); B2 and B3 on a word of the detail Exec and Look it as a typed word would be (mouse.zig, body_layer.wordAt reads the word as it is drawn: the body layer's row, else the grid's); a step reads only what its own state makes, so every replica's click lands alike; cost: one more check a pane at collectNotices (none without a live message, the detail only in the pane with the keyboard), nothing a frame; TEST FIRST: body_layer.zig 'a click under a message's detail lands on the row drawn there, a click on the detail on the line it explains, and B2 on a word of the detail runs it' (on the parent: expected 4, found 6, the click on line 5 as drawn landing on line 7's row); NOT DONE: B1's sweep cannot select the detail's text itself (it is no buffer's; the band has the same gap, A's report), a hover underlines the word of the line under the detail row; tui core-test 1036/1205 (169 skipped), process-test 0 of 146 failed Co-Authored-By: Claude Opus 5.5 --- core/Messages.zig | 28 +++++++++++++++ core/body_layer.zig | 100 ++++++++++++++++++++++++++++++++++++++++++++-------- core/edit.zig | 14 ++++++-- core/mouse.zig | 11 ++++-- core/panes.zig | 16 ++++++++- core/root.zig | 2 ++ 6 files changed, 151 insertions(+), 20 deletions(-) (limited to 'core') diff --git a/core/Messages.zig b/core/Messages.zig index c9166aed..e995659a 100644 --- a/core/Messages.zig +++ b/core/Messages.zig @@ -920,6 +920,11 @@ pub fn reportFailure(p: *Pardes, id: usize, text: []const u8) void { /// them instead of letting the last one win the row. pub fn collectNotices(p: *Pardes, pane: *Pane, r: Rect) void { pane.notices = bands(p, pane, pane.tag_rows, r); + // A message's detail under the caret's line, where the body draws it + // and a click's row is read (Pane.textRow): the step's, as the bands. + const under = if (r.h > pane.tag_rows) detailUnder(p, pane, p.panes[p.view().active] == pane, r.h - pane.tag_rows) else null; + pane.ghost_at = if (under) |d| d.row else 0; + pane.ghost_rows = if (under) |d| d.rows else 0; // Each band's chip at the right edge, as wide as its own text and a // blank cell either side, the pane's width at most: a click finds it // whether or not a frame has drawn it. An overlay over the body's top @@ -934,6 +939,29 @@ pub fn collectNotices(p: *Pardes, pane: *Pane, r: Rect) void { } } +/// A message's detail under the caret's line (SPEC §10.2), as a body of +/// `body_h` rows shows it: the row of that line's last row, how many of the +/// detail's lines fit under it, the message (the first naming the line, as +/// the words after it are); only in the pane with the keyboard, in its body. +/// Bounded by the live messages' places and the rows shown. +pub fn detailUnder(p: *const Pardes, pane: *Pane, active: bool, body_h: u16) ?struct { row: u16, rows: u16, said: *const Said } { + if (!active or pane.focus != .body or p.messages.live_len == 0) return null; + const f = pane.file orelse return null; + const caret = @max(0, pane.body.cur_row); + const said = found: for (p.messages.live[0..p.messages.live_len]) |*s| { + for (s.places[0..s.places_len]) |*place| if (place.line == caret + 1 and std.mem.eql(u8, place.path.slice(), f.path)) break :found s; + } else return null; + if (said.detail_len == 0) return null; + var row: u16 = 0; + while (row < body_h) : (row += 1) { + if (pane.wrapAt(p.settings.tab, row).line != caret) continue; + if (row + 1 < body_h and pane.wrapAt(p.settings.tab, row + 1).line == caret) continue; + const k: u16 = @intCast(@min(said.detail_len, body_h - row - 1)); + return if (k == 0) null else .{ .row = row, .rows = k, .said = said }; + } + return null; +} + /// The bands a pane shows over `r` below its `tag_rows`: the live view's /// each step, and any view's for its kind's facts (Kind.viewOf), made from /// what a dump keeps (the bands are derived). diff --git a/core/body_layer.zig b/core/body_layer.zig index c2a20213..ef3401ed 100644 --- a/core/body_layer.zig +++ b/core/body_layer.zig @@ -468,7 +468,7 @@ pub fn renderBody(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane } } - if (pane.file != null and p.messages.live_len > 0) messageGhosts(p, s, pane, tx, tw, body_y, body_h, active); + if (pane.file != null and p.messages.live_len > 0) messageGhosts(p, s, pane, tx, tw, body_y, body_h); // `gw`'s labels, two letters over the start of each word if (pane.jump.items.len > 0) labels: { @@ -553,10 +553,12 @@ pub fn renderBody(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane /// A message at the lines it names, drawn and never written (SPEC §10.2, /// ruling R3): after the end of each such line shown, the message's glyph /// and its words there; under the caret's line, when the keyboard is in -/// this body, the lines that explain it, pushing the rows below down (the -/// rows past the body's foot are not shown meanwhile). Bounded by the rows -/// shown and the live messages' places (Messages.max_live, Said.max_places). -fn messageGhosts(p: *Pardes, s: *Surface, pane: *Pane, tx: u16, tw: u16, body_y: u16, body_h: u16, active: bool) void { +/// this body, the lines that explain it where the step put them +/// (Pane.ghost_at, ghost_rows: Messages.detailUnder), pushing the rows +/// below down (the rows past the body's foot are not shown meanwhile). +/// Bounded by the rows shown and the live messages' places +/// (Messages.max_live, Said.max_places). +fn messageGhosts(p: *Pardes, s: *Surface, pane: *Pane, tx: u16, tw: u16, body_y: u16, body_h: u16) void { const Messages = pardes.Messages; const f = pane.file.?; const chrome = p.chromeTheme(); @@ -571,8 +573,7 @@ fn messageGhosts(p: *Pardes, s: *Surface, pane: *Pane, tx: u16, tw: u16, body_y: n += 1; }; if (n == 0) return; - const caret_line: usize = @intCast(@max(0, pane.body.cur_row)); - var detail: ?struct { row: u16, said: *const Messages.Said } = null; + var detail: ?*const Messages.Said = null; var row: u16 = 0; while (row < body_h) : (row += 1) { const w = pane.wrapAt(p.settings.tab, row); @@ -592,26 +593,46 @@ fn messageGhosts(p: *Pardes, s: *Surface, pane: *Pane, tx: u16, tw: u16, body_y: const words = std.fmt.bufPrint(&buf, "{s} {s}", .{ hit.said.kind.glyph(), hit.place.text.slice() }) catch &buf; _ = s.print(tx + @as(u16, @intCast(at)), body_y + row, @intCast(tw - at), words, .{ .fg = .{ .rgb = Messages.ink(chrome, hit.said.kind) }, .bg = pane_bg }); } - if (active and pane.focus == .body and line == caret_line and hit.said.detail_len > 0 and detail == null) detail = .{ .row = row, .said = hit.said }; + if (pane.ghost_rows > 0 and row == pane.ghost_at) detail = hit.said; } - const under = detail orelse return; + const said = detail orelse return; + const under_row = pane.ghost_at; // The rows below the caret's line move down by the detail's rows. - const k: u16 = @intCast(@min(under.said.detail_len, body_h - under.row - 1)); + const k: u16 = @min(pane.ghost_rows, body_h -| (under_row + 1)); if (k == 0) return; var dst: u16 = body_h - 1; - while (dst >= under.row + 1 + k) : (dst -= 1) { + while (dst >= under_row + 1 + k) : (dst -= 1) { const to = @as(usize, body_y + dst) * s.cols + tx; const from = @as(usize, body_y + dst - k) * s.cols + tx; @memcpy(s.cells[to..][0..tw], s.cells[from..][0..tw]); } const gutter: u16 = panes.File.gutterWidth(pane); - for (under.said.detail[0..k], 0..) |*text, i| { - const y = body_y + under.row + 1 + @as(u16, @intCast(i)); + for (said.detail[0..k], 0..) |*text, i| { + const y = body_y + under_row + 1 + @as(u16, @intCast(i)); s.fill(tx, y, tw, 1, .{ .bg = pane_bg }); - if (gutter + 2 < tw) _ = s.print(tx + gutter + 2, y, tw - gutter - 2, text.slice(), .{ .fg = .{ .rgb = Messages.ink(chrome, under.said.kind) }, .bg = pane_bg }); + if (gutter + 2 < tw) _ = s.print(tx + gutter + 2, y, tw - gutter - 2, text.slice(), .{ .fg = .{ .rgb = Messages.ink(chrome, said.kind) }, .bg = pane_bg }); } } +/// The word drawn at body row `row`, text column `col` of pane `id` in the +/// frame drawn last (its body layer when it has one, else the grid): a +/// message's detail is text no buffer holds, read as it is shown. +pub fn wordAt(p: *Pardes, arena: std.mem.Allocator, id: usize, row: usize, col: usize) !?[]const u8 { + const layer = &p.surface.body_layers[id]; + const body = (p.surface.region(.body, id) orelse return null).rect; + var text: std.ArrayList(u8) = .empty; + if (layer.rows > 0) { + if (row >= layer.rows) return null; + for (layer.cells[row * layer.cols ..][0..layer.cols]) |cell| try text.appendSlice(arena, cell.grapheme()); + } else { + if (row >= body.h) return null; + for (0..body.w) |x| try text.appendSlice(arena, p.surface.at(body.x + @as(u16, @intCast(x)), body.y + @as(u16, @intCast(row))).grapheme()); + } + const b = pardes.tagline.wordBoundsAtCol(text.items, panes.File.rawAtDisplay(p.settings.tab, text.items, col)) orelse return null; + if (b.lo == b.hi) return null; + return text.items[b.lo..b.hi]; +} + pub fn bodyText(tab: u16, arena: std.mem.Allocator, pane: *Pane) ![]const u8 { if (pane.addon != null) { const buf = try arena.alloc(u8, pane.rows -| 1); @@ -776,3 +797,54 @@ test "a build error is said where its cause is: the mark on its file's tag, its const foot = s.region(.notice_row, pardes.Region.none).?; try std.testing.expect(std.mem.endsWith(u8, std.mem.trimEnd(u8, rowText(s, 0, foot.rect.y, &buf), " "), "SPC e walk")); } + +test "a click under a message's detail lands on the row drawn there, a click on the detail on the line it explains, and B2 on a word of the detail runs it" { + const p = try Pardes.init(std.testing.allocator, .{ .cols = 80, .rows = 20 }); + defer p.deinit(); + const pane = try p.setTestFile("one\ntwo\nthree\nfour\nfive\nsix\n"); + const id = p.view().active; + const err = pardes.Messages.keep(p, .@"error", 0, "@p9 zig build", "exit 1"); + err.places[0] = .{ .line = 3, .col = 1 }; + err.places[0].path.set("/test.txt"); + err.places[0].text.set("bad"); + err.places_len = 1; + err.detail[0] = .{}; + err.detail[0].set("try Wrap here"); + err.detail[1] = .{}; + err.detail[1].set(" ^"); + err.detail_len = 2; + pane.focus = .body; + pane.body.cur_row = 2; + p.sync(); + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + var s = try p.render(frame.allocator()); + const body = s.region(.body, id).?.rect; + var buf: [512]u8 = undefined; + // Line 5 drawn two rows lower than its place in the row map, under the + // detail: a click on it lands on it. + const five = std.mem.indexOf(u8, rowText(s, body.x, body.y + 6, &buf), "five").?; + const x = body.x + @as(u16, @intCast(five)) + 1; + p.update(.{ .mouse = .{ .button = .left, .kind = .press, .col = x, .row = body.y + 6 } }); + p.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = x, .row = body.y + 6 } }); + try std.testing.expectEqual(@as(i32, 4), pane.body.cur_row); + // On line 3 again, its detail drawn: a click on the detail is on the + // line it explains, and the detail stays. + pane.body.cur_row = 2; + _ = frame.reset(.retain_capacity); + s = try p.render(frame.allocator()); + const wrap_at = std.mem.indexOf(u8, rowText(s, body.x, body.y + 3, &buf), "Wrap").?; + const wx = body.x + @as(u16, @intCast(wrap_at)) + 1; + p.update(.{ .mouse = .{ .button = .left, .kind = .press, .col = wx, .row = body.y + 4 } }); + p.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = wx, .row = body.y + 4 } }); + try std.testing.expectEqual(@as(i32, 2), pane.body.cur_row); + // B2 on a word of it: the word runs, as a typed one would. + _ = frame.reset(.retain_capacity); + s = try p.render(frame.allocator()); + const wrapped = p.settings.wrap; + p.update(.{ .mouse = .{ .button = .middle, .kind = .press, .col = wx, .row = body.y + 3 } }); + p.update(.{ .mouse = .{ .button = .middle, .kind = .release, .col = wx, .row = body.y + 3 } }); + while (p.nextEffect()) |_| {} + try std.testing.expect(p.settings.wrap != wrapped); + try std.testing.expectEqualStrings("one\ntwo\nthree\nfour\nfive\nsix\n", pane.file.?.content); +} diff --git a/core/edit.zig b/core/edit.zig index ac78fd32..ce13b5ed 100644 --- a/core/edit.zig +++ b/core/edit.zig @@ -2133,6 +2133,9 @@ fn stepOwn(e: Editor, t: *Text, back: bool) error{OutOfMemory}!void { const PointerTextSelection = struct { sel: Pane.Sel, on_tag: bool, + /// On a row of a message's detail (Pane.textRow): that body row as + /// drawn; the selection is on the line it explains. + ghost: ?u16 = null, }; pub fn pointerTextSelection(p: *const Pardes, id: usize, col: u16, row: u16, body_hit: ?Mouse.BodyHit, tag_hit: ?TagHit) ?PointerTextSelection { @@ -2151,16 +2154,21 @@ pub fn pointerTextSelection(p: *const Pardes, id: usize, col: u16, row: u16, bod if (!on_tag) if (body_hit) |supplied| { const mapped_hit = mouse.bodyHitForPane(p, id, supplied, false) orelse return null; const c = @as(i32, mapped_hit.col) - config.GUTTER; - const v = @as(i32, mapped_hit.row) + pane.tag_rows; - return .{ .sel = .{ .state = .dragging, .c0 = c, .c1 = c, .r0 = v, .r1 = v }, .on_tag = false }; + const drawn: i32 = mapped_hit.row; + const v = pane.textRow(drawn) + pane.tag_rows; + const ghost = pane.ghost_rows > 0 and drawn > pane.ghost_at and drawn <= pane.ghost_at + pane.ghost_rows; + return .{ .sel = .{ .state = .dragging, .c0 = c, .c1 = c, .r0 = v, .r1 = v }, .on_tag = false, .ghost = if (ghost) mapped_hit.row else null }; }; const tag_col = if (on_tag) Layer.columnAt(p, .pane, id, tag_hit, false, row - tag_y) else null; const c: i32 = (if (tag_col) |value| @as(i32, value) else @as(i32, col) - @as(i32, r.x + (if (on_tag) TAG_TEXT_INSET else config.GUTTER))) + (if (on_tag) @as(i32, pane.tag_scroll) else 0); - const v: i32 = if (on_tag) @as(i32, row) - @as(i32, tag_y) else @as(i32, row) - @as(i32, body_y) + @as(i32, pane.tag_rows); + const drawn: i32 = @as(i32, row) - @as(i32, body_y); + const v: i32 = if (on_tag) @as(i32, row) - @as(i32, tag_y) else pane.textRow(drawn) + @as(i32, pane.tag_rows); + const ghost = !on_tag and pane.ghost_rows > 0 and drawn > pane.ghost_at and drawn <= pane.ghost_at + pane.ghost_rows; return .{ .sel = .{ .state = .dragging, .c0 = c, .c1 = c, .r0 = v, .r1 = v }, .on_tag = on_tag, + .ghost = if (ghost) @intCast(drawn) else null, }; } diff --git a/core/mouse.zig b/core/mouse.zig index feb5ac08..9f0cfd8b 100644 --- a/core/mouse.zig +++ b/core/mouse.zig @@ -877,6 +877,13 @@ pub fn handleMouse(p: *Pardes, event: Mouse) void { pane.scrollBy(p.settings.tab, if (m.button == config.look_button) local else -local); } else if (edit.pointerTextSelection(p, id, mcol, mrow, m.body_hit, v.pointer_tag_hit)) |pointed| { if (m.button == config.look_button) v.active = id; + // A word of a message's detail, read as it is drawn: + // run or looked at as a typed one would be. + if (pointed.ghost) |row| { + const arena = p.scratch.allocator(); + const word = (body_layer.wordAt(p, arena, id, row, @intCast(@max(0, pointed.sel.c0))) catch return) orelse return; + return exec.runBuiltin(p, if (m.button == config.exec_button) config.exec_cmd else config.look_cmd, id, "", arena.dupe(u8, word) catch return); + } pane.clearPointerSelection(@backingInt(m.button)); pane.sel[@backingInt(m.button)] = pointed.sel; v.drag = .{ @@ -989,7 +996,7 @@ pub fn dragUpdate(p: *Pardes, mcol: u16, mrow: u16, body_hit: ?Mouse.BodyHit) vo if (supplied.serial == pane.serial) s.body = supplied.place; const mapped_hit = bodyHitForPane(p, s.id, supplied, true) orelse return; pane.sel[b].c1 = @as(i32, mapped_hit.col) - config.GUTTER; - pane.sel[b].r1 = @as(i32, mapped_hit.row) + tag_rows; + pane.sel[b].r1 = pane.textRow(mapped_hit.row) + tag_rows; return; }; // A sweep that began on the tag stays on it, on the tag line @@ -1025,7 +1032,7 @@ pub fn dragUpdate(p: *Pardes, mcol: u16, mrow: u16, body_hit: ?Mouse.BodyHit) vo else if (body_h == 0) 0 else - @as(i32, @min(@max(mrow, body_y), body_y + body_h - 1)) - @as(i32, body_y) + tag_rows; + pane.textRow(@as(i32, @min(@max(mrow, body_y), body_y + body_h - 1)) - @as(i32, body_y)) + tag_rows; if (pane.sel[b].r1 < tag_rows) pane.sel[b].c1 += @as(i32, pane.tag_scroll); } }, diff --git a/core/panes.zig b/core/panes.zig index f9c63e37..f608e6a3 100644 --- a/core/panes.zig +++ b/core/panes.zig @@ -32,7 +32,7 @@ pub const Pane = struct { /// it (views.zig). (The 9P server's part of `fs` says so in its type.) pub const replica = .{ .local = .{ "pv", "pv_known" }, - .derived = .{ "band_rows", "wrap_line", "wrap_col", "wrap_pad", "wrap_n", "body_wrapped", "context_rows", "body_rows", "body_visible_rows", "context_row_limit", "geometry_key", "geometry_len", "top_skip", "notices", "pan" }, + .derived = .{ "band_rows", "ghost_at", "ghost_rows", "wrap_line", "wrap_col", "wrap_pad", "wrap_n", "body_wrapped", "context_rows", "body_rows", "body_visible_rows", "context_row_limit", "geometry_key", "geometry_len", "top_skip", "notices", "pan" }, .len = .{ .{ "question", "question_len" }, .{ "shell_tail", "shell_tail_len" }, .{ "sel_snap", "nsel_snap" } }, // A picture's memory is its own allocator's (a dump loaded makes // it there). @@ -358,6 +358,12 @@ pub const Pane = struct { /// body layer reserves exactly `len` rows for them, the way it already /// reserves rows for sticky context headers. notices: Notices = .{}, + /// A message's detail as the body drawn last has it (body_layer.zig + /// messageGhosts): `ghost_rows` read-only rows after body row + /// `ghost_at` (its line's last), the rows below drawn that many lower. + /// The mouse reads it, so that a click lands on what is drawn under it. + ghost_at: u16 = 0, + ghost_rows: u16 = 0, /// Each other view's share of this pane, while it is not live /// (views.zig): its cursor, scroll and place in that view's layout. pv: [views.max]views.PaneView = undefined, @@ -554,6 +560,14 @@ pub const Pane = struct { return @min(pane.scrolled_to orelse output, output); } + /// Body row `vr` as drawn, as the row map counts it (`wrapAt`): a row + /// under a message's detail is `ghost_rows` higher there; a row of the + /// detail is the line's it explains. + pub fn textRow(pane: *const Pane, vr: i32) i32 { + if (pane.ghost_rows == 0 or vr <= pane.ghost_at) return vr; + return @max(pane.ghost_at, vr - pane.ghost_rows); + } + /// The line and byte a body row shows from. pub fn wrapAt(pane: *Pane, tab: u16, vr: i32) struct { line: i32, at: i32, pad: i32 = 0 } { File.geometry(tab, pane); diff --git a/core/root.zig b/core/root.zig index 61c236ae..b41a1336 100644 --- a/core/root.zig +++ b/core/root.zig @@ -772,6 +772,8 @@ const kept_out = [_]struct { []const u8, []const u8, []const u8 }{ .{ "panes.zig", "pv pv_known", "the views' shares as this process parks them (views.zig)" }, .{ "panes.zig", "wrap_line wrap_col wrap_pad wrap_n body_wrapped context_rows body_rows body_visible_rows context_row_limit geometry_key geometry_len top_skip", "File.geometry's, made by whoever asks first (a frame or a step) from everything its key hashes of the step's state (the tab's width too); top_skip, keyed by the geometry it is of, read only through File.topSkip, which walks one of another key again" }, .{ "panes.zig views.zig", "notices", "made again from the step's state at the top of update and presented, and at sync (Messages.collectNotices); a frame's are its own" }, + .{ "panes.zig", "ghost_at", "where a message's detail is drawn under the caret's line (Messages.detailUnder), made again from the step's state with the notices (Messages.collectNotices: at the top of update, presented, sync, place); a frame's are its own" }, + .{ "panes.zig", "ghost_rows", "the detail's rows there, made again with ghost_at" }, .{ "panes.zig views.zig", "band_rows", "the band's rows under a tag (Band.takes), made again from the view's tag state at every sync before anything reads them (layout.floorRows reads the last sync's, as it reads tag_rows)" }, .{ "panes.zig views.zig", "pan", "written by steps only (sync, a view unparked) from the pane and its terminal" }, .{ "File.zig", "cap line_starts line_cap widest", "the text's line index, of its bytes, and the widest line it found (an upper bound: which walk answers, never what)" }, -- cgit v1.3