diff options
author | Sébastien Helleu <flashcode@flashtux.org> | 2022-05-23 22:58:46 +0200 |
---|---|---|
committer | Sébastien Helleu <flashcode@flashtux.org> | 2022-05-27 17:53:50 +0200 |
commit | cefed2591293873d25718613fcfda460ff32eb49 (patch) | |
tree | 2738dd915235cd620023aa76271427620367bc6c /src/core | |
parent | 79438c72e190283c72f07f1350017b3857b78251 (diff) | |
download | weechat-cefed2591293873d25718613fcfda460ff32eb49.zip |
core: add command /item (closes #808)
This allows to create custom bar items with evaluated content (like the script
text_item.py does).
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/wee-command.c | 225 | ||||
-rw-r--r-- | src/core/wee-completion.c | 86 | ||||
-rw-r--r-- | src/core/wee-config.c | 102 | ||||
-rw-r--r-- | src/core/wee-config.h | 1 |
4 files changed, 414 insertions, 0 deletions
diff --git a/src/core/wee-command.c b/src/core/wee-command.c index f5cab35cc..bc72efa76 100644 --- a/src/core/wee-command.c +++ b/src/core/wee-command.c @@ -66,6 +66,7 @@ #include "wee-version.h" #include "../gui/gui-bar.h" #include "../gui/gui-bar-item.h" +#include "../gui/gui-bar-item-custom.h" #include "../gui/gui-buffer.h" #include "../gui/gui-chat.h" #include "../gui/gui-color.h" @@ -3388,6 +3389,195 @@ COMMAND_CALLBACK(input) } /* + * Callback for command "/item": manages custom bar items + */ + +COMMAND_CALLBACK(item) +{ + struct t_gui_bar_item_custom *ptr_bar_item_custom; + char str_command[4096], str_pos[16]; + int i, update; + + /* make C compiler happy */ + (void) pointer; + (void) data; + + if ((argc == 1) + || ((argc == 2) && (string_strcasecmp (argv[1], "list") == 0))) + { + /* display all custom bar items */ + if (gui_custom_bar_items) + { + gui_chat_printf (NULL, ""); + gui_chat_printf (NULL, + _("Custom bar items:")); + for (ptr_bar_item_custom = gui_custom_bar_items; ptr_bar_item_custom; + ptr_bar_item_custom = ptr_bar_item_custom->next_item) + { + gui_chat_printf ( + NULL, " %s -> \"%s\"", + ptr_bar_item_custom->bar_item->name, + CONFIG_STRING(ptr_bar_item_custom->options[GUI_BAR_ITEM_CUSTOM_OPTION_CONTENT])); + } + } + else + { + gui_chat_printf (NULL, _("No custom bar item defined")); + } + + return WEECHAT_RC_OK; + } + + /* add (or add/replace) a custom bar item */ + if ((string_strcasecmp (argv[1], "add") == 0) + || (string_strcasecmp (argv[1], "addreplace") == 0)) + { + COMMAND_MIN_ARGS(4, argv[1]); + + update = 0; + if (string_strcasecmp (argv[1], "addreplace") == 0) + { + ptr_bar_item_custom = gui_bar_item_custom_search (argv[2]); + if (ptr_bar_item_custom) + { + gui_bar_item_custom_free (ptr_bar_item_custom); + update = 1; + } + } + + ptr_bar_item_custom = gui_bar_item_custom_new (argv[2], argv_eol[3]); + if (ptr_bar_item_custom) + { + gui_chat_printf (NULL, + (update) ? + _("Custom bar item \"%s\" updated") : + _("Custom bar item \"%s\" added"), + argv[2]); + } + else + { + gui_chat_printf (NULL, + _("%sUnable to add custom bar item \"%s\""), + gui_chat_prefix[GUI_CHAT_PREFIX_ERROR], + argv[2]); + } + + return WEECHAT_RC_OK; + } + + /* refresh bar items */ + if (string_strcasecmp (argv[1], "refresh") == 0) + { + for (i = 2; i < argc; i++) + { + gui_bar_item_update (argv[i]); + } + return WEECHAT_RC_OK; + } + + /* recreate a custom bar item */ + if (string_strcasecmp (argv[1], "recreate") == 0) + { + COMMAND_MIN_ARGS(3, "recreate"); + ptr_bar_item_custom = gui_bar_item_custom_search (argv[2]); + if (ptr_bar_item_custom) + { + snprintf (str_command, sizeof (str_command), + "/item addreplace %s %s", + ptr_bar_item_custom->bar_item->name, + CONFIG_STRING(ptr_bar_item_custom->options[GUI_BAR_ITEM_CUSTOM_OPTION_CONTENT])); + gui_buffer_set (buffer, "input", str_command); + snprintf (str_pos, sizeof (str_pos), + "%d", utf8_strlen (str_command)); + gui_buffer_set (buffer, "input_pos", str_pos); + } + else + { + gui_chat_printf (NULL, + _("%sCustom bar item \"%s\" not found"), + gui_chat_prefix[GUI_CHAT_PREFIX_ERROR], + argv[2]); + } + return WEECHAT_RC_OK; + } + + /* rename a custom bar item */ + if (string_strcasecmp (argv[1], "rename") == 0) + { + COMMAND_MIN_ARGS(4, "rename"); + ptr_bar_item_custom = gui_bar_item_custom_search (argv[2]); + if (ptr_bar_item_custom) + { + if (gui_bar_item_custom_rename (ptr_bar_item_custom, argv[3])) + { + gui_chat_printf (NULL, + _("Custom bar item \"%s\" renamed to \"%s\""), + argv[2], argv[3]); + } + else + { + gui_chat_printf (NULL, + _("%sUnable to rename custom bar item " + "\"%s\" to \"%s\""), + gui_chat_prefix[GUI_CHAT_PREFIX_ERROR], + argv[2], argv[3]); + } + } + else + { + gui_chat_printf (NULL, + _("%sCustom bar item \"%s\" not found"), + gui_chat_prefix[GUI_CHAT_PREFIX_ERROR], + argv[2]); + } + return WEECHAT_RC_OK; + } + + /* delete a custom bar item */ + if (string_strcasecmp (argv[1], "del") == 0) + { + COMMAND_MIN_ARGS(3, "del"); + if (string_strcasecmp (argv[2], "-all") == 0) + { + if (gui_custom_bar_items) + { + gui_bar_item_custom_free_all (); + gui_chat_printf (NULL, + _("All custom bar items have been deleted")); + } + else + { + gui_chat_printf (NULL, _("No custom bar item defined")); + } + } + else + { + for (i = 2; i < argc; i++) + { + ptr_bar_item_custom = gui_bar_item_custom_search (argv[i]); + if (ptr_bar_item_custom) + { + gui_bar_item_custom_free (ptr_bar_item_custom); + gui_chat_printf (NULL, + _("Custom bar item \"%s\" deleted"), + argv[i]); + } + else + { + gui_chat_printf (NULL, + _("%sCustom bar item \"%s\" not found"), + gui_chat_prefix[GUI_CHAT_PREFIX_ERROR], + argv[i]); + } + } + } + return WEECHAT_RC_OK; + } + + COMMAND_ERROR; +} + +/* * Displays a key binding. */ @@ -7828,6 +8018,41 @@ command_init () "zoom_merged_buffer || insert || send || paste_start || paste_stop", &command_input, NULL, NULL); hook_command ( + NULL, "item", + N_("manage custom bar items"), + N_("list" + " || add|addreplace <name> <content>" + " || rename <name> <new_name>" + " || refresh <name> [<name>...]" + " || recreate <name>" + " || del <name>|-all"), + N_(" list: list all custom bar items\n" + " add: add a custom bar item\n" + "addreplace: add or replace an existing custom bar item\n" + " name: custom bar item name\n" + " content: content (evaluated, see /help eval)\n" + " rename: rename a custom bar item\n" + " refresh: update content of item in all bars where the item is " + "displayed; any item can be refreshed: default/plugin/custom " + "bar items\n" + " recreate: set input with the command used to edit the custom " + "bar item\n" + " del: delete a custom bar item\n" + " -all: delete all custom bar items\n" + "\n" + "Examples:\n" + " /item add terminfo term:${info:term_width}x${info:term_height}\n" + " /item add bufinfo ${buffer.number}:${buffer.name}" + "${if:${buffer.zoomed}?(Z)}"), + "list" + " || add %(custom_bar_items_names)" + " || addreplace %(custom_bar_items_names) %(custom_bar_item_content)" + " || rename %(custom_bar_items_names) %(custom_bar_items_names)" + " || refresh %(custom_bar_items_names)|%*" + " || recreate %(custom_bar_items_names)" + " || del %(custom_bar_items_names)|-all", + &command_item, NULL, NULL); + hook_command ( NULL, "key", N_("bind/unbind keys"), N_("list|listdefault|listdiff [<context>]" diff --git a/src/core/wee-completion.c b/src/core/wee-completion.c index bbf1c9398..4222e5af4 100644 --- a/src/core/wee-completion.c +++ b/src/core/wee-completion.c @@ -46,6 +46,8 @@ #include "wee-string.h" #include "../gui/gui-completion.h" #include "../gui/gui-bar.h" +#include "../gui/gui-bar-item.h" +#include "../gui/gui-bar-item-custom.h" #include "../gui/gui-bar-window.h" #include "../gui/gui-buffer.h" #include "../gui/gui-color.h" @@ -88,6 +90,84 @@ completion_list_add_bars_names_cb (const void *pointer, void *data, } /* + * Adds custom bar items names to completion list. + */ + +int +completion_list_add_custom_bar_items_names_cb (const void *pointer, void *data, + const char *completion_item, + struct t_gui_buffer *buffer, + struct t_gui_completion *completion) +{ + struct t_gui_bar_item_custom *ptr_item; + + /* make C compiler happy */ + (void) pointer; + (void) data; + (void) completion_item; + (void) buffer; + + for (ptr_item = gui_custom_bar_items; ptr_item; + ptr_item = ptr_item->next_item) + { + gui_completion_list_add (completion, ptr_item->bar_item->name, + 0, WEECHAT_LIST_POS_SORT); + } + + return WEECHAT_RC_OK; +} + +/* + * Adds value of custom bar item content to completion list. + * + * The item name is read in previous argument. + */ + +int +completion_list_add_custom_bar_item_content_cb (const void *pointer, void *data, + const char *completion_item, + struct t_gui_buffer *buffer, + struct t_gui_completion *completion) +{ + char **argv; + int argc; + struct t_gui_bar_item_custom *ptr_item; + + /* make C compiler happy */ + (void) pointer; + (void) data; + (void) completion_item; + (void) buffer; + + if (!completion->args) + return WEECHAT_RC_OK; + + argv = string_split (completion->args, " ", NULL, + WEECHAT_STRING_SPLIT_STRIP_LEFT + | WEECHAT_STRING_SPLIT_STRIP_RIGHT + | WEECHAT_STRING_SPLIT_COLLAPSE_SEPS, + 0, &argc); + if (!argv) + return WEECHAT_RC_OK; + + if (argc > 1) + { + ptr_item = gui_bar_item_custom_search (argv[1]); + if (ptr_item) + { + gui_completion_list_add ( + completion, + CONFIG_STRING(ptr_item->options[GUI_BAR_ITEM_CUSTOM_OPTION_CONTENT]), + 0, + WEECHAT_LIST_POS_SORT); + } + } + string_free_split (argv); + + return WEECHAT_RC_OK; +} + +/* * Adds bar options to completion list. */ @@ -1865,6 +1945,12 @@ completion_init () hook_completion (NULL, "bars_names", /* formerly "%r" */ N_("names of bars"), &completion_list_add_bars_names_cb, NULL, NULL); + hook_completion (NULL, "custom_bar_items_names", + N_("names of custom bar items"), + &completion_list_add_custom_bar_items_names_cb, NULL, NULL); + hook_completion (NULL, "custom_bar_item_content", + N_("content of custom bar item"), + &completion_list_add_custom_bar_item_content_cb, NULL, NULL); hook_completion (NULL, "config_option_values", /* formerly "%v" */ N_("values for a configuration option"), &completion_list_add_config_option_values_cb, NULL, NULL); diff --git a/src/core/wee-config.c b/src/core/wee-config.c index 8edfdb216..dc8d74975 100644 --- a/src/core/wee-config.c +++ b/src/core/wee-config.c @@ -50,6 +50,7 @@ #include "wee-version.h" #include "../gui/gui-bar.h" #include "../gui/gui-bar-item.h" +#include "../gui/gui-bar-item-custom.h" #include "../gui/gui-buffer.h" #include "../gui/gui-chat.h" #include "../gui/gui-color.h" @@ -70,6 +71,7 @@ struct t_config_section *weechat_config_section_debug = NULL; struct t_config_section *weechat_config_section_color = NULL; struct t_config_section *weechat_config_section_proxy = NULL; struct t_config_section *weechat_config_section_bar = NULL; +struct t_config_section *weechat_config_section_custom_bar_item = NULL; struct t_config_section *weechat_config_section_notify = NULL; /* config, startup section */ @@ -1435,6 +1437,8 @@ config_weechat_init_after_read () gui_bar_create_default (); } + gui_bar_item_custom_use_temp_items (); + /* if no key was found configuration file, then we use default bindings */ for (i = 0; i < GUI_KEY_NUM_CONTEXTS; i++) { @@ -1931,6 +1935,86 @@ config_weechat_bar_read_cb (const void *pointer, void *data, } /* + * Reads a custom bar item option in WeeChat configuration file. + */ + +int +config_weechat_bar_item_read_cb (const void *pointer, void *data, + struct t_config_file *config_file, + struct t_config_section *section, + const char *option_name, const char *value) +{ + char *pos_option, *item_name; + struct t_gui_bar_item_custom *ptr_temp_item; + int index_option; + + /* make C compiler happy */ + (void) pointer; + (void) data; + (void) config_file; + (void) section; + + if (!option_name) + return WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE; + + pos_option = strchr (option_name, '.'); + if (!pos_option) + return WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE; + + item_name = string_strndup (option_name, pos_option - option_name); + if (!item_name) + return WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE; + + pos_option++; + + /* search temporary custom bar item */ + for (ptr_temp_item = gui_temp_custom_bar_items; ptr_temp_item; + ptr_temp_item = ptr_temp_item->next_item) + { + if (strcmp (ptr_temp_item->name, item_name) == 0) + break; + } + if (!ptr_temp_item) + { + /* create new temporary custom bar item */ + ptr_temp_item = gui_bar_item_custom_alloc (item_name); + if (ptr_temp_item) + { + /* add new custom bar item at the end */ + ptr_temp_item->prev_item = last_gui_temp_custom_bar_item; + ptr_temp_item->next_item = NULL; + if (last_gui_temp_custom_bar_item) + last_gui_temp_custom_bar_item->next_item = ptr_temp_item; + else + gui_temp_custom_bar_items = ptr_temp_item; + last_gui_temp_custom_bar_item = ptr_temp_item; + } + } + + if (ptr_temp_item) + { + index_option = gui_bar_item_custom_search_option (pos_option); + if (index_option >= 0) + { + gui_bar_item_custom_create_option_temp (ptr_temp_item, index_option, + value); + } + else + { + gui_chat_printf (NULL, + _("%sWarning: unknown option for section \"%s\": " + "%s (value: \"%s\")"), + gui_chat_prefix[GUI_CHAT_PREFIX_ERROR], + section->name, option_name, value); + } + } + + free (item_name); + + return WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE; +} + +/* * Reads a layout option in WeeChat configuration file. */ @@ -4658,6 +4742,24 @@ config_weechat_init_options () weechat_config_section_bar = ptr_section; + /* custom bar items */ + ptr_section = config_file_new_section ( + weechat_config_file, "custom_bar_item", + 0, 0, + &config_weechat_bar_item_read_cb, NULL, NULL, + NULL, NULL, NULL, + NULL, NULL, NULL, + NULL, NULL, NULL, + NULL, NULL, NULL); + if (!ptr_section) + { + config_file_free (weechat_config_file); + weechat_config_file = NULL; + return 0; + } + + weechat_config_section_custom_bar_item = ptr_section; + /* layout */ ptr_section = config_file_new_section ( weechat_config_file, "layout", diff --git a/src/core/wee-config.h b/src/core/wee-config.h index 5cae8d136..0feec6c42 100644 --- a/src/core/wee-config.h +++ b/src/core/wee-config.h @@ -127,6 +127,7 @@ extern struct t_config_file *weechat_config_file; extern struct t_config_section *weechat_config_section_color; extern struct t_config_section *weechat_config_section_proxy; extern struct t_config_section *weechat_config_section_bar; +extern struct t_config_section *weechat_config_section_custom_bar_item; extern struct t_config_section *weechat_config_section_notify; extern struct t_config_option *config_startup_command_after_plugins; |