302 lines
6.3 KiB
Perl
302 lines
6.3 KiB
Perl
#! perl -w
|
|
# Author: Bert Muennich
|
|
# Website: http://www.github.com/muennich/urxvt-perls
|
|
# Based on: http://www.jukie.net/~bart/blog/urxvt-url-yank
|
|
# Version: git-20100820
|
|
# 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.
|
|
|
|
# Usage: put the following lines in your .Xdefaults:
|
|
# URxvt.perl-ext-common: ...,url-select
|
|
# URxvt.keysym.M-u: perl:url-select:select_next
|
|
|
|
# Use Meta-u to activate URL selection mode, then use the following keys:
|
|
# k: Select next upward URL (also with Meta-u)
|
|
# j: Select next downward URL
|
|
# 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
|
|
|
|
# Options:
|
|
# URxvt.urlLauncher: Browser/command to open selected URL with
|
|
# URxvt.underlineURLs: If set to true, all URLs get underlined
|
|
|
|
|
|
use strict;
|
|
|
|
my $url_matcher = qr{(
|
|
(?:https?://|ftp://|news://|mailto:|file://|www\.)
|
|
[ab-zA-Z0-9\-\@;\/?:&=%\$_.+!*\x27(),~#]+[ab-zA-Z0-9\-\@;\/?&=%\$_+!*\x27()~]
|
|
)}x;
|
|
|
|
|
|
sub on_start {
|
|
my ($self) = @_;
|
|
|
|
eval { require Regexp::Common::URI };
|
|
if(!$@) {
|
|
require Regexp::Common;
|
|
Regexp::Common->import('URI');
|
|
$url_matcher = $Regexp::Common::RE{URI}{HTTP};
|
|
}
|
|
|
|
# read resource settings
|
|
$self->{browser} = $self->x_resource('urlLauncher') || 'x-www-browser';
|
|
if ($self->x_resource('underlineURLs') eq 'true') {
|
|
$self->enable(line_update => \&line_update);
|
|
}
|
|
|
|
()
|
|
}
|
|
|
|
|
|
sub line_update {
|
|
my ($self, $row) = @_;
|
|
|
|
my $line = $self->line($row);
|
|
my $text = $line->t;
|
|
my $rend = $line->r;
|
|
|
|
while ($text =~ /$url_matcher/g) {
|
|
my $url = $1;
|
|
my ($beg, $end) = ($-[1], $+[1] - 1);
|
|
--$end if $url =~ /["')]$/;
|
|
|
|
for (@{$rend}[$beg .. $end]) {
|
|
$_ |= urxvt::RS_Uline;
|
|
}
|
|
$line->r($rend);
|
|
}
|
|
|
|
()
|
|
}
|
|
|
|
|
|
sub on_user_command {
|
|
my ($self, $cmd) = @_;
|
|
|
|
if ($cmd eq 'url-select:select_next') {
|
|
if (not $self->{active}) {
|
|
activate($self);
|
|
}
|
|
select_next($self, -1);
|
|
}
|
|
|
|
()
|
|
}
|
|
|
|
|
|
sub key_press {
|
|
my ($self, $event, $keysym) = @_;
|
|
my $char = chr($keysym);
|
|
|
|
if ($keysym == 0xff1b || lc($char) eq 'q') {
|
|
deactivate($self);
|
|
} elsif ($keysym == 0xff0d || $char eq 'o') {
|
|
$self->exec_async($self->{browser}, ${$self->{found}[$self->{n}]}[4]);
|
|
deactivate($self) unless $char eq 'o';
|
|
} elsif ($char eq 'y') {
|
|
$self->selection(${$self->{found}[$self->{n}]}[4]);
|
|
$self->selection_grab($event->{time});
|
|
deactivate($self);
|
|
} elsif ($char eq 'k') {
|
|
select_next($self, -1);
|
|
} elsif ($char eq 'j') {
|
|
select_next($self, 1);
|
|
}
|
|
|
|
return 1;
|
|
}
|
|
|
|
|
|
sub on_button_release {
|
|
my ($self, $event) = @_;
|
|
|
|
if ($self->{active}) {
|
|
if ($event->{button} == 4 || $event->{button} == 5) {
|
|
return;
|
|
} else {
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
my $mask = $self->ModLevel3Mask | $self->ModMetaMask |
|
|
urxvt::ShiftMask | urxvt::ControlMask;
|
|
|
|
if ($event->{button} == 2 && ($event->{state} & $mask) == 0) {
|
|
my $col = $event->{col};
|
|
my $line = $self->line($event->{row});
|
|
my $text = $line->t;
|
|
|
|
while ($text =~ /$url_matcher/g) {
|
|
my ($url, $beg, $end) = ($1, $-[0], $+[0]);
|
|
|
|
if ($url =~ s/["')]$//) {
|
|
--$end;
|
|
}
|
|
if ($col >= $beg && $col <= $end) {
|
|
$self->exec_async($self->{browser}, $url);
|
|
return 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
()
|
|
}
|
|
|
|
|
|
sub select_next {
|
|
# $dir < 0: up, > 0: down
|
|
my ($self, $dir) = @_;
|
|
my $row = $self->{row};
|
|
|
|
if (($dir < 0 && $self->{n} > 0) ||
|
|
($dir > 0 && $self->{n} < $#{ $self->{found} })) {
|
|
# another url on current line
|
|
$self->{n} += $dir;
|
|
hilight($self);
|
|
return;
|
|
}
|
|
|
|
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;
|
|
|
|
if ($text =~ /$url_matcher/g) {
|
|
delete $self->{found};
|
|
|
|
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);
|
|
|
|
$self->{row} = $row;
|
|
$self->{n} = $dir < 0 ? $#{$self->{found}} : 0;
|
|
hilight($self);
|
|
return;
|
|
}
|
|
}
|
|
|
|
deactivate($self) unless $self->{found};
|
|
|
|
()
|
|
}
|
|
|
|
|
|
sub hilight {
|
|
my ($self) = @_;
|
|
|
|
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);
|
|
}
|
|
|
|
status_area($self);
|
|
$self->want_refresh();
|
|
}
|
|
|
|
()
|
|
}
|
|
|
|
|
|
sub refresh {
|
|
my ($self) = @_;
|
|
|
|
if ($self->{found}) {
|
|
$self->scr_xor_span(@{$self->{found}[$self->{n}]}[0 .. 3], urxvt::RS_RVid);
|
|
}
|
|
|
|
()
|
|
}
|
|
|
|
|
|
sub status_area {
|
|
my ($self) = @_;
|
|
|
|
my $row = $self->{row} < 0 ?
|
|
$self->{row} - $self->top_row : abs($self->top_row) + $self->{row};
|
|
my $text = sprintf("%d,%d ", $row + 1, $self->{n} + 1);
|
|
|
|
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);
|
|
}
|
|
|
|
my $text_len = length($text);
|
|
|
|
if ($self->{overlay_len} != $text_len) {
|
|
delete $self->{overlay} if $self->{overlay};
|
|
$self->{overlay} = $self->overlay(-1, -1, $text_len, 1,
|
|
urxvt::OVERLAY_RSTYLE, 0);
|
|
$self->{overlay_len} = $text_len;
|
|
}
|
|
|
|
$self->{overlay}->set(0, 0, $self->special_encode($text));
|
|
$self->{overlay}->show();
|
|
|
|
()
|
|
}
|
|
|
|
|
|
sub tt_write {
|
|
return 1;
|
|
}
|
|
|
|
|
|
sub activate {
|
|
my ($self) = @_;
|
|
|
|
$self->{active} = 1;
|
|
|
|
$self->{row} = $self->view_start() + $self->nrow;
|
|
$self->{n} = 0;
|
|
$self->{overlay_len} = 0;
|
|
|
|
$self->{view_start} = $self->view_start();
|
|
$self->{pty_ev_events} = $self->pty_ev_events(urxvt::EV_NONE);
|
|
|
|
$self->enable(
|
|
key_press => \&key_press,
|
|
refresh_begin => \&refresh,
|
|
refresh_end => \&refresh,
|
|
tt_write => \&tt_write,
|
|
);
|
|
|
|
()
|
|
}
|
|
|
|
|
|
sub deactivate {
|
|
my ($self) = @_;
|
|
|
|
$self->disable("key_press", "refresh_begin", "refresh_end", "tt_write");
|
|
$self->view_start($self->{view_start});
|
|
$self->pty_ev_events($self->{pty_ev_events});
|
|
|
|
delete $self->{overlay} if $self->{overlay};
|
|
delete $self->{found} if $self->{found};
|
|
|
|
$self->want_refresh();
|
|
|
|
$self->{active} = 0;
|
|
|
|
()
|
|
}
|