1 |
root |
1.1 |
#! perl |
2 |
|
|
|
3 |
root |
1.29 |
#:META:RESOURCE:url-launcher:string:shell command to use |
4 |
root |
1.25 |
|
5 |
root |
1.26 |
=head1 NAME |
6 |
|
|
|
7 |
root |
1.28 |
selection-popup (enabled by default) |
8 |
root |
1.26 |
|
9 |
|
|
=head1 DESCRIPTION |
10 |
|
|
|
11 |
sf-exg |
1.30 |
Binds a popup menu to Ctrl-Button3 that lets you paste the X |
12 |
|
|
selections and either modify or use the internal selection text in |
13 |
|
|
various ways (such as uri unescaping, perl evaluation, web-browser |
14 |
|
|
starting etc.), depending on content. |
15 |
root |
1.26 |
|
16 |
|
|
Other extensions can extend this popup menu by pushing a code reference |
17 |
root |
1.27 |
onto C<< @{ $term->{selection_popup_hook} } >>, which gets called whenever |
18 |
|
|
the popup is being displayed. |
19 |
root |
1.26 |
|
20 |
|
|
Its sole argument is the popup menu, which can be modified. The selection |
21 |
|
|
is in C<$_>, which can be used to decide whether to add something or not. |
22 |
|
|
It should either return nothing or a string and a code reference. The |
23 |
|
|
string will be used as button text and the code reference will be called |
24 |
|
|
when the button gets activated and should transform C<$_>. |
25 |
|
|
|
26 |
|
|
The following will add an entry C<a to b> that transforms all C<a>s in |
27 |
|
|
the selection to C<b>s, but only if the selection currently contains any |
28 |
|
|
C<a>s: |
29 |
|
|
|
30 |
|
|
push @{ $self->{term}{selection_popup_hook} }, sub { |
31 |
|
|
/a/ ? ("a to b" => sub { s/a/b/g } |
32 |
|
|
: () |
33 |
|
|
}; |
34 |
|
|
|
35 |
|
|
=cut |
36 |
|
|
|
37 |
root |
1.4 |
sub msg { |
38 |
root |
1.6 |
my ($self, $msg) = @_; |
39 |
root |
1.4 |
|
40 |
root |
1.6 |
my $overlay = $self->overlay (0, 0, $self->strwidth ($msg), 1); |
41 |
|
|
$overlay->set (0, 0, $msg); |
42 |
root |
1.13 |
my $iow; $iow = urxvt::timer->new->after (1)->cb (sub { |
43 |
root |
1.4 |
undef $overlay; |
44 |
|
|
undef $iow; |
45 |
|
|
}); |
46 |
|
|
} |
47 |
|
|
|
48 |
root |
1.1 |
sub on_start { |
49 |
|
|
my ($self) = @_; |
50 |
|
|
|
51 |
root |
1.25 |
$self->{browser} = $self->x_resource ("url-launcher") || "sensible-browser"; |
52 |
root |
1.8 |
|
53 |
root |
1.1 |
$self->grab_button (3, urxvt::ControlMask); |
54 |
root |
1.9 |
|
55 |
|
|
() |
56 |
root |
1.1 |
} |
57 |
|
|
|
58 |
|
|
sub on_button_press { |
59 |
|
|
my ($self, $event) = @_; |
60 |
|
|
|
61 |
|
|
if ($event->{button} == 3 && $event->{state} & urxvt::ControlMask) { |
62 |
|
|
my $popup = $self->popup ($event) |
63 |
|
|
or return 1; |
64 |
|
|
|
65 |
sf-exg |
1.30 |
$popup->add_title ("Selection"); |
66 |
root |
1.1 |
|
67 |
|
|
my $text = $self->selection; |
68 |
|
|
|
69 |
|
|
my $title = $text; |
70 |
|
|
$title =~ s/[\x00-\x1f\x80-\x9f]/·/g; |
71 |
|
|
substr $title, 40, -1, "..." if 40 < length $title; |
72 |
|
|
$popup->add_title ($title); |
73 |
|
|
$popup->add_separator; |
74 |
|
|
|
75 |
|
|
my $add_button = sub { |
76 |
|
|
my ($title, $cb) = @_; |
77 |
|
|
|
78 |
|
|
$popup->add_button ($title => sub { |
79 |
|
|
for ($text) { |
80 |
root |
1.4 |
my $orig = $_; |
81 |
root |
1.1 |
$cb->(); |
82 |
root |
1.4 |
|
83 |
|
|
if ($orig ne $_) { |
84 |
|
|
$self->selection ($_); |
85 |
root |
1.5 |
s/[\x00-\x1f\x80-\x9f]/·/g; |
86 |
root |
1.6 |
$self->msg ($self->special_encode ($_)); |
87 |
root |
1.4 |
} |
88 |
root |
1.1 |
} |
89 |
|
|
}); |
90 |
|
|
}; |
91 |
|
|
|
92 |
|
|
for ($text) { |
93 |
root |
1.22 |
/\n/ |
94 |
sf-exg |
1.30 |
and $add_button->("paste primary selection" => sub { $self->selection_request (urxvt::CurrentTime, 1) }); |
95 |
|
|
|
96 |
|
|
/./ |
97 |
|
|
and $add_button->("paste clipboard selection" => sub { $self->selection_request (urxvt::CurrentTime, 3) }); |
98 |
|
|
|
99 |
|
|
/./ |
100 |
|
|
and $add_button->("copy selection to clipboard" => sub { $self->selection ($self->selection, 1); |
101 |
|
|
$self->selection_grab (urxvt::CurrentTime, 1) }); |
102 |
|
|
|
103 |
|
|
/./ |
104 |
root |
1.22 |
and $add_button->("newlines to spaces" => sub { y/\n/ / }); |
105 |
|
|
|
106 |
root |
1.16 |
/./ |
107 |
|
|
and $add_button->("rot13" => sub { y/A-Za-z/N-ZA-Mn-za-m/ }); |
108 |
|
|
|
109 |
|
|
/./ |
110 |
root |
1.24 |
and $add_button->("eval perl expression" => sub { my $self = $self; no warnings; $_ = eval $_; $_ = "$@" if $@ }); |
111 |
root |
1.3 |
|
112 |
root |
1.19 |
/./ |
113 |
|
|
and $add_button->((sprintf "to unicode hex index (%x)", ord) => sub { $_ = sprintf "%x", ord }); |
114 |
|
|
|
115 |
root |
1.23 |
/(\S+):(\d+):?/ |
116 |
root |
1.1 |
and $add_button->("vi-commands to load '$1'" => sub { s/^(\S+):(\d+):?$/\x1b:e $1\x0d:$2\x0d/ }); |
117 |
root |
1.2 |
|
118 |
root |
1.1 |
/%[0-9a-fA-F]{2}/ && !/%[^0-9a-fA-F]/ && !/%.[^0-9a-fA-F]/ |
119 |
|
|
and $add_button->("uri unescape" => sub { s/%([0-9a-fA-F]{2})/chr hex $1/ge }); |
120 |
root |
1.2 |
|
121 |
root |
1.4 |
/[\\"'\ \t|&;<>()]/ |
122 |
|
|
and $add_button->("shell quote" => sub { $_ = "\Q$_" }); |
123 |
|
|
|
124 |
ayin |
1.20 |
/^(https?|ftp|telnet|irc|news):\// |
125 |
root |
1.14 |
and $add_button->("run $self->{browser}" => sub { $self->exec_async ($self->{browser}, $_) }); |
126 |
root |
1.10 |
|
127 |
root |
1.15 |
for my $hook (@{ $self->{term}{selection_popup_hook} || [] }) { |
128 |
root |
1.12 |
if (my ($title, $cb) = $hook->($popup)) { |
129 |
|
|
$add_button->($title, $cb); |
130 |
|
|
} |
131 |
|
|
} |
132 |
|
|
|
133 |
root |
1.10 |
if (/^\s*((?:0x)?\d+)\s*$/) { |
134 |
|
|
$popup->add_title (sprintf "%20s", eval $1); |
135 |
|
|
$popup->add_title (sprintf "%20s", sprintf "0x%x", eval $1); |
136 |
|
|
$popup->add_title (sprintf "%20s", sprintf "0%o", eval $1); |
137 |
|
|
} |
138 |
root |
1.1 |
} |
139 |
|
|
|
140 |
|
|
$popup->show; |
141 |
|
|
|
142 |
|
|
return 1; |
143 |
|
|
} |
144 |
|
|
|
145 |
|
|
() |
146 |
|
|
} |
147 |
|
|
|