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.9 by root, Sun Jun 10 13:58:05 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);
35 } 128 }
36 () 129 ()
37} 130}
38 131
39sub my_resource { 132sub my_resource {
40 my $self = shift; 133 $_[0]->x_resource ("%.$_[1]")
41 $self->x_resource ("$self->{name}.$_[0]");
42} 134}
43 135
44# turn a rendition spec in the resource into a sub that implements it on $_ 136# turn a rendition spec in the resource into a sub that implements it on $_
45sub parse_rend { 137sub parse_rend {
46 my ($self, $str) = @_; 138 my ($self, $str) = @_;
47 my ($mask, $fg, $bg, $failed) = $str ? urxvt::rend2mask($str) 139 my ($mask, $fg, $bg, $failed) = $str ? urxvt::rend2mask($str)
48 : (urxvt::RS_Uline, undef, undef, []); 140 : (urxvt::RS_Uline, undef, undef, []);
49 warn "Failed to parse rendition string: " . join(',', @$failed) if @$failed; 141 warn "Failed to parse rendition string: " . join(',', @$failed) if @$failed;
50 my @rend; 142 my @rend;
51 push @rend, sub { $_ |= $mask } if $mask; 143 push @rend, sub { $_ |= $mask } if $mask;
52 push @rend, sub { $_ = urxvt::SET_FGCOLOR($_, $fg) } if defined $fg; 144 push @rend, sub { $_ = urxvt::SET_FGCOLOR($_, $fg) } if defined $fg;
57} 149}
58 150
59sub on_start { 151sub on_start {
60 my ($self) = @_; 152 my ($self) = @_;
61 153
62 ($self->{name} = __PACKAGE__) =~ s/.*:://; 154 $self->{launcher} = $self->my_resource ("launcher") || $self->x_resource("url-launcher") || "sensible-browser";
63 $self->{name} =~ tr/_/-/;
64 $self->{launcher} = $self->my_resource("launcher") ||
65 $self->x_resource("urlLauncher") ||
66 "sensible-browser";
67 155
156 $self->{urls} = [];
157 $self->{showing} = 0;
68 $self->{button} = 2; 158 $self->{button} = 2;
69 $self->{state} = 0; 159 $self->{state} = 0;
70 if($self->{argv}[0] || $self->my_resource("button")) { 160 if($self->{argv}[0] || $self->my_resource ("button")) {
71 my @mods = split('', $self->{argv}[0] || $self->my_resource("button")); 161 my @mods = split '', $self->{argv}[0] || $self->my_resource ("button");
72 for my $mod (@mods) { 162 for my $mod (@mods) {
73 if($mod =~ /^\d+$/) { 163 if($mod =~ /^\d+$/) {
74 $self->{button} = $mod; 164 $self->{button} = $mod;
75 } elsif($mod eq "C") { 165 } elsif($mod eq "C") {
76 $self->{state} |= urxvt::ControlMask; 166 $self->{state} |= urxvt::ControlMask;
77 } elsif($mod eq "S") { 167 } elsif($mod eq "S") {
78 $self->{state} |= urxvt::ShiftMask; 168 $self->{state} |= urxvt::ShiftMask;
79 } elsif($mod eq "M") { 169 } elsif($mod eq "M") {
80 $self->{state} |= $self->ModMetaMask; 170 $self->{state} |= $self->ModMetaMask;
81 } elsif($mod ne "-" && $mod ne " ") { 171 } elsif($mod ne "-" && $mod ne " ") {
82 warn("$mod is invalid in $self->{name}<$self->{argv}[0]>\n"); 172 warn("$mod is invalid in $self->{_name}<$self->{argv}[0]>\n");
83 } 173 }
84 } 174 }
85 } 175 }
86 176
87 my @defaults = ($url); 177 my @defaults = ($url);
88 my @matchers; 178 my @matchers;
89 for (my $idx = 0; defined (my $res = $self->my_resource("pattern.$idx") || $defaults[$idx]); $idx++) { 179 for (my $idx = 0; defined (my $res = $self->my_resource ("pattern.$idx") || $defaults[$idx]); $idx++) {
90 $res = $self->locale_decode ($res); 180 $res = $self->locale_decode ($res);
91 utf8::encode $res; 181 utf8::encode $res;
92 my $launcher = $self->my_resource("launcher.$idx"); 182 my $launcher = $self->my_resource ("launcher.$idx");
93 $launcher =~ s/\$&|\$\{&\}/\${0}/g if ($launcher); 183 $launcher =~ s/\$&|\$\{&\}/\${0}/g if $launcher;
94 my $rend = $self->parse_rend($self->my_resource("rend.$idx")); 184 my $rend = $self->parse_rend($self->my_resource ("rend.$idx"));
95 unshift @matchers, [qr($res)x,$launcher,$rend]; 185 unshift @matchers, [qr($res)x,$launcher,$rend];
96 } 186 }
97 $self->{matchers} = \@matchers; 187 $self->{matchers} = \@matchers;
98 188
99 () 189 ()
190}
191
192sub get_urls_from_line {
193 my ($self, $line) = @_;
194 my @urls;
195 for my $matcher (@{$self->{matchers}}) {
196 while ($line =~ /$matcher->[0]/g) {
197 push @urls, substr( $line, $-[0], $+[0] - $-[0] );
198 }
199 }
200 return @urls;
100} 201}
101 202
102sub on_line_update { 203sub on_line_update {
103 my ($self, $row) = @_; 204 my ($self, $row) = @_;
104 205

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines