1 |
#! perl |
2 |
|
3 |
# Author: Tim Pope <rxvt-unicodeNOSPAM@tpope.org> |
4 |
# Bob Farrell <robertanthonyfarrell@gmail.com> |
5 |
|
6 |
#:META:RESOURCE:%.launcher:string:default launcher command |
7 |
#:META:RESOURCE:%.button:string:the button, yeah |
8 |
#:META:RESOURCE:%.pattern.:string:extra pattern to match |
9 |
#:META:RESOURCE:%.launcher.:string:custom launcher for pattern |
10 |
#:META:RESOURCE:%.rend.:string:custom rendition for pattern |
11 |
|
12 |
=head1 NAME |
13 |
|
14 |
matcher - match strings in terminal output and change their rendition |
15 |
|
16 |
=head1 DESCRIPTION |
17 |
|
18 |
Uses per-line display filtering (C<on_line_update>) to underline text |
19 |
matching a certain pattern and make it clickable. When clicked with the |
20 |
mouse button specified in the C<matcher.button> resource (default 2, or |
21 |
middle), the program specified in the C<matcher.launcher> resource |
22 |
(default, the C<url-launcher> resource, C<sensible-browser>) will be started |
23 |
with the matched text as first argument. The default configuration is |
24 |
suitable for matching URLs and launching a web browser, like the |
25 |
former "mark-urls" extension. |
26 |
|
27 |
The default pattern to match URLs can be overridden with the |
28 |
C<matcher.pattern.0> resource, and additional patterns can be specified |
29 |
with numbered patterns, in a manner similar to the "selection" extension. |
30 |
The launcher can also be overridden on a per-pattern basis. |
31 |
|
32 |
It is possible to activate the most recently seen match or a list of matches |
33 |
from the keyboard. Simply bind a keysym to "matcher:last" or |
34 |
"matcher:list" as seen in the example below. |
35 |
|
36 |
Example: load and use the matcher extension with defaults. |
37 |
|
38 |
URxvt.perl-ext: default,matcher |
39 |
|
40 |
Example: use a custom configuration. |
41 |
|
42 |
URxvt.url-launcher: sensible-browser |
43 |
URxvt.keysym.C-Delete: matcher:last |
44 |
URxvt.keysym.M-Delete: matcher:list |
45 |
URxvt.matcher.button: 1 |
46 |
URxvt.matcher.pattern.1: \\bwww\\.[\\w-]+\\.[\\w./?&@#-]*[\\w/-] |
47 |
URxvt.matcher.pattern.2: \\B(/\\S+?):(\\d+)(?=:|$) |
48 |
URxvt.matcher.launcher.2: gvim +$2 $1 |
49 |
|
50 |
=cut |
51 |
|
52 |
my $url = |
53 |
qr{ |
54 |
(?:https?://|ftp://|news://|mailto:|file://|\bwww\.) |
55 |
[\w\-\@;\/?:&=%\$.+!*\x27,~#]* |
56 |
( |
57 |
\([\w\-\@;\/?:&=%\$.+!*\x27,~#]*\)| # Allow a pair of matched parentheses |
58 |
[\w\-\@;\/?:&=%\$+*~] # exclude some trailing characters (heuristic) |
59 |
)+ |
60 |
}x; |
61 |
|
62 |
sub matchlist_key_press { |
63 |
my ($self, $event, $keysym, $octets) = @_; |
64 |
|
65 |
delete $self->{overlay}; |
66 |
$self->disable ("key_press"); |
67 |
|
68 |
my $i = ($keysym == 96 ? 0 : $keysym - 48); |
69 |
if ($i >= 0 && $i < @{ $self->{matches} }) { |
70 |
my @exec = @{ $self->{matches}[$i] }; |
71 |
$self->exec_async (@exec[5 .. $#exec]); |
72 |
} |
73 |
|
74 |
1 |
75 |
} |
76 |
|
77 |
# backwards compat |
78 |
sub on_user_command { |
79 |
my ($self, $cmd) = @_; |
80 |
|
81 |
if ($cmd =~ s/^matcher:list\b//) { |
82 |
$self->matchlist; |
83 |
} else { |
84 |
if ($cmd =~ s/^matcher:last\b//) { |
85 |
$self->most_recent; |
86 |
} elsif ($cmd =~ s/^matcher\b//) { |
87 |
# for backward compatibility |
88 |
$self->most_recent; |
89 |
} |
90 |
} |
91 |
|
92 |
() |
93 |
} |
94 |
|
95 |
sub 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 |
|
107 |
sub matchlist { |
108 |
my ($self) = @_; |
109 |
|
110 |
$self->{matches} = []; |
111 |
my $row = $self->nrow - 1; |
112 |
while ($row >= 0 && @{ $self->{matches} } < 10) { |
113 |
my $line = $self->line ($row); |
114 |
my @matches = $self->find_matches ($row); |
115 |
|
116 |
for (sort { $b->[0] <=> $a->[0] or $b->[1] <=> $a->[1] } @matches) { |
117 |
push @{ $self->{matches} }, $_; |
118 |
last if @{ $self->{matches} } == 10; |
119 |
} |
120 |
|
121 |
$row = $line->beg - 1; |
122 |
} |
123 |
|
124 |
return unless @{ $self->{matches} }; |
125 |
|
126 |
my $width = 0; |
127 |
|
128 |
my $i = 0; |
129 |
for my $match (@{ $self->{matches} }) { |
130 |
my $text = $match->[4]; |
131 |
my $w = $self->strwidth ("$i-$text"); |
132 |
|
133 |
$width = $w if $w > $width; |
134 |
$i++; |
135 |
} |
136 |
|
137 |
$width = $self->ncol - 2 if $width > $self->ncol - 2; |
138 |
|
139 |
$self->{overlay} = $self->overlay (0, 0, $width, scalar (@{ $self->{matches} }), urxvt::OVERLAY_RSTYLE, 2); |
140 |
my $i = 0; |
141 |
for my $match (@{ $self->{matches} }) { |
142 |
my $text = $match->[4]; |
143 |
|
144 |
$self->{overlay}->set (0, $i, "$i-$text"); |
145 |
$i++; |
146 |
} |
147 |
|
148 |
$self->enable (key_press => \&matchlist_key_press); |
149 |
} |
150 |
|
151 |
sub most_recent { |
152 |
my ($self) = shift; |
153 |
my $row = $self->nrow; |
154 |
my @exec; |
155 |
while($row-- > $self->top_row) { |
156 |
@exec = $self->command_for($row); |
157 |
last if(@exec); |
158 |
} |
159 |
if(@exec) { |
160 |
return $self->exec_async (@exec); |
161 |
} |
162 |
() |
163 |
} |
164 |
|
165 |
sub my_resource { |
166 |
$_[0]->x_resource ("%.$_[1]") |
167 |
} |
168 |
|
169 |
# turn a rendition spec in the resource into a sub that implements it on $_ |
170 |
sub parse_rend { |
171 |
my ($self, $str) = @_; |
172 |
my ($mask, $fg, $bg, $failed) = $str ? urxvt::rend2mask($str) |
173 |
: (urxvt::RS_Uline, undef, undef, []); |
174 |
warn "Failed to parse rendition string: " . join(',', @$failed) if @$failed; |
175 |
my @rend; |
176 |
push @rend, sub { $_ |= $mask } if $mask; |
177 |
push @rend, sub { $_ = urxvt::SET_FGCOLOR($_, $fg) } if defined $fg; |
178 |
push @rend, sub { $_ = urxvt::SET_BGCOLOR($_, $bg) } if defined $bg; |
179 |
sub { |
180 |
for my $s ( @rend ) { &$s }; |
181 |
} |
182 |
} |
183 |
|
184 |
sub on_start { |
185 |
my ($self) = @_; |
186 |
|
187 |
$self->{launcher} = $self->my_resource ("launcher") || $self->x_resource("url-launcher") || "sensible-browser"; |
188 |
|
189 |
$self->{button} = 2; |
190 |
$self->{state} = 0; |
191 |
if($self->{argv}[0] || $self->my_resource ("button")) { |
192 |
my @mods = split '', $self->{argv}[0] || $self->my_resource ("button"); |
193 |
for my $mod (@mods) { |
194 |
if($mod =~ /^\d+$/) { |
195 |
$self->{button} = $mod; |
196 |
} elsif($mod eq "C") { |
197 |
$self->{state} |= urxvt::ControlMask; |
198 |
} elsif($mod eq "S") { |
199 |
$self->{state} |= urxvt::ShiftMask; |
200 |
} elsif($mod eq "M") { |
201 |
$self->{state} |= $self->ModMetaMask; |
202 |
} elsif($mod ne "-" && $mod ne " ") { |
203 |
warn("$mod is invalid in $self->{_name}<$self->{argv}[0]>\n"); |
204 |
} |
205 |
} |
206 |
} |
207 |
|
208 |
my @defaults = ($url); |
209 |
my @matchers; |
210 |
for (my $idx = 0; defined (my $res = $self->my_resource ("pattern.$idx") || $defaults[$idx]); $idx++) { |
211 |
$res = $self->locale_decode ($res); |
212 |
utf8::encode $res; |
213 |
my $launcher = $self->my_resource ("launcher.$idx"); |
214 |
$launcher =~ s/\$&|\$\{&\}/\${0}/g if $launcher; |
215 |
my $rend = $self->parse_rend($self->my_resource ("rend.$idx")); |
216 |
unshift @matchers, [qr($res)x,$launcher,$rend]; |
217 |
} |
218 |
$self->{matchers} = \@matchers; |
219 |
|
220 |
() |
221 |
} |
222 |
|
223 |
sub on_line_update { |
224 |
my ($self, $row) = @_; |
225 |
|
226 |
# fetch the line that has changed |
227 |
my $line = $self->line ($row); |
228 |
my $text = $line->t; |
229 |
|
230 |
# find all urls (if any) |
231 |
for my $matcher (@{$self->{matchers}}) { |
232 |
while ($text =~ /$matcher->[0]/g) { |
233 |
#print "$&\n"; |
234 |
my $rend = $line->r; |
235 |
|
236 |
# mark all characters as underlined. we _must_ not toggle underline, |
237 |
# as we might get called on an already-marked url. |
238 |
&{$matcher->[2]} |
239 |
for @{$rend}[ $-[0] .. $+[0] - 1]; |
240 |
|
241 |
$line->r ($rend); |
242 |
} |
243 |
} |
244 |
|
245 |
() |
246 |
} |
247 |
|
248 |
sub valid_button { |
249 |
my ($self, $event) = @_; |
250 |
my $mask = $self->ModLevel3Mask | $self->ModMetaMask |
251 |
| urxvt::ShiftMask | urxvt::ControlMask; |
252 |
return ($event->{button} == $self->{button} && |
253 |
($event->{state} & $mask) == $self->{state}); |
254 |
} |
255 |
|
256 |
sub find_matches { |
257 |
my ($self, $row, $col) = @_; |
258 |
my $line = $self->line ($row); |
259 |
my $text = $line->t; |
260 |
my $off = $line->offset_of ($row, $col) if defined $col; |
261 |
|
262 |
my @matches; |
263 |
for my $matcher (@{$self->{matchers}}) { |
264 |
my $launcher = $matcher->[1] || $self->{launcher}; |
265 |
while ($text =~ /$matcher->[0]/g) { |
266 |
my $match = substr $text, $-[0], $+[0] - $-[0]; |
267 |
my @begin = @-; |
268 |
my @end = @+; |
269 |
my @exec; |
270 |
|
271 |
if (!defined($off) || ($-[0] <= $off && $+[0] >= $off)) { |
272 |
if ($launcher !~ /\$/) { |
273 |
@exec = ($launcher, $match); |
274 |
} else { |
275 |
# It'd be nice to just access a list like ($&,$1,$2...), |
276 |
# but alas, m//g behaves differently in list context. |
277 |
@exec = map { s/\$(\d+)|\$\{(\d+)\}/ |
278 |
substr $text, $begin[$1 || $2], $end[$1 || $2] - $begin[$1 || $2] |
279 |
/egx; $_ } split /\s+/, $launcher; |
280 |
} |
281 |
|
282 |
push @matches, [ $line->coord_of ($begin[0]), $line->coord_of ($end[0]), $match, @exec ]; |
283 |
} |
284 |
} |
285 |
} |
286 |
|
287 |
@matches; |
288 |
} |
289 |
|
290 |
sub 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]; |
297 |
} |
298 |
|
299 |
() |
300 |
} |
301 |
|
302 |
sub on_button_press { |
303 |
my ($self, $event) = @_; |
304 |
if($self->valid_button($event) |
305 |
&& (my @exec = $self->command_for($event->{row},$event->{col}))) { |
306 |
$self->{row} = $event->{row}; |
307 |
$self->{col} = $event->{col}; |
308 |
$self->{cmd} = \@exec; |
309 |
return 1; |
310 |
} else { |
311 |
delete $self->{row}; |
312 |
delete $self->{col}; |
313 |
delete $self->{cmd}; |
314 |
} |
315 |
|
316 |
() |
317 |
} |
318 |
|
319 |
sub on_button_release { |
320 |
my ($self, $event) = @_; |
321 |
|
322 |
my $row = delete $self->{row}; |
323 |
my $col = delete $self->{col}; |
324 |
my $cmd = delete $self->{cmd}; |
325 |
|
326 |
return if !defined $row; |
327 |
|
328 |
if($row == $event->{row} && abs($col-$event->{col}) < 2 |
329 |
&& join("\x00", @$cmd) eq join("\x00", $self->command_for($row,$col))) { |
330 |
if($self->valid_button($event)) { |
331 |
|
332 |
$self->exec_async (@$cmd); |
333 |
|
334 |
} |
335 |
} |
336 |
|
337 |
1; |
338 |
} |
339 |
|
340 |
# vim:set sw=3 sts=3 et: |