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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
/*
* Copyright (c) 2003-2007 by FlashCode <flashcode@flashtux.org>
* See README for License detail, AUTHORS for developers list.
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* gui-hotlist.c: hotlist management (buffers with activity) */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <string.h>
#include "../core/weechat.h"
#include "../core/wee-config.h"
#include "../core/wee-log.h"
#include "../core/wee-util.h"
#include "gui-hotlist.h"
#include "gui-buffer.h"
#include "gui-window.h"
struct t_gui_hotlist *gui_hotlist = NULL;
struct t_gui_hotlist *last_gui_hotlist = NULL;
struct t_gui_buffer *gui_hotlist_initial_buffer = NULL;
int gui_add_hotlist = 1; /* 0 is for temporarly disable */
/* hotlist add for all buffers */
/*
* gui_hotlist_search: find hotlist with buffer pointer
*/
struct t_gui_hotlist *
gui_hotlist_search (struct t_gui_hotlist *hotlist, struct t_gui_buffer *buffer)
{
struct t_gui_hotlist *ptr_hotlist;
for (ptr_hotlist = hotlist; ptr_hotlist;
ptr_hotlist = ptr_hotlist->next_hotlist)
{
if (ptr_hotlist->buffer == buffer)
return ptr_hotlist;
}
return NULL;
}
/*
* gui_hotlist_find_pos: find position for a inserting in hotlist
* (for sorting hotlist)
*/
struct t_gui_hotlist *
gui_hotlist_find_pos (struct t_gui_hotlist *hotlist, struct t_gui_hotlist *new_hotlist)
{
struct t_gui_hotlist *ptr_hotlist;
switch (CONFIG_INTEGER(config_look_hotlist_sort))
{
case CONFIG_LOOK_HOTLIST_SORT_GROUP_TIME_ASC:
for (ptr_hotlist = hotlist; ptr_hotlist;
ptr_hotlist = ptr_hotlist->next_hotlist)
{
if ((new_hotlist->priority > ptr_hotlist->priority)
|| ((new_hotlist->priority == ptr_hotlist->priority)
&& (util_timeval_diff (&(new_hotlist->creation_time),
&(ptr_hotlist->creation_time)) > 0)))
return ptr_hotlist;
}
break;
case CONFIG_LOOK_HOTLIST_SORT_GROUP_TIME_DESC:
for (ptr_hotlist = hotlist; ptr_hotlist;
ptr_hotlist = ptr_hotlist->next_hotlist)
{
if ((new_hotlist->priority > ptr_hotlist->priority)
|| ((new_hotlist->priority == ptr_hotlist->priority)
&& (util_timeval_diff (&(new_hotlist->creation_time),
&(ptr_hotlist->creation_time)) < 0)))
return ptr_hotlist;
}
break;
case CONFIG_LOOK_HOTLIST_SORT_GROUP_NUMBER_ASC:
for (ptr_hotlist = hotlist; ptr_hotlist;
ptr_hotlist = ptr_hotlist->next_hotlist)
{
if ((new_hotlist->priority > ptr_hotlist->priority)
|| ((new_hotlist->priority == ptr_hotlist->priority)
&& (new_hotlist->buffer->number < ptr_hotlist->buffer->number)))
return ptr_hotlist;
}
break;
case CONFIG_LOOK_HOTLIST_SORT_GROUP_NUMBER_DESC:
for (ptr_hotlist = hotlist; ptr_hotlist;
ptr_hotlist = ptr_hotlist->next_hotlist)
{
if ((new_hotlist->priority > ptr_hotlist->priority)
|| ((new_hotlist->priority == ptr_hotlist->priority)
&& (new_hotlist->buffer->number > ptr_hotlist->buffer->number)))
return ptr_hotlist;
}
break;
case CONFIG_LOOK_HOTLIST_SORT_NUMBER_ASC:
for (ptr_hotlist = hotlist; ptr_hotlist;
ptr_hotlist = ptr_hotlist->next_hotlist)
{
if (new_hotlist->buffer->number < ptr_hotlist->buffer->number)
return ptr_hotlist;
}
break;
case CONFIG_LOOK_HOTLIST_SORT_NUMBER_DESC:
for (ptr_hotlist = hotlist; ptr_hotlist;
ptr_hotlist = ptr_hotlist->next_hotlist)
{
if (new_hotlist->buffer->number > ptr_hotlist->buffer->number)
return ptr_hotlist;
}
break;
}
return NULL;
}
/*
* gui_hotlist_add_hotlist: add new hotlist in list
*/
void
gui_hotlist_add_hotlist (struct t_gui_hotlist **hotlist,
struct t_gui_hotlist **last_hotlist,
struct t_gui_hotlist *new_hotlist)
{
struct t_gui_hotlist *pos_hotlist;
if (*hotlist)
{
pos_hotlist = gui_hotlist_find_pos (*hotlist, new_hotlist);
if (pos_hotlist)
{
/* insert hotlist into the hotlist (before hotlist found) */
new_hotlist->prev_hotlist = pos_hotlist->prev_hotlist;
new_hotlist->next_hotlist = pos_hotlist;
if (pos_hotlist->prev_hotlist)
pos_hotlist->prev_hotlist->next_hotlist = new_hotlist;
else
*hotlist = new_hotlist;
pos_hotlist->prev_hotlist = new_hotlist;
}
else
{
/* add hotlist to the end */
new_hotlist->prev_hotlist = *last_hotlist;
new_hotlist->next_hotlist = NULL;
(*last_hotlist)->next_hotlist = new_hotlist;
*last_hotlist = new_hotlist;
}
}
else
{
new_hotlist->prev_hotlist = NULL;
new_hotlist->next_hotlist = NULL;
*hotlist = new_hotlist;
*last_hotlist = new_hotlist;
}
}
/*
* gui_hotlist_add: add a buffer to hotlist, with priority
* if creation_time is NULL, current time is used
*/
void
gui_hotlist_add (struct t_gui_buffer *buffer, int priority,
struct timeval *creation_time, int allow_current_buffer)
{
struct t_gui_hotlist *new_hotlist, *ptr_hotlist;
if (!buffer)
return;
/* do not highlight current buffer */
if ((buffer == gui_current_window->buffer)
&& (!allow_current_buffer) && (!gui_buffer_is_scrolled (buffer)))
return;
if ((ptr_hotlist = gui_hotlist_search (gui_hotlist, buffer)))
{
/* return if priority is greater or equal than the one to add */
if (ptr_hotlist->priority >= priority)
return;
/* remove buffer if present with lower priority and go on */
gui_hotlist_free (&gui_hotlist, &last_gui_hotlist, ptr_hotlist);
}
if ((new_hotlist = (struct t_gui_hotlist *) malloc (sizeof (struct t_gui_hotlist))) == NULL)
{
log_printf (_("Error: not enough memory to add a buffer to "
"hotlist"));
return;
}
new_hotlist->priority = priority;
if (creation_time)
memcpy (&(new_hotlist->creation_time),
creation_time, sizeof (creation_time));
else
gettimeofday (&(new_hotlist->creation_time), NULL);
new_hotlist->buffer = buffer;
new_hotlist->next_hotlist = NULL;
new_hotlist->prev_hotlist = NULL;
gui_hotlist_add_hotlist (&gui_hotlist, &last_gui_hotlist, new_hotlist);
}
/*
* gui_hotlist_dup: duplicate hotlist element
*/
struct t_gui_hotlist *
gui_hotlist_dup (struct t_gui_hotlist *hotlist)
{
struct t_gui_hotlist *new_hotlist;
if ((new_hotlist = (struct t_gui_hotlist *) malloc (sizeof (struct t_gui_hotlist))))
{
new_hotlist->priority = hotlist->priority;
memcpy (&(new_hotlist->creation_time), &(hotlist->creation_time),
sizeof (new_hotlist->creation_time));
new_hotlist->buffer = hotlist->buffer;
new_hotlist->prev_hotlist = NULL;
new_hotlist->next_hotlist = NULL;
return new_hotlist;
}
return NULL;
}
/*
* gui_hotlist_resort: resort hotlist with new sort type
*/
void
gui_hotlist_resort ()
{
struct t_gui_hotlist *new_hotlist, *last_new_hotlist;
struct t_gui_hotlist *ptr_hotlist, *element;
/* copy and resort hotlist in new linked list */
new_hotlist = NULL;
last_new_hotlist = NULL;
for (ptr_hotlist = gui_hotlist; ptr_hotlist;
ptr_hotlist = ptr_hotlist->next_hotlist)
{
element = gui_hotlist_dup (ptr_hotlist);
gui_hotlist_add_hotlist (&new_hotlist, &last_new_hotlist, element);
}
gui_hotlist_free_all (&gui_hotlist, &last_gui_hotlist);
gui_hotlist = new_hotlist;
last_gui_hotlist = last_new_hotlist;
}
/*
* gui_hotlist_free: free a hotlist and remove it from hotlist queue
*/
void
gui_hotlist_free (struct t_gui_hotlist **hotlist,
struct t_gui_hotlist **last_hotlist,
struct t_gui_hotlist *ptr_hotlist)
{
struct t_gui_hotlist *new_hotlist;
/* remove hotlist from queue */
if (*last_hotlist == ptr_hotlist)
*last_hotlist = ptr_hotlist->prev_hotlist;
if (ptr_hotlist->prev_hotlist)
{
(ptr_hotlist->prev_hotlist)->next_hotlist = ptr_hotlist->next_hotlist;
new_hotlist = *hotlist;
}
else
new_hotlist = ptr_hotlist->next_hotlist;
if (ptr_hotlist->next_hotlist)
(ptr_hotlist->next_hotlist)->prev_hotlist = ptr_hotlist->prev_hotlist;
free (ptr_hotlist);
*hotlist = new_hotlist;
}
/*
* gui_hotlist_free_all: free all hotlists
*/
void
gui_hotlist_free_all (struct t_gui_hotlist **hotlist,
struct t_gui_hotlist **last_hotlist)
{
/* remove all hotlists */
while (*hotlist)
{
gui_hotlist_free (hotlist, last_hotlist, *hotlist);
}
}
/*
* gui_hotlist_remove_buffer: remove a buffer from hotlist
*/
void
gui_hotlist_remove_buffer (struct t_gui_buffer *buffer)
{
struct t_gui_hotlist *pos_hotlist;
pos_hotlist = gui_hotlist_search (gui_hotlist, buffer);
if (pos_hotlist)
gui_hotlist_free (&gui_hotlist, &last_gui_hotlist, pos_hotlist);
}
/*
* gui_hotlist_print_log: print hotlist in log (usually for crash dump)
*/
void
gui_hotlist_print_log ()
{
struct t_gui_hotlist *ptr_hotlist;
for (ptr_hotlist = gui_hotlist; ptr_hotlist;
ptr_hotlist = ptr_hotlist->next_hotlist)
{
log_printf ("[hotlist (addr:0x%X)]", ptr_hotlist);
log_printf (" priority . . . . . . . : %d", ptr_hotlist->priority);
log_printf (" creation_time. . . . . : tv_sec:%d, tv_usec:%d",
ptr_hotlist->creation_time.tv_sec,
ptr_hotlist->creation_time.tv_usec);
log_printf (" buffer . . . . . . . . : 0x%X", ptr_hotlist->buffer);
log_printf (" prev_hotlist . . . . . : 0x%X", ptr_hotlist->prev_hotlist);
log_printf (" next_hotlist . . . . . : 0x%X", ptr_hotlist->next_hotlist);
}
}
|