From 44c643815abcdf2c3cb786337c5305664c7e3e2d Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Mon, 18 Nov 2024 00:01:23 +0100 Subject: [PATCH] fix: move_to_char with dynamic bindings --- src/keybind/dynamic/keybindings.json | 5 +++++ src/tui/mode/mini/move_to_char.zig | 33 +++++++++++++--------------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/keybind/dynamic/keybindings.json b/src/keybind/dynamic/keybindings.json index 5da5621..0eb77c1 100644 --- a/src/keybind/dynamic/keybindings.json +++ b/src/keybind/dynamic/keybindings.json @@ -258,6 +258,11 @@ "syntax": "flow", "on_match_failure": "insert", "bindings": [ + ["ctrl+g", "mini_mode_cancel"], + ["ctrl+c", "mini_mode_cancel"], + ["ctrl+l", "scroll_view_center_cycle"], + ["escape", "mini_mode_cancel"], + ["backspace", "mini_mode_cancel"] ] }, "mini/file_browser": { diff --git a/src/tui/mode/mini/move_to_char.zig b/src/tui/mode/mini/move_to_char.zig index da7dee4..38c363b 100644 --- a/src/tui/mode/mini/move_to_char.zig +++ b/src/tui/mode/mini/move_to_char.zig @@ -43,7 +43,9 @@ pub fn create(allocator: Allocator, ctx: command.Context) !struct { tui.Mode, tu try self.commands.init(self); return .{ .{ - .input_handler = try keybind.mode.mini.move_to_char.create(allocator, .{}), + .input_handler = try keybind.mode.mini.move_to_char.create(allocator, .{ + .insert_command = "mini_mode_insert_bytes", + }), .event_handler = EventHandler.to_owned(self), }, .{ @@ -74,7 +76,7 @@ pub fn receive(_: *Self, _: tp.pid_ref, _: tp.message) error{Exit}!bool { return false; } -fn execute_operation(self: *Self, c: u32) void { +fn execute_operation(self: *Self, ctx: command.Context) command.Result { const cmd = switch (self.direction) { .left => switch (self.operation) { .move => "move_to_char_left", @@ -85,10 +87,8 @@ fn execute_operation(self: *Self, c: u32) void { .select => "select_to_char_right", }, }; - var buf: [6]u8 = undefined; - const bytes = input.ucs32_to_utf8(&[_]u32{c}, &buf) catch return; - command.executeName(cmd, command.fmt(.{buf[0..bytes]})) catch {}; - command.executeName("exit_mini_mode", .{}) catch {}; + try command.executeName(cmd, ctx); + try command.executeName("exit_mini_mode", .{}); } const cmds = struct { @@ -102,21 +102,18 @@ const cmds = struct { return error.InvalidArgument; var buf: [6]u8 = undefined; const bytes = input.ucs32_to_utf8(&[_]u32{code_point}, &buf) catch return error.InvalidArgument; - const cmd = switch (self.direction) { - .left => switch (self.operation) { - .move => "move_to_char_left", - .select => "select_to_char_left", - }, - .right => switch (self.operation) { - .move => "move_to_char_right", - .select => "select_to_char_right", - }, - }; - try command.executeName(cmd, command.fmt(.{buf[0..bytes]})); - try command.executeName("exit_mini_mode", .{}); + return self.execute_operation(command.fmt(.{buf[0..bytes]})); } pub const mini_mode_insert_code_point_meta = .{ .interactive = false }; + 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.InvalidArgument; + return self.execute_operation(ctx); + } + pub const mini_mode_insert_bytes_meta = .{ .interactive = false }; + pub fn mini_mode_cancel(_: *Self, _: Ctx) Result { command.executeName("exit_mini_mode", .{}) catch {}; }