… | |
… | |
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 C<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 |
… | |
… | |
86 | URxvt.matcher.button: 1 |
86 | URxvt.matcher.button: 1 |
87 | URxvt.matcher.pattern.1: \\bwww\\.[\\w-]+\\.[\\w./?&@#-]*[\\w/-] |
87 | URxvt.matcher.pattern.1: \\bwww\\.[\\w-]+\\.[\\w./?&@#-]*[\\w/-] |
88 | URxvt.matcher.pattern.2: \\B(/\\S+?):(\\d+)(?=:|$) |
88 | URxvt.matcher.pattern.2: \\B(/\\S+?):(\\d+)(?=:|$) |
89 | URxvt.matcher.launcher.2: gvim +$2 $1 |
89 | URxvt.matcher.launcher.2: gvim +$2 $1 |
90 | |
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 | |
91 | =cut |
121 | =cut |
92 | |
122 | |
93 | my $url = |
123 | my $url = |
94 | qr{ |
124 | qr{ |
95 | (?:https?://|ftp://|news://|mailto:|file://|\bwww\.) |
125 | (?:https?://|ftp://|news://|mailto:|file://|\bwww\.) |
96 | [\w\-\@;\/?:&=%\$.+!*\x27,~#]* |
126 | [\w\-\@;\/?:&=%\$.+!*\x27,~#$urxvt::NOCHAR]* |
97 | ( |
127 | ( |
98 | \([\w\-\@;\/?:&=%\$.+!*\x27,~#]*\)| # Allow a pair of matched parentheses |
128 | \([\w\-\@;\/?:&=%\$.+!*\x27,~#$urxvt::NOCHAR]*\)| # Allow a pair of matched parentheses |
99 | [\w\-\@;\/?:&=%\$+*~] # exclude some trailing characters (heuristic) |
129 | [\w\-\@;\/?:&=%\$+*~] # exclude some trailing characters (heuristic) |
100 | )+ |
130 | )+ |
101 | }x; |
131 | }x; |
102 | |
132 | |
103 | sub matchlist_key_press { |
133 | sub matchlist_key_press { |
… | |
… | |
190 | |
220 | |
191 | $self->enable (key_press => \&matchlist_key_press); |
221 | $self->enable (key_press => \&matchlist_key_press); |
192 | } |
222 | } |
193 | |
223 | |
194 | sub most_recent { |
224 | sub most_recent { |
195 | my ($self) = shift; |
225 | my ($self) = @_; |
196 | my $row = $self->nrow - 1; |
226 | my $row = $self->nrow - 1; |
197 | my @exec; |
227 | |
198 | while ($row >= $self->top_row) { |
228 | while ($row >= $self->top_row) { |
199 | my $line = $self->line ($row); |
229 | my $line = $self->line ($row); |
200 | @exec = $self->command_for($row); |
230 | my @exec = $self->command_for ($row); |
201 | last if(@exec); |
231 | if (@exec) { |
|
|
232 | return $self->exec_async (@exec); |
|
|
233 | } |
202 | |
234 | |
203 | $row = $line->beg - 1; |
235 | $row = $line->beg - 1; |
204 | } |
236 | } |
205 | if(@exec) { |
237 | |
206 | return $self->exec_async (@exec); |
|
|
207 | } |
|
|
208 | () |
238 | () |
209 | } |
239 | } |
210 | |
240 | |
211 | sub my_resource { |
241 | sub my_resource { |
212 | $_[0]->x_resource ("%.$_[1]") |
242 | $_[0]->x_resource ("%.$_[1]") |
… | |
… | |
251 | } |
281 | } |
252 | } |
282 | } |
253 | |
283 | |
254 | my @defaults = ($url); |
284 | my @defaults = ($url); |
255 | my @matchers; |
285 | my @matchers; |
256 | 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++) { |
257 | $res = $self->locale_decode ($res); |
|
|
258 | utf8::encode $res; |
|
|
259 | my $launcher = $self->my_resource ("launcher.$idx"); |
287 | my $launcher = $self->my_resource ("launcher.$idx"); |
260 | $launcher =~ s/\$&|\$\{&\}/\${0}/g if $launcher; |
288 | $launcher =~ s/\$&|\$\{&\}/\${0}/g if $launcher; |
261 | my $rend = $self->parse_rend($self->my_resource ("rend.$idx")); |
289 | my $rend = $self->parse_rend($self->my_resource ("rend.$idx")); |
262 | unshift @matchers, [qr($res)x,$launcher,$rend]; |
290 | unshift @matchers, [qr($res)x,$launcher,$rend]; |
263 | } |
291 | } |
… | |
… | |
313 | my $match = substr $text, $-[0], $+[0] - $-[0]; |
341 | my $match = substr $text, $-[0], $+[0] - $-[0]; |
314 | my @begin = @-; |
342 | my @begin = @-; |
315 | my @end = @+; |
343 | my @end = @+; |
316 | my @exec; |
344 | my @exec; |
317 | |
345 | |
318 | if (!defined($off) || ($-[0] <= $off && $+[0] >= $off)) { |
346 | if (!(defined $off) || ($-[0] <= $off && $+[0] >= $off)) { |
319 | if ($launcher !~ /\$/) { |
347 | if ($launcher !~ /\$/) { |
320 | @exec = ($launcher, $match); |
348 | @exec = ($launcher, $match); |
321 | } else { |
349 | } else { |
322 | # It'd be nice to just access a list like ($&,$1,$2...), |
350 | # It'd be nice to just access a list like ($&,$1,$2...), |
323 | # but alas, m//g behaves differently in list context. |
351 | # but alas, m//g behaves differently in list context. |
324 | @exec = map { s/\$(\d+)|\$\{(\d+)\}/ |
352 | @exec = map { |
|
|
353 | s{\$(\d+)|\$\{(\d+)\}}{ |
325 | substr $text, $begin[$1 || $2], $end[$1 || $2] - $begin[$1 || $2] |
354 | substr $text, $begin[$1 || $2], $end[$1 || $2] - $begin[$1 || $2] |
|
|
355 | }egx; |
|
|
356 | $_ |
326 | /egx; $_ } split /\s+/, $launcher; |
357 | } split /\s+/, $launcher; |
327 | } |
358 | } |
328 | |
359 | |
329 | push @matches, [ $line->coord_of ($begin[0]), $line->coord_of ($end[0]), $match, @exec ]; |
360 | push @matches, [ $line->coord_of ($begin[0]), $line->coord_of ($end[0]), $match, @exec ]; |
330 | } |
361 | } |
331 | } |
362 | } |
332 | } |
363 | } |
333 | |
364 | |
334 | @matches; |
365 | @matches |
335 | } |
366 | } |
336 | |
367 | |
337 | sub command_for { |
368 | sub command_for { |
338 | my ($self, $row, $col) = @_; |
369 | my ($self, $row, $col) = @_; |
339 | |
370 | |
… | |
… | |
346 | () |
377 | () |
347 | } |
378 | } |
348 | |
379 | |
349 | sub on_button_press { |
380 | sub on_button_press { |
350 | my ($self, $event) = @_; |
381 | my ($self, $event) = @_; |
|
|
382 | |
|
|
383 | if ( |
351 | if($self->valid_button($event) |
384 | $self->valid_button ($event) |
352 | && (my @exec = $self->command_for($event->{row},$event->{col}))) { |
385 | && (my @exec = $self->command_for ($event->{row}, $event->{col})) |
|
|
386 | ) { |
353 | $self->{row} = $event->{row}; |
387 | $self->{row} = $event->{row}; |
354 | $self->{col} = $event->{col}; |
388 | $self->{col} = $event->{col}; |
355 | $self->{cmd} = \@exec; |
389 | $self->{cmd} = \@exec; |
356 | return 1; |
390 | return 1; |
357 | } else { |
391 | } else { |
… | |
… | |
370 | my $col = delete $self->{col}; |
404 | my $col = delete $self->{col}; |
371 | my $cmd = delete $self->{cmd}; |
405 | my $cmd = delete $self->{cmd}; |
372 | |
406 | |
373 | return if !defined $row; |
407 | return if !defined $row; |
374 | |
408 | |
375 | if($row == $event->{row} && abs($col-$event->{col}) < 2 |
409 | if ( |
|
|
410 | $row == $event->{row} |
|
|
411 | && (abs $col-$event->{col}) < 2 |
376 | && join("\x00", @$cmd) eq join("\x00", $self->command_for($row,$col))) { |
412 | && (join "\x00", @$cmd) eq (join "\x00", $self->command_for ($row, $col)) |
|
|
413 | ) { |
377 | if($self->valid_button($event)) { |
414 | if ($self->valid_button ($event)) { |
378 | |
|
|
379 | $self->exec_async (@$cmd); |
415 | $self->exec_async (@$cmd); |
380 | |
|
|
381 | } |
416 | } |
382 | } |
417 | } |
383 | |
418 | |
384 | 1; |
419 | 1; |
385 | } |
420 | } |