summaryrefslogtreecommitdiffstats
path: root/src/plugins/buflist/buflist-config.c
blob: f187bcfbba20fa306a58400a5e3e15b8123c7b1e (plain) (blame)
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
/*
 * buflist-config.c - buflist configuration options (file buflist.conf)
 *
 * Copyright (C) 2003-2021 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/>.
 */

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

#include "../weechat-plugin.h"
#include "buflist.h"
#include "buflist-config.h"
#include "buflist-bar-item.h"


struct t_config_file *buflist_config_file = NULL;

/* buflist config, look section */

struct t_config_option *buflist_config_look_add_newline;
struct t_config_option *buflist_config_look_auto_scroll;
struct t_config_option *buflist_config_look_display_conditions;
struct t_config_option *buflist_config_look_enabled;
struct t_config_option *buflist_config_look_mouse_jump_visited_buffer;
struct t_config_option *buflist_config_look_mouse_move_buffer;
struct t_config_option *buflist_config_look_mouse_wheel;
struct t_config_option *buflist_config_look_nick_prefix;
struct t_config_option *buflist_config_look_nick_prefix_empty;
struct t_config_option *buflist_config_look_signals_refresh;
struct t_config_option *buflist_config_look_sort;

/* buflist config, format section */

struct t_config_option *buflist_config_format_buffer;
struct t_config_option *buflist_config_format_buffer_current;
struct t_config_option *buflist_config_format_hotlist;
struct t_config_option *buflist_config_format_hotlist_level[4];
struct t_config_option *buflist_config_format_hotlist_level_none;
struct t_config_option *buflist_config_format_hotlist_separator;
struct t_config_option *buflist_config_format_indent;
struct t_config_option *buflist_config_format_lag;
struct t_config_option *buflist_config_format_name;
struct t_config_option *buflist_config_format_nick_prefix;
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[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;


/*
 * Frees the signals hooked for refresh.
 */

void
buflist_config_free_signals_refresh ()
{
    int i;

    if (!buflist_config_signals_refresh)
        return;

    for (i = 0; i < buflist_config_num_signals_refresh; i++)
    {
        weechat_unhook (buflist_config_signals_refresh[i]);
    }

    free (buflist_config_signals_refresh);
    buflist_config_signals_refresh = NULL;

    buflist_config_num_signals_refresh = 0;
}

/*
 * Compares two signals to add them in the sorted arraylist.
 *
 * Returns:
 *   -1: signal1 < signal2
 *    0: signal1 == signal2
 *    1: signal1 > signal2
 */

int
buflist_config_compare_signals (void *data, struct t_arraylist *arraylist,
                                void *pointer1, void *pointer2)
{
    /* make C compiler happy */
    (void) data;
    (void) arraylist;

    return strcmp ((const char *)pointer1, (const char *)pointer2);
}

/*
 * Callback for a signal on a buffer.
 */

int
buflist_config_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;

    buflist_bar_item_update (0);

    return WEECHAT_RC_OK;
}

/*
 * Hooks the signals for refresh.
 */

void
buflist_config_hook_signals_refresh ()
{
    char **all_signals, **signals;
    const char *ptr_signals_refresh;
    struct t_arraylist *signals_list;
    int count, list_size, i;

    all_signals = weechat_string_dyn_alloc (256);
    if (!all_signals)
        return;

    ptr_signals_refresh = weechat_config_string (
        buflist_config_look_signals_refresh);

    weechat_string_dyn_concat (all_signals, BUFLIST_CONFIG_SIGNALS_REFRESH, -1);
    if (ptr_signals_refresh && ptr_signals_refresh[0])
    {
        weechat_string_dyn_concat (all_signals, ",", -1);
        weechat_string_dyn_concat (all_signals, ptr_signals_refresh, -1);
    }
    if (weechat_config_boolean (buflist_config_look_nick_prefix))
    {
        weechat_string_dyn_concat (all_signals, ",", -1);
        weechat_string_dyn_concat (
            all_signals,
            BUFLIST_CONFIG_SIGNALS_REFRESH_NICK_PREFIX,
            -1);
    }

    signals = weechat_string_split (*all_signals, ",", NULL,
                                    WEECHAT_STRING_SPLIT_STRIP_LEFT
                                    | WEECHAT_STRING_SPLIT_STRIP_RIGHT
                                    | WEECHAT_STRING_SPLIT_COLLAPSE_SEPS,
                                    0, &count);
    if (signals)
    {
        signals_list = weechat_arraylist_new (
            32, 1, 0,
            &buflist_config_compare_signals,
            NULL, NULL, NULL);
        if (signals_list)
        {
            for (i = 0; i < count; i++)
            {
                weechat_arraylist_add (signals_list, signals[i]);
            }
            list_size = weechat_arraylist_size (signals_list);
            buflist_config_signals_refresh = malloc (
                list_size * sizeof (*buflist_config_signals_refresh));
            if (buflist_config_signals_refresh)
            {
                buflist_config_num_signals_refresh = list_size;
                for (i = 0; i < list_size; i++)
                {
                    buflist_config_signals_refresh[i] = weechat_hook_signal (
                        weechat_arraylist_get (signals_list, i),
                        &buflist_config_signal_buffer_cb, NULL, NULL);
                }
                if (weechat_buflist_plugin->debug >= 1)
                {
                    weechat_printf (NULL, _("%s: %d signals hooked"),
                                    BUFLIST_PLUGIN_NAME, list_size);
                }
            }
            weechat_arraylist_free (signals_list);
        }
        weechat_string_free_split (signals);
    }

    weechat_string_dyn_free (all_signals, 1);
}

/*
 * Callback for changes on option "buflist.look.enabled".
 */

void
buflist_config_change_enabled (const void *pointer, void *data,
                               struct t_config_option *option)
{
    /* make C compiler happy */
    (void) pointer;
    (void) data;
    (void) option;

    buflist_config_free_signals_refresh ();

    if (weechat_config_boolean (buflist_config_look_enabled))
    {
        /* buflist enabled */
        buflist_config_hook_signals_refresh ();
        weechat_command (NULL, "/mute /bar show buflist");
        buflist_bar_item_update (0);
    }
    else
    {
        /* buflist disabled */
        weechat_command (NULL, "/mute /bar hide buflist");
        buflist_bar_item_update (1);
    }
}

/*
 * Callback for changes on option "buflist.look.sort".
 */

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;

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

    hashtable_pointers = weechat_hashtable_new (
        32,
        WEECHAT_HASHTABLE_STRING,
        WEECHAT_HASHTABLE_POINTER,
        NULL,
        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);
}

/*
 * Callback for changes on option "buflist.look.signals_refresh".
 */

void
buflist_config_change_signals_refresh (const void *pointer, void *data,
                                       struct t_config_option *option)
{
    /* make C compiler happy */
    (void) pointer;
    (void) data;
    (void) option;

    buflist_config_free_signals_refresh ();
    buflist_config_hook_signals_refresh ();
}

/*
 * Callback for changes on option "buflist.look.nick_prefix".
 */

void
buflist_config_change_nick_prefix (const void *pointer, void *data,
                                   struct t_config_option *option)
{
    /* make C compiler happy */
    (void) pointer;
    (void) data;
    (void) option;

    buflist_config_change_signals_refresh (NULL, NULL, NULL);
    buflist_bar_item_update (0);
}

/*
 * Callback for changes on options needing bar item refresh.
 */

void
buflist_config_change_buflist (const void *pointer, void *data,
                               struct t_config_option *option)
{
    /* make C compiler happy */
    (void) pointer;
    (void) data;
    (void) option;

    buflist_bar_item_update (0);
}

/*
 * Replace formats like ${format_xxx} by evaluated form: ${eval:${format_xxx}}.
 *
 * Note: result must be freed after use.
 */

char *
buflist_config_add_eval_for_formats (const char *string)
{
    char *formats[] = { "format_buffer", "format_number", "indent",
                        "format_nick_prefix", "format_name",
                        "format_hotlist", "hotlist", "format_lag",
                        "color_hotlist", NULL };
    char *result, *tmp, format[512], format_eval[512];
    int i;

    result = strdup (string);
    for (i = 0; formats[i]; i++)
    {
        snprintf (format, sizeof (format),
                  "${%s}", formats[i]);
        snprintf (format_eval, sizeof (format_eval),
                  "${eval:${%s}}", formats[i]);
        tmp = weechat_string_replace (result, format, format_eval);
        free (result);
        result = tmp;
    }
    return result;
}

/*
 * Callback for changes on some format options.
 */

void
buflist_config_change_format (const void *pointer, void *data,
                              struct t_config_option *option)
{
    /* make C compiler happy */
    (void) pointer;
    (void) data;
    (void) option;

    if (buflist_config_format_buffer_eval)
        free (buflist_config_format_buffer_eval);
    buflist_config_format_buffer_eval = buflist_config_add_eval_for_formats (
        weechat_config_string (buflist_config_format_buffer));

    if (buflist_config_format_buffer_current_eval)
        free (buflist_config_format_buffer_current_eval);
    buflist_config_format_buffer_current_eval = buflist_config_add_eval_for_formats (
        weechat_config_string (buflist_config_format_buffer_current));

    if (buflist_config_format_hotlist_eval)
        free (buflist_config_format_hotlist_eval);
    buflist_config_format_hotlist_eval = buflist_config_add_eval_for_formats (
        weechat_config_string (buflist_config_format_hotlist));

    buflist_bar_item_update (0);
}

/*
 * Initializes buflist configuration file.
 *
 * Returns:
 *   1: OK
 *   0: error
 */

int
buflist_config_init ()
{
    struct t_config_section *ptr_section;

    buflist_config_file = weechat_config_new (BUFLIST_CONFIG_NAME,
                                              NULL, NULL, NULL);
    if (!buflist_config_file)
        return 0;

    /* look */
    ptr_section = weechat_config_new_section (buflist_config_file, "look",
                                              0, 0,
                                              NULL, NULL, NULL,
                                              NULL, NULL, NULL,
                                              NULL, NULL, NULL,
                                              NULL, NULL, NULL,
                                              NULL, NULL, NULL);
    if (!ptr_section)
    {
        weechat_config_free (buflist_config_file);
        buflist_config_file = NULL;
        return 0;
    }

    buflist_config_look_add_newline = weechat_config_new_option (
        buflist_config_file, ptr_section,
        "add_newline", "boolean",
        N_("add newline between the buffers displayed, so each buffer is "
           "displayed on a separate line (recommended); if disabled, newlines "
           "must be manually added in the formats with \"${\\n}\", "
           "and the mouse actions are not possible any more"),
        NULL, 0, 0, "on", NULL, 0,
        NULL, NULL, NULL,
        &buflist_config_change_buflist, NULL, NULL,
        NULL, NULL, NULL);
    buflist_config_look_auto_scroll = weechat_config_new_option (
        buflist_config_file, ptr_section,
        "auto_scroll", "integer",
        N_("automatically scroll the buflist bar to always see the current "
           "buffer (this works only with a bar on the left/right position "
           "with a \"vertical\" filling); this value is the percent number "
           "of lines displayed before the current buffer when scrolling "
           "(-1 = disable scroll); for example 50 means that after a scroll, "
           "the current buffer is at the middle of bar, 0 means on top of "
           "bar, 100 means at bottom of bar"),
        NULL, -1, 100, "50", NULL, 0,
        NULL, NULL, NULL,
        NULL, NULL, NULL,
        NULL, NULL, NULL);
    buflist_config_look_display_conditions = weechat_config_new_option (
        buflist_config_file, ptr_section,
        "display_conditions", "string",
        N_("conditions to display a buffer "
           "(note: content is evaluated, see /help buflist); for example "
           "to hide server buffers if they are merged with core buffer: "
           "\"${buffer.hidden}==0 && ((${type}!=server && "
           "${buffer.full_name}!=core.weechat) || ${buffer.active}==1)\""),
        NULL, 0, 0, "${buffer.hidden}==0", NULL, 0,
        NULL, NULL, NULL,
        &buflist_config_change_buflist, NULL, NULL,
        NULL, NULL, NULL);
    buflist_config_look_enabled = weechat_config_new_option (
        buflist_config_file, ptr_section,
        "enabled", "boolean",
        N_("enable buflist"),
        NULL, 0, 0, "on", NULL, 0,
        NULL, NULL, NULL,
        &buflist_config_change_enabled, NULL, NULL,
        NULL, NULL, NULL);
    buflist_config_look_mouse_jump_visited_buffer = weechat_config_new_option (
        buflist_config_file, ptr_section,
        "mouse_jump_visited_buffer", "boolean",
        N_("if enabled, clicks with left/right buttons on the line with "
           "current buffer jump to previous/next visited buffer"),
        NULL, 0, 0, "off", NULL, 0,
        NULL, NULL, NULL,
        NULL, NULL, NULL,
        NULL, NULL, NULL);
    buflist_config_look_mouse_move_buffer = weechat_config_new_option (
        buflist_config_file, ptr_section,
        "mouse_move_buffer", "boolean",
        N_("if enabled, mouse gestures (drag & drop) move buffers in list"),
        NULL, 0, 0, "on", NULL, 0,
        NULL, NULL, NULL,
        NULL, NULL, NULL,
        NULL, NULL, NULL);
    buflist_config_look_mouse_wheel = weechat_config_new_option (
        buflist_config_file, ptr_section,
        "mouse_wheel", "boolean",
        N_("if enabled, mouse wheel up/down actions jump to previous/next "
           "buffer in list"),
        NULL, 0, 0, "on", NULL, 0,
        NULL, NULL, NULL,
        NULL, NULL, NULL,
        NULL, NULL, NULL);
    buflist_config_look_nick_prefix = weechat_config_new_option (
        buflist_config_file, ptr_section,
        "nick_prefix", "boolean",
        N_("get the nick prefix and its color from nicklist so that "
           "${nick_prefix} can be used in format; this can be slow on buffers "
           "with lot of nicks in nicklist, so this option is disabled "
           "by default"),
        NULL, 0, 0, "off", NULL, 0,
        NULL, NULL, NULL,
        &buflist_config_change_nick_prefix, NULL, NULL,
        NULL, NULL, NULL);
    buflist_config_look_nick_prefix_empty = weechat_config_new_option (
        buflist_config_file, ptr_section,
        "nick_prefix_empty", "boolean",
        N_("when the nick prefix is enabled, display a space instead if there "
           "is no nick prefix on the buffer"),
        NULL, 0, 0, "on", NULL, 0,
        NULL, NULL, NULL,
        &buflist_config_change_buflist, NULL, NULL,
        NULL, NULL, NULL);
    buflist_config_look_signals_refresh = weechat_config_new_option (
        buflist_config_file, ptr_section,
        "signals_refresh", "string",
        N_("comma-separated list of extra signals that are hooked and trigger "
           "the refresh of buffers list; this can be useful if some custom "
           "variables are used in formats and need specific refresh"),
        NULL, 0, 0, "", NULL, 0,
        NULL, NULL, NULL,
        &buflist_config_change_signals_refresh, NULL, NULL,
        NULL, NULL, NULL);
    buflist_config_look_sort = weechat_config_new_option (
        buflist_config_file, ptr_section,
        "sort", "string",
        N_("comma-separated list of fields to sort buffers; each field is "
           "a hdata variable of buffer (\"var\"), a hdata variable of "
           "IRC server (\"irc_server.var\") or a hdata variable of "
           "IRC channel (\"irc_channel.var\"); "
           "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 "
           "(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,
        NULL, NULL, NULL);

    /* format */
    ptr_section = weechat_config_new_section (buflist_config_file, "format",
                                              0, 0,
                                              NULL, NULL, NULL,
                                              NULL, NULL, NULL,
                                              NULL, NULL, NULL,
                                              NULL, NULL, NULL,
                                              NULL, NULL, NULL);
    if (!ptr_section)
    {
        weechat_config_free (buflist_config_file);
        buflist_config_file = NULL;
        return 0;
    }

    buflist_config_format_buffer = weechat_config_new_option (
        buflist_config_file, ptr_section,
        "buffer", "string",
        N_("format of each line with a buffer "
           "(note: content is evaluated, see /help buflist); "
           "example: standard format for bar item \"buflist\" and only the "
           "buffer number between square brackets for other bar items "
           "(\"buflist2\" and \"buflist3\"): "
           "\"${if:${bar_item.name}==buflist?${format_number}${indent}"
           "${format_nick_prefix}${color_hotlist}${format_name}:"
           "[${number}]}\""),
        NULL, 0, 0,
        "${format_number}${indent}${format_nick_prefix}${color_hotlist}"
        "${format_name}",
        NULL, 0,
        NULL, NULL, NULL,
        &buflist_config_change_format, NULL, NULL,
        NULL, NULL, NULL);
    buflist_config_format_buffer_current = weechat_config_new_option (
        buflist_config_file, ptr_section,
        "buffer_current", "string",
        N_("format for the line with current buffer "
           "(note: content is evaluated, see /help buflist)"),
        NULL, 0, 0, "${color:,blue}${format_buffer}", NULL, 0,
        NULL, NULL, NULL,
        &buflist_config_change_format, NULL, NULL,
        NULL, NULL, NULL);
    buflist_config_format_hotlist = weechat_config_new_option (
        buflist_config_file, ptr_section,
        "hotlist", "string",
        N_("format for hotlist "
           "(note: content is evaluated, see /help buflist)"),
        NULL, 0, 0,
        " ${color:green}(${hotlist}${color:green})",
        NULL, 0,
        NULL, NULL, NULL,
        &buflist_config_change_format, NULL, NULL,
        NULL, NULL, NULL);
    buflist_config_format_hotlist_level[3] = weechat_config_new_option (
        buflist_config_file, ptr_section,
        "hotlist_highlight", "string",
        N_("format for a buffer with hotlist level \"highlight\" "
           "(note: content is evaluated, see /help buflist)"),
        NULL, 0, 0, "${color:magenta}", NULL, 0,
        NULL, NULL, NULL,
        &buflist_config_change_buflist, NULL, NULL,
        NULL, NULL, NULL);
    buflist_config_format_hotlist_level[0] = weechat_config_new_option (
        buflist_config_file, ptr_section,
        "hotlist_low", "string",
        N_("format for a buffer with hotlist level \"low\" "
           "(note: content is evaluated, see /help buflist)"),
        NULL, 0, 0, "${color:white}", NULL, 0,
        NULL, NULL, NULL,
        &buflist_config_change_buflist, NULL, NULL,
        NULL, NULL, NULL);
    buflist_config_format_hotlist_level[1] = weechat_config_new_option (
        buflist_config_file, ptr_section,
        "hotlist_message", "string",
        N_("format for a buffer with hotlist level \"message\" "
           "(note: content is evaluated, see /help buflist)"),
        NULL, 0, 0, "${color:brown}", NULL, 0,
        NULL, NULL, NULL,
        &buflist_config_change_buflist, NULL, NULL,
        NULL, NULL, NULL);
    buflist_config_format_hotlist_level_none = weechat_config_new_option (
        buflist_config_file, ptr_section,
        "hotlist_none", "string",
        N_("format for a buffer not in hotlist "
           "(note: content is evaluated, see /help buflist)"),
        NULL, 0, 0, "${color:default}", NULL, 0,
        NULL, NULL, NULL,
        &buflist_config_change_buflist, NULL, NULL,
        NULL, NULL, NULL);
    buflist_config_format_hotlist_level[2] = weechat_config_new_option (
        buflist_config_file, ptr_section,
        "hotlist_private", "string",
        N_("format for a buffer with hotlist level \"private\" "
           "(note: content is evaluated, see /help buflist)"),
        NULL, 0, 0, "${color:green}", NULL, 0,
        NULL, NULL, NULL,
        &buflist_config_change_buflist, NULL, NULL,
        NULL, NULL, NULL);
    buflist_config_format_hotlist_separator = weechat_config_new_option (
        buflist_config_file, ptr_section,
        "hotlist_separator", "string",
        N_("separator for counts in hotlist "
           "(note: content is evaluated, see /help buflist)"),
        NULL, 0, 0, "${color:default},", NULL, 0,
        NULL, NULL, NULL,
        &buflist_config_change_buflist, NULL, NULL,
        NULL, NULL, NULL);
    buflist_config_format_indent = weechat_config_new_option (
        buflist_config_file, ptr_section,
        "indent", "string",
        N_("string displayed to indent channel and private buffers "
           "(note: content is evaluated, see /help buflist)"),
        NULL, 0, 0, "  ", NULL, 0,
        NULL, NULL, NULL,
        &buflist_config_change_buflist, NULL, NULL,
        NULL, NULL, NULL);
    buflist_config_format_lag = weechat_config_new_option (
        buflist_config_file, ptr_section,
        "lag", "string",
        N_("format for lag on an IRC server buffer "
           "(note: content is evaluated, see /help buflist)"),
        NULL, 0, 0,
        " ${color:green}[${color:brown}${lag}${color:green}]",
        NULL, 0,
        NULL, NULL, NULL,
        &buflist_config_change_buflist, NULL, NULL,
        NULL, NULL, NULL);
    buflist_config_format_name = weechat_config_new_option (
        buflist_config_file, ptr_section,
        "name", "string",
        N_("format for buffer name "
           "(note: content is evaluated, see /help buflist)"),
        NULL, 0, 0, "${name}", NULL, 0,
        NULL, NULL, NULL,
        &buflist_config_change_buflist, NULL, NULL,
        NULL, NULL, NULL);
    buflist_config_format_nick_prefix = weechat_config_new_option (
        buflist_config_file, ptr_section,
        "nick_prefix", "string",
        N_("format for nick prefix on a channel "
           "(note: content is evaluated, see /help buflist)"),
        NULL, 0, 0, "${color_nick_prefix}${nick_prefix}", NULL, 0,
        NULL, NULL, NULL,
        &buflist_config_change_buflist, NULL, NULL,
        NULL, NULL, NULL);
    buflist_config_format_number = weechat_config_new_option (
        buflist_config_file, ptr_section,
        "number", "string",
        N_("format for buffer number, ${number} is the indented number "
           "(note: content is evaluated, see /help buflist)"),
        NULL, 0, 0,
        "${color:green}${number}${if:${number_displayed}?.: }",
        NULL, 0,
        NULL, NULL, NULL,
        &buflist_config_change_buflist, NULL, NULL,
        NULL, NULL, NULL);

    return 1;
}

/*
 * Reads buflist configuration file.
 */

int
buflist_config_read ()
{
    int rc;

    rc = weechat_config_read (buflist_config_file);

    if (rc == WEECHAT_CONFIG_READ_OK)
    {
        buflist_config_change_sort (NULL, NULL, NULL);
        buflist_config_change_signals_refresh (NULL, NULL, NULL);
        buflist_config_change_format (NULL, NULL, NULL);
    }

    return rc;
}

/*
 * Writes buflist configuration file.
 */

int
buflist_config_write ()
{
    return weechat_config_write (buflist_config_file);
}

/*
 * Frees buflist configuration.
 */

void
buflist_config_free ()
{
    int i;

    weechat_config_free (buflist_config_file);

    if (buflist_config_signals_refresh)
        buflist_config_free_signals_refresh ();

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

    if (buflist_config_format_buffer_eval)
        free (buflist_config_format_buffer_eval);
    if (buflist_config_format_buffer_current_eval)
        free (buflist_config_format_buffer_current_eval);
    if (buflist_config_format_hotlist_eval)
        free (buflist_config_format_hotlist_eval);
}