ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/perl/selection-popup
(Generate patch)

Comparing rxvt-unicode/src/perl/selection-popup (file contents):
Revision 1.4 by root, Mon Jan 9 04:19:36 2006 UTC vs.
Revision 1.32 by root, Wed Jun 23 12:45:30 2021 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines