diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-10 05:37:21 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-10 05:42:39 -0300 |
| commit | 29e6abf9ccb07b1c10997588e1cf4cfc1ed53021 (patch) | |
| tree | e0a82d7ab1f4a184ee3398dc2c5452dbc42e31e0 /src | |
| parent | f30dfdb3d5546195a69f8046ded9acc669882b8d (diff) | |
| download | pardes-29e6abf9ccb07b1c10997588e1cf4cfc1ed53021.tar.gz pardes-29e6abf9ccb07b1c10997588e1cf4cfc1ed53021.zip | |
normal: the selection and motion actions only executeNormalAction calls (multiSelAction, maxLine, pointMove, wordMove, findMove, verticalMove with filePane, halfPageMove, gotoWindow, lineSelect, lineBoundsSelect and shrinkSelToLineBounds with lineStartOrEof) and selStart, which only replaySels calls, move from Text.zig to just before their callers as file-private fns taking the *Text, their bodies as they were, so a key's motion is read in the file that dispatches it
Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src')
| -rw-r--r-- | src/Text.zig | 336 | ||||
| -rw-r--r-- | src/normal.zig | 389 | ||||
| -rw-r--r-- | src/panes.zig | 2 |
3 files changed, 364 insertions, 363 deletions
diff --git a/src/Text.zig b/src/Text.zig index 650caea1..43a515e1 100644 --- a/src/Text.zig +++ b/src/Text.zig @@ -311,276 +311,6 @@ pub fn setRanges(t: *Text, text: []const u8, in: []const modal.Selection, sticky t.nsel = @intCast(w); } -pub fn multiSelAction(t: *Text, text: []const u8, role: modal.Normal.Role, cnt: usize) void { - var rs: [max_selections]modal.Selection = undefined; - const got = ranges(t, text, 0, &rs); - const n = got.n; - const expl = t.vsel.active and t.vsel.explicit; - if (role == .remove_primary_sel) { - if (n < 2) return; // helix: "no selections remaining" - var out: [max_selections]modal.Selection = undefined; - var m: usize = 0; - for (rs[0..n], 0..) |r, i| { - if (i == got.pri) continue; - out[m] = r; - m += 1; - } - // helix Selection::remove: the NEXT range takes over, or the - // previous one when the primary was last - return setRanges(t, text, out[0..m], &.{}, @min(got.pri, m - 1), expl); - } - if (role == .rotate_sel_fwd or role == .rotate_sel_back) { - const turn = cnt % n; - const pri = if (role == .rotate_sel_fwd) (got.pri + turn) % n else (got.pri + (n - turn)) % n; - return setRanges(t, text, rs[0..n], &.{}, pri, expl); - } - if (role == .merge_sels) { - // helix merge_selections: first.merge(last) — the ranges are - // sorted, so that is simply the whole span - const lo = @min(rs[0].anchor, rs[0].head); - const hi = @max(rs[n - 1].anchor, rs[n - 1].head); - const rev = rs[0].anchor > rs[0].head and rs[n - 1].anchor > rs[n - 1].head; - const one: modal.Selection = if (rev) .{ .anchor = hi, .head = lo } else .{ .anchor = lo, .head = hi }; - return setRanges(t, text, &.{one}, &.{}, 0, expl); - } - if (role == .merge_consecutive_sels) { - var out: [max_selections]modal.Selection = undefined; - var m: usize = 0; - var pri: usize = 0; - for (rs[0..n], 0..) |r, i| { - if (m > 0 and @min(r.anchor, r.head) == @max(out[m - 1].anchor, out[m - 1].head)) { - const lo = @min(@min(out[m - 1].anchor, out[m - 1].head), @min(r.anchor, r.head)); - const hi = @max(@max(out[m - 1].anchor, out[m - 1].head), @max(r.anchor, r.head)); - out[m - 1] = .{ .anchor = lo, .head = hi }; - if (i == got.pri) pri = m - 1; - continue; - } - if (i == got.pri) pri = m; - out[m] = r; - m += 1; - } - return setRanges(t, text, out[0..m], &.{}, pri, expl); - } - if (role == .ensure_forward) { - // helix ensure_selections_forward: every range points forward - for (rs[0..n]) |*r| { - const from = @min(r.anchor, r.head); - r.head = @max(r.anchor, r.head); - r.anchor = from; - } - return setRanges(t, text, rs[0..n], &.{}, got.pri, expl); - } - if (role == .split_sel_newline) { - // helix selection::split_on_newline — one range per line the - // selection covers, the newlines themselves left out - var out: [max_selections]modal.Selection = undefined; - var m: usize = 0; - for (rs[0..n]) |r| { - const from = @min(r.anchor, r.head); - const to = @max(r.anchor, r.head); - if (from == to) { - if (m < max_selections) { - out[m] = r; - m += 1; - } - continue; - } - var start = from; - while (start < to and m < max_selections) { - const eol = modal.lineEndOffset(text, modal.lineAtOffset(text, start)); - if (eol >= to) { - out[m] = .{ .anchor = start, .head = to }; - m += 1; - break; - } - out[m] = .{ .anchor = start, .head = eol }; - m += 1; - start = eol + 1; - } - } - if (m == 0) return; - return setRanges(t, text, out[0..m], &.{}, 0, true); // helix keeps primary 0 - } - if (role == .trim_sels) { - // helix trim_selections: whitespace off both ends; ranges that are - // empty or all whitespace are dropped entirely - var out: [max_selections]modal.Selection = undefined; - var m: usize = 0; - for (rs[0..n]) |r| { - var from = @min(r.anchor, r.head); - var to = @max(r.anchor, r.head); - while (from < to and std.ascii.isWhitespace(text[from])) from += 1; - while (to > from and std.ascii.isWhitespace(text[to - 1])) to -= 1; - if (from >= to) continue; - out[m] = if (r.anchor > r.head) .{ .anchor = to, .head = from } else .{ .anchor = from, .head = to }; - m += 1; - } - if (m == 0) { // helix: collapse_selection + keep_primary_selection - const c = modal.selectionCursor(text, rs[got.pri]); - return setRange(t, text, 0, .{ .anchor = c, .head = c }, false); - } - // helix: the first survivor that OVERLAPS the old primary, else the last - const pf = @min(rs[got.pri].anchor, rs[got.pri].head); - const pt = @max(rs[got.pri].anchor, rs[got.pri].head); - var pri = m - 1; - for (out[0..m], 0..) |r, i| { - const f = @min(r.anchor, r.head); - const to = @max(r.anchor, r.head); - if (f == pf or (to > pf and pt > f)) { - pri = i; - break; - } - } - return setRanges(t, text, out[0..m], &.{}, pri, expl); - } - const below = role == .copy_sel_below; - var out: [max_selections]modal.Selection = undefined; - var m: usize = 0; - var pri: usize = 0; - const nlines = modal.cursorLineCount(text); - for (rs[0..n], 0..) |r, ri| { - const is_pri = ri == got.pri; - // head-exclusive: back the leading end off onto its own cell - const hp = modal.positionAt(text, if (r.anchor < r.head) modal.prevGrapheme(text, r.head) else r.head); - const ap = modal.positionAt(text, if (r.anchor < r.head) r.anchor else modal.prevGrapheme(text, r.anchor)); - const height = @max(hp.row, ap.row) - @min(hp.row, ap.row) + 1; - if (m >= max_selections) break; - if (is_pri) pri = m; - out[m] = r; - m += 1; - var made: usize = 0; - var k: usize = 0; - while (made < cnt and m < max_selections) : (k += 1) { - const d = (k + 1) * height; - const arow = if (below) ap.row + d else ap.row -| d; - const hrow = if (below) hp.row + d else hp.row -| d; - if (arow >= nlines or hrow >= nlines) break; - const a2 = modal.offsetAt(text, .{ .row = arow, .col = ap.col }); - const h2 = modal.offsetAt(text, .{ .row = hrow, .col = hp.col }); - // a line too short to reach the column is skipped, not clamped - if (modal.positionAt(text, a2).col == ap.col and modal.positionAt(text, h2).col == hp.col) { - if (is_pri) pri = m; - out[m] = modal.moveSelectionCursor(text, .{ .anchor = a2, .head = a2 }, h2, true); - m += 1; - made += 1; - } - if (arow == 0 and hrow == 0) break; - } - } - setRanges(t, text, out[0..m], &.{}, pri, expl); -} - -/// a range's start CELL (document order key) — the smaller of its two ends -pub fn selStart(s: SelRange) struct { row: i32, col: i32 } { - if (s.arow < s.row or (s.arow == s.row and s.acol < s.col)) return .{ .row = s.arow, .col = s.acol }; - return .{ .row = s.row, .col = s.col }; -} - -pub fn maxLine(text: []const u8) usize { - const nl = modal.cursorLineCount(text); - return if (text.len == 0 or text[text.len - 1] == '\n') nl -| 2 else nl - 1; -} - -/// point-target motion: collapse there (extend in select mode) -pub fn pointMove(t: *Text, text: []const u8, range: modal.Selection, target: usize) void { - setRange(t, text, 0, modal.moveSelectionCursor(text, range, target, t.select), false); -} - -/// word motions select their traversed span (extend mode: head only) -pub fn wordMove(t: *Text, text: []const u8, range: modal.Selection, cnt: usize, target: modal.WordTarget) void { - const wr = modal.moveWord(text, range, cnt, target); - const res = if (t.select) modal.moveSelectionCursor(text, range, modal.selectionCursor(text, wr), true) else wr; - setRange(t, text, 0, res, false); -} - -/// f/t/F/T: anchor at the old cursor cell, head on the hit (not found: no move) -pub fn findMove(t: *Text, text: []const u8, range: modal.Selection, ch: u21, kind: modal.Normal.Find, cnt: usize) void { - const cur = modal.selectionCursor(text, range); - const target = modal.findTarget(text, cur, ch, kind, cnt) orelse return; - const res = if (t.select) - modal.moveSelectionCursor(text, range, target, true) - else - modal.moveSelectionCursor(text, .{ .anchor = cur, .head = cur }, target, true); - setRange(t, text, 0, res, false); -} - -/// The file pane whose text `text` is, when `t` is its body: its rows are -/// measured through their table pads (File.rowPads), so j and k keep the -/// column a table shows, not the one its source has. -fn filePane(t: *Text, text: []const u8) ?*const Pane { - if (t.what != .body) return null; - const fp = t.pane() orelse return null; - const f = if (fp.file) |*file| file else return null; - return if (f.content.ptr == text.ptr and f.content.len == text.len) fp else null; -} - -/// j/k (`width` 0: a line is one row) and gj/gk (the rows a body `width` -/// cells wide wraps a line into): sticky goal column, clamped onto short -/// lines' newline. -pub fn verticalMove(t: *Text, text: []const u8, range: modal.Selection, down: bool, cnt: usize, width: usize) void { - const cur = modal.selectionCursor(text, range); - const pos = File.textPosition(t, text, cur); - const last_row = File.textLineCount(t, text) - 1; - var row = pos.row; - var line = File.textLine(t, text, row); - var vrow = File.visualRow(line, pos.col, width); - const shown = filePane(t, text); - const goal: usize = if (t.sticky_col >= 0) - @intCast(t.sticky_col) - else if (shown) |fp| - @intCast(@max(0, File.displayOffset(fp, @intCast(row), @intCast(vrow.start), @intCast(pos.col)))) - else - File.rawDisplayCol(line[vrow.start..vrow.end], pos.col -| vrow.start); - if (width == 0) { - row = if (down) @min(pos.row + @max(1, cnt), last_row) else pos.row -| @max(1, cnt); - line = File.textLine(t, text, row); - vrow = .{ .start = 0, .end = line.len }; - } else for (0..@max(1, cnt)) |_| { - if (down) { - if (vrow.end < line.len) { - vrow = File.visualRow(line, vrow.end, width); - continue; - } - if (row == last_row) break; - row += 1; - line = File.textLine(t, text, row); - vrow = File.visualRow(line, 0, width); - } else { - if (vrow.start > 0) { - vrow = File.visualRow(line, vrow.start - 1, width); - continue; - } - if (row == 0) break; - row -= 1; - line = File.textLine(t, text, row); - vrow = File.visualRow(line, line.len, width); - } - } - // The newline slot is a real cursor position, but the first byte of the - // NEXT visual row is not: landing there would read as two rows moved. - var target_col: usize = if (shown) |fp| - @intCast(File.byteAtRowDisplay(fp, @intCast(row), @intCast(vrow.start), @intCast(@min(goal, std.math.maxInt(i32))))) - else - vrow.start + File.rawAtDisplay(line[vrow.start..vrow.end], goal); - if (vrow.end < line.len and target_col >= vrow.end) - target_col = modal.graphemeStart(line, vrow.end - 1); - const target = File.textOffset(t, text, .{ .row = row, .col = target_col }); - // extend mode never walks onto the empty trailing line (helix) - if (t.select and target == text.len and text.len > 0 and text[text.len - 1] == '\n') return; - setRange(t, text, 0, modal.moveSelectionCursor(text, range, target, t.select), false); - t.sticky_col = @intCast(goal); -} - -/// Ctrl-d/u: scroll half a page AND move the cursor by the same rows -pub fn halfPageMove(t: *Text, text: []const u8, range: modal.Selection, down: bool) void { - // a view to move through is a body's; a tag or an answer is all shown - if (t.what != .body) return; - const w = t.pane() orelse return; - const half: i32 = @max(1, @divTrunc(@as(i32, w.rows), 2)); - w.scrollBy(if (down) half else -half); - verticalMove(t, text, range, down, @intCast(half), 0); -} - /// helix `scroll` without cursor sync (Ctrl-f/b, PgUp/PgDn, zj/zk): shift /// the view, then snap a fallen-out cursor to the near scrolloff edge, col 0 pub fn scrollViewMove(t: *Text, text: []const u8, range: modal.Selection, delta: i32) void { @@ -607,26 +337,6 @@ pub fn scrollViewMove(t: *Text, text: []const u8, range: modal.Selection, delta: } } -/// gt/gb: view-relative rows, col 0, scrolloff clamped (helix goto_window) -pub fn gotoWindow(t: *Text, text: []const u8, range: modal.Selection, which: enum { top, bottom }, cnt: usize) void { - // a view to move through is a body's; a tag or an answer is all shown - if (t.what != .body) return; - const w = t.pane() orelse return; - const margin: i32 = @min(config.scroll_off, @divTrunc(@as(i32, w.rows) - 1, 2)); - const top = w.scroll(); - const last_row: i32 = @intCast(File.textLineCount(t, text) - 1); - const last_vis: i32 = @min(@as(i32, w.rows) - 1, last_row - top); - const n: i32 = @intCast(cnt - 1); - var vline: i32 = switch (which) { - .top => top + margin + n, - .bottom => top + last_vis - (margin + n), - }; - vline = @max(vline, top + margin); - vline = @min(vline, top + last_vis - margin); - const row: i32 = std.math.clamp(vline, 0, last_row); - pointMove(t, text, range, File.textLineStart(t, text, @intCast(row))); -} - /// helix Range::line_range — the inclusive line span a range covers pub fn rangeLineSpan(text: []const u8, r: modal.Selection) struct { start: usize, end: usize } { const from = @min(r.anchor, r.head); @@ -635,52 +345,6 @@ pub fn rangeLineSpan(text: []const u8, r: modal.Selection) struct { start: usize return .{ .start = modal.lineAtOffset(text, from), .end = modal.lineAtOffset(text, to_adj) }; } -fn lineStartOrEof(text: []const u8, line: usize) usize { - if (line >= modal.cursorLineCount(text)) return text.len; - return modal.lineStartOffset(text, line); -} - -/// helix `x` extend_line_below: full lines incl. the newline, cursor ON -/// the last one's '\n'; an already-line-bounded selection grows downward -pub fn lineSelect(t: *Text, text: []const u8, range: modal.Selection, cnt: usize) void { - const span = rangeLineSpan(text, range); - const start = modal.lineStartOffset(text, span.start); - const end = lineStartOrEof(text, span.end + 1); - const full = @min(range.anchor, range.head) == start and @max(range.anchor, range.head) == end; - const head = lineStartOrEof(text, span.end + cnt + @intFromBool(full)); - setRange(t, text, 0, .{ .anchor = start, .head = head }, true); -} - -/// helix `X` extend_to_line_bounds (direction kept) -pub fn lineBoundsSelect(t: *Text, text: []const u8, range: modal.Selection) void { - const span = rangeLineSpan(text, range); - const start = modal.lineStartOffset(text, span.start); - const end = lineStartOrEof(text, span.end + 1); - const r: modal.Selection = if (range.head < range.anchor) - .{ .anchor = end, .head = start } - else - .{ .anchor = start, .head = end }; - setRange(t, text, 0, r, true); -} - -/// helix `Alt-x` shrink_to_line_bounds (single-line selections untouched) -pub fn shrinkSelToLineBounds(t: *Text, text: []const u8, range: modal.Selection) void { - const span = rangeLineSpan(text, range); - if (span.start == span.end) return; - const from = @min(range.anchor, range.head); - const to = @max(range.anchor, range.head); - var start = modal.lineStartOffset(text, span.start); - var end = lineStartOrEof(text, span.end + 1); - if (start != from) start = lineStartOrEof(text, span.start + 1); - if (end != to) end = modal.lineStartOffset(text, span.end); - const expl = t.vsel.active and t.vsel.explicit; - const r: modal.Selection = if (range.head < range.anchor) - .{ .anchor = end, .head = start } - else - .{ .anchor = start, .head = end }; - setRange(t, text, 0, r, expl); -} - /// pull the cursor back inside `text` after a rewrite; `row0` is the /// absolute surface row of its first line (0 for a file) pub fn clampCursor(t: *Text, text: []const u8, row0: i32) void { diff --git a/src/normal.zig b/src/normal.zig index e95a5798..dbd35fb5 100644 --- a/src/normal.zig +++ b/src/normal.zig @@ -15,6 +15,7 @@ const config = @import("config.zig"); const keyboard = @import("keys.zig"); const Pane = panes.Pane; const Text = panes.Text; +const File = panes.File; const Key = pardes.Key; const hit = pardes.hit; const sel_slot = pardes.sel_slot; @@ -387,6 +388,12 @@ const Replay = union(enum) { insert: Key, }; +/// a range's start CELL (document order key) — the smaller of its two ends +fn selStart(s: Text.SelRange) struct { row: i32, col: i32 } { + if (s.arow < s.row or (s.arow == s.row and s.acol < s.col)) return .{ .row = s.arow, .col = s.acol }; + return .{ .row = s.row, .col = s.col }; +} + pub fn replaySels(p: *Pardes, t: *Text, what: Replay) void { const id = p.view().active; // a header belongs to no pane and cannot go away under the replay @@ -404,10 +411,10 @@ pub fn replaySels(p: *Pardes, t: *Text, what: Replay) void { .acol = if (t.vsel.active) t.vsel.col else t.cur_col, .sticky = t.sticky_col, }; - const pr = Text.selStart(prim); + const pr = selStart(prim); var placed = false; for (t.sels[0..t.nsel]) |s| { - const sr = Text.selStart(s); + const sr = selStart(s); if (!placed and (pr.row < sr.row or (pr.row == sr.row and pr.col <= sr.col))) { pri = n; list[n] = prim; @@ -607,6 +614,336 @@ pub fn searchSelection(p: *Pardes, t: *Text, text: []const u8, boundary: bool) e p.registers.last_search = name; } +fn multiSelAction(t: *Text, text: []const u8, role: modal.Normal.Role, cnt: usize) void { + var rs: [Text.max_selections]modal.Selection = undefined; + const got = t.ranges(text, 0, &rs); + const n = got.n; + const expl = t.vsel.active and t.vsel.explicit; + if (role == .remove_primary_sel) { + if (n < 2) return; // helix: "no selections remaining" + var out: [Text.max_selections]modal.Selection = undefined; + var m: usize = 0; + for (rs[0..n], 0..) |r, i| { + if (i == got.pri) continue; + out[m] = r; + m += 1; + } + // helix Selection::remove: the NEXT range takes over, or the + // previous one when the primary was last + return t.setRanges(text, out[0..m], &.{}, @min(got.pri, m - 1), expl); + } + if (role == .rotate_sel_fwd or role == .rotate_sel_back) { + const turn = cnt % n; + const pri = if (role == .rotate_sel_fwd) (got.pri + turn) % n else (got.pri + (n - turn)) % n; + return t.setRanges(text, rs[0..n], &.{}, pri, expl); + } + if (role == .merge_sels) { + // helix merge_selections: first.merge(last) — the ranges are + // sorted, so that is simply the whole span + const lo = @min(rs[0].anchor, rs[0].head); + const hi = @max(rs[n - 1].anchor, rs[n - 1].head); + const rev = rs[0].anchor > rs[0].head and rs[n - 1].anchor > rs[n - 1].head; + const one: modal.Selection = if (rev) .{ .anchor = hi, .head = lo } else .{ .anchor = lo, .head = hi }; + return t.setRanges(text, &.{one}, &.{}, 0, expl); + } + if (role == .merge_consecutive_sels) { + var out: [Text.max_selections]modal.Selection = undefined; + var m: usize = 0; + var pri: usize = 0; + for (rs[0..n], 0..) |r, i| { + if (m > 0 and @min(r.anchor, r.head) == @max(out[m - 1].anchor, out[m - 1].head)) { + const lo = @min(@min(out[m - 1].anchor, out[m - 1].head), @min(r.anchor, r.head)); + const hi = @max(@max(out[m - 1].anchor, out[m - 1].head), @max(r.anchor, r.head)); + out[m - 1] = .{ .anchor = lo, .head = hi }; + if (i == got.pri) pri = m - 1; + continue; + } + if (i == got.pri) pri = m; + out[m] = r; + m += 1; + } + return t.setRanges(text, out[0..m], &.{}, pri, expl); + } + if (role == .ensure_forward) { + // helix ensure_selections_forward: every range points forward + for (rs[0..n]) |*r| { + const from = @min(r.anchor, r.head); + r.head = @max(r.anchor, r.head); + r.anchor = from; + } + return t.setRanges(text, rs[0..n], &.{}, got.pri, expl); + } + if (role == .split_sel_newline) { + // helix selection::split_on_newline — one range per line the + // selection covers, the newlines themselves left out + var out: [Text.max_selections]modal.Selection = undefined; + var m: usize = 0; + for (rs[0..n]) |r| { + const from = @min(r.anchor, r.head); + const to = @max(r.anchor, r.head); + if (from == to) { + if (m < Text.max_selections) { + out[m] = r; + m += 1; + } + continue; + } + var start = from; + while (start < to and m < Text.max_selections) { + const eol = modal.lineEndOffset(text, modal.lineAtOffset(text, start)); + if (eol >= to) { + out[m] = .{ .anchor = start, .head = to }; + m += 1; + break; + } + out[m] = .{ .anchor = start, .head = eol }; + m += 1; + start = eol + 1; + } + } + if (m == 0) return; + return t.setRanges(text, out[0..m], &.{}, 0, true); // helix keeps primary 0 + } + if (role == .trim_sels) { + // helix trim_selections: whitespace off both ends; ranges that are + // empty or all whitespace are dropped entirely + var out: [Text.max_selections]modal.Selection = undefined; + var m: usize = 0; + for (rs[0..n]) |r| { + var from = @min(r.anchor, r.head); + var to = @max(r.anchor, r.head); + while (from < to and std.ascii.isWhitespace(text[from])) from += 1; + while (to > from and std.ascii.isWhitespace(text[to - 1])) to -= 1; + if (from >= to) continue; + out[m] = if (r.anchor > r.head) .{ .anchor = to, .head = from } else .{ .anchor = from, .head = to }; + m += 1; + } + if (m == 0) { // helix: collapse_selection + keep_primary_selection + const c = modal.selectionCursor(text, rs[got.pri]); + return t.setRange(text, 0, .{ .anchor = c, .head = c }, false); + } + // helix: the first survivor that OVERLAPS the old primary, else the last + const pf = @min(rs[got.pri].anchor, rs[got.pri].head); + const pt = @max(rs[got.pri].anchor, rs[got.pri].head); + var pri = m - 1; + for (out[0..m], 0..) |r, i| { + const f = @min(r.anchor, r.head); + const to = @max(r.anchor, r.head); + if (f == pf or (to > pf and pt > f)) { + pri = i; + break; + } + } + return t.setRanges(text, out[0..m], &.{}, pri, expl); + } + const below = role == .copy_sel_below; + var out: [Text.max_selections]modal.Selection = undefined; + var m: usize = 0; + var pri: usize = 0; + const nlines = modal.cursorLineCount(text); + for (rs[0..n], 0..) |r, ri| { + const is_pri = ri == got.pri; + // head-exclusive: back the leading end off onto its own cell + const hp = modal.positionAt(text, if (r.anchor < r.head) modal.prevGrapheme(text, r.head) else r.head); + const ap = modal.positionAt(text, if (r.anchor < r.head) r.anchor else modal.prevGrapheme(text, r.anchor)); + const height = @max(hp.row, ap.row) - @min(hp.row, ap.row) + 1; + if (m >= Text.max_selections) break; + if (is_pri) pri = m; + out[m] = r; + m += 1; + var made: usize = 0; + var k: usize = 0; + while (made < cnt and m < Text.max_selections) : (k += 1) { + const d = (k + 1) * height; + const arow = if (below) ap.row + d else ap.row -| d; + const hrow = if (below) hp.row + d else hp.row -| d; + if (arow >= nlines or hrow >= nlines) break; + const a2 = modal.offsetAt(text, .{ .row = arow, .col = ap.col }); + const h2 = modal.offsetAt(text, .{ .row = hrow, .col = hp.col }); + // a line too short to reach the column is skipped, not clamped + if (modal.positionAt(text, a2).col == ap.col and modal.positionAt(text, h2).col == hp.col) { + if (is_pri) pri = m; + out[m] = modal.moveSelectionCursor(text, .{ .anchor = a2, .head = a2 }, h2, true); + m += 1; + made += 1; + } + if (arow == 0 and hrow == 0) break; + } + } + t.setRanges(text, out[0..m], &.{}, pri, expl); +} + +fn maxLine(text: []const u8) usize { + const nl = modal.cursorLineCount(text); + return if (text.len == 0 or text[text.len - 1] == '\n') nl -| 2 else nl - 1; +} + +/// point-target motion: collapse there (extend in select mode) +fn pointMove(t: *Text, text: []const u8, range: modal.Selection, target: usize) void { + t.setRange(text, 0, modal.moveSelectionCursor(text, range, target, t.select), false); +} + +/// word motions select their traversed span (extend mode: head only) +fn wordMove(t: *Text, text: []const u8, range: modal.Selection, cnt: usize, target: modal.WordTarget) void { + const wr = modal.moveWord(text, range, cnt, target); + const res = if (t.select) modal.moveSelectionCursor(text, range, modal.selectionCursor(text, wr), true) else wr; + t.setRange(text, 0, res, false); +} + +/// f/t/F/T: anchor at the old cursor cell, head on the hit (not found: no move) +fn findMove(t: *Text, text: []const u8, range: modal.Selection, ch: u21, kind: modal.Normal.Find, cnt: usize) void { + const cur = modal.selectionCursor(text, range); + const target = modal.findTarget(text, cur, ch, kind, cnt) orelse return; + const res = if (t.select) + modal.moveSelectionCursor(text, range, target, true) + else + modal.moveSelectionCursor(text, .{ .anchor = cur, .head = cur }, target, true); + t.setRange(text, 0, res, false); +} + +/// The file pane whose text `text` is, when `t` is its body: its rows are +/// measured through their table pads (File.rowPads), so j and k keep the +/// column a table shows, not the one its source has. +fn filePane(t: *Text, text: []const u8) ?*const Pane { + if (t.what != .body) return null; + const fp = t.pane() orelse return null; + const f = if (fp.file) |*file| file else return null; + return if (f.content.ptr == text.ptr and f.content.len == text.len) fp else null; +} + +/// j/k (`width` 0: a line is one row) and gj/gk (the rows a body `width` +/// cells wide wraps a line into): sticky goal column, clamped onto short +/// lines' newline. +fn verticalMove(t: *Text, text: []const u8, range: modal.Selection, down: bool, cnt: usize, width: usize) void { + const cur = modal.selectionCursor(text, range); + const pos = File.textPosition(t, text, cur); + const last_row = File.textLineCount(t, text) - 1; + var row = pos.row; + var line = File.textLine(t, text, row); + var vrow = File.visualRow(line, pos.col, width); + const shown = filePane(t, text); + const goal: usize = if (t.sticky_col >= 0) + @intCast(t.sticky_col) + else if (shown) |fp| + @intCast(@max(0, File.displayOffset(fp, @intCast(row), @intCast(vrow.start), @intCast(pos.col)))) + else + File.rawDisplayCol(line[vrow.start..vrow.end], pos.col -| vrow.start); + if (width == 0) { + row = if (down) @min(pos.row + @max(1, cnt), last_row) else pos.row -| @max(1, cnt); + line = File.textLine(t, text, row); + vrow = .{ .start = 0, .end = line.len }; + } else for (0..@max(1, cnt)) |_| { + if (down) { + if (vrow.end < line.len) { + vrow = File.visualRow(line, vrow.end, width); + continue; + } + if (row == last_row) break; + row += 1; + line = File.textLine(t, text, row); + vrow = File.visualRow(line, 0, width); + } else { + if (vrow.start > 0) { + vrow = File.visualRow(line, vrow.start - 1, width); + continue; + } + if (row == 0) break; + row -= 1; + line = File.textLine(t, text, row); + vrow = File.visualRow(line, line.len, width); + } + } + // The newline slot is a real cursor position, but the first byte of the + // NEXT visual row is not: landing there would read as two rows moved. + var target_col: usize = if (shown) |fp| + @intCast(File.byteAtRowDisplay(fp, @intCast(row), @intCast(vrow.start), @intCast(@min(goal, std.math.maxInt(i32))))) + else + vrow.start + File.rawAtDisplay(line[vrow.start..vrow.end], goal); + if (vrow.end < line.len and target_col >= vrow.end) + target_col = modal.graphemeStart(line, vrow.end - 1); + const target = File.textOffset(t, text, .{ .row = row, .col = target_col }); + // extend mode never walks onto the empty trailing line (helix) + if (t.select and target == text.len and text.len > 0 and text[text.len - 1] == '\n') return; + t.setRange(text, 0, modal.moveSelectionCursor(text, range, target, t.select), false); + t.sticky_col = @intCast(goal); +} + +/// Ctrl-d/u: scroll half a page AND move the cursor by the same rows +fn halfPageMove(t: *Text, text: []const u8, range: modal.Selection, down: bool) void { + // a view to move through is a body's; a tag or an answer is all shown + if (t.what != .body) return; + const w = t.pane() orelse return; + const half: i32 = @max(1, @divTrunc(@as(i32, w.rows), 2)); + w.scrollBy(if (down) half else -half); + verticalMove(t, text, range, down, @intCast(half), 0); +} + +/// gt/gb: view-relative rows, col 0, scrolloff clamped (helix goto_window) +fn gotoWindow(t: *Text, text: []const u8, range: modal.Selection, which: enum { top, bottom }, cnt: usize) void { + // a view to move through is a body's; a tag or an answer is all shown + if (t.what != .body) return; + const w = t.pane() orelse return; + const margin: i32 = @min(config.scroll_off, @divTrunc(@as(i32, w.rows) - 1, 2)); + const top = w.scroll(); + const last_row: i32 = @intCast(File.textLineCount(t, text) - 1); + const last_vis: i32 = @min(@as(i32, w.rows) - 1, last_row - top); + const n: i32 = @intCast(cnt - 1); + var vline: i32 = switch (which) { + .top => top + margin + n, + .bottom => top + last_vis - (margin + n), + }; + vline = @max(vline, top + margin); + vline = @min(vline, top + last_vis - margin); + const row: i32 = std.math.clamp(vline, 0, last_row); + pointMove(t, text, range, File.textLineStart(t, text, @intCast(row))); +} + +fn lineStartOrEof(text: []const u8, line: usize) usize { + if (line >= modal.cursorLineCount(text)) return text.len; + return modal.lineStartOffset(text, line); +} + +/// helix `x` extend_line_below: full lines incl. the newline, cursor ON +/// the last one's '\n'; an already-line-bounded selection grows downward +fn lineSelect(t: *Text, text: []const u8, range: modal.Selection, cnt: usize) void { + const span = Text.rangeLineSpan(text, range); + const start = modal.lineStartOffset(text, span.start); + const end = lineStartOrEof(text, span.end + 1); + const full = @min(range.anchor, range.head) == start and @max(range.anchor, range.head) == end; + const head = lineStartOrEof(text, span.end + cnt + @intFromBool(full)); + t.setRange(text, 0, .{ .anchor = start, .head = head }, true); +} + +/// helix `X` extend_to_line_bounds (direction kept) +fn lineBoundsSelect(t: *Text, text: []const u8, range: modal.Selection) void { + const span = Text.rangeLineSpan(text, range); + const start = modal.lineStartOffset(text, span.start); + const end = lineStartOrEof(text, span.end + 1); + const r: modal.Selection = if (range.head < range.anchor) + .{ .anchor = end, .head = start } + else + .{ .anchor = start, .head = end }; + t.setRange(text, 0, r, true); +} + +/// helix `Alt-x` shrink_to_line_bounds (single-line selections untouched) +fn shrinkSelToLineBounds(t: *Text, text: []const u8, range: modal.Selection) void { + const span = Text.rangeLineSpan(text, range); + if (span.start == span.end) return; + const from = @min(range.anchor, range.head); + const to = @max(range.anchor, range.head); + var start = modal.lineStartOffset(text, span.start); + var end = lineStartOrEof(text, span.end + 1); + if (start != from) start = lineStartOrEof(text, span.start + 1); + if (end != to) end = modal.lineStartOffset(text, span.end); + const expl = t.vsel.active and t.vsel.explicit; + const r: modal.Selection = if (range.head < range.anchor) + .{ .anchor = end, .head = start } + else + .{ .anchor = start, .head = end }; + t.setRange(text, 0, r, expl); +} + fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) edit.Error!void { const v = p.view(); // What reaches past the text -- a builtin, a search, a language query -- @@ -635,15 +972,15 @@ fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) edit.Error! // gg and G: a count names the line; bare G is no move (helix) .goto_file_start, .goto_line => { if (!a.explicit and a.role == .goto_line) return; - const to = if (a.explicit) @min(@as(usize, a.count) - 1, Text.maxLine(text)) else 0; - return t.pointMove(text, range, modal.lineStartOffset(text, to)); + const to = if (a.explicit) @min(@as(usize, a.count) - 1, maxLine(text)) else 0; + return pointMove(t, text, range, modal.lineStartOffset(text, to)); }, - .goto_last_line => return t.pointMove(text, range, modal.lineStartOffset(text, Text.maxLine(text))), - .goto_line_start, .line_start => return t.pointMove(text, range, modal.lineStartOffset(text, modal.lineAtOffset(text, cur))), + .goto_last_line => return pointMove(t, text, range, modal.lineStartOffset(text, maxLine(text))), + .goto_line_start, .line_start => return pointMove(t, text, range, modal.lineStartOffset(text, modal.lineAtOffset(text, cur))), .goto_line_end, .line_end => { const line = modal.lineAtOffset(text, cur); const ls = modal.lineStartOffset(text, line); - return t.pointMove(text, range, @max(ls, modal.prevGrapheme(text, modal.lineEndOffset(text, line)))); + return pointMove(t, text, range, @max(ls, modal.prevGrapheme(text, modal.lineEndOffset(text, line)))); }, .goto_first_nonws, .line_first_nonws => { const line = modal.lineAtOffset(text, cur); @@ -651,20 +988,20 @@ fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) edit.Error! const slice = text[ls..modal.lineEndOffset(text, line)]; const nw = modal.firstNonWs(slice); if (nw == slice.len) return; - return t.pointMove(text, range, ls + nw); + return pointMove(t, text, range, ls + nw); }, - .goto_line_down => return t.verticalMove(text, range, true, a.count, edit.paneWrapWidth(p, t)), - .goto_line_up => return t.verticalMove(text, range, false, a.count, edit.paneWrapWidth(p, t)), + .goto_line_down => return verticalMove(t, text, range, true, a.count, edit.paneWrapWidth(p, t)), + .goto_line_up => return verticalMove(t, text, range, false, a.count, edit.paneWrapWidth(p, t)), .goto_column => { const line = modal.lineAtOffset(text, cur); const ls = modal.lineStartOffset(text, line); - return t.pointMove(text, range, ls + modal.graphemeAtColumn(text[ls..modal.lineEndOffset(text, line)], @as(usize, a.count) - 1)); + return pointMove(t, text, range, ls + modal.graphemeAtColumn(text[ls..modal.lineEndOffset(text, line)], @as(usize, a.count) - 1)); }, - .goto_view_top => return t.gotoWindow(text, range, .top, a.count), - .goto_view_bottom => return t.gotoWindow(text, range, .bottom, a.count), + .goto_view_top => return gotoWindow(t, text, range, .top, a.count), + .goto_view_bottom => return gotoWindow(t, text, range, .bottom, a.count), .goto_last_modification => { const at = t.last_edit orelse return; - return t.pointMove(text, range, modal.offsetAt(text, .{ .row = @intCast(@max(0, at.row)), .col = @intCast(@max(0, at.col)) })); + return pointMove(t, text, range, modal.offsetAt(text, .{ .row = @intCast(@max(0, at.row)), .col = @intCast(@max(0, at.col)) })); }, // helix align_view: the cursor's row goes `above` rows down the // view, the middle of an odd one and the upper middle of an even one @@ -698,14 +1035,14 @@ fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) edit.Error! else => .till_backward, }; t.find_ch = a.char; - return t.findMove(text, range, a.char, t.find_op.?, a.count); + return findMove(t, text, range, a.char, t.find_op.?, a.count); }, .replace_prefix => return edit.normalReplaceChar(p, t, a.char), // `mm` .match_prefix => { const lines = try edit.paneCursorLines(p, t); const mc = modal.matchBracket(lines, modal.positionAt(text, cur)) orelse return; - return t.pointMove(text, range, modal.offsetAt(text, mc)); + return pointMove(t, text, range, modal.offsetAt(text, mc)); }, .match_inside, .match_around => return edit.textobjectSelect(p, t, text, a.char, a.role == .match_around), .surround_add => return edit.surroundAdd(p, t, a.char), @@ -734,11 +1071,11 @@ fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) edit.Error! .move_left, .move_right => { var target = cur; for (0..a.count) |_| target = if (a.role == .move_left) modal.prevGrapheme(text, target) else modal.nextGrapheme(text, target); - return t.pointMove(text, range, target); + return pointMove(t, text, range, target); }, - .move_down => return t.verticalMove(text, range, true, a.count, 0), - .move_up => return t.verticalMove(text, range, false, a.count, 0), - inline .next_word_start, .prev_word_start, .next_word_end, .next_long_word_start, .prev_long_word_start, .next_long_word_end => |role| return t.wordMove(text, range, a.count, @field(modal.WordTarget, @tagName(role))), + .move_down => return verticalMove(t, text, range, true, a.count, 0), + .move_up => return verticalMove(t, text, range, false, a.count, 0), + inline .next_word_start, .prev_word_start, .next_word_end, .next_long_word_start, .prev_long_word_start, .next_long_word_end => |role| return wordMove(t, text, range, a.count, @field(modal.WordTarget, @tagName(role))), .repeat_find => { const kind = t.find_op orelse return; var repeated = range; @@ -756,8 +1093,8 @@ fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) edit.Error! return t.setRange(text, 0, repeated, false); }, // These four move exactly one viewport unit, whatever the count. - .half_page_down => return t.halfPageMove(text, range, true), - .half_page_up => return t.halfPageMove(text, range, false), + .half_page_down => return halfPageMove(t, text, range, true), + .half_page_up => return halfPageMove(t, text, range, false), .page_down => return t.scrollViewMove(text, range, @as(i32, pane.rows)), .page_up => return t.scrollViewMove(text, range, -@as(i32, pane.rows)), .insert, .append, .insert_line_start, .insert_line_end, .open_below, .open_above => { @@ -784,9 +1121,9 @@ fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) edit.Error! } t.cur_pinned = true; }, - .select_line => return t.lineSelect(text, range, a.count), - .select_line_bounds => return t.lineBoundsSelect(text, range), - .shrink_to_line_bounds => return t.shrinkSelToLineBounds(text, range), + .select_line => return lineSelect(t, text, range, a.count), + .select_line_bounds => return lineBoundsSelect(t, text, range), + .shrink_to_line_bounds => return shrinkSelToLineBounds(t, text, range), .collapse_selection => return t.setRange(text, 0, .{ .anchor = cur, .head = cur }, false), .flip_selection => { const explicit = t.vsel.active and t.vsel.explicit; @@ -794,7 +1131,7 @@ fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) edit.Error! }, .select_all => return t.setRange(text, 0, .{ .anchor = 0, .head = text.len }, false), .keep_primary_sel => return t.dropSels(), - .copy_sel_below, .copy_sel_above, .remove_primary_sel, .rotate_sel_fwd, .rotate_sel_back, .split_sel_newline, .merge_sels, .merge_consecutive_sels, .trim_sels, .ensure_forward => return t.multiSelAction(text, a.role, a.count), + .copy_sel_below, .copy_sel_above, .remove_primary_sel, .rotate_sel_fwd, .rotate_sel_back, .split_sel_newline, .merge_sels, .merge_consecutive_sels, .trim_sels, .ensure_forward => return multiSelAction(t, text, a.role, a.count), // s, S and | answer for the text they are pressed in; `/` searches // the body wherever it is pressed, as acme's Look from a tag does. inline .select_regex, .split_regex, .keep_regex, .remove_regex => |role| return startSelRegex(p, pane, t, @field(modal.Normal.RegexMode, @tagName(role)[0 .. @tagName(role).len - "_regex".len])), diff --git a/src/panes.zig b/src/panes.zig index 02d5024a..37db35f6 100644 --- a/src/panes.zig +++ b/src/panes.zig @@ -284,7 +284,7 @@ pub const Pane = struct { geometry_len: usize = 0, /// The session's part of the geometry, as of the last step (`sync`). /// Digested and dumped, not made again: a key's move reads it before its - /// step's `sync` (Text.halfPageMove's scroll), and a core loaded from a + /// step's `sync` (normal.halfPageMove's scroll), and a core loaded from a /// dump has had none (Replica.initDump). view: SessionView = .{}, sel: [3]Sel = @splat(.{}), |
