#include "llvm/LineEditor/LineEditor.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Config/config.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <cassert>
#include <cstdio>
#ifdef HAVE_LIBEDIT
#include <histedit.h>
#endif
usingnamespacellvm;
std::string LineEditor::getDefaultHistoryPath(StringRef ProgName) { … }
LineEditor::CompleterConcept::~CompleterConcept() = default;
LineEditor::ListCompleterConcept::~ListCompleterConcept() = default;
std::string LineEditor::ListCompleterConcept::getCommonPrefix(
const std::vector<Completion> &Comps) { … }
LineEditor::CompletionAction
LineEditor::ListCompleterConcept::complete(StringRef Buffer, size_t Pos) const { … }
LineEditor::CompletionAction LineEditor::getCompletionAction(StringRef Buffer,
size_t Pos) const { … }
#ifdef HAVE_LIBEDIT
struct LineEditor::InternalData {
LineEditor *LE;
History *Hist;
EditLine *EL;
unsigned PrevCount;
std::string ContinuationOutput;
FILE *Out;
};
namespace {
const char *ElGetPromptFn(EditLine *EL) {
LineEditor::InternalData *Data;
if (el_get(EL, EL_CLIENTDATA, &Data) == 0)
return Data->LE->getPrompt().c_str();
return "> ";
}
unsigned char ElCompletionFn(EditLine *EL, int ch) {
LineEditor::InternalData *Data;
if (el_get(EL, EL_CLIENTDATA, &Data) == 0) {
if (!Data->ContinuationOutput.empty()) {
FILE *Out = Data->Out;
::fwrite(Data->ContinuationOutput.c_str(),
Data->ContinuationOutput.size(), 1, Out);
std::string Prevs(Data->PrevCount, '\02');
::el_push(EL, const_cast<char *>(Prevs.c_str()));
Data->ContinuationOutput.clear();
return CC_REFRESH;
}
const LineInfo *LI = ::el_line(EL);
LineEditor::CompletionAction Action = Data->LE->getCompletionAction(
StringRef(LI->buffer, LI->lastchar - LI->buffer),
LI->cursor - LI->buffer);
switch (Action.Kind) {
case LineEditor::CompletionAction::AK_Insert:
::el_insertstr(EL, Action.Text.c_str());
return CC_REFRESH;
case LineEditor::CompletionAction::AK_ShowCompletions:
if (Action.Completions.empty()) {
return CC_REFRESH_BEEP;
} else {
::el_push(EL, const_cast<char *>("\05\t"));
raw_string_ostream OS(Data->ContinuationOutput);
OS << "\n";
for (const std::string &Completion : Action.Completions)
OS << Completion << "\n";
OS << Data->LE->getPrompt()
<< StringRef(LI->buffer, LI->lastchar - LI->buffer);
Data->PrevCount = LI->lastchar - LI->cursor;
return CC_REFRESH;
}
}
}
return CC_ERROR;
}
}
LineEditor::LineEditor(StringRef ProgName, StringRef HistoryPath, FILE *In,
FILE *Out, FILE *Err)
: Prompt((ProgName + "> ").str()), HistoryPath(std::string(HistoryPath)),
Data(new InternalData) {
if (HistoryPath.empty())
this->HistoryPath = getDefaultHistoryPath(ProgName);
Data->LE = this;
Data->Out = Out;
Data->Hist = ::history_init();
assert(Data->Hist);
Data->EL = ::el_init(ProgName.str().c_str(), In, Out, Err);
assert(Data->EL);
::el_set(Data->EL, EL_PROMPT, ElGetPromptFn);
::el_set(Data->EL, EL_EDITOR, "emacs");
::el_set(Data->EL, EL_HIST, history, Data->Hist);
::el_set(Data->EL, EL_ADDFN, "tab_complete", "Tab completion function",
ElCompletionFn);
::el_set(Data->EL, EL_BIND, "\t", "tab_complete", NULL);
::el_set(Data->EL, EL_BIND, "^r", "em-inc-search-prev",
NULL);
::el_set(Data->EL, EL_BIND, "^w", "ed-delete-prev-word",
NULL);
::el_set(Data->EL, EL_BIND, "\033[3~", "ed-delete-next-char",
NULL);
::el_set(Data->EL, EL_CLIENTDATA, Data.get());
HistEvent HE;
::history(Data->Hist, &HE, H_SETSIZE, 800);
::history(Data->Hist, &HE, H_SETUNIQUE, 1);
loadHistory();
}
LineEditor::~LineEditor() {
saveHistory();
::history_end(Data->Hist);
::el_end(Data->EL);
::fwrite("\n", 1, 1, Data->Out);
}
void LineEditor::saveHistory() {
if (!HistoryPath.empty()) {
HistEvent HE;
::history(Data->Hist, &HE, H_SAVE, HistoryPath.c_str());
}
}
void LineEditor::loadHistory() {
if (!HistoryPath.empty()) {
HistEvent HE;
::history(Data->Hist, &HE, H_LOAD, HistoryPath.c_str());
}
}
std::optional<std::string> LineEditor::readLine() const {
int LineLen = 0;
const char *Line = ::el_gets(Data->EL, &LineLen);
if (!Line || LineLen == 0)
return std::nullopt;
while (LineLen > 0 &&
(Line[LineLen - 1] == '\n' || Line[LineLen - 1] == '\r'))
--LineLen;
HistEvent HE;
if (LineLen > 0)
::history(Data->Hist, &HE, H_ENTER, Line);
return std::string(Line, LineLen);
}
#else
struct LineEditor::InternalData { … };
LineEditor::LineEditor(StringRef ProgName, StringRef HistoryPath, FILE *In,
FILE *Out, FILE *Err)
: … { … }
LineEditor::~LineEditor() { … }
void LineEditor::saveHistory() { … }
void LineEditor::loadHistory() { … }
std::optional<std::string> LineEditor::readLine() const { … }
#endif