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> |
4 | |
5 | |
5 | my $url = |
6 | my $url = |
6 | qr{ |
7 | qr{ |
7 | (?:https?://|ftp://|news://|mailto:|file://|\bwww\.) |
8 | (?:https?://|ftp://|news://|mailto:|file://|\bwww\.) |
8 | [a-zA-Z0-9\-\@;\/?:&=%\$_.+!*\x27,~#]* |
9 | [a-zA-Z0-9\-\@;\/?:&=%\$_.+!*\x27,~#]* |
… | |
… | |
10 | \([a-zA-Z0-9\-\@;\/?:&=%\$_.+!*\x27,~#]*\)| # Allow a pair of matched parentheses |
11 | \([a-zA-Z0-9\-\@;\/?:&=%\$_.+!*\x27,~#]*\)| # Allow a pair of matched parentheses |
11 | [a-zA-Z0-9\-\@;\/?:&=%\$_+*~] # exclude some trailing characters (heuristic) |
12 | [a-zA-Z0-9\-\@;\/?:&=%\$_+*~] # exclude some trailing characters (heuristic) |
12 | )+ |
13 | )+ |
13 | }x; |
14 | }x; |
14 | |
15 | |
|
|
16 | sub on_key_press { |
|
|
17 | my ($self, $event, $keysym, $octets) = @_; |
|
|
18 | |
|
|
19 | if (! $self->{showing} ) { |
|
|
20 | return; |
|
|
21 | } |
|
|
22 | |
|
|
23 | my $i = ($keysym == 96 ? 0 : $keysym - 48); |
|
|
24 | if (($i > scalar(@{$self->{urls}})) || ($i < 0)) { |
|
|
25 | $self->matchlist(); |
|
|
26 | return; |
|
|
27 | } |
|
|
28 | |
|
|
29 | my @args = ($self->{urls}[ -$i-1 ]); |
|
|
30 | $self->matchlist(); |
|
|
31 | |
|
|
32 | $self->exec_async( $self->{launcher}, @args ); |
|
|
33 | } |
|
|
34 | |
15 | sub on_user_command { |
35 | sub on_user_command { |
16 | my ($self, $cmd) = @_; |
36 | my ($self, $cmd) = @_; |
|
|
37 | |
|
|
38 | if($cmd =~ s/^matcher:list\b//) { |
|
|
39 | $self->matchlist(); |
|
|
40 | } else { |
|
|
41 | if($cmd =~ s/^matcher:last\b//) { |
|
|
42 | $self->most_recent; |
|
|
43 | } |
|
|
44 | # For backward compatibility |
|
|
45 | else { |
17 | if($cmd =~ s/^matcher\b//) { |
46 | if($cmd =~ s/^matcher\b//) { |
18 | $self->most_recent; |
47 | $self->most_recent; |
|
|
48 | } |
|
|
49 | } |
19 | } |
50 | } |
20 | () |
51 | () |
|
|
52 | } |
|
|
53 | |
|
|
54 | sub matchlist { |
|
|
55 | my ($self) = @_; |
|
|
56 | if ( $self->{showing} ) { |
|
|
57 | $self->{url_overlay}->hide(); |
|
|
58 | $self->{showing} = 0; |
|
|
59 | return; |
|
|
60 | } |
|
|
61 | @{$self->{urls}} = (); |
|
|
62 | my $line; |
|
|
63 | for (my $i = 0; $i < $self->nrow; $i ++) { |
|
|
64 | $line = $self->line($i); |
|
|
65 | next if ($line->beg != $i); |
|
|
66 | for my $url ($self->get_urls_from_line($line->t)) { |
|
|
67 | if (scalar(@{$self->{urls}}) == 10) { |
|
|
68 | shift @{$self->{urls}}; |
|
|
69 | } |
|
|
70 | push @{$self->{urls}}, $url; |
|
|
71 | } |
|
|
72 | } |
|
|
73 | |
|
|
74 | if (! scalar(@{$self->{urls}})) { |
|
|
75 | return; |
|
|
76 | } |
|
|
77 | |
|
|
78 | my $max = 0; |
|
|
79 | my $i = scalar( @{$self->{urls}} ) - 1 ;; |
|
|
80 | |
|
|
81 | my @temp = (); |
|
|
82 | |
|
|
83 | for my $url (@{$self->{urls}}) { |
|
|
84 | my $url = "$i-$url"; |
|
|
85 | my $xpos = 0; |
|
|
86 | |
|
|
87 | if ($self->ncol + (length $url) >= $self->ncol) { |
|
|
88 | $url = substr( $url, 0, $self->ncol ); |
|
|
89 | } |
|
|
90 | |
|
|
91 | push @temp, $url; |
|
|
92 | |
|
|
93 | if( length $url > $max ) { |
|
|
94 | $max = length $url; |
|
|
95 | } |
|
|
96 | |
|
|
97 | $i--; |
|
|
98 | } |
|
|
99 | |
|
|
100 | @temp = reverse @temp; |
|
|
101 | |
|
|
102 | $self->{url_overlay} = $self->overlay(0, 0, $max, scalar( @temp ), urxvt::OVERLAY_RSTYLE, 2); |
|
|
103 | my $i = 0; |
|
|
104 | for my $url (@temp) { |
|
|
105 | $self->{url_overlay}->set( 0, $i, $url, [(urxvt::OVERLAY_RSTYLE) x length $url]); |
|
|
106 | $self->{showing} = 1; |
|
|
107 | $i++; |
|
|
108 | } |
|
|
109 | |
21 | } |
110 | } |
22 | |
111 | |
23 | sub most_recent { |
112 | sub most_recent { |
24 | my ($self) = shift; |
113 | my ($self) = shift; |
25 | my $row = $self->nrow; |
114 | my $row = $self->nrow; |
26 | my @exec; |
115 | my @exec; |
27 | while($row-- > $self->top_row) { |
116 | while($row-- > $self->top_row) { |
28 | #my $line = $self->line ($row); |
|
|
29 | #my $text = $line->t; |
|
|
30 | @exec = $self->command_for($row); |
117 | @exec = $self->command_for($row); |
31 | last if(@exec); |
118 | last if(@exec); |
32 | } |
119 | } |
33 | if(@exec) { |
120 | if(@exec) { |
34 | return $self->exec_async (@exec); |
121 | return $self->exec_async (@exec); |
… | |
… | |
42 | } |
129 | } |
43 | |
130 | |
44 | # turn a rendition spec in the resource into a sub that implements it on $_ |
131 | # turn a rendition spec in the resource into a sub that implements it on $_ |
45 | sub parse_rend { |
132 | sub parse_rend { |
46 | my ($self, $str) = @_; |
133 | my ($self, $str) = @_; |
47 | my ($mask, $fg, $bg, $failed) = $str ? urxvt::rend2mask($str) |
134 | my ($mask, $fg, $bg, $failed) = $str ? urxvt::rend2mask($str) |
48 | : (urxvt::RS_Uline, undef, undef, []); |
135 | : (urxvt::RS_Uline, undef, undef, []); |
49 | warn "Failed to parse rendition string: " . join(',', @$failed) if @$failed; |
136 | warn "Failed to parse rendition string: " . join(',', @$failed) if @$failed; |
50 | my @rend; |
137 | my @rend; |
51 | push @rend, sub { $_ |= $mask } if $mask; |
138 | push @rend, sub { $_ |= $mask } if $mask; |
52 | push @rend, sub { $_ = urxvt::SET_FGCOLOR($_, $fg) } if defined $fg; |
139 | push @rend, sub { $_ = urxvt::SET_FGCOLOR($_, $fg) } if defined $fg; |
… | |
… | |
63 | $self->{name} =~ tr/_/-/; |
150 | $self->{name} =~ tr/_/-/; |
64 | $self->{launcher} = $self->my_resource("launcher") || |
151 | $self->{launcher} = $self->my_resource("launcher") || |
65 | $self->x_resource("urlLauncher") || |
152 | $self->x_resource("urlLauncher") || |
66 | "sensible-browser"; |
153 | "sensible-browser"; |
67 | |
154 | |
|
|
155 | $self->{urls} = []; |
|
|
156 | $self->{showing} = 0; |
68 | $self->{button} = 2; |
157 | $self->{button} = 2; |
69 | $self->{state} = 0; |
158 | $self->{state} = 0; |
70 | if($self->{argv}[0] || $self->my_resource("button")) { |
159 | if($self->{argv}[0] || $self->my_resource("button")) { |
71 | my @mods = split('', $self->{argv}[0] || $self->my_resource("button")); |
160 | my @mods = split('', $self->{argv}[0] || $self->my_resource("button")); |
72 | for my $mod (@mods) { |
161 | for my $mod (@mods) { |
… | |
… | |
95 | unshift @matchers, [qr($res)x,$launcher,$rend]; |
184 | unshift @matchers, [qr($res)x,$launcher,$rend]; |
96 | } |
185 | } |
97 | $self->{matchers} = \@matchers; |
186 | $self->{matchers} = \@matchers; |
98 | |
187 | |
99 | () |
188 | () |
|
|
189 | } |
|
|
190 | |
|
|
191 | sub get_urls_from_line { |
|
|
192 | my ($self, $line) = @_; |
|
|
193 | my @urls; |
|
|
194 | for my $matcher (@{$self->{matchers}}) { |
|
|
195 | while ($line =~ /$matcher->[0]/g) { |
|
|
196 | push @urls, substr( $line, $-[0], $+[0] - $-[0] ); |
|
|
197 | } |
|
|
198 | } |
|
|
199 | return @urls; |
100 | } |
200 | } |
101 | |
201 | |
102 | sub on_line_update { |
202 | sub on_line_update { |
103 | my ($self, $row) = @_; |
203 | my ($self, $row) = @_; |
104 | |
204 | |