1
0
mirror of https://github.com/weechat/weechat.git synced 2026-07-01 07:16:37 +02:00

buflist: evaluate option buflist.look.sort (closes #1465)

The evaluation allows to use different sort for the bar items, for example with
such value:

    ${if:${bar_item.name}==buflist?number:short_name}

This sorts by number on the first bar item and by short name on the two other
bar items.
This commit is contained in:
Sébastien Helleu
2020-04-06 20:49:17 +02:00
parent a072d769fd
commit fdd39c6b97
26 changed files with 185 additions and 88 deletions
+24 -4
View File
@@ -29,13 +29,15 @@
#include "buflist-config.h"
struct t_gui_bar_item *buflist_bar_item_buflist[BUFLIST_BAR_NUM_ITEMS];
struct t_gui_bar_item *buflist_bar_item_buflist[BUFLIST_BAR_NUM_ITEMS] =
{ NULL, NULL, NULL };
struct t_hashtable *buflist_hashtable_pointers = NULL;
struct t_hashtable *buflist_hashtable_extra_vars = NULL;
struct t_hashtable *buflist_hashtable_options_conditions = NULL;
struct t_arraylist *buflist_list_buffers[BUFLIST_BAR_NUM_ITEMS];
struct t_arraylist *buflist_list_buffers[BUFLIST_BAR_NUM_ITEMS] =
{ NULL, NULL, NULL };
int old_line_number_current_buffer[BUFLIST_BAR_NUM_ITEMS];
int old_line_number_current_buffer[BUFLIST_BAR_NUM_ITEMS] = { -1, -1, -1 };
/*
@@ -81,6 +83,24 @@ buflist_bar_item_get_index (const char *item_name)
return -1;
}
/*
* Returns the bar item index with a bar item pointer, -1 if not found.
*/
int
buflist_bar_item_get_index_with_pointer (struct t_gui_bar_item *item)
{
int i;
for (i = 0; i < BUFLIST_BAR_NUM_ITEMS; i++)
{
if (buflist_bar_item_buflist[i] == item)
return i;
}
return -1;
}
/*
* Updates buflist bar item if buflist is enabled (or if force argument is 1).
*/
@@ -332,7 +352,7 @@ buflist_bar_item_buflist_cb (const void *pointer, void *data,
16, 0, 1,
NULL, NULL, NULL, NULL);
buffers = buflist_sort_buffers ();
buffers = buflist_sort_buffers (item);
num_buffers = weechat_arraylist_size (buffers);
for (i = 0; i < num_buffers; i++)
+4
View File
@@ -24,10 +24,14 @@
#define BUFLIST_BAR_NUM_ITEMS 3
struct t_gui_bar_item;
extern struct t_gui_bar_item *buflist_bar_item_buflist[BUFLIST_BAR_NUM_ITEMS];
extern struct t_arraylist *buflist_list_buffers[BUFLIST_BAR_NUM_ITEMS];
extern const char *buflist_bar_item_get_name (int index);
extern int buflist_bar_item_get_index (const char *item_name);
extern int buflist_bar_item_get_index_with_pointer (struct t_gui_bar_item *item);
extern void buflist_bar_item_update (int force);
extern int buflist_bar_item_init ();
extern void buflist_bar_item_end ();
+59 -17
View File
@@ -61,8 +61,8 @@ struct t_config_option *buflist_config_format_number;
struct t_hook **buflist_config_signals_refresh = NULL;
int buflist_config_num_signals_refresh = 0;
char **buflist_config_sort_fields = NULL;
int buflist_config_sort_fields_count = 0;
char **buflist_config_sort_fields[BUFLIST_BAR_NUM_ITEMS] = { NULL, NULL, NULL };
int buflist_config_sort_fields_count[BUFLIST_BAR_NUM_ITEMS] = { 0, 0, 0 };
char *buflist_config_format_buffer_eval = NULL;
char *buflist_config_format_buffer_current_eval = NULL;
char *buflist_config_format_hotlist_eval = NULL;
@@ -246,23 +246,58 @@ void
buflist_config_change_sort (const void *pointer, void *data,
struct t_config_option *option)
{
int i;
struct t_hashtable *hashtable_pointers;
char *sort;
/* make C compiler happy */
(void) pointer;
(void) data;
(void) option;
if (buflist_config_sort_fields)
weechat_string_free_split (buflist_config_sort_fields);
for (i = 0; i < BUFLIST_BAR_NUM_ITEMS; i++)
{
if (buflist_config_sort_fields[i])
{
weechat_string_free_split (buflist_config_sort_fields[i]);
buflist_config_sort_fields[i] = NULL;
buflist_config_sort_fields_count[i] = 0;
}
}
buflist_config_sort_fields = weechat_string_split (
weechat_config_string (buflist_config_look_sort),
",",
hashtable_pointers = weechat_hashtable_new (
32,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_POINTER,
NULL,
WEECHAT_STRING_SPLIT_STRIP_LEFT
| WEECHAT_STRING_SPLIT_STRIP_RIGHT
| WEECHAT_STRING_SPLIT_COLLAPSE_SEPS,
0,
&buflist_config_sort_fields_count);
NULL);
if (!hashtable_pointers)
return;
for (i = 0; i < BUFLIST_BAR_NUM_ITEMS; i++)
{
weechat_hashtable_set (hashtable_pointers,
"bar_item", buflist_bar_item_buflist[i]);
sort = weechat_string_eval_expression (
weechat_config_string (buflist_config_look_sort),
hashtable_pointers,
NULL,
NULL);
buflist_config_sort_fields[i] = weechat_string_split (
(sort) ? sort : "",
",",
NULL,
WEECHAT_STRING_SPLIT_STRIP_LEFT
| WEECHAT_STRING_SPLIT_STRIP_RIGHT
| WEECHAT_STRING_SPLIT_COLLAPSE_SEPS,
0,
&buflist_config_sort_fields_count[i]);
if (sort)
free (sort);
}
buflist_bar_item_update (0);
}
@@ -522,7 +557,9 @@ buflist_config_init ()
"char \"-\" can be used before field to reverse order, "
"char \"~\" can be used to do a case insensitive comparison; "
"example: \"-~short_name\" for case insensitive and reverse "
"sort on buffer short name"),
"sort on buffer short name "
"(note: content is evaluated, only the pointer to bar_item can be "
"used, for example \"bar_item.name\")"),
NULL, 0, 0, "number,-active", NULL, 0,
NULL, NULL, NULL,
&buflist_config_change_sort, NULL, NULL,
@@ -726,16 +763,21 @@ buflist_config_write ()
void
buflist_config_free ()
{
int i;
weechat_config_free (buflist_config_file);
if (buflist_config_signals_refresh)
buflist_config_free_signals_refresh ();
if (buflist_config_sort_fields)
for (i = 0; i < BUFLIST_BAR_NUM_ITEMS; i++)
{
weechat_string_free_split (buflist_config_sort_fields);
buflist_config_sort_fields = NULL;
buflist_config_sort_fields_count = 0;
if (buflist_config_sort_fields[i])
{
weechat_string_free_split (buflist_config_sort_fields[i]);
buflist_config_sort_fields[i] = NULL;
buflist_config_sort_fields_count[i] = 0;
}
}
if (buflist_config_format_buffer_eval)
+6 -2
View File
@@ -20,6 +20,8 @@
#ifndef WEECHAT_PLUGIN_BUFLIST_CONFIG_H
#define WEECHAT_PLUGIN_BUFLIST_CONFIG_H
#include "buflist-bar-item.h"
#define BUFLIST_CONFIG_NAME "buflist"
#define BUFLIST_CONFIG_SIGNALS_REFRESH \
@@ -56,12 +58,14 @@ extern struct t_config_option *buflist_config_format_name;
extern struct t_config_option *buflist_config_format_nick_prefix;
extern struct t_config_option *buflist_config_format_number;
extern char **buflist_config_sort_fields;
extern int buflist_config_sort_fields_count;
extern char **buflist_config_sort_fields[BUFLIST_BAR_NUM_ITEMS];
extern int buflist_config_sort_fields_count[BUFLIST_BAR_NUM_ITEMS];
extern char *buflist_config_format_buffer_eval;
extern char *buflist_config_format_buffer_current_eval;
extern char *buflist_config_format_hotlist_eval;
extern void buflist_config_change_sort (const void *pointer, void *data,
struct t_config_option *option);
extern int buflist_config_init ();
extern int buflist_config_read ();
extern int buflist_config_write ();
+13 -6
View File
@@ -221,25 +221,30 @@ int
buflist_compare_buffers (void *data, struct t_arraylist *arraylist,
void *pointer1, void *pointer2)
{
int i, reverse, case_sensitive, rc;
int i, item_number, reverse, case_sensitive, rc;
const char *ptr_field;
struct t_gui_hotlist *ptr_hotlist1, *ptr_hotlist2;
void *ptr_server1, *ptr_server2, *ptr_channel1, *ptr_channel2;
struct t_hdata *hdata_irc_server, *hdata_irc_channel;
struct t_gui_bar_item *ptr_item;
/* make C compiler happy */
(void) data;
(void) arraylist;
ptr_item = (struct t_gui_bar_item *)data;
item_number = buflist_bar_item_get_index_with_pointer (ptr_item);
if (item_number < 0)
item_number= 0;
hdata_irc_server = weechat_hdata_get ("irc_server");
hdata_irc_channel = weechat_hdata_get ("irc_channel");
for (i = 0; i < buflist_config_sort_fields_count; i++)
for (i = 0; i < buflist_config_sort_fields_count[item_number]; i++)
{
rc = 0;
reverse = 1;
case_sensitive = 1;
ptr_field = buflist_config_sort_fields[i];
ptr_field = buflist_config_sort_fields[item_number][i];
while ((ptr_field[0] == '-') || (ptr_field[0] == '~'))
{
if (ptr_field[0] == '-')
@@ -337,13 +342,13 @@ buflist_compare_buffers (void *data, struct t_arraylist *arraylist,
*/
struct t_arraylist *
buflist_sort_buffers ()
buflist_sort_buffers (struct t_gui_bar_item *item)
{
struct t_arraylist *buffers;
struct t_gui_buffer *ptr_buffer;
buffers = weechat_arraylist_new (128, 1, 1,
&buflist_compare_buffers, NULL,
&buflist_compare_buffers, item,
NULL, NULL);
ptr_buffer = weechat_hdata_get_list (buflist_hdata_buffer, "gui_buffers");
@@ -454,6 +459,8 @@ weechat_plugin_init (struct t_weechat_plugin *plugin, int argc, char *argv[])
if (!buflist_bar_item_init ())
return WEECHAT_RC_ERROR;
buflist_config_change_sort (NULL, NULL, NULL);
buflist_command_init ();
if (weechat_config_boolean (buflist_config_look_enabled))
+3 -1
View File
@@ -25,6 +25,8 @@
#define BUFLIST_BAR_NAME "buflist"
struct t_gui_bar_item;
extern struct t_weechat_plugin *weechat_buflist_plugin;
extern struct t_hdata *buflist_hdata_window;
@@ -38,6 +40,6 @@ extern void buflist_add_bar ();
extern void buflist_buffer_get_irc_pointers (struct t_gui_buffer *buffer,
void **irc_server,
void **irc_channel);
extern struct t_arraylist *buflist_sort_buffers ();
extern struct t_arraylist *buflist_sort_buffers (struct t_gui_bar_item *item);
#endif /* WEECHAT_PLUGIN_BUFLIST_H */