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.18 by sf-exg, Sun Jun 1 11:47:20 2014 UTC vs.
Revision 1.30 by sf-exg, Sat Jun 21 14:11:18 2014 UTC

5 5
6#:META:RESOURCE:%.launcher:string:default launcher command 6#:META:RESOURCE:%.launcher:string:default launcher command
7#:META:RESOURCE:%.button:string:the button, yeah 7#:META:RESOURCE:%.button:string:the button, yeah
8#:META:RESOURCE:%.pattern.:string:extra pattern to match 8#:META:RESOURCE:%.pattern.:string:extra pattern to match
9#:META:RESOURCE:%.launcher.:string:custom launcher for pattern 9#:META:RESOURCE:%.launcher.:string:custom launcher for pattern
10#:META:RESOURCE:%.rend.:string:custom rednition for pattern 10#:META:RESOURCE:%.rend.:string:custom rendition for pattern
11 11
12=head1 NAME 12=head1 NAME
13 13
14matcher - match strings in terminal output and change their rendition 14matcher - match strings in terminal output and change their rendition
15 15
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
57 \([\w\-\@;\/?:&=%\$.+!*\x27,~#]*\)| # Allow a pair of matched parentheses 57 \([\w\-\@;\/?:&=%\$.+!*\x27,~#]*\)| # Allow a pair of matched parentheses
58 [\w\-\@;\/?:&=%\$+*~] # exclude some trailing characters (heuristic) 58 [\w\-\@;\/?:&=%\$+*~] # exclude some trailing characters (heuristic)
59 )+ 59 )+
60 }x; 60 }x;
61 61
62sub on_key_press { 62sub matchlist_key_press {
63 my ($self, $event, $keysym, $octets) = @_; 63 my ($self, $event, $keysym, $octets) = @_;
64 64
65 return unless $self->{overlay};
66
67 delete $self->{overlay}; 65 delete $self->{overlay};
66 $self->disable ("key_press");
68 67
69 my $i = ($keysym == 96 ? 0 : $keysym - 48); 68 my $i = ($keysym == 96 ? 0 : $keysym - 48);
70 if ($i >= 0 && $i < @{ $self->{matches} }) { 69 if ($i >= 0 && $i < @{ $self->{matches} }) {
71 my @exec = @{ $self->{matches}[$i] }; 70 my @exec = @{ $self->{matches}[$i] };
72 shift @exec;
73 $self->exec_async (@exec); 71 $self->exec_async (@exec[5 .. $#exec]);
74 } 72 }
75 73
76 1 74 1
77} 75}
78 76
92 } 90 }
93 91
94 () 92 ()
95} 93}
96 94
95sub on_action {
96 my ($self, $action) = @_;
97
98 if ($action eq "list") {
99 $self->matchlist;
100 } elsif ($action eq "last") {
101 $self->most_recent;
102 }
103
104 ()
105}
106
97sub matchlist { 107sub matchlist {
98 my ($self) = @_; 108 my ($self) = @_;
99 109
100 @{ $self->{matches} } = (); 110 $self->{matches} = [];
101 my $row = $self->nrow - 1; 111 my $row = $self->nrow - 1;
102 while ($row >= 0 && @{ $self->{matches} } < 10) { 112 while ($row >= 0 && @{ $self->{matches} } < 10) {
103 my $line = $self->line ($row); 113 my $line = $self->line ($row);
104 my $text = $line->t; 114 my @matches = $self->find_matches ($row);
105 115
106 # FIXME: code duplicated from 'command_for' 116 for (sort { $b->[0] <=> $a->[0] or $b->[1] <=> $a->[1] } @matches) {
107 for my $matcher (@{$self->{matchers}}) { 117 push @{ $self->{matches} }, $_;
108 my $launcher = $matcher->[1] || $self->{launcher}; 118 last if @{ $self->{matches} } == 10;
109 while ($text =~ /$matcher->[0]/g) {
110 my $match = substr ($text, $-[0], $+[0] - $-[0]);
111 my @beg = @-;
112 my @end = @+;
113 my @exec;
114
115 if ($launcher !~ /\$/) {
116 @exec = ($launcher, $match);
117 } else {
118 @exec = map { s/\$(\d+)|\$\{(\d+)\}/
119 substr ($text, $beg[$1 || $2], $end[$1 || $2] - $beg[$1 || $2])
120 /egx; $_ } split /\s+/, $launcher;
121 }
122
123 push @{ $self->{matches} }, [ $match, @exec ];
124 }
125 } 119 }
126 120
127 $row = $line->beg - 1; 121 $row = $line->beg - 1;
128 } 122 }
129 123
131 125
132 my $width = 0; 126 my $width = 0;
133 127
134 my $i = 0; 128 my $i = 0;
135 for my $match (@{ $self->{matches} }) { 129 for my $match (@{ $self->{matches} }) {
136 my $text = $match->[0]; 130 my $text = $match->[4];
137 my $w = $self->strwidth ("$i-$text"); 131 my $w = $self->strwidth ("$i-$text");
138 132
139 $width = $w if $w > $width; 133 $width = $w if $w > $width;
140 $i++; 134 $i++;
141 } 135 }
143 $width = $self->ncol - 2 if $width > $self->ncol - 2; 137 $width = $self->ncol - 2 if $width > $self->ncol - 2;
144 138
145 $self->{overlay} = $self->overlay (0, 0, $width, scalar (@{ $self->{matches} }), urxvt::OVERLAY_RSTYLE, 2); 139 $self->{overlay} = $self->overlay (0, 0, $width, scalar (@{ $self->{matches} }), urxvt::OVERLAY_RSTYLE, 2);
146 my $i = 0; 140 my $i = 0;
147 for my $match (@{ $self->{matches} }) { 141 for my $match (@{ $self->{matches} }) {
148 my $text = $match->[0]; 142 my $text = $match->[4];
149 143
150 $self->{overlay}->set (0, $i, "$i-$text"); 144 $self->{overlay}->set (0, $i, "$i-$text");
151 $i++; 145 $i++;
152 } 146 }
153 147
148 $self->enable (key_press => \&matchlist_key_press);
154} 149}
155 150
156sub most_recent { 151sub most_recent {
157 my ($self) = shift; 152 my ($self) = shift;
158 my $row = $self->nrow; 153 my $row = $self->nrow;
189sub on_start { 184sub on_start {
190 my ($self) = @_; 185 my ($self) = @_;
191 186
192 $self->{launcher} = $self->my_resource ("launcher") || $self->x_resource("url-launcher") || "sensible-browser"; 187 $self->{launcher} = $self->my_resource ("launcher") || $self->x_resource("url-launcher") || "sensible-browser";
193 188
194 $self->{matches} = [];
195 $self->{button} = 2; 189 $self->{button} = 2;
196 $self->{state} = 0; 190 $self->{state} = 0;
197 if($self->{argv}[0] || $self->my_resource ("button")) { 191 if($self->{argv}[0] || $self->my_resource ("button")) {
198 my @mods = split '', $self->{argv}[0] || $self->my_resource ("button"); 192 my @mods = split '', $self->{argv}[0] || $self->my_resource ("button");
199 for my $mod (@mods) { 193 for my $mod (@mods) {
230 my ($self, $row) = @_; 224 my ($self, $row) = @_;
231 225
232 # fetch the line that has changed 226 # fetch the line that has changed
233 my $line = $self->line ($row); 227 my $line = $self->line ($row);
234 my $text = $line->t; 228 my $text = $line->t;
235 my $i = 0;
236 229
237 # find all urls (if any) 230 # find all urls (if any)
238 for my $matcher (@{$self->{matchers}}) { 231 for my $matcher (@{$self->{matchers}}) {
239 while ($text =~ /$matcher->[0]/g) { 232 while ($text =~ /$matcher->[0]/g) {
240 #print "$&\n"; 233 #print "$&\n";
241 my $rend = $line->r; 234 my $rend = $line->r;
242 235
243 # mark all characters as underlined. we _must_ not toggle underline, 236 # mark all characters as underlined. we _must_ not toggle underline,
244 # as we might get called on an already-marked url. 237 # as we might get called on an already-marked url.
245 &{$matcher->[2]} 238 &{$matcher->[2]}
246 for @{$rend}[ $-[0] .. $+[0] - 1]; 239 for @{$rend}[$-[0] .. $+[0] - 1];
247 240
248 $line->r ($rend); 241 $line->r ($rend);
249 } 242 }
250 } 243 }
251 244
258 | urxvt::ShiftMask | urxvt::ControlMask; 251 | urxvt::ShiftMask | urxvt::ControlMask;
259 return ($event->{button} == $self->{button} && 252 return ($event->{button} == $self->{button} &&
260 ($event->{state} & $mask) == $self->{state}); 253 ($event->{state} & $mask) == $self->{state});
261} 254}
262 255
263sub command_for { 256sub find_matches {
264 my ($self, $row, $col) = @_; 257 my ($self, $row, $col) = @_;
265 my $line = $self->line ($row); 258 my $line = $self->line ($row);
266 my $text = $line->t; 259 my $text = $line->t;
260 my $off = $line->offset_of ($row, $col) if defined $col;
267 261
262 my @matches;
268 for my $matcher (@{$self->{matchers}}) { 263 for my $matcher (@{$self->{matchers}}) {
269 my $launcher = $matcher->[1] || $self->{launcher}; 264 my $launcher = $matcher->[1] || $self->{launcher};
270 while (($text =~ /$matcher->[0]/g)) { 265 while ($text =~ /$matcher->[0]/g) {
271 my $match = $&; 266 my $match = substr $text, $-[0], $+[0] - $-[0];
272 my @begin = @-; 267 my @begin = @-;
273 my @end = @+; 268 my @end = @+;
269 my @exec;
270
274 if (!defined($col) || ($-[0] <= $col && $+[0] >= $col)) { 271 if (!defined($off) || ($-[0] <= $off && $+[0] >= $off)) {
275 if ($launcher !~ /\$/) { 272 if ($launcher !~ /\$/) {
276 return ($launcher,$match); 273 @exec = ($launcher, $match);
277 } else { 274 } else {
278 # It'd be nice to just access a list like ($&,$1,$2...), 275 # It'd be nice to just access a list like ($&,$1,$2...),
279 # but alas, m//g behaves differently in list context. 276 # but alas, m//g behaves differently in list context.
280 my @exec = map { s/\$(\d+)|\$\{(\d+)\}/ 277 @exec = map { s/\$(\d+)|\$\{(\d+)\}/
281 substr($text,$begin[$1||$2],$end[$1||$2]-$begin[$1||$2]) 278 substr $text, $begin[$1 || $2], $end[$1 || $2] - $begin[$1 || $2]
282 /egx; $_ } split(/\s+/, $launcher); 279 /egx; $_ } split /\s+/, $launcher;
283 return @exec;
284 } 280 }
281
282 push @matches, [ $line->coord_of ($begin[0]), $line->coord_of ($end[0]), $match, @exec ];
285 } 283 }
286 } 284 }
285 }
286
287 @matches;
288}
289
290sub command_for {
291 my ($self, $row, $col) = @_;
292
293 my @matches = $self->find_matches ($row, $col);
294 if (@matches) {
295 my @match = @{ $matches[0] };
296 return @match[5 .. $#match];
287 } 297 }
288 298
289 () 299 ()
290} 300}
291 301

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines