… | |
… | |
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 | |
62 | sub on_key_press { |
62 | sub matchlist_key_press { |
63 | my ($self, $event, $keysym, $octets) = @_; |
63 | my ($self, $event, $keysym, $octets) = @_; |
64 | |
64 | |
65 | return unless $self->{overlay}; |
|
|
66 | |
|
|
67 | delete $self->{overlay}; |
65 | delete $self->{overlay}; |
|
|
66 | $self->disable ("key_press"); |
68 | |
67 | |
69 | my $i = ($keysym == 96 ? 0 : $keysym - 48); |
68 | my $i = ($keysym == 96 ? 0 : $keysym - 48); |
70 | if ($i >= 0 && $i < @{ $self->{matches} }) { |
69 | if ($i >= 0 && $i < @{ $self->{matches} }) { |
71 | my @exec = @{ $self->{matches}[$i] }; |
70 | my @exec = @{ $self->{matches}[$i] }; |
72 | shift @exec; |
|
|
73 | $self->exec_async (@exec); |
71 | $self->exec_async (@exec[5 .. $#exec]); |
74 | } |
72 | } |
75 | |
73 | |
76 | 1 |
74 | 1 |
77 | } |
75 | } |
78 | |
76 | |
… | |
… | |
107 | } |
105 | } |
108 | |
106 | |
109 | sub matchlist { |
107 | sub matchlist { |
110 | my ($self) = @_; |
108 | my ($self) = @_; |
111 | |
109 | |
112 | @{ $self->{matches} } = (); |
110 | $self->{matches} = []; |
113 | my $row = $self->nrow - 1; |
111 | my $row = $self->nrow - 1; |
114 | while ($row >= 0 && @{ $self->{matches} } < 10) { |
112 | while ($row >= 0 && @{ $self->{matches} } < 10) { |
115 | my $line = $self->line ($row); |
113 | my $line = $self->line ($row); |
116 | my $text = $line->t; |
114 | my @matches = $self->find_matches ($row); |
117 | |
115 | |
118 | # FIXME: code duplicated from 'command_for' |
|
|
119 | my @matches; |
|
|
120 | for my $matcher (@{$self->{matchers}}) { |
|
|
121 | my $launcher = $matcher->[1] || $self->{launcher}; |
|
|
122 | while ($text =~ /$matcher->[0]/g) { |
|
|
123 | my $match = substr ($text, $-[0], $+[0] - $-[0]); |
|
|
124 | my @beg = @-; |
|
|
125 | my @end = @+; |
|
|
126 | my @exec; |
|
|
127 | |
|
|
128 | if ($launcher !~ /\$/) { |
|
|
129 | @exec = ($launcher, $match); |
|
|
130 | } else { |
|
|
131 | @exec = map { s/\$(\d+)|\$\{(\d+)\}/ |
|
|
132 | substr ($text, $beg[$1 || $2], $end[$1 || $2] - $beg[$1 || $2]) |
|
|
133 | /egx; $_ } split /\s+/, $launcher; |
|
|
134 | } |
|
|
135 | |
|
|
136 | push @matches, [ $beg[0], $match, @exec ]; |
|
|
137 | } |
|
|
138 | } |
|
|
139 | |
|
|
140 | for (sort { $b->[0] <=> $a->[0] } @matches) { |
116 | for (sort { $b->[0] <=> $a->[0] or $b->[1] <=> $a->[1] } @matches) { |
141 | shift @$_; |
|
|
142 | push @{ $self->{matches} }, $_; |
117 | push @{ $self->{matches} }, $_; |
143 | last if @{ $self->{matches} } == 10; |
118 | last if @{ $self->{matches} } == 10; |
144 | } |
119 | } |
145 | |
120 | |
146 | $row = $line->beg - 1; |
121 | $row = $line->beg - 1; |
… | |
… | |
150 | |
125 | |
151 | my $width = 0; |
126 | my $width = 0; |
152 | |
127 | |
153 | my $i = 0; |
128 | my $i = 0; |
154 | for my $match (@{ $self->{matches} }) { |
129 | for my $match (@{ $self->{matches} }) { |
155 | my $text = $match->[0]; |
130 | my $text = $match->[4]; |
156 | my $w = $self->strwidth ("$i-$text"); |
131 | my $w = $self->strwidth ("$i-$text"); |
157 | |
132 | |
158 | $width = $w if $w > $width; |
133 | $width = $w if $w > $width; |
159 | $i++; |
134 | $i++; |
160 | } |
135 | } |
… | |
… | |
162 | $width = $self->ncol - 2 if $width > $self->ncol - 2; |
137 | $width = $self->ncol - 2 if $width > $self->ncol - 2; |
163 | |
138 | |
164 | $self->{overlay} = $self->overlay (0, 0, $width, scalar (@{ $self->{matches} }), urxvt::OVERLAY_RSTYLE, 2); |
139 | $self->{overlay} = $self->overlay (0, 0, $width, scalar (@{ $self->{matches} }), urxvt::OVERLAY_RSTYLE, 2); |
165 | my $i = 0; |
140 | my $i = 0; |
166 | for my $match (@{ $self->{matches} }) { |
141 | for my $match (@{ $self->{matches} }) { |
167 | my $text = $match->[0]; |
142 | my $text = $match->[4]; |
168 | |
143 | |
169 | $self->{overlay}->set (0, $i, "$i-$text"); |
144 | $self->{overlay}->set (0, $i, "$i-$text"); |
170 | $i++; |
145 | $i++; |
171 | } |
146 | } |
172 | |
147 | |
|
|
148 | $self->enable (key_press => \&matchlist_key_press); |
173 | } |
149 | } |
174 | |
150 | |
175 | sub most_recent { |
151 | sub most_recent { |
176 | my ($self) = shift; |
152 | my ($self) = shift; |
177 | my $row = $self->nrow; |
153 | my $row = $self->nrow; |
… | |
… | |
208 | sub on_start { |
184 | sub on_start { |
209 | my ($self) = @_; |
185 | my ($self) = @_; |
210 | |
186 | |
211 | $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"; |
212 | |
188 | |
213 | $self->{matches} = []; |
|
|
214 | $self->{button} = 2; |
189 | $self->{button} = 2; |
215 | $self->{state} = 0; |
190 | $self->{state} = 0; |
216 | if($self->{argv}[0] || $self->my_resource ("button")) { |
191 | if($self->{argv}[0] || $self->my_resource ("button")) { |
217 | my @mods = split '', $self->{argv}[0] || $self->my_resource ("button"); |
192 | my @mods = split '', $self->{argv}[0] || $self->my_resource ("button"); |
218 | for my $mod (@mods) { |
193 | for my $mod (@mods) { |
… | |
… | |
249 | my ($self, $row) = @_; |
224 | my ($self, $row) = @_; |
250 | |
225 | |
251 | # fetch the line that has changed |
226 | # fetch the line that has changed |
252 | my $line = $self->line ($row); |
227 | my $line = $self->line ($row); |
253 | my $text = $line->t; |
228 | my $text = $line->t; |
254 | my $i = 0; |
|
|
255 | |
229 | |
256 | # find all urls (if any) |
230 | # find all urls (if any) |
257 | for my $matcher (@{$self->{matchers}}) { |
231 | for my $matcher (@{$self->{matchers}}) { |
258 | while ($text =~ /$matcher->[0]/g) { |
232 | while ($text =~ /$matcher->[0]/g) { |
259 | #print "$&\n"; |
233 | #print "$&\n"; |
… | |
… | |
277 | | urxvt::ShiftMask | urxvt::ControlMask; |
251 | | urxvt::ShiftMask | urxvt::ControlMask; |
278 | return ($event->{button} == $self->{button} && |
252 | return ($event->{button} == $self->{button} && |
279 | ($event->{state} & $mask) == $self->{state}); |
253 | ($event->{state} & $mask) == $self->{state}); |
280 | } |
254 | } |
281 | |
255 | |
282 | sub command_for { |
256 | sub find_matches { |
283 | my ($self, $row, $col) = @_; |
257 | my ($self, $row, $col) = @_; |
284 | my $line = $self->line ($row); |
258 | my $line = $self->line ($row); |
285 | my $text = $line->t; |
259 | my $text = $line->t; |
|
|
260 | my $off = $line->offset_of ($row, $col) if $col; |
286 | |
261 | |
|
|
262 | my @matches; |
287 | for my $matcher (@{$self->{matchers}}) { |
263 | for my $matcher (@{$self->{matchers}}) { |
288 | my $launcher = $matcher->[1] || $self->{launcher}; |
264 | my $launcher = $matcher->[1] || $self->{launcher}; |
289 | while (($text =~ /$matcher->[0]/g)) { |
265 | while ($text =~ /$matcher->[0]/g) { |
290 | my $match = $&; |
266 | my $match = substr $text, $-[0], $+[0] - $-[0]; |
291 | my @begin = @-; |
267 | my @begin = @-; |
292 | my @end = @+; |
268 | my @end = @+; |
|
|
269 | my @exec; |
|
|
270 | |
293 | if (!defined($col) || ($-[0] <= $col && $+[0] >= $col)) { |
271 | if (!defined($off) || ($-[0] <= $off && $+[0] >= $off)) { |
294 | if ($launcher !~ /\$/) { |
272 | if ($launcher !~ /\$/) { |
295 | return ($launcher,$match); |
273 | @exec = ($launcher, $match); |
296 | } else { |
274 | } else { |
297 | # 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...), |
298 | # but alas, m//g behaves differently in list context. |
276 | # but alas, m//g behaves differently in list context. |
299 | my @exec = map { s/\$(\d+)|\$\{(\d+)\}/ |
277 | @exec = map { s/\$(\d+)|\$\{(\d+)\}/ |
300 | substr($text,$begin[$1||$2],$end[$1||$2]-$begin[$1||$2]) |
278 | substr $text, $begin[$1 || $2], $end[$1 || $2] - $begin[$1 || $2] |
301 | /egx; $_ } split(/\s+/, $launcher); |
279 | /egx; $_ } split /\s+/, $launcher; |
302 | return @exec; |
|
|
303 | } |
280 | } |
|
|
281 | |
|
|
282 | push @matches, [ $line->coord_of ($begin[0]), $line->coord_of ($end[0]), $match, @exec ]; |
304 | } |
283 | } |
305 | } |
284 | } |
|
|
285 | } |
|
|
286 | |
|
|
287 | @matches; |
|
|
288 | } |
|
|
289 | |
|
|
290 | sub 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]; |
306 | } |
297 | } |
307 | |
298 | |
308 | () |
299 | () |
309 | } |
300 | } |
310 | |
301 | |