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