summaryrefslogtreecommitdiffstats
path: root/.urxvt/ext/url-select
blob: f7b7eafc5d320d6a272d70b9b62d48bf112136c9 (plain)
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
#! perl -w
# Author:   Bert Muennich
# Website:  http://www.github.com/muennich/urxvt-perls
# Based on: http://www.jukie.net/~bart/blog/urxvt-url-yank
# Version:  git-20120120
# License:  GPLv2

# Use keyboard shortcuts to select URLs.
# This should be used as a replacement for the default matcher extension,
# it also makes URLs clickable with the middle mouse button.

# Usage: put the following lines in your .Xdefaults/.Xresources:
#   URxvt.perl-ext-common: ...,url-select
#   URxvt.keysym.M-u: perl:url-select:select_next

# Use Meta-u to activate URL selection mode, then use the following keys:
#   j/k:      Select next downward/upward URL (also with arrow keys)
#   g/G:      Select first/last URL (also with home/end key)
#   o/Return: Open selected URL in browser, Return: deactivate afterwards
#   y:        Copy (yank) selected URL and deactivate selection mode
#   q/Escape: Deactivate URL selection mode

# Options:
#   URxvt.urlLauncher:   Browser/command to open selected URL with
#   URxvt.underlineURLs: If set to true, all URLs get underlined
#   URvxt.urlButton:     Mouse button to click-open URLs (default: 2)


use strict;

my $url_matcher = qr{(
	(?:https?://|ftp://|news://|mailto:|file://|www\.)
	[\w\-\@;\/?:&=%\$_.+!*\x27(),~#]+[\w\-\@;\/?&=%\$_+!*\x27()~]
)}x;

sub on_start {
	my ($self) = @_;

	# read resource settings
	if ($self->x_resource('urlLauncher')) {
		@{$self->{browser}} = split /\s+/, $self->x_resource('urlLauncher');
	} else {
		@{$self->{browser}} = ('x-www-browser');
	}
	if ($self->x_resource('underlineURLs') eq 'true') {
		$self->enable(line_update => \&line_update);
	}
	if($self->x_resource('urlButton') =~ /^\d+$/) {
		$self->{button} = $self->x_resource('urlButton');
	} else {
		$self->{button} = 2;
	}

	()
}


sub line_update {
	my ($self, $row) = @_;

	my $line = $self->line($row);
	my $text = $line->t;
	my $rend = $line->r;

	while ($text =~ /$url_matcher/g) {
		my $url = $1;
		my ($beg, $end) = ($-[1], $+[1] - 1);
		--$end if $url =~ /["')]$/;

		for (@{$rend}[$beg .. $end]) {
			$_ |= urxvt::RS_Uline;
		}
		$line->r($rend);
	}

	()
}


sub on_user_command {
	my ($self, $cmd) = @_;

	if ($cmd eq 'url-select:select_next') {
		if (not $self->{active}) {
			activate($self);
		}
		select_next($self, -1);
	}

	()
}


sub key_press {
	my ($self, $event, $keysym) = @_;
	my $char = chr($keysym);

	if ($keysym == 0xff1b || lc($char) eq 'q') {
		deactivate($self);
	} elsif ($keysym == 0xff0d || $char eq 'o') {
		$self->exec_async(@{$self->{browser}}, ${$self->{found}[$self->{n}]}[4]);
		deactivate($self) unless $char eq 'o';
	} elsif ($char eq 'y') {
		my $found = $self->{found}[$self->{n}];
		$self->selection_beg(${$found}[0], ${$found}[1]);
		$self->selection_end(${$found}[2], ${$found}[3]);
		$self->selection_make($event->{time});
		$self->selection_beg(1, 0);
		$self->selection_end(1, 0);
		deactivate($self);
	} elsif ($char eq 'k' || $keysym == 0xff52 || $keysym == 0xff51) {
		select_next($self, -1);
	} elsif ($char eq 'j' || $keysym == 0xff54 || $keysym == 0xff53) {
		select_next($self, 1);
	} elsif ($char eq 'g' || $keysym == 0xff50) {
		$self->{row} = $self->top_row - 1;
		delete $self->{found};
		select_next($self, 1);
	} elsif ($char eq 'G' || $keysym == 0xff57) {
		$self->{row} = $self->nrow;
		delete $self->{found};
		select_next($self, -1);
	}

	return 1;
}


sub on_button_press {
	my ($self, $event) = @_;

	my $mask = $self->ModLevel3Mask | $self->ModMetaMask |
	           urxvt::ShiftMask | urxvt::ControlMask;

	if ($event->{button} == $self->{button} && ($event->{state} & $mask) == 0) {
		$self->{button_pressed} = 1;
		$self->{button_col} = $event->{col};
		$self->{button_row} = $event->{row};
	}

	()
}

sub on_button_release {
	my ($self, $event) = @_;

	if ($self->{button_pressed} && $event->{button} == $self->{button}) {
		my $col = $event->{col};
		my $row = $event->{row};

		$self->{button_pressed} = 0;

		if ($col == $self->{button_col} && $row == $self->{button_row}) {
			my $line = $self->line($row);
			my $text = $line->t;

			while ($text =~ /$url_matcher/g) {
				my ($url, $beg, $end) = ($1, $-[0], $+[0]);
				--$end if $url =~ s/["')]$//;

				if ($col >= $beg && $col <= $end) {
					$self->exec_async(@{$self->{browser}}, $url);
					return 1;
				}
			}
		}
	}

	()
}


sub select_next {
	# $dir < 0: up, > 0: down
	my ($self, $dir) = @_;
	my $row = $self->{row};

	if (($dir < 0 && $self->{n} > 0) ||
			($dir > 0 && $self->{n} < $#{ $self->{found} })) {
		# another url on current line
		$self->{n} += $dir;
		hilight($self);
		return;
	}

	while (($dir < 0 && $row > $self->top_row) ||
	       ($dir > 0 && $row < $self->nrow - 1)) {
		my $line = $self->line($row);
		$row = ($dir < 0 ? $line->beg : $line->end) + $dir;
		$line = $self->line($row);
		my $text = $line->t;

		if ($text =~ /$url_matcher/g) {
			delete $self->{found};

			do {
				my ($beg, $end) = ($-[0], $+[0]);
				--$end if $& =~ /['")]$/;
				push @{$self->{found}}, [$line->coord_of($beg),
						$line->coord_of($end), substr($text, $beg, $end - $beg)];
			} while ($text =~ /$url_matcher/g);

			$self->{row} = $row;
			$self->{n} = $dir < 0 ? $#{$self->{found}} : 0;
			hilight($self);
			return;
		}
	}
	
	deactivate($self) unless $self->{found};

	()
}


sub hilight {
	my ($self) = @_;

	if ($self->{found}) {
		if ($self->{row} < $self->view_start() ||
				$self->{row} >= $self->view_start() + $self->nrow) {
			# scroll selected url into visible area
			my $top = $self->{row} - ($self->nrow >> 1);
			$self->view_start($top < 0 ? $top : 0);
		}

		status_area($self);
		$self->want_refresh();
	}

	()
}


sub refresh {
	my ($self) = @_;

	if ($self->{found}) {
		$self->scr_xor_span(@{$self->{found}[$self->{n}]}[0 .. 3], urxvt::RS_RVid);
	}

	()
}


sub status_area {
	my ($self) = @_;

	my $row = $self->{row} < 0 ?
			$self->{row} - $self->top_row : abs($self->top_row) + $self->{row};
	my $text = sprintf("%d,%d ", $row + 1, $self->{n} + 1);

	if ($self->top_row == 0) {
		$text .= "All";
	} elsif ($self->view_start() == $self->top_row) {
		$text .= "Top";
	} elsif ($self->view_start() == 0) {
		$text .= "Bot";
	} else {
		$text .= sprintf("%2d%",
				($self->top_row - $self->view_start) * 100 / $self->top_row);
	}

	my $text_len = length($text);

	if ($self->{overlay_len} != $text_len) {
		delete $self->{overlay} if $self->{overlay};
		$self->{overlay} = $self->overlay(-1, -1, $text_len, 1,
				urxvt::OVERLAY_RSTYLE, 0);
		$self->{overlay_len} = $text_len;
	}

	$self->{overlay}->set(0, 0, $self->special_encode($text));
	$self->{overlay}->show();

	()
}


sub tt_write {
	return 1;
}


sub activate {
	my ($self) = @_;

	$self->{active} = 1;

	$self->{row} = $self->view_start() + $self->nrow;
	$self->{n} = 0;
	$self->{overlay_len} = 0;
	$self->{button_pressed} = 0;

	$self->{view_start} = $self->view_start();
	$self->{pty_ev_events} = $self->pty_ev_events(urxvt::EV_NONE);

	$self->enable(
		key_press     => \&key_press,
		refresh_begin => \&refresh,
		refresh_end   => \&refresh,
		tt_write      => \&tt_write,
	);

	()
}


sub deactivate {
	my ($self) = @_;

	$self->disable("key_press", "refresh_begin", "refresh_end", "tt_write");
	$self->view_start($self->{view_start});
	$self->pty_ev_events($self->{pty_ev_events});

	delete $self->{overlay} if $self->{overlay};
	delete $self->{found} if $self->{found};

	$self->want_refresh();

	$self->{active} = 0;

	()
}