Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mark-yank-url: Generic url support #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 40 additions & 15 deletions mark-yank-urls
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,37 @@ use strict;
use warnings;

my $url_matcher = qr{(
(?:https?://|ftp://|news://|mailto:|file://|www\.)[ab-zA-Z0-9\-\@;\/?:&=%\$_.+!*\x27(),~#]+
[ab-zA-Z0-9\-\@;\/?&=%\$_+!*\x27()~] # exclude some trailing characters (heuristic)
)}x;
\b
(?:
[a-z][a-z0-9+]+:// | # scheme
www\. # or www. subdomain
)
[\[\]a-z0-9\@:;.-]* # hostname
(?:
/[a-z0-9.,:;~\@?\$\&()/=#%+!*_-]* # path (and query string etc)
)?
(?<![.,;:?!]) # exclude some trailing characters (heuristic)
)}xi;

my %handlers = (
http => 'x-www-browser',
https => 'x-www-browser',
ftp => 'x-www-browser',
);

sub scheme {
my $uri = shift;
my ($scheme) = $uri =~ m|^([^:]+)://|;
return $scheme;
}

sub get_handler {
my $uri = shift;
my $scheme = scheme($uri);

$scheme = 'http' if not $scheme and $uri =~ /^www\./;
return $handlers{$scheme};
}

sub on_start {
my ($term) = @_;
Expand All @@ -20,15 +47,11 @@ sub on_start {
import Clipboard;
}

eval { require Regexp::Common::URI };
if(!$@) {
require Regexp::Common;
Regexp::Common->import('URI');

$url_matcher = $Regexp::Common::RE{URI}{HTTP};
}

$term->{browser} = $term->x_resource ("urlLauncher") || "x-www-browser";
if (my $browser = $term->x_resource ('urlLauncher')) {
$handlers{http} = $browser;
$handlers{https} = $browser;
$handlers{ftp} = $browser;
}

()
}
Expand Down Expand Up @@ -77,10 +100,11 @@ sub on_button_release {

while ($text =~ /($url_matcher)/g) {
my ($url, $first, $last) = ($1, $-[1], $+[1]);
my $handler = get_handler($url);

if($first <= $col && $last >= $col) {
if($handler and $first <= $col && $last >= $col) {
$url =~ s/["']$//;
$term->exec_async($term->{browser}, $url);
$term->exec_async($handler, $url);
return 1;
}
}
Expand Down Expand Up @@ -171,7 +195,8 @@ sub on_key_release {

} elsif ($keysym == 65293) { # <enter>
my $url = get_active_url($term);
$term->exec_async($term->{browser}, $url);
my $handler = get_handler($url);
$term->exec_async($handler, $url) if $handler;
deactivate_mark_mode ($term);
return 1;

Expand Down