aboutsummaryrefslogtreecommitdiffstats
path: root/feature-tests/bindings.pl
blob: ece220d23078c349197f7c311cbaa390e4b78319 (plain) (blame)
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
110
111
112
113
114
use strict;
use warnings;


use Irssi;
use Irssi::Irc;
use Irssi::TextUI;

use Data::Dumper;


our $VERSION = '0.1';
our %IRSSI = (
              authors     => 'shabble',
              contact     => 'shabble+irssi@metavore.org',
              name        => '',
              description => '',
              licence     => q(GNU GPLv2 or later),

             );

# code taken from adv_windowlist

my $keymap;

sub STATE_HEADER () { 0 }
sub STATE_BODY   () { 1 }
sub STATE_END    () { 2 }
my $parse_state = STATE_HEADER;

my $binding_formats = {};

init();

sub init {

    $keymap = {};

    Irssi::command_bind('showbinds', 'cmd_showbinds');
	Irssi::signal_add('command bind' => 'watch_keymap');

    $binding_formats = get_binding_formats();

    capture_bind_data();
}

sub get_binding_formats {
    my $theme = Irssi::current_theme();
    my @keys = qw/bind_header bind_list bind_command_list
                  bind_footer bind_unknown_id/;

    my $ret = {};
    foreach my $key (@keys) {
        my $tmp = $theme->get_format('fe-common/core', $key);
        #$tmp =~ s/%/%%/g; # escape colour codes?
        $ret->{$key} = $tmp;
    }
    return $ret;
}

sub cmd_showbinds {
    my ($args, @rest) = @_;

    my $win = Irssi::active_win();
    $win->print("Change window bindings:", Irssi::MSGLEVEL_CLIENTCRAP);
    for my $w (sort keys %$keymap) {
        my $x = $keymap->{$w};
        $win->print("$w ==> $x", Irssi::MSGLEVEL_CLIENTCRAP);
    }
    $win->print("Done showing window bindings:", Irssi::MSGLEVEL_CLIENTCRAP);

}

sub sig_print_text {
	my ($text_dest, $str, $str_stripped) = @_;

    return unless $text_dest->{level} == Irssi::MSGLEVEL_CLIENTCRAP;
    return unless $text_dest->{target} eq '';
    return unless not defined $text_dest->{server};

    # if ($parse_state = STATE_HEADER) {
    #     if ($str =~ m/\Q$binding_formats->{bind_header}\E/) {
    #         $parse_state = STATE_BODY;
    #     }
    # } elsif ($parse_state = STATE_BODY) {
    print "Data is: $str_stripped";
    if ($str_stripped =~ m/^.*?(\S{,20})\s+(\S+)\s+(\S+)/) {
        $keymap->{$1} = "$2, $3";
        print "Parsed $1 as $2, $3";
    }
    Irssi::signal_stop();
    #     } elsif ($str =~ m/$binding_formats->{bind_footer}\E/) {
    #         $parse_state = STATE_END;
    #     }
    # }
}


sub capture_bind_data {
	Irssi::signal_remove('command bind' => 'watch_keymap');
	Irssi::signal_add_first('print text' => 'sig_print_text');
	Irssi::command('bind'); # stolen from grep
	Irssi::signal_remove('print text' => 'sig_print_text');

}


# watch keymap changes
sub watch_keymap {
	Irssi::timeout_add_once(1000, 'capture_bind_data', undef);
}