#!/usr/bin/perl =pod =encoding utf8 =head1 NAME mollyguard.pl - prevent accidental 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: This shouldn' 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, =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; ### }