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
25 #include "lib-config/iconfig.h"
28 #include "completion.h"
30 #define wordreplace_find(word) \
31 iconfig_list_find("replaces", "text", word, "replace")
33 #define completion_find(completion) \
34 iconfig_list_find("completions", "short", completion, "long")
36 static GList *complist; /* list of commands we're currently completing */
37 static char *last_line;
38 static int last_want_space, last_line_pos;
40 #define isseparator_notspace(c) \
43 #define isseparator(c) \
44 (i_isspace(c) || isseparator_notspace(c))
46 void chat_completion_init(void);
47 void chat_completion_deinit(void);
49 /* Return whole word at specified position in string */
50 char *get_word_at(const char *str, int pos, char **startpos)
52 const char *start, *end;
54 g_return_val_if_fail(str != NULL, NULL);
55 g_return_val_if_fail(pos >= 0, NULL);
57 /* get previous word if char at `pos' is space */
59 while (start > str && isseparator(start[-1])) start--;
62 while (start > str && !isseparator(start[-1])) start--;
63 while (*end != '\0' && !isseparator(*end)) end++;
64 while (*end != '\0' && isseparator_notspace(*end)) end++;
66 *startpos = (char *) start;
67 return g_strndup(start, (int) (end-start));
70 /* automatic word completion - called when space/enter is pressed */
71 char *auto_word_complete(const char *line, int *pos)
75 char *word, *wordstart, *ret;
78 g_return_val_if_fail(line != NULL, NULL);
79 g_return_val_if_fail(pos != NULL, NULL);
81 word = get_word_at(line, *pos, &wordstart);
82 startpos = (int) (wordstart-line);
84 result = g_string_new(line);
85 g_string_erase(result, startpos, strlen(word));
87 /* check for words in autocompletion list */
88 replace = wordreplace_find(word);
89 if (replace == NULL) {
91 g_string_free(result, TRUE);
93 *pos = startpos+strlen(replace);
95 g_string_insert(result, startpos, replace);
97 g_string_free(result, FALSE);
104 static void free_completions(void)
106 complist = g_list_first(complist);
108 g_list_foreach(complist, (GFunc) g_free, NULL);
109 g_list_free(complist);
112 g_free_and_null(last_line);
115 /* manual word completion - called when TAB is pressed */
116 char *word_complete(WINDOW_REC *window, const char *line, int *pos, int erase)
118 static int startpos = 0, wordlen = 0;
119 int old_startpos, old_wordlen;
122 char *word, *wordstart, *linestart, *ret;
123 int continue_complete, want_space;
125 g_return_val_if_fail(line != NULL, NULL);
126 g_return_val_if_fail(pos != NULL, NULL);
128 continue_complete = complist != NULL && *pos == last_line_pos &&
129 strcmp(line, last_line) == 0;
131 old_startpos = startpos;
132 old_wordlen = wordlen;
134 if (!erase && continue_complete) {
138 /* get the word we want to complete */
139 word = get_word_at(line, *pos, &wordstart);
140 startpos = (int) (wordstart-line);
141 wordlen = strlen(word);
143 /* get the start of line until the word we're completing */
144 if (isseparator(*line)) {
145 /* empty space at the start of line */
146 if (wordstart == line)
147 wordstart += strlen(wordstart);
149 while (wordstart > line && isseparator(wordstart[-1]))
152 linestart = g_strndup(line, (int) (wordstart-line));
154 /* completions usually add space after the word, that makes
155 things a bit harder. When continuing a completion
156 "/msg nick1 "<tab> we have to cycle to nick2, etc.
157 BUT if we start completion with "/msg "<tab>, we don't
158 want to complete the /msg word, but instead complete empty
159 word with /msg being in linestart. */
160 if (!erase && *pos > 0 && line[*pos-1] == ' ' &&
161 (*linestart == '\0' || wordstart[-1] != ' ')) {
165 linestart = *linestart == '\0' ?
167 g_strconcat(linestart, " ", word, NULL);
172 startpos = strlen(linestart)+1;
179 signal_emit("complete erase", 3, window, word, linestart);
181 if (!continue_complete)
184 /* jump to next completion */
187 startpos = old_startpos;
188 wordlen = old_wordlen;
191 if (continue_complete) {
192 /* complete from old list */
193 complist = complist->next != NULL ? complist->next :
194 g_list_first(complist);
195 want_space = last_want_space;
197 /* get new completion list */
201 signal_emit("complete word", 5, &complist, window, word, linestart, &want_space);
202 last_want_space = want_space;
208 if (complist == NULL)
212 *pos = startpos+strlen(complist->data);
214 /* replace the word in line - we need to return
216 result = g_string_new(line);
217 g_string_erase(result, startpos, wordlen);
218 g_string_insert(result, startpos, complist->data);
221 if (!isseparator(result->str[*pos]))
222 g_string_insert_c(result, *pos, ' ');
226 wordlen = strlen(complist->data);
227 last_line_pos = *pos;
228 g_free_not_null(last_line);
229 last_line = g_strdup(result->str);
232 g_string_free(result, FALSE);
236 #define IS_CURRENT_DIR(dir) \
237 ((dir)[0] == '.' && ((dir)[1] == '\0' || (dir)[1] == G_DIR_SEPARATOR))
239 #define USE_DEFAULT_PATH(path, default_path) \
240 ((!g_path_is_absolute(path) || IS_CURRENT_DIR(path)) && \
241 default_path != NULL)
243 GList *list_add_file(GList *list, const char *name, const char *default_path)
248 g_return_val_if_fail(name != NULL, NULL);
250 fname = convert_home(name);
251 if (USE_DEFAULT_PATH(fname, default_path)) {
253 fname = g_strconcat(default_path, G_DIR_SEPARATOR_S,
256 if (stat(fname, &statbuf) == 0) {
257 list = g_list_append(list, !S_ISDIR(statbuf.st_mode) ? g_strdup(name) :
258 g_strconcat(name, G_DIR_SEPARATOR_S, NULL));
265 GList *filename_complete(const char *path, const char *default_path)
270 char *realpath, *dir, *basename, *name;
273 g_return_val_if_fail(path != NULL, NULL);
277 /* get directory part of the path - expand ~/ */
278 realpath = convert_home(path);
279 if (USE_DEFAULT_PATH(realpath, default_path)) {
281 realpath = g_strconcat(default_path, G_DIR_SEPARATOR_S,
285 /* open directory for reading */
286 dir = g_dirname(realpath);
294 dir = g_dirname(path);
295 if (*dir == G_DIR_SEPARATOR && dir[1] == '\0') {
296 /* completing file in root directory */
298 } else if (IS_CURRENT_DIR(dir) && !IS_CURRENT_DIR(path)) {
299 /* completing file in default_path
300 (path not set, and leave it that way) */
301 g_free_and_null(dir);
304 basename = g_basename(path);
305 len = strlen(basename);
307 /* add all files in directory to completion list */
308 while ((dp = readdir(dirp)) != NULL) {
309 if (dp->d_name[0] == '.') {
310 if (dp->d_name[1] == '\0' ||
311 (dp->d_name[1] == '.' && dp->d_name[2] == '\0'))
312 continue; /* skip . and .. */
314 if (basename[0] != '.')
318 if (len == 0 || strncmp(dp->d_name, basename, len) == 0) {
319 name = dir == NULL ? g_strdup(dp->d_name) :
320 g_strdup_printf("%s"G_DIR_SEPARATOR_S"%s", dir, dp->d_name);
321 list = list_add_file(list, name, default_path);
327 g_free_not_null(dir);
331 static GList *completion_get_settings(const char *key)
337 g_return_val_if_fail(key != NULL, NULL);
339 sets = settings_get_sorted();
343 for (tmp = sets; tmp != NULL; tmp = tmp->next) {
344 SETTINGS_REC *rec = tmp->data;
346 if (g_strncasecmp(rec->key, key, len) == 0)
347 complist = g_list_insert_sorted(complist, g_strdup(rec->key), (GCompareFunc) g_istr_cmp);
353 static GList *completion_get_bool_settings(const char *key)
359 g_return_val_if_fail(key != NULL, NULL);
361 sets = settings_get_sorted();
365 for (tmp = sets; tmp != NULL; tmp = tmp->next) {
366 SETTINGS_REC *rec = tmp->data;
368 if (rec->type == SETTING_TYPE_BOOLEAN &&
369 g_strncasecmp(rec->key, key, len) == 0)
370 complist = g_list_insert_sorted(complist, g_strdup(rec->key), (GCompareFunc) g_istr_cmp);
376 static GList *completion_get_aliases(const char *alias, char cmdchar)
384 g_return_val_if_fail(alias != NULL, NULL);
386 /* get list of aliases from mainconfig */
387 node = iconfig_node_traverse("aliases", FALSE);
388 tmp = node == NULL ? NULL : config_node_first(node->value);
392 for (; tmp != NULL; tmp = config_node_next(tmp)) {
393 CONFIG_NODE *node = tmp->data;
395 if (node->type != NODE_TYPE_KEY)
398 if (g_strncasecmp(node->key, alias, len) == 0) {
399 word = g_strdup_printf("%c%s", cmdchar, node->key);
400 /* add matching alias to completion list, aliases will
401 be appended after command completions and kept in
402 uppercase to show it's an alias */
403 if (glist_find_icase_string(complist, word) == NULL)
404 complist = g_list_insert_sorted(complist, word, (GCompareFunc) g_istr_cmp);
412 static GList *completion_get_commands(const char *cmd, char cmdchar)
419 g_return_val_if_fail(cmd != NULL, NULL);
423 for (tmp = commands; tmp != NULL; tmp = tmp->next) {
424 COMMAND_REC *rec = tmp->data;
426 if (strchr(rec->cmd, ' ') != NULL)
429 if (g_strncasecmp(rec->cmd, cmd, len) == 0) {
430 word = cmdchar == '\0' ? g_strdup(rec->cmd) :
431 g_strdup_printf("%c%s", cmdchar, rec->cmd);
432 if (glist_find_icase_string(complist, word) == NULL)
433 complist = g_list_insert_sorted(complist, word, (GCompareFunc) g_istr_cmp);
441 static GList *completion_get_subcommands(const char *cmd)
448 g_return_val_if_fail(cmd != NULL, NULL);
450 /* get the number of chars to skip at the start of command. */
451 spacepos = strrchr(cmd, ' ');
452 skip = spacepos == NULL ? strlen(cmd)+1 :
453 ((int) (spacepos-cmd) + 1);
457 for (tmp = commands; tmp != NULL; tmp = tmp->next) {
458 COMMAND_REC *rec = tmp->data;
460 if ((int)strlen(rec->cmd) < len)
463 if (strchr(rec->cmd+len, ' ') != NULL)
466 if (g_strncasecmp(rec->cmd, cmd, len) == 0)
467 complist = g_list_insert_sorted(complist, g_strdup(rec->cmd+skip), (GCompareFunc) g_istr_cmp);
472 GList *completion_get_options(const char *cmd, const char *option)
479 g_return_val_if_fail(cmd != NULL, NULL);
480 g_return_val_if_fail(option != NULL, NULL);
482 rec = command_find(cmd);
483 if (rec == NULL || rec->options == NULL) return NULL;
486 len = strlen(option);
487 for (tmp = rec->options; *tmp != NULL; tmp++) {
488 const char *optname = *tmp + iscmdtype(**tmp);
490 if (len == 0 || g_strncasecmp(optname, option, len) == 0)
491 list = g_list_append(list, g_strconcat("-", optname, NULL));
497 /* split the line to command and arguments */
498 static char *line_get_command(const char *line, char **args, int aliases)
500 const char *ptr, *cmdargs;
501 char *cmd, *checkcmd;
503 g_return_val_if_fail(line != NULL, NULL);
504 g_return_val_if_fail(args != NULL, NULL);
506 cmd = checkcmd = NULL; *args = "";
507 cmdargs = NULL; ptr = line;
510 ptr = strchr(ptr, ' ');
512 checkcmd = g_strdup(line);
515 checkcmd = g_strndup(line, (int) (ptr-line));
517 while (i_isspace(*ptr)) ptr++;
521 if (aliases ? !alias_find(checkcmd) :
522 !command_find(checkcmd)) {
523 /* not found, use the previous */
528 /* found, check if it has subcommands */
529 g_free_not_null(cmd);
533 cmd = g_strdup(alias_find(checkcmd));
536 *args = (char *) cmdargs;
537 } while (ptr != NULL);
544 static char *expand_aliases(const char *line)
546 char *cmd, *args, *ret;
548 g_return_val_if_fail(line != NULL, NULL);
550 cmd = line_get_command(line, &args, TRUE);
551 if (cmd == NULL) return g_strdup(line);
552 if (*args == '\0') return cmd;
554 ret = g_strconcat(cmd, " ", args, NULL);
559 static void sig_complete_word(GList **list, WINDOW_REC *window,
560 const char *word, const char *linestart,
563 const char *newword, *cmdchars;
564 char *signal, *cmd, *args, *line;
566 g_return_if_fail(list != NULL);
567 g_return_if_fail(word != NULL);
568 g_return_if_fail(linestart != NULL);
570 /* check against "completion words" list */
571 newword = completion_find(word);
572 if (newword != NULL) {
573 *list = g_list_append(*list, g_strdup(newword));
579 /* command completion? */
580 cmdchars = settings_get_str("cmdchars");
581 if (*word != '\0' && *linestart == '\0' && strchr(cmdchars, *word)) {
582 /* complete /command */
583 *list = completion_get_commands(word+1, *word);
585 /* complete aliases, too */
586 *list = g_list_concat(*list,
587 completion_get_aliases(word+1, *word));
589 if (*list != NULL) signal_stop();
593 /* check only for /command completions from now on */
594 if (*linestart == '\0')
597 cmdchars = strchr(cmdchars, *linestart);
598 if (cmdchars == NULL) return;
600 /* check if there's aliases */
601 line = linestart[1] == *cmdchars ? g_strdup(linestart+2) :
602 expand_aliases(linestart+1);
604 cmd = line_get_command(line, &args, FALSE);
610 /* we're completing -option? */
612 *list = completion_get_options(cmd, word+1);
618 /* complete parameters */
619 signal = g_strconcat("complete command ", cmd, NULL);
620 signal_emit(signal, 5, list, window, word, args, want_space);
622 if (command_have_sub(line)) {
623 /* complete subcommand */
625 cmd = g_strconcat(line, " ", word, NULL);
626 *list = g_list_concat(completion_get_subcommands(cmd), *list);
628 if (*list != NULL) signal_stop();
637 static void sig_complete_erase(WINDOW_REC *window, const char *word,
638 const char *linestart)
640 const char *cmdchars;
641 char *line, *cmd, *args, *signal;
643 if (*linestart == '\0')
646 /* we only want to check for commands */
647 cmdchars = settings_get_str("cmdchars");
648 cmdchars = strchr(cmdchars, *linestart);
649 if (cmdchars == NULL)
652 /* check if there's aliases */
653 line = linestart[1] == *cmdchars ? g_strdup(linestart+2) :
654 expand_aliases(linestart+1);
656 cmd = line_get_command(line, &args, FALSE);
662 signal = g_strconcat("complete erase command ", cmd, NULL);
663 signal_emit(signal, 3, window, word, args);
670 static void sig_complete_set(GList **list, WINDOW_REC *window,
671 const char *word, const char *line, int *want_space)
673 g_return_if_fail(list != NULL);
674 g_return_if_fail(word != NULL);
675 g_return_if_fail(line != NULL);
677 if (*line != '\0') return;
679 *list = completion_get_settings(word);
680 if (*list != NULL) signal_stop();
683 static void sig_complete_toggle(GList **list, WINDOW_REC *window,
684 const char *word, const char *line, int *want_space)
686 g_return_if_fail(list != NULL);
687 g_return_if_fail(word != NULL);
688 g_return_if_fail(line != NULL);
690 if (*line != '\0') return;
692 *list = completion_get_bool_settings(word);
693 if (*list != NULL) signal_stop();
696 /* first argument of command is file name - complete it */
697 static void sig_complete_filename(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 = filename_complete(word, NULL);
713 /* first argument of command is .. command :) (/HELP command) */
714 static void sig_complete_command(GList **list, WINDOW_REC *window,
715 const char *word, const char *line, int *want_space)
719 g_return_if_fail(list != NULL);
720 g_return_if_fail(word != NULL);
721 g_return_if_fail(line != NULL);
724 /* complete base command */
725 *list = completion_get_commands(word, '\0');
726 } else if (command_have_sub(line)) {
727 /* complete subcommand */
728 cmd = g_strconcat(line, " ", word, NULL);
729 *list = completion_get_subcommands(cmd);
733 if (*list != NULL) signal_stop();
736 void completion_init(void)
739 last_line = NULL; last_line_pos = -1;
741 chat_completion_init();
743 signal_add_first("complete word", (SIGNAL_FUNC) sig_complete_word);
744 signal_add_first("complete erase", (SIGNAL_FUNC) sig_complete_erase);
745 signal_add("complete command set", (SIGNAL_FUNC) sig_complete_set);
746 signal_add("complete command toggle", (SIGNAL_FUNC) sig_complete_toggle);
747 signal_add("complete command cat", (SIGNAL_FUNC) sig_complete_filename);
748 signal_add("complete command save", (SIGNAL_FUNC) sig_complete_filename);
749 signal_add("complete command reload", (SIGNAL_FUNC) sig_complete_filename);
750 signal_add("complete command rawlog open", (SIGNAL_FUNC) sig_complete_filename);
751 signal_add("complete command rawlog save", (SIGNAL_FUNC) sig_complete_filename);
752 signal_add("complete command help", (SIGNAL_FUNC) sig_complete_command);
755 void completion_deinit(void)
759 chat_completion_deinit();
761 signal_remove("complete word", (SIGNAL_FUNC) sig_complete_word);
762 signal_remove("complete erase", (SIGNAL_FUNC) sig_complete_erase);
763 signal_remove("complete command set", (SIGNAL_FUNC) sig_complete_set);
764 signal_remove("complete command toggle", (SIGNAL_FUNC) sig_complete_toggle);
765 signal_remove("complete command cat", (SIGNAL_FUNC) sig_complete_filename);
766 signal_remove("complete command save", (SIGNAL_FUNC) sig_complete_filename);
767 signal_remove("complete command reload", (SIGNAL_FUNC) sig_complete_filename);
768 signal_remove("complete command rawlog open", (SIGNAL_FUNC) sig_complete_filename);
769 signal_remove("complete command rawlog save", (SIGNAL_FUNC) sig_complete_filename);
770 signal_remove("complete command help", (SIGNAL_FUNC) sig_complete_command);