summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMaxime Coste <frrrwww@gmail.com>2013-04-03 19:05:57 +0200
committerMaxime Coste <frrrwww@gmail.com>2013-04-03 19:07:43 +0200
commitd56f6444b5ff1e16b4f84d5052e02346d68684ca (patch)
treeca99ac3f0216fcfc3758040e816bcf1d4060f9d6 /src
parent5bb8e656c57999e39495ade40de5c2eaf49c4d69 (diff)
Add alt-[kK] for keeping only selections matching/not matching a given regex
Diffstat (limited to 'src')
-rw-r--r--src/main.cc32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/main.cc b/src/main.cc
index a20a633b..e4219515 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -359,6 +359,30 @@ void do_join(Context& context)
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)
{
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, 'k' }, do_keep<true> },
+ { { Key::Modifiers::Alt, 'K' }, do_keep<false> },
+
{ { Key::Modifiers::None, '<' }, do_deindent },
{ { Key::Modifiers::None, '>' }, do_indent },
@@ -751,6 +778,11 @@ void register_env_vars()
shell_manager.register_env_var("cursor_column",
[](const String& name, const Context& context)
{ 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()