paste_huggie.pl


Code Index:


paste.pl

paste.pl - a script for irssi to manage reformatting before pasting to channels

To stop people pasting from webpages with very poor formatting this script allows you to reformat as you paste.


USAGE

Load the script then create a new unused window and paste your text into it. The defaults should be reasonable so then /paste will paste it in the current window (either by saying it to the current channel, msging it to the current recipient in a query window or by printing it up as client messages in a blank window).

Try altering paste_width (0 is the autoformatted default based on your nick length) to affect the width of the pasted text.

Alter paste_prefix to change the prefix added to each line (/set -clear paste_prefix to remove it altogether).

Set paste_showbuf to show the lines pasted into the buffer as you paste it.


AUTHOR

Send suggestions to Simon Huggins <huggie@earth.li>


   1 # Paste script for irssi
   2 # (C) Simon Huggins 2002
   3 # huggie@earth.li
   4 
   5 # Reformat pasted text ready to paste onto channels.
   6 
   7 # This program is free software; you can redistribute it and/or modify it
   8 # under the terms of the GNU General Public License as published by the Free
   9 # Software Foundation; either version 2 of the License, or (at your option)
  10 # any later version.
  11 #
  12 # This program is distributed in the hope that it will be useful, but
  13 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  14 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15 # for more details.
  16 #
  17 # You should have received a copy of the GNU General Public License along
  18 # with this program; if not, write to the Free Software Foundation, Inc., 59
  19 # Temple Place, Suite 330, Boston, MA 02111-1307  USA
  20 #
  21 
  22 use strict;
  23 
  24 use vars qw($VERSION %IRSSI);
  25     
  26 use Irssi 20020217.1542 (); # Version 0.8.1
  27 $VERSION = "0.5";
  28 %IRSSI = (
  29 authors     => "Simon Huggins",
  30 contact     => "huggie-irssi\@earth.li",
  31 name        => "Paste",
  32 description => "Paste reformats long pieces of text typically pasted into your client from webpages so that they fit nicely into your channel.  Width of client may be specified",
  33 license     => "GPLv2",
  34 url         => "http://the.earth.li/~huggie/irssi/",
  35 changed     => "Sat Mar  9 10:59:49 GMT 2002",
  36 );
  37 
  38 use Irssi::Irc;
  39 use Text::Wrap;
  40 use Text::Tabs;
  41 use POSIX qw(strftime);
  42 
  73 
  74 my $pastewin;
  75 
  76 BEGIN {
  77 	$pastewin = Irssi::window_find_name('paste');
  78 	if (!$pastewin) {
  79 		Irssi::command("window new hide");
  80 		Irssi::command("window name paste");
  81 		$pastewin = Irssi::window_find_name('paste');
  82 	}
  83 	$pastewin->print(">>> Paste your buffer here to start <<<");
  84 }
  85 
  86 Irssi::settings_add_bool("paste","paste_showbuf",0);
  87 Irssi::settings_add_int("paste","paste_width",0);
  88 Irssi::settings_add_str("paste","paste_prefix",">> ");
  89 
  90 {
  91 	my @buffer;
  92 	my $buf=0;
  93 	my $last_ts;
  94 
  95 sub event_send_text {
  96 	my ($line, $server, $windowitem) = @_;
  97 
  98 	return if $windowitem;
  99 
 100 	if ($last_ts < (time() - 60)) {
 101 		@buffer=();
 102 		$buf= 0;
 103 		$pastewin->print("Buffer cleared!");
 104 	}
 105 	$line =~ s/^\s+/ /;
 106 	$line =~ s/\s+$/ /;
 107 	$buffer[$buf] .= $line." ";
 108 
 109 	if (!$line and $buffer[$buf] ne "") {
 110 		$buf++;
 111 	}
 112 
 113 	if (Irssi::settings_get_bool("paste_showbuf")) {
 114 		$pastewin->print($line,MSGLEVEL_CLIENTCRAP);
 115 	}
 116 	$last_ts = time();
 117 
 118 	Irssi::signal_stop();
 119 }
 120 
 121 sub paste {
 122 	my ($data, $server, $witem) = @_;
 123 
 124 	my $offset;
 125 
 126 	if (!$buf and $buffer[0] eq "") {
 127 		$pastewin->print("No buffer to paste!",MSGLEVEL_HILIGHT);
 128 		return;
 129 	}
 130 
 131 	my $anyoldwin = Irssi::active_win();
 132 	my $width = Irssi::settings_get_int("paste_width");
 133 	my $prefix = Irssi::settings_get_str("paste_prefix");
 134 	my $prefixlen = length($prefix);
 135 	if ($width > 0) {
 136 		if ($width < 3+$prefixlen) {
 137 			$pastewin->print("paste_width is too small ($width<".
 138 					(3+$prefixlen).")!",
 139 					MSGLEVEL_HILIGHT);
 140 			return;
 141 		}
 142 		$Text::Wrap::columns = $width;
 143 	} else {
 144 		if ($server->{nick}) {
 145 			$offset+=length($server->{nick})+$prefixlen+15;
 146 		}
 147 		$Text::Wrap::columns = $anyoldwin->{'width'} - $offset;
 148 		if ($Text::Wrap::columns < 3+$prefixlen) {
 149 			$pastewin->print("Width would be too small (".
 150 					$Text::Wrap::columns."<".
 151 					(3+$prefixlen).", window width was ".
 152 					$anyoldwin->{'width'}.
 153 					")!",
 154 					MSGLEVEL_HILIGHT);
 155 			return;
 156 		}
 157 	}
 158 
 159 	foreach my $outbuffer (@buffer) {
 160 		$outbuffer =~ s/^\s*//;
 161 		$outbuffer =~ s/\s*$//;
 162 		$outbuffer = wrap("","", $outbuffer);
 163 		$outbuffer = expand($outbuffer);
 164 
 165 		if ($witem) {
 166 			foreach (split '\n', $outbuffer) {
 167 				$witem->command("say ".$prefix.$_);
 168 			}
 169 		} else {
 170 			foreach (split '\n', $outbuffer) {
 171 				$anyoldwin->print($prefix.$_, MSGLEVEL_HILIGHT);
 172 			}
 173 		}
 174 	}
 175 }
 176 
 177 sub clear_buffer {
 178 	@buffer = ();
 179 	$buf = 0;
 180 	$pastewin->print("Buffer cleared!");
 181 }
 182 
 183 }
 184 
 185 Irssi::signal_add_first("send text", "event_send_text");
 186 Irssi::command_bind("paste", "paste");
 187 Irssi::command_bind("clear_buffer", "clear_buffer");