From 18aac3d4e80743e5410a4c8ae36c32ba2f7514ae Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 19 Feb 2013 19:04:09 +0100 Subject: Add '*' binding, which (smart) copy current selection to search pattern --- src/main.cc | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'src') diff --git a/src/main.cc b/src/main.cc index 118ab4c7..cf1253c3 100644 --- a/src/main.cc +++ b/src/main.cc @@ -189,6 +189,25 @@ void do_search_next(Context& context) context.print_status("no search pattern"); } +void use_selection_as_search_pattern(Context& context) +{ + std::vector patterns; + auto& sels = context.editor().selections(); + for (auto& sel : sels) + { + auto begin = sel.begin(); + auto end = sel.end(); + auto content = "\\Q" + context.buffer().string(begin, end) + "\\E"; + if (begin.is_begin() or (is_word(utf8::codepoint(begin)) and not is_word(utf8::codepoint(utf8::previous(begin))))) + content = "\\b" + content; + if (end.is_end() or (is_word(utf8::codepoint(utf8::previous(end))) and not is_word(utf8::codepoint(end)))) + content = content + "\\b"; + + patterns.push_back(std::move(content)); + } + RegisterManager::instance()['/'] = patterns; +} + void do_yank(Context& context) { RegisterManager::instance()['"'] = context.editor().selections_content(); @@ -620,6 +639,8 @@ std::unordered_map> keymap = { { Key::Modifiers::None, 'q' }, start_or_end_macro_recording }, { { Key::Modifiers::None, '@' }, replay_macro }, + + { { Key::Modifiers::None, '*' }, use_selection_as_search_pattern }, }; } -- cgit v1.2.3