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);