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 |
my ($self) = @_; |
64 |
|
65 |
my $str = `$self->{paste_cmd}`; |
66 |
if ($? == 0) { |
67 |
$self->tt_paste($str); |
68 |
} else { |
69 |
print STDERR "error running '$self->{paste_cmd}': $!\n"; |
70 |
} |
71 |
|
72 |
() |
73 |
} |
74 |
|
75 |
sub paste_escaped { |
76 |
my ($self) = @_; |
77 |
|
78 |
my $str = `$self->{paste_cmd}`; |
79 |
if ($? == 0) { |
80 |
$str =~ s/([!#\$%&\*\(\) ='"\\\|\[\]`~,<>\?])/\\\1/g; |
81 |
$self->tt_paste($str); |
82 |
} else { |
83 |
print STDERR "error running '$self->{paste_cmd}': $!\n"; |
84 |
} |
85 |
|
86 |
() |
87 |
} |
88 |
|
89 |
sub on_user_command { |
90 |
my ($self, $cmd) = @_; |
91 |
|
92 |
if ($cmd eq "clipboard:copy") { |
93 |
$self->copy; |
94 |
} elsif ($cmd eq "clipboard:paste") { |
95 |
$self->paste; |
96 |
} elsif ($cmd eq "clipboard:paste_escaped") { |
97 |
$self->paste_escaped; |
98 |
} |
99 |
|
100 |
() |
101 |
} |
102 |
|
103 |
sub sel_grab { |
104 |
my ($self) = @_; |
105 |
|
106 |
$self->copy; |
107 |
|
108 |
() |
109 |
} |