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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
|
#! perl
# ------------------------------------------------------------------------
# Author: Bart Trojanowski <bart@jukie.net>
#
# This script intends to provide functionality similar to screen's
# copy-mode, but instead copy to the X clipboard. In order to copy to
# the clipboard we need either the Clipboard.pm from CPAN or the xclip
# command line utility.
#
# More details here: http://www.jukie.net/~bart/blog/tag/urxvt
#
# This script is based on the mark-urls script from the rxvt-unicode
# distribution.
#
# ------------------------------------------------------------------------
# configuration
#
# Put this in your .Xdefaults
#
# URxvt.keysym.M-y: perl:mark-and-yank:activate_mark_mode
# URxvt.keysym.M-u: perl:mark-and-yank:activate_mark_url_mode
# URxvt.perl-lib: /home/jukie/bart/.urxvt/
# URxvt.perl-ext: mark-and-yank
# URxvt.urlLauncher: firefox
#
# you might have to edit the perl-lib line.
#
# ------------------------------------------------------------------------
use List::Util qw(first max maxstr min minstr reduce shuffle sum);
# same url as used in "selection"
my $url_matcher =
qr{(
(?:https?://|ftp://|news://|mailto:|file://)[ab-zA-Z0-9\-\@;\/?:&=%\$_.+!*\x27(),~#]+
[ab-zA-Z0-9\-\@;\/?:&=%\$_+!*\x27()~] # exclude some trailing characters (heuristic)
)}x;
sub canonicalise_coordinates {
if ($_[0] > $_[2] || ($_[0] == $_[2] && $_[1] > $_[3])) {
(@_[2,3],@_[0,1])
} else {
(@_);
}
}
sub clamp { min( max( $_[0], $_[1] ), $_[2] ) }
sub on_start {
my ($term) = @_;
$term->{have_Clipboard} = eval { require Clipboard; };
if ($term->{have_Clipboard}) {
import Clipboard;
}
$term->{browser} = $term->x_resource ("urlLauncher") || "x-www-browser";
()
}
sub open_url {
my ($term, $url) = @_;
$term->exec_async (split(/[[:blank:]]/, $term->{browser}), $url);
}
sub on_line_update {
my ($term, $row) = @_;
return unless ($term->x_resource ("underlineURLs") eq "true");
# 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;
# mark all characters as underlined. we _must_ not toggle underline,
# as we might get called on an already-marked url.
$_ |= urxvt::RS_Uline
for @{$rend}[ $-[1] .. $+[1] - 1];
$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) {
if ($-[1] <= $col && $+[1] >= $col) {
open_url($term, $1);
return 1;
}
}
}
()
}
# ------------------------------------------------------------------------
my %key2mod = (
65505 => urxvt::ShiftMask,
65507 => urxvt::ControlMask,
65513 => urxvt::Mod1Mask, # Alt
65514 => urxvt::Mod1Mask, # Alt
65515 => urxvt::Mod4Mask, # Super
65516 => urxvt::Mod4Mask, # Super
);
my $mod = 0;
#my %mod = ( 'control' => 0, 'shift' => 0 );
my $mark_mode_active = 0;
my @backup_cursor = ();
my @visual_start = ();
my $visual_mode = 0; # 'v', 'V', or '^v'
my @cursor = ();
my $url_selected = -1;
my @url_db = ();
my $first_mark_set = 0;
my $msg_timeout = 2;
# ------------------------------------------------------------------------
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<=$row_end; $row++) {
# 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 $url = $1;
my %h = ( 'row' => $row,
'col_from' => $-[1],
'col_to' => $+[1] - 1,
'url' => $url);
push @url_db, \%h;
}
}
# 0 for none, positive count otherwise
return $#url_db + 1;
}
sub status_message {
my ($self, $text, $timeout) = @_;
$timeout = $msg_timeout unless defined $timeout;
$self->{msg} = {
ov => $self->overlay (0, -1, length($text)+1, 1, urxvt::OVERLAY_RSTYLE, 1),
to => urxvt::timer
->new
->start (urxvt::NOW + $timeout)
->cb (sub {
delete $self->{msg};
}),
};
$self->{msg}{ov}->set(0,0,$text);
}
sub on_user_command {
my ($term, $cmd) = @_;
if ($cmd eq "mark-and-yank:activate_mark_mode") {
activate_mark_mode($term);
} elsif ($cmd eq "mark-and-yank:activate_mark_url_mode") {
activate_mark_url_mode($term);
}
status_message ($term, "urxvt copy mode started");
()
}
# ------------------------------------------------------------------------
sub on_key_press {
my ($term, $event, $keysym, $octets) = @_;
foreach my $key (keys %key2mod) {
if ($keysym == $key) {
$mod |= $key2mod{$key};
return ();
}
}
# ignore all input when we are active
$mark_mode_active && return 1;
()
}
sub on_key_release {
my ($term, $event, $keysym) = @_;
foreach my $key (keys %key2mod) {
if ($keysym == $key) {
$mod &= ~$key2mod{$key};
return ();
}
}
return () unless ($mark_mode_active);
my $ch = chr($keysym);
if ($mod & urxvt::ShiftMask && $ch =~ m/[[:alpha:]]/) {
$ch = uc $ch;
$mod &= ~urxvt::ShiftMask;
}
if (!$mod && $keysym == 65307) { # <esc>
deactivate_mark_mode ($term);
visual_mode_disable ($term, @cursor);
} elsif (($mod & urxvt::ControlMask) && $ch eq 'c') {# ^c - abort
deactivate_mark_mode ($term);
visual_mode_disable ($term, @cursor);
} elsif (!$mod && $keysym == 65293) { # <enter>
if ($first_mark_set) {
do_copy($term, @visual_start, @cursor);
deactivate_mark_mode ($term);
visual_mode_disable ($term, @cursor);
} else {
my %url = get_active_url($term);
if (not %url) {
$first_mark_set = 1;
visual_mode_enable ($term, 'v', @cursor);
} else {
my $urltext = $url{url};
$urltext =~ s/\(["|><&()]\)/\\$1/;
open_url($term, $urltext);
deactivate_mark_mode ($term);
visual_mode_disable ($term, @cursor);
}
}
} elsif (!$mod && $keysym == 32) { # <space>
if ($first_mark_set) {
do_copy($term, @visual_start, @cursor);
deactivate_mark_mode ($term);
visual_mode_disable ($term, @cursor);
} else {
$first_mark_set = 1;
visual_mode_enable ($term, 'v', @cursor);
}
} elsif (!$mod && $ch eq 'o') { # o - go to other end of region
if ($first_mark_set) {
my @dest = @visual_start;
@visual_start = @cursor;
@cursor = @dest;
$term->screen_cur (@dest);
$term->want_refresh;
}
} elsif (($mod & urxvt::ControlMask) && $ch eq 'w') {# w - copy the word under the cursor
my ($y1, $x1, $y2, $x2) = (@cursor, @cursor);
--$x1 while substr($term->ROW_t($y1), $x1 - 1, 1) =~ m/\w/;
++$x2 while substr($term->ROW_t($y2), $x2 + 1, 1) =~ m/\w/;
do_copy($term, $y1, $x1, $y2, $x2);
deactivate_mark_mode ($term);
visual_mode_disable ($term, @cursor);
} elsif ($ch eq 'Y') { # Y - yank from cursor to EOL
do_copy($term, @cursor, $cursor[0], $term->ROW_l($cursor[0]) - $cursor[0]);
deactivate_mark_mode ($term);
visual_mode_disable ($term, @cursor);
} elsif (!$mod && $ch eq 'y') { # y - yank selected
my %url = get_active_url ($term);
do_copy($term, $url{row}, $url{col_from}, $url{row}, $url{col_to}) if %url;
deactivate_mark_mode ($term);
visual_mode_disable ($term, @cursor);
} elsif (($mod & urxvt::ControlMask) && (($ch eq 'n') || ($ch eq 'p'))) {
# ^n and ^p to cycle list
my $dir = ($ch eq 'n') ? 1 : -1;
move_url_highlight ($term, $dir);
visual_mode_disable ($term, @cursor);
} elsif (($mod & urxvt::ControlMask) && (($ch eq 'f') || ($ch eq 'b'))) {
# ^f and ^b to scroll
my $ofs = ($ch eq 'f') ? 1 : -1;
visual_mode_update ($term, \@cursor, [$cursor[0] + $ofs*($term->nrow - 1), $cursor[1]]);
} elsif (!$mod && $ch eq 'h') { # left
if ($cursor[1] > 0) {
visual_mode_update ($term, \@cursor, [$cursor[0], $cursor[1] - 1]);
}
} elsif (!$mod && $ch eq 'j') { # down
if ($cursor[0] < $term->nrow) {
visual_mode_update ($term, \@cursor, [$cursor[0] + 1, $cursor[1]]);
}
} elsif (!$mod && $ch eq 'k') { # up
if ($cursor[0] > $term->top_row) {
visual_mode_update ($term, \@cursor, [$cursor[0] - 1, $cursor[1]]);
}
} elsif (!$mod && $ch eq 'l') { # right
if ($cursor[1] < ($term->ncol - 1)) {
visual_mode_update ($term, \@cursor, [$cursor[0], $cursor[1] + 1]);
}
} elsif ($ch eq 'H') {
visual_mode_update ($term, \@cursor, [0, $cursor[1]]);
} elsif ($ch eq 'M') {
visual_mode_update ($term, \@cursor, [$term->nrow / 2, $cursor[1]]);
} elsif ($ch eq 'L') {
visual_mode_update ($term, \@cursor, [$term->nrow, $cursor[1]]);
} elsif ($ch eq '^') {
$term->ROW_t($cursor[0]) =~ m/^\s*/;
visual_mode_update ($term, \@cursor, [$cursor[0], $+[0]]);
} elsif ($ch eq '0') {
visual_mode_update ($term, \@cursor, [$cursor[0], 0]);
} elsif ($ch eq '$') {
visual_mode_update ($term, \@cursor, [$cursor[0], $term->ROW_l($cursor[0]) - 1]);
} elsif ($ch eq 'g') {
visual_mode_update ($term, \@cursor, [$term->top_row, 0]);
} elsif ($ch eq 'G') {
visual_mode_update ($term, \@cursor, [$term->nrow - 1, $term->ROW_l($term->nrow - 1) - 1]);
} elsif ($ch eq 'w') {
my @dest = @cursor;
my $line = $term->ROW_t($dest[0]);
++$dest[1] while substr($line, $dest[1], 1) =~ m/\w/;
until (substr($line, $dest[1], 1) =~ m/\w/) {
++$dest[1];
next if $dest[1] <= $term->ROW_l($dest[0]);
++$dest[0];
return 1 if $dest[0] >= $term->nrow;
$dest[1] = 0;
$line = $term->ROW_t($dest[0]);
}
visual_mode_update ($term, \@cursor, \@dest);
} elsif ($ch eq 'e') {
my @dest = @cursor;
my $line = $term->ROW_t($dest[0]);
++$dest[1];
++$dest[1] while substr($line, $dest[1], 1) =~ m/\W/;
until (substr($line, $dest[1], 1) =~ m/\w/) {
++$dest[1];
next if $dest[1] <= $term->ROW_l($dest[0]);
++$dest[0];
return 1 if $dest[0] >= $term->nrow;
$dest[1] = 0;
$line = $term->ROW_t($dest[0]);
}
++$dest[1] while substr($line, $dest[1] + 1, 1) =~ m/\w/;
visual_mode_update ($term, \@cursor, \@dest);
} elsif ($ch eq 'b') {
my @dest = @cursor;
my $line = $term->ROW_t($dest[0]);
# unless at the beginning of a word, jump to the beginning
--$dest[1] while ($dest[1] >= 1 && substr($line, $dest[1] - 1, 1) =~ m/\w/);
if ($dest[1] == $cursor[1]) { # at beginning of a word
# skip non-word characters
until ($dest[1] >= 1 && substr($line, $dest[1] - 1, 1) =~ m/\w/) {
--$dest[1];
next if $dest[1] > 0;
--$dest[0];
return 1 if $dest[0] < 0;
$dest[1] = $term->ROW_l($dest[0]);
$line = $term->ROW_t($dest[0]);
}
--$dest[1] while $dest[1] >= 1 && substr($line, $dest[1] - 1, 1) =~ m/\w/;
}
visual_mode_update ($term, \@cursor, \@dest);
} elsif ($ch eq 'W') {
my @dest = @cursor;
my $line = $term->ROW_t($dest[0]);
++$dest[1] while substr($line, $dest[1], 1) =~ m/\S/;
until (substr($line, $dest[1], 1) =~ m/\S/) {
++$dest[1];
next if $dest[1] <= $term->ROW_l($dest[0]);
++$dest[0];
return 1 if $dest[0] >= $term->nrow;
$dest[1] = 0;
$line = $term->ROW_t($dest[0]);
}
visual_mode_update ($term, \@cursor, \@dest);
} elsif ($ch eq 'E') {
my @dest = @cursor;
my $line = $term->ROW_t($dest[0]);
++$dest[1];
++$dest[1] while substr($line, $dest[1], 1) =~ m/\s/;
until (substr($line, $dest[1], 1) =~ m/\S/) {
++$dest[1];
next if $dest[1] <= $term->ROW_l($dest[0]);
++$dest[0];
return 1 if $dest[0] >= $term->nrow;
$dest[1] = 0;
$line = $term->ROW_t($dest[0]);
}
++$dest[1] while substr($line, $dest[1] + 1, 1) =~ m/\S/;
visual_mode_update ($term, \@cursor, \@dest);
} elsif ($ch eq 'B') {
my @dest = @cursor;
my $line = $term->ROW_t($dest[0]);
--$dest[1] while ($dest[1] >= 1 && substr($line, $dest[1] - 1, 1) =~ m/\S/);
until ($dest[1] >= 1 && substr($line, $dest[1] - 1, 1) =~ m/\S/) {
--$dest[1];
next if $dest[1] > 0;
--$dest[0];
return 1 if $dest[0] < 0;
$dest[1] = $term->ROW_l($dest[0]);
$line = $term->ROW_t($dest[0]);
}
--$dest[1] while $dest[1] >= 1 && substr($line, $dest[1] - 1, 1) =~ m/\S/;
visual_mode_update ($term, \@cursor, \@dest);
}
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];
return %{$url_db[$url_selected]};
}
sub do_copy {
my $term = shift;
my ($y1, $x1, $y2, $x2) = canonicalise_coordinates(@_);
$term->selection_beg($y1, $x1);
$term->selection_end($y2, 1 + $x2);
$term->selection_make(urxvt::CurrentTime);
my $text = $term->selection();
if ($term->{have_Clipboard}) {
Clipboard->copy($text);
} else {
my $pid = open(XCLIP, "|xclip -i");
print XCLIP $text;
close(XCLIP)
}
status_message ($term, "copied ".length($text)." characters into clipboard");
}
# ------------------------------------------------------------------------
sub move_url_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 visual_mode_enable {
my ($term, $mode, @cur) = @_;
$visual_mode = $mode;
@visual_start = @cur;
}
sub visual_mode_disable {
my ($term, @cursor) = @_;
if ($visual_mode) {
$term->scr_xor_span(canonicalise_coordinates(@visual_start, @cursor));
$visual_mode = 0;
$term->want_refresh;
}
}
sub visual_mode_update {
my ($term, $oldref, $newref) = @_;
my @old = @$oldref;
my @new = @$newref;
@cursor = @new;
$cursor[0] = clamp $term->top_row, $cursor[0], $term->nrow - 1;
if ($visual_mode eq 'v') {
$term->scr_xor_span(canonicalise_coordinates(@old, @new));
}
$term->screen_cur (@cursor);
$term->view_start (clamp $cursor[0] - $term->nrow + 1,
$term->view_start,
min 0, $cursor[0]);
$term->want_refresh;
}
# ------------------------------------------------------------------------
sub activate_mark_mode {
my ($term) = @_;
print "title: " . ($term->resource("title")) . "\n";
print "name: " . ($term->resource("name")) . "\n";
print "term_name: " . ($term->resource("term_name")) . "\n";
print "color: " . ($term->resource("color")) . "\n";
my ($row, $col) = $term->screen_cur;
print "cursor: $row / $col \n";
$mark_mode_active = 1;
@backup_cursor = @cursor = $term->screen_cur;
}
sub activate_mark_url_mode {
my ($term) = @_;
if ($mark_mode_active) {
move_url_highlight ($term, -1);
} elsif ( do_scan_for_urls ($term) ) {
$term->{save_view_start} = $term->view_start;
move_url_highlight ($term, 0);
if ($url_selected > -1) {
$mark_mode_active = 1;
@backup_cursor = @cursor = $term->screen_cur;
}
}
}
sub deactivate_mark_mode {
my ($term) = @_;
do_highlight ($term, 0);
$mark_mode_active = 0;
$term->screen_cur (@backup_cursor);
$first_mark_set = 0;
$url_selected = -1;
$term->view_start ($term->{save_view_start});
$term->want_refresh;
}
# vim: set et ts=4 sw=4:
|