xclip.pl 2.09 KB
Newer Older
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
#
# Copyright (C) 2011 by stfn <stfnmd@googlemail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

#
# Development is currently hosted at
# https://github.com/stfnm/weechat-scripts
#

use strict;
use warnings;

my %SCRIPT = (
	name => 'xclip',
	author => 'stfn <stfnmd@googlemail.com>',
29
	version => '0.2',
30 31 32 33
	license => 'GPL3',
	desc => 'Paste content from X11 clipboard',
);
my $TIMEOUT = 10 * 1000;
34
my $BUF = "";
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

weechat::register($SCRIPT{"name"}, $SCRIPT{"author"}, $SCRIPT{"version"}, $SCRIPT{"license"}, $SCRIPT{"desc"}, "", "");
weechat::hook_command($SCRIPT{"name"}, $SCRIPT{"desc"}, "", "You should bind the command to a key, e.g. \"/key bind ctrl-V /xclip\"", "", "command_cb", "");

sub command_cb
{
	my ($data, $buffer, $args) = @_;
	weechat::hook_process("xclip -o", $TIMEOUT, "process_cb", $buffer);

	return weechat::WEECHAT_RC_OK;
}

sub process_cb
{
	my ($data, $command, $return_code, $out, $err) = @_;
	my $buffer = $data;

	if ($return_code == 0 && $out) {
53 54 55
		$BUF .= $out;
		$BUF =~ s/[\t\n\r]/ /g; # strip some escape sequences
		$BUF =~ s/ {2,}/ /g; # strip multiple spaces
56 57 58

		my $input = weechat::buffer_get_string($buffer, "input");
		my $pos = weechat::buffer_get_integer($buffer, "input_pos");
59 60 61
		substr($input, $pos, 0, $BUF);
		$pos += length($BUF);
		$BUF = "";
62 63 64

		weechat::buffer_set($buffer, "input", $input);
		weechat::buffer_set($buffer, "input_pos", $pos);
65 66 67 68
	} elsif ($return_code == weechat::WEECHAT_HOOK_PROCESS_RUNNING && $out) {
		$BUF .= $out;
	} else {
		$BUF = "";
69 70 71 72
	}

	return weechat::WEECHAT_RC_OK;
}