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.5 by sf-tpope, Fri Aug 19 23:02:46 2011 UTC vs.
Revision 1.17 by sf-exg, Sat May 31 19:47:42 2014 UTC

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
14matcher - match strings in terminal output and change their rendition
15
16=head1 DESCRIPTION
17
18Uses per-line display filtering (C<on_line_update>) to underline text
19matching a certain pattern and make it clickable. When clicked with the
20mouse button specified in the C<matcher.button> resource (default 2, or
21middle), the program specified in the C<matcher.launcher> resource
22(default, the C<url-launcher> resource, C<sensible-browser>) will be started
23with the matched text as first argument. The default configuration is
24suitable for matching URLs and launching a web browser, like the
25former "mark-urls" extension.
26
27The default pattern to match URLs can be overridden with the
28C<matcher.pattern.0> resource, and additional patterns can be specified
29with numbered patterns, in a manner similar to the "selection" extension.
30The launcher can also be overridden on a per-pattern basis.
31
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
34"perl:matcher:list" as seen in the example below.
35
36Example: load and use the matcher extension with defaults.
37
38 URxvt.perl-ext: default,matcher
39
40Example: 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
5my $url = 52my $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
62sub 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
15sub on_user_command { 82sub 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 }
19 } 94 }
95
20 () 96 ()
97}
98
99sub 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 }
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
21} 155}
22 156
23sub most_recent { 157sub most_recent {
24 my ($self) = shift; 158 my ($self) = shift;
25 my $row = $self->nrow; 159 my $row = $self->nrow;
26 my @exec; 160 my @exec;
27 while($row-- > $self->top_row) { 161 while($row-- > $self->top_row) {
28 #my $line = $self->line ($row);
29 #my $text = $line->t;
30 @exec = $self->command_for($row); 162 @exec = $self->command_for($row);
31 last if(@exec); 163 last if(@exec);
32 } 164 }
33 if(@exec) { 165 if(@exec) {
34 return $self->exec_async (@exec); 166 return $self->exec_async (@exec);
35 } 167 }
36 () 168 ()
37} 169}
38 170
39sub my_resource { 171sub 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 $_
45sub parse_rend { 176sub 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
59sub on_start { 190sub 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
231sub 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
102sub on_line_update { 242sub on_line_update {
103 my ($self, $row) = @_; 243 my ($self, $row) = @_;
104 244
190 330
191 if($row == $event->{row} && abs($col-$event->{col}) < 2 331 if($row == $event->{row} && abs($col-$event->{col}) < 2
192 && join("\x00", @$cmd) eq join("\x00", $self->command_for($row,$col))) { 332 && join("\x00", @$cmd) eq join("\x00", $self->command_for($row,$col))) {
193 if($self->valid_button($event)) { 333 if($self->valid_button($event)) {
194 334
195 $self->exec_async (@$cmd); 335 $self->exec_async (@$cmd);
196 336
197 } 337 }
198 } 338 }
199 339
200 1; 340 1;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines