diff options
author | B. Watson <urchlay@slackware.uk> | 2025-01-12 12:20:45 -0500 |
---|---|---|
committer | B. Watson <urchlay@slackware.uk> | 2025-01-12 12:20:45 -0500 |
commit | 400ff3c07bcd156b28870b0635c5f2b73c49e9c8 (patch) | |
tree | 18a77bae85eb0daaba625f7ec8c7bbe27f4a828f | |
parent | e6924def82c4035f9ee520602654c5912184585f (diff) | |
download | stupid-irssi-tricks-400ff3c07bcd156b28870b0635c5f2b73c49e9c8.tar.gz |
mollyguard.pl: added (ignore Enter key right after single-quote).
-rw-r--r-- | complete_text.pl | 9 | ||||
-rw-r--r-- | mollyguard.pl | 166 |
2 files changed, 175 insertions, 0 deletions
diff --git a/complete_text.pl b/complete_text.pl index bac2fa0..8917d60 100644 --- a/complete_text.pl +++ b/complete_text.pl @@ -127,10 +127,19 @@ sub setup_changed { load_static_seen(); } +sub typo { + my $text = shift; + for(split(/\s+/, $text)) { + delete $seen{$_}; + } + @dict = keys %seen; +} + load_static_seen(); signal_add_last('setup changed', \&setup_changed); signal_add_last('complete word', \&complete_word); command_bind('dumpdict', \&dumpdict); +command_bind('typo', \&typo); for( "message public", "message private", diff --git a/mollyguard.pl b/mollyguard.pl new file mode 100644 index 0000000..ae305bd --- /dev/null +++ b/mollyguard.pl @@ -0,0 +1,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; +### } |