From 621be2ceabee709a81d897fb892dda81b063090c Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Thu, 17 Jan 2013 13:44:14 +0100 Subject: [PATCH] Hooks now takes a non-const context --- src/buffer.cc | 6 +++++- src/commands.cc | 7 ++----- src/hook_manager.cc | 2 +- src/hook_manager.hh | 4 ++-- src/window.cc | 6 ++++-- 5 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/buffer.cc b/src/buffer.cc index b0a48543..12597b69 100644 --- a/src/buffer.cc +++ b/src/buffer.cc @@ -49,7 +49,11 @@ Buffer::Buffer(String name, Flags flags, std::vector lines) Buffer::~Buffer() { - m_hooks.run_hook("BufClose", m_name, Context(Editor(*this))); + { + Editor hook_editor{*this}; + Context hook_context{hook_editor}; + m_hooks.run_hook("BufClose", m_name, hook_context); + } BufferManager::instance().unregister_buffer(*this); assert(m_change_listeners.empty()); diff --git a/src/commands.cc b/src/commands.cc index f2798e88..0b8e6b7a 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -344,12 +344,9 @@ void add_hook(const CommandParameters& params, Context& context) // copy so that the lambda gets a copy as well Regex regex(params[2].begin(), params[2].end()); String command = params[3]; - auto hook_func = [=](const String& param, const Context& context) { + auto hook_func = [=](const String& param, Context& context) { if (boost::regex_match(param.begin(), param.end(), regex)) - { - Context new_context(context.editor()); - CommandManager::instance().execute(command, new_context); - } + CommandManager::instance().execute(command, context); }; const String& scope = params[0]; diff --git a/src/hook_manager.cc b/src/hook_manager.cc index dff3506c..874bc9ca 100644 --- a/src/hook_manager.cc +++ b/src/hook_manager.cc @@ -10,7 +10,7 @@ void HookManager::add_hook(const String& hook_name, HookFunc hook) void HookManager::run_hook(const String& hook_name, const String& param, - const Context& context) const + Context& context) const { if (m_parent) m_parent->run_hook(hook_name, param, context); diff --git a/src/hook_manager.hh b/src/hook_manager.hh index a1da230f..cb8b60c1 100644 --- a/src/hook_manager.hh +++ b/src/hook_manager.hh @@ -9,7 +9,7 @@ namespace Kakoune { class Context; -typedef std::function HookFunc; +typedef std::function HookFunc; class HookManager { @@ -18,7 +18,7 @@ public: void add_hook(const String& hook_name, HookFunc hook); void run_hook(const String& hook_name, const String& param, - const Context& context) const; + Context& context) const; private: HookManager() diff --git a/src/window.cc b/src/window.cc index e2b51b30..c23989b8 100644 --- a/src/window.cc +++ b/src/window.cc @@ -18,7 +18,8 @@ Window::Window(Buffer& buffer) { HighlighterRegistry& registry = HighlighterRegistry::instance(); - m_hooks.run_hook("WinCreate", buffer.name(), Context(*this)); + Context hook_context{*this}; + m_hooks.run_hook("WinCreate", buffer.name(), hook_context); m_options.register_watcher(*this); m_highlighters.append(registry["expand_tabs"](*this, {})); @@ -185,7 +186,8 @@ void Window::on_incremental_insertion_end() void Window::on_option_changed(const String& name, const Option& option) { String desc = name + "=" + option.as_string(); - m_hooks.run_hook("WinSetOption", desc, Context(*this)); + Context hook_context{*this}; + m_hooks.run_hook("WinSetOption", desc, hook_context); } }