Compare commits

...

4 commits

Author SHA1 Message Date
63745f1e6b refactor: use @memcpy to splat replacement string
Also, don't leak replacement string.
2025-10-30 22:40:53 +01:00
18bc89edf8 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.
2025-10-30 22:40:53 +01:00
Igor Támara
8246f2b0ba 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.
2025-10-30 22:40:53 +01:00
Igor Támara
411b26d4aa 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.
2025-10-30 22:40:53 +01:00
5 changed files with 179 additions and 0 deletions

View file

@ -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",

View file

@ -2031,6 +2031,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 +2117,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;

View file

@ -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,37 @@ 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;
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 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;
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;
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 +930,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_;

View file

@ -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" };
};

View file

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