diff --git a/src/keybind/builtin/vim.json b/src/keybind/builtin/vim.json index b069ed3..761744b 100644 --- a/src/keybind/builtin/vim.json +++ b/src/keybind/builtin/vim.json @@ -9,12 +9,15 @@ "name": "NORMAL", "line_numbers": "relative", "cursor": "block", + "selection": "normal", "press": [ ["b", "move_word_left_vim"], ["w", "move_word_right_vim"], + ["W", "move_word_right"], + ["B", "move_word_left"], ["e", "move_word_right_end_vim"], - ["x", "delete_forward"], - ["s", ["delete_forward"], ["enter_mode", "insert"]], + ["x", "cut_forward_internal"], + ["s", ["cut_forward_internal"], ["enter_mode", "insert"]], ["u", "undo"], ["j", "move_down"], @@ -31,15 +34,15 @@ ["O", ["smart_insert_line_before"], ["enter_mode", "insert"]], ["v", "enter_mode", "visual"], + ["V", ["move_begin"], ["enter_mode", "visual"], ["select_end"]], - ["/", "find"], ["n", "goto_next_match"], ["0", "move_begin"], + ["^", "smart_move_begin"], ["$", "move_end"], [":", "open_command_palette"], - ["p", "paste"], + ["p", "paste_internal_vim"], - ["gd", "goto_definition"], ["gi", "goto_implementation"], ["gy", "goto_type_definition"], ["gg", "move_buffer_begin"], @@ -48,10 +51,12 @@ ["G", "move_buffer_end"], ["d$", "delete_to_end"], - ["dd", "cut"], + ["dw", "cut_word_right_vim"], + ["db", "cut_word_left_vim"], + ["dd", "cut_internal_vim"], ["\"_dd", "delete_line"], - ["yy", "copy_line"], + ["yy", ["copy_line_internal_vim"], ["cancel"]], ["", "move_scroll_page_up"], ["", "move_scroll_page_down"], @@ -61,10 +66,12 @@ ["", "jump_forward"], ["", "redo"], + ["/", "find"], + ["", "TODO"], - ["", "smart_insert_line_after"], - ["", "smart_insert_line"] + ["", ["move_down"], ["move_begin"]], + ["", ["move_down"], ["move_begin"]] ] }, "visual": { @@ -72,13 +79,20 @@ "on_match_failure": "ignore", "name": "VISUAL", "line_numbers": "relative", - "cursor": "underline", + "cursor": "block", + "selection": "normal", "press": [ ["", "enter_mode", "normal"], ["k", "select_up"], ["j", "select_down"], ["h", "select_left"], - ["l", "select_right"] + ["l", "select_right"], + + ["y", ["copy_internal_vim"], ["cancel"], ["enter_mode", "normal"]], + + ["x", ["cut_forward_internal"], ["cancel"], ["enter_mode", "normal"]], + ["d", ["cut_forward_internal"], ["cancel"], ["enter_mode", "normal"]], + ["s", ["cut_forward_internal"], ["cancel"], ["enter_mode", "insert"]] ] }, "insert": { @@ -91,7 +105,10 @@ ["", "enter_mode", "normal"], ["", "delete_forward"], ["", "delete_backward"], - ["", "insert_line_after"] + ["", "smart_insert_line"], + + ["", "delete_word_left"], + [" 0) { + if (text.items.len > 100) { + self.logger.print("copy:{s}...", .{std.fmt.fmtSliceEscapeLower(text.items[0..100])}); + } else { + self.logger.print("copy:{s}", .{std.fmt.fmtSliceEscapeLower(text.items)}); + } + self.set_clipboard_internal(text.items); + } + } + pub const copy_internal_vim_meta = .{ .description = "Copy selection to internal clipboard (vim)" }; + + pub fn copy_line_internal_vim(self: *Self, _: Context) Result { + const primary = self.get_primary(); + const root = self.buf_root() catch return; + var first = true; + var text = std.ArrayList(u8).init(self.allocator); + try text.appendSlice("\n"); + if (primary.selection) |_| {} else { + const sel = primary.enable_selection(root, self.metrics) catch return; + try move_cursor_begin(root, &sel.begin, self.metrics); + try move_cursor_end(root, &sel.end, self.metrics); + try move_cursor_right(root, &sel.end, self.metrics); + } + for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel| { + if (cursel.selection) |sel| { + const copy_text = try copy_selection(root, sel, self.allocator, self.metrics); + if (first) { + first = false; + } else { + try text.appendSlice("\n"); + } + try text.appendSlice(copy_text); + } + }; + if (text.items.len > 0) { + if (text.items.len > 100) { + self.logger.print("copy:{s}...", .{std.fmt.fmtSliceEscapeLower(text.items[0..100])}); + } else { + self.logger.print("copy:{s}", .{std.fmt.fmtSliceEscapeLower(text.items)}); + } + self.set_clipboard_internal(text.items); + } + } + pub const copy_line_internal_vim_meta = .{ .description = "Copy line to internal clipboard (vim)" }; + pub fn paste(self: *Self, ctx: Context) Result { var text: []const u8 = undefined; if (!(ctx.args.buf.len > 0 and try ctx.args.match(.{tp.extract(&text)}))) { @@ -2508,6 +2649,51 @@ pub const Editor = struct { } pub const paste_meta = .{ .description = "Paste from internal clipboard" }; + pub fn paste_internal_vim(self: *Self, ctx: Context) Result { + var text: []const u8 = undefined; + if (!(ctx.args.buf.len > 0 and try ctx.args.match(.{tp.extract(&text)}))) { + if (self.clipboard) |text_| text = text_ else return; + } + + self.logger.print("paste: {d} bytes", .{text.len}); + const b = try self.buf_for_update(); + var root = b.root; + + if(std.mem.eql(u8, text[text.len-1..], "\n")) text = text[0..text.len-1]; + + if (std.mem.indexOfScalar(u8, text, '\n')) |idx| { + if(idx == 0) { + for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel| { + try move_cursor_end(root, &cursel.cursor, self.metrics); + root = try self.insert(root, cursel, "\n", b.allocator); + }; + text = text[1..]; + } + if (self.cursels.items.len == 1) { + const primary = self.get_primary(); + root = try self.insert_line_vim(root, primary, text, b.allocator); + } else { + for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel| { + root = try self.insert_line_vim(root, cursel, text, b.allocator); + }; + } + } else { + if (self.cursels.items.len == 1) { + const primary = self.get_primary(); + root = try self.insert(root, primary, text, b.allocator); + } else { + for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel| { + root = try self.insert(root, cursel, text, b.allocator); + }; + } + } + + try self.update_buf(root); + self.clamp(); + self.need_render(); + } + pub const paste_internal_vim_meta = .{ .description = "Paste from internal clipboard (vim)" }; + pub fn delete_forward(self: *Self, _: Context) Result { const b = try self.buf_for_update(); const root = try self.delete_to(move_cursor_right, b.root, b.allocator); @@ -2516,6 +2702,15 @@ pub const Editor = struct { } pub const delete_forward_meta = .{ .description = "Delete next character" }; + pub fn cut_forward_internal(self: *Self, _: Context) Result { + const b = try self.buf_for_update(); + const text, const root= try self.cut_to(move_cursor_right, b.root); + self.set_clipboard_internal(text); + try self.update_buf(root); + self.clamp(); + } + pub const cut_forward_internal_meta = .{ .description = "Cut next character to internal clipboard" }; + pub fn delete_backward(self: *Self, _: Context) Result { const b = try self.buf_for_update(); const root = try self.delete_to(move_cursor_left, b.root, b.allocator); @@ -2532,6 +2727,15 @@ pub const Editor = struct { } pub const delete_word_left_meta = .{ .description = "Delete previous word" }; + pub fn cut_word_left_vim(self: *Self, _: Context) Result { + const b = try self.buf_for_update(); + const text, const root= try self.cut_to(move_cursor_word_left_vim, b.root); + self.set_clipboard_internal(text); + try self.update_buf(root); + self.clamp(); + } + pub const cut_word_left_vim_meta = .{ .description = "Cut next character to internal clipboard" }; + pub fn delete_word_right(self: *Self, _: Context) Result { const b = try self.buf_for_update(); const root = try self.delete_to(move_cursor_word_right_space, b.root, b.allocator); @@ -2540,6 +2744,15 @@ pub const Editor = struct { } pub const delete_word_right_meta = .{ .description = "Delete next word" }; + pub fn cut_word_right_vim(self: *Self, _: Context) Result { + const b = try self.buf_for_update(); + const text, const root= try self.cut_to(move_cursor_word_right_vim, b.root); + self.set_clipboard_internal(text); + try self.update_buf(root); + self.clamp(); + } + pub const cut_word_right_vim_meta = .{ .description = "Cut next character to internal clipboard" }; + pub fn delete_to_begin(self: *Self, _: Context) Result { const b = try self.buf_for_update(); const root = try self.delete_to(move_cursor_begin, b.root, b.allocator);