about summary refs log tree commit diff stats
path: root/src/tools/autocomplete.c
blob: 275320810a81db87d4661b57966c2a065eb5882b (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
/*
 * autocomplete.c
 * vim: expandtab:ts=4:sts=4:sw=4
 *
 * Copyright (C) 2012 - 2019 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 <https://www.gnu.org/licenses/>.
 *
 * In addition, as a special exception, the copyright holders give permission to
 * link the code of portions of this program with the OpenSSL library under
 * certain conditions as described in each individual source file, and
 * distribute linked combinations including the two.
 *
 * You must obey the GNU General Public License in all respects for all of the
 * code used other than OpenSSL. If you modify file(s) with this exception, you
 * may extend this exception to your version of the file(s), but you are not
 * obligated to do so. If you do not wish to do so, delete this exception
 * statement from your version. If you delete this exception statement from all
 * source files in the program, then also delete it here.
 *
 */

#include "config.h"

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

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

typedef enum {
    PREVIOUS,
    NEXT
} search_direction;

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

static gchar* _search(Autocomplete ac, GList* curr, gboolean quote, search_direction direction);

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)
{
    if (ac) {
        if (ac->items) {
            g_list_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)
{
    if (ac) {
        autocomplete_clear(ac);
        free(ac);
    }
}

gint
autocomplete_length(Autocomplete ac)
{
    if (!ac) {
        return 0;
    } else if (!ac->items) {
        return 0;
    } else {
        return g_list_length(ac->items);
    }
}

void
autocomplete_update(Autocomplete ac, char** items)
{
    gchar* last_found = NULL;
    gchar* search_str = NULL;

    if (ac->last_found) {
        last_found = strdup(ac->last_found->data);
    }

    if (ac->search_str) {
        search_str = strdup(ac->search_str);
    }

    autocomplete_clear(ac);
    autocomplete_add_all(ac, items);

    if (last_found) {
        // NULL if last_found was removed on update.
        ac->last_found = g_list_find_custom(ac->items, last_found, (GCompareFunc)strcmp);
        free(last_found);
    }

    if (search_str) {
        ac->search_str = strdup(search_str);
        free(search_str);
    }
}

void
autocomplete_add_unsorted(Autocomplete ac, const char* item, const gboolean is_reversed)
{
    if (ac) {
        char* item_cpy;
        GList* curr = g_list_find_custom(ac->items, item, (GCompareFunc)strcmp);

        // if item already exists
        if (curr) {
            return;
        }

        item_cpy = strdup(item);

        if (is_reversed) {
            ac->items = g_list_prepend(ac->items, item_cpy);
        } else {
            ac->items = g_list_append(ac->items, item_cpy);
        }
    }
}

void
autocomplete_add(Autocomplete ac, const char* item)
{
    if (ac) {
        char* item_cpy;
        GList* curr = g_list_find_custom(ac->items, item, (GCompareFunc)strcmp);

        // if item already exists
        if (curr) {
            return;
        }

        item_cpy = strdup(item);
        ac->items = g_list_insert_sorted(ac->items, item_cpy, (GCompareFunc)strcmp);
    }
}

void
autocomplete_add_all(Autocomplete ac, char** items)
{
    for (int i = 0; i < g_strv_length(items); i++) {
        autocomplete_add(ac, items[i]);
    }
}

void
autocomplete_remove(Autocomplete ac, const char* const item)
{
    if (ac) {
        GList* curr = g_list_find_custom(ac->items, item, (GCompareFunc)strcmp);

        if (!curr) {
            return;
        }

        // 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_list_delete_link(ac->items, curr);
    }

    return;
}

void
autocomplete_remove_all(Autocomplete ac, char** items)
{
    for (int i = 0; i < g_strv_length(items); i++) {
        autocomplete_remove(ac, items[i]);
    }
}

GList*
autocomplete_create_list(Autocomplete ac)
{
    GList* copy = NULL;
    GList* curr = ac->items;

    while (curr) {
        copy = g_list_append(copy, strdup(curr->data));
        curr = g_list_next(curr);
    }

    return copy;
}

gboolean
autocomplete_contains(Autocomplete ac, const char* value)
{
    GList* curr = ac->items;

    while (curr) {
        if (strcmp(curr->data, value) == 0) {
            return TRUE;
        }
        curr = g_list_next(curr);
    }

    return FALSE;
}

gchar*
autocomplete_complete(Autocomplete ac, const gchar* search_str, gboolean quote, gboolean previous)
{
    gchar* found = NULL;

    // no autocomplete to search
    if (!ac) {
        return NULL;
    }

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

    // first search attempt
    if (!ac->last_found) {
        if (ac->search_str) {
            FREE_SET_NULL(ac->search_str);
        }

        ac->search_str = strdup(search_str);
        found = _search(ac, ac->items, quote, NEXT);

        return found;

        // subsequent search attempt
    } else {
        if (previous) {
            // search from here-1 to beginning
            found = _search(ac, g_list_previous(ac->last_found), quote, PREVIOUS);
            if (found) {
                return found;
            }
        } else {
            // search from here+1 to end
            found = _search(ac, g_list_next(ac->last_found), quote, NEXT);
            if (found) {
                return found;
            }
        }

        if (previous) {
            // search from end
            found = _search(ac, g_list_last(ac->items), quote, PREVIOUS);
            if (found) {
                return found;
            }
        } else {
            // search from beginning
            found = _search(ac, ac->items, quote, NEXT);
            if (found) {
                return found;
            }
        }

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

        return NULL;
    }
}

// autocomplete_func func is used -> autocomplete_param_with_func
// Autocomplete ac, gboolean quote are used -> autocomplete_param_with_ac
static char*
_autocomplete_param_common(const char* const input, char* command, autocomplete_func func, Autocomplete ac, gboolean quote, gboolean previous, void* context)
{
    int len;
    auto_char char* command_cpy = g_strdup_printf("%s ", command);
    if (!command_cpy) {
        return NULL;
    }

    len = strlen(command_cpy);

    if (strncmp(input, command_cpy, len) == 0) {
        int inp_len = strlen(input);
        auto_char char* found = NULL;
        auto_char char* prefix = malloc(inp_len + 1);
        if (!prefix) {
            return NULL;
        }

        for (int i = len; i < inp_len; i++) {
            prefix[i - len] = input[i];
        }

        prefix[inp_len - len] = '\0';

        if (func) {
            found = func(prefix, previous, context);
        } else {
            found = autocomplete_complete(ac, prefix, quote, previous);
        }

        if (found) {
            return g_strdup_printf("%s%s", command_cpy, found);
        }
    }

    return NULL;
}

char*
autocomplete_param_with_func(const char* const input, char* command, autocomplete_func func, gboolean previous, void* context)
{
    return _autocomplete_param_common(input, command, func, NULL, FALSE, previous, context);
}

char*
autocomplete_param_with_ac(const char* const input, char* command, Autocomplete ac, gboolean quote, gboolean previous)
{
    return _autocomplete_param_common(input, command, NULL, ac, quote, previous, NULL);
}

char*
autocomplete_param_no_with_func(const char* const input, char* command, int arg_number, autocomplete_func func, gboolean previous, void* context)
{
    if (strncmp(input, command, strlen(command)) == 0) {
        // count tokens properly
        int num_tokens = count_tokens(input);

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

            // autocomplete param
            if (comp_str) {
                auto_gchar gchar* found = func(comp_str, previous, context);
                if (found) {
                    return g_strdup_printf("%s%s", start_str, found);
                }
            }
        }
    }

    return NULL;
}

/* remove the last message if we have more than max */
void
autocomplete_remove_older_than_max_reverse(Autocomplete ac, int maxsize)
{
    if (autocomplete_length(ac) > maxsize) {
        GList* last = g_list_last(ac->items);
        if (last) {
            free(last->data);
            ac->items = g_list_delete_link(ac->items, last);
        }
    }
}

static gchar*
_search(Autocomplete ac, GList* curr, gboolean quote, search_direction direction)
{
    auto_gchar gchar* search_str_ascii = g_str_to_ascii(ac->search_str, NULL);
    auto_gchar gchar* search_str_lower = g_ascii_strdown(search_str_ascii, -1);

    while (curr) {
        auto_gchar gchar* curr_ascii = g_str_to_ascii(curr->data, NULL);
        auto_gchar gchar* curr_lower = g_ascii_strdown(curr_ascii, -1);

        // match found
        if (strncmp(curr_lower, search_str_lower, strlen(search_str_lower)) == 0) {

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

            // if contains space, quote before returning
            if (quote && g_strrstr(curr->data, " ")) {
                return g_strdup_printf("\"%s\"", (gchar*)curr->data);
                // otherwise just return the string
            } else {
                return strdup(curr->data);
            }
        }

        if (direction == PREVIOUS) {
            curr = g_list_previous(curr);
        } else {
            curr = g_list_next(curr);
        }
    }

    return NULL;
}