# Idea: Separate completion lists for different ircnets.
use strict;
use Irssi ();
use vars qw($VERSION %IRSSI @recent_words);

$VERSION = "1.0";
%IRSSI = (
    authors     => 'Simon Shine',
    contact     => 'http://shine.eu.org/',
    name        => 'complete_recent_words',
    description => 'Enables you to complete recently typed words',
    license     => 'Public domain',
);

@recent_words = ();


Irssi::settings_add_int 'completion', 'max_recent_words', 1000;
Irssi::signal_add 'send command', \&add_words;
Irssi::signal_add 'complete word', \&complete_words;

sub add_words {
    my ($text, $server, $witem) = @_;

    $text =~ s/^\s+//;
    $text =~ s/[\s.,:;]+$//;
    
    my @words = split /\s+-\s|[.,:;?!]*\s+/, $text;

    push @recent_words, @words;
    my $i = scalar(@recent_words) - Irssi::settings_get_int('max_recent_words');
    if ($i > 0) {
        shift @recent_words for (1..$i);
    }
}

sub complete_words {
    my ($complist, $window, $word, $linestart, $want_space) = @_;
    push @$complist, grep { index($_, lc $word) == 0 } @recent_words;
}
