use strict;
use Irssi::TextUI;
use Text::Aspell;

my @misspelt;
use vars qw($VERSION %IRSSI);

$VERSION = '1.0';
%IRSSI = (
    authors     => 'Tom Wesley',
    contact     => 'tom@tomaw.org',
    name        => 'Spellcheck',
    description => 'Spellchecks the irssi input line',
    license => 'GPLv2',
    changed => '2005-12-05',
);

sub key_pressed
{
	my $speller = Text::Aspell->new;
	die unless $speller;

	my $text = Irssi::parse_special('$L');
	my @words = split(/\W+/, $text);
	use vars qw(@misspelt);
	@misspelt = ();
	foreach my $word (@words)
	{
		if (!$speller->check($word))
		{
			push(@misspelt, $word);
		}
	}
	#if (@misspelt > 0)
	#{
	#	Irssi::command('statusbar spellcheck enable');
	#	Irssi::command('statusbar spellcheck add -priority 100 -alignment left barstart');
	#	Irssi::command('statusbar spellcheck add misspelt');
	#	Irssi::command('statusbar spellcheck add -priority 100 -alignment right barend');
	#} else {
	#	Irssi::command('statusbar spellcheck disable');
	#}
	Irssi::statusbar_items_redraw('misspelt');
}

sub spellcheckMisspelt()
{
	my ($item, $get_size_only) = @_;
	use vars qw(@misspelt);
	$item->default_handler($get_size_only, "{sb %Y" . join(" ", @misspelt) . "}", undef, 1);
}

Irssi::statusbar_item_register('misspelt', '$0', 'spellcheckMisspelt');
Irssi::signal_add_last('gui key pressed', 'key_pressed');
