From dfdbb4736781cd398256f6642a30027d2590d3a9 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Wed, 17 Apr 2024 18:08:11 +0200 Subject: [PATCH] feat: add close_file_without_saving command and keybinding (C-S-w) --- src/tui/editor.zig | 15 ++++++++++++++- src/tui/mode/input/flow.zig | 1 + src/tui/mode/input/vim/insert.zig | 1 + src/tui/mode/input/vim/normal.zig | 1 + src/tui/mode/input/vim/visual.zig | 1 + src/tui/mode/overlay/open_recent.zig | 1 + 6 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/tui/editor.zig b/src/tui/editor.zig index b1fdd3e..b250d27 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -377,8 +377,16 @@ pub const Editor = struct { } fn close(self: *Self) !void { + return self.close_internal(false); + } + + fn close_dirty(self: *Self) !void { + return self.close_internal(true); + } + + fn close_internal(self: *Self, allow_dirty_close: bool) !void { const b = if (self.buffer) |p| p else return error.Stop; - if (b.is_dirty()) return tp.exit("unsaved changes"); + if (!allow_dirty_close and b.is_dirty()) return tp.exit("unsaved changes"); if (self.buffer) |b_mut| b_mut.deinit(); self.buffer = null; self.plane.erase(); @@ -2855,6 +2863,11 @@ pub const Editor = struct { self.close() catch |e| return tp.exit_error(e); } + pub fn close_file_without_saving(self: *Self, _: command.Context) tp.result { + self.cancel_all_selections(); + self.close_dirty() catch |e| return tp.exit_error(e); + } + pub fn find(self: *Self, ctx: command.Context) tp.result { var query: []const u8 = undefined; if (ctx.args.match(.{tp.extract(&query)}) catch false) { diff --git a/src/tui/mode/input/flow.zig b/src/tui/mode/input/flow.zig index 05e1466..1caff3d 100644 --- a/src/tui/mode/input/flow.zig +++ b/src/tui/mode/input/flow.zig @@ -119,6 +119,7 @@ fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) tp.result { 'D' => self.cmd("dupe_down", .{}), 'Z' => self.cmd("redo", .{}), 'Q' => self.cmd("quit_without_saving", .{}), + 'W' => self.cmd("close_file_without_saving", .{}), 'R' => self.cmd("restart", .{}), 'F' => self.cmd("enter_find_in_files_mode", .{}), 'L' => self.cmd_async("add_cursor_all_matches"), diff --git a/src/tui/mode/input/vim/insert.zig b/src/tui/mode/input/vim/insert.zig index a13e1eb..dab0b4c 100644 --- a/src/tui/mode/input/vim/insert.zig +++ b/src/tui/mode/input/vim/insert.zig @@ -122,6 +122,7 @@ fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) tp.result { 'D' => self.cmd("dupe_down", .{}), 'Z' => self.cmd("redo", .{}), 'Q' => self.cmd("quit_without_saving", .{}), + 'W' => self.cmd("close_file_without_saving", .{}), 'R' => self.cmd("restart", .{}), 'F' => self.cmd("enter_find_in_files_mode", .{}), 'L' => self.cmd_async("add_cursor_all_matches"), diff --git a/src/tui/mode/input/vim/normal.zig b/src/tui/mode/input/vim/normal.zig index 35cae94..09a53c4 100644 --- a/src/tui/mode/input/vim/normal.zig +++ b/src/tui/mode/input/vim/normal.zig @@ -125,6 +125,7 @@ fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) tp.result { 'D' => self.cmd("dupe_down", .{}), 'Z' => self.cmd("redo", .{}), 'Q' => self.cmd("quit_without_saving", .{}), + 'W' => self.cmd("close_file_without_saving", .{}), 'R' => self.cmd("restart", .{}), 'F' => self.cmd("enter_find_in_files_mode", .{}), 'L' => self.cmd_async("add_cursor_all_matches"), diff --git a/src/tui/mode/input/vim/visual.zig b/src/tui/mode/input/vim/visual.zig index 996dd4e..7bc52ac 100644 --- a/src/tui/mode/input/vim/visual.zig +++ b/src/tui/mode/input/vim/visual.zig @@ -125,6 +125,7 @@ fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) tp.result { 'D' => self.cmd("dupe_down", .{}), 'Z' => self.cmd("redo", .{}), 'Q' => self.cmd("quit_without_saving", .{}), + 'W' => self.cmd("close_file_without_saving", .{}), 'R' => self.cmd("restart", .{}), 'F' => self.cmd("enter_find_in_files_mode", .{}), 'L' => self.cmd_async("add_cursor_all_matches"), diff --git a/src/tui/mode/overlay/open_recent.zig b/src/tui/mode/overlay/open_recent.zig index 5136c82..a9ea72f 100644 --- a/src/tui/mode/overlay/open_recent.zig +++ b/src/tui/mode/overlay/open_recent.zig @@ -232,6 +232,7 @@ fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) tp.result { }, nc.mod.CTRL | nc.mod.SHIFT => switch (keynormal) { 'Q' => self.cmd("quit_without_saving", .{}), + 'W' => self.cmd("close_file_without_saving", .{}), 'R' => self.cmd("restart", .{}), 'L' => self.cmd_async("toggle_logview"), 'I' => self.cmd_async("toggle_inputview"),