]> git.r.bdr.sh - rbdr/dotfiles/blame - weechat/perl/iset.pl
Update the stuff
[rbdr/dotfiles] / weechat / perl / iset.pl
CommitLineData
f853d479
BB
1#
2# Copyright (C) 2008-2012 Sebastien Helleu <flashcode@flashtux.org>
3# Copyright (C) 2010-2012 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# 2012-03-17, Sebastien Helleu <flashcode@flashtux.org>:
23# version 2.5: fix check of sections when creating config file
24# 2012-03-09, Sebastien Helleu <flashcode@flashtux.org>:
25# version 2.4: fix reload of config file
26# 2012-02-02, nils_2 <weechatter@arcor.de>:
27# version 2.3: fixed: refresh problem with new search results and cursor was outside window.
28# : add: new option "current_line" in title bar
29# version 2.2: fixed: refresh error when toggling plugins description
30# 2011-11-05, nils_2 <weechatter@arcor.de>:
31# version 2.1: use own config file (iset.conf), fix own help color (used immediately)
32# 2011-10-16, nils_2 <weechatter@arcor.de>:
33# version 2.0: add support for left-mouse-button and more sensitive mouse gesture (for integer/color options)
34# add help text for mouse support
35# 2011-09-20, Sebastien Helleu <flashcode@flashtux.org>:
36# version 1.9: add mouse support, fix iset buffer, fix errors on first load under FreeBSD
37# 2011-07-21, nils_2 <weechatter@arcor.de>:
38# version 1.8: added: option "show_plugin_description" (alt+p)
39# fixed: typos in /help iset (lower case for alt+'x' keys)
40# 2011-05-29, nils_2 <weechatter@arcor.de>:
41# version 1.7: added: version check for future needs
42# added: new option (scroll_horiz) and usage of scroll_horiz function (weechat >= 0.3.6 required)
43# fixed: help_bar did not pop up immediately using key-shortcut
44# 2011-02-19, nils_2 <weechatter@arcor.de>:
45# version 1.6: added: display of all possible values in help bar (show_help_extra_info)
46# fixed: external user options never loaded when starting iset first time
47# 2011-02-13, Sebastien Helleu <flashcode@flashtux.org>:
48# version 1.5: use new help format for command arguments
49# 2011-02-03, nils_2 <weechatter@arcor.de>:
50# version 1.4: fixed: restore value filter after /upgrade using buffer local variable.
51# 2011-01-14, nils_2 <weechatter@arcor.de>:
52# version 1.3: added function to search for values (option value_search_char).
53# code optimization.
54# 2010-12-26, Sebastien Helleu <flashcode@flashtux.org>:
55# version 1.2: improve speed of /upgrade when iset buffer is open,
56# restore filter used after /upgrade using buffer local variable,
57# use /iset filter argument if buffer is open.
58# 2010-11-21, drubin <drubin+weechat@smartcube.co.za>:
59# version 1.1.1: fix bugs with cursor position
60# 2010-11-20, nils_2 <weechatter@arcor.de>:
61# version 1.1: cursor position set to value
62# 2010-08-03, Sebastien Helleu <flashcode@flashtux.org>:
63# version 1.0: move misplaced call to infolist_free()
64# 2010-02-02, rettub <rettub@gmx.net>:
65# version 0.9: turn all the help stuff off if option 'show_help_bar' is 'off',
66# new key binding <alt>-<v> to toggle help_bar and help stuff on/off
67# 2010-01-30, nils_2 <weechatter@arcor.de>:
68# version 0.8: fix error when option does not exist
69# 2010-01-24, Sebastien Helleu <flashcode@flashtux.org>:
70# version 0.7: display iset bar only on iset buffer
71# 2010-01-22, nils_2 <weechatter@arcor.de> and drubin:
72# version 0.6: add description in a bar, fix singular/plural bug in title bar,
73# fix selected line when switching buffer
74# 2009-06-21, Sebastien Helleu <flashcode@flashtux.org>:
75# version 0.5: fix bug with iset buffer after /upgrade
76# 2009-05-02, Sebastien Helleu <flashcode@flashtux.org>:
77# version 0.4: sync with last API changes
78# 2009-01-04, Sebastien Helleu <flashcode@flashtux.org>:
79# version 0.3: open iset buffer when /iset command is executed
80# 2009-01-04, Sebastien Helleu <flashcode@flashtux.org>:
81# version 0.2: use null values for options, add colors, fix refresh bugs,
82# use new keys to reset/unset options, sort options by name,
83# display number of options in buffer's title
84# 2008-11-05, Sebastien Helleu <flashcode@flashtux.org>:
85# version 0.1: first official version
86# 2008-04-19, Sebastien Helleu <flashcode@flashtux.org>:
87# script creation
88
89use strict;
90
91my $PRGNAME = "iset";
92my $VERSION = "2.5";
93my $DESCR = "Interactive Set for configuration options";
94my $AUTHOR = "Sebastien Helleu <flashcode\@flashtux.org>";
95my $LICENSE = "GPL3";
96my $LANG = "perl";
97my $ISET_CONFIG_FILE_NAME = "iset";
98
99my $iset_config_file;
100my $iset_buffer = "";
101my $wee_version_number = 0;
102my @iset_focus = ();
103my @options_names = ();
104my @options_types = ();
105my @options_values = ();
106my @options_is_null = ();
107my $option_max_length = 0;
108my $current_line = 0;
109my $filter = "*";
110my $description = "";
111my $options_name_copy = "";
112my $iset_filter_title = "";
113
114my %options_iset;
115
116my %mouse_keys = ("\@chat(perl.$PRGNAME):button1" => "hsignal:iset_mouse",
117 "\@chat(perl.$PRGNAME):button2*" => "hsignal:iset_mouse",
118 "\@chat(perl.$PRGNAME):wheelup" => "/repeat 5 /iset **up",
119 "\@chat(perl.$PRGNAME):wheeldown" => "/repeat 5 /iset **down");
120
121
122sub iset_title
123{
124 if ($iset_buffer ne "")
125 {
126 my $current_line_text = "";
127 $current_line_text = ($current_line + 1) . "/" if (weechat::config_boolean($options_iset{"show_current_line"}) == 1);
128 $iset_filter_title = "Filter: " if ($iset_filter_title eq "");
129 $filter = "*" if ($filter eq "");
130 my $postfix = "s";
131 my $option_txt = " option";
132 my $opt_txt = $option_txt;
133 $opt_txt = $option_txt.$postfix if (@options_names > 1);
134 my $show_plugin_descr_txt = "";
135 $show_plugin_descr_txt = " (plugins description hidden)" if (weechat::config_boolean($options_iset{"show_plugin_description"}) == 0);
136 weechat::buffer_set($iset_buffer, "title",
137 "Interactive set (iset.pl v$VERSION) | "
138 .$iset_filter_title.weechat::color("yellow").$filter.weechat::color("default")." | "
139 .$current_line_text.@options_names.$opt_txt . $show_plugin_descr_txt);
140 }
141}
142
143sub iset_filter
144{
145 $filter = $_[0];
146 $filter = "$1.*" if ($filter =~ /f (.*)/);
147 $filter = "*.$1.*" if ($filter =~ /s (.*)/);
148 if ((substr($filter, 0, 1) ne "*") && (substr($filter, -1, 1) ne "*"))
149 {
150 $filter = "*".$filter."*";
151 }
152 if ($iset_buffer ne "")
153 {
154 weechat::buffer_set($iset_buffer, "localvar_set_iset_filter", $filter);
155 }
156}
157
158sub iset_buffer_input
159{
160 my ($data, $buffer, $string) = ($_[0], $_[1], $_[2]);
161
162 my $string2 = substr($string, 0, 1);
163 if ($string2 eq weechat::config_string($options_iset{"value_search_char"}))
164 {
165 $filter = substr($string, 1);
166 iset_get_values($filter);
167 $iset_filter_title = "Filter (by value): ";
168 if ($iset_buffer ne "")
169 {
170 weechat::buffer_set($iset_buffer, "localvar_set_iset_filter", $filter);
171 }
172 }
173 else
174 {
175 $iset_filter_title = "";
176 iset_filter($string);
177 iset_get_options();
178 }
179 weechat::buffer_clear($buffer);
180 iset_title($iset_filter_title);
181 $current_line = 0;
182 iset_refresh();
183 return weechat::WEECHAT_RC_OK;
184}
185
186sub iset_buffer_close
187{
188 $iset_buffer = "";
189
190 return weechat::WEECHAT_RC_OK;
191}
192
193sub iset_init
194{
195 $current_line = 0;
196 $iset_buffer = weechat::buffer_search($LANG, $PRGNAME);
197 if ($iset_buffer eq "")
198 {
199 $iset_buffer = weechat::buffer_new($PRGNAME, "iset_buffer_input", "", "iset_buffer_close", "");
200 }
201 else
202 {
203 my $new_filter = weechat::buffer_get_string($iset_buffer, "localvar_iset_filter");
204 $filter = $new_filter if ($new_filter ne "");
205 }
206 if ($iset_buffer ne "")
207 {
208 weechat::buffer_set($iset_buffer, "type", "free");
209 iset_title();
210 weechat::buffer_set($iset_buffer, "key_bind_ctrl-L", "/iset **refresh");
211 weechat::buffer_set($iset_buffer, "key_bind_meta2-A", "/iset **up");
212 weechat::buffer_set($iset_buffer, "key_bind_meta2-B", "/iset **down");
213 weechat::buffer_set($iset_buffer, "key_bind_meta2-23~", "/iset **left");
214 weechat::buffer_set($iset_buffer, "key_bind_meta2-24~" , "/iset **right");
215 weechat::buffer_set($iset_buffer, "key_bind_meta- ", "/iset **toggle");
216 weechat::buffer_set($iset_buffer, "key_bind_meta-+", "/iset **incr");
217 weechat::buffer_set($iset_buffer, "key_bind_meta--", "/iset **decr");
218 weechat::buffer_set($iset_buffer, "key_bind_meta-imeta-r", "/iset **reset");
219 weechat::buffer_set($iset_buffer, "key_bind_meta-imeta-u", "/iset **unset");
220 weechat::buffer_set($iset_buffer, "key_bind_meta-ctrl-J", "/iset **set");
221 weechat::buffer_set($iset_buffer, "key_bind_meta-ctrl-M", "/iset **set");
222 weechat::buffer_set($iset_buffer, "key_bind_meta-meta2-1~", "/iset **scroll_top");
223 weechat::buffer_set($iset_buffer, "key_bind_meta-meta2-4~", "/iset **scroll_bottom");
224 weechat::buffer_set($iset_buffer, "key_bind_meta-v", "/iset **toggle_help");
225 weechat::buffer_set($iset_buffer, "key_bind_meta-p", "/iset **toggle_show_plugin_desc");
226 weechat::buffer_set($iset_buffer, "localvar_set_iset_filter", $filter);
227 }
228}
229
230sub iset_get_options
231{
232 @iset_focus = ();
233
234 @options_names = ();
235 @options_types = ();
236 @options_values = ();
237 @options_is_null = ();
238 $option_max_length = 0;
239 my %options_internal = ();
240 my $i = 0;
241 my $key;
242 my $iset_struct;
243 my %iset_struct;
244
245 my $infolist = weechat::infolist_get("option", "", $filter);
246 while (weechat::infolist_next($infolist))
247 {
248 $key = sprintf("%08d", $i);
249 my $name = weechat::infolist_string($infolist, "full_name");
250 next if (weechat::config_boolean($options_iset{"show_plugin_description"}) == 0 and index ($name, "plugins.desc.") != -1);
251 my $type = weechat::infolist_string($infolist, "type");
252 my $value = weechat::infolist_string($infolist, "value");
253 my $is_null = weechat::infolist_integer($infolist, "value_is_null");
254
255 $options_internal{$name}{"type"} = $type;
256 $options_internal{$name}{"value"} = $value;
257 $options_internal{$name}{"is_null"} = $is_null;
258 $option_max_length = length($name) if (length($name) > $option_max_length);
259
260 $iset_struct{$key} = $options_internal{$name};
261 push(@iset_focus, $iset_struct{$key});
262 $i++;
263 }
264 weechat::infolist_free($infolist);
265
266 foreach my $name (sort keys %options_internal)
267 {
268 push(@options_names, $name);
269 push(@options_types, $options_internal{$name}{"type"});
270 push(@options_values, $options_internal{$name}{"value"});
271 push(@options_is_null, $options_internal{$name}{"is_null"});
272 }
273}
274
275sub iset_get_values
276{
277 my $var_value = $_[0];
278 @options_names = ();
279 @options_types = ();
280 @options_values = ();
281 @options_is_null = ();
282 $option_max_length = 0;
283 my %options_internal = ();
284 my $i = 0;
285 my $infolist = weechat::infolist_get("option", "", "*");
286 $var_value =~ tr/[a-z][0-9].=-_!//cd; # kill meta chars
287 $var_value = lc($var_value);
288 while (weechat::infolist_next($infolist))
289 {
290 my $name = weechat::infolist_string($infolist, "full_name");
291 next if (weechat::config_boolean($options_iset{"show_plugin_description"}) == 0 and index ($name, "plugins.desc.") != -1);
292 my $type = weechat::infolist_string($infolist, "type");
293 my $value = weechat::infolist_string($infolist, "value");
294 my $is_null = weechat::infolist_integer($infolist, "value_is_null");
295 if (lc($value) =~ m/$var_value/)
296 {
297 $options_internal{$name}{"type"} = $type;
298 $options_internal{$name}{"value"} = $value;
299 $options_internal{$name}{"is_null"} = $is_null;
300 $option_max_length = length($name) if (length($name) > $option_max_length);
301 }
302 $i++;
303 }
304 weechat::infolist_free($infolist);
305 foreach my $name (sort keys %options_internal)
306 {
307 push(@options_names, $name);
308 push(@options_types, $options_internal{$name}{"type"});
309 push(@options_values, $options_internal{$name}{"value"});
310 push(@options_is_null, $options_internal{$name}{"is_null"});
311 }
312 weechat::buffer_set($iset_buffer, "localvar_set_iset_filter", $var_value);
313}
314
315sub iset_refresh_line
316{
317 if ($iset_buffer ne "")
318 {
319 my $y = $_[0];
320 if ($y <= $#options_names)
321 {
322 return if (! defined($options_types[$y]));
323 my $format = sprintf("%%s%%-%ds %%s %%-7s %%s %%s%%s%%s", $option_max_length);
324 my $around = "";
325 $around = "\"" if ((!$options_is_null[$y]) && ($options_types[$y] eq "string"));
326
327 my $color1 = weechat::color(weechat::config_color($options_iset{"color_option"}));
328 my $color2 = weechat::color(weechat::config_color($options_iset{"color_type"}));
329 my $color3 = "";
330 if ($options_is_null[$y])
331 {
332 $color3 = weechat::color(weechat::config_color($options_iset{"color_value_undef"}));
333 }
334 else
335 {
336 $color3 = weechat::color(weechat::config_color($options_iset{"color_value"}));
337 }
338 if ($y == $current_line)
339 {
340 $color1 = weechat::color(weechat::config_color($options_iset{"color_option_selected"}).",".weechat::config_color($options_iset{"color_bg_selected"}));
341 $color2 = weechat::color(weechat::config_color($options_iset{"color_type_selected"}).",".weechat::config_color($options_iset{"color_bg_selected"}));
342 if ($options_is_null[$y])
343 {
344 $color3 = weechat::color(weechat::config_color($options_iset{"color_value_undef_selected"}).",".weechat::config_color($options_iset{"color_bg_selected"}));
345 }
346 else
347 {
348 $color3 = weechat::color(weechat::config_color($options_iset{"color_value_selected"}).",".weechat::config_color($options_iset{"color_bg_selected"}));
349 }
350 }
351 my $value = $options_values[$y];
352 $value = "(undef)" if ($options_is_null[$y]);
353 my $strline = sprintf($format,
354 $color1, $options_names[$y],
355 $color2, $options_types[$y],
356 $color3, $around, $value, $around);
357 weechat::print_y($iset_buffer, $y, $strline);
358 }
359 }
360}
361
362sub iset_refresh
363{
364 iset_title();
365 if (($iset_buffer ne "") && ($#options_names >= 0))
366 {
367 foreach my $y (0 .. $#options_names)
368 {
369 iset_refresh_line($y);
370 }
371 }
372
373 weechat::bar_item_update("isetbar_help") if (weechat::config_boolean($options_iset{"show_help_bar"}) == 1);
374}
375
376sub iset_full_refresh
377{
378 $iset_buffer = weechat::buffer_search($LANG, $PRGNAME);
379 if ($iset_buffer ne "")
380 {
381 weechat::buffer_clear($iset_buffer);
382 # search for "*" in $filter.
383 if ($filter =~ m/\*/)
384 {
385 iset_get_options();
386 }
387 else
388 {
389 iset_get_values($filter);
390 $iset_filter_title = "Filter (by value): ";
391 }
392 if (weechat::config_boolean($options_iset{"show_plugin_description"}) == 1)
393 {
394 iset_set_current_line($current_line);
395 }else
396 {
397 $current_line = $#options_names if ($current_line > $#options_names);
398 }
399 iset_refresh();
400 weechat::command($iset_buffer, "/window refresh");
401 }
402}
403
404sub iset_set_current_line
405{
406 my $new_current_line = $_[0];
407 my $old_current_line = $current_line;
408 $current_line = $new_current_line;
409 $current_line = $#options_names if ($current_line > $#options_names);
410 if ($old_current_line != $current_line)
411 {
412 iset_refresh_line($old_current_line);
413 iset_refresh_line($current_line);
414 weechat::bar_item_update("isetbar_help") if (weechat::config_boolean($options_iset{"show_help_bar"}) == 1);
415 }
416}
417
418sub iset_signal_window_scrolled_cb
419{
420 my ($data, $signal, $signal_data) = ($_[0], $_[1], $_[2]);
421 if ($iset_buffer ne "")
422 {
423 my $infolist = weechat::infolist_get("window", $signal_data, "");
424 if (weechat::infolist_next($infolist))
425 {
426 if (weechat::infolist_pointer($infolist, "buffer") eq $iset_buffer)
427 {
428 my $old_current_line = $current_line;
429 my $new_current_line = $current_line;
430 my $start_line_y = weechat::infolist_integer($infolist, "start_line_y");
431 my $chat_height = weechat::infolist_integer($infolist, "chat_height");
432 $new_current_line += $chat_height if ($new_current_line < $start_line_y);
433 $new_current_line -= $chat_height if ($new_current_line >= $start_line_y + $chat_height);
434 $new_current_line = $start_line_y if ($new_current_line < $start_line_y);
435 $new_current_line = $start_line_y + $chat_height - 1 if ($new_current_line >= $start_line_y + $chat_height);
436 iset_set_current_line($new_current_line);
437 }
438 }
439 weechat::infolist_free($infolist);
440 }
441
442 return weechat::WEECHAT_RC_OK;
443}
444
445sub iset_get_window_number
446{
447 if ($iset_buffer ne "")
448 {
449 my $window = weechat::window_search_with_buffer($iset_buffer);
450 return "-window ".weechat::window_get_integer ($window, "number")." " if ($window ne "");
451 }
452 return "";
453}
454
455sub iset_check_line_outside_window
456{
457 if ($iset_buffer ne "")
458 {
459 undef my $infolist;
460 if ($wee_version_number >= 0x00030500)
461 {
462 my $window = weechat::window_search_with_buffer($iset_buffer);
463 $infolist = weechat::infolist_get("window", $window, "") if $window;
464 }
465 else
466 {
467 $infolist = weechat::infolist_get("window", "", "current");
468 }
469 if ($infolist)
470 {
471 if (weechat::infolist_next($infolist))
472 {
473 my $start_line_y = weechat::infolist_integer($infolist, "start_line_y");
474 my $chat_height = weechat::infolist_integer($infolist, "chat_height");
475 my $window_number = "";
476 if ($wee_version_number >= 0x00030500)
477 {
478 $window_number = "-window ".weechat::infolist_integer($infolist, "number")." ";
479 }
480 if ($start_line_y > $current_line)
481 {
482 weechat::command($iset_buffer, "/window scroll ".$window_number."-".($start_line_y - $current_line));
483 }
484 else
485 {
486 if ($start_line_y <= $current_line - $chat_height)
487 {
488 weechat::command($iset_buffer, "/window scroll ".$window_number."+".($current_line - $start_line_y - $chat_height + 1));
489 }
490 }
491 }
492 weechat::infolist_free($infolist);
493 }
494 }
495}
496
497sub iset_get_option_name_index
498{
499 my $option_name = $_[0];
500 my $index = 0;
501 while ($index <= $#options_names)
502 {
503 return -1 if ($options_names[$index] gt $option_name);
504 return $index if ($options_names[$index] eq $option_name);
505 $index++;
506 }
507 return -1;
508}
509
510sub iset_config_cb
511{
512 my ($data, $option_name, $value) = ($_[0], $_[1], $_[2]);
513
514 if ($iset_buffer ne "")
515 {
516 return weechat::WEECHAT_RC_OK if (weechat::info_get("weechat_upgrading", "") eq "1");
517
518 my $index = iset_get_option_name_index($option_name);
519 if ($index >= 0)
520 {
521 # refresh info about changed option
522 my $infolist = weechat::infolist_get("option", "", $option_name);
523 if ($infolist)
524 {
525 weechat::infolist_next($infolist);
526 if (weechat::infolist_fields($infolist))
527 {
528 $options_types[$index] = weechat::infolist_string($infolist, "type");
529 $options_values[$index] = weechat::infolist_string($infolist, "value");
530 $options_is_null[$index] = weechat::infolist_integer($infolist, "value_is_null");
531 iset_refresh_line($index);
532 iset_title($iset_filter_title) if ($option_name eq "iset.look.show_current_line");
533 }
534 else
535 {
536 iset_full_refresh();
537 }
538 weechat::infolist_free($infolist);
539 }
540 }
541 else
542 {
543 iset_full_refresh() if ($option_name ne "weechat.bar.isetbar.hidden");
544 }
545 }
546
547 return weechat::WEECHAT_RC_OK;
548}
549
550sub iset_set_option
551{
552 my $option = weechat::config_get($_[0]);
553 weechat::config_option_set($option, $_[1], 1) if ($option ne "");
554}
555
556sub iset_reset_option
557{
558 my $option = weechat::config_get($_[0]);
559 weechat::config_option_reset($option, 1) if ($option ne "");
560}
561
562sub iset_unset_option
563{
564 my $option = weechat::config_get($_[0]);
565 weechat::config_option_unset($option) if ($option ne "");
566 weechat::buffer_clear($iset_buffer);
567 iset_refresh();
568}
569
570
571sub iset_cmd_cb
572{
573 my ($data, $buffer, $args) = ($_[0], $_[1], $_[2]);
574 my $filter_set = 0;
575 if (($args ne "") && (substr($args, 0, 2) ne "**"))
576 {
577 if (substr($args, 0, 1) eq weechat::config_string($options_iset{"value_search_char"}))
578 {
579 my $var_value = substr($args, 1); # cut value_search_char
580 if ($iset_buffer ne "")
581 {
582 weechat::buffer_clear($iset_buffer);
583 weechat::command($iset_buffer, "/window refresh");
584 }
585 iset_init();
586 iset_get_values($var_value);
587 iset_refresh();
588 weechat::buffer_set($iset_buffer, "display", "1");
589 $iset_filter_title = "Filter (by value): ";
590 $filter = $var_value;
591 iset_title($iset_filter_title);
592 return weechat::WEECHAT_RC_OK;
593 }
594 else
595 {
596 iset_filter($args);
597 $filter_set = 1;
598 my $ptrbuf = weechat::buffer_search($LANG, $PRGNAME);
599 if ($ptrbuf eq "")
600 {
601 iset_init();
602 iset_get_options();
603 iset_full_refresh();
604 weechat::buffer_set(weechat::buffer_search($LANG, $PRGNAME), "display", "1");
605 return weechat::WEECHAT_RC_OK;
606 }
607 }
608 }
609
610 if ($iset_buffer eq "")
611 {
612 iset_init();
613 iset_get_options();
614 iset_refresh();
615 }
616 else
617 {
618 iset_full_refresh() if ($filter_set);
619 }
620
621 if ($args eq "")
622 {
623 weechat::buffer_set($iset_buffer, "display", "1");
624 }
625 else
626 {
627 if ($args eq "**refresh")
628 {
629 iset_full_refresh();
630 }
631 if ($args eq "**up")
632 {
633 if ($current_line > 0)
634 {
635 $current_line--;
636 iset_refresh_line($current_line + 1);
637 iset_refresh_line($current_line);
638 iset_check_line_outside_window();
639 }
640 }
641 if ($args eq "**down")
642 {
643 if ($current_line < $#options_names)
644 {
645 $current_line++;
646 iset_refresh_line($current_line - 1);
647 iset_refresh_line($current_line);
648 iset_check_line_outside_window();
649 }
650 }
651 if ($args eq "**left" && $wee_version_number >= 0x00030600)
652 {
653 weechat::command($iset_buffer, "/window scroll_horiz ".iset_get_window_number()."-".weechat::config_integer($options_iset{"scroll_horiz"})."%");
654 }
655 if ($args eq "**right" && $wee_version_number >= 0x00030600)
656 {
657 weechat::command($iset_buffer, "/window scroll_horiz ".iset_get_window_number().weechat::config_integer($options_iset{"scroll_horiz"})."%");
658 }
659 if ($args eq "**scroll_top")
660 {
661 my $old_current_line = $current_line;
662 $current_line = 0;
663 iset_refresh_line ($old_current_line);
664 iset_refresh_line ($current_line);
665 weechat::command($iset_buffer, "/window scroll_top ".iset_get_window_number());
666 }
667 if ($args eq "**scroll_bottom")
668 {
669 my $old_current_line = $current_line;
670 $current_line = $#options_names;
671 iset_refresh_line ($old_current_line);
672 iset_refresh_line ($current_line);
673 weechat::command($iset_buffer, "/window scroll_bottom ".iset_get_window_number());
674 }
675 if ($args eq "**toggle")
676 {
677 if ($options_types[$current_line] eq "boolean")
678 {
679 iset_set_option($options_names[$current_line], "toggle");
680 }
681 }
682 if ($args eq "**incr")
683 {
684 if (($options_types[$current_line] eq "integer")
685 || ($options_types[$current_line] eq "color"))
686 {
687 iset_set_option($options_names[$current_line], "++1");
688 }
689 }
690 if ($args eq "**decr")
691 {
692 if (($options_types[$current_line] eq "integer")
693 || ($options_types[$current_line] eq "color"))
694 {
695 iset_set_option($options_names[$current_line], "--1");
696 }
697 }
698 if ($args eq "**reset")
699 {
700 iset_reset_option($options_names[$current_line]);
701 }
702 if ($args eq "**unset")
703 {
704 iset_unset_option($options_names[$current_line]);
705 }
706 if ($args eq "**toggle_help")
707 {
708 if (weechat::config_boolean($options_iset{"show_help_bar"}) == 1)
709 {
710 weechat::config_option_set($options_iset{"show_help_bar"},0,1);
711 iset_show_bar(0);
712 }
713 else
714 {
715 weechat::config_option_set($options_iset{"show_help_bar"},1,1);
716 iset_show_bar(1);
717 }
718 }
719 if ($args eq "**toggle_show_plugin_desc")
720 {
721 if (weechat::config_boolean($options_iset{"show_plugin_description"}) == 1)
722 {
723 weechat::config_option_set($options_iset{"show_plugin_description"},0,1);
724 iset_full_refresh();
725 iset_check_line_outside_window();
726 }
727 else
728 {
729 weechat::config_option_set($options_iset{"show_plugin_description"},1,1);
730 iset_full_refresh();
731 iset_check_line_outside_window();
732 }
733 }
734 if ($args eq "**set")
735 {
736 my $quote = "";
737 my $value = $options_values[$current_line];
738 if ($options_is_null[$current_line])
739 {
740 $value = "null";
741 }
742 else
743 {
744 $quote = "\"" if ($options_types[$current_line] eq "string");
745 }
746 weechat::buffer_set($iset_buffer, "input", "/set ".$options_names[$current_line]." ".$quote.$value.$quote);
747 weechat::command($iset_buffer, "/input move_beginning_of_line");
748 weechat::command($iset_buffer, "/input move_next_word");
749 weechat::command($iset_buffer, "/input move_next_word");
750 weechat::command($iset_buffer, "/input move_next_char");
751 weechat::command($iset_buffer, "/input move_next_char") if ($quote ne "");
752 }
753 }
754 weechat::bar_item_update("isetbar_help") if (weechat::config_boolean($options_iset{"show_help_bar"}) == 1);
755 return weechat::WEECHAT_RC_OK;
756}
757
758sub iset_get_help
759{
760 my ($redraw) = ($_[0]);
761
762 return '' if (weechat::config_boolean($options_iset{"show_help_bar"}) == 0);
763
764 if (not defined $options_names[$current_line])
765 {
766 return "No option selected. Set a new filter using command line (use '*' to see all options)";
767 }
768 if ($options_name_copy eq $options_names[$current_line] and not defined $redraw)
769 {
770 return $description;
771 }
772 $options_name_copy = $options_names[$current_line];
773 my $optionlist ="";
774 $optionlist = weechat::infolist_get("option", "", $options_names[$current_line]);
775 weechat::infolist_next($optionlist);
776 my $full_name = weechat::infolist_string($optionlist,"full_name");
777 my $option_desc = "";
778 my $option_default_value = "";
779 my $option_range = "";
780 my $possible_values = "";
781 my $re = qq(\Q$full_name);
782 if (grep (/^$re$/,$options_names[$current_line]))
783 {
784 $option_desc = weechat::infolist_string($optionlist, "description_nls");
785 $option_desc = weechat::infolist_string($optionlist, "description") if ($option_desc eq "");
786 $option_desc = "No help found" if ($option_desc eq "");
787 $option_default_value = weechat::infolist_string($optionlist, "default_value");
788 $possible_values = weechat::infolist_string($optionlist, "string_values") if (weechat::infolist_string($optionlist, "string_values") ne "");
789 if ((weechat::infolist_string($optionlist, "type") eq "integer") && ($possible_values eq ""))
790 {
791 $option_range = weechat::infolist_integer($optionlist, "min")
792 ." .. ".weechat::infolist_integer($optionlist, "max");
793 }
794 }
795 weechat::infolist_free($optionlist);
796 iset_title();
797
798 $description = weechat::color(weechat::config_color($options_iset{"color_help_option_name"})).$options_names[$current_line]
799 .weechat::color("bar_fg").": "
800 .weechat::color(weechat::config_color($options_iset{"color_help_text"})).$option_desc;
801
802 # show additional infos like default value and possible values
803
804 if (weechat::config_boolean($options_iset{"show_help_extra_info"}) == 1)
805 {
806 $description .=
807 weechat::color("bar_delim")." ["
808 .weechat::color("bar_fg")."default: "
809 .weechat::color("bar_delim")."\""
810 .weechat::color(weechat::config_color($options_iset{"color_help_default_value"})).$option_default_value
811 .weechat::color("bar_delim")."\"";
812 if ($option_range ne "")
813 {
814 $description .= weechat::color("bar_fg").", values: ".$option_range;
815 }
816 if ($possible_values ne "")
817 {
818 $possible_values =~ s/\|/", "/g; # replace '|' to '", "'
819 $description .= weechat::color("bar_fg").", values: ". "\"" . $possible_values . "\"";
820
821 }
822 $description .= weechat::color("bar_delim")."]";
823 }
824 return $description;
825}
826
827sub iset_check_condition_isetbar_cb
828{
829 my ($data, $modifier, $modifier_data, $string) = ($_[0], $_[1], $_[2], $_[3]);
830 my $buffer = weechat::window_get_pointer($modifier_data, "buffer");
831 if ($buffer ne "")
832 {
833 if ((weechat::buffer_get_string($buffer, "plugin") eq $LANG)
834 && (weechat::buffer_get_string($buffer, "name") eq $PRGNAME))
835 {
836 return "1";
837 }
838 }
839 return "0";
840}
841
842sub iset_show_bar
843{
844 my $show = $_[0];
845 my $barhidden = weechat::config_get("weechat.bar.isetbar.hidden");
846 if ($barhidden)
847 {
848 if ($show)
849 {
850 if (weechat::config_boolean($options_iset{"show_help_bar"}) == 1)
851 {
852 if (weechat::config_boolean($barhidden))
853 {
854 weechat::config_option_set($barhidden, 0, 1);
855 }
856 }
857 }
858 else
859 {
860 if (!weechat::config_boolean($barhidden))
861 {
862 weechat::config_option_set($barhidden, 1, 1);
863 }
864 }
865 }
866}
867
868sub iset_signal_buffer_switch_cb
869{
870 my $buffer_pointer = $_[2];
871 my $show_bar = 0;
872 $show_bar = 1 if (weechat::buffer_get_integer($iset_buffer, "num_displayed") > 0);
873 iset_show_bar($show_bar);
874 iset_check_line_outside_window() if ($buffer_pointer eq $iset_buffer);
875 return weechat::WEECHAT_RC_OK;
876}
877
878sub iset_item_cb
879{
880 return iset_get_help();
881}
882
883sub iset_upgrade_ended
884{
885 iset_full_refresh();
886}
887
888sub iset_end
889{
890 # when script is unloaded, we hide bar
891 iset_show_bar(0);
892}
893
894# -------------------------------[ mouse support ]-------------------------------------
895
896sub hook_focus_iset_cb
897{
898 my %info = %{$_[1]};
899 my $bar_item_line = int($info{"_bar_item_line"});
900 undef my $hash;
901 if (($info{"_buffer_name"} eq $PRGNAME) && $info{"_buffer_plugin"} eq $LANG && ($bar_item_line >= 0) && ($bar_item_line <= $#iset_focus))
902 {
903 $hash = $iset_focus[$bar_item_line];
904 }
905 else
906 {
907 $hash = {};
908 my $hash_focus = $iset_focus[0];
909 foreach my $key (keys %$hash_focus)
910 {
911 $hash->{$key} = "?";
912 }
913 }
914 return $hash;
915}
916
917# _chat_line_y contains selected line
918sub iset_hsignal_mouse_cb
919{
920 my ($data, $signal, %hash) = ($_[0], $_[1], %{$_[2]});
921
922 if ($hash{"_buffer_name"} eq $PRGNAME && ($hash{"_buffer_plugin"} eq $LANG))
923 {
924 if ($hash{"_key"} eq "button1")
925 {
926 $current_line = $hash{"_chat_line_y"};
927 iset_refresh_line($current_line);
928 iset_refresh();
929 }
930 elsif ($hash{"_key"} eq "button2")
931 {
932 if ($options_types[$hash{"_chat_line_y"}] eq "boolean")
933 {
934 iset_set_option($options_names[$hash{"_chat_line_y"}], "toggle");
935 $current_line = $hash{"_chat_line_y"};
936 iset_refresh_line($current_line);
937 iset_refresh();
938 }
939 elsif ($options_types[$hash{"_chat_line_y"}] eq "string")
940 {
941 $current_line = $hash{"_chat_line_y"};
942 iset_refresh_line($current_line);
943 iset_refresh();
944 weechat::command("", "/$PRGNAME **set");
945 }
946 }
947 elsif ($hash{"_key"} eq "button2-gesture-left" or $hash{"_key"} eq "button2-gesture-left-long")
948 {
949 if ($options_types[$hash{"_chat_line_y"}] eq "integer" or ($options_types[$hash{"_chat_line_y"}] eq "color"))
950 {
951 $current_line = $hash{"_chat_line_y"};
952 iset_refresh_line($current_line);
953 iset_refresh();
954 my $distance = distance($hash{"_chat_line_x"},$hash{"_chat_line_x2"});
955 weechat::command("", "/repeat $distance /$PRGNAME **decr");
956 }
957 }
958 elsif ($hash{"_key"} eq "button2-gesture-right" or $hash{"_key"} eq "button2-gesture-right-long")
959 {
960 if ($options_types[$hash{"_chat_line_y"}] eq "integer" or ($options_types[$hash{"_chat_line_y"}] eq "color"))
961 {
962 $current_line = $hash{"_chat_line_y"};
963 iset_refresh_line($current_line);
964 iset_refresh();
965 my $distance = distance($hash{"_chat_line_x"},$hash{"_chat_line_x2"});
966 weechat::command("", "/repeat $distance /$PRGNAME **incr");
967 }
968 }
969 }
970}
971sub distance
972{
973 my ($x1,$x2) = ($_[0], $_[1]);
974 my $distance;
975 $distance = $x1 - $x2;
976 $distance = abs($distance);
977 if ($distance > 0)
978 {
979 use integer;
980 $distance = $distance / 3;
981 $distance = 1 if ($distance == 0);
982 }
983 elsif ($distance == 0)
984 {
985 $distance = 1;
986 }
987 return $distance;
988}
989
990# -----------------------------------[ config ]---------------------------------------
991
992sub iset_config_init
993{
994 $iset_config_file = weechat::config_new($ISET_CONFIG_FILE_NAME,"iset_config_reload_cb","");
995 return if ($iset_config_file eq "");
996
997 # section "color"
998 my $section_color = weechat::config_new_section($iset_config_file,"color", 0, 0, "", "", "", "", "", "", "", "", "", "");
999 if ($section_color eq "")
1000 {
1001 weechat::config_free($iset_config_file);
1002 return;
1003 }
1004 $options_iset{"color_option"} = weechat::config_new_option(
1005 $iset_config_file, $section_color,
1006 "option", "color", "Color for option name in iset buffer", "", 0, 0,
1007 "default", "default", 0, "", "", "full_refresh_cb", "", "", "");
1008 $options_iset{"color_option_selected"} = weechat::config_new_option(
1009 $iset_config_file, $section_color,
1010 "option_selected", "color", "Color for selected option name in iset buffer", "", 0, 0,
1011 "white", "white", 0, "", "", "full_refresh_cb", "", "", "");
1012 $options_iset{"color_type"} = weechat::config_new_option(
1013 $iset_config_file, $section_color,
1014 "type", "color", "Color for option type (integer, boolean, string)", "", 0, 0,
1015 "brown", "brown", 0, "", "", "full_refresh_cb", "", "", "");
1016 $options_iset{"color_type_selected"} = weechat::config_new_option(
1017 $iset_config_file, $section_color,
1018 "type_selected", "color", "Color for selected option type (integer, boolean, string)", "", 0, 0,
1019 "yellow", "yellow", 0, "", "", "full_refresh_cb", "", "", "");
1020 $options_iset{"color_value"} = weechat::config_new_option(
1021 $iset_config_file, $section_color,
1022 "value", "color", "Color for option value", "", 0, 0,
1023 "cyan", "cyan", 0, "", "", "full_refresh_cb", "", "", "");
1024 $options_iset{"color_value_selected"} = weechat::config_new_option(
1025 $iset_config_file, $section_color,
1026 "value_selected", "color", "Color for selected option value", "", 0, 0,
1027 "lightcyan", "lightcyan", 0, "", "", "full_refresh_cb", "", "", "");
1028 $options_iset{"color_value_undef"} = weechat::config_new_option(
1029 $iset_config_file, $section_color,
1030 "value_undef", "color", "Color for option value undef", "", 0, 0,
1031 "green", "green", 0, "", "", "full_refresh_cb", "", "", "");
1032 $options_iset{"color_value_undef_selected"} = weechat::config_new_option(
1033 $iset_config_file, $section_color,
1034 "value_undef_selected", "color", "Color for selected option value undef", "", 0, 0,
1035 "lightgreen", "lightgreen", 0, "", "", "full_refresh_cb", "", "", "");
1036 $options_iset{"color_bg_selected"} = weechat::config_new_option(
1037 $iset_config_file, $section_color,
1038 "bg_selected", "color", "Background color for current selected option", "", 0, 0,
1039 "red", "red", 0, "", "", "full_refresh_cb", "", "", "");
1040 $options_iset{"color_help_option_name"} = weechat::config_new_option(
1041 $iset_config_file, $section_color,
1042 "help_option_name", "color", "Color for option name in help-bar", "", 0, 0,
1043 "white", "white", 0, "", "", "bar_refresh", "", "", "");
1044 $options_iset{"color_help_text"} = weechat::config_new_option(
1045 $iset_config_file, $section_color,
1046 "help_text", "color", "Color for option description in help-bar", "", 0, 0,
1047 "default", "default", 0, "", "", "bar_refresh", "", "", "");
1048 $options_iset{"color_help_default_value"} = weechat::config_new_option(
1049 $iset_config_file, $section_color,
1050 "help_default_value", "color", "Color for default option value in help-bar", "", 0, 0,
1051 "green", "green", 0, "", "", "bar_refresh", "", "", "");
1052
1053 # section "help"
1054 my $section_help = weechat::config_new_section($iset_config_file,"help", 0, 0, "", "", "", "", "", "", "", "", "", "");
1055 if ($section_help eq "")
1056 {
1057 weechat::config_free($iset_config_file);
1058 return;
1059 }
1060 $options_iset{"show_help_bar"} = weechat::config_new_option(
1061 $iset_config_file, $section_help,
1062 "show_help_bar", "boolean", "Show help bar", "", 0, 0,
1063 "on", "on", 0, "", "", "toggle_help_cb", "", "", "");
1064 $options_iset{"show_help_extra_info"} = weechat::config_new_option(
1065 $iset_config_file, $section_help,
1066 "show_help_extra_info", "boolean", "Show additional information in help bar (default value, max./min. value) ", "", 0, 0,
1067 "on", "on", 0, "", "", "", "", "", "");
1068 $options_iset{"show_plugin_description"} = weechat::config_new_option(
1069 $iset_config_file, $section_help,
1070 "show_plugin_description", "boolean", "Show plugin description in iset buffer", "", 0, 0,
1071 "on", "on", 0, "", "", "full_refresh_cb", "", "", "");
1072
1073 # section "look"
1074 my $section_look = weechat::config_new_section($iset_config_file, "look", 0, 0, "", "", "", "", "", "", "", "", "", "");
1075 if ($section_look eq "")
1076 {
1077 weechat::config_free($iset_config_file);
1078 return;
1079 }
1080 $options_iset{"value_search_char"} = weechat::config_new_option(
1081 $iset_config_file, $section_look,
1082 "value_search_char", "string", "Trigger char to tell iset to search for value instead of option (for example: =red)", "", 0, 0,
1083 "=", "=", 0, "", "", "", "", "", "");
1084 $options_iset{"scroll_horiz"} = weechat::config_new_option(
1085 $iset_config_file, $section_look,
1086 "scroll_horiz", "integer", "scroll content of iset buffer n%", "", 1, 100,
1087 "10", "10", 0, "", "", "", "", "", "");
1088 $options_iset{"show_current_line"} = weechat::config_new_option(
1089 $iset_config_file, $section_look,
1090 "show_current_line", "boolean", "show current line in title bar.", "", 0, 0,
1091 "on", "on", 0, "", "", "", "", "", "");
1092}
1093
1094sub iset_config_reload_cb
1095{
1096 my ($data,$config_file) = ($_[0], $_[1]);
1097 return weechat::config_reload($config_file)
1098}
1099
1100sub iset_config_read
1101{
1102 return weechat::config_read($iset_config_file) if ($iset_config_file ne "");
1103}
1104
1105sub iset_config_write
1106{
1107 return weechat::config_write($iset_config_file) if ($iset_config_file ne "");
1108}
1109
1110sub full_refresh_cb
1111{
1112 iset_full_refresh();
1113 return weechat::WEECHAT_RC_OK;
1114}
1115
1116sub bar_refresh
1117{
1118 iset_get_help(1);
1119 weechat::bar_item_update("isetbar_help") if (weechat::config_boolean($options_iset{"show_help_bar"}) == 1);
1120 return weechat::WEECHAT_RC_OK;
1121}
1122
1123sub toggle_help_cb
1124{
1125 my $value = weechat::config_boolean($options_iset{"show_help_bar"});
1126 iset_show_bar($value);
1127 return weechat::WEECHAT_RC_OK;
1128}
1129
1130# -----------------------------------[ main ]-----------------------------------------
1131
1132weechat::register($PRGNAME, $AUTHOR, $VERSION, $LICENSE,
1133 $DESCR, "iset_end", "");
1134
1135$wee_version_number = weechat::info_get("version_number", "") || 0;
1136
1137iset_config_init();
1138iset_config_read();
1139
1140weechat::hook_command($PRGNAME, "Interactive set", "f <file> || s <section> || [=]<text>",
1141 "f file : show options for a file\n".
1142 "s section : show options for a section\n".
1143 "text : show options with 'text' in name\n".
1144 weechat::config_string($options_iset{"value_search_char"})."text : show options with 'text' in value\n\n".
1145 "Keys for iset buffer:\n".
1146 "f11,f12 : move iset content left/right\n".
1147 "up,down : move one option up/down\n".
1148 "pgup,pdwn : move one page up/down\n".
1149 "home,end : move to first/last option\n".
1150 "ctrl+'L' : refresh options and screen\n".
1151 "alt+space : toggle boolean on/off\n".
1152 "alt+'+' : increase value (for integer or color)\n".
1153 "alt+'-' : decrease value (for integer or color)\n".
1154 "alt+'i',alt+'r': reset value of option\n".
1155 "alt+'i',alt+'u': unset option\n".
1156 "alt+enter : set new value for option (edit it with command line)\n".
1157 "text,enter : set a new filter using command line (use '*' to see all options)\n".
1158 "alt+'v' : toggle help bar on/off\n".
1159 "alt+'p' : toggle option \"show_plugin_description\" on/off\n".
1160 "\n".
1161 "standard mouse actions:\n".
1162 "wheel up / wheel down : move option up/down\n".
1163 "left-mouse-button : select an option from list\n".
1164 "right-mouse-button : toggle boolean (on/off) or set a new value for option (edit it with command line)\n".
1165 "right-mouse-button + gesture left/right: increase/decrease value (for integer or color)\n".
1166 "\n".
1167 "Examples:\n".
1168 " show options for file 'weechat'\n".
1169 " /iset f weechat\n".
1170 " show options for file 'irc'\n".
1171 " /iset f irc\n".
1172 " show options for section 'look'\n".
1173 " /iset s look\n".
1174 " show all options with text 'nicklist' in name\n".
1175 " /iset nicklist\n".
1176 " show all values which contain 'red'. '" . weechat::config_string($options_iset{"value_search_char"}) . "' is a trigger char.\n".
1177 " /iset ". weechat::config_string($options_iset{"value_search_char"}) ."red\n",
1178 "", "iset_cmd_cb", "");
1179weechat::hook_signal("upgrade_ended", "iset_upgrade_ended", "");
1180weechat::hook_signal("window_scrolled", "iset_signal_window_scrolled_cb", "");
1181weechat::hook_signal("buffer_switch", "iset_signal_buffer_switch_cb","");
1182weechat::bar_item_new("isetbar_help", "iset_item_cb", "");
1183weechat::bar_new("isetbar", "on", "0", "window", "", "top", "horizontal",
1184 "vertical", "3", "3", "default", "cyan", "default", "1",
1185 "isetbar_help");
1186weechat::hook_modifier("bar_condition_isetbar", "iset_check_condition_isetbar_cb", "");
1187weechat::hook_config("*", "iset_config_cb", "");
1188$iset_buffer = weechat::buffer_search($LANG, $PRGNAME);
1189iset_init() if ($iset_buffer ne "");
1190
1191if ($wee_version_number >= 0x00030600)
1192{
1193 weechat::hook_focus("chat", "hook_focus_iset_cb", "");
1194 weechat::hook_hsignal($PRGNAME."_mouse", "iset_hsignal_mouse_cb", "");
1195 weechat::key_bind("mouse", \%mouse_keys);
1196}