diff --git a/src/tui/mode/helix.zig b/src/tui/mode/helix.zig index cadd990..0230b82 100644 --- a/src/tui/mode/helix.zig +++ b/src/tui/mode/helix.zig @@ -522,6 +522,17 @@ const cmds_ = struct { try ed.add_cursor_next_match(.{}); } pub const add_next_match_helix_meta: Meta = .{}; + + pub fn switch_to_lowercase(_: *void, ctx: Ctx) Result { + const mv = tui.mainview() orelse return; + const ed = mv.get_active_editor() orelse return; + const b = try ed.buf_for_update(); + const root = try ed.with_cursels_mut_once_arg(b.root, switch_to_lowercase_cursel, ed.allocator, ctx); + try ed.update_buf(root); + ed.clamp(); + ed.need_render(); + } + pub const switch_to_lowercase_meta: Meta = .{ .description = "Switch to lowercase" }; }; fn match_bracket(root: Buffer.Root, cursel: *CurSel, ctx: command.Context, metrics: Buffer.Metrics) error{Stop}!void { @@ -832,31 +843,26 @@ fn replace_cursel_with_character(ed: *Editor, root: Buffer.Root, cursel: *CurSel 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; + + const saved = cursel.*; + defer cursel.* = saved; + + const selection = cursel.enable_selection(root, ed.metrics); + selection.normalize(); + 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; + _ = root.get_range(selection.*, null, null, &sel_length, ed.metrics) catch return error.Stop; + var sfa = std.heap.stackFallback(4096, ed.allocator); const sfa_allocator = sfa.get(); + + const total_length = sel_length * egc.len; 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_; + return insert_replace_selection(ed, root, cursel, replacement, allocator) catch return error.Stop; } fn move_noop(_: Buffer.Root, _: *Cursor, _: Buffer.Metrics) error{Stop}!void {} @@ -1214,21 +1220,6 @@ 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 = 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_; @@ -1238,6 +1229,29 @@ fn is_cursel_from_extend_line_below(cursel: CurSel) bool { return false; } +fn switch_to_lowercase_cursel( + ed: *Editor, + root: Buffer.Root, + cursel: *CurSel, + allocator: Allocator, + _: command.Context, +) error{Stop}!Buffer.Root { + const saved = cursel.*; + defer cursel.* = saved; + + const selection = cursel.enable_selection(root, ed.metrics); + + const ucased = blk: { + const selected_text = Editor.copy_selection(root, selection.*, allocator, ed.metrics) catch return error.Stop; + defer allocator.free(selected_text); + + break :blk Buffer.unicode.to_lower(allocator, selected_text) catch return error.Stop; + }; + defer allocator.free(ucased); + + return insert_replace_selection(ed, root, cursel, ucased, allocator) catch return error.Stop; +} + const private = @This(); // exports for unittests pub const test_internal = struct { diff --git a/src/tui/status/bar.zig b/src/tui/status/bar.zig index ce5638b..529a247 100644 --- a/src/tui/status/bar.zig +++ b/src/tui/status/bar.zig @@ -33,6 +33,9 @@ fn on_layout(_: ?*anyopaque, w: *WidgetList) Widget.Layout { fn render_grip(ctx: ?*anyopaque, theme: *const Widget.Theme) void { const w: *WidgetList = @ptrCast(@alignCast(ctx.?)); if (w.hover()) { + w.plane.cursor_move_yx(0, 0); + w.plane.set_base_style(theme.editor); + _ = w.plane.putstr(" ") catch {}; w.plane.set_style(theme.statusbar_hover); w.plane.cursor_move_yx(0, 0); _ = w.plane.putstr("  ") catch {};