summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMaxime Coste <frrrwww@gmail.com>2011-11-25 14:26:29 +0000
committerMaxime Coste <frrrwww@gmail.com>2011-11-25 14:26:29 +0000
commit9775958012ebeace0af3721fd58727f3e77e2a67 (patch)
tree0ee371f7ccdf9bfe21898479d7c5e97219854b2a /src
parent9a4d8d5f4d36ed64563da05970d686a6a364628d (diff)
Add a HooksManager class
Diffstat (limited to 'src')
-rw-r--r--src/hooks_manager.cc22
-rw-r--r--src/hooks_manager.hh42
-rw-r--r--src/main.cc2
3 files changed, 66 insertions, 0 deletions
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 <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
+
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 <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 });