summaryrefslogtreecommitdiffstats
path: root/scripts/clearable.pl
blob: 79fef1a7cd56e13af2a49654f350cf4202519364 (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
use strict;
use warnings;

our $VERSION = '0.1'; # 5ef9502616f1301
our %IRSSI = (
    contact     => 'Nei @ anti@conference.jabber.teamidiot.de',
    url         => "http://anti.teamidiot.de/",
    name	=> 'clearable',
    description	=> 'make some command output clearable',
    license	=> 'ISC',
   );

use Irssi 20140701;

sub cmd_help {
    return unless $_[0] =~ /^clearable\s*$/i;
    print CLIENTCRAP <<HELP
%9Syntax:%9

CLEARABLE <command>

%9Description:%9

    Runs command and tags each line of immediate output with the
    lastlog-flag so it can be cleared with /LASTLOG -clear

%9Example:%9

    /CLEARABLE NAMES
    /LASTLOG -clear

%9See also:%9 LASTLOG, SCROLLBACK CLEAR
HELP
}

my %refreshers;

sub sig_prt {
    my $win = $_[0]{window};
    my $view = $win && $win->view;
    return unless $view;
    my $llp = $view->{buffer}{cur_line}{_irssi}//0;
    &Irssi::signal_continue;
    $view = $win->view;
    my $l2 = $view->{buffer}{cur_line};
    return unless ($l2 && $l2->{_irssi} != $llp);
    for (my $line = $l2; $line && $line->{_irssi} != $llp; ) {
	$win->gui_printtext_after($line->prev, $line->{info}{level} | MSGLEVEL_NEVER | MSGLEVEL_LASTLOG, $line->get_text(1)."\n", $line->{info}{time});
	my $ll = $win->last_line_insert;
	$view->remove_line($line);
	$line = $ll && $ll->prev;
	$refreshers{ $win->{refnum} } //= $view->{bottom};
    }
}

sub cmd_clearable {
    my ($data, $server, $item) = @_;
    Irssi::signal_add_first('print text' => 'sig_prt');
    Irssi::signal_emit('send command'	 => Irssi::parse_special('$k').$data, $server, $item);
    Irssi::signal_remove('print text'	 => 'sig_prt');
    for my $refnum (keys %refreshers) {
	my $bottom = delete $refreshers{$refnum};
	my $win = Irssi::window_find_refnum($refnum) // next;
	my $view = $win->view;
	$win->command('^scrollback end') if $bottom && !$view->{bottom};
	$view->redraw;
    }
}

Irssi::command_bind('clearable' => 'cmd_clearable');
Irssi::command_bind_last('help' => 'cmd_help');