Skip to content

Commit a7ec94e

Browse files
committed
fix copy on select
1 parent d4190c9 commit a7ec94e

File tree

1 file changed

+58
-60
lines changed

1 file changed

+58
-60
lines changed

src/Surface.zig

+58-60
Original file line numberDiff line numberDiff line change
@@ -1393,67 +1393,7 @@ fn clipboardWrite(self: *const Surface, data: []const u8, loc: apprt.Clipboard)
13931393
///
13941394
/// This must be called with the renderer mutex held.
13951395
fn setSelection(self: *Surface, sel_: ?terminal.Selection) !void {
1396-
const prev_ = self.io.terminal.screen.selection;
13971396
try self.io.terminal.screen.select(sel_);
1398-
1399-
// If copy on select is false then exit early.
1400-
if (self.config.copy_on_select == .false) return;
1401-
1402-
// Set our selection clipboard. If the selection is cleared we do not
1403-
// clear the clipboard. If the selection is set, we only set the clipboard
1404-
// again if it changed, since setting the clipboard can be an expensive
1405-
// operation.
1406-
const sel = sel_ orelse return;
1407-
if (prev_) |prev| if (sel.eql(prev)) return;
1408-
1409-
const buf = self.io.terminal.screen.selectionString(self.alloc, .{
1410-
.sel = sel,
1411-
.trim = self.config.clipboard_trim_trailing_spaces,
1412-
}) catch |err| {
1413-
log.err("error reading selection string err={}", .{err});
1414-
return;
1415-
};
1416-
defer self.alloc.free(buf);
1417-
1418-
// Set the clipboard. This is not super DRY but it is clear what
1419-
// we're doing for each setting without being clever.
1420-
switch (self.config.copy_on_select) {
1421-
.false => unreachable, // handled above with an early exit
1422-
1423-
// Both standard and selection clipboards are set.
1424-
.clipboard => {
1425-
const clipboards: []const apprt.Clipboard = &.{ .standard, .selection };
1426-
for (clipboards) |clipboard| self.rt_surface.setClipboardString(
1427-
buf,
1428-
clipboard,
1429-
false,
1430-
) catch |err| {
1431-
log.err(
1432-
"error setting clipboard string clipboard={} err={}",
1433-
.{ clipboard, err },
1434-
);
1435-
};
1436-
},
1437-
1438-
// The selection clipboard is set if supported, otherwise the standard.
1439-
.true => {
1440-
const clipboard: apprt.Clipboard = if (self.rt_surface.supportsClipboard(.selection))
1441-
.selection
1442-
else
1443-
.standard;
1444-
1445-
self.rt_surface.setClipboardString(
1446-
buf,
1447-
clipboard,
1448-
false,
1449-
) catch |err| {
1450-
log.err(
1451-
"error setting clipboard string clipboard={} err={}",
1452-
.{ clipboard, err },
1453-
);
1454-
};
1455-
},
1456-
}
14571397
}
14581398

14591399
/// Change the cell size for the terminal grid. This can happen as
@@ -2879,6 +2819,64 @@ pub fn mouseButtonCallback(
28792819
}
28802820
}
28812821

2822+
// Set the clipboard if configured with copy_on_select
2823+
if (button == .left and
2824+
action == .release and
2825+
self.config.copy_on_select != .false)
2826+
set_clipboard: {
2827+
self.renderer_state.mutex.lock();
2828+
defer self.renderer_state.mutex.unlock();
2829+
2830+
// Only update clipboard if something is actually selected.
2831+
const selection = self.io.terminal.screen.selection orelse break :set_clipboard;
2832+
2833+
const buf = self.io.terminal.screen.selectionString(self.alloc, .{
2834+
.sel = selection,
2835+
.trim = self.config.clipboard_trim_trailing_spaces,
2836+
}) catch |err| {
2837+
log.err("error reading selection string err={}", .{err});
2838+
break :set_clipboard;
2839+
};
2840+
defer self.alloc.free(buf);
2841+
2842+
switch (self.config.copy_on_select) {
2843+
.false => unreachable, // should not be in this block at all
2844+
// Both standard and selection clipboards are set.
2845+
.clipboard => {
2846+
const clipboards: []const apprt.Clipboard = &.{ .standard, .selection };
2847+
for (clipboards) |clipboard| self.rt_surface.setClipboardString(
2848+
buf,
2849+
clipboard,
2850+
false,
2851+
) catch |err| {
2852+
log.err(
2853+
"error setting clipboard string clipboard={} err={}",
2854+
.{ clipboard, err },
2855+
);
2856+
break :set_clipboard;
2857+
};
2858+
},
2859+
.true => {
2860+
const clipboard: apprt.Clipboard = if (self.rt_surface.supportsClipboard(.selection))
2861+
.selection
2862+
else
2863+
.standard;
2864+
2865+
self.rt_surface.setClipboardString(
2866+
buf,
2867+
clipboard,
2868+
false,
2869+
) catch |err| {
2870+
log.err(
2871+
"error setting clipboard string clipboard={} err={}",
2872+
.{ clipboard, err },
2873+
);
2874+
break :set_clipboard;
2875+
};
2876+
},
2877+
}
2878+
}
2879+
28822880
// Report mouse events if enabled
28832881
{
28842882
self.renderer_state.mutex.lock();

0 commit comments

Comments
 (0)