From 8135a44c6c3d9802e912153cb0e09f41f40d5af2 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sat, 16 Feb 2019 20:02:46 +1100 Subject: [PATCH] Run WinClose hook before putting the window into trash --- src/buffer_manager.cc | 17 ++--------------- src/client_manager.cc | 6 +++++- src/commands.cc | 1 + src/window.cc | 1 - src/window.hh | 1 + 5 files changed, 9 insertions(+), 17 deletions(-) diff --git a/src/buffer_manager.cc b/src/buffer_manager.cc index a26caa40..6b083c55 100644 --- a/src/buffer_manager.cc +++ b/src/buffer_manager.cc @@ -50,13 +50,13 @@ void BufferManager::delete_buffer(Buffer& buffer) auto it = find_if(m_buffers, [&](auto& p) { return p.get() == &buffer; }); kak_assert(it != m_buffers.end()); + buffer.on_unregistered(); + m_buffer_trash.emplace_back(std::move(*it)); m_buffers.erase(it); if (ClientManager::has_instance()) ClientManager::instance().ensure_no_client_uses_buffer(buffer); - - buffer.on_unregistered(); } Buffer* BufferManager::get_buffer_ifp(StringView name) @@ -99,19 +99,6 @@ void BufferManager::backup_modified_buffers() void BufferManager::clear_buffer_trash() { - for (auto& buffer : m_buffer_trash) - { - // Do that again, to be tolerant in some corner cases, where a buffer is - // deleted during its creation - if (ClientManager::has_instance()) - { - ClientManager::instance().ensure_no_client_uses_buffer(*buffer); - ClientManager::instance().clear_window_trash(); - } - - buffer.reset(); - } - m_buffer_trash.clear(); } diff --git a/src/client_manager.cc b/src/client_manager.cc index 18aa175f..d90526a7 100644 --- a/src/client_manager.cc +++ b/src/client_manager.cc @@ -158,7 +158,11 @@ void ClientManager::ensure_no_client_uses_buffer(Buffer& buffer) { return &ws.window->buffer() == &buffer; }); for (auto it = end; it != m_free_windows.end(); ++it) - m_window_trash.push_back(std::move(it->window)); + { + auto& win = it->window; + win->run_hook_in_own_context(Hook::WinClose, win->buffer().name()); + m_window_trash.push_back(std::move(win)); + } m_free_windows.erase(end, m_free_windows.end()); } diff --git a/src/commands.cc b/src/commands.cc index 3bc145bc..5db90b5c 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -275,6 +275,7 @@ void edit(const ParametersParser& parser, Context& context, const ShellContext&) if (buffer != current_buffer) context.change_buffer(*buffer); + buffer = &context.buffer(); // change_buffer hooks might change the buffer again if (parser.get_switch("fifo") and not parser.get_switch("scroll")) context.selections_write_only() = { *buffer, Selection{} }; diff --git a/src/window.cc b/src/window.cc index b4bc922d..1b145d66 100644 --- a/src/window.cc +++ b/src/window.cc @@ -39,7 +39,6 @@ Window::Window(Buffer& buffer) Window::~Window() { - run_hook_in_own_context(Hook::WinClose, buffer().name()); options().unregister_watcher(*this); } diff --git a/src/window.hh b/src/window.hh index cf7afebf..6d3b7393 100644 --- a/src/window.hh +++ b/src/window.hh @@ -54,6 +54,7 @@ private: void on_option_changed(const Option& option) override; DisplaySetup compute_display_setup(const Context& context) const; + friend class ClientManager; void run_hook_in_own_context(Hook hook, StringView param, String client_name = "");