Commits

Anonymous committed d09a349

Restrict URL search to marked selection, if there is one.

Comments (0)

Files changed (2)

 
 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 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 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 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 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 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
     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(