--- rxvt-unicode/src/perl/matcher 2007/06/10 23:42:36 1.4 +++ rxvt-unicode/src/perl/matcher 2014/06/21 14:11:18 1.30 @@ -1,32 +1,158 @@ #! perl -# Author: Tim Pope +# Author: Tim Pope +# Bob Farrell + +#:META:RESOURCE:%.launcher:string:default launcher command +#:META:RESOURCE:%.button:string:the button, yeah +#:META:RESOURCE:%.pattern.:string:extra pattern to match +#:META:RESOURCE:%.launcher.:string:custom launcher for pattern +#:META:RESOURCE:%.rend.:string:custom rendition for pattern + +=head1 NAME + +matcher - match strings in terminal output and change their rendition + +=head1 DESCRIPTION + +Uses per-line display filtering (C) to underline text +matching a certain pattern and make it clickable. When clicked with the +mouse button specified in the C resource (default 2, or +middle), the program specified in the C resource +(default, the C resource, C) will be started +with the matched text as first argument. The default configuration is +suitable for matching URLs and launching a web browser, like the +former "mark-urls" extension. + +The default pattern to match URLs can be overridden with the +C resource, and additional patterns can be specified +with numbered patterns, in a manner similar to the "selection" extension. +The launcher can also be overridden on a per-pattern basis. + +It is possible to activate the most recently seen match or a list of matches +from the keyboard. Simply bind a keysym to "matcher:last" or +"matcher:list" as seen in the example below. + +Example: load and use the matcher extension with defaults. + + URxvt.perl-ext: default,matcher + +Example: use a custom configuration. + + URxvt.url-launcher: sensible-browser + URxvt.keysym.C-Delete: matcher:last + URxvt.keysym.M-Delete: matcher:list + URxvt.matcher.button: 1 + URxvt.matcher.pattern.1: \\bwww\\.[\\w-]+\\.[\\w./?&@#-]*[\\w/-] + URxvt.matcher.pattern.2: \\B(/\\S+?):(\\d+)(?=:|$) + URxvt.matcher.launcher.2: gvim +$2 $1 + +=cut my $url = qr{ (?:https?://|ftp://|news://|mailto:|file://|\bwww\.) - [a-zA-Z0-9\-\@;\/?:&=%\$_.+!*\x27,~#]* + [\w\-\@;\/?:&=%\$.+!*\x27,~#]* ( - \([a-zA-Z0-9\-\@;\/?:&=%\$_.+!*\x27,~#]*\)| # Allow a pair of matched parentheses - [a-zA-Z0-9\-\@;\/?:&=%\$_+*~] # exclude some trailing characters (heuristic) + \([\w\-\@;\/?:&=%\$.+!*\x27,~#]*\)| # Allow a pair of matched parentheses + [\w\-\@;\/?:&=%\$+*~] # exclude some trailing characters (heuristic) )+ }x; +sub matchlist_key_press { + my ($self, $event, $keysym, $octets) = @_; + + delete $self->{overlay}; + $self->disable ("key_press"); + + my $i = ($keysym == 96 ? 0 : $keysym - 48); + if ($i >= 0 && $i < @{ $self->{matches} }) { + my @exec = @{ $self->{matches}[$i] }; + $self->exec_async (@exec[5 .. $#exec]); + } + + 1 +} + +# backwards compat sub on_user_command { my ($self, $cmd) = @_; - if($cmd =~ s/^matcher\b//) { + + if ($cmd =~ s/^matcher:list\b//) { + $self->matchlist; + } else { + if ($cmd =~ s/^matcher:last\b//) { + $self->most_recent; + } elsif ($cmd =~ s/^matcher\b//) { + # for backward compatibility + $self->most_recent; + } + } + + () +} + +sub on_action { + my ($self, $action) = @_; + + if ($action eq "list") { + $self->matchlist; + } elsif ($action eq "last") { $self->most_recent; } + () } +sub matchlist { + my ($self) = @_; + + $self->{matches} = []; + my $row = $self->nrow - 1; + while ($row >= 0 && @{ $self->{matches} } < 10) { + my $line = $self->line ($row); + my @matches = $self->find_matches ($row); + + for (sort { $b->[0] <=> $a->[0] or $b->[1] <=> $a->[1] } @matches) { + push @{ $self->{matches} }, $_; + last if @{ $self->{matches} } == 10; + } + + $row = $line->beg - 1; + } + + return unless @{ $self->{matches} }; + + my $width = 0; + + my $i = 0; + for my $match (@{ $self->{matches} }) { + my $text = $match->[4]; + my $w = $self->strwidth ("$i-$text"); + + $width = $w if $w > $width; + $i++; + } + + $width = $self->ncol - 2 if $width > $self->ncol - 2; + + $self->{overlay} = $self->overlay (0, 0, $width, scalar (@{ $self->{matches} }), urxvt::OVERLAY_RSTYLE, 2); + my $i = 0; + for my $match (@{ $self->{matches} }) { + my $text = $match->[4]; + + $self->{overlay}->set (0, $i, "$i-$text"); + $i++; + } + + $self->enable (key_press => \&matchlist_key_press); +} + sub most_recent { my ($self) = shift; my $row = $self->nrow; my @exec; while($row-- > $self->top_row) { - #my $line = $self->line ($row); - #my $text = $line->t; @exec = $self->command_for($row); last if(@exec); } @@ -37,14 +163,13 @@ } sub my_resource { - my $self = shift; - $self->x_resource ("$self->{name}.$_[0]"); + $_[0]->x_resource ("%.$_[1]") } # turn a rendition spec in the resource into a sub that implements it on $_ sub parse_rend { my ($self, $str) = @_; - my ($mask, $fg, $bg, $failed) = $str ? urxvt::rend2mask($str) + my ($mask, $fg, $bg, $failed) = $str ? urxvt::rend2mask($str) : (urxvt::RS_Uline, undef, undef, []); warn "Failed to parse rendition string: " . join(',', @$failed) if @$failed; my @rend; @@ -59,16 +184,12 @@ sub on_start { my ($self) = @_; - ($self->{name} = __PACKAGE__) =~ s/.*:://; - $self->{name} =~ tr/_/-/; - $self->{launcher} = $self->my_resource("launcher") || - $self->x_resource("urlLauncher") || - "sensible-browser"; + $self->{launcher} = $self->my_resource ("launcher") || $self->x_resource("url-launcher") || "sensible-browser"; $self->{button} = 2; $self->{state} = 0; - if($self->{argv}[0] || $self->my_resource("button")) { - my @mods = split('', $self->{argv}[0] || $self->my_resource("button")); + if($self->{argv}[0] || $self->my_resource ("button")) { + my @mods = split '', $self->{argv}[0] || $self->my_resource ("button"); for my $mod (@mods) { if($mod =~ /^\d+$/) { $self->{button} = $mod; @@ -79,19 +200,19 @@ } elsif($mod eq "M") { $self->{state} |= $self->ModMetaMask; } elsif($mod ne "-" && $mod ne " ") { - warn("$mod is invalid in $self->{name}<$self->{argv}[0]>\n"); + warn("$mod is invalid in $self->{_name}<$self->{argv}[0]>\n"); } } } my @defaults = ($url); my @matchers; - for (my $idx = 0; defined (my $res = $self->my_resource("pattern.$idx") || $defaults[$idx]); $idx++) { + for (my $idx = 0; defined (my $res = $self->my_resource ("pattern.$idx") || $defaults[$idx]); $idx++) { $res = $self->locale_decode ($res); utf8::encode $res; - my $launcher = $self->my_resource("launcher.$idx"); - $launcher =~ s/\$&|\$\{&\}/\${0}/g if ($launcher); - my $rend = $self->parse_rend($self->my_resource("rend.$idx")); + my $launcher = $self->my_resource ("launcher.$idx"); + $launcher =~ s/\$&|\$\{&\}/\${0}/g if $launcher; + my $rend = $self->parse_rend($self->my_resource ("rend.$idx")); unshift @matchers, [qr($res)x,$launcher,$rend]; } $self->{matchers} = \@matchers; @@ -105,7 +226,6 @@ # fetch the line that has changed my $line = $self->line ($row); my $text = $line->t; - my $i = 0; # find all urls (if any) for my $matcher (@{$self->{matchers}}) { @@ -116,7 +236,7 @@ # mark all characters as underlined. we _must_ not toggle underline, # as we might get called on an already-marked url. &{$matcher->[2]} - for @{$rend}[ $-[0] .. $+[0] - 1]; + for @{$rend}[$-[0] .. $+[0] - 1]; $line->r ($rend); } @@ -133,43 +253,64 @@ ($event->{state} & $mask) == $self->{state}); } -sub command_for { +sub find_matches { my ($self, $row, $col) = @_; my $line = $self->line ($row); my $text = $line->t; + my $off = $line->offset_of ($row, $col) if defined $col; + my @matches; for my $matcher (@{$self->{matchers}}) { my $launcher = $matcher->[1] || $self->{launcher}; - while (($text =~ /$matcher->[0]/g)) { - my $match = $&; + while ($text =~ /$matcher->[0]/g) { + my $match = substr $text, $-[0], $+[0] - $-[0]; my @begin = @-; my @end = @+; - if (!defined($col) || ($-[0] <= $col && $+[0] >= $col)) { + my @exec; + + if (!defined($off) || ($-[0] <= $off && $+[0] >= $off)) { if ($launcher !~ /\$/) { - return ($launcher,$match); + @exec = ($launcher, $match); } else { # It'd be nice to just access a list like ($&,$1,$2...), # but alas, m//g behaves differently in list context. - my @exec = map { s/\$(\d+)|\$\{(\d+)\}/ - substr($text,$begin[$1||$2],$end[$1||$2]-$begin[$1||$2]) - /egx; $_ } split(/\s+/, $launcher); - return @exec; + @exec = map { s/\$(\d+)|\$\{(\d+)\}/ + substr $text, $begin[$1 || $2], $end[$1 || $2] - $begin[$1 || $2] + /egx; $_ } split /\s+/, $launcher; } + + push @matches, [ $line->coord_of ($begin[0]), $line->coord_of ($end[0]), $match, @exec ]; } } } + @matches; +} + +sub command_for { + my ($self, $row, $col) = @_; + + my @matches = $self->find_matches ($row, $col); + if (@matches) { + my @match = @{ $matches[0] }; + return @match[5 .. $#match]; + } + () } sub on_button_press { my ($self, $event) = @_; - if($self->valid_button($event)) { + if($self->valid_button($event) + && (my @exec = $self->command_for($event->{row},$event->{col}))) { $self->{row} = $event->{row}; $self->{col} = $event->{col}; + $self->{cmd} = \@exec; + return 1; } else { delete $self->{row}; delete $self->{col}; + delete $self->{cmd}; } () @@ -180,19 +321,20 @@ my $row = delete $self->{row}; my $col = delete $self->{col}; + my $cmd = delete $self->{cmd}; - if(defined($row) && $row == $event->{row} && abs($col-$event->{col}) < 2) { + return if !defined $row; + + if($row == $event->{row} && abs($col-$event->{col}) < 2 + && join("\x00", @$cmd) eq join("\x00", $self->command_for($row,$col))) { if($self->valid_button($event)) { - my @exec = $self->command_for($row,$col); - if(@exec) { - return $self->exec_async (@exec); - } + $self->exec_async (@$cmd); } } - () + 1; } # vim:set sw=3 sts=3 et: