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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines