Added SILC Thread Queue API
[runtime.git] / apps / irssi / src / fe-common / core / completion.c
index cd86ab4f7438b8a6b2375b4ffc0408eff64dfbb8..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) \
-       (isspace((int) (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);
@@ -113,27 +128,28 @@ static void free_completions(void)
 }
 
 /* manual word completion - called when TAB is pressed */
-char *word_complete(WINDOW_REC *window, const char *line, int *pos)
+char *word_complete(WINDOW_REC *window, const char *line, int *pos, int erase)
 {
        static int startpos = 0, wordlen = 0;
+        int old_startpos, old_wordlen;
 
        GString *result;
        char *word, *wordstart, *linestart, *ret;
-       int want_space;
+       int continue_complete, want_space;
 
        g_return_val_if_fail(line != NULL, NULL);
        g_return_val_if_fail(pos != NULL, NULL);
 
-       if (complist != NULL && *pos == last_line_pos &&
-           strcmp(line, last_line) == 0) {
-               /* 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();
+       continue_complete = complist != NULL && *pos == last_line_pos &&
+               strcmp(line, last_line) == 0;
+
+       old_startpos = startpos;
+       old_wordlen = wordlen;
 
+       if (!erase && continue_complete) {
+               word = NULL;
+                linestart = NULL;
+       } else {
                /* get the word we want to complete */
                word = get_word_at(line, *pos, &wordstart);
                startpos = (int) (wordstart-line);
@@ -156,7 +172,8 @@ char *word_complete(WINDOW_REC *window, const char *line, int *pos)
                   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. */
-               if (*pos > 0 && line[*pos-1] == ' ') {
+               if (!erase && *pos > 0 && line[*pos-1] == ' ' &&
+                   (*linestart == '\0' || wordstart[-1] != ' ')) {
                        char *old;
 
                         old = linestart;
@@ -171,14 +188,38 @@ char *word_complete(WINDOW_REC *window, const char *line, int *pos)
                        wordlen = 0;
                }
 
+       }
+
+       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();
+
                want_space = TRUE;
                signal_emit("complete word", 5, &complist, window, word, linestart, &want_space);
                last_want_space = want_space;
-
-               g_free(linestart);
-               g_free(word);
        }
 
+       g_free(linestart);
+       g_free(word);
+
        if (complist == NULL)
                return NULL;
 
@@ -207,7 +248,14 @@ char *word_complete(WINDOW_REC *window, const char *line, int *pos)
        return ret;
 }
 
-GList *list_add_file(GList *list, const char *name)
+#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)
 {
        struct stat statbuf;
        char *fname;
@@ -215,6 +263,11 @@ GList *list_add_file(GList *list, const char *name)
        g_return_val_if_fail(name != NULL, NULL);
 
        fname = convert_home(name);
+       if (USE_DEFAULT_PATH(fname, default_path)) {
+                g_free(fname);
+               fname = g_strconcat(default_path, G_DIR_SEPARATOR_S,
+                                   name, NULL);
+       }
        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));
@@ -224,12 +277,13 @@ GList *list_add_file(GList *list, const char *name)
        return list;
 }
 
-GList *filename_complete(const char *path)
+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);
@@ -238,17 +292,31 @@ GList *filename_complete(const char *path)
 
        /* get directory part of the path - expand ~/ */
        realpath = convert_home(path);
-       dir = g_dirname(realpath);
-       g_free(realpath);
+       if (USE_DEFAULT_PATH(realpath, default_path)) {
+                g_free(realpath);
+               realpath = g_strconcat(default_path, G_DIR_SEPARATOR_S,
+                                      path, NULL);
+       }
 
        /* open directory for reading */
+       dir = g_dirname(realpath);
        dirp = opendir(dir);
        g_free(dir);
-       if (dirp == NULL) return NULL;
+        g_free(realpath);
+
+       if (dirp == NULL)
+               return NULL;
 
        dir = g_dirname(path);
-       if (*dir == G_DIR_SEPARATOR && dir[1] == '\0')
-               *dir = '\0'; /* completing file in root directory */
+       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);
+       }
+
        basename = g_basename(path);
        len = strlen(basename);
 
@@ -264,14 +332,15 @@ GList *filename_complete(const char *path)
                }
 
                if (len == 0 || strncmp(dp->d_name, basename, len) == 0) {
-                       name = g_strdup_printf("%s"G_DIR_SEPARATOR_S"%s", dir, dp->d_name);
-                       list = list_add_file(list, name);
+                       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);
                        g_free(name);
                }
        }
        closedir(dirp);
 
-       g_free(dir);
+       g_free_not_null(dir);
         return list;
 }
 
@@ -332,11 +401,11 @@ static GList *completion_get_aliases(const char *alias, char cmdchar)
 
        /* get list of aliases from mainconfig */
        node = iconfig_node_traverse("aliases", FALSE);
-       tmp = node == NULL ? NULL : node->value;
+       tmp = node == NULL ? NULL : config_node_first(node->value);
 
        len = strlen(alias);
        complist = NULL;
-       for (; tmp != NULL; tmp = tmp->next) {
+       for (; tmp != NULL; tmp = config_node_next(tmp)) {
                CONFIG_NODE *node = tmp->data;
 
                if (node->type != NODE_TYPE_KEY)
@@ -461,7 +530,7 @@ static char *line_get_command(const char *line, char **args, int aliases)
                } else {
                        checkcmd = g_strndup(line, (int) (ptr-line));
 
-                       while (isspace(*ptr)) ptr++;
+                       while (*ptr == ' ') ptr++;
                        cmdargs = ptr;
                }
 
@@ -483,6 +552,8 @@ static char *line_get_command(const char *line, char **args, int aliases)
                *args = (char *) cmdargs;
        } while (ptr != NULL);
 
+        if (cmd != NULL)
+               g_strdown(cmd);
        return cmd;
 }
 
@@ -502,7 +573,8 @@ static char *expand_aliases(const char *line)
 }
 
 static void sig_complete_word(GList **list, WINDOW_REC *window,
-                             const char *word, const char *linestart, int *want_space)
+                             const char *word, const char *linestart,
+                             int *want_space)
 {
        const char *newword, *cmdchars;
        char *signal, *cmd, *args, *line;
@@ -512,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));
 
@@ -520,9 +592,19 @@ 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 (strchr(cmdchars, *word) && *linestart == '\0') {
+       if (*word != '\0' && *linestart == '\0' && strchr(cmdchars, *word)) {
                /* complete /command */
                *list = completion_get_commands(word+1, *word);
 
@@ -578,6 +660,39 @@ static void sig_complete_word(GList **list, WINDOW_REC *window,
        g_free(line);
 }
 
+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);
+}
+
 static void sig_complete_set(GList **list, WINDOW_REC *window,
                             const char *word, const char *line, int *want_space)
 {
@@ -614,7 +729,7 @@ static void sig_complete_filename(GList **list, WINDOW_REC *window,
 
        if (*line != '\0') return;
 
-       *list = filename_complete(word);
+       *list = filename_complete(word, NULL);
        if (*list != NULL) {
                *want_space = FALSE;
                signal_stop();
@@ -644,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;
@@ -651,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 run", (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)
@@ -669,16 +862,17 @@ 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 run", (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);
        signal_remove("complete command rawlog open", (SIGNAL_FUNC) sig_complete_filename);
        signal_remove("complete command rawlog save", (SIGNAL_FUNC) sig_complete_filename);
        signal_remove("complete command help", (SIGNAL_FUNC) sig_complete_command);
 }
-
-