Skip to content

Commit

Permalink
Add a ClientCreate and ClientClose hook
Browse files Browse the repository at this point in the history
As discussed in #2830.
Closes #2500.
  • Loading branch information
mawww committed Apr 8, 2019
1 parent 08f1a47 commit c8839e7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
7 changes: 7 additions & 0 deletions doc/pages/hooks.asciidoc
Expand Up @@ -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

Expand Down
8 changes: 7 additions & 1 deletion src/client_manager.cc
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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();
}
Expand Down
6 changes: 5 additions & 1 deletion src/hook_manager.hh
Expand Up @@ -28,6 +28,8 @@ enum class Hook
BufCloseFifo,
BufReadFifo,
BufSetOption,
ClientCreate,
ClientClose,
InsertBegin,
InsertChar,
InsertDelete,
Expand Down Expand Up @@ -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"},
Expand All @@ -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"},
Expand Down

0 comments on commit c8839e7

Please sign in to comment.