gnu读线制表符完成和制表符插入 我用GNU读写了一些C++写的,我在写文章是为了好玩。(如果您愿意的话,也可以给我反馈意见。:)
如果按TAB键,我希望实现以下目标:gnu读线制表符完成和制表符插入 我用GNU读写了一些C++写的,我在写文章是为了好玩。(如果您愿意的话,也可以给我反馈意见。:),c++,c,readline,C++,C,Readline,如果按TAB键,我希望实现以下目标: 如果没有可用的完成项,请插入选项卡 如果可以完成,请插入完成字符串 使用rl\u bind\u键('\t',rl\u insert)我可以禁用文件名完成,而是插入选项卡 使用rl\u尝试的完成功能我能够获得自定义完成 但是如果同时使用这两个键,rl_bind_key优先,我得到了tab insert,但没有完成,有没有办法让这两个键同时工作 以下是我的最小可复制示例: #include <iostream> #include <memo
- 如果没有可用的完成项,请插入选项卡
- 如果可以完成,请插入完成字符串
rl\u bind\u键('\t',rl\u insert)
我可以禁用文件名完成,而是插入选项卡
使用rl\u尝试的完成功能
我能够获得自定义完成
但是如果同时使用这两个键,rl_bind_key
优先,我得到了tab insert,但没有完成,有没有办法让这两个键同时工作
以下是我的最小可复制示例:
#include <iostream>
#include <memory>
#include <vector>
#include <readline/readline.h>
char *completion_generator(const char *text, int state)
{
static std::vector<std::string> matches;
static size_t match_index = 0;
if (state == 0)
{
// During initialization, compute the actual matches for 'text' and keep
// them in a static vector.
matches.clear();
match_index = 0;
static std::vector<std::string> words { "aaa", "bbb", "ccc" };
// Collect a vector of matches: vocabulary words that begin with text.
std::string textstr(text);
for (auto word : words)
{
if (word.size() >= textstr.size() && word.compare(0, textstr.size(), textstr) == 0)
{
matches.push_back(word);
}
}
}
if (match_index >= matches.size())
{
// We return nullptr to notify the caller no more matches are available.
return nullptr;
}
else
{
// Return a malloc'd char* for the match. The caller frees it.
return strdup(matches[match_index++].c_str());
}
}
char **completer(const char *text, int, int)
{
::rl_attempted_completion_over = 1;
return ::rl_completion_matches(text, completion_generator);
}
template<typename T>
struct MallocDeleter
{
void operator()(T *p) { free(p); }
};
int main()
{
// this takes precedence
::rl_bind_key('\t', rl_insert);
// this gets ignored if line above is there
::rl_attempted_completion_function = completer;
auto buf = std::unique_ptr<char, MallocDeleter<char>>{ ::readline(">>>") };
return 0;
}
#包括
#包括
#包括
#包括
字符*完成_生成器(常量字符*文本,整数状态)
{
静态std::向量匹配;
静态大小匹配索引=0;
如果(状态==0)
{
//在初始化过程中,计算“text”的实际匹配项并保留
//它们在一个静态向量中。
匹配。清除();
匹配指数=0;
静态std::向量词{“aaa”、“bbb”、“ccc”};
//收集匹配向量:以文本开头的词汇。
std::string textstr(文本);
for(自动字:字)
{
if(word.size()>=textstr.size()&&word.compare(0,textstr.size(),textstr)==0)
{
匹配。推回(word);
}
}
}
if(match_index>=matches.size())
{
//我们返回nullptr以通知调用方不再有匹配项可用。
返回空ptr;
}
其他的
{
//为匹配返回malloc'd char*。调用者释放它。
返回strdup(匹配[match_index++].c_str());
}
}
字符**完成符(常量字符*文本,整数,整数)
{
::rl_尝试_完成_超过=1;
return::rl_completion_匹配(文本,completion_生成器);
}
模板
结构MallocDeleter
{
void运算符()(T*p){free(p);}
};
int main()
{
//这是优先的
::rl_bind_key('\t',rl_insert);
//如果上面有一行,则忽略此项
::rl_尝试的_完成函数=完成器;
自动buf=std::unique_ptr{::readline(“>>>”)};
返回0;
}