Commit 4ce363d0 authored by Nei's avatar Nei Committed by Sébastien Helleu

spell_menu.pl 0.4: update to match change in menu.pl, /unset menu.var.spell.* before update

parent c37cc215
......@@ -31,6 +31,21 @@ you also need to have a menu script, if you don't have it yet:
hint: if it breaks your tab completion, you can toggle the spell
checker (default key in weechat is Alt+S)
=head1 CAVEATS
=over
=item *
on update of spell_menu from v0.3 to v0.4 you might need to do
/unset menu.var.spell.*
/script reload spell_menu
to get rid of stale entries. sorry about that.
=back
=head1 SETTINGS
the settings are usually found in the
......@@ -72,7 +87,7 @@ don't spell correct with tab if exactly at the end of a word
=cut
use constant SCRIPT_NAME => 'spell_menu';
weechat::register(SCRIPT_NAME, 'Nei <anti.teamidiot.de>', '0.3', 'GPL3', 'spell checker menu', '', '') || return;
weechat::register(SCRIPT_NAME, 'Nei <anti.teamidiot.de>', '0.4', 'GPL3', 'spell checker menu', '', '') || return;
sub SCRIPT_FILE() {
my $infolistptr = weechat::infolist_get('perl_script', '', SCRIPT_NAME);
my $filename = weechat::infolist_string($infolistptr, 'filename') if weechat::infolist_next($infolistptr);
......@@ -137,10 +152,6 @@ sub i2h {
}
$$target = $r;
my $code = qq{
local \$[=1;
\$list{"\Q$key\E"}$idx = \$r
};
$key => $list{$key}
}
else {
......@@ -154,6 +165,11 @@ sub i2h {
}
## hdh -- hdata helper
## $_[0] - arg pointer or hdata list name
## $_[1] - hdata name
## $_[2..$#_] - hdata variable name
## $_[-1] - hashref with key/value to update (optional)
## returns value of hdata, and hdata name in list ctx, or number of variables updated
sub hdh {
if (@_ > 1 && $_[0] !~ /^0x/ && $_[0] !~ /^\d+$/) {
my $arg = shift;
......@@ -162,17 +178,22 @@ sub hdh {
while (@_ > 2) {
my ($arg, $name, $var) = splice @_, 0, 3;
my $hdata = weechat::hdata_get($name);
unless (ref $var eq 'HASH') {
$var =~ s/!(.*)/weechat::hdata_get_string($hdata, $1)/e;
(my $plain_var = $var) =~ s/^\d+\|//;
my $type = weechat::hdata_get_var_type_string($hdata, $plain_var);
if ($type eq 'pointer') {
my $name = weechat::hdata_get_var_hdata($hdata, $var);
unshift @_, $name if $name;
}
(my $plain_var = $var) =~ s/^\d+\|//;
my $type = weechat::hdata_get_var_type_string($hdata, $plain_var);
if ($type eq 'pointer') {
my $name = weechat::hdata_get_var_hdata($hdata, $var);
unshift @_, $name if $name;
my $fn = "weechat::hdata_$type";
unshift @_, do { no strict 'refs';
&$fn($hdata, $arg, $var) };
}
else {
return weechat::hdata_update($hdata, $arg, $var);
}
my $fn = "weechat::hdata_$type";
unshift @_, do { no strict 'refs';
&$fn($hdata, $arg, $var) };
}
wantarray ? @_ : $_[0]
}
......@@ -218,6 +239,8 @@ sub get_settings_from_pod {
}
## mangle_man_for_wee -- turn man output into weechat codes
## @_ - list of grotty lines that should be turned into weechat attributes
## returns modified lines and modifies lines in-place
sub mangle_man_for_wee {
for (@_) {
s/_\x08(.)/weechat::color('underline').$1.weechat::color('-underline')/ge;
......@@ -249,8 +272,9 @@ sub read_manpage {
my $buf = weechat::buffer_new("man $name", '', '', '', '');
return weechat::WEECHAT_RC_OK unless $buf;
my $width = $wininfo->{'chat_width'};
--$width if $wininfo->{'chat_width'} < $wininfo->{'width'} || ($wininfo->{'width_pct'} < 100 && (grep { $_->{'y'} == $wininfo->{'y'} } Nlib::i2h('window'))[-1]{'x'} > $wininfo->{'x'});
my $width = $wininfo->{chat_width};
--$width if $wininfo->{chat_width} < $wininfo->{width} || ($wininfo->{width_pct} < 100 && (grep { $_->{y} == $wininfo->{y} } Nlib::i2h('window'))[-1]{x} > $wininfo->{x});
$width -= 2; # when prefix is shown
weechat::buffer_set($buf, 'time_for_each_line', 0);
eval qq{
......@@ -259,22 +283,22 @@ sub read_manpage {
};
die $@ if $@;
@keys = map { $_->{'key'} }
grep { $_->{'command'} eq '/input history_previous' ||
$_->{'command'} eq '/input history_global_previous' } @wee_keys;
@keys = map { $_->{key} }
grep { $_->{command} eq '/input history_previous' ||
$_->{command} eq '/input history_global_previous' } @wee_keys;
@keys = 'meta2-A' unless @keys;
weechat::buffer_set($buf, "key_bind_$_", '/window scroll -1') for @keys;
@keys = map { $_->{'key'} }
grep { $_->{'command'} eq '/input history_next' ||
$_->{'command'} eq '/input history_global_next' } @wee_keys;
@keys = map { $_->{key} }
grep { $_->{command} eq '/input history_next' ||
$_->{command} eq '/input history_global_next' } @wee_keys;
@keys = 'meta2-B' unless @keys;
weechat::buffer_set($buf, "key_bind_$_", '/window scroll +1') for @keys;
weechat::buffer_set($buf, 'key_bind_ ', '/window page_down');
@keys = map { $_->{'key'} }
grep { $_->{'command'} eq '/input delete_previous_char' } @wee_keys;
@keys = map { $_->{key} }
grep { $_->{command} eq '/input delete_previous_char' } @wee_keys;
@keys = ('ctrl-?', 'ctrl-H') unless @keys;
weechat::buffer_set($buf, "key_bind_$_", '/window page_up') for @keys;
......@@ -283,7 +307,7 @@ sub read_manpage {
weechat::buffer_set($buf, 'key_bind_q', '/buffer close');
weechat::print($buf, " \t".mangle_man_for_wee($_))
weechat::print($buf, " \t".mangle_man_for_wee($_)) # weird bug with \t\t showing nothing?
for `pod2man \Q$file\E 2>/dev/null | GROFF_NO_SGR=1 nroff -mandoc -rLL=${width}n -rLT=${width}n -Tutf8 2>/dev/null`;
weechat::command($buf, '/window scroll_top');
......@@ -306,18 +330,23 @@ sub read_manpage {
use constant ASPELL_PLANG_PWS => 1;
my %spell_menu;
init_spell_menu();
weechat::hook_config('plugins.var.perl.'.SCRIPT_NAME.'.*', 'default_options', '');
weechat::hook_command_run('/input complete_next', 'spell_menu', '');
weechat::hook_info_hashtable('spell_menu', 'spell menu content', '', 'list of 123.command and 123.name pairs for insertion into menu', 'spell_menu', '');
weechat::hook_command(SCRIPT_NAME, 'open the spell correction menu', '',
"use @{[weechat::color('bold')]}/@{[SCRIPT_NAME]} help@{[weechat::color('-bold')]} to read the manual",
'', 'spell_menu', '');
## spell_menu -- show the spell menu and fix spellings
## () - command_run or command handler
## $_[1] - buffer
## $_[1] - buffer or infohash name
## $_[2] - command or arg
sub spell_menu {
if (ref $_[2]) {
return \%spell_menu;
}
if ($_[2] =~ /^\s*help\s*$/i) {
Nlib::read_manpage(SCRIPT_FILE, SCRIPT_NAME);
return weechat::WEECHAT_RC_OK
......@@ -328,14 +357,10 @@ sub spell_menu {
my $fix = $1 if $_[2] =~ /^fix (\d+)/;
my $badword;
($badword, $sugs) = split ':', $sugs, 2;
weechat::command('', '/mute /unset menu.var.spell.*');
Encode::_utf8_on(my $q = weechat::buffer_get_string($_[1], 'input'));
my $pos = weechat::buffer_get_integer($_[1], 'input_pos');
my $rpos = index $q, $badword;
my $rpos = rindex $q, $badword, $pos;
return weechat::WEECHAT_RC_OK unless $rpos >= 0;
for (my $f = $rpos; $f >= 0 && $f <= $pos; $f = index $q, $badword, $f+1) {
$rpos = $f;
}
if ($fix) {
my $goodword = (split '[,/]', $sugs)[$fix-1];
(substr $q, $rpos, length $badword) = $goodword;
......@@ -356,6 +381,7 @@ sub spell_menu {
if weechat::config_string_to_boolean(weechat::config_get_plugin('complete_noend'))
&& $pos == $offset;
}
my %r;
my ($i, $j, $dc) = (0, 0, 0);
my %seen;
my @shortcut = (undef, 1..9, 0, 'a'..'z');
......@@ -365,15 +391,15 @@ sub spell_menu {
if ($sug eq '/') { # next dict
++$dc;
last if $dc >= @dict; # work around bug in weechat
weechat::command('', "/mute /set menu.var.spell.${j}9.name [@{[weechat::color('bold')]}$dict[$dc]@{[weechat::color('-bold')]}]");
$r{"${j}9.name"} = "[@{[weechat::color('bold')]}$dict[$dc]@{[weechat::color('-bold')]}]";
next;
}
++$i;
next if $seen{$sug}++;
$j = sprintf '%02d', $j + 1;
last if $j >= @shortcut;
weechat::command('', "/mute /set menu.var.spell.$j.command /@{[SCRIPT_NAME]} fix $i");
weechat::command('', "/mute /set menu.var.spell.$j.name &$shortcut[$j] $sug");
$r{"$j.command"} = "/@{[SCRIPT_NAME]} fix $i";
$r{"$j.name"} = "&$shortcut[$j] $sug";
}
if (@dict > 1) {
$dc = 0;
......@@ -383,14 +409,15 @@ sub spell_menu {
}
for (@dict) {
$dc = sprintf '%02d', $dc + 1;
weechat::command('', "/mute /set menu.var.spell.9$dc.command /aspell addword $dict[$dc-1] \$0");
weechat::command('', "/mute /set menu.var.spell.9$dc.name ADD($dict[$dc-1]) $badword");
$r{"9$dc.command"} = "/aspell addword $dict[$dc-1] \$0";
$r{"9$dc.name"} = "ADD($dict[$dc-1]) $badword";
}
}
else {
weechat::command('', '/mute /set menu.var.spell.9.command /aspell addword $0');
weechat::command('', "/mute /set menu.var.spell.9.name ADD $badword");
$r{'9.command'} = '/aspell addword $0';
$r{'9.name'} = "ADD $badword";
}
%spell_menu = %r;
weechat::command($_[1], "/menu spell $badword");
}
$_[2] =~ /^\// ? weechat::WEECHAT_RC_OK_EAT : weechat::WEECHAT_RC_OK
......@@ -417,5 +444,6 @@ sub init_spell_menu {
for (Nlib::get_settings_from_pod($sf)) {
weechat::config_set_desc_plugin($_, Nlib::get_desc_from_pod($sf, $_));
}
weechat::command('', '/mute /set menu.var.spell.0.command %#spell_menu% % ');
weechat::WEECHAT_RC_OK
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment