fset: add export of options

This commit is contained in:
Sébastien Helleu 2017-06-15 22:03:01 +02:00
parent 28dcb1049b
commit f519499057
6 changed files with 383 additions and 71 deletions

View File

@ -545,6 +545,23 @@ fset_buffer_display_line (int y, struct t_fset_option *fset_option)
weechat_hashtable_set (fset_buffer_hashtable_extra_vars,
"description", str_field);
/* description2 */
ptr_field = weechat_hdata_string (fset_hdata_fset_option,
fset_option, "description");
snprintf (str_field, sizeof (str_field),
"%s", (ptr_field && ptr_field[0]) ? _(ptr_field) : _("(no description)"));
weechat_hashtable_set (fset_buffer_hashtable_extra_vars,
"__description2", str_field);
snprintf (str_field, sizeof (str_field),
"%s%s",
weechat_color (weechat_config_string (fset_config_color_description[selected_line])),
(ptr_field && ptr_field[0]) ? _(ptr_field) : _("(no description)"));
weechat_hashtable_set (fset_buffer_hashtable_extra_vars,
"_description2", str_field);
fset_buffer_fills_field (str_field, sizeof (str_field), "description2", 64);
weechat_hashtable_set (fset_buffer_hashtable_extra_vars,
"description2", str_field);
/* description_en */
ptr_field = weechat_hdata_string (fset_hdata_fset_option,
fset_option, "description");
@ -557,10 +574,27 @@ fset_buffer_display_line (int y, struct t_fset_option *fset_option)
(ptr_field) ? ptr_field : "");
weechat_hashtable_set (fset_buffer_hashtable_extra_vars,
"_description_en", str_field);
fset_buffer_fills_field (str_field, sizeof (str_field), "description", 64);
fset_buffer_fills_field (str_field, sizeof (str_field), "description_en", 64);
weechat_hashtable_set (fset_buffer_hashtable_extra_vars,
"description_en", str_field);
/* description_en2 */
ptr_field = weechat_hdata_string (fset_hdata_fset_option,
fset_option, "description");
snprintf (str_field, sizeof (str_field),
"%s", (ptr_field && ptr_field[0]) ? ptr_field : "(no description)");
weechat_hashtable_set (fset_buffer_hashtable_extra_vars,
"__description_en2", str_field);
snprintf (str_field, sizeof (str_field),
"%s%s",
weechat_color (weechat_config_string (fset_config_color_description[selected_line])),
(ptr_field && ptr_field[0]) ? ptr_field : "(no description)");
weechat_hashtable_set (fset_buffer_hashtable_extra_vars,
"_description_en2", str_field);
fset_buffer_fills_field (str_field, sizeof (str_field), "description_en2", 64);
weechat_hashtable_set (fset_buffer_hashtable_extra_vars,
"description_en2", str_field);
/* string_values */
ptr_field = weechat_hdata_string (fset_hdata_fset_option,
fset_option, "string_values");
@ -862,6 +896,34 @@ fset_buffer_input_cb (const void *pointer, void *data,
return WEECHAT_RC_OK;
}
/* export options in a file */
if (strncmp (input_data, "w:", 2) == 0)
{
if (input_data[2])
{
fset_option_export (
input_data + 2,
weechat_config_boolean (fset_config_look_export_help_default));
}
return WEECHAT_RC_OK;
}
/* export options in a file (without help) */
if (strncmp (input_data, "w-:", 3) == 0)
{
if (input_data[3])
fset_option_export (input_data + 3, 0);
return WEECHAT_RC_OK;
}
/* export options in a file (with help) */
if (strncmp (input_data, "w+:", 3) == 0)
{
if (input_data[3])
fset_option_export (input_data + 3, 1);
return WEECHAT_RC_OK;
}
/* execute action on an option */
for (i = 0; actions[i][0]; i++)
{

View File

@ -81,8 +81,9 @@ fset_command_fset (const void *pointer, void *data,
struct t_gui_buffer *buffer, int argc,
char **argv, char **argv_eol)
{
int num_options, line, value, i;
int num_options, line, value, i, with_help;
char str_command[512];
const char *ptr_filename;
struct t_fset_option *ptr_fset_option;
struct t_config_option *ptr_option;
struct t_gui_window *ptr_window;
@ -91,7 +92,6 @@ fset_command_fset (const void *pointer, void *data,
(void) pointer;
(void) data;
(void) buffer;
(void) argv_eol;
if (argc == 1)
{
@ -373,6 +373,40 @@ fset_command_fset (const void *pointer, void *data,
return WEECHAT_RC_OK;
}
if (weechat_strcasecmp (argv[1], "-export") == 0)
{
if (argc < 3)
WEECHAT_COMMAND_ERROR;
with_help = weechat_config_boolean (fset_config_look_export_help_default);
ptr_filename = argv_eol[2];
if (weechat_strcasecmp (argv[2], "-help") == 0)
{
with_help = 1;
if (argc < 4)
WEECHAT_COMMAND_ERROR;
ptr_filename = argv_eol[3];
}
else if (weechat_strcasecmp (argv[2], "-nohelp") == 0)
{
with_help = 0;
if (argc < 4)
WEECHAT_COMMAND_ERROR;
ptr_filename = argv_eol[3];
}
num_options = weechat_arraylist_size (fset_options);
if (num_options == 0)
{
weechat_printf (NULL,
_("%s%s: there are no options displayed, "
"unable to export."),
weechat_prefix ("error"), FSET_PLUGIN_NAME);
return WEECHAT_RC_OK;
}
if (!fset_option_export (ptr_filename, with_help))
WEECHAT_COMMAND_ERROR;
return WEECHAT_RC_OK;
}
WEECHAT_COMMAND_ERROR;
}
else
@ -539,6 +573,7 @@ fset_command_init ()
" || -set"
" || -append"
" || -mark [<number>]"
" || -export [-help|-nohelp] <filename>"
" || filter"),
N_(" -bar: add the help bar\n"
" -refresh: refresh list of options, then whole screen "
@ -562,6 +597,12 @@ fset_command_init ()
"of option (move the cursor at the end of value)\n"
" -mark: toggle mark on the option and move \"number\" lines "
"(up/down, default is 1: one line down)\n"
" -export: export the options and values displayed in a file "
"(each line has format: \"/set name value\" or \"/unset name\")\n"
" -help: force writing of help on options in exported file "
"(see /help fset.look.export_help_default)\n"
" -nohelp: do not write help on options in exported file "
"(see /help fset.look.export_help_default)\n"
" filter: set a new filter to see only matching options (this "
"filter can be used as input in fset buffer as well); allowed "
"formats are:\n"
@ -580,10 +621,11 @@ fset_command_init ()
" ==xxx show only options with exact value \"xxx\"\n"
" c:xxx show only options matching the evaluated "
"condition \"xxx\", using following variables: file, section, "
"option, name, parent_name, type (bool/int/str/col), "
"default_value, default_value_undef, value, value_undef, "
"value_changed, parent_value, min, max, description, "
"description_en, string_values\n"
"option, name, parent_name, type, type_en, type_short "
"(bool/int/str/col), type_tiny (b/i/s/c), default_value, "
"default_value_undef, value, quoted_value, value_undef, "
"value_changed, parent_value, min, max, description, description2, "
"description_en, description_en2, string_values\n"
" s:x,y sort options by fields x,y "
"(see /help fset.look.sort)\n"
" s: reset sort to its default value "
@ -605,6 +647,8 @@ fset_command_init ()
" - ${parent_name}: parent option name\n"
" - ${type}: option type (translated)\n"
" - ${type_en}: option type (in English)\n"
" - ${type_short}: short option type (bool/int/str/col)\n"
" - ${type_tiny}: tiny option type (b/i/s/c)\n"
" - ${default_value}: option default value\n"
" - ${default_value_undef}: \"1\" if default value is null, "
"otherwise \"0\"\n"
@ -617,7 +661,11 @@ fset_command_init ()
" - ${min}: min value\n"
" - ${max}: max value\n"
" - ${description}: option description (translated)\n"
" - ${description2}: option description (translated), "
"\"(no description)\" (translated) if there's no description\n"
" - ${description_en}: option description (in English)\n"
" - ${description_en2}: option description (in English), "
"\"(no description)\" if there's no description\n"
" - ${string_values}: string values allowed for set of an "
"integer option using strings\n"
" - ${marked}: \"1\" if option is marked, otherwise \"0\"\n"
@ -629,38 +677,41 @@ fset_command_init ()
"${__name}, ${__type}, ...\n"
"\n"
"Keys and input to move in on fset buffer:\n"
" up move one line up\n"
" down move one line down\n"
" pgup move one page up\n"
" pgdn move one page down\n"
" alt-home << move to first line\n"
" alt-end >> move to last line\n"
" F11 < scroll horizontally on the left\n"
" F12 > scroll horizontally on the right\n"
" up move one line up\n"
" down move one line down\n"
" pgup move one page up\n"
" pgdn move one page down\n"
" alt-home << move to first line\n"
" alt-end >> move to last line\n"
" F11 < scroll horizontally on the left\n"
" F12 > scroll horizontally on the right\n"
"\n"
"Keys and input to set options on fset buffer:\n"
" alt+space t toggle boolean value\n"
" alt+'-' - subtract 1 from value for integer/color, "
" alt+space t toggle boolean value\n"
" alt+'-' - subtract 1 from value for integer/color, "
"set value for other types\n"
" alt+'+' + add 1 to value for integer/color, append "
" alt+'+' + add 1 to value for integer/color, append "
"to value for other types\n"
" alt+f, alt+r r reset value\n"
" alt+f, alt+u u unset value\n"
" alt+enter s set value\n"
" alt+f, alt+a a append to value\n"
" alt+',' , mark/unmark option and move one line down\n"
" shift+down mark/unmark option and move one line down\n"
" shift+up mark/unmark option and move one line up\n"
" alt+f, alt+r r reset value\n"
" alt+f, alt+u u unset value\n"
" alt+enter s set value\n"
" alt+f, alt+a a append to value\n"
" alt+',' , mark/unmark option and move one line down\n"
" shift+down mark/unmark option and move one line down\n"
" shift+up mark/unmark option and move one line up\n"
"\n"
"Other keys and input on fset buffer:\n"
" ctrl+L refresh options and whole screen "
" ctrl+L refresh options and whole screen "
"(command: /fset -refresh)\n"
" $ refresh options (keep marked options)\n"
" $$ refresh options (unmark all options)\n"
" p toggle plugin description options "
" $ refresh options (keep marked options)\n"
" $$ refresh options (unmark all options)\n"
" p toggle plugin description options "
"(plugins.desc.*)\n"
" v toggle help bar\n"
" q close fset buffer\n"
" v toggle help bar\n"
" w:xxx export options in file \"xxx\"\n"
" w-:xxx export options in file \"xxx\" without help\n"
" w+:xxx export options in file \"xxx\" with help\n"
" q close fset buffer\n"
"\n"
"Mouse actions on fset buffer:\n"
" wheel up/down move line up/down\n"
@ -681,8 +732,10 @@ fset_command_init ()
" /fset nicklist\n"
" show all values which contain \"red\":\n"
" /fset =red\n"
" show all values which are exactly \"red\":\n"
" /fset ==red\n"
" show all integer options in irc plugin:\n"
" /fset c:${file} == irc && ${type} == int"),
" /fset c:${file} == irc && ${type_en} == integer"),
"-bar"
" || -refresh"
" || -up 1|2|3|4|5"
@ -697,6 +750,7 @@ fset_command_init ()
" || -set"
" || -append"
" || -mark"
" || -export -help|-nohelp|%(filename) %(filename)"
" || *|c:|f:|s:|d|d:|d=|d==|=|==",
&fset_command_fset, NULL, NULL);
weechat_hook_command_run ("/set", &fset_command_run_set_cb, NULL, NULL);

View File

@ -36,6 +36,7 @@ struct t_config_file *fset_config_file = NULL;
struct t_config_option *fset_config_look_auto_unmark;
struct t_config_option *fset_config_look_condition_catch_set;
struct t_config_option *fset_config_look_export_help_default;
struct t_config_option *fset_config_look_marked_string;
struct t_config_option *fset_config_look_scroll_horizontal;
struct t_config_option *fset_config_look_show_help_bar;
@ -50,6 +51,9 @@ struct t_config_option *fset_config_look_use_mute;
struct t_config_option *fset_config_format_option;
struct t_config_option *fset_config_format_option_current;
struct t_config_option *fset_config_format_export_help;
struct t_config_option *fset_config_format_export_option;
struct t_config_option *fset_config_format_export_option_null;
/* fset config, color section */
@ -315,6 +319,16 @@ fset_config_init ()
NULL, NULL, NULL,
NULL, NULL, NULL,
NULL, NULL, NULL);
fset_config_look_export_help_default = weechat_config_new_option (
fset_config_file, ptr_section,
"export_help_default", "boolean",
N_("write help for each option exported by default (this can be "
"overridden with arguments \"-help\" and \"-nohelp\" for command "
"/fset -export"),
NULL, 0, 0, "on", NULL, 0,
NULL, NULL, NULL,
NULL, NULL, NULL,
NULL, NULL, NULL);
fset_config_look_marked_string = weechat_config_new_option (
fset_config_file, ptr_section,
"marked_string", "string",
@ -429,6 +443,39 @@ fset_config_init ()
NULL, NULL, NULL,
&fset_config_change_format_cb, NULL, NULL,
NULL, NULL, NULL);
fset_config_format_export_help = weechat_config_new_option (
fset_config_file, ptr_section,
"export_help", "string",
N_("format of help line written before each option exported in a file "
"(note: content is evaluated, see /help fset)"),
NULL, 0, 0,
"# ${description2}",
NULL, 0,
NULL, NULL, NULL,
NULL, NULL, NULL,
NULL, NULL, NULL);
fset_config_format_export_option = weechat_config_new_option (
fset_config_file, ptr_section,
"export_option", "string",
N_("format of each option exported in a file "
"(note: content is evaluated, see /help fset)"),
NULL, 0, 0,
"/set ${name} ${quoted_value}",
NULL, 0,
NULL, NULL, NULL,
NULL, NULL, NULL,
NULL, NULL, NULL);
fset_config_format_export_option_null = weechat_config_new_option (
fset_config_file, ptr_section,
"export_option_null", "string",
N_("format of each option with \"null\" value exported in a file "
"(note: content is evaluated, see /help fset)"),
NULL, 0, 0,
"/unset ${name}",
NULL, 0,
NULL, NULL, NULL,
NULL, NULL, NULL,
NULL, NULL, NULL);
/* color */
ptr_section = weechat_config_new_section (fset_config_file, "color",

View File

@ -26,6 +26,7 @@ extern struct t_config_file *fset_config_file;
extern struct t_config_option *fset_config_look_auto_unmark;
extern struct t_config_option *fset_config_look_condition_catch_set;
extern struct t_config_option *fset_config_look_export_help_default;
extern struct t_config_option *fset_config_look_marked_string;
extern struct t_config_option *fset_config_look_scroll_horizontal;
extern struct t_config_option *fset_config_look_show_help_bar;
@ -38,6 +39,9 @@ extern struct t_config_option *fset_config_look_use_mute;
extern struct t_config_option *fset_config_format_option;
extern struct t_config_option *fset_config_format_option_current;
extern struct t_config_option *fset_config_format_export_help;
extern struct t_config_option *fset_config_format_export_option;
extern struct t_config_option *fset_config_format_export_option_null;
extern struct t_config_option *fset_config_color_default_value[2];
extern struct t_config_option *fset_config_color_description[2];

View File

@ -22,6 +22,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include "../weechat-plugin.h"
#include "fset.h"
@ -215,6 +216,85 @@ fset_option_string_match (const char *string, const char *mask)
return (weechat_strcasestr (string, mask)) ? 1 : 0;
}
/*
* Adds the properties of an fset option in a hashtable
* (keys and values must be strings).
*/
void
fset_option_add_option_in_hashtable (struct t_hashtable *hashtable,
struct t_fset_option *fset_option)
{
int length;
char *value;
weechat_hashtable_set (hashtable, "file", fset_option->file);
weechat_hashtable_set (hashtable, "section", fset_option->section);
weechat_hashtable_set (hashtable, "option", fset_option->option);
weechat_hashtable_set (hashtable, "name", fset_option->name);
weechat_hashtable_set (hashtable, "parent_name", fset_option->parent_name);
weechat_hashtable_set (hashtable,
"type", _(fset_option_type_string[fset_option->type]));
weechat_hashtable_set (hashtable,
"type_en", fset_option_type_string[fset_option->type]);
weechat_hashtable_set (hashtable,
"type_short", fset_option_type_string_short[fset_option->type]);
weechat_hashtable_set (hashtable,
"type_tiny", fset_option_type_string_tiny[fset_option->type]);
weechat_hashtable_set (hashtable,
"default_value", fset_option->default_value);
weechat_hashtable_set (hashtable, "value", fset_option->value);
if (fset_option->value && (fset_option->type == FSET_OPTION_TYPE_STRING))
{
length = 1 + strlen (fset_option->value) + 1 + 1;
value = malloc (length);
if (value)
{
snprintf (value, length, "\"%s\"", fset_option->value);
weechat_hashtable_set (hashtable, "quoted_value", value);
free (value);
}
else
{
weechat_hashtable_set (hashtable,
"quoted_value", fset_option->value);
}
}
else
{
weechat_hashtable_set (hashtable, "quoted_value", fset_option->value);
}
weechat_hashtable_set (hashtable,
"parent_value", fset_option->parent_value);
weechat_hashtable_set (hashtable, "min", fset_option->min);
weechat_hashtable_set (hashtable, "max", fset_option->max);
weechat_hashtable_set (hashtable,
"description",
(fset_option->description && fset_option->description[0]) ?
_(fset_option->description) : "");
weechat_hashtable_set (hashtable,
"description2",
(fset_option->description && fset_option->description[0]) ?
_(fset_option->description) : _("(no description)"));
weechat_hashtable_set (hashtable,
"description_en", fset_option->description);
weechat_hashtable_set (hashtable,
"description_en2",
(fset_option->description && fset_option->description[0]) ?
fset_option->description : "(no description)");
weechat_hashtable_set (hashtable,
"string_values", fset_option->string_values);
weechat_hashtable_set (hashtable,
"default_value_undef",
(fset_option->default_value == NULL) ? "1" : "0");
weechat_hashtable_set (hashtable,
"value_undef",
(fset_option->value == NULL) ? "1" : "0");
weechat_hashtable_set (hashtable,
"value_changed",
(fset_option_value_is_changed (fset_option)) ? "1" : "0");
}
/*
* Checks if an option is matching current filter(s).
*
@ -243,44 +323,9 @@ fset_option_match_filters (struct t_fset_option *fset_option)
{
weechat_hashtable_set (fset_option_filter_hashtable_pointers,
"fset_option", fset_option);
weechat_hashtable_set (fset_option_filter_hashtable_extra_vars,
"file", fset_option->file);
weechat_hashtable_set (fset_option_filter_hashtable_extra_vars,
"section", fset_option->section);
weechat_hashtable_set (fset_option_filter_hashtable_extra_vars,
"option", fset_option->option);
weechat_hashtable_set (fset_option_filter_hashtable_extra_vars,
"name", fset_option->name);
weechat_hashtable_set (fset_option_filter_hashtable_extra_vars,
"parent_name", fset_option->parent_name);
weechat_hashtable_set (fset_option_filter_hashtable_extra_vars,
"type", fset_option_type_string_short[fset_option->type]);
weechat_hashtable_set (fset_option_filter_hashtable_extra_vars,
"default_value", fset_option->default_value);
weechat_hashtable_set (fset_option_filter_hashtable_extra_vars,
"value", fset_option->value);
weechat_hashtable_set (fset_option_filter_hashtable_extra_vars,
"parent_value", fset_option->parent_value);
weechat_hashtable_set (fset_option_filter_hashtable_extra_vars,
"min", fset_option->min);
weechat_hashtable_set (fset_option_filter_hashtable_extra_vars,
"max", fset_option->max);
weechat_hashtable_set (fset_option_filter_hashtable_extra_vars,
"description",
(fset_option->description) ? _(fset_option->description) : "");
weechat_hashtable_set (fset_option_filter_hashtable_extra_vars,
"description_en", fset_option->description);
weechat_hashtable_set (fset_option_filter_hashtable_extra_vars,
"string_values", fset_option->string_values);
weechat_hashtable_set (fset_option_filter_hashtable_extra_vars,
"default_value_undef",
(fset_option->default_value == NULL) ? "1" : "0");
weechat_hashtable_set (fset_option_filter_hashtable_extra_vars,
"value_undef",
(fset_option->value == NULL) ? "1" : "0");
weechat_hashtable_set (fset_option_filter_hashtable_extra_vars,
"value_changed",
(fset_option_value_is_changed (fset_option)) ? "1" : "0");
fset_option_add_option_in_hashtable (
fset_option_filter_hashtable_extra_vars,
fset_option);
result = weechat_string_eval_expression (
fset_option_filter + 2,
fset_option_filter_hashtable_pointers,
@ -649,9 +694,23 @@ fset_option_set_max_length_fields_option (struct t_fset_option *fset_option)
(fset_option->description && fset_option->description[0]) ?
_(fset_option->description) : ""));
/* description2 */
fset_option_set_max_length_field (
"description2",
weechat_strlen_screen (
(fset_option->description && fset_option->description[0]) ?
_(fset_option->description) : _("(no description)")));
/* description_en */
fset_option_set_max_length_field (
"description", weechat_strlen_screen (fset_option->description));
"description_en", weechat_strlen_screen (fset_option->description));
/* description_en2 */
fset_option_set_max_length_field (
"description_en2",
weechat_strlen_screen (
(fset_option->description && fset_option->description[0]) ?
fset_option->description : "(no description)"));
/* string_values */
fset_option_set_max_length_field (
@ -1144,6 +1203,91 @@ fset_option_unmark_all ()
fset_buffer_refresh (0);
}
/*
* Exports options currently displayed in fset buffer.
*
* If with_help == 1, the help is displayed above each option
* and options are separated by an empty line.
*
* Returns:
* 1: export OK
* 0: error
*/
int
fset_option_export (const char *filename, int with_help)
{
int num_options, i;
char *line;
FILE *file;
struct t_fset_option *ptr_fset_option;
struct t_hashtable *hashtable_pointers, *hashtable_extra_vars;
file = fopen (filename, "w");
if (!file)
return 0;
chmod (filename, 0600);
hashtable_pointers = weechat_hashtable_new (
8,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_POINTER,
NULL, NULL);
hashtable_extra_vars = weechat_hashtable_new (
128,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_STRING,
NULL, NULL);
num_options = weechat_arraylist_size (fset_options);
for (i = 0; i < num_options; i++)
{
ptr_fset_option = weechat_arraylist_get (fset_options, i);
if (ptr_fset_option)
{
weechat_hashtable_set (hashtable_pointers,
"fset_option", ptr_fset_option);
fset_option_add_option_in_hashtable (hashtable_extra_vars,
ptr_fset_option);
if (with_help)
{
if (i > 0)
fprintf (file, "\n");
line = weechat_string_eval_expression (
weechat_config_string (fset_config_format_export_help),
hashtable_pointers,
hashtable_extra_vars,
NULL);
if (line && line[0])
fprintf (file, "%s\n", line);
if (line)
free (line);
}
line = weechat_string_eval_expression (
(ptr_fset_option->value) ?
weechat_config_string (fset_config_format_export_option) :
weechat_config_string (fset_config_format_export_option_null),
hashtable_pointers,
hashtable_extra_vars,
NULL);
if (line && line[0])
fprintf (file, "%s\n", line);
if (line)
free (line);
}
}
fclose (file);
if (hashtable_pointers)
weechat_hashtable_free (hashtable_pointers);
if (hashtable_extra_vars)
weechat_hashtable_free (hashtable_extra_vars);
return 1;
}
/*
* Refreshes the fset buffer after the change of an option.
*/

View File

@ -88,6 +88,7 @@ extern void fset_option_toggle_mark (struct t_fset_option *fset_option,
struct t_config_option *option,
int value);
extern void fset_option_unmark_all ();
extern int fset_option_export (const char *filename, int with_help);
extern int fset_option_config_cb (const void *pointer,
void *data,
const char *option,