From 5d6086a1b11cfb66e99a7a6b3aa9ce2147c2cb9d Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 9 Aug 2023 14:44:24 -0700 Subject: [PATCH] "copy-on-select" configuation to disable --- src/Surface.zig | 20 ++++++++++++++++++-- src/config.zig | 25 +++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/Surface.zig b/src/Surface.zig index 51960d864..572fbc721 100644 --- a/src/Surface.zig +++ b/src/Surface.zig @@ -144,6 +144,7 @@ const DerivedConfig = struct { clipboard_read: bool, clipboard_write: bool, clipboard_trim_trailing_spaces: bool, + copy_on_select: configpkg.CopyOnSelect, confirm_close_surface: bool, mouse_interval: u64, macos_non_native_fullscreen: bool, @@ -159,6 +160,7 @@ const DerivedConfig = struct { .clipboard_read = config.@"clipboard-read", .clipboard_write = config.@"clipboard-write", .clipboard_trim_trailing_spaces = config.@"clipboard-trim-trailing-spaces", + .copy_on_select = config.@"copy-on-select", .confirm_close_surface = config.@"confirm-close-surface", .mouse_interval = config.@"click-repeat-interval" * 1_000_000, // 500ms .macos_non_native_fullscreen = config.@"macos-non-native-fullscreen", @@ -833,6 +835,13 @@ fn setSelection(self: *Surface, sel_: ?terminal.Selection) void { const prev_ = self.io.terminal.screen.selection; self.io.terminal.screen.selection = sel_; + // Determine the clipboard we want to copy selection to, if it is enabled. + const clipboard: apprt.Clipboard = switch (self.config.copy_on_select) { + .false => return, + .true => .selection, + .clipboard => .standard, + }; + // Set our selection clipboard. If the selection is cleared we do not // clear the clipboard. If the selection is set, we only set the clipboard // again if it changed, since setting the clipboard can be an expensive @@ -850,7 +859,7 @@ fn setSelection(self: *Surface, sel_: ?terminal.Selection) void { }; defer self.alloc.free(buf); - self.rt_surface.setClipboardString(buf, .selection) catch |err| { + self.rt_surface.setClipboardString(buf, clipboard) catch |err| { log.err("error setting clipboard string err={}", .{err}); return; }; @@ -1697,7 +1706,14 @@ pub fn mouseButtonCallback( // Middle-click pastes from our selection clipboard if (button == .middle and action == .press) { - try self.clipboardPaste(.selection, false); + if (self.config.copy_on_select != .false) { + const clipboard: apprt.Clipboard = switch (self.config.copy_on_select) { + .true => .selection, + .clipboard => .standard, + .false => unreachable, + }; + try self.clipboardPaste(clipboard, false); + } } } diff --git a/src/config.zig b/src/config.zig index b5afc0684..ae41072a0 100644 --- a/src/config.zig +++ b/src/config.zig @@ -186,6 +186,17 @@ pub const Config = struct { /// This does not affect data sent to the clipboard via "clipboard-write". @"clipboard-trim-trailing-spaces": bool = true, + /// Whether to automatically copy selected text to the clipboard. "true" + /// will only copy on systems that support a selection clipboard. + /// + /// The value "clipboard" will copy to the system clipboard, making this + /// work on macOS. Note that middle-click will also paste from the system + /// clipboard in this case. + /// + /// Note that if this is disabled, middle-click paste will also be + /// disabled. + @"copy-on-select": CopyOnSelect = .true, + /// The time in milliseconds between clicks to consider a click a repeat /// (double, triple, etc.) or an entirely new single click. A value of /// zero will use a platform-specific default. The default on macOS @@ -1375,6 +1386,20 @@ pub const Keybinds = struct { } }; +/// Options for copy on select behavior. +pub const CopyOnSelect = enum { + /// Disables copy on select entirely. + false, + + /// Copy on select is enabled, but goes to the selection clipboard. + /// This is not supported on platforms such as macOS. This is the default. + true, + + /// Copy on select is enabled and goes to the system clipboard. + clipboard, +}; + +/// Shell integration values pub const ShellIntegration = enum { none, detect,