diff --git a/src/buffer.cc b/src/buffer.cc index 13a0a131..f619edb1 100644 --- a/src/buffer.cc +++ b/src/buffer.cc @@ -33,21 +33,23 @@ Buffer::Buffer(String name, Type type, if (not initial_content.empty()) apply_modification(Modification::make_insert(begin(), std::move(initial_content))); + Editor editor_for_hooks(*this); + Context context(editor_for_hooks); if (type == Type::NewFile) - m_hook_manager.run_hook("BufNew", m_name, Context(*this)); + m_hook_manager.run_hook("BufNew", m_name, context); else if (type == Type::File) - m_hook_manager.run_hook("BufOpen", m_name, Context(*this)); + m_hook_manager.run_hook("BufOpen", m_name, context); - m_hook_manager.run_hook("BufCreate", m_name, Context(*this)); + m_hook_manager.run_hook("BufCreate", m_name, context); } Buffer::~Buffer() { + m_hook_manager.run_hook("BufClose", m_name, Context(Editor(*this))); + m_windows.clear(); BufferManager::instance().unregister_buffer(this); assert(m_change_listeners.empty()); - - m_hook_manager.run_hook("BufClose", m_name, Context(*this)); } BufferIterator Buffer::iterator_at(const BufferCoord& line_and_column) const diff --git a/src/context.hh b/src/context.hh index b868540d..8f4bc785 100644 --- a/src/context.hh +++ b/src/context.hh @@ -13,8 +13,10 @@ struct Context Context() {} Context(Editor& editor) : m_editor(&editor), m_buffer(&editor.buffer()) {} - Context(Buffer& buffer) - : m_buffer(&buffer) {} + + // to allow func(Context(Editor(...))) + Context(Editor&& editor) + : m_editor(&editor), m_buffer(&editor.buffer()) {} Buffer& buffer() const {