From 0a1061278616bf5f8282af6366228ebf74cb5346 Mon Sep 17 00:00:00 2001
From: Philipp Jungkamp
Date: Thu, 6 Jun 2024 17:24:31 +0200
Subject: [PATCH] Add EnterDirectory hook
This hook runs on `change-directory` and is also emitted just before
KakBegin after kakrc has been sourced.
---
doc/pages/hooks.asciidoc | 5 +++++
src/commands.cc | 6 ++++--
src/hook_manager.hh | 2 ++
src/main.cc | 1 +
4 files changed, 12 insertions(+), 2 deletions(-)
diff --git a/doc/pages/hooks.asciidoc b/doc/pages/hooks.asciidoc
index 8d339d9d7..673118687 100644
--- a/doc/pages/hooks.asciidoc
+++ b/doc/pages/hooks.asciidoc
@@ -147,6 +147,11 @@ name. Hooks with no description will always use an empty string.
*SessionRenamed* `:`::
executed when a session is renamed using the `rename-session` command
+*EnterDirectory* `path`::
+ executed on startup and when the current working directory is changed
+ using the `change-directory` command. The hook param is an absolute path
+ to the new working directory.
+
*RuntimeError* `error message`::
an error was encountered while executing a user command
diff --git a/src/commands.cc b/src/commands.cc
index 0febf160a..ef51555e2 100644
--- a/src/commands.cc
+++ b/src/commands.cc
@@ -2600,13 +2600,15 @@ const CommandDesc change_directory_cmd = {
cursor_pos, FilenameFlags::OnlyDirectories),
Completions::Flags::Menu };
}),
- [](const ParametersParser& parser, Context&, const ShellContext&)
+ [](const ParametersParser& parser, Context& ctx, const ShellContext&)
{
StringView target = parser.positional_count() == 1 ? StringView{parser[0]} : "~";
- if (chdir(parse_filename(target).c_str()) != 0)
+ auto path = real_path(parse_filename(target));
+ if (chdir(path.c_str()) != 0)
throw runtime_error(format("unable to change to directory: '{}'", target));
for (auto& buffer : BufferManager::instance())
buffer->update_display_name();
+ ctx.hooks().run_hook(Hook::EnterDirectory, path, ctx);
}
};
diff --git a/src/hook_manager.hh b/src/hook_manager.hh
index 228a4f237..bee060642 100644
--- a/src/hook_manager.hh
+++ b/src/hook_manager.hh
@@ -50,6 +50,7 @@ enum class Hook
NextKeyIdle,
NormalKey,
ModeChange,
+ EnterDirectory,
RawKey,
RegisterModified,
WinClose,
@@ -97,6 +98,7 @@ constexpr auto enum_desc(Meta::Type)
{Hook::NextKeyIdle, "NextKeyIdle"},
{Hook::NormalKey, "NormalKey"},
{Hook::ModeChange, "ModeChange"},
+ {Hook::EnterDirectory, "EnterDirectory"},
{Hook::RawKey, "RawKey"},
{Hook::RegisterModified, "RegisterModified"},
{Hook::WinClose, "WinClose"},
diff --git a/src/main.cc b/src/main.cc
index 0c63403bd..f2e31cabe 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -842,6 +842,7 @@ int run_server(StringView session, StringView server_init,
{
Context empty_context{Context::EmptyContextFlag{}};
+ global_scope.hooks().run_hook(Hook::EnterDirectory, real_path("."), empty_context);
global_scope.hooks().run_hook(Hook::KakBegin, session, empty_context);
}