diff --git a/src/input_handler.cc b/src/input_handler.cc index d2fb8275..095d85a2 100644 --- a/src/input_handler.cc +++ b/src/input_handler.cc @@ -1345,10 +1345,7 @@ public: selections.sort_and_merge_overlapping(); } else if (auto cp = key.codepoint()) - { - m_completer.try_accept(); insert(*cp); - } else if (key == ctrl('r')) { on_next_key_with_autoinfo(context(), "register", KeymapMode::None, @@ -1356,7 +1353,6 @@ public: auto cp = key.codepoint(); if (not cp or key == Key::Escape) return; - m_completer.try_accept(); insert(RegisterManager::instance()[*cp].get(context())); }, "enter register name", register_doc.str()); update_completions = false; @@ -1419,7 +1415,6 @@ public: [this, transient](Key key, Context&) { if (auto cp = get_raw_codepoint(key)) { - m_completer.try_accept(); insert(*cp); context().hooks().run_hook(Hook::InsertKey, key_to_str(key), context()); if (enabled() and not transient) @@ -1473,6 +1468,7 @@ private: void insert(ConstArrayView strings) { + m_completer.try_accept(); context().selections().for_each([strings, &buffer=context().buffer()] (size_t index, Selection& sel) { Kakoune::insert(buffer, sel, sel.cursor(), strings[std::min(strings.size()-1, index)]); @@ -1482,10 +1478,7 @@ private: void insert(Codepoint key) { String str{key}; - context().selections().for_each([&buffer=context().buffer(), &str] - (size_t index, Selection& sel) { - Kakoune::insert(buffer, sel, sel.cursor(), str); - }); + insert(str); context().hooks().run_hook(Hook::InsertChar, str, context()); } diff --git a/src/insert_completer.cc b/src/insert_completer.cc index d06df6f0..dd048733 100644 --- a/src/insert_completer.cc +++ b/src/insert_completer.cc @@ -474,38 +474,40 @@ void InsertCompleter::update(bool allow_implicit) auto& get_first(BufferRange& range) { return range.begin; } auto& get_last(BufferRange& range) { return range.end; } +bool InsertCompleter::has_candidate_selected() const +{ + return m_current_candidate >= 0 and m_current_candidate < m_completions.candidates.size() - 1; +} + void InsertCompleter::try_accept() { - if (m_completions.is_valid() and m_context.has_client() - and m_current_candidate >= 0 and m_current_candidate < m_completions.candidates.size() - 1) - { + if (m_completions.is_valid() and m_context.has_client() and has_candidate_selected()) reset(); - } } void InsertCompleter::reset() { - if (m_explicit_completer or m_completions.is_valid()) - { - String hook_param; - if (m_context.has_client() and m_current_candidate >= 0 and m_current_candidate < m_completions.candidates.size() - 1) - { - auto& buffer = m_context.buffer(); - update_ranges(buffer, m_completions.timestamp, m_inserted_ranges); - hook_param = join(m_inserted_ranges | filter([](auto&& r) { return not r.empty(); }) | transform([&](auto&& r) { - return selection_to_string(ColumnType::Byte, buffer, {r.begin, buffer.char_prev(r.end)}); - }), ' '); - } + if (not m_explicit_completer and not m_completions.is_valid()) + return; - m_explicit_completer = nullptr; - m_completions = InsertCompletion{}; - m_inserted_ranges.clear(); - if (m_context.has_client()) - { - m_context.client().menu_hide(); - m_context.client().info_hide(); - m_context.hooks().run_hook(Hook::InsertCompletionHide, hook_param, m_context); - } + String hook_param; + if (m_context.has_client() and has_candidate_selected()) + { + auto& buffer = m_context.buffer(); + update_ranges(buffer, m_completions.timestamp, m_inserted_ranges); + hook_param = join(m_inserted_ranges | filter([](auto&& r) { return not r.empty(); }) | transform([&](auto&& r) { + return selection_to_string(ColumnType::Byte, buffer, {r.begin, buffer.char_prev(r.end)}); + }), ' '); + } + + m_explicit_completer = nullptr; + m_completions = InsertCompletion{}; + m_inserted_ranges.clear(); + if (m_context.has_client()) + { + m_context.client().menu_hide(); + m_context.client().info_hide(); + m_context.hooks().run_hook(Hook::InsertCompletionHide, hook_param, m_context); } } diff --git a/src/insert_completer.hh b/src/insert_completer.hh index c8c266a9..269368d5 100644 --- a/src/insert_completer.hh +++ b/src/insert_completer.hh @@ -101,6 +101,7 @@ private: void on_option_changed(const Option& opt) override; void menu_show(); + bool has_candidate_selected() const; Context& m_context; OptionManager& m_options;