4 Copyright (C) 2000 Timo Sirainen
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.
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.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "module-formats.h"
27 #include "lib-config/iconfig.h"
30 #include "completion.h"
31 #include "printtext.h"
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;
37 #define isseparator_notspace(c) \
40 #define isseparator(c) \
41 ((c) == ' ' || isseparator_notspace(c))
43 void chat_completion_init(void);
44 void chat_completion_deinit(void);
46 static const char *completion_find(const char *key, int automatic)
50 node = iconfig_node_traverse("completions", FALSE);
51 if (node == NULL || node->type != NODE_TYPE_BLOCK)
54 node = config_node_section(node, key, -1);
58 if (automatic && !config_node_get_bool(node, "auto", FALSE))
61 return config_node_get_str(node, "value", NULL);
64 /* Return whole word at specified position in string */
65 static char *get_word_at(const char *str, int pos, char **startpos)
67 const char *start, *end;
69 g_return_val_if_fail(str != NULL, NULL);
70 g_return_val_if_fail(pos >= 0, NULL);
72 /* get previous word if char at `pos' is space */
74 while (start > str && isseparator(start[-1])) start--;
77 while (start > str && !isseparator(start[-1])) start--;
78 while (*end != '\0' && !isseparator(*end)) end++;
79 while (*end != '\0' && isseparator_notspace(*end)) end++;
81 *startpos = (char *) start;
82 return g_strndup(start, (int) (end-start));
85 /* automatic word completion - called when space/enter is pressed */
86 char *auto_word_complete(const char *line, int *pos)
90 char *word, *wordstart, *ret;
93 g_return_val_if_fail(line != NULL, NULL);
94 g_return_val_if_fail(pos != NULL, NULL);
96 word = get_word_at(line, *pos, &wordstart);
97 startpos = (int) (wordstart-line);
99 result = g_string_new(line);
100 g_string_erase(result, startpos, strlen(word));
102 /* check for words in autocompletion list */
103 replace = completion_find(word, TRUE);
104 if (replace == NULL) {
106 g_string_free(result, TRUE);
108 *pos = startpos+strlen(replace);
110 g_string_insert(result, startpos, replace);
112 g_string_free(result, FALSE);
119 static void free_completions(void)
121 complist = g_list_first(complist);
123 g_list_foreach(complist, (GFunc) g_free, NULL);
124 g_list_free(complist);
127 g_free_and_null(last_line);
130 /* manual word completion - called when TAB is pressed */
131 char *word_complete(WINDOW_REC *window, const char *line, int *pos, int erase, int backward)
133 static int startpos = 0, wordlen = 0;
134 int old_startpos, old_wordlen;
137 char *word, *wordstart, *linestart, *ret;
138 int continue_complete, want_space;
140 g_return_val_if_fail(line != NULL, NULL);
141 g_return_val_if_fail(pos != NULL, NULL);
143 continue_complete = complist != NULL && *pos == last_line_pos &&
144 strcmp(line, last_line) == 0;
146 if (erase && !continue_complete)
149 old_startpos = startpos;
150 old_wordlen = wordlen;
152 if (!erase && continue_complete) {
156 /* get the word we want to complete */
157 word = get_word_at(line, *pos, &wordstart);
158 startpos = (int) (wordstart-line);
159 wordlen = strlen(word);
161 /* get the start of line until the word we're completing */
162 if (isseparator(*line)) {
163 /* empty space at the start of line */
164 if (wordstart == line)
165 wordstart += strlen(wordstart);
167 while (wordstart > line && isseparator(wordstart[-1]))
170 linestart = g_strndup(line, (int) (wordstart-line));
172 /* completions usually add space after the word, that makes
173 things a bit harder. When continuing a completion
174 "/msg nick1 "<tab> we have to cycle to nick2, etc.
175 BUT if we start completion with "/msg "<tab>, we don't
176 want to complete the /msg word, but instead complete empty
177 word with /msg being in linestart. */
178 if (!erase && *pos > 0 && line[*pos-1] == ' ' &&
179 (*linestart == '\0' || wordstart[-1] != ' ')) {
183 linestart = *linestart == '\0' ?
185 g_strconcat(linestart, " ", word, NULL);
190 startpos = strlen(linestart)+1;
197 signal_emit("complete erase", 3, window, word, linestart);
199 /* jump to next completion */
200 startpos = old_startpos;
201 wordlen = old_wordlen;
204 if (continue_complete) {
205 /* complete from old list */
207 complist = complist->prev != NULL ? complist->prev :
208 g_list_last(complist);
210 complist = complist->next != NULL ? complist->next :
211 g_list_first(complist);
212 want_space = last_want_space;
214 /* get new completion list */
218 signal_emit("complete word", 5, &complist, window, word, linestart, &want_space);
219 last_want_space = want_space;
225 if (complist == NULL)
229 *pos = startpos+strlen(complist->data);
231 /* replace the word in line - we need to return
233 result = g_string_new(line);
234 g_string_erase(result, startpos, wordlen);
235 g_string_insert(result, startpos, complist->data);
238 if (!isseparator(result->str[*pos]))
239 g_string_insert_c(result, *pos, ' ');
243 wordlen = strlen(complist->data);
244 last_line_pos = *pos;
245 g_free_not_null(last_line);
246 last_line = g_strdup(result->str);
249 g_string_free(result, FALSE);
253 #define IS_CURRENT_DIR(dir) \
254 ((dir)[0] == '.' && ((dir)[1] == '\0' || (dir)[1] == G_DIR_SEPARATOR))
256 #define USE_DEFAULT_PATH(path, default_path) \
257 ((!g_path_is_absolute(path) || IS_CURRENT_DIR(path)) && \
258 default_path != NULL)
260 static GList *list_add_file(GList *list, const char *name, const char *default_path)
265 g_return_val_if_fail(name != NULL, NULL);
267 fname = convert_home(name);
268 if (USE_DEFAULT_PATH(fname, default_path)) {
270 fname = g_strconcat(default_path, G_DIR_SEPARATOR_S,
273 if (stat(fname, &statbuf) == 0) {
274 list = g_list_append(list, !S_ISDIR(statbuf.st_mode) ? g_strdup(name) :
275 g_strconcat(name, G_DIR_SEPARATOR_S, NULL));
282 GList *filename_complete(const char *path, const char *default_path)
287 const char *basename;
288 char *realpath, *dir, *name;
291 g_return_val_if_fail(path != NULL, NULL);
295 /* get directory part of the path - expand ~/ */
296 realpath = convert_home(path);
297 if (USE_DEFAULT_PATH(realpath, default_path)) {
299 realpath = g_strconcat(default_path, G_DIR_SEPARATOR_S,
303 /* open directory for reading */
304 dir = g_path_get_dirname(realpath);
312 dir = g_path_get_dirname(path);
313 if (*dir == G_DIR_SEPARATOR && dir[1] == '\0') {
314 /* completing file in root directory */
316 } else if (IS_CURRENT_DIR(dir) && !IS_CURRENT_DIR(path)) {
317 /* completing file in default_path
318 (path not set, and leave it that way) */
319 g_free_and_null(dir);
322 basename = g_basename(path);
323 len = strlen(basename);
325 /* add all files in directory to completion list */
326 while ((dp = readdir(dirp)) != NULL) {
327 if (dp->d_name[0] == '.') {
328 if (dp->d_name[1] == '\0' ||
329 (dp->d_name[1] == '.' && dp->d_name[2] == '\0'))
330 continue; /* skip . and .. */
332 if (basename[0] != '.')
336 if (len == 0 || strncmp(dp->d_name, basename, len) == 0) {
337 name = dir == NULL ? g_strdup(dp->d_name) :
338 g_strdup_printf("%s"G_DIR_SEPARATOR_S"%s", dir, dp->d_name);
339 list = list_add_file(list, name, default_path);
345 g_free_not_null(dir);
349 static GList *completion_get_settings(const char *key, SettingType type)
355 g_return_val_if_fail(key != NULL, NULL);
357 sets = settings_get_sorted();
361 for (tmp = sets; tmp != NULL; tmp = tmp->next) {
362 SETTINGS_REC *rec = tmp->data;
364 if ((type == -1 || rec->type == type) &&
365 g_strncasecmp(rec->key, key, len) == 0)
366 complist = g_list_insert_sorted(complist, g_strdup(rec->key), (GCompareFunc) g_istr_cmp);
372 static GList *completion_get_aliases(const char *alias, char cmdchar)
380 g_return_val_if_fail(alias != NULL, NULL);
382 /* get list of aliases from mainconfig */
383 node = iconfig_node_traverse("aliases", FALSE);
384 tmp = node == NULL ? NULL : config_node_first(node->value);
388 for (; tmp != NULL; tmp = config_node_next(tmp)) {
389 CONFIG_NODE *node = tmp->data;
391 if (node->type != NODE_TYPE_KEY)
394 if (g_strncasecmp(node->key, alias, len) == 0) {
395 word = g_strdup_printf("%c%s", cmdchar, node->key);
396 /* add matching alias to completion list, aliases will
397 be appended after command completions and kept in
398 uppercase to show it's an alias */
399 if (glist_find_icase_string(complist, word) == NULL)
400 complist = g_list_insert_sorted(complist, word, (GCompareFunc) g_istr_cmp);
408 static GList *completion_get_commands(const char *cmd, char cmdchar)
415 g_return_val_if_fail(cmd != NULL, NULL);
419 for (tmp = commands; tmp != NULL; tmp = tmp->next) {
420 COMMAND_REC *rec = tmp->data;
422 if (strchr(rec->cmd, ' ') != NULL)
425 if (g_strncasecmp(rec->cmd, cmd, len) == 0) {
426 word = cmdchar == '\0' ? g_strdup(rec->cmd) :
427 g_strdup_printf("%c%s", cmdchar, rec->cmd);
428 if (glist_find_icase_string(complist, word) == NULL)
429 complist = g_list_insert_sorted(complist, word, (GCompareFunc) g_istr_cmp);
437 static GList *completion_get_subcommands(const char *cmd)
444 g_return_val_if_fail(cmd != NULL, NULL);
446 /* get the number of chars to skip at the start of command. */
447 spacepos = strrchr(cmd, ' ');
448 skip = spacepos == NULL ? strlen(cmd)+1 :
449 ((int) (spacepos-cmd) + 1);
453 for (tmp = commands; tmp != NULL; tmp = tmp->next) {
454 COMMAND_REC *rec = tmp->data;
456 if ((int)strlen(rec->cmd) < len)
459 if (strchr(rec->cmd+len, ' ') != NULL)
462 if (g_strncasecmp(rec->cmd, cmd, len) == 0)
463 complist = g_list_insert_sorted(complist, g_strdup(rec->cmd+skip), (GCompareFunc) g_istr_cmp);
468 static GList *completion_get_options(const char *cmd, const char *option)
475 g_return_val_if_fail(cmd != NULL, NULL);
476 g_return_val_if_fail(option != NULL, NULL);
478 rec = command_find(cmd);
479 if (rec == NULL || rec->options == NULL) return NULL;
482 len = strlen(option);
483 for (tmp = rec->options; *tmp != NULL; tmp++) {
484 const char *optname = *tmp + iscmdtype(**tmp);
486 if (len == 0 || g_strncasecmp(optname, option, len) == 0)
487 list = g_list_append(list, g_strconcat("-", optname, NULL));
493 /* split the line to command and arguments */
494 static char *line_get_command(const char *line, char **args, int aliases)
496 const char *ptr, *cmdargs;
497 char *cmd, *checkcmd;
499 g_return_val_if_fail(line != NULL, NULL);
500 g_return_val_if_fail(args != NULL, NULL);
502 cmd = checkcmd = NULL; *args = "";
503 cmdargs = NULL; ptr = line;
506 ptr = strchr(ptr, ' ');
508 checkcmd = g_strdup(line);
511 checkcmd = g_strndup(line, (int) (ptr-line));
513 while (*ptr == ' ') ptr++;
517 if (aliases ? !alias_find(checkcmd) :
518 !command_find(checkcmd)) {
519 /* not found, use the previous */
524 /* found, check if it has subcommands */
525 g_free_not_null(cmd);
529 cmd = g_strdup(alias_find(checkcmd));
532 *args = (char *) cmdargs;
533 } while (ptr != NULL);
540 static char *expand_aliases(const char *line)
542 char *cmd, *args, *ret;
544 g_return_val_if_fail(line != NULL, NULL);
546 cmd = line_get_command(line, &args, TRUE);
547 if (cmd == NULL) return g_strdup(line);
548 if (*args == '\0') return cmd;
550 ret = g_strconcat(cmd, " ", args, NULL);
555 static void sig_complete_word(GList **list, WINDOW_REC *window,
556 const char *word, const char *linestart,
559 const char *newword, *cmdchars;
560 char *signal, *cmd, *args, *line;
562 g_return_if_fail(list != NULL);
563 g_return_if_fail(word != NULL);
564 g_return_if_fail(linestart != NULL);
566 /* check against "completion words" list */
567 newword = completion_find(word, FALSE);
568 if (newword != NULL) {
569 *list = g_list_append(*list, g_strdup(newword));
575 if (*linestart != '\0' && (*word == '/' || *word == '~')) {
576 /* quite likely filename completion */
577 *list = g_list_concat(*list, filename_complete(word, NULL));
585 /* command completion? */
586 cmdchars = settings_get_str("cmdchars");
587 if (*word != '\0' && *linestart == '\0' && strchr(cmdchars, *word)) {
588 /* complete /command */
589 *list = completion_get_commands(word+1, *word);
591 /* complete aliases, too */
592 *list = g_list_concat(*list,
593 completion_get_aliases(word+1, *word));
595 if (*list != NULL) signal_stop();
599 /* check only for /command completions from now on */
600 if (*linestart == '\0')
603 cmdchars = strchr(cmdchars, *linestart);
604 if (cmdchars == NULL) return;
606 /* check if there's aliases */
607 line = linestart[1] == *cmdchars ? g_strdup(linestart+2) :
608 expand_aliases(linestart+1);
610 cmd = line_get_command(line, &args, FALSE);
616 /* we're completing -option? */
618 *list = completion_get_options(cmd, word+1);
619 if (*list != NULL) signal_stop();
625 /* complete parameters */
626 signal = g_strconcat("complete command ", cmd, NULL);
627 signal_emit(signal, 5, list, window, word, args, want_space);
629 if (command_have_sub(line)) {
630 /* complete subcommand */
632 cmd = g_strconcat(line, " ", word, NULL);
633 *list = g_list_concat(completion_get_subcommands(cmd), *list);
636 if (*list != NULL) signal_stop();
642 static void sig_complete_erase(WINDOW_REC *window, const char *word,
643 const char *linestart)
645 const char *cmdchars;
646 char *line, *cmd, *args, *signal;
648 if (*linestart == '\0')
651 /* we only want to check for commands */
652 cmdchars = settings_get_str("cmdchars");
653 cmdchars = strchr(cmdchars, *linestart);
654 if (cmdchars == NULL)
657 /* check if there's aliases */
658 line = linestart[1] == *cmdchars ? g_strdup(linestart+2) :
659 expand_aliases(linestart+1);
661 cmd = line_get_command(line, &args, FALSE);
667 signal = g_strconcat("complete erase command ", cmd, NULL);
668 signal_emit(signal, 3, window, word, args);
675 static void sig_complete_set(GList **list, WINDOW_REC *window,
676 const char *word, const char *line, int *want_space)
678 g_return_if_fail(list != NULL);
679 g_return_if_fail(word != NULL);
680 g_return_if_fail(line != NULL);
683 !strcmp("-clear", line) || !strcmp("-default", line))
684 *list = completion_get_settings(word, -1);
685 else if (*line != '\0' && *word == '\0') {
686 SETTINGS_REC *rec = settings_get_record(line);
688 char *value = settings_get_print(rec);
690 *list = g_list_append(*list, value);
694 if (*list != NULL) signal_stop();
697 static void sig_complete_toggle(GList **list, WINDOW_REC *window,
698 const char *word, const char *line, int *want_space)
700 g_return_if_fail(list != NULL);
701 g_return_if_fail(word != NULL);
702 g_return_if_fail(line != NULL);
704 if (*line != '\0') return;
706 *list = completion_get_settings(word, SETTING_TYPE_BOOLEAN);
707 if (*list != NULL) signal_stop();
710 /* first argument of command is file name - complete it */
711 static void sig_complete_filename(GList **list, WINDOW_REC *window,
712 const char *word, const char *line, int *want_space)
714 g_return_if_fail(list != NULL);
715 g_return_if_fail(word != NULL);
716 g_return_if_fail(line != NULL);
718 if (*line != '\0') return;
720 *list = filename_complete(word, NULL);
727 /* first argument of command is .. command :) (/HELP command) */
728 static void sig_complete_command(GList **list, WINDOW_REC *window,
729 const char *word, const char *line, int *want_space)
733 g_return_if_fail(list != NULL);
734 g_return_if_fail(word != NULL);
735 g_return_if_fail(line != NULL);
738 /* complete base command */
739 *list = completion_get_commands(word, '\0');
740 } else if (command_have_sub(line)) {
741 /* complete subcommand */
742 cmd = g_strconcat(line, " ", word, NULL);
743 *list = completion_get_subcommands(cmd);
747 if (*list != NULL) signal_stop();
750 static void cmd_completion(const char *data)
759 if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_OPTIONS |
761 "completion", &optlist, &key, &value))
764 node = iconfig_node_traverse("completions", *value != '\0');
765 if (node != NULL && node->type != NODE_TYPE_BLOCK) {
766 /* FIXME: remove after 0.8.5 */
767 iconfig_node_remove(mainconfig->mainnode, node);
768 node = iconfig_node_traverse("completions", *value != '\0');
771 if (node == NULL || (node->value == NULL && *value == '\0')) {
772 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
774 cmd_params_free(free_arg);
778 if (g_hash_table_lookup(optlist, "delete") != NULL && *key != '\0') {
779 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
780 TXT_COMPLETION_REMOVED, key);
782 iconfig_set_str("completions", key, NULL);
783 signal_emit("completion removed", 1, key);
784 } else if (*key != '\0' && *value != '\0') {
785 int automatic = g_hash_table_lookup(optlist, "auto") != NULL;
787 node = config_node_section(node, key, NODE_TYPE_BLOCK);
788 iconfig_node_set_str(node, "value", value);
790 iconfig_node_set_bool(node, "auto", TRUE);
792 iconfig_node_set_str(node, "auto", NULL);
794 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
796 key, value, automatic ? "yes" : "no");
798 signal_emit("completion added", 1, key);
800 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
801 TXT_COMPLETION_HEADER);
804 for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
808 g_strncasecmp(node->key, key, len) == 0) {
809 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
810 TXT_COMPLETION_LINE, node->key,
811 config_node_get_str(node, "value", ""),
812 config_node_get_bool(node, "auto", FALSE) ? "yes" : "no");
816 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
817 TXT_COMPLETION_FOOTER);
820 cmd_params_free(free_arg);
823 void completion_init(void)
826 last_line = NULL; last_line_pos = -1;
828 chat_completion_init();
830 command_bind("completion", NULL, (SIGNAL_FUNC) cmd_completion);
832 signal_add_first("complete word", (SIGNAL_FUNC) sig_complete_word);
833 signal_add_first("complete erase", (SIGNAL_FUNC) sig_complete_erase);
834 signal_add("complete command set", (SIGNAL_FUNC) sig_complete_set);
835 signal_add("complete command toggle", (SIGNAL_FUNC) sig_complete_toggle);
836 signal_add("complete command load", (SIGNAL_FUNC) sig_complete_filename);
837 signal_add("complete command cat", (SIGNAL_FUNC) sig_complete_filename);
838 signal_add("complete command save", (SIGNAL_FUNC) sig_complete_filename);
839 signal_add("complete command reload", (SIGNAL_FUNC) sig_complete_filename);
840 signal_add("complete command rawlog open", (SIGNAL_FUNC) sig_complete_filename);
841 signal_add("complete command rawlog save", (SIGNAL_FUNC) sig_complete_filename);
842 signal_add("complete command help", (SIGNAL_FUNC) sig_complete_command);
844 command_set_options("completion", "auto delete");
847 void completion_deinit(void)
851 chat_completion_deinit();
853 command_unbind("completion", (SIGNAL_FUNC) cmd_completion);
855 signal_remove("complete word", (SIGNAL_FUNC) sig_complete_word);
856 signal_remove("complete erase", (SIGNAL_FUNC) sig_complete_erase);
857 signal_remove("complete command set", (SIGNAL_FUNC) sig_complete_set);
858 signal_remove("complete command toggle", (SIGNAL_FUNC) sig_complete_toggle);
859 signal_remove("complete command load", (SIGNAL_FUNC) sig_complete_filename);
860 signal_remove("complete command cat", (SIGNAL_FUNC) sig_complete_filename);
861 signal_remove("complete command save", (SIGNAL_FUNC) sig_complete_filename);
862 signal_remove("complete command reload", (SIGNAL_FUNC) sig_complete_filename);
863 signal_remove("complete command rawlog open", (SIGNAL_FUNC) sig_complete_filename);
864 signal_remove("complete command rawlog save", (SIGNAL_FUNC) sig_complete_filename);
865 signal_remove("complete command help", (SIGNAL_FUNC) sig_complete_command);