/* automatic word completion - called when space/enter is pressed */ char *auto_word_complete(const char *line, int *pos) { GString *result; const char *replace; char *word, *wordstart, *ret; int startpos; g_return_val_if_fail(line != NULL, NULL); g_return_val_if_fail(pos != NULL, NULL); word = get_word_at(line, *pos, &wordstart); startpos = (int) (wordstart-line); result = g_string_new(line); g_string_erase(result, startpos, strlen(word)); /* check for words in autocompletion list */ replace = completion_find(word, TRUE); if (replace == NULL) { ret = NULL; g_string_free(result, TRUE); } else { *pos = startpos+strlen(replace); g_string_insert(result, startpos, replace); ret = result->str; g_string_free(result, FALSE); } g_free(word); return ret; }
static void sig_complete_word(GList **list, WINDOW_REC *window, const char *word, const char *linestart, int *want_space) { const char *newword, *cmdchars; char *signal, *cmd, *args, *line; g_return_if_fail(list != NULL); g_return_if_fail(word != NULL); g_return_if_fail(linestart != NULL); /* check against "completion words" list */ newword = completion_find(word); if (newword != NULL) { *list = g_list_append(*list, g_strdup(newword)); signal_stop(); return; } /* command completion? */ cmdchars = settings_get_str("cmdchars"); if (strchr(cmdchars, *word) && *linestart == '\0') { /* complete /command */ *list = completion_get_commands(word+1, *word); if (*list != NULL) signal_stop(); return; } /* check only for /command completions from now on */ 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; } /* we're completing -option? */ if (*word == '-') { *list = completion_get_options(cmd, word+1); g_free(cmd); g_free(line); return; } /* complete parameters */ signal = g_strconcat("complete command ", cmd, NULL); signal_emit(signal, 5, list, window, word, args, want_space); if (command_have_sub(line)) { /* complete subcommand */ g_free(cmd); cmd = g_strconcat(line, " ", word, NULL); *list = g_list_concat(completion_get_subcommands(cmd), *list); if (*list != NULL) signal_stop(); } g_free(signal); g_free(cmd); g_free(line); }