--- rxvt-unicode/src/perl/matcher 2012/06/10 17:39:54 1.11 +++ rxvt-unicode/src/perl/matcher 2014/06/12 12:16:42 1.22 @@ -1,17 +1,17 @@ #! perl # Author: Tim Pope -# Bob Farrell +# Bob Farrell -#:META:X_RESOURCE:%.launcher:string:default launcher command -#:META:X_RESOURCE:%.button:string:the button, yeah -#:META:X_RESOURCE:%.pattern.:string:extra pattern to match -#:META:X_RESOURCE:%.launcher.:string:custom launcher for pattern -#:META:X_RESOURCE:%.rend.:string:custom rednition for pattern +#: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 rednition for pattern =head1 NAME - matcher - match strings in terminal output and change their rendition +matcher - match strings in terminal output and change their rendition =head1 DESCRIPTION @@ -19,7 +19,7 @@ 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 +(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. @@ -30,15 +30,18 @@ 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 "perl:matcher:last" or -"perl:matcher:list" as seen in the example below. +from the keyboard. Simply bind a keysym to "matcher:last" or +"matcher:list" as seen in the example below. -Example configuration: +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: perl:matcher:last - URxvt.keysym.M-Delete: perl:matcher:list + 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+)(?=:|$) @@ -49,106 +52,101 @@ 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 on_key_press { my ($self, $event, $keysym, $octets) = @_; - if (! $self->{showing} ) { - return; - } + return unless $self->{overlay}; + + delete $self->{overlay}; my $i = ($keysym == 96 ? 0 : $keysym - 48); - if (($i > scalar(@{$self->{urls}})) || ($i < 0)) { - $self->matchlist(); - return; + if ($i >= 0 && $i < @{ $self->{matches} }) { + my @exec = @{ $self->{matches}[$i] }; + shift @exec; + $self->exec_async (@exec); } - my @args = ($self->{urls}[ -$i-1 ]); - $self->matchlist(); - - $self->exec_async( $self->{launcher}, @args ); + 1 } +# backwards compat sub on_user_command { my ($self, $cmd) = @_; - if($cmd =~ s/^matcher:list\b//) { - $self->matchlist(); + if ($cmd =~ s/^matcher:list\b//) { + $self->matchlist; } else { - if($cmd =~ s/^matcher:last\b//) { + if ($cmd =~ s/^matcher:last\b//) { $self->most_recent; - } - # For backward compatibility - else { - if($cmd =~ s/^matcher\b//) { + } 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) = @_; - if ( $self->{showing} ) { - $self->{url_overlay}->hide(); - $self->{showing} = 0; - return; - } - @{$self->{urls}} = (); - my $line; - for (my $i = 0; $i < $self->nrow; $i ++) { - $line = $self->line($i); - next if ($line->beg != $i); - for my $url ($self->get_urls_from_line($line->t)) { - if (scalar(@{$self->{urls}}) == 10) { - shift @{$self->{urls}}; - } - push @{$self->{urls}}, $url; - } - } - - if (! scalar(@{$self->{urls}})) { - return; - } - - my $max = 0; - my $i = scalar( @{$self->{urls}} ) - 1 ;; - - my @temp = (); - - for my $url (@{$self->{urls}}) { - my $url = "$i-$url"; - my $xpos = 0; - - if ($self->ncol + (length $url) >= $self->ncol) { - $url = substr( $url, 0, $self->ncol ); - } - - push @temp, $url; - - if( length $url > $max ) { - $max = length $url; - } - - $i--; - } - - @temp = reverse @temp; - - $self->{url_overlay} = $self->overlay(0, 0, $max, scalar( @temp ), urxvt::OVERLAY_RSTYLE, 2); - my $i = 0; - for my $url (@temp) { - $self->{url_overlay}->set( 0, $i, $url, [(urxvt::OVERLAY_RSTYLE) x length $url]); - $self->{showing} = 1; - $i++; - } + + @{ $self->{matches} } = (); + my $row = $self->nrow - 1; + while ($row >= 0 && @{ $self->{matches} } < 10) { + my $line = $self->line ($row); + my @matches = $self->find_matches ($row, 0); + + for (sort { $b->[0] <=> $a->[0] } @matches) { + shift @$_; + 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->[0]; + 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->[0]; + + $self->{overlay}->set (0, $i, "$i-$text"); + $i++; + } } @@ -190,8 +188,7 @@ $self->{launcher} = $self->my_resource ("launcher") || $self->x_resource("url-launcher") || "sensible-browser"; - $self->{urls} = []; - $self->{showing} = 0; + $self->{matches} = []; $self->{button} = 2; $self->{state} = 0; if($self->{argv}[0] || $self->my_resource ("button")) { @@ -226,17 +223,6 @@ () } -sub get_urls_from_line { - my ($self, $line) = @_; - my @urls; - for my $matcher (@{$self->{matchers}}) { - while ($line =~ /$matcher->[0]/g) { - push @urls, substr( $line, $-[0], $+[0] - $-[0] ); - } - } - return @urls; -} - sub on_line_update { my ($self, $row) = @_; @@ -271,32 +257,48 @@ ($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 @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 = @+; + my @exec; + if (!defined($col) || ($-[0] <= $col && $+[0] >= $col)) { 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, [ $begin[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[2 .. $#match]; + } + () } @@ -330,7 +332,7 @@ && join("\x00", @$cmd) eq join("\x00", $self->command_for($row,$col))) { if($self->valid_button($event)) { - $self->exec_async (@$cmd); + $self->exec_async (@$cmd); } }