*/
#include "module.h"
+#include "module-formats.h"
#include "signals.h"
#include "commands.h"
+#include "levels.h"
#include "misc.h"
#include "lib-config/iconfig.h"
#include "settings.h"
#include "completion.h"
-
-#define wordreplace_find(word) \
- iconfig_list_find("replaces", "text", word, "replace")
-
-#define completion_find(completion) \
- iconfig_list_find("completions", "short", completion, "long")
+#include "printtext.h"
static GList *complist; /* list of commands we're currently completing */
static char *last_line;
((c) == ',')
#define isseparator(c) \
- (i_isspace(c) || isseparator_notspace(c))
+ ((c) == ' ' || isseparator_notspace(c))
void chat_completion_init(void);
void chat_completion_deinit(void);
+static const char *completion_find(const char *key, int automatic)
+{
+ CONFIG_NODE *node;
+
+ node = iconfig_node_traverse("completions", FALSE);
+ if (node == NULL || node->type != NODE_TYPE_BLOCK)
+ return NULL;
+
+ node = config_node_section(node, key, -1);
+ if (node == NULL)
+ return NULL;
+
+ if (automatic && !config_node_get_bool(node, "auto", FALSE))
+ return NULL;
+
+ return config_node_get_str(node, "value", NULL);
+}
+
/* Return whole word at specified position in string */
char *get_word_at(const char *str, int pos, char **startpos)
{
g_string_erase(result, startpos, strlen(word));
/* check for words in autocompletion list */
- replace = wordreplace_find(word);
+ replace = completion_find(word, TRUE);
if (replace == NULL) {
ret = NULL;
g_string_free(result, TRUE);
} else {
checkcmd = g_strndup(line, (int) (ptr-line));
- while (i_isspace(*ptr)) ptr++;
+ while (*ptr == ' ') ptr++;
cmdargs = ptr;
}
g_return_if_fail(linestart != NULL);
/* check against "completion words" list */
- newword = completion_find(word);
+ newword = completion_find(word, FALSE);
if (newword != NULL) {
*list = g_list_append(*list, g_strdup(newword));
return;
}
+ if (*linestart != '\0' && (*word == '/' || *word == '~')) {
+ /* quite likely filename completion */
+ *list = g_list_concat(*list, filename_complete(word, NULL));
+ if (*list != NULL) {
+ *want_space = FALSE;
+ signal_stop();
+ return;
+ }
+ }
+
/* command completion? */
cmdchars = settings_get_str("cmdchars");
if (*word != '\0' && *linestart == '\0' && strchr(cmdchars, *word)) {
if (*list != NULL) signal_stop();
}
+static void cmd_completion(const char *data)
+{
+ GHashTable *optlist;
+ CONFIG_NODE *node;
+ GSList *tmp;
+ char *key, *value;
+ void *free_arg;
+ int len;
+
+ if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_OPTIONS |
+ PARAM_FLAG_GETREST,
+ "completion", &optlist, &key, &value))
+ return;
+
+ node = iconfig_node_traverse("completions", *value != '\0');
+ if (node != NULL && node->type != NODE_TYPE_BLOCK) {
+ /* FIXME: remove after 0.8.5 */
+ iconfig_node_remove(mainconfig->mainnode, node);
+ node = iconfig_node_traverse("completions", *value != '\0');
+ }
+
+ if (node == NULL || (node->value == NULL && *value == '\0')) {
+ printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
+ TXT_NO_COMPLETIONS);
+ cmd_params_free(free_arg);
+ return;
+ }
+
+ if (g_hash_table_lookup(optlist, "delete") != NULL && *key != '\0') {
+ printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
+ TXT_COMPLETION_REMOVED, key);
+
+ iconfig_set_str("completions", key, NULL);
+ signal_emit("completion removed", 1, key);
+ } else if (*key != '\0' && *value != '\0') {
+ int automatic = g_hash_table_lookup(optlist, "auto") != NULL;
+
+ node = config_node_section(node, key, NODE_TYPE_BLOCK);
+ iconfig_node_set_str(node, "value", value);
+ if (automatic)
+ iconfig_node_set_bool(node, "auto", TRUE);
+ else
+ iconfig_node_set_str(node, "auto", NULL);
+
+ printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
+ TXT_COMPLETION_LINE,
+ key, value, automatic ? "yes" : "no");
+
+ signal_emit("completion added", 1, key);
+ } else {
+ printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
+ TXT_COMPLETION_HEADER);
+
+ len = strlen(key);
+ for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
+ node = tmp->data;
+
+ if (len == 0 ||
+ g_strncasecmp(node->key, key, len) == 0) {
+ printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
+ TXT_COMPLETION_LINE, node->key,
+ config_node_get_str(node, "value", ""),
+ config_node_get_bool(node, "auto", FALSE) ? "yes" : "no");
+ }
+ }
+
+ printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
+ TXT_COMPLETION_FOOTER);
+ }
+
+ cmd_params_free(free_arg);
+}
+
void completion_init(void)
{
complist = NULL;
chat_completion_init();
+ command_bind("completion", NULL, (SIGNAL_FUNC) cmd_completion);
+
signal_add_first("complete word", (SIGNAL_FUNC) sig_complete_word);
signal_add_first("complete erase", (SIGNAL_FUNC) sig_complete_erase);
signal_add("complete command set", (SIGNAL_FUNC) sig_complete_set);
signal_add("complete command rawlog open", (SIGNAL_FUNC) sig_complete_filename);
signal_add("complete command rawlog save", (SIGNAL_FUNC) sig_complete_filename);
signal_add("complete command help", (SIGNAL_FUNC) sig_complete_command);
+
+ command_set_options("completion", "auto delete");
}
void completion_deinit(void)
chat_completion_deinit();
+ command_unbind("completion", (SIGNAL_FUNC) cmd_completion);
+
signal_remove("complete word", (SIGNAL_FUNC) sig_complete_word);
signal_remove("complete erase", (SIGNAL_FUNC) sig_complete_erase);
signal_remove("complete command set", (SIGNAL_FUNC) sig_complete_set);
signal_remove("complete command rawlog save", (SIGNAL_FUNC) sig_complete_filename);
signal_remove("complete command help", (SIGNAL_FUNC) sig_complete_command);
}
-
-