1 | #! perl -w |
1 | #! perl -w |
|
|
2 | |
|
|
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 | |
2 | # Author: Bert Muennich |
8 | # Author: Bert Muennich |
3 | # Website: http://www.github.com/muennich/urxvt-perls |
9 | # Website: http://www.github.com/muennich/urxvt-perls |
4 | # License: GPLv2 |
10 | # License: GPLv2 |
5 | |
11 | |
6 | # Use keyboard shortcuts to copy the selection to the clipboard and to paste |
12 | # Use keyboard shortcuts to copy the selection to the clipboard and to paste |
… | |
… | |
32 | use strict; |
38 | use strict; |
33 | |
39 | |
34 | sub on_start { |
40 | sub on_start { |
35 | my ($self) = @_; |
41 | my ($self) = @_; |
36 | |
42 | |
37 | $self->{copy_cmd} = $self->x_resource('clipboard.copycmd') || 'xsel -ib'; |
43 | $self->{copy_cmd} = $self->x_resource ('%.copycmd' ) || 'xsel -ib'; |
38 | $self->{paste_cmd} = $self->x_resource('clipboard.pastecmd') || 'xsel -ob'; |
44 | $self->{paste_cmd} = $self->x_resource ('%.pastecmd') || 'xsel -ob'; |
39 | |
45 | |
40 | if ($self->x_resource('clipboard.autocopy') eq 'true') { |
46 | if ($self->x_resource_boolean ('%.autocopy')) { |
41 | $self->enable(sel_grab => \&sel_grab); |
47 | $self->enable (sel_grab => \&sel_grab); |
42 | } |
48 | } |
43 | |
49 | |
44 | () |
50 | () |
45 | } |
51 | } |
46 | |
52 | |
47 | sub copy { |
53 | sub copy { |
48 | my ($self) = @_; |
54 | my ($self) = @_; |
49 | |
55 | |
50 | if (open(CLIPBOARD, "| $self->{copy_cmd}")) { |
56 | if (open my $fh, "| $self->{copy_cmd}") { |
51 | my $sel = $self->selection(); |
57 | my $sel = $self->selection; |
52 | $sel = $self->locale_encode ($sel); |
58 | $sel = $self->locale_encode ($sel); |
53 | print CLIPBOARD $sel; |
59 | syswrite $fh, $sel; |
54 | close(CLIPBOARD); |
|
|
55 | } else { |
60 | } else { |
56 | print STDERR "error running '$self->{copy_cmd}': $!\n"; |
61 | print STDERR "error running '$self->{copy_cmd}': $!\n"; |
57 | } |
62 | } |
58 | |
63 | |
59 | () |
64 | () |
60 | } |
65 | } |
61 | |
66 | |
62 | sub paste { |
67 | sub paste { |
63 | my ($self, $escape) = @_; |
68 | my ($self, $escape) = @_; |
64 | |
69 | |
65 | my $str = `$self->{paste_cmd}`; |
70 | my $str = qx<$self->{paste_cmd}>; |
|
|
71 | |
66 | if ($? == 0) { |
72 | if ($? == 0) { |
67 | $str =~ s/([!#\$%&\*\(\) ='"\\\|\[\]`~,<>\?])/\\\1/g |
73 | $str =~ s/([!#\$%&\*\(\) ='"\\\|\[\]`~,<>\?])/\\\1/g |
68 | if ($escape); |
74 | if $escape; |
|
|
75 | |
69 | $self->tt_paste($str); |
76 | $self->tt_paste ($str); |
70 | } else { |
77 | } else { |
71 | print STDERR "error running '$self->{paste_cmd}': $!\n"; |
78 | print STDERR "error running '$self->{paste_cmd}': $!\n"; |
72 | } |
79 | } |
73 | |
80 | |
74 | () |
81 | () |
… | |
… | |
93 | |
100 | |
94 | $self->copy; |
101 | $self->copy; |
95 | |
102 | |
96 | () |
103 | () |
97 | } |
104 | } |
|
|
105 | |