blob: dae796bef288d8f6b5599714954d557be6d504cd (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
#!/usr/bin/env perl
use strict;
use warnings;
use 5.020;
use feature 'signatures';
no warnings 'experimental::signatures';
use Config;
use constant {
COPY_CMD => ($Config{osname} eq 'linux' ? 'xclip -i' : 'pbcopy'),
PASTE_CMD => ($Config{osname} eq 'linux' ? 'xclip -o' : 'pbpaste'),
};
sub main($cmd, $selection) {
if ($cmd eq 'copy') {
tmux_copy($selection);
}
elsif ($cmd eq 'paste') {
tmux_paste($selection);
}
else {
die "usage: $0 [copy|paste]";
}
}
sub tmux_copy($selection='primary') {
set_clipboard_contents(get_tmux_buffer(), $selection);
}
sub tmux_paste($selection='primary') {
write_to_tmux(get_clipboard_contents($selection));
}
sub set_clipboard_contents($contents, $selection) {
my $copy_cmd = COPY_CMD;
if ($Config{osname} eq 'linux') {
$copy_cmd .= " -selection $selection";
}
open my $clipboard, '|-', $copy_cmd
or die "can't set clipboard contents using `$copy_cmd`: $!";
print $clipboard $contents;
close $clipboard;
}
sub get_clipboard_contents($selection) {
my $paste_cmd = PASTE_CMD;
if ($Config{osname} eq 'linux') {
$paste_cmd .= " -selection $selection";
}
open my $clipboard, '-|', $paste_cmd
or die "can't get clipboard contents using `$paste_cmd`: $!";
my $contents = do { local $/; <$clipboard> };
close $clipboard;
$contents
}
sub get_tmux_buffer {
scalar `tmux show-buffer`
}
sub write_to_tmux($contents) {
my $tmux_cmd = "tmux load-buffer -b tmux-clipboard -";
open my $tmux, '|-', $tmux_cmd
or die "can't set tmux buffer contents using `$tmux_cmd`: $!";
print $tmux $contents;
close $tmux;
system("tmux paste-buffer -b tmux-clipboard -dp");
}
main(@ARGV)
|