diff --git a/src/hooks_manager.cc b/src/hooks_manager.cc new file mode 100644 index 00000000..7dbbaf42 --- /dev/null +++ b/src/hooks_manager.cc @@ -0,0 +1,22 @@ +#include "hooks_manager.hh" + +namespace Kakoune +{ + +void HooksManager::add_hook(const std::string& hook_name, HookFunc hook) +{ + m_hooks[hook_name].push_back(hook); +} + +void HooksManager::run_hook(const std::string& hook_name, + const HookContext& context) const +{ + auto hook_list_it = m_hooks.find(hook_name); + if (hook_list_it == m_hooks.end()) + return; + + for (auto& hook : hook_list_it->second) + hook(context); +} + +} diff --git a/src/hooks_manager.hh b/src/hooks_manager.hh new file mode 100644 index 00000000..81549f60 --- /dev/null +++ b/src/hooks_manager.hh @@ -0,0 +1,42 @@ +#ifndef hooks_manager_hh_INCLUDED +#define hooks_manager_hh_INCLUDED + +#include "window.hh" +#include "utils.hh" + +#include + +namespace Kakoune +{ + +struct HookContext +{ + Window* window; + Buffer* buffer; + std::string context; + + HookContext(const std::string& context) + : window(nullptr), buffer(nullptr), context(context) {} + HookContext(const std::string& context, Window& window) + : window(&window), buffer(&window.buffer()), context(context) {} + HookContext(const std::string& context, Buffer& buffer) + : window(nullptr), buffer(&buffer), context(context) {} +}; + +typedef std::function HookFunc; + +class HooksManager : public Singleton +{ +public: + void add_hook(const std::string& hook_name, HookFunc hook); + void run_hook(const std::string& hook_name, + const HookContext& context) const; + +private: + std::unordered_map> m_hooks; +}; + +} + +#endif // hooks_manager_hh_INCLUDED + diff --git a/src/main.cc b/src/main.cc index 3ecfae3f..d51ed348 100644 --- a/src/main.cc +++ b/src/main.cc @@ -9,6 +9,7 @@ #include "debug.hh" #include "filters.hh" #include "filter_registry.hh" +#include "hooks_manager.hh" #include #include @@ -637,6 +638,7 @@ int main(int argc, char* argv[]) BufferManager buffer_manager; RegisterManager register_manager; FilterRegistry filter_registry; + HooksManager hooks_manager; command_manager.register_command(std::vector{ "e", "edit" }, edit, PerArgumentCommandCompleter{ complete_filename });