1 | #! perl |
1 | #! perl |
|
|
2 | |
|
|
3 | #:META:RESOURCE:url-launcher:string:shell command to use |
|
|
4 | |
|
|
5 | =head1 NAME |
|
|
6 | |
|
|
7 | selection-popup - selection management pop-up |
|
|
8 | |
|
|
9 | =head1 DESCRIPTION |
|
|
10 | |
|
|
11 | 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 | |
|
|
16 | Other extensions can extend this popup menu by pushing a code reference |
|
|
17 | onto C<< @{ $term->{selection_popup_hook} } >>, which gets called whenever |
|
|
18 | the popup is being displayed. |
|
|
19 | |
|
|
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 |
2 | |
36 | |
3 | sub msg { |
37 | sub msg { |
4 | my ($self, $msg) = @_; |
38 | my ($self, $msg) = @_; |
5 | |
39 | |
6 | my $overlay = $self->overlay (0, 0, $self->strwidth ($msg), 1); |
40 | my $overlay = $self->overlay (0, 0, $self->strwidth ($msg), 1); |
7 | $overlay->set (0, 0, $msg); |
41 | $overlay->set (0, 0, $msg); |
8 | my $iow; $iow = urxvt::timer->new->after (1)->cb (sub { |
42 | $self->{timer} = urxvt::timer->new->after (1)->cb (sub { |
|
|
43 | delete $self->{timer}; |
9 | undef $overlay; |
44 | undef $overlay; |
10 | undef $iow; |
|
|
11 | }); |
45 | }); |
12 | } |
46 | } |
13 | |
47 | |
14 | sub on_start { |
48 | sub on_start { |
15 | my ($self) = @_; |
49 | my ($self) = @_; |
16 | |
50 | |
17 | $self->{browser} = $self->x_resource ("urlLauncher") || "sensible-browser"; |
51 | $self->{browser} = $self->x_resource ("url-launcher") || "sensible-browser"; |
18 | |
52 | |
19 | $self->grab_button (3, urxvt::ControlMask); |
53 | $self->grab_button (3, urxvt::ControlMask); |
20 | |
54 | |
21 | () |
55 | () |
22 | } |
56 | } |
… | |
… | |
26 | |
60 | |
27 | if ($event->{button} == 3 && $event->{state} & urxvt::ControlMask) { |
61 | if ($event->{button} == 3 && $event->{state} & urxvt::ControlMask) { |
28 | my $popup = $self->popup ($event) |
62 | my $popup = $self->popup ($event) |
29 | or return 1; |
63 | or return 1; |
30 | |
64 | |
31 | $popup->add_title ("Convert Selection"); |
65 | $popup->add_title ("Selection"); |
32 | |
66 | |
33 | my $text = $self->selection; |
67 | my $text = $self->selection; |
34 | |
68 | |
35 | my $title = $text; |
69 | my $title = $text; |
36 | $title =~ s/[\x00-\x1f\x80-\x9f]/·/g; |
70 | $title =~ s/[\x00-\x1f\x80-\x9f]/·/g; |
… | |
… | |
55 | }); |
89 | }); |
56 | }; |
90 | }; |
57 | |
91 | |
58 | for ($text) { |
92 | for ($text) { |
59 | /\n/ |
93 | /\n/ |
|
|
94 | 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 | /./ |
60 | and $add_button->("newlines to spaces" => sub { y/\n/ / }); |
104 | and $add_button->("newlines to spaces" => sub { y/\n/ / }); |
61 | |
105 | |
62 | /./ |
106 | /./ |
63 | and $add_button->("rot13" => sub { y/A-Za-z/N-ZA-Mn-za-m/ }); |
107 | and $add_button->("rot13" => sub { y/A-Za-z/N-ZA-Mn-za-m/ }); |
64 | |
108 | |