--- rxvt-unicode/src/perl/matcher 2007/01/09 16:18:56 1.2 +++ rxvt-unicode/src/perl/matcher 2011/08/30 20:02:32 1.7 @@ -1,6 +1,7 @@ #! perl -# Author: Tim Pope +# Author: Tim Pope +# Bob Farrell my $url = qr{ @@ -12,16 +13,107 @@ )+ }x; +sub on_key_press { + my ($self, $event, $keysym, $octets) = @_; + + if (! $self->{showing} ) { + return; + } + + my $i = ($keysym == 96 ? 0 : $keysym - 48); + if (($i > scalar(@{$self->{urls}})) || ($i < 0)) { + $self->matchlist(); + return; + } + + my @args = ($self->{urls}[ -$i-1 ]); + $self->matchlist(); + + $self->exec_async( $self->{launcher}, @args ); +} + sub on_user_command { my ($self, $cmd) = @_; - if($cmd =~ s/^matcher\b//) { - $self->most_recent; + + if($cmd =~ s/^matcher:list\b//) { + $self->matchlist(); + } else { + if($cmd =~ s/^matcher:last\b//) { + $self->most_recent; + } + # For backward compatibility + else { + if($cmd =~ s/^matcher\b//) { + $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++; + } + +} + +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); } @@ -31,20 +123,15 @@ () } -sub most_recent { - my ($self) = shift; - () -} - 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) + 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; @@ -65,6 +152,8 @@ $self->x_resource("urlLauncher") || "sensible-browser"; + $self->{urls} = []; + $self->{showing} = 0; $self->{button} = 2; $self->{state} = 0; if($self->{argv}[0] || $self->my_resource("button")) { @@ -99,6 +188,17 @@ () } +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) = @_; @@ -164,12 +264,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}; } () @@ -180,19 +284,20 @@ my $row = delete $self->{row}; my $col = delete $self->{col}; + my $cmd = delete $self->{cmd}; + + return if !defined $row; - if(defined($row) && $row == $event->{row} && abs($col-$event->{col}) < 2) { + 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: