Add alt-[kK] for keeping only selections matching/not matching a given regex

This commit is contained in:
Maxime Coste 2013-04-03 19:05:57 +02:00
parent 5bb8e656c5
commit d56f6444b5
2 changed files with 36 additions and 0 deletions

View File

@ -151,6 +151,10 @@ To clear multiple selections, use _space_. To keep only the nth selection
use _n_ followed by _space_, to remove only the nth selection, use _n_ use _n_ followed by _space_, to remove only the nth selection, use _n_
followed by _alt-space_. followed by _alt-space_.
_alt-k_ allows you to enter a regex and keep only the selections that
contains a match for this regex. using _alt-K_ you can keep the selections
not containing a match.
Object Selection Object Selection
---------------- ----------------

View File

@ -359,6 +359,30 @@ void do_join(Context& context)
editor.insert(" ", InsertMode::Replace); editor.insert(" ", InsertMode::Replace);
} }
template<bool matching>
void do_keep(Context& context)
{
constexpr const char* prompt = matching ? "keep matching: " : "keep not matching: ";
context.input_handler().prompt(prompt, complete_nothing,
[](const String& str, PromptEvent event, Context& context) {
if (event == PromptEvent::Validate)
{
Regex re(str);
Editor& editor = context.editor();
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));
}
});
}
void do_indent(Context& context) void do_indent(Context& context)
{ {
size_t width = context.options()["indentwidth"].get<int>(); size_t width = context.options()["indentwidth"].get<int>();
@ -689,6 +713,9 @@ std::unordered_map<Key, std::function<void (Context& context)>> keymap =
{ { Key::Modifiers::Alt, 'j' }, do_join }, { { Key::Modifiers::Alt, 'j' }, do_join },
{ { Key::Modifiers::Alt, 'k' }, do_keep<true> },
{ { Key::Modifiers::Alt, 'K' }, do_keep<false> },
{ { Key::Modifiers::None, '<' }, do_deindent }, { { Key::Modifiers::None, '<' }, do_deindent },
{ { Key::Modifiers::None, '>' }, do_indent }, { { Key::Modifiers::None, '>' }, do_indent },
@ -751,6 +778,11 @@ void register_env_vars()
shell_manager.register_env_var("cursor_column", shell_manager.register_env_var("cursor_column",
[](const String& name, const Context& context) [](const String& name, const Context& context)
{ return int_to_str((int)context.editor().main_selection().last().column() + 1); }); { return int_to_str((int)context.editor().main_selection().last().column() + 1); });
shell_manager.register_env_var("selection_desc",
[](const String& name, const Context& context)
{ auto& sel = context.editor().main_selection();
auto beg = sel.begin();
return int_to_str((int)beg.line() + 1) + ':' + int_to_str((int)beg.column() + 1) + '+' + int_to_str((int)(sel.end() - beg)); });
} }
void register_registers() void register_registers()