From c05f9f31dda0e4cac5ac9a318aaf0eabed6734e8 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sun, 4 Aug 2013 19:34:08 +0200 Subject: [PATCH] add support for explicit insert mode completion using C-x prefix --- README.asciidoc | 12 ++++++++++++ src/input_handler.cc | 32 +++++++++++++++++++++----------- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/README.asciidoc b/README.asciidoc index 6d0dfc14..3bccd879 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -380,6 +380,18 @@ Some options are built in kakoune, and can be used to control it's behaviour: * +insert_hide_sel+ _bool_: hide the selection (except the last element) in insert mode +Insert mode completion +---------------------- + +Kakoune can propose completions while inserting text, the +completers+ option +control automatic completion, which kicks in when a certain idle timeout is +reached (100 milliseconds). Insert mode completion can be explicitely triggered +using *control-x*, followed, by: + + * *f* : filename completion + * *w* : buffer word completion + * *o* : option based completion + Highlighters ------------ diff --git a/src/input_handler.cc b/src/input_handler.cc index bf0cd368..a2ed3cae 100644 --- a/src/input_handler.cc +++ b/src/input_handler.cc @@ -689,7 +689,7 @@ public: return { begin.coord(), pos.coord(), std::move(res), buffer.timestamp() }; } - BufferCompletion complete_opt(const Buffer& buffer, BufferCoord cursor_pos) + BufferCompletion complete_option(const Buffer& buffer, BufferCoord cursor_pos) { const StringList& opt = options()["completions"].get(); if (opt.empty()) @@ -748,7 +748,7 @@ private: if (not m_completions.is_valid()) { auto& completers = options()["completers"].get(); - if (contains(completers, "option") and try_complete<&BufferCompleter::complete_opt>()) + if (contains(completers, "option") and try_complete<&BufferCompleter::complete_option>()) return true; if (contains(completers, "word=buffer") and try_complete<&BufferCompleter::complete_word>()) return true; @@ -792,13 +792,25 @@ public: return reset_normal_mode().on_key(key); last_insert().second.push_back(key); - if (m_insert_reg) + if (m_mode == Mode::InsertReg) { if (key.modifiers == Key::Modifiers::None) m_inserter.insert(RegisterManager::instance()[key.key].values(context())); - m_insert_reg = false; + m_mode = Mode::Default; return; } + if (m_mode == Mode::Complete) + { + if (key.key == 'f') + m_completer.try_complete<&BufferCompleter::complete_filename>(); + if (key.key == 'w') + m_completer.try_complete<&BufferCompleter::complete_word>(); + if (key.key == 'o') + m_completer.try_complete<&BufferCompleter::complete_option>(); + m_mode = Mode::Default; + return; + } + bool update_completions = true; bool moved = false; if (key == Key::Escape or key == Key{ Key::Modifiers::Control, 'c' }) @@ -835,7 +847,7 @@ public: context().hooks().run_hook("InsertKey", key_to_str(key), context()); } else if (key == Key{ Key::Modifiers::Control, 'r' }) - m_insert_reg = true; + m_mode = Mode::InsertReg; else if ( key == Key{ Key::Modifiers::Control, 'm' }) m_inserter.insert(String() + '\n'); else if ( key == Key{ Key::Modifiers::Control, 'i' }) @@ -850,11 +862,8 @@ public: m_completer.select(-1); update_completions = false; } - else if ( key == Key{ Key::Modifiers::Control, 'f' }) - { - m_completer.try_complete<&BufferCompleter::complete_filename>(); - update_completions = false; - } + else if ( key == Key{ Key::Modifiers::Control, 'x' }) + m_mode = Mode::Complete; if (update_completions) m_idle_timer.set_next_date(Clock::now() + idle_timeout); @@ -862,7 +871,8 @@ public: context().hooks().run_hook("InsertMove", key_to_str(key), context()); } private: - bool m_insert_reg = false; + enum class Mode { Default, Complete, InsertReg }; + Mode m_mode = Mode::Default; IncrementalInserter m_inserter; BufferCompleter m_completer; Timer m_idle_timer;