2012-10-21 15:02:20 -04:00
|
|
|
/*
|
2013-01-24 20:11:49 -05:00
|
|
|
* autocomplete.c
|
2019-11-13 06:11:05 -05:00
|
|
|
* vim: expandtab:ts=4:sts=4:sw=4
|
2012-05-13 16:40:39 -04:00
|
|
|
*
|
2019-01-22 05:31:45 -05:00
|
|
|
* Copyright (C) 2012 - 2019 James Booth <boothj5@gmail.com>
|
2012-10-21 15:02:20 -04:00
|
|
|
*
|
2012-05-13 16:40:39 -04:00
|
|
|
* 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
|
2016-07-23 20:14:49 -04:00
|
|
|
* along with Profanity. If not, see <https://www.gnu.org/licenses/>.
|
2012-05-13 16:40:39 -04:00
|
|
|
*
|
2014-08-24 15:57:39 -04:00
|
|
|
* 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.
|
|
|
|
*
|
2012-05-13 16:40:39 -04:00
|
|
|
*/
|
|
|
|
|
2013-06-02 14:56:35 -04:00
|
|
|
#include <stdio.h>
|
2012-05-13 16:40:39 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2017-03-24 21:48:40 -04:00
|
|
|
#include <glib.h>
|
2012-05-13 16:40:39 -04:00
|
|
|
|
2013-08-03 07:27:07 -04:00
|
|
|
#include "common.h"
|
2013-07-11 17:57:35 -04:00
|
|
|
#include "tools/autocomplete.h"
|
|
|
|
#include "tools/parser.h"
|
2017-03-31 19:27:11 -04:00
|
|
|
#include "ui/ui.h"
|
2012-05-13 16:40:39 -04:00
|
|
|
|
2013-01-24 20:11:49 -05:00
|
|
|
struct autocomplete_t {
|
2017-03-31 19:27:11 -04:00
|
|
|
GList *items;
|
|
|
|
GList *last_found;
|
2012-05-13 16:40:39 -04:00
|
|
|
gchar *search_str;
|
|
|
|
};
|
|
|
|
|
2017-03-31 19:27:11 -04:00
|
|
|
static gchar* _search_next(Autocomplete ac, GList *curr, gboolean quote);
|
|
|
|
static gchar* _search_prev(Autocomplete ac, GList *curr, gboolean quote);
|
2012-05-13 16:40:39 -04:00
|
|
|
|
2013-01-24 20:11:49 -05:00
|
|
|
Autocomplete
|
|
|
|
autocomplete_new(void)
|
2012-05-13 16:40:39 -04:00
|
|
|
{
|
2013-01-24 20:11:49 -05:00
|
|
|
Autocomplete new = malloc(sizeof(struct autocomplete_t));
|
2012-05-13 16:40:39 -04:00
|
|
|
new->items = NULL;
|
|
|
|
new->last_found = NULL;
|
|
|
|
new->search_str = NULL;
|
|
|
|
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
2012-07-24 18:19:48 -04:00
|
|
|
void
|
2013-01-24 20:11:49 -05:00
|
|
|
autocomplete_clear(Autocomplete ac)
|
2012-05-13 16:40:39 -04:00
|
|
|
{
|
2014-09-25 19:06:50 -04:00
|
|
|
if (ac) {
|
2019-07-13 13:08:10 -04:00
|
|
|
if (ac->items) {
|
|
|
|
g_list_free_full(ac->items, free);
|
|
|
|
ac->items = NULL;
|
|
|
|
}
|
2012-05-13 16:40:39 -04:00
|
|
|
|
2014-09-14 17:31:27 -04:00
|
|
|
autocomplete_reset(ac);
|
|
|
|
}
|
2012-05-13 16:40:39 -04:00
|
|
|
}
|
|
|
|
|
2012-07-24 18:19:48 -04:00
|
|
|
void
|
2013-01-24 20:11:49 -05:00
|
|
|
autocomplete_reset(Autocomplete ac)
|
2012-05-13 16:40:39 -04:00
|
|
|
{
|
|
|
|
ac->last_found = NULL;
|
2013-08-03 07:27:07 -04:00
|
|
|
FREE_SET_NULL(ac->search_str);
|
2012-05-13 16:40:39 -04:00
|
|
|
}
|
|
|
|
|
2013-01-10 19:17:18 -05:00
|
|
|
void
|
2013-01-24 20:11:49 -05:00
|
|
|
autocomplete_free(Autocomplete ac)
|
2013-01-10 19:17:18 -05:00
|
|
|
{
|
2014-09-25 19:48:48 -04:00
|
|
|
if (ac) {
|
|
|
|
autocomplete_clear(ac);
|
|
|
|
free(ac);
|
|
|
|
}
|
2013-01-10 19:17:18 -05:00
|
|
|
}
|
|
|
|
|
2013-04-24 18:50:47 -04:00
|
|
|
gint
|
|
|
|
autocomplete_length(Autocomplete ac)
|
|
|
|
{
|
2014-09-25 19:06:50 -04:00
|
|
|
if (!ac) {
|
2013-05-05 19:33:33 -04:00
|
|
|
return 0;
|
2014-09-25 19:06:50 -04:00
|
|
|
} else if (!ac->items) {
|
2013-05-05 19:33:33 -04:00
|
|
|
return 0;
|
|
|
|
} else {
|
2017-03-31 19:27:11 -04:00
|
|
|
return g_list_length(ac->items);
|
2013-05-05 19:33:33 -04:00
|
|
|
}
|
2013-04-24 18:50:47 -04:00
|
|
|
}
|
|
|
|
|
2019-07-13 13:08:10 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-29 05:26:18 -04:00
|
|
|
void
|
|
|
|
autocomplete_add_reverse(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_prepend(ac->items, item_cpy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-09 18:51:13 -05:00
|
|
|
void
|
2013-08-25 19:43:59 -04:00
|
|
|
autocomplete_add(Autocomplete ac, const char *item)
|
2012-05-13 16:40:39 -04:00
|
|
|
{
|
2014-09-25 19:06:50 -04:00
|
|
|
if (ac) {
|
2014-09-17 18:34:48 -04:00
|
|
|
char *item_cpy;
|
2017-03-31 19:27:11 -04:00
|
|
|
GList *curr = g_list_find_custom(ac->items, item, (GCompareFunc)strcmp);
|
2012-10-21 15:02:20 -04:00
|
|
|
|
2014-09-17 18:34:48 -04:00
|
|
|
// if item already exists
|
2014-09-25 19:06:50 -04:00
|
|
|
if (curr) {
|
2014-09-17 18:34:48 -04:00
|
|
|
return;
|
|
|
|
}
|
2013-08-25 19:43:59 -04:00
|
|
|
|
2014-09-17 18:34:48 -04:00
|
|
|
item_cpy = strdup(item);
|
2017-03-31 19:27:11 -04:00
|
|
|
ac->items = g_list_insert_sorted(ac->items, item_cpy, (GCompareFunc)strcmp);
|
2014-09-17 18:34:48 -04:00
|
|
|
}
|
2012-05-13 16:40:39 -04:00
|
|
|
}
|
|
|
|
|
2016-07-05 16:46:36 -04:00
|
|
|
void
|
|
|
|
autocomplete_add_all(Autocomplete ac, char **items)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
for (i = 0; i < g_strv_length(items); i++) {
|
|
|
|
autocomplete_add(ac, items[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-09 18:51:13 -05:00
|
|
|
void
|
2015-10-25 18:47:42 -04:00
|
|
|
autocomplete_remove(Autocomplete ac, const char *const item)
|
2012-05-13 16:40:39 -04:00
|
|
|
{
|
2014-09-25 19:06:50 -04:00
|
|
|
if (ac) {
|
2017-03-31 19:27:11 -04:00
|
|
|
GList *curr = g_list_find_custom(ac->items, item, (GCompareFunc)strcmp);
|
2012-05-13 16:40:39 -04:00
|
|
|
|
2014-09-17 18:21:14 -04:00
|
|
|
if (!curr) {
|
|
|
|
return;
|
|
|
|
}
|
2012-10-21 15:02:20 -04:00
|
|
|
|
2014-09-17 18:21:14 -04:00
|
|
|
// reset last found if it points to the item to be removed
|
|
|
|
if (ac->last_found == curr) {
|
|
|
|
ac->last_found = NULL;
|
|
|
|
}
|
2012-05-13 16:40:39 -04:00
|
|
|
|
2014-09-17 18:21:14 -04:00
|
|
|
free(curr->data);
|
2017-03-31 19:27:11 -04:00
|
|
|
ac->items = g_list_delete_link(ac->items, curr);
|
2014-09-17 18:21:14 -04:00
|
|
|
}
|
2012-05-13 16:40:39 -04:00
|
|
|
|
2013-12-09 18:51:13 -05:00
|
|
|
return;
|
2012-05-13 16:40:39 -04:00
|
|
|
}
|
|
|
|
|
2016-07-05 16:46:36 -04:00
|
|
|
void
|
|
|
|
autocomplete_remove_all(Autocomplete ac, char **items)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
for (i = 0; i < g_strv_length(items); i++) {
|
|
|
|
autocomplete_remove(ac, items[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-31 19:27:11 -04:00
|
|
|
GList*
|
2014-09-25 19:06:50 -04:00
|
|
|
autocomplete_create_list(Autocomplete ac)
|
2012-05-13 16:40:39 -04:00
|
|
|
{
|
2017-03-31 19:27:11 -04:00
|
|
|
GList *copy = NULL;
|
|
|
|
GList *curr = ac->items;
|
2012-05-13 16:40:39 -04:00
|
|
|
|
|
|
|
while(curr) {
|
2017-03-31 19:27:11 -04:00
|
|
|
copy = g_list_append(copy, strdup(curr->data));
|
|
|
|
curr = g_list_next(curr);
|
2012-05-13 16:40:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
2014-01-24 20:39:12 -05:00
|
|
|
gboolean
|
2014-02-01 20:05:52 -05:00
|
|
|
autocomplete_contains(Autocomplete ac, const char *value)
|
2014-01-24 20:39:12 -05:00
|
|
|
{
|
2017-03-31 19:27:11 -04:00
|
|
|
GList *curr = ac->items;
|
2014-01-24 20:39:12 -05:00
|
|
|
|
|
|
|
while(curr) {
|
|
|
|
if (strcmp(curr->data, value) == 0) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
2017-03-31 19:27:11 -04:00
|
|
|
curr = g_list_next(curr);
|
2014-01-24 20:39:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2015-10-25 18:47:42 -04:00
|
|
|
gchar*
|
2017-03-31 19:27:11 -04:00
|
|
|
autocomplete_complete(Autocomplete ac, const gchar *search_str, gboolean quote, gboolean previous)
|
2012-05-13 16:40:39 -04:00
|
|
|
{
|
|
|
|
gchar *found = NULL;
|
|
|
|
|
2014-05-08 15:49:41 -04:00
|
|
|
// no autocomplete to search
|
2014-09-25 19:06:50 -04:00
|
|
|
if (!ac) {
|
2014-05-08 15:49:41 -04:00
|
|
|
return NULL;
|
2014-09-25 19:06:50 -04:00
|
|
|
}
|
2014-05-08 15:49:41 -04:00
|
|
|
|
2012-05-13 16:40:39 -04:00
|
|
|
// no items to search
|
2014-09-25 19:06:50 -04:00
|
|
|
if (!ac->items) {
|
2012-05-13 16:40:39 -04:00
|
|
|
return NULL;
|
2014-09-25 19:06:50 -04:00
|
|
|
}
|
2012-05-13 16:40:39 -04:00
|
|
|
|
|
|
|
// first search attempt
|
2014-09-25 19:06:50 -04:00
|
|
|
if (!ac->last_found) {
|
|
|
|
if (ac->search_str) {
|
2014-06-15 12:32:30 -04:00
|
|
|
FREE_SET_NULL(ac->search_str);
|
|
|
|
}
|
2014-09-25 19:06:50 -04:00
|
|
|
|
2014-04-02 16:01:20 -04:00
|
|
|
ac->search_str = strdup(search_str);
|
2017-03-31 19:27:11 -04:00
|
|
|
found = _search_next(ac, ac->items, quote);
|
2014-09-25 19:06:50 -04:00
|
|
|
|
2012-05-13 16:40:39 -04:00
|
|
|
return found;
|
|
|
|
|
2012-10-21 15:02:20 -04:00
|
|
|
// subsequent search attempt
|
2012-05-13 16:40:39 -04:00
|
|
|
} else {
|
2017-03-31 19:27:11 -04:00
|
|
|
if (previous) {
|
|
|
|
// search from here-1 to beginning
|
|
|
|
found = _search_prev(ac, g_list_previous(ac->last_found), quote);
|
|
|
|
if (found) {
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// search from here+1 to end
|
|
|
|
found = _search_next(ac, g_list_next(ac->last_found), quote);
|
|
|
|
if (found) {
|
|
|
|
return found;
|
|
|
|
}
|
2014-09-25 19:06:50 -04:00
|
|
|
}
|
2012-05-13 16:40:39 -04:00
|
|
|
|
2017-03-31 19:27:11 -04:00
|
|
|
if (previous) {
|
|
|
|
// search from end
|
|
|
|
found = _search_prev(ac, g_list_last(ac->items), quote);
|
|
|
|
if (found) {
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// search from beginning
|
|
|
|
found = _search_next(ac, ac->items, quote);
|
|
|
|
if (found) {
|
|
|
|
return found;
|
|
|
|
}
|
2014-09-25 19:06:50 -04:00
|
|
|
}
|
2012-10-21 15:02:20 -04:00
|
|
|
|
2012-05-13 16:40:39 -04:00
|
|
|
// we found nothing, reset search
|
2013-01-24 20:11:49 -05:00
|
|
|
autocomplete_reset(ac);
|
2014-09-25 19:06:50 -04:00
|
|
|
|
2012-05-13 16:40:39 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-25 18:47:42 -04:00
|
|
|
char*
|
2020-01-30 17:44:43 -05:00
|
|
|
autocomplete_param_with_func(const char *const input, char *command, autocomplete_func func, gboolean previous, void *context)
|
2013-06-02 14:56:35 -04:00
|
|
|
{
|
2014-04-02 16:01:20 -04:00
|
|
|
GString *auto_msg = NULL;
|
|
|
|
char *result = NULL;
|
2013-09-22 18:11:28 -04:00
|
|
|
char command_cpy[strlen(command) + 2];
|
2013-06-02 14:56:35 -04:00
|
|
|
sprintf(command_cpy, "%s ", command);
|
|
|
|
int len = strlen(command_cpy);
|
2014-04-02 16:01:20 -04:00
|
|
|
|
2016-05-23 19:32:53 -04:00
|
|
|
if (strncmp(input, command_cpy, len) == 0) {
|
2014-04-25 19:36:36 -04:00
|
|
|
int i;
|
2015-01-16 17:50:40 -05:00
|
|
|
int inp_len = strlen(input);
|
|
|
|
char prefix[inp_len];
|
|
|
|
for(i = len; i < inp_len; i++) {
|
|
|
|
prefix[i-len] = input[i];
|
2013-06-02 14:56:35 -04:00
|
|
|
}
|
2015-01-16 17:50:40 -05:00
|
|
|
prefix[inp_len - len] = '\0';
|
2014-04-25 19:36:36 -04:00
|
|
|
|
2020-01-30 17:44:43 -05:00
|
|
|
char *found = func(prefix, previous, context);
|
2014-09-25 19:06:50 -04:00
|
|
|
if (found) {
|
2014-04-02 16:01:20 -04:00
|
|
|
auto_msg = g_string_new(command_cpy);
|
|
|
|
g_string_append(auto_msg, found);
|
2013-06-02 14:56:35 -04:00
|
|
|
free(found);
|
2014-04-02 16:01:20 -04:00
|
|
|
result = auto_msg->str;
|
|
|
|
g_string_free(auto_msg, FALSE);
|
2013-06-02 14:56:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-02 16:01:20 -04:00
|
|
|
return result;
|
2013-06-02 14:56:35 -04:00
|
|
|
}
|
|
|
|
|
2015-10-25 18:47:42 -04:00
|
|
|
char*
|
2017-03-31 19:27:11 -04:00
|
|
|
autocomplete_param_with_ac(const char *const input, char *command, Autocomplete ac, gboolean quote, gboolean previous)
|
2013-06-02 14:56:35 -04:00
|
|
|
{
|
2014-04-02 16:01:20 -04:00
|
|
|
GString *auto_msg = NULL;
|
|
|
|
char *result = NULL;
|
2013-06-02 14:56:35 -04:00
|
|
|
char *command_cpy = malloc(strlen(command) + 2);
|
|
|
|
sprintf(command_cpy, "%s ", command);
|
|
|
|
int len = strlen(command_cpy);
|
2015-01-16 17:50:40 -05:00
|
|
|
int inp_len = strlen(input);
|
2016-05-23 19:32:53 -04:00
|
|
|
if (strncmp(input, command_cpy, len) == 0) {
|
2014-04-25 19:36:36 -04:00
|
|
|
int i;
|
2015-01-16 17:50:40 -05:00
|
|
|
char prefix[inp_len];
|
|
|
|
for(i = len; i < inp_len; i++) {
|
|
|
|
prefix[i-len] = input[i];
|
2013-06-02 14:56:35 -04:00
|
|
|
}
|
2015-01-16 17:50:40 -05:00
|
|
|
prefix[inp_len - len] = '\0';
|
2014-04-25 19:36:36 -04:00
|
|
|
|
2017-03-31 19:27:11 -04:00
|
|
|
char *found = autocomplete_complete(ac, prefix, quote, previous);
|
2014-09-25 19:06:50 -04:00
|
|
|
if (found) {
|
2014-04-02 16:01:20 -04:00
|
|
|
auto_msg = g_string_new(command_cpy);
|
|
|
|
g_string_append(auto_msg, found);
|
2013-06-02 14:56:35 -04:00
|
|
|
free(found);
|
2014-04-02 16:01:20 -04:00
|
|
|
result = auto_msg->str;
|
|
|
|
g_string_free(auto_msg, FALSE);
|
2013-06-02 14:56:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
free(command_cpy);
|
|
|
|
|
2014-04-02 16:01:20 -04:00
|
|
|
return result;
|
2013-06-02 14:56:35 -04:00
|
|
|
}
|
|
|
|
|
2015-10-25 18:47:42 -04:00
|
|
|
char*
|
2020-01-30 17:44:43 -05:00
|
|
|
autocomplete_param_no_with_func(const char *const input, char *command, int arg_number, autocomplete_func func, gboolean previous, void *context)
|
2013-06-22 20:23:44 -04:00
|
|
|
{
|
2016-05-23 19:32:53 -04:00
|
|
|
if (strncmp(input, command, strlen(command)) == 0) {
|
2013-06-22 20:23:44 -04:00
|
|
|
GString *result_str = NULL;
|
|
|
|
|
2013-07-09 17:34:55 -04:00
|
|
|
// count tokens properly
|
2015-01-16 17:50:40 -05:00
|
|
|
int num_tokens = count_tokens(input);
|
2013-06-22 20:23:44 -04:00
|
|
|
|
2013-07-09 17:34:55 -04:00
|
|
|
// if correct number of tokens, then candidate for autocompletion of last param
|
|
|
|
if (num_tokens == arg_number) {
|
2015-01-16 17:50:40 -05:00
|
|
|
gchar *start_str = get_start(input, arg_number);
|
|
|
|
gchar *comp_str = g_strdup(&input[strlen(start_str)]);
|
2013-06-22 20:23:44 -04:00
|
|
|
|
|
|
|
// autocomplete param
|
2014-09-25 19:06:50 -04:00
|
|
|
if (comp_str) {
|
2020-01-30 17:44:43 -05:00
|
|
|
char *found = func(comp_str, previous, context);
|
2014-09-25 19:06:50 -04:00
|
|
|
if (found) {
|
2013-06-22 20:23:44 -04:00
|
|
|
result_str = g_string_new("");
|
2013-07-09 17:34:55 -04:00
|
|
|
g_string_append(result_str, start_str);
|
2013-06-22 20:23:44 -04:00
|
|
|
g_string_append(result_str, found);
|
2019-10-06 13:05:27 -04:00
|
|
|
|
|
|
|
free(start_str);
|
|
|
|
free(comp_str);
|
|
|
|
|
2014-04-25 19:36:36 -04:00
|
|
|
char *result = result_str->str;
|
2013-06-22 20:23:44 -04:00
|
|
|
g_string_free(result_str, FALSE);
|
2019-10-06 13:05:27 -04:00
|
|
|
|
2013-06-22 20:23:44 -04:00
|
|
|
return result;
|
|
|
|
}
|
2019-10-06 13:05:27 -04:00
|
|
|
} else {
|
|
|
|
free(start_str);
|
2013-06-22 20:23:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-06-02 14:56:35 -04:00
|
|
|
|
2020-05-29 05:26:18 -04:00
|
|
|
/* remove the last message if we have more than max */
|
2020-05-20 04:26:48 -04:00
|
|
|
void
|
2020-05-29 05:26:18 -04:00
|
|
|
autocomplete_remove_older_than_max_reverse(Autocomplete ac, int maxsize)
|
2020-05-20 04:26:48 -04:00
|
|
|
{
|
|
|
|
if (autocomplete_length(ac) > maxsize) {
|
2020-05-29 05:26:18 -04:00
|
|
|
GList *last = g_list_last(ac->items);
|
|
|
|
if (last) {
|
2020-05-29 05:28:58 -04:00
|
|
|
free(last->data);
|
2020-05-29 05:26:18 -04:00
|
|
|
ac->items = g_list_delete_link(ac->items, last);
|
2020-05-20 04:26:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-25 18:47:42 -04:00
|
|
|
static gchar*
|
2017-03-31 19:27:11 -04:00
|
|
|
_search_next(Autocomplete ac, GList *curr, gboolean quote)
|
|
|
|
{
|
|
|
|
gchar *search_str_ascii = g_str_to_ascii(ac->search_str, NULL);
|
|
|
|
gchar *search_str_lower = g_ascii_strdown(search_str_ascii, -1);
|
|
|
|
g_free(search_str_ascii);
|
|
|
|
|
|
|
|
while(curr) {
|
|
|
|
gchar *curr_ascii = g_str_to_ascii(curr->data, NULL);
|
|
|
|
gchar *curr_lower = g_ascii_strdown(curr_ascii, -1);
|
|
|
|
g_free(curr_ascii);
|
|
|
|
|
|
|
|
// 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, " ")) {
|
|
|
|
GString *quoted = g_string_new("\"");
|
|
|
|
g_string_append(quoted, curr->data);
|
|
|
|
g_string_append(quoted, "\"");
|
|
|
|
|
|
|
|
gchar *result = quoted->str;
|
|
|
|
g_string_free(quoted, FALSE);
|
|
|
|
|
|
|
|
g_free(search_str_lower);
|
|
|
|
g_free(curr_lower);
|
|
|
|
return result;
|
|
|
|
|
|
|
|
// otherwise just return the string
|
|
|
|
} else {
|
|
|
|
g_free(search_str_lower);
|
|
|
|
g_free(curr_lower);
|
|
|
|
return strdup(curr->data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free(curr_lower);
|
|
|
|
curr = g_list_next(curr);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free(search_str_lower);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gchar*
|
|
|
|
_search_prev(Autocomplete ac, GList *curr, gboolean quote)
|
2012-05-13 16:40:39 -04:00
|
|
|
{
|
2017-03-24 21:40:20 -04:00
|
|
|
gchar *search_str_ascii = g_str_to_ascii(ac->search_str, NULL);
|
|
|
|
gchar *search_str_lower = g_ascii_strdown(search_str_ascii, -1);
|
|
|
|
g_free(search_str_ascii);
|
|
|
|
|
2012-05-13 16:40:39 -04:00
|
|
|
while(curr) {
|
2017-03-24 21:40:20 -04:00
|
|
|
gchar *curr_ascii = g_str_to_ascii(curr->data, NULL);
|
|
|
|
gchar *curr_lower = g_ascii_strdown(curr_ascii, -1);
|
|
|
|
g_free(curr_ascii);
|
2012-10-21 15:02:20 -04:00
|
|
|
|
2012-05-13 16:40:39 -04:00
|
|
|
// match found
|
2017-03-24 21:40:20 -04:00
|
|
|
if (strncmp(curr_lower, search_str_lower, strlen(search_str_lower)) == 0) {
|
2012-10-21 15:02:20 -04:00
|
|
|
|
2012-05-13 16:40:39 -04:00
|
|
|
// set pointer to last found
|
|
|
|
ac->last_found = curr;
|
|
|
|
|
2013-01-14 17:16:12 -05:00
|
|
|
// if contains space, quote before returning
|
2014-07-09 15:15:20 -04:00
|
|
|
if (quote && g_strrstr(curr->data, " ")) {
|
2013-01-14 17:16:12 -05:00
|
|
|
GString *quoted = g_string_new("\"");
|
|
|
|
g_string_append(quoted, curr->data);
|
|
|
|
g_string_append(quoted, "\"");
|
|
|
|
|
|
|
|
gchar *result = quoted->str;
|
|
|
|
g_string_free(quoted, FALSE);
|
|
|
|
|
2017-03-24 21:40:20 -04:00
|
|
|
g_free(search_str_lower);
|
|
|
|
g_free(curr_lower);
|
2013-01-14 17:16:12 -05:00
|
|
|
return result;
|
|
|
|
|
|
|
|
// otherwise just return the string
|
|
|
|
} else {
|
2017-03-24 21:40:20 -04:00
|
|
|
g_free(search_str_lower);
|
|
|
|
g_free(curr_lower);
|
2013-01-14 17:16:12 -05:00
|
|
|
return strdup(curr->data);
|
|
|
|
}
|
2012-05-13 16:40:39 -04:00
|
|
|
}
|
|
|
|
|
2017-03-24 21:40:20 -04:00
|
|
|
g_free(curr_lower);
|
2017-03-31 19:27:11 -04:00
|
|
|
curr = g_list_previous(curr);
|
2012-05-13 16:40:39 -04:00
|
|
|
}
|
|
|
|
|
2017-03-24 21:40:20 -04:00
|
|
|
g_free(search_str_lower);
|
2012-05-13 16:40:39 -04:00
|
|
|
return NULL;
|
2014-09-14 17:31:27 -04:00
|
|
|
}
|