--- rxvt-unicode/src/perl/matcher 2014/06/07 20:07:38 1.19 +++ rxvt-unicode/src/perl/matcher 2014/06/15 12:24:43 1.28 @@ -7,7 +7,7 @@ #: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 +#:META:RESOURCE:%.rend.:string:custom rendition for pattern =head1 NAME @@ -30,8 +30,8 @@ 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: load and use the matcher extension with defaults. @@ -40,8 +40,8 @@ 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+)(?=:|$) @@ -59,18 +59,16 @@ )+ }x; -sub on_key_press { +sub matchlist_key_press { my ($self, $event, $keysym, $octets) = @_; - return unless $self->{overlay}; - 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] }; - shift @exec; - $self->exec_async (@exec); + $self->exec_async (@exec[5 .. $#exec]); } 1 @@ -94,39 +92,28 @@ () } +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} } = (); + $self->{matches} = []; my $row = $self->nrow - 1; while ($row >= 0 && @{ $self->{matches} } < 10) { my $line = $self->line ($row); - my $text = $line->t; - - # FIXME: code duplicated from 'command_for' - my @matches; - for my $matcher (@{$self->{matchers}}) { - my $launcher = $matcher->[1] || $self->{launcher}; - while ($text =~ /$matcher->[0]/g) { - my $match = substr ($text, $-[0], $+[0] - $-[0]); - my @beg = @-; - my @end = @+; - my @exec; - - if ($launcher !~ /\$/) { - @exec = ($launcher, $match); - } else { - @exec = map { s/\$(\d+)|\$\{(\d+)\}/ - substr ($text, $beg[$1 || $2], $end[$1 || $2] - $beg[$1 || $2]) - /egx; $_ } split /\s+/, $launcher; - } - - push @matches, [ $beg[0], $match, @exec ]; - } - } + my @matches = $self->find_matches ($row); - for (sort { $b->[0] <=> $a->[0] } @matches) { - shift @$_; + for (sort { $b->[0] <=> $a->[0] or $b->[1] <=> $a->[1] } @matches) { push @{ $self->{matches} }, $_; last if @{ $self->{matches} } == 10; } @@ -140,7 +127,7 @@ my $i = 0; for my $match (@{ $self->{matches} }) { - my $text = $match->[0]; + my $text = $match->[4]; my $w = $self->strwidth ("$i-$text"); $width = $w if $w > $width; @@ -152,12 +139,13 @@ $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]; + my $text = $match->[4]; $self->{overlay}->set (0, $i, "$i-$text"); $i++; } + $self->enable (key_press => \&matchlist_key_press); } sub most_recent { @@ -198,7 +186,6 @@ $self->{launcher} = $self->my_resource ("launcher") || $self->x_resource("url-launcher") || "sensible-browser"; - $self->{matches} = []; $self->{button} = 2; $self->{state} = 0; if($self->{argv}[0] || $self->my_resource ("button")) { @@ -239,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}}) { @@ -267,32 +253,49 @@ ($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 $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]; + } + () }