Add a ClientCreate and ClientClose hook

As discussed in #2830.
Closes #2500.
This commit is contained in:
Maxime Coste 2019-04-08 21:58:12 +10:00
parent 08f1a471fd
commit c8839e7904
3 changed files with 19 additions and 2 deletions

View File

@ -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

View File

@ -64,7 +64,9 @@ Client* ClientManager::create_client(std::unique_ptr<UserInterface>&& 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();
}

View File

@ -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<Hook>)
{
return make_array<EnumDesc<Hook>, 39>({
return make_array<EnumDesc<Hook>, 41>({
{Hook::BufCreate, "BufCreate"},
{Hook::BufNewFile, "BufNewFile"},
{Hook::BufOpenFile, "BufOpenFile"},
@ -72,6 +74,8 @@ constexpr auto enum_desc(Meta::Type<Hook>)
{Hook::BufCloseFifo, "BufCloseFifo"},
{Hook::BufReadFifo, "BufReadFifo"},
{Hook::BufSetOption, "BufSetOption"},
{Hook::ClientCreate, "ClientCreate"},
{Hook::ClientClose, "ClientClose"},
{Hook::InsertBegin, "InsertBegin"},
{Hook::InsertChar, "InsertChar"},
{Hook::InsertDelete, "InsertDelete"},