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 |
$sel = $self->locale_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 |
my ($self, $escape) = @_; |
64 |
|
65 |
my $str = `$self->{paste_cmd}`; |
66 |
if ($? == 0) { |
67 |
$str =~ s/([!#\$%&\*\(\) ='"\\\|\[\]`~,<>\?])/\\\1/g |
68 |
if ($escape); |
69 |
$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 |
$self->paste (0); |
84 |
} elsif ($cmd eq "clipboard:paste_escaped") { |
85 |
$self->paste (1); |
86 |
} |
87 |
|
88 |
() |
89 |
} |
90 |
|
91 |
sub sel_grab { |
92 |
my ($self) = @_; |
93 |
|
94 |
$self->copy; |
95 |
|
96 |
() |
97 |
} |