urxvt-perls/clipboard

95 lines
2.1 KiB
Plaintext
Raw Normal View History

#! 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
# You can also overwrite the system commands to use for copying/pasting.
# The default ones are:
# URxvt.copyCommand: xsel -ib
# URxvt.pasteCommand: xsel -ob
# If you prefer xclip, then put these lines in your .Xdefaults:
# URxvt.copyCommand: xclip -i -selection clipboard
# URxvt.pasteCommand: xclip -o -selection clipboard
# On Mac OS X, put these lines in your .Xdefaults:
# URxvt.copyCommand: pbcopy
# URxvt.pasteCommand: pbpaste
# The use of the functions should be self-explanatory!
use strict;
sub on_start {
my ($self) = @_;
$self->{copy_cmd} = $self->x_resource('copyCommand') || 'xsel -ib';
$self->{paste_cmd} = $self->x_resource('pasteCommand') || 'xsel -ob';
()
}
sub copy {
2011-01-14 17:28:23 -05:00
my ($self) = @_;
if (open(CLIPBOARD, "| $self->{copy_cmd}")) {
2011-01-14 17:42:17 -05:00
my $sel = $self->selection();
utf8::encode($sel);
print CLIPBOARD $sel;
close(CLIPBOARD);
} else {
print STDERR "error running '$self->{copy_cmd}': $!\n";
2011-01-14 17:42:17 -05:00
}
2011-01-14 17:28:23 -05:00
()
}
sub paste {
2011-01-14 17:28:23 -05:00
my ($self) = @_;
my $str = `$self->{paste_cmd}`;
2011-01-14 17:42:17 -05:00
if ($? == 0) {
$self->tt_paste($self->locale_encode($str));
2011-01-14 17:42:17 -05:00
} else {
print STDERR "error running '$self->{paste_cmd}': $!\n";
2011-01-14 17:42:17 -05:00
}
2011-01-14 17:28:23 -05:00
()
}
sub paste_escaped {
2011-01-14 17:28:23 -05:00
my ($self) = @_;
my $str = `$self->{paste_cmd}`;
2011-01-14 17:42:17 -05:00
if ($? == 0) {
$str =~ s/([!#\$%&\*\(\) ='"\\\|\[\]`~,<>\?])/\\\1/g;
$self->tt_paste($self->locale_encode($str));
2011-01-14 17:42:17 -05:00
} else {
print STDERR "error running '$self->{paste_cmd}': $!\n";
2011-01-14 17:42:17 -05:00
}
2011-01-14 17:28:23 -05:00
()
}
sub on_user_command {
2011-01-14 17:28:23 -05:00
my ($self, $cmd) = @_;
2011-01-14 17:28:23 -05:00
if ($cmd eq "clipboard:copy") {
$self->copy;
} elsif ($cmd eq "clipboard:paste") {
$self->paste;
} elsif ($cmd eq "clipboard:paste_escaped") {
$self->paste_escaped;
}
2011-01-14 17:28:23 -05:00
()
}