2012-10-21 15:02:20 -04:00
|
|
|
/*
|
2013-01-24 20:11:49 -05:00
|
|
|
* autocomplete.c
|
2012-05-13 16:40:39 -04:00
|
|
|
*
|
2013-01-10 21:05:29 -05:00
|
|
|
* Copyright (C) 2012, 2013 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
|
|
|
|
* along with Profanity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
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>
|
|
|
|
|
2013-01-24 20:11:49 -05:00
|
|
|
#include "autocomplete.h"
|
2012-05-13 16:40:39 -04:00
|
|
|
|
2013-01-24 20:11:49 -05:00
|
|
|
struct autocomplete_t {
|
2012-05-13 16:40:39 -04:00
|
|
|
GSList *items;
|
|
|
|
GSList *last_found;
|
|
|
|
gchar *search_str;
|
|
|
|
};
|
|
|
|
|
2013-01-24 20:11:49 -05:00
|
|
|
static gchar * _search_from(Autocomplete ac, GSList *curr);
|
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
|
|
|
{
|
2012-10-28 19:38:10 -04:00
|
|
|
g_slist_free_full(ac->items, free);
|
2012-05-13 16:40:39 -04:00
|
|
|
ac->items = NULL;
|
|
|
|
|
2013-01-24 20:11:49 -05: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;
|
|
|
|
if (ac->search_str != NULL) {
|
|
|
|
free(ac->search_str);
|
|
|
|
ac->search_str = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2013-01-24 20:11:49 -05:00
|
|
|
autocomplete_clear(ac);
|
2013-01-10 19:17:18 -05:00
|
|
|
g_free(ac);
|
|
|
|
ac = NULL;
|
|
|
|
}
|
|
|
|
|
2013-04-24 18:50:47 -04:00
|
|
|
gint
|
|
|
|
autocomplete_length(Autocomplete ac)
|
|
|
|
{
|
2013-05-05 19:33:33 -04:00
|
|
|
if (ac == NULL) {
|
|
|
|
return 0;
|
|
|
|
} else if (ac->items == NULL) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return g_slist_length(ac->items);
|
|
|
|
}
|
2013-04-24 18:50:47 -04:00
|
|
|
}
|
|
|
|
|
2012-07-24 18:19:48 -04:00
|
|
|
gboolean
|
2013-01-24 20:11:49 -05:00
|
|
|
autocomplete_add(Autocomplete ac, void *item)
|
2012-05-13 16:40:39 -04:00
|
|
|
{
|
|
|
|
if (ac->items == NULL) {
|
|
|
|
ac->items = g_slist_append(ac->items, item);
|
2012-05-23 18:46:54 -04:00
|
|
|
return TRUE;
|
2012-05-13 16:40:39 -04:00
|
|
|
} else {
|
|
|
|
GSList *curr = ac->items;
|
2012-10-21 15:02:20 -04:00
|
|
|
|
2012-05-13 16:40:39 -04:00
|
|
|
while(curr) {
|
2012-10-21 15:02:20 -04:00
|
|
|
|
2012-05-13 16:40:39 -04:00
|
|
|
// insert
|
2012-10-28 19:38:10 -04:00
|
|
|
if (g_strcmp0(curr->data, item) > 0) {
|
2012-05-13 16:40:39 -04:00
|
|
|
ac->items = g_slist_insert_before(ac->items,
|
|
|
|
curr, item);
|
2012-05-23 18:46:54 -04:00
|
|
|
return TRUE;
|
2012-10-21 15:02:20 -04:00
|
|
|
|
2012-05-13 16:40:39 -04:00
|
|
|
// update
|
2012-10-28 19:38:10 -04:00
|
|
|
} else if (g_strcmp0(curr->data, item) == 0) {
|
2012-05-23 19:33:23 -04:00
|
|
|
// only update if data different
|
2012-10-28 19:38:10 -04:00
|
|
|
if (strcmp(curr->data, item) != 0) {
|
|
|
|
free(curr->data);
|
2012-05-23 19:33:23 -04:00
|
|
|
curr->data = item;
|
|
|
|
return TRUE;
|
|
|
|
} else {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2012-05-13 16:40:39 -04:00
|
|
|
}
|
2012-10-21 15:02:20 -04:00
|
|
|
|
2012-05-13 16:40:39 -04:00
|
|
|
curr = g_slist_next(curr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// hit end, append
|
|
|
|
ac->items = g_slist_append(ac->items, item);
|
|
|
|
|
2012-05-23 18:46:54 -04:00
|
|
|
return TRUE;
|
2012-05-13 16:40:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-24 18:19:48 -04:00
|
|
|
gboolean
|
2013-01-24 20:11:49 -05:00
|
|
|
autocomplete_remove(Autocomplete ac, const char * const item)
|
2012-05-13 16:40:39 -04:00
|
|
|
{
|
|
|
|
// reset last found if it points to the item to be removed
|
|
|
|
if (ac->last_found != NULL)
|
2012-10-28 19:38:10 -04:00
|
|
|
if (g_strcmp0(ac->last_found->data, item) == 0)
|
2012-05-13 16:40:39 -04:00
|
|
|
ac->last_found = NULL;
|
|
|
|
|
|
|
|
if (!ac->items) {
|
2012-05-23 18:46:54 -04:00
|
|
|
return FALSE;
|
2012-05-13 16:40:39 -04:00
|
|
|
} else {
|
|
|
|
GSList *curr = ac->items;
|
2012-10-21 15:02:20 -04:00
|
|
|
|
2012-05-13 16:40:39 -04:00
|
|
|
while(curr) {
|
2012-10-28 19:38:10 -04:00
|
|
|
if (g_strcmp0(curr->data, item) == 0) {
|
2012-05-13 18:06:01 -04:00
|
|
|
void *current_item = curr->data;
|
2012-05-13 16:40:39 -04:00
|
|
|
ac->items = g_slist_remove(ac->items, curr->data);
|
2012-10-28 19:38:10 -04:00
|
|
|
free(current_item);
|
2012-10-21 15:02:20 -04:00
|
|
|
|
2012-05-23 18:46:54 -04:00
|
|
|
return TRUE;
|
2012-05-13 16:40:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
curr = g_slist_next(curr);
|
|
|
|
}
|
|
|
|
|
2012-05-23 18:46:54 -04:00
|
|
|
return FALSE;
|
2012-05-13 16:40:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-24 18:19:48 -04:00
|
|
|
GSList *
|
2013-01-24 20:11:49 -05:00
|
|
|
autocomplete_get_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;
|
|
|
|
}
|
|
|
|
|
2012-07-24 18:19:48 -04:00
|
|
|
gchar *
|
2013-01-24 20:11:49 -05:00
|
|
|
autocomplete_complete(Autocomplete ac, gchar *search_str)
|
2012-05-13 16:40:39 -04:00
|
|
|
{
|
|
|
|
gchar *found = NULL;
|
|
|
|
|
|
|
|
// no items to search
|
|
|
|
if (!ac->items)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// first search attempt
|
|
|
|
if (ac->last_found == NULL) {
|
2012-10-21 15:02:20 -04:00
|
|
|
ac->search_str =
|
2012-05-13 16:40:39 -04:00
|
|
|
(gchar *) malloc((strlen(search_str) + 1) * sizeof(gchar));
|
|
|
|
strcpy(ac->search_str, search_str);
|
|
|
|
|
2012-05-19 19:38:16 -04:00
|
|
|
found = _search_from(ac, ac->items);
|
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 {
|
|
|
|
// search from here+1 tp end
|
2012-05-19 19:38:16 -04:00
|
|
|
found = _search_from(ac, g_slist_next(ac->last_found));
|
2012-05-13 16:40:39 -04:00
|
|
|
if (found != NULL)
|
|
|
|
return found;
|
|
|
|
|
|
|
|
// search from beginning
|
2012-05-19 19:38:16 -04:00
|
|
|
found = _search_from(ac, ac->items);
|
2012-05-13 16:40:39 -04:00
|
|
|
if (found != NULL)
|
|
|
|
return found;
|
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);
|
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)
|
|
|
|
{
|
|
|
|
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 = func(inp_cpy);
|
|
|
|
if (found != NULL) {
|
|
|
|
auto_msg = (char *) malloc((len + (strlen(found) + 1)) * sizeof(char));
|
|
|
|
strcpy(auto_msg, command_cpy);
|
|
|
|
strcat(auto_msg, found);
|
|
|
|
free(found);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(command_cpy);
|
|
|
|
|
|
|
|
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)) * sizeof(char));
|
|
|
|
strcpy(auto_msg, command_cpy);
|
|
|
|
strcat(auto_msg, found);
|
|
|
|
free(found);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(command_cpy);
|
|
|
|
|
|
|
|
return auto_msg;
|
|
|
|
}
|
|
|
|
|
2013-06-22 20:23:44 -04:00
|
|
|
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;
|
|
|
|
int quote_count = 0;
|
|
|
|
char *found = NULL;
|
|
|
|
GString *result_str = NULL;
|
|
|
|
|
|
|
|
// copy and null terminate input, count quotes
|
|
|
|
gchar inp_cpy[*size];
|
|
|
|
for (i = 0; i < *size; i++) {
|
|
|
|
if (input[i] == '"') {
|
|
|
|
quote_count++;
|
|
|
|
}
|
|
|
|
inp_cpy[i] = input[i];
|
|
|
|
}
|
|
|
|
inp_cpy[i] = '\0';
|
|
|
|
g_strstrip(inp_cpy);
|
|
|
|
|
|
|
|
// count tokens
|
|
|
|
gchar **tokens = g_strsplit(inp_cpy, " ", 0);
|
|
|
|
int num_tokens = g_strv_length(tokens);
|
|
|
|
|
|
|
|
// if num tokens, or 2 quotes then candidate for autocompletion of last param
|
|
|
|
if (((num_tokens > arg_number - 1) && quote_count == 0) || quote_count == 2) {
|
|
|
|
|
|
|
|
gchar *comp_str = NULL;
|
|
|
|
|
|
|
|
// find start of autocompletion string
|
|
|
|
if (num_tokens > 3 && quote_count == 0) {
|
|
|
|
comp_str = g_strrstr(inp_cpy, tokens[arg_number - 1]);
|
|
|
|
} else {
|
|
|
|
comp_str = g_strrstr(inp_cpy, "\"");
|
|
|
|
comp_str = comp_str + 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// autocomplete param
|
|
|
|
if (comp_str != NULL) {
|
|
|
|
found = func(comp_str);
|
|
|
|
if (found != NULL) {
|
|
|
|
result_str = g_string_new("");
|
|
|
|
g_string_append(result_str, g_strndup(inp_cpy, strlen(inp_cpy) - strlen(comp_str)));
|
|
|
|
g_string_append(result_str, found);
|
|
|
|
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 *
|
2013-01-24 20:11:49 -05:00
|
|
|
_search_from(Autocomplete ac, GSList *curr)
|
2012-05-13 16:40:39 -04:00
|
|
|
{
|
|
|
|
while(curr) {
|
2012-10-21 15:02:20 -04:00
|
|
|
|
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-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
|
|
|
|
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);
|
|
|
|
}
|
2012-05-13 16:40:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
curr = g_slist_next(curr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|