ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/perl/matcher
(Generate patch)

Comparing rxvt-unicode/src/perl/matcher (file contents):
Revision 1.5 by sf-tpope, Fri Aug 19 23:02:46 2011 UTC vs.
Revision 1.8 by root, Wed Jun 6 15:06:41 2012 UTC

1#! perl 1#! perl
2 2
3# Author: Tim Pope <rxvt-unicodeNOSPAM@tpope.info> 3# Author: Tim Pope <rxvt-unicodeNOSPAM@tpope.org>
4# Bob Farrell <robertanthonyfarrell@gmail.com>
5
6#:META:X_RESOURCE:%.launcher:string:default launcher command
7#:META:X_RESOURCE:%.button:string:the button, yeah
8#:META:X_RESOURCE:%.pattern.:string:extra pattern to match
9#:META:X_RESOURCE:%.launcher.:string:custom launcher for pattern
10#:META:X_RESOURCE:%.rend.:string:custom rednition for pattern
4 11
5my $url = 12my $url =
6 qr{ 13 qr{
7 (?:https?://|ftp://|news://|mailto:|file://|\bwww\.) 14 (?:https?://|ftp://|news://|mailto:|file://|\bwww\.)
8 [a-zA-Z0-9\-\@;\/?:&=%\$_.+!*\x27,~#]* 15 [a-zA-Z0-9\-\@;\/?:&=%\$_.+!*\x27,~#]*
10 \([a-zA-Z0-9\-\@;\/?:&=%\$_.+!*\x27,~#]*\)| # Allow a pair of matched parentheses 17 \([a-zA-Z0-9\-\@;\/?:&=%\$_.+!*\x27,~#]*\)| # Allow a pair of matched parentheses
11 [a-zA-Z0-9\-\@;\/?:&=%\$_+*~] # exclude some trailing characters (heuristic) 18 [a-zA-Z0-9\-\@;\/?:&=%\$_+*~] # exclude some trailing characters (heuristic)
12 )+ 19 )+
13 }x; 20 }x;
14 21
22sub on_key_press {
23 my ($self, $event, $keysym, $octets) = @_;
24
25 if (! $self->{showing} ) {
26 return;
27 }
28
29 my $i = ($keysym == 96 ? 0 : $keysym - 48);
30 if (($i > scalar(@{$self->{urls}})) || ($i < 0)) {
31 $self->matchlist();
32 return;
33 }
34
35 my @args = ($self->{urls}[ -$i-1 ]);
36 $self->matchlist();
37
38 $self->exec_async( $self->{launcher}, @args );
39}
40
15sub on_user_command { 41sub on_user_command {
16 my ($self, $cmd) = @_; 42 my ($self, $cmd) = @_;
43
44 if($cmd =~ s/^matcher:list\b//) {
45 $self->matchlist();
46 } else {
47 if($cmd =~ s/^matcher:last\b//) {
48 $self->most_recent;
49 }
50 # For backward compatibility
51 else {
17 if($cmd =~ s/^matcher\b//) { 52 if($cmd =~ s/^matcher\b//) {
18 $self->most_recent; 53 $self->most_recent;
54 }
55 }
19 } 56 }
20 () 57 ()
58}
59
60sub matchlist {
61 my ($self) = @_;
62 if ( $self->{showing} ) {
63 $self->{url_overlay}->hide();
64 $self->{showing} = 0;
65 return;
66 }
67 @{$self->{urls}} = ();
68 my $line;
69 for (my $i = 0; $i < $self->nrow; $i ++) {
70 $line = $self->line($i);
71 next if ($line->beg != $i);
72 for my $url ($self->get_urls_from_line($line->t)) {
73 if (scalar(@{$self->{urls}}) == 10) {
74 shift @{$self->{urls}};
75 }
76 push @{$self->{urls}}, $url;
77 }
78 }
79
80 if (! scalar(@{$self->{urls}})) {
81 return;
82 }
83
84 my $max = 0;
85 my $i = scalar( @{$self->{urls}} ) - 1 ;;
86
87 my @temp = ();
88
89 for my $url (@{$self->{urls}}) {
90 my $url = "$i-$url";
91 my $xpos = 0;
92
93 if ($self->ncol + (length $url) >= $self->ncol) {
94 $url = substr( $url, 0, $self->ncol );
95 }
96
97 push @temp, $url;
98
99 if( length $url > $max ) {
100 $max = length $url;
101 }
102
103 $i--;
104 }
105
106 @temp = reverse @temp;
107
108 $self->{url_overlay} = $self->overlay(0, 0, $max, scalar( @temp ), urxvt::OVERLAY_RSTYLE, 2);
109 my $i = 0;
110 for my $url (@temp) {
111 $self->{url_overlay}->set( 0, $i, $url, [(urxvt::OVERLAY_RSTYLE) x length $url]);
112 $self->{showing} = 1;
113 $i++;
114 }
115
21} 116}
22 117
23sub most_recent { 118sub most_recent {
24 my ($self) = shift; 119 my ($self) = shift;
25 my $row = $self->nrow; 120 my $row = $self->nrow;
26 my @exec; 121 my @exec;
27 while($row-- > $self->top_row) { 122 while($row-- > $self->top_row) {
28 #my $line = $self->line ($row);
29 #my $text = $line->t;
30 @exec = $self->command_for($row); 123 @exec = $self->command_for($row);
31 last if(@exec); 124 last if(@exec);
32 } 125 }
33 if(@exec) { 126 if(@exec) {
34 return $self->exec_async (@exec); 127 return $self->exec_async (@exec);
42} 135}
43 136
44# turn a rendition spec in the resource into a sub that implements it on $_ 137# turn a rendition spec in the resource into a sub that implements it on $_
45sub parse_rend { 138sub parse_rend {
46 my ($self, $str) = @_; 139 my ($self, $str) = @_;
47 my ($mask, $fg, $bg, $failed) = $str ? urxvt::rend2mask($str) 140 my ($mask, $fg, $bg, $failed) = $str ? urxvt::rend2mask($str)
48 : (urxvt::RS_Uline, undef, undef, []); 141 : (urxvt::RS_Uline, undef, undef, []);
49 warn "Failed to parse rendition string: " . join(',', @$failed) if @$failed; 142 warn "Failed to parse rendition string: " . join(',', @$failed) if @$failed;
50 my @rend; 143 my @rend;
51 push @rend, sub { $_ |= $mask } if $mask; 144 push @rend, sub { $_ |= $mask } if $mask;
52 push @rend, sub { $_ = urxvt::SET_FGCOLOR($_, $fg) } if defined $fg; 145 push @rend, sub { $_ = urxvt::SET_FGCOLOR($_, $fg) } if defined $fg;
59sub on_start { 152sub on_start {
60 my ($self) = @_; 153 my ($self) = @_;
61 154
62 ($self->{name} = __PACKAGE__) =~ s/.*:://; 155 ($self->{name} = __PACKAGE__) =~ s/.*:://;
63 $self->{name} =~ tr/_/-/; 156 $self->{name} =~ tr/_/-/;
64 $self->{launcher} = $self->my_resource("launcher") || 157 $self->{launcher} = $self->my_resource ("launcher") || $self->x_resource("url-launcher") || "sensible-browser";
65 $self->x_resource("urlLauncher") ||
66 "sensible-browser";
67 158
159 $self->{urls} = [];
160 $self->{showing} = 0;
68 $self->{button} = 2; 161 $self->{button} = 2;
69 $self->{state} = 0; 162 $self->{state} = 0;
70 if($self->{argv}[0] || $self->my_resource("button")) { 163 if($self->{argv}[0] || $self->my_resource ("button")) {
71 my @mods = split('', $self->{argv}[0] || $self->my_resource("button")); 164 my @mods = split '', $self->{argv}[0] || $self->my_resource ("button");
72 for my $mod (@mods) { 165 for my $mod (@mods) {
73 if($mod =~ /^\d+$/) { 166 if($mod =~ /^\d+$/) {
74 $self->{button} = $mod; 167 $self->{button} = $mod;
75 } elsif($mod eq "C") { 168 } elsif($mod eq "C") {
76 $self->{state} |= urxvt::ControlMask; 169 $self->{state} |= urxvt::ControlMask;
84 } 177 }
85 } 178 }
86 179
87 my @defaults = ($url); 180 my @defaults = ($url);
88 my @matchers; 181 my @matchers;
89 for (my $idx = 0; defined (my $res = $self->my_resource("pattern.$idx") || $defaults[$idx]); $idx++) { 182 for (my $idx = 0; defined (my $res = $self->my_resource ("pattern.$idx") || $defaults[$idx]); $idx++) {
90 $res = $self->locale_decode ($res); 183 $res = $self->locale_decode ($res);
91 utf8::encode $res; 184 utf8::encode $res;
92 my $launcher = $self->my_resource("launcher.$idx"); 185 my $launcher = $self->my_resource ("launcher.$idx");
93 $launcher =~ s/\$&|\$\{&\}/\${0}/g if ($launcher); 186 $launcher =~ s/\$&|\$\{&\}/\${0}/g if $launcher;
94 my $rend = $self->parse_rend($self->my_resource("rend.$idx")); 187 my $rend = $self->parse_rend($self->my_resource ("rend.$idx"));
95 unshift @matchers, [qr($res)x,$launcher,$rend]; 188 unshift @matchers, [qr($res)x,$launcher,$rend];
96 } 189 }
97 $self->{matchers} = \@matchers; 190 $self->{matchers} = \@matchers;
98 191
99 () 192 ()
193}
194
195sub get_urls_from_line {
196 my ($self, $line) = @_;
197 my @urls;
198 for my $matcher (@{$self->{matchers}}) {
199 while ($line =~ /$matcher->[0]/g) {
200 push @urls, substr( $line, $-[0], $+[0] - $-[0] );
201 }
202 }
203 return @urls;
100} 204}
101 205
102sub on_line_update { 206sub on_line_update {
103 my ($self, $row) = @_; 207 my ($self, $row) = @_;
104 208

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines