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.16 by sf-exg, Wed May 28 17:01:32 2014 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:RESOURCE:%.launcher:string:default launcher command
7#:META:RESOURCE:%.button:string:the button, yeah
8#:META:RESOURCE:%.pattern.:string:extra pattern to match
9#:META:RESOURCE:%.launcher.:string:custom launcher for pattern
10#:META:RESOURCE:%.rend.:string:custom rednition for pattern
11
12=head1 NAME
13
14matcher - match strings in terminal output and change their rendition
15
16=head1 DESCRIPTION
17
18Uses per-line display filtering (C<on_line_update>) to underline text
19matching a certain pattern and make it clickable. When clicked with the
20mouse button specified in the C<matcher.button> resource (default 2, or
21middle), the program specified in the C<matcher.launcher> resource
22(default, the C<url-launcher> resource, C<sensible-browser>) will be started
23with the matched text as first argument. The default configuration is
24suitable for matching URLs and launching a web browser, like the
25former "mark-urls" extension.
26
27The default pattern to match URLs can be overridden with the
28C<matcher.pattern.0> resource, and additional patterns can be specified
29with numbered patterns, in a manner similar to the "selection" extension.
30The launcher can also be overridden on a per-pattern basis.
31
32It is possible to activate the most recently seen match or a list of matches
33from the keyboard. Simply bind a keysym to "perl:matcher:last" or
34"perl:matcher:list" as seen in the example below.
35
36Example: load and use the matcher extension with defaults.
37
38 URxvt.perl-ext: default,matcher
39
40Example: use a custom configuration.
41
42 URxvt.url-launcher: sensible-browser
43 URxvt.keysym.C-Delete: perl:matcher:last
44 URxvt.keysym.M-Delete: perl:matcher:list
45 URxvt.matcher.button: 1
46 URxvt.matcher.pattern.1: \\bwww\\.[\\w-]+\\.[\\w./?&@#-]*[\\w/-]
47 URxvt.matcher.pattern.2: \\B(/\\S+?):(\\d+)(?=:|$)
48 URxvt.matcher.launcher.2: gvim +$2 $1
49
50=cut
4 51
5my $url = 52my $url =
6 qr{ 53 qr{
7 (?:https?://|ftp://|news://|mailto:|file://|\bwww\.)[ab-zA-Z0-9\-\@;\/?:&=%\$_.+!*\x27(),~#]+ 54 (?:https?://|ftp://|news://|mailto:|file://|\bwww\.)
55 [\w\-\@;\/?:&=%\$.+!*\x27,~#]*
56 (
57 \([\w\-\@;\/?:&=%\$.+!*\x27,~#]*\)| # Allow a pair of matched parentheses
8 [ab-zA-Z0-9\-\@;\/?:&=%\$_+*()~] # exclude some trailing characters (heuristic) 58 [\w\-\@;\/?:&=%\$+*~] # exclude some trailing characters (heuristic)
59 )+
9 }x; 60 }x;
10 61
62sub on_key_press {
63 my ($self, $event, $keysym, $octets) = @_;
64
65 if (! $self->{showing} ) {
66 return;
67 }
68
69 my $i = ($keysym == 96 ? 0 : $keysym - 48);
70 if (($i > scalar(@{$self->{urls}})) || ($i < 0)) {
71 $self->matchlist();
72 return;
73 }
74
75 my @args = ($self->{urls}[ -$i-1 ]);
76 $self->matchlist();
77
78 $self->exec_async( $self->{launcher}, @args );
79}
80
81# backwards compat
82sub on_user_command {
83 my ($self, $cmd) = @_;
84
85 if ($cmd =~ s/^matcher:list\b//) {
86 $self->matchlist;
87 } else {
88 if ($cmd =~ s/^matcher:last\b//) {
89 $self->most_recent;
90 } elsif ($cmd =~ s/^matcher\b//) {
91 # for backward compatibility
92 $self->most_recent;
93 }
94 }
95
96 ()
97}
98
99sub matchlist {
100 my ($self) = @_;
101 if ( $self->{showing} ) {
102 $self->{url_overlay}->hide;
103 $self->{showing} = 0;
104 return;
105 }
106 @{$self->{urls}} = ();
107 my $line;
108 for (my $i = 0; $i < $self->nrow; $i ++) {
109 $line = $self->line($i);
110 next if ($line->beg != $i);
111 for my $url ($self->get_urls_from_line($line->t)) {
112 if (scalar(@{$self->{urls}}) == 10) {
113 shift @{$self->{urls}};
114 }
115 push @{$self->{urls}}, $url;
116 }
117 }
118
119 if (! scalar(@{$self->{urls}})) {
120 return;
121 }
122
123 my $max = 0;
124 my $i = scalar( @{$self->{urls}} ) - 1 ;;
125
126 my @temp = ();
127
128 for my $url (@{$self->{urls}}) {
129 my $url = "$i-$url";
130 my $xpos = 0;
131
132 if ($self->ncol + (length $url) >= $self->ncol) {
133 $url = substr( $url, 0, $self->ncol );
134 }
135
136 push @temp, $url;
137
138 if( length $url > $max ) {
139 $max = length $url;
140 }
141
142 $i--;
143 }
144
145 @temp = reverse @temp;
146
147 $self->{url_overlay} = $self->overlay(0, 0, $max, scalar( @temp ), urxvt::OVERLAY_RSTYLE, 2);
148 my $i = 0;
149 for my $url (@temp) {
150 $self->{url_overlay}->set( 0, $i, $url, [(urxvt::OVERLAY_RSTYLE) x length $url]);
151 $self->{showing} = 1;
152 $i++;
153 }
154
155}
156
157sub most_recent {
158 my ($self) = shift;
159 my $row = $self->nrow;
160 my @exec;
161 while($row-- > $self->top_row) {
162 @exec = $self->command_for($row);
163 last if(@exec);
164 }
165 if(@exec) {
166 return $self->exec_async (@exec);
167 }
168 ()
169}
170
11sub my_resource { 171sub my_resource {
12 my $self = shift; 172 $_[0]->x_resource ("%.$_[1]")
13 $self->x_resource("$self->{name}.$_[0]"); 173}
174
175# turn a rendition spec in the resource into a sub that implements it on $_
176sub parse_rend {
177 my ($self, $str) = @_;
178 my ($mask, $fg, $bg, $failed) = $str ? urxvt::rend2mask($str)
179 : (urxvt::RS_Uline, undef, undef, []);
180 warn "Failed to parse rendition string: " . join(',', @$failed) if @$failed;
181 my @rend;
182 push @rend, sub { $_ |= $mask } if $mask;
183 push @rend, sub { $_ = urxvt::SET_FGCOLOR($_, $fg) } if defined $fg;
184 push @rend, sub { $_ = urxvt::SET_BGCOLOR($_, $bg) } if defined $bg;
185 sub {
186 for my $s ( @rend ) { &$s };
187 }
14} 188}
15 189
16sub on_start { 190sub on_start {
17 my ($self) = @_; 191 my ($self) = @_;
18 192
19 ($self->{name} = __PACKAGE__) =~ s/.*:://; 193 $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 194
195 $self->{urls} = [];
196 $self->{showing} = 0;
25 $self->{button} = 2; 197 $self->{button} = 2;
26 $self->{state} = 0; 198 $self->{state} = 0;
27 if($self->{argv}[0] || $self->my_resource("button")) { 199 if($self->{argv}[0] || $self->my_resource ("button")) {
28 my @mods = split('', $self->{argv}[0] || $self->my_resource("button")); 200 my @mods = split '', $self->{argv}[0] || $self->my_resource ("button");
29 for my $mod (@mods) { 201 for my $mod (@mods) {
30 if($mod =~ /^\d+$/) { 202 if($mod =~ /^\d+$/) {
31 $self->{button} = $mod; 203 $self->{button} = $mod;
32 } elsif($mod eq "C") { 204 } elsif($mod eq "C") {
33 $self->{state} |= urxvt::ControlMask; 205 $self->{state} |= urxvt::ControlMask;
34 } elsif($mod eq "S") { 206 } elsif($mod eq "S") {
35 $self->{state} |= urxvt::ShiftMask; 207 $self->{state} |= urxvt::ShiftMask;
36 } elsif($mod eq "M") { 208 } elsif($mod eq "M") {
37 $self->{state} |= $self->ModMetaMask; 209 $self->{state} |= $self->ModMetaMask;
38 } elsif($mod ne "-" && $mod ne " ") { 210 } elsif($mod ne "-" && $mod ne " ") {
39 warn("$mod is invalid in $self->{name}<$self->{argv}[0]>\n"); 211 warn("$mod is invalid in $self->{_name}<$self->{argv}[0]>\n");
40 } 212 }
41 } 213 }
42 } 214 }
43 215
44 my @defaults = ($url); 216 my @defaults = ($url);
45 my @matchers; 217 my @matchers;
46 for (my $idx = 0; defined (my $res = $self->my_resource("pattern.$idx") || $defaults[$idx]); $idx++) { 218 for (my $idx = 0; defined (my $res = $self->my_resource ("pattern.$idx") || $defaults[$idx]); $idx++) {
47 $res = $self->locale_decode ($res); 219 $res = $self->locale_decode ($res);
48 utf8::encode $res; 220 utf8::encode $res;
49 my $launcher = $self->my_resource("launcher.$idx"); 221 my $launcher = $self->my_resource ("launcher.$idx");
50 $launcher =~ s/\$&|\$\{&\}/\${0}/g if ($launcher); 222 $launcher =~ s/\$&|\$\{&\}/\${0}/g if $launcher;
223 my $rend = $self->parse_rend($self->my_resource ("rend.$idx"));
51 push @matchers, [qr($res)x,$launcher]; 224 unshift @matchers, [qr($res)x,$launcher,$rend];
52 } 225 }
53 $self->{matchers} = \@matchers; 226 $self->{matchers} = \@matchers;
54 227
55 () 228 ()
229}
230
231sub get_urls_from_line {
232 my ($self, $line) = @_;
233 my @urls;
234 for my $matcher (@{$self->{matchers}}) {
235 while ($line =~ /$matcher->[0]/g) {
236 push @urls, substr( $line, $-[0], $+[0] - $-[0] );
237 }
238 }
239 return @urls;
56} 240}
57 241
58sub on_line_update { 242sub on_line_update {
59 my ($self, $row) = @_; 243 my ($self, $row) = @_;
60 244
64 my $i = 0; 248 my $i = 0;
65 249
66 # find all urls (if any) 250 # find all urls (if any)
67 for my $matcher (@{$self->{matchers}}) { 251 for my $matcher (@{$self->{matchers}}) {
68 while ($text =~ /$matcher->[0]/g) { 252 while ($text =~ /$matcher->[0]/g) {
253 #print "$&\n";
69 my $rend = $line->r; 254 my $rend = $line->r;
70 255
71 # mark all characters as underlined. we _must_ not toggle underline, 256 # mark all characters as underlined. we _must_ not toggle underline,
72 # as we might get called on an already-marked url. 257 # as we might get called on an already-marked url.
73 $_ |= urxvt::RS_Uline 258 &{$matcher->[2]}
74 for @{$rend}[ $-[0] .. $+[0] - 1]; 259 for @{$rend}[ $-[0] .. $+[0] - 1];
75 260
76 $line->r ($rend); 261 $line->r ($rend);
77 } 262 }
78 } 263 }
97 my $launcher = $matcher->[1] || $self->{launcher}; 282 my $launcher = $matcher->[1] || $self->{launcher};
98 while (($text =~ /$matcher->[0]/g)) { 283 while (($text =~ /$matcher->[0]/g)) {
99 my $match = $&; 284 my $match = $&;
100 my @begin = @-; 285 my @begin = @-;
101 my @end = @+; 286 my @end = @+;
102 if ($-[0] <= $col && $+[0] >= $col) { 287 if (!defined($col) || ($-[0] <= $col && $+[0] >= $col)) {
103 if ($launcher !~ /\$/) { 288 if ($launcher !~ /\$/) {
104 return ($launcher,$match); 289 return ($launcher,$match);
105 } else { 290 } else {
106 # It'd be nice to just access a list like ($&,$1,$2...), 291 # It'd be nice to just access a list like ($&,$1,$2...),
107 # but alas, m//g behaves differently in list context. 292 # but alas, m//g behaves differently in list context.
117 () 302 ()
118} 303}
119 304
120sub on_button_press { 305sub on_button_press {
121 my ($self, $event) = @_; 306 my ($self, $event) = @_;
122 if($self->valid_button($event)) { 307 if($self->valid_button($event)
308 && (my @exec = $self->command_for($event->{row},$event->{col}))) {
123 $self->{row} = $event->{row}; 309 $self->{row} = $event->{row};
124 $self->{col} = $event->{col}; 310 $self->{col} = $event->{col};
311 $self->{cmd} = \@exec;
312 return 1;
125 } else { 313 } else {
126 delete $self->{row}; 314 delete $self->{row};
127 delete $self->{col}; 315 delete $self->{col};
316 delete $self->{cmd};
128 } 317 }
129 318
130 () 319 ()
131} 320}
132 321
133sub on_button_release { 322sub on_button_release {
134 my ($self, $event) = @_; 323 my ($self, $event) = @_;
135 324
136 my $row = delete $self->{row}; 325 my $row = delete $self->{row};
137 my $col = delete $self->{col}; 326 my $col = delete $self->{col};
327 my $cmd = delete $self->{cmd};
138 328
329 return if !defined $row;
330
139 if(defined($row) && $row == $event->{row} && abs($col-$event->{col}) < 2) { 331 if($row == $event->{row} && abs($col-$event->{col}) < 2
332 && join("\x00", @$cmd) eq join("\x00", $self->command_for($row,$col))) {
140 if($self->valid_button($event)) { 333 if($self->valid_button($event)) {
141 334
142 my @exec = $self->command_for($row,$col); 335 $self->exec_async (@$cmd);
143 if(@exec) { 336
144 return $self->exec_async (@exec);
145 } 337 }
146
147 }
148 } 338 }
149 339
150 () 340 1;
151} 341}
152 342
153# vim:set sw=3 sts=3 et: 343# vim:set sw=3 sts=3 et:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines