]> ruderich.org/simon Gitweb - fcscs/fcscs.git/blobdiff - bin/fcscs
debug: enable autoflush
[fcscs/fcscs.git] / bin / fcscs
index 35e50e3a4fe7891e9018df4edb30ab20ca7527cb..6e3d9cb0ca5800986ed8289fab9162c0f6400029 100755 (executable)
--- a/bin/fcscs
+++ b/bin/fcscs
@@ -62,6 +62,7 @@ Short overview of the general usage, details below:
         - ...
     - select mode (optional, URL mode is used on startup):
         - f: file paths
+        - i: IPs
         - u: URLs
         - ...
         - /: search mode
@@ -365,12 +366,13 @@ package Screen {
                 $fh = undef; # a failed open still writes a value to $fh
                 return;
             }
+            $fh->autoflush(1);
         }
 
         foreach (@args) {
             $_ = $self->encode($_);
         }
-        say $fh "$module: @args";
+        say $fh "$module: @args" or CORE::die $!;
         return;
     }
 
@@ -878,7 +880,7 @@ sub handler_yank {
     # Use a temporary file to prevent leaking the yanked data to other users
     # with the command line, e.g. ps aux or top.
     my ($fh, $tmp) = File::Temp::tempfile(); # dies on its own
-    print $fh $screen->encode($match->{value});
+    print $fh $screen->encode($match->{value}) or die $!;
     close $fh or die $!;
 
     if ($config->{setting}{multiplexer} eq 'screen') {
@@ -899,6 +901,28 @@ sub handler_yank {
     }
 
     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 {
@@ -967,15 +991,18 @@ settings see below):
 
     # Draw matches in blue.
     $config{attribute}{match_string} = color_pair(COLOR_BLUE, -1);
-    # Enable Vim-like 'smartcase', ignore case until an upper character is
+    # Draw numbers in bold yellow.
+    $config{attribute}{match_id} = color_pair(COLOR_YELLOW, -1)
+                                 | A_BOLD;
+    # Disable Vim-like 'smartcase', ignore case until an upper character is
     # searched.
-    $config{setting}{smartcase} = 1;
+    $config{setting}{smartcase} = 0;
 
     # Use chromium to open URLs if running under X, elinks otherwise.
     if (defined $ENV{DISPLAY}) {
         $config{setting}{browser} = ['chromium'];
     } else {
-        $config{setting}{browser} = ['elinks'];
+        $config{setting}{browser} = ['elinks', '-remote'];
     }
 
     # Let fcscs know the file was loaded successfully.
@@ -1012,7 +1039,7 @@ local $SIG{__WARN__} = sub {
 
 I<NOTE>: Mappings are split in two categories: Mode mappings which change the
 selection and may receive additional input (e.g. a search string) and simple
-mappings which only change some value. Mode mappings are configured via
+mappings which only change some config value. Mode mappings are configured via
 C<$config{mapping}{mode}>, simple mappings via C<$config{mapping}{simple}>.
 
 The following mode mappings are available by default (the function to remap
@@ -1042,6 +1069,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
@@ -1133,6 +1163,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>)
@@ -1156,6 +1188,7 @@ my %setting = (
     multiplexer        => undef,
     ignorecase         => 0,
     smartcase          => 1,
+    yank_x11           => 0,
     paste_sleep        => 100_000,
     screen_msgwait     => 5,
     # global mappings
@@ -1269,7 +1302,6 @@ Used as mappings, see L</MAPPINGS> above.
 
 Used as handler to yank, paste selection or open URL in browser.
 
-    debug()
     get_regex_matches()
     select_match()
     run_command()
@@ -1279,7 +1311,47 @@ Helper functions when writing custom mappings, see the source for details.
 
 Example:
 
-    TODO
+    # Enhance URL mode by updating the mapping.
+    $config{mapping}{mode}{u} = sub {
+        my ($key, $screen, $config, $input) = @_;
+
+        # First get matches of normal URL mode.
+        my $result = mapping_mode_url(@_);
+
+        # Add all strings matching "CVE-1234-1234" with URLs pointing to the
+        # Debian security tracker. "->{value}" is the string which is used as
+        # result of the match (e.g. the URL in this case).
+        my @matches = get_regex_matches($input, qr/\b(CVE-\d+-\d+)\b/);
+        foreach (@matches) {
+            $_->{value} = "https://security-tracker.debian.org/$_->{string}";
+        }
+        push @{$result->{matches}}, @matches;
+
+        # Change all YouTube links to use the custom "youtube" handler (see
+        # below). This will allow us to automatically open YouTube URLs with a
+        # custom program, like `youtube-dl` or `mpv`.
+        foreach (@{$result->{matches}}) {
+            if ($_->{string} =~ m{^https://www.youtube.com/}) {
+                $_->{handler} = $config{handler}{youtube};
+            }
+        }
+
+        return $result;
+    }
+    # Also update initial mode to use our new "URL mode".
+    $config{setting}{initial_mode} = $config{mapping}{mode}{u};
+
+    # Special handler to download YouTube URLs with `youtube-dl`. You could
+    # also use `mpv` here to immediately play them.
+    $config{handler}{youtube} = sub {
+        my ($screen, $config, $match) = @_;
+
+        return run_in_background($screen, sub {
+            run_command($screen, ['youtube-dl', $match->{value}]);
+        });
+    };
+
+
 
 =cut