refactor regex prompting commands
This commit is contained in:
parent
9bb9eda302
commit
c53319ce5e
97
src/main.cc
97
src/main.cc
|
@ -312,20 +312,13 @@ void do_paste(Context& context)
|
||||||
editor.insert(strings, mode);
|
editor.insert(strings, mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
void do_select_regex(Context& context)
|
template<typename T>
|
||||||
|
void regex_prompt(Context& context, const String prompt, T on_validate)
|
||||||
{
|
{
|
||||||
context.input_handler().prompt("select: ", get_color("Prompt"), complete_nothing,
|
context.input_handler().prompt(prompt, get_color("Prompt"), complete_nothing,
|
||||||
[](const String& str, PromptEvent event, Context& context) {
|
[=](const String& str, PromptEvent event, Context& context) {
|
||||||
if (event == PromptEvent::Validate)
|
if (event == PromptEvent::Validate)
|
||||||
{
|
on_validate(str, context);
|
||||||
String ex = str;
|
|
||||||
if (ex.empty())
|
|
||||||
ex = RegisterManager::instance()['/'].values(context)[0];
|
|
||||||
else
|
|
||||||
RegisterManager::instance()['/'] = ex;
|
|
||||||
if (not ex.empty())
|
|
||||||
context.editor().multi_select(std::bind(select_all_matches, _1, ex));
|
|
||||||
}
|
|
||||||
else if (event == PromptEvent::Change)
|
else if (event == PromptEvent::Change)
|
||||||
{
|
{
|
||||||
const bool ok = Regex{str, boost::regex_constants::no_except}.status() == 0;
|
const bool ok = Regex{str, boost::regex_constants::no_except}.status() == 0;
|
||||||
|
@ -334,26 +327,30 @@ void do_select_regex(Context& context)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void do_select_regex(Context& context)
|
||||||
|
{
|
||||||
|
regex_prompt(context, "select: ", [](const String& str, Context& context) {
|
||||||
|
String ex = str;
|
||||||
|
if (ex.empty())
|
||||||
|
ex = RegisterManager::instance()['/'].values(context)[0];
|
||||||
|
else
|
||||||
|
RegisterManager::instance()['/'] = ex;
|
||||||
|
if (not ex.empty())
|
||||||
|
context.editor().multi_select(std::bind(select_all_matches, _1, ex));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
void do_split_regex(Context& context)
|
void do_split_regex(Context& context)
|
||||||
{
|
{
|
||||||
context.input_handler().prompt("split: ", get_color("Prompt"), complete_nothing,
|
regex_prompt(context, "split: ", [](const String& str, Context& context) {
|
||||||
[](const String& str, PromptEvent event, Context& context) {
|
String ex = str;
|
||||||
if (event == PromptEvent::Validate)
|
if (ex.empty())
|
||||||
{
|
ex = RegisterManager::instance()['/'].values(context)[0];
|
||||||
String ex = str;
|
else
|
||||||
if (ex.empty())
|
RegisterManager::instance()['/'] = ex;
|
||||||
ex = RegisterManager::instance()['/'].values(context)[0];
|
if (not ex.empty())
|
||||||
else
|
context.editor().multi_select(std::bind(split_selection, _1, ex));
|
||||||
RegisterManager::instance()['/'] = ex;
|
});
|
||||||
if (not ex.empty())
|
|
||||||
context.editor().multi_select(std::bind(split_selection, _1, ex));
|
|
||||||
}
|
|
||||||
else if (event == PromptEvent::Change)
|
|
||||||
{
|
|
||||||
const bool ok = Regex{str, boost::regex_constants::no_except}.status() == 0;
|
|
||||||
context.input_handler().set_prompt_colors(get_color(ok ? "Prompt" : "Error"));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void do_split_lines(Context& context)
|
void do_split_lines(Context& context)
|
||||||
|
@ -386,29 +383,27 @@ template<bool matching>
|
||||||
void do_keep(Context& context)
|
void do_keep(Context& context)
|
||||||
{
|
{
|
||||||
constexpr const char* prompt = matching ? "keep matching: " : "keep not matching: ";
|
constexpr const char* prompt = matching ? "keep matching: " : "keep not matching: ";
|
||||||
context.input_handler().prompt(prompt, get_color("Prompt"), complete_nothing,
|
regex_prompt(context, prompt, [](const String& str, Context& context) {
|
||||||
[](const String& str, PromptEvent event, Context& context) {
|
try
|
||||||
if (event == PromptEvent::Validate)
|
{
|
||||||
|
Regex re(str);
|
||||||
|
Editor& editor = context.editor();
|
||||||
|
SelectionList sels = editor.selections();
|
||||||
|
SelectionList keep;
|
||||||
|
for (auto& sel : sels)
|
||||||
{
|
{
|
||||||
Regex re(str);
|
if (boost::regex_search(sel.begin(), sel.end(), re) == matching)
|
||||||
Editor& editor = context.editor();
|
keep.push_back(sel);
|
||||||
SelectionList sels = editor.selections();
|
|
||||||
SelectionList keep;
|
|
||||||
for (auto& sel : sels)
|
|
||||||
{
|
|
||||||
if (boost::regex_search(sel.begin(), sel.end(), re) == matching)
|
|
||||||
keep.push_back(sel);
|
|
||||||
}
|
|
||||||
if (keep.empty())
|
|
||||||
throw runtime_error("no selections remaining");
|
|
||||||
editor.select(std::move(keep));
|
|
||||||
}
|
}
|
||||||
else if (event == PromptEvent::Change)
|
if (keep.empty())
|
||||||
{
|
throw runtime_error("no selections remaining");
|
||||||
const bool ok = Regex{str, boost::regex_constants::no_except}.status() == 0;
|
editor.select(std::move(keep));
|
||||||
context.input_handler().set_prompt_colors(get_color(ok ? "Prompt" : "Error"));
|
}
|
||||||
}
|
catch (boost::regex_error& error)
|
||||||
});
|
{
|
||||||
|
throw runtime_error("regex_error: "_str + error.what());
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void do_indent(Context& context)
|
void do_indent(Context& context)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user