1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
/*
* buflist.c - Bar with list of buffers
*
* Copyright (C) 2003-2017 Sébastien Helleu <flashcode@flashtux.org>
*
* This file is part of WeeChat, the extensible chat client.
*
* WeeChat is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* WeeChat is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with WeeChat. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string.h>
#include "../weechat-plugin.h"
#include "buflist.h"
#include "buflist-bar-item.h"
#include "buflist-config.h"
WEECHAT_PLUGIN_NAME(BUFLIST_PLUGIN_NAME);
WEECHAT_PLUGIN_DESCRIPTION(N_("Buffers list"));
WEECHAT_PLUGIN_AUTHOR("Sébastien Helleu <flashcode@flashtux.org>");
WEECHAT_PLUGIN_VERSION(WEECHAT_VERSION);
WEECHAT_PLUGIN_LICENSE(WEECHAT_LICENSE);
WEECHAT_PLUGIN_PRIORITY(8000);
struct t_weechat_plugin *weechat_buflist_plugin = NULL;
struct t_hdata *buflist_hdata_buffer = NULL;
struct t_hdata *buflist_hdata_hotlist = NULL;
/*
* Searches the hotlist pointer for the buffer.
*
* Returns pointer to hotlit, NULL if buffer is not in hotlist.
*/
struct t_gui_hotlist *
buflist_search_hotlist_for_buffer (struct t_gui_buffer *buffer)
{
struct t_gui_hotlist *ptr_hotlist;
struct t_gui_buffer *ptr_buffer;
ptr_hotlist = weechat_hdata_get_list (buflist_hdata_hotlist,
"gui_hotlist");
while (ptr_hotlist)
{
ptr_buffer = weechat_hdata_pointer (buflist_hdata_hotlist,
ptr_hotlist, "buffer");
if (ptr_buffer == buffer)
break;
ptr_hotlist = weechat_hdata_move (buflist_hdata_hotlist,
ptr_hotlist, 1);
}
return ptr_hotlist;
}
/*
* Compares a hdata variable of two objects.
*
* Returns:
* -1: variable1 < variable2
* 0: variable1 == variable2
* 1: variable1 > variable2
*/
int
buflist_compare_hdata_var (struct t_hdata *hdata,
void *pointer1, void *pointer2,
const char *variable)
{
int type, rc, int_value1, int_value2;
long long_value1, long_value2;
char char_value1, char_value2;
const char *pos, *str_value1, *str_value2;
void *ptr_value1, *ptr_value2;
time_t time_value1, time_value2;
rc = 0;
pos = strchr (variable, '|');
type = weechat_hdata_get_var_type (hdata, (pos) ? pos + 1 : variable);
switch (type)
{
case WEECHAT_HDATA_CHAR:
char_value1 = weechat_hdata_char (hdata, pointer1, variable);
char_value2 = weechat_hdata_char (hdata, pointer2, variable);
rc = (char_value1 < char_value2) ?
-1 : ((char_value1 > char_value2) ? 1 : 0);
break;
case WEECHAT_HDATA_INTEGER:
int_value1 = weechat_hdata_integer (hdata, pointer1, variable);
int_value2 = weechat_hdata_integer (hdata, pointer2, variable);
rc = (int_value1 < int_value2) ?
-1 : ((int_value1 > int_value2) ? 1 : 0);
break;
case WEECHAT_HDATA_LONG:
long_value1 = weechat_hdata_long (hdata, pointer1, variable);
long_value2 = weechat_hdata_long (hdata, pointer2, variable);
rc = (long_value1 < long_value2) ?
-1 : ((long_value1 > long_value2) ? 1 : 0);
break;
case WEECHAT_HDATA_STRING:
case WEECHAT_HDATA_SHARED_STRING:
str_value1 = weechat_hdata_string (hdata, pointer1, variable);
str_value2 = weechat_hdata_string (hdata, pointer2, variable);
if (!str_value1 && !str_value2)
rc = 0;
else if (str_value1 && !str_value2)
rc = 1;
else if (!str_value1 && str_value2)
rc = -1;
else
{
rc = strcmp (str_value1, str_value2);
if (rc < 0)
rc = -1;
else if (rc > 0)
rc = 1;
}
break;
case WEECHAT_HDATA_POINTER:
ptr_value1 = weechat_hdata_pointer (hdata, pointer1, variable);
ptr_value2 = weechat_hdata_pointer (hdata, pointer2, variable);
rc = (ptr_value1 < ptr_value2) ?
-1 : ((ptr_value1 > ptr_value2) ? 1 : 0);
break;
case WEECHAT_HDATA_TIME:
time_value1 = weechat_hdata_time (hdata, pointer1, variable);
time_value2 = weechat_hdata_time (hdata, pointer2, variable);
rc = (time_value1 < time_value2) ?
-1 : ((time_value1 > time_value2) ? 1 : 0);
break;
}
return rc;
}
/*
* Compares two buffers in order to add them in the sorted arraylist.
*
* The comparison is made using the list of fields defined in the option
* "buflist.look.sort".
*
* Returns:
* -1: buffer1 < buffer2
* 0: buffer1 == buffer2
* 1: buffer1 > buffer2
*/
int
buflist_compare_buffers (void *data, struct t_arraylist *arraylist,
void *pointer1, void *pointer2)
{
int i, reverse, rc, hotlist_scanned;
const char *ptr_field;
struct t_gui_hotlist *ptr_hotlist1, *ptr_hotlist2;
/* make C compiler happy */
(void) data;
(void) arraylist;
hotlist_scanned = 0;
ptr_hotlist1 = NULL;
ptr_hotlist2 = NULL;
for (i = 0; i < buflist_config_sort_fields_count; i++)
{
reverse = 1;
if (buflist_config_sort_fields[i][0] == '-')
{
ptr_field = buflist_config_sort_fields[i] + 1;
reverse = -1;
}
else
{
ptr_field = buflist_config_sort_fields[i];
}
rc = 0;
if (strncmp (ptr_field, "hotlist.", 8) == 0)
{
if (!hotlist_scanned)
{
ptr_hotlist1 = buflist_search_hotlist_for_buffer (pointer1);
ptr_hotlist2 = buflist_search_hotlist_for_buffer (pointer2);
}
if (!ptr_hotlist1 && !ptr_hotlist2)
rc = 0;
else if (ptr_hotlist1 && !ptr_hotlist2)
rc = 1;
else if (!ptr_hotlist1 && ptr_hotlist2)
rc = -1;
else
{
rc = buflist_compare_hdata_var (buflist_hdata_hotlist,
pointer1, pointer2,
ptr_field + 8);
}
}
else
{
rc = buflist_compare_hdata_var (buflist_hdata_buffer,
pointer1, pointer2,
ptr_field);
}
rc *= reverse;
if (rc != 0)
return rc;
}
return rc;
}
/*
* Builds a list of pointers to buffers, sorted according to option
* "buflist.look.sort".
*
* Returns an arraylist that must be freed by weechat_arraylist_free after use.
*/
struct t_arraylist *
buflist_sort_buffers ()
{
struct t_arraylist *buffers;
struct t_gui_buffer *ptr_buffer;
buffers = weechat_arraylist_new (128, 1, 1,
&buflist_compare_buffers, NULL,
NULL, NULL);
ptr_buffer = weechat_hdata_get_list (buflist_hdata_buffer, "gui_buffers");
while (ptr_buffer)
{
weechat_arraylist_add (buffers, ptr_buffer);
ptr_buffer = weechat_hdata_move (buflist_hdata_buffer, ptr_buffer, 1);
}
return buffers;
}
/*
* Callback for a signal on a buffer.
*/
int
buflist_signal_buffer_cb (const void *pointer, void *data,
const char *signal, const char *type_data,
void *signal_data)
{
/* make C compiler happy */
(void) pointer;
(void) data;
(void) signal;
(void) type_data;
(void) signal_data;
weechat_bar_item_update (BUFLIST_BAR_ITEM_NAME);
return WEECHAT_RC_OK;
}
/*
* Initializes buflist plugin.
*/
int
weechat_plugin_init (struct t_weechat_plugin *plugin, int argc, char *argv[])
{
char *signals_buffers[] =
{ "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", "hotlist_changed", NULL
};
int i;
/* make C compiler happy */
(void) argc;
(void) argv;
weechat_plugin = plugin;
buflist_hdata_buffer = weechat_hdata_get ("buffer");
buflist_hdata_hotlist = weechat_hdata_get ("hotlist");
if (!buflist_config_init ())
return WEECHAT_RC_ERROR;
buflist_config_read ();
if (!buflist_bar_item_init ())
return WEECHAT_RC_ERROR;
/* hook some signals */
for (i = 0; signals_buffers[i]; i++)
{
weechat_hook_signal (signals_buffers[i],
&buflist_signal_buffer_cb, NULL, NULL);
}
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;
}
/*
* Ends buflist plugin.
*/
int
weechat_plugin_end (struct t_weechat_plugin *plugin)
{
/* make C compiler happy */
(void) plugin;
buflist_bar_item_end ();
buflist_config_write ();
buflist_config_free ();
return WEECHAT_RC_OK;
}
|