From c909a2a50a405c3c6d71141f9bd97eaf28dd0839 Mon Sep 17 00:00:00 2001 From: Robert Burnett Date: Thu, 21 Nov 2024 17:57:17 -0600 Subject: [PATCH] feat: add more vim keybinds This is a combination of 6 commits. - added a new command - change dd to use cut - add prototypes for A I o O commands - fixed keybind test compilation bug - add keybinds for the new enter_mode commands - added prototype for copy line --- src/command.zig | 2 ++ src/keybind/builtin/vim.json | 16 +++++++-- src/keybind/keybind.zig | 4 ++- src/tui/mode/vim.zig | 68 ++++++++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 3 deletions(-) diff --git a/src/command.zig b/src/command.zig index 0f8d36c..efbad11 100644 --- a/src/command.zig +++ b/src/command.zig @@ -43,7 +43,9 @@ pub fn Closure(comptime T: type) type { f: FunT, data: T, + ///The type signature of commands const FunT: type = *const fn (T, ctx: Context) Result; + const Self = @This(); pub fn init(f: FunT, data: T, name: []const u8, meta: Metadata) Self { diff --git a/src/keybind/builtin/vim.json b/src/keybind/builtin/vim.json index 2b64a63..66d6de0 100644 --- a/src/keybind/builtin/vim.json +++ b/src/keybind/builtin/vim.json @@ -18,8 +18,16 @@ ["l", "move_right_vim"], ["h", "move_left"], ["", "move_right_vim"], + ["i", "enter_mode", "insert"], + ["a", "enter_mode_at_next_char", "insert"], + ["I", "enter_mode_at_line_begin", "insert"], + ["A", "enter_mode_at_line_end", "insert"], + ["o", "enter_mode_on_newline_down", "insert"], + ["O", "enter_mode_on_newline_up", "insert"], + ["v", "enter_mode", "visual"], + ["/", "find"], ["n", "goto_next_match"], ["0", "move_begin"], @@ -36,7 +44,10 @@ ["", "move_buffer_end"], ["d", "delete_to_end"], - ["d4", "delete_to_end"], + ["dd", "cut"], + ["dd", "delete_line"], + + ["yy", "copy_line"], ["", "move_scroll_page_up"], ["", "move_scroll_page_down"], @@ -58,7 +69,8 @@ ["jk", "enter_mode", "normal"], ["", "enter_mode", "normal"], ["", "delete_forward"], - ["", "delete_backward"] + ["", "delete_backward"], + ["", "insert_line_after"] ] } } diff --git a/src/keybind/keybind.zig b/src/keybind/keybind.zig index 48ff395..f29326f 100644 --- a/src/keybind/keybind.zig +++ b/src/keybind/keybind.zig @@ -325,7 +325,7 @@ const Binding = struct { pub const KeybindHints = std.StringHashMapUnmanaged([]u8); -const max_key_sequence_time_interval = 750; +const max_key_sequence_time_interval = 1500; const max_input_buffer_size = 4096; var globals: struct { @@ -678,6 +678,8 @@ const match_test_cases = .{ .{ "", "", .matched }, .{ "", "", .match_possible }, .{ "", "", .matched }, + .{ "dd", "dd", .matched }, + .{ "dd", "da", .match_impossible }, }; test "match" { diff --git a/src/tui/mode/vim.zig b/src/tui/mode/vim.zig index 34438a4..d13d16a 100644 --- a/src/tui/mode/vim.zig +++ b/src/tui/mode/vim.zig @@ -44,4 +44,72 @@ const cmds_ = struct { try cmd("quit_without_saving", .{}); } pub const @"wq!_meta" = .{ .description = "wq! (write file and quit without saving)" }; + + pub fn enter_mode_at_next_char(self: *void, ctx: Ctx) Result { + _ = self; // autofix + _ = ctx; // autofix + //TODO + return undefined; + } + + pub const enter_mode_at_next_char_meta = .{ .description = "Move forward one char and change mode" }; + + pub fn enter_mode_on_newline_down(self: *void, ctx: Ctx) Result { + _ = self; // autofix + _ = ctx; // autofix + //TODO + return undefined; + } + + pub const enter_mode_on_newline_down_meta = .{ .description = "Insert a newline and change mode" }; + + pub fn enter_mode_on_newline_up(self: *void, ctx: Ctx) Result { + _ = self; // autofix + _ = ctx; // autofix + //TODO + return undefined; + } + pub const enter_mode_on_newline_up_meta = .{ .description = "Insert a newline above the current line and change mode" }; + + pub fn enter_mode_at_line_begin(self: *void, ctx: Ctx) Result { + _ = self; // autofix + _ = ctx; // autofix + //TODO + return undefined; + } + + pub const enter_mode_at_line_begin_meta = .{ .description = "Goto line begin and change mode" }; + + pub fn enter_mode_at_line_end(self: *void, ctx: Ctx) Result { + _ = self; // autofix + _ = ctx; // autofix + //TODO + return undefined; + } + pub const enter_mode_at_line_end_meta = .{ .description = "Goto line end and change mode" }; + + pub fn copy_line(self: *void, ctx: Ctx) Result { + _ = self; // autofix + _ = ctx; // autofix + //TODO + return undefined; + } + + pub const copy_line_meta = .{ .description = "Copies the current line" }; + + pub fn delete_line(self: *void, ctx: Ctx) Result { + _ = self; // autofix + _ = ctx; // autofix + //TODO + return undefined; + //try self.move_begin(ctx); + //const b = try self.buf_for_update(); + //var root = try self.delete_to(move_cursor_end, b.root, b.allocator); + //root = try self.delete_to(move_cursor_right, b.root, b.allocator); + //try self.delete_forward(ctx); + //try self.update_buf(root); + //self.clamp(); + } + pub const delete_line_meta = .{ .description = "Delete the current line without copying" }; + };