feat: add move_left_vim and move_right_vim commands

closes #3
This commit is contained in:
CJ van den Berg 2024-04-09 23:01:16 +02:00
parent b8261f924e
commit 053bf19110
2 changed files with 43 additions and 5 deletions

View file

@ -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);

View file

@ -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", .{}),