]> ruderich.org/simon Gitweb - fcscs/fcscs.git/blob - bin/fcscs
allow yanking to X11 selection with new yank_x11 option
[fcscs/fcscs.git] / bin / fcscs
1 #!/usr/bin/perl
2
3 # fcscs - fast curses screen content select
4
5 # Copyright (C) 2013-2016  Simon Ruderich
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20
21 use strict;
22 use warnings;
23
24 use v5.10; # say, state
25
26 use Encode ();
27 use Fcntl ();
28 use I18N::Langinfo ();
29
30 use Curses ();
31
32 our $VERSION = '0.01';
33
34
35 =head1 NAME
36
37 fcscs - fast curses screen content select
38
39 =head1 SYNOPSIS
40
41 B<fcscs> [I<options>] I<path/to/screen/capture/file>
42
43 =head1 DESCRIPTION
44
45 B<fcscs> is a small tool which allows quick selection of terminal screen
46 contents (like URLs, paths, regex matches, etc.) and passes the selection to
47 GNU Screen's or Tmux's buffer or any other program. The selection can then
48 quickly be pasted, e.g. in the shell. Requires GNU Screen or Tmux. It's
49 licensed under the GPL 3 or later.
50
51 =head1 OPTIONS
52
53 None so far.
54
55 =head1 USAGE
56
57 Short overview of the general usage, details below:
58
59     - start fcscs
60     - configure actions (optional)
61         - enable pasting
62         - ...
63     - select mode (optional, URL mode is used on startup):
64         - f: file paths
65         - u: URLs
66         - ...
67         - /: search mode
68     - for `normal' modes:
69         - select match by displayed number or <return> for lowest numbered
70           match
71         - configured action is run, e.g. URL is opened with browser
72     - for `search' mode:
73         - perform incremental search
74         - on <return> go to `normal' mode to select a match
75         - after the match is selected wait for confirmation or extension
76         - confirmation: <return> run previously selected action
77         - extension: change match, e.g. select complete word or line
78
79 GNU Screen setup (add to F<~/.screenrc>):
80
81     bind ^B eval "hardcopy $HOME/.tmp/screen-fcscs" "screen fcscs $HOME/.tmp/screen-fcscs"
82
83 Tmux setup (add to F<~/.tmux.conf>):
84
85     bind-key C-b capture-pane \; save-buffer ~/.tmp/tmux-fcscs \; delete-buffer \; new-window "fcscs ~/.tmp/tmux-fcscs"
86
87 This requires a writable ~/.tmp directory. Adapt the mapping according to your
88 preferences. Ensure these files are not readable by others as they can contain
89 private data (umask or protected directory). B<fcscs> must be in your C<$PATH>
90 for the above mappings to work.
91
92 Pressing the configured mapping (Prefix Ctrl-B in this example) opens B<fcscs>
93 in a new GNU screen/Tmux window. After selection, the content is either passed
94 to external programs (e.g. for URLs) or copied to the paste buffer or directly
95 pasted in your previous window and the new window is closed.
96
97 To select a match just type its number. If the match is unique, the entry is
98 automatically selected (e.g. you press 2 and there are only 19 matches). If
99 there are multiple matches left (e.g. you press 1 and there are more than ten
100 matches available), press return to select the current match (1 in this case)
101 or another number to select the longer match. Use backspace to remove the last
102 entered number.
103
104 Press return before entering a number to select the last (lowest numbered)
105 match (underlined by default). To abort without selecting any match use "q".
106
107 To change the selection mode (e.g. paths, files, etc.) use one of the mappings
108 explained below. Per default URLs are selected, see options for a way to
109 change this.
110
111 I<NOTE>: Opening URLs in the browser passes the URL via the command line which
112 leaks URLs to other users on the current system via C<ps aux> or C<top>.
113
114 I<NOTE>: When yanking (copying) a temporary file is used to pass the data to
115 GNU screen/Tmux without exposing it to C<ps aux> or C<top>. However this may
116 leak data if those temporary files are written to disk. To prevent this change
117 your C<$TMP> to point to a memory-only location or encrypted storage.
118
119 If no window appears, try running B<fcscs> manually to catch the error message
120 and please report the bug:
121
122     fcscs /path/to/screen-or-tmux-fcscs-file
123
124
125 =head1 MODES
126
127 =cut
128
129
130 # CLASSES
131
132 # Helper class for drawing on the screen using Curses.
133 package Screen {
134     sub init {
135         my ($class, $encoding) = @_;
136
137         # Prefer strict UTF-8 handling (see perldoc Encode); just in case.
138         if (lc $encoding eq 'utf8') {
139             $encoding = 'UTF-8';
140         }
141         # Get Encode object to speed up decode()/encode().
142         my $encoding_object = Encode::find_encoding($encoding);
143         die "unsupported encoding '$encoding'" unless ref $encoding_object;
144
145         my $curses = Curses->new or die $!;
146
147         my $self = {
148             encoding        => $encoding,
149             encoding_object => $encoding_object,
150             curses          => $curses,
151             debug           => 0,
152             prompt          => {
153                 flags => undef,
154                 name  => undef,
155                 value => undef,
156             },
157         };
158         bless $self, $class;
159
160         Curses::start_color;
161         # Allow default colors by passing -1 to init_pair. A default color is
162         # not drawn on screen thus allowing terminals with pseudo-transparency
163         # to use the transparent background in place of the default color.
164         Curses::use_default_colors;
165
166         Curses::cbreak;
167         Curses::noecho;
168         $self->cursor(0);
169
170         return $self;
171     }
172     sub deinit {
173         my ($self) = @_;
174
175         Curses::nocbreak;
176         Curses::echo;
177         $self->cursor(1);
178
179         Curses::endwin;
180         return;
181     }
182
183     # Convert between Perl's internal encoding and the terminal's encoding.
184     sub encode {
185         my ($self, $string) = @_;
186         return $self->{encoding_object}->encode($string);
187     }
188     sub decode {
189         my ($self, $string) = @_;
190         return eval { # returns undef on decode failure
191             $self->{encoding_object}->decode($string, Encode::FB_CROAK);
192         };
193     }
194
195     # Create attribute for the given fore-/background colors.
196     sub color_pair {
197         my ($self, $fg, $bg) = @_;
198
199         state $next_color_pair = 1; # must start at 1 for init_pair()
200
201         Curses::init_pair($next_color_pair, $fg, $bg);
202         return Curses::COLOR_PAIR($next_color_pair++);
203     }
204
205     # Draw a string which must fit in the current line. Wrapping/clipping is
206     # not supported and must be handled by the caller.
207     sub draw_simple {
208         my ($self, $y, $x, $attributes, $string) = @_;
209
210         die if $string =~ /\n/;
211         # FIXME: wide characters
212         die if $x + length $string > $self->width;
213
214         $self->{curses}->attron($attributes) if defined $attributes;
215         $self->{curses}->addstr($y, $x, $self->encode($string));
216         $self->{curses}->attroff($attributes) if defined $attributes;
217         return;
218     }
219     # Like draw_simple(), but the string is automatically clipped.
220     sub draw_clipped {
221         my ($self, $y, $x, $attributes, $string) = @_;
222
223         # FIXME: wide characters
224         $string = substr $string, 0, $self->width - $x;
225         $self->draw_simple($y, $x, $attributes, $string);
226         return;
227     }
228     sub draw {
229         my ($self, $y, $x, $attributes, $string) = @_;
230
231         die unless defined $string;
232
233         while (1) {
234             my $offset;
235             # We must print each line separately. Search for next newline or
236             # line end, whichever is closer.
237             if ($string =~ /\n/) {
238                 $offset = $-[0];
239             }
240             # FIXME: wide characters
241             if ($x + length $string > $self->width) {
242                 my $new_offset = $self->width - $x;
243                 if (not defined $offset or $offset > $new_offset) {
244                     $offset = $new_offset;
245                 }
246             }
247             last unless defined $offset;
248
249             # FIXME: wide characters
250             $self->draw_simple($y, $x, $attributes, substr $string, 0, $offset);
251
252             # Don't draw "\n" itself.
253             if ("\n" eq substr $string, $offset, 1) {
254                 $offset++;
255             }
256
257             $string = substr $string, $offset;
258
259             $y++;
260             $x = 0;
261         }
262
263         $self->draw_simple($y, $x, $attributes, $string);
264         return $y;
265     }
266
267     sub draw_prompt {
268         my ($self, $config) = @_;
269
270         $self->debug('draw_prompt', 'started');
271
272         my $x = 0;
273         my $y = $self->height - 1;
274
275         # Clear line for better visibility.
276         $self->draw_simple($y, $x, undef, ' ' x $self->width);
277
278         # Draw prompt flags.
279         if (defined (my $s = $self->{prompt}{flags})) {
280             $s = "[$s]";
281             $self->debug('draw_prompt', $s);
282             $self->draw_clipped($y, $x, $config->{attribute}{prompt_flags}, $s);
283             $x += length($s) + 1; # space between next element
284         }
285         # Draw prompt name.
286         if (defined (my $s = $self->{prompt}{name})) {
287             $s = "[$s]";
288             $self->debug('draw_prompt', $s);
289             $self->draw_clipped($y, $x, $config->{attribute}{prompt_name}, $s);
290             $x += length($s) + 1;
291         }
292         # Draw prompt value, e.g. a search field.
293         if (defined (my $s = $self->{prompt}{value})) {
294             $self->debug('draw_prompt', $s);
295             $self->draw_clipped($y, $x, undef, $s);
296             $x += length($s) + 1;
297         }
298         return;
299     }
300
301     sub draw_matches {
302         my ($self, $config, $matches_remove, $matches_add) = @_;
303
304         foreach (@{$matches_remove}) {
305             $self->draw($_->{y}, $_->{x}, Curses::A_NORMAL, $_->{string});
306         }
307
308         my $attr_id     = $config->{attribute}{match_id};
309         my $attr_string = $config->{attribute}{match_string};
310         my $attr_last   = $config->{attribute}{match_last};
311
312         foreach (@{$matches_add}) {
313             my $attr = (defined $_->{id} and $_->{id} == 1)
314                      ? $attr_last
315                      : $attr_string;
316             $self->draw($_->{y}, $_->{x}, $attr, $_->{string});
317             if (defined $_->{id}) {
318                 $self->draw($_->{y}, $_->{x}, $attr_id, $_->{id});
319             }
320         }
321         return;
322     }
323
324     sub die {
325         my ($self, @args) = @_;
326
327         my $attr = $self->color_pair(Curses::COLOR_RED, -1) | Curses::A_BOLD;
328
329         # Clear the screen to improve visibility of the error message.
330         $self->{curses}->clear;
331
332         my $y = $self->draw(0, 0, $attr, "@args");
333
334         if ($self->{debug}) {
335             my $msg;
336             eval {
337                 require Devel::StackTrace;
338             };
339             if ($@) {
340                 $msg = "Devel::StackTrace missing, no stack trace.\n";
341             } else {
342                 my $trace = Devel::StackTrace->new;
343                 $msg = "Stack trace:\n" . $trace->as_string;
344             }
345             $y = $self->draw($y + 1, 0, Curses::A_NORMAL, $msg);
346         }
347
348         $self->draw($y + 1, 0, Curses::A_NORMAL,
349                     'Press any key to terminate fcscs.');
350         $self->refresh;
351
352         $self->getch;
353         $self->deinit;
354         exit 1;
355     }
356     sub debug {
357         my ($self, $module, @args) = @_;
358
359         return if not $self->{debug};
360
361         state $fh; # only open the file once per run
362         if (not defined $fh) {
363             # Ignore errors if the directory doesn't exist.
364             if (not open $fh, '>', "$ENV{HOME}/.config/fcscs/log") {
365                 $fh = undef; # a failed open still writes a value to $fh
366                 return;
367             }
368         }
369
370         foreach (@args) {
371             $_ = $self->encode($_);
372         }
373         say $fh "$module: @args";
374         return;
375     }
376
377
378     sub prompt {
379         my ($self, %settings) = @_;
380
381         foreach (keys %settings) {
382             CORE::die if not exists $self->{prompt}{$_};
383             $self->{prompt}{$_} = $settings{$_};
384         }
385         return;
386     }
387
388     # Wrapper for Curses.
389     sub width   { return $Curses::COLS; }
390     sub height  { return $Curses::LINES; }
391     sub refresh { return $_[0]->{curses}->refresh; }
392     sub getch   { return $_[0]->{curses}->getch; }
393     sub cursor  { Curses::curs_set($_[1]); return; }
394 }
395
396
397
398 # FUNCTIONS
399
400 sub prepare_input {
401     my ($screen, $input_ref) = @_;
402
403     # Make sure the input fits on the screen by removing the top lines if
404     # necessary.
405     splice @{$input_ref}, 0, -$screen->height;
406
407     # Pad each line with spaces to the screen width to correctly handle
408     # multi-line regexes.
409     # FIXME: wide characters
410     my @padded = map { sprintf '%-*s', $screen->width, $_ } @{$input_ref};
411
412     my $string = join "\n", @padded;
413     return {
414         string => $string,
415         lines  => $input_ref,
416         width  => $screen->width + 1,
417                   # + 1 = "\n", used in input_match_offset_to_coordinates
418     };
419 }
420
421 sub input_match_offset_to_coordinates {
422     my ($width, $offset) = @_;
423
424     die unless defined $offset;
425
426     my $y = int($offset / $width);
427     my $x = $offset - $y * $width;
428     return ($x, $y);
429 }
430
431 sub get_regex_matches {
432     my ($input, $regex) = @_;
433
434     my @matches;
435     while ($input->{string} =~ /$regex/g) {
436         my $offset = $-[1];
437         die "Match group required in regex '$regex'" if not defined $offset;
438
439         my ($x, $y) = input_match_offset_to_coordinates($input->{width},
440                                                         $offset);
441         push @matches, { x => $x, y => $y, offset => $offset, string => $1 };
442     }
443     return @matches;
444 }
445
446
447 sub run_command {
448     my ($screen, $cmd) = @_;
449
450     $screen->debug('run_command', "running @{$cmd}");
451
452     my $exit = do {
453         # Perl's system() combined with a $SIG{__WARN__} which die()s has
454         # issues due to the fork. The die() in the __WARN__ handler doesn't
455         # die but the program continues after the system().
456         #
457         # If the forked process fails to exec (e.g. program not found) then
458         # the __WARN__ handler is called (because a warning is about to be
459         # displayed) and the die() should display a message and terminate the
460         # process. But due to the fork it doesn't terminate the parent process
461         # and instead changes the return value of system(); it's no longer -1
462         # which makes it impossible to detect that case.
463         #
464         # Perl < 5.18 (found in 5.14) doesn't setup $$ during system() which
465         # makes it impossible to detect if the handler was called from inside
466         # the child.
467         #
468         # Instead, just ignore any warnings during the system(). Thanks to
469         # mauke in #perl on Freenode (2013-10-29 23:30 CET) for the idea to
470         # use no warnings and anno for testing a more recent Perl version with
471         # a working $$.
472         no warnings;
473
474         my @cmd = map { $screen->encode($_) } @{$cmd};
475         system { $cmd[0] } @cmd;
476     };
477     if ($exit != 0) {
478         my $msg;
479         if ($? == -1) {
480             $msg = 'failed to execute: ' . $!;
481         } elsif ($? & 127) {
482             $msg = 'killed by signal ' . ($? & 127);
483         } else {
484             $msg = 'exited with code ' . ($? >> 8);
485         }
486         die "system(@{$cmd}) $msg.";
487     }
488     return;
489 }
490 sub run_in_background {
491     my ($screen, $sub) = @_;
492
493     $screen->debug('run_in_background', "running $sub");
494
495     my $pid = fork;
496     defined $pid or die $!;
497
498     if ($pid == 0) {
499         # The terminal multiplexer sends a SIGHUP to the process when it
500         # closes the window (because the parent process has exited).
501         local $SIG{HUP} = 'IGNORE';
502
503         # Necessary for GNU screen or it'll keep the window open until an
504         # external command has run.
505         require File::Spec;
506         my $devnull = File::Spec->devnull();
507         open STDIN,  '<', $devnull or die $!;
508         open STDOUT, '>', $devnull or die $!;
509         open STDERR, '>', $devnull or die $!;
510
511         # Double-fork to prevent zombies.
512         my $pid = fork;
513         defined $pid or die $!;
514         if ($pid == 0) { # child
515             $sub->();
516         }
517         exit;
518     }
519     waitpid $pid, 0 or die $!;
520     return;
521 }
522
523
524 sub select_match {
525     my ($name, $screen, $config, $input, $matches) = @_;
526
527     $screen->debug('select_match', 'started');
528
529     return if @{$matches} == 0;
530     # Don't return on initial run to give the user a chance to select another
531     # mode, e.g. to switch from URL selection to search selection.
532     if (@{$matches} == 1 and not $config->{state}{initial}) {
533         return { match => $matches->[0] };
534     }
535     $config->{state}{initial} = 0;
536
537     my @sorted = sort { $b->{y} <=> $a->{y} or $b->{x} <=> $a->{x} } @{$matches};
538
539     my $i = 1;
540     foreach (@sorted) {
541         $_->{id} = $i++;
542     }
543
544     $screen->prompt(name => $name, value => undef);
545     $screen->draw_prompt($config);
546
547     $screen->draw_matches($config, [], $matches);
548     $screen->refresh;
549
550     my $number = 0;
551     while (1) {
552         my $char = $screen->getch;
553         if ($char =~ /^\d$/) {
554             $number = $number * 10 + $char;
555         } elsif ($char eq "\b" or $char eq "\x7f") { # backspace
556             $number = int($number / 10);
557         } elsif ($char eq "\n"
558                 or $char eq $config->{setting}{alternative_return}) {
559             if ($number == 0) { # number without selection matches last entry
560                 $number = 1;
561             }
562             last;
563
564         # Selecting a new mode requires falling through into the main input
565         # loop and then starting the new mode.
566         } elsif (defined $config->{mapping}{mode}{$char}) {
567             $screen->draw_matches($config, $matches, []); # clear matches
568             return { key => $char };
569         # All other mappings stay in the current mode.
570         } elsif (defined (my $m = $config->{mapping}{simple}{$char})) {
571             $m->($char, $screen, $config, $input);
572             next;
573
574         } else {
575             next; # ignore unknown mappings
576         }
577
578         last if $number > 0 and $number * 10 > @{$matches}; # unique match
579
580         my @remaining = $number == 0
581                       ? @{$matches}
582                       : grep { $_->{id} =~ /^$number/ } @{$matches};
583         $screen->draw_matches($config, $matches, \@remaining);
584         $screen->refresh;
585     }
586
587     $screen->draw_matches($config, $matches, []); # remove matches
588
589     foreach (@{$matches}) {
590         return { match => $_ } if $_->{id} == $number;
591     }
592     $screen->debug('select_match', 'no match selected');
593     return { match => undef };
594 }
595
596 sub extend_match_regex_left {
597     my ($line, $match, $regex) = @_;
598
599     my $s = reverse substr $line, 0, $match->{x};
600     if ($s =~ /^($regex)/) {
601         $match->{string}  = reverse($1) . $match->{string};
602         $match->{x}      -= length $1;
603         $match->{offset} -= length $1;
604     }
605     return;
606 }
607 sub extend_match_regex_right {
608     my ($line, $match, $regex) = @_;
609
610     my $s = substr $line, $match->{x} + length $match->{string};
611     if ($s =~ /^($regex)/) {
612         $match->{string} .= $1;
613     }
614     return;
615 }
616 sub extend_match {
617     my ($screen, $config, $input, $match) = @_;
618
619     $screen->debug('extend_match', 'started');
620
621     return if not defined $match;
622
623     $screen->prompt(name => 'extend', value => undef);
624     $screen->draw_prompt($config);
625
626     delete $match->{id}; # don't draw any match ids
627     $screen->draw_matches($config, [], [$match]);
628     $screen->refresh;
629
630     my $line = $input->{lines}[$match->{y}];
631
632     while (1) {
633         my $match_old = \%{$match};
634
635         my $char = $screen->getch;
636         if ($char eq "\n"
637                 or $char eq $config->{setting}{alternative_return}) {
638             last; # accept match
639
640         } elsif ($char eq 'w') { # select current word (both directions)
641             extend_match_regex_left($line,  $match, qr/\w+/);
642             extend_match_regex_right($line, $match, qr/\w+/);
643         } elsif ($char eq 'b') { # select current word (only left)
644             extend_match_regex_left($line,  $match, qr/\w+/);
645         } elsif ($char eq 'e') { # select current word (only right)
646             extend_match_regex_right($line, $match, qr/\w+/);
647
648         } elsif ($char eq 'W') { # select current WORD (both directions)
649             extend_match_regex_left($line,  $match, qr/\S+/);
650             extend_match_regex_right($line, $match, qr/\S+/);
651         } elsif ($char eq 'B') { # select current WORD (only left)
652             extend_match_regex_left($line,  $match, qr/\S+/);
653         } elsif ($char eq 'E') { # select current WORD (only right)
654             extend_match_regex_right($line, $match, qr/\S+/);
655
656         } elsif ($char eq '0') { # select to beginning of line
657             extend_match_regex_left($line, $match, qr/.+/);
658         } elsif ($char eq '$') { # select to end of line
659             extend_match_regex_right($line, $match, qr/.+/);
660
661         # Allow mode changes if not overwritten by local mappings.
662         } elsif (defined $config->{mapping}{mode}{$char}) {
663             $screen->draw_matches($config, [$match_old], []); # clear match
664             return { key => $char };
665
666         } else {
667             next; # ignore unknown mappings
668         }
669
670         $screen->draw_matches($config, [$match_old], [$match]);
671         $screen->refresh;
672     }
673
674     $screen->debug('extend_match', 'done');
675
676     return { match => $match };
677 }
678
679
680 sub mapping_paste {
681     my ($key, $screen, $config, $input) = @_;
682
683     $screen->debug('mapping_paste', 'started');
684
685     $config->{state}{handler} = $config->{handler}{paste};
686
687     $screen->prompt(flags => 'P'); # paste
688     $screen->draw_prompt($config);
689     $screen->refresh;
690
691     return {};
692 }
693 sub mapping_yank {
694     my ($key, $screen, $config, $input) = @_;
695
696     $screen->debug('mapping_yank', 'started');
697
698     $config->{state}{handler} = $config->{handler}{yank};
699
700     $screen->prompt(flags => 'Y'); # yank
701     $screen->draw_prompt($config);
702     $screen->refresh;
703
704     return {};
705 }
706
707
708 =head2 NORMAL MODES
709
710 Normal modes select matches by calling a function which returns them, e.g. by
711 using a regex.
712
713 The following normal modes are available:
714
715 =over 4
716
717 =item B<path mode> select relative/absolute paths
718
719 =item B<url mode>  select URLs
720
721 =item B<ip mode>   select IPv4 and IPv6 addresses
722
723 =back
724
725 =cut
726 sub mapping_mode_path {
727     my ($key, $screen, $config, $input) = @_;
728
729     $screen->debug('mapping_mode_path', 'started');
730
731     my @matches = get_regex_matches($input, $config->{regex}{path});
732     return {
733         select  => 'path select',
734         matches => \@matches,
735         handler => $config->{handler}{yank},
736     };
737 }
738 sub mapping_mode_url {
739     my ($key, $screen, $config, $input) = @_;
740
741     $screen->debug('mapping_mode_url', 'started');
742
743     my @matches = get_regex_matches($input, $config->{regex}{url});
744     return {
745         select  => 'url select',
746         matches => \@matches,
747         handler => $config->{handler}{url},
748     };
749 }
750 sub mapping_mode_ip {
751     my ($key, $screen, $config, $input) = @_;
752
753     $screen->debug('mapping_mode_ip', 'started');
754
755     my @ipv4 = get_regex_matches($input, $config->{regex}{ipv4});
756     my @ipv6 = get_regex_matches($input, $config->{regex}{ipv6});
757     return {
758         select  => 'ip select',
759         matches => [@ipv4, @ipv6],
760         handler => $config->{handler}{ip},
761     };
762 }
763
764 =head2 SEARCH MODE (AND EXTEND MODE)
765
766 Search mode is a special mode which lets you type a search string (a Perl
767 regex) and then select one of the matches. Afterwards you can extend the
768 match. For example select the complete word or to the end of the line. This
769 allows quick selection of arbitrary text.
770
771 The following mappings are available during the extension mode (not
772 configurable at the moment):
773
774 =over 4
775
776 =item B<w> select current word
777
778 =item B<b> extend word to the left
779
780 =item B<e> extend word to the right
781
782 =item B<W> select current WORD
783
784 =item B<B> extend WORD to the left
785
786 =item B<E> extend WORD to the right
787
788 =item B<0> extend to beginning of line
789
790 =item B<$> extend to end of line
791
792 =back
793
794 C<word> includes any characters matching C<\w+>, C<WORD> any non-whitespace
795 characters (C<\S+>), just like in Vim.
796
797 =cut
798 sub mapping_mode_search {
799     my ($key, $screen, $config, $input) = @_;
800
801     $screen->debug('mapping_mode_search', 'started');
802
803     $screen->cursor(1);
804
805     my $search = ''; # encoded
806     my @last_matches;
807     while (1) {
808         # getch doesn't return decoded characters but raw input bytes. Wait
809         # until the input character is complete.
810         my $value = $screen->decode($search);
811         $value = '' unless defined $value; # undef on decode failure
812
813         $screen->prompt(name => 'search', value => $value);
814         $screen->draw_prompt($config);
815         $screen->refresh;
816
817         my $char = $screen->getch;
818         # TODO: readline editing support
819         if ($char eq "\n") {
820             last;
821         } elsif ($char eq "\b" or $char eq "\x7f") { # backspace
822             # Remove a character, not a byte.
823             $search = $screen->decode($search);
824             chop $search;
825             $search = $screen->encode($search);
826         } else {
827             $search .= $char;
828             next unless defined $screen->decode($search);
829         }
830
831         my @matches;
832         if ($search ne '') {
833             my $case = '';
834             if (($config->{setting}{smartcase} and $search eq lc $search)
835                     or $config->{setting}{ignorecase}) {
836                 $case = '(?i)';
837             }
838             # Ignore invalid regexps.
839             # TODO: display warning on error?
840             eval {
841                 @matches = get_regex_matches($input, qr/($case$search)/);
842             };
843         }
844         $screen->draw_matches($config, \@last_matches, \@matches);
845         @last_matches = @matches;
846     }
847
848     $screen->cursor(0);
849
850     $screen->prompt(name => undef, value => undef); # clear prompt
851     $screen->draw_prompt($config);
852
853     $screen->debug('mapping_mode_search', 'done');
854
855     return {
856         select  => 'search',
857         matches => \@last_matches,
858         extend  => 1,
859         handler => $config->{handler}{yank},
860     };
861 }
862
863 sub mapping_quit {
864     my ($key, $screen, $config, $input) = @_;
865
866     # Key is necessary to fall through to main event loop which then quits.
867     return { key => $key, quit => 1 };
868 }
869
870
871 sub handler_yank {
872     my ($screen, $config, $match) = @_;
873
874     $screen->debug('handler_yank', 'started');
875
876     require File::Temp;
877
878     # Use a temporary file to prevent leaking the yanked data to other users
879     # with the command line, e.g. ps aux or top.
880     my ($fh, $tmp) = File::Temp::tempfile(); # dies on its own
881     print $fh $screen->encode($match->{value});
882     close $fh or die $!;
883
884     if ($config->{setting}{multiplexer} eq 'screen') {
885         $screen->debug('handler_yank', 'using screen');
886
887         # GNU screen displays an annoying "Slurping X characters into buffer".
888         # Use 'msgwait 0' as a hack to disable it.
889         my $msgwait = $config->{setting}{screen_msgwait};
890         run_command($screen, ['screen', '-X', 'msgwait', 0]);
891         run_command($screen, ['screen', '-X', 'readbuf', $tmp]);
892         run_command($screen, ['screen', '-X', 'msgwait', $msgwait]);
893     } elsif ($config->{setting}{multiplexer} eq 'tmux') {
894         $screen->debug('handler_yank', 'using tmux');
895
896         run_command($screen, ['tmux', 'load-buffer', $tmp]);
897     } else {
898         die 'unsupported multiplexer';
899     }
900
901     unlink $tmp or die $!;
902
903     if ($config->{setting}{yank_x11}) {
904         $screen->debug('handler_yank', 'setting X11 selection');
905
906         my @xsel_cmd  = qw( xsel --input --primary );
907         my @xclip_cmd = qw( xclip -in -selection primary );
908
909         my $fh;
910         {
911             # We don't care if a program doesn't exist.
912             no warnings;
913
914             if (not open $fh, '|-', @xsel_cmd) {
915                 if (not open $fh, '|-', @xclip_cmd) {
916                     die "install xsel or xlip to yank to X11 selection\n";
917                 }
918             }
919         }
920         print $fh $match->{value} or die $!;
921         close $fh or die $!;
922     }
923
924     return;
925 }
926 sub handler_paste {
927     my ($screen, $config, $match) = @_;
928
929     $screen->debug('handler_paste', 'started');
930
931     require Time::HiRes;
932
933     my @cmd;
934     if ($config->{setting}{multiplexer} eq 'screen') {
935         $screen->debug('handler_paste', 'using screen');
936         @cmd = qw( screen -X paste . );
937     } elsif ($config->{setting}{multiplexer} eq 'tmux') {
938         $screen->debug('handler_paste', 'using tmux');
939         @cmd = qw( tmux paste-buffer );
940     } else {
941         die 'unsupported multiplexer';
942     }
943
944     run_in_background($screen, sub {
945         # We need to get the data in the paste buffer before we can paste
946         # it.
947         handler_yank($screen, $config, $match);
948
949         # Sleep until we switch back to the current window.
950         Time::HiRes::usleep($config->{setting}{paste_sleep});
951
952         run_command($screen, \@cmd);
953     });
954     return;
955 }
956 sub handler_url {
957     my ($screen, $config, $match) = @_;
958
959     $screen->debug('handler_url', "opening $match->{value}");
960
961     run_in_background($screen, sub {
962         my @cmd = ( @{$config->{setting}{browser}}, $match->{value} );
963         run_command($screen, \@cmd);
964     });
965     return;
966 }
967
968
969
970 # CONFIGURATION DEFAULTS
971
972 =head1 CONFIGURATION
973
974 fcscs is configured through F<~/.fcscsrc> or F<~/.config/fcscs/fcscsrc> which
975 is a normal Perl script with all of Perl's usual features.
976
977 All configuration values are stored in the hash C<%config>. All manually
978 defined keys overwrite the default settings.
979
980 A simple F<~/.fcscsrc> could look like this (for details about the used
981 settings see below):
982
983     use strict;
984     use warnings;
985
986     use Curses; # for COLOR_* and A_* constants
987
988     our %config;
989
990     # Draw matches in blue.
991     $config{attribute}{match_string} = color_pair(COLOR_BLUE, -1);
992     # Enable Vim-like 'smartcase', ignore case until an upper character is
993     # searched.
994     $config{setting}{smartcase} = 1;
995
996     # Use chromium to open URLs if running under X, elinks otherwise.
997     if (defined $ENV{DISPLAY}) {
998         $config{setting}{browser} = ['chromium'];
999     } else {
1000         $config{setting}{browser} = ['elinks'];
1001     }
1002
1003     # Let fcscs know the file was loaded successfully.
1004     1;
1005
1006 =cut
1007
1008
1009 if (@ARGV != 1) {
1010     require Pod::Usage;
1011     Pod::Usage::pod2usage(2);
1012 }
1013
1014
1015 # Determine terminal encoding from the environment ($ENV{LANG} or similar).
1016 my $encoding = I18N::Langinfo::langinfo(I18N::Langinfo::CODESET);
1017
1018 my $screen = Screen->init($encoding);
1019
1020 # We must restore the screen before exiting.
1021 local $SIG{INT} = sub {
1022     $screen->deinit;
1023     exit 128 + 2;
1024 };
1025 # Make all warnings fatal to make sure they don't get lost (stderr is normally
1026 # not displayed).
1027 local $SIG{__WARN__} = sub {
1028     $screen->die('warning', @_);
1029 };
1030
1031
1032
1033 =head2 MAPPINGS
1034
1035 I<NOTE>: Mappings are split in two categories: Mode mappings which change the
1036 selection and may receive additional input (e.g. a search string) and simple
1037 mappings which only change some value. Mode mappings are configured via
1038 C<$config{mapping}{mode}>, simple mappings via C<$config{mapping}{simple}>.
1039
1040 The following mode mappings are available by default (the function to remap
1041 them in parentheses):
1042
1043 =over
1044
1045 =item B<f> select absolute/relative paths (C<\&mapping_mode_path>)
1046
1047 =item B<u> select URLs (C<\&mapping_mode_url>)
1048
1049 =item B<i> select IPv4 and IPv6 addresses (C<\&mapping_mode_ip>)
1050
1051 =item B</> search for regex to get selection (C<\&mapping_mode_search>)
1052
1053 =item B<q> quit fcscs (C<\&mapping_quit>)
1054
1055 =back
1056
1057 The following simple mappings are available by default:
1058
1059 =over
1060
1061 =item B<p> enable pasting (C<\&mapping_paste>)
1062
1063 =item B<y> enable yanking (copying) (C<\&mapping_yank>)
1064
1065 =back
1066
1067 Note that yanking only uses the GNU screen or Tmux paste buffer by default. To
1068 also copy to X11 selection, enable the B<yank_x11> option.
1069
1070 The following additional mappings are available by default:
1071
1072 =over
1073
1074 =item B<\n> accept current selection (not customizable)
1075
1076 =item B<s>  additional key to accept selection (B<alternative_return> option)
1077
1078 =back
1079
1080 All (single-byte) keys except numbers, backspace and return can be mapped.
1081
1082 Unknown mappings are ignored when pressing keys.
1083
1084 To remove a default mapping, delete it from the mapping hash.
1085
1086 Example:
1087
1088     # Map 'p' to select paths, 'P' to enable pasting.
1089     $config{mapping}{mode}{p} = \&mapping_mode_path;
1090     $config{mapping}{simple}{P} = \&mapping_paste;
1091
1092     # Disable 'f' mapping.
1093     delete $config{mapping}{mode}{f};
1094
1095 =cut
1096 my %mapping_mode = (
1097     f   => \&mapping_mode_path,
1098     u   => \&mapping_mode_url,
1099     i   => \&mapping_mode_ip,
1100     '/' => \&mapping_mode_search,
1101     q   => \&mapping_quit,
1102 );
1103 my %mapping_simple = (
1104     p => \&mapping_paste,
1105     y => \&mapping_yank,
1106 );
1107
1108 =head2 ATTRIBUTES
1109
1110 Attributes are used to style the output. They must be Curses attributes.
1111 Defaults in parentheses (foreground, background, attribute).
1112
1113 =over
1114
1115 =item B<match_id>      attribute for match numbers (red, default, bold)
1116
1117 =item B<match_string>  attribute for matches (yellow, default, normal)
1118
1119 =item B<match_last>    attribute for the match selected by return (yellow, default, underline)
1120
1121 =item B<prompt_name>   attribute for prompt name (standout)
1122
1123 =item B<prompt_flags>  attribute for prompt flags (standout)
1124
1125 =back
1126
1127 Example:
1128
1129     # Draw prompt flags in bold red with default background color.
1130     $config{attribute}{prompt_flags}
1131         = Curses::A_BOLD
1132         | color_pair(Curses::COLOR_RED, -1);
1133
1134 =cut
1135 my %attribute = (
1136     match_id     => $screen->color_pair(Curses::COLOR_RED, -1)
1137                     | Curses::A_BOLD,
1138     match_string => $screen->color_pair(Curses::COLOR_YELLOW, -1),
1139     match_last   => $screen->color_pair(Curses::COLOR_YELLOW, -1)
1140                     | Curses::A_UNDERLINE,
1141     prompt_name  => Curses::A_STANDOUT,
1142     prompt_flags => Curses::A_STANDOUT,
1143 );
1144
1145 =head2 SETTINGS
1146
1147 Defaults in parentheses.
1148
1149 =over
1150
1151 =item B<debug>              enable debug mode, writes to I<~/.config/fcscs/log> (C<0>)
1152
1153 =item B<initial_mode>       start in this mode, must be a valid mode mapping (C<\&mapping_mode_url>)
1154
1155 =item B<multiplexer>        set multiplexer ("screen" or "tmux") if not autodetected (C<undef>)
1156
1157 =item B<ignorecase>         ignore case when searching (C<0>)
1158
1159 =item B<smartcase>          ignore case unless one uppercase character is searched (C<1>)
1160
1161 =item B<yank_x11>           copy selection also to X11 primary selection when yanking (C<0>)
1162
1163 =item B<paste_sleep>        sleep x us before running paste command (C<100_000>)
1164
1165 =item B<screen_msgwait>     GNU Screen's msgwait variable, used when yanking (C<5>)
1166
1167 =item B<alternative_return> additional accept key like return, set to C<\n> to disable (C<s>)
1168
1169 =item B<browser>            browser command as array reference (C<['x-www-browser']>)
1170
1171 =back
1172
1173 Example:
1174
1175     # Select paths on startup instead of URLs.
1176     $config{setting}{initial_mode} = \&mapping_mode_path;
1177
1178 =cut
1179 my %setting = (
1180     # options
1181     debug              => 0,
1182     initial_mode       => \&mapping_mode_url,
1183     multiplexer        => undef,
1184     ignorecase         => 0,
1185     smartcase          => 1,
1186     yank_x11           => 0,
1187     paste_sleep        => 100_000,
1188     screen_msgwait     => 5,
1189     # global mappings
1190     alternative_return => 's',
1191     # commands
1192     browser            => ['x-www-browser'],
1193 );
1194
1195 =head2 REGEXPS
1196
1197 =over
1198
1199 =item B<url>  used by C<\&mapping_mode_url>
1200
1201 =item B<path> used by C<\&mapping_mode_path>
1202
1203 =item B<ipv4> used by C<\&mapping_mode_ip>
1204
1205 =item B<ipv6> used by C<\&mapping_mode_ip>
1206
1207 =back
1208
1209 Example:
1210
1211     # Select all non-whitespace characters when searching for paths.
1212     $config{regex}{path} = qr{(\S+)};
1213
1214 =cut
1215 my %regex = (
1216     # Taken from urlview's default configuration file, thanks.
1217     url  => qr{((?:(?:(?:http|https|ftp|gopher)|mailto):(?://)?[^ <>"\t]*|(?:www|ftp)[0-9]?\.[-a-z0-9.]+)[^ .,;\t\n\r<">\):]?[^, <>"\t]*[^ .,;\t\n\r<">\):])},
1218     path => qr{(~?[a-zA-Z0-9_./-]*/[a-zA-Z0-9_./-]+)},
1219     # IP addresses with optional prefix. Not perfectly accurate but good
1220     # enough.
1221     ipv4 => qr!\b(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(?:/\d{1,2})?)\b!,
1222     ipv6 => qr!\b((?:[0-9a-fA-F]{1,4})?(?::+[0-9a-fA-F]{1,4})+(?:/\d{1,3})?)\b!,
1223 );
1224
1225 =head2 HANDLERS
1226
1227 Handlers are used to perform actions on the selected string.
1228
1229 The following handlers are available, defaults in parentheses.
1230
1231 =over
1232
1233 =item B<yank>  used to yank (copy) selection to paste buffer (C<\&handler_yank>)
1234
1235 =item B<paste> used to paste selection into window (C<\&handler_paste>)
1236
1237 =item B<url>   used to open URLs (e.g. in a browser) (C<\&handler_url>)
1238
1239 =item B<ip>    used to handle IPs (C<\&handler_yank>)
1240
1241 =back
1242
1243 Example:
1244
1245     # Download YouTube videos with a custom wrapper, handle all other URLs
1246     # with the default URL handler.
1247     $config{handler}{url} = sub {
1248         my ($screen, $config, $match) = @_;
1249
1250         if ($match->{value} =~ m{^https://www.youtube.com/}) {
1251             return run_in_background($screen, sub {
1252                 run_command($screen, ['youtube-dl-wrapper', $match->{value}]);
1253             });
1254         }
1255         handler_url(@_);
1256     };
1257
1258 =cut
1259 my %handler = (
1260     yank  => \&handler_yank,
1261     paste => \&handler_paste,
1262     url   => \&handler_url,
1263     ip    => \&handler_yank,
1264 );
1265
1266 my %state = (
1267     initial => 1, # used by select_match() for 'initial_mode'
1268     handler => undef,
1269 );
1270
1271
1272
1273 # CONFIGURATION "API"
1274
1275 =head2 FUNCTIONS
1276
1277 The following functions are available:
1278
1279     color_pair($fg, $bg)
1280
1281 Create a new Curses attribute with the given fore- and background color.
1282
1283     mapping_mode_path()
1284     mapping_mode_url()
1285     mapping_mode_ip()
1286     mapping_mode_search()
1287
1288     mapping_paste()
1289     mapping_yank()
1290     mapping_quit()
1291
1292 Used as mappings, see L</MAPPINGS> above.
1293
1294     handler_yank()
1295     handler_paste()
1296     handler_url()
1297
1298 Used as handler to yank, paste selection or open URL in browser.
1299
1300     debug()
1301     get_regex_matches()
1302     select_match()
1303     run_command()
1304     run_in_background()
1305
1306 Helper functions when writing custom mappings, see the source for details.
1307
1308 Example:
1309
1310     TODO
1311
1312 =cut
1313
1314 # All variables and functions which are usable by ~/.fcscsrc.
1315 package Fcscs {
1316     our $screen; # "private"
1317     our %config;
1318
1319     sub color_pair { return $screen->color_pair(@_); }
1320
1321     sub mapping_mode_path { return main::mapping_mode_path(@_); }
1322     sub mapping_mode_url { return main::mapping_mode_url(@_); }
1323     sub mapping_mode_ip { return main::mapping_mode_ip(@_); }
1324     sub mapping_mode_search { return main::mapping_mode_search(@_); }
1325
1326     sub mapping_paste { return main::mapping_paste(@_); }
1327     sub mapping_yank { return main::mapping_yank(@_); }
1328     sub mapping_quit { return main::mapping_quit(@_); }
1329
1330     sub handler_yank { return main::handler_yank(@_); }
1331     sub handler_paste { return main::handler_paste(@_); }
1332     sub handler_url { return main::handler_url(@_); }
1333
1334     sub debug { return main::debug(@_); }
1335
1336     sub get_regex_matches { return main::get_regex_matches(@_); }
1337     sub select_match { return main::select_match(@_); }
1338
1339     sub run_command { return main::run_command(@_); }
1340     sub run_in_background { return main::run_in_background(@_); }
1341 }
1342 $Fcscs::screen = $screen;
1343
1344
1345
1346 # LOAD USER CONFIG
1347
1348 # Alias %config and %Fcscs::config. %config is less to type.
1349 our %config;
1350 local *config = \%Fcscs::config;
1351
1352 $config{mapping}{mode}   = \%mapping_mode;
1353 $config{mapping}{simple} = \%mapping_simple;
1354 $config{attribute}       = \%attribute;
1355 $config{setting}         = \%setting;
1356 $config{regex}           = \%regex;
1357 $config{handler}         = \%handler;
1358 $config{state}           = \%state;
1359
1360 package Fcscs {
1361     my @configs = ("$ENV{HOME}/.fcscsrc",
1362                    "$ENV{HOME}/.config/fcscs/fcscsrc");
1363     foreach my $path (@configs) {
1364         my $decoded = $screen->decode($path);
1365
1366         # Load configuration file. Checks have a race condition if the home
1367         # directory is writable by an attacker (but then the user is screwed
1368         # anyway).
1369         next unless -e $path;
1370         if (not -O $path) {
1371             $screen->die("Config '$decoded' not owned by current user!");
1372         }
1373         # Make sure the file is not writable by other users. Doesn't handle
1374         # ACLs and see comment above about race conditions.
1375         my @stat = stat $path or die $!;
1376         my $mode = $stat[2];
1377         if (($mode & Fcntl::S_IWGRP) or ($mode & Fcntl::S_IWOTH)) {
1378             die "Config '$decoded' must not be writable by other users.";
1379         }
1380
1381         my $result = do $path;
1382         if (not $result) {
1383             $screen->die("Failed to parse '$decoded': $@") if $@;
1384             $screen->die("Failed to do '$decoded': $!") unless defined $result;
1385             $screen->die("Failed to run '$decoded'.");
1386         }
1387
1388         last; # success, don't load more files
1389     }
1390 }
1391 $screen->{debug} = $config{setting}{debug};
1392
1393
1394 # MAIN
1395
1396 eval {
1397     # Auto-detect current multiplexer.
1398     if (not defined $config{setting}{multiplexer}) {
1399         if (defined $ENV{STY} and defined $ENV{TMUX}) {
1400             die 'Found both $STY and $TMUX, set $config{setting}{multiplexer}.';
1401         } elsif (defined $ENV{STY}) {
1402             $config{setting}{multiplexer} = 'screen';
1403         } elsif (defined $ENV{TMUX}) {
1404             $config{setting}{multiplexer} = 'tmux';
1405         } else {
1406             die 'No multiplexer found.';
1407         }
1408     }
1409
1410     my $binmode = $encoding;
1411     # GNU screen stores the screen dump for unknown reasons as ISO-8859-1
1412     # instead of the currently active encoding.
1413     if ($config{setting}{multiplexer} eq 'screen') {
1414         $binmode = 'ISO-8859-1';
1415     }
1416
1417     my @input_lines;
1418     open my $fh, '<', $ARGV[0] or die $!;
1419     binmode $fh, ":encoding($binmode)" or die $!;
1420     while (<$fh>) {
1421         chomp;
1422         push @input_lines, $_;
1423     }
1424     close $fh or die $!;
1425
1426     my $input = prepare_input($screen, \@input_lines);
1427
1428     # Display original screen content.
1429     my $y = 0;
1430     foreach (@{$input->{lines}}) {
1431         $screen->draw_simple($y++, 0, undef, $_);
1432     }
1433     $screen->refresh;
1434
1435
1436     my $mapping = $config{setting}{initial_mode};
1437
1438     my $key;
1439     while (1) {
1440         if (not defined $mapping) {
1441             $key = $screen->getch unless defined $key;
1442             $screen->debug('input', "got key '$key'");
1443
1444             $mapping = $config{mapping}{mode}{$key};
1445             $mapping = $config{mapping}{simple}{$key} unless defined $mapping;
1446             if (not defined $mapping) { # ignore unknown mappings
1447                 $key = undef;
1448                 next;
1449             }
1450         }
1451
1452         $screen->debug('input', 'running mapping');
1453         my $result = $mapping->($key, $screen, \%config, $input);
1454         $mapping = undef;
1455
1456 RESULT:
1457         if (defined $result->{quit}) {
1458             $screen->debug('input', 'quitting');
1459             last;
1460         }
1461         if (defined $result->{key}) {
1462             $key = $result->{key}; # lookup another mapping
1463             $screen->debug('input', "processing new key: '$key'");
1464             next;
1465         }
1466         if (defined $result->{select}) {
1467             $screen->debug('input', 'selecting match');
1468             my $tmp = $result;
1469             $result = select_match($result->{select},
1470                                    $screen, \%config, $input,
1471                                    $result->{matches});
1472             $result->{handler} = $tmp->{handler};
1473             $result->{extend}  = $tmp->{extend};
1474             goto RESULT; # reprocess special entries in result
1475         }
1476         if (defined $result->{extend}) {
1477             $screen->debug('input', 'extending match');
1478             $result = extend_match($screen, \%config, $input,
1479                                    $result->{match});
1480             goto RESULT; # reprocess special entries in result
1481         }
1482         if (defined $result->{match}) {
1483             if (not defined $result->{match}{value}) {
1484                 $result->{match}{value} = $result->{match}{string};
1485             }
1486
1487             $screen->debug('input', 'running handler');
1488
1489             # Choose handler with falling priority.
1490             my @handlers = (
1491                 $config{state}{handler},     # set by user
1492                 $result->{match}{handler},   # set by match
1493                 $result->{handler},          # set by mapping
1494                 $config{handler}{yank},      # fallback
1495             );
1496             foreach my $handler (@handlers) {
1497                 next unless defined $handler;
1498
1499                 $handler->($screen, \%config, $result->{match});
1500                 last;
1501             }
1502             last;
1503         }
1504
1505         $key = undef; # get next key from user
1506     }
1507 };
1508 if ($@) {
1509     $screen->die("$@");
1510 }
1511
1512 $screen->deinit;
1513
1514 __END__
1515
1516 =head1 EXIT STATUS
1517
1518 =over 4
1519
1520 =item B<0>
1521
1522 Success.
1523
1524 =item B<1>
1525
1526 An error occurred.
1527
1528 =item B<2>
1529
1530 Invalid arguments/options.
1531
1532 =back
1533
1534 =head1 AUTHOR
1535
1536 Simon Ruderich E<lt>simon@ruderich.orgE<gt>
1537
1538 =head1 LICENSE AND COPYRIGHT
1539
1540 Copyright (C) 2013-2016 by Simon Ruderich
1541
1542 This program is free software: you can redistribute it and/or modify
1543 it under the terms of the GNU General Public License as published by
1544 the Free Software Foundation, either version 3 of the License, or
1545 (at your option) any later version.
1546
1547 This program is distributed in the hope that it will be useful,
1548 but WITHOUT ANY WARRANTY; without even the implied warranty of
1549 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1550 GNU General Public License for more details.
1551
1552 You should have received a copy of the GNU General Public License
1553 along with this program.  If not, see E<lt>http://www.gnu.org/licenses/E<gt>.
1554
1555 =head1 SEE ALSO
1556
1557 L<screen(1)>, L<tmux(1)>, L<urlview(1)>
1558
1559 =cut