summaryrefslogtreecommitdiff
path: root/src/plugins/fset/fset-completion.c
blob: efb634cad91656042674665cc8e45b1c64c19736 (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
/*
 * fset-completion.c - completion for Fast Set commands
 *
 * Copyright (C) 2003-2020 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 "../weechat-plugin.h"
#include "fset.h"


/*
 * Adds current server to completion list.
 */

int
fset_completion_option_cb (const void *pointer, void *data,
                           const char *completion_item,
                           struct t_gui_buffer *buffer,
                           struct t_gui_completion *completion)
{
    struct t_config_file *ptr_config;
    struct t_config_section *ptr_section;
    struct t_config_option *ptr_option;
    char **words;
    int config_section_added, num_words, i;

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

    ptr_config = weechat_hdata_get_list (fset_hdata_config_file,
                                         "config_files");
    while (ptr_config)
    {
        ptr_section = weechat_hdata_pointer (fset_hdata_config_file,
                                             ptr_config, "sections");
        while (ptr_section)
        {
            config_section_added = 0;
            ptr_option = weechat_hdata_pointer (fset_hdata_config_section,
                                                ptr_section, "options");
            while (ptr_option)
            {
                if (!config_section_added)
                {
                    weechat_hook_completion_list_add (
                        completion,
                        weechat_config_option_get_string (ptr_option,
                                                          "config_name"),
                        0, WEECHAT_LIST_POS_SORT);
                    weechat_hook_completion_list_add (
                        completion,
                        weechat_config_option_get_string (ptr_option,
                                                          "section_name"),
                        0, WEECHAT_LIST_POS_SORT);
                    config_section_added = 1;
                }
                weechat_hook_completion_list_add (
                    completion,
                    weechat_config_option_get_string (ptr_option, "name"),
                    0,
                    WEECHAT_LIST_POS_SORT);
                words = weechat_string_split (
                    weechat_config_option_get_string (ptr_option, "name"),
                    "_",
                    NULL,
                    WEECHAT_STRING_SPLIT_STRIP_LEFT
                    | WEECHAT_STRING_SPLIT_STRIP_RIGHT
                    | WEECHAT_STRING_SPLIT_COLLAPSE_SEPS,
                    0,
                    &num_words);
                if (words && (num_words > 1))
                {
                    for (i = 0; i < num_words; i++)
                    {
                        weechat_hook_completion_list_add (
                            completion, words[i], 0, WEECHAT_LIST_POS_SORT);
                    }
                }
                if (words)
                    weechat_string_free_split (words);
                ptr_option = weechat_hdata_move (fset_hdata_config_option,
                                                 ptr_option, 1);
            }
            ptr_section = weechat_hdata_move (fset_hdata_config_section,
                                              ptr_section, 1);
        }
        ptr_config = weechat_hdata_move (fset_hdata_config_file,
                                         ptr_config, 1);
    }

    return WEECHAT_RC_OK;
}

/*
 * Hooks completions.
 */

void
fset_completion_init ()
{
    weechat_hook_completion ("fset_options",
                             N_("configuration files, sections, options and "
                                "words of options"),
                             &fset_completion_option_cb, NULL, NULL);
}