ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/perl/clipboard
Revision: 1.2
Committed: Tue Jan 28 08:06:45 2014 UTC (10 years, 3 months ago) by sf-exg
Branch: MAIN
Changes since 1.1: +5 -17 lines
Log Message:
Remove code duplication.

File Contents

# User Rev Content
1 sf-exg 1.1 #! perl -w
2     # Author: Bert Muennich
3     # Website: http://www.github.com/muennich/urxvt-perls
4     # License: GPLv2
5    
6     # Use keyboard shortcuts to copy the selection to the clipboard and to paste
7     # the clipboard contents (optionally escaping all special characters).
8     # Requires xsel to be installed!
9    
10     # Usage: put the following lines in your .Xdefaults/.Xresources:
11     # URxvt.perl-ext-common: ...,clipboard
12     # URxvt.keysym.M-c: perl:clipboard:copy
13     # URxvt.keysym.M-v: perl:clipboard:paste
14     # URxvt.keysym.M-C-v: perl:clipboard:paste_escaped
15    
16     # Options:
17     # URxvt.clipboard.autocopy: If true, PRIMARY overwrites clipboard
18    
19     # You can also overwrite the system commands to use for copying/pasting.
20     # The default ones are:
21     # URxvt.clipboard.copycmd: xsel -ib
22     # URxvt.clipboard.pastecmd: xsel -ob
23     # If you prefer xclip, then put these lines in your .Xdefaults/.Xresources:
24     # URxvt.clipboard.copycmd: xclip -i -selection clipboard
25     # URxvt.clipboard.pastecmd: xclip -o -selection clipboard
26     # On Mac OS X, put these lines in your .Xdefaults/.Xresources:
27     # URxvt.clipboard.copycmd: pbcopy
28     # URxvt.clipboard.pastecmd: pbpaste
29    
30     # The use of the functions should be self-explanatory!
31    
32     use strict;
33    
34     sub on_start {
35     my ($self) = @_;
36    
37     $self->{copy_cmd} = $self->x_resource('clipboard.copycmd') || 'xsel -ib';
38     $self->{paste_cmd} = $self->x_resource('clipboard.pastecmd') || 'xsel -ob';
39    
40     if ($self->x_resource('clipboard.autocopy') eq 'true') {
41     $self->enable(sel_grab => \&sel_grab);
42     }
43    
44     ()
45     }
46    
47     sub copy {
48     my ($self) = @_;
49    
50     if (open(CLIPBOARD, "| $self->{copy_cmd}")) {
51     my $sel = $self->selection();
52     utf8::encode($sel);
53     print CLIPBOARD $sel;
54     close(CLIPBOARD);
55     } else {
56     print STDERR "error running '$self->{copy_cmd}': $!\n";
57     }
58    
59     ()
60     }
61    
62     sub paste {
63 sf-exg 1.2 my ($self, $escape) = @_;
64 sf-exg 1.1
65     my $str = `$self->{paste_cmd}`;
66     if ($? == 0) {
67 sf-exg 1.2 $str =~ s/([!#\$%&\*\(\) ='"\\\|\[\]`~,<>\?])/\\\1/g
68     if ($escape);
69 sf-exg 1.1 $self->tt_paste($str);
70     } else {
71     print STDERR "error running '$self->{paste_cmd}': $!\n";
72     }
73    
74     ()
75     }
76    
77     sub on_user_command {
78     my ($self, $cmd) = @_;
79    
80     if ($cmd eq "clipboard:copy") {
81     $self->copy;
82     } elsif ($cmd eq "clipboard:paste") {
83 sf-exg 1.2 $self->paste (0);
84 sf-exg 1.1 } elsif ($cmd eq "clipboard:paste_escaped") {
85 sf-exg 1.2 $self->paste (1);
86 sf-exg 1.1 }
87    
88     ()
89     }
90    
91     sub sel_grab {
92     my ($self) = @_;
93    
94     $self->copy;
95    
96     ()
97     }