urxvt-perls/clipboard

75 lines
1.5 KiB
Perl

#! perl -w
# Author: Bert Muennich
# Website: http://www.github.com/muennich/urxvt-perls
# License: GPLv2
# Use keyboard shortcuts to copy the selection to the clipboard and to paste
# the clipboard contents (optionally escaping all special characters).
# Requires xsel to be installed!
# Usage: put the following lines in your .Xdefaults:
# URxvt.perl-ext-common: ...,clipboard
# URxvt.keysym.M-c: perl:clipboard:copy
# URxvt.keysym.M-v: perl:clipboard:paste
# URxvt.keysym.M-C-v: perl:clipboard:paste_escaped
# The use of the functions should be self-explanatory!
use strict;
sub copy {
my ($self) = @_;
if (open(CLIPBOARD, "| xsel -ib")) {
my $sel = $self->selection();
utf8::encode($sel);
print CLIPBOARD $sel;
close(CLIPBOARD);
} else {
print STDERR "Error running xsel: $!\n";
}
()
}
sub paste {
my ($self) = @_;
my $str = `xsel -ob`;
if ($? == 0) {
$self->tt_paste($self->locale_encode($str));
} else {
print STDERR "Error running xsel: $!\n";
}
()
}
sub paste_escaped {
my ($self) = @_;
my $str = `xsel -ob`;
if ($? == 0) {
$str =~ s/([!#\$%&\*\(\) ='"\\\|\[\]`~,<>\?])/\\\1/g;
$self->tt_paste($self->locale_encode($str));
} else {
print STDERR "Error running xsel: $!\n";
}
()
}
sub on_user_command {
my ($self, $cmd) = @_;
if ($cmd eq "clipboard:copy") {
$self->copy;
} elsif ($cmd eq "clipboard:paste") {
$self->paste;
} elsif ($cmd eq "clipboard:paste_escaped") {
$self->paste_escaped;
}
()
}