diff --git a/src/tui/mode/input/vim/insert.zig b/src/tui/mode/input/vim/insert.zig index e9f2c8e..c12af88 100644 --- a/src/tui/mode/input/vim/insert.zig +++ b/src/tui/mode/input/vim/insert.zig @@ -22,6 +22,7 @@ a: Allocator, input: ArrayList(u8), last_cmd: []const u8 = "", leader: ?struct { keypress: u32, modifiers: u32 } = null, +commands: Commands = undefined, pub fn create(a: Allocator) !tui.Mode { const self: *Self = try a.create(Self); @@ -29,6 +30,7 @@ pub fn create(a: Allocator) !tui.Mode { .a = a, .input = try ArrayList(u8).initCapacity(a, input_buffer_size), }; + try self.commands.init(self); return .{ .handler = EventHandler.to_owned(self), .name = root.application_logo ++ "INSERT", @@ -38,6 +40,7 @@ pub fn create(a: Allocator) !tui.Mode { } pub fn deinit(self: *Self) void { + self.commands.deinit(); self.input.deinit(); self.a.destroy(self); } @@ -298,3 +301,31 @@ fn cmd_async(self: *Self, name_: []const u8) tp.result { self.last_cmd = name_; return tp.self_pid().send(.{ "cmd", name_ }); } + +const Commands = command.Collection(cmds_); +const cmds_ = struct { + pub const Target = Self; + const Ctx = command.Context; + + pub fn @"w"(self: *Self, _: Ctx) tp.result { + try self.cmd("save_file", .{}); + } + + pub fn @"q"(self: *Self, _: Ctx) tp.result { + try self.cmd("quit", .{}); + } + + pub fn @"q!"(self: *Self, _: Ctx) tp.result { + try self.cmd("quit_without_saving", .{}); + } + + pub fn @"wq"(self: *Self, _: Ctx) tp.result { + try self.cmd("save_file", .{}); + try self.cmd("quit", .{}); + } + + pub fn @"wq!"(self: *Self, _: Ctx) tp.result { + self.cmd("save_file", .{}) catch {}; + try self.cmd("quit_without_saving", .{}); + } +}; diff --git a/src/tui/mode/input/vim/normal.zig b/src/tui/mode/input/vim/normal.zig index 2deeee9..1b6193d 100644 --- a/src/tui/mode/input/vim/normal.zig +++ b/src/tui/mode/input/vim/normal.zig @@ -23,6 +23,7 @@ input: ArrayList(u8), last_cmd: []const u8 = "", leader: ?struct { keypress: u32, modifiers: u32 } = null, count: usize = 0, +commands: Commands = undefined, pub fn create(a: Allocator) !tui.Mode { const self: *Self = try a.create(Self); @@ -30,6 +31,7 @@ pub fn create(a: Allocator) !tui.Mode { .a = a, .input = try ArrayList(u8).initCapacity(a, input_buffer_size), }; + try self.commands.init(self); return .{ .handler = EventHandler.to_owned(self), .name = root.application_logo ++ "NORMAL", @@ -40,6 +42,7 @@ pub fn create(a: Allocator) !tui.Mode { } pub fn deinit(self: *Self) void { + self.commands.deinit(); self.input.deinit(); self.a.destroy(self); } @@ -602,3 +605,31 @@ const hints = tui.KeybindHints.initComptime(.{ .{ "undo", "u" }, .{ "unindent", "S-tab" }, }); + +const Commands = command.Collection(cmds_); +const cmds_ = struct { + pub const Target = Self; + const Ctx = command.Context; + + pub fn @"w"(self: *Self, _: Ctx) tp.result { + try self.cmd("save_file", .{}); + } + + pub fn @"q"(self: *Self, _: Ctx) tp.result { + try self.cmd("quit", .{}); + } + + pub fn @"q!"(self: *Self, _: Ctx) tp.result { + try self.cmd("quit_without_saving", .{}); + } + + pub fn @"wq"(self: *Self, _: Ctx) tp.result { + try self.cmd("save_file", .{}); + try self.cmd("quit", .{}); + } + + pub fn @"wq!"(self: *Self, _: Ctx) tp.result { + self.cmd("save_file", .{}) catch {}; + try self.cmd("quit_without_saving", .{}); + } +}; diff --git a/src/tui/mode/input/vim/visual.zig b/src/tui/mode/input/vim/visual.zig index 2886218..85e279f 100644 --- a/src/tui/mode/input/vim/visual.zig +++ b/src/tui/mode/input/vim/visual.zig @@ -23,6 +23,7 @@ input: ArrayList(u8), last_cmd: []const u8 = "", leader: ?struct { keypress: u32, modifiers: u32 } = null, count: usize = 0, +commands: Commands = undefined, pub fn create(a: Allocator) !tui.Mode { const self: *Self = try a.create(Self); @@ -30,6 +31,7 @@ pub fn create(a: Allocator) !tui.Mode { .a = a, .input = try ArrayList(u8).initCapacity(a, input_buffer_size), }; + try self.commands.init(self); return .{ .handler = EventHandler.to_owned(self), .name = root.application_logo ++ "VISUAL", @@ -40,6 +42,7 @@ pub fn create(a: Allocator) !tui.Mode { } pub fn deinit(self: *Self) void { + self.commands.deinit(); self.input.deinit(); self.a.destroy(self); } @@ -562,3 +565,31 @@ const hints = tui.KeybindHints.initComptime(.{ .{ "undo", "u" }, .{ "unindent", "S-tab" }, }); + +const Commands = command.Collection(cmds_); +const cmds_ = struct { + pub const Target = Self; + const Ctx = command.Context; + + pub fn @"w"(self: *Self, _: Ctx) tp.result { + try self.cmd("save_file", .{}); + } + + pub fn @"q"(self: *Self, _: Ctx) tp.result { + try self.cmd("quit", .{}); + } + + pub fn @"q!"(self: *Self, _: Ctx) tp.result { + try self.cmd("quit_without_saving", .{}); + } + + pub fn @"wq"(self: *Self, _: Ctx) tp.result { + try self.cmd("save_file", .{}); + try self.cmd("quit", .{}); + } + + pub fn @"wq!"(self: *Self, _: Ctx) tp.result { + self.cmd("save_file", .{}) catch {}; + try self.cmd("quit_without_saving", .{}); + } +};