forked from bartman/urxvt-scripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mark-yank-urls
301 lines (211 loc) · 6.43 KB
/
mark-yank-urls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/usr/bin/perl
# Author: Bart Trojanowski <[email protected]>
# Website: http://www.jukie.net/~bart/blog/urxvt-url-yank
# License: GPLv2
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;
sub on_start {
my ($term) = @_;
$term->{have_Clipboard} = eval { require Clipboard; };
if ($term->{have_Clipboard}) {
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";
()
}
sub on_line_update {
my ($term, $row) = @_;
# Fetch the line that has changed.
my $line = $term->line($row);
my $text = $line->t;
# Find and underline URLs.
while ($text =~ /($url_matcher)/g) {
my $url = $1;
my $rend = $line->r;
# Mark all characters as underlined. we _must_ not toggle underline, as
# we might get called on an already-marked URL.
my $underlineURLs = $term->x_resource ('underlineURLs') || 'false';
if($underlineURLs eq 'true') {
my ($first, $last) = ($-[1], $+[1] - 1);
--$last if $url =~ s/["']$//;
$_ |= urxvt::RS_Uline for @{$rend}[$first .. $last];
$line->r($rend);
}
}
()
}
sub on_button_release {
my ($term, $event) = @_;
my $mask = $term->ModLevel3Mask | $term->ModMetaMask
| urxvt::ShiftMask | urxvt::ControlMask;
if ($event->{button} == 2 && ($event->{state} & $mask) == 0) {
my $row = $event->{row};
my $col = $event->{col};
my $line = $term->line ($row);
my $text = $line->t;
while ($text =~ /($url_matcher)/g) {
my ($url, $first, $last) = ($1, $-[1], $+[1]);
if($first <= $col && $last >= $col) {
$url =~ s/["']$//;
$term->exec_async($term->{browser}, $url);
return 1;
}
}
}
()
}
my $mark_mode_active = 0;
my %mod = ( 'control' => 0, 'shift' => 0 );
my $url_selected = -1;
my @url_db = ();
sub do_scan_for_urls {
my ($term) = @_;
@url_db = ();
my $row_start = $term->top_row;
my $row_end = $term->nrow;
for my $row ($row_start .. $row_end) {
# Fetch the line that has changed.
my $line = $term->line ($row);
my $text = $line->t;
# Find all urls (if any).
while ($text =~ /($url_matcher)/g) {
my $rend = $line->r;
my ($url, $first, $last) = ($1, $-[1], $+[1] - 1);
--$last if $url =~ s/["']$//;
my %h = (
row => $row,
col_from => $first,
col_to => $last,
url => $url,
);
push @url_db, \%h;
}
}
# 0 for none, positive count otherwise
return $#url_db + 1;
}
sub on_user_command {
my ($term, $cmd) = @_;
activate_mark_mode($term) if $cmd eq 'mark-yank-urls:activate_mark_mode';
()
}
sub on_key_press {
my ($term, $event, $keysym, $octets) = @_;
if ($keysym == 65507) { # <control>
$mod{control} = 1;
} elsif ($keysym == 65505) { # <shift>
$mod{shift} = 1;
}
# Ignore all input when we are active.
$mark_mode_active && return 1;
()
}
sub on_key_release {
my ($term, $event, $keysym, $octets) = @_;
if ($mark_mode_active) {
my $ch = chr($keysym);
if ($keysym == 65307) { # <esc>
deactivate_mark_mode ($term);
return 1;
} elsif ($keysym == 65293) { # <enter>
my $url = get_active_url($term);
$term->exec_async($term->{browser}, $url);
deactivate_mark_mode ($term);
return 1;
} elsif ($keysym == 65507) { # <control>
$mod{control} = 0;
return 1;
} elsif ($keysym == 65505) { # <shift>
$mod{shift} = 0;
return 1;
} elsif ($mod{control} && (($ch eq 'n') || ($ch eq 'p'))) {
# ^n and ^p to cycle list
my $dir = ($ch eq 'n') ? 1 : -1;
move_highlight ($term, $dir);
} elsif ($ch eq 'y') { # y
do_copy ($term);
deactivate_mark_mode ($term);
return 1;
}
return 1;
}
()
}
sub get_active_url {
my ($term) = @_;
my $max = $#url_db + 1;
return if $url_selected < 0 || $url_selected >= $max;
return if not defined $url_db[$url_selected];
my $o = $url_db[$url_selected];
my %h = %$o;
return $h{url};
}
sub do_copy {
my ($term) = @_;
my $text = get_active_url ($term);
if ($term->{have_Clipboard}) {
Clipboard->copy($text);
} else {
$text =~ s/\(["|><&()]\)/\\$1/;
system ("echo -n \"$text\" | xclip -i");
}
}
sub move_highlight {
my ($term, $dir) = @_;
my $max = $#url_db + 1;
do_highlight ($term, 0);
$url_selected = ($max + $url_selected + $dir) % $max;
do_highlight ($term, 1);
$term->want_refresh;
}
sub do_highlight {
my ($term, $enable) = @_;
my $max = $#url_db + 1;
return if $url_selected < 0 || $url_selected >= $max;
return if not defined $url_db[$url_selected];
my $o = $url_db[$url_selected];
my %h = %$o;
my $row = $h{row};
my $line = $term->line ($row);
my $text = $line->t;
my $rend = $line->r;
if ($enable) {
$_ |= urxvt::RS_RVid
for @{$rend}[ $h{col_from} .. $h{col_to}];
# make it visible
$term->view_start ( $row < 0 ? $row : 0 );
} else {
$_ &= ~urxvt::RS_RVid
for @{$rend}[ $h{col_from} .. $h{col_to}];
}
$line->r ($rend);
}
sub activate_mark_mode {
my ($term) = @_;
if ($mark_mode_active) {
move_highlight ($term, -1);
} elsif ( do_scan_for_urls ($term) ) {
$term->{save_view_start} = $term->view_start;
move_highlight ($term, 0);
$mark_mode_active=1 if ($url_selected > -1);
}
}
sub deactivate_mark_mode {
my ($term) = @_;
do_highlight ($term, 0);
$mark_mode_active = 0;
$url_selected = -1;
$term->view_start ($term->{save_view_start});
$term->want_refresh;
}
# vim: set et ts=4 sw=4: