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.12 by root, Wed Jan 18 10:11:03 2006 UTC vs.
Revision 1.26 by root, Sun Jun 10 17:31:53 2012 UTC

1#! perl 1#! perl
2
3#:META:X_RESOURCE:url-launcher:string:shell command to use
4
5=head1 NAME
6
7 selection-popup (enabled by default)
8
9=head1 DESCRIPTION
10
11Binds a popup menu to Ctrl-Button3 that lets you convert the selection
12text into various other formats/action (such as uri unescaping, perl
13evaluation, web-browser starting etc.), depending on content.
14
15Other extensions can extend this popup menu by pushing a code reference
16onto C<@{ $term->{selection_popup_hook} }>, which gets called whenever the
17popup is being displayed.
18
19Its sole argument is the popup menu, which can be modified. The selection
20is in C<$_>, which can be used to decide whether to add something or not.
21It should either return nothing or a string and a code reference. The
22string will be used as button text and the code reference will be called
23when the button gets activated and should transform C<$_>.
24
25The following will add an entry C<a to b> that transforms all C<a>s in
26the selection to C<b>s, but only if the selection currently contains any
27C<a>s:
28
29 push @{ $self->{term}{selection_popup_hook} }, sub {
30 /a/ ? ("a to b" => sub { s/a/b/g }
31 : ()
32 };
33
34=cut
2 35
3sub msg { 36sub msg {
4 my ($self, $msg) = @_; 37 my ($self, $msg) = @_;
5 38
6 my $overlay = $self->overlay (0, 0, $self->strwidth ($msg), 1); 39 my $overlay = $self->overlay (0, 0, $self->strwidth ($msg), 1);
7 $overlay->set (0, 0, $msg); 40 $overlay->set (0, 0, $msg);
8 my $iow; $iow = urxvt::timer->new->start (urxvt::NOW + 1)->cb (sub { 41 my $iow; $iow = urxvt::timer->new->after (1)->cb (sub {
9 undef $overlay; 42 undef $overlay;
10 undef $iow; 43 undef $iow;
11 }); 44 });
12} 45}
13 46
14sub on_start { 47sub on_start {
15 my ($self) = @_; 48 my ($self) = @_;
16 49
17 $self->{browser} = $self->x_resource ("urlLauncher") || "x-www-browser"; 50 $self->{browser} = $self->x_resource ("url-launcher") || "sensible-browser";
18 51
19 $self->grab_button (3, urxvt::ControlMask); 52 $self->grab_button (3, urxvt::ControlMask);
20 53
21 () 54 ()
22} 55}
23
24our @hook;
25 56
26sub on_button_press { 57sub on_button_press {
27 my ($self, $event) = @_; 58 my ($self, $event) = @_;
28 59
29 if ($event->{button} == 3 && $event->{state} & urxvt::ControlMask) { 60 if ($event->{button} == 3 && $event->{state} & urxvt::ControlMask) {
56 } 87 }
57 }); 88 });
58 }; 89 };
59 90
60 for ($text) { 91 for ($text) {
61 $add_button->("rot13" => sub { y/A-Za-z/N-ZA-Mn-za-m/ }); 92 /\n/
62 $add_button->("eval perl expression" => sub { no warnings; $_ = eval $_ }); 93 and $add_button->("newlines to spaces" => sub { y/\n/ / });
63 94
95 /./
96 and $add_button->("rot13" => sub { y/A-Za-z/N-ZA-Mn-za-m/ });
97
98 /./
99 and $add_button->("eval perl expression" => sub { my $self = $self; no warnings; $_ = eval $_; $_ = "$@" if $@ });
100
101 /./
102 and $add_button->((sprintf "to unicode hex index (%x)", ord) => sub { $_ = sprintf "%x", ord });
103
64 /^(\S+):(\d+):?$/ 104 /(\S+):(\d+):?/
65 and $add_button->("vi-commands to load '$1'" => sub { s/^(\S+):(\d+):?$/\x1b:e $1\x0d:$2\x0d/ }); 105 and $add_button->("vi-commands to load '$1'" => sub { s/^(\S+):(\d+):?$/\x1b:e $1\x0d:$2\x0d/ });
66 106
67 /%[0-9a-fA-F]{2}/ && !/%[^0-9a-fA-F]/ && !/%.[^0-9a-fA-F]/ 107 /%[0-9a-fA-F]{2}/ && !/%[^0-9a-fA-F]/ && !/%.[^0-9a-fA-F]/
68 and $add_button->("uri unescape" => sub { s/%([0-9a-fA-F]{2})/chr hex $1/ge }); 108 and $add_button->("uri unescape" => sub { s/%([0-9a-fA-F]{2})/chr hex $1/ge });
69 109
70 /[\\"'\ \t|&;<>()]/ 110 /[\\"'\ \t|&;<>()]/
71 and $add_button->("shell quote" => sub { $_ = "\Q$_" }); 111 and $add_button->("shell quote" => sub { $_ = "\Q$_" });
72 112
73 /^(http|ftp|telnet|irc|news):\// 113 /^(https?|ftp|telnet|irc|news):\//
74 and $add_button->("run $self->{browser}" => sub { urxvt::exec_async $self->{browser}, $_ }); 114 and $add_button->("run $self->{browser}" => sub { $self->exec_async ($self->{browser}, $_) });
75 115
76 for my $hook (@hook) { 116 for my $hook (@{ $self->{term}{selection_popup_hook} || [] }) {
77 if (my ($title, $cb) = $hook->($popup)) { 117 if (my ($title, $cb) = $hook->($popup)) {
78 $add_button->($title, $cb); 118 $add_button->($title, $cb);
79 } 119 }
80 } 120 }
81 121

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines