【发布时间】:2013-10-30 17:31:14
【问题描述】:
这是一个使用 libedit 或 GNU readlines 生成自动完成选项的简单程序
#include <readline/readline.h>
#include <stdlib.h>
#include <string.h>
char *command_generator(char *word, int state) {
char *options[] = {"I_love_math", "he_love_math", "she_loves_math", "they_love_math", NULL};
return options[state] ? strdup(options[state]) : NULL;
}
int main(int argc, char **argv) {
rl_readline_name = "rl_example";
rl_completion_entry_function = (void*)command_generator;
rl_initialize();
rl_parse_and_bind("TAB: menu-complete");
while (1) {
char *line = readline("rl> ");
if (line == NULL) break;
printf("echo %s\n", line);
free(line);
}
return 0;
}
这个程序有效,因为它显示了完成选项,但实际上并没有完成它们。也就是说,它只会显示可用的选项
$ gcc -ggdb3 -ledit example.c && ./a.out
rl> a
he_love_math I_love_math she_loves_math they_love_math
rl> a
即使在我按 Tab 后,它也不会将“a”替换为“he_love_math”,它只是显示选项。
通过一个小的互联网搜索,我发现需要将 TAB 键绑定到菜单完成,但是正如您所见,两者都没有运行
rl_parse_and_bind("TAB: menu-complete");
不将“TAB:菜单完成”放在我的主目录中会有所帮助。
我怎样才能完成工作?如何让libedit 用补全建议替换当前单词?
【问题讨论】:
标签: macos autocomplete terminal readline