html/niq.pl


   1 # BitchX TAB complete style
   2 # for irssi 0.7.99 by bd@bc-bd.org
   3 #
   4 # <tab> signal handling learned from dictcomplete by Timo Sirainen
   5 #
   6 # thx go out to fuchs, darix, dg, peder and all on #irssi who helped
   7 #
   8 #########
   9 # USAGE
  10 ###
  11 # 
  12 # In a channel window type "ab<tab>" to see a list of nicks starting
  13 # with "ab".
  14 # If you now press <tab> again, irssi will default to its own nick
  15 # completion method.
  16 # If you enter more characters you can use <tab> again to see a list
  17 # of the matching nicks, or to complete the nick if there is only
  18 # one matching.
  19 #
  20 # The last completion is saved so if you press "<tab>" with an empty
  21 # input line, you get the last completed nick.
  22 #
  23 # Now there is a statusbar item where you can see the completing
  24 # nicks instead of in the channel window. There are two ways to 
  25 # use it:
  26 #
  27 #	1) Inside another statusbar
  28 #
  29 #		/set niq_show_in_statusbar ON
  30 #		/statusbar window add -before more niq
  31 #
  32 #	2) In an own statusbar
  33 #
  34 #		/statusbar niq enable
  35 #		/statusbar niq add niq
  36 #		/statusbar niq disable
  37 #		/set niq_show_in_statusbar ON
  38 #		/set niq_own_statusbar ON
  39 #
  40 #	  You can also hide the bar when not completing nicks by using
  41 #
  42 #		/set niq_hide_on_inactive ON
  43 #
  44 #########
  45 # OPTIONS
  46 #########
  47 #
  48 # /set niq_show_in_statusbar <ON|OFF>
  49 #		* ON  : show the completing nicks in a statusbar item
  50 #		* OFF : show the nicks in the channel window
  51 #
  52 # /set niq_own_statusbar <ON|OFF>
  53 #		* ON  : use an own statusbar for the nicks
  54 #		* OFF : just use an item
  55 #
  56 # /set niq_hide_on_inactive <ON|OFF>
  57 #		* ON  : hide the own statusbar on inactivity
  58 #		* OFF : dont hide it
  59 #
  60 # /set niq_color_char <ON|OFF>
  61 #		* ON  : colors the next unlikely character
  62 #		* OFF : boring no colors
  63 #
  64 ###
  65 ################
  66 ###
  67 # Changelog
  68 #
  69 # Version 0.5.7
  70 # - use configured completion_char instead of a colon
  71 # - removed old, unused code
  72 # - fixed url
  73 # - fixed documentation leading to emtpy statusbar
  74 # - removed warning about a problem with irssi version 0.8.4
  75 #
  76 # Version 0.5.6
  77 # - work around an use problem
  78 #
  79 # Version 0.5.5
  80 # - fixed completion for nicks starting with special chars
  81 #
  82 # Version 0.5.4
  83 # - removed unneeded sort() of colored nicks
  84 # - moved colored nick generation to where it is needed
  85 # - the statusbar only worked with colorized nicks (duh!)
  86 # 
  87 # Version 0.5.3
  88 #	- stop nickcompleting if last char is the completion_char
  89 #	  which is in most cases ':'
  90 #
  91 # Version 0.5.2
  92 #	- fixed vanishing statusbar. it wrongly was reset on any
  93 #	  privmsg.
  94 #
  95 # Version 0.5.1
  96 #	- changed statusbar to be off by default since most people 
  97 #	  dont use the latest fixed version.
  98 #
  99 # Version 0.5
 100 #	- added own statusbar option
 101 #  - added color char option
 102 #
 103 # Version 0.4
 104 #	- added an niq statusbar
 105 #
 106 # Version 0.3
 107 #  - added default to irssi method on <tab><tab>
 108 #
 109 # Version 0.2 
 110 #  - added lastcomp support
 111 #
 112 # Version 0.1
 113 #  - initial release
 114 ###
 115 ################
 116 
 117 use Irssi;
 118 use Irssi::TextUI;
 119 use strict;
 120 
 121 use vars qw($VERSION %IRSSI);
 122 
 123 $VERSION="0.5.7";
 124 %IRSSI = (
 125 	authors=> 'BC-bd',
 126 	contact=> 'bd@bc-bd.org',
 127 	name=> 'niq',
 128 	description=> 'BitchX like Nickcompletion at line start plus statusbar',
 129 	license=> 'GPL v2',
 130 	url=> 'https://bc-bd.org/cgi-bin/gitweb.cgi?p=irssi.git;a=summary',
 131 );
 132 
 133 my($lastword,$lastcomp,$niqString);
 134 
 135 $lastcomp = "";
 136 $lastword = "";
 137 
 138 # build our nick with completion_char, add to complist and stop the signal
 139 sub buildNickAndStop {
 140 	my ($complist,$nick) = @_;
 141 	my $push = $nick.Irssi::settings_get_str('completion_char');
 142 		
 143 	$lastcomp = $nick;
 144 	$lastword = "";
 145 	push (@{$complist}, $push);
 146 
 147 	if (Irssi::settings_get_bool('niq_show_in_statusbar') == 1) {
 148 		drawStatusbar("");
 149 	}
 150 
 151 	Irssi::signal_stop();
 152 }
 153 
 154 # the signal handler
 155 sub sig_complete {
 156 	my ($complist, $window, $word, $linestart, $want_space) = @_;
 157 
 158 	# still allow channel- #<tab>, /set n<tab>, etc completion.
 159 	if ($linestart ne "") {
 160 		return;
 161 	}
 162 
 163 	# also back out if nothing has been entered and lastcomp is ""
 164 	if ($word eq "") {
 165 		if ($lastcomp ne "") {
 166 			buildNickAndStop($complist,$lastcomp);
 167 			return;
 168 		} else {
 169 			return;
 170 		}
 171 	}
 172 	if (rindex($word,Irssi::settings_get_str('completion_char')) == length($word) -1) {
 173 		chop($word);
 174 		buildNickAndStop($complist,$word,0);
 175 		return;
 176 	}
 177 
 178 	my $channel = $window->{active};
 179 
 180 	# the completion is ok if this is a channel
 181 	if ($channel->{type} ne "CHANNEL") 
 182 	{
 183 		return;
 184 	}
 185 
 186 	my (@nicks);
 187 
 188 	# get the matching nicks but quote this l33t special chars like ^
 189 	my $shortestNick = 999;
 190 	my $quoted = quotemeta $word;
 191 	foreach my $n ($channel->nicks()) {
 192 		if ($n->{nick} =~ /^$quoted/i && $window->{active_server}->{nick} ne $n->{nick}) {
 193 			push(@nicks,$n->{nick});
 194 			if (length($n->{nick}) < $shortestNick) {
 195 				$shortestNick = length($n->{nick});
 196 			}
 197 		}
 198 	}
 199 
 200 	@nicks = sort(@nicks);
 201 	
 202 	# if theres only one nick return it.
 203 	if (scalar @nicks eq 1)
 204 	{
 205 		buildNickAndStop($complist,$nicks[0]);
 206 	} elsif (scalar @nicks gt 1) {
 207 		# check if this is <tab> or <tab><tab>
 208 		if ($lastword eq $word) {
 209 			# <tab><tab> so default to the irssi method
 210 			sort(@nicks);
 211 			for (@nicks) {
 212 				$_ .= Irssi::settings_get_str ('completion_char');
 213 			}
 214 
 215 			push (@{$complist}, @nicks);
 216 
 217 			# but delete lastword to be ready for the next <tab>
 218 			$lastword = "";
 219 
 220 			if (Irssi::settings_get_bool('niq_show_in_statusbar') == 1) {
 221 				drawStatusbar("");
 222 			}
 223 
 224 			return;
 225 		} else {
 226 			# <tab> only so just print
 227 		
 228 			# build string w/o colored nicks
 229 			if (Irssi::settings_get_bool('niq_color_char') == 1) {
 230 				$niqString = "";
 231 				foreach my $n (@nicks) {
 232 					my $coloredNick = $n;
 233 					$coloredNick =~ s/($quoted)(.)(.*)/$1%_$2%_$3/i;
 234 					$niqString .= "$coloredNick ";
 235 				}
 236 			} else {
 237 				$niqString = join(" ",@nicks);
 238 			}
 239 			
 240 			if (Irssi::settings_get_bool('niq_show_in_statusbar') == 1) {
 241 				drawStatusbar($niqString);
 242 			} else {
 243 				$window->print($niqString);
 244 			}
 245 
 246 			Irssi::signal_stop();
 247 
 248 			# remember last word
 249 			$lastword = $word;
 250 
 251 			return;
 252 		}
 253 	} 
 254 }
 255 
 256 sub emptyBar() {
 257 	$lastword = "";
 258 	
 259 	drawStatusbar("");
 260 }
 261 
 262 sub drawStatusbar() {
 263 	my ($word) = @_;
 264 
 265 	if (Irssi::settings_get_bool('niq_own_statusbar') == 1) {
 266 		if (Irssi::settings_get_bool('niq_hide_on_inactive') == 1) {
 267 			if ($word eq "") {
 268 				Irssi::command("statusbar niq disable");
 269 			} else {
 270 				Irssi::command("statusbar niq enable");
 271 			}
 272 		}
 273 	}
 274 
 275 	$niqString = "{sb $word}";
 276 	Irssi::statusbar_items_redraw('niq');
 277 }
 278 
 279 sub niqStatusbar() {
 280 	my ($item, $get_size_only) = @_;
 281 
 282 	$item->default_handler($get_size_only, $niqString, undef, 1);
 283 }
 284 
 285 Irssi::signal_add_first('complete word', 'sig_complete');
 286 Irssi::signal_add_last('window changed', 'emptyBar');
 287 Irssi::signal_add('message own_public', 'emptyBar');
 288 
 289 Irssi::statusbar_item_register('niq', '$0', 'niqStatusbar');
 290 Irssi::statusbars_recreate_items();
 291 
 292 Irssi::settings_add_bool('misc', 'niq_show_in_statusbar', 0);
 293 Irssi::settings_add_bool('misc', 'niq_own_statusbar', 0);
 294 Irssi::settings_add_bool('misc', 'niq_hide_on_inactive', 1);
 295 Irssi::settings_add_bool('misc', 'niq_color_char', 1);