summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-25 10:46:46 -0300
committerGabriel Schneider <[email protected]>2026-10-25 11:58:11 -0300
commitb8646ef838e4f33f1f3adbf3c08c251afc39ae73 (patch)
tree09a044c9c9792d37706bfb510b4de369b4f7b150 /src
parentbfdc8a9e61dd20c91b68f62187e724d1a98445a4 (diff)
downloadpardes-b8646ef838e4f33f1f3adbf3c08c251afc39ae73.tar.gz
pardes-b8646ef838e4f33f1f3adbf3c08c251afc39ae73.zip
query: B1 on a query pane is acme's B1: it places the caret and selects, nothing else (no other pane moves, nothing is previewed, the current row stays); only the keyboard moves the current row: Ctrl-n/Ctrl-p typing, n/N in the pane's normal mode (the caret with them, caretTo), j/k taking the caret onto a row (produce follows the caret on a key step only); Enter lands the current row; the current row's line mark is no selection B2/B3 borrow (exec.operandAt), so a click on it takes the word as on any row
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016u3hBPFfja5pK4EBRC1UVD
Diffstat (limited to 'src')
-rw-r--r--src/Query.zig116
-rw-r--r--src/exec.zig4
-rw-r--r--src/mouse.zig2
-rw-r--r--src/pardes.zig2
4 files changed, 94 insertions, 30 deletions
diff --git a/src/Query.zig b/src/Query.zig
index 2ab687ee..a5d33cf4 100644
--- a/src/Query.zig
+++ b/src/Query.zig
@@ -145,7 +145,8 @@ made_rev: u32 = 0,
/// The owner's revision seen last tick: rows follow an edit of the owner
/// once it has stood a frame (the rows refresh on idle frames).
seen_rev: u32 = 0,
-/// The body cursor's row as last seen: B1, j or k on a row makes it current.
+/// The body cursor's row as last seen: j or k onto a row makes it current
+/// (a caret B1 put there is only a caret).
seen_row: i32 = 0,
/// The places a `/`'s rows are a window of: [lo, hi) of its owner's hits.
window: struct { lo: usize = 0, hi: usize = 0 } = .{},
@@ -274,7 +275,7 @@ pub fn open(p: *Pardes, from: usize, source: Source, arg: []const u8, focus: boo
// Every view as the search found it, for Esc to put back.
owner.places.remember(p, owner);
// Before the first key: a names source lists every candidate at once.
- produce(p, id, false);
+ produce(p, id, false, false);
// What a 9P exec reads back: the pane it filled (ninep/ctl.zig).
p.fs.refilled = p.panes[id].?.serial;
if (focus) toLineOne(p, id) else p.active = from;
@@ -397,11 +398,11 @@ fn save(p: *Pardes, rp: *Pane, q: *Query) !Saved {
/// source has something new, has its rows made again. A walk takes a slice
/// on a tick. Nothing here is synchronous with an edit of the owner: its
/// places shift (Places.shift) and the rows follow once it stands a frame.
-pub fn produceAll(p: *Pardes, tick: bool) void {
+pub fn produceAll(p: *Pardes, tick: bool, keyed: bool) void {
for (p.panes, 0..) |slot, id| {
const pane = slot orelse continue;
if (pane.query == null) continue;
- produce(p, id, tick);
+ produce(p, id, tick, keyed);
}
}
@@ -416,7 +417,9 @@ pub fn busy(p: *const Pardes) bool {
return false;
}
-fn produce(p: *Pardes, id: usize, tick: bool) void {
+/// `keyed`: this step was a key, so a caret moved onto a row (j, k) makes
+/// it current; a caret B1 placed is only a caret.
+fn produce(p: *Pardes, id: usize, tick: bool, keyed: bool) void {
const rp = p.panes[id] orelse return;
const q = rp.query orelse return;
const f = if (rp.file) |*file| file else return;
@@ -445,10 +448,12 @@ fn produce(p: *Pardes, id: usize, tick: bool) void {
if (asked == null or asked.?.source != .text) clearPlaces(p, q, rp.serial, owner_id, asked);
}
const a = asked orelse return noteRows(p, id, " line 1 asks: /text, Find, Grep, Recent, Panes, Symbols, All, Refs, Def, Impl or Diagnostics, then what to find\n");
- // A row clicked or stepped onto (B1, j, k): current, and shown.
+ // A row stepped onto (j, k): current, and shown. One B1 put the caret
+ // on is not: B1 places the caret and selects, nothing else, as on any
+ // text; no other pane moves.
if (rp.body.cur_row != q.seen_row) {
q.seen_row = rp.body.cur_row;
- if (rp.body.cur_row > 0 and rp.body.cur_row <= q.items.items.len) {
+ if (keyed and rp.body.cur_row > 0 and rp.body.cur_row <= q.items.items.len) {
const i: usize = @intCast(rp.body.cur_row - 1);
if (q.items.items[i].kind == .hit) setCurrent(p, id, i, true);
}
@@ -1217,8 +1222,7 @@ pub fn fillList(p: *Pardes, owner_id: usize, source: Source, rows: []const u8) v
if (t.mode == .insert) edit.exitInsert(p, t);
t.vsel.active = false;
t.nsel = 0;
- t.cur_row = @intCast((q.current orelse 0) + 1);
- showCurrent(rp);
+ caretTo(rp, q.current orelse 0);
showTarget(p, qid);
}
@@ -1350,25 +1354,12 @@ fn clearPlaces(p: *Pardes, q: *Query, serial: u32, owner_id: ?usize, asked: anyt
/// is current. Scrolled into view, line 1 kept when it can be.
fn showCurrent(rp: *Pane) void {
const q = rp.query orelse return;
- const on_rows = rp.body.cur_row != 0 and rp.focus == .body;
- if (on_rows and rp.body.mode != .normal) return;
+ if (rp.body.cur_row != 0 and rp.focus == .body) return;
const c = q.current orelse {
rp.body.msel.active = false;
return;
};
const r: i32 = @intCast(c + 1);
- if (on_rows) {
- // The keyboard on the rows is the current row: n and N take it there.
- rp.body.msel.active = false;
- rp.body.cur_row = r;
- rp.body.cur_col = 0;
- rp.body.cur_pinned = true;
- q.seen_row = r;
- const off = rp.scroll();
- const shown = off + @as(i32, rp.rows) - 1;
- if (r > shown) rp.scrollBy(r - shown) else if (r < off) rp.scrollBy(r - off);
- return;
- }
rp.body.msel = .{ .active = true, .r0 = r, .r1 = r };
// Typed into, line 1 stays in view whatever the current row: what you
// type is never scrolled away (the live rows start just before the
@@ -1382,6 +1373,19 @@ fn showCurrent(rp: *Pane) void {
if (r > shown) rp.scrollBy(r - shown) else if (r < off) rp.scrollBy(r - off);
}
+/// The caret on item `i`'s row, the row in view: the current row's own
+/// mark from here (the line selection is for the caret on line 1).
+fn caretTo(rp: *Pane, i: usize) void {
+ const r: i32 = @intCast(i + 1);
+ rp.body.msel.active = false;
+ rp.body.cur_row = r;
+ rp.body.cur_col = 0;
+ rp.body.cur_pinned = true;
+ rp.query.?.seen_row = r;
+ const off = rp.scroll();
+ if (r >= off + @as(i32, rp.rows)) rp.scrollBy(r - off - @as(i32, rp.rows) + 1) else if (r < off) rp.scrollBy(r - off);
+}
+
/// The line (0-based) of byte `at` of `f`'s text.
fn lineOfOffset(p: *Pardes, f: *File.State, at: usize) usize {
const index = File.lineIndex(p.gpa, f) catch return 0;
@@ -1848,6 +1852,11 @@ pub fn key(p: *Pardes, id: usize, k: Key) bool {
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') {
step(p, id, if (k.cp == 'n') 1 else -1);
+ // The caret on the rows goes to the current row, as j and k
+ // would have taken it there.
+ if (t.cur_row != 0) if (q.current) |c| {
+ caretTo(rp, c);
+ };
return true;
}
// A language server's list, live: Esc is back to the text it was
@@ -1856,10 +1865,10 @@ pub fn key(p: *Pardes, id: usize, k: Key) bool {
cancel(p, id);
return true;
}
- if (hit(k, config.look_key) and t.cur_row > 0) {
- 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);
+ // Enter lands the current row: the caret's when j or k took it
+ // there, never one a B1 only put the caret on.
+ if (hit(k, config.look_key) and t.cur_row > 0 and q.current != null) {
+ land(p, id, null);
return true;
}
return false;
@@ -2428,3 +2437,56 @@ test "a language server's list: one answer lands with no pane, several open the
try std.testing.expectEqual(@as(usize, 0), p.active);
try std.testing.expectEqual(@as(i32, 0), pane.body.cur_row);
}
+
+test "B1 on a query pane's rows places the caret and selects, nothing else; j, k and n make a row current, Enter lands the current" {
+ const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 40 });
+ defer p.deinit();
+ const pane = try p.setTestFile("alpha one\nbeta\nalphabet\ngamma alpha\nalpha two\ndelta\n");
+ typeKeys(p, "/alpha");
+ const qid = queryPane(p).?;
+ const qp = p.panes[qid].?;
+ const q = qp.query.?;
+ p.acknowledgePanelPresentation(&.{});
+ const owner_sel = pane.body.primaryRange(pane.file.?.content, 0);
+ const owner_scroll = pane.file.?.scroll;
+ const current = q.current;
+ const place = pane.places.current;
+ const r = p.rects[qid];
+ const y = r.y + qp.tag_rows + 3;
+ const x = r.x + config.GUTTER + File.gutterWidth(qp) + 6;
+ // a click: the caret there, the current row and every other pane as they were
+ p.update(.{ .mouse = .{ .button = .left, .kind = .press, .col = x, .row = y } });
+ p.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = x, .row = y } });
+ try std.testing.expectEqual(@as(i32, 3), qp.body.cur_row);
+ try std.testing.expectEqual(current, q.current);
+ try std.testing.expectEqual(place, pane.places.current);
+ try std.testing.expectEqual(owner_sel, pane.body.primaryRange(pane.file.?.content, 0));
+ try std.testing.expectEqual(owner_scroll, pane.file.?.scroll);
+ try std.testing.expect(p.panes[qid] != null and queryPane2(p, qid) == null);
+ // a sweep selects the text it went over, and nothing else
+ p.update(.{ .mouse = .{ .button = .left, .kind = .press, .col = x, .row = y } });
+ p.update(.{ .mouse = .{ .button = .left, .kind = .drag, .col = x + 4, .row = y } });
+ p.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = x + 4, .row = y } });
+ const swept = qp.sel[@backingInt(config.select_button)];
+ try std.testing.expect(swept.state == .done and swept.r0 == swept.r1 and swept.c1 - swept.c0 == 4);
+ try std.testing.expectEqual(current, q.current);
+ try std.testing.expectEqual(owner_sel, pane.body.primaryRange(pane.file.?.content, 0));
+ // the keyboard: j onto the next row makes it current, the text follows
+ p.active = qid;
+ if (qp.body.mode == .insert) p.update(.{ .key = .{ .cp = Key.escape } });
+ try std.testing.expect(p.panes[qid] != null);
+ typeKeys(p, "j");
+ try std.testing.expectEqual(@as(i32, 4), qp.body.cur_row);
+ try std.testing.expectEqual(@as(?usize, 3), pane.places.current);
+ // n round the end: the caret goes with the current row
+ typeKeys(p, "n");
+ try std.testing.expectEqual(@as(?usize, 0), pane.places.current);
+ try std.testing.expectEqual(@as(i32, 1), qp.body.cur_row);
+ // B1 elsewhere, then Enter: the current row lands, not the caret's
+ p.update(.{ .mouse = .{ .button = .left, .kind = .press, .col = x, .row = y } });
+ p.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = x, .row = y } });
+ p.active = qid;
+ p.update(.{ .key = .{ .cp = Key.enter } });
+ try std.testing.expectEqual(@as(usize, 0), p.active);
+ try std.testing.expectEqual(@as(i32, 0), pane.body.cur_row);
+}
diff --git a/src/exec.zig b/src/exec.zig
index 5b0eb204..7248632b 100644
--- a/src/exec.zig
+++ b/src/exec.zig
@@ -182,7 +182,9 @@ pub fn operandAt(p: *Pardes, pane: *Pane, clicked: Pane.Sel, borrow: bool, for_l
result.text = edit.currentSelText(p, pane);
return result;
}
- if (pane.body.msel.active and row >= @min(pane.body.msel.r0, pane.body.msel.r1) and
+ // A query pane's line selection is its current row's mark, not a
+ // selection made: a click on that row takes its word, as on any row.
+ if (pane.body.msel.active and pane.query == null and row >= @min(pane.body.msel.r0, pane.body.msel.r1) and
row <= @max(pane.body.msel.r0, pane.body.msel.r1))
{
result.text = edit.currentSelText(p, pane);
diff --git a/src/mouse.zig b/src/mouse.zig
index d8f19059..2ff4fef3 100644
--- a/src/mouse.zig
+++ b/src/mouse.zig
@@ -1104,7 +1104,7 @@ pub fn dispatchPointerBuiltin(
p.gpa.free(q.asked);
q.asked = &.{};
q.frozen = false;
- panes.Query.produceAll(p, false);
+ panes.Query.produceAll(p, false, false);
return;
};
// A middle click on a body is the exec a bound REPL may take.
diff --git a/src/pardes.zig b/src/pardes.zig
index a35d1604..20415e69 100644
--- a/src/pardes.zig
+++ b/src/pardes.zig
@@ -6204,7 +6204,7 @@ pub const Pardes = struct {
// step the ones an edit or a scroll left unfound in the view.
for (p.panes) |slot| if (slot) |pane| pane.places.settle(p, pane, ev == .tick);
// Each live query pane's rows, made from what changed this step.
- panes.Query.produceAll(p, ev == .tick);
+ panes.Query.produceAll(p, ev == .tick, ev == .key);
_ = p.scratch.reset(.{ .retain_with_limit = memory.limits.scratch_kept });
p.sync();
p.fsReport();