Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kaiha/pr/readline #3196

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 33 additions & 1 deletion src/nix/repl.cc
Expand Up @@ -137,6 +137,30 @@ NixRepl::~NixRepl()

static NixRepl * curRepl; // ugly

#ifdef READLINE
static char * completionFuncReadline(const char * text, int state)
{
static StringSet matches = curRepl->completePrefix(text);

if (state == 0) {
matches = curRepl->completePrefix(text);
}

auto it = matches.begin();
while (state > 0 && it != matches.end()) {
it++;
state--;
}

if (it == matches.end()) {
return nullptr;
}
else {
return strdup(it->c_str());
}
}

#else
static char * completionCallback(char * s, int *match) {
auto possible = curRepl->completePrefix(s);
if (possible.size() == 1) {
Expand Down Expand Up @@ -198,6 +222,7 @@ static int listPossibleCallback(char *s, char ***avp) {

return ac;
}
#endif

namespace {
// Used to communicate to NixRepl::getLine whether a signal occurred in ::readline.
Expand Down Expand Up @@ -227,7 +252,9 @@ void NixRepl::mainLoop(const std::vector<std::string> & files)
#endif
read_history(historyFile.c_str());
curRepl = this;
#ifndef READLINE
#ifdef READLINE
rl_completion_entry_function = completionFuncReadline;
#else
rl_set_complete_func(completionCallback);
rl_set_list_possib_func(listPossibleCallback);
#endif
Expand Down Expand Up @@ -302,6 +329,11 @@ bool NixRepl::getLine(string & input, const std::string &prompt)

if (!s)
return false;
#ifdef READLINE
if (*s) {
add_history(s);
}
#endif
input += s;
input += '\n';
return true;
Expand Down