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