diff options
Diffstat (limited to 'src/Text.zig')
| -rw-r--r-- | src/Text.zig | 336 |
1 files changed, 0 insertions, 336 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 { |
