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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines