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.32 by sf-exg, Wed Jul 30 15:22:51 2014 UTC vs.
Revision 1.41 by sf-exg, Sun May 28 10:40:41 2023 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# 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
18Uses per-line display filtering (C<on_line_update>) to underline text 19Uses per-line display filtering (C<on_line_update>) to underline text
19matching a certain pattern and make it clickable. When clicked with the 20matching a certain pattern and make it clickable. When clicked with the
20mouse button specified in the C<matcher.button> resource (default 2, or 21mouse button specified in the C<matcher.button> resource (default 2, or
21middle), the program specified in the C<matcher.launcher> resource 22middle), 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
23with the matched text as first argument. The default configuration is 24with the matched text as first argument. The default configuration is
24suitable for matching URLs and launching a web browser, like the 25suitable for matching URLs and launching a web browser, like the
25former "mark-urls" extension. 26former "mark-urls" extension.
26 27
27The default pattern to match URLs can be overridden with the 28The default pattern to match URLs can be overridden with the
28C<matcher.pattern.0> resource, and additional patterns can be specified 29C<matcher.pattern.0> resource, and additional patterns can be specified
29with numbered patterns, in a manner similar to the "selection" extension. 30with numbered patterns, in a manner similar to the "selection" extension.
30The launcher can also be overridden on a per-pattern basis. 31The launcher can also be overridden on a per-pattern basis.
31 32
32It is possible to activate the most recently seen match or a list of matches 33It 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 34from 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
36The 'matcher:select' action enables a mode in which it is possible to 37The C<matcher:select> action enables a mode in which it is possible to
37iterate over the matches using the keyboard and either activate them 38iterate over the matches using the keyboard and either activate them
38or copy them to the clipboard. While the mode is active, normal terminal 39or copy them to the clipboard. While the mode is active, normal terminal
39input/output is suspended and the following bindings are recognized: 40input/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
45Search for a match upwards. 46Search for a match upwards.
46 47
67=item C<y> 68=item C<y>
68 69
69Copy the current match to the clipboard. 70Copy the current match to the clipboard.
70 71
71=back 72=back
73
74It is also possible to cycle through the matches using a key
75combination bound to the C<matcher:select> action.
72 76
73Example: load and use the matcher extension with defaults. 77Example: 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
93Urxvt stores all text as unicode, in a special encoding that uses
94one character/code point per column. For various reasons, the regular
95expressions are matched directly against this encoding, which means there are a few things
96you need to keep in mind:
97
98=over
99
100=item X resources/command line arguments are locale-encoded
101
102The regexes taken from the command line or resources will be converted
103from locale encoding to unicode. This can change the number of code points
104per character.
105
106=item Wide characters are column-padded with C<$urxvt::NOCHAR>
107
108Wide characters (such as kanji and sometimes tabs) are padded with
109a special character value (C<$urxvt::NOCHAR>). That means that
110constructs such as C<\w> or C<.> will only match part of a character, as
111C<$urxvt::NOCHAR> is not matched by C<\w> and both only match the first
112"column" of a wide character.
113
114That means you have to incorporate C<$urxvt::NOCHAR> into parts of regexes
115that may match wide characters. For example, to match C<\w+> you might
116want 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
89my $url = 123my $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
99sub matchlist_key_press { 133sub matchlist_key_press {
113 147
114# backwards compat 148# backwards compat
115sub on_user_command { 149sub 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
186 220
187 $self->enable (key_press => \&matchlist_key_press); 221 $self->enable (key_press => \&matchlist_key_press);
188} 222}
189 223
190sub most_recent { 224sub most_recent {
191 my ($self) = shift; 225 my ($self) = @_;
192 my $row = $self->nrow - 1; 226 my $row = $self->nrow - 1;
193 my @exec; 227
194 while ($row >= $self->top_row) { 228 while ($row >= $self->top_row) {
195 my $line = $self->line ($row); 229 my $line = $self->line ($row);
196 @exec = $self->command_for($row); 230 my @exec = $self->command_for ($row);
197 last if(@exec); 231 if (@exec) {
232 return $self->exec_async (@exec);
233 }
198 234
199 $row = $line->beg - 1; 235 $row = $line->beg - 1;
200 } 236 }
201 if(@exec) { 237
202 return $self->exec_async (@exec);
203 }
204 () 238 ()
205} 239}
206 240
207sub my_resource { 241sub my_resource {
208 $_[0]->x_resource ("%.$_[1]") 242 $_[0]->x_resource ("%.$_[1]")
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
290sub valid_button { 323sub valid_button {
308 my $match = substr $text, $-[0], $+[0] - $-[0]; 341 my $match = substr $text, $-[0], $+[0] - $-[0];
309 my @begin = @-; 342 my @begin = @-;
310 my @end = @+; 343 my @end = @+;
311 my @exec; 344 my @exec;
312 345
313 if (!defined($off) || ($-[0] <= $off && $+[0] >= $off)) { 346 if (!(defined $off) || ($-[0] <= $off && $+[0] >= $off)) {
314 if ($launcher !~ /\$/) { 347 if ($launcher !~ /\$/) {
315 @exec = ($launcher, $match); 348 @exec = ($launcher, $match);
316 } else { 349 } else {
317 # 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...),
318 # but alas, m//g behaves differently in list context. 351 # but alas, m//g behaves differently in list context.
319 @exec = map { s/\$(\d+)|\$\{(\d+)\}/ 352 @exec = map {
353 s{\$(\d+)|\$\{(\d+)\}}{
320 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 $_
321 /egx; $_ } split /\s+/, $launcher; 357 } split /\s+/, $launcher;
322 } 358 }
323 359
324 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 ];
325 } 361 }
326 } 362 }
327 } 363 }
328 364
329 @matches; 365 @matches
330} 366}
331 367
332sub command_for { 368sub command_for {
333 my ($self, $row, $col) = @_; 369 my ($self, $row, $col) = @_;
334 370
341 () 377 ()
342} 378}
343 379
344sub on_button_press { 380sub on_button_press {
345 my ($self, $event) = @_; 381 my ($self, $event) = @_;
382
383 if (
346 if($self->valid_button($event) 384 $self->valid_button ($event)
347 && (my @exec = $self->command_for($event->{row},$event->{col}))) { 385 && (my @exec = $self->command_for ($event->{row}, $event->{col}))
386 ) {
348 $self->{row} = $event->{row}; 387 $self->{row} = $event->{row};
349 $self->{col} = $event->{col}; 388 $self->{col} = $event->{col};
350 $self->{cmd} = \@exec; 389 $self->{cmd} = \@exec;
351 return 1; 390 return 1;
352 } else { 391 } else {
365 my $col = delete $self->{col}; 404 my $col = delete $self->{col};
366 my $cmd = delete $self->{cmd}; 405 my $cmd = delete $self->{cmd};
367 406
368 return if !defined $row; 407 return if !defined $row;
369 408
370 if($row == $event->{row} && abs($col-$event->{col}) < 2 409 if (
410 $row == $event->{row}
411 && (abs $col-$event->{col}) < 2
371 && join("\x00", @$cmd) eq join("\x00", $self->command_for($row,$col))) { 412 && (join "\x00", @$cmd) eq (join "\x00", $self->command_for ($row, $col))
413 ) {
372 if($self->valid_button($event)) { 414 if ($self->valid_button ($event)) {
373
374 $self->exec_async (@$cmd); 415 $self->exec_async (@$cmd);
375
376 } 416 }
377 } 417 }
378 418
379 1; 419 1;
380} 420}
418 if (@matches) { 458 if (@matches) {
419 @matches = sort { $a->[0] <=> $b->[0] or $a->[1] <=> $b->[1] } @matches; 459 @matches = sort { $a->[0] <=> $b->[0] or $a->[1] <=> $b->[1] } @matches;
420 $self->{matches} = \@matches; 460 $self->{matches} = \@matches;
421 $self->{cur_row} = $row; 461 $self->{cur_row} = $row;
422 $self->{id} = $dir < 0 ? @{ $self->{matches} } - 1 : 0; 462 $self->{id} = $dir < 0 ? @{ $self->{matches} } - 1 : 0;
423 $self->view_start (List::Util::min 0, $row - ($self->nrow >> 1)); 463 $self->view_start ($row - ($self->nrow >> 1));
424 $self->want_refresh; 464 $self->want_refresh;
425 return; 465 return 1;
426 } 466 }
427 467
428 $row = $dir < 0 ? $line->beg - 1 : $line->end + 1; 468 $row = $dir < 0 ? $line->beg - 1 : $line->end + 1;
429 } 469 }
430 470
431 $self->scr_bell; 471 $self->scr_bell;
472
473 ()
432} 474}
433 475
434sub select_refresh { 476sub select_refresh {
435 my ($self) = @_; 477 my ($self) = @_;
436 478
480 } else { 522 } else {
481 my $line = $self->line ($self->{cur_row}); 523 my $line = $self->line ($self->{cur_row});
482 $self->select_search (+1, $line->end + 1) 524 $self->select_search (+1, $line->end + 1)
483 if $line->end < $self->nrow; 525 if $line->end < $self->nrow;
484 } 526 }
527 } elsif ($self->lookup_keysym ($keysym, $event->{state}) eq "matcher:select") {
528 if ($self->{id} > 0) {
529 $self->{id}--;
530 $self->want_refresh;
531 } else {
532 my $line = $self->line ($self->{cur_row});
533 $self->select_search (-1, $self->nrow - 1)
534 unless $self->select_search (-1, $line->beg - 1);
535 }
485 } 536 }
486 537
487 1 538 1
488} 539}
489 540

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines