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 | |
5 | |
6 | #:META:X_RESOURCE:%.launcher:string:default launcher command |
6 | #:META:RESOURCE:%.launcher:string:default launcher command |
7 | #:META:X_RESOURCE:%.button:string:the button, yeah |
7 | #:META:RESOURCE:%.button:string:the button, yeah |
8 | #:META:X_RESOURCE:%.pattern.:string:extra pattern to match |
8 | #:META:RESOURCE:%.pattern.:string:extra pattern to match |
9 | #:META:X_RESOURCE:%.launcher.:string:custom launcher for pattern |
9 | #:META:RESOURCE:%.launcher.:string:custom launcher for pattern |
10 | #:META:X_RESOURCE:%.rend.:string:custom rednition for pattern |
10 | #:META:RESOURCE:%.rend.:string:custom rednition 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 |
11 | |
51 | |
12 | my $url = |
52 | my $url = |
13 | qr{ |
53 | qr{ |
14 | (?:https?://|ftp://|news://|mailto:|file://|\bwww\.) |
54 | (?:https?://|ftp://|news://|mailto:|file://|\bwww\.) |
15 | [a-zA-Z0-9\-\@;\/?:&=%\$_.+!*\x27,~#]* |
55 | [\w\-\@;\/?:&=%\$.+!*\x27,~#]* |
16 | ( |
56 | ( |
17 | \([a-zA-Z0-9\-\@;\/?:&=%\$_.+!*\x27,~#]*\)| # Allow a pair of matched parentheses |
57 | \([\w\-\@;\/?:&=%\$.+!*\x27,~#]*\)| # Allow a pair of matched parentheses |
18 | [a-zA-Z0-9\-\@;\/?:&=%\$_+*~] # exclude some trailing characters (heuristic) |
58 | [\w\-\@;\/?:&=%\$+*~] # exclude some trailing characters (heuristic) |
19 | )+ |
59 | )+ |
20 | }x; |
60 | }x; |
21 | |
61 | |
22 | sub on_key_press { |
62 | sub matchlist_key_press { |
23 | my ($self, $event, $keysym, $octets) = @_; |
63 | my ($self, $event, $keysym, $octets) = @_; |
24 | |
64 | |
25 | if (! $self->{showing} ) { |
65 | delete $self->{overlay}; |
26 | return; |
66 | $self->disable ("key_press"); |
27 | } |
|
|
28 | |
67 | |
29 | my $i = ($keysym == 96 ? 0 : $keysym - 48); |
68 | my $i = ($keysym == 96 ? 0 : $keysym - 48); |
30 | if (($i > scalar(@{$self->{urls}})) || ($i < 0)) { |
69 | if ($i >= 0 && $i < @{ $self->{matches} }) { |
31 | $self->matchlist(); |
70 | my @exec = @{ $self->{matches}[$i] }; |
32 | return; |
71 | $self->exec_async (@exec[5 .. $#exec]); |
|
|
72 | } |
|
|
73 | |
33 | } |
74 | 1 |
34 | |
|
|
35 | my @args = ($self->{urls}[ -$i-1 ]); |
|
|
36 | $self->matchlist(); |
|
|
37 | |
|
|
38 | $self->exec_async( $self->{launcher}, @args ); |
|
|
39 | } |
75 | } |
40 | |
76 | |
|
|
77 | # backwards compat |
41 | sub on_user_command { |
78 | sub on_user_command { |
42 | my ($self, $cmd) = @_; |
79 | my ($self, $cmd) = @_; |
43 | |
80 | |
44 | if($cmd =~ s/^matcher:list\b//) { |
81 | if ($cmd =~ s/^matcher:list\b//) { |
45 | $self->matchlist(); |
82 | $self->matchlist; |
46 | } else { |
83 | } else { |
47 | if($cmd =~ s/^matcher:last\b//) { |
84 | if ($cmd =~ s/^matcher:last\b//) { |
48 | $self->most_recent; |
85 | $self->most_recent; |
49 | } |
|
|
50 | # For backward compatibility |
|
|
51 | else { |
|
|
52 | if($cmd =~ s/^matcher\b//) { |
86 | } elsif ($cmd =~ s/^matcher\b//) { |
|
|
87 | # for backward compatibility |
53 | $self->most_recent; |
88 | $self->most_recent; |
54 | } |
89 | } |
55 | } |
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; |
56 | } |
102 | } |
|
|
103 | |
57 | () |
104 | () |
58 | } |
105 | } |
59 | |
106 | |
60 | sub matchlist { |
107 | sub matchlist { |
61 | my ($self) = @_; |
108 | my ($self) = @_; |
62 | if ( $self->{showing} ) { |
109 | |
63 | $self->{url_overlay}->hide(); |
110 | $self->{matches} = []; |
64 | $self->{showing} = 0; |
111 | my $row = $self->nrow - 1; |
65 | return; |
112 | while ($row >= 0 && @{ $self->{matches} } < 10) { |
66 | } |
|
|
67 | @{$self->{urls}} = (); |
|
|
68 | my $line; |
|
|
69 | for (my $i = 0; $i < $self->nrow; $i ++) { |
|
|
70 | $line = $self->line($i); |
113 | my $line = $self->line ($row); |
71 | next if ($line->beg != $i); |
114 | my @matches = $self->find_matches ($row); |
72 | for my $url ($self->get_urls_from_line($line->t)) { |
115 | |
73 | if (scalar(@{$self->{urls}}) == 10) { |
116 | for (sort { $b->[0] <=> $a->[0] or $b->[1] <=> $a->[1] } @matches) { |
74 | shift @{$self->{urls}}; |
|
|
75 | } |
|
|
76 | push @{$self->{urls}}, $url; |
117 | push @{ $self->{matches} }, $_; |
|
|
118 | last if @{ $self->{matches} } == 10; |
77 | } |
119 | } |
|
|
120 | |
|
|
121 | $row = $line->beg - 1; |
78 | } |
122 | } |
79 | |
123 | |
80 | if (! scalar(@{$self->{urls}})) { |
124 | return unless @{ $self->{matches} }; |
81 | return; |
|
|
82 | } |
|
|
83 | |
125 | |
84 | my $max = 0; |
126 | my $width = 0; |
85 | my $i = scalar( @{$self->{urls}} ) - 1 ;; |
|
|
86 | |
127 | |
87 | my @temp = (); |
|
|
88 | |
|
|
89 | for my $url (@{$self->{urls}}) { |
|
|
90 | my $url = "$i-$url"; |
|
|
91 | my $xpos = 0; |
|
|
92 | |
|
|
93 | if ($self->ncol + (length $url) >= $self->ncol) { |
|
|
94 | $url = substr( $url, 0, $self->ncol ); |
|
|
95 | } |
|
|
96 | |
|
|
97 | push @temp, $url; |
|
|
98 | |
|
|
99 | if( length $url > $max ) { |
|
|
100 | $max = length $url; |
|
|
101 | } |
|
|
102 | |
|
|
103 | $i--; |
|
|
104 | } |
|
|
105 | |
|
|
106 | @temp = reverse @temp; |
|
|
107 | |
|
|
108 | $self->{url_overlay} = $self->overlay(0, 0, $max, scalar( @temp ), urxvt::OVERLAY_RSTYLE, 2); |
|
|
109 | my $i = 0; |
128 | my $i = 0; |
110 | for my $url (@temp) { |
129 | for my $match (@{ $self->{matches} }) { |
111 | $self->{url_overlay}->set( 0, $i, $url, [(urxvt::OVERLAY_RSTYLE) x length $url]); |
130 | my $text = $match->[4]; |
112 | $self->{showing} = 1; |
131 | my $w = $self->strwidth ("$i-$text"); |
|
|
132 | |
|
|
133 | $width = $w if $w > $width; |
113 | $i++; |
134 | $i++; |
114 | } |
135 | } |
115 | |
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); |
116 | } |
149 | } |
117 | |
150 | |
118 | sub most_recent { |
151 | sub most_recent { |
119 | my ($self) = shift; |
152 | my ($self) = shift; |
120 | my $row = $self->nrow; |
153 | my $row = $self->nrow; |
… | |
… | |
128 | } |
161 | } |
129 | () |
162 | () |
130 | } |
163 | } |
131 | |
164 | |
132 | sub my_resource { |
165 | sub my_resource { |
133 | my $self = shift; |
166 | $_[0]->x_resource ("%.$_[1]") |
134 | $self->x_resource ("$self->{name}.$_[0]"); |
|
|
135 | } |
167 | } |
136 | |
168 | |
137 | # turn a rendition spec in the resource into a sub that implements it on $_ |
169 | # turn a rendition spec in the resource into a sub that implements it on $_ |
138 | sub parse_rend { |
170 | sub parse_rend { |
139 | my ($self, $str) = @_; |
171 | my ($self, $str) = @_; |
… | |
… | |
150 | } |
182 | } |
151 | |
183 | |
152 | sub on_start { |
184 | sub on_start { |
153 | my ($self) = @_; |
185 | my ($self) = @_; |
154 | |
186 | |
155 | ($self->{name} = __PACKAGE__) =~ s/.*:://; |
|
|
156 | $self->{name} =~ tr/_/-/; |
|
|
157 | $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"; |
158 | |
188 | |
159 | $self->{urls} = []; |
|
|
160 | $self->{showing} = 0; |
|
|
161 | $self->{button} = 2; |
189 | $self->{button} = 2; |
162 | $self->{state} = 0; |
190 | $self->{state} = 0; |
163 | if($self->{argv}[0] || $self->my_resource ("button")) { |
191 | if($self->{argv}[0] || $self->my_resource ("button")) { |
164 | my @mods = split '', $self->{argv}[0] || $self->my_resource ("button"); |
192 | my @mods = split '', $self->{argv}[0] || $self->my_resource ("button"); |
165 | for my $mod (@mods) { |
193 | for my $mod (@mods) { |
… | |
… | |
170 | } elsif($mod eq "S") { |
198 | } elsif($mod eq "S") { |
171 | $self->{state} |= urxvt::ShiftMask; |
199 | $self->{state} |= urxvt::ShiftMask; |
172 | } elsif($mod eq "M") { |
200 | } elsif($mod eq "M") { |
173 | $self->{state} |= $self->ModMetaMask; |
201 | $self->{state} |= $self->ModMetaMask; |
174 | } elsif($mod ne "-" && $mod ne " ") { |
202 | } elsif($mod ne "-" && $mod ne " ") { |
175 | warn("$mod is invalid in $self->{name}<$self->{argv}[0]>\n"); |
203 | warn("$mod is invalid in $self->{_name}<$self->{argv}[0]>\n"); |
176 | } |
204 | } |
177 | } |
205 | } |
178 | } |
206 | } |
179 | |
207 | |
180 | my @defaults = ($url); |
208 | my @defaults = ($url); |
… | |
… | |
190 | $self->{matchers} = \@matchers; |
218 | $self->{matchers} = \@matchers; |
191 | |
219 | |
192 | () |
220 | () |
193 | } |
221 | } |
194 | |
222 | |
195 | sub get_urls_from_line { |
|
|
196 | my ($self, $line) = @_; |
|
|
197 | my @urls; |
|
|
198 | for my $matcher (@{$self->{matchers}}) { |
|
|
199 | while ($line =~ /$matcher->[0]/g) { |
|
|
200 | push @urls, substr( $line, $-[0], $+[0] - $-[0] ); |
|
|
201 | } |
|
|
202 | } |
|
|
203 | return @urls; |
|
|
204 | } |
|
|
205 | |
|
|
206 | sub on_line_update { |
223 | sub on_line_update { |
207 | my ($self, $row) = @_; |
224 | my ($self, $row) = @_; |
208 | |
225 | |
209 | # fetch the line that has changed |
226 | # fetch the line that has changed |
210 | my $line = $self->line ($row); |
227 | my $line = $self->line ($row); |
… | |
… | |
235 | | urxvt::ShiftMask | urxvt::ControlMask; |
252 | | urxvt::ShiftMask | urxvt::ControlMask; |
236 | return ($event->{button} == $self->{button} && |
253 | return ($event->{button} == $self->{button} && |
237 | ($event->{state} & $mask) == $self->{state}); |
254 | ($event->{state} & $mask) == $self->{state}); |
238 | } |
255 | } |
239 | |
256 | |
240 | sub command_for { |
257 | sub find_matches { |
241 | my ($self, $row, $col) = @_; |
258 | my ($self, $row, $col) = @_; |
242 | my $line = $self->line ($row); |
259 | my $line = $self->line ($row); |
243 | my $text = $line->t; |
260 | my $text = $line->t; |
|
|
261 | my $off = $line->offset_of ($row, $col) if $col; |
244 | |
262 | |
|
|
263 | my @matches; |
245 | for my $matcher (@{$self->{matchers}}) { |
264 | for my $matcher (@{$self->{matchers}}) { |
246 | my $launcher = $matcher->[1] || $self->{launcher}; |
265 | my $launcher = $matcher->[1] || $self->{launcher}; |
247 | while (($text =~ /$matcher->[0]/g)) { |
266 | while ($text =~ /$matcher->[0]/g) { |
248 | my $match = $&; |
267 | my $match = substr $text, $-[0], $+[0] - $-[0]; |
249 | my @begin = @-; |
268 | my @begin = @-; |
250 | my @end = @+; |
269 | my @end = @+; |
|
|
270 | my @exec; |
|
|
271 | |
251 | if (!defined($col) || ($-[0] <= $col && $+[0] >= $col)) { |
272 | if (!defined($off) || ($-[0] <= $off && $+[0] >= $off)) { |
252 | if ($launcher !~ /\$/) { |
273 | if ($launcher !~ /\$/) { |
253 | return ($launcher,$match); |
274 | @exec = ($launcher, $match); |
254 | } else { |
275 | } else { |
255 | # It'd be nice to just access a list like ($&,$1,$2...), |
276 | # It'd be nice to just access a list like ($&,$1,$2...), |
256 | # but alas, m//g behaves differently in list context. |
277 | # but alas, m//g behaves differently in list context. |
257 | my @exec = map { s/\$(\d+)|\$\{(\d+)\}/ |
278 | @exec = map { s/\$(\d+)|\$\{(\d+)\}/ |
258 | substr($text,$begin[$1||$2],$end[$1||$2]-$begin[$1||$2]) |
279 | substr $text, $begin[$1 || $2], $end[$1 || $2] - $begin[$1 || $2] |
259 | /egx; $_ } split(/\s+/, $launcher); |
280 | /egx; $_ } split /\s+/, $launcher; |
260 | return @exec; |
|
|
261 | } |
281 | } |
|
|
282 | |
|
|
283 | push @matches, [ $line->coord_of ($begin[0]), $line->coord_of ($end[0]), $match, @exec ]; |
262 | } |
284 | } |
263 | } |
285 | } |
|
|
286 | } |
|
|
287 | |
|
|
288 | @matches; |
|
|
289 | } |
|
|
290 | |
|
|
291 | sub command_for { |
|
|
292 | my ($self, $row, $col) = @_; |
|
|
293 | |
|
|
294 | my @matches = $self->find_matches ($row, $col); |
|
|
295 | if (@matches) { |
|
|
296 | my @match = @{ $matches[0] }; |
|
|
297 | return @match[5 .. $#match]; |
264 | } |
298 | } |
265 | |
299 | |
266 | () |
300 | () |
267 | } |
301 | } |
268 | |
302 | |
… | |
… | |
294 | |
328 | |
295 | if($row == $event->{row} && abs($col-$event->{col}) < 2 |
329 | if($row == $event->{row} && abs($col-$event->{col}) < 2 |
296 | && join("\x00", @$cmd) eq join("\x00", $self->command_for($row,$col))) { |
330 | && join("\x00", @$cmd) eq join("\x00", $self->command_for($row,$col))) { |
297 | if($self->valid_button($event)) { |
331 | if($self->valid_button($event)) { |
298 | |
332 | |
299 | $self->exec_async (@$cmd); |
333 | $self->exec_async (@$cmd); |
300 | |
334 | |
301 | } |
335 | } |
302 | } |
336 | } |
303 | |
337 | |
304 | 1; |
338 | 1; |