From 5286975257a9b04475513b2722f8b1b66cb763c9 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Fri, 22 Aug 2025 22:18:57 +0200 Subject: [PATCH] fix: move internal clipboard from buffer local to session wide closes #287 --- src/tui/editor.zig | 21 ++++++--------------- src/tui/mode/helix.zig | 2 +- src/tui/tui.zig | 14 ++++++++++++++ 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/tui/editor.zig b/src/tui/editor.zig index 393cd0e..06c0480 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -280,7 +280,6 @@ pub const Editor = struct { cursels_saved: CurSel.List = .empty, selection_mode: SelectMode = .char, selection_drag_initial: ?Selection = null, - clipboard: ?[]const u8 = null, target_column: ?Cursor = null, filter_: ?struct { before_root: Buffer.Root, @@ -385,9 +384,8 @@ pub const Editor = struct { } pub fn write_state(self: *const Self, writer: Buffer.MetaWriter) !void { - try cbor.writeArrayHeader(writer, 12); + try cbor.writeArrayHeader(writer, 11); try cbor.writeValue(writer, self.file_path orelse ""); - try cbor.writeValue(writer, self.clipboard orelse ""); try cbor.writeValue(writer, self.last_find_query orelse ""); try cbor.writeValue(writer, self.enable_format_on_save); try cbor.writeValue(writer, self.enable_auto_save); @@ -419,12 +417,10 @@ pub const Editor = struct { var file_path: []const u8 = undefined; var view_cbor: []const u8 = undefined; var cursels_cbor: []const u8 = undefined; - var clipboard: []const u8 = undefined; var last_find_query: []const u8 = undefined; var find_history: []const u8 = undefined; if (!try cbor.matchValue(iter, .{ tp.extract(&file_path), - tp.extract(&clipboard), tp.extract(&last_find_query), tp.extract(&self.enable_format_on_save), tp.extract(&self.enable_auto_save), @@ -440,7 +436,6 @@ pub const Editor = struct { self.refresh_tab_width(); if (op == .open_file) try self.open(file_path); - self.clipboard = if (clipboard.len > 0) try self.allocator.dupe(u8, clipboard) else null; self.last_find_query = if (last_find_query.len > 0) try self.allocator.dupe(u8, last_find_query) else null; const rows = self.view.rows; const cols = self.view.cols; @@ -2605,9 +2600,7 @@ pub const Editor = struct { pub const scroll_view_bottom_meta: Meta = .{}; fn set_clipboard(self: *Self, text: []const u8) void { - if (self.clipboard) |old| - self.allocator.free(old); - self.clipboard = text; + tui.set_clipboard(text); if (builtin.os.tag == .windows) { @import("renderer").copy_to_windows_clipboard(text) catch |e| self.logger.print_err("clipboard", "failed to set clipboard: {any}", .{e}); @@ -2616,10 +2609,8 @@ pub const Editor = struct { } } - pub fn set_clipboard_internal(self: *Self, text: []const u8) void { - if (self.clipboard) |old| - self.allocator.free(old); - self.clipboard = text; + pub fn set_clipboard_internal(_: *Self, text: []const u8) void { + tui.set_clipboard(text); } pub fn copy_selection(root: Buffer.Root, sel: Selection, text_allocator: Allocator, metrics: Buffer.Metrics) ![]u8 { @@ -2952,7 +2943,7 @@ pub const Editor = struct { pub fn paste(self: *Self, ctx: Context) Result { var text: []const u8 = undefined; if (!(ctx.args.buf.len > 0 and try ctx.args.match(.{tp.extract(&text)}))) { - if (self.clipboard) |text_| text = text_ else return; + if (tui.get_clipboard()) |text_| text = text_ else return; } self.logger.print("paste: {d} bytes", .{text.len}); const b = try self.buf_for_update(); @@ -2987,7 +2978,7 @@ pub const Editor = struct { pub fn paste_internal_vim(self: *Self, ctx: Context) Result { var text: []const u8 = undefined; if (!(ctx.args.buf.len > 0 and try ctx.args.match(.{tp.extract(&text)}))) { - if (self.clipboard) |text_| text = text_ else return; + if (tui.get_clipboard()) |text_| text = text_ else return; } self.logger.print("paste: {d} bytes", .{text.len}); diff --git a/src/tui/mode/helix.zig b/src/tui/mode/helix.zig index 84eaffe..80a998b 100644 --- a/src/tui/mode/helix.zig +++ b/src/tui/mode/helix.zig @@ -267,7 +267,7 @@ const cmds_ = struct { var text: []const u8 = undefined; if (!(ctx.args.buf.len > 0 and try ctx.args.match(.{tp.extract(&text)}))) { - if (ed.clipboard) |text_| text = text_ else return; + if (tui.get_clipboard()) |text_| text = text_ else return; } ed.logger.print("paste: {d} bytes", .{text.len}); diff --git a/src/tui/tui.zig b/src/tui/tui.zig index 810a723..7f0c172 100644 --- a/src/tui/tui.zig +++ b/src/tui/tui.zig @@ -64,6 +64,7 @@ fontfaces_: std.ArrayListUnmanaged([]const u8) = .{}, enable_mouse_idle_timer: bool = false, query_cache_: *syntax.QueryCache, frames_rendered_: usize = 0, +clipboard: ?[]const u8 = null, const keepalive = std.time.us_per_day * 365; // one year const idle_frames = 0; @@ -252,6 +253,7 @@ fn deinit(self: *Self) void { self.logger.deinit(); self.query_cache_.deinit(); root.free_config(self.allocator, self.config_bufs); + if (self.clipboard) |text| self.allocator.free(text); self.allocator.destroy(self); } @@ -1645,3 +1647,15 @@ fn widget_type_config_variable(widget_type: WidgetType) *ConfigWidgetStyle { .home => &config_.home_style, }; } + +pub fn get_clipboard() ?[]const u8 { + const self = current(); + return self.clipboard; +} + +pub fn set_clipboard(text: []const u8) void { + const self = current(); + if (self.clipboard) |old| + self.allocator.free(old); + self.clipboard = text; +}