diff --git a/src/tui/editor.zig b/src/tui/editor.zig index 305a3e6..c41b6d9 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -627,7 +627,7 @@ pub const Editor = struct { Widget.need_render(); } - pub fn buf_for_update(self: *Self) error{ Stop, OutOfMemory }!*const Buffer { + pub fn buf_for_update(self: *Self) !*const Buffer { if (!self.pause_undo) { self.cursels_saved.clearAndFree(self.allocator); self.cursels_saved = try self.cursels.clone(self.allocator); @@ -635,19 +635,19 @@ pub const Editor = struct { return self.buffer orelse error.Stop; } - pub fn buf_root(self: *const Self) error{Stop}!Buffer.Root { + pub fn buf_root(self: *const Self) !Buffer.Root { return if (self.buffer) |p| p.root else error.Stop; } - fn buf_eol_mode(self: *const Self) error{Stop}!Buffer.EolMode { + fn buf_eol_mode(self: *const Self) !Buffer.EolMode { return if (self.buffer) |p| p.file_eol_mode else error.Stop; } - fn buf_utf8_sanitized(self: *const Self) error{Stop}!bool { + fn buf_utf8_sanitized(self: *const Self) !bool { return if (self.buffer) |p| p.file_utf8_sanitized else error.Stop; } - fn buf_a(self: *const Self) error{Stop}!Allocator { + fn buf_a(self: *const Self) !Allocator { return if (self.buffer) |p| p.allocator else error.Stop; } @@ -2623,9 +2623,8 @@ pub const Editor = struct { primary.disable_selection(root, self.metrics); self.selection_mode = .word; primary.cursor.move_abs(root, &self.view, @intCast(y), @intCast(x), self.metrics) catch return; - self.selection_drag_initial = self.select_word_at_cursor(primary) catch |e| switch (e) { - error.Stop => primary.to_selection_normal(), - }; + _ = try self.select_word_at_cursor(primary); + self.selection_drag_initial = primary.selection; self.collapse_cursors(); self.clamp_mouse(); } @@ -2636,15 +2635,8 @@ pub const Editor = struct { primary.disable_selection(root, self.metrics); self.selection_mode = .line; primary.cursor.move_abs(root, &self.view, @intCast(y), @intCast(x), self.metrics) catch return; - blk: { - self.select_line_at_cursor(root, primary, .exclude_eol) catch |e| switch (e) { - error.Stop => { - self.selection_drag_initial = primary.to_selection_normal(); - break :blk; - }, - }; - self.selection_drag_initial = primary.selection; - } + try self.select_line_at_cursor(root, primary, .exclude_eol); + self.selection_drag_initial = primary.selection; self.collapse_cursors(); self.clamp_mouse(); } @@ -2660,23 +2652,23 @@ pub const Editor = struct { switch (self.selection_mode) { .char => {}, .word => { - if (sel.is_reversed()) { + if (sel.begin.right_of(sel.end)) { sel.begin = initial.end; - move_cursor_word_begin(root, &sel.end, self.metrics) catch {}; + with_selection_const(root, move_cursor_word_begin, primary, self.metrics) catch {}; } else { sel.begin = initial.begin; - move_cursor_word_end(root, &sel.end, self.metrics) catch {}; + with_selection_const(root, move_cursor_word_end, primary, self.metrics) catch {}; } }, .line => { - if (sel.is_reversed()) { + if (sel.begin.right_of(sel.end)) { sel.begin = initial.end; - move_cursor_begin(root, &sel.end, self.metrics) catch {}; + with_selection_const(root, move_cursor_begin, primary, self.metrics) catch {}; } else { sel.begin = initial.begin; blk: { - move_cursor_end(root, &sel.end, self.metrics) catch break :blk; - move_cursor_right(root, &sel.end, self.metrics) catch {}; + with_selection_const(root, move_cursor_end, primary, self.metrics) catch break :blk; + with_selection_const(root, move_cursor_right, primary, self.metrics) catch {}; } } }, @@ -2848,8 +2840,8 @@ pub const Editor = struct { fn copy_word_at_cursor(self: *Self, text_allocator: Allocator) ![]const u8 { const root = try self.buf_root(); const primary = self.get_primary(); - const sel = if (primary.selection) |sel| sel else try self.select_word_at_cursor(primary); - return try copy_selection(root, sel, text_allocator, self.metrics); + const sel = if (primary.selection) |*sel| sel else try self.select_word_at_cursor(primary); + return try copy_selection(root, sel.*, text_allocator, self.metrics); } pub fn cut_selection(self: *Self, root: Buffer.Root, cursel: *CurSel, text_allocator: Allocator) !struct { []const u8, Buffer.Root } { @@ -4539,7 +4531,7 @@ pub const Editor = struct { } pub const select_all_meta: Meta = .{ .description = "Select all" }; - fn select_word_at_cursor(self: *Self, cursel: *CurSel) error{Stop}!Selection { + fn select_word_at_cursor(self: *Self, cursel: *CurSel) !*Selection { const root = try self.buf_root(); const sel = cursel.enable_selection(root, self.metrics); defer cursel.check_selection(root, self.metrics); @@ -4547,7 +4539,7 @@ pub const Editor = struct { try move_cursor_word_begin(root, &sel.begin, self.metrics); move_cursor_word_end(root, &sel.end, self.metrics) catch {}; cursel.cursor = sel.end; - return sel.*; + return sel; } pub fn select_line_at_cursor(self: *Self, root: Buffer.Root, cursel: *CurSel, mode: enum { include_eol, exclude_eol, hold_cursor }) !void { diff --git a/src/tui/mainview.zig b/src/tui/mainview.zig index a68e0cd..f77291e 100644 --- a/src/tui/mainview.zig +++ b/src/tui/mainview.zig @@ -785,10 +785,10 @@ const cmds = struct { var existing = false; if (self.buffer_manager.get_buffer_for_file(file_path)) |new_buffer| { - if (buffer == new_buffer) - return tp.exit("same file"); if (new_buffer.is_dirty()) return tp.exit("save as would overwrite unsaved changes"); + if (buffer == new_buffer) + return tp.exit("same file"); existing = true; } try self.create_editor();