ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/perl/selection-popup
Revision: 1.4
Committed: Mon Jan 9 04:19:36 2006 UTC (18 years, 4 months ago) by root
Branch: MAIN
Changes since 1.3: +22 -1 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #! perl
2    
3 root 1.4 sub msg {
4     my ($self, $msg1, $msg2) = @_;
5    
6     my $overlay = $self->overlay (0, 0,
7     (List::Util::max map $self->strwidth ($_), $msg1, $msg2), 2);
8     $overlay->set (0, 0, $msg1);
9     $overlay->set (0, 1, $msg2);
10     my $iow; $iow = urxvt::timer->new->start (urxvt::NOW + 1)->cb (sub {
11     undef $overlay;
12     undef $iow;
13     });
14     }
15    
16 root 1.1 sub on_start {
17     my ($self) = @_;
18    
19     $self->grab_button (3, urxvt::ControlMask);
20     }
21    
22     sub on_button_press {
23     my ($self, $event) = @_;
24    
25     if ($event->{button} == 3 && $event->{state} & urxvt::ControlMask) {
26     my $popup = $self->popup ($event)
27     or return 1;
28    
29     $popup->add_title ("Convert Selection");
30    
31     my $text = $self->selection;
32    
33     my $title = $text;
34     $title =~ s/[\x00-\x1f\x80-\x9f]/ยท/g;
35     substr $title, 40, -1, "..." if 40 < length $title;
36     $popup->add_title ($title);
37     $popup->add_separator;
38    
39     my $add_button = sub {
40     my ($title, $cb) = @_;
41    
42     $popup->add_button ($title => sub {
43     for ($text) {
44 root 1.4 my $orig = $_;
45 root 1.1 $cb->();
46 root 1.4
47     if ($orig ne $_) {
48     $self->selection ($_);
49     $self->msg ("Selection replaced by", $_);
50     }
51 root 1.1 }
52     });
53     };
54    
55     for ($text) {
56 root 1.2 $add_button->("rot13" => sub { y/A-Za-z/N-ZA-Mn-za-m/ });
57    
58 root 1.3 urxvt::safe
59     and $add_button->("eval perl expression" => sub { $_ = eval urxvt::untaint $_ });
60    
61 root 1.1 /^(\S+):(\d+):?$/
62     and $add_button->("vi-commands to load '$1'" => sub { s/^(\S+):(\d+):?$/\x1b:e $1\x0d:$2\x0d/ });
63 root 1.2
64 root 1.1 /%[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 });
66 root 1.2
67 root 1.4 /[\\"'\ \t|&;<>()]/
68     and $add_button->("shell quote" => sub { $_ = "\Q$_" });
69    
70 root 1.1 /^(http|ftp|telnet|irc|news):\//
71     and $add_button->("run x-www-browser" => sub { system "x-www-browser \Q$_\E &" });
72     }
73    
74     $popup->show;
75    
76     return 1;
77     }
78    
79     ()
80     }
81