fix: move internal clipboard from buffer local to session wide

closes #287
This commit is contained in:
CJ van den Berg 2025-08-22 22:18:57 +02:00
parent a227eb925c
commit 5286975257
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
3 changed files with 21 additions and 16 deletions

View file

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

View file

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

View file

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