From 1fcec1bab5943a56919806396693198f36c29575 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Tue, 12 Aug 2025 12:02:41 +0200 Subject: [PATCH 1/5] feat: add support for numeric arguments in cli exec calls --- src/main.zig | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main.zig b/src/main.zig index f36f642..e914235 100644 --- a/src/main.zig +++ b/src/main.zig @@ -331,7 +331,12 @@ pub fn main() anyerror!void { try cbor.writeValue(writer, cmd_); try cbor.writeArrayHeader(writer, count - 1); - while (cmd_args.next()) |arg| try cbor.writeValue(writer, arg); + while (cmd_args.next()) |arg| { + if (std.fmt.parseInt(isize, arg, 10) catch null) |i| + try cbor.writeValue(writer, i) + else + try cbor.writeValue(writer, arg); + } try tui_proc.send_raw(.{ .buf = msg.items }); } From 63a527726a16b762df4b55ce05fb162d6e8f517b Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Tue, 12 Aug 2025 12:04:36 +0200 Subject: [PATCH 2/5] feat: add support for arguments to mini/numeric_input modes --- src/tui/mode/mini/goto.zig | 9 ++++++++- src/tui/mode/mini/numeric_input.zig | 19 +++++++++++-------- src/tui/mode/mini/tab_width.zig | 28 ++++++++++++++++++++++------ 3 files changed, 41 insertions(+), 15 deletions(-) diff --git a/src/tui/mode/mini/goto.zig b/src/tui/mode/mini/goto.zig index 13abeec..6607dc6 100644 --- a/src/tui/mode/mini/goto.zig +++ b/src/tui/mode/mini/goto.zig @@ -1,5 +1,7 @@ const command = @import("command"); +const tui = @import("../../tui.zig"); + pub const Type = @import("numeric_input.zig").Create(@This()); pub const create = Type.create; @@ -7,10 +9,15 @@ pub fn name(_: *Type) []const u8 { return "#goto"; } +pub fn start(_: *Type) usize { + const editor = tui.get_active_editor() orelse return 1; + return editor.get_primary().cursor.row + 1; +} + pub const preview = goto; pub const apply = goto; pub const cancel = goto; -fn goto(self: *Type) void { +fn goto(self: *Type, _: command.Context) void { command.executeName("goto_line", command.fmt(.{self.input orelse self.start})) catch {}; } diff --git a/src/tui/mode/mini/numeric_input.zig b/src/tui/mode/mini/numeric_input.zig index 2781a45..5b6be0f 100644 --- a/src/tui/mode/mini/numeric_input.zig +++ b/src/tui/mode/mini/numeric_input.zig @@ -22,16 +22,18 @@ pub fn Create(options: type) type { buf: [30]u8 = undefined, input: ?usize = null, start: usize, + ctx: command.Context, commands: Commands = undefined, - pub fn create(allocator: Allocator, _: command.Context) !struct { tui.Mode, tui.MiniMode } { - const editor = tui.get_active_editor() orelse return error.NotFound; + pub fn create(allocator: Allocator, ctx: command.Context) !struct { tui.Mode, tui.MiniMode } { const self = try allocator.create(Self); errdefer allocator.destroy(self); self.* = .{ .allocator = allocator, - .start = editor.get_primary().cursor.row + 1, + .ctx = .{ .args = try ctx.args.clone(allocator) }, + .start = 0, }; + self.start = options.start(self); try self.commands.init(self); var mode = try keybind.mode("mini/numeric", allocator, .{ .insert_command = "mini_mode_insert_bytes", @@ -41,6 +43,7 @@ pub fn Create(options: type) type { } pub fn deinit(self: *Self) void { + self.allocator.free(self.ctx.args.buf); self.commands.deinit(); self.allocator.destroy(self); } @@ -92,7 +95,7 @@ pub fn Create(options: type) type { pub fn mini_mode_cancel(self: *Self, _: Ctx) Result { self.input = null; self.update_mini_mode_text(); - options.cancel(self); + options.cancel(self, self.ctx); command.executeName("exit_mini_mode", .{}) catch {}; } pub const mini_mode_cancel_meta: Meta = .{ .description = "Cancel input" }; @@ -102,7 +105,7 @@ pub fn Create(options: type) type { const newval = if (linenum < 10) 0 else linenum / 10; self.input = if (newval == 0) null else newval; self.update_mini_mode_text(); - options.preview(self); + options.preview(self, self.ctx); } } pub const mini_mode_delete_backwards_meta: Meta = .{ .description = "Delete backwards" }; @@ -116,7 +119,7 @@ pub fn Create(options: type) type { else => {}, } self.update_mini_mode_text(); - options.preview(self); + options.preview(self, self.ctx); } pub const mini_mode_insert_code_point_meta: Meta = .{ .arguments = &.{.integer} }; @@ -126,7 +129,7 @@ pub fn Create(options: type) type { return error.InvalidGotoInsertBytesArgument; self.insert_bytes(bytes); self.update_mini_mode_text(); - options.preview(self); + options.preview(self, self.ctx); } pub const mini_mode_insert_bytes_meta: Meta = .{ .arguments = &.{.string} }; @@ -136,7 +139,7 @@ pub fn Create(options: type) type { pub const mini_mode_paste_meta: Meta = .{ .arguments = &.{.string} }; pub fn mini_mode_select(self: *Self, _: Ctx) Result { - options.apply(self); + options.apply(self, self.ctx); command.executeName("exit_mini_mode", .{}) catch {}; } pub const mini_mode_select_meta: Meta = .{ .description = "Select" }; diff --git a/src/tui/mode/mini/tab_width.zig b/src/tui/mode/mini/tab_width.zig index a2ca836..a617973 100644 --- a/src/tui/mode/mini/tab_width.zig +++ b/src/tui/mode/mini/tab_width.zig @@ -1,5 +1,8 @@ +const cbor = @import("cbor"); const command = @import("command"); +const tui = @import("../../tui.zig"); + pub const Type = @import("numeric_input.zig").Create(@This()); pub const create = Type.create; @@ -7,10 +10,23 @@ pub fn name(_: *Type) []const u8 { return " tab size"; } -pub const preview = goto; -pub const apply = goto; -pub const cancel = goto; - -fn goto(self: *Type) void { - command.executeName("set_tab_width", command.fmt(.{self.input orelse self.start})) catch {}; +pub fn start(self: *Type) usize { + const tab_width = if (tui.get_active_editor()) |editor| editor.tab_width else tui.config().tab_width; + self.input = tab_width; + return tab_width; +} + +const default_cmd = "set_editor_tab_width"; + +pub const cancel = preview; + +pub fn preview(self: *Type, _: command.Context) void { + command.executeName(default_cmd, command.fmt(.{self.input orelse self.start})) catch {}; +} + +pub fn apply(self: *Type, ctx: command.Context) void { + var cmd: []const u8 = undefined; + if (!(ctx.args.match(.{cbor.extract(&cmd)}) catch false)) + cmd = default_cmd; + command.executeName(cmd, command.fmt(.{self.input orelse self.start})) catch {}; } From 4037d67fe9037565f599cabb16deb8a48f534efa Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Tue, 12 Aug 2025 12:29:08 +0200 Subject: [PATCH 3/5] feat: add support for session local tab_width setting --- src/tui/editor.zig | 2 +- src/tui/mode/mini/tab_width.zig | 2 +- src/tui/tui.zig | 6 ++++++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/tui/editor.zig b/src/tui/editor.zig index b9fce38..911a5b2 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -469,7 +469,7 @@ pub const Editor = struct { fn init(self: *Self, allocator: Allocator, n: Plane, buffer_manager: *Buffer.Manager) void { const logger = log.logger("editor"); const frame_rate = tp.env.get().num("frame-rate"); - const tab_width = tui.config().tab_width; + const tab_width = tui.get_tab_width(); const indent_mode = tui.config().indent_mode; const indent_size = if (indent_mode == .tabs) tab_width else tui.config().indent_size; self.* = Self{ diff --git a/src/tui/mode/mini/tab_width.zig b/src/tui/mode/mini/tab_width.zig index a617973..53e4868 100644 --- a/src/tui/mode/mini/tab_width.zig +++ b/src/tui/mode/mini/tab_width.zig @@ -11,7 +11,7 @@ pub fn name(_: *Type) []const u8 { } pub fn start(self: *Type) usize { - const tab_width = if (tui.get_active_editor()) |editor| editor.tab_width else tui.config().tab_width; + const tab_width = if (tui.get_active_editor()) |editor| editor.tab_width else tui.get_tab_width(); self.input = tab_width; return tab_width; } diff --git a/src/tui/tui.zig b/src/tui/tui.zig index f762102..84ecc1e 100644 --- a/src/tui/tui.zig +++ b/src/tui/tui.zig @@ -24,6 +24,7 @@ allocator: Allocator, rdr_: renderer, config_: @import("config"), config_bufs: [][]const u8, +session_tab_width: ?usize = null, highlight_columns_: []const u16, highlight_columns_configured: []const u16, frame_time: usize, // in microseconds @@ -1122,6 +1123,11 @@ pub fn config() *const @import("config") { return ¤t().config_; } +pub fn get_tab_width() usize { + const self = current(); + return self.session_tab_width orelse self.config_.tab_width; +} + pub fn highlight_columns() []const u16 { return current().highlight_columns_; } From 80002e4d6ba31bf69deb5b978e8137f1398a99dc Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Tue, 12 Aug 2025 12:54:34 +0200 Subject: [PATCH 4/5] feat: add set_buffer_tab_width and set_session_tab_width commands Also, fold the tab_width and set_tab_width commands into one. The default command (set_tab_width) now stores the tab_width in the persistent config. --- src/tui/editor.zig | 4 ++-- src/tui/tui.zig | 35 ++++++++++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/tui/editor.zig b/src/tui/editor.zig index 911a5b2..03b5900 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -711,14 +711,14 @@ pub const Editor = struct { } } - pub fn set_tab_width(self: *Self, ctx: Context) Result { + pub fn set_editor_tab_width(self: *Self, ctx: Context) Result { var tab_width: usize = 0; if (!try ctx.args.match(.{tp.extract(&tab_width)})) return error.InvalidSetTabWidthArgument; self.tab_width = tab_width; self.refresh_tab_width(); } - pub const set_tab_width_meta: Meta = .{ .arguments = &.{.integer} }; + pub const set_editor_tab_width_meta: Meta = .{ .arguments = &.{.integer} }; fn close(self: *Self) !void { var meta = std.ArrayListUnmanaged(u8).empty; diff --git a/src/tui/tui.zig b/src/tui/tui.zig index 84ecc1e..70bc51d 100644 --- a/src/tui/tui.zig +++ b/src/tui/tui.zig @@ -745,10 +745,39 @@ const cmds = struct { } pub const force_terminate_meta: Meta = .{ .description = "Force quit without saving" }; - pub fn tab_width(self: *Self, ctx: Ctx) Result { - return enter_mini_mode(self, @import("mode/mini/tab_width.zig"), ctx); + pub fn set_tab_width(self: *Self, ctx: Ctx) Result { + var tab_width: usize = 0; + if (!try ctx.args.match(.{tp.extract(&tab_width)})) + return enter_mini_mode(self, @import("mode/mini/tab_width.zig"), Ctx.fmt(.{"set_tab_width"})); + + self.config_.tab_width = tab_width; + self.session_tab_width = null; + command.executeName("set_editor_tab_width", ctx) catch {}; + try save_config(); + self.logger.print("tab width {}", .{tab_width}); } - pub const tab_width_meta: Meta = .{ .description = "Set tab width" }; + pub const set_tab_width_meta: Meta = .{ .description = "Set tab width" }; + + pub fn set_buffer_tab_width(self: *Self, ctx: Ctx) Result { + var tab_width: usize = 0; + if (!try ctx.args.match(.{tp.extract(&tab_width)})) + return enter_mini_mode(self, @import("mode/mini/tab_width.zig"), Ctx.fmt(.{"set_buffer_tab_width"})); + + command.executeName("set_editor_tab_width", ctx) catch {}; + self.logger.print("buffer tab width {}", .{tab_width}); + } + pub const set_buffer_tab_width_meta: Meta = .{ .description = "Set tab width for current buffer" }; + + pub fn set_session_tab_width(self: *Self, ctx: Ctx) Result { + var tab_width: usize = 0; + if (!try ctx.args.match(.{tp.extract(&tab_width)})) + return enter_mini_mode(self, @import("mode/mini/tab_width.zig"), Ctx.fmt(.{"set_session_tab_width"})); + + self.session_tab_width = tab_width; + command.executeName("set_editor_tab_width", ctx) catch {}; + self.logger.print("session tab width {}", .{tab_width}); + } + pub const set_session_tab_width_meta: Meta = .{ .description = "Set tab width for current session" }; pub fn set_theme(self: *Self, ctx: Ctx) Result { var name: []const u8 = undefined; From 21fe6103bf86c53db0643c50606124fea4c2dc16 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Tue, 12 Aug 2025 13:04:35 +0200 Subject: [PATCH 5/5] feat: add open_most_recent_file command --- src/tui/mainview.zig | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/tui/mainview.zig b/src/tui/mainview.zig index 2ac5a5f..c726c6f 100644 --- a/src/tui/mainview.zig +++ b/src/tui/mainview.zig @@ -938,6 +938,12 @@ const cmds = struct { } pub const open_previous_file_meta: Meta = .{ .description = "Open the previous file" }; + pub fn open_most_recent_file(self: *Self, _: Ctx) Result { + if (try project_manager.request_most_recent_file(self.allocator)) |file_path| + self.show_file_async(file_path); + } + pub const open_most_recent_file_meta: Meta = .{ .description = "Open the last changed file" }; + pub fn system_paste(self: *Self, _: Ctx) Result { if (builtin.os.tag == .windows) { const text = try @import("renderer").request_windows_clipboard(self.allocator);