1use strict;
2use vars qw($VERSION %IRSSI);
3
4use Irssi;
5use Irssi::Irc;
6use Tk;
7
8$VERSION = '1.2';
9%IRSSI = (
10authors     => 'Dominic Battre',
11contact     => 'dominic@battre.de',
12name        => 'Quoting from X clipboard',
13description => 'Better quoting of content from clipboard (without leading spaces) -- requires Perl/Tk',
14license     => 'Public Domain',
15url         => 'http://www.battre.de',
16changed     => 'Fri Dec  6 23:23:31 CET 2002',
17);
18
19# if you quote long lines by selecting the text and inserting via middle
20# mousebutton you get something like this:
21# 23:12 <@DominicB> 23:11 <@DominicB> This is a very long line. This is a very
22#                   long line. This is a
23# 23:12 <@DominicB>                   very long line. This is a very long line.
24#                   This is a very long
25# 23:12 <@DominicB>                   line.
26#
27# this script queries the clipboard of X11, strips leading blanks and
28# joins lines if needed so the result would be
29# 23:16 <@DominicB> 23:11 <@DominicB> This is a very long line. This is a very
30#                   long line. This is a very long line. This is a very long
31#                   line. This is a very long line.
32#
33# just execute by /qc ("quote clipboard")
34# for print only use /qc -p
35
36
37# Known problem
38# if you
39# 1) connect via `ssh -X user@localhost`
40# 2) start `screen irssi`
41# 3) use /qc,
42# 4) disconnect ssh
43# 5) reconnect via `ssh -X user@localhost`
44# 6) `screen -R -D`
45# 7) use /qc again
46# => screen and along with it irssi terminate
47# the problem persists if you try
48# perl -e 'use Tk;print MainWindow->new->SelectionGet("-selection","CLIPBOARD")'
49# in a ssh -X/screen environment. Thus it seems to be a problem of
50# X forwarding - not of Perl/Tk
51
52# credits to
53#
54# Hugo Haas          for s/CLIPBOARD/PRIMARY/ (using PRIMARY instead of
55#                    CLIPBOARD in order to use highlighted text instead of the
56#                    X clipboard (identical to middle clicking)
57#
58# Clemens Heidinger  using Irssi::print() now if /qc is executed outside a channel/query
59#                    -p for printing only
60
61Irssi::command_bind('qc','cmd_quoteclipboard');
62
63sub cmd_quoteclipboard {
64    my ($arguments, $server, $witem) = @_;
65
66    my $main = MainWindow->new;
67    my $text = $main->SelectionGet('-selection','PRIMARY');
68    $main->destroy();
69
70    my $sendMsg = ( $arguments !~ /-p/ &&  # no parameter -p
71                     defined($witem) && $witem &&
72                    ($witem->{'type'} eq 'CHANNEL' || $witem->{'type'} eq 'QUERY') )
73                  ? sub { $server->command("msg $witem->{'name'} @_[0]"); }
74                  : sub { Irssi::print(@_[0], MSGLEVEL_CRAP); };
75
76    my $prev = "";
77
78    while ( $text =~ /^( *)(.*)$/gm ) {
79        if ( $1 eq "" and $prev ne "") {
80            $sendMsg->($prev);
81            $prev = "$2 ";
82        } else {
83            $prev .= "$2 ";
84        }
85    }
86
87    if ( $prev ne "" ) {
88        $sendMsg->($prev);
89    }
90}
91