1 |
#! perl |
2 |
|
3 |
#:META:X_RESOURCE:url-launcher:string:shell command to use |
4 |
|
5 |
sub msg { |
6 |
my ($self, $msg) = @_; |
7 |
|
8 |
my $overlay = $self->overlay (0, 0, $self->strwidth ($msg), 1); |
9 |
$overlay->set (0, 0, $msg); |
10 |
my $iow; $iow = urxvt::timer->new->after (1)->cb (sub { |
11 |
undef $overlay; |
12 |
undef $iow; |
13 |
}); |
14 |
} |
15 |
|
16 |
sub on_start { |
17 |
my ($self) = @_; |
18 |
|
19 |
$self->{browser} = $self->x_resource ("url-launcher") || "sensible-browser"; |
20 |
|
21 |
$self->grab_button (3, urxvt::ControlMask); |
22 |
|
23 |
() |
24 |
} |
25 |
|
26 |
sub on_button_press { |
27 |
my ($self, $event) = @_; |
28 |
|
29 |
if ($event->{button} == 3 && $event->{state} & urxvt::ControlMask) { |
30 |
my $popup = $self->popup ($event) |
31 |
or return 1; |
32 |
|
33 |
$popup->add_title ("Convert Selection"); |
34 |
|
35 |
my $text = $self->selection; |
36 |
|
37 |
my $title = $text; |
38 |
$title =~ s/[\x00-\x1f\x80-\x9f]/·/g; |
39 |
substr $title, 40, -1, "..." if 40 < length $title; |
40 |
$popup->add_title ($title); |
41 |
$popup->add_separator; |
42 |
|
43 |
my $add_button = sub { |
44 |
my ($title, $cb) = @_; |
45 |
|
46 |
$popup->add_button ($title => sub { |
47 |
for ($text) { |
48 |
my $orig = $_; |
49 |
$cb->(); |
50 |
|
51 |
if ($orig ne $_) { |
52 |
$self->selection ($_); |
53 |
s/[\x00-\x1f\x80-\x9f]/·/g; |
54 |
$self->msg ($self->special_encode ($_)); |
55 |
} |
56 |
} |
57 |
}); |
58 |
}; |
59 |
|
60 |
for ($text) { |
61 |
/\n/ |
62 |
and $add_button->("newlines to spaces" => sub { y/\n/ / }); |
63 |
|
64 |
/./ |
65 |
and $add_button->("rot13" => sub { y/A-Za-z/N-ZA-Mn-za-m/ }); |
66 |
|
67 |
/./ |
68 |
and $add_button->("eval perl expression" => sub { my $self = $self; no warnings; $_ = eval $_; $_ = "$@" if $@ }); |
69 |
|
70 |
/./ |
71 |
and $add_button->((sprintf "to unicode hex index (%x)", ord) => sub { $_ = sprintf "%x", ord }); |
72 |
|
73 |
/(\S+):(\d+):?/ |
74 |
and $add_button->("vi-commands to load '$1'" => sub { s/^(\S+):(\d+):?$/\x1b:e $1\x0d:$2\x0d/ }); |
75 |
|
76 |
/%[0-9a-fA-F]{2}/ && !/%[^0-9a-fA-F]/ && !/%.[^0-9a-fA-F]/ |
77 |
and $add_button->("uri unescape" => sub { s/%([0-9a-fA-F]{2})/chr hex $1/ge }); |
78 |
|
79 |
/[\\"'\ \t|&;<>()]/ |
80 |
and $add_button->("shell quote" => sub { $_ = "\Q$_" }); |
81 |
|
82 |
/^(https?|ftp|telnet|irc|news):\// |
83 |
and $add_button->("run $self->{browser}" => sub { $self->exec_async ($self->{browser}, $_) }); |
84 |
|
85 |
for my $hook (@{ $self->{term}{selection_popup_hook} || [] }) { |
86 |
if (my ($title, $cb) = $hook->($popup)) { |
87 |
$add_button->($title, $cb); |
88 |
} |
89 |
} |
90 |
|
91 |
if (/^\s*((?:0x)?\d+)\s*$/) { |
92 |
$popup->add_title (sprintf "%20s", eval $1); |
93 |
$popup->add_title (sprintf "%20s", sprintf "0x%x", eval $1); |
94 |
$popup->add_title (sprintf "%20s", sprintf "0%o", eval $1); |
95 |
} |
96 |
} |
97 |
|
98 |
$popup->show; |
99 |
|
100 |
return 1; |
101 |
} |
102 |
|
103 |
() |
104 |
} |
105 |
|