2 # Author: Bart Trojanowski <bart@jukie.net>
3 # Website: http://www.jukie.net/~bart/blog/urxvt-url-yank
10 (?
:https?
://|ftp
://|news
://|mailto
:|file
://|www\
.)[ab
-zA
-Z0
-9\
-\@
;\
/?
:&=%\
$_.+!*\x27(),~#]+
11 [ab
-zA
-Z0
-9\
-\@
;\
/?
&=%\
$_+!*\x27()~] # exclude some trailing characters (heuristic)
18 $term->{have_Clipboard
} = eval { require Clipboard
; };
19 if ($term->{have_Clipboard
}) {
23 eval { require Regexp
::Common
::URI
};
25 require Regexp
::Common
;
26 Regexp
::Common
->import('URI');
28 $url_matcher = $Regexp::Common
::RE
{URI
}{HTTP
};
31 $term->{browser
} = $term->x_resource ("urlLauncher") || "x-www-browser";
37 my ($term, $row) = @_;
39 # Fetch the line that has changed.
40 my $line = $term->line($row);
43 # Find and underline URLs.
44 while ($text =~ /($url_matcher)/g) {
48 # Mark all characters as underlined. we _must_ not toggle underline, as
49 # we might get called on an already-marked URL.
50 my $underlineURLs = $term->x_resource ('underlineURLs') || 'false';
51 if($underlineURLs eq 'true') {
52 my ($first, $last) = ($-[1], $+[1] - 1);
54 --$last if $url =~ s/["']$//;
56 $_ |= urxvt
::RS_Uline
for @
{$rend}[$first .. $last];
65 sub on_button_release
{
66 my ($term, $event) = @_;
68 my $mask = $term->ModLevel3Mask | $term->ModMetaMask
69 | urxvt
::ShiftMask
| urxvt
::ControlMask
;
71 if ($event->{button
} == 2 && ($event->{state} & $mask) == 0) {
72 my $row = $event->{row
};
73 my $col = $event->{col
};
75 my $line = $term->line ($row);
78 while ($text =~ /($url_matcher)/g) {
79 my ($url, $first, $last) = ($1, $-[1], $+[1]);
81 if($first <= $col && $last >= $col) {
83 $term->exec_async($term->{browser
}, $url);
93 my $mark_mode_active = 0;
94 my %mod = ( 'control' => 0, 'shift' => 0 );
95 my $url_selected = -1;
99 sub do_scan_for_urls
{
104 my $row_start = $term->top_row;
105 my $row_end = $term->nrow;
107 for my $row ($row_start .. $row_end) {
109 # Fetch the line that has changed.
110 my $line = $term->line ($row);
113 # Find all urls (if any).
114 while ($text =~ /($url_matcher)/g) {
117 my ($url, $first, $last) = ($1, $-[1], $+[1] - 1);
119 --$last if $url =~ s/["']$//;
132 # 0 for none, positive count otherwise
137 sub on_user_command
{
138 my ($term, $cmd) = @_;
140 activate_mark_mode
($term) if $cmd eq 'mark-yank-urls:activate_mark_mode';
146 my ($term, $event, $keysym, $octets) = @_;
148 if ($keysym == 65507) { # <control>
151 } elsif ($keysym == 65505) { # <shift>
156 # Ignore all input when we are active.
157 $mark_mode_active && return 1;
163 my ($term, $event, $keysym, $octets) = @_;
165 if ($mark_mode_active) {
166 my $ch = chr($keysym);
168 if ($keysym == 65307) { # <esc>
169 deactivate_mark_mode
($term);
172 } elsif ($keysym == 65293) { # <enter>
173 my $url = get_active_url
($term);
174 $term->exec_async($term->{browser
}, $url);
175 deactivate_mark_mode
($term);
178 } elsif ($keysym == 65507) { # <control>
182 } elsif ($keysym == 65505) { # <shift>
186 } elsif ($mod{control
} && (($ch eq 'n') || ($ch eq 'p'))) {
187 # ^n and ^p to cycle list
188 my $dir = ($ch eq 'n') ?
1 : -1;
189 move_highlight
($term, $dir);
191 } elsif ($ch eq 'y') { # y
193 deactivate_mark_mode
($term);
206 my $max = $#url_db + 1;
208 return if $url_selected < 0 || $url_selected >= $max;
209 return if not defined $url_db[$url_selected];
210 my $o = $url_db[$url_selected];
219 my $text = get_active_url
($term);
221 if ($term->{have_Clipboard
}) {
222 Clipboard
->copy($text);
224 $text =~ s/\(["|><&()]\)/\\$1/;
225 system ("echo -n \"$text\" | xclip -i");
230 my ($term, $dir) = @_;
231 my $max = $#url_db + 1;
233 do_highlight
($term, 0);
235 $url_selected = ($max + $url_selected + $dir) % $max;
237 do_highlight
($term, 1);
243 my ($term, $enable) = @_;
244 my $max = $#url_db + 1;
246 return if $url_selected < 0 || $url_selected >= $max;
247 return if not defined $url_db[$url_selected];
249 my $o = $url_db[$url_selected];
253 my $line = $term->line ($row);
259 for @
{$rend}[ $h{col_from
} .. $h{col_to
}];
262 $term->view_start ( $row < 0 ?
$row : 0 );
265 $_ &= ~urxvt
::RS_RVid
266 for @
{$rend}[ $h{col_from
} .. $h{col_to
}];
272 sub activate_mark_mode
{
275 if ($mark_mode_active) {
277 move_highlight
($term, -1);
279 } elsif ( do_scan_for_urls
($term) ) {
281 $term->{save_view_start
} = $term->view_start;
283 move_highlight
($term, 0);
285 $mark_mode_active=1 if ($url_selected > -1);
289 sub deactivate_mark_mode
{
292 do_highlight
($term, 0);
294 $mark_mode_active = 0;
297 $term->view_start ($term->{save_view_start
});
301 # vim: set et ts=4 sw=4: