diff options
author | Sébastien Helleu <flashcode@flashtux.org> | 2023-03-16 20:41:05 +0100 |
---|---|---|
committer | Sébastien Helleu <flashcode@flashtux.org> | 2023-03-16 20:41:05 +0100 |
commit | 5441c52d64f5d5561ff7ea001fbdcac8b71d3b19 (patch) | |
tree | cca68227b126ecedf89da4703063ea7fa3d0b2d9 /src/plugins | |
parent | b21bc633135aed6dddcb583f83f6a57914c30243 (diff) | |
download | weechat-5441c52d64f5d5561ff7ea001fbdcac8b71d3b19.zip |
fset: remove scroll to top of fset buffer when options are added or removed (closes #1892)
Diffstat (limited to 'src/plugins')
-rw-r--r-- | src/plugins/fset/fset-buffer.c | 91 | ||||
-rw-r--r-- | src/plugins/fset/fset-buffer.h | 2 | ||||
-rw-r--r-- | src/plugins/fset/fset-option.c | 50 |
3 files changed, 118 insertions, 25 deletions
diff --git a/src/plugins/fset/fset-buffer.c b/src/plugins/fset/fset-buffer.c index c558f3cc0..564e60813 100644 --- a/src/plugins/fset/fset-buffer.c +++ b/src/plugins/fset/fset-buffer.c @@ -157,20 +157,25 @@ fset_buffer_fills_field (char *field, char *field_spaces, /* * Displays a line with an fset option using an evaluated format. + * + * Returns the index of last line displayed in buffer (this depends on the + * format number used), -1 if no option was displayed. */ -void +int fset_buffer_display_option_eval (struct t_fset_option *fset_option) { char *line, str_color_line[128], **lines; char *str_field, *str_field2; char str_color_value[128], str_color_quotes[128], str_number[64]; - int length, length_field, selected_line, y, i, num_lines; + int length, length_field, selected_line, y, y_max, i, num_lines; int default_value_undef, value_undef, value_changed; int add_quotes, add_quotes_parent, format_number; if (!fset_option) - return; + return -1; + + y_max = -1; length_field = 4096; length = (fset_option->value) ? @@ -184,12 +189,12 @@ fset_buffer_display_option_eval (struct t_fset_option *fset_option) str_field = malloc (length_field); if (!str_field) - return; + return -1; str_field2 = malloc (length_field); if (!str_field2) { free (str_field); - return; + return -1; } selected_line = (fset_option->index == fset_buffer_selected_line) ? 1 : 0; @@ -797,6 +802,7 @@ fset_buffer_display_option_eval (struct t_fset_option *fset_option) "%s%s", (str_color_line[0]) ? weechat_color (str_color_line) : "", lines[i]); + y_max = y; y++; } weechat_string_free_split (lines); @@ -806,14 +812,19 @@ fset_buffer_display_option_eval (struct t_fset_option *fset_option) free (str_field); free (str_field2); + + return y_max; } /* * Displays a line with an fset option using a predefined format * (much faster because there is no eval). + * + * Returns the index of last line displayed in buffer (this depends on the + * format number used), -1 if no option was displayed. */ -void +int fset_buffer_display_option_predefined_format (struct t_fset_option *fset_option) { int selected_line, value_undef, value_changed, format_number; @@ -821,6 +832,9 @@ fset_buffer_display_option_predefined_format (struct t_fset_option *fset_option) char str_marked[128], str_name[4096], str_type[128], *str_value; char str_color_line[128], str_color_value[128], str_color_quotes[128]; + if (!fset_option) + return -1; + selected_line = (fset_option->index == fset_buffer_selected_line) ? 1 : 0; value_undef = (fset_option->value == NULL) ? 1 : 0; value_changed = (fset_option_value_is_changed (fset_option)) ? 1 : 0; @@ -969,13 +983,18 @@ fset_buffer_display_option_predefined_format (struct t_fset_option *fset_option) if (str_value) free (str_value); + + return fset_option->index; } /* * Displays a line with an fset option. + * + * Returns the index of last line displayed in buffer (this depends on the + * format number used), -1 if no option was displayed. */ -void +int fset_buffer_display_option (struct t_fset_option *fset_option) { int format_number; @@ -985,9 +1004,39 @@ fset_buffer_display_option (struct t_fset_option *fset_option) ptr_format = weechat_config_string (fset_config_format_option[format_number - 1]); if (ptr_format && ptr_format[0]) - fset_buffer_display_option_eval (fset_option); + return fset_buffer_display_option_eval (fset_option); else - fset_buffer_display_option_predefined_format (fset_option); + return fset_buffer_display_option_predefined_format (fset_option); +} + +/* + * Returns the last line index (y) for a buffer with free content, + * -1 if buffer is empty. + */ + +int +fset_buffer_get_last_y (struct t_gui_buffer *buffer) +{ + struct t_hdata *hdata_buffer, *hdata_lines, *hdata_line, *hdata_line_data; + void *own_lines, *last_line, *line_data; + + hdata_buffer = weechat_hdata_get ("buffer"); + own_lines = weechat_hdata_pointer (hdata_buffer, buffer, "own_lines"); + if (!own_lines) + return -1; + + hdata_lines = weechat_hdata_get ("lines"); + last_line = weechat_hdata_pointer (hdata_lines, own_lines, "last_line"); + if (!last_line) + return -1; + + hdata_line = weechat_hdata_get ("line"); + line_data = weechat_hdata_pointer (hdata_line, last_line, "data"); + if (!line_data) + return -1; + + hdata_line_data = weechat_hdata_get ("line_data"); + return weechat_hdata_integer (hdata_line_data, line_data, "y"); } /* @@ -997,7 +1046,7 @@ fset_buffer_display_option (struct t_fset_option *fset_option) void fset_buffer_refresh (int clear) { - int num_options, i; + int num_options, i, y, y_max_displayed, last_y; struct t_fset_option *ptr_fset_option; if (!fset_buffer) @@ -1006,13 +1055,29 @@ fset_buffer_refresh (int clear) num_options = weechat_arraylist_size (fset_options); if (clear) + { weechat_buffer_clear (fset_buffer); + fset_buffer_selected_line = 0; + } + + y_max_displayed = -1; for (i = 0; i < num_options; i++) { ptr_fset_option = weechat_arraylist_get (fset_options, i); if (ptr_fset_option) - fset_buffer_display_option (ptr_fset_option); + { + y = fset_buffer_display_option (ptr_fset_option); + if (y > y_max_displayed) + y_max_displayed = y; + } + } + + /* remove lines displayed after the last one just displayed */ + last_y = fset_buffer_get_last_y (fset_buffer); + for (y = last_y; y > y_max_displayed; y--) + { + weechat_printf_y (fset_buffer, y, ""); } fset_buffer_set_title (); @@ -1035,10 +1100,10 @@ fset_buffer_set_current_line (int line) if (old_line != fset_buffer_selected_line) { - fset_buffer_display_option ( + (void) fset_buffer_display_option ( weechat_arraylist_get (fset_options, old_line)); } - fset_buffer_display_option ( + (void) fset_buffer_display_option ( weechat_arraylist_get (fset_options, fset_buffer_selected_line)); fset_buffer_set_title (); diff --git a/src/plugins/fset/fset-buffer.h b/src/plugins/fset/fset-buffer.h index 013dab9eb..bf0b2c2bc 100644 --- a/src/plugins/fset/fset-buffer.h +++ b/src/plugins/fset/fset-buffer.h @@ -28,7 +28,7 @@ extern struct t_gui_buffer *fset_buffer; extern int fset_buffer_selected_line; extern void fset_buffer_set_title (); -extern void fset_buffer_display_option (struct t_fset_option *fset_option); +extern int fset_buffer_display_option (struct t_fset_option *fset_option); extern void fset_buffer_refresh (int clear); extern void fset_buffer_set_current_line (int line); extern void fset_buffer_check_line_outside_window (); diff --git a/src/plugins/fset/fset-option.c b/src/plugins/fset/fset-option.c index e6d9c0e86..a8fdf2057 100644 --- a/src/plugins/fset/fset-option.c +++ b/src/plugins/fset/fset-option.c @@ -1244,7 +1244,7 @@ fset_option_toggle_mark (struct t_fset_option *fset_option, fset_option->marked ^= 1; fset_option_count_marked += (fset_option->marked) ? 1 : -1; - fset_buffer_display_option (fset_option); + (void) fset_buffer_display_option (fset_option); } /* @@ -1273,14 +1273,14 @@ fset_option_mark_options_matching_filter (const char *filter, int mark) { ptr_fset_option->marked = 1; fset_option_count_marked++; - fset_buffer_display_option (ptr_fset_option); + (void) fset_buffer_display_option (ptr_fset_option); set_title = 1; } else if (mark_old && !mark) { ptr_fset_option->marked = 0; fset_option_count_marked--; - fset_buffer_display_option (ptr_fset_option); + (void) fset_buffer_display_option (ptr_fset_option); set_title = 1; } } @@ -1313,7 +1313,7 @@ fset_option_unmark_all () ptr_fset_option->marked = 0; if (marked) { - fset_buffer_display_option (ptr_fset_option); + (void) fset_buffer_display_option (ptr_fset_option); set_title = 1; } } @@ -1418,28 +1418,38 @@ fset_option_config_changed (const char *option_name) { struct t_fset_option *ptr_fset_option, *new_fset_option; struct t_config_option *ptr_option; - int full_refresh, line, num_options; + int option_removed, option_added, line, num_options; + char *old_name_selected; if (!fset_buffer) return; - full_refresh = 0; + option_removed = 0; + option_added = 0; + + ptr_fset_option = weechat_arraylist_get (fset_options, + fset_buffer_selected_line); + old_name_selected = (ptr_fset_option) ? + strdup (ptr_fset_option->name) : NULL; ptr_fset_option = (option_name) ? fset_option_search_by_name (option_name, &line) : NULL; ptr_option = (option_name) ? weechat_config_get (option_name) : NULL; + if (ptr_fset_option) { if (ptr_option) { fset_option_set_values (ptr_fset_option, ptr_option); - fset_buffer_display_option (ptr_fset_option); + (void) fset_buffer_display_option (ptr_fset_option); } else { /* option removed: get options and refresh the whole buffer */ - full_refresh = 1; + option_removed = 1; + if (ptr_fset_option->index < fset_buffer_selected_line) + fset_buffer_selected_line--; } } else if (ptr_option) @@ -1448,15 +1458,30 @@ fset_option_config_changed (const char *option_name) if (fset_option_match_filter (new_fset_option, fset_option_filter)) { /* option added: get options and refresh the whole buffer */ - full_refresh = 1; + option_added = 1; } fset_option_free (new_fset_option); } - if (full_refresh) + if (option_removed || option_added) { fset_option_get_options (); - fset_buffer_refresh (1); + /* + * in case of option added, we move to the next one if is was the + * selected one + */ + if (option_added && old_name_selected) + { + ptr_fset_option = weechat_arraylist_get ( + fset_options, fset_buffer_selected_line + 1); + if (ptr_fset_option + && (strcmp (old_name_selected, ptr_fset_option->name) == 0)) + { + fset_buffer_selected_line++; + } + } + fset_buffer_refresh (0); + fset_buffer_check_line_outside_window (); } else { @@ -1477,6 +1502,9 @@ fset_option_config_changed (const char *option_name) fset_option_set_max_length_fields_all (); fset_buffer_refresh (0); } + + if (old_name_selected) + free (old_name_selected); } /* |