From 053bf1911085e38a7ee45a1e889524f1807adeed Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Tue, 9 Apr 2024 23:01:16 +0200 Subject: [PATCH] feat: add move_left_vim and move_right_vim commands closes #3 --- src/tui/editor.zig | 38 +++++++++++++++++++++++++++++++ src/tui/mode/input/vim/normal.zig | 10 ++++---- 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/src/tui/editor.zig b/src/tui/editor.zig index 335217a..f8ac4ed 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -1478,6 +1478,14 @@ pub const Editor = struct { return false; } + fn is_eol_right_vim(root: Buffer.Root, cursor: *const Cursor) bool { + const line_width = root.line_width(cursor.row) catch return true; + if (line_width == 0) return true; + if (cursor.col >= line_width - 1) + return true; + return false; + } + fn move_cursor_left(root: Buffer.Root, cursor: *Cursor) error{Stop}!void { try cursor.move_left(root); } @@ -1487,6 +1495,11 @@ pub const Editor = struct { move_cursor_left(root, cursor) catch return; } + fn move_cursor_left_unless(root: Buffer.Root, cursor: *Cursor, pred: cursor_predicate) void { + if (!pred(root, cursor)) + move_cursor_left(root, cursor) catch return; + } + fn move_cursor_begin(_: Buffer.Root, cursor: *Cursor) !void { cursor.move_begin(); } @@ -1505,6 +1518,11 @@ pub const Editor = struct { move_cursor_right(root, cursor) catch return; } + fn move_cursor_right_unless(root: Buffer.Root, cursor: *Cursor, pred: cursor_predicate) void { + if (!pred(root, cursor)) + move_cursor_right(root, cursor) catch return; + } + fn move_cursor_end(root: Buffer.Root, cursor: *Cursor) !void { cursor.move_end(root); } @@ -1928,6 +1946,26 @@ pub const Editor = struct { self.clamp(); } + fn move_cursor_left_vim(root: Buffer.Root, cursor: *Cursor) error{Stop}!void { + move_cursor_left_unless(root, cursor, is_eol_left); + } + + fn move_cursor_right_vim(root: Buffer.Root, cursor: *Cursor) error{Stop}!void { + move_cursor_right_unless(root, cursor, is_eol_right_vim); + } + + pub fn move_left_vim(self: *Self, _: command.Context) tp.result { + const root = self.buf_root() catch |e| return tp.exit_error(e); + self.with_cursors_const(root, move_cursor_left_vim) catch {}; + self.clamp(); + } + + pub fn move_right_vim(self: *Self, _: command.Context) tp.result { + const root = self.buf_root() catch |e| return tp.exit_error(e); + self.with_cursors_const(root, move_cursor_right_vim) catch {}; + self.clamp(); + } + fn move_cursor_word_begin(root: Buffer.Root, cursor: *Cursor) error{Stop}!void { if (is_non_word_char_at_cursor(root, cursor)) { move_cursor_left_until(root, cursor, is_word_boundary_right); diff --git a/src/tui/mode/input/vim/normal.zig b/src/tui/mode/input/vim/normal.zig index b006d31..c9669ac 100644 --- a/src/tui/mode/input/vim/normal.zig +++ b/src/tui/mode/input/vim/normal.zig @@ -224,11 +224,11 @@ fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) tp.result { '/' => self.cmd("enter_find_mode", .{}), 'n' => self.cmd("goto_next_match", .{}), - 'h' => self.cmd_count("move_left", .{}), + 'h' => self.cmd_count("move_left_vim", .{}), 'j' => self.cmd_count("move_down", .{}), 'k' => self.cmd_count("move_up", .{}), - 'l' => self.cmd_count("move_right", .{}), - ' ' => self.cmd_count("move_right", .{}), + 'l' => self.cmd_count("move_right_vim", .{}), + ' ' => self.cmd_count("move_right_vim", .{}), 'b' => self.cmd_count("move_word_left", .{}), 'w' => self.cmd_count("move_word_right_vim", .{}), @@ -260,8 +260,8 @@ fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) tp.result { 'p' => self.cmd("paste", .{}), 'o' => self.seq(.{ "smart_insert_line_after", "enter_mode" }, command.fmt(.{"vim/insert"})), - key.LEFT => self.cmd("move_left", .{}), - key.RIGHT => self.cmd("move_right", .{}), + key.LEFT => self.cmd("move_left_vim", .{}), + key.RIGHT => self.cmd("move_right_vim", .{}), key.UP => self.cmd("move_up", .{}), key.DOWN => self.cmd("move_down", .{}), key.HOME => self.cmd("smart_move_begin", .{}),