aboutsummaryrefslogtreecommitdiff
path: root/mollyguard.pl
blob: ae305bd9f084056d101a8b6d1b51fc47de828932 (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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/perl

=pod

=encoding utf8

=head1 NAME

mollyguard.pl - prevent accidental <enter> keypress after single-quote

=head1 SYNOPSIS

/script load mollyguard.pl

=head1 DESCRIPTION

When this script is loaded, the Enter key will be ignored for a short
period after the ' (single quote) key has been pressed. This exists
because at least once a day I try to type ' and end up hitting both
keys, resulting in:

  <Urchlay> This shouldn'
  <Urchlay> t have been split into 2 lines.

With the plugin loaded, it won't happen... although it's still
possible to hit Enter first, then single-quote (I don't seem to do
that very often).

Pressing Enter twice always works, regardless of the timeout.

=head1 COMMANDS

=over 4

=item /mollyguard_help

Prints this help.

=back

=head1 SETTINGS

=over 4

=item mollyguard_delay

Milliseconds; ignore Enter key for this long after ' was
typed. Default is 250 (aka 1/4 sec).

=item mollyguard_enable

Boolean; set to OFF to disable the script. Default is ON.

=item mollyguard_debug

Boolean; set to ON to have the script print what it's doing in great
detail. Default is OFF.

=back

=head1 AUTHOR

Urchlay on Libera, <urchlay@slackware.uk>

=head1 LICENSE

WTFPL: Do WTF you want with this.

=cut

use warnings;
use Time::HiRes qw/gettimeofday/;
use Irssi qw/settings_get_int settings_get_bool
             settings_set_int settings_set_bool
				 settings_add_int settings_add_bool
             timeout_add_once timeout_remove
             signal_stop signal_continue command_bind command
             signal_add_last signal_register signal_add_first/;

our $VERSION = "0.2";
our %IRSSI = (
	authors     => 'Urchlay',
	contact     => 'Urchlay on Libera',
	name        => 'mollyguard',
	description => 'Prevent annoying typo (Enter after single-quote)',
	license     => 'WTFPL',
	url         => 'https://slackware.uk/~urchlay/repos/stupid-irssi-tricks',
);

our $SELF = $IRSSI{name};

our $default_delay = 250;

our $last_squote_time;

our $enabled;
our $delay;
our $DEBUG;

sub debug {
	my (undef, $file, $line) = caller;
	$file =~ s,.*\/,,;
	Irssi::print("$file:$line: " . join(" ", @_)) if $DEBUG;
}

sub handle_keypress {
	my $key = shift;

	if(!$enabled) {
		signal_continue($key);
		return;
	}

	if($key == 0x0a || $key == 0x0d) {
		debug("enter pressed");
		if($last_squote_time) {
			my $tod = gettimeofday();
			my $interval = $tod - $last_squote_time;
			if($interval < $delay) {
				debug("IGNORED enter @ $tod, $interval < $delay");
				undef $last_squote_time;
				signal_stop();
				return;
			} else {
				debug("ALLOWED enter @ $tod, $interval >= $delay");
			}
		}
	}

	if($key == 0x27) {
		$last_squote_time = gettimeofday();
		debug("single-quote pressed @ $last_squote_time");
	} else {
		undef $last_squote_time;
	}

	signal_continue($key);
}

sub init {
	$DEBUG = settings_get_bool('mollyguard_debug');
	$delay = settings_get_int('mollyguard_delay');
	if($delay < 1) {
		Irssi::print "$SELF: mollyguard_delay must be >= 0; using default ($default_delay)";
		$delay = $default_delay;
		settings_set_int('mollyguard_delay', $delay);
	}
	$enabled = settings_get_bool('mollyguard_enable');
	Irssi::print "$SELF: mollyguard_delay $delay, mollyguard_enable $enabled, /mollyguard_help for help";
	$delay /= 1000; # we want seconds
}

sub help {
	command("/exec - pod2text " . __FILE__);
}

### main() {
settings_add_int($SELF, 'mollyguard_delay', $default_delay);
settings_add_bool($SELF, 'mollyguard_enable', 1);
settings_add_bool($SELF, 'mollyguard_debug', 0);
signal_register({ "gui key pressed", [ "integer" ] });
signal_add_first("gui key pressed", \&handle_keypress);
signal_add_last('setup changed', \&init);
command_bind("mollyguard_help", \&help);
init;
### }