From c8839e79041ef37849a4cb8ac5017eadc8ec9653 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 8 Apr 2019 21:58:12 +1000 Subject: [PATCH] Add a ClientCreate and ClientClose hook As discussed in #2830. Closes #2500. --- doc/pages/hooks.asciidoc | 7 +++++++ src/client_manager.cc | 8 +++++++- src/hook_manager.hh | 6 +++++- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/doc/pages/hooks.asciidoc b/doc/pages/hooks.asciidoc index 0fe6a566fa..a0f00bf4d8 100644 --- a/doc/pages/hooks.asciidoc +++ b/doc/pages/hooks.asciidoc @@ -140,6 +140,13 @@ name. Hooks with no description will always use an empty string. executed when a fifo buffer closes its fifo file descriptor either because the buffer is being deleted or the writing end has been closed +*ClientCreate* `client name`:: + executed when a new client is created. + +*ClientClose* `client name`:: + executed when a client is closed, after it was removed from the client + list. + *RuntimeError* `error message`:: an error was encountered while executing a user command diff --git a/src/client_manager.cc b/src/client_manager.cc index 943d7963e1..9ca5f55556 100644 --- a/src/client_manager.cc +++ b/src/client_manager.cc @@ -64,7 +64,9 @@ Client* ClientManager::create_client(std::unique_ptr&& ui, int pi try { - CommandManager::instance().execute(init_cmds, client->context()); + auto& context = client->context(); + context.hooks().run_hook(Hook::ClientCreate, context.name(), context); + CommandManager::instance().execute(init_cmds, context); } catch (Kakoune::runtime_error& error) { @@ -116,10 +118,14 @@ void ClientManager::remove_client(Client& client, bool graceful, int status) kak_assert(contains(m_client_trash, &client)); return; } + client.exit(status); m_client_trash.push_back(std::move(*it)); m_clients.erase(it); + auto& context = client.context(); + context.hooks().run_hook(Hook::ClientClose, context.name(), context); + if (not graceful and m_clients.empty()) BufferManager::instance().backup_modified_buffers(); } diff --git a/src/hook_manager.hh b/src/hook_manager.hh index fa72cdc3f0..1653791c7e 100644 --- a/src/hook_manager.hh +++ b/src/hook_manager.hh @@ -28,6 +28,8 @@ enum class Hook BufCloseFifo, BufReadFifo, BufSetOption, + ClientCreate, + ClientClose, InsertBegin, InsertChar, InsertDelete, @@ -60,7 +62,7 @@ enum class Hook constexpr auto enum_desc(Meta::Type) { - return make_array, 39>({ + return make_array, 41>({ {Hook::BufCreate, "BufCreate"}, {Hook::BufNewFile, "BufNewFile"}, {Hook::BufOpenFile, "BufOpenFile"}, @@ -72,6 +74,8 @@ constexpr auto enum_desc(Meta::Type) {Hook::BufCloseFifo, "BufCloseFifo"}, {Hook::BufReadFifo, "BufReadFifo"}, {Hook::BufSetOption, "BufSetOption"}, + {Hook::ClientCreate, "ClientCreate"}, + {Hook::ClientClose, "ClientClose"}, {Hook::InsertBegin, "InsertBegin"}, {Hook::InsertChar, "InsertChar"}, {Hook::InsertDelete, "InsertDelete"},