1 | #! perl |
1 | #! perl |
2 | |
2 | |
3 | # Author: Tim Pope <rxvt-unicodeNOSPAM@tpope.info> |
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 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 "perl:matcher:last" or |
|
|
34 | "perl: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: perl:matcher:last |
|
|
44 | URxvt.keysym.M-Delete: perl: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 |
4 | |
51 | |
5 | my $url = |
52 | my $url = |
6 | qr{ |
53 | qr{ |
7 | (?:https?://|ftp://|news://|mailto:|file://|\bwww\.) |
54 | (?:https?://|ftp://|news://|mailto:|file://|\bwww\.) |
8 | [a-zA-Z0-9\-\@;\/?:&=%\$_.+!*\x27,~#]* |
55 | [\w\-\@;\/?:&=%\$.+!*\x27,~#]* |
9 | ( |
56 | ( |
10 | \([a-zA-Z0-9\-\@;\/?:&=%\$_.+!*\x27,~#]*\)| # Allow a pair of matched parentheses |
57 | \([\w\-\@;\/?:&=%\$.+!*\x27,~#]*\)| # Allow a pair of matched parentheses |
11 | [a-zA-Z0-9\-\@;\/?:&=%\$_+*~] # exclude some trailing characters (heuristic) |
58 | [\w\-\@;\/?:&=%\$+*~] # exclude some trailing characters (heuristic) |
12 | )+ |
59 | )+ |
13 | }x; |
60 | }x; |
14 | |
61 | |
|
|
62 | sub on_key_press { |
|
|
63 | my ($self, $event, $keysym, $octets) = @_; |
|
|
64 | |
|
|
65 | if (! $self->{showing} ) { |
|
|
66 | return; |
|
|
67 | } |
|
|
68 | |
|
|
69 | my $i = ($keysym == 96 ? 0 : $keysym - 48); |
|
|
70 | if (($i > scalar(@{$self->{urls}})) || ($i < 0)) { |
|
|
71 | $self->matchlist(); |
|
|
72 | return; |
|
|
73 | } |
|
|
74 | |
|
|
75 | my @args = ($self->{urls}[ -$i-1 ]); |
|
|
76 | $self->matchlist(); |
|
|
77 | |
|
|
78 | $self->exec_async( $self->{launcher}, @args ); |
|
|
79 | } |
|
|
80 | |
|
|
81 | # backwards compat |
15 | sub on_user_command { |
82 | sub on_user_command { |
16 | my ($self, $cmd) = @_; |
83 | my ($self, $cmd) = @_; |
|
|
84 | |
17 | if($cmd =~ s/^matcher\b//) { |
85 | if ($cmd =~ s/^matcher:list\b//) { |
|
|
86 | $self->matchlist; |
|
|
87 | } else { |
|
|
88 | if ($cmd =~ s/^matcher:last\b//) { |
18 | $self->most_recent; |
89 | $self->most_recent; |
|
|
90 | } elsif ($cmd =~ s/^matcher\b//) { |
|
|
91 | # for backward compatibility |
|
|
92 | $self->most_recent; |
|
|
93 | } |
|
|
94 | } |
|
|
95 | |
|
|
96 | () |
|
|
97 | } |
|
|
98 | |
|
|
99 | sub matchlist { |
|
|
100 | my ($self) = @_; |
|
|
101 | if ( $self->{showing} ) { |
|
|
102 | $self->{url_overlay}->hide; |
|
|
103 | $self->{showing} = 0; |
|
|
104 | return; |
|
|
105 | } |
|
|
106 | @{$self->{urls}} = (); |
|
|
107 | my $line; |
|
|
108 | for (my $i = 0; $i < $self->nrow; $i ++) { |
|
|
109 | $line = $self->line($i); |
|
|
110 | next if ($line->beg != $i); |
|
|
111 | for my $url ($self->get_urls_from_line($line->t)) { |
|
|
112 | if (scalar(@{$self->{urls}}) == 10) { |
|
|
113 | shift @{$self->{urls}}; |
|
|
114 | } |
|
|
115 | push @{$self->{urls}}, $url; |
|
|
116 | } |
19 | } |
117 | } |
|
|
118 | |
|
|
119 | if (! scalar(@{$self->{urls}})) { |
|
|
120 | return; |
|
|
121 | } |
|
|
122 | |
|
|
123 | my $max = 0; |
|
|
124 | my $i = scalar( @{$self->{urls}} ) - 1 ;; |
|
|
125 | |
|
|
126 | my @temp = (); |
|
|
127 | |
|
|
128 | for my $url (@{$self->{urls}}) { |
|
|
129 | my $url = "$i-$url"; |
|
|
130 | my $xpos = 0; |
|
|
131 | |
|
|
132 | if ($self->ncol + (length $url) >= $self->ncol) { |
|
|
133 | $url = substr( $url, 0, $self->ncol ); |
|
|
134 | } |
|
|
135 | |
|
|
136 | push @temp, $url; |
|
|
137 | |
|
|
138 | if( length $url > $max ) { |
|
|
139 | $max = length $url; |
|
|
140 | } |
|
|
141 | |
|
|
142 | $i--; |
|
|
143 | } |
|
|
144 | |
|
|
145 | @temp = reverse @temp; |
|
|
146 | |
|
|
147 | $self->{url_overlay} = $self->overlay(0, 0, $max, scalar( @temp ), urxvt::OVERLAY_RSTYLE, 2); |
|
|
148 | my $i = 0; |
|
|
149 | for my $url (@temp) { |
|
|
150 | $self->{url_overlay}->set( 0, $i, $url, [(urxvt::OVERLAY_RSTYLE) x length $url]); |
|
|
151 | $self->{showing} = 1; |
|
|
152 | $i++; |
|
|
153 | } |
|
|
154 | |
|
|
155 | } |
|
|
156 | |
|
|
157 | sub most_recent { |
|
|
158 | my ($self) = shift; |
20 | my $row = $self->nrow; |
159 | my $row = $self->nrow; |
21 | my @exec; |
160 | my @exec; |
22 | while($row-- > $self->top_row) { |
161 | while($row-- > $self->top_row) { |
23 | #my $line = $self->line ($row); |
|
|
24 | #my $text = $line->t; |
|
|
25 | @exec = $self->command_for($row); |
162 | @exec = $self->command_for($row); |
26 | last if(@exec); |
163 | last if(@exec); |
27 | } |
164 | } |
28 | if(@exec) { |
165 | if(@exec) { |
29 | return $self->exec_async (@exec); |
166 | return $self->exec_async (@exec); |
30 | } |
167 | } |
31 | () |
168 | () |
32 | } |
169 | } |
33 | |
170 | |
34 | sub most_recent { |
|
|
35 | my ($self) = shift; |
|
|
36 | () |
|
|
37 | } |
|
|
38 | |
|
|
39 | sub my_resource { |
171 | sub my_resource { |
40 | my $self = shift; |
172 | $_[0]->x_resource ("%.$_[1]") |
41 | $self->x_resource("$self->{name}.$_[0]"); |
|
|
42 | } |
173 | } |
43 | |
174 | |
44 | # turn a rendition spec in the resource into a sub that implements it on $_ |
175 | # turn a rendition spec in the resource into a sub that implements it on $_ |
45 | sub parse_rend { |
176 | sub parse_rend { |
46 | my ($self, $str) = @_; |
177 | my ($self, $str) = @_; |
47 | my ($mask, $fg, $bg, $failed) = $str ? urxvt::rend2mask($str) |
178 | my ($mask, $fg, $bg, $failed) = $str ? urxvt::rend2mask($str) |
48 | : (urxvt::RS_Uline, undef, undef, []); |
179 | : (urxvt::RS_Uline, undef, undef, []); |
49 | warn "Failed to parse rendition string: " . join(',', @$failed) if @$failed; |
180 | warn "Failed to parse rendition string: " . join(',', @$failed) if @$failed; |
50 | my @rend; |
181 | my @rend; |
51 | push @rend, sub { $_ |= $mask } if $mask; |
182 | push @rend, sub { $_ |= $mask } if $mask; |
52 | push @rend, sub { $_ = urxvt::SET_FGCOLOR($_, $fg) } if defined $fg; |
183 | push @rend, sub { $_ = urxvt::SET_FGCOLOR($_, $fg) } if defined $fg; |
… | |
… | |
57 | } |
188 | } |
58 | |
189 | |
59 | sub on_start { |
190 | sub on_start { |
60 | my ($self) = @_; |
191 | my ($self) = @_; |
61 | |
192 | |
62 | ($self->{name} = __PACKAGE__) =~ s/.*:://; |
193 | $self->{launcher} = $self->my_resource ("launcher") || $self->x_resource("url-launcher") || "sensible-browser"; |
63 | $self->{name} =~ tr/_/-/; |
|
|
64 | $self->{launcher} = $self->my_resource("launcher") || |
|
|
65 | $self->x_resource("urlLauncher") || |
|
|
66 | "sensible-browser"; |
|
|
67 | |
194 | |
|
|
195 | $self->{urls} = []; |
|
|
196 | $self->{showing} = 0; |
68 | $self->{button} = 2; |
197 | $self->{button} = 2; |
69 | $self->{state} = 0; |
198 | $self->{state} = 0; |
70 | if($self->{argv}[0] || $self->my_resource("button")) { |
199 | if($self->{argv}[0] || $self->my_resource ("button")) { |
71 | my @mods = split('', $self->{argv}[0] || $self->my_resource("button")); |
200 | my @mods = split '', $self->{argv}[0] || $self->my_resource ("button"); |
72 | for my $mod (@mods) { |
201 | for my $mod (@mods) { |
73 | if($mod =~ /^\d+$/) { |
202 | if($mod =~ /^\d+$/) { |
74 | $self->{button} = $mod; |
203 | $self->{button} = $mod; |
75 | } elsif($mod eq "C") { |
204 | } elsif($mod eq "C") { |
76 | $self->{state} |= urxvt::ControlMask; |
205 | $self->{state} |= urxvt::ControlMask; |
77 | } elsif($mod eq "S") { |
206 | } elsif($mod eq "S") { |
78 | $self->{state} |= urxvt::ShiftMask; |
207 | $self->{state} |= urxvt::ShiftMask; |
79 | } elsif($mod eq "M") { |
208 | } elsif($mod eq "M") { |
80 | $self->{state} |= $self->ModMetaMask; |
209 | $self->{state} |= $self->ModMetaMask; |
81 | } elsif($mod ne "-" && $mod ne " ") { |
210 | } elsif($mod ne "-" && $mod ne " ") { |
82 | warn("$mod is invalid in $self->{name}<$self->{argv}[0]>\n"); |
211 | warn("$mod is invalid in $self->{_name}<$self->{argv}[0]>\n"); |
83 | } |
212 | } |
84 | } |
213 | } |
85 | } |
214 | } |
86 | |
215 | |
87 | my @defaults = ($url); |
216 | my @defaults = ($url); |
88 | my @matchers; |
217 | my @matchers; |
89 | for (my $idx = 0; defined (my $res = $self->my_resource("pattern.$idx") || $defaults[$idx]); $idx++) { |
218 | for (my $idx = 0; defined (my $res = $self->my_resource ("pattern.$idx") || $defaults[$idx]); $idx++) { |
90 | $res = $self->locale_decode ($res); |
219 | $res = $self->locale_decode ($res); |
91 | utf8::encode $res; |
220 | utf8::encode $res; |
92 | my $launcher = $self->my_resource("launcher.$idx"); |
221 | my $launcher = $self->my_resource ("launcher.$idx"); |
93 | $launcher =~ s/\$&|\$\{&\}/\${0}/g if ($launcher); |
222 | $launcher =~ s/\$&|\$\{&\}/\${0}/g if $launcher; |
94 | my $rend = $self->parse_rend($self->my_resource("rend.$idx")); |
223 | my $rend = $self->parse_rend($self->my_resource ("rend.$idx")); |
95 | unshift @matchers, [qr($res)x,$launcher,$rend]; |
224 | unshift @matchers, [qr($res)x,$launcher,$rend]; |
96 | } |
225 | } |
97 | $self->{matchers} = \@matchers; |
226 | $self->{matchers} = \@matchers; |
98 | |
227 | |
99 | () |
228 | () |
|
|
229 | } |
|
|
230 | |
|
|
231 | sub get_urls_from_line { |
|
|
232 | my ($self, $line) = @_; |
|
|
233 | my @urls; |
|
|
234 | for my $matcher (@{$self->{matchers}}) { |
|
|
235 | while ($line =~ /$matcher->[0]/g) { |
|
|
236 | push @urls, substr( $line, $-[0], $+[0] - $-[0] ); |
|
|
237 | } |
|
|
238 | } |
|
|
239 | return @urls; |
100 | } |
240 | } |
101 | |
241 | |
102 | sub on_line_update { |
242 | sub on_line_update { |
103 | my ($self, $row) = @_; |
243 | my ($self, $row) = @_; |
104 | |
244 | |
… | |
… | |
162 | () |
302 | () |
163 | } |
303 | } |
164 | |
304 | |
165 | sub on_button_press { |
305 | sub on_button_press { |
166 | my ($self, $event) = @_; |
306 | my ($self, $event) = @_; |
167 | if($self->valid_button($event)) { |
307 | if($self->valid_button($event) |
|
|
308 | && (my @exec = $self->command_for($event->{row},$event->{col}))) { |
168 | $self->{row} = $event->{row}; |
309 | $self->{row} = $event->{row}; |
169 | $self->{col} = $event->{col}; |
310 | $self->{col} = $event->{col}; |
|
|
311 | $self->{cmd} = \@exec; |
|
|
312 | return 1; |
170 | } else { |
313 | } else { |
171 | delete $self->{row}; |
314 | delete $self->{row}; |
172 | delete $self->{col}; |
315 | delete $self->{col}; |
|
|
316 | delete $self->{cmd}; |
173 | } |
317 | } |
174 | |
318 | |
175 | () |
319 | () |
176 | } |
320 | } |
177 | |
321 | |
178 | sub on_button_release { |
322 | sub on_button_release { |
179 | my ($self, $event) = @_; |
323 | my ($self, $event) = @_; |
180 | |
324 | |
181 | my $row = delete $self->{row}; |
325 | my $row = delete $self->{row}; |
182 | my $col = delete $self->{col}; |
326 | my $col = delete $self->{col}; |
|
|
327 | my $cmd = delete $self->{cmd}; |
183 | |
328 | |
|
|
329 | return if !defined $row; |
|
|
330 | |
184 | if(defined($row) && $row == $event->{row} && abs($col-$event->{col}) < 2) { |
331 | if($row == $event->{row} && abs($col-$event->{col}) < 2 |
|
|
332 | && join("\x00", @$cmd) eq join("\x00", $self->command_for($row,$col))) { |
185 | if($self->valid_button($event)) { |
333 | if($self->valid_button($event)) { |
186 | |
334 | |
187 | my @exec = $self->command_for($row,$col); |
335 | $self->exec_async (@$cmd); |
188 | if(@exec) { |
336 | |
189 | return $self->exec_async (@exec); |
|
|
190 | } |
337 | } |
191 | |
|
|
192 | } |
|
|
193 | } |
338 | } |
194 | |
339 | |
195 | () |
340 | 1; |
196 | } |
341 | } |
197 | |
342 | |
198 | # vim:set sw=3 sts=3 et: |
343 | # vim:set sw=3 sts=3 et: |