html/per_window_prompt.pl


   1 # Keeps a prompt per window.
   2 
   3 # Copyright 2007  Wouter Coekaerts <coekie@irssi.org>
   4 #
   5 # This program is free software; you can redistribute it and/or modify
   6 # it under the terms of the GNU General Public License as published by
   7 # the Free Software Foundation; either version 2 of the License, or
   8 # (at your option) any later version.
   9 #
  10 # This program is distributed in the hope that it will be useful,
  11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13 # GNU General Public License for more details.
  14 #
  15 # You should have received a copy of the GNU General Public License
  16 # along with this program; if not, write to the Free Software
  17 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  18 
  19 use strict;
  20 use Irssi 20070804;
  21 use Irssi::TextUI;
  22 
  23 use vars qw($VERSION %IRSSI);
  24 $VERSION = '1.0';
  25 %IRSSI = (
  26     authors     => 'Wouter Coekaerts',
  27     contact     => 'coekie@irssi.org',
  28     name        => 'per_window_prompt',
  29     description => 'Keeps a prompt per window',
  30     license     => 'GPLv2 or later',
  31     url         => 'http://wouter.coekaerts.be/irssi/',
  32     changed     => '04/08/07'
  33 );
  34 
  35 my %prompts;
  36 
  37 my $in_command = 0;
  38 my $win_before_command;
  39 my $win_before_command_deleted;
  40 my $unloading = 0;
  41 
  42 # key to use to identify window
  43 sub winkey {
  44 	my ($win) = @_;
  45 	return defined($win) ? $win->{'_irssi'} : 0;
  46 }
  47 
  48 sub get_prompt {
  49 	return {
  50 			text => Irssi::parse_special('$L'),
  51 			pos => Irssi::gui_input_get_pos()
  52 	};
  53 }
  54 
  55 sub set_prompt {
  56 	my ($prompt) = @_;
  57 	if (defined($prompt)) {
  58 		Irssi::gui_input_set($prompt->{text});
  59 		Irssi::gui_input_set_pos($prompt->{pos});
  60 	} else {
  61 		Irssi::gui_input_set('');
  62 	}	
  63 }
  64 
  65 Irssi::signal_add_last 'window changed' => sub {
  66 	my ($win, $oldwin)= @_;
  67 	if (!$in_command) {
  68 		if ($oldwin) {
  69 			$prompts{winkey($oldwin)} = get_prompt();
  70 		}
  71 		set_prompt($prompts{winkey($win)});
  72 	}
  73 };
  74 
  75 sub UNLOAD {
  76 	$unloading = 1;
  77 }
  78 
  79 # needed when switching windows by command
  80 Irssi::signal_add_first 'gui key pressed' => sub {
  81 	my ($key) = @_;
  82 	
  83 	if ($key == 10 && ! $in_command) {
  84 		$win_before_command = winkey(Irssi::active_win);
  85 		$win_before_command_deleted = 0;
  86 		$in_command = 1;
  87 		Irssi::signal_continue(@_);
  88 		if ($unloading) {
  89 			return; # avoid crash when unloading by command
  90 		}
  91 		$in_command = 0;
  92 		my $win_after_command = winkey(Irssi::active_win);
  93 		
  94 		if ($win_before_command != $win_after_command) {
  95 			if (! $win_before_command_deleted) {
  96 				$prompts{$win_before_command} = get_prompt();
  97 			}
  98 			set_prompt($prompts{$win_after_command})
  99 		}
 100 	}
 101 };
 102 
 103 Irssi::signal_add_first 'window destroyed' => sub {
 104 	my ($win) = @_;
 105 	delete $prompts{winkey($win)};
 106 	if ($win_before_command == winkey($win)) {
 107 		$win_before_command_deleted = 1;
 108 	}
 109 };