From da0fea8a60dfff26381f0520a510b31333770f9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= Date: Wed, 8 Mar 2017 21:57:16 +0100 Subject: buflist: add hotlist format options (for color according to hotlist priority) --- src/plugins/buflist/buflist-bar-item.c | 176 +++++++++++++++++++++++++-------- src/plugins/buflist/buflist-bar-item.h | 3 +- src/plugins/buflist/buflist-config.c | 59 ++++++++++- src/plugins/buflist/buflist-config.h | 2 + src/plugins/buflist/buflist.c | 13 ++- 5 files changed, 202 insertions(+), 51 deletions(-) (limited to 'src') diff --git a/src/plugins/buflist/buflist-bar-item.c b/src/plugins/buflist/buflist-bar-item.c index ac15a8d49..50a8b5d07 100644 --- a/src/plugins/buflist/buflist-bar-item.c +++ b/src/plugins/buflist/buflist-bar-item.c @@ -25,28 +25,40 @@ #include "../weechat-plugin.h" #include "buflist.h" +#include "buflist-bar-item.h" #include "buflist-config.h" +struct t_gui_bar_item *buflist_bar_item_buflist = NULL; +struct t_hashtable *buflist_hashtable_pointers = NULL; +struct t_hashtable *buflist_hashtable_extra_vars = NULL; +struct t_hashtable *buflist_hashtable_options = NULL; + + /* * Returns content of bar item "buffer_plugin": bar item with buffer plugin. */ char * -buflist_bar_item_buflist (const void *pointer, void *data, - struct t_gui_bar_item *item, - struct t_gui_window *window, - struct t_gui_buffer *buffer, - struct t_hashtable *extra_info) +buflist_bar_item_buflist_cb (const void *pointer, void *data, + struct t_gui_bar_item *item, + struct t_gui_window *window, + struct t_gui_buffer *buffer, + struct t_hashtable *extra_info) { - struct t_hdata *hdata_buffer; + struct t_hdata *hdata_buffer, *hdata_hotlist; struct t_gui_buffer *ptr_buffer, *ptr_next_buffer, *ptr_current_buffer; + struct t_gui_buffer *ptr_buffer_hotlist; + struct t_gui_hotlist *ptr_hotlist; char **buflist, *str_buflist; char str_format_number[32], str_format_number_empty[32]; char str_number[32], str_indent_name[4], *line; const char *ptr_format, *ptr_format_current, *ptr_name, *ptr_type; - int length_max_number, current_buffer, number, prev_number; - struct t_hashtable *pointers, *extra_vars; + const char *ptr_hotlist_format, *ptr_hotlist_priority; + const char *hotlist_priority_none = "none"; + const char *hotlist_priority[4] = { "low", "message", "private", + "highlight" }; + int length_max_number, current_buffer, number, prev_number, priority, rc; /* make C compiler happy */ (void) pointer; @@ -58,24 +70,6 @@ buflist_bar_item_buflist (const void *pointer, void *data, prev_number = -1; - pointers = weechat_hashtable_new (8, - WEECHAT_HASHTABLE_STRING, - WEECHAT_HASHTABLE_POINTER, - NULL, - NULL); - if (!pointers) - return NULL; - extra_vars = weechat_hashtable_new (32, - WEECHAT_HASHTABLE_STRING, - WEECHAT_HASHTABLE_POINTER, - NULL, - NULL); - if (!extra_vars) - { - weechat_hashtable_free (pointers); - return NULL; - } - buflist = weechat_string_dyn_alloc (1); ptr_format = weechat_config_string (buflist_config_format_buffer); @@ -86,6 +80,8 @@ buflist_bar_item_buflist (const void *pointer, void *data, hdata_buffer = weechat_hdata_get ("buffer"); ptr_buffer = weechat_hdata_get_list (hdata_buffer, "last_gui_buffer"); + hdata_hotlist = weechat_hdata_get ("hotlist"); + length_max_number = snprintf ( str_number, sizeof (str_number), "%d", weechat_hdata_integer (hdata_buffer, ptr_buffer, "number")); @@ -102,6 +98,19 @@ buflist_bar_item_buflist (const void *pointer, void *data, current_buffer = (ptr_buffer == ptr_current_buffer); + /* search hotlist for this buffer (level and counts) */ + ptr_hotlist = weechat_hdata_get_list (hdata_hotlist, "gui_hotlist"); + while (ptr_hotlist) + { + ptr_buffer_hotlist = weechat_hdata_pointer (hdata_hotlist, + ptr_hotlist, + "buffer"); + if (ptr_buffer_hotlist == ptr_buffer) + break; + + ptr_hotlist = weechat_hdata_move (hdata_hotlist, ptr_hotlist, 1); + } + ptr_name = weechat_hdata_string (hdata_buffer, ptr_buffer, "short_name"); if (!ptr_name) @@ -138,31 +147,51 @@ buflist_bar_item_buflist (const void *pointer, void *data, } /* set pointers */ - weechat_hashtable_set (pointers, "buffer", ptr_buffer); + weechat_hashtable_set (buflist_hashtable_pointers, + "buffer", ptr_buffer); /* set extra variables */ - weechat_hashtable_set (extra_vars, "number", str_number); - weechat_hashtable_set (extra_vars, "indent", str_indent_name); - weechat_hashtable_set (extra_vars, "name", ptr_name); + weechat_hashtable_set (buflist_hashtable_extra_vars, + "number", str_number); + weechat_hashtable_set (buflist_hashtable_extra_vars, + "indent", str_indent_name); + weechat_hashtable_set (buflist_hashtable_extra_vars, + "name", ptr_name); + + ptr_hotlist_format = weechat_config_string (buflist_config_format_hotlist_none); + ptr_hotlist_priority = hotlist_priority_none; + if (ptr_hotlist) + { + priority = weechat_hdata_integer (hdata_hotlist, ptr_hotlist, + "priority"); + if ((priority >= 0) && (priority < 4)) + { + ptr_hotlist_format = weechat_config_string ( + buflist_config_format_hotlist[priority]); + ptr_hotlist_priority = hotlist_priority[priority]; + } + } + weechat_hashtable_set (buflist_hashtable_extra_vars, + "color_hotlist", ptr_hotlist_format); + weechat_hashtable_set (buflist_hashtable_extra_vars, + "hotlist_priority", ptr_hotlist_priority); /* build string */ line = weechat_string_eval_expression ( (current_buffer) ? ptr_format_current : ptr_format, - pointers, extra_vars, NULL); - if (!weechat_string_dyn_concat (buflist, line)) - { - free (line); - weechat_hashtable_free (extra_vars); - return NULL; - } + buflist_hashtable_pointers, + buflist_hashtable_extra_vars, + buflist_hashtable_options); + + /* concatenate string */ + rc = weechat_string_dyn_concat (buflist, line); free (line); + if (!rc) + return NULL; ptr_buffer = ptr_next_buffer; } - weechat_hashtable_free (pointers); - weechat_hashtable_free (extra_vars); - str_buflist = *buflist; weechat_string_dyn_free (buflist, 0); @@ -171,11 +200,72 @@ buflist_bar_item_buflist (const void *pointer, void *data, /* * Initializes buflist bar items. + * + * Returns: + * 1: OK + * 0: error */ -void +int buflist_bar_item_init () { - weechat_bar_item_new ("buflist", - &buflist_bar_item_buflist, NULL, NULL); + /* create hashtable used by the bar item callback */ + buflist_hashtable_pointers = weechat_hashtable_new ( + 8, + WEECHAT_HASHTABLE_STRING, + WEECHAT_HASHTABLE_POINTER, + NULL, + NULL); + if (!buflist_hashtable_pointers) + return 0; + buflist_hashtable_extra_vars = weechat_hashtable_new ( + 32, + WEECHAT_HASHTABLE_STRING, + WEECHAT_HASHTABLE_POINTER, + NULL, + NULL); + if (!buflist_hashtable_extra_vars) + { + weechat_hashtable_free (buflist_hashtable_pointers); + return 0; + } + buflist_hashtable_options = weechat_hashtable_new ( + 8, + WEECHAT_HASHTABLE_STRING, + WEECHAT_HASHTABLE_STRING, + NULL, + NULL); + if (!buflist_hashtable_options) + { + weechat_hashtable_free (buflist_hashtable_pointers); + weechat_hashtable_free (buflist_hashtable_extra_vars); + return 0; + } + weechat_hashtable_set (buflist_hashtable_options, "extra", "eval"); + + /* bar items */ + buflist_bar_item_buflist = weechat_bar_item_new ( + BUFLIST_BAR_ITEM_NAME, + &buflist_bar_item_buflist_cb, NULL, NULL); + + return 1; +} + +/* + * Ends buflist bar items. + */ + +void +buflist_bar_item_end () +{ + weechat_bar_item_remove (buflist_bar_item_buflist); + + weechat_hashtable_free (buflist_hashtable_pointers); + buflist_hashtable_pointers = NULL; + + weechat_hashtable_free (buflist_hashtable_extra_vars); + buflist_hashtable_extra_vars = NULL; + + weechat_hashtable_free (buflist_hashtable_options); + buflist_hashtable_options = NULL; } diff --git a/src/plugins/buflist/buflist-bar-item.h b/src/plugins/buflist/buflist-bar-item.h index 2eaaa2fca..caf861f62 100644 --- a/src/plugins/buflist/buflist-bar-item.h +++ b/src/plugins/buflist/buflist-bar-item.h @@ -22,6 +22,7 @@ #define BUFLIST_BAR_ITEM_NAME "buflist" -extern void buflist_bar_item_init (); +extern int buflist_bar_item_init (); +extern void buflist_bar_item_end (); #endif /* WEECHAT_BUFLIST_BAR_ITEM_H */ diff --git a/src/plugins/buflist/buflist-config.c b/src/plugins/buflist/buflist-config.c index daceed1cf..b02d5e48d 100644 --- a/src/plugins/buflist/buflist-config.c +++ b/src/plugins/buflist/buflist-config.c @@ -24,6 +24,7 @@ #include "../weechat-plugin.h" #include "buflist.h" #include "buflist-config.h" +#include "buflist-bar-item.h" struct t_config_file *buflist_config_file = NULL; @@ -32,6 +33,8 @@ struct t_config_file *buflist_config_file = NULL; struct t_config_option *buflist_config_format_buffer; struct t_config_option *buflist_config_format_buffer_current; +struct t_config_option *buflist_config_format_hotlist[4]; +struct t_config_option *buflist_config_format_hotlist_none; /* @@ -47,7 +50,7 @@ buflist_config_change_buflist (const void *pointer, void *data, (void) data; (void) option; - weechat_bar_item_update ("buflist"); + weechat_bar_item_update (BUFLIST_BAR_ITEM_NAME); } /* @@ -87,7 +90,7 @@ buflist_config_init () "buffer", "string", N_("format of each line with a buffer"), NULL, 0, 0, - "${color:green}${number}.${indent}${color:default}${name}", + "${color:green}${number}.${indent}${color_hotlist}${name}", NULL, 0, NULL, NULL, NULL, &buflist_config_change_buflist, NULL, NULL, @@ -97,7 +100,57 @@ buflist_config_init () "buffer_current", "string", N_("format for the line with current buffer"), NULL, 0, 0, - "${color:lightgreen,blue}${number}.${indent}${color:*white}${name}", + "${color:lightgreen,blue}${number}.${indent}${color_hotlist}${name}", + NULL, 0, + NULL, NULL, NULL, + &buflist_config_change_buflist, NULL, NULL, + NULL, NULL, NULL); + buflist_config_format_hotlist[0] = weechat_config_new_option ( + buflist_config_file, ptr_section, + "hotlist_low", "string", + N_("format for a buffer with hotlist level \"low\""), + NULL, 0, 0, + "${color:white}", + NULL, 0, + NULL, NULL, NULL, + &buflist_config_change_buflist, NULL, NULL, + NULL, NULL, NULL); + buflist_config_format_hotlist[1] = weechat_config_new_option ( + buflist_config_file, ptr_section, + "hotlist_message", "string", + N_("format for a buffer with hotlist level \"message\""), + NULL, 0, 0, + "${color:brown}", + NULL, 0, + NULL, NULL, NULL, + &buflist_config_change_buflist, NULL, NULL, + NULL, NULL, NULL); + buflist_config_format_hotlist[2] = weechat_config_new_option ( + buflist_config_file, ptr_section, + "hotlist_private", "string", + N_("format for a buffer with hotlist level \"private\""), + NULL, 0, 0, + "${color:green}", + NULL, 0, + NULL, NULL, NULL, + &buflist_config_change_buflist, NULL, NULL, + NULL, NULL, NULL); + buflist_config_format_hotlist[3] = weechat_config_new_option ( + buflist_config_file, ptr_section, + "hotlist_highlight", "string", + N_("format for a buffer with hotlist level \"highlight\""), + NULL, 0, 0, + "${color:magenta}", + NULL, 0, + NULL, NULL, NULL, + &buflist_config_change_buflist, NULL, NULL, + NULL, NULL, NULL); + buflist_config_format_hotlist_none = weechat_config_new_option ( + buflist_config_file, ptr_section, + "hotlist_none", "string", + N_("format for a buffer not in hotlist"), + NULL, 0, 0, + "${color:default}", NULL, 0, NULL, NULL, NULL, &buflist_config_change_buflist, NULL, NULL, diff --git a/src/plugins/buflist/buflist-config.h b/src/plugins/buflist/buflist-config.h index d69737196..69964e544 100644 --- a/src/plugins/buflist/buflist-config.h +++ b/src/plugins/buflist/buflist-config.h @@ -26,6 +26,8 @@ extern struct t_config_file *buflist_config_file; extern struct t_config_option *buflist_config_format_buffer; extern struct t_config_option *buflist_config_format_buffer_current; +extern struct t_config_option *buflist_config_format_hotlist[4]; +extern struct t_config_option *buflist_config_format_hotlist_none; extern int buflist_config_init (); extern int buflist_config_read (); diff --git a/src/plugins/buflist/buflist.c b/src/plugins/buflist/buflist.c index 3e197071b..0d93a5997 100644 --- a/src/plugins/buflist/buflist.c +++ b/src/plugins/buflist/buflist.c @@ -53,7 +53,7 @@ buflist_signal_buffer_cb (const void *pointer, void *data, (void) type_data; (void) signal_data; - weechat_bar_item_update ("buflist"); + weechat_bar_item_update (BUFLIST_BAR_ITEM_NAME); return WEECHAT_RC_OK; } @@ -69,7 +69,7 @@ weechat_plugin_init (struct t_weechat_plugin *plugin, int argc, char *argv[]) { "buffer_opened", "buffer_closed", "buffer_merged", "buffer_unmerged", "buffer_moved", "buffer_renamed", "buffer_switch", "buffer_hidden", "buffer_unhidden", "buffer_localvar_added", "buffer_localvar_changed", - "window_switch", NULL + "window_switch", "hotlist_changed", NULL }; int i; @@ -84,6 +84,9 @@ weechat_plugin_init (struct t_weechat_plugin *plugin, int argc, char *argv[]) buflist_config_read (); + if (!buflist_bar_item_init ()) + return WEECHAT_RC_ERROR; + /* hook some signals */ for (i = 0; signals_buffers[i]; i++) { @@ -91,13 +94,13 @@ weechat_plugin_init (struct t_weechat_plugin *plugin, int argc, char *argv[]) &buflist_signal_buffer_cb, NULL, NULL); } - buflist_bar_item_init (); - weechat_bar_new (BUFLIST_BAR_NAME, "off", "0", "root", "", "left", "columns_vertical", "vertical", "0", "0", "default", "default", "default", "on", BUFLIST_BAR_ITEM_NAME); + weechat_bar_item_update (BUFLIST_BAR_ITEM_NAME); + return WEECHAT_RC_OK; } @@ -111,6 +114,8 @@ weechat_plugin_end (struct t_weechat_plugin *plugin) /* make C compiler happy */ (void) plugin; + buflist_bar_item_end (); + buflist_config_write (); buflist_config_free (); -- cgit v1.2.3