summaryrefslogtreecommitdiff
path: root/core/normal.zig
diff options
context:
space:
mode:
Diffstat (limited to 'core/normal.zig')
-rw-r--r--core/normal.zig52
1 files changed, 42 insertions, 10 deletions
diff --git a/core/normal.zig b/core/normal.zig
index 89fd72f6..61f8f26b 100644
--- a/core/normal.zig
+++ b/core/normal.zig
@@ -946,14 +946,31 @@ fn verticalMove(tab: u16, t: *Text, text: []const u8, range: modal.Selection, do
t.sticky_col = @intCast(goal);
}
-/// Ctrl-d/u: scroll half a page AND move the cursor by the same rows
+/// Ctrl-d/u: the view half the body's rows on, and the cursor where it
+/// stood on the screen (inside the margins): on the line whose first row is
+/// now there, the next one the way it went past a wrapped line's later
+/// row, and the rows the view could not move (the text's end) further.
+/// None in the body (inside a line taller than it): it stays, off the
+/// view, and the view where it went. A terminal's rows are its lines: both
+/// move by them.
fn halfPageMove(tab: u16, 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(tab, t, text, range, down, @intCast(half), 0);
+ if (w.file == null) {
+ w.scrollBy(tab, if (down) half else -half);
+ return verticalMove(tab, t, text, range, down, @intCast(half), 0);
+ }
+ const body: i64 = File.shownRows(tab, w);
+ const margin = @min(config.scroll_off, @divTrunc(body - 1, 2));
+ const from = File.top(tab, w);
+ const line: usize = @intCast(@max(0, t.cur_row));
+ const on: i64 = if (File.rowsTo(tab, w, from, line, @intCast(body))) |d| d + File.rowOf(tab, w, line, @intCast(@max(0, t.cur_col))) else if (line < from.line) 0 else body - 1;
+ const moved = File.scrollRows(tab, w, if (down) half else -half);
+ const to = File.textRowLine(tab, w, on + (if (down) half else -half) - moved, margin, if (down) .down else .up) orelse return;
+ if (to == t.cur_row) return;
+ verticalMove(tab, t, text, range, to > t.cur_row, @abs(to - t.cur_row), 0);
}
/// gt/gb: view-relative rows, col 0, scrolloff clamped (helix goto_window)
@@ -1235,13 +1252,28 @@ fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) edit.Error!
// 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
.view_top, .view_center, .view_bottom => if (t.what == .body) {
- const above: i32 = switch (a.role) {
- .view_top => 0,
- .view_center => @divTrunc(@as(i32, pane.rows) - 1, 2),
- else => @as(i32, pane.rows) - 1,
- };
- pane.scrollBy(t.cur_row - (pane.scroll() + above));
- pane.ensureCursorVisible(p.settings.tab);
+ // In rows: zt shows the cursor's line from its first row, a
+ // line taller than the body from the cursor's.
+ const tab = p.settings.tab;
+ if (pane.file == null) {
+ const above: i32 = switch (a.role) {
+ .view_top => 0,
+ .view_center => @divTrunc(@as(i32, pane.rows) - 1, 2),
+ else => @as(i32, pane.rows) - 1,
+ };
+ pane.scrollBy(tab, t.cur_row - (pane.scroll() + above));
+ } else {
+ const body: i64 = File.shownRows(tab, pane);
+ const line: usize = @intCast(@max(0, t.cur_row));
+ const row = File.rowOf(tab, pane, line, @intCast(@max(0, t.cur_col)));
+ const at: File.Top = .{ .line = line, .row = if (a.role == .view_top and row < body) 0 else row };
+ File.setTop(pane, File.walkRows(tab, pane, at, switch (a.role) {
+ .view_top => 0,
+ .view_center => -@divTrunc(body - 1, 2),
+ else => -(body - 1),
+ }).to);
+ }
+ pane.ensureCursorVisible(tab);
},
// helix align_view_middle: the cursor's column to the middle of an
// unwrapped view