diff --git a/src/keybind/builtin/helix.json b/src/keybind/builtin/helix.json index 1225b17..c012290 100644 --- a/src/keybind/builtin/helix.json +++ b/src/keybind/builtin/helix.json @@ -190,7 +190,7 @@ ["n", "goto_next_match"], ["u", "undo"], - ["y", "copy"], + ["y", ["enable_selection"], ["copy_internal_vim"], ["enter_mode", "normal"]], ["p", "paste_after"], ["q", "record_macro"], @@ -468,7 +468,7 @@ ["n", "goto_next_match"], ["u", "undo"], - ["y", "copy"], + ["y", ["copy_internal_vim"], ["enter_mode", "normal"]], ["p", "paste_after"], ["q", "record_macro"], diff --git a/src/tui/editor.zig b/src/tui/editor.zig index 96d3f13..23b7837 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -118,6 +118,7 @@ pub const CurSel = struct { else cod: { self.selection = Selection.from_cursor(&self.cursor); try self.selection.?.end.move_right(root, metrics); + try self.cursor.move_right(root, metrics); break :cod &self.selection.?; }; } @@ -490,7 +491,7 @@ pub const Editor = struct { .none; } - fn need_render(_: *Self) void { + pub fn need_render(_: *Self) void { Widget.need_render(); } @@ -1942,7 +1943,7 @@ pub const Editor = struct { self.collapse_cursors(); } - fn nudge_insert(self: *Self, nudge: Selection, exclude: *const CurSel, _: usize) void { + pub fn nudge_insert(self: *Self, nudge: Selection, exclude: *const CurSel, _: usize) void { for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel| if (cursel != exclude) cursel.nudge_insert(nudge); @@ -2536,7 +2537,7 @@ pub const Editor = struct { try move_cursor_buffer_end(root, &sel.end, metrics); } - fn insert(self: *Self, root: Buffer.Root, cursel: *CurSel, s: []const u8, allocator: Allocator) !Buffer.Root { + pub fn insert(self: *Self, root: Buffer.Root, cursel: *CurSel, s: []const u8, allocator: Allocator) !Buffer.Root { var root_ = if (cursel.selection) |_| try self.delete_selection(root, cursel, allocator) else root; const cursor = &cursel.cursor; const begin = cursel.cursor; @@ -2546,7 +2547,7 @@ pub const Editor = struct { return root_; } - fn insert_line_vim(self: *Self, root: Buffer.Root, cursel: *CurSel, s: []const u8, allocator: Allocator) !Buffer.Root { + pub fn insert_line_vim(self: *Self, root: Buffer.Root, cursel: *CurSel, s: []const u8, allocator: Allocator) !Buffer.Root { var root_ = if (cursel.selection) |_| try self.delete_selection(root, cursel, allocator) else root; const cursor = &cursel.cursor; const begin = cursel.cursor; diff --git a/src/tui/mode/helix.zig b/src/tui/mode/helix.zig index bca1dfe..63e7589 100644 --- a/src/tui/mode/helix.zig +++ b/src/tui/mode/helix.zig @@ -7,6 +7,7 @@ const cmd = command.executeName; const tui = @import("../tui.zig"); const Editor = @import("../editor.zig").Editor; +const CurSel = @import("../editor.zig").CurSel; const Buffer = @import("Buffer"); const Cursor = Buffer.Cursor; const Selection = Buffer.Selection; @@ -229,6 +230,54 @@ const cmds_ = struct { ed.clamp(); } pub const select_to_char_right_helix_meta: Meta = .{ .description = "Move to char right" }; + + pub fn paste_after(_: *void, ctx: Ctx) Result { + const mv = tui.mainview() orelse return; + const ed = mv.get_active_editor() orelse return; + + var text: []const u8 = undefined; + if (!(ctx.args.buf.len > 0 and try ctx.args.match(.{tp.extract(&text)}))) { + if (ed.clipboard) |text_| text = text_ else return; + } + + ed.logger.print("paste: {d} bytes", .{text.len}); + const b = try ed.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 (ed.cursels.items) |*cursel_| if (cursel_.*) |*cursel| { + try Editor.move_cursor_end(root, &cursel.cursor, ed.metrics); + root = try ed.insert(root, cursel, "\n", b.allocator); + }; + text = text[1..]; + } + if (ed.cursels.items.len == 1) { + const primary = ed.get_primary(); + root = try ed.insert_line_vim(root, primary, text, b.allocator); + } else { + for (ed.cursels.items) |*cursel_| if (cursel_.*) |*cursel| { + root = try ed.insert_line_vim(root, cursel, text, b.allocator); + }; + } + } else { + if (ed.cursels.items.len == 1) { + const primary = ed.get_primary(); + root = try insert(ed, root, primary, text, b.allocator); + } else { + for (ed.cursels.items) |*cursel_| if (cursel_.*) |*cursel| { + root = try insert(ed, root, cursel, text, b.allocator); + }; + } + } + + try ed.update_buf(root); + ed.clamp(); + ed.need_render(); + } + pub const paste_after_meta: Meta = .{ .description = "Paste from clipboard after selection" }; }; fn move_cursor_word_left_helix(root: Buffer.Root, cursor: *Cursor, metrics: Buffer.Metrics) error{Stop}!void { @@ -261,3 +310,14 @@ fn move_cursor_word_right_end_helix(root: Buffer.Root, cursor: *Cursor, metrics: Editor.move_cursor_right_until(root, cursor, Editor.is_word_boundary_right_vim, metrics); try cursor.move_right(root, metrics); } + +fn insert(ed: *Editor, root: Buffer.Root, cursel: *CurSel, s: []const u8, allocator: std.mem.Allocator) !Buffer.Root { + var root_ = root; + const cursor = &cursel.cursor; + if (cursel.selection == null) try cursor.move_right(root_, ed.metrics); + const begin = cursel.cursor; + cursor.row, cursor.col, root_ = try root_.insert_chars(cursor.row, cursor.col, s, allocator, ed.metrics); + cursor.target = cursor.col; + ed.nudge_insert(.{ .begin = begin, .end = cursor.* }, cursel, s.len); + return root_; +}