fix: don't revert to default mode on keybind namespace change

This commit is contained in:
CJ van den Berg 2024-12-05 19:49:02 +01:00
parent f8dff2a7bb
commit bbd42fec16
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
2 changed files with 22 additions and 2 deletions

View file

@ -52,8 +52,10 @@ const Handler = struct {
), ),
}; };
return .{ return .{
.allocator = allocator,
.input_handler = EventHandler.to_owned(self), .input_handler = EventHandler.to_owned(self),
.keybind_hints = self.bindings.hints(), .keybind_hints = self.bindings.hints(),
.mode = try allocator.dupe(u8, mode_name),
.name = self.bindings.name, .name = self.bindings.name,
.line_numbers = self.bindings.line_numbers, .line_numbers = self.bindings.line_numbers,
.cursor_shape = self.bindings.cursor_shape, .cursor_shape = self.bindings.cursor_shape,
@ -68,15 +70,18 @@ const Handler = struct {
}; };
pub const Mode = struct { pub const Mode = struct {
allocator: std.mem.Allocator,
input_handler: EventHandler, input_handler: EventHandler,
event_handler: ?EventHandler = null, event_handler: ?EventHandler = null,
mode: []const u8,
name: []const u8 = "", name: []const u8 = "",
line_numbers: LineNumbers = .absolute, line_numbers: LineNumbers = .absolute,
keybind_hints: *const KeybindHints, keybind_hints: *const KeybindHints,
cursor_shape: CursorShape = .block, cursor_shape: CursorShape = .block,
pub fn deinit(self: *Mode) void { pub fn deinit(self: *Mode) void {
self.allocator.free(self.mode);
self.input_handler.deinit(); self.input_handler.deinit();
if (self.event_handler) |eh| eh.deinit(); if (self.event_handler) |eh| eh.deinit();
} }

View file

@ -597,7 +597,7 @@ fn get_input_mode(self: *Self, mode_name: []const u8) !Mode {
return keybind.mode(mode_name, self.allocator, .{}); return keybind.mode(mode_name, self.allocator, .{});
} }
fn enter_input_mode(self: *Self, new_mode: Mode, mode_name: []const u8) command.Result { fn enter_input_mode(self: *Self, new_mode: Mode) command.Result {
if (self.mini_mode) |_| try cmds.exit_mini_mode(self, .{}); if (self.mini_mode) |_| try cmds.exit_mini_mode(self, .{});
if (self.input_mode_outer) |_| try cmds.exit_overlay_mode(self, .{}); if (self.input_mode_outer) |_| try cmds.exit_overlay_mode(self, .{});
if (self.input_mode) |*m| { if (self.input_mode) |*m| {
@ -607,6 +607,21 @@ fn enter_input_mode(self: *Self, new_mode: Mode, mode_name: []const u8) command.
self.input_mode = new_mode; self.input_mode = new_mode;
} }
fn refresh_input_mode(self: *Self) command.Result {
const mode = (self.input_mode orelse return).mode;
var new_mode = self.get_input_mode(mode) catch ret: {
self.logger.print("unknown mode {s}", .{mode});
break :ret try self.get_input_mode(keybind.default_mode);
};
errdefer new_mode.deinit();
if (self.input_mode) |*m| {
m.deinit();
self.input_mode = null;
}
self.input_mode = new_mode;
}
pub const enter_mode_meta = .{ .arguments = &.{.string} };
const cmds = struct { const cmds = struct {
pub const Target = Self; pub const Target = Self;
const Ctx = command.Context; const Ctx = command.Context;
@ -691,7 +706,7 @@ const cmds = struct {
try self.save_config(); try self.save_config();
self.logger.print("input mode {s}", .{self.config.input_mode}); self.logger.print("input mode {s}", .{self.config.input_mode});
try keybind.set_namespace(self.config.input_mode); try keybind.set_namespace(self.config.input_mode);
return enter_mode(self, Ctx.fmt(.{keybind.default_mode})); return self.refresh_input_mode();
} }
pub const toggle_input_mode_meta = .{ .description = "Switch to next input mode" }; pub const toggle_input_mode_meta = .{ .description = "Switch to next input mode" };