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.19 by sf-exg, Sat Jun 7 20:07:38 2014 UTC vs.
Revision 1.25 by sf-exg, Fri Jun 13 21:49:19 2014 UTC

28C<matcher.pattern.0> resource, and additional patterns can be specified 28C<matcher.pattern.0> resource, and additional patterns can be specified
29with numbered patterns, in a manner similar to the "selection" extension. 29with numbered patterns, in a manner similar to the "selection" extension.
30The launcher can also be overridden on a per-pattern basis. 30The launcher can also be overridden on a per-pattern basis.
31 31
32It is possible to activate the most recently seen match or a list of matches 32It is possible to activate the most recently seen match or a list of matches
33from the keyboard. Simply bind a keysym to "perl:matcher:last" or 33from the keyboard. Simply bind a keysym to "matcher:last" or
34"perl:matcher:list" as seen in the example below. 34"matcher:list" as seen in the example below.
35 35
36Example: load and use the matcher extension with defaults. 36Example: load and use the matcher extension with defaults.
37 37
38 URxvt.perl-ext: default,matcher 38 URxvt.perl-ext: default,matcher
39 39
40Example: use a custom configuration. 40Example: use a custom configuration.
41 41
42 URxvt.url-launcher: sensible-browser 42 URxvt.url-launcher: sensible-browser
43 URxvt.keysym.C-Delete: perl:matcher:last 43 URxvt.keysym.C-Delete: matcher:last
44 URxvt.keysym.M-Delete: perl:matcher:list 44 URxvt.keysym.M-Delete: matcher:list
45 URxvt.matcher.button: 1 45 URxvt.matcher.button: 1
46 URxvt.matcher.pattern.1: \\bwww\\.[\\w-]+\\.[\\w./?&@#-]*[\\w/-] 46 URxvt.matcher.pattern.1: \\bwww\\.[\\w-]+\\.[\\w./?&@#-]*[\\w/-]
47 URxvt.matcher.pattern.2: \\B(/\\S+?):(\\d+)(?=:|$) 47 URxvt.matcher.pattern.2: \\B(/\\S+?):(\\d+)(?=:|$)
48 URxvt.matcher.launcher.2: gvim +$2 $1 48 URxvt.matcher.launcher.2: gvim +$2 $1
49 49
92 } 92 }
93 93
94 () 94 ()
95} 95}
96 96
97sub on_action {
98 my ($self, $action) = @_;
99
100 if ($action eq "list") {
101 $self->matchlist;
102 } elsif ($action eq "last") {
103 $self->most_recent;
104 }
105
106 ()
107}
108
97sub matchlist { 109sub matchlist {
98 my ($self) = @_; 110 my ($self) = @_;
99 111
100 @{ $self->{matches} } = (); 112 @{ $self->{matches} } = ();
101 my $row = $self->nrow - 1; 113 my $row = $self->nrow - 1;
102 while ($row >= 0 && @{ $self->{matches} } < 10) { 114 while ($row >= 0 && @{ $self->{matches} } < 10) {
103 my $line = $self->line ($row); 115 my $line = $self->line ($row);
104 my $text = $line->t; 116 my @matches = $self->find_matches ($row);
105
106 # FIXME: code duplicated from 'command_for'
107 my @matches;
108 for my $matcher (@{$self->{matchers}}) {
109 my $launcher = $matcher->[1] || $self->{launcher};
110 while ($text =~ /$matcher->[0]/g) {
111 my $match = substr ($text, $-[0], $+[0] - $-[0]);
112 my @beg = @-;
113 my @end = @+;
114 my @exec;
115
116 if ($launcher !~ /\$/) {
117 @exec = ($launcher, $match);
118 } else {
119 @exec = map { s/\$(\d+)|\$\{(\d+)\}/
120 substr ($text, $beg[$1 || $2], $end[$1 || $2] - $beg[$1 || $2])
121 /egx; $_ } split /\s+/, $launcher;
122 }
123
124 push @matches, [ $beg[0], $match, @exec ];
125 }
126 }
127 117
128 for (sort { $b->[0] <=> $a->[0] } @matches) { 118 for (sort { $b->[0] <=> $a->[0] } @matches) {
129 shift @$_; 119 shift @$_;
130 push @{ $self->{matches} }, $_; 120 push @{ $self->{matches} }, $_;
131 last if @{ $self->{matches} } == 10; 121 last if @{ $self->{matches} } == 10;
265 | urxvt::ShiftMask | urxvt::ControlMask; 255 | urxvt::ShiftMask | urxvt::ControlMask;
266 return ($event->{button} == $self->{button} && 256 return ($event->{button} == $self->{button} &&
267 ($event->{state} & $mask) == $self->{state}); 257 ($event->{state} & $mask) == $self->{state});
268} 258}
269 259
270sub command_for { 260sub find_matches {
271 my ($self, $row, $col) = @_; 261 my ($self, $row, $col) = @_;
272 my $line = $self->line ($row); 262 my $line = $self->line ($row);
273 my $text = $line->t; 263 my $text = $line->t;
264 my $off = $line->offset_of ($row, $col) if $col;
274 265
266 my @matches;
275 for my $matcher (@{$self->{matchers}}) { 267 for my $matcher (@{$self->{matchers}}) {
276 my $launcher = $matcher->[1] || $self->{launcher}; 268 my $launcher = $matcher->[1] || $self->{launcher};
277 while (($text =~ /$matcher->[0]/g)) { 269 while ($text =~ /$matcher->[0]/g) {
278 my $match = $&; 270 my $match = substr $text, $-[0], $+[0] - $-[0];
279 my @begin = @-; 271 my @begin = @-;
280 my @end = @+; 272 my @end = @+;
273 my @exec;
274
281 if (!defined($col) || ($-[0] <= $col && $+[0] >= $col)) { 275 if (!defined($off) || ($-[0] <= $off && $+[0] >= $off)) {
282 if ($launcher !~ /\$/) { 276 if ($launcher !~ /\$/) {
283 return ($launcher,$match); 277 @exec = ($launcher, $match);
284 } else { 278 } else {
285 # It'd be nice to just access a list like ($&,$1,$2...), 279 # It'd be nice to just access a list like ($&,$1,$2...),
286 # but alas, m//g behaves differently in list context. 280 # but alas, m//g behaves differently in list context.
287 my @exec = map { s/\$(\d+)|\$\{(\d+)\}/ 281 @exec = map { s/\$(\d+)|\$\{(\d+)\}/
288 substr($text,$begin[$1||$2],$end[$1||$2]-$begin[$1||$2]) 282 substr $text, $begin[$1 || $2], $end[$1 || $2] - $begin[$1 || $2]
289 /egx; $_ } split(/\s+/, $launcher); 283 /egx; $_ } split /\s+/, $launcher;
290 return @exec;
291 } 284 }
285
286 push @matches, [ $begin[0], $match, @exec ];
292 } 287 }
293 } 288 }
289 }
290
291 @matches;
292}
293
294sub command_for {
295 my ($self, $row, $col) = @_;
296
297 my @matches = $self->find_matches ($row, $col);
298 if (@matches) {
299 my @match = @{ $matches[0] };
300 return @match[2 .. $#match];
294 } 301 }
295 302
296 () 303 ()
297} 304}
298 305

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines