Compare commits

...

6 commits

Author SHA1 Message Date
ivel.santos
6283d04442 Helix: fixing c keymap behavior 2025-04-13 14:18:54 +02:00
ivel.santos
66f94a40e9 Helix: normal mode after delete 2025-04-13 14:18:54 +02:00
ivel.santos
17f0faed68 Fix insert_line 2025-04-13 14:18:54 +02:00
ivel.santos
ccaeded0c8 Fix pasting line 2025-04-13 14:18:54 +02:00
ivel.santos
7778512c35 Correcting selection after paste 2025-04-10 06:42:58 +02:00
ivel.santos
e59cd32ed8 copy and paste improvs and inclusive selection correction 2025-04-10 06:42:58 +02:00
3 changed files with 101 additions and 13 deletions

View file

@ -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"],
@ -190,7 +190,7 @@
["n", "goto_next_match"],
["u", "undo"],
["y", "copy"],
["y", ["enable_selection"], ["copy_helix"], ["enter_mode", "normal"]],
["p", "paste_after"],
["q", "record_macro"],
@ -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"],
@ -468,7 +468,7 @@
["n", "goto_next_match"],
["u", "undo"],
["y", "copy"],
["y", ["copy_helix"], ["enter_mode", "normal"]],
["p", "paste_after"],
["q", "record_macro"],

View file

@ -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);
@ -1962,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;
@ -2499,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);
@ -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;
@ -3992,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();

View file

@ -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;
@ -92,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);
};
}
@ -229,6 +229,68 @@ const cmds_ = struct {
ed.clamp();
}
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;
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') != null and text[0] == '\n') {
for (ed.cursels.items) |*cursel_| if (cursel_.*) |*cursel| {
root = try insert_line(ed, root, cursel, 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 +323,28 @@ 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);
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;
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.* };
return root_;
}