2010-08-02 01:07:10 -04:00
|
|
|
#! perl -w
|
|
|
|
# Author: Bert Muennich
|
|
|
|
# Website: http://www.github.com/muennich/urxvt-perls
|
|
|
|
# Based on: http://www.jukie.net/~bart/blog/urxvt-url-yank
|
2012-01-20 17:26:58 -05:00
|
|
|
# Version: git-20120120
|
2010-08-02 01:07:10 -04:00
|
|
|
# License: GPLv2
|
|
|
|
|
|
|
|
# Use keyboard shortcuts to select URLs.
|
|
|
|
# This should be used as a replacement for the default matcher extension,
|
|
|
|
# it also makes URLs clickable with the middle mouse button.
|
|
|
|
|
2011-05-28 06:52:44 -04:00
|
|
|
# Usage: put the following lines in your .Xdefaults/.Xresources:
|
2010-08-13 16:04:04 -04:00
|
|
|
# URxvt.perl-ext-common: ...,url-select
|
|
|
|
# URxvt.keysym.M-u: perl:url-select:select_next
|
2010-08-02 01:07:10 -04:00
|
|
|
|
|
|
|
# Use Meta-u to activate URL selection mode, then use the following keys:
|
2011-11-05 08:18:49 -04:00
|
|
|
# j/k: Select next downward/upward URL (also with arrow keys)
|
|
|
|
# g/G: Select first/last URL (also with home/end key)
|
2010-08-17 11:04:58 -04:00
|
|
|
# o/Return: Open selected URL in browser, Return: deactivate afterwards
|
|
|
|
# y: Copy (yank) selected URL and deactivate selection mode
|
|
|
|
# q/Escape: Deactivate URL selection mode
|
2010-08-02 01:07:10 -04:00
|
|
|
|
|
|
|
# Options:
|
2010-08-17 11:04:58 -04:00
|
|
|
# URxvt.urlLauncher: Browser/command to open selected URL with
|
|
|
|
# URxvt.underlineURLs: If set to true, all URLs get underlined
|
2011-04-20 05:19:25 -04:00
|
|
|
# URvxt.urlButton: Mouse button to click-open URLs (default: 2)
|
2010-08-02 01:07:10 -04:00
|
|
|
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
|
|
|
|
my $url_matcher = qr{(
|
|
|
|
(?:https?://|ftp://|news://|mailto:|file://|www\.)
|
2010-08-29 16:08:22 -04:00
|
|
|
[\w\-\@;\/?:&=%\$_.+!*\x27(),~#]+[\w\-\@;\/?&=%\$_+!*\x27()~]
|
2010-08-02 01:07:10 -04:00
|
|
|
)}x;
|
|
|
|
|
|
|
|
sub on_start {
|
2010-08-02 18:44:04 -04:00
|
|
|
my ($self) = @_;
|
2010-08-02 01:07:10 -04:00
|
|
|
|
|
|
|
# read resource settings
|
2011-04-23 09:26:57 -04:00
|
|
|
if ($self->x_resource('urlLauncher')) {
|
2011-04-23 09:32:34 -04:00
|
|
|
@{$self->{browser}} = split /\s+/, $self->x_resource('urlLauncher');
|
2011-04-23 09:26:57 -04:00
|
|
|
} else {
|
|
|
|
@{$self->{browser}} = ('x-www-browser');
|
|
|
|
}
|
2010-08-02 18:44:04 -04:00
|
|
|
if ($self->x_resource('underlineURLs') eq 'true') {
|
2010-08-17 14:41:12 -04:00
|
|
|
$self->enable(line_update => \&line_update);
|
2010-08-02 01:07:10 -04:00
|
|
|
}
|
2011-04-15 01:50:34 -04:00
|
|
|
if($self->x_resource('urlButton') =~ /^\d+$/) {
|
|
|
|
$self->{button} = $self->x_resource('urlButton');
|
2011-04-20 05:19:25 -04:00
|
|
|
} else {
|
2011-04-15 01:50:34 -04:00
|
|
|
$self->{button} = 2;
|
|
|
|
}
|
|
|
|
|
2010-08-02 01:07:10 -04:00
|
|
|
()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-17 14:41:12 -04:00
|
|
|
sub line_update {
|
2010-08-02 18:44:04 -04:00
|
|
|
my ($self, $row) = @_;
|
2010-08-02 01:07:10 -04:00
|
|
|
|
2010-08-17 14:41:12 -04:00
|
|
|
my $line = $self->line($row);
|
|
|
|
my $text = $line->t;
|
|
|
|
my $rend = $line->r;
|
2010-08-02 01:07:10 -04:00
|
|
|
|
2010-08-17 14:41:12 -04:00
|
|
|
while ($text =~ /$url_matcher/g) {
|
|
|
|
my $url = $1;
|
|
|
|
my ($beg, $end) = ($-[1], $+[1] - 1);
|
|
|
|
--$end if $url =~ /["')]$/;
|
2010-08-02 01:07:10 -04:00
|
|
|
|
2010-08-17 14:41:12 -04:00
|
|
|
for (@{$rend}[$beg .. $end]) {
|
|
|
|
$_ |= urxvt::RS_Uline;
|
2010-08-02 01:07:10 -04:00
|
|
|
}
|
2010-08-17 14:41:12 -04:00
|
|
|
$line->r($rend);
|
2010-08-02 01:07:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sub on_user_command {
|
2010-08-02 18:44:04 -04:00
|
|
|
my ($self, $cmd) = @_;
|
2010-08-02 01:07:10 -04:00
|
|
|
|
|
|
|
if ($cmd eq 'url-select:select_next') {
|
2010-08-02 18:44:04 -04:00
|
|
|
if (not $self->{active}) {
|
|
|
|
activate($self);
|
2010-08-02 01:07:10 -04:00
|
|
|
}
|
2010-08-02 18:44:04 -04:00
|
|
|
select_next($self, -1);
|
2010-08-02 01:07:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-02 18:44:04 -04:00
|
|
|
sub key_press {
|
|
|
|
my ($self, $event, $keysym) = @_;
|
|
|
|
my $char = chr($keysym);
|
2010-08-02 01:07:10 -04:00
|
|
|
|
2010-08-17 11:04:58 -04:00
|
|
|
if ($keysym == 0xff1b || lc($char) eq 'q') {
|
2010-08-02 18:44:04 -04:00
|
|
|
deactivate($self);
|
2010-08-17 11:04:58 -04:00
|
|
|
} elsif ($keysym == 0xff0d || $char eq 'o') {
|
2011-04-23 09:26:57 -04:00
|
|
|
$self->exec_async(@{$self->{browser}}, ${$self->{found}[$self->{n}]}[4]);
|
2010-08-17 11:04:58 -04:00
|
|
|
deactivate($self) unless $char eq 'o';
|
2010-08-02 18:44:04 -04:00
|
|
|
} elsif ($char eq 'y') {
|
2012-01-18 01:40:38 -05:00
|
|
|
my $found = $self->{found}[$self->{n}];
|
|
|
|
$self->selection_beg(${$found}[0], ${$found}[1]);
|
|
|
|
$self->selection_end(${$found}[2], ${$found}[3]);
|
|
|
|
$self->selection_make($event->{time});
|
|
|
|
$self->selection_beg(1, 0);
|
|
|
|
$self->selection_end(1, 0);
|
2010-08-02 18:44:04 -04:00
|
|
|
deactivate($self);
|
2011-11-04 15:11:59 -04:00
|
|
|
} elsif ($char eq 'k' || $keysym == 0xff52 || $keysym == 0xff51) {
|
2010-08-02 18:44:04 -04:00
|
|
|
select_next($self, -1);
|
2011-11-04 15:11:59 -04:00
|
|
|
} elsif ($char eq 'j' || $keysym == 0xff54 || $keysym == 0xff53) {
|
2010-08-02 18:44:04 -04:00
|
|
|
select_next($self, 1);
|
2011-11-04 15:11:59 -04:00
|
|
|
} elsif ($char eq 'g' || $keysym == 0xff50) {
|
2011-08-03 04:14:22 -04:00
|
|
|
$self->{row} = $self->top_row - 1;
|
2011-08-03 04:19:51 -04:00
|
|
|
delete $self->{found};
|
2011-08-03 04:14:22 -04:00
|
|
|
select_next($self, 1);
|
2011-11-04 15:11:59 -04:00
|
|
|
} elsif ($char eq 'G' || $keysym == 0xff57) {
|
2011-08-03 04:14:22 -04:00
|
|
|
$self->{row} = $self->nrow;
|
2011-08-03 04:19:51 -04:00
|
|
|
delete $self->{found};
|
2011-08-03 04:14:22 -04:00
|
|
|
select_next($self, -1);
|
2010-08-02 01:07:10 -04:00
|
|
|
}
|
|
|
|
|
2010-08-13 16:04:04 -04:00
|
|
|
return 1;
|
2010-08-02 01:07:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-24 06:00:08 -05:00
|
|
|
sub on_button_press {
|
2010-08-02 18:44:04 -04:00
|
|
|
my ($self, $event) = @_;
|
2010-08-02 01:07:10 -04:00
|
|
|
|
2010-08-02 18:44:04 -04:00
|
|
|
my $mask = $self->ModLevel3Mask | $self->ModMetaMask |
|
2011-12-24 06:00:08 -05:00
|
|
|
urxvt::ShiftMask | urxvt::ControlMask;
|
2010-08-02 01:07:10 -04:00
|
|
|
|
2011-04-15 01:50:34 -04:00
|
|
|
if ($event->{button} == $self->{button} && ($event->{state} & $mask) == 0) {
|
2011-12-24 06:00:08 -05:00
|
|
|
$self->{button_pressed} = 1;
|
|
|
|
$self->{button_col} = $event->{col};
|
|
|
|
$self->{button_row} = $event->{row};
|
|
|
|
}
|
|
|
|
|
|
|
|
()
|
|
|
|
}
|
|
|
|
|
|
|
|
sub on_button_release {
|
|
|
|
my ($self, $event) = @_;
|
|
|
|
|
|
|
|
if ($self->{button_pressed} && $event->{button} == $self->{button}) {
|
2010-08-02 01:07:10 -04:00
|
|
|
my $col = $event->{col};
|
2011-12-24 06:00:08 -05:00
|
|
|
my $row = $event->{row};
|
|
|
|
|
|
|
|
$self->{button_pressed} = 0;
|
|
|
|
|
|
|
|
if ($col == $self->{button_col} && $row == $self->{button_row}) {
|
|
|
|
my $line = $self->line($row);
|
|
|
|
my $text = $line->t;
|
2010-08-02 01:07:10 -04:00
|
|
|
|
2011-12-24 06:00:08 -05:00
|
|
|
while ($text =~ /$url_matcher/g) {
|
|
|
|
my ($url, $beg, $end) = ($1, $-[0], $+[0]);
|
|
|
|
--$end if $url =~ s/["')]$//;
|
2010-08-02 01:07:10 -04:00
|
|
|
|
2011-12-24 06:00:08 -05:00
|
|
|
if ($col >= $beg && $col <= $end) {
|
|
|
|
$self->exec_async(@{$self->{browser}}, $url);
|
|
|
|
return 1;
|
|
|
|
}
|
2010-08-02 01:07:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sub select_next {
|
2010-08-02 18:44:04 -04:00
|
|
|
# $dir < 0: up, > 0: down
|
|
|
|
my ($self, $dir) = @_;
|
|
|
|
my $row = $self->{row};
|
|
|
|
|
2010-08-16 11:24:27 -04:00
|
|
|
if (($dir < 0 && $self->{n} > 0) ||
|
|
|
|
($dir > 0 && $self->{n} < $#{ $self->{found} })) {
|
2010-08-02 01:07:10 -04:00
|
|
|
# another url on current line
|
2010-08-16 11:24:27 -04:00
|
|
|
$self->{n} += $dir;
|
2010-08-02 18:44:04 -04:00
|
|
|
hilight($self);
|
2010-08-02 01:07:10 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-08-02 18:44:04 -04:00
|
|
|
while (($dir < 0 && $row > $self->top_row) ||
|
|
|
|
($dir > 0 && $row < $self->nrow - 1)) {
|
|
|
|
my $line = $self->line($row);
|
|
|
|
$row = ($dir < 0 ? $line->beg : $line->end) + $dir;
|
|
|
|
$line = $self->line($row);
|
|
|
|
my $text = $line->t;
|
2010-08-02 01:07:10 -04:00
|
|
|
|
2010-08-02 18:44:04 -04:00
|
|
|
if ($text =~ /$url_matcher/g) {
|
|
|
|
delete $self->{found};
|
2010-08-02 01:07:10 -04:00
|
|
|
|
2010-08-02 18:44:04 -04:00
|
|
|
do {
|
|
|
|
my ($beg, $end) = ($-[0], $+[0]);
|
|
|
|
--$end if $& =~ /['")]$/;
|
|
|
|
push @{$self->{found}}, [$line->coord_of($beg),
|
|
|
|
$line->coord_of($end), substr($text, $beg, $end - $beg)];
|
|
|
|
} while ($text =~ /$url_matcher/g);
|
2010-08-02 01:07:10 -04:00
|
|
|
|
2010-08-02 18:44:04 -04:00
|
|
|
$self->{row} = $row;
|
2010-08-16 11:24:27 -04:00
|
|
|
$self->{n} = $dir < 0 ? $#{$self->{found}} : 0;
|
2010-08-02 18:44:04 -04:00
|
|
|
hilight($self);
|
2010-08-02 01:07:10 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-02 18:44:04 -04:00
|
|
|
deactivate($self) unless $self->{found};
|
2010-08-02 01:07:10 -04:00
|
|
|
|
|
|
|
()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-02 18:44:04 -04:00
|
|
|
sub hilight {
|
|
|
|
my ($self) = @_;
|
2010-08-02 01:07:10 -04:00
|
|
|
|
2010-08-02 18:44:04 -04:00
|
|
|
if ($self->{found}) {
|
|
|
|
if ($self->{row} < $self->view_start() ||
|
|
|
|
$self->{row} >= $self->view_start() + $self->nrow) {
|
|
|
|
# scroll selected url into visible area
|
|
|
|
my $top = $self->{row} - ($self->nrow >> 1);
|
|
|
|
$self->view_start($top < 0 ? $top : 0);
|
|
|
|
}
|
2010-08-02 01:07:10 -04:00
|
|
|
|
2010-08-16 11:24:27 -04:00
|
|
|
status_area($self);
|
2010-08-02 18:44:04 -04:00
|
|
|
$self->want_refresh();
|
2010-08-02 01:07:10 -04:00
|
|
|
}
|
|
|
|
|
2010-08-02 18:44:04 -04:00
|
|
|
()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sub refresh {
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
if ($self->{found}) {
|
2010-08-16 11:24:27 -04:00
|
|
|
$self->scr_xor_span(@{$self->{found}[$self->{n}]}[0 .. 3], urxvt::RS_RVid);
|
2010-08-15 09:28:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sub status_area {
|
2010-08-16 11:24:27 -04:00
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
my $row = $self->{row} < 0 ?
|
|
|
|
$self->{row} - $self->top_row : abs($self->top_row) + $self->{row};
|
2010-08-20 14:02:41 -04:00
|
|
|
my $text = sprintf("%d,%d ", $row + 1, $self->{n} + 1);
|
2010-08-16 11:24:27 -04:00
|
|
|
|
|
|
|
if ($self->top_row == 0) {
|
|
|
|
$text .= "All";
|
|
|
|
} elsif ($self->view_start() == $self->top_row) {
|
|
|
|
$text .= "Top";
|
|
|
|
} elsif ($self->view_start() == 0) {
|
|
|
|
$text .= "Bot";
|
|
|
|
} else {
|
|
|
|
$text .= sprintf("%2d%",
|
|
|
|
($self->top_row - $self->view_start) * 100 / $self->top_row);
|
|
|
|
}
|
|
|
|
|
2010-08-20 14:02:41 -04:00
|
|
|
my $text_len = length($text);
|
2010-08-15 09:28:19 -04:00
|
|
|
|
2010-08-16 11:24:27 -04:00
|
|
|
if ($self->{overlay_len} != $text_len) {
|
|
|
|
delete $self->{overlay} if $self->{overlay};
|
|
|
|
$self->{overlay} = $self->overlay(-1, -1, $text_len, 1,
|
2010-08-15 09:28:19 -04:00
|
|
|
urxvt::OVERLAY_RSTYLE, 0);
|
2010-08-16 11:24:27 -04:00
|
|
|
$self->{overlay_len} = $text_len;
|
2010-08-02 18:44:04 -04:00
|
|
|
}
|
2010-08-02 01:07:10 -04:00
|
|
|
|
2010-08-20 14:02:41 -04:00
|
|
|
$self->{overlay}->set(0, 0, $self->special_encode($text));
|
2010-08-15 09:28:19 -04:00
|
|
|
$self->{overlay}->show();
|
|
|
|
|
2010-08-02 01:07:10 -04:00
|
|
|
()
|
|
|
|
}
|
|
|
|
|
2010-08-03 16:15:07 -04:00
|
|
|
|
2010-08-04 05:44:33 -04:00
|
|
|
sub tt_write {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-02 18:44:04 -04:00
|
|
|
sub activate {
|
|
|
|
my ($self) = @_;
|
2010-08-02 01:07:10 -04:00
|
|
|
|
2010-08-02 18:44:04 -04:00
|
|
|
$self->{active} = 1;
|
2010-08-02 01:07:10 -04:00
|
|
|
|
2010-08-02 18:44:04 -04:00
|
|
|
$self->{row} = $self->view_start() + $self->nrow;
|
2010-08-16 11:24:27 -04:00
|
|
|
$self->{n} = 0;
|
|
|
|
$self->{overlay_len} = 0;
|
2011-12-24 06:00:08 -05:00
|
|
|
$self->{button_pressed} = 0;
|
2010-08-15 09:28:19 -04:00
|
|
|
|
2010-08-02 18:44:04 -04:00
|
|
|
$self->{view_start} = $self->view_start();
|
|
|
|
$self->{pty_ev_events} = $self->pty_ev_events(urxvt::EV_NONE);
|
2010-08-02 01:07:10 -04:00
|
|
|
|
2010-08-02 18:44:04 -04:00
|
|
|
$self->enable(
|
|
|
|
key_press => \&key_press,
|
|
|
|
refresh_begin => \&refresh,
|
|
|
|
refresh_end => \&refresh,
|
2010-08-04 05:44:33 -04:00
|
|
|
tt_write => \&tt_write,
|
2010-08-02 18:44:04 -04:00
|
|
|
);
|
2010-08-02 01:07:10 -04:00
|
|
|
|
2010-08-02 18:44:04 -04:00
|
|
|
()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sub deactivate {
|
|
|
|
my ($self) = @_;
|
|
|
|
|
2010-08-04 05:44:33 -04:00
|
|
|
$self->disable("key_press", "refresh_begin", "refresh_end", "tt_write");
|
2010-08-02 18:44:04 -04:00
|
|
|
$self->view_start($self->{view_start});
|
|
|
|
$self->pty_ev_events($self->{pty_ev_events});
|
|
|
|
|
2010-08-15 09:28:19 -04:00
|
|
|
delete $self->{overlay} if $self->{overlay};
|
|
|
|
delete $self->{found} if $self->{found};
|
|
|
|
|
|
|
|
$self->want_refresh();
|
2010-08-02 01:07:10 -04:00
|
|
|
|
2010-08-02 18:44:04 -04:00
|
|
|
$self->{active} = 0;
|
2010-08-02 01:07:10 -04:00
|
|
|
|
|
|
|
()
|
|
|
|
}
|
2011-04-15 01:50:34 -04:00
|
|
|
|