Add a HooksManager class
This commit is contained in:
parent
9a4d8d5f4d
commit
9775958012
22
src/hooks_manager.cc
Normal file
22
src/hooks_manager.cc
Normal file
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
42
src/hooks_manager.hh
Normal file
42
src/hooks_manager.hh
Normal file
|
@ -0,0 +1,42 @@
|
|||
#ifndef hooks_manager_hh_INCLUDED
|
||||
#define hooks_manager_hh_INCLUDED
|
||||
|
||||
#include "window.hh"
|
||||
#include "utils.hh"
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
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<void (const HookContext&)> HookFunc;
|
||||
|
||||
class HooksManager : public Singleton<HooksManager>
|
||||
{
|
||||
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<std::string, std::vector<HookFunc>> m_hooks;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // hooks_manager_hh_INCLUDED
|
||||
|
|
@ -9,6 +9,7 @@
|
|||
#include "debug.hh"
|
||||
#include "filters.hh"
|
||||
#include "filter_registry.hh"
|
||||
#include "hooks_manager.hh"
|
||||
|
||||
#include <unordered_map>
|
||||
#include <map>
|
||||
|
@ -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<std::string>{ "e", "edit" }, edit,
|
||||
PerArgumentCommandCompleter{ complete_filename });
|
||||
|
|
Loading…
Reference in New Issue
Block a user