aboutsummaryrefslogtreecommitdiffstats
path: root/history-search/history_search.pl
blob: 0a2282e59eec5870bab0ed715f5303b8f7f5dd09 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# Search within your typed history as you type (like ctrl-R in bash)
# Usage:
# * First do: /bind ^R /history_search
# * Then type ctrl-R and type what you're searching for

# 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;
use Irssi::TextUI;
use Data::Dumper;

use vars qw($VERSION %IRSSI);
$VERSION = '1.0';
%IRSSI = (
    authors     => 'Wouter Coekaerts',
    contact     => 'coekie@irssi.org',
    name        => 'history_search',
    description => 'Search within your typed history as you type (like ctrl-R in bash)',
    license     => 'GPLv2 or later',
    url         => 'http://wouter.coekaerts.be/irssi/',
    changed     => '04/08/07'
);

my $prompt_append  = 1;
my $prompt_content = '';

# create a new statusbar item

Irssi::statusbar_item_register ( 'custom_prompt', 0, 'custom_prompt' );

Irssi::signal_add_last 'gui print text finished' => sub {
    Irssi::statusbar_items_redraw ( 'custom_prompt' );
};

Irssi::signal_register({'change prompt' => [qw/string int/]});
Irssi::signal_add('change prompt' => \&handle_change_prompt_sig);

sub handle_change_prompt_sig {
    my ($text, $append) = @_;
    print "sig args: ", Dumper(\@_);
    $prompt_content = $text;
    $prompt_append = $append;
    print "text: $prompt_content, append: $prompt_append";

    Irssi::statusbar_items_redraw('custom_prompt');
}

# TODO: make these work wiht subcommand (runsub)

Irssi::command_bind('set_prompt' => \&dothing );
Irssi::command_set_options('set_prompt', '+string @append');

Irssi::command_bind('install_prompt' => \&install_prompt);
Irssi::command_bind('uninstall_prompt' => \&uninstall_prompt);

Irssi::settings_add_bool('custom_prompt', 'autoinstall_custom_prompt', 0);

if (Irssi::settings_get_bool('autoinstall_custom_prompt')) {
    install_prompt();
}

sub install_prompt {
    Irssi::command("/statusbar prompt add -priority '-5' -alignment left"
                   . " -before prompt custom_prompt");
    Irssi::command("/statusbar prompt remove prompt");
    Irssi::command("/statusbar prompt remove prompt_empty");
}

sub uninstall_prompt {
    Irssi::command("/statusbar prompt remove custom_prompt");
    Irssi::command("/statusbar prompt add -priority '-1' "
                   . "-before input -alignment left prompt");
    Irssi::command("/statusbar prompt add -priority '-2' "
                   . "-before input -alignment left prompt_empty");
}

sub dothing {
    #my ($str, $append) = @_;
    #Irssi::print("str is $str, append=$append");
    my $parsed = [Irssi::command_parse_options('set_prompt', $_[0])];
    my $args = $parsed->[0] // {};
    my $remainder = $parsed->[1] // '';

    my ($str, $append) = ('', 1);
    print Dumper $args;
    if (exists ($args->{string})) {
        $str = $args->{string};
    }

    if (exists($args->{append})) {
        $append = $args->{append};
    }
    print "append in dothing: $append";
    Irssi::signal_emit('change prompt', $str, $append);
}

sub custom_prompt {
    my ($sb_item, $get_size_only) = @_;

    my ($width, $padChar, $padNum, $length);

    #my $prompt_str = '%K[%W$tag%c/%K$cumode%n$*%K]%n ';

    my $theme = Irssi::current_theme;
    my $prompt = '';

    my $var = '';
    if (window_is_empty(Irssi::active_win)) {
        $var = 'winname';
    } else {
        $var = '[.15]itemname';
    }

    if ($prompt_append) {
        $prompt = $theme->format_expand("{prompt \$$var}");
    }

    my $trailing_space = '';
    if ($prompt =~ /(\s*)$/ && length $prompt_content) {
        $trailing_space = $1;
    }

    $prompt .= $prompt_content . $trailing_space;
    $sb_item->default_handler($get_size_only, $prompt, undef, 1);
}

sub window_is_empty {
    my $window = shift;
    return $window->{name} eq $window->get_active_name;
}

sub UNLOAD {
    print Dumper(\@_);
    uninstall_prompt();
}