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