From: Simon Ruderich Date: Sat, 23 Jan 2016 23:04:02 +0000 (+0100) Subject: make handlers configurable via $config{handler}{..} X-Git-Url: https://ruderich.org/simon/gitweb/?p=fcscs%2Ffcscs.git;a=commitdiff_plain;h=34457d80b6c3dab5820e5e1306f8acf0ce77cd13 make handlers configurable via $config{handler}{..} --- diff --git a/bin/fcscs b/bin/fcscs index 25a5302..675b7f2 100755 --- a/bin/fcscs +++ b/bin/fcscs @@ -548,7 +548,7 @@ sub mapping_paste { debug $config, 'mapping_paste', 'started'; - $config->{state}{handler} = \&handler_paste; + $config->{state}{handler} = $config->{handler}{paste}; $screen->prompt(flags => 'P'); # paste $screen->draw_prompt($config); @@ -561,7 +561,7 @@ sub mapping_yank { debug $config, 'mapping_yank', 'started'; - $config->{state}{handler} = \&handler_yank; + $config->{state}{handler} = $config->{handler}{yank}; $screen->prompt(flags => 'Y'); # yank $screen->draw_prompt($config); @@ -580,7 +580,7 @@ sub mapping_mode_path { return { select => 'path select', matches => \@matches, - handler => \&handler_yank, + handler => $config->{handler}{yank}, }; } sub mapping_mode_url { @@ -592,7 +592,7 @@ sub mapping_mode_url { return { select => 'url select', matches => \@matches, - handler => \&handler_url, + handler => $config->{handler}{url}, }; } @@ -651,7 +651,7 @@ sub mapping_mode_search { return { select => 'search', matches => \@last_matches, - handler => \&handler_yank, + handler => $config->{handler}{yank}, }; } @@ -967,6 +967,45 @@ my %regex = ( path => qr{(~?[a-zA-Z0-9_./-]*/[a-zA-Z0-9_./-]+)}, ); +=head2 HANDLERS + +Handlers are used to perform actions on the selected string. + +The following handlers are available, defaults in parentheses. + +=over + +=item B used to yank (copy) selection to paste buffer (C<\&handler_yank>) + +=item B used to paste selection into window (C<\&handler_paste>) + +=item B used to open URLs (e.g. in a browser) (C<\&handler_url>) + +=back + +Example: + + # Download YouTube videos with a custom wrapper, handle all other URLs + # with the default URL handler. + $config{handler}{url} = sub { + my ($screen, $config, $match) = @_; + + my $url = defined $match->{url} ? $match->{url} : $match->{string}; + if ($url =~ m{^https://www.youtube.com/}) { + return run_in_background($config, sub { + run_command($config, ['youtube-dl-wrapper', $url]); + }); + } + handler_url(@_); + }; + +=cut +my %handler = ( + yank => \&handler_yank, + paste => \&handler_paste, + url => \&handler_url, +); + my %state = ( initial => 1, # used by select_match() for 'initial_mode' handler => undef, @@ -998,9 +1037,7 @@ Used as mappings, see L above. handler_paste() handler_url() -Used as handler to yank, paste selection or open URL in browser. They are -either set by the matching mapping function (C, etc.) or -configured by the current mode. +Used as handler to yank, paste selection or open URL in browser. get_regex_matches() select_match() @@ -1055,6 +1092,7 @@ $config{mapping}{simple} = \%mapping_simple; $config{attribute} = \%attribute; $config{setting} = \%setting; $config{regex} = \%regex; +$config{handler} = \%handler; $config{state} = \%state; package Fcscs { @@ -1176,7 +1214,7 @@ RESULT: debug \%config, 'input', 'running handler'; my $handler = $config{state}{handler}; # set by user $handler = $result->{handler} unless defined $handler; # set by mapping - $handler = \&handler_yank unless defined $handler; # fallback + $handler = $config{handler}{yank} unless defined $handler; # fallback $handler->($screen, \%config, $result->{match}); last; }