From e59cd32ed8900503af5b3c78d0bda82071259707 Mon Sep 17 00:00:00 2001 From: "ivel.santos" Date: Wed, 9 Apr 2025 19:36:07 -0300 Subject: [PATCH 1/6] copy and paste improvs and inclusive selection correction --- src/keybind/builtin/helix.json | 4 +-- src/tui/editor.zig | 9 ++--- src/tui/mode/helix.zig | 60 ++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 6 deletions(-) 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_; +} From 7778512c35a5af18af8b2d99f9c306e741aa7b1d Mon Sep 17 00:00:00 2001 From: "ivel.santos" Date: Wed, 9 Apr 2025 21:16:41 -0300 Subject: [PATCH 2/6] Correcting selection after paste --- src/tui/editor.zig | 4 ++-- src/tui/mode/helix.zig | 21 +++++++++++++++------ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/tui/editor.zig b/src/tui/editor.zig index 23b7837..fbd5740 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -1963,7 +1963,7 @@ pub const Editor = struct { }; } - fn delete_selection(self: *Self, root: Buffer.Root, cursel: *CurSel, allocator: Allocator) error{Stop}!Buffer.Root { + pub fn delete_selection(self: *Self, root: Buffer.Root, cursel: *CurSel, allocator: Allocator) error{Stop}!Buffer.Root { var sel: Selection = cursel.selection orelse return error.Stop; sel.normalize(); cursel.cursor = sel.begin; @@ -3993,7 +3993,7 @@ pub const Editor = struct { cursel.cursor = sel.end; } - fn select_line_around_cursor(self: *Self, cursel: *CurSel) !void { + pub fn select_line_around_cursor(self: *Self, cursel: *CurSel) !void { const root = try self.buf_root(); const sel = try cursel.enable_selection(root, self.metrics); sel.normalize(); diff --git a/src/tui/mode/helix.zig b/src/tui/mode/helix.zig index 63e7589..4588a2c 100644 --- a/src/tui/mode/helix.zig +++ b/src/tui/mode/helix.zig @@ -93,9 +93,8 @@ const cmds_ = struct { try Editor.move_cursor_begin(root, &sel.begin, ed.metrics); try Editor.move_cursor_end(root, &sel.end, ed.metrics); + try Editor.move_cursor_right(root, &sel.end, ed.metrics); cursel.cursor = sel.end; - try cursel.selection.?.end.move_right(root, ed.metrics); - try cursel.cursor.move_right(root, ed.metrics); }; } @@ -244,8 +243,6 @@ const cmds_ = struct { 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| { @@ -256,10 +253,10 @@ const cmds_ = struct { } if (ed.cursels.items.len == 1) { const primary = ed.get_primary(); - root = try ed.insert_line_vim(root, primary, text, b.allocator); + root = try insert_line(ed, 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); + root = try insert_line(ed, root, cursel, text, b.allocator); }; } } else { @@ -319,5 +316,17 @@ fn insert(ed: *Editor, root: Buffer.Root, cursel: *CurSel, s: []const u8, alloca 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); + cursel.selection = Selection{ .begin = begin, .end = cursor.* }; + return root_; +} + +fn insert_line(ed: *Editor, root: Buffer.Root, cursel: *CurSel, s: []const u8, allocator: std.mem.Allocator) !Buffer.Root { + var root_ = root; + const cursor = &cursel.cursor; + const begin = cursel.cursor; + _, _, 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); + cursel.selection = Selection{ .begin = begin, .end = cursor.* }; return root_; } From ccaeded0c86cfbdb5deeea1afa0bdfc7eca0a0d2 Mon Sep 17 00:00:00 2001 From: "ivel.santos" Date: Thu, 10 Apr 2025 17:41:37 -0300 Subject: [PATCH 3/6] Fix pasting line --- src/keybind/builtin/helix.json | 4 +-- src/tui/editor.zig | 2 +- src/tui/mode/helix.zig | 53 +++++++++++++++++++++++++++------- 3 files changed, 45 insertions(+), 14 deletions(-) diff --git a/src/keybind/builtin/helix.json b/src/keybind/builtin/helix.json index c012290..a9ddeec 100644 --- a/src/keybind/builtin/helix.json +++ b/src/keybind/builtin/helix.json @@ -190,7 +190,7 @@ ["n", "goto_next_match"], ["u", "undo"], - ["y", ["enable_selection"], ["copy_internal_vim"], ["enter_mode", "normal"]], + ["y", ["enable_selection"], ["copy_helix"], ["enter_mode", "normal"]], ["p", "paste_after"], ["q", "record_macro"], @@ -468,7 +468,7 @@ ["n", "goto_next_match"], ["u", "undo"], - ["y", ["copy_internal_vim"], ["enter_mode", "normal"]], + ["y", ["copy_helix"], ["enter_mode", "normal"]], ["p", "paste_after"], ["q", "record_macro"], diff --git a/src/tui/editor.zig b/src/tui/editor.zig index fbd5740..bb3da72 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -2500,7 +2500,7 @@ pub const Editor = struct { self.clipboard = text; } - fn copy_selection(root: Buffer.Root, sel: Selection, text_allocator: Allocator, metrics: Buffer.Metrics) ![]u8 { + pub fn copy_selection(root: Buffer.Root, sel: Selection, text_allocator: Allocator, metrics: Buffer.Metrics) ![]u8 { var size: usize = 0; _ = try root.get_range(sel, null, &size, null, metrics); const buf__ = try text_allocator.alloc(u8, size); diff --git a/src/tui/mode/helix.zig b/src/tui/mode/helix.zig index 4588a2c..4af2ac3 100644 --- a/src/tui/mode/helix.zig +++ b/src/tui/mode/helix.zig @@ -230,6 +230,37 @@ const cmds_ = struct { } pub const select_to_char_right_helix_meta: Meta = .{ .description = "Move to char right" }; + pub fn copy_helix(_: *void, _: Ctx) Result { + const mv = tui.mainview() orelse return; + const ed = mv.get_active_editor() orelse return; + const root = ed.buf_root() catch return; + var first = true; + var text = std.ArrayList(u8).init(ed.allocator); + + if (ed.get_primary().selection) |sel| if (sel.begin.col == 0 and sel.end.row > sel.begin.row) try text.appendSlice("\n"); + + for (ed.cursels.items) |*cursel_| if (cursel_.*) |*cursel| { + if (cursel.selection) |sel| { + const copy_text = try Editor.copy_selection(root, sel, ed.allocator, ed.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) { + ed.logger.print("copy:{s}...", .{std.fmt.fmtSliceEscapeLower(text.items[0..100])}); + } else { + ed.logger.print("copy:{s}", .{std.fmt.fmtSliceEscapeLower(text.items)}); + } + ed.set_clipboard_internal(text.items); + } + } + pub const copy_helix_meta: Meta = .{ .description = "Copy selection to clipboard (helix)" }; + pub fn paste_after(_: *void, ctx: Ctx) Result { const mv = tui.mainview() orelse return; const ed = mv.get_active_editor() orelse return; @@ -243,14 +274,9 @@ const cmds_ = struct { const b = try ed.buf_for_update(); var root = b.root; - 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 (std.mem.eql(u8, text[text.len - 1 ..], "\n")) text = text[0 .. text.len - 1]; + + if (std.mem.indexOfScalar(u8, text, '\n')) |_| { if (ed.cursels.items.len == 1) { const primary = ed.get_primary(); root = try insert_line(ed, root, primary, text, b.allocator); @@ -323,10 +349,15 @@ fn insert(ed: *Editor, root: Buffer.Root, cursel: *CurSel, s: []const u8, alloca fn insert_line(ed: *Editor, root: Buffer.Root, cursel: *CurSel, s: []const u8, allocator: std.mem.Allocator) !Buffer.Root { var root_ = root; const cursor = &cursel.cursor; - const begin = cursel.cursor; - _, _, root_ = try root_.insert_chars(cursor.row, cursor.col, s, allocator, ed.metrics); + + cursel.disable_selection(root, ed.metrics); + cursel.cursor.move_end(root, ed.metrics); + + var begin = cursel.cursor; + try begin.move_right(root, ed.metrics); + + 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); cursel.selection = Selection{ .begin = begin, .end = cursor.* }; return root_; } From 17f0faed68d14fceaf2847e516e67250fb7a6d19 Mon Sep 17 00:00:00 2001 From: "ivel.santos" Date: Thu, 10 Apr 2025 18:28:27 -0300 Subject: [PATCH 4/6] Fix insert_line --- src/tui/mode/helix.zig | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/src/tui/mode/helix.zig b/src/tui/mode/helix.zig index 4af2ac3..84eaffe 100644 --- a/src/tui/mode/helix.zig +++ b/src/tui/mode/helix.zig @@ -276,24 +276,14 @@ const cmds_ = struct { if (std.mem.eql(u8, text[text.len - 1 ..], "\n")) text = text[0 .. text.len - 1]; - if (std.mem.indexOfScalar(u8, text, '\n')) |_| { - if (ed.cursels.items.len == 1) { - const primary = ed.get_primary(); - root = try insert_line(ed, root, primary, text, b.allocator); - } else { - for (ed.cursels.items) |*cursel_| if (cursel_.*) |*cursel| { - root = try insert_line(ed, root, cursel, text, b.allocator); - }; - } + if (std.mem.indexOfScalar(u8, text, '\n') != null and text[0] == '\n') { + for (ed.cursels.items) |*cursel_| if (cursel_.*) |*cursel| { + root = try insert_line(ed, 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); - }; - } + for (ed.cursels.items) |*cursel_| if (cursel_.*) |*cursel| { + root = try insert(ed, root, cursel, text, b.allocator); + }; } try ed.update_buf(root); @@ -349,13 +339,10 @@ fn insert(ed: *Editor, root: Buffer.Root, cursel: *CurSel, s: []const u8, alloca fn insert_line(ed: *Editor, root: Buffer.Root, cursel: *CurSel, s: []const u8, allocator: std.mem.Allocator) !Buffer.Root { var root_ = root; const cursor = &cursel.cursor; - cursel.disable_selection(root, ed.metrics); cursel.cursor.move_end(root, ed.metrics); - var begin = cursel.cursor; try begin.move_right(root, ed.metrics); - cursor.row, cursor.col, root_ = try root_.insert_chars(cursor.row, cursor.col, s, allocator, ed.metrics); cursor.target = cursor.col; cursel.selection = Selection{ .begin = begin, .end = cursor.* }; From 66f94a40e964b928456f4294d2a0797a3607da5c Mon Sep 17 00:00:00 2001 From: "ivel.santos" Date: Thu, 10 Apr 2025 18:34:20 -0300 Subject: [PATCH 5/6] Helix: normal mode after delete --- src/keybind/builtin/helix.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/keybind/builtin/helix.json b/src/keybind/builtin/helix.json index a9ddeec..fbc9fb3 100644 --- a/src/keybind/builtin/helix.json +++ b/src/keybind/builtin/helix.json @@ -422,7 +422,7 @@ ["a", ["enter_mode", "insert"], ["move_right"]], ["o", ["enter_mode", "insert"], ["smart_insert_line_after"]], - ["d", "cut"], + ["d", ["cut"], ["enter_mode", "normal"]], ["c", ["enter_mode", "insert"], ["cut"]], ["s", "select_regex"], From 6283d04442aad23468457b42e30a485b88c45218 Mon Sep 17 00:00:00 2001 From: "ivel.santos" Date: Fri, 11 Apr 2025 19:15:08 -0300 Subject: [PATCH 6/6] Helix: fixing c keymap behavior --- src/keybind/builtin/helix.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/keybind/builtin/helix.json b/src/keybind/builtin/helix.json index fbc9fb3..59c9a92 100644 --- a/src/keybind/builtin/helix.json +++ b/src/keybind/builtin/helix.json @@ -147,7 +147,7 @@ ["o", ["enter_mode", "insert"], ["smart_insert_line_after"]], ["d", "cut_forward_internal_inclusive"], - ["c", ["enter_mode", "insert"], ["cut_forward_internal_inclusive"]], + ["c", ["enable_selection"], ["enter_mode", "insert"], ["cut_forward_internal_inclusive"]], ["s", "select_regex"], [";", "collapse_selections"],