summaryrefslogtreecommitdiff
path: root/src/gui/gui-hotlist.c
blob: 56bf5fe7f32ba3cc77718649a9ea11b9f6bae594 (plain)
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
/*
 * gui-hotlist.c - hotlist management (used by all GUI)
 *
 * Copyright (C) 2003-2024 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 <https://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdlib.h>
#include <stddef.h>
#include <string.h>

#include "../core/weechat.h"
#include "../core/core-config.h"
#include "../core/core-eval.h"
#include "../core/core-hashtable.h"
#include "../core/core-hdata.h"
#include "../core/core-hook.h"
#include "../core/core-infolist.h"
#include "../core/core-log.h"
#include "../core/core-string.h"
#include "../core/core-util.h"
#include "../plugins/plugin.h"
#include "gui-hotlist.h"
#include "gui-buffer.h"
#include "gui-color.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;
struct t_hashtable *gui_hotlist_hashtable_add_conditions_pointers = NULL;
struct t_hashtable *gui_hotlist_hashtable_add_conditions_vars = NULL;
struct t_hashtable *gui_hotlist_hashtable_add_conditions_options = NULL;

int gui_add_hotlist = 1;                    /* 0 is for temporarily disable */
                                            /* hotlist add for all buffers  */

char *gui_hotlist_priority_string[GUI_HOTLIST_NUM_PRIORITIES] =
{ "low", "message", "private", "highlight" };


/*
 * Sends signal "hotlist_changed".
 */

void
gui_hotlist_changed_signal (struct t_gui_buffer *buffer)
{
    (void) hook_signal_send ("hotlist_changed",
                             WEECHAT_HOOK_SIGNAL_POINTER, buffer);
}

/*
 * Searches for hotlist priority.
 *
 * Returns index of hotlist priority found, -1 if not found.
 */

int
gui_hotlist_search_priority (const char *priority)
{
    int i;

    if (!priority)
        return -1;

    for (i = 0; i < GUI_HOTLIST_NUM_PRIORITIES; i++)
    {
        if (strcmp (gui_hotlist_priority_string[i], priority) == 0)
            return i;
    }

    return -1;
}

/*
 * Searches for hotlist with buffer pointer.
 *
 * Returns pointer to hotlist found, NULL if not found.
 */

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;
}

/*
 * Duplicates a hotlist element.
 *
 * Returns pointer to new hotlist, NULL if error.
 */

struct t_gui_hotlist *
gui_hotlist_dup (struct t_gui_hotlist *hotlist)
{
    struct t_gui_hotlist *new_hotlist;

    new_hotlist = malloc (sizeof (*new_hotlist));
    if (!new_hotlist)
        return NULL;

    new_hotlist->priority = hotlist->priority;
    memcpy (&(new_hotlist->creation_time), &(hotlist->creation_time),
            sizeof (new_hotlist->creation_time));
    new_hotlist->buffer = hotlist->buffer;
    memcpy (new_hotlist->count, hotlist->count, sizeof (hotlist->count));
    new_hotlist->prev_hotlist = NULL;
    new_hotlist->next_hotlist = NULL;

    return new_hotlist;
}

/*
 * Frees a hotlist and removes 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,
                  int save_removed_hotlist)
{
    struct t_gui_hotlist *new_hotlist;

    if (!ptr_hotlist)
        return;

    if (save_removed_hotlist)
    {
        if (ptr_hotlist->buffer->hotlist_removed)
            free (ptr_hotlist->buffer->hotlist_removed);
        ptr_hotlist->buffer->hotlist_removed = gui_hotlist_dup (ptr_hotlist);
        ptr_hotlist->buffer->hotlist_removed->prev_hotlist = NULL;
        ptr_hotlist->buffer->hotlist_removed->next_hotlist = NULL;
    }

    ptr_hotlist->buffer->hotlist = NULL;

    /* 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;
}

/*
 * Frees 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, 0);
    }
}

/*
 * Checks if a buffer must be added to hotlist, according to its notify level.
 *
 * Returns:
 *   1: buffer must be added to hotlist
 *   0: buffer must not be added to hotlist
 */

int
gui_hotlist_check_buffer_notify (struct t_gui_buffer *buffer,
                                 enum t_gui_hotlist_priority priority)
{
    switch (priority)
    {
        case GUI_HOTLIST_LOW:
            return (buffer->notify >= 3);
        case GUI_HOTLIST_MESSAGE:
            return (buffer->notify >= 2);
        case GUI_HOTLIST_PRIVATE:
        case GUI_HOTLIST_HIGHLIGHT:
            return (buffer->notify >= 1);
        case GUI_HOTLIST_NUM_PRIORITIES:
            /*
             * this constant is used to count hotlist priorities only,
             * it is never used as priority
             */
            break;
    }
    return 1;
}

/*
 * Searches for position of hotlist (to keep hotlist sorted).
 */

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_ENUM(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;
}

/*
 * Adds 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;
    }
}

/*
 * Adds a buffer to hotlist, with priority.
 *
 * If creation_time is NULL, current time is used.
 *
 * Returns pointer to hotlist created or changed, NULL if no hotlist was
 * created/changed.
 */

struct t_gui_hotlist *
gui_hotlist_add (struct t_gui_buffer *buffer,
                 enum t_gui_hotlist_priority priority,
                 struct timeval *creation_time,
                 int check_conditions)
{
    struct t_gui_hotlist *new_hotlist, *ptr_hotlist;
    int i, count[GUI_HOTLIST_NUM_PRIORITIES], rc;
    char *value, str_value[32];

    if (!buffer || !gui_add_hotlist)
        return NULL;

    /* do not add core buffer if upgrading */
    if (weechat_upgrading && (buffer == gui_buffer_search_main ()))
        return NULL;

    if (priority > GUI_HOTLIST_MAX)
        priority = GUI_HOTLIST_MAX;

    /* check if priority is OK according to buffer notify level value */
    if (!gui_hotlist_check_buffer_notify (buffer, priority))
        return NULL;

    if (check_conditions)
    {
        /* create hashtable if needed (to evaluate conditions) */
        if (!gui_hotlist_hashtable_add_conditions_pointers)
        {
            gui_hotlist_hashtable_add_conditions_pointers = hashtable_new (
                32,
                WEECHAT_HASHTABLE_STRING,
                WEECHAT_HASHTABLE_POINTER,
                NULL, NULL);
            if (!gui_hotlist_hashtable_add_conditions_pointers)
                return NULL;
        }
        if (!gui_hotlist_hashtable_add_conditions_vars)
        {
            gui_hotlist_hashtable_add_conditions_vars = hashtable_new (
                32,
                WEECHAT_HASHTABLE_STRING,
                WEECHAT_HASHTABLE_STRING,
                NULL, NULL);
            if (!gui_hotlist_hashtable_add_conditions_vars)
                return NULL;
        }
        if (!gui_hotlist_hashtable_add_conditions_options)
        {
            gui_hotlist_hashtable_add_conditions_options = hashtable_new (
                32,
                WEECHAT_HASHTABLE_STRING,
                WEECHAT_HASHTABLE_STRING,
                NULL, NULL);
            if (!gui_hotlist_hashtable_add_conditions_options)
                return NULL;
            hashtable_set (gui_hotlist_hashtable_add_conditions_options,
                           "type", "condition");
        }

        /* set data in hashtables */
        hashtable_set (gui_hotlist_hashtable_add_conditions_pointers,
                       "window", gui_current_window);
        hashtable_set (gui_hotlist_hashtable_add_conditions_pointers,
                       "buffer", buffer);
        snprintf (str_value, sizeof (str_value), "%d", priority);
        hashtable_set (gui_hotlist_hashtable_add_conditions_vars,
                       "priority", str_value);

        /* check if conditions are true */
        value = eval_expression (
            CONFIG_STRING(config_look_hotlist_add_conditions),
            gui_hotlist_hashtable_add_conditions_pointers,
            gui_hotlist_hashtable_add_conditions_vars,
            gui_hotlist_hashtable_add_conditions_options);
        rc = (value && (strcmp (value, "1") == 0));
        if (value)
            free (value);
        if (!rc)
            return NULL;
    }

    /* init count */
    for (i = 0; i < GUI_HOTLIST_NUM_PRIORITIES; i++)
    {
        count[i] = 0;
    }

    ptr_hotlist = gui_hotlist_search (gui_hotlist, buffer);
    if (ptr_hotlist)
    {
        /* return if priority is greater or equal than the one to add */
        if (ptr_hotlist->priority >= priority)
        {
            ptr_hotlist->count[priority]++;
            gui_hotlist_changed_signal (buffer);
            return ptr_hotlist;
        }

        /*
         * if buffer is present with lower priority: save counts, remove it
         * and go on
         */
        memcpy (count, ptr_hotlist->count, sizeof (ptr_hotlist->count));
        gui_hotlist_free (&gui_hotlist, &last_gui_hotlist, ptr_hotlist, 1);
    }

    new_hotlist = malloc (sizeof (*new_hotlist));
    if (!new_hotlist)
        return NULL;

    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;
    buffer->hotlist = new_hotlist;
    memcpy (new_hotlist->count, count, sizeof (new_hotlist->count));
    new_hotlist->count[priority]++;
    new_hotlist->next_hotlist = NULL;
    new_hotlist->prev_hotlist = NULL;

    gui_hotlist_add_hotlist (&gui_hotlist, &last_gui_hotlist, new_hotlist);

    gui_hotlist_changed_signal (buffer);

    return new_hotlist;
}

/*
 * Restores a hotlist that was removed from a buffer.
 */

void
gui_hotlist_restore_buffer (struct t_gui_buffer *buffer)
{
    struct t_gui_hotlist *ptr_hotlist;

    if (!buffer->hotlist_removed)
        return;

    /* remove hotlist with buffer from list (if found) */
    ptr_hotlist = gui_hotlist_search (gui_hotlist, buffer);
    if (ptr_hotlist)
        gui_hotlist_free (&gui_hotlist, &last_gui_hotlist, ptr_hotlist, 0);

    /* restore the removed hotlist */
    buffer->hotlist_removed->buffer = buffer;
    ptr_hotlist = gui_hotlist_dup (buffer->hotlist_removed);
    gui_hotlist_add_hotlist (&gui_hotlist, &last_gui_hotlist, ptr_hotlist);

    free (buffer->hotlist_removed);
    buffer->hotlist_removed = NULL;

    gui_hotlist_changed_signal (buffer);
}

/*
 * Restores latest hotlist removed in all buffers.
 */

void
gui_hotlist_restore_all_buffers ()
{
    struct t_gui_buffer *ptr_buffer;

    for (ptr_buffer = gui_buffers; ptr_buffer;
         ptr_buffer = ptr_buffer->next_buffer)
    {
        gui_hotlist_restore_buffer (ptr_buffer);
    }
}


/*
 * Resorts 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);
    }

    /* clear whole hotlist */
    gui_hotlist_free_all (&gui_hotlist, &last_gui_hotlist);

    /* switch to new sorted hotlist */
    gui_hotlist = new_hotlist;
    last_gui_hotlist = last_new_hotlist;

    /* reassign hotlist in buffers */
    for (ptr_hotlist = gui_hotlist; ptr_hotlist;
         ptr_hotlist = ptr_hotlist->next_hotlist)
    {
        ptr_hotlist->buffer->hotlist = ptr_hotlist;
    }

    gui_hotlist_changed_signal (NULL);
}

/*
 * Clears hotlist with a level mask (integer).
 *
 * Argument "level_mask" is a combination of:
 *   1 = join/part
 *   2 = message
 *   4 = private
 *   8 = highlight
 *
 * So for example :
 *   1 = clear only join/part
 *   12 = clear only private and highlight
 *   15 = clear whole hotlist
 */

void
gui_hotlist_clear (int level_mask)
{
    struct t_gui_hotlist *ptr_hotlist, *ptr_next_hotlist;
    int hotlist_changed;

    hotlist_changed = 0;

    ptr_hotlist = gui_hotlist;
    while (ptr_hotlist)
    {
        ptr_next_hotlist = ptr_hotlist->next_hotlist;
        if (level_mask & (1 << ptr_hotlist->priority))
        {
            gui_hotlist_free (&gui_hotlist, &last_gui_hotlist, ptr_hotlist, 1);
            hotlist_changed = 1;
        }
        ptr_hotlist = ptr_next_hotlist;
    }

    if (hotlist_changed)
        gui_hotlist_changed_signal (NULL);
}

/*
 * Clears hotlist with a level mask (string).
 */

void
gui_hotlist_clear_level_string (struct t_gui_buffer *buffer,
                                const char *str_level_mask)
{
    long level_mask;
    char *error;
    struct t_gui_hotlist *ptr_hotlist;
    int priority;

    if (str_level_mask)
    {
        if (strcmp (str_level_mask, "lowest") == 0)
        {
            /* clear only lowest priority currently in hotlist */
            priority = GUI_HOTLIST_MAX + 1;
            for (ptr_hotlist = gui_hotlist; ptr_hotlist;
                 ptr_hotlist = ptr_hotlist->next_hotlist)
            {
                if ((int)ptr_hotlist->priority < priority)
                    priority = ptr_hotlist->priority;
            }
            if (priority <= GUI_HOTLIST_MAX)
            {
                gui_hotlist_clear (1 << priority);
                gui_hotlist_initial_buffer = buffer;
            }
        }
        else if (strcmp (str_level_mask, "highest") == 0)
        {
            /* clear only highest priority currently in hotlist */
            priority = GUI_HOTLIST_MIN - 1;
            for (ptr_hotlist = gui_hotlist; ptr_hotlist;
                 ptr_hotlist = ptr_hotlist->next_hotlist)
            {
                if ((int)ptr_hotlist->priority > priority)
                    priority = ptr_hotlist->priority;
            }
            if (priority >= GUI_HOTLIST_MIN)
            {
                gui_hotlist_clear (1 << priority);
                gui_hotlist_initial_buffer = buffer;
            }
        }
        else
        {
            /* clear hotlist using a mask of levels */
            error = NULL;
            level_mask = strtol (str_level_mask, &error, 10);
            if (error && !error[0] && (level_mask > 0))
            {
                gui_hotlist_clear ((int)level_mask);
                gui_hotlist_initial_buffer = buffer;
            }
        }
    }
    else
    {
        gui_hotlist_clear (GUI_HOTLIST_MASK_MAX);
        gui_hotlist_initial_buffer = buffer;
    }
}

/*
 * Removes a buffer from hotlist.
 */

void
gui_hotlist_remove_buffer (struct t_gui_buffer *buffer,
                           int force_remove_buffer)
{
    int hotlist_changed, hotlist_remove, buffer_to_remove;
    struct t_gui_hotlist *ptr_hotlist, *next_hotlist;

    if (!buffer || weechat_upgrading)
        return;

    hotlist_changed = 0;

    hotlist_remove = CONFIG_ENUM(config_look_hotlist_remove);

    ptr_hotlist = gui_hotlist;
    while (ptr_hotlist)
    {
        next_hotlist = ptr_hotlist->next_hotlist;

        buffer_to_remove = (force_remove_buffer) ?
            (ptr_hotlist->buffer == buffer) : 0;

        switch (hotlist_remove)
        {
            case CONFIG_LOOK_HOTLIST_REMOVE_BUFFER:
                buffer_to_remove |= (ptr_hotlist->buffer == buffer);
                break;
            case CONFIG_LOOK_HOTLIST_REMOVE_MERGED:
                buffer_to_remove |=
                    ((ptr_hotlist->buffer->number == buffer->number)
                     && (!ptr_hotlist->buffer->zoomed
                         || (ptr_hotlist->buffer->active == 2)));
                break;
        }

        if (buffer_to_remove)
        {
            gui_hotlist_free (&gui_hotlist, &last_gui_hotlist, ptr_hotlist, 1);
            hotlist_changed = 1;
        }

        ptr_hotlist = next_hotlist;
    }

    if (hotlist_changed)
        gui_hotlist_changed_signal (NULL);
}

/*
 * Returns hdata for hotlist.
 */

struct t_hdata *
gui_hotlist_hdata_hotlist_cb (const void *pointer, void *data,
                              const char *hdata_name)
{
    struct t_hdata *hdata;

    /* make C compiler happy */
    (void) pointer;
    (void) data;

    hdata = hdata_new (NULL, hdata_name, "prev_hotlist", "next_hotlist",
                       0, 0, NULL, NULL);
    if (hdata)
    {
        HDATA_VAR(struct t_gui_hotlist, priority, INTEGER, 0, NULL, NULL);
        HDATA_VAR(struct t_gui_hotlist, creation_time.tv_sec, TIME, 0, NULL, NULL);
        HDATA_VAR(struct t_gui_hotlist, creation_time.tv_usec, LONG, 0, NULL, NULL);
        HDATA_VAR(struct t_gui_hotlist, buffer, POINTER, 0, NULL, NULL);
        HDATA_VAR(struct t_gui_hotlist, count, INTEGER, 0, GUI_HOTLIST_NUM_PRIORITIES_STR, NULL);
        HDATA_VAR(struct t_gui_hotlist, prev_hotlist, POINTER, 0, NULL, hdata_name);
        HDATA_VAR(struct t_gui_hotlist, next_hotlist, POINTER, 0, NULL, hdata_name);
        HDATA_LIST(gui_hotlist, WEECHAT_HDATA_LIST_CHECK_POINTERS);
        HDATA_LIST(last_gui_hotlist, 0);
    }
    return hdata;
}

/*
 * Adds a hotlist in an infolist.
 *
 * Returns:
 *   1: OK
 *   0: error
 */

int
gui_hotlist_add_to_infolist (struct t_infolist *infolist,
                             struct t_gui_hotlist *hotlist)
{
    struct t_infolist_item *ptr_item;
    int i;
    char option_name[64];

    if (!infolist || !hotlist)
        return 0;

    ptr_item = infolist_new_item (infolist);
    if (!ptr_item)
        return 0;

    if (!infolist_new_var_integer (ptr_item, "priority", hotlist->priority))
        return 0;
    switch (hotlist->priority)
    {
        case GUI_HOTLIST_LOW:
            if (!infolist_new_var_string (ptr_item, "color",
                                          gui_color_get_name (CONFIG_COLOR(config_color_status_data_other))))
                return 0;
            break;
        case GUI_HOTLIST_MESSAGE:
            if (!infolist_new_var_string (ptr_item, "color",
                                          gui_color_get_name (CONFIG_COLOR(config_color_status_data_msg))))
                return 0;
            break;
        case GUI_HOTLIST_PRIVATE:
            if (!infolist_new_var_string (ptr_item, "color",
                                          gui_color_get_name (CONFIG_COLOR(config_color_status_data_private))))
                return 0;
            break;
        case GUI_HOTLIST_HIGHLIGHT:
            if (!infolist_new_var_string (ptr_item, "color",
                                          gui_color_get_name (CONFIG_COLOR(config_color_status_data_highlight))))
                return 0;
            break;
        case GUI_HOTLIST_NUM_PRIORITIES:
            /*
             * this constant is used to count hotlist priorities only,
             * it is never used as priority
             */
            break;
    }
    if (!infolist_new_var_buffer (ptr_item, "creation_time", &(hotlist->creation_time), sizeof (struct timeval)))
        return 0;
    if (!infolist_new_var_pointer (ptr_item, "buffer_pointer", hotlist->buffer))
        return 0;
    if (!infolist_new_var_integer (ptr_item, "buffer_number", hotlist->buffer->number))
        return 0;
    if (!infolist_new_var_string (ptr_item, "plugin_name", gui_buffer_get_plugin_name (hotlist->buffer)))
        return 0;
    if (!infolist_new_var_string (ptr_item, "buffer_name", hotlist->buffer->name))
        return 0;
    for (i = 0; i < GUI_HOTLIST_NUM_PRIORITIES; i++)
    {
        snprintf (option_name, sizeof (option_name), "count_%02d", i);
        if (!infolist_new_var_integer (ptr_item, option_name, hotlist->count[i]))
            return 0;
    }

    return 1;
}

/*
 * Prints hotlist in WeeChat log file (usually for crash dump).
 */

void
gui_hotlist_print_log ()
{
    struct t_gui_hotlist *ptr_hotlist;
    int i;

    for (ptr_hotlist = gui_hotlist; ptr_hotlist;
         ptr_hotlist = ptr_hotlist->next_hotlist)
    {
        log_printf ("[hotlist (addr:0x%lx)]", ptr_hotlist);
        log_printf ("  priority . . . . . . . : %d",    ptr_hotlist->priority);
        log_printf ("  creation_time. . . . . : tv_sec:%ld, tv_usec:%ld",
                    ptr_hotlist->creation_time.tv_sec,
                    ptr_hotlist->creation_time.tv_usec);
        log_printf ("  buffer . . . . . . . . : 0x%lx", ptr_hotlist->buffer);
        for (i = 0; i < GUI_HOTLIST_NUM_PRIORITIES; i++)
        {
            log_printf ("  count[%02d]. . . . . . . : %d", i, ptr_hotlist->count[i]);
        }
        log_printf ("  prev_hotlist . . . . . : 0x%lx", ptr_hotlist->prev_hotlist);
        log_printf ("  next_hotlist . . . . . : 0x%lx", ptr_hotlist->next_hotlist);
    }
}

/*
 * Ends hotlist.
 */

void
gui_hotlist_end ()
{
    if (gui_hotlist_hashtable_add_conditions_pointers)
    {
        hashtable_free (gui_hotlist_hashtable_add_conditions_pointers);
        gui_hotlist_hashtable_add_conditions_pointers = NULL;
    }
    if (gui_hotlist_hashtable_add_conditions_vars)
    {
        hashtable_free (gui_hotlist_hashtable_add_conditions_vars);
        gui_hotlist_hashtable_add_conditions_vars = NULL;
    }
    if (gui_hotlist_hashtable_add_conditions_options)
    {
        hashtable_free (gui_hotlist_hashtable_add_conditions_options);
        gui_hotlist_hashtable_add_conditions_options = NULL;
    }
}