Added SILC Thread Queue API
[runtime.git] / apps / irssi / src / fe-common / core / completion.c
index f10fbb6dec0414866ecd35da3df8d0607e1eae9f..be8b76c5ccb7bb04dffe3e4071ec4d4315546892 100644 (file)
 */
 
 #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;
@@ -41,11 +38,29 @@ static int last_want_space, last_line_pos;
         ((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)
 {
@@ -85,7 +100,7 @@ char *auto_word_complete(const char *line, int *pos)
        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);
@@ -267,7 +282,8 @@ GList *filename_complete(const char *path, const char *default_path)
         GList *list;
        DIR *dirp;
        struct dirent *dp;
-       char *realpath, *dir, *basename, *name;
+       const char *basename;
+       char *realpath, *dir, *name;
        int len;
 
        g_return_val_if_fail(path != NULL, NULL);
@@ -514,7 +530,7 @@ static char *line_get_command(const char *line, char **args, int aliases)
                } else {
                        checkcmd = g_strndup(line, (int) (ptr-line));
 
-                       while (i_isspace(*ptr)) ptr++;
+                       while (*ptr == ' ') ptr++;
                        cmdargs = ptr;
                }
 
@@ -568,7 +584,7 @@ static void sig_complete_word(GList **list, WINDOW_REC *window,
        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));
 
@@ -576,6 +592,16 @@ static void sig_complete_word(GList **list, WINDOW_REC *window,
                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)) {
@@ -733,6 +759,79 @@ static void sig_complete_command(GList **list, WINDOW_REC *window,
        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;
@@ -740,16 +839,21 @@ void completion_init(void)
 
        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 toggle", (SIGNAL_FUNC) sig_complete_toggle);
+       signal_add("complete command load", (SIGNAL_FUNC) sig_complete_filename);
        signal_add("complete command cat", (SIGNAL_FUNC) sig_complete_filename);
        signal_add("complete command save", (SIGNAL_FUNC) sig_complete_filename);
        signal_add("complete command reload", (SIGNAL_FUNC) sig_complete_filename);
        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)
@@ -758,10 +862,13 @@ 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 toggle", (SIGNAL_FUNC) sig_complete_toggle);
+       signal_remove("complete command load", (SIGNAL_FUNC) sig_complete_filename);
        signal_remove("complete command cat", (SIGNAL_FUNC) sig_complete_filename);
        signal_remove("complete command save", (SIGNAL_FUNC) sig_complete_filename);
        signal_remove("complete command reload", (SIGNAL_FUNC) sig_complete_filename);
@@ -769,5 +876,3 @@ void completion_deinit(void)
        signal_remove("complete command rawlog save", (SIGNAL_FUNC) sig_complete_filename);
        signal_remove("complete command help", (SIGNAL_FUNC) sig_complete_command);
 }
-
-