Added SILC Thread Queue API
[crypto.git] / apps / irssi / src / fe-common / core / completion.c
1 /*
2  completion.c : irssi
3
4     Copyright (C) 2000 Timo Sirainen
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "module.h"
22 #include "module-formats.h"
23 #include "signals.h"
24 #include "commands.h"
25 #include "levels.h"
26 #include "misc.h"
27 #include "lib-config/iconfig.h"
28 #include "settings.h"
29
30 #include "completion.h"
31 #include "printtext.h"
32
33 static GList *complist; /* list of commands we're currently completing */
34 static char *last_line;
35 static int last_want_space, last_line_pos;
36
37 #define isseparator_notspace(c) \
38         ((c) == ',')
39
40 #define isseparator(c) \
41         ((c) == ' ' || isseparator_notspace(c))
42
43 void chat_completion_init(void);
44 void chat_completion_deinit(void);
45
46 static const char *completion_find(const char *key, int automatic)
47 {
48         CONFIG_NODE *node;
49
50         node = iconfig_node_traverse("completions", FALSE);
51         if (node == NULL || node->type != NODE_TYPE_BLOCK)
52                 return NULL;
53
54         node = config_node_section(node, key, -1);
55         if (node == NULL)
56                 return NULL;
57
58         if (automatic && !config_node_get_bool(node, "auto", FALSE))
59                 return NULL;
60
61         return config_node_get_str(node, "value", NULL);
62 }
63
64 /* Return whole word at specified position in string */
65 char *get_word_at(const char *str, int pos, char **startpos)
66 {
67         const char *start, *end;
68
69         g_return_val_if_fail(str != NULL, NULL);
70         g_return_val_if_fail(pos >= 0, NULL);
71
72         /* get previous word if char at `pos' is space */
73         start = str+pos;
74         while (start > str && isseparator(start[-1])) start--;
75
76         end = start;
77         while (start > str && !isseparator(start[-1])) start--;
78         while (*end != '\0' && !isseparator(*end)) end++;
79         while (*end != '\0' && isseparator_notspace(*end)) end++;
80
81         *startpos = (char *) start;
82         return g_strndup(start, (int) (end-start));
83 }
84
85 /* automatic word completion - called when space/enter is pressed */
86 char *auto_word_complete(const char *line, int *pos)
87 {
88         GString *result;
89         const char *replace;
90         char *word, *wordstart, *ret;
91         int startpos;
92
93         g_return_val_if_fail(line != NULL, NULL);
94         g_return_val_if_fail(pos != NULL, NULL);
95
96         word = get_word_at(line, *pos, &wordstart);
97         startpos = (int) (wordstart-line);
98
99         result = g_string_new(line);
100         g_string_erase(result, startpos, strlen(word));
101
102         /* check for words in autocompletion list */
103         replace = completion_find(word, TRUE);
104         if (replace == NULL) {
105                 ret = NULL;
106                 g_string_free(result, TRUE);
107         } else {
108                 *pos = startpos+strlen(replace);
109
110                 g_string_insert(result, startpos, replace);
111                 ret = result->str;
112                 g_string_free(result, FALSE);
113         }
114
115         g_free(word);
116         return ret;
117 }
118
119 static void free_completions(void)
120 {
121         complist = g_list_first(complist);
122
123         g_list_foreach(complist, (GFunc) g_free, NULL);
124         g_list_free(complist);
125         complist = NULL;
126
127         g_free_and_null(last_line);
128 }
129
130 /* manual word completion - called when TAB is pressed */
131 char *word_complete(WINDOW_REC *window, const char *line, int *pos, int erase)
132 {
133         static int startpos = 0, wordlen = 0;
134         int old_startpos, old_wordlen;
135
136         GString *result;
137         char *word, *wordstart, *linestart, *ret;
138         int continue_complete, want_space;
139
140         g_return_val_if_fail(line != NULL, NULL);
141         g_return_val_if_fail(pos != NULL, NULL);
142
143         continue_complete = complist != NULL && *pos == last_line_pos &&
144                 strcmp(line, last_line) == 0;
145
146         old_startpos = startpos;
147         old_wordlen = wordlen;
148
149         if (!erase && continue_complete) {
150                 word = NULL;
151                 linestart = NULL;
152         } else {
153                 /* get the word we want to complete */
154                 word = get_word_at(line, *pos, &wordstart);
155                 startpos = (int) (wordstart-line);
156                 wordlen = strlen(word);
157
158                 /* get the start of line until the word we're completing */
159                 if (isseparator(*line)) {
160                         /* empty space at the start of line */
161                         if (wordstart == line)
162                                 wordstart += strlen(wordstart);
163                 } else {
164                         while (wordstart > line && isseparator(wordstart[-1]))
165                                 wordstart--;
166                 }
167                 linestart = g_strndup(line, (int) (wordstart-line));
168
169                 /* completions usually add space after the word, that makes
170                    things a bit harder. When continuing a completion
171                    "/msg nick1 "<tab> we have to cycle to nick2, etc.
172                    BUT if we start completion with "/msg "<tab>, we don't
173                    want to complete the /msg word, but instead complete empty
174                    word with /msg being in linestart. */
175                 if (!erase && *pos > 0 && line[*pos-1] == ' ' &&
176                     (*linestart == '\0' || wordstart[-1] != ' ')) {
177                         char *old;
178
179                         old = linestart;
180                         linestart = *linestart == '\0' ?
181                                 g_strdup(word) :
182                                 g_strconcat(linestart, " ", word, NULL);
183                         g_free(old);
184
185                         g_free(word);
186                         word = g_strdup("");
187                         startpos = strlen(linestart)+1;
188                         wordlen = 0;
189                 }
190
191         }
192
193         if (erase) {
194                 signal_emit("complete erase", 3, window, word, linestart);
195
196                 if (!continue_complete)
197                         return NULL;
198
199                 /* jump to next completion */
200                 word = NULL;
201                 linestart = NULL;
202                 startpos = old_startpos;
203                 wordlen = old_wordlen;
204         }
205
206         if (continue_complete) {
207                 /* complete from old list */
208                 complist = complist->next != NULL ? complist->next :
209                         g_list_first(complist);
210                 want_space = last_want_space;
211         } else {
212                 /* get new completion list */
213                 free_completions();
214
215                 want_space = TRUE;
216                 signal_emit("complete word", 5, &complist, window, word, linestart, &want_space);
217                 last_want_space = want_space;
218         }
219
220         g_free(linestart);
221         g_free(word);
222
223         if (complist == NULL)
224                 return NULL;
225
226         /* word completed */
227         *pos = startpos+strlen(complist->data);
228
229         /* replace the word in line - we need to return
230            a full new line */
231         result = g_string_new(line);
232         g_string_erase(result, startpos, wordlen);
233         g_string_insert(result, startpos, complist->data);
234
235         if (want_space) {
236                 if (!isseparator(result->str[*pos]))
237                         g_string_insert_c(result, *pos, ' ');
238                 (*pos)++;
239         }
240
241         wordlen = strlen(complist->data);
242         last_line_pos = *pos;
243         g_free_not_null(last_line);
244         last_line = g_strdup(result->str);
245
246         ret = result->str;
247         g_string_free(result, FALSE);
248         return ret;
249 }
250
251 #define IS_CURRENT_DIR(dir) \
252         ((dir)[0] == '.' && ((dir)[1] == '\0' || (dir)[1] == G_DIR_SEPARATOR))
253
254 #define USE_DEFAULT_PATH(path, default_path) \
255         ((!g_path_is_absolute(path) || IS_CURRENT_DIR(path)) && \
256          default_path != NULL)
257
258 GList *list_add_file(GList *list, const char *name, const char *default_path)
259 {
260         struct stat statbuf;
261         char *fname;
262
263         g_return_val_if_fail(name != NULL, NULL);
264
265         fname = convert_home(name);
266         if (USE_DEFAULT_PATH(fname, default_path)) {
267                 g_free(fname);
268                 fname = g_strconcat(default_path, G_DIR_SEPARATOR_S,
269                                     name, NULL);
270         }
271         if (stat(fname, &statbuf) == 0) {
272                 list = g_list_append(list, !S_ISDIR(statbuf.st_mode) ? g_strdup(name) :
273                                      g_strconcat(name, G_DIR_SEPARATOR_S, NULL));
274         }
275
276         g_free(fname);
277         return list;
278 }
279
280 GList *filename_complete(const char *path, const char *default_path)
281 {
282         GList *list;
283         DIR *dirp;
284         struct dirent *dp;
285         const char *basename;
286         char *realpath, *dir, *name;
287         int len;
288
289         g_return_val_if_fail(path != NULL, NULL);
290
291         list = NULL;
292
293         /* get directory part of the path - expand ~/ */
294         realpath = convert_home(path);
295         if (USE_DEFAULT_PATH(realpath, default_path)) {
296                 g_free(realpath);
297                 realpath = g_strconcat(default_path, G_DIR_SEPARATOR_S,
298                                        path, NULL);
299         }
300
301         /* open directory for reading */
302         dir = g_dirname(realpath);
303         dirp = opendir(dir);
304         g_free(dir);
305         g_free(realpath);
306
307         if (dirp == NULL)
308                 return NULL;
309
310         dir = g_dirname(path);
311         if (*dir == G_DIR_SEPARATOR && dir[1] == '\0') {
312                 /* completing file in root directory */
313                 *dir = '\0';
314         } else if (IS_CURRENT_DIR(dir) && !IS_CURRENT_DIR(path)) {
315                 /* completing file in default_path
316                    (path not set, and leave it that way) */
317                 g_free_and_null(dir);
318         }
319
320         basename = g_basename(path);
321         len = strlen(basename);
322
323         /* add all files in directory to completion list */
324         while ((dp = readdir(dirp)) != NULL) {
325                 if (dp->d_name[0] == '.') {
326                         if (dp->d_name[1] == '\0' ||
327                             (dp->d_name[1] == '.' && dp->d_name[2] == '\0'))
328                                 continue; /* skip . and .. */
329
330                         if (basename[0] != '.')
331                                 continue;
332                 }
333
334                 if (len == 0 || strncmp(dp->d_name, basename, len) == 0) {
335                         name = dir == NULL ? g_strdup(dp->d_name) :
336                                 g_strdup_printf("%s"G_DIR_SEPARATOR_S"%s", dir, dp->d_name);
337                         list = list_add_file(list, name, default_path);
338                         g_free(name);
339                 }
340         }
341         closedir(dirp);
342
343         g_free_not_null(dir);
344         return list;
345 }
346
347 static GList *completion_get_settings(const char *key)
348 {
349         GList *complist;
350         GSList *tmp, *sets;
351         int len;
352
353         g_return_val_if_fail(key != NULL, NULL);
354
355         sets = settings_get_sorted();
356
357         len = strlen(key);
358         complist = NULL;
359         for (tmp = sets; tmp != NULL; tmp = tmp->next) {
360                 SETTINGS_REC *rec = tmp->data;
361
362                 if (g_strncasecmp(rec->key, key, len) == 0)
363                         complist = g_list_insert_sorted(complist, g_strdup(rec->key), (GCompareFunc) g_istr_cmp);
364         }
365         g_slist_free(sets);
366         return complist;
367 }
368
369 static GList *completion_get_bool_settings(const char *key)
370 {
371         GList *complist;
372         GSList *tmp, *sets;
373         int len;
374
375         g_return_val_if_fail(key != NULL, NULL);
376
377         sets = settings_get_sorted();
378
379         len = strlen(key);
380         complist = NULL;
381         for (tmp = sets; tmp != NULL; tmp = tmp->next) {
382                 SETTINGS_REC *rec = tmp->data;
383
384                 if (rec->type == SETTING_TYPE_BOOLEAN &&
385                     g_strncasecmp(rec->key, key, len) == 0)
386                         complist = g_list_insert_sorted(complist, g_strdup(rec->key), (GCompareFunc) g_istr_cmp);
387         }
388         g_slist_free(sets);
389         return complist;
390 }
391
392 static GList *completion_get_aliases(const char *alias, char cmdchar)
393 {
394         CONFIG_NODE *node;
395         GList *complist;
396         GSList *tmp;
397         char *word;
398         int len;
399
400         g_return_val_if_fail(alias != NULL, NULL);
401
402         /* get list of aliases from mainconfig */
403         node = iconfig_node_traverse("aliases", FALSE);
404         tmp = node == NULL ? NULL : config_node_first(node->value);
405
406         len = strlen(alias);
407         complist = NULL;
408         for (; tmp != NULL; tmp = config_node_next(tmp)) {
409                 CONFIG_NODE *node = tmp->data;
410
411                 if (node->type != NODE_TYPE_KEY)
412                         continue;
413
414                 if (g_strncasecmp(node->key, alias, len) == 0) {
415                         word = g_strdup_printf("%c%s", cmdchar, node->key);
416                         /* add matching alias to completion list, aliases will
417                            be appended after command completions and kept in
418                            uppercase to show it's an alias */
419                         if (glist_find_icase_string(complist, word) == NULL)
420                                 complist = g_list_insert_sorted(complist, word, (GCompareFunc) g_istr_cmp);
421                         else
422                                 g_free(word);
423                 }
424         }
425         return complist;
426 }
427
428 static GList *completion_get_commands(const char *cmd, char cmdchar)
429 {
430         GList *complist;
431         GSList *tmp;
432         char *word;
433         int len;
434
435         g_return_val_if_fail(cmd != NULL, NULL);
436
437         len = strlen(cmd);
438         complist = NULL;
439         for (tmp = commands; tmp != NULL; tmp = tmp->next) {
440                 COMMAND_REC *rec = tmp->data;
441
442                 if (strchr(rec->cmd, ' ') != NULL)
443                         continue;
444
445                 if (g_strncasecmp(rec->cmd, cmd, len) == 0) {
446                         word = cmdchar == '\0' ? g_strdup(rec->cmd) :
447                                 g_strdup_printf("%c%s", cmdchar, rec->cmd);
448                         if (glist_find_icase_string(complist, word) == NULL)
449                                 complist = g_list_insert_sorted(complist, word, (GCompareFunc) g_istr_cmp);
450                         else
451                                 g_free(word);
452                 }
453         }
454         return complist;
455 }
456
457 static GList *completion_get_subcommands(const char *cmd)
458 {
459         GList *complist;
460         GSList *tmp;
461         char *spacepos;
462         int len, skip;
463
464         g_return_val_if_fail(cmd != NULL, NULL);
465
466         /* get the number of chars to skip at the start of command. */
467         spacepos = strrchr(cmd, ' ');
468         skip = spacepos == NULL ? strlen(cmd)+1 :
469                 ((int) (spacepos-cmd) + 1);
470
471         len = strlen(cmd);
472         complist = NULL;
473         for (tmp = commands; tmp != NULL; tmp = tmp->next) {
474                 COMMAND_REC *rec = tmp->data;
475
476                 if ((int)strlen(rec->cmd) < len)
477                         continue;
478
479                 if (strchr(rec->cmd+len, ' ') != NULL)
480                         continue;
481
482                 if (g_strncasecmp(rec->cmd, cmd, len) == 0)
483                         complist = g_list_insert_sorted(complist, g_strdup(rec->cmd+skip), (GCompareFunc) g_istr_cmp);
484         }
485         return complist;
486 }
487
488 GList *completion_get_options(const char *cmd, const char *option)
489 {
490         COMMAND_REC *rec;
491         GList *list;
492         char **tmp;
493         int len;
494
495         g_return_val_if_fail(cmd != NULL, NULL);
496         g_return_val_if_fail(option != NULL, NULL);
497
498         rec = command_find(cmd);
499         if (rec == NULL || rec->options == NULL) return NULL;
500
501         list = NULL;
502         len = strlen(option);
503         for (tmp = rec->options; *tmp != NULL; tmp++) {
504                 const char *optname = *tmp + iscmdtype(**tmp);
505
506                 if (len == 0 || g_strncasecmp(optname, option, len) == 0)
507                         list = g_list_append(list, g_strconcat("-", optname, NULL));
508         }
509
510         return list;
511 }
512
513 /* split the line to command and arguments */
514 static char *line_get_command(const char *line, char **args, int aliases)
515 {
516         const char *ptr, *cmdargs;
517         char *cmd, *checkcmd;
518
519         g_return_val_if_fail(line != NULL, NULL);
520         g_return_val_if_fail(args != NULL, NULL);
521
522         cmd = checkcmd = NULL; *args = "";
523         cmdargs = NULL; ptr = line;
524
525         do {
526                 ptr = strchr(ptr, ' ');
527                 if (ptr == NULL) {
528                         checkcmd = g_strdup(line);
529                         cmdargs = "";
530                 } else {
531                         checkcmd = g_strndup(line, (int) (ptr-line));
532
533                         while (*ptr == ' ') ptr++;
534                         cmdargs = ptr;
535                 }
536
537                 if (aliases ? !alias_find(checkcmd) :
538                     !command_find(checkcmd)) {
539                         /* not found, use the previous */
540                         g_free(checkcmd);
541                         break;
542                 }
543
544                 /* found, check if it has subcommands */
545                 g_free_not_null(cmd);
546                 if (!aliases)
547                         cmd = checkcmd;
548                 else {
549                         cmd = g_strdup(alias_find(checkcmd));
550                         g_free(checkcmd);
551                 }
552                 *args = (char *) cmdargs;
553         } while (ptr != NULL);
554
555         if (cmd != NULL)
556                 g_strdown(cmd);
557         return cmd;
558 }
559
560 static char *expand_aliases(const char *line)
561 {
562         char *cmd, *args, *ret;
563
564         g_return_val_if_fail(line != NULL, NULL);
565
566         cmd = line_get_command(line, &args, TRUE);
567         if (cmd == NULL) return g_strdup(line);
568         if (*args == '\0') return cmd;
569
570         ret = g_strconcat(cmd, " ", args, NULL);
571         g_free(cmd);
572         return ret;
573 }
574
575 static void sig_complete_word(GList **list, WINDOW_REC *window,
576                               const char *word, const char *linestart,
577                               int *want_space)
578 {
579         const char *newword, *cmdchars;
580         char *signal, *cmd, *args, *line;
581
582         g_return_if_fail(list != NULL);
583         g_return_if_fail(word != NULL);
584         g_return_if_fail(linestart != NULL);
585
586         /* check against "completion words" list */
587         newword = completion_find(word, FALSE);
588         if (newword != NULL) {
589                 *list = g_list_append(*list, g_strdup(newword));
590
591                 signal_stop();
592                 return;
593         }
594
595         if (*linestart != '\0' && (*word == '/' || *word == '~')) {
596                 /* quite likely filename completion */
597                 *list = g_list_concat(*list, filename_complete(word, NULL));
598                 if (*list != NULL) {
599                         *want_space = FALSE;
600                         signal_stop();
601                         return;
602                 }
603         }
604
605         /* command completion? */
606         cmdchars = settings_get_str("cmdchars");
607         if (*word != '\0' && *linestart == '\0' && strchr(cmdchars, *word)) {
608                 /* complete /command */
609                 *list = completion_get_commands(word+1, *word);
610
611                 /* complete aliases, too */
612                 *list = g_list_concat(*list,
613                                       completion_get_aliases(word+1, *word));
614
615                 if (*list != NULL) signal_stop();
616                 return;
617         }
618
619         /* check only for /command completions from now on */
620         if (*linestart == '\0')
621                 return;
622
623         cmdchars = strchr(cmdchars, *linestart);
624         if (cmdchars == NULL) return;
625
626         /* check if there's aliases */
627         line = linestart[1] == *cmdchars ? g_strdup(linestart+2) :
628                 expand_aliases(linestart+1);
629
630         cmd = line_get_command(line, &args, FALSE);
631         if (cmd == NULL) {
632                 g_free(line);
633                 return;
634         }
635
636         /* we're completing -option? */
637         if (*word == '-') {
638                 *list = completion_get_options(cmd, word+1);
639                 g_free(cmd);
640                 g_free(line);
641                 return;
642         }
643
644         /* complete parameters */
645         signal = g_strconcat("complete command ", cmd, NULL);
646         signal_emit(signal, 5, list, window, word, args, want_space);
647
648         if (command_have_sub(line)) {
649                 /* complete subcommand */
650                 g_free(cmd);
651                 cmd = g_strconcat(line, " ", word, NULL);
652                 *list = g_list_concat(completion_get_subcommands(cmd), *list);
653
654                 if (*list != NULL) signal_stop();
655         }
656
657         g_free(signal);
658         g_free(cmd);
659
660         g_free(line);
661 }
662
663 static void sig_complete_erase(WINDOW_REC *window, const char *word,
664                                const char *linestart)
665 {
666         const char *cmdchars;
667         char *line, *cmd, *args, *signal;
668
669         if (*linestart == '\0')
670                 return;
671
672         /* we only want to check for commands */
673         cmdchars = settings_get_str("cmdchars");
674         cmdchars = strchr(cmdchars, *linestart);
675         if (cmdchars == NULL)
676                 return;
677
678         /* check if there's aliases */
679         line = linestart[1] == *cmdchars ? g_strdup(linestart+2) :
680                 expand_aliases(linestart+1);
681
682         cmd = line_get_command(line, &args, FALSE);
683         if (cmd == NULL) {
684                 g_free(line);
685                 return;
686         }
687
688         signal = g_strconcat("complete erase command ", cmd, NULL);
689         signal_emit(signal, 3, window, word, args);
690
691         g_free(signal);
692         g_free(cmd);
693         g_free(line);
694 }
695
696 static void sig_complete_set(GList **list, WINDOW_REC *window,
697                              const char *word, const char *line, int *want_space)
698 {
699         g_return_if_fail(list != NULL);
700         g_return_if_fail(word != NULL);
701         g_return_if_fail(line != NULL);
702
703         if (*line != '\0') return;
704
705         *list = completion_get_settings(word);
706         if (*list != NULL) signal_stop();
707 }
708
709 static void sig_complete_toggle(GList **list, WINDOW_REC *window,
710                                 const char *word, const char *line, int *want_space)
711 {
712         g_return_if_fail(list != NULL);
713         g_return_if_fail(word != NULL);
714         g_return_if_fail(line != NULL);
715
716         if (*line != '\0') return;
717
718         *list = completion_get_bool_settings(word);
719         if (*list != NULL) signal_stop();
720 }
721
722 /* first argument of command is file name - complete it */
723 static void sig_complete_filename(GList **list, WINDOW_REC *window,
724                                   const char *word, const char *line, int *want_space)
725 {
726         g_return_if_fail(list != NULL);
727         g_return_if_fail(word != NULL);
728         g_return_if_fail(line != NULL);
729
730         if (*line != '\0') return;
731
732         *list = filename_complete(word, NULL);
733         if (*list != NULL) {
734                 *want_space = FALSE;
735                 signal_stop();
736         }
737 }
738
739 /* first argument of command is .. command :) (/HELP command) */
740 static void sig_complete_command(GList **list, WINDOW_REC *window,
741                                   const char *word, const char *line, int *want_space)
742 {
743         char *cmd;
744
745         g_return_if_fail(list != NULL);
746         g_return_if_fail(word != NULL);
747         g_return_if_fail(line != NULL);
748
749         if (*line == '\0') {
750                 /* complete base command */
751                 *list = completion_get_commands(word, '\0');
752         } else if (command_have_sub(line)) {
753                 /* complete subcommand */
754                 cmd = g_strconcat(line, " ", word, NULL);
755                 *list = completion_get_subcommands(cmd);
756                 g_free(cmd);
757         }
758
759         if (*list != NULL) signal_stop();
760 }
761
762 static void cmd_completion(const char *data)
763 {
764         GHashTable *optlist;
765         CONFIG_NODE *node;
766         GSList *tmp;
767         char *key, *value;
768         void *free_arg;
769         int len;
770
771         if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_OPTIONS |
772                             PARAM_FLAG_GETREST,
773                             "completion", &optlist, &key, &value))
774                 return;
775
776         node = iconfig_node_traverse("completions", *value != '\0');
777         if (node != NULL && node->type != NODE_TYPE_BLOCK) {
778                 /* FIXME: remove after 0.8.5 */
779                 iconfig_node_remove(mainconfig->mainnode, node);
780                 node = iconfig_node_traverse("completions", *value != '\0');
781         }
782
783         if (node == NULL || (node->value == NULL && *value == '\0')) {
784                 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
785                             TXT_NO_COMPLETIONS);
786                 cmd_params_free(free_arg);
787                 return;
788         }
789
790         if (g_hash_table_lookup(optlist, "delete") != NULL && *key != '\0') {
791                 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
792                             TXT_COMPLETION_REMOVED, key);
793
794                 iconfig_set_str("completions", key, NULL);
795                 signal_emit("completion removed", 1, key);
796         } else if (*key != '\0' && *value != '\0') {
797                 int automatic = g_hash_table_lookup(optlist, "auto") != NULL;
798
799                 node = config_node_section(node, key, NODE_TYPE_BLOCK);
800                 iconfig_node_set_str(node, "value", value);
801                 if (automatic)
802                         iconfig_node_set_bool(node, "auto", TRUE);
803                 else
804                         iconfig_node_set_str(node, "auto", NULL);
805
806                 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
807                             TXT_COMPLETION_LINE,
808                             key, value, automatic ? "yes" : "no");
809
810                 signal_emit("completion added", 1, key);
811         } else {
812                 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
813                             TXT_COMPLETION_HEADER);
814
815                 len = strlen(key);
816                 for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
817                         node = tmp->data;
818
819                         if (len == 0 ||
820                             g_strncasecmp(node->key, key, len) == 0) {
821                                 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
822                                             TXT_COMPLETION_LINE, node->key,
823                                             config_node_get_str(node, "value", ""),
824                                             config_node_get_bool(node, "auto", FALSE) ? "yes" : "no");
825                         }
826                 }
827
828                 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
829                             TXT_COMPLETION_FOOTER);
830         }
831
832         cmd_params_free(free_arg);
833 }
834
835 void completion_init(void)
836 {
837         complist = NULL;
838         last_line = NULL; last_line_pos = -1;
839
840         chat_completion_init();
841
842         command_bind("completion", NULL, (SIGNAL_FUNC) cmd_completion);
843
844         signal_add_first("complete word", (SIGNAL_FUNC) sig_complete_word);
845         signal_add_first("complete erase", (SIGNAL_FUNC) sig_complete_erase);
846         signal_add("complete command set", (SIGNAL_FUNC) sig_complete_set);
847         signal_add("complete command toggle", (SIGNAL_FUNC) sig_complete_toggle);
848         signal_add("complete command load", (SIGNAL_FUNC) sig_complete_filename);
849         signal_add("complete command cat", (SIGNAL_FUNC) sig_complete_filename);
850         signal_add("complete command save", (SIGNAL_FUNC) sig_complete_filename);
851         signal_add("complete command reload", (SIGNAL_FUNC) sig_complete_filename);
852         signal_add("complete command rawlog open", (SIGNAL_FUNC) sig_complete_filename);
853         signal_add("complete command rawlog save", (SIGNAL_FUNC) sig_complete_filename);
854         signal_add("complete command help", (SIGNAL_FUNC) sig_complete_command);
855
856         command_set_options("completion", "auto delete");
857 }
858
859 void completion_deinit(void)
860 {
861         free_completions();
862
863         chat_completion_deinit();
864
865         command_unbind("completion", (SIGNAL_FUNC) cmd_completion);
866
867         signal_remove("complete word", (SIGNAL_FUNC) sig_complete_word);
868         signal_remove("complete erase", (SIGNAL_FUNC) sig_complete_erase);
869         signal_remove("complete command set", (SIGNAL_FUNC) sig_complete_set);
870         signal_remove("complete command toggle", (SIGNAL_FUNC) sig_complete_toggle);
871         signal_remove("complete command load", (SIGNAL_FUNC) sig_complete_filename);
872         signal_remove("complete command cat", (SIGNAL_FUNC) sig_complete_filename);
873         signal_remove("complete command save", (SIGNAL_FUNC) sig_complete_filename);
874         signal_remove("complete command reload", (SIGNAL_FUNC) sig_complete_filename);
875         signal_remove("complete command rawlog open", (SIGNAL_FUNC) sig_complete_filename);
876         signal_remove("complete command rawlog save", (SIGNAL_FUNC) sig_complete_filename);
877         signal_remove("complete command help", (SIGNAL_FUNC) sig_complete_command);
878 }