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 | # Emanuele Giaquinta |
5 | |
6 | |
6 | #:META:RESOURCE:%.launcher:string:default launcher command |
7 | #:META:RESOURCE:%.launcher:string:default launcher command |
7 | #:META:RESOURCE:%.button:string:the button, yeah |
8 | #:META:RESOURCE:%.button:string:the mouse button used to activate a match |
8 | #:META:RESOURCE:%.pattern.:string:extra pattern to match |
9 | #:META:RESOURCE:%.pattern.:string:extra pattern to match |
9 | #:META:RESOURCE:%.launcher.:string:custom launcher for pattern |
10 | #:META:RESOURCE:%.launcher.:string:custom launcher for pattern |
10 | #:META:RESOURCE:%.rend.:string:custom rendition for pattern |
11 | #:META:RESOURCE:%.rend.:string:custom rendition for pattern |
11 | |
12 | |
12 | =head1 NAME |
13 | =head1 NAME |
… | |
… | |
18 | Uses per-line display filtering (C<on_line_update>) to underline text |
19 | Uses per-line display filtering (C<on_line_update>) to underline text |
19 | matching a certain pattern and make it clickable. When clicked with the |
20 | matching a certain pattern and make it clickable. When clicked with the |
20 | mouse button specified in the C<matcher.button> resource (default 2, or |
21 | mouse button specified in the C<matcher.button> resource (default 2, or |
21 | middle), the program specified in the C<matcher.launcher> resource |
22 | middle), the program specified in the C<matcher.launcher> resource |
22 | (default, the C<url-launcher> resource, C<sensible-browser>) will be started |
23 | (default, the C<url-launcher> resource, C<sensible-browser>) will be started |
23 | with the matched text as first argument. The default configuration is |
24 | with the matched text as first argument. The default configuration is |
24 | suitable for matching URLs and launching a web browser, like the |
25 | suitable for matching URLs and launching a web browser, like the |
25 | former "mark-urls" extension. |
26 | former "mark-urls" extension. |
26 | |
27 | |
27 | The default pattern to match URLs can be overridden with the |
28 | The default pattern to match URLs can be overridden with the |
28 | C<matcher.pattern.0> resource, and additional patterns can be specified |
29 | C<matcher.pattern.0> resource, and additional patterns can be specified |
29 | with numbered patterns, in a manner similar to the "selection" extension. |
30 | with numbered patterns, in a manner similar to the "selection" extension. |
30 | The launcher can also be overridden on a per-pattern basis. |
31 | The launcher can also be overridden on a per-pattern basis. |
31 | |
32 | |
32 | It is possible to activate the most recently seen match or a list of matches |
33 | It is possible to activate the most recently seen match or a list of matches |
33 | from the keyboard. Simply bind a keysym to "matcher:last" or |
34 | from the keyboard. Simply bind a keysym to "matcher:last" or |
34 | "matcher:list" as seen in the example below. |
35 | "matcher:list" as seen in the example below. |
35 | |
36 | |
36 | The 'matcher:select' action enables a mode in which it is possible to |
37 | The C<matcher:select> action enables a mode in which it is possible to |
37 | iterate over the matches using the keyboard and either activate them |
38 | iterate over the matches using the keyboard and either activate them |
38 | or copy them to the clipboard. While the mode is active, normal terminal |
39 | or copy them to the clipboard. While the mode is active, normal terminal |
39 | input/output is suspended and the following bindings are recognized: |
40 | input/output is suspended and the following bindings are recognized: |
40 | |
41 | |
41 | =over 4 |
42 | =over |
42 | |
43 | |
43 | =item C<Up> |
44 | =item C<Up> |
44 | |
45 | |
45 | Search for a match upwards. |
46 | Search for a match upwards. |
46 | |
47 | |
… | |
… | |
67 | =item C<y> |
68 | =item C<y> |
68 | |
69 | |
69 | Copy the current match to the clipboard. |
70 | Copy the current match to the clipboard. |
70 | |
71 | |
71 | =back |
72 | =back |
|
|
73 | |
|
|
74 | It is also possible to cycle through the matches using a key |
|
|
75 | combination bound to the C<matcher:select> action. |
72 | |
76 | |
73 | Example: load and use the matcher extension with defaults. |
77 | Example: load and use the matcher extension with defaults. |
74 | |
78 | |
75 | URxvt.perl-ext: default,matcher |
79 | URxvt.perl-ext: default,matcher |
76 | |
80 | |
… | |
… | |
82 | URxvt.matcher.button: 1 |
86 | URxvt.matcher.button: 1 |
83 | URxvt.matcher.pattern.1: \\bwww\\.[\\w-]+\\.[\\w./?&@#-]*[\\w/-] |
87 | URxvt.matcher.pattern.1: \\bwww\\.[\\w-]+\\.[\\w./?&@#-]*[\\w/-] |
84 | URxvt.matcher.pattern.2: \\B(/\\S+?):(\\d+)(?=:|$) |
88 | URxvt.matcher.pattern.2: \\B(/\\S+?):(\\d+)(?=:|$) |
85 | URxvt.matcher.launcher.2: gvim +$2 $1 |
89 | URxvt.matcher.launcher.2: gvim +$2 $1 |
86 | |
90 | |
|
|
91 | =head2 Regex encoding/wide character matching |
|
|
92 | |
|
|
93 | Urxvt stores all text as unicode, in a special encoding that uses |
|
|
94 | one character/code point per column. For various reasons, the regular |
|
|
95 | expressions are matched directly against this encoding, which means there are a few things |
|
|
96 | you need to keep in mind: |
|
|
97 | |
|
|
98 | =over |
|
|
99 | |
|
|
100 | =item X resources/command line arguments are locale-encoded |
|
|
101 | |
|
|
102 | The regexes taken from the command line or resources will be converted |
|
|
103 | from locale encoding to unicode. This can change the number of code points |
|
|
104 | per character. |
|
|
105 | |
|
|
106 | =item Wide characters are column-padded with C<$urxvt::NOCHAR> |
|
|
107 | |
|
|
108 | Wide characters (such as kanji and sometimes tabs) are padded with |
|
|
109 | a special character value (C<$urxvt::NOCHAR>). That means that |
|
|
110 | constructs such as C<\w> or C<.> will only match part of a character, as |
|
|
111 | C<$urxvt::NOCHAR> is not matched by C<\w> and both only match the first |
|
|
112 | "column" of a wide character. |
|
|
113 | |
|
|
114 | That means you have to incorporate C<$urxvt::NOCHAR> into parts of regexes |
|
|
115 | that may match wide characters. For example, to match C<\w+> you might |
|
|
116 | want to use C<[\w$urxvt::NOCHAR]+> instead, and to match a single character |
|
|
117 | (C<.>) you might want to use C<.$urxvt::NOCHAR*> instead. |
|
|
118 | |
|
|
119 | =back |
|
|
120 | |
87 | =cut |
121 | =cut |
88 | |
122 | |
89 | my $url = |
123 | my $url = |
90 | qr{ |
124 | qr{ |
91 | (?:https?://|ftp://|news://|mailto:|file://|\bwww\.) |
125 | (?:https?://|ftp://|news://|mailto:|file://|\bwww\.) |
92 | [\w\-\@;\/?:&=%\$.+!*\x27,~#]* |
126 | [\w\-\@;\/?:&=%\$.+!*\x27,~#$urxvt::NOCHAR]* |
93 | ( |
127 | ( |
94 | \([\w\-\@;\/?:&=%\$.+!*\x27,~#]*\)| # Allow a pair of matched parentheses |
128 | \([\w\-\@;\/?:&=%\$.+!*\x27,~#$urxvt::NOCHAR]*\)| # Allow a pair of matched parentheses |
95 | [\w\-\@;\/?:&=%\$+*~] # exclude some trailing characters (heuristic) |
129 | [\w\-\@;\/?:&=%\$+*~] # exclude some trailing characters (heuristic) |
96 | )+ |
130 | )+ |
97 | }x; |
131 | }x; |
98 | |
132 | |
99 | sub matchlist_key_press { |
133 | sub matchlist_key_press { |
… | |
… | |
113 | |
147 | |
114 | # backwards compat |
148 | # backwards compat |
115 | sub on_user_command { |
149 | sub on_user_command { |
116 | my ($self, $cmd) = @_; |
150 | my ($self, $cmd) = @_; |
117 | |
151 | |
118 | if ($cmd =~ s/^matcher:list\b//) { |
152 | if ($cmd eq "matcher:list") { |
119 | $self->matchlist; |
153 | $self->matchlist; |
120 | } else { |
154 | } elsif ($cmd eq "matcher:last") { |
121 | if ($cmd =~ s/^matcher:last\b//) { |
|
|
122 | $self->most_recent; |
155 | $self->most_recent; |
|
|
156 | } elsif ($cmd eq "matcher:select") { |
|
|
157 | $self->select_enter; |
123 | } elsif ($cmd =~ s/^matcher\b//) { |
158 | } elsif ($cmd eq "matcher") { |
124 | # for backward compatibility |
159 | # for backward compatibility |
125 | $self->most_recent; |
160 | $self->most_recent; |
126 | } |
|
|
127 | } |
161 | } |
128 | |
162 | |
129 | () |
163 | () |
130 | } |
164 | } |
131 | |
165 | |
… | |
… | |
187 | $self->enable (key_press => \&matchlist_key_press); |
221 | $self->enable (key_press => \&matchlist_key_press); |
188 | } |
222 | } |
189 | |
223 | |
190 | sub most_recent { |
224 | sub most_recent { |
191 | my ($self) = shift; |
225 | my ($self) = shift; |
192 | my $row = $self->nrow; |
226 | my $row = $self->nrow - 1; |
193 | my @exec; |
227 | my @exec; |
|
|
228 | |
194 | while($row-- > $self->top_row) { |
229 | while ($row >= $self->top_row) { |
|
|
230 | my $line = $self->line ($row); |
195 | @exec = $self->command_for($row); |
231 | @exec = $self->command_for ($row); |
196 | last if(@exec); |
232 | last if @exec; |
|
|
233 | |
|
|
234 | $row = $line->beg - 1; |
197 | } |
235 | } |
|
|
236 | |
198 | if(@exec) { |
237 | if (@exec) { |
199 | return $self->exec_async (@exec); |
238 | return $self->exec_async (@exec); |
200 | } |
239 | } |
|
|
240 | |
201 | () |
241 | () |
202 | } |
242 | } |
203 | |
243 | |
204 | sub my_resource { |
244 | sub my_resource { |
205 | $_[0]->x_resource ("%.$_[1]") |
245 | $_[0]->x_resource ("%.$_[1]") |
… | |
… | |
244 | } |
284 | } |
245 | } |
285 | } |
246 | |
286 | |
247 | my @defaults = ($url); |
287 | my @defaults = ($url); |
248 | my @matchers; |
288 | my @matchers; |
249 | for (my $idx = 0; defined (my $res = $self->my_resource ("pattern.$idx") || $defaults[$idx]); $idx++) { |
289 | for (my $idx = 0; defined (my $res = $self->locale_decode ($self->my_resource ("pattern.$idx")) || $defaults[$idx]); $idx++) { |
250 | $res = $self->locale_decode ($res); |
|
|
251 | utf8::encode $res; |
|
|
252 | my $launcher = $self->my_resource ("launcher.$idx"); |
290 | my $launcher = $self->my_resource ("launcher.$idx"); |
253 | $launcher =~ s/\$&|\$\{&\}/\${0}/g if $launcher; |
291 | $launcher =~ s/\$&|\$\{&\}/\${0}/g if $launcher; |
254 | my $rend = $self->parse_rend($self->my_resource ("rend.$idx")); |
292 | my $rend = $self->parse_rend($self->my_resource ("rend.$idx")); |
255 | unshift @matchers, [qr($res)x,$launcher,$rend]; |
293 | unshift @matchers, [qr($res)x,$launcher,$rend]; |
256 | } |
294 | } |
… | |
… | |
263 | my ($self, $row) = @_; |
301 | my ($self, $row) = @_; |
264 | |
302 | |
265 | # fetch the line that has changed |
303 | # fetch the line that has changed |
266 | my $line = $self->line ($row); |
304 | my $line = $self->line ($row); |
267 | my $text = $line->t; |
305 | my $text = $line->t; |
|
|
306 | my $rend; |
268 | |
307 | |
269 | # find all urls (if any) |
308 | # find all urls (if any) |
270 | for my $matcher (@{$self->{matchers}}) { |
309 | for my $matcher (@{$self->{matchers}}) { |
271 | while ($text =~ /$matcher->[0]/g) { |
310 | while ($text =~ /$matcher->[0]/g) { |
272 | #print "$&\n"; |
311 | #print "$&\n"; |
273 | my $rend = $line->r; |
312 | $rend ||= $line->r; |
274 | |
313 | |
275 | # mark all characters as underlined. we _must_ not toggle underline, |
314 | # mark all characters as underlined. we _must_ not toggle underline, |
276 | # as we might get called on an already-marked url. |
315 | # as we might get called on an already-marked url. |
277 | &{$matcher->[2]} |
316 | &{$matcher->[2]} |
278 | for @{$rend}[$-[0] .. $+[0] - 1]; |
317 | for @{$rend}[$-[0] .. $+[0] - 1]; |
279 | |
|
|
280 | $line->r ($rend); |
|
|
281 | } |
318 | } |
282 | } |
319 | } |
|
|
320 | |
|
|
321 | $line->r ($rend) if $rend; |
283 | |
322 | |
284 | () |
323 | () |
285 | } |
324 | } |
286 | |
325 | |
287 | sub valid_button { |
326 | sub valid_button { |
… | |
… | |
305 | my $match = substr $text, $-[0], $+[0] - $-[0]; |
344 | my $match = substr $text, $-[0], $+[0] - $-[0]; |
306 | my @begin = @-; |
345 | my @begin = @-; |
307 | my @end = @+; |
346 | my @end = @+; |
308 | my @exec; |
347 | my @exec; |
309 | |
348 | |
310 | if (!defined($off) || ($-[0] <= $off && $+[0] >= $off)) { |
349 | if (!(defined $off) || ($-[0] <= $off && $+[0] >= $off)) { |
311 | if ($launcher !~ /\$/) { |
350 | if ($launcher !~ /\$/) { |
312 | @exec = ($launcher, $match); |
351 | @exec = ($launcher, $match); |
313 | } else { |
352 | } else { |
314 | # It'd be nice to just access a list like ($&,$1,$2...), |
353 | # It'd be nice to just access a list like ($&,$1,$2...), |
315 | # but alas, m//g behaves differently in list context. |
354 | # but alas, m//g behaves differently in list context. |
316 | @exec = map { s/\$(\d+)|\$\{(\d+)\}/ |
355 | @exec = map { |
|
|
356 | s{\$(\d+)|\$\{(\d+)\}}{ |
317 | substr $text, $begin[$1 || $2], $end[$1 || $2] - $begin[$1 || $2] |
357 | substr $text, $begin[$1 || $2], $end[$1 || $2] - $begin[$1 || $2] |
|
|
358 | }egx; |
|
|
359 | $_ |
318 | /egx; $_ } split /\s+/, $launcher; |
360 | } split /\s+/, $launcher; |
319 | } |
361 | } |
320 | |
362 | |
321 | push @matches, [ $line->coord_of ($begin[0]), $line->coord_of ($end[0]), $match, @exec ]; |
363 | push @matches, [ $line->coord_of ($begin[0]), $line->coord_of ($end[0]), $match, @exec ]; |
322 | } |
364 | } |
323 | } |
365 | } |
324 | } |
366 | } |
325 | |
367 | |
326 | @matches; |
368 | @matches |
327 | } |
369 | } |
328 | |
370 | |
329 | sub command_for { |
371 | sub command_for { |
330 | my ($self, $row, $col) = @_; |
372 | my ($self, $row, $col) = @_; |
331 | |
373 | |
… | |
… | |
338 | () |
380 | () |
339 | } |
381 | } |
340 | |
382 | |
341 | sub on_button_press { |
383 | sub on_button_press { |
342 | my ($self, $event) = @_; |
384 | my ($self, $event) = @_; |
|
|
385 | |
|
|
386 | if ( |
343 | if($self->valid_button($event) |
387 | $self->valid_button ($event) |
344 | && (my @exec = $self->command_for($event->{row},$event->{col}))) { |
388 | && (my @exec = $self->command_for ($event->{row}, $event->{col})) |
|
|
389 | ) { |
345 | $self->{row} = $event->{row}; |
390 | $self->{row} = $event->{row}; |
346 | $self->{col} = $event->{col}; |
391 | $self->{col} = $event->{col}; |
347 | $self->{cmd} = \@exec; |
392 | $self->{cmd} = \@exec; |
348 | return 1; |
393 | return 1; |
349 | } else { |
394 | } else { |
… | |
… | |
362 | my $col = delete $self->{col}; |
407 | my $col = delete $self->{col}; |
363 | my $cmd = delete $self->{cmd}; |
408 | my $cmd = delete $self->{cmd}; |
364 | |
409 | |
365 | return if !defined $row; |
410 | return if !defined $row; |
366 | |
411 | |
367 | if($row == $event->{row} && abs($col-$event->{col}) < 2 |
412 | if ( |
|
|
413 | $row == $event->{row} |
|
|
414 | && (abs $col-$event->{col}) < 2 |
368 | && join("\x00", @$cmd) eq join("\x00", $self->command_for($row,$col))) { |
415 | && (join "\x00", @$cmd) eq (join "\x00", $self->command_for ($row, $col)) |
|
|
416 | ) { |
369 | if($self->valid_button($event)) { |
417 | if ($self->valid_button ($event)) { |
370 | |
|
|
371 | $self->exec_async (@$cmd); |
418 | $self->exec_async (@$cmd); |
372 | |
|
|
373 | } |
419 | } |
374 | } |
420 | } |
375 | |
421 | |
376 | 1; |
422 | 1; |
377 | } |
423 | } |
… | |
… | |
415 | if (@matches) { |
461 | if (@matches) { |
416 | @matches = sort { $a->[0] <=> $b->[0] or $a->[1] <=> $b->[1] } @matches; |
462 | @matches = sort { $a->[0] <=> $b->[0] or $a->[1] <=> $b->[1] } @matches; |
417 | $self->{matches} = \@matches; |
463 | $self->{matches} = \@matches; |
418 | $self->{cur_row} = $row; |
464 | $self->{cur_row} = $row; |
419 | $self->{id} = $dir < 0 ? @{ $self->{matches} } - 1 : 0; |
465 | $self->{id} = $dir < 0 ? @{ $self->{matches} } - 1 : 0; |
420 | $self->view_start (List::Util::min 0, $row - ($self->nrow >> 1)); |
466 | $self->view_start ($row - ($self->nrow >> 1)); |
421 | $self->want_refresh; |
467 | $self->want_refresh; |
422 | return; |
468 | return 1; |
423 | } |
469 | } |
424 | |
470 | |
425 | $row = $dir < 0 ? $line->beg - 1 : $line->end + 1; |
471 | $row = $dir < 0 ? $line->beg - 1 : $line->end + 1; |
426 | } |
472 | } |
427 | |
473 | |
428 | $self->scr_bell; |
474 | $self->scr_bell; |
|
|
475 | |
|
|
476 | () |
429 | } |
477 | } |
430 | |
478 | |
431 | sub select_refresh { |
479 | sub select_refresh { |
432 | my ($self) = @_; |
480 | my ($self) = @_; |
433 | |
481 | |
… | |
… | |
477 | } else { |
525 | } else { |
478 | my $line = $self->line ($self->{cur_row}); |
526 | my $line = $self->line ($self->{cur_row}); |
479 | $self->select_search (+1, $line->end + 1) |
527 | $self->select_search (+1, $line->end + 1) |
480 | if $line->end < $self->nrow; |
528 | if $line->end < $self->nrow; |
481 | } |
529 | } |
|
|
530 | } elsif ($self->lookup_keysym ($keysym, $event->{state}) eq "matcher:select") { |
|
|
531 | if ($self->{id} > 0) { |
|
|
532 | $self->{id}--; |
|
|
533 | $self->want_refresh; |
|
|
534 | } else { |
|
|
535 | my $line = $self->line ($self->{cur_row}); |
|
|
536 | $self->select_search (-1, $self->nrow - 1) |
|
|
537 | unless $self->select_search (-1, $line->beg - 1); |
|
|
538 | } |
482 | } |
539 | } |
483 | |
540 | |
484 | 1 |
541 | 1 |
485 | } |
542 | } |
486 | |
543 | |