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.31 by sf-exg, Sun Jun 22 07:51:21 2014 UTC vs.
Revision 1.38 by sf-exg, Sun Nov 21 17:08:57 2021 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
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
87=cut 91=cut
88 92
89my $url = 93my $url =
90 qr{ 94 qr{
91 (?:https?://|ftp://|news://|mailto:|file://|\bwww\.) 95 (?:https?://|ftp://|news://|mailto:|file://|\bwww\.)
92 [\w\-\@;\/?:&=%\$.+!*\x27,~#]* 96 [\w\-\@;\/?:&=%\$.+!*\x27,~#$urxvt::NOCHAR]*
93 ( 97 (
94 \([\w\-\@;\/?:&=%\$.+!*\x27,~#]*\)| # Allow a pair of matched parentheses 98 \([\w\-\@;\/?:&=%\$.+!*\x27,~#$urxvt::NOCHAR]*\)| # Allow a pair of matched parentheses
95 [\w\-\@;\/?:&=%\$+*~] # exclude some trailing characters (heuristic) 99 [\w\-\@;\/?:&=%\$+*~] # exclude some trailing characters (heuristic)
96 )+ 100 )+
97 }x; 101 }x;
98 102
99sub matchlist_key_press { 103sub matchlist_key_press {
113 117
114# backwards compat 118# backwards compat
115sub on_user_command { 119sub on_user_command {
116 my ($self, $cmd) = @_; 120 my ($self, $cmd) = @_;
117 121
118 if ($cmd =~ s/^matcher:list\b//) { 122 if ($cmd eq "matcher:list") {
119 $self->matchlist; 123 $self->matchlist;
120 } else { 124 } elsif ($cmd eq "matcher:last") {
121 if ($cmd =~ s/^matcher:last\b//) {
122 $self->most_recent; 125 $self->most_recent;
126 } elsif ($cmd eq "matcher:select") {
127 $self->select_enter;
123 } elsif ($cmd =~ s/^matcher\b//) { 128 } elsif ($cmd eq "matcher") {
124 # for backward compatibility 129 # for backward compatibility
125 $self->most_recent; 130 $self->most_recent;
126 }
127 } 131 }
128 132
129 () 133 ()
130} 134}
131 135
187 $self->enable (key_press => \&matchlist_key_press); 191 $self->enable (key_press => \&matchlist_key_press);
188} 192}
189 193
190sub most_recent { 194sub most_recent {
191 my ($self) = shift; 195 my ($self) = shift;
192 my $row = $self->nrow; 196 my $row = $self->nrow - 1;
193 my @exec; 197 my @exec;
194 while($row-- > $self->top_row) { 198 while ($row >= $self->top_row) {
199 my $line = $self->line ($row);
195 @exec = $self->command_for($row); 200 @exec = $self->command_for($row);
196 last if(@exec); 201 last if(@exec);
202
203 $row = $line->beg - 1;
197 } 204 }
198 if(@exec) { 205 if(@exec) {
199 return $self->exec_async (@exec); 206 return $self->exec_async (@exec);
200 } 207 }
201 () 208 ()
244 } 251 }
245 } 252 }
246 253
247 my @defaults = ($url); 254 my @defaults = ($url);
248 my @matchers; 255 my @matchers;
249 for (my $idx = 0; defined (my $res = $self->my_resource ("pattern.$idx") || $defaults[$idx]); $idx++) { 256 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"); 257 my $launcher = $self->my_resource ("launcher.$idx");
253 $launcher =~ s/\$&|\$\{&\}/\${0}/g if $launcher; 258 $launcher =~ s/\$&|\$\{&\}/\${0}/g if $launcher;
254 my $rend = $self->parse_rend($self->my_resource ("rend.$idx")); 259 my $rend = $self->parse_rend($self->my_resource ("rend.$idx"));
255 unshift @matchers, [qr($res)x,$launcher,$rend]; 260 unshift @matchers, [qr($res)x,$launcher,$rend];
256 } 261 }
263 my ($self, $row) = @_; 268 my ($self, $row) = @_;
264 269
265 # fetch the line that has changed 270 # fetch the line that has changed
266 my $line = $self->line ($row); 271 my $line = $self->line ($row);
267 my $text = $line->t; 272 my $text = $line->t;
273 my $rend;
268 274
269 # find all urls (if any) 275 # find all urls (if any)
270 for my $matcher (@{$self->{matchers}}) { 276 for my $matcher (@{$self->{matchers}}) {
271 while ($text =~ /$matcher->[0]/g) { 277 while ($text =~ /$matcher->[0]/g) {
272 #print "$&\n"; 278 #print "$&\n";
273 my $rend = $line->r; 279 $rend ||= $line->r;
274 280
275 # mark all characters as underlined. we _must_ not toggle underline, 281 # mark all characters as underlined. we _must_ not toggle underline,
276 # as we might get called on an already-marked url. 282 # as we might get called on an already-marked url.
277 &{$matcher->[2]} 283 &{$matcher->[2]}
278 for @{$rend}[$-[0] .. $+[0] - 1]; 284 for @{$rend}[$-[0] .. $+[0] - 1];
279
280 $line->r ($rend);
281 } 285 }
282 } 286 }
287
288 $line->r ($rend) if $rend;
283 289
284 () 290 ()
285} 291}
286 292
287sub valid_button { 293sub valid_button {
415 if (@matches) { 421 if (@matches) {
416 @matches = sort { $a->[0] <=> $b->[0] or $a->[1] <=> $b->[1] } @matches; 422 @matches = sort { $a->[0] <=> $b->[0] or $a->[1] <=> $b->[1] } @matches;
417 $self->{matches} = \@matches; 423 $self->{matches} = \@matches;
418 $self->{cur_row} = $row; 424 $self->{cur_row} = $row;
419 $self->{id} = $dir < 0 ? @{ $self->{matches} } - 1 : 0; 425 $self->{id} = $dir < 0 ? @{ $self->{matches} } - 1 : 0;
420 $self->view_start (List::Util::min 0, $row - ($self->nrow >> 1)); 426 $self->view_start ($row - ($self->nrow >> 1));
421 $self->want_refresh; 427 $self->want_refresh;
422 return; 428 return 1;
423 } 429 }
424 430
425 $row = $dir < 0 ? $line->beg - 1 : $line->end + 1; 431 $row = $dir < 0 ? $line->beg - 1 : $line->end + 1;
426 } 432 }
427 433
428 $self->scr_bell; 434 $self->scr_bell;
435
436 ()
429} 437}
430 438
431sub select_refresh { 439sub select_refresh {
432 my ($self) = @_; 440 my ($self) = @_;
433 441
477 } else { 485 } else {
478 my $line = $self->line ($self->{cur_row}); 486 my $line = $self->line ($self->{cur_row});
479 $self->select_search (+1, $line->end + 1) 487 $self->select_search (+1, $line->end + 1)
480 if $line->end < $self->nrow; 488 if $line->end < $self->nrow;
481 } 489 }
490 } elsif ($self->lookup_keysym ($keysym, $event->{state}) eq "matcher:select") {
491 if ($self->{id} > 0) {
492 $self->{id}--;
493 $self->want_refresh;
494 } else {
495 my $line = $self->line ($self->{cur_row});
496 $self->select_search (-1, $self->nrow - 1)
497 unless $self->select_search (-1, $line->beg - 1);
498 }
482 } 499 }
483 500
484 1 501 1
485} 502}
486 503

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines