]> ruderich.org/simon Gitweb - fcscs/fcscs.git/blobdiff - bin/fcscs
allow yanking to X11 selection with new yank_x11 option
[fcscs/fcscs.git] / bin / fcscs
index c06db37987e69e80cccb9d8c706eb4993a81a783..da614bc396b3af1d6214d42aec7f425fc9fe118f 100755 (executable)
--- a/bin/fcscs
+++ b/bin/fcscs
@@ -445,7 +445,7 @@ sub get_regex_matches {
 
 
 sub run_command {
-    my ($screen, $config, $cmd) = @_;
+    my ($screen, $cmd) = @_;
 
     $screen->debug('run_command', "running @{$cmd}");
 
@@ -502,9 +502,11 @@ sub run_in_background {
 
         # Necessary for GNU screen or it'll keep the window open until an
         # external command has run.
-        close STDIN  or die $!;
-        close STDOUT or die $!;
-        close STDERR or die $!;
+        require File::Spec;
+        my $devnull = File::Spec->devnull();
+        open STDIN,  '<', $devnull or die $!;
+        open STDOUT, '>', $devnull or die $!;
+        open STDERR, '>', $devnull or die $!;
 
         # Double-fork to prevent zombies.
         my $pid = fork;
@@ -885,18 +887,40 @@ sub handler_yank {
         # GNU screen displays an annoying "Slurping X characters into buffer".
         # Use 'msgwait 0' as a hack to disable it.
         my $msgwait = $config->{setting}{screen_msgwait};
-        run_command($screen, $config, ['screen', '-X', 'msgwait', 0]);
-        run_command($screen, $config, ['screen', '-X', 'readbuf', $tmp]);
-        run_command($screen, $config, ['screen', '-X', 'msgwait', $msgwait]);
+        run_command($screen, ['screen', '-X', 'msgwait', 0]);
+        run_command($screen, ['screen', '-X', 'readbuf', $tmp]);
+        run_command($screen, ['screen', '-X', 'msgwait', $msgwait]);
     } elsif ($config->{setting}{multiplexer} eq 'tmux') {
         $screen->debug('handler_yank', 'using tmux');
 
-        run_command($screen, $config, ['tmux', 'load-buffer', $tmp]);
+        run_command($screen, ['tmux', 'load-buffer', $tmp]);
     } else {
         die 'unsupported multiplexer';
     }
 
     unlink $tmp or die $!;
+
+    if ($config->{setting}{yank_x11}) {
+        $screen->debug('handler_yank', 'setting X11 selection');
+
+        my @xsel_cmd  = qw( xsel --input --primary );
+        my @xclip_cmd = qw( xclip -in -selection primary );
+
+        my $fh;
+        {
+            # We don't care if a program doesn't exist.
+            no warnings;
+
+            if (not open $fh, '|-', @xsel_cmd) {
+                if (not open $fh, '|-', @xclip_cmd) {
+                    die "install xsel or xlip to yank to X11 selection\n";
+                }
+            }
+        }
+        print $fh $match->{value} or die $!;
+        close $fh or die $!;
+    }
+
     return;
 }
 sub handler_paste {
@@ -925,7 +949,7 @@ sub handler_paste {
         # Sleep until we switch back to the current window.
         Time::HiRes::usleep($config->{setting}{paste_sleep});
 
-        run_command($screen, $config, \@cmd);
+        run_command($screen, \@cmd);
     });
     return;
 }
@@ -936,7 +960,7 @@ sub handler_url {
 
     run_in_background($screen, sub {
         my @cmd = ( @{$config->{setting}{browser}}, $match->{value} );
-        run_command($screen, $config, \@cmd);
+        run_command($screen, \@cmd);
     });
     return;
 }
@@ -1040,6 +1064,9 @@ The following simple mappings are available by default:
 
 =back
 
+Note that yanking only uses the GNU screen or Tmux paste buffer by default. To
+also copy to X11 selection, enable the B<yank_x11> option.
+
 The following additional mappings are available by default:
 
 =over
@@ -1131,6 +1158,8 @@ Defaults in parentheses.
 
 =item B<smartcase>          ignore case unless one uppercase character is searched (C<1>)
 
+=item B<yank_x11>           copy selection also to X11 primary selection when yanking (C<0>)
+
 =item B<paste_sleep>        sleep x us before running paste command (C<100_000>)
 
 =item B<screen_msgwait>     GNU Screen's msgwait variable, used when yanking (C<5>)
@@ -1154,6 +1183,7 @@ my %setting = (
     multiplexer        => undef,
     ignorecase         => 0,
     smartcase          => 1,
+    yank_x11           => 0,
     paste_sleep        => 100_000,
     screen_msgwait     => 5,
     # global mappings
@@ -1219,8 +1249,7 @@ Example:
 
         if ($match->{value} =~ m{^https://www.youtube.com/}) {
             return run_in_background($screen, sub {
-                run_command($screen, $config,
-                            ['youtube-dl-wrapper', $match->{value}]);
+                run_command($screen, ['youtube-dl-wrapper', $match->{value}]);
             });
         }
         handler_url(@_);