summaryrefslogtreecommitdiffstats
path: root/scripts/chansort.pl
blob: 7f85237b33680eb95c076c4866954fc8f596bd31 (plain)
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
#
# Copyright (C) 2004-2021 by Peder Stray <peder.stray@gmail.com>
#

use strict;
use Irssi;
use Irssi::Irc;

use vars qw{$VERSION %IRSSI};
($VERSION) = '$Revision: 1.5.1 $' =~ / (\d+(\.\d+)+) /;
%IRSSI = (
	  name        => 'chansort',
	  authors     => 'Peder Stray',
	  contact     => 'peder.stray@gmail.com',
	  url         => 'https://github.com/pstray/irssi-chansort',
	  license     => 'GPL',
	  description => 'Sort all channel and query windows',
	 );

sub sig_sort_trigger {
    return unless Irssi::settings_get_bool('chansort_autosort');
    cmd_chansort();
}

# Usage: /CHANSORT
sub cmd_chansort {
    my(@windows);
    my($minwin);

    my $netonly = Irssi::settings_get_bool('chansort_netonly');

    for my $win (Irssi::windows()) {
	my $act = $win->{active};
	my $key;

	my $id = sprintf "%05d", $win->{refnum};

	if ($act->{type} eq 'CHANNEL') {
	    $key = "C".$act->{server}{tag}.' '.($netonly ? $id : substr($act->{visible_name}, 1));
	}
	elsif ($act->{type} eq 'QUERY') {
	    $key = "Q".$act->{server}{tag}.' '.($netonly ? $id : $act->{visible_name});
	}
	else {
	    next;
	}
	if (!defined($minwin) || $minwin > $win->{refnum}) {
	    $minwin = $win->{refnum};
	}
	push @windows, [ lc $key, $win ];

    }

    for (sort {$a->[0] cmp $b->[0]} @windows) {
	my($key,$win) = @$_;
	my($act) = $win->{active};

#	printf("win[%d->%d]: t[%s] [%s] [%s] {%s}\n",
#	       $win->{refnum},
#	       $minwin,
#	       $act->{type},
#	       $act->{visible_name},
#	       $act->{server}{tag},
#	       $key,
#	      );

	$win->command("window move $minwin");
	$minwin++;
    }
}

Irssi::command_bind('chansort', 'cmd_chansort');

Irssi::settings_add_bool('chansort', 'chansort_autosort', 0);
Irssi::settings_add_bool('chansort', 'chansort_netonly', 0);

Irssi::signal_add_last('window item name changed', 'sig_sort_trigger');
Irssi::signal_add_last('channel created', 'sig_sort_trigger');
Irssi::signal_add_last('query created', 'sig_sort_trigger');