diff options
author | Sébastien Helleu <flashcode@flashtux.org> | 2017-06-08 06:56:42 +0200 |
---|---|---|
committer | Sébastien Helleu <flashcode@flashtux.org> | 2017-06-08 06:56:42 +0200 |
commit | f140a9198ef677ca34ff69741225a99c312fd6a3 (patch) | |
tree | f9d4e66a83acad2e2dc6c891e0ff64dddf28916b /src | |
parent | e2589aaaca104d9ef66d1405401d814bb63fb4b0 (diff) | |
download | weechat-f140a9198ef677ca34ff69741225a99c312fd6a3.zip |
buflist: add option buflist.look.auto_scroll (issue #332)
Diffstat (limited to 'src')
-rw-r--r-- | src/plugins/buflist/buflist-bar-item.c | 170 | ||||
-rw-r--r-- | src/plugins/buflist/buflist-config.c | 15 | ||||
-rw-r--r-- | src/plugins/buflist/buflist-config.h | 1 | ||||
-rw-r--r-- | src/plugins/buflist/buflist.c | 6 | ||||
-rw-r--r-- | src/plugins/buflist/buflist.h | 3 | ||||
-rw-r--r-- | src/plugins/weechat-plugin.h | 1 |
6 files changed, 195 insertions, 1 deletions
diff --git a/src/plugins/buflist/buflist-bar-item.c b/src/plugins/buflist/buflist-bar-item.c index 232d1fdf0..2612259be 100644 --- a/src/plugins/buflist/buflist-bar-item.c +++ b/src/plugins/buflist/buflist-bar-item.c @@ -36,6 +36,8 @@ struct t_hashtable *buflist_hashtable_options = NULL; struct t_hashtable *buflist_hashtable_options_conditions = NULL; struct t_arraylist *buflist_list_buffers = NULL; +int old_line_number_current_buffer = -1; + /* * Updates buflist bar item if buflist is enabled. @@ -49,6 +51,159 @@ buflist_bar_item_update () } /* + * Checks if the bar can be scrolled, the bar must have: + * - a position "left" or "right" + * - a filling "vertical" + * - "buflist" as first item. + * + * Returns: + * 1: bar can be scrolled + * 0: bar must not be scrolled + */ + +int +buflist_bar_item_bar_can_scroll (struct t_gui_bar *bar) +{ + const char *ptr_bar_name, *ptr_bar_position, *ptr_bar_filling; + int items_count, *items_subcount; + char ***items_name, str_option[1024]; + + ptr_bar_name = weechat_hdata_string (buflist_hdata_bar, bar, "name"); + if (!ptr_bar_name) + return 0; + + /* check that bar option "position" is "left" or "right" */ + snprintf (str_option, sizeof (str_option), + "weechat.bar.%s.position", + ptr_bar_name); + ptr_bar_position = weechat_config_string (weechat_config_get (str_option)); + if (!ptr_bar_position + || ((strcmp (ptr_bar_position, "left") != 0) + && (strcmp (ptr_bar_position, "right") != 0))) + { + return 0; + } + + /* check that bar option "filling_left_right" is "vertical" */ + snprintf (str_option, sizeof (str_option), + "weechat.bar.%s.filling_left_right", + ptr_bar_name); + ptr_bar_filling = weechat_config_string (weechat_config_get (str_option)); + if (!ptr_bar_filling || (strcmp (ptr_bar_filling, "vertical") != 0)) + { + return 0; + } + + /* check that "buflist" is the first item in bar */ + items_count = weechat_hdata_integer (buflist_hdata_bar, bar, + "items_count"); + if (items_count <= 0) + return 0; + items_subcount = weechat_hdata_pointer (buflist_hdata_bar, bar, + "items_subcount"); + if (!items_subcount || (items_subcount[0] <= 0)) + return 0; + items_name = weechat_hdata_pointer (buflist_hdata_bar, bar, "items_name"); + if (!items_name || (strcmp (items_name[0][0], BUFLIST_BAR_ITEM_NAME) != 0)) + return 0; + + /* OK, bar can be scrolled! */ + return 1; +} + +/* + * Auto-scrolls a bar window displaying buflist item. + */ + +void +buflist_bar_item_auto_scroll_bar_window (struct t_gui_bar_window *bar_window, + int line_number) +{ + int height, scroll_y, new_scroll_y, auto_scroll; + char str_scroll[64]; + struct t_hashtable *hashtable; + + if (!bar_window || (line_number < 0)) + return; + + height = weechat_hdata_integer (buflist_hdata_bar_window, bar_window, + "height"); + scroll_y = weechat_hdata_integer (buflist_hdata_bar_window, bar_window, + "scroll_y"); + + /* no scroll needed if the line_number is already displayed */ + if ((line_number >= scroll_y) && (line_number < scroll_y + height)) + return; + + hashtable = weechat_hashtable_new (8, + WEECHAT_HASHTABLE_STRING, + WEECHAT_HASHTABLE_STRING, + NULL, NULL); + if (hashtable) + { + auto_scroll = weechat_config_integer (buflist_config_look_auto_scroll); + new_scroll_y = line_number - (((height - 1) * auto_scroll) / 100); + if (new_scroll_y < 0) + new_scroll_y = 0; + snprintf (str_scroll, sizeof (str_scroll), + "%d", new_scroll_y); + weechat_hashtable_set (hashtable, "scroll_y", str_scroll); + weechat_hdata_update (buflist_hdata_bar_window, bar_window, hashtable); + weechat_hashtable_free (hashtable); + } +} + +/* + * Auto-scrolls all bars with buflist item as first item. + */ + +void +buflist_bar_item_auto_scroll (int line_number) +{ + struct t_gui_bar *ptr_bar; + struct t_gui_bar_window *ptr_bar_window; + struct t_gui_window *ptr_window; + + if (line_number < 0) + return; + + /* auto-scroll in root bars */ + ptr_bar = weechat_hdata_get_list (buflist_hdata_bar, "gui_bars"); + while (ptr_bar) + { + ptr_bar_window = weechat_hdata_pointer (buflist_hdata_bar, ptr_bar, + "bar_window"); + if (ptr_bar_window && buflist_bar_item_bar_can_scroll (ptr_bar)) + { + buflist_bar_item_auto_scroll_bar_window (ptr_bar_window, + line_number); + } + ptr_bar = weechat_hdata_move (buflist_hdata_bar, ptr_bar, 1); + } + + /* auto-scroll in window bars */ + ptr_window = weechat_hdata_get_list (buflist_hdata_window, "gui_windows"); + while (ptr_window) + { + ptr_bar_window = weechat_hdata_pointer (buflist_hdata_window, + ptr_window, "bar_windows"); + while (ptr_bar_window) + { + ptr_bar = weechat_hdata_pointer (buflist_hdata_bar_window, + ptr_bar_window, "bar"); + if (buflist_bar_item_bar_can_scroll (ptr_bar)) + { + buflist_bar_item_auto_scroll_bar_window (ptr_bar_window, + line_number); + } + ptr_bar_window = weechat_hdata_move (buflist_hdata_bar_window, + ptr_bar_window, 1); + } + ptr_window = weechat_hdata_move (buflist_hdata_window, ptr_window, 1); + } +} + +/* * Returns content of bar item "buffer_plugin": bar item with buffer plugin. */ @@ -79,7 +234,7 @@ buflist_bar_item_buflist_cb (const void *pointer, void *data, const char *ptr_lag; int num_buffers, is_channel, is_private; int i, j, length_max_number, current_buffer, number, prev_number, priority; - int rc, count; + int rc, count, line_number, line_number_current_buffer; /* make C compiler happy */ (void) pointer; @@ -93,6 +248,8 @@ buflist_bar_item_buflist_cb (const void *pointer, void *data, return NULL; prev_number = -1; + line_number = 0; + line_number_current_buffer = 0; buflist = weechat_string_dyn_alloc (256); @@ -171,6 +328,8 @@ buflist_bar_item_buflist_cb (const void *pointer, void *data, weechat_hashtable_set (buflist_hashtable_extra_vars, "current_buffer", (current_buffer) ? "1" : "0"); + if (current_buffer) + line_number_current_buffer = line_number; /* buffer number */ number = weechat_hdata_integer (buflist_hdata_buffer, @@ -349,6 +508,8 @@ buflist_bar_item_buflist_cb (const void *pointer, void *data, free (line); if (!rc) goto error; + + line_number++; } str_buflist = *buflist; @@ -362,6 +523,13 @@ end: weechat_string_dyn_free (buflist, 0); weechat_arraylist_free (buffers); + if ((line_number_current_buffer != old_line_number_current_buffer) + && (weechat_config_integer (buflist_config_look_auto_scroll) >= 0)) + { + buflist_bar_item_auto_scroll (line_number_current_buffer); + } + old_line_number_current_buffer = line_number_current_buffer; + return str_buflist; } diff --git a/src/plugins/buflist/buflist-config.c b/src/plugins/buflist/buflist-config.c index b24044e87..0c3df353d 100644 --- a/src/plugins/buflist/buflist-config.c +++ b/src/plugins/buflist/buflist-config.c @@ -32,6 +32,7 @@ struct t_config_file *buflist_config_file = NULL; /* buflist config, look section */ +struct t_config_option *buflist_config_look_auto_scroll; struct t_config_option *buflist_config_look_display_conditions; struct t_config_option *buflist_config_look_enabled; struct t_config_option *buflist_config_look_mouse_jump_visited_buffer; @@ -336,6 +337,20 @@ buflist_config_init () return 0; } + buflist_config_look_auto_scroll = weechat_config_new_option ( + buflist_config_file, ptr_section, + "auto_scroll", "integer", + N_("automatically scroll the buflist bar to always see the current " + "buffer (this works only with a bar on the left/right position " + "with a \"vertical\" filling); this value is the percent number " + "of lines displayed before the current buffer when scrolling " + "(-1 = disable scroll); for example 50 means that after a scroll, " + "the current buffer is at the middle of bar, 0 means on top of " + "bar, 100 means at bottom of bar"), + NULL, -1, 100, "50", NULL, 0, + NULL, NULL, NULL, + NULL, NULL, NULL, + NULL, NULL, NULL); buflist_config_look_display_conditions = weechat_config_new_option ( buflist_config_file, ptr_section, "display_conditions", "string", diff --git a/src/plugins/buflist/buflist-config.h b/src/plugins/buflist/buflist-config.h index e62cb36c9..263dff9f2 100644 --- a/src/plugins/buflist/buflist-config.h +++ b/src/plugins/buflist/buflist-config.h @@ -32,6 +32,7 @@ extern struct t_config_file *buflist_config_file; +extern struct t_config_option *buflist_config_look_auto_scroll; extern struct t_config_option *buflist_config_look_display_conditions; extern struct t_config_option *buflist_config_look_enabled; extern struct t_config_option *buflist_config_look_mouse_jump_visited_buffer; diff --git a/src/plugins/buflist/buflist.c b/src/plugins/buflist/buflist.c index 5e87e205c..97d33387c 100644 --- a/src/plugins/buflist/buflist.c +++ b/src/plugins/buflist/buflist.c @@ -40,8 +40,11 @@ WEECHAT_PLUGIN_PRIORITY(8000); struct t_weechat_plugin *weechat_buflist_plugin = NULL; +struct t_hdata *buflist_hdata_window = NULL; struct t_hdata *buflist_hdata_buffer = NULL; struct t_hdata *buflist_hdata_hotlist = NULL; +struct t_hdata *buflist_hdata_bar = NULL; +struct t_hdata *buflist_hdata_bar_window = NULL; /* @@ -403,8 +406,11 @@ weechat_plugin_init (struct t_weechat_plugin *plugin, int argc, char *argv[]) weechat_plugin = plugin; + buflist_hdata_window = weechat_hdata_get ("window"); buflist_hdata_buffer = weechat_hdata_get ("buffer"); buflist_hdata_hotlist = weechat_hdata_get ("hotlist"); + buflist_hdata_bar = weechat_hdata_get ("bar"); + buflist_hdata_bar_window = weechat_hdata_get ("bar_window"); if (!buflist_config_init ()) return WEECHAT_RC_ERROR; diff --git a/src/plugins/buflist/buflist.h b/src/plugins/buflist/buflist.h index 7574f678d..91e3062e3 100644 --- a/src/plugins/buflist/buflist.h +++ b/src/plugins/buflist/buflist.h @@ -27,8 +27,11 @@ extern struct t_weechat_plugin *weechat_buflist_plugin; +extern struct t_hdata *buflist_hdata_window; extern struct t_hdata *buflist_hdata_buffer; extern struct t_hdata *buflist_hdata_hotlist; +extern struct t_hdata *buflist_hdata_bar; +extern struct t_hdata *buflist_hdata_bar_window; extern void buflist_add_bar (); extern void buflist_buffer_get_irc_pointers(struct t_gui_buffer *buffer, diff --git a/src/plugins/weechat-plugin.h b/src/plugins/weechat-plugin.h index d45dd7344..a78db2cd6 100644 --- a/src/plugins/weechat-plugin.h +++ b/src/plugins/weechat-plugin.h @@ -42,6 +42,7 @@ struct t_gui_window; struct t_gui_buffer; struct t_gui_bar; struct t_gui_bar_item; +struct t_gui_bar_window; struct t_gui_completion; struct t_gui_nick; struct t_gui_nick_group; |