From c45838cc57aa4a30323b4db3b888466eaff38202 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Thu, 17 Jan 2013 13:58:57 +0100 Subject: [PATCH] Move Insertion Hooks handing to the input handler --- src/editor.cc | 3 --- src/editor.hh | 2 -- src/input_handler.cc | 27 ++++++++++++++++++++------- src/rc/cpp.kak | 2 +- src/window.cc | 7 ------- src/window.hh | 1 - 6 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/editor.cc b/src/editor.cc index e755f50fe..cd1b760f1 100644 --- a/src/editor.cc +++ b/src/editor.cc @@ -408,7 +408,6 @@ using utf8_it = utf8::utf8_iterator m_buffer; DynamicSelectionList m_selections; diff --git a/src/input_handler.cc b/src/input_handler.cc index 3f57279ef..eff655510 100644 --- a/src/input_handler.cc +++ b/src/input_handler.cc @@ -31,6 +31,8 @@ private: namespace InputModes { +static constexpr std::chrono::milliseconds idle_timeout{100}; + class Normal : public InputMode { public: @@ -513,12 +515,10 @@ private: class Insert : public InputMode { public: - static constexpr std::chrono::milliseconds idle_timeout(){ return std::chrono::milliseconds{100}; } - Insert(Context& context, InsertMode mode) : InputMode(context.input_handler()), m_inserter(context.editor(), mode), - m_idle_timer{Clock::time_point::max(), + m_idle_timer{Clock::now() + idle_timeout, [this, &context](Timer& timer) { context.hooks().run_hook("InsertIdle", "", context); m_completer.reset(context); @@ -532,7 +532,7 @@ public: { context.last_insert().first = mode; context.last_insert().second.clear(); - m_idle_timer.set_next_date(Clock::now() + idle_timeout()); + context.hooks().run_hook("InsertBegin", "", context); } void on_key(const Key& key, Context& context) override @@ -546,8 +546,10 @@ public: return; } bool reset_completer = true; + bool moved = false; if (key == Key::Escape or key == Key{ Key::Modifiers::Control, 'c' }) { + context.hooks().run_hook("InsertEnd", "", context); m_completer.reset(context); reset_normal_mode(); } @@ -556,11 +558,20 @@ public: else if (key == Key::Left) m_inserter.move_cursors(-1_char); else if (key == Key::Right) + { m_inserter.move_cursors(1_char); + moved = true; + } else if (key == Key::Up) + { m_inserter.move_cursors(-1_line); + moved = true; + } else if (key == Key::Down) + { m_inserter.move_cursors(1_line); + moved = true; + } else if (key.modifiers == Key::Modifiers::None) { m_inserter.insert(codepoint_to_str(key.key)); @@ -585,8 +596,10 @@ public: if (reset_completer) { // m_completer.reset(context); - m_idle_timer.set_next_date(Clock::now() + idle_timeout()); + m_idle_timer.set_next_date(Clock::now() + idle_timeout); } + if (moved) + context.hooks().run_hook("InsertMove", "", context); } private: bool m_insert_reg = false; @@ -639,7 +652,7 @@ void InputHandler::repeat_last_insert(Context& context) } void InputHandler::prompt(const String& prompt, Completer completer, - PromptCallback callback, Context& context) + PromptCallback callback, Context& context) { assert(&context.input_handler() == this); m_mode_trash.emplace_back(std::move(m_mode)); @@ -647,7 +660,7 @@ void InputHandler::prompt(const String& prompt, Completer completer, } void InputHandler::menu(const memoryview& choices, - MenuCallback callback, Context& context) + MenuCallback callback, Context& context) { assert(&context.input_handler() == this); m_mode_trash.emplace_back(std::move(m_mode)); diff --git a/src/rc/cpp.kak b/src/rc/cpp.kak index 730ca3739..2848b542e 100644 --- a/src/rc/cpp.kak +++ b/src/rc/cpp.kak @@ -22,7 +22,7 @@ hook global WinSetOption filetype=cpp %~ addfilter -group cpp-filters regex ^(\h+)([^\n]*[^([{]\h*|$) \n \n$1 addfilter -group cpp-filters regex ^(\h*)[^\n]*[([{]\h* \n '\n$1 ' addfilter -group cpp-filters cleanup_whitespaces - hook window InsertEnd .* %{ exec xs\h+(?=\n)d } + hook window InsertEnd .* %{ exec -restore-selections s\h+$d } ~ hook global WinSetOption filetype=(?!cpp).* %{ diff --git a/src/window.cc b/src/window.cc index c23989b8b..8d45d6fa9 100644 --- a/src/window.cc +++ b/src/window.cc @@ -176,13 +176,6 @@ String Window::status_line() const return oss.str(); } -void Window::on_incremental_insertion_end() -{ - DynamicSelectionList backup(buffer(), selections()); - hooks().run_hook("InsertEnd", "", Context(*this)); - select((SelectionList)backup); -} - void Window::on_option_changed(const String& name, const Option& option) { String desc = name + "=" + option.as_string(); diff --git a/src/window.hh b/src/window.hh index d49641116..7eb138604 100644 --- a/src/window.hh +++ b/src/window.hh @@ -53,7 +53,6 @@ public: private: Window(const Window&) = delete; - void on_incremental_insertion_end() override; void on_option_changed(const String& name, const Option& option) override; void scroll_to_keep_cursor_visible_ifn();