From 411b26d4aad9e731c899930e657e205e27337569 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Igor=20T=C3=A1mara?= Date: Wed, 29 Oct 2025 00:47:11 -0500 Subject: [PATCH 1/4] feat: Add functionals with parameters * The group of mut cursels has a new member that receives a parameter * Added a function to get the number of items(one visible cell characters) within a cursel. --- src/tui/editor.zig | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/tui/editor.zig b/src/tui/editor.zig index f23cda4..59f55cd 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -1632,6 +1632,26 @@ pub const Editor = struct { return self.primary.col - self.view.col; } + pub fn cursel_length(root: Buffer.Root, cursel_: CurSel, metrics: Buffer.Metrics) usize { + var length: usize = 0; + var cursel = cursel_; + cursel.check_selection(root, metrics); + if (cursel.selection) |*sel| { + sel.normalize(); + if (sel.begin.row == sel.end.row) { + length = sel.end.col - sel.begin.col; + } else { + var row = sel.begin.row; + while (row < sel.end.row) { + length += root.line_width(row, metrics) catch 0; + row += 1; + } + length = length + sel.end.col - sel.begin.col; + } + } + return length; + } + fn update_event(self: *Self) !void { const primary = self.get_primary(); const dirty = if (self.buffer) |buf| buf.is_dirty() else false; @@ -2031,6 +2051,23 @@ pub const Editor = struct { return if (someone_stopped) error.Stop else {}; } + fn with_cursel_mut_arg(self: *Self, root: Buffer.Root, op: cursel_operator_mut_arg, cursel: *CurSel, allocator: Allocator, ctx: Context) error{Stop}!Buffer.Root { + return op(self, root, cursel, allocator, ctx); + } + + pub fn with_cursels_mut_once_arg(self: *Self, root_: Buffer.Root, move: cursel_operator_mut_arg, allocator: Allocator, ctx: Context) error{Stop}!Buffer.Root { + var root = root_; + var someone_stopped = false; + for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel| { + root = self.with_cursel_mut_arg(root, move, cursel, allocator, ctx) catch ret: { + someone_stopped = true; + break :ret root; + }; + }; + self.collapse_cursors(); + return if (someone_stopped) error.Stop else root; + } + fn with_cursel_const(root: Buffer.Root, op: cursel_operator_const, cursel: *CurSel) error{Stop}!void { return op(root, cursel); } @@ -2100,6 +2137,7 @@ pub const Editor = struct { const cursor_operator = *const fn (root: Buffer.Root, cursor: *Cursor, allocator: Allocator) error{Stop}!Buffer.Root; const cursel_operator = *const fn (root: Buffer.Root, cursel: *CurSel, allocator: Allocator) error{Stop}!Buffer.Root; const cursel_operator_mut = *const fn (self: *Self, root: Buffer.Root, cursel: *CurSel, allocator: Allocator) error{Stop}!Buffer.Root; + const cursel_operator_mut_arg = *const fn (self: *Self, root: Buffer.Root, cursel: *CurSel, allocator: Allocator, ctx: Context) error{Stop}!Buffer.Root; pub fn is_not_word_char(c: []const u8) bool { if (c.len == 0) return true; From 8246f2b0ba5b29c165ff0e165f278bb8b579cea4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Igor=20T=C3=A1mara?= Date: Wed, 29 Oct 2025 00:51:18 -0500 Subject: [PATCH 2/4] feat: [hx] r to replace with a character If no selection, the character under the cursor is replaced, if selection, each character is replaced by the typed character. --- src/keybind/builtin/helix.json | 12 +++++ src/tui/mode/helix.zig | 60 +++++++++++++++++++++++ src/tui/mode/mini/replace.zig | 87 ++++++++++++++++++++++++++++++++++ src/tui/tui.zig | 5 ++ 4 files changed, 164 insertions(+) create mode 100644 src/tui/mode/mini/replace.zig diff --git a/src/keybind/builtin/helix.json b/src/keybind/builtin/helix.json index af3314e..649433a 100644 --- a/src/keybind/builtin/helix.json +++ b/src/keybind/builtin/helix.json @@ -379,6 +379,7 @@ ["N", "extend_search_next"], ["*", "extend_search_prev"], + ["r", "replace"], ["P", "paste_clipboard_before"], ["R", ["replace_selections_with_clipboard"], ["enter_mode", "normal"]], ["p", "paste_after"], @@ -546,6 +547,17 @@ ["8", "add_integer_argument_digit", 8], ["9", "add_integer_argument_digit", 9] ] + }, + "mini/replace": { + "press": [ + ["ctrl+g", "mini_mode_cancel"], + ["ctrl+c", "mini_mode_cancel"], + ["ctrl+l", "scroll_view_center_cycle"], + ["tab", "mini_mode_insert_bytes", "\t"], + ["enter", "mini_mode_insert_bytes", "\n"], + ["escape", "mini_mode_cancel"], + ["backspace", "mini_mode_cancel"] + ] }, "home": { "on_match_failure": "ignore", diff --git a/src/tui/mode/helix.zig b/src/tui/mode/helix.zig index 4e4a95c..3857ae3 100644 --- a/src/tui/mode/helix.zig +++ b/src/tui/mode/helix.zig @@ -441,6 +441,17 @@ const cmds_ = struct { try paste_helix(ctx, insert_before); } pub const paste_clipboard_before_meta: Meta = .{ .description = "Paste from clipboard before selection" }; + + pub fn replace_with_character_helix(_: *void, ctx: Ctx) Result { + const mv = tui.mainview() orelse return; + const ed = mv.get_active_editor() orelse return; + var root = ed.buf_root() catch return; + root = try ed.with_cursels_mut_once_arg(root, replace_cursel_with_character, ed.allocator, ctx); + try ed.update_buf(root); + ed.clamp(); + ed.need_render(); + } + pub const replace_with_character_helix_meta: Meta = .{ .description = "Replace with character" }; }; fn to_char_helix(ctx: command.Context, move: Editor.cursel_operator_mut_once_arg) command.Result { @@ -598,6 +609,40 @@ fn move_cursor_word_left_helix(root: Buffer.Root, cursor: *Cursor, metrics: Buff } } +fn replace_cursel_with_character(ed: *Editor, root: Buffer.Root, cursel: *CurSel, allocator: Allocator, ctx: command.Context) error{Stop}!Buffer.Root { + var egc: []const u8 = undefined; + if (!(ctx.args.match(.{tp.extract(&egc)}) catch return error.Stop)) + return error.Stop; + const no_selection = try select_char_if_no_selection(cursel, root, ed.metrics); + var begin: Cursor = undefined; + if (cursel.selection) |*sel| { + sel.normalize(); + begin = sel.*.begin; + } + + const sel_length = Editor.cursel_length(root, cursel.*, ed.metrics); + const total_length = sel_length * egc.len; + var sfa = std.heap.stackFallback(4096, ed.allocator); + const sfa_allocator = sfa.get(); + const replacement = sfa_allocator.alloc(u8, total_length) catch return error.Stop; + errdefer allocator.free(replacement); + for (0..sel_length) |i| { + for (0..egc.len) |j| { + replacement[i * egc.len + j] = egc[j]; + } + } + + const root_ = insert_replace_selection(ed, root, cursel, replacement, allocator) catch return error.Stop; + + if (no_selection) { + try cursel.cursor.move_left(root, ed.metrics); + cursel.disable_selection(root, ed.metrics); + } else { + cursel.selection = Selection{ .begin = begin, .end = cursel.cursor }; + } + return root_; +} + fn move_noop(_: Buffer.Root, _: *Cursor, _: Buffer.Metrics) error{Stop}!void {} fn move_cursor_word_right_end_helix(root: Buffer.Root, cursor: *Cursor, metrics: Buffer.Metrics) error{Stop}!void { @@ -888,6 +933,21 @@ fn move_cursor_carriage_return(root: Buffer.Root, cursel: CurSel, cursor: *Curso try Editor.move_cursor_right(root, cursor, metrics); } +fn select_char_if_no_selection(cursel: *CurSel, root: Buffer.Root, metrics: Buffer.Metrics) !bool { + if (cursel.selection) |*sel_| { + const sel: *Selection = sel_; + if (sel.*.empty()) { + sel.*.begin = .{ .row = cursel.cursor.row, .col = cursel.cursor.col + 1, .target = cursel.cursor.target + 1 }; + return true; + } + return false; + } else { + const sel = try cursel.enable_selection(root, metrics); + sel.begin = .{ .row = cursel.cursor.row, .col = cursel.cursor.col + 1, .target = cursel.cursor.target + 1 }; + return true; + } +} + fn is_cursel_from_extend_line_below(cursel: CurSel) bool { if (cursel.selection) |sel_| { var sel = sel_; diff --git a/src/tui/mode/mini/replace.zig b/src/tui/mode/mini/replace.zig new file mode 100644 index 0000000..9740f40 --- /dev/null +++ b/src/tui/mode/mini/replace.zig @@ -0,0 +1,87 @@ +const std = @import("std"); +const tp = @import("thespian"); +const log = @import("log"); +const input = @import("input"); +const keybind = @import("keybind"); +const command = @import("command"); +const EventHandler = @import("EventHandler"); + +const tui = @import("../../tui.zig"); + +const Allocator = @import("std").mem.Allocator; + +const Self = @This(); + +const Commands = command.Collection(cmds); + +allocator: Allocator, +commands: Commands = undefined, + +pub fn create(allocator: Allocator, ctx: command.Context) !struct { tui.Mode, tui.MiniMode } { + var operation_command: []const u8 = undefined; + _ = ctx.args.match(.{tp.extract(&operation_command)}) catch return error.InvalidReplaceArgument; + + const self = try allocator.create(Self); + errdefer allocator.destroy(self); + self.* = .{ + .allocator = allocator, + }; + try self.commands.init(self); + var mode = try keybind.mode("mini/replace", allocator, .{ + .insert_command = "mini_mode_insert_bytes", + }); + mode.event_handler = EventHandler.to_owned(self); + return .{ mode, .{ .name = self.name() } }; +} + +pub fn deinit(self: *Self) void { + self.commands.deinit(); + self.allocator.destroy(self); +} + +fn name(_: *Self) []const u8 { + return "🗘 replace"; +} + +pub fn receive(_: *Self, _: tp.pid_ref, _: tp.message) error{Exit}!bool { + return false; +} + +fn execute_operation(_: *Self, ctx: command.Context) command.Result { + try command.executeName("replace_with_character_helix", ctx); + try command.executeName("exit_mini_mode", .{}); +} + +const cmds = struct { + pub const Target = Self; + const Ctx = command.Context; + const Meta = command.Metadata; + const Result = command.Result; + + pub fn mini_mode_insert_code_point(self: *Self, ctx: Ctx) Result { + var code_point: u32 = 0; + if (!try ctx.args.match(.{tp.extract(&code_point)})) + return error.InvalidRepaceInsertCodePointArgument; + + log.logger("replace").print("replacement '{d}'", .{code_point}); + var buf: [6]u8 = undefined; + const bytes = input.ucs32_to_utf8(&[_]u32{code_point}, &buf) catch return error.InvalidReplaceCodePoint; + log.logger("replace").print("replacement '{s}'", .{buf[0..bytes]}); + return self.execute_operation(ctx); + } + pub const mini_mode_insert_code_point_meta: Meta = .{ .description = "🗘 Replace" }; + + pub fn mini_mode_insert_bytes(self: *Self, ctx: Ctx) Result { + var bytes: []const u8 = undefined; + if (!try ctx.args.match(.{tp.extract(&bytes)})) + return error.InvalidReplaceInsertBytesArgument; + log.logger("replace").print("replacement '{s}'", .{bytes}); + return self.execute_operation(ctx); + } + pub const mini_mode_insert_bytes_meta: Meta = .{ .arguments = &.{.string} }; + + pub fn mini_mode_cancel(_: *Self, _: Ctx) Result { + command.executeName("exit_mini_mode", .{}) catch {}; + } + pub const mini_mode_cancel_meta: Meta = .{ .description = "Cancel replace" }; +}; diff --git a/src/tui/tui.zig b/src/tui/tui.zig index 2f050d1..4d2dca6 100644 --- a/src/tui/tui.zig +++ b/src/tui/tui.zig @@ -1167,6 +1167,11 @@ const cmds = struct { } pub const move_to_char_meta: Meta = .{ .description = "Move to character" }; + pub fn replace(self: *Self, ctx: Ctx) Result { + return enter_mini_mode(self, @import("mode/mini/replace.zig"), ctx); + } + pub const replace_meta: Meta = .{ .description = "Replace with character" }; + pub fn open_file(self: *Self, ctx: Ctx) Result { if (get_active_selection(self.allocator)) |text| { defer self.allocator.free(text); From 18bc89edf8fd4297081cf260d238685d3e3f7618 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Thu, 30 Oct 2025 22:32:57 +0100 Subject: [PATCH 3/4] refactor: replace cursel_length with Buffer.Node.get_range get_range is likely much faster because it only walks the buffer tree once. Besides the performance difference it should give identical results. --- src/tui/editor.zig | 20 -------------------- src/tui/mode/helix.zig | 4 ++-- 2 files changed, 2 insertions(+), 22 deletions(-) diff --git a/src/tui/editor.zig b/src/tui/editor.zig index 59f55cd..f4c97c3 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -1632,26 +1632,6 @@ pub const Editor = struct { return self.primary.col - self.view.col; } - pub fn cursel_length(root: Buffer.Root, cursel_: CurSel, metrics: Buffer.Metrics) usize { - var length: usize = 0; - var cursel = cursel_; - cursel.check_selection(root, metrics); - if (cursel.selection) |*sel| { - sel.normalize(); - if (sel.begin.row == sel.end.row) { - length = sel.end.col - sel.begin.col; - } else { - var row = sel.begin.row; - while (row < sel.end.row) { - length += root.line_width(row, metrics) catch 0; - row += 1; - } - length = length + sel.end.col - sel.begin.col; - } - } - return length; - } - fn update_event(self: *Self) !void { const primary = self.get_primary(); const dirty = if (self.buffer) |buf| buf.is_dirty() else false; diff --git a/src/tui/mode/helix.zig b/src/tui/mode/helix.zig index 3857ae3..f5a33c0 100644 --- a/src/tui/mode/helix.zig +++ b/src/tui/mode/helix.zig @@ -615,12 +615,12 @@ fn replace_cursel_with_character(ed: *Editor, root: Buffer.Root, cursel: *CurSel return error.Stop; const no_selection = try select_char_if_no_selection(cursel, root, ed.metrics); var begin: Cursor = undefined; + var sel_length: usize = 1; if (cursel.selection) |*sel| { sel.normalize(); begin = sel.*.begin; + _ = root.get_range(sel.*, null, null, &sel_length, ed.metrics) catch return error.Stop; } - - const sel_length = Editor.cursel_length(root, cursel.*, ed.metrics); const total_length = sel_length * egc.len; var sfa = std.heap.stackFallback(4096, ed.allocator); const sfa_allocator = sfa.get(); From 63745f1e6b871348208e8b3c25df0c93b9e1282a Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Thu, 30 Oct 2025 22:35:38 +0100 Subject: [PATCH 4/4] refactor: use @memcpy to splat replacement string Also, don't leak replacement string. --- src/tui/mode/helix.zig | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/tui/mode/helix.zig b/src/tui/mode/helix.zig index f5a33c0..06fef1d 100644 --- a/src/tui/mode/helix.zig +++ b/src/tui/mode/helix.zig @@ -625,12 +625,9 @@ fn replace_cursel_with_character(ed: *Editor, root: Buffer.Root, cursel: *CurSel var sfa = std.heap.stackFallback(4096, ed.allocator); const sfa_allocator = sfa.get(); const replacement = sfa_allocator.alloc(u8, total_length) catch return error.Stop; - errdefer allocator.free(replacement); - for (0..sel_length) |i| { - for (0..egc.len) |j| { - replacement[i * egc.len + j] = egc[j]; - } - } + defer sfa_allocator.free(replacement); + for (0..sel_length) |i| + @memcpy(replacement[i * egc.len .. (i + 1) * egc.len], egc); const root_ = insert_replace_selection(ed, root, cursel, replacement, allocator) catch return error.Stop;