From 78d7ab73bda5fc675641fbcca45961bfd5299041 Mon Sep 17 00:00:00 2001 From: q66 Date: Fri, 2 Sep 2016 22:15:05 +0100 Subject: [PATCH] add tab completion for readline --- tools/edit_readline.hh | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/tools/edit_readline.hh b/tools/edit_readline.hh index 97720a3d..dba1e333 100644 --- a/tools/edit_readline.hh +++ b/tools/edit_readline.hh @@ -3,13 +3,52 @@ #define CS_REPL_HAS_EDIT /* use the GNU readline library */ +#include + #include #include #include #include +#ifdef CS_REPL_HAS_COMPLETE +static char *ln_complete_list(char const *buf, int state) { + static ostd::ConstCharRange cmd; + static ostd::PointerRange itr; + + if (!state) { + cmd = get_complete_cmd(buf); + itr = gcs->identmap.iter(); + } + + for (; !itr.empty(); itr.pop_front()) { + CsIdent *id = itr.front(); + if (!id->is_command()) { + continue; + } + ostd::ConstCharRange idname = id->get_name(); + if (idname.size() <= cmd.size()) { + continue; + } + if (idname.slice(0, cmd.size()) == cmd) { + itr.pop_front(); + return strdup(idname.data()); + } + } + + return nullptr; +} + +static char **ln_complete(char const *buf, int, int) { + rl_attempted_completion_over = 1; + return rl_completion_matches(buf, ln_complete_list); +} +#endif + static void init_lineedit(ostd::ConstCharRange) { +#ifdef CS_REPL_HAS_COMPLETE + rl_attempted_completion_function = ln_complete; +#endif } static ostd::Maybe read_line(CsSvar *pr) {