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.1 by root, Sat Nov 11 20:06:34 2006 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\.)[ab-zA-Z0-9\-\@;\/?:&=%\$_.+!*\x27(),~#]+ 14 (?:https?://|ftp://|news://|mailto:|file://|\bwww\.)
15 [a-zA-Z0-9\-\@;\/?:&=%\$_.+!*\x27,~#]*
16 (
17 \([a-zA-Z0-9\-\@;\/?:&=%\$_.+!*\x27,~#]*\)| # Allow a pair of matched parentheses
8 [ab-zA-Z0-9\-\@;\/?:&=%\$_+*()~] # exclude some trailing characters (heuristic) 18 [a-zA-Z0-9\-\@;\/?:&=%\$_+*~] # exclude some trailing characters (heuristic)
19 )+
9 }x; 20 }x;
10 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
41sub on_user_command {
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 {
52 if($cmd =~ s/^matcher\b//) {
53 $self->most_recent;
54 }
55 }
56 }
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
116}
117
118sub most_recent {
119 my ($self) = shift;
120 my $row = $self->nrow;
121 my @exec;
122 while($row-- > $self->top_row) {
123 @exec = $self->command_for($row);
124 last if(@exec);
125 }
126 if(@exec) {
127 return $self->exec_async (@exec);
128 }
129 ()
130}
131
11sub my_resource { 132sub my_resource {
12 my $self = shift; 133 $_[0]->x_resource ("%.$_[1]")
13 $self->x_resource("$self->{name}.$_[0]"); 134}
135
136# turn a rendition spec in the resource into a sub that implements it on $_
137sub parse_rend {
138 my ($self, $str) = @_;
139 my ($mask, $fg, $bg, $failed) = $str ? urxvt::rend2mask($str)
140 : (urxvt::RS_Uline, undef, undef, []);
141 warn "Failed to parse rendition string: " . join(',', @$failed) if @$failed;
142 my @rend;
143 push @rend, sub { $_ |= $mask } if $mask;
144 push @rend, sub { $_ = urxvt::SET_FGCOLOR($_, $fg) } if defined $fg;
145 push @rend, sub { $_ = urxvt::SET_BGCOLOR($_, $bg) } if defined $bg;
146 sub {
147 for my $s ( @rend ) { &$s };
148 }
14} 149}
15 150
16sub on_start { 151sub on_start {
17 my ($self) = @_; 152 my ($self) = @_;
18 153
19 ($self->{name} = __PACKAGE__) =~ s/.*:://; 154 $self->{launcher} = $self->my_resource ("launcher") || $self->x_resource("url-launcher") || "sensible-browser";
20 $self->{name} =~ tr/_/-/;
21 $self->{launcher} = $self->my_resource("launcher") ||
22 $self->x_resource("urlLauncher") ||
23 "sensible-browser";
24 155
156 $self->{urls} = [];
157 $self->{showing} = 0;
25 $self->{button} = 2; 158 $self->{button} = 2;
26 $self->{state} = 0; 159 $self->{state} = 0;
27 if($self->{argv}[0] || $self->my_resource("button")) { 160 if($self->{argv}[0] || $self->my_resource ("button")) {
28 my @mods = split('', $self->{argv}[0] || $self->my_resource("button")); 161 my @mods = split '', $self->{argv}[0] || $self->my_resource ("button");
29 for my $mod (@mods) { 162 for my $mod (@mods) {
30 if($mod =~ /^\d+$/) { 163 if($mod =~ /^\d+$/) {
31 $self->{button} = $mod; 164 $self->{button} = $mod;
32 } elsif($mod eq "C") { 165 } elsif($mod eq "C") {
33 $self->{state} |= urxvt::ControlMask; 166 $self->{state} |= urxvt::ControlMask;
34 } elsif($mod eq "S") { 167 } elsif($mod eq "S") {
35 $self->{state} |= urxvt::ShiftMask; 168 $self->{state} |= urxvt::ShiftMask;
36 } elsif($mod eq "M") { 169 } elsif($mod eq "M") {
37 $self->{state} |= $self->ModMetaMask; 170 $self->{state} |= $self->ModMetaMask;
38 } elsif($mod ne "-" && $mod ne " ") { 171 } elsif($mod ne "-" && $mod ne " ") {
39 warn("$mod is invalid in $self->{name}<$self->{argv}[0]>\n"); 172 warn("$mod is invalid in $self->{_name}<$self->{argv}[0]>\n");
40 } 173 }
41 } 174 }
42 } 175 }
43 176
44 my @defaults = ($url); 177 my @defaults = ($url);
45 my @matchers; 178 my @matchers;
46 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++) {
47 $res = $self->locale_decode ($res); 180 $res = $self->locale_decode ($res);
48 utf8::encode $res; 181 utf8::encode $res;
49 my $launcher = $self->my_resource("launcher.$idx"); 182 my $launcher = $self->my_resource ("launcher.$idx");
50 $launcher =~ s/\$&|\$\{&\}/\${0}/g if ($launcher); 183 $launcher =~ s/\$&|\$\{&\}/\${0}/g if $launcher;
184 my $rend = $self->parse_rend($self->my_resource ("rend.$idx"));
51 push @matchers, [qr($res)x,$launcher]; 185 unshift @matchers, [qr($res)x,$launcher,$rend];
52 } 186 }
53 $self->{matchers} = \@matchers; 187 $self->{matchers} = \@matchers;
54 188
55 () 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;
56} 201}
57 202
58sub on_line_update { 203sub on_line_update {
59 my ($self, $row) = @_; 204 my ($self, $row) = @_;
60 205
64 my $i = 0; 209 my $i = 0;
65 210
66 # find all urls (if any) 211 # find all urls (if any)
67 for my $matcher (@{$self->{matchers}}) { 212 for my $matcher (@{$self->{matchers}}) {
68 while ($text =~ /$matcher->[0]/g) { 213 while ($text =~ /$matcher->[0]/g) {
214 #print "$&\n";
69 my $rend = $line->r; 215 my $rend = $line->r;
70 216
71 # mark all characters as underlined. we _must_ not toggle underline, 217 # mark all characters as underlined. we _must_ not toggle underline,
72 # as we might get called on an already-marked url. 218 # as we might get called on an already-marked url.
73 $_ |= urxvt::RS_Uline 219 &{$matcher->[2]}
74 for @{$rend}[ $-[0] .. $+[0] - 1]; 220 for @{$rend}[ $-[0] .. $+[0] - 1];
75 221
76 $line->r ($rend); 222 $line->r ($rend);
77 } 223 }
78 } 224 }
97 my $launcher = $matcher->[1] || $self->{launcher}; 243 my $launcher = $matcher->[1] || $self->{launcher};
98 while (($text =~ /$matcher->[0]/g)) { 244 while (($text =~ /$matcher->[0]/g)) {
99 my $match = $&; 245 my $match = $&;
100 my @begin = @-; 246 my @begin = @-;
101 my @end = @+; 247 my @end = @+;
102 if ($-[0] <= $col && $+[0] >= $col) { 248 if (!defined($col) || ($-[0] <= $col && $+[0] >= $col)) {
103 if ($launcher !~ /\$/) { 249 if ($launcher !~ /\$/) {
104 return ($launcher,$match); 250 return ($launcher,$match);
105 } else { 251 } else {
106 # It'd be nice to just access a list like ($&,$1,$2...), 252 # It'd be nice to just access a list like ($&,$1,$2...),
107 # but alas, m//g behaves differently in list context. 253 # but alas, m//g behaves differently in list context.
117 () 263 ()
118} 264}
119 265
120sub on_button_press { 266sub on_button_press {
121 my ($self, $event) = @_; 267 my ($self, $event) = @_;
122 if($self->valid_button($event)) { 268 if($self->valid_button($event)
269 && (my @exec = $self->command_for($event->{row},$event->{col}))) {
123 $self->{row} = $event->{row}; 270 $self->{row} = $event->{row};
124 $self->{col} = $event->{col}; 271 $self->{col} = $event->{col};
272 $self->{cmd} = \@exec;
273 return 1;
125 } else { 274 } else {
126 delete $self->{row}; 275 delete $self->{row};
127 delete $self->{col}; 276 delete $self->{col};
277 delete $self->{cmd};
128 } 278 }
129 279
130 () 280 ()
131} 281}
132 282
133sub on_button_release { 283sub on_button_release {
134 my ($self, $event) = @_; 284 my ($self, $event) = @_;
135 285
136 my $row = delete $self->{row}; 286 my $row = delete $self->{row};
137 my $col = delete $self->{col}; 287 my $col = delete $self->{col};
288 my $cmd = delete $self->{cmd};
138 289
290 return if !defined $row;
291
139 if(defined($row) && $row == $event->{row} && abs($col-$event->{col}) < 2) { 292 if($row == $event->{row} && abs($col-$event->{col}) < 2
293 && join("\x00", @$cmd) eq join("\x00", $self->command_for($row,$col))) {
140 if($self->valid_button($event)) { 294 if($self->valid_button($event)) {
141 295
142 my @exec = $self->command_for($row,$col); 296 $self->exec_async (@$cmd);
143 if(@exec) { 297
144 return $self->exec_async (@exec);
145 } 298 }
146
147 }
148 } 299 }
149 300
150 () 301 1;
151} 302}
152 303
153# vim:set sw=3 sts=3 et: 304# vim:set sw=3 sts=3 et:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines