From a21f0e6ac52cab6f821647c17b892c25a271fad6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Pedro?= Date: Thu, 9 Jan 2025 22:17:43 -0300 Subject: [PATCH] Fix half and whole page movements --- src/buffer/Cursor.zig | 18 +++++++++ src/keybind/builtin/helix.json | 12 +++--- src/tui/editor.zig | 67 ++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 6 deletions(-) diff --git a/src/buffer/Cursor.zig b/src/buffer/Cursor.zig index a401fb1..8c68265 100644 --- a/src/buffer/Cursor.zig +++ b/src/buffer/Cursor.zig @@ -103,6 +103,24 @@ pub fn move_page_down(self: *Self, root: Buffer.Root, view: *const View, metrics self.move_right_no_target(root, metrics) catch return; } +pub fn move_half_page_up(self: *Self, root: Buffer.Root, view: *const View, metrics: Metrics) void { + const half_view_rows = @divTrunc(view.rows, 2); + self.row = if (self.row > half_view_rows) self.row - half_view_rows else 0; + self.follow_target(root, metrics); + self.move_left_no_target(root, metrics) catch return; + self.move_right_no_target(root, metrics) catch return; +} + +pub fn move_half_page_down(self: *Self, root: Buffer.Root, view: *const View, metrics: Metrics) void { + const half_view_rows = @divTrunc(view.rows, 2); + if (root.lines() > self.row + half_view_rows) { + self.row += half_view_rows; + } else self.move_buffer_last(root, metrics); + self.follow_target(root, metrics); + self.move_left_no_target(root, metrics) catch return; + self.move_right_no_target(root, metrics) catch return; +} + pub fn move_to(self: *Self, root: Buffer.Root, row: usize, col: usize, metrics: Metrics) !void { if (row < root.lines()) { self.row = row; diff --git a/src/keybind/builtin/helix.json b/src/keybind/builtin/helix.json index 1a85aad..bcd1b74 100644 --- a/src/keybind/builtin/helix.json +++ b/src/keybind/builtin/helix.json @@ -11,8 +11,8 @@ "press": [ ["ctrl+b", "move_scroll_page_up"], ["ctrl+f", "move_scroll_page_down"], - ["ctrl+u", "page_cursor_half_up"], - ["ctrl+d", "page_cursor_half_down"], + ["ctrl+u", "move_scroll_half_page_up"], + ["ctrl+d", "move_scroll_half_page_down"], ["ctrl+c", "toggle_comment"], ["ctrl+i", "jump_forward"], ["ctrl+o", "jump_back"], @@ -253,10 +253,10 @@ "line_numbers": "relative", "cursor": "block", "press": [ - ["ctrl+b", "move_scroll_page_up"], - ["ctrl+f", "move_scroll_page_down"], - ["ctrl+u", "page_cursor_half_up"], - ["ctrl+d", "page_cursor_half_down"], + ["ctrl+b", "select_page_up"], + ["ctrl+f", "select_page_down"], + ["ctrl+u", "select_half_page_up"], + ["ctrl+d", "select_half_page_down"], ["ctrl+c", "toggle_comment"], diff --git a/src/tui/editor.zig b/src/tui/editor.zig index a87ff25..c61220b 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -1955,6 +1955,14 @@ pub const Editor = struct { cursor.move_page_down(root, view, metrics); } + fn move_cursor_half_page_up(root: Buffer.Root, cursor: *Cursor, view: *const View, metrics: Buffer.Metrics) !void { + cursor.move_half_page_up(root, view, metrics); + } + + fn move_cursor_half_page_down(root: Buffer.Root, cursor: *Cursor, view: *const View, metrics: Buffer.Metrics) !void { + cursor.move_half_page_down(root, view, metrics); + } + pub fn primary_click(self: *Self, y: c_int, x: c_int) !void { if (self.fast_scroll) try self.push_cursor() @@ -2889,6 +2897,30 @@ pub const Editor = struct { } pub const move_scroll_page_down_meta = .{ .description = "Move and scroll page down" }; + pub fn move_scroll_half_page_up(self: *Self, _: Context) Result { + if (self.screen_cursor(&self.get_primary().cursor)) |cursor| { + const root = try self.buf_root(); + self.with_cursors_and_view_const(root, move_cursor_half_page_up, &self.view) catch {}; + const new_cursor_row = self.get_primary().cursor.row; + self.update_scroll_dest_abs(if (cursor.row > new_cursor_row) 0 else new_cursor_row - cursor.row); + } else { + return self.move_half_page_up(.{}); + } + } + pub const move_scroll_half_page_up_meta = .{ .description = "Move and scroll half a page up" }; + + pub fn move_scroll_half_page_down(self: *Self, _: Context) Result { + if (self.screen_cursor(&self.get_primary().cursor)) |cursor| { + const root = try self.buf_root(); + self.with_cursors_and_view_const(root, move_cursor_half_page_down, &self.view) catch {}; + const new_cursor_row = self.get_primary().cursor.row; + self.update_scroll_dest_abs(if (cursor.row > new_cursor_row) 0 else new_cursor_row - cursor.row); + } else { + return self.move_half_page_down(.{}); + } + } + pub const move_scroll_half_page_down_meta = .{ .description = "Move and scroll half a page down" }; + pub fn smart_move_begin(self: *Self, _: Context) Result { const root = try self.buf_root(); try self.with_cursors_const(root, smart_move_cursor_begin); @@ -2927,6 +2959,23 @@ pub const Editor = struct { } pub const move_page_down_meta = .{ .description = "Move cursor page down" }; + pub fn move_half_page_up(self: *Self, _: Context) Result { + try self.send_editor_jump_source(); + const root = try self.buf_root(); + try self.with_cursors_and_view_const(root, move_cursor_page_up, &self.view); + self.clamp(); + } + pub const move_half_page_up_meta = .{ .description = "Move cursor half a page up" }; + + pub fn move_half_page_down(self: *Self, _: Context) Result { + try self.send_editor_jump_source(); + const root = try self.buf_root(); + try self.with_cursors_and_view_const(root, move_cursor_page_down, &self.view); + self.clamp(); + try self.send_editor_jump_destination(); + } + pub const move_half_page_down_meta = .{ .description = "Move cursor half a page down" }; + pub fn move_buffer_begin(self: *Self, _: Context) Result { try self.send_editor_jump_source(); self.cancel_all_selections(); @@ -3095,6 +3144,24 @@ pub const Editor = struct { } pub const select_page_down_meta = .{ .description = "Select page down" }; + pub fn select_half_page_up(self: *Self, _: Context) Result { + try self.send_editor_jump_source(); + const root = try self.buf_root(); + try self.with_selections_and_view_const(root, move_cursor_half_page_up, &self.view); + self.clamp(); + try self.send_editor_jump_destination(); + } + pub const select_half_page_up_meta = .{ .description = "Select half a page up" }; + + pub fn select_half_page_down(self: *Self, _: Context) Result { + try self.send_editor_jump_source(); + const root = try self.buf_root(); + try self.with_selections_and_view_const(root, move_cursor_half_page_down, &self.view); + self.clamp(); + try self.send_editor_jump_destination(); + } + pub const select_half_page_down_meta = .{ .description = "Select half a page down" }; + pub fn select_all(self: *Self, _: Context) Result { try self.send_editor_jump_source(); self.cancel_all_selections();