aboutsummaryrefslogtreecommitdiffstats
path: root/undo/kill-ring.pl
blob: c3600a65b4bf4364178bf394a3b810f5e1cabae6 (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
# DOCUMENTATION:
#
# 
#
#
# LICENCE:
#
# Copyright (c) 2011 Tom Feist <shabble+irssi@metavore.org>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#

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        => 'kill-ring',
              description => 'keeps track of changes to the cutbuffer'
                             . ' and allows you to cycle through them',
              license     => 'MIT',
              updated     => '$DATE'
             );

my @cut_buf_history;

sub cut_buffer_init {
    Irssi::signal_add_first('gui key pressed' => \&sig_cmd_undo);
    Irssi::command_bind('cut_buf_cycle' => \&cmd_cut_buf_cycle);
}

sub add_to_history {
    my ($str) = @_;

    return unless defined $str;

    my $head = $cut_buf_history[-1];

    if (defined $head and $str ne $head) {
        push @cut_buf_history, $str;
    } elsif (not defined $head) {
        push @cut_buf_history, $str;
    }

    # enforce maximum size
    # TODO: make this a setting?

    if (@cut_buf_history > 100) {
        shift @cut_buf_history;
    }
}

sub cmd_cut_buf_cycle {
    print '%_Cut buffer contains:%_';
    foreach my $buf (@cut_buf_history) {
        print "$buf"
   }
}

sub sig_cmd_undo {
    my ($key) = @_;
    add_to_history(_cut_buf());
}

sub _cut_buf {
    return Irssi::parse_special('$U', 0, 0);
}

sub _input {
    my ($data) = @_;

    my $current_data = Irssi::parse_special('$L', 0, 0);

    # TODO: set this back up.

    # if ($settings->{utf8}->{value}) {
    #     $current_data = decode_utf8($current_data);
    # }

    if (defined $data) {
        # if ($settings->{utf8}->{value}) {
        #     Irssi::gui_input_set(encode_utf8($data));
        # } else {
        Irssi::gui_input_set($data);
        #}
    } else {
        $data = $current_data;
    }

    return $data;
}

sub _input_pos {
    my ($pos) = @_;
    my $cur_pos = Irssi::gui_input_get_pos();
    if (defined $pos) {
        Irssi::gui_input_set_pos($pos) if $pos != $cur_pos;
    } else {
        $pos = $cur_pos;
    }

    return $pos;
}

cut_buffer_init();