Simple image host.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

url-select 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. #! perl -w
  2. # Author: Bert Muennich
  3. # Website: http://www.github.com/muennich/urxvt-perls
  4. # Based on: http://www.jukie.net/~bart/blog/urxvt-url-yank
  5. # License: GPLv2
  6. # Use keyboard shortcuts to select URLs.
  7. # This should be used as a replacement for the default matcher extension,
  8. # it also makes URLs clickable with the middle mouse button.
  9. # Usage: put the following lines in your .Xdefaults/.Xresources:
  10. # URxvt.perl-ext-common: ...,url-select
  11. # URxvt.keysym.M-u: perl:url-select:select_next
  12. # Use Meta-u to activate URL selection mode, then use the following keys:
  13. # j/k: Select next downward/upward URL (also with arrow keys)
  14. # g/G: Select first/last URL (also with home/end key)
  15. # o/Return: Open selected URL in browser, Return: deactivate afterwards
  16. # y: Copy (yank) selected URL and deactivate selection mode
  17. # q/Escape: Deactivate URL selection mode
  18. # Options:
  19. # URxvt.url-select.autocopy: If true, selected URLs are copied to PRIMARY
  20. # URvxt.url-select.button: Mouse button to click-open URLs (default: 2)
  21. # URxvt.url-select.launcher: Browser/command to open selected URL with
  22. # URxvt.url-select.underline: If set to true, all URLs get underlined
  23. use strict;
  24. # The custom rendition bit to use for marking the cell as being underlined
  25. # by us so we can unset it again after a line has changed.
  26. use constant UNDERLINED => 1<<3; # arbitrarily chosen in hope of no collision
  27. sub on_start {
  28. my ($self) = @_;
  29. # read resource settings
  30. if ($self->x_resource('url-select.launcher')) {
  31. @{$self->{browser}} = split /\s+/, $self->x_resource('url-select.launcher');
  32. } else {
  33. @{$self->{browser}} = ('x-www-browser');
  34. }
  35. if ($self->x_resource('url-select.underline') eq 'true') {
  36. $self->enable(line_update => \&line_update);
  37. }
  38. if ($self->x_resource('url-select.autocopy') eq 'true') {
  39. $self->{autocopy} = 1;
  40. }
  41. $self->{state} = 0;
  42. for my $mod (split '', $self->x_resource("url-select.button") ||
  43. $self->x_resource("matcher.button") || 2) {
  44. if ($mod =~ /^\d+$/) {
  45. $self->{button} = $mod;
  46. } elsif ($mod eq "C") {
  47. $self->{state} |= urxvt::ControlMask;
  48. } elsif ($mod eq "S") {
  49. $self->{state} |= urxvt::ShiftMask;
  50. } elsif ($mod eq "M") {
  51. $self->{state} |= $self->ModMetaMask;
  52. } elsif ($mod ne "-" && $mod ne " ") {
  53. warn("invalid button/modifier in $self->{_name}<$self->{argv}[0]>: $mod\n");
  54. }
  55. }
  56. if ($self->x_resource('matcher.pattern')) {
  57. @{$self->{pattern}} = ($self->x_resource('matcher.pattern'));
  58. } elsif ($self->x_resource('matcher.pattern.0')) {
  59. my $current = 0;
  60. while (defined (my $res = $self->x_resource("matcher.pattern.$current"))) {
  61. $res = $self->locale_decode($res);
  62. utf8::encode $res;
  63. push @{$self->{pattern}}, qr($res)x;
  64. $current++;
  65. }
  66. } else {
  67. @{$self->{pattern}} = qr{
  68. (?:https?://|ftp://|news://|mailto:|file://|\bwww\.)
  69. [\w\-\@;\/?:&=%\$.+!*\x27,~#]*
  70. (
  71. \([\w\-\@;\/?:&=%\$.+!*\x27,~#]*\) # Allow a pair of matched parentheses
  72. | #
  73. [\w\-\@;\/?:&=%\$+*~] # exclude some trailing characters (heuristic)
  74. )+
  75. }x;
  76. }
  77. ()
  78. }
  79. sub line_update {
  80. my ($self, $row) = @_;
  81. my $line = $self->line($row);
  82. my $text = $line->t;
  83. my $rend = $line->r;
  84. # clear all underlines that were set by us
  85. for (@$rend) {
  86. if (urxvt::GET_CUSTOM($_) & UNDERLINED) {
  87. $_ = urxvt::SET_CUSTOM($_, urxvt::GET_CUSTOM($_) & ~UNDERLINED) &
  88. ~urxvt::RS_Uline;
  89. }
  90. }
  91. for my $pattern (@{$self->{pattern}}) {
  92. while ($text =~ /$pattern/g) {
  93. my $url = $&;
  94. my ($beg, $end) = ($-[0], $+[0] - 1);
  95. for (@{$rend}[$beg .. $end]) {
  96. unless ($_ & urxvt::RS_Uline) {
  97. $_ = urxvt::SET_CUSTOM($_, urxvt::GET_CUSTOM($_) | UNDERLINED);
  98. $_ |= urxvt::RS_Uline;
  99. }
  100. }
  101. }
  102. }
  103. $line->r($rend);
  104. ()
  105. }
  106. sub on_action {
  107. my ($self, $action) = @_;
  108. on_user_command($self, "url-select:" . $action);
  109. }
  110. sub on_user_command {
  111. my ($self, $cmd) = @_;
  112. if ($cmd eq 'url-select:select_next') {
  113. if (not $self->{active}) {
  114. activate($self);
  115. }
  116. select_next($self, -1);
  117. }
  118. ()
  119. }
  120. sub key_press {
  121. my ($self, $event, $keysym) = @_;
  122. my $char = chr($keysym);
  123. if ($keysym == 0xff1b || lc($char) eq 'q' ||
  124. (lc($char) eq 'c' && $event->{state} & urxvt::ControlMask)) {
  125. deactivate($self);
  126. } elsif ($keysym == 0xff0d || $char eq 'o') {
  127. $self->exec_async(@{$self->{browser}}, ${$self->{found}[$self->{n}]}[4]);
  128. deactivate($self) unless $char eq 'o';
  129. } elsif ($char eq 'y') {
  130. my $found = $self->{found}[$self->{n}];
  131. $self->selection_beg(${$found}[0], ${$found}[1]);
  132. $self->selection_end(${$found}[2], ${$found}[3]);
  133. $self->selection_make($event->{time});
  134. $self->selection_beg(1, 0);
  135. $self->selection_end(1, 0);
  136. deactivate($self);
  137. } elsif ($char eq 'k' || $keysym == 0xff52 || $keysym == 0xff51) {
  138. select_next($self, -1, $event);
  139. } elsif ($char eq 'j' || $keysym == 0xff54 || $keysym == 0xff53) {
  140. select_next($self, 1, $event);
  141. } elsif ($char eq 'g' || $keysym == 0xff50) {
  142. $self->{row} = $self->top_row - 1;
  143. delete $self->{found};
  144. select_next($self, 1, $event);
  145. } elsif ($char eq 'G' || $keysym == 0xff57) {
  146. $self->{row} = $self->nrow;
  147. delete $self->{found};
  148. select_next($self, -1, $event);
  149. }
  150. return 1;
  151. }
  152. sub on_button_press {
  153. my ($self, $event) = @_;
  154. my $mask = $self->ModLevel3Mask | $self->ModMetaMask |
  155. urxvt::ShiftMask | urxvt::ControlMask;
  156. if ($event->{button} == $self->{button} && ($event->{state} & $mask) == $self->{state}) {
  157. my $col = $event->{col};
  158. my $row = $event->{row};
  159. my $line = $self->line($row);
  160. my $text = $line->t;
  161. for my $pattern (@{$self->{pattern}}) {
  162. while ($text =~ /$pattern/g) {
  163. my ($url, $beg, $end) = ($&, $-[0], $+[0]);
  164. --$end if $url =~ s/["')]$//;
  165. if ($col >= $beg && $col <= $end) {
  166. $self->{button_pressed} = 1;
  167. $self->{button_col} = $col;
  168. $self->{button_row} = $row;
  169. $self->{button_url} = $url;
  170. return 1;
  171. }
  172. }
  173. }
  174. }
  175. ()
  176. }
  177. sub on_button_release {
  178. my ($self, $event) = @_;
  179. if ($self->{button_pressed} && $event->{button} == $self->{button}) {
  180. my $col = $event->{col};
  181. my $row = $event->{row};
  182. $self->{button_pressed} = 0;
  183. if ($col == $self->{button_col} && $row == $self->{button_row}) {
  184. $self->exec_async(@{$self->{browser}}, $self->{button_url});
  185. return 1;
  186. }
  187. }
  188. ()
  189. }
  190. sub select_next {
  191. # $dir < 0: up, > 0: down
  192. my ($self, $dir, $event) = @_;
  193. my $row = $self->{row};
  194. if (($dir < 0 && $self->{n} > 0) ||
  195. ($dir > 0 && $self->{n} < $#{ $self->{found} })) {
  196. # another url on current line
  197. $self->{n} += $dir;
  198. hilight($self);
  199. if ($self->{autocopy}) {
  200. my $found = $self->{found}[$self->{n}];
  201. $self->selection_beg(${$found}[0], ${$found}[1]);
  202. $self->selection_end(${$found}[2], ${$found}[3]);
  203. $self->selection_make($event->{time});
  204. $self->selection_beg(1, 0);
  205. $self->selection_end(1, 0);
  206. }
  207. return;
  208. }
  209. while (($dir < 0 && $row > $self->top_row) ||
  210. ($dir > 0 && $row < $self->nrow - 1)) {
  211. my $line = $self->line($row);
  212. $row = ($dir < 0 ? $line->beg : $line->end) + $dir;
  213. $line = $self->line($row);
  214. my $text = $line->t;
  215. for my $pattern (@{$self->{pattern}}) {
  216. if ($text =~ /$pattern/g) {
  217. delete $self->{found};
  218. do {
  219. my ($beg, $end) = ($-[0], $+[0]);
  220. push @{$self->{found}}, [$line->coord_of($beg),
  221. $line->coord_of($end), substr($text, $beg, $end - $beg)];
  222. } while ($text =~ /$pattern/g);
  223. $self->{row} = $row;
  224. $self->{n} = $dir < 0 ? $#{$self->{found}} : 0;
  225. hilight($self);
  226. if ($self->{autocopy}) {
  227. my $found = $self->{found}[$self->{n}];
  228. $self->selection_beg(${$found}[0], ${$found}[1]);
  229. $self->selection_end(${$found}[2], ${$found}[3]);
  230. $self->selection_make($event->{time});
  231. $self->selection_beg(1, 0);
  232. $self->selection_end(1, 0);
  233. }
  234. return;
  235. }
  236. }
  237. }
  238. deactivate($self) unless $self->{found};
  239. ()
  240. }
  241. sub hilight {
  242. my ($self) = @_;
  243. if ($self->{found}) {
  244. if ($self->{row} < $self->view_start() ||
  245. $self->{row} >= $self->view_start() + $self->nrow) {
  246. # scroll selected url into visible area
  247. my $top = $self->{row} - ($self->nrow >> 1);
  248. $self->view_start($top < 0 ? $top : 0);
  249. }
  250. status_area($self);
  251. $self->want_refresh();
  252. }
  253. ()
  254. }
  255. sub refresh {
  256. my ($self) = @_;
  257. if ($self->{found}) {
  258. $self->scr_xor_span(@{$self->{found}[$self->{n}]}[0 .. 3], urxvt::RS_RVid);
  259. }
  260. ()
  261. }
  262. sub status_area {
  263. my ($self) = @_;
  264. my $row = $self->{row} < 0 ?
  265. $self->{row} - $self->top_row : abs($self->top_row) + $self->{row};
  266. my $text = sprintf("%d,%d ", $row + 1, $self->{n} + 1);
  267. if ($self->top_row == 0) {
  268. $text .= "All";
  269. } elsif ($self->view_start() == $self->top_row) {
  270. $text .= "Top";
  271. } elsif ($self->view_start() == 0) {
  272. $text .= "Bot";
  273. } else {
  274. $text .= sprintf("%2d%",
  275. ($self->top_row - $self->view_start) * 100 / $self->top_row);
  276. }
  277. my $text_len = length($text);
  278. if ($self->{overlay_len} != $text_len) {
  279. delete $self->{overlay} if $self->{overlay};
  280. $self->{overlay} = $self->overlay(-1, -1, $text_len, 1,
  281. urxvt::OVERLAY_RSTYLE, 0);
  282. $self->{overlay_len} = $text_len;
  283. }
  284. $self->{overlay}->set(0, 0, $self->special_encode($text));
  285. $self->{overlay}->show();
  286. ()
  287. }
  288. sub tt_write {
  289. return 1;
  290. }
  291. sub activate {
  292. my ($self) = @_;
  293. $self->{active} = 1;
  294. $self->{row} = $self->view_start() + $self->nrow;
  295. $self->{n} = 0;
  296. $self->{overlay_len} = 0;
  297. $self->{button_pressed} = 0;
  298. $self->{view_start} = $self->view_start();
  299. $self->{pty_ev_events} = $self->pty_ev_events(urxvt::EV_NONE);
  300. $self->enable(
  301. key_press => \&key_press,
  302. refresh_begin => \&refresh,
  303. refresh_end => \&refresh,
  304. tt_write => \&tt_write,
  305. );
  306. ()
  307. }
  308. sub deactivate {
  309. my ($self) = @_;
  310. $self->disable("key_press", "refresh_begin", "refresh_end", "tt_write");
  311. $self->view_start($self->{view_start});
  312. $self->pty_ev_events($self->{pty_ev_events});
  313. delete $self->{overlay} if $self->{overlay};
  314. delete $self->{found} if $self->{found};
  315. $self->want_refresh();
  316. $self->{active} = 0;
  317. ()
  318. }