1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-09-15 19:38:07 -04:00
profanity/src/tools/autocomplete.c

353 lines
8.6 KiB
C
Raw Normal View History

/*
* autocomplete.c
2012-05-13 16:40:39 -04:00
*
2014-03-08 20:18:19 -05:00
* Copyright (C) 2012 - 2014 James Booth <boothj5@gmail.com>
*
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
* along with Profanity. If not, see <http://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.
*
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>
#include "common.h"
#include "tools/autocomplete.h"
#include "tools/parser.h"
2012-05-13 16:40:39 -04:00
struct autocomplete_t {
2012-05-13 16:40:39 -04:00
GSList *items;
GSList *last_found;
gchar *search_str;
};
static gchar * _search_from(Autocomplete ac, GSList *curr, gboolean quote);
2012-05-13 16:40:39 -04:00
Autocomplete
autocomplete_new(void)
2012-05-13 16:40:39 -04: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
autocomplete_clear(Autocomplete ac)
2012-05-13 16:40:39 -04:00
{
if (ac) {
2014-09-14 17:31:27 -04:00
g_slist_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
autocomplete_reset(Autocomplete ac)
2012-05-13 16:40:39 -04:00
{
ac->last_found = NULL;
FREE_SET_NULL(ac->search_str);
2012-05-13 16:40:39 -04:00
}
void
autocomplete_free(Autocomplete ac)
{
autocomplete_clear(ac);
2013-07-14 14:34:02 -04:00
free(ac);
}
gint
autocomplete_length(Autocomplete ac)
{
if (!ac) {
return 0;
} else if (!ac->items) {
return 0;
} else {
return g_slist_length(ac->items);
}
}
void
autocomplete_add(Autocomplete ac, const char *item)
2012-05-13 16:40:39 -04:00
{
if (ac) {
char *item_cpy;
GSList *curr = g_slist_find_custom(ac->items, item, (GCompareFunc)strcmp);
// if item already exists
if (curr) {
return;
}
item_cpy = strdup(item);
ac->items = g_slist_insert_sorted(ac->items, item_cpy, (GCompareFunc)strcmp);
}
return;
2012-05-13 16:40:39 -04:00
}
void
autocomplete_remove(Autocomplete ac, const char * const item)
2012-05-13 16:40:39 -04:00
{
if (ac) {
GSList *curr = g_slist_find_custom(ac->items, item, (GCompareFunc)strcmp);
2012-05-13 16:40:39 -04:00
if (!curr) {
return;
}
// 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
free(curr->data);
ac->items = g_slist_delete_link(ac->items, curr);
}
2012-05-13 16:40:39 -04:00
return;
2012-05-13 16:40:39 -04:00
}
2012-07-24 18:19:48 -04:00
GSList *
autocomplete_create_list(Autocomplete ac)
2012-05-13 16:40:39 -04:00
{
GSList *copy = NULL;
GSList *curr = ac->items;
while(curr) {
2012-10-28 19:38:10 -04:00
copy = g_slist_append(copy, strdup(curr->data));
2012-05-13 16:40:39 -04:00
curr = g_slist_next(curr);
}
return copy;
}
gboolean
autocomplete_contains(Autocomplete ac, const char *value)
{
GSList *curr = ac->items;
while(curr) {
if (strcmp(curr->data, value) == 0) {
return TRUE;
}
curr = g_slist_next(curr);
}
return FALSE;
}
2012-07-24 18:19:48 -04:00
gchar *
autocomplete_complete(Autocomplete ac, gchar *search_str, gboolean quote)
2012-05-13 16:40:39 -04:00
{
gchar *found = NULL;
// no autocomplete to search
if (!ac) {
return NULL;
}
2012-05-13 16:40:39 -04:00
// no items to search
if (!ac->items) {
2012-05-13 16:40:39 -04:00
return NULL;
}
2012-05-13 16:40:39 -04:00
// first search attempt
if (!ac->last_found) {
if (ac->search_str) {
2014-06-15 12:32:30 -04:00
FREE_SET_NULL(ac->search_str);
}
2014-04-02 16:01:20 -04:00
ac->search_str = strdup(search_str);
found = _search_from(ac, ac->items, quote);
2012-05-13 16:40:39 -04:00
return found;
// subsequent search attempt
2012-05-13 16:40:39 -04:00
} else {
2014-07-15 16:30:23 -04:00
// search from here+1 to end
found = _search_from(ac, g_slist_next(ac->last_found), quote);
if (found) {
2012-05-13 16:40:39 -04:00
return found;
}
2012-05-13 16:40:39 -04:00
// search from beginning
found = _search_from(ac, ac->items, quote);
if (found) {
2012-05-13 16:40:39 -04:00
return found;
}
2012-05-13 16:40:39 -04:00
// we found nothing, reset search
autocomplete_reset(ac);
2012-05-13 16:40:39 -04:00
return NULL;
}
}
2013-06-02 14:56:35 -04:00
char *
autocomplete_param_with_func(char *input, int *size, char *command,
autocomplete_func func)
{
2014-04-02 16:01:20 -04:00
GString *auto_msg = NULL;
char *result = NULL;
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
2013-06-02 14:56:35 -04:00
if ((strncmp(input, command_cpy, len) == 0) && (*size > len)) {
2014-04-25 19:36:36 -04:00
int i;
char inp_cpy[*size];
2013-06-02 14:56:35 -04:00
for(i = len; i < *size; i++) {
inp_cpy[i-len] = input[i];
}
inp_cpy[(*size) - len] = '\0';
2014-04-25 19:36:36 -04:00
char *found = func(inp_cpy);
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
}
char *
autocomplete_param_with_ac(char *input, int *size, char *command,
Autocomplete ac, gboolean quote)
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);
if ((strncmp(input, command_cpy, len) == 0) && (*size > len)) {
2014-04-25 19:36:36 -04:00
int i;
char inp_cpy[*size];
2013-06-02 14:56:35 -04:00
for(i = len; i < *size; i++) {
inp_cpy[i-len] = input[i];
}
inp_cpy[(*size) - len] = '\0';
2014-04-25 19:36:36 -04:00
char *found = autocomplete_complete(ac, inp_cpy, quote);
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
}
char *
autocomplete_param_no_with_func(char *input, int *size, char *command,
int arg_number, autocomplete_func func)
{
if (strncmp(input, command, strlen(command)) == 0 && (*size > strlen(command))) {
int i = 0;
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) {
2014-04-25 19:36:36 -04:00
char *found = func(comp_str);
if (found) {
result_str = g_string_new("");
g_string_append(result_str, start_str);
g_string_append(result_str, found);
2014-04-25 19:36:36 -04:00
char *result = result_str->str;
g_string_free(result_str, FALSE);
return result;
}
}
}
}
return NULL;
}
2013-06-02 14:56:35 -04:00
2012-07-24 18:19:48 -04:00
static gchar *
_search_from(Autocomplete ac, GSList *curr, gboolean quote)
2012-05-13 16:40:39 -04:00
{
while(curr) {
2012-05-13 16:40:39 -04:00
// match found
2013-01-14 17:09:31 -05:00
if (strncmp(curr->data, ac->search_str, strlen(ac->search_str)) == 0) {
2012-05-13 16:40:39 -04:00
// 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);
return result;
// otherwise just return the string
} else {
return strdup(curr->data);
}
2012-05-13 16:40:39 -04:00
}
curr = g_slist_next(curr);
}
return NULL;
2014-09-14 17:31:27 -04:00
}