about summary refs log tree commit diff stats
path: root/src/tools/autocomplete.c
blob: 438b48b72da2bcc7616cd1ba685437deee0c6aaa (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
/*
 * autocomplete.c
 *
 * Copyright (C) 2012, 2013 James Booth <boothj5@gmail.com>
 *
 * This file is part of Profanity.
 *
 * Profanity 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.
 *
 * Profanity 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 Profanity.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

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

#include "common.h"
#include "tools/autocomplete.h"
#include "tools/parser.h"

struct autocomplete_t {
    GSList *items;
    GSList *last_found;
    gchar *search_str;
};

static gchar * _search_from(Autocomplete ac, GSList *curr);

Autocomplete
autocomplete_new(void)
{
    Autocomplete new = malloc(sizeof(struct autocomplete_t));
    new->items = NULL;
    new->last_found = NULL;
    new->search_str = NULL;

    return new;
}

void
autocomplete_clear(Autocomplete ac)
{
    g_slist_free_full(ac->items, free);
    ac->items = NULL;

    autocomplete_reset(ac);
}

void
autocomplete_reset(Autocomplete ac)
{
    ac->last_found = NULL;
    FREE_SET_NULL(ac->search_str);
}

void
autocomplete_free(Autocomplete ac)
{
    autocomplete_clear(ac);
    free(ac);
}

gint
autocomplete_length(Autocomplete ac)
{
    if (ac == NULL) {
        return 0;
    } else if (ac->items == NULL) {
        return 0;
    } else {
        return g_slist_length(ac->items);
    }
}

gboolean
autocomplete_add(Autocomplete ac, const char *item)
{
    char *item_cpy;
    GSList *curr = g_slist_find_custom(ac->items, item, (GCompareFunc)strcmp);

    // if item already exists
    if (curr != NULL) {
        return FALSE;
    }

    item_cpy = strdup(item);
    ac->items = g_slist_insert_sorted(ac->items, item_cpy, (GCompareFunc)strcmp);
    return TRUE;
}

gboolean
autocomplete_remove(Autocomplete ac, const char * const item)
{
    GSList *curr = g_slist_find_custom(ac->items, item, (GCompareFunc)strcmp);

    if (!curr) {
        return FALSE;
    }

    // reset last found if it points to the item to be removed
    if (ac->last_found == curr) {
        ac->last_found = NULL;
    }

    free(curr->data);
    ac->items = g_slist_delete_link(ac->items, curr);

    return TRUE;
}

GSList *
autocomplete_get_list(Autocomplete ac)
{
    GSList *copy = NULL;
    GSList *curr = ac->items;

    while(curr) {
        copy = g_slist_append(copy, strdup(curr->data));
        curr = g_slist_next(curr);
    }

    return copy;
}

gchar *
autocomplete_complete(Autocomplete ac, gchar *search_str)
{
    gchar *found = NULL;

    // no items to search
    if (!ac->items)
        return NULL;

    // first search attempt
    if (ac->last_found == NULL) {
        ac->search_str =
            (gchar *) malloc((strlen(search_str) + 1) * sizeof(gchar));
        strcpy(ac->search_str, search_str);

        found = _search_from(ac, ac->items);
        return found;

    // subsequent search attempt
    } else {
        // search from here+1 tp end
        found = _search_from(ac, g_slist_next(ac->last_found));
        if (found != NULL)
            return found;

        // search from beginning
        found = _search_from(ac, ac->items);
        if (found != NULL)
            return found;

        // we found nothing, reset search
        autocomplete_reset(ac);
        return NULL;
    }
}

char *
autocomplete_param_with_func(char *input, int *size, char *command,
    autocomplete_func func)
{
    char *found = NULL;
    char *auto_msg = NULL;
    char inp_cpy[*size];
    int i;
    char command_cpy[strlen(command) + 2];
    sprintf(command_cpy, "%s ", command);
    int len = strlen(command_cpy);
    if ((strncmp(input, command_cpy, len) == 0) && (*size > len)) {
        for(i = len; i < *size; i++) {
            inp_cpy[i-len] = input[i];
        }
        inp_cpy[(*size) - len] = '\0';
        found = func(inp_cpy);
        if (found != NULL) {
            auto_msg = (char *) malloc(len + strlen(found) + 1);
            strcpy(auto_msg, command_cpy);
            strcat(auto_msg, found);
            free(found);
        }
    }

    return auto_msg;
}

char *
autocomplete_param_with_ac(char *input, int *size, char *command,
    Autocomplete ac)
{
    char *found = NULL;
    char *auto_msg = NULL;
    char inp_cpy[*size];
    int i;
    char *command_cpy = malloc(strlen(command) + 2);
    sprintf(command_cpy, "%s ", command);
    int len = strlen(command_cpy);
    if ((strncmp(input, command_cpy, len) == 0) && (*size > len)) {
        for(i = len; i < *size; i++) {
            inp_cpy[i-len] = input[i];
        }
        inp_cpy[(*size) - len] = '\0';
        found = autocomplete_complete(ac, inp_cpy);
        if (found != NULL) {
            auto_msg = (char *) malloc(len + strlen(found) + 1);
            strcpy(auto_msg, command_cpy);
            strcat(auto_msg, found);
            free(found);
        }
    }
    free(command_cpy);

    return auto_msg;
}

char *
autocomplete_param_no_with_func(char *input, int *size, char *command,
    int arg_number, autocomplete_func func)
{
    char *result = NULL;
    if (strncmp(input, command, strlen(command)) == 0 && (*size > strlen(command))) {
        int i = 0;
        char *found = NULL;
        GString *result_str = NULL;

        // copy and null terminate input
        gchar inp_cpy[*size];
        for (i = 0; i < *size; i++) {
            inp_cpy[i] = input[i];
        }
        inp_cpy[i] = '\0';
        g_strstrip(inp_cpy);

        // count tokens properly
        int num_tokens = count_tokens(inp_cpy);

        // if correct number of tokens, then candidate for autocompletion of last param
        if (num_tokens == arg_number) {
            gchar *start_str = get_start(inp_cpy, arg_number);
            gchar *comp_str = g_strdup(&inp_cpy[strlen(start_str)]);

            // autocomplete param
            if (comp_str != NULL) {
                found = func(comp_str);
                if (found != NULL) {
                    result_str = g_string_new("");
                    g_string_append(result_str, start_str);
                    g_string_append(result_str, found);
                    result = result_str->str;
                    g_string_free(result_str, FALSE);
                    return result;
                }
            }
        }
    }

    return NULL;
}

static gchar *
_search_from(Autocomplete ac, GSList *curr)
{
    while(curr) {

        // match found
        if (strncmp(curr->data, ac->search_str, strlen(ac->search_str)) == 0) {

            // set pointer to last found
            ac->last_found = curr;

            // if contains space, quote before returning
            if (g_strrstr(curr->data, " ")) {
                GString *quoted = g_string_new("\"");
                g_string_append(quoted, curr->data);
                g_string_append(quoted, "\"");

                gchar *result = quoted->str;
                g_string_free(quoted, FALSE);

                return result;

            // otherwise just return the string
            } else {
                return strdup(curr->data);
            }
        }

        curr = g_slist_next(curr);
    }

    return NULL;
}