# irssi-script querymove.pl

# Description
# Problem:
#  You have open channel-windows and after them query-windows. Now you
#  join another channel and the channel- and query-windows get mixed.
# Solution:
#  querymove puts newly created queries after a certain position (by
#  default 11, variable first_query_position). Now queries are up from
#  first_query_position and channel-windows are sorted before them (or get
#  mixed again if there is not enough room before, bad luck).

use strict;
use Irssi;
use vars qw($VERSION %IRSSI);


$VERSION = "0.5";

%IRSSI = (
	  name	      => 'querymove',
          authors     => 'Wolfgang Kroener',
          contact     => 'http://www.azog.de/impressum.shtml',
          url         => 'http://www.azog.de/irssi/querymove.pl',
          license     => 'GPL',
          description => 'Allow creation of query-windows only after a certain position; Useful, if you want to have the queries at the end of the window-list.',
	 );

sub sig_query_created {
  my ($query, $auto) = @_;
  my $qwin = $query->window();
  my $first_query_position = Irssi::settings_get_int('first_query_position');

  #are there more than $first_query_position windows?
  return if ($qwin->{refnum} > $first_query_position);

  my $last_window = Irssi::windows_refnum_last();

  if ($last_window >= $first_query_position) {
    $qwin->set_refnum($last_window + 1);
  } else {
    $qwin->set_refnum($first_query_position);
  }
}

Irssi::signal_add_last('query created', 'sig_query_created');

Irssi::settings_add_int('lookandfeel', 'first_query_position',11);

