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 |
… | |
… | |
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 | |
… | |
… | |
247 | } |
281 | } |
248 | } |
282 | } |
249 | |
283 | |
250 | my @defaults = ($url); |
284 | my @defaults = ($url); |
251 | my @matchers; |
285 | my @matchers; |
252 | for (my $idx = 0; defined (my $res = $self->my_resource ("pattern.$idx") || $defaults[$idx]); $idx++) { |
286 | for (my $idx = 0; defined (my $res = $self->locale_decode ($self->my_resource ("pattern.$idx")) || $defaults[$idx]); $idx++) { |
253 | $res = $self->locale_decode ($res); |
|
|
254 | utf8::encode $res; |
|
|
255 | my $launcher = $self->my_resource ("launcher.$idx"); |
287 | my $launcher = $self->my_resource ("launcher.$idx"); |
256 | $launcher =~ s/\$&|\$\{&\}/\${0}/g if $launcher; |
288 | $launcher =~ s/\$&|\$\{&\}/\${0}/g if $launcher; |
257 | my $rend = $self->parse_rend($self->my_resource ("rend.$idx")); |
289 | my $rend = $self->parse_rend($self->my_resource ("rend.$idx")); |
258 | unshift @matchers, [qr($res)x,$launcher,$rend]; |
290 | unshift @matchers, [qr($res)x,$launcher,$rend]; |
259 | } |
291 | } |
… | |
… | |
266 | my ($self, $row) = @_; |
298 | my ($self, $row) = @_; |
267 | |
299 | |
268 | # fetch the line that has changed |
300 | # fetch the line that has changed |
269 | my $line = $self->line ($row); |
301 | my $line = $self->line ($row); |
270 | my $text = $line->t; |
302 | my $text = $line->t; |
|
|
303 | my $rend; |
271 | |
304 | |
272 | # find all urls (if any) |
305 | # find all urls (if any) |
273 | for my $matcher (@{$self->{matchers}}) { |
306 | for my $matcher (@{$self->{matchers}}) { |
274 | while ($text =~ /$matcher->[0]/g) { |
307 | while ($text =~ /$matcher->[0]/g) { |
275 | #print "$&\n"; |
308 | #print "$&\n"; |
276 | my $rend = $line->r; |
309 | $rend ||= $line->r; |
277 | |
310 | |
278 | # mark all characters as underlined. we _must_ not toggle underline, |
311 | # mark all characters as underlined. we _must_ not toggle underline, |
279 | # as we might get called on an already-marked url. |
312 | # as we might get called on an already-marked url. |
280 | &{$matcher->[2]} |
313 | &{$matcher->[2]} |
281 | for @{$rend}[$-[0] .. $+[0] - 1]; |
314 | for @{$rend}[$-[0] .. $+[0] - 1]; |
282 | |
|
|
283 | $line->r ($rend); |
|
|
284 | } |
315 | } |
285 | } |
316 | } |
|
|
317 | |
|
|
318 | $line->r ($rend) if $rend; |
286 | |
319 | |
287 | () |
320 | () |
288 | } |
321 | } |
289 | |
322 | |
290 | sub valid_button { |
323 | sub valid_button { |
… | |
… | |
418 | if (@matches) { |
451 | if (@matches) { |
419 | @matches = sort { $a->[0] <=> $b->[0] or $a->[1] <=> $b->[1] } @matches; |
452 | @matches = sort { $a->[0] <=> $b->[0] or $a->[1] <=> $b->[1] } @matches; |
420 | $self->{matches} = \@matches; |
453 | $self->{matches} = \@matches; |
421 | $self->{cur_row} = $row; |
454 | $self->{cur_row} = $row; |
422 | $self->{id} = $dir < 0 ? @{ $self->{matches} } - 1 : 0; |
455 | $self->{id} = $dir < 0 ? @{ $self->{matches} } - 1 : 0; |
423 | $self->view_start (List::Util::min 0, $row - ($self->nrow >> 1)); |
456 | $self->view_start ($row - ($self->nrow >> 1)); |
424 | $self->want_refresh; |
457 | $self->want_refresh; |
425 | return; |
458 | return 1; |
426 | } |
459 | } |
427 | |
460 | |
428 | $row = $dir < 0 ? $line->beg - 1 : $line->end + 1; |
461 | $row = $dir < 0 ? $line->beg - 1 : $line->end + 1; |
429 | } |
462 | } |
430 | |
463 | |
431 | $self->scr_bell; |
464 | $self->scr_bell; |
|
|
465 | |
|
|
466 | () |
432 | } |
467 | } |
433 | |
468 | |
434 | sub select_refresh { |
469 | sub select_refresh { |
435 | my ($self) = @_; |
470 | my ($self) = @_; |
436 | |
471 | |
… | |
… | |
480 | } else { |
515 | } else { |
481 | my $line = $self->line ($self->{cur_row}); |
516 | my $line = $self->line ($self->{cur_row}); |
482 | $self->select_search (+1, $line->end + 1) |
517 | $self->select_search (+1, $line->end + 1) |
483 | if $line->end < $self->nrow; |
518 | if $line->end < $self->nrow; |
484 | } |
519 | } |
|
|
520 | } elsif ($self->lookup_keysym ($keysym, $event->{state}) eq "matcher:select") { |
|
|
521 | if ($self->{id} > 0) { |
|
|
522 | $self->{id}--; |
|
|
523 | $self->want_refresh; |
|
|
524 | } else { |
|
|
525 | my $line = $self->line ($self->{cur_row}); |
|
|
526 | $self->select_search (-1, $self->nrow - 1) |
|
|
527 | unless $self->select_search (-1, $line->beg - 1); |
|
|
528 | } |
485 | } |
529 | } |
486 | |
530 | |
487 | 1 |
531 | 1 |
488 | } |
532 | } |
489 | |
533 | |