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
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
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 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)
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 old_startpos = startpos;
147 old_wordlen = wordlen;
149 if (!erase && continue_complete) {
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);
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);
164 while (wordstart > line && isseparator(wordstart[-1]))
167 linestart = g_strndup(line, (int) (wordstart-line));
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] != ' ')) {
180 linestart = *linestart == '\0' ?
182 g_strconcat(linestart, " ", word, NULL);
187 startpos = strlen(linestart)+1;
194 signal_emit("complete erase", 3, window, word, linestart);
196 if (!continue_complete)
199 /* jump to next completion */
202 startpos = old_startpos;
203 wordlen = old_wordlen;
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;
212 /* get new completion list */
216 signal_emit("complete word", 5, &complist, window, word, linestart, &want_space);
217 last_want_space = want_space;
223 if (complist == NULL)
227 *pos = startpos+strlen(complist->data);
229 /* replace the word in line - we need to return
231 result = g_string_new(line);
232 g_string_erase(result, startpos, wordlen);
233 g_string_insert(result, startpos, complist->data);
236 if (!isseparator(result->str[*pos]))
237 g_string_insert_c(result, *pos, ' ');
241 wordlen = strlen(complist->data);
242 last_line_pos = *pos;
243 g_free_not_null(last_line);
244 last_line = g_strdup(result->str);
247 g_string_free(result, FALSE);
251 #define IS_CURRENT_DIR(dir) \
252 ((dir)[0] == '.' && ((dir)[1] == '\0' || (dir)[1] == G_DIR_SEPARATOR))
254 #define USE_DEFAULT_PATH(path, default_path) \
255 ((!g_path_is_absolute(path) || IS_CURRENT_DIR(path)) && \
256 default_path != NULL)
258 GList *list_add_file(GList *list, const char *name, const char *default_path)
263 g_return_val_if_fail(name != NULL, NULL);
265 fname = convert_home(name);
266 if (USE_DEFAULT_PATH(fname, default_path)) {
268 fname = g_strconcat(default_path, G_DIR_SEPARATOR_S,
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));
280 GList *filename_complete(const char *path, const char *default_path)
285 const char *basename;
286 char *realpath, *dir, *name;
289 g_return_val_if_fail(path != NULL, NULL);
293 /* get directory part of the path - expand ~/ */
294 realpath = convert_home(path);
295 if (USE_DEFAULT_PATH(realpath, default_path)) {
297 realpath = g_strconcat(default_path, G_DIR_SEPARATOR_S,
301 /* open directory for reading */
302 dir = g_dirname(realpath);
310 dir = g_dirname(path);
311 if (*dir == G_DIR_SEPARATOR && dir[1] == '\0') {
312 /* completing file in root directory */
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);
320 basename = g_basename(path);
321 len = strlen(basename);
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 .. */
330 if (basename[0] != '.')
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);
343 g_free_not_null(dir);
347 static GList *completion_get_settings(const char *key)
353 g_return_val_if_fail(key != NULL, NULL);
355 sets = settings_get_sorted();
359 for (tmp = sets; tmp != NULL; tmp = tmp->next) {
360 SETTINGS_REC *rec = tmp->data;
362 if (g_strncasecmp(rec->key, key, len) == 0)
363 complist = g_list_insert_sorted(complist, g_strdup(rec->key), (GCompareFunc) g_istr_cmp);
369 static GList *completion_get_bool_settings(const char *key)
375 g_return_val_if_fail(key != NULL, NULL);
377 sets = settings_get_sorted();
381 for (tmp = sets; tmp != NULL; tmp = tmp->next) {
382 SETTINGS_REC *rec = tmp->data;
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);
392 static GList *completion_get_aliases(const char *alias, char cmdchar)
400 g_return_val_if_fail(alias != NULL, NULL);
402 /* get list of aliases from mainconfig */
403 node = iconfig_node_traverse("aliases", FALSE);
404 tmp = node == NULL ? NULL : config_node_first(node->value);
408 for (; tmp != NULL; tmp = config_node_next(tmp)) {
409 CONFIG_NODE *node = tmp->data;
411 if (node->type != NODE_TYPE_KEY)
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);
428 static GList *completion_get_commands(const char *cmd, char cmdchar)
435 g_return_val_if_fail(cmd != NULL, NULL);
439 for (tmp = commands; tmp != NULL; tmp = tmp->next) {
440 COMMAND_REC *rec = tmp->data;
442 if (strchr(rec->cmd, ' ') != NULL)
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);
457 static GList *completion_get_subcommands(const char *cmd)
464 g_return_val_if_fail(cmd != NULL, NULL);
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);
473 for (tmp = commands; tmp != NULL; tmp = tmp->next) {
474 COMMAND_REC *rec = tmp->data;
476 if ((int)strlen(rec->cmd) < len)
479 if (strchr(rec->cmd+len, ' ') != NULL)
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);
488 GList *completion_get_options(const char *cmd, const char *option)
495 g_return_val_if_fail(cmd != NULL, NULL);
496 g_return_val_if_fail(option != NULL, NULL);
498 rec = command_find(cmd);
499 if (rec == NULL || rec->options == NULL) return NULL;
502 len = strlen(option);
503 for (tmp = rec->options; *tmp != NULL; tmp++) {
504 const char *optname = *tmp + iscmdtype(**tmp);
506 if (len == 0 || g_strncasecmp(optname, option, len) == 0)
507 list = g_list_append(list, g_strconcat("-", optname, NULL));
513 /* split the line to command and arguments */
514 static char *line_get_command(const char *line, char **args, int aliases)
516 const char *ptr, *cmdargs;
517 char *cmd, *checkcmd;
519 g_return_val_if_fail(line != NULL, NULL);
520 g_return_val_if_fail(args != NULL, NULL);
522 cmd = checkcmd = NULL; *args = "";
523 cmdargs = NULL; ptr = line;
526 ptr = strchr(ptr, ' ');
528 checkcmd = g_strdup(line);
531 checkcmd = g_strndup(line, (int) (ptr-line));
533 while (*ptr == ' ') ptr++;
537 if (aliases ? !alias_find(checkcmd) :
538 !command_find(checkcmd)) {
539 /* not found, use the previous */
544 /* found, check if it has subcommands */
545 g_free_not_null(cmd);
549 cmd = g_strdup(alias_find(checkcmd));
552 *args = (char *) cmdargs;
553 } while (ptr != NULL);
560 static char *expand_aliases(const char *line)
562 char *cmd, *args, *ret;
564 g_return_val_if_fail(line != NULL, NULL);
566 cmd = line_get_command(line, &args, TRUE);
567 if (cmd == NULL) return g_strdup(line);
568 if (*args == '\0') return cmd;
570 ret = g_strconcat(cmd, " ", args, NULL);
575 static void sig_complete_word(GList **list, WINDOW_REC *window,
576 const char *word, const char *linestart,
579 const char *newword, *cmdchars;
580 char *signal, *cmd, *args, *line;
582 g_return_if_fail(list != NULL);
583 g_return_if_fail(word != NULL);
584 g_return_if_fail(linestart != NULL);
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));
595 if (*linestart != '\0' && (*word == '/' || *word == '~')) {
596 /* quite likely filename completion */
597 *list = g_list_concat(*list, filename_complete(word, NULL));
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);
611 /* complete aliases, too */
612 *list = g_list_concat(*list,
613 completion_get_aliases(word+1, *word));
615 if (*list != NULL) signal_stop();
619 /* check only for /command completions from now on */
620 if (*linestart == '\0')
623 cmdchars = strchr(cmdchars, *linestart);
624 if (cmdchars == NULL) return;
626 /* check if there's aliases */
627 line = linestart[1] == *cmdchars ? g_strdup(linestart+2) :
628 expand_aliases(linestart+1);
630 cmd = line_get_command(line, &args, FALSE);
636 /* we're completing -option? */
638 *list = completion_get_options(cmd, word+1);
644 /* complete parameters */
645 signal = g_strconcat("complete command ", cmd, NULL);
646 signal_emit(signal, 5, list, window, word, args, want_space);
648 if (command_have_sub(line)) {
649 /* complete subcommand */
651 cmd = g_strconcat(line, " ", word, NULL);
652 *list = g_list_concat(completion_get_subcommands(cmd), *list);
654 if (*list != NULL) signal_stop();
663 static void sig_complete_erase(WINDOW_REC *window, const char *word,
664 const char *linestart)
666 const char *cmdchars;
667 char *line, *cmd, *args, *signal;
669 if (*linestart == '\0')
672 /* we only want to check for commands */
673 cmdchars = settings_get_str("cmdchars");
674 cmdchars = strchr(cmdchars, *linestart);
675 if (cmdchars == NULL)
678 /* check if there's aliases */
679 line = linestart[1] == *cmdchars ? g_strdup(linestart+2) :
680 expand_aliases(linestart+1);
682 cmd = line_get_command(line, &args, FALSE);
688 signal = g_strconcat("complete erase command ", cmd, NULL);
689 signal_emit(signal, 3, window, word, args);
696 static void sig_complete_set(GList **list, WINDOW_REC *window,
697 const char *word, const char *line, int *want_space)
699 g_return_if_fail(list != NULL);
700 g_return_if_fail(word != NULL);
701 g_return_if_fail(line != NULL);
703 if (*line != '\0') return;
705 *list = completion_get_settings(word);
706 if (*list != NULL) signal_stop();
709 static void sig_complete_toggle(GList **list, WINDOW_REC *window,
710 const char *word, const char *line, int *want_space)
712 g_return_if_fail(list != NULL);
713 g_return_if_fail(word != NULL);
714 g_return_if_fail(line != NULL);
716 if (*line != '\0') return;
718 *list = completion_get_bool_settings(word);
719 if (*list != NULL) signal_stop();
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)
726 g_return_if_fail(list != NULL);
727 g_return_if_fail(word != NULL);
728 g_return_if_fail(line != NULL);
730 if (*line != '\0') return;
732 *list = filename_complete(word, NULL);
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)
745 g_return_if_fail(list != NULL);
746 g_return_if_fail(word != NULL);
747 g_return_if_fail(line != NULL);
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);
759 if (*list != NULL) signal_stop();
762 static void cmd_completion(const char *data)
771 if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_OPTIONS |
773 "completion", &optlist, &key, &value))
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');
783 if (node == NULL || (node->value == NULL && *value == '\0')) {
784 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
786 cmd_params_free(free_arg);
790 if (g_hash_table_lookup(optlist, "delete") != NULL && *key != '\0') {
791 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
792 TXT_COMPLETION_REMOVED, key);
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;
799 node = config_node_section(node, key, NODE_TYPE_BLOCK);
800 iconfig_node_set_str(node, "value", value);
802 iconfig_node_set_bool(node, "auto", TRUE);
804 iconfig_node_set_str(node, "auto", NULL);
806 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
808 key, value, automatic ? "yes" : "no");
810 signal_emit("completion added", 1, key);
812 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
813 TXT_COMPLETION_HEADER);
816 for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
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");
828 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
829 TXT_COMPLETION_FOOTER);
832 cmd_params_free(free_arg);
835 void completion_init(void)
838 last_line = NULL; last_line_pos = -1;
840 chat_completion_init();
842 command_bind("completion", NULL, (SIGNAL_FUNC) cmd_completion);
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);
856 command_set_options("completion", "auto delete");
859 void completion_deinit(void)
863 chat_completion_deinit();
865 command_unbind("completion", (SIGNAL_FUNC) cmd_completion);
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);