Compare commits

...

5 commits

Author SHA1 Message Date
21fe6103bf
feat: add open_most_recent_file command 2025-08-12 13:04:35 +02:00
80002e4d6b
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.
2025-08-12 12:54:34 +02:00
4037d67fe9
feat: add support for session local tab_width setting 2025-08-12 12:53:45 +02:00
63a527726a
feat: add support for arguments to mini/numeric_input modes 2025-08-12 12:04:36 +02:00
1fcec1bab5
feat: add support for numeric arguments in cli exec calls 2025-08-12 12:02:41 +02:00
7 changed files with 94 additions and 22 deletions

View file

@ -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 });
}

View file

@ -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{
@ -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;

View file

@ -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);

View file

@ -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 {};
}

View file

@ -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" };

View file

@ -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.get_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 {};
}

View file

@ -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
@ -744,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;
@ -1122,6 +1152,11 @@ pub fn config() *const @import("config") {
return &current().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_;
}