urxvt-perls/clipboard

116 lines
2.5 KiB
Plaintext
Raw Normal View History

#! perl -w
2012-08-19 07:56:32 -04:00
# 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!
2011-05-28 06:52:44 -04:00
# Usage: put the following lines in your .Xdefaults/.Xresources:
# 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
# Options:
# URxvt.clipboard.autocopy: If true, PRIMARY overwrites clipboard
# You can also overwrite the system commands to use for copying/pasting.
# The default ones are:
2012-08-16 05:41:33 -04:00
# URxvt.clipboard.copycmd: xsel -ib
# URxvt.clipboard.pastecmd: xsel -ob
2011-05-28 06:52:44 -04:00
# If you prefer xclip, then put these lines in your .Xdefaults/.Xresources:
2012-08-16 05:41:33 -04:00
# URxvt.clipboard.copycmd: xclip -i -selection clipboard
# URxvt.clipboard.pastecmd: xclip -o -selection clipboard
2011-05-28 06:52:44 -04:00
# On Mac OS X, put these lines in your .Xdefaults/.Xresources:
2012-08-16 05:41:33 -04:00
# URxvt.clipboard.copycmd: pbcopy
# URxvt.clipboard.pastecmd: pbpaste
# The use of the functions should be self-explanatory!
use strict;
sub on_start {
my ($self) = @_;
2012-08-16 05:41:33 -04:00
$self->{copy_cmd} = $self->x_resource('clipboard.copycmd') || 'xsel -ib';
$self->{paste_cmd} = $self->x_resource('clipboard.pastecmd') || 'xsel -ob';
2012-10-28 08:45:32 -04:00
if ($self->x_resource('clipboard.autocopy') eq 'true') {
2012-09-13 15:51:25 -04:00
$self->enable(sel_grab => \&sel_grab);
}
()
}
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) {
2012-01-03 13:30:05 -05:00
$self->tt_paste($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;
2012-01-03 13:30:05 -05:00
$self->tt_paste($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
()
}
2015-01-22 11:28:44 -05:00
sub on_action {
my ($self, $action) = @_;
on_user_command($self, "clipboard:" . $action);
}
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
()
}
2012-09-13 15:51:25 -04:00
sub sel_grab {
my ($self) = @_;
$self->copy;
()
}