blob: 338d2a25ecb85d3bf6719726818f7de4f58a3fdf (
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
|
#!/usr/bin/env perl
use strict;
use warnings;
use 5.014;
use Config;
use Path::Class;
use POSIX qw(mkfifo);
use constant {
FIFO_PATH => "$ENV{HOME}/.cache/tmux/clipboard/",
COPY_CMD => ($Config{osname} eq 'linux' ? 'xclip -i' : 'pbcopy'),
PASTE_CMD => ($Config{osname} eq 'linux' ? 'xclip -o' : 'pbpaste'),
};
my $fifo_file = file(FIFO_PATH, $$);
$fifo_file->dir->mkpath;
$fifo_file->remove;
END { $fifo_file->remove }
mkfifo("$fifo_file", 0700);
if ($ARGV[0] eq 'copy') {
my $selection = $ARGV[1] || 'primary';
if (fork) {
exec("tmux save-buffer -a $fifo_file");
}
else {
close STDOUT;
close STDERR;
my $copy_cmd = COPY_CMD;
if ($Config{osname} eq 'linux') {
$copy_cmd .= " -selection $selection";
}
open my $clipboard, '|-', $copy_cmd
or die "can't copy from clipboard using $copy_cmd: $!";
$clipboard->print($fifo_file->slurp);
$clipboard->close;
}
}
elsif ($ARGV[0] eq 'paste') {
my $selection = $ARGV[1] || 'primary';
if (fork) {
system("tmux load-buffer -b tmux-clipboard $fifo_file");
exec("tmux paste-buffer -b tmux-clipboard -dp");
}
else {
close STDOUT;
close STDERR;
my $paste_cmd = PASTE_CMD;
if ($Config{osname} eq 'linux') {
$paste_cmd .= " -selection $selection";
}
open my $clipboard, '-|', $paste_cmd
or die "can't paste from clipboard using $paste_cmd: $!";
my $contents = do { local $/; <$clipboard> };
$clipboard->close;
$fifo_file->spew(iomode => 'a', $contents);
}
}
else {
die "usage: $0 [copy|paste]";
}
|