refactor regex prompting commands
This commit is contained in:
parent
9bb9eda302
commit
c53319ce5e
51
src/main.cc
51
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,12 +327,22 @@ 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) {
|
|
||||||
if (event == PromptEvent::Validate)
|
|
||||||
{
|
|
||||||
String ex = str;
|
String ex = str;
|
||||||
if (ex.empty())
|
if (ex.empty())
|
||||||
ex = RegisterManager::instance()['/'].values(context)[0];
|
ex = RegisterManager::instance()['/'].values(context)[0];
|
||||||
|
@ -347,12 +350,6 @@ void do_split_regex(Context& context)
|
||||||
RegisterManager::instance()['/'] = ex;
|
RegisterManager::instance()['/'] = ex;
|
||||||
if (not ex.empty())
|
if (not ex.empty())
|
||||||
context.editor().multi_select(std::bind(split_selection, _1, ex));
|
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"));
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -386,9 +383,8 @@ 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);
|
Regex re(str);
|
||||||
Editor& editor = context.editor();
|
Editor& editor = context.editor();
|
||||||
|
@ -403,10 +399,9 @@ void do_keep(Context& context)
|
||||||
throw runtime_error("no selections remaining");
|
throw runtime_error("no selections remaining");
|
||||||
editor.select(std::move(keep));
|
editor.select(std::move(keep));
|
||||||
}
|
}
|
||||||
else if (event == PromptEvent::Change)
|
catch (boost::regex_error& error)
|
||||||
{
|
{
|
||||||
const bool ok = Regex{str, boost::regex_constants::no_except}.status() == 0;
|
throw runtime_error("regex_error: "_str + error.what());
|
||||||
context.input_handler().set_prompt_colors(get_color(ok ? "Prompt" : "Error"));
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user