… | |
… | |
19 | 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 |
20 | 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 |
21 | 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 |
22 | middle), the program specified in the C<matcher.launcher> resource |
22 | middle), the program specified in the C<matcher.launcher> resource |
23 | (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 |
24 | with the matched text as first argument. The default configuration is |
24 | with the matched text as first argument. The default configuration is |
25 | suitable for matching URLs and launching a web browser, like the |
25 | suitable for matching URLs and launching a web browser, like the |
26 | former "mark-urls" extension. |
26 | former "mark-urls" extension. |
27 | |
27 | |
28 | The default pattern to match URLs can be overridden with the |
28 | The default pattern to match URLs can be overridden with the |
29 | C<matcher.pattern.0> resource, and additional patterns can be specified |
29 | C<matcher.pattern.0> resource, and additional patterns can be specified |
30 | with numbered patterns, in a manner similar to the "selection" extension. |
30 | with numbered patterns, in a manner similar to the "selection" extension. |
31 | The launcher can also be overridden on a per-pattern basis. |
31 | The launcher can also be overridden on a per-pattern basis. |
32 | |
32 | |
33 | 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 |
34 | from the keyboard. Simply bind a keysym to "matcher:last" or |
34 | from the keyboard. Simply bind a keysym to "matcher:last" or |
35 | "matcher:list" as seen in the example below. |
35 | "matcher:list" as seen in the example below. |
36 | |
36 | |
37 | 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 |
38 | iterate over the matches using the keyboard and either activate them |
38 | iterate over the matches using the keyboard and either activate them |
39 | 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 |
40 | input/output is suspended and the following bindings are recognized: |
40 | input/output is suspended and the following bindings are recognized: |
41 | |
41 | |
42 | =over 4 |
42 | =over |
43 | |
43 | |
44 | =item C<Up> |
44 | =item C<Up> |
45 | |
45 | |
46 | Search for a match upwards. |
46 | Search for a match upwards. |
47 | |
47 | |
… | |
… | |
68 | =item C<y> |
68 | =item C<y> |
69 | |
69 | |
70 | Copy the current match to the clipboard. |
70 | Copy the current match to the clipboard. |
71 | |
71 | |
72 | =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. |
73 | |
76 | |
74 | Example: load and use the matcher extension with defaults. |
77 | Example: load and use the matcher extension with defaults. |
75 | |
78 | |
76 | URxvt.perl-ext: default,matcher |
79 | URxvt.perl-ext: default,matcher |
77 | |
80 | |
… | |
… | |
83 | URxvt.matcher.button: 1 |
86 | URxvt.matcher.button: 1 |
84 | URxvt.matcher.pattern.1: \\bwww\\.[\\w-]+\\.[\\w./?&@#-]*[\\w/-] |
87 | URxvt.matcher.pattern.1: \\bwww\\.[\\w-]+\\.[\\w./?&@#-]*[\\w/-] |
85 | URxvt.matcher.pattern.2: \\B(/\\S+?):(\\d+)(?=:|$) |
88 | URxvt.matcher.pattern.2: \\B(/\\S+?):(\\d+)(?=:|$) |
86 | URxvt.matcher.launcher.2: gvim +$2 $1 |
89 | URxvt.matcher.launcher.2: gvim +$2 $1 |
87 | |
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 | |
88 | =cut |
121 | =cut |
89 | |
122 | |
90 | my $url = |
123 | my $url = |
91 | qr{ |
124 | qr{ |
92 | (?:https?://|ftp://|news://|mailto:|file://|\bwww\.) |
125 | (?:https?://|ftp://|news://|mailto:|file://|\bwww\.) |
93 | [\w\-\@;\/?:&=%\$.+!*\x27,~#]* |
126 | [\w\-\@;\/?:&=%\$.+!*\x27,~#$urxvt::NOCHAR]* |
94 | ( |
127 | ( |
95 | \([\w\-\@;\/?:&=%\$.+!*\x27,~#]*\)| # Allow a pair of matched parentheses |
128 | \([\w\-\@;\/?:&=%\$.+!*\x27,~#$urxvt::NOCHAR]*\)| # Allow a pair of matched parentheses |
96 | [\w\-\@;\/?:&=%\$+*~] # exclude some trailing characters (heuristic) |
129 | [\w\-\@;\/?:&=%\$+*~] # exclude some trailing characters (heuristic) |
97 | )+ |
130 | )+ |
98 | }x; |
131 | }x; |
99 | |
132 | |
100 | sub matchlist_key_press { |
133 | sub matchlist_key_press { |
… | |
… | |
190 | |
223 | |
191 | sub most_recent { |
224 | sub most_recent { |
192 | my ($self) = shift; |
225 | my ($self) = shift; |
193 | my $row = $self->nrow - 1; |
226 | my $row = $self->nrow - 1; |
194 | my @exec; |
227 | my @exec; |
|
|
228 | |
195 | while ($row >= $self->top_row) { |
229 | while ($row >= $self->top_row) { |
196 | my $line = $self->line ($row); |
230 | my $line = $self->line ($row); |
197 | @exec = $self->command_for($row); |
231 | @exec = $self->command_for ($row); |
198 | last if(@exec); |
232 | last if @exec; |
199 | |
233 | |
200 | $row = $line->beg - 1; |
234 | $row = $line->beg - 1; |
201 | } |
235 | } |
|
|
236 | |
202 | if(@exec) { |
237 | if (@exec) { |
203 | return $self->exec_async (@exec); |
238 | return $self->exec_async (@exec); |
204 | } |
239 | } |
|
|
240 | |
205 | () |
241 | () |
206 | } |
242 | } |
207 | |
243 | |
208 | sub my_resource { |
244 | sub my_resource { |
209 | $_[0]->x_resource ("%.$_[1]") |
245 | $_[0]->x_resource ("%.$_[1]") |
… | |
… | |
248 | } |
284 | } |
249 | } |
285 | } |
250 | |
286 | |
251 | my @defaults = ($url); |
287 | my @defaults = ($url); |
252 | my @matchers; |
288 | my @matchers; |
253 | 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++) { |
254 | $res = $self->locale_decode ($res); |
|
|
255 | utf8::encode $res; |
|
|
256 | my $launcher = $self->my_resource ("launcher.$idx"); |
290 | my $launcher = $self->my_resource ("launcher.$idx"); |
257 | $launcher =~ s/\$&|\$\{&\}/\${0}/g if $launcher; |
291 | $launcher =~ s/\$&|\$\{&\}/\${0}/g if $launcher; |
258 | my $rend = $self->parse_rend($self->my_resource ("rend.$idx")); |
292 | my $rend = $self->parse_rend($self->my_resource ("rend.$idx")); |
259 | unshift @matchers, [qr($res)x,$launcher,$rend]; |
293 | unshift @matchers, [qr($res)x,$launcher,$rend]; |
260 | } |
294 | } |
… | |
… | |
310 | my $match = substr $text, $-[0], $+[0] - $-[0]; |
344 | my $match = substr $text, $-[0], $+[0] - $-[0]; |
311 | my @begin = @-; |
345 | my @begin = @-; |
312 | my @end = @+; |
346 | my @end = @+; |
313 | my @exec; |
347 | my @exec; |
314 | |
348 | |
315 | if (!defined($off) || ($-[0] <= $off && $+[0] >= $off)) { |
349 | if (!(defined $off) || ($-[0] <= $off && $+[0] >= $off)) { |
316 | if ($launcher !~ /\$/) { |
350 | if ($launcher !~ /\$/) { |
317 | @exec = ($launcher, $match); |
351 | @exec = ($launcher, $match); |
318 | } else { |
352 | } else { |
319 | # 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...), |
320 | # but alas, m//g behaves differently in list context. |
354 | # but alas, m//g behaves differently in list context. |
321 | @exec = map { s/\$(\d+)|\$\{(\d+)\}/ |
355 | @exec = map { |
|
|
356 | s{\$(\d+)|\$\{(\d+)\}}{ |
322 | 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 | $_ |
323 | /egx; $_ } split /\s+/, $launcher; |
360 | } split /\s+/, $launcher; |
324 | } |
361 | } |
325 | |
362 | |
326 | 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 ]; |
327 | } |
364 | } |
328 | } |
365 | } |
329 | } |
366 | } |
330 | |
367 | |
331 | @matches; |
368 | @matches |
332 | } |
369 | } |
333 | |
370 | |
334 | sub command_for { |
371 | sub command_for { |
335 | my ($self, $row, $col) = @_; |
372 | my ($self, $row, $col) = @_; |
336 | |
373 | |
… | |
… | |
343 | () |
380 | () |
344 | } |
381 | } |
345 | |
382 | |
346 | sub on_button_press { |
383 | sub on_button_press { |
347 | my ($self, $event) = @_; |
384 | my ($self, $event) = @_; |
|
|
385 | |
|
|
386 | if ( |
348 | if($self->valid_button($event) |
387 | $self->valid_button ($event) |
349 | && (my @exec = $self->command_for($event->{row},$event->{col}))) { |
388 | && (my @exec = $self->command_for ($event->{row}, $event->{col})) |
|
|
389 | ) { |
350 | $self->{row} = $event->{row}; |
390 | $self->{row} = $event->{row}; |
351 | $self->{col} = $event->{col}; |
391 | $self->{col} = $event->{col}; |
352 | $self->{cmd} = \@exec; |
392 | $self->{cmd} = \@exec; |
353 | return 1; |
393 | return 1; |
354 | } else { |
394 | } else { |
… | |
… | |
367 | my $col = delete $self->{col}; |
407 | my $col = delete $self->{col}; |
368 | my $cmd = delete $self->{cmd}; |
408 | my $cmd = delete $self->{cmd}; |
369 | |
409 | |
370 | return if !defined $row; |
410 | return if !defined $row; |
371 | |
411 | |
372 | if($row == $event->{row} && abs($col-$event->{col}) < 2 |
412 | if ( |
|
|
413 | $row == $event->{row} |
|
|
414 | && (abs $col-$event->{col}) < 2 |
373 | && join("\x00", @$cmd) eq join("\x00", $self->command_for($row,$col))) { |
415 | && (join "\x00", @$cmd) eq (join "\x00", $self->command_for ($row, $col)) |
|
|
416 | ) { |
374 | if($self->valid_button($event)) { |
417 | if ($self->valid_button ($event)) { |
375 | |
|
|
376 | $self->exec_async (@$cmd); |
418 | $self->exec_async (@$cmd); |
377 | |
|
|
378 | } |
419 | } |
379 | } |
420 | } |
380 | |
421 | |
381 | 1; |
422 | 1; |
382 | } |
423 | } |
… | |
… | |
420 | if (@matches) { |
461 | if (@matches) { |
421 | @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; |
422 | $self->{matches} = \@matches; |
463 | $self->{matches} = \@matches; |
423 | $self->{cur_row} = $row; |
464 | $self->{cur_row} = $row; |
424 | $self->{id} = $dir < 0 ? @{ $self->{matches} } - 1 : 0; |
465 | $self->{id} = $dir < 0 ? @{ $self->{matches} } - 1 : 0; |
425 | $self->view_start (List::Util::min 0, $row - ($self->nrow >> 1)); |
466 | $self->view_start ($row - ($self->nrow >> 1)); |
426 | $self->want_refresh; |
467 | $self->want_refresh; |
427 | return; |
468 | return 1; |
428 | } |
469 | } |
429 | |
470 | |
430 | $row = $dir < 0 ? $line->beg - 1 : $line->end + 1; |
471 | $row = $dir < 0 ? $line->beg - 1 : $line->end + 1; |
431 | } |
472 | } |
432 | |
473 | |
433 | $self->scr_bell; |
474 | $self->scr_bell; |
|
|
475 | |
|
|
476 | () |
434 | } |
477 | } |
435 | |
478 | |
436 | sub select_refresh { |
479 | sub select_refresh { |
437 | my ($self) = @_; |
480 | my ($self) = @_; |
438 | |
481 | |
… | |
… | |
482 | } else { |
525 | } else { |
483 | my $line = $self->line ($self->{cur_row}); |
526 | my $line = $self->line ($self->{cur_row}); |
484 | $self->select_search (+1, $line->end + 1) |
527 | $self->select_search (+1, $line->end + 1) |
485 | if $line->end < $self->nrow; |
528 | if $line->end < $self->nrow; |
486 | } |
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 | } |
487 | } |
539 | } |
488 | |
540 | |
489 | 1 |
541 | 1 |
490 | } |
542 | } |
491 | |
543 | |