diff options
| -rw-r--r-- | src/Text.zig | 51 | ||||
| -rw-r--r-- | src/normal.zig | 8 |
2 files changed, 17 insertions, 42 deletions
diff --git a/src/Text.zig b/src/Text.zig index 7801d364..594307a8 100644 --- a/src/Text.zig +++ b/src/Text.zig @@ -531,43 +531,15 @@ fn filePane(t: *Text, text: []const u8) ?*const Pane { return if (f.content.ptr == text.ptr and f.content.len == text.len) fp else null; } -/// j/k and friends: sticky goal column, clamped onto short lines' newline -pub fn verticalMove(t: *Text, text: []const u8, range: modal.Selection, down: bool, cnt: usize) void { - const cur = modal.selectionCursor(text, range); - const pos = File.textPosition(t, text, cur); - 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(pos.row), 0, @intCast(pos.col)))) - else - File.rawDisplayCol(File.textLine(t, text, pos.row), pos.col); - const last_row = File.textLineCount(t, text) - 1; - const nline = if (down) @min(pos.row + @max(1, cnt), last_row) else pos.row -| @max(1, cnt); - const target_col: usize = if (shown) |fp| - @intCast(File.byteAtRowDisplay(fp, @intCast(nline), 0, @intCast(@min(goal, std.math.maxInt(i32))))) - else - File.rawAtDisplay(File.textLine(t, text, nline), goal); - const target = File.textOffset(t, text, .{ .row = nline, .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); -} - -pub fn visualMove( - t: *Text, - text: []const u8, - range: modal.Selection, - down: bool, - cnt: usize, - width: usize, -) void { +/// 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 = modal.lineSlice(text, 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) @@ -576,8 +548,11 @@ pub fn visualMove( @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); - var steps = @max(1, cnt); - while (steps > 0) : (steps -= 1) { + 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); @@ -585,7 +560,7 @@ pub fn visualMove( } if (row == last_row) break; row += 1; - line = modal.lineSlice(text, row); + line = File.textLine(t, text, row); vrow = File.visualRow(line, 0, width); } else { if (vrow.start > 0) { @@ -594,7 +569,7 @@ pub fn visualMove( } if (row == 0) break; row -= 1; - line = modal.lineSlice(text, row); + line = File.textLine(t, text, row); vrow = File.visualRow(line, line.len, width); } } @@ -620,7 +595,7 @@ pub fn halfPageMove(t: *Text, text: []const u8, range: modal.Selection, down: bo 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)); + verticalMove(t, text, range, down, @intCast(half), 0); } /// helix `scroll` without cursor sync (Ctrl-f/b, PgUp/PgDn, zj/zk): shift diff --git a/src/normal.zig b/src/normal.zig index 48ac636c..8f777ec7 100644 --- a/src/normal.zig +++ b/src/normal.zig @@ -652,8 +652,8 @@ fn executeNormalAction(p: *Pardes, t: *Text, semantic: modal.Normal.Action) void if (nw == slice.len) return; return t.pointMove(text, range, ls + nw); }, - .line_down => return t.visualMove(text, range, true, go.count, edit.paneWrapWidth(p, t)), - .line_up => return t.visualMove(text, range, false, go.count, edit.paneWrapWidth(p, t)), + .line_down => return t.verticalMove(text, range, true, go.count, edit.paneWrapWidth(p, t)), + .line_up => return t.verticalMove(text, range, false, go.count, edit.paneWrapWidth(p, t)), .column => { const line = modal.lineAtOffset(text, cur); const ls = modal.lineStartOffset(text, line); @@ -740,8 +740,8 @@ fn executeNormalAction(p: *Pardes, t: *Text, semantic: modal.Normal.Action) void for (0..move.count) |_| target = modal.nextGrapheme(text, target); return t.pointMove(text, range, target); }, - .down => return t.verticalMove(text, range, true, move.count), - .up => return t.verticalMove(text, range, false, move.count), + .down => return t.verticalMove(text, range, true, move.count, 0), + .up => return t.verticalMove(text, range, false, move.count, 0), .next_word_start => return t.wordMove(text, range, move.count, .next_word_start), .prev_word_start => return t.wordMove(text, range, move.count, .prev_word_start), .next_word_end => return t.wordMove(text, range, move.count, .next_word_end), |
