Merge remote-tracking branch 'topisani/session-client-rename-hooks'

This commit is contained in:
Maxime Coste 2024-03-31 20:55:21 +11:00
commit 7d0f25a99c
5 changed files with 19 additions and 2 deletions

View File

@ -140,6 +140,12 @@ name. Hooks with no description will always use an empty string.
*ClientClose* `client name`::
executed when a client is closed, after it was removed from the client
list.
*ClientRenamed* `<old name>:<new name>`::
executed when a client is renamed using the `rename-client` command
*SessionRenamed* `<old name>:<new name>`::
executed when a session is renamed using the `rename-session` command
*RuntimeError* `error message`::
an error was encountered while executing a user command

View File

@ -2577,10 +2577,12 @@ const CommandDesc rename_session_cmd = {
CommandFlags::None,
CommandHelper{},
make_single_word_completer([](const Context&){ return Server::instance().session(); }),
[](const ParametersParser& parser, Context&, const ShellContext&)
[](const ParametersParser& parser, Context& ctx, const ShellContext&)
{
String old_name = Server::instance().session();
if (not Server::instance().rename_session(parser[0]))
throw runtime_error(format("unable to rename current session: '{}' may be already in use", parser[0]));
ctx.hooks().run_hook(Hook::SessionRenamed, format("{}:{}", old_name, Server::instance().session()), ctx);
}
};

View File

@ -4,6 +4,7 @@
#include "client.hh"
#include "face_registry.hh"
#include "buffer_manager.hh"
#include "hook_manager.hh"
#include "register_manager.hh"
#include "window.hh"
@ -417,4 +418,8 @@ StringView Context::main_sel_register_value(StringView reg) const
return RegisterManager::instance()[reg].get_main(*this, index);
}
void Context::set_name(String name) {
String old_name = std::exchange(m_name, std::move(name));
hooks().run_hook(Hook::ClientRenamed, format("{}:{}", old_name, m_name), *this);
}
}

View File

@ -110,7 +110,7 @@ public:
StringView main_sel_register_value(StringView reg) const;
const String& name() const { return m_name; }
void set_name(String name) { m_name = std::move(name); }
void set_name(String name);
bool is_editing() const { return m_edition_level!= 0; }
void disable_undo_handling() { m_edition_level = -1; }

View File

@ -30,6 +30,8 @@ enum class Hook
BufSetOption,
ClientCreate,
ClientClose,
ClientRenamed,
SessionRenamed,
InsertChar,
InsertDelete,
InsertIdle,
@ -75,6 +77,8 @@ constexpr auto enum_desc(Meta::Type<Hook>)
{Hook::BufSetOption, "BufSetOption"},
{Hook::ClientCreate, "ClientCreate"},
{Hook::ClientClose, "ClientClose"},
{Hook::ClientRenamed, "ClientRenamed"},
{Hook::SessionRenamed, "SessionRenamed"},
{Hook::InsertChar, "InsertChar"},
{Hook::InsertDelete, "InsertDelete"},
{Hook::InsertIdle, "InsertIdle"},