html/dictcomplete.pl
1 use strict;
2 use vars qw($VERSION %IRSSI);
3
4 use Irssi qw(signal_add_last settings_add_bool settings_add_str
5 settings_get_bool settings_get_str);
6 $VERSION = '1.31';
7 %IRSSI = (
8 authors => 'Juerd (first version: Timo Sirainen)',
9 contact => 'juerd@juerd.nl',
10 name => 'Dictionary complete',
11 description => 'Caching dictionary based tab completion',
12 license => 'Public Domain',
13 url => 'http://juerd.nl/irssi/',
14 changed => 'Fri Dec 6 11:12 CET 2002',
15 changes => 'Removed a silly mistake'
16 );
17
18 my $file = '/usr/share/dict/words'; # file must be sorted!
19
20 my @array;
21 my %index;
22
23 {
24 my $old = '';
25 my $start = 0;
26 my $pointer = 0;
27 open(DICT, $file) or die $!;
28 while (<DICT>) {
29 chomp;
30 push @array, $_;
31 my $letter = lc substr $_, 0, 1;
32 if ($letter ne $old) {
33 $index{$old} = [ $start, $pointer - 1 ];
34 $start = $pointer;
35 }
36 $old = $letter;
37 $pointer++;
38 }
39 close DICT;
40 $index{$old} = [ $start, $pointer ];
41 }
42
43 my %cache;
44 sub sig_complete {
45 my ($complist, $window, $word, $linestart, $want_space) = @_;
46 if (defined($cache{$word})){
47 push @$complist, @{$cache{$word}};
48 return;
49 }
50
51 my $found;
52 my $mylist = [];
53 my $regex = $word =~ /[^\w-\']/;
54 return unless my $index = (($word =~ /^[^\w-\']/)
55 ? [0, $#array]
56 : $index{lc substr $word, 0, 1});
57 eval {
58 for ($index->[0] .. $index->[1]) {
59 if ($array[$_] =~ /^$word/i) {
60 $found = 1;
61 push @$complist, $array[$_];
62 push @$mylist, $array[$_];
63 } else {
64 last if $found && not $regex;
65 }
66 }
67 }; return if $@;
68
69 $cache{$word} = $mylist;
70 my $max = settings_get_str 'dictcomplete_display' or 20;
71 $window->print(@$complist > $max ? "@$complist[0..($max-1)] ..." : "@$complist")
72 unless @$complist < 2 or settings_get_bool 'dictcomplete_quiet';
73 }
74
75 signal_add_last 'complete word' => \&sig_complete;
76
77 settings_add_bool 'dictcomplete', 'dictcomplete_quiet' => 0;
78 settings_add_str 'dictcomplete', 'dictcomplete_display' => 20;