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.9 by root, Sun Jun 10 13:58:05 2012 UTC vs.
Revision 1.27 by sf-exg, Sun Jun 15 11:38:00 2014 UTC

1#! perl 1#! perl
2 2
3# Author: Tim Pope <rxvt-unicodeNOSPAM@tpope.org> 3# Author: Tim Pope <rxvt-unicodeNOSPAM@tpope.org>
4# Bob Farrell <robertanthonyfarrell@gmail.com> 4# Bob Farrell <robertanthonyfarrell@gmail.com>
5 5
6#:META:X_RESOURCE:%.launcher:string:default launcher command 6#:META:RESOURCE:%.launcher:string:default launcher command
7#:META:X_RESOURCE:%.button:string:the button, yeah 7#:META:RESOURCE:%.button:string:the button, yeah
8#:META:X_RESOURCE:%.pattern.:string:extra pattern to match 8#:META:RESOURCE:%.pattern.:string:extra pattern to match
9#:META:X_RESOURCE:%.launcher.:string:custom launcher for pattern 9#:META:RESOURCE:%.launcher.:string:custom launcher for pattern
10#:META:X_RESOURCE:%.rend.:string:custom rednition 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 "matcher:last" or
34"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: matcher:last
44 URxvt.keysym.M-Delete: 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
11 51
12my $url = 52my $url =
13 qr{ 53 qr{
14 (?:https?://|ftp://|news://|mailto:|file://|\bwww\.) 54 (?:https?://|ftp://|news://|mailto:|file://|\bwww\.)
15 [a-zA-Z0-9\-\@;\/?:&=%\$_.+!*\x27,~#]* 55 [\w\-\@;\/?:&=%\$.+!*\x27,~#]*
16 ( 56 (
17 \([a-zA-Z0-9\-\@;\/?:&=%\$_.+!*\x27,~#]*\)| # Allow a pair of matched parentheses 57 \([\w\-\@;\/?:&=%\$.+!*\x27,~#]*\)| # Allow a pair of matched parentheses
18 [a-zA-Z0-9\-\@;\/?:&=%\$_+*~] # exclude some trailing characters (heuristic) 58 [\w\-\@;\/?:&=%\$+*~] # exclude some trailing characters (heuristic)
19 )+ 59 )+
20 }x; 60 }x;
21 61
22sub on_key_press { 62sub matchlist_key_press {
23 my ($self, $event, $keysym, $octets) = @_; 63 my ($self, $event, $keysym, $octets) = @_;
24 64
25 if (! $self->{showing} ) { 65 delete $self->{overlay};
26 return; 66 $self->disable ("key_press");
27 }
28 67
29 my $i = ($keysym == 96 ? 0 : $keysym - 48); 68 my $i = ($keysym == 96 ? 0 : $keysym - 48);
30 if (($i > scalar(@{$self->{urls}})) || ($i < 0)) { 69 if ($i >= 0 && $i < @{ $self->{matches} }) {
31 $self->matchlist(); 70 my @exec = @{ $self->{matches}[$i] };
32 return; 71 $self->exec_async (@exec[5 .. $#exec]);
72 }
73
33 } 74 1
34
35 my @args = ($self->{urls}[ -$i-1 ]);
36 $self->matchlist();
37
38 $self->exec_async( $self->{launcher}, @args );
39} 75}
40 76
77# backwards compat
41sub on_user_command { 78sub on_user_command {
42 my ($self, $cmd) = @_; 79 my ($self, $cmd) = @_;
43 80
44 if($cmd =~ s/^matcher:list\b//) { 81 if ($cmd =~ s/^matcher:list\b//) {
45 $self->matchlist(); 82 $self->matchlist;
46 } else { 83 } else {
47 if($cmd =~ s/^matcher:last\b//) { 84 if ($cmd =~ s/^matcher:last\b//) {
48 $self->most_recent; 85 $self->most_recent;
49 }
50 # For backward compatibility
51 else {
52 if($cmd =~ s/^matcher\b//) { 86 } elsif ($cmd =~ s/^matcher\b//) {
87 # for backward compatibility
53 $self->most_recent; 88 $self->most_recent;
54 } 89 }
55 } 90 }
91
92 ()
93}
94
95sub on_action {
96 my ($self, $action) = @_;
97
98 if ($action eq "list") {
99 $self->matchlist;
100 } elsif ($action eq "last") {
101 $self->most_recent;
56 } 102 }
103
57 () 104 ()
58} 105}
59 106
60sub matchlist { 107sub matchlist {
61 my ($self) = @_; 108 my ($self) = @_;
62 if ( $self->{showing} ) { 109
63 $self->{url_overlay}->hide(); 110 $self->{matches} = [];
64 $self->{showing} = 0; 111 my $row = $self->nrow - 1;
65 return; 112 while ($row >= 0 && @{ $self->{matches} } < 10) {
66 }
67 @{$self->{urls}} = ();
68 my $line;
69 for (my $i = 0; $i < $self->nrow; $i ++) {
70 $line = $self->line($i); 113 my $line = $self->line ($row);
71 next if ($line->beg != $i); 114 my @matches = $self->find_matches ($row);
72 for my $url ($self->get_urls_from_line($line->t)) { 115
73 if (scalar(@{$self->{urls}}) == 10) { 116 for (sort { $b->[0] <=> $a->[0] or $b->[1] <=> $a->[1] } @matches) {
74 shift @{$self->{urls}};
75 }
76 push @{$self->{urls}}, $url; 117 push @{ $self->{matches} }, $_;
118 last if @{ $self->{matches} } == 10;
77 } 119 }
120
121 $row = $line->beg - 1;
78 } 122 }
79 123
80 if (! scalar(@{$self->{urls}})) { 124 return unless @{ $self->{matches} };
81 return;
82 }
83 125
84 my $max = 0; 126 my $width = 0;
85 my $i = scalar( @{$self->{urls}} ) - 1 ;;
86 127
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; 128 my $i = 0;
110 for my $url (@temp) { 129 for my $match (@{ $self->{matches} }) {
111 $self->{url_overlay}->set( 0, $i, $url, [(urxvt::OVERLAY_RSTYLE) x length $url]); 130 my $text = $match->[4];
112 $self->{showing} = 1; 131 my $w = $self->strwidth ("$i-$text");
132
133 $width = $w if $w > $width;
113 $i++; 134 $i++;
114 } 135 }
115 136
137 $width = $self->ncol - 2 if $width > $self->ncol - 2;
138
139 $self->{overlay} = $self->overlay (0, 0, $width, scalar (@{ $self->{matches} }), urxvt::OVERLAY_RSTYLE, 2);
140 my $i = 0;
141 for my $match (@{ $self->{matches} }) {
142 my $text = $match->[4];
143
144 $self->{overlay}->set (0, $i, "$i-$text");
145 $i++;
146 }
147
148 $self->enable (key_press => \&matchlist_key_press);
116} 149}
117 150
118sub most_recent { 151sub most_recent {
119 my ($self) = shift; 152 my ($self) = shift;
120 my $row = $self->nrow; 153 my $row = $self->nrow;
151sub on_start { 184sub on_start {
152 my ($self) = @_; 185 my ($self) = @_;
153 186
154 $self->{launcher} = $self->my_resource ("launcher") || $self->x_resource("url-launcher") || "sensible-browser"; 187 $self->{launcher} = $self->my_resource ("launcher") || $self->x_resource("url-launcher") || "sensible-browser";
155 188
156 $self->{urls} = [];
157 $self->{showing} = 0;
158 $self->{button} = 2; 189 $self->{button} = 2;
159 $self->{state} = 0; 190 $self->{state} = 0;
160 if($self->{argv}[0] || $self->my_resource ("button")) { 191 if($self->{argv}[0] || $self->my_resource ("button")) {
161 my @mods = split '', $self->{argv}[0] || $self->my_resource ("button"); 192 my @mods = split '', $self->{argv}[0] || $self->my_resource ("button");
162 for my $mod (@mods) { 193 for my $mod (@mods) {
187 $self->{matchers} = \@matchers; 218 $self->{matchers} = \@matchers;
188 219
189 () 220 ()
190} 221}
191 222
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;
201}
202
203sub on_line_update { 223sub on_line_update {
204 my ($self, $row) = @_; 224 my ($self, $row) = @_;
205 225
206 # fetch the line that has changed 226 # fetch the line that has changed
207 my $line = $self->line ($row); 227 my $line = $self->line ($row);
208 my $text = $line->t; 228 my $text = $line->t;
209 my $i = 0;
210 229
211 # find all urls (if any) 230 # find all urls (if any)
212 for my $matcher (@{$self->{matchers}}) { 231 for my $matcher (@{$self->{matchers}}) {
213 while ($text =~ /$matcher->[0]/g) { 232 while ($text =~ /$matcher->[0]/g) {
214 #print "$&\n"; 233 #print "$&\n";
232 | urxvt::ShiftMask | urxvt::ControlMask; 251 | urxvt::ShiftMask | urxvt::ControlMask;
233 return ($event->{button} == $self->{button} && 252 return ($event->{button} == $self->{button} &&
234 ($event->{state} & $mask) == $self->{state}); 253 ($event->{state} & $mask) == $self->{state});
235} 254}
236 255
237sub command_for { 256sub find_matches {
238 my ($self, $row, $col) = @_; 257 my ($self, $row, $col) = @_;
239 my $line = $self->line ($row); 258 my $line = $self->line ($row);
240 my $text = $line->t; 259 my $text = $line->t;
260 my $off = $line->offset_of ($row, $col) if $col;
241 261
262 my @matches;
242 for my $matcher (@{$self->{matchers}}) { 263 for my $matcher (@{$self->{matchers}}) {
243 my $launcher = $matcher->[1] || $self->{launcher}; 264 my $launcher = $matcher->[1] || $self->{launcher};
244 while (($text =~ /$matcher->[0]/g)) { 265 while ($text =~ /$matcher->[0]/g) {
245 my $match = $&; 266 my $match = substr $text, $-[0], $+[0] - $-[0];
246 my @begin = @-; 267 my @begin = @-;
247 my @end = @+; 268 my @end = @+;
269 my @exec;
270
248 if (!defined($col) || ($-[0] <= $col && $+[0] >= $col)) { 271 if (!defined($off) || ($-[0] <= $off && $+[0] >= $off)) {
249 if ($launcher !~ /\$/) { 272 if ($launcher !~ /\$/) {
250 return ($launcher,$match); 273 @exec = ($launcher, $match);
251 } else { 274 } else {
252 # It'd be nice to just access a list like ($&,$1,$2...), 275 # It'd be nice to just access a list like ($&,$1,$2...),
253 # but alas, m//g behaves differently in list context. 276 # but alas, m//g behaves differently in list context.
254 my @exec = map { s/\$(\d+)|\$\{(\d+)\}/ 277 @exec = map { s/\$(\d+)|\$\{(\d+)\}/
255 substr($text,$begin[$1||$2],$end[$1||$2]-$begin[$1||$2]) 278 substr $text, $begin[$1 || $2], $end[$1 || $2] - $begin[$1 || $2]
256 /egx; $_ } split(/\s+/, $launcher); 279 /egx; $_ } split /\s+/, $launcher;
257 return @exec;
258 } 280 }
281
282 push @matches, [ $line->coord_of ($begin[0]), $line->coord_of ($end[0]), $match, @exec ];
259 } 283 }
260 } 284 }
285 }
286
287 @matches;
288}
289
290sub command_for {
291 my ($self, $row, $col) = @_;
292
293 my @matches = $self->find_matches ($row, $col);
294 if (@matches) {
295 my @match = @{ $matches[0] };
296 return @match[5 .. $#match];
261 } 297 }
262 298
263 () 299 ()
264} 300}
265 301
291 327
292 if($row == $event->{row} && abs($col-$event->{col}) < 2 328 if($row == $event->{row} && abs($col-$event->{col}) < 2
293 && join("\x00", @$cmd) eq join("\x00", $self->command_for($row,$col))) { 329 && join("\x00", @$cmd) eq join("\x00", $self->command_for($row,$col))) {
294 if($self->valid_button($event)) { 330 if($self->valid_button($event)) {
295 331
296 $self->exec_async (@$cmd); 332 $self->exec_async (@$cmd);
297 333
298 } 334 }
299 } 335 }
300 336
301 1; 337 1;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines