diff options
author | Jesse Luehrs <doy@tozt.net> | 2018-11-10 17:12:57 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2018-11-10 17:12:57 -0500 |
commit | 3499152841e851e26562164de5ec9d71081583c3 (patch) | |
tree | 93c993803a8f2b5c483ebcffa878059f33e2bfec | |
parent | 26ccecfd2f8b00cc03a16145de7d83514dfc99a8 (diff) | |
download | conf-3499152841e851e26562164de5ec9d71081583c3.tar.gz conf-3499152841e851e26562164de5ec9d71081583c3.zip |
simplify tmux-clipboard a bunch
-rwxr-xr-x | bin/tmux-clipboard | 96 | ||||
-rw-r--r-- | tmux.conf | 7 |
2 files changed, 53 insertions, 50 deletions
diff --git a/bin/tmux-clipboard b/bin/tmux-clipboard index 338d2a2..2dc5a1a 100755 --- a/bin/tmux-clipboard +++ b/bin/tmux-clipboard @@ -1,68 +1,74 @@ #!/usr/bin/env perl use strict; use warnings; -use 5.014; +use 5.020; +use feature 'signatures'; +no warnings 'experimental::signatures'; 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"); +sub main($cmd, $selection) { + if ($cmd eq 'copy') { + tmux_copy($selection); + } + elsif ($cmd eq 'paste') { + tmux_paste($selection); } else { - close STDOUT; - close STDERR; + die "usage: $0 [copy|paste]"; + } +} - my $copy_cmd = COPY_CMD; - if ($Config{osname} eq 'linux') { - $copy_cmd .= " -selection $selection"; - } +sub tmux_copy($selection='primary') { + set_clipboard_contents(get_tmux_buffer(), $selection); +} - open my $clipboard, '|-', $copy_cmd - or die "can't copy from clipboard using $copy_cmd: $!"; - $clipboard->print($fifo_file->slurp); - $clipboard->close; - } +sub tmux_paste($selection='primary') { + write_to_tmux(get_clipboard_contents($selection)); } -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"); +sub set_clipboard_contents($contents, $selection) { + my $copy_cmd = COPY_CMD; + if ($Config{osname} eq 'linux') { + $copy_cmd .= " -selection $selection"; } - else { - close STDOUT; - close STDERR; - my $paste_cmd = PASTE_CMD; - if ($Config{osname} eq 'linux') { - $paste_cmd .= " -selection $selection"; - } + open my $clipboard, '|-', $copy_cmd + or die "can't set clipboard contents using `$copy_cmd`: $!"; + print $clipboard $contents; + close $clipboard; +} - 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); +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 } -else { - die "usage: $0 [copy|paste]"; + +sub get_tmux_buffer { + `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) @@ -42,11 +42,8 @@ bind -T copy-mode-vi v send-keys -X begin-selection bind -T copy-mode-vi ) send-keys -X start-of-line bind -T copy-mode-vi _ send-keys -X back-to-indentation -bind -T copy-mode-vi y run "tmux send-keys -X copy-selection-and-cancel; tmux-clipboard copy primary" -bind -T copy-mode-vi ^C run "tmux send-keys -X copy-selection-and-cancel; tmux-clipboard copy clipboard" -bind y run "tmux-clipboard copy primary" -bind ^C run "tmux-clipboard copy clipboard" -bind IC run "tmux-clipboard paste primary" +bind -T copy-mode-vi y run "tmux send-keys -X copy-selection-and-cancel; tmux run -b 'tmux-clipboard copy primary'" +bind -T copy-mode-vi ^C run "tmux send-keys -X copy-selection-and-cancel; tmux run -b 'tmux-clipboard copy clipboard'" bind ^V run "tmux-clipboard paste clipboard" unbind ^B |