diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/gui/gui-nick.c | 145 | ||||
-rw-r--r-- | src/gui/gui-nick.h | 6 | ||||
-rw-r--r-- | src/plugins/plugin-api-info.c | 42 | ||||
-rw-r--r-- | src/plugins/weechat-plugin.h | 2 |
4 files changed, 116 insertions, 79 deletions
diff --git a/src/gui/gui-nick.c b/src/gui/gui-nick.c index db1f825c8..bcf26b119 100644 --- a/src/gui/gui-nick.c +++ b/src/gui/gui-nick.c @@ -242,97 +242,106 @@ gui_nick_strdup_for_color (const char *nickname) } /* - * Finds a color code for a nick (according to nick letters). + * Finds a color name for a nick (according to nick letters). * - * Returns a WeeChat color code (that can be used for display). + * If colors is NULL (most common case), the color returned is either a forced + * color (from option "weechat.look.nick_color_force") or a color from option + * "weechat.color.chat_nick_colors". + * + * If colors is set and not empty, a color from this list is returned + * (format of argument: comma-separated list of colors, a background is + * allowed with format "fg:bg", for example: "blue,yellow:red" for blue and + * yellow on red). + * + * Returns the name of a color (for example: "green"). + * + * Note: result must be freed after use. */ -const char * -gui_nick_find_color (const char *nickname) +char * +gui_nick_find_color_name (const char *nickname, const char *colors) { - int color; - char *nickname2; - const char *forced_color, *str_color; + int color, num_colors; + char *nickname2, **list_colors, *result; + const char *forced_color, *ptr_result; + static char *default_color = "default"; - if (!nickname || !nickname[0]) - return gui_color_get_custom ("default"); + list_colors = NULL; + num_colors = 0; + nickname2 = NULL; + ptr_result = NULL; - if (!config_nick_colors) - config_set_nick_colors (); + if (!nickname || !nickname[0]) + goto end; - if (config_num_nick_colors == 0) - return gui_color_get_custom ("default"); + if (colors && colors[0]) + { + list_colors = string_split (colors, ",", NULL, 0, 0, &num_colors); + if (!list_colors || (num_colors == 0)) + goto end; + } nickname2 = gui_nick_strdup_for_color (nickname); - /* look if color is forced */ - forced_color = gui_nick_get_forced_color ( - (nickname2) ? nickname2 : nickname); - if (forced_color) + if (!list_colors) { - forced_color = gui_color_get_custom (forced_color); - if (forced_color && forced_color[0]) + /* look if color is forced for the nick */ + forced_color = gui_nick_get_forced_color ( + (nickname2) ? nickname2 : nickname); + if (forced_color) { - if (nickname2) - free (nickname2); - return forced_color; + ptr_result = forced_color; + goto end; } + /* ensure nick colors are properly set */ + if (!config_nick_colors) + config_set_nick_colors (); + if (config_num_nick_colors == 0) + goto end; } /* hash nickname to get color */ - color = gui_nick_hash_color ((nickname2) ? nickname2 : nickname, - config_num_nick_colors); - + color = gui_nick_hash_color ( + (nickname2) ? nickname2 : nickname, + (list_colors) ? num_colors : config_num_nick_colors); + ptr_result = (list_colors) ? + list_colors[color] : config_nick_colors[color]; + +end: + result = strdup ((ptr_result) ? ptr_result : default_color); + if (list_colors) + string_free_split (list_colors); if (nickname2) free (nickname2); - - /* return color */ - str_color = gui_color_get_custom (config_nick_colors[color]); - return (str_color[0]) ? str_color : gui_color_get_custom ("default"); + return result; } /* - * Finds a color name for a nick (according to nick letters). + * Finds a color code for a nick (according to nick letters). * - * Returns the name of a color (for example: "green"). + * If colors is NULL (most common case), the color returned is either a forced + * color (from option "weechat.look.nick_color_force") or a color from option + * "weechat.color.chat_nick_colors". + * + * If colors is set and not empty, a color from this list is returned + * (format of argument: comma-separated list of colors, a background is + * allowed with format "fg:bg", for example: "blue,yellow:red" for blue and + * yellow on red). + * + * Returns a WeeChat color code (that can be used for display). + * + * Note: result must be freed after use. */ -const char * -gui_nick_find_color_name (const char *nickname) +char * +gui_nick_find_color (const char *nickname, const char *colors) { - int color; - char *nickname2; - const char *forced_color; - static char *default_color = "default"; - - if (!nickname || !nickname[0]) - return default_color; - - if (!config_nick_colors) - config_set_nick_colors (); - - if (config_num_nick_colors == 0) - return default_color; - - nickname2 = gui_nick_strdup_for_color (nickname); - - /* look if color is forced */ - forced_color = gui_nick_get_forced_color ( - (nickname2) ? nickname2 : nickname); - if (forced_color) - { - if (nickname2) - free (nickname2); - return forced_color; - } - - /* hash nickname to get color */ - color = gui_nick_hash_color ((nickname2) ? nickname2 : nickname, - config_num_nick_colors); - - if (nickname2) - free (nickname2); - - /* return color name */ - return config_nick_colors[color]; + char *color; + const char *ptr_result; + + color = gui_nick_find_color_name (nickname, colors); + ptr_result = gui_color_get_custom (color); + if (color) + free (color); + return (ptr_result) ? strdup (ptr_result) : NULL; } diff --git a/src/gui/gui-nick.h b/src/gui/gui-nick.h index 560c03549..ff8bb6a29 100644 --- a/src/gui/gui-nick.h +++ b/src/gui/gui-nick.h @@ -20,7 +20,9 @@ #ifndef WEECHAT_GUI_NICK_H #define WEECHAT_GUI_NICK_H -extern const char *gui_nick_find_color (const char *nickname); -extern const char *gui_nick_find_color_name (const char *nickname); +extern char *gui_nick_find_color_name (const char *nickname, + const char *colors); +extern char *gui_nick_find_color (const char *nickname, + const char *colors); #endif /* WEECHAT_GUI_NICK_H */ diff --git a/src/plugins/plugin-api-info.c b/src/plugins/plugin-api-info.c index 429ceafd7..b37fcc44e 100644 --- a/src/plugins/plugin-api-info.c +++ b/src/plugins/plugin-api-info.c @@ -665,15 +665,24 @@ plugin_api_info_nick_color_cb (const void *pointer, void *data, const char *info_name, const char *arguments) { - const char *ptr_color; + char **items, *result; + int num_items; /* make C compiler happy */ (void) pointer; (void) data; (void) info_name; - ptr_color = gui_nick_find_color (arguments); - return (ptr_color) ? strdup (ptr_color) : NULL; + items = string_split (arguments, ";", NULL, 0, 2, &num_items); + + result = gui_nick_find_color ( + (num_items >= 1) ? items[0] : NULL, + (num_items >= 2) ? items[1] : NULL); + + if (items) + string_free_split (items); + + return result; } /* @@ -685,15 +694,24 @@ plugin_api_info_nick_color_name_cb (const void *pointer, void *data, const char *info_name, const char *arguments) { - const char *ptr_color; + char **items, *result; + int num_items; /* make C compiler happy */ (void) pointer; (void) data; (void) info_name; - ptr_color = gui_nick_find_color_name (arguments); - return (ptr_color) ? strdup (ptr_color) : NULL; + items = string_split (arguments, ";", NULL, 0, 2, &num_items); + + result = gui_nick_find_color_name ( + (num_items >= 1) ? items[0] : NULL, + (num_items >= 2) ? items[1] : NULL); + + if (items) + string_free_split (items); + + return result; } /* @@ -1835,11 +1853,19 @@ plugin_api_info_init () &plugin_api_info_color_rgb2term_cb, NULL, NULL); hook_info (NULL, "nick_color", N_("get nick color code"), - N_("nickname"), + N_("nickname;colors (colors is an optional comma-separated " + "list of colors to use; background is allowed for a color " + "with format text:background; if colors is present, WeeChat " + "options with nick colors and forced nick colors are " + "ignored)"), &plugin_api_info_nick_color_cb, NULL, NULL); hook_info (NULL, "nick_color_name", N_("get nick color name"), - N_("nickname"), + N_("nickname;colors (colors is an optional comma-separated " + "list of colors to use; background is allowed for a color " + "with format text:background; if colors is present, WeeChat " + "options with nick colors and forced nick colors are " + "ignored)"), &plugin_api_info_nick_color_name_cb, NULL, NULL); hook_info (NULL, "uptime", N_("WeeChat uptime (format: \"days:hh:mm:ss\")"), diff --git a/src/plugins/weechat-plugin.h b/src/plugins/weechat-plugin.h index e2ba4cec2..8a6f7c3b7 100644 --- a/src/plugins/weechat-plugin.h +++ b/src/plugins/weechat-plugin.h @@ -67,7 +67,7 @@ struct timeval; * please change the date with current one; for a second change at same * date, increment the 01, otherwise please keep 01. */ -#define WEECHAT_PLUGIN_API_VERSION "20200823-01" +#define WEECHAT_PLUGIN_API_VERSION "20201004-01" /* macros for defining plugin infos */ #define WEECHAT_PLUGIN_NAME(__name) \ |