summaryrefslogtreecommitdiff
path: root/urxvt/ext/clipboard
blob: 05e1601ab5ecfd96d53dda66d1994ace394a93de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#! 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/.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:
#   URxvt.clipboard.copycmd:  xsel -ib
#   URxvt.clipboard.pastecmd: xsel -ob
# If you prefer xclip, then put these lines in your .Xdefaults/.Xresources:
#   URxvt.clipboard.copycmd:  xclip -i -selection clipboard
#   URxvt.clipboard.pastecmd: xclip -o -selection clipboard
# On Mac OS X, put these lines in your .Xdefaults/.Xresources:
#   URxvt.clipboard.copycmd:  pbcopy
#   URxvt.clipboard.pastecmd: pbpaste

# The use of the functions should be self-explanatory!

use strict;

sub on_start {
	my ($self) = @_;

	$self->{copy_cmd} = $self->x_resource('clipboard.copycmd') || 'xsel -ib';
	$self->{paste_cmd} = $self->x_resource('clipboard.pastecmd') || 'xsel -ob';

	if ($self->x_resource('clipboard.autocopy') eq 'true') {
		$self->enable(sel_grab => \&sel_grab);
	}

	()
}

sub copy {
	my ($self) = @_;

	if (open(CLIPBOARD, "| $self->{copy_cmd}")) {
		my $sel = $self->selection();
		utf8::encode($sel);
		print CLIPBOARD $sel;
		close(CLIPBOARD);
	} else {
		print STDERR "error running '$self->{copy_cmd}': $!\n";
	}

	()
}

sub paste {
	my ($self) = @_;

	my $str = `$self->{paste_cmd}`;
	if ($? == 0) {
		$self->tt_paste($str);
	} else {
		print STDERR "error running '$self->{paste_cmd}': $!\n";
	}

	()
}

sub paste_escaped {
	my ($self) = @_;

	my $str = `$self->{paste_cmd}`;
	if ($? == 0) {
		$str =~ s/([!#\$%&\*\(\) ='"\\\|\[\]`~,<>\?])/\\\1/g;
		$self->tt_paste($str);
	} else {
		print STDERR "error running '$self->{paste_cmd}': $!\n";
	}

	()
}

sub on_action {
    my ($self, $action) = @_;

    on_user_command($self, "clipboard:" . $action);
}

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;
	}

	()
}

sub sel_grab {
	my ($self) = @_;

	$self->copy;

	()
}