all repos — dotfiles @ d55c5f6f5c96fb57f45bdf1712feb30cb94d0795

my *nix dotfiles

weechat/perl/iset.pl (view raw)

   1#
   2# Copyright (C) 2008-2017 Sebastien Helleu <flashcode@flashtux.org>
   3# Copyright (C) 2010-2017 Nils Görs <weechatter@arcor.de>
   4#
   5# This program is free software; you can redistribute it and/or modify
   6# it under the terms of the GNU General Public License as published by
   7# the Free Software Foundation; either version 3 of the License, or
   8# (at your option) any later version.
   9#
  10# This program is distributed in the hope that it will be useful,
  11# but WITHOUT ANY WARRANTY; without even the implied warranty of
  12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13# GNU General Public License for more details.
  14#
  15# You should have received a copy of the GNU General Public License
  16# along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17#
  18# Set WeeChat and plugins options interactively.
  19#
  20# History:
  21#
  22# 2017-04-14, nils_2 <freenode.#weechat>
  23#     version 4.3: add option "use_color" (https://github.com/weechat/scripts/issues/93)
  24# 2016-07-08, nils_2 <weechatter@arcor.de>
  25#     version 4.2: add diff function
  26# 2016-02-06, Sebastien Helleu <flashcode@flashtux.org>:
  27#     version 4.1: remove debug print
  28# 2015-12-24, Sebastien Helleu <flashcode@flashtux.org>:
  29#     version 4.0: add support of parent options (inherited values in irc servers)
  30#                  with WeeChat >= 1.4
  31# 2015-05-16, Sebastien Helleu <flashcode@flashtux.org>:
  32#     version 3.9: fix cursor position when editing an option with WeeChat >= 1.2
  33# 2015-05-02, arza <arza@arza.us>:
  34#     version 3.8: don't append "null" to /set when setting an undefined setting
  35# 2015-05-01, nils_2 <weechatter@arcor.de>:
  36#     version 3.7: fix two perl warnings (reported by t3chguy)
  37# 2014-09-30, arza <arza@arza.us>:
  38#     version 3.6: fix current line counter when options aren't found
  39# 2014-06-03, nils_2 <weechatter@arcor.de>:
  40#     version 3.5: add new option "use_mute"
  41# 2014-01-30, stfn <stfnmd@gmail.com>:
  42#     version 3.4: add new options "color_value_diff" and "color_value_diff_selected"
  43# 2014-01-16, luz <ne.tetewi@gmail.com>:
  44#     version 3.3: fix bug with column alignment in iset buffer when option
  45#                  name contains unicode characters
  46# 2013-08-03, Sebastien Helleu <flashcode@flashtux.org>:
  47#     version 3.2: allow "q" as input in iset buffer to close it
  48# 2013-07-14, Sebastien Helleu <flashcode@flashtux.org>:
  49#     version 3.1: remove unneeded calls to iset_refresh() in mouse callback
  50#                  (faster mouse actions when lot of options are displayed),
  51#                  fix bug when clicking on a line after the last option displayed
  52# 2013-04-30, arza <arza@arza.us>:
  53#     version 3.0: simpler title, fix refresh on unset
  54# 2012-12-16, nils_2 <weechatter@arcor.de>:
  55#     version 2.9: fix focus window with iset buffer on mouse click
  56# 2012-08-25, nils_2 <weechatter@arcor.de>:
  57#     version 2.8: most important key and mouse bindings for iset buffer added to title-bar (idea The-Compiler)
  58# 2012-07-31, nils_2 <weechatter@arcor.de>:
  59#     version 2.7: add combined option and value search (see /help iset)
  60#                : add exact value search (see /help iset)
  61#                : fix problem with metacharacter in value search
  62#                : fix use of uninitialized value for unset option and reset value of option
  63# 2012-07-25, nils_2 <weechatter@arcor.de>:
  64#     version 2.6: switch to iset buffer (if existing) when command /iset is called with arguments
  65# 2012-03-17, Sebastien Helleu <flashcode@flashtux.org>:
  66#     version 2.5: fix check of sections when creating config file
  67# 2012-03-09, Sebastien Helleu <flashcode@flashtux.org>:
  68#     version 2.4: fix reload of config file
  69# 2012-02-02, nils_2 <weechatter@arcor.de>:
  70#     version 2.3: fixed: refresh problem with new search results and cursor was outside window.
  71#                : add: new option "current_line" in title bar
  72#     version 2.2: fixed: refresh error when toggling plugins description
  73# 2011-11-05, nils_2 <weechatter@arcor.de>:
  74#     version 2.1: use own config file (iset.conf), fix own help color (used immediately)
  75# 2011-10-16, nils_2 <weechatter@arcor.de>:
  76#     version 2.0: add support for left-mouse-button and more sensitive mouse gesture (for integer/color options)
  77#                  add help text for mouse support
  78# 2011-09-20, Sebastien Helleu <flashcode@flashtux.org>:
  79#     version 1.9: add mouse support, fix iset buffer, fix errors on first load under FreeBSD
  80# 2011-07-21, nils_2 <weechatter@arcor.de>:
  81#     version 1.8: added: option "show_plugin_description" (alt+p)
  82#                  fixed: typos in /help iset (lower case for alt+'x' keys)
  83# 2011-05-29, nils_2 <weechatter@arcor.de>:
  84#     version 1.7: added: version check for future needs
  85#                  added: new option (scroll_horiz) and usage of scroll_horiz function (weechat >= 0.3.6 required)
  86#                  fixed: help_bar did not pop up immediately using key-shortcut
  87# 2011-02-19, nils_2 <weechatter@arcor.de>:
  88#     version 1.6: added: display of all possible values in help bar (show_help_extra_info)
  89#                  fixed: external user options never loaded when starting iset first time
  90# 2011-02-13, Sebastien Helleu <flashcode@flashtux.org>:
  91#     version 1.5: use new help format for command arguments
  92# 2011-02-03, nils_2 <weechatter@arcor.de>:
  93#     version 1.4: fixed: restore value filter after /upgrade using buffer local variable.
  94# 2011-01-14, nils_2 <weechatter@arcor.de>:
  95#     version 1.3: added function to search for values (option value_search_char).
  96#                  code optimization.
  97# 2010-12-26, Sebastien Helleu <flashcode@flashtux.org>:
  98#     version 1.2: improve speed of /upgrade when iset buffer is open,
  99#                  restore filter used after /upgrade using buffer local variable,
 100#                  use /iset filter argument if buffer is open.
 101# 2010-11-21, drubin <drubin+weechat@smartcube.co.za>:
 102#     version 1.1.1: fix bugs with cursor position
 103# 2010-11-20, nils_2 <weechatter@arcor.de>:
 104#     version 1.1: cursor position set to value
 105# 2010-08-03, Sebastien Helleu <flashcode@flashtux.org>:
 106#     version 1.0: move misplaced call to infolist_free()
 107# 2010-02-02, rettub <rettub@gmx.net>:
 108#     version 0.9: turn all the help stuff off if option 'show_help_bar' is 'off',
 109#                  new key binding <alt>-<v> to toggle help_bar and help stuff on/off
 110# 2010-01-30, nils_2 <weechatter@arcor.de>:
 111#     version 0.8: fix error when option does not exist
 112# 2010-01-24, Sebastien Helleu <flashcode@flashtux.org>:
 113#     version 0.7: display iset bar only on iset buffer
 114# 2010-01-22, nils_2 <weechatter@arcor.de> and drubin:
 115#     version 0.6: add description in a bar, fix singular/plural bug in title bar,
 116#                  fix selected line when switching buffer
 117# 2009-06-21, Sebastien Helleu <flashcode@flashtux.org>:
 118#     version 0.5: fix bug with iset buffer after /upgrade
 119# 2009-05-02, Sebastien Helleu <flashcode@flashtux.org>:
 120#     version 0.4: sync with last API changes
 121# 2009-01-04, Sebastien Helleu <flashcode@flashtux.org>:
 122#     version 0.3: open iset buffer when /iset command is executed
 123# 2009-01-04, Sebastien Helleu <flashcode@flashtux.org>:
 124#     version 0.2: use null values for options, add colors, fix refresh bugs,
 125#                  use new keys to reset/unset options, sort options by name,
 126#                  display number of options in buffer's title
 127# 2008-11-05, Sebastien Helleu <flashcode@flashtux.org>:
 128#     version 0.1: first official version
 129# 2008-04-19, Sebastien Helleu <flashcode@flashtux.org>:
 130#     script creation
 131
 132use strict;
 133
 134my $PRGNAME = "iset";
 135my $VERSION = "4.3";
 136my $DESCR   = "Interactive Set for configuration options";
 137my $AUTHOR  = "Sebastien Helleu <flashcode\@flashtux.org>";
 138my $LICENSE = "GPL3";
 139my $LANG    = "perl";
 140my $ISET_CONFIG_FILE_NAME = "iset";
 141
 142my $iset_config_file;
 143my $iset_buffer = "";
 144my $wee_version_number = 0;
 145my @iset_focus = ();
 146my @options_names = ();
 147my @options_parent_names = ();
 148my @options_types = ();
 149my @options_values = ();
 150my @options_default_values = ();
 151my @options_parent_values = ();
 152my @options_is_null = ();
 153my $option_max_length = 0;
 154my $current_line = 0;
 155my $filter = "*";
 156my $description = "";
 157my $options_name_copy = "";
 158my $iset_filter_title = "";
 159# search modes: 0 = index() on value, 1 = grep() on value, 2 = grep() on option, 3 = grep on option & value, 4 = diff all, 5 = diff parts
 160my $search_mode = 2;
 161my $search_value = "";
 162my $help_text_keys = "alt + space: toggle, +/-: increase/decrease, enter: change, ir: reset, iu: unset, v: toggle help bar";
 163my $help_text_mouse = "Mouse: left: select, right: toggle/set, right + drag left/right: increase/decrease";
 164my %options_iset;
 165
 166my %mouse_keys = ("\@chat(perl.$PRGNAME):button1" => "hsignal:iset_mouse",
 167                  "\@chat(perl.$PRGNAME):button2*" => "hsignal:iset_mouse",
 168                  "\@chat(perl.$PRGNAME):wheelup" => "/repeat 5 /iset **up",
 169                  "\@chat(perl.$PRGNAME):wheeldown" => "/repeat 5 /iset **down");
 170
 171
 172sub iset_title
 173{
 174    if ($iset_buffer ne "")
 175    {
 176        my $current_line_counter = "";
 177        if (weechat::config_boolean($options_iset{"show_current_line"}) == 1)
 178        {
 179            if (@options_names eq 0)
 180            {
 181                $current_line_counter = "0/";
 182            }
 183            else
 184            {
 185                $current_line_counter = ($current_line + 1) . "/";
 186            }
 187        }
 188        my $show_filter = "";
 189        if ($search_mode eq 0)
 190        {
 191            $iset_filter_title = "(value) ";
 192            $show_filter = $search_value;
 193            if ( substr($show_filter,0,1) eq weechat::config_string($options_iset{"value_search_char"}) )
 194            {
 195                $show_filter = substr($show_filter,1,length($show_filter));
 196            }
 197        }
 198        elsif ($search_mode eq 1)
 199        {
 200            $iset_filter_title = "(value) ";
 201            $show_filter = "*".$search_value."*";
 202        }
 203        elsif ($search_mode eq 2)
 204        {
 205            $iset_filter_title = "";
 206            $filter = "*" if ($filter eq "");
 207            $show_filter = $filter;
 208        }
 209        elsif ($search_mode == 4 or $search_mode == 5)
 210        {
 211            $iset_filter_title = "diff: ";
 212            $show_filter = "all";
 213            $show_filter = $search_value if $search_mode == 5;
 214        }
 215        elsif ($search_mode eq 3)
 216        {
 217            $iset_filter_title = "(option) ";
 218            $show_filter = $filter
 219                            .weechat::color("default")
 220                            ." / (value) "
 221                            .weechat::color("yellow")
 222                            ."*".$search_value."*";
 223        }
 224        weechat::buffer_set($iset_buffer, "title",
 225                             $iset_filter_title
 226                            .weechat::color("yellow")
 227                            .$show_filter
 228                            .weechat::color("default")." | "
 229                            .$current_line_counter
 230                            .@options_names
 231                            ." | "
 232                            .$help_text_keys
 233                            ." | "
 234                            .$help_text_mouse);
 235    }
 236}
 237
 238sub iset_create_filter
 239{
 240    $filter = $_[0];
 241    if ( $search_mode == 3 )
 242    {
 243        my @cmd_array = split(/ /,$filter);
 244        my $array_count = @cmd_array;
 245        $filter = $cmd_array[0];
 246        $filter = $cmd_array[0] . " " . $cmd_array[1] if ( $array_count >2 );
 247    }
 248    $filter = "$1.*" if ($filter =~ /f (.*)/); # search file
 249    $filter = "*.$1.*" if ($filter =~ /s (.*)/); # search section
 250    if ((substr($filter, 0, 1) ne "*") && (substr($filter, -1, 1) ne "*"))
 251    {
 252        $filter = "*".$filter."*";
 253    }
 254    if ($iset_buffer ne "")
 255    {
 256        weechat::buffer_set($iset_buffer, "localvar_set_iset_filter", $filter);
 257    }
 258}
 259
 260sub iset_buffer_input
 261{
 262    my ($data, $buffer, $string) = ($_[0], $_[1], $_[2]);
 263
 264    # string begins with space?
 265    return weechat::WEECHAT_RC_OK if (substr($string, 0, 1 ) eq " ");
 266
 267    if ($string eq "q")
 268    {
 269        weechat::buffer_close($buffer);
 270        return weechat::WEECHAT_RC_OK;
 271    }
 272    $search_value = "";
 273    my @cmd_array = split(/ /,$string);
 274    my $array_count = @cmd_array;
 275    my $string2 = substr($string, 0, 1);
 276    if ($string2 eq weechat::config_string($options_iset{"value_search_char"})
 277    or (defined $cmd_array[0] and $cmd_array[0] eq weechat::config_string($options_iset{"value_search_char"}).weechat::config_string($options_iset{"value_search_char"})) )
 278    {
 279        $search_mode = 1;
 280        $search_value = substr($string, 1);
 281        iset_get_values($search_value);
 282        if ($iset_buffer ne "")
 283        {
 284            weechat::buffer_set($iset_buffer, "localvar_set_iset_search_value", $search_value);
 285        }
 286    }
 287    # show all diff values
 288    elsif ($string eq "d")
 289    {
 290        $search_mode = 4;
 291#        iset_title();
 292        iset_create_filter("*");
 293        iset_get_options("*");
 294    }
 295    elsif ( $array_count >= 2 and $cmd_array[0] eq "d")
 296    {
 297        $search_mode = 5;
 298        $search_value = substr($cmd_array[1], 0);  # cut value_search_char
 299        $search_value = substr($cmd_array[2], 0) if ( $array_count > 2);  # cut value_search_char
 300        iset_create_filter($search_value);
 301        iset_get_options($search_value);
 302
 303    }
 304    else
 305    {
 306        $search_mode = 2;
 307        if ( $array_count >= 2 and $cmd_array[0] ne "f" or $cmd_array[0] ne "s" )
 308        {
 309            if ( defined $cmd_array[1] and substr($cmd_array[1], 0, 1) eq weechat::config_string($options_iset{"value_search_char"})
 310            or defined $cmd_array[2] and substr($cmd_array[2], 0, 1) eq weechat::config_string($options_iset{"value_search_char"}) )
 311            {
 312                $search_mode = 3;
 313                $search_value = substr($cmd_array[1], 1);  # cut value_search_char
 314                $search_value = substr($cmd_array[2], 1) if ( $array_count > 2);  # cut value_search_char
 315            }
 316        }
 317        if ( $search_mode == 3)
 318        {
 319            iset_create_filter($string);
 320            iset_get_options($search_value);
 321        }
 322        else
 323        {
 324            iset_create_filter($string);
 325            iset_get_options("");
 326        }
 327    }
 328    weechat::buffer_set($iset_buffer, "localvar_set_iset_search_mode", $search_mode);
 329    weechat::buffer_clear($buffer);
 330    $current_line = 0;
 331    iset_refresh();
 332    return weechat::WEECHAT_RC_OK;
 333}
 334
 335sub iset_buffer_close
 336{
 337    $iset_buffer = "";
 338
 339    return weechat::WEECHAT_RC_OK;
 340}
 341
 342sub iset_init
 343{
 344    $current_line = 0;
 345    $iset_buffer = weechat::buffer_search($LANG, $PRGNAME);
 346    if ($iset_buffer eq "")
 347    {
 348        $iset_buffer = weechat::buffer_new($PRGNAME, "iset_buffer_input", "", "iset_buffer_close", "");
 349    }
 350    else
 351    {
 352        my $new_filter = weechat::buffer_get_string($iset_buffer, "localvar_iset_filter");
 353        $search_mode = weechat::buffer_get_string($iset_buffer, "localvar_iset_search_mode");
 354        $search_value = weechat::buffer_get_string($iset_buffer, "localvar_iset_search_value");
 355        $filter = $new_filter if ($new_filter ne "");
 356    }
 357    if ($iset_buffer ne "")
 358    {
 359        weechat::buffer_set($iset_buffer, "type", "free");
 360        iset_title();
 361        weechat::buffer_set($iset_buffer, "key_bind_ctrl-L",        "/iset **refresh");
 362        weechat::buffer_set($iset_buffer, "key_bind_meta2-A",       "/iset **up");
 363        weechat::buffer_set($iset_buffer, "key_bind_meta2-B",       "/iset **down");
 364        weechat::buffer_set($iset_buffer, "key_bind_meta2-23~",     "/iset **left");
 365        weechat::buffer_set($iset_buffer, "key_bind_meta2-24~" ,    "/iset **right");
 366        weechat::buffer_set($iset_buffer, "key_bind_meta- ",        "/iset **toggle");
 367        weechat::buffer_set($iset_buffer, "key_bind_meta-+",        "/iset **incr");
 368        weechat::buffer_set($iset_buffer, "key_bind_meta--",        "/iset **decr");
 369        weechat::buffer_set($iset_buffer, "key_bind_meta-imeta-r",  "/iset **reset");
 370        weechat::buffer_set($iset_buffer, "key_bind_meta-imeta-u",  "/iset **unset");
 371        weechat::buffer_set($iset_buffer, "key_bind_meta-ctrl-J",   "/iset **set");
 372        weechat::buffer_set($iset_buffer, "key_bind_meta-ctrl-M",   "/iset **set");
 373        weechat::buffer_set($iset_buffer, "key_bind_meta-meta2-1~", "/iset **scroll_top");
 374        weechat::buffer_set($iset_buffer, "key_bind_meta-meta2-4~", "/iset **scroll_bottom");
 375        weechat::buffer_set($iset_buffer, "key_bind_meta-v",        "/iset **toggle_help");
 376        weechat::buffer_set($iset_buffer, "key_bind_meta-p",        "/iset **toggle_show_plugin_desc");
 377        weechat::buffer_set($iset_buffer, "localvar_set_iset_filter", $filter);
 378        weechat::buffer_set($iset_buffer, "localvar_set_iset_search_mode", $search_mode);
 379        weechat::buffer_set($iset_buffer, "localvar_set_iset_search_value", $search_value);
 380    }
 381}
 382
 383sub iset_get_options
 384{
 385    my $var_value = $_[0];
 386    $var_value = "" if (not defined $var_value);
 387    $var_value = lc($var_value);
 388    $search_value = $var_value;
 389    @iset_focus = ();
 390    @options_names = ();
 391    @options_parent_names = ();
 392    @options_types = ();
 393    @options_values = ();
 394    @options_default_values = ();
 395    @options_parent_values = ();
 396    @options_is_null = ();
 397    $option_max_length = 0;
 398    my %options_internal = ();
 399    my $i = 0;
 400    my $key;
 401    my $iset_struct;
 402    my %iset_struct;
 403
 404    weechat::buffer_set($iset_buffer, "localvar_set_iset_search_value", $var_value) if ($search_mode == 3);
 405
 406    my $infolist = weechat::infolist_get("option", "", $filter);
 407    while (weechat::infolist_next($infolist))
 408    {
 409        $key = sprintf("%08d", $i);
 410        my $name = weechat::infolist_string($infolist, "full_name");
 411        my $parent_name = weechat::infolist_string($infolist, "parent_name");
 412        next if (weechat::config_boolean($options_iset{"show_plugin_description"}) == 0 and index ($name, "plugins.desc.") != -1);
 413        my $type = weechat::infolist_string($infolist, "type");
 414        my $value = weechat::infolist_string($infolist, "value");
 415        my $default_value = weechat::infolist_string($infolist, "default_value");
 416        my $parent_value;
 417        if ($parent_name && (($wee_version_number < 0x00040300) || (weechat::infolist_search_var($infolist, "parent_value"))))
 418        {
 419            $parent_value = weechat::infolist_string($infolist, "parent_value");
 420        }
 421        my $is_null = weechat::infolist_integer($infolist, "value_is_null");
 422
 423        if ($search_mode == 3)
 424        {
 425            my $value = weechat::infolist_string($infolist, "value");
 426            if ( grep /\Q$var_value/,lc($value) )
 427            {
 428                $options_internal{$name}{"parent_name"} = $parent_name;
 429                $options_internal{$name}{"type"} = $type;
 430                $options_internal{$name}{"value"} = $value;
 431                $options_internal{$name}{"default_value"} = $default_value;
 432                $options_internal{$name}{"parent_value"} = $parent_value;
 433                $options_internal{$name}{"is_null"} = $is_null;
 434                $option_max_length = length($name) if (length($name) > $option_max_length);
 435                $iset_struct{$key} = $options_internal{$name};
 436                push(@iset_focus, $iset_struct{$key});
 437            }
 438        }
 439        # search for diff?
 440        elsif ( $search_mode == 4 or $search_mode == 5)
 441        {
 442            if ($value ne $default_value )
 443            {
 444                $options_internal{$name}{"parent_name"} = $parent_name;
 445                $options_internal{$name}{"type"} = $type;
 446                $options_internal{$name}{"value"} = $value;
 447                $options_internal{$name}{"default_value"} = $default_value;
 448                $options_internal{$name}{"parent_value"} = $parent_value;
 449                $options_internal{$name}{"is_null"} = $is_null;
 450                $option_max_length = length($name) if (length($name) > $option_max_length);
 451                $iset_struct{$key} = $options_internal{$name};
 452                push(@iset_focus, $iset_struct{$key});
 453            }
 454        }
 455        else
 456        {
 457            $options_internal{$name}{"parent_name"} = $parent_name;
 458            $options_internal{$name}{"type"} = $type;
 459            $options_internal{$name}{"value"} = $value;
 460            $options_internal{$name}{"default_value"} = $default_value;
 461            $options_internal{$name}{"parent_value"} = $parent_value;
 462            $options_internal{$name}{"is_null"} = $is_null;
 463            $option_max_length = length($name) if (length($name) > $option_max_length);
 464            $iset_struct{$key} = $options_internal{$name};
 465            push(@iset_focus, $iset_struct{$key});
 466        }
 467        $i++;
 468    }
 469    weechat::infolist_free($infolist);
 470
 471    foreach my $name (sort keys %options_internal)
 472    {
 473        push(@options_names, $name);
 474        push(@options_parent_names, $options_internal{$name}{"parent_name"});
 475        push(@options_types, $options_internal{$name}{"type"});
 476        push(@options_values, $options_internal{$name}{"value"});
 477        push(@options_default_values, $options_internal{$name}{"default_value"});
 478        push(@options_parent_values, $options_internal{$name}{"parent_value"});
 479        push(@options_is_null, $options_internal{$name}{"is_null"});
 480    }
 481}
 482
 483sub iset_get_values
 484{
 485    my $var_value = $_[0];
 486    $var_value = lc($var_value);
 487    if (substr($var_value,0,1) eq weechat::config_string($options_iset{"value_search_char"}) and $var_value ne weechat::config_string($options_iset{"value_search_char"}))
 488    {
 489        $var_value = substr($var_value,1,length($var_value));
 490        $search_mode = 0;
 491    }
 492    iset_search_values($var_value,$search_mode);
 493    weechat::buffer_set($iset_buffer, "localvar_set_iset_search_mode", $search_mode);
 494    weechat::buffer_set($iset_buffer, "localvar_set_iset_search_value", $var_value);
 495    $search_value = $var_value;
 496}
 497sub iset_search_values
 498{
 499    my ($var_value,$search_mode) = ($_[0],$_[1]);
 500    @options_names = ();
 501    @options_parent_names = ();
 502    @options_types = ();
 503    @options_values = ();
 504    @options_default_values = ();
 505    @options_parent_values = ();
 506    @options_is_null = ();
 507    $option_max_length = 0;
 508    my %options_internal = ();
 509    my $i = 0;
 510    my $infolist = weechat::infolist_get("option", "", "*");
 511    while (weechat::infolist_next($infolist))
 512    {
 513        my $name = weechat::infolist_string($infolist, "full_name");
 514        my $parent_name = weechat::infolist_string($infolist, "parent_name");
 515        next if (weechat::config_boolean($options_iset{"show_plugin_description"}) == 0 and index ($name, "plugins.desc.") != -1);
 516        my $type = weechat::infolist_string($infolist, "type");
 517        my $is_null = weechat::infolist_integer($infolist, "value_is_null");
 518        my $value = weechat::infolist_string($infolist, "value");
 519        my $default_value = weechat::infolist_string($infolist, "default_value");
 520        my $parent_value;
 521        if ($parent_name && (($wee_version_number < 0x00040300) || (weechat::infolist_search_var($infolist, "parent_value"))))
 522        {
 523            $parent_value = weechat::infolist_string($infolist, "parent_value");
 524        }
 525        if ($search_mode)
 526        {
 527            if ( grep /\Q$var_value/,lc($value) )
 528            {
 529                $options_internal{$name}{"parent_name"} = $parent_name;
 530                $options_internal{$name}{"type"} = $type;
 531                $options_internal{$name}{"value"} = $value;
 532                $options_internal{$name}{"default_value"} = $default_value;
 533                $options_internal{$name}{"parent_value"} = $parent_value;
 534                $options_internal{$name}{"is_null"} = $is_null;
 535                $option_max_length = length($name) if (length($name) > $option_max_length);
 536            }
 537        }
 538        else
 539        {
 540#            if ($value =~ /\Q$var_value/si)
 541            if (lc($value) eq $var_value)
 542            {
 543                $options_internal{$name}{"parent_name"} = $parent_name;
 544                $options_internal{$name}{"type"} = $type;
 545                $options_internal{$name}{"value"} = $value;
 546                $options_internal{$name}{"default_value"} = $default_value;
 547                $options_internal{$name}{"parent_value"} = $parent_value;
 548                $options_internal{$name}{"is_null"} = $is_null;
 549                $option_max_length = length($name) if (length($name) > $option_max_length);
 550            }
 551        }
 552        $i++;
 553    }
 554    weechat::infolist_free($infolist);
 555    foreach my $name (sort keys %options_internal)
 556    {
 557        push(@options_names, $name);
 558        push(@options_parent_names, $options_internal{$name}{"parent_name"});
 559        push(@options_types, $options_internal{$name}{"type"});
 560        push(@options_values, $options_internal{$name}{"value"});
 561        push(@options_default_values, $options_internal{$name}{"default_value"});
 562        push(@options_parent_values, $options_internal{$name}{"parent_value"});
 563        push(@options_is_null, $options_internal{$name}{"is_null"});
 564    }
 565}
 566
 567sub iset_refresh_line
 568{
 569    if ($iset_buffer ne "")
 570    {
 571        my $y = $_[0];
 572        if ($y <= $#options_names)
 573        {
 574            return if (! defined($options_types[$y]));
 575            my $format = sprintf("%%s%%s%%s %%s %%-7s %%s %%s%%s%%s");
 576            my $padding;
 577            if ($wee_version_number >= 0x00040200)
 578            {
 579                $padding = " " x ($option_max_length - weechat::strlen_screen($options_names[$y]));
 580            }
 581            else
 582            {
 583                $padding = " " x ($option_max_length - length($options_names[$y]));
 584            }
 585            my $around = "";
 586            $around = "\"" if ((!$options_is_null[$y]) && ($options_types[$y] eq "string"));
 587
 588            my $color1 = weechat::color(weechat::config_color($options_iset{"color_option"}));
 589            my $color2 = weechat::color(weechat::config_color($options_iset{"color_type"}));
 590            my $color3 = "";
 591            my $color4 = "";
 592            if ($options_is_null[$y])
 593            {
 594                $color3 = weechat::color(weechat::config_color($options_iset{"color_value_undef"}));
 595                $color4 = weechat::color(weechat::config_color($options_iset{"color_value"}));
 596            }
 597            elsif ($options_values[$y] ne $options_default_values[$y])
 598            {
 599                $color3 = weechat::color(weechat::config_color($options_iset{"color_value_diff"}));
 600            }
 601            else
 602            {
 603                $color3 = weechat::color(weechat::config_color($options_iset{"color_value"}));
 604            }
 605            if ($y == $current_line)
 606            {
 607                $color1 = weechat::color(weechat::config_color($options_iset{"color_option_selected"}).",".weechat::config_color($options_iset{"color_bg_selected"}));
 608                $color2 = weechat::color(weechat::config_color($options_iset{"color_type_selected"}).",".weechat::config_color($options_iset{"color_bg_selected"}));
 609                if ($options_is_null[$y])
 610                {
 611                    $color3 = weechat::color(weechat::config_color($options_iset{"color_value_undef_selected"}).",".weechat::config_color($options_iset{"color_bg_selected"}));
 612                    $color4 = weechat::color(weechat::config_color($options_iset{"color_value_selected"}).",".weechat::config_color($options_iset{"color_bg_selected"}));
 613                }
 614                elsif ($options_values[$y] ne $options_default_values[$y])
 615                {
 616                    $color3 = weechat::color(weechat::config_color($options_iset{"color_value_diff_selected"}).",".weechat::config_color($options_iset{"color_bg_selected"}));
 617                }
 618                else
 619                {
 620                    $color3 = weechat::color(weechat::config_color($options_iset{"color_value_selected"}).",".weechat::config_color($options_iset{"color_bg_selected"}));
 621                }
 622            }
 623            my $value = $options_values[$y];
 624            if (weechat::config_boolean($options_iset{"use_color"}) == 1 and $options_types[$y] eq "color")
 625            {
 626                $value = weechat::color($options_values[$y]) . $options_values[$y];
 627            }
 628            if ($options_is_null[$y])
 629            {
 630                $value = "null";
 631                if ($options_parent_names[$y])
 632                {
 633                    if (defined $options_parent_values[$y])
 634                    {
 635                        my $around_parent = "";
 636                        $around_parent = "\"" if ($options_types[$y] eq "string");
 637                        $value .= $color1." -> ".$color4.$around_parent.$options_parent_values[$y].$around_parent;
 638                    }
 639                    else
 640                    {
 641                        $value .= $color1." -> ".$color3."null";
 642                    }
 643                }
 644            }
 645            my $strline = sprintf($format,
 646                                  $color1, $options_names[$y], $padding,
 647                                  $color2, $options_types[$y],
 648                                  $color3, $around, $value, $around);
 649            weechat::print_y($iset_buffer, $y, $strline);
 650        }
 651    }
 652}
 653
 654sub iset_refresh
 655{
 656    iset_title();
 657    if (($iset_buffer ne "") && ($#options_names >= 0))
 658    {
 659        foreach my $y (0 .. $#options_names)
 660        {
 661            iset_refresh_line($y);
 662        }
 663    }
 664
 665    weechat::bar_item_update("isetbar_help") if (weechat::config_boolean($options_iset{"show_help_bar"}) == 1);
 666}
 667
 668sub iset_full_refresh
 669{
 670    $iset_buffer = weechat::buffer_search($LANG, $PRGNAME);
 671    if ($iset_buffer ne "")
 672    {
 673        weechat::buffer_clear($iset_buffer) unless defined $_[0]; # iset_full_refresh(1) does a full refresh without clearing buffer
 674        # search for "*" in $filter.
 675        if ($filter =~ m/\*/ and $search_mode == 2)
 676        {
 677            iset_get_options("");
 678        }
 679        else
 680        {
 681            if ($search_mode == 0)
 682            {
 683                $search_value = "=" . $search_value;
 684                iset_get_values($search_value);
 685            }
 686            elsif ($search_mode == 1)
 687            {
 688                iset_get_values($search_value);
 689            }
 690            elsif ($search_mode == 3)
 691            {
 692                iset_create_filter($filter);
 693                iset_get_options($search_value);
 694            }
 695        }
 696        if (weechat::config_boolean($options_iset{"show_plugin_description"}) == 1)
 697        {
 698            iset_set_current_line($current_line);
 699        }else
 700        {
 701            $current_line = $#options_names if ($current_line > $#options_names);
 702        }
 703        iset_refresh();
 704        weechat::command($iset_buffer, "/window refresh");
 705    }
 706}
 707
 708sub iset_set_current_line
 709{
 710    my $new_current_line = $_[0];
 711    if ($new_current_line >= 0)
 712    {
 713        my $old_current_line = $current_line;
 714        $current_line = $new_current_line;
 715        $current_line = $#options_names if ($current_line > $#options_names);
 716        if ($old_current_line != $current_line)
 717        {
 718            iset_refresh_line($old_current_line);
 719            iset_refresh_line($current_line);
 720            weechat::bar_item_update("isetbar_help") if (weechat::config_boolean($options_iset{"show_help_bar"}) == 1);
 721        }
 722    }
 723}
 724
 725sub iset_signal_window_scrolled_cb
 726{
 727    my ($data, $signal, $signal_data) = ($_[0], $_[1], $_[2]);
 728    if ($iset_buffer ne "")
 729    {
 730        my $infolist = weechat::infolist_get("window", $signal_data, "");
 731        if (weechat::infolist_next($infolist))
 732        {
 733            if (weechat::infolist_pointer($infolist, "buffer") eq $iset_buffer)
 734            {
 735                my $old_current_line = $current_line;
 736                my $new_current_line = $current_line;
 737                my $start_line_y = weechat::infolist_integer($infolist, "start_line_y");
 738                my $chat_height = weechat::infolist_integer($infolist, "chat_height");
 739                $new_current_line += $chat_height if ($new_current_line < $start_line_y);
 740                $new_current_line -= $chat_height if ($new_current_line >= $start_line_y + $chat_height);
 741                $new_current_line = $start_line_y if ($new_current_line < $start_line_y);
 742                $new_current_line = $start_line_y + $chat_height - 1 if ($new_current_line >= $start_line_y + $chat_height);
 743                iset_set_current_line($new_current_line);
 744            }
 745        }
 746        weechat::infolist_free($infolist);
 747    }
 748
 749    return weechat::WEECHAT_RC_OK;
 750}
 751
 752sub iset_get_window_number
 753{
 754    if ($iset_buffer ne "")
 755    {
 756        my $window = weechat::window_search_with_buffer($iset_buffer);
 757        return "-window ".weechat::window_get_integer ($window, "number")." " if ($window ne "");
 758    }
 759    return "";
 760}
 761
 762sub iset_check_line_outside_window
 763{
 764    if ($iset_buffer ne "")
 765    {
 766        undef my $infolist;
 767        if ($wee_version_number >= 0x00030500)
 768        {
 769            my $window = weechat::window_search_with_buffer($iset_buffer);
 770            $infolist = weechat::infolist_get("window", $window, "") if $window;
 771        }
 772        else
 773        {
 774            $infolist = weechat::infolist_get("window", "", "current");
 775        }
 776        if ($infolist)
 777        {
 778            if (weechat::infolist_next($infolist))
 779            {
 780                my $start_line_y = weechat::infolist_integer($infolist, "start_line_y");
 781                my $chat_height = weechat::infolist_integer($infolist, "chat_height");
 782                my $window_number = "";
 783                if ($wee_version_number >= 0x00030500)
 784                {
 785                    $window_number = "-window ".weechat::infolist_integer($infolist, "number")." ";
 786                }
 787                if ($start_line_y > $current_line)
 788                {
 789                    weechat::command($iset_buffer, "/window scroll ".$window_number."-".($start_line_y - $current_line));
 790                }
 791                else
 792                {
 793                    if ($start_line_y <= $current_line - $chat_height)
 794                    {
 795                        weechat::command($iset_buffer, "/window scroll ".$window_number."+".($current_line - $start_line_y - $chat_height + 1));
 796
 797                    }
 798                }
 799            }
 800            weechat::infolist_free($infolist);
 801        }
 802    }
 803}
 804
 805sub iset_get_option_name_index
 806{
 807    my $option_name = $_[0];
 808    my $index = 0;
 809    while ($index <= $#options_names)
 810    {
 811        return -1 if ($options_names[$index] gt $option_name);
 812        return $index if ($options_names[$index] eq $option_name);
 813        $index++;
 814    }
 815    return -1;
 816}
 817
 818sub iset_refresh_option
 819{
 820    my $option_name = $_[0];
 821    my $index = $_[1];
 822    my $infolist = weechat::infolist_get("option", "", $option_name);
 823    if ($infolist)
 824    {
 825        weechat::infolist_next($infolist);
 826        if (weechat::infolist_fields($infolist))
 827        {
 828            $options_parent_names[$index] = weechat::infolist_string($infolist, "parent_name");
 829            $options_types[$index] = weechat::infolist_string($infolist, "type");
 830            $options_values[$index] = weechat::infolist_string($infolist, "value");
 831            $options_default_values[$index] = weechat::infolist_string($infolist, "default_value");
 832            $options_is_null[$index] = weechat::infolist_integer($infolist, "value_is_null");
 833            $options_parent_values[$index] = undef;
 834            if ($options_parent_names[$index]
 835                && (($wee_version_number < 0x00040300) || (weechat::infolist_search_var($infolist, "parent_value"))))
 836            {
 837                $options_parent_values[$index] = weechat::infolist_string($infolist, "parent_value");
 838            }
 839            iset_refresh_line($index);
 840            iset_title() if ($option_name eq "iset.look.show_current_line");
 841        }
 842        else
 843        {
 844            iset_full_refresh(1);  # if not found, refresh fully without clearing buffer
 845            weechat::print_y($iset_buffer, $#options_names + 1, "");
 846        }
 847        weechat::infolist_free($infolist);
 848    }
 849}
 850
 851sub iset_config_cb
 852{
 853    my ($data, $option_name, $value) = ($_[0], $_[1], $_[2]);
 854
 855    if ($iset_buffer ne "")
 856    {
 857        return weechat::WEECHAT_RC_OK if (weechat::info_get("weechat_upgrading", "") eq "1");
 858
 859        my $index = iset_get_option_name_index($option_name);
 860        if ($index >= 0)
 861        {
 862            # refresh info about changed option
 863            iset_refresh_option($option_name, $index);
 864            # refresh any other option having this changed option as parent
 865            foreach my $i (0 .. $#options_names)
 866            {
 867                if ($options_parent_names[$i] eq $option_name)
 868                {
 869                    iset_refresh_option($options_names[$i], $i);
 870                }
 871            }
 872        }
 873        else
 874        {
 875            iset_full_refresh() if ($option_name ne "weechat.bar.isetbar.hidden");
 876        }
 877    }
 878
 879    return weechat::WEECHAT_RC_OK;
 880}
 881
 882sub iset_set_option
 883{
 884    my ($option, $value) = ($_[0],$_[1]);
 885    if (defined $option and defined $value)
 886    {
 887        $option = weechat::config_get($option);
 888        weechat::config_option_set($option, $value, 1) if ($option ne "");
 889    }
 890}
 891
 892sub iset_reset_option
 893{
 894    my $option = $_[0];
 895    if (defined $option)
 896    {
 897        $option = weechat::config_get($option);
 898        weechat::config_option_reset($option, 1) if ($option ne "");
 899    }
 900}
 901
 902sub iset_unset_option
 903{
 904    my $option = $_[0];
 905    if (defined $option)
 906    {
 907        $option = weechat::config_get($option);
 908        weechat::config_option_unset($option) if ($option ne "");
 909    }
 910}
 911
 912
 913sub iset_cmd_cb
 914{
 915    my ($data, $buffer, $args) = ($_[0], $_[1], $_[2]);
 916    my $filter_set = 0;
 917#    $search_value = "";
 918    if (($args ne "") && (substr($args, 0, 2) ne "**"))
 919    {
 920        my @cmd_array = split(/ /,$args);
 921        my $array_count = @cmd_array;
 922        if (substr($args, 0, 1) eq weechat::config_string($options_iset{"value_search_char"})
 923        or (defined $cmd_array[0] and $cmd_array[0] eq weechat::config_string($options_iset{"value_search_char"}).weechat::config_string($options_iset{"value_search_char"})) )
 924        {
 925            $search_mode = 1;
 926            my $search_value = substr($args, 1);  # cut value_search_char
 927            if ($iset_buffer ne "")
 928            {
 929                weechat::buffer_clear($iset_buffer);
 930                weechat::command($iset_buffer, "/window refresh");
 931            }
 932            weechat::buffer_set($iset_buffer, "localvar_set_iset_search_mode", $search_mode);
 933            weechat::buffer_set($iset_buffer, "localvar_set_iset_search_value", $search_value);
 934            iset_init();
 935            iset_get_values($search_value);
 936            iset_refresh();
 937            weechat::buffer_set($iset_buffer, "display", "1");
 938#            $filter = $var_value;
 939            return weechat::WEECHAT_RC_OK;
 940        }
 941        else
 942        {
 943            # f/s option =value
 944            # option =value
 945            $search_mode = 2; # grep on option
 946            if ( $array_count >= 2 and $cmd_array[0] ne "f" or $cmd_array[0] ne "s")
 947            {
 948                if ( defined $cmd_array[1] and substr($cmd_array[1], 0, 1) eq weechat::config_string($options_iset{"value_search_char"})
 949                or defined $cmd_array[2] and substr($cmd_array[2], 0, 1) eq weechat::config_string($options_iset{"value_search_char"}) )
 950                {
 951                    $search_mode = 3; # grep on option and value
 952                    $search_value = substr($cmd_array[1], 1);  # cut value_search_char
 953                    $search_value = substr($cmd_array[2], 1) if ( $array_count > 2);  # cut value_search_char
 954                }
 955            }
 956
 957            # show all diff values
 958            if ( $args eq "d")
 959            {
 960                $search_mode = 4;
 961                $search_value = "*";
 962                $args = $search_value;
 963            }
 964            if ( $array_count >= 2 and $cmd_array[0] eq "d")
 965            {
 966                $search_mode = 5;
 967                $search_value = substr($cmd_array[1], 0);  # cut value_search_char
 968                $search_value = substr($cmd_array[2], 0) if ( $array_count > 2);  # cut value_search_char
 969                $args = $search_value;
 970            }
 971
 972            iset_create_filter($args);
 973            $filter_set = 1;
 974            my $ptrbuf = weechat::buffer_search($LANG, $PRGNAME);
 975
 976            if ($ptrbuf eq "")
 977            {
 978                iset_init();
 979                iset_get_options($search_value);
 980                iset_full_refresh();
 981                weechat::buffer_set(weechat::buffer_search($LANG, $PRGNAME), "display", "1");
 982                weechat::buffer_set($iset_buffer, "localvar_set_iset_search_value", $search_value);
 983                weechat::buffer_set($iset_buffer, "localvar_set_iset_search_mode", $search_mode);
 984                return weechat::WEECHAT_RC_OK;
 985            }
 986            else
 987            {
 988                iset_get_options($search_value);
 989                iset_full_refresh();
 990                weechat::buffer_set($ptrbuf, "display", "1");
 991            }
 992        }
 993    weechat::buffer_set($iset_buffer, "localvar_set_iset_search_mode", $search_mode);
 994    weechat::buffer_set($iset_buffer, "localvar_set_iset_search_value", $search_value);
 995    }
 996    if ($iset_buffer eq "")
 997    {
 998        iset_init();
 999        iset_get_options("");
1000        iset_refresh();
1001    }
1002    else
1003    {
1004#        iset_get_options($search_value);
1005        iset_full_refresh() if ($filter_set);
1006    }
1007
1008    if ($args eq "")
1009    {
1010        weechat::buffer_set($iset_buffer, "display", "1");
1011    }
1012    else
1013    {
1014        if ($args eq "**refresh")
1015        {
1016            iset_full_refresh();
1017        }
1018        if ($args eq "**up")
1019        {
1020            if ($current_line > 0)
1021            {
1022                $current_line--;
1023                iset_refresh_line($current_line + 1);
1024                iset_refresh_line($current_line);
1025                iset_check_line_outside_window();
1026            }
1027        }
1028        if ($args eq "**down")
1029        {
1030            if ($current_line < $#options_names)
1031            {
1032                $current_line++;
1033                iset_refresh_line($current_line - 1);
1034                iset_refresh_line($current_line);
1035                iset_check_line_outside_window();
1036            }
1037        }
1038        if ($args eq "**left" && $wee_version_number >= 0x00030600)
1039        {
1040            weechat::command($iset_buffer, "/window scroll_horiz ".iset_get_window_number()."-".weechat::config_integer($options_iset{"scroll_horiz"})."%");
1041        }
1042        if ($args eq "**right" && $wee_version_number >= 0x00030600)
1043        {
1044            weechat::command($iset_buffer, "/window scroll_horiz ".iset_get_window_number().weechat::config_integer($options_iset{"scroll_horiz"})."%");
1045        }
1046        if ($args eq "**scroll_top")
1047        {
1048            my $old_current_line = $current_line;
1049            $current_line = 0;
1050            iset_refresh_line ($old_current_line);
1051            iset_refresh_line ($current_line);
1052            iset_title();
1053            weechat::command($iset_buffer, "/window scroll_top ".iset_get_window_number());
1054        }
1055        if ($args eq "**scroll_bottom")
1056        {
1057            my $old_current_line = $current_line;
1058            $current_line = $#options_names;
1059            iset_refresh_line ($old_current_line);
1060            iset_refresh_line ($current_line);
1061            iset_title();
1062            weechat::command($iset_buffer, "/window scroll_bottom ".iset_get_window_number());
1063        }
1064        if ($args eq "**toggle")
1065        {
1066            if ($options_types[$current_line] eq "boolean")
1067            {
1068                iset_set_option($options_names[$current_line], "toggle");
1069            }
1070        }
1071        if ($args eq "**incr")
1072        {
1073            if (($options_types[$current_line] eq "integer")
1074                || ($options_types[$current_line] eq "color"))
1075            {
1076                iset_set_option($options_names[$current_line], "++1");
1077            }
1078        }
1079        if ($args eq "**decr")
1080        {
1081            if (($options_types[$current_line] eq "integer")
1082                || ($options_types[$current_line] eq "color"))
1083            {
1084                iset_set_option($options_names[$current_line], "--1");
1085            }
1086        }
1087        if ($args eq "**reset")
1088        {
1089            iset_reset_option($options_names[$current_line]);
1090        }
1091        if ($args eq "**unset")
1092        {
1093            iset_unset_option($options_names[$current_line]);
1094        }
1095        if ($args eq "**toggle_help")
1096        {
1097            if (weechat::config_boolean($options_iset{"show_help_bar"})  == 1)
1098            {
1099                weechat::config_option_set($options_iset{"show_help_bar"},0,1);
1100                iset_show_bar(0);
1101            }
1102            else
1103            {
1104                weechat::config_option_set($options_iset{"show_help_bar"},1,1);
1105                iset_show_bar(1);
1106            }
1107        }
1108        if ($args eq "**toggle_show_plugin_desc")
1109        {
1110            if (weechat::config_boolean($options_iset{"show_plugin_description"}) == 1)
1111            {
1112                weechat::config_option_set($options_iset{"show_plugin_description"},0,1);
1113                iset_full_refresh();
1114                iset_check_line_outside_window();
1115                iset_title();
1116            }
1117            else
1118            {
1119                weechat::config_option_set($options_iset{"show_plugin_description"},1,1);
1120                iset_full_refresh();
1121                iset_check_line_outside_window();
1122                iset_title();
1123            }
1124        }
1125        if ($args eq "**set")
1126        {
1127            my $quote = "";
1128            my $value = $options_values[$current_line];
1129            if ($options_is_null[$current_line])
1130            {
1131                $value = "";
1132            }
1133            else
1134            {
1135                $quote = "\"" if ($options_types[$current_line] eq "string");
1136            }
1137            $value = " ".$quote.$value.$quote if ($value ne "" or $quote ne "");
1138
1139            my $set_command = "/set";
1140            my $start_index = 5;
1141            if (weechat::config_boolean($options_iset{"use_mute"}) == 1)
1142            {
1143                $set_command = "/mute ".$set_command;
1144                $start_index += 11;
1145            }
1146            $set_command = $set_command." ".$options_names[$current_line].$value;
1147            my $pos_space = index($set_command, " ", $start_index);
1148            if ($pos_space < 0)
1149            {
1150                $pos_space = 9999;
1151            }
1152            else
1153            {
1154                $pos_space = $pos_space + 1;
1155                $pos_space = $pos_space + 1 if ($quote ne "");
1156            }
1157            weechat::buffer_set($iset_buffer, "input", $set_command);
1158            weechat::buffer_set($iset_buffer, "input_pos", "".$pos_space);
1159        }
1160    }
1161    weechat::bar_item_update("isetbar_help") if (weechat::config_boolean($options_iset{"show_help_bar"}) == 1);
1162    return weechat::WEECHAT_RC_OK;
1163}
1164
1165sub iset_get_help
1166{
1167    my ($redraw) = ($_[0]);
1168
1169    return '' if (weechat::config_boolean($options_iset{"show_help_bar"}) == 0);
1170
1171    if (not defined $options_names[$current_line])
1172    {
1173        return "No option selected. Set a new filter using command line (use '*' to see all options)";
1174    }
1175    if ($options_name_copy eq $options_names[$current_line] and not defined $redraw)
1176    {
1177        return $description;
1178    }
1179    $options_name_copy = $options_names[$current_line];
1180    my $optionlist ="";
1181    $optionlist = weechat::infolist_get("option", "", $options_names[$current_line]);
1182    weechat::infolist_next($optionlist);
1183    my $full_name = weechat::infolist_string($optionlist,"full_name");
1184    my $option_desc = "";
1185    my $option_default_value = "";
1186    my $option_range = "";
1187    my $possible_values = "";
1188    my $re = qq(\Q$full_name);
1189    if (grep (/^$re$/,$options_names[$current_line]))
1190    {
1191        $option_desc = weechat::infolist_string($optionlist, "description_nls");
1192        $option_desc = weechat::infolist_string($optionlist, "description") if ($option_desc eq "");
1193        $option_desc = "No help found" if ($option_desc eq "");
1194        $option_default_value = weechat::infolist_string($optionlist, "default_value");
1195        $possible_values = weechat::infolist_string($optionlist, "string_values") if (weechat::infolist_string($optionlist, "string_values") ne "");
1196        if ((weechat::infolist_string($optionlist, "type") eq "integer") && ($possible_values eq ""))
1197        {
1198            $option_range = weechat::infolist_integer($optionlist, "min")
1199                ." .. ".weechat::infolist_integer($optionlist, "max");
1200        }
1201    }
1202    weechat::infolist_free($optionlist);
1203    iset_title();
1204
1205    $description = weechat::color(weechat::config_color($options_iset{"color_help_option_name"})).$options_names[$current_line]
1206        .weechat::color("bar_fg").": "
1207        .weechat::color(weechat::config_color($options_iset{"color_help_text"})).$option_desc;
1208
1209    # show additional infos like default value and possible values
1210
1211    if (weechat::config_boolean($options_iset{"show_help_extra_info"}) == 1)
1212    {
1213        $description .=
1214            weechat::color("bar_delim")." ["
1215            .weechat::color("bar_fg")."default: "
1216            .weechat::color("bar_delim")."\""
1217            .weechat::color(weechat::config_color($options_iset{"color_help_default_value"})).$option_default_value
1218            .weechat::color("bar_delim")."\"";
1219        if ($option_range ne "")
1220        {
1221            $description .= weechat::color("bar_fg").", values: ".$option_range;
1222        }
1223        if ($possible_values ne "")
1224        {
1225            $possible_values =~ s/\|/", "/g;      # replace '|' to '", "'
1226            $description .= weechat::color("bar_fg").", values: ". "\"" . $possible_values . "\"";
1227
1228        }
1229        $description .= weechat::color("bar_delim")."]";
1230    }
1231    return $description;
1232}
1233
1234sub iset_check_condition_isetbar_cb
1235{
1236    my ($data, $modifier, $modifier_data, $string) = ($_[0], $_[1], $_[2], $_[3]);
1237    my $buffer = weechat::window_get_pointer($modifier_data, "buffer");
1238    if ($buffer ne "")
1239    {
1240        if ((weechat::buffer_get_string($buffer, "plugin") eq $LANG)
1241            && (weechat::buffer_get_string($buffer, "name") eq $PRGNAME))
1242        {
1243            return "1";
1244        }
1245    }
1246    return "0";
1247}
1248
1249sub iset_show_bar
1250{
1251    my $show = $_[0];
1252    my $barhidden = weechat::config_get("weechat.bar.isetbar.hidden");
1253    if ($barhidden)
1254    {
1255        if ($show)
1256        {
1257            if (weechat::config_boolean($options_iset{"show_help_bar"}) == 1)
1258            {
1259                if (weechat::config_boolean($barhidden))
1260                {
1261                    weechat::config_option_set($barhidden, 0, 1);
1262                }
1263            }
1264        }
1265        else
1266        {
1267            if (!weechat::config_boolean($barhidden))
1268            {
1269                weechat::config_option_set($barhidden, 1, 1);
1270            }
1271        }
1272    }
1273}
1274
1275sub iset_signal_buffer_switch_cb
1276{
1277    my $buffer_pointer = $_[2];
1278    my $show_bar = 0;
1279    $show_bar = 1 if (weechat::buffer_get_integer($iset_buffer, "num_displayed") > 0);
1280    iset_show_bar($show_bar);
1281    iset_check_line_outside_window() if ($buffer_pointer eq $iset_buffer);
1282    return weechat::WEECHAT_RC_OK;
1283}
1284
1285sub iset_item_cb
1286{
1287    return iset_get_help();
1288}
1289
1290sub iset_upgrade_ended
1291{
1292    iset_full_refresh();
1293}
1294
1295sub iset_end
1296{
1297    # when script is unloaded, we hide bar
1298    iset_show_bar(0);
1299}
1300
1301# -------------------------------[ mouse support ]-------------------------------------
1302
1303sub hook_focus_iset_cb
1304{
1305    my %info = %{$_[1]};
1306    my $bar_item_line = int($info{"_bar_item_line"});
1307    undef my $hash;
1308    if (($info{"_buffer_name"} eq $PRGNAME) && $info{"_buffer_plugin"} eq $LANG && ($bar_item_line >= 0) && ($bar_item_line <= $#iset_focus))
1309    {
1310        $hash = $iset_focus[$bar_item_line];
1311    }
1312    else
1313    {
1314        $hash = {};
1315        my $hash_focus = $iset_focus[0];
1316        foreach my $key (keys %$hash_focus)
1317        {
1318            $hash->{$key} = "?";
1319        }
1320    }
1321    return $hash;
1322}
1323
1324# _chat_line_y contains selected line
1325sub iset_hsignal_mouse_cb
1326{
1327    my ($data, $signal, %hash) = ($_[0], $_[1], %{$_[2]});
1328
1329    return weechat::WEECHAT_RC_OK unless (@options_types);
1330
1331    if ($hash{"_buffer_name"} eq $PRGNAME && ($hash{"_buffer_plugin"} eq $LANG))
1332    {
1333        if ($hash{"_key"} eq "button1")
1334        {
1335            iset_set_current_line($hash{"_chat_line_y"});
1336        }
1337        elsif ($hash{"_key"} eq "button2")
1338        {
1339            if ($options_types[$hash{"_chat_line_y"}] eq "boolean")
1340            {
1341                iset_set_option($options_names[$hash{"_chat_line_y"}], "toggle");
1342                iset_set_current_line($hash{"_chat_line_y"});
1343            }
1344            elsif ($options_types[$hash{"_chat_line_y"}] eq "string")
1345            {
1346                iset_set_current_line($hash{"_chat_line_y"});
1347                weechat::command("", "/$PRGNAME **set");
1348            }
1349        }
1350        elsif ($hash{"_key"} eq "button2-gesture-left" or $hash{"_key"} eq "button2-gesture-left-long")
1351        {
1352            if ($options_types[$hash{"_chat_line_y"}] eq "integer" or ($options_types[$hash{"_chat_line_y"}] eq "color"))
1353            {
1354                iset_set_current_line($hash{"_chat_line_y"});
1355                my $distance = distance($hash{"_chat_line_x"},$hash{"_chat_line_x2"});
1356                weechat::command("", "/repeat $distance /$PRGNAME **decr");
1357            }
1358        }
1359        elsif ($hash{"_key"} eq "button2-gesture-right" or $hash{"_key"} eq "button2-gesture-right-long")
1360        {
1361            if ($options_types[$hash{"_chat_line_y"}] eq "integer" or ($options_types[$hash{"_chat_line_y"}] eq "color"))
1362            {
1363                iset_set_current_line($hash{"_chat_line_y"});
1364                my $distance = distance($hash{"_chat_line_x"},$hash{"_chat_line_x2"});
1365                weechat::command("", "/repeat $distance /$PRGNAME **incr");
1366            }
1367        }
1368    }
1369    window_switch();
1370}
1371
1372sub window_switch
1373{
1374    my $current_window = weechat::current_window();
1375    my $dest_window = weechat::window_search_with_buffer(weechat::buffer_search("perl","iset"));
1376    return 0 if ($dest_window eq "" or $current_window eq $dest_window);
1377
1378    my $infolist = weechat::infolist_get("window", $dest_window, "");
1379    weechat::infolist_next($infolist);
1380    my $number = weechat::infolist_integer($infolist, "number");
1381    weechat::infolist_free($infolist);
1382    weechat::command("","/window " . $number);
1383}
1384
1385sub distance
1386{
1387    my ($x1,$x2) = ($_[0], $_[1]);
1388    my $distance;
1389    $distance = $x1 - $x2;
1390    $distance = abs($distance);
1391    if ($distance > 0)
1392    {
1393        use integer;
1394        $distance  =  $distance / 3;
1395        $distance = 1 if ($distance == 0);
1396    }
1397    elsif ($distance == 0)
1398    {
1399        $distance = 1;
1400    }
1401    return $distance;
1402}
1403
1404# -----------------------------------[ config ]---------------------------------------
1405
1406sub iset_config_init
1407{
1408    $iset_config_file = weechat::config_new($ISET_CONFIG_FILE_NAME,"iset_config_reload_cb","");
1409    return if ($iset_config_file eq "");
1410
1411    # section "color"
1412    my $section_color = weechat::config_new_section($iset_config_file,"color", 0, 0, "", "", "", "", "", "", "", "", "", "");
1413    if ($section_color eq "")
1414    {
1415        weechat::config_free($iset_config_file);
1416        return;
1417    }
1418    $options_iset{"color_option"} = weechat::config_new_option(
1419        $iset_config_file, $section_color,
1420        "option", "color", "Color for option name in iset buffer", "", 0, 0,
1421        "default", "default", 0, "", "", "full_refresh_cb", "", "", "");
1422    $options_iset{"color_option_selected"} = weechat::config_new_option(
1423        $iset_config_file, $section_color,
1424        "option_selected", "color", "Color for selected option name in iset buffer", "", 0, 0,
1425        "white", "white", 0, "", "", "full_refresh_cb", "", "", "");
1426    $options_iset{"color_type"} = weechat::config_new_option(
1427        $iset_config_file, $section_color,
1428        "type", "color", "Color for option type (integer, boolean, string)", "", 0, 0,
1429        "brown", "brown", 0, "", "", "full_refresh_cb", "", "", "");
1430    $options_iset{"color_type_selected"} = weechat::config_new_option(
1431        $iset_config_file, $section_color,
1432        "type_selected", "color", "Color for selected option type (integer, boolean, string)", "", 0, 0,
1433        "yellow", "yellow", 0, "", "", "full_refresh_cb", "", "", "");
1434    $options_iset{"color_value"} = weechat::config_new_option(
1435        $iset_config_file, $section_color,
1436        "value", "color", "Color for option value", "", 0, 0,
1437        "cyan", "cyan", 0, "", "", "full_refresh_cb", "", "", "");
1438    $options_iset{"color_value_selected"} = weechat::config_new_option(
1439        $iset_config_file, $section_color,
1440        "value_selected", "color", "Color for selected option value", "", 0, 0,
1441        "lightcyan", "lightcyan", 0, "", "", "full_refresh_cb", "", "", "");
1442    $options_iset{"color_value_diff"} = weechat::config_new_option(
1443        $iset_config_file, $section_color,
1444        "value_diff", "color", "Color for option value different from default", "", 0, 0,
1445        "magenta", "magenta", 0, "", "", "full_refresh_cb", "", "", "");
1446    $options_iset{"color_value_diff_selected"} = weechat::config_new_option(
1447        $iset_config_file, $section_color,
1448        "value_diff_selected", "color", "Color for selected option value different from default", "", 0, 0,
1449        "lightmagenta", "lightmagenta", 0, "", "", "full_refresh_cb", "", "", "");
1450    $options_iset{"color_value_undef"} = weechat::config_new_option(
1451        $iset_config_file, $section_color,
1452        "value_undef", "color", "Color for option value undef", "", 0, 0,
1453        "green", "green", 0, "", "", "full_refresh_cb", "", "", "");
1454    $options_iset{"color_value_undef_selected"} = weechat::config_new_option(
1455        $iset_config_file, $section_color,
1456        "value_undef_selected", "color", "Color for selected option value undef", "", 0, 0,
1457        "lightgreen", "lightgreen", 0, "", "", "full_refresh_cb", "", "", "");
1458    $options_iset{"color_bg_selected"} = weechat::config_new_option(
1459        $iset_config_file, $section_color,
1460        "bg_selected", "color", "Background color for current selected option", "", 0, 0,
1461        "red", "red", 0, "", "", "full_refresh_cb", "", "", "");
1462    $options_iset{"color_help_option_name"} = weechat::config_new_option(
1463        $iset_config_file, $section_color,
1464        "help_option_name", "color", "Color for option name in help-bar", "", 0, 0,
1465        "white", "white", 0, "", "", "bar_refresh", "", "", "");
1466    $options_iset{"color_help_text"} = weechat::config_new_option(
1467        $iset_config_file, $section_color,
1468        "help_text", "color", "Color for option description in help-bar", "", 0, 0,
1469        "default", "default", 0, "", "", "bar_refresh", "", "", "");
1470    $options_iset{"color_help_default_value"} = weechat::config_new_option(
1471        $iset_config_file, $section_color,
1472        "help_default_value", "color", "Color for default option value in help-bar", "", 0, 0,
1473        "green", "green", 0, "", "", "bar_refresh", "", "", "");
1474
1475    # section "help"
1476    my $section_help = weechat::config_new_section($iset_config_file,"help", 0, 0, "", "", "", "", "", "", "", "", "", "");
1477    if ($section_help eq "")
1478    {
1479        weechat::config_free($iset_config_file);
1480        return;
1481    }
1482    $options_iset{"show_help_bar"} = weechat::config_new_option(
1483        $iset_config_file, $section_help,
1484        "show_help_bar", "boolean", "Show help bar", "", 0, 0,
1485        "on", "on", 0, "", "", "toggle_help_cb", "", "", "");
1486    $options_iset{"show_help_extra_info"} = weechat::config_new_option(
1487        $iset_config_file, $section_help,
1488        "show_help_extra_info", "boolean", "Show additional information in help bar (default value, max./min. value) ", "", 0, 0,
1489        "on", "on", 0, "", "", "", "", "", "");
1490    $options_iset{"show_plugin_description"} = weechat::config_new_option(
1491        $iset_config_file, $section_help,
1492        "show_plugin_description", "boolean", "Show plugin description in iset buffer", "", 0, 0,
1493        "off", "off", 0, "", "", "full_refresh_cb", "", "", "");
1494
1495    # section "look"
1496    my $section_look = weechat::config_new_section($iset_config_file, "look", 0, 0, "", "", "", "", "", "", "", "", "", "");
1497    if ($section_look eq "")
1498    {
1499        weechat::config_free($iset_config_file);
1500        return;
1501    }
1502    $options_iset{"value_search_char"} = weechat::config_new_option(
1503        $iset_config_file, $section_look,
1504        "value_search_char", "string", "Trigger char to tell iset to search for value instead of option (for example: =red)", "", 0, 0,
1505        "=", "=", 0, "", "", "", "", "", "");
1506    $options_iset{"scroll_horiz"} = weechat::config_new_option(
1507        $iset_config_file, $section_look,
1508        "scroll_horiz", "integer", "scroll content of iset buffer n%", "", 1, 100,
1509        "10", "10", 0, "", "", "", "", "", "");
1510    $options_iset{"show_current_line"} = weechat::config_new_option(
1511        $iset_config_file, $section_look,
1512        "show_current_line", "boolean", "show current line in title bar.", "", 0, 0,
1513        "on", "on", 0, "", "", "", "", "", "");
1514    $options_iset{"use_mute"} = weechat::config_new_option(
1515        $iset_config_file, $section_look,
1516        "use_mute", "boolean", "/mute command will be used in input bar", "", 0, 0,
1517        "off", "off", 0, "", "", "", "", "", "");
1518    $options_iset{"use_color"} = weechat::config_new_option(
1519        $iset_config_file, $section_look,
1520        "use_color", "boolean", "display the color value in the corresponding color", "", 0, 0,
1521        "off", "off", 0, "", "", "full_refresh_cb", "", "", "");
1522}
1523
1524sub iset_config_reload_cb
1525{
1526    my ($data,$config_file) = ($_[0], $_[1]);
1527    return weechat::config_reload($config_file)
1528}
1529
1530sub iset_config_read
1531{
1532    return weechat::config_read($iset_config_file) if ($iset_config_file ne "");
1533}
1534
1535sub iset_config_write
1536{
1537    return weechat::config_write($iset_config_file) if ($iset_config_file ne "");
1538}
1539
1540sub full_refresh_cb
1541{
1542    iset_full_refresh();
1543    return weechat::WEECHAT_RC_OK;
1544}
1545
1546sub bar_refresh
1547{
1548    iset_get_help(1);
1549    weechat::bar_item_update("isetbar_help") if (weechat::config_boolean($options_iset{"show_help_bar"}) == 1);
1550    return weechat::WEECHAT_RC_OK;
1551}
1552
1553sub toggle_help_cb
1554{
1555    my $value = weechat::config_boolean($options_iset{"show_help_bar"});
1556    iset_show_bar($value);
1557    return weechat::WEECHAT_RC_OK;
1558}
1559
1560# -----------------------------------[ main ]-----------------------------------------
1561
1562weechat::register($PRGNAME, $AUTHOR, $VERSION, $LICENSE,
1563                  $DESCR, "iset_end", "");
1564
1565$wee_version_number = weechat::info_get("version_number", "") || 0;
1566
1567iset_config_init();
1568iset_config_read();
1569
1570weechat::hook_command($PRGNAME, "Interactive set", "d <text> || f <file> || s <section> || [=][=]<text>",
1571                      "d <text> : show only changed options\n".
1572                      "f file   : show options for a file\n".
1573                      "s section: show options for a section\n".
1574                      "text     : show options with 'text' in name\n".
1575                      weechat::config_string($options_iset{"value_search_char"})."text    : show options with 'text' in value\n".
1576                      weechat::config_string($options_iset{"value_search_char"}).weechat::config_string($options_iset{"value_search_char"})."text   : show options with exact 'text' in value\n\n".
1577                      "Keys for iset buffer:\n".
1578                      "f11,f12        : move iset content left/right\n".
1579                      "up,down        : move one option up/down\n".
1580                      "pgup,pdwn      : move one page up/down\n".
1581                      "home,end       : move to first/last option\n".
1582                      "ctrl+'L'       : refresh options and screen\n".
1583                      "alt+space      : toggle boolean on/off\n".
1584                      "alt+'+'        : increase value (for integer or color)\n".
1585                      "alt+'-'        : decrease value (for integer or color)\n".
1586                      "alt+'i',alt+'r': reset value of option\n".
1587                      "alt+'i',alt+'u': unset option\n".
1588                      "alt+enter      : set new value for option (edit it with command line)\n".
1589                      "text,enter     : set a new filter using command line (use '*' to see all options)\n".
1590                      "alt+'v'        : toggle help bar on/off\n".
1591                      "alt+'p'        : toggle option \"show_plugin_description\" on/off\n".
1592                      "q              : as input in iset buffer to close it\n".
1593                      "\n".
1594                      "Mouse actions:\n".
1595                      "wheel up/down                 : move cursor up/down\n".
1596                      "left button                   : select an option from list\n".
1597                      "right button                  : toggle boolean (on/off) or set a new value for option (edit it with command line)\n".
1598                      "right button + drag left/right: increase/decrease value (for integer or color)\n".
1599                      "\n".
1600                      "Examples:\n".
1601                      "  show changed options in 'aspell' plugin\n".
1602                      "    /iset d aspell\n".
1603                      "  show options for file 'irc'\n".
1604                      "    /iset f irc\n".
1605                      "  show options for section 'look'\n".
1606                      "    /iset s look\n".
1607                      "  show all options with text 'nicklist' in name\n".
1608                      "    /iset nicklist\n".
1609                      "  show all values which contain 'red'. ('" . weechat::config_string($options_iset{"value_search_char"}) . "' is a trigger char).\n".
1610                      "    /iset ". weechat::config_string($options_iset{"value_search_char"}) ."red\n".
1611                      "  show all values which hit 'off'. ('" . weechat::config_string($options_iset{"value_search_char"}) . weechat::config_string($options_iset{"value_search_char"}) . "' is a trigger char).\n".
1612                      "    /iset ". weechat::config_string($options_iset{"value_search_char"}) . weechat::config_string($options_iset{"value_search_char"}) ."off\n".
1613                      "  show options for file 'weechat' which contains value 'off'\n".
1614                      "    /iset f weechat ".weechat::config_string($options_iset{"value_search_char"})."off\n".
1615                      "",
1616                      "", "iset_cmd_cb", "");
1617weechat::hook_signal("upgrade_ended", "iset_upgrade_ended", "");
1618weechat::hook_signal("window_scrolled", "iset_signal_window_scrolled_cb", "");
1619weechat::hook_signal("buffer_switch", "iset_signal_buffer_switch_cb","");
1620weechat::bar_item_new("isetbar_help", "iset_item_cb", "");
1621weechat::bar_new("isetbar", "on", "0", "window", "", "top", "horizontal",
1622                 "vertical", "3", "3", "default", "cyan", "default", "1",
1623                 "isetbar_help");
1624weechat::hook_modifier("bar_condition_isetbar", "iset_check_condition_isetbar_cb", "");
1625weechat::hook_config("*", "iset_config_cb", "");
1626$iset_buffer = weechat::buffer_search($LANG, $PRGNAME);
1627iset_init() if ($iset_buffer ne "");
1628
1629if ($wee_version_number >= 0x00030600)
1630{
1631    weechat::hook_focus("chat", "hook_focus_iset_cb", "");
1632    weechat::hook_hsignal($PRGNAME."_mouse", "iset_hsignal_mouse_cb", "");
1633    weechat::key_bind("mouse", \%mouse_keys);
1634}