diff --git a/README.asciidoc b/README.asciidoc index 38dfd3d7..f45de173 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -180,6 +180,13 @@ selection. Registers are lists, instead of simply text in order to interact well with multiselection. Each selection have it's own captures, or yank buffer. +Search selection +---------------- + +Using the _*_ key, you can set the search pattern to the current selection. +This tries to be intelligent. It will for example detect if current selection +begins and/or end at word boundaries, and set the search pattern accordingly. + Basic Commands -------------- @@ -227,7 +234,7 @@ Kakoune support three string syntax: * +"strings" and \'strings\'+: classic strings, use \' or \" to escape the separator. - * +%\{strings\}+: these strings are very usefull when entering commands + * +%\{strings\}+: these strings are very useful when entering commands - the '{' and '}' delimiter are configurable: you can use any non alphanumeric character. like %[string], %, %(string), %~string~ 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 }, }; }