From a2fd1528e6083e08848d0d4bfd6ca1ccb2fa855c Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 26 Sep 2012 20:07:06 +0200 Subject: [PATCH] Move last insertion from Client to Context --- src/client.cc | 32 +++++++++++++++++--------------- src/client.hh | 3 +-- src/context.hh | 9 ++++++--- src/main.cc | 2 +- 4 files changed, 25 insertions(+), 21 deletions(-) diff --git a/src/client.cc b/src/client.cc index 97d72412..829bfd3f 100644 --- a/src/client.cc +++ b/src/client.cc @@ -22,7 +22,6 @@ public: virtual void on_key(const Key& key, Context& context) = 0; protected: void reset_normal_mode(); - std::pair>& last_insert() { return m_client.m_last_insert; } private: Client& m_client; }; @@ -47,7 +46,7 @@ public: auto it = keymap.find(key); if (it != keymap.end()) { - context.numeric_param(m_count); + context.numeric_param() = m_count; // it's important to do that before calling the command, // as we may die during the command execution. m_count = 0; @@ -318,16 +317,17 @@ private: class Insert : public ClientMode { public: - Insert(Client& client, Editor& editor, InsertMode mode) - : ClientMode(client), m_inserter(editor, mode) + Insert(Context& context, InsertMode mode) + : ClientMode(context.client()), + m_inserter(context.editor(), mode) { - last_insert().first = mode; - last_insert().second.clear(); + context.last_insert().first = mode; + context.last_insert().second.clear(); } void on_key(const Key& key, Context& context) override { - last_insert().second.push_back(key); + context.last_insert().second.push_back(key); if (m_insert_reg) { if (key.modifiers == Key::Modifiers::None) @@ -392,8 +392,7 @@ void ClientMode::reset_normal_mode() Client::Client() - : m_mode(new ClientModes::Normal(*this)), - m_last_insert(InsertMode::Insert, {}) + : m_mode(new ClientModes::Normal(*this)) { } @@ -401,21 +400,24 @@ Client::~Client() { } -void Client::insert(Editor& editor, InsertMode mode) +void Client::insert(Context& context, InsertMode mode) { - m_mode.reset(new ClientModes::Insert(*this, editor, mode)); + assert(&context.client() == this); + m_mode.reset(new ClientModes::Insert(context, mode)); } void Client::repeat_last_insert(Context& context) { - if (m_last_insert.second.empty()) + assert(&context.client() == this); + Context::Insertion& last_insert = context.last_insert(); + if (last_insert.second.empty()) return; std::vector keys; - swap(keys, m_last_insert.second); - // m_last_insert will be refilled by the new InsertMode + swap(keys, last_insert.second); + // context.last_insert will be refilled by the new Insert // this is very inefficient. - m_mode.reset(new ClientModes::Insert(*this, context.editor(), m_last_insert.first)); + m_mode.reset(new ClientModes::Insert(context, last_insert.first)); for (auto& key : keys) m_mode->on_key(key, context); assert(dynamic_cast(m_mode.get()) != nullptr); diff --git a/src/client.hh b/src/client.hh index d0a64bb3..9601feb5 100644 --- a/src/client.hh +++ b/src/client.hh @@ -25,7 +25,7 @@ public: Client(); ~Client(); - void insert(Editor& editor, InsertMode mode); + void insert(Context& context, InsertMode mode); void repeat_last_insert(Context& context); void prompt(const String& prompt, Completer completer, @@ -41,7 +41,6 @@ public: private: friend class ClientMode; std::unique_ptr m_mode; - std::pair> m_last_insert; }; struct prompt_aborted {}; diff --git a/src/context.hh b/src/context.hh index 801cb289..05acef73 100644 --- a/src/context.hh +++ b/src/context.hh @@ -97,13 +97,16 @@ struct Context ui().print_status(status); } - int numeric_param() const { return m_numeric_param; } - void numeric_param(int param) { m_numeric_param = param; } + using Insertion = std::pair>; + Insertion& last_insert() { return m_last_insert; } -public: + int& numeric_param() { return m_numeric_param; } +private: safe_ptr m_editor; safe_ptr m_client; safe_ptr m_ui; + + Insertion m_last_insert = {InsertMode::Insert, {}}; int m_numeric_param = 0; }; diff --git a/src/main.cc b/src/main.cc index 17f37a7a..8fb35c09 100644 --- a/src/main.cc +++ b/src/main.cc @@ -35,7 +35,7 @@ bool quit_requested = false; template void do_insert(Context& context) { - context.client().insert(context.editor(), mode); + context.client().insert(context, mode); } void do_repeat_insert(Context& context)