--- rxvt-unicode/src/perl/matcher 2006/11/11 20:06:34 1.1 +++ rxvt-unicode/src/perl/matcher 2011/08/19 23:02:46 1.5 @@ -4,13 +4,56 @@ my $url = qr{ - (?:https?://|ftp://|news://|mailto:|file://|\bwww\.)[ab-zA-Z0-9\-\@;\/?:&=%\$_.+!*\x27(),~#]+ - [ab-zA-Z0-9\-\@;\/?:&=%\$_+*()~] # exclude some trailing characters (heuristic) + (?:https?://|ftp://|news://|mailto:|file://|\bwww\.) + [a-zA-Z0-9\-\@;\/?:&=%\$_.+!*\x27,~#]* + ( + \([a-zA-Z0-9\-\@;\/?:&=%\$_.+!*\x27,~#]*\)| # Allow a pair of matched parentheses + [a-zA-Z0-9\-\@;\/?:&=%\$_+*~] # exclude some trailing characters (heuristic) + )+ }x; +sub on_user_command { + my ($self, $cmd) = @_; + if($cmd =~ s/^matcher\b//) { + $self->most_recent; + } + () +} + +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); + } + if(@exec) { + return $self->exec_async (@exec); + } + () +} + sub my_resource { my $self = shift; - $self->x_resource("$self->{name}.$_[0]"); + $self->x_resource ("$self->{name}.$_[0]"); +} + +# 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) + : (urxvt::RS_Uline, undef, undef, []); + warn "Failed to parse rendition string: " . join(',', @$failed) if @$failed; + my @rend; + push @rend, sub { $_ |= $mask } if $mask; + push @rend, sub { $_ = urxvt::SET_FGCOLOR($_, $fg) } if defined $fg; + push @rend, sub { $_ = urxvt::SET_BGCOLOR($_, $bg) } if defined $bg; + sub { + for my $s ( @rend ) { &$s }; + } } sub on_start { @@ -48,7 +91,8 @@ utf8::encode $res; my $launcher = $self->my_resource("launcher.$idx"); $launcher =~ s/\$&|\$\{&\}/\${0}/g if ($launcher); - push @matchers, [qr($res)x,$launcher]; + my $rend = $self->parse_rend($self->my_resource("rend.$idx")); + unshift @matchers, [qr($res)x,$launcher,$rend]; } $self->{matchers} = \@matchers; @@ -66,11 +110,12 @@ # find all urls (if any) for my $matcher (@{$self->{matchers}}) { while ($text =~ /$matcher->[0]/g) { + #print "$&\n"; my $rend = $line->r; # mark all characters as underlined. we _must_ not toggle underline, # as we might get called on an already-marked url. - $_ |= urxvt::RS_Uline + &{$matcher->[2]} for @{$rend}[ $-[0] .. $+[0] - 1]; $line->r ($rend); @@ -99,7 +144,7 @@ my $match = $&; my @begin = @-; my @end = @+; - if ($-[0] <= $col && $+[0] >= $col) { + if (!defined($col) || ($-[0] <= $col && $+[0] >= $col)) { if ($launcher !~ /\$/) { return ($launcher,$match); } else { @@ -119,12 +164,16 @@ 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}; } () @@ -135,19 +184,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: