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.16 by sf-exg, Wed May 28 17:01:32 2014 UTC vs.
Revision 1.27 by sf-exg, Sun Jun 15 11:38:00 2014 UTC

28C<matcher.pattern.0> resource, and additional patterns can be specified 28C<matcher.pattern.0> resource, and additional patterns can be specified
29with numbered patterns, in a manner similar to the "selection" extension. 29with numbered patterns, in a manner similar to the "selection" extension.
30The launcher can also be overridden on a per-pattern basis. 30The launcher can also be overridden on a per-pattern basis.
31 31
32It is possible to activate the most recently seen match or a list of matches 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 33from the keyboard. Simply bind a keysym to "matcher:last" or
34"perl:matcher:list" as seen in the example below. 34"matcher:list" as seen in the example below.
35 35
36Example: load and use the matcher extension with defaults. 36Example: load and use the matcher extension with defaults.
37 37
38 URxvt.perl-ext: default,matcher 38 URxvt.perl-ext: default,matcher
39 39
40Example: use a custom configuration. 40Example: use a custom configuration.
41 41
42 URxvt.url-launcher: sensible-browser 42 URxvt.url-launcher: sensible-browser
43 URxvt.keysym.C-Delete: perl:matcher:last 43 URxvt.keysym.C-Delete: matcher:last
44 URxvt.keysym.M-Delete: perl:matcher:list 44 URxvt.keysym.M-Delete: matcher:list
45 URxvt.matcher.button: 1 45 URxvt.matcher.button: 1
46 URxvt.matcher.pattern.1: \\bwww\\.[\\w-]+\\.[\\w./?&@#-]*[\\w/-] 46 URxvt.matcher.pattern.1: \\bwww\\.[\\w-]+\\.[\\w./?&@#-]*[\\w/-]
47 URxvt.matcher.pattern.2: \\B(/\\S+?):(\\d+)(?=:|$) 47 URxvt.matcher.pattern.2: \\B(/\\S+?):(\\d+)(?=:|$)
48 URxvt.matcher.launcher.2: gvim +$2 $1 48 URxvt.matcher.launcher.2: gvim +$2 $1
49 49
57 \([\w\-\@;\/?:&=%\$.+!*\x27,~#]*\)| # Allow a pair of matched parentheses 57 \([\w\-\@;\/?:&=%\$.+!*\x27,~#]*\)| # Allow a pair of matched parentheses
58 [\w\-\@;\/?:&=%\$+*~] # exclude some trailing characters (heuristic) 58 [\w\-\@;\/?:&=%\$+*~] # exclude some trailing characters (heuristic)
59 )+ 59 )+
60 }x; 60 }x;
61 61
62sub on_key_press { 62sub matchlist_key_press {
63 my ($self, $event, $keysym, $octets) = @_; 63 my ($self, $event, $keysym, $octets) = @_;
64 64
65 if (! $self->{showing} ) { 65 delete $self->{overlay};
66 return; 66 $self->disable ("key_press");
67 }
68 67
69 my $i = ($keysym == 96 ? 0 : $keysym - 48); 68 my $i = ($keysym == 96 ? 0 : $keysym - 48);
70 if (($i > scalar(@{$self->{urls}})) || ($i < 0)) { 69 if ($i >= 0 && $i < @{ $self->{matches} }) {
71 $self->matchlist(); 70 my @exec = @{ $self->{matches}[$i] };
72 return; 71 $self->exec_async (@exec[5 .. $#exec]);
72 }
73
73 } 74 1
74
75 my @args = ($self->{urls}[ -$i-1 ]);
76 $self->matchlist();
77
78 $self->exec_async( $self->{launcher}, @args );
79} 75}
80 76
81# backwards compat 77# backwards compat
82sub on_user_command { 78sub on_user_command {
83 my ($self, $cmd) = @_; 79 my ($self, $cmd) = @_;
94 } 90 }
95 91
96 () 92 ()
97} 93}
98 94
95sub 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
99sub matchlist { 107sub matchlist {
100 my ($self) = @_; 108 my ($self) = @_;
101 if ( $self->{showing} ) { 109
102 $self->{url_overlay}->hide; 110 $self->{matches} = [];
103 $self->{showing} = 0; 111 my $row = $self->nrow - 1;
104 return; 112 while ($row >= 0 && @{ $self->{matches} } < 10) {
105 }
106 @{$self->{urls}} = ();
107 my $line;
108 for (my $i = 0; $i < $self->nrow; $i ++) {
109 $line = $self->line($i); 113 my $line = $self->line ($row);
110 next if ($line->beg != $i); 114 my @matches = $self->find_matches ($row);
111 for my $url ($self->get_urls_from_line($line->t)) { 115
112 if (scalar(@{$self->{urls}}) == 10) { 116 for (sort { $b->[0] <=> $a->[0] or $b->[1] <=> $a->[1] } @matches) {
113 shift @{$self->{urls}};
114 }
115 push @{$self->{urls}}, $url; 117 push @{ $self->{matches} }, $_;
118 last if @{ $self->{matches} } == 10;
116 } 119 }
120
121 $row = $line->beg - 1;
117 } 122 }
118 123
119 if (! scalar(@{$self->{urls}})) { 124 return unless @{ $self->{matches} };
120 return;
121 }
122 125
123 my $max = 0; 126 my $width = 0;
124 my $i = scalar( @{$self->{urls}} ) - 1 ;;
125 127
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; 128 my $i = 0;
149 for my $url (@temp) { 129 for my $match (@{ $self->{matches} }) {
150 $self->{url_overlay}->set( 0, $i, $url, [(urxvt::OVERLAY_RSTYLE) x length $url]); 130 my $text = $match->[4];
151 $self->{showing} = 1; 131 my $w = $self->strwidth ("$i-$text");
132
133 $width = $w if $w > $width;
152 $i++; 134 $i++;
153 } 135 }
154 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);
155} 149}
156 150
157sub most_recent { 151sub most_recent {
158 my ($self) = shift; 152 my ($self) = shift;
159 my $row = $self->nrow; 153 my $row = $self->nrow;
190sub on_start { 184sub on_start {
191 my ($self) = @_; 185 my ($self) = @_;
192 186
193 $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";
194 188
195 $self->{urls} = [];
196 $self->{showing} = 0;
197 $self->{button} = 2; 189 $self->{button} = 2;
198 $self->{state} = 0; 190 $self->{state} = 0;
199 if($self->{argv}[0] || $self->my_resource ("button")) { 191 if($self->{argv}[0] || $self->my_resource ("button")) {
200 my @mods = split '', $self->{argv}[0] || $self->my_resource ("button"); 192 my @mods = split '', $self->{argv}[0] || $self->my_resource ("button");
201 for my $mod (@mods) { 193 for my $mod (@mods) {
226 $self->{matchers} = \@matchers; 218 $self->{matchers} = \@matchers;
227 219
228 () 220 ()
229} 221}
230 222
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;
240}
241
242sub on_line_update { 223sub on_line_update {
243 my ($self, $row) = @_; 224 my ($self, $row) = @_;
244 225
245 # fetch the line that has changed 226 # fetch the line that has changed
246 my $line = $self->line ($row); 227 my $line = $self->line ($row);
247 my $text = $line->t; 228 my $text = $line->t;
248 my $i = 0;
249 229
250 # find all urls (if any) 230 # find all urls (if any)
251 for my $matcher (@{$self->{matchers}}) { 231 for my $matcher (@{$self->{matchers}}) {
252 while ($text =~ /$matcher->[0]/g) { 232 while ($text =~ /$matcher->[0]/g) {
253 #print "$&\n"; 233 #print "$&\n";
271 | urxvt::ShiftMask | urxvt::ControlMask; 251 | urxvt::ShiftMask | urxvt::ControlMask;
272 return ($event->{button} == $self->{button} && 252 return ($event->{button} == $self->{button} &&
273 ($event->{state} & $mask) == $self->{state}); 253 ($event->{state} & $mask) == $self->{state});
274} 254}
275 255
276sub command_for { 256sub find_matches {
277 my ($self, $row, $col) = @_; 257 my ($self, $row, $col) = @_;
278 my $line = $self->line ($row); 258 my $line = $self->line ($row);
279 my $text = $line->t; 259 my $text = $line->t;
260 my $off = $line->offset_of ($row, $col) if $col;
280 261
262 my @matches;
281 for my $matcher (@{$self->{matchers}}) { 263 for my $matcher (@{$self->{matchers}}) {
282 my $launcher = $matcher->[1] || $self->{launcher}; 264 my $launcher = $matcher->[1] || $self->{launcher};
283 while (($text =~ /$matcher->[0]/g)) { 265 while ($text =~ /$matcher->[0]/g) {
284 my $match = $&; 266 my $match = substr $text, $-[0], $+[0] - $-[0];
285 my @begin = @-; 267 my @begin = @-;
286 my @end = @+; 268 my @end = @+;
269 my @exec;
270
287 if (!defined($col) || ($-[0] <= $col && $+[0] >= $col)) { 271 if (!defined($off) || ($-[0] <= $off && $+[0] >= $off)) {
288 if ($launcher !~ /\$/) { 272 if ($launcher !~ /\$/) {
289 return ($launcher,$match); 273 @exec = ($launcher, $match);
290 } else { 274 } else {
291 # It'd be nice to just access a list like ($&,$1,$2...), 275 # It'd be nice to just access a list like ($&,$1,$2...),
292 # but alas, m//g behaves differently in list context. 276 # but alas, m//g behaves differently in list context.
293 my @exec = map { s/\$(\d+)|\$\{(\d+)\}/ 277 @exec = map { s/\$(\d+)|\$\{(\d+)\}/
294 substr($text,$begin[$1||$2],$end[$1||$2]-$begin[$1||$2]) 278 substr $text, $begin[$1 || $2], $end[$1 || $2] - $begin[$1 || $2]
295 /egx; $_ } split(/\s+/, $launcher); 279 /egx; $_ } split /\s+/, $launcher;
296 return @exec;
297 } 280 }
281
282 push @matches, [ $line->coord_of ($begin[0]), $line->coord_of ($end[0]), $match, @exec ];
298 } 283 }
299 } 284 }
285 }
286
287 @matches;
288}
289
290sub 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];
300 } 297 }
301 298
302 () 299 ()
303} 300}
304 301
330 327
331 if($row == $event->{row} && abs($col-$event->{col}) < 2 328 if($row == $event->{row} && abs($col-$event->{col}) < 2
332 && join("\x00", @$cmd) eq join("\x00", $self->command_for($row,$col))) { 329 && join("\x00", @$cmd) eq join("\x00", $self->command_for($row,$col))) {
333 if($self->valid_button($event)) { 330 if($self->valid_button($event)) {
334 331
335 $self->exec_async (@$cmd); 332 $self->exec_async (@$cmd);
336 333
337 } 334 }
338 } 335 }
339 336
340 1; 337 1;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines