From b398b6d6ad331c2b25fee97376f981adc7121251 Mon Sep 17 00:00:00 2001 From: Sterling Camden Date: Wed, 24 Nov 2010 15:24:48 -0800 Subject: Restrict URL search to marked selection, if there is one. --- README | 32 ++++++++++++++++++-------------- url-picker | 47 ++++++++++++++++++++++++++++++++++------------- 2 files changed, 52 insertions(+), 27 deletions(-) diff --git a/README b/README index 85e9bb8..fc12ca3 100644 --- a/README +++ b/README @@ -32,20 +32,24 @@ function of url-picker. OPERATION -If no URLs are visible on the screen, url-picker just pops up an overlay -window that warns "no URLs found on screen". The overlay disappears -after five seconds, but the terminal is immediately operable. - -If the terminal display contains only one URL, that URL is launched to -the browser. The browser is determined by X resources -url-picker.launcher if defined, otherwise URxvt.urlLauncher. If neither -of these are defined, the 'sensible-browser' is used. The URL will be -passed as the first argument. - -If more than one URL is visible in the terminal window, url-picker places -numbered labels over the first characters of each URL on the screen. A -prompt "Follow: " appears at the bottom of the terminal window, at which -you can type only numbers, backspace, enter, or escape (all other input is +If a portion of the terminal text has been marked for selection, then the +search for URLs will be restricted to that region. Otherwise, the visible +text of the termainl will be used. + +If no URLs are found, url-picker just pops up an overlay window that +warns "no URLs found on visible screen" or "no URLs found in selected +text". The overlay disappears after five seconds, but the terminal is +immediately operable. + +If the region contains only one URL, that URL is launched to the browser. +The browser is determined by X resources url-picker.launcher if defined, +otherwise URxvt.urlLauncher. If neither of these are defined, the +'sensible-browser' is used. The URL will be passed as the first argument. + +If more than one URL available in the region, url-picker places numbered +labels over the first characters of each URL on the screen. A prompt +"Follow: " appears at the bottom of the terminal window, at which you +can type only numbers, backspace, enter, or escape (all other input is ignored). Escape exits this mode and removes all overlay windows, without doing diff --git a/url-picker b/url-picker index 7cb6407..adb1831 100644 --- a/url-picker +++ b/url-picker @@ -20,23 +20,44 @@ sub on_user_command { my $labels = {}; my $hrefs = {}; my $num = 0; - for my $row (0..$self->nrow) { - my $text = $self->ROW_t($row); - while ($text =~ /$url/g) { - my $col = $-[0]; - my $href = $&; - $num++; - my $overlay = $self->overlay( - $col, $row, $self->strwidth($num), 1, urxvt::OVERLAY_RSTYLE, 0 - ); - $overlay->set(0,0,$num); - $labels->{$num} = $overlay; - $hrefs->{$num} = $href; + my $row = 0; + my $base_col = 0; + my $text = ''; + my $label_urls = sub { + while ($text =~ /$url/g) { + my $col = $-[0] + $base_col; + my $href = $&; + $num++; + my $overlay = $self->overlay( + $col, $row, $self->strwidth($num), 1, urxvt::OVERLAY_RSTYLE, 0 + ); + $overlay->set(0,0,$num); + $labels->{$num} = $overlay; + $hrefs->{$num} = $href; + } + }; + my ($brow, $bcol) = $self->selection_beg(); + my ($erow, $ecol) = $self->selection_end(); + my $issel = ($ecol > $bcol) || ($erow > $brow); + if ($issel) { # restrict to selection if one exists + ($row, $base_col) = ($brow, $bcol); + for (split(/\n/, $self->selection())) { + $text = $_; + $label_urls->(); + $base_col = 0; + $row++; + } + } else { # no selection, use visible terminal + for (0..$self->nrow) { + $row = $_; + $text = $self->ROW_t($row); + $label_urls->(); } } if ($num < 1) { - $self->status_msg("url-picker: no URLs found on screen"); + my $desc = $issel ? "in selected text" : "on visible screen"; + $self->status_msg("url-picker: no URLs found $desc"); } else { my $url_picker = {}; $url_picker->{prompt} = $self->overlay( -- cgit v1.2.3