2000-06-17 08:23:59 -04:00
|
|
|
/*
|
|
|
|
completion.c : irssi
|
|
|
|
|
|
|
|
Copyright (C) 2000 Timo Sirainen
|
|
|
|
|
|
|
|
This program 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 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program 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 this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "module.h"
|
|
|
|
#include "signals.h"
|
|
|
|
#include "commands.h"
|
2000-06-17 09:16:42 -04:00
|
|
|
#include "misc.h"
|
2000-06-17 08:23:59 -04:00
|
|
|
#include "lib-config/iconfig.h"
|
|
|
|
#include "settings.h"
|
|
|
|
|
|
|
|
#include "completion.h"
|
|
|
|
|
2000-07-10 19:00:56 -04:00
|
|
|
#define wordreplace_find(word) \
|
|
|
|
iconfig_list_find("replaces", "text", word, "replace")
|
2000-06-17 08:23:59 -04:00
|
|
|
|
|
|
|
#define completion_find(completion) \
|
|
|
|
iconfig_list_find("completions", "short", completion, "long")
|
|
|
|
|
|
|
|
static GList *complist; /* list of commands we're currently completing */
|
2000-06-28 16:00:39 -04:00
|
|
|
static char *last_line;
|
|
|
|
static int last_want_space, last_line_pos;
|
2000-06-17 08:23:59 -04:00
|
|
|
|
|
|
|
#define isseparator_notspace(c) \
|
|
|
|
((c) == ',')
|
|
|
|
|
|
|
|
#define isseparator(c) \
|
2002-01-27 15:45:59 -05:00
|
|
|
(i_isspace(c) || isseparator_notspace(c))
|
2000-06-17 08:23:59 -04:00
|
|
|
|
2000-10-14 10:07:31 -04:00
|
|
|
void chat_completion_init(void);
|
|
|
|
void chat_completion_deinit(void);
|
|
|
|
|
2000-06-17 08:23:59 -04:00
|
|
|
/* Return whole word at specified position in string */
|
|
|
|
char *get_word_at(const char *str, int pos, char **startpos)
|
|
|
|
{
|
|
|
|
const char *start, *end;
|
|
|
|
|
|
|
|
g_return_val_if_fail(str != NULL, NULL);
|
|
|
|
g_return_val_if_fail(pos >= 0, NULL);
|
|
|
|
|
|
|
|
/* get previous word if char at `pos' is space */
|
|
|
|
start = str+pos;
|
|
|
|
while (start > str && isseparator(start[-1])) start--;
|
|
|
|
|
|
|
|
end = start;
|
|
|
|
while (start > str && !isseparator(start[-1])) start--;
|
|
|
|
while (*end != '\0' && !isseparator(*end)) end++;
|
|
|
|
while (*end != '\0' && isseparator_notspace(*end)) end++;
|
|
|
|
|
|
|
|
*startpos = (char *) start;
|
|
|
|
return g_strndup(start, (int) (end-start));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* automatic word completion - called when space/enter is pressed */
|
|
|
|
char *auto_word_complete(const char *line, int *pos)
|
|
|
|
{
|
|
|
|
GString *result;
|
|
|
|
const char *replace;
|
|
|
|
char *word, *wordstart, *ret;
|
|
|
|
int startpos;
|
|
|
|
|
|
|
|
g_return_val_if_fail(line != NULL, NULL);
|
|
|
|
g_return_val_if_fail(pos != NULL, NULL);
|
|
|
|
|
|
|
|
word = get_word_at(line, *pos, &wordstart);
|
|
|
|
startpos = (int) (wordstart-line);
|
|
|
|
|
|
|
|
result = g_string_new(line);
|
|
|
|
g_string_erase(result, startpos, strlen(word));
|
|
|
|
|
|
|
|
/* check for words in autocompletion list */
|
|
|
|
replace = wordreplace_find(word);
|
|
|
|
if (replace == NULL) {
|
|
|
|
ret = NULL;
|
|
|
|
g_string_free(result, TRUE);
|
|
|
|
} else {
|
|
|
|
*pos = startpos+strlen(replace);
|
|
|
|
|
|
|
|
g_string_insert(result, startpos, replace);
|
|
|
|
ret = result->str;
|
|
|
|
g_string_free(result, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free(word);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void free_completions(void)
|
|
|
|
{
|
|
|
|
complist = g_list_first(complist);
|
|
|
|
|
|
|
|
g_list_foreach(complist, (GFunc) g_free, NULL);
|
|
|
|
g_list_free(complist);
|
|
|
|
complist = NULL;
|
|
|
|
|
2000-06-28 16:00:39 -04:00
|
|
|
g_free_and_null(last_line);
|
2000-06-17 08:23:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* manual word completion - called when TAB is pressed */
|
2002-02-10 05:07:12 -05:00
|
|
|
char *word_complete(WINDOW_REC *window, const char *line, int *pos, int erase)
|
2000-06-17 08:23:59 -04:00
|
|
|
{
|
2000-06-28 16:00:39 -04:00
|
|
|
static int startpos = 0, wordlen = 0;
|
2002-02-10 05:07:12 -05:00
|
|
|
int old_startpos, old_wordlen;
|
2000-06-28 16:00:39 -04:00
|
|
|
|
2000-06-17 08:23:59 -04:00
|
|
|
GString *result;
|
|
|
|
char *word, *wordstart, *linestart, *ret;
|
2002-02-10 05:07:12 -05:00
|
|
|
int continue_complete, want_space;
|
2000-06-17 08:23:59 -04:00
|
|
|
|
|
|
|
g_return_val_if_fail(line != NULL, NULL);
|
|
|
|
g_return_val_if_fail(pos != NULL, NULL);
|
|
|
|
|
2002-02-10 05:07:12 -05:00
|
|
|
continue_complete = complist != NULL && *pos == last_line_pos &&
|
|
|
|
strcmp(line, last_line) == 0;
|
2000-06-17 08:23:59 -04:00
|
|
|
|
2002-02-10 05:07:12 -05:00
|
|
|
old_startpos = startpos;
|
|
|
|
old_wordlen = wordlen;
|
|
|
|
|
|
|
|
if (!erase && continue_complete) {
|
|
|
|
word = NULL;
|
|
|
|
linestart = NULL;
|
|
|
|
} else {
|
2000-06-28 16:00:39 -04:00
|
|
|
/* get the word we want to complete */
|
|
|
|
word = get_word_at(line, *pos, &wordstart);
|
|
|
|
startpos = (int) (wordstart-line);
|
|
|
|
wordlen = strlen(word);
|
|
|
|
|
|
|
|
/* get the start of line until the word we're completing */
|
2001-01-27 22:04:44 -05:00
|
|
|
if (isseparator(*line)) {
|
|
|
|
/* empty space at the start of line */
|
|
|
|
if (wordstart == line)
|
|
|
|
wordstart += strlen(wordstart);
|
|
|
|
} else {
|
|
|
|
while (wordstart > line && isseparator(wordstart[-1]))
|
|
|
|
wordstart--;
|
|
|
|
}
|
2000-06-28 16:00:39 -04:00
|
|
|
linestart = g_strndup(line, (int) (wordstart-line));
|
|
|
|
|
|
|
|
/* completions usually add space after the word, that makes
|
|
|
|
things a bit harder. When continuing a completion
|
|
|
|
"/msg nick1 "<tab> we have to cycle to nick2, etc.
|
|
|
|
BUT if we start completion with "/msg "<tab>, we don't
|
|
|
|
want to complete the /msg word, but instead complete empty
|
|
|
|
word with /msg being in linestart. */
|
2002-02-10 05:07:12 -05:00
|
|
|
if (!erase && *pos > 0 && line[*pos-1] == ' ' &&
|
2001-07-29 00:38:31 -04:00
|
|
|
(*linestart == '\0' || wordstart[-1] != ' ')) {
|
2000-06-28 16:00:39 -04:00
|
|
|
char *old;
|
|
|
|
|
|
|
|
old = linestart;
|
|
|
|
linestart = *linestart == '\0' ?
|
|
|
|
g_strdup(word) :
|
|
|
|
g_strconcat(linestart, " ", word, NULL);
|
|
|
|
g_free(old);
|
|
|
|
|
|
|
|
g_free(word);
|
|
|
|
word = g_strdup("");
|
2001-01-27 22:04:44 -05:00
|
|
|
startpos = strlen(linestart)+1;
|
2000-06-28 16:00:39 -04:00
|
|
|
wordlen = 0;
|
|
|
|
}
|
|
|
|
|
2002-02-10 05:07:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (erase) {
|
|
|
|
signal_emit("complete erase", 3, window, word, linestart);
|
|
|
|
|
|
|
|
if (!continue_complete)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* jump to next completion */
|
|
|
|
word = NULL;
|
|
|
|
linestart = NULL;
|
|
|
|
startpos = old_startpos;
|
|
|
|
wordlen = old_wordlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (continue_complete) {
|
|
|
|
/* complete from old list */
|
|
|
|
complist = complist->next != NULL ? complist->next :
|
|
|
|
g_list_first(complist);
|
|
|
|
want_space = last_want_space;
|
|
|
|
} else {
|
|
|
|
/* get new completion list */
|
|
|
|
free_completions();
|
|
|
|
|
2000-06-17 11:58:40 -04:00
|
|
|
want_space = TRUE;
|
2000-06-17 12:03:43 -04:00
|
|
|
signal_emit("complete word", 5, &complist, window, word, linestart, &want_space);
|
2000-06-17 11:58:40 -04:00
|
|
|
last_want_space = want_space;
|
2000-06-17 08:23:59 -04:00
|
|
|
}
|
|
|
|
|
2002-02-10 05:07:12 -05:00
|
|
|
g_free(linestart);
|
|
|
|
g_free(word);
|
|
|
|
|
2000-06-17 08:23:59 -04:00
|
|
|
if (complist == NULL)
|
2000-06-28 16:00:39 -04:00
|
|
|
return NULL;
|
2000-06-17 08:23:59 -04:00
|
|
|
|
2000-06-28 16:00:39 -04:00
|
|
|
/* word completed */
|
2000-06-30 15:51:53 -04:00
|
|
|
*pos = startpos+strlen(complist->data);
|
2000-06-17 08:23:59 -04:00
|
|
|
|
2000-06-28 16:00:39 -04:00
|
|
|
/* replace the word in line - we need to return
|
|
|
|
a full new line */
|
|
|
|
result = g_string_new(line);
|
2001-07-14 14:19:08 -04:00
|
|
|
g_string_erase(result, startpos, wordlen);
|
2000-06-28 16:00:39 -04:00
|
|
|
g_string_insert(result, startpos, complist->data);
|
2000-06-17 08:23:59 -04:00
|
|
|
|
2000-06-30 15:51:53 -04:00
|
|
|
if (want_space) {
|
|
|
|
if (!isseparator(result->str[*pos]))
|
|
|
|
g_string_insert_c(result, *pos, ' ');
|
|
|
|
(*pos)++;
|
|
|
|
}
|
2000-06-17 08:23:59 -04:00
|
|
|
|
2000-06-28 16:00:39 -04:00
|
|
|
wordlen = strlen(complist->data);
|
|
|
|
last_line_pos = *pos;
|
|
|
|
g_free_not_null(last_line);
|
|
|
|
last_line = g_strdup(result->str);
|
|
|
|
|
|
|
|
ret = result->str;
|
|
|
|
g_string_free(result, FALSE);
|
2000-06-17 08:23:59 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2001-07-29 05:17:53 -04:00
|
|
|
#define IS_CURRENT_DIR(dir) \
|
|
|
|
((dir)[0] == '.' && ((dir)[1] == '\0' || (dir)[1] == G_DIR_SEPARATOR))
|
|
|
|
|
|
|
|
#define USE_DEFAULT_PATH(path, default_path) \
|
|
|
|
((!g_path_is_absolute(path) || IS_CURRENT_DIR(path)) && \
|
|
|
|
default_path != NULL)
|
|
|
|
|
|
|
|
GList *list_add_file(GList *list, const char *name, const char *default_path)
|
2000-06-17 11:58:40 -04:00
|
|
|
{
|
|
|
|
struct stat statbuf;
|
2000-06-18 06:05:14 -04:00
|
|
|
char *fname;
|
2000-06-17 11:58:40 -04:00
|
|
|
|
2000-06-28 11:36:57 -04:00
|
|
|
g_return_val_if_fail(name != NULL, NULL);
|
|
|
|
|
2000-06-18 06:05:14 -04:00
|
|
|
fname = convert_home(name);
|
2001-07-29 05:17:53 -04:00
|
|
|
if (USE_DEFAULT_PATH(fname, default_path)) {
|
|
|
|
g_free(fname);
|
|
|
|
fname = g_strconcat(default_path, G_DIR_SEPARATOR_S,
|
|
|
|
name, NULL);
|
|
|
|
}
|
2000-06-18 06:05:14 -04:00
|
|
|
if (stat(fname, &statbuf) == 0) {
|
|
|
|
list = g_list_append(list, !S_ISDIR(statbuf.st_mode) ? g_strdup(name) :
|
|
|
|
g_strconcat(name, G_DIR_SEPARATOR_S, NULL));
|
|
|
|
}
|
2000-06-17 11:58:40 -04:00
|
|
|
|
2000-06-18 06:05:14 -04:00
|
|
|
g_free(fname);
|
|
|
|
return list;
|
2000-06-17 11:58:40 -04:00
|
|
|
}
|
|
|
|
|
2001-07-29 05:17:53 -04:00
|
|
|
GList *filename_complete(const char *path, const char *default_path)
|
2000-06-17 11:58:40 -04:00
|
|
|
{
|
|
|
|
GList *list;
|
|
|
|
DIR *dirp;
|
|
|
|
struct dirent *dp;
|
2002-04-09 22:53:06 -04:00
|
|
|
const char *basename;
|
|
|
|
char *realpath, *dir, *name;
|
2000-06-17 11:58:40 -04:00
|
|
|
int len;
|
|
|
|
|
2000-06-28 11:36:57 -04:00
|
|
|
g_return_val_if_fail(path != NULL, NULL);
|
|
|
|
|
2000-06-17 11:58:40 -04:00
|
|
|
list = NULL;
|
|
|
|
|
2000-06-25 17:59:30 -04:00
|
|
|
/* get directory part of the path - expand ~/ */
|
|
|
|
realpath = convert_home(path);
|
2001-07-29 05:17:53 -04:00
|
|
|
if (USE_DEFAULT_PATH(realpath, default_path)) {
|
|
|
|
g_free(realpath);
|
|
|
|
realpath = g_strconcat(default_path, G_DIR_SEPARATOR_S,
|
|
|
|
path, NULL);
|
|
|
|
}
|
2000-06-25 17:59:30 -04:00
|
|
|
|
|
|
|
/* open directory for reading */
|
2001-07-29 05:17:53 -04:00
|
|
|
dir = g_dirname(realpath);
|
2000-06-17 11:58:40 -04:00
|
|
|
dirp = opendir(dir);
|
2000-06-18 06:05:14 -04:00
|
|
|
g_free(dir);
|
2001-07-29 05:17:53 -04:00
|
|
|
g_free(realpath);
|
|
|
|
|
|
|
|
if (dirp == NULL)
|
|
|
|
return NULL;
|
2000-06-18 06:05:14 -04:00
|
|
|
|
|
|
|
dir = g_dirname(path);
|
2001-07-29 05:17:53 -04:00
|
|
|
if (*dir == G_DIR_SEPARATOR && dir[1] == '\0') {
|
|
|
|
/* completing file in root directory */
|
|
|
|
*dir = '\0';
|
|
|
|
} else if (IS_CURRENT_DIR(dir) && !IS_CURRENT_DIR(path)) {
|
|
|
|
/* completing file in default_path
|
|
|
|
(path not set, and leave it that way) */
|
|
|
|
g_free_and_null(dir);
|
|
|
|
}
|
|
|
|
|
2000-06-18 06:05:14 -04:00
|
|
|
basename = g_basename(path);
|
2000-06-17 11:58:40 -04:00
|
|
|
len = strlen(basename);
|
|
|
|
|
2000-06-25 17:59:30 -04:00
|
|
|
/* add all files in directory to completion list */
|
2000-06-17 11:58:40 -04:00
|
|
|
while ((dp = readdir(dirp)) != NULL) {
|
2000-06-18 06:05:14 -04:00
|
|
|
if (dp->d_name[0] == '.') {
|
|
|
|
if (dp->d_name[1] == '\0' ||
|
|
|
|
(dp->d_name[1] == '.' && dp->d_name[2] == '\0'))
|
|
|
|
continue; /* skip . and .. */
|
|
|
|
|
|
|
|
if (basename[0] != '.')
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2000-06-17 11:58:40 -04:00
|
|
|
if (len == 0 || strncmp(dp->d_name, basename, len) == 0) {
|
2001-07-29 05:17:53 -04:00
|
|
|
name = dir == NULL ? g_strdup(dp->d_name) :
|
|
|
|
g_strdup_printf("%s"G_DIR_SEPARATOR_S"%s", dir, dp->d_name);
|
|
|
|
list = list_add_file(list, name, default_path);
|
2000-06-17 11:58:40 -04:00
|
|
|
g_free(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dirp);
|
|
|
|
|
2001-07-29 05:17:53 -04:00
|
|
|
g_free_not_null(dir);
|
2000-06-17 11:58:40 -04:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2000-06-17 08:23:59 -04:00
|
|
|
static GList *completion_get_settings(const char *key)
|
|
|
|
{
|
|
|
|
GList *complist;
|
|
|
|
GSList *tmp, *sets;
|
|
|
|
int len;
|
|
|
|
|
2000-06-28 11:36:57 -04:00
|
|
|
g_return_val_if_fail(key != NULL, NULL);
|
|
|
|
|
2000-06-17 08:23:59 -04:00
|
|
|
sets = settings_get_sorted();
|
|
|
|
|
|
|
|
len = strlen(key);
|
|
|
|
complist = NULL;
|
|
|
|
for (tmp = sets; tmp != NULL; tmp = tmp->next) {
|
|
|
|
SETTINGS_REC *rec = tmp->data;
|
|
|
|
|
|
|
|
if (g_strncasecmp(rec->key, key, len) == 0)
|
2000-06-17 09:16:42 -04:00
|
|
|
complist = g_list_insert_sorted(complist, g_strdup(rec->key), (GCompareFunc) g_istr_cmp);
|
2000-06-17 08:23:59 -04:00
|
|
|
}
|
|
|
|
g_slist_free(sets);
|
|
|
|
return complist;
|
|
|
|
}
|
|
|
|
|
2000-06-25 18:14:09 -04:00
|
|
|
static GList *completion_get_bool_settings(const char *key)
|
|
|
|
{
|
|
|
|
GList *complist;
|
|
|
|
GSList *tmp, *sets;
|
|
|
|
int len;
|
|
|
|
|
2000-06-28 11:36:57 -04:00
|
|
|
g_return_val_if_fail(key != NULL, NULL);
|
|
|
|
|
2000-06-25 18:14:09 -04:00
|
|
|
sets = settings_get_sorted();
|
|
|
|
|
|
|
|
len = strlen(key);
|
|
|
|
complist = NULL;
|
|
|
|
for (tmp = sets; tmp != NULL; tmp = tmp->next) {
|
|
|
|
SETTINGS_REC *rec = tmp->data;
|
|
|
|
|
|
|
|
if (rec->type == SETTING_TYPE_BOOLEAN &&
|
|
|
|
g_strncasecmp(rec->key, key, len) == 0)
|
|
|
|
complist = g_list_insert_sorted(complist, g_strdup(rec->key), (GCompareFunc) g_istr_cmp);
|
|
|
|
}
|
|
|
|
g_slist_free(sets);
|
|
|
|
return complist;
|
|
|
|
}
|
|
|
|
|
2001-05-21 20:29:31 -04:00
|
|
|
static GList *completion_get_aliases(const char *alias, char cmdchar)
|
|
|
|
{
|
|
|
|
CONFIG_NODE *node;
|
|
|
|
GList *complist;
|
|
|
|
GSList *tmp;
|
|
|
|
char *word;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
g_return_val_if_fail(alias != NULL, NULL);
|
|
|
|
|
|
|
|
/* get list of aliases from mainconfig */
|
|
|
|
node = iconfig_node_traverse("aliases", FALSE);
|
2002-02-02 12:37:44 -05:00
|
|
|
tmp = node == NULL ? NULL : config_node_first(node->value);
|
2001-05-21 20:29:31 -04:00
|
|
|
|
|
|
|
len = strlen(alias);
|
|
|
|
complist = NULL;
|
2002-02-02 12:37:44 -05:00
|
|
|
for (; tmp != NULL; tmp = config_node_next(tmp)) {
|
2001-05-21 20:29:31 -04:00
|
|
|
CONFIG_NODE *node = tmp->data;
|
|
|
|
|
|
|
|
if (node->type != NODE_TYPE_KEY)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (g_strncasecmp(node->key, alias, len) == 0) {
|
|
|
|
word = g_strdup_printf("%c%s", cmdchar, node->key);
|
|
|
|
/* add matching alias to completion list, aliases will
|
|
|
|
be appended after command completions and kept in
|
|
|
|
uppercase to show it's an alias */
|
|
|
|
if (glist_find_icase_string(complist, word) == NULL)
|
|
|
|
complist = g_list_insert_sorted(complist, word, (GCompareFunc) g_istr_cmp);
|
|
|
|
else
|
|
|
|
g_free(word);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return complist;
|
|
|
|
}
|
|
|
|
|
2000-06-17 08:23:59 -04:00
|
|
|
static GList *completion_get_commands(const char *cmd, char cmdchar)
|
|
|
|
{
|
|
|
|
GList *complist;
|
|
|
|
GSList *tmp;
|
2000-06-17 09:04:19 -04:00
|
|
|
char *word;
|
2000-06-17 08:23:59 -04:00
|
|
|
int len;
|
|
|
|
|
2000-06-28 11:36:57 -04:00
|
|
|
g_return_val_if_fail(cmd != NULL, NULL);
|
|
|
|
|
2000-06-17 08:23:59 -04:00
|
|
|
len = strlen(cmd);
|
|
|
|
complist = NULL;
|
|
|
|
for (tmp = commands; tmp != NULL; tmp = tmp->next) {
|
|
|
|
COMMAND_REC *rec = tmp->data;
|
|
|
|
|
|
|
|
if (strchr(rec->cmd, ' ') != NULL)
|
|
|
|
continue;
|
|
|
|
|
2000-06-17 09:04:19 -04:00
|
|
|
if (g_strncasecmp(rec->cmd, cmd, len) == 0) {
|
2000-06-30 15:51:53 -04:00
|
|
|
word = cmdchar == '\0' ? g_strdup(rec->cmd) :
|
|
|
|
g_strdup_printf("%c%s", cmdchar, rec->cmd);
|
2000-06-17 09:04:19 -04:00
|
|
|
if (glist_find_icase_string(complist, word) == NULL)
|
2000-06-17 09:16:42 -04:00
|
|
|
complist = g_list_insert_sorted(complist, word, (GCompareFunc) g_istr_cmp);
|
2000-06-17 09:04:19 -04:00
|
|
|
else
|
|
|
|
g_free(word);
|
|
|
|
}
|
2000-06-17 08:23:59 -04:00
|
|
|
}
|
|
|
|
return complist;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GList *completion_get_subcommands(const char *cmd)
|
|
|
|
{
|
|
|
|
GList *complist;
|
|
|
|
GSList *tmp;
|
|
|
|
char *spacepos;
|
|
|
|
int len, skip;
|
|
|
|
|
2000-06-28 11:36:57 -04:00
|
|
|
g_return_val_if_fail(cmd != NULL, NULL);
|
|
|
|
|
2000-06-17 08:23:59 -04:00
|
|
|
/* get the number of chars to skip at the start of command. */
|
2000-06-17 09:04:19 -04:00
|
|
|
spacepos = strrchr(cmd, ' ');
|
2000-06-28 16:00:39 -04:00
|
|
|
skip = spacepos == NULL ? strlen(cmd)+1 :
|
2000-06-17 08:23:59 -04:00
|
|
|
((int) (spacepos-cmd) + 1);
|
|
|
|
|
|
|
|
len = strlen(cmd);
|
|
|
|
complist = NULL;
|
|
|
|
for (tmp = commands; tmp != NULL; tmp = tmp->next) {
|
|
|
|
COMMAND_REC *rec = tmp->data;
|
|
|
|
|
2000-11-18 10:31:28 -05:00
|
|
|
if ((int)strlen(rec->cmd) < len)
|
2000-06-17 08:23:59 -04:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (strchr(rec->cmd+len, ' ') != NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (g_strncasecmp(rec->cmd, cmd, len) == 0)
|
2000-06-17 09:16:42 -04:00
|
|
|
complist = g_list_insert_sorted(complist, g_strdup(rec->cmd+skip), (GCompareFunc) g_istr_cmp);
|
2000-06-17 08:23:59 -04:00
|
|
|
}
|
|
|
|
return complist;
|
|
|
|
}
|
|
|
|
|
2000-06-28 11:36:57 -04:00
|
|
|
GList *completion_get_options(const char *cmd, const char *option)
|
|
|
|
{
|
|
|
|
COMMAND_REC *rec;
|
|
|
|
GList *list;
|
|
|
|
char **tmp;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
g_return_val_if_fail(cmd != NULL, NULL);
|
|
|
|
g_return_val_if_fail(option != NULL, NULL);
|
|
|
|
|
|
|
|
rec = command_find(cmd);
|
2000-06-28 16:00:39 -04:00
|
|
|
if (rec == NULL || rec->options == NULL) return NULL;
|
2000-06-28 11:36:57 -04:00
|
|
|
|
|
|
|
list = NULL;
|
|
|
|
len = strlen(option);
|
|
|
|
for (tmp = rec->options; *tmp != NULL; tmp++) {
|
2000-07-09 09:28:14 -04:00
|
|
|
const char *optname = *tmp + iscmdtype(**tmp);
|
|
|
|
|
|
|
|
if (len == 0 || g_strncasecmp(optname, option, len) == 0)
|
|
|
|
list = g_list_append(list, g_strconcat("-", optname, NULL));
|
2000-06-28 11:36:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2000-06-17 11:58:40 -04:00
|
|
|
/* split the line to command and arguments */
|
2000-06-25 18:59:49 -04:00
|
|
|
static char *line_get_command(const char *line, char **args, int aliases)
|
2000-06-17 11:58:40 -04:00
|
|
|
{
|
|
|
|
const char *ptr, *cmdargs;
|
|
|
|
char *cmd, *checkcmd;
|
|
|
|
|
2000-06-28 11:36:57 -04:00
|
|
|
g_return_val_if_fail(line != NULL, NULL);
|
|
|
|
g_return_val_if_fail(args != NULL, NULL);
|
|
|
|
|
2000-06-25 18:59:49 -04:00
|
|
|
cmd = checkcmd = NULL; *args = "";
|
2000-06-17 11:58:40 -04:00
|
|
|
cmdargs = NULL; ptr = line;
|
|
|
|
|
|
|
|
do {
|
|
|
|
ptr = strchr(ptr, ' ');
|
|
|
|
if (ptr == NULL) {
|
|
|
|
checkcmd = g_strdup(line);
|
|
|
|
cmdargs = "";
|
|
|
|
} else {
|
|
|
|
checkcmd = g_strndup(line, (int) (ptr-line));
|
|
|
|
|
2002-01-27 15:45:59 -05:00
|
|
|
while (i_isspace(*ptr)) ptr++;
|
2000-06-17 11:58:40 -04:00
|
|
|
cmdargs = ptr;
|
|
|
|
}
|
|
|
|
|
2000-06-25 18:59:49 -04:00
|
|
|
if (aliases ? !alias_find(checkcmd) :
|
|
|
|
!command_find(checkcmd)) {
|
2000-06-17 11:58:40 -04:00
|
|
|
/* not found, use the previous */
|
|
|
|
g_free(checkcmd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* found, check if it has subcommands */
|
|
|
|
g_free_not_null(cmd);
|
2000-06-25 18:59:49 -04:00
|
|
|
if (!aliases)
|
|
|
|
cmd = checkcmd;
|
|
|
|
else {
|
|
|
|
cmd = g_strdup(alias_find(checkcmd));
|
|
|
|
g_free(checkcmd);
|
|
|
|
}
|
2000-06-17 11:58:40 -04:00
|
|
|
*args = (char *) cmdargs;
|
|
|
|
} while (ptr != NULL);
|
|
|
|
|
2001-07-14 16:06:00 -04:00
|
|
|
if (cmd != NULL)
|
|
|
|
g_strdown(cmd);
|
2000-06-17 11:58:40 -04:00
|
|
|
return cmd;
|
|
|
|
}
|
|
|
|
|
2000-06-25 18:59:49 -04:00
|
|
|
static char *expand_aliases(const char *line)
|
|
|
|
{
|
|
|
|
char *cmd, *args, *ret;
|
|
|
|
|
2000-06-28 11:36:57 -04:00
|
|
|
g_return_val_if_fail(line != NULL, NULL);
|
|
|
|
|
2000-06-25 18:59:49 -04:00
|
|
|
cmd = line_get_command(line, &args, TRUE);
|
|
|
|
if (cmd == NULL) return g_strdup(line);
|
|
|
|
if (*args == '\0') return cmd;
|
|
|
|
|
|
|
|
ret = g_strconcat(cmd, " ", args, NULL);
|
|
|
|
g_free(cmd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2000-06-17 12:03:43 -04:00
|
|
|
static void sig_complete_word(GList **list, WINDOW_REC *window,
|
2002-02-10 05:07:12 -05:00
|
|
|
const char *word, const char *linestart,
|
|
|
|
int *want_space)
|
2000-06-17 08:23:59 -04:00
|
|
|
{
|
|
|
|
const char *newword, *cmdchars;
|
2000-06-25 18:59:49 -04:00
|
|
|
char *signal, *cmd, *args, *line;
|
2000-06-17 08:23:59 -04:00
|
|
|
|
2000-06-17 11:58:40 -04:00
|
|
|
g_return_if_fail(list != NULL);
|
2000-06-17 08:23:59 -04:00
|
|
|
g_return_if_fail(word != NULL);
|
2000-06-17 11:58:40 -04:00
|
|
|
g_return_if_fail(linestart != NULL);
|
2000-06-17 08:23:59 -04:00
|
|
|
|
|
|
|
/* check against "completion words" list */
|
|
|
|
newword = completion_find(word);
|
|
|
|
if (newword != NULL) {
|
|
|
|
*list = g_list_append(*list, g_strdup(newword));
|
|
|
|
|
|
|
|
signal_stop();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* command completion? */
|
|
|
|
cmdchars = settings_get_str("cmdchars");
|
2001-08-28 07:12:03 -04:00
|
|
|
if (*word != '\0' && *linestart == '\0' && strchr(cmdchars, *word)) {
|
2000-06-17 08:23:59 -04:00
|
|
|
/* complete /command */
|
|
|
|
*list = completion_get_commands(word+1, *word);
|
|
|
|
|
2001-05-21 20:29:31 -04:00
|
|
|
/* complete aliases, too */
|
|
|
|
*list = g_list_concat(*list,
|
|
|
|
completion_get_aliases(word+1, *word));
|
|
|
|
|
2000-06-17 08:23:59 -04:00
|
|
|
if (*list != NULL) signal_stop();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2000-06-25 18:59:49 -04:00
|
|
|
/* check only for /command completions from now on */
|
2001-07-25 21:38:46 -04:00
|
|
|
if (*linestart == '\0')
|
|
|
|
return;
|
|
|
|
|
2000-06-25 18:59:49 -04:00
|
|
|
cmdchars = strchr(cmdchars, *linestart);
|
|
|
|
if (cmdchars == NULL) return;
|
|
|
|
|
|
|
|
/* check if there's aliases */
|
|
|
|
line = linestart[1] == *cmdchars ? g_strdup(linestart+2) :
|
|
|
|
expand_aliases(linestart+1);
|
2000-06-17 08:23:59 -04:00
|
|
|
|
2000-06-25 18:59:49 -04:00
|
|
|
cmd = line_get_command(line, &args, FALSE);
|
2000-06-28 11:36:57 -04:00
|
|
|
if (cmd == NULL) {
|
|
|
|
g_free(line);
|
|
|
|
return;
|
|
|
|
}
|
2000-06-17 11:58:40 -04:00
|
|
|
|
2000-06-28 11:36:57 -04:00
|
|
|
/* we're completing -option? */
|
|
|
|
if (*word == '-') {
|
2000-10-14 10:32:53 -04:00
|
|
|
*list = completion_get_options(cmd, word+1);
|
2000-06-25 18:59:49 -04:00
|
|
|
g_free(cmd);
|
2000-06-28 11:36:57 -04:00
|
|
|
g_free(line);
|
|
|
|
return;
|
2000-06-17 11:58:40 -04:00
|
|
|
}
|
2000-06-25 18:59:49 -04:00
|
|
|
|
2000-06-28 11:36:57 -04:00
|
|
|
/* complete parameters */
|
|
|
|
signal = g_strconcat("complete command ", cmd, NULL);
|
|
|
|
signal_emit(signal, 5, list, window, word, args, want_space);
|
|
|
|
|
2000-10-14 10:32:53 -04:00
|
|
|
if (command_have_sub(line)) {
|
|
|
|
/* complete subcommand */
|
|
|
|
g_free(cmd);
|
|
|
|
cmd = g_strconcat(line, " ", word, NULL);
|
|
|
|
*list = g_list_concat(completion_get_subcommands(cmd), *list);
|
|
|
|
|
|
|
|
if (*list != NULL) signal_stop();
|
|
|
|
}
|
|
|
|
|
2000-06-28 11:36:57 -04:00
|
|
|
g_free(signal);
|
|
|
|
g_free(cmd);
|
|
|
|
|
2000-06-25 18:59:49 -04:00
|
|
|
g_free(line);
|
2000-06-17 08:23:59 -04:00
|
|
|
}
|
|
|
|
|
2002-02-10 05:07:12 -05:00
|
|
|
static void sig_complete_erase(WINDOW_REC *window, const char *word,
|
|
|
|
const char *linestart)
|
|
|
|
{
|
|
|
|
const char *cmdchars;
|
|
|
|
char *line, *cmd, *args, *signal;
|
|
|
|
|
|
|
|
if (*linestart == '\0')
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* we only want to check for commands */
|
|
|
|
cmdchars = settings_get_str("cmdchars");
|
|
|
|
cmdchars = strchr(cmdchars, *linestart);
|
|
|
|
if (cmdchars == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* check if there's aliases */
|
|
|
|
line = linestart[1] == *cmdchars ? g_strdup(linestart+2) :
|
|
|
|
expand_aliases(linestart+1);
|
|
|
|
|
|
|
|
cmd = line_get_command(line, &args, FALSE);
|
|
|
|
if (cmd == NULL) {
|
|
|
|
g_free(line);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
signal = g_strconcat("complete erase command ", cmd, NULL);
|
|
|
|
signal_emit(signal, 3, window, word, args);
|
|
|
|
|
|
|
|
g_free(signal);
|
|
|
|
g_free(cmd);
|
|
|
|
g_free(line);
|
|
|
|
}
|
|
|
|
|
2000-06-25 18:14:09 -04:00
|
|
|
static void sig_complete_set(GList **list, WINDOW_REC *window,
|
|
|
|
const char *word, const char *line, int *want_space)
|
|
|
|
{
|
|
|
|
g_return_if_fail(list != NULL);
|
|
|
|
g_return_if_fail(word != NULL);
|
|
|
|
g_return_if_fail(line != NULL);
|
|
|
|
|
|
|
|
if (*line != '\0') return;
|
|
|
|
|
|
|
|
*list = completion_get_settings(word);
|
2000-06-30 15:51:53 -04:00
|
|
|
if (*list != NULL) signal_stop();
|
2000-06-25 18:14:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void sig_complete_toggle(GList **list, WINDOW_REC *window,
|
|
|
|
const char *word, const char *line, int *want_space)
|
|
|
|
{
|
|
|
|
g_return_if_fail(list != NULL);
|
|
|
|
g_return_if_fail(word != NULL);
|
|
|
|
g_return_if_fail(line != NULL);
|
|
|
|
|
|
|
|
if (*line != '\0') return;
|
|
|
|
|
|
|
|
*list = completion_get_bool_settings(word);
|
2000-06-30 15:51:53 -04:00
|
|
|
if (*list != NULL) signal_stop();
|
2000-06-25 18:14:09 -04:00
|
|
|
}
|
|
|
|
|
2000-06-17 12:10:29 -04:00
|
|
|
/* first argument of command is file name - complete it */
|
|
|
|
static void sig_complete_filename(GList **list, WINDOW_REC *window,
|
|
|
|
const char *word, const char *line, int *want_space)
|
|
|
|
{
|
|
|
|
g_return_if_fail(list != NULL);
|
|
|
|
g_return_if_fail(word != NULL);
|
|
|
|
g_return_if_fail(line != NULL);
|
|
|
|
|
|
|
|
if (*line != '\0') return;
|
|
|
|
|
2001-07-29 05:17:53 -04:00
|
|
|
*list = filename_complete(word, NULL);
|
2000-06-17 12:10:29 -04:00
|
|
|
if (*list != NULL) {
|
|
|
|
*want_space = FALSE;
|
|
|
|
signal_stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-06-30 15:51:53 -04:00
|
|
|
/* first argument of command is .. command :) (/HELP command) */
|
|
|
|
static void sig_complete_command(GList **list, WINDOW_REC *window,
|
|
|
|
const char *word, const char *line, int *want_space)
|
|
|
|
{
|
|
|
|
char *cmd;
|
|
|
|
|
|
|
|
g_return_if_fail(list != NULL);
|
|
|
|
g_return_if_fail(word != NULL);
|
|
|
|
g_return_if_fail(line != NULL);
|
|
|
|
|
|
|
|
if (*line == '\0') {
|
|
|
|
/* complete base command */
|
|
|
|
*list = completion_get_commands(word, '\0');
|
2000-07-08 20:03:46 -04:00
|
|
|
} else if (command_have_sub(line)) {
|
2000-06-30 15:51:53 -04:00
|
|
|
/* complete subcommand */
|
|
|
|
cmd = g_strconcat(line, " ", word, NULL);
|
|
|
|
*list = completion_get_subcommands(cmd);
|
|
|
|
g_free(cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*list != NULL) signal_stop();
|
|
|
|
}
|
|
|
|
|
2000-06-17 08:23:59 -04:00
|
|
|
void completion_init(void)
|
|
|
|
{
|
|
|
|
complist = NULL;
|
2000-06-28 16:00:39 -04:00
|
|
|
last_line = NULL; last_line_pos = -1;
|
2000-06-17 08:23:59 -04:00
|
|
|
|
2000-10-14 10:07:31 -04:00
|
|
|
chat_completion_init();
|
|
|
|
|
2000-07-26 13:18:52 -04:00
|
|
|
signal_add_first("complete word", (SIGNAL_FUNC) sig_complete_word);
|
2002-02-10 05:07:12 -05:00
|
|
|
signal_add_first("complete erase", (SIGNAL_FUNC) sig_complete_erase);
|
2000-06-25 18:14:09 -04:00
|
|
|
signal_add("complete command set", (SIGNAL_FUNC) sig_complete_set);
|
|
|
|
signal_add("complete command toggle", (SIGNAL_FUNC) sig_complete_toggle);
|
2002-05-02 18:39:06 -04:00
|
|
|
signal_add("complete command load", (SIGNAL_FUNC) sig_complete_filename);
|
2000-06-17 12:10:29 -04:00
|
|
|
signal_add("complete command cat", (SIGNAL_FUNC) sig_complete_filename);
|
|
|
|
signal_add("complete command save", (SIGNAL_FUNC) sig_complete_filename);
|
2000-06-28 18:29:06 -04:00
|
|
|
signal_add("complete command reload", (SIGNAL_FUNC) sig_complete_filename);
|
2000-06-17 12:10:29 -04:00
|
|
|
signal_add("complete command rawlog open", (SIGNAL_FUNC) sig_complete_filename);
|
|
|
|
signal_add("complete command rawlog save", (SIGNAL_FUNC) sig_complete_filename);
|
2000-06-30 15:51:53 -04:00
|
|
|
signal_add("complete command help", (SIGNAL_FUNC) sig_complete_command);
|
2000-06-17 08:23:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void completion_deinit(void)
|
|
|
|
{
|
|
|
|
free_completions();
|
|
|
|
|
2000-10-14 10:07:31 -04:00
|
|
|
chat_completion_deinit();
|
|
|
|
|
2000-06-17 12:03:43 -04:00
|
|
|
signal_remove("complete word", (SIGNAL_FUNC) sig_complete_word);
|
2002-02-10 05:07:12 -05:00
|
|
|
signal_remove("complete erase", (SIGNAL_FUNC) sig_complete_erase);
|
2000-06-25 18:14:09 -04:00
|
|
|
signal_remove("complete command set", (SIGNAL_FUNC) sig_complete_set);
|
|
|
|
signal_remove("complete command toggle", (SIGNAL_FUNC) sig_complete_toggle);
|
2002-05-02 18:39:06 -04:00
|
|
|
signal_remove("complete command load", (SIGNAL_FUNC) sig_complete_filename);
|
2000-06-17 12:10:29 -04:00
|
|
|
signal_remove("complete command cat", (SIGNAL_FUNC) sig_complete_filename);
|
|
|
|
signal_remove("complete command save", (SIGNAL_FUNC) sig_complete_filename);
|
2000-06-28 18:29:06 -04:00
|
|
|
signal_remove("complete command reload", (SIGNAL_FUNC) sig_complete_filename);
|
2000-06-17 12:10:29 -04:00
|
|
|
signal_remove("complete command rawlog open", (SIGNAL_FUNC) sig_complete_filename);
|
|
|
|
signal_remove("complete command rawlog save", (SIGNAL_FUNC) sig_complete_filename);
|
2000-06-30 15:51:53 -04:00
|
|
|
signal_remove("complete command help", (SIGNAL_FUNC) sig_complete_command);
|
2000-06-17 08:23:59 -04:00
|
|
|
}
|
2001-05-21 20:29:31 -04:00
|
|
|
|
|
|
|
|