2012-04-03 14:01:01 +02:00
|
|
|
#ifndef hook_manager_hh_INCLUDED
|
|
|
|
#define hook_manager_hh_INCLUDED
|
2011-11-25 15:26:29 +01:00
|
|
|
|
2013-11-18 23:24:31 +01:00
|
|
|
#include "id_map.hh"
|
2011-11-25 15:26:29 +01:00
|
|
|
#include "utils.hh"
|
|
|
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2013-11-11 20:10:49 +01:00
|
|
|
class Context;
|
2014-01-09 20:50:01 +01:00
|
|
|
using HookFunc = std::function<void (const String&, Context&)>;
|
2011-11-25 15:26:29 +01:00
|
|
|
|
2012-04-03 14:01:01 +02:00
|
|
|
class HookManager
|
2011-11-25 15:26:29 +01:00
|
|
|
{
|
|
|
|
public:
|
2012-06-07 15:29:44 +02:00
|
|
|
HookManager(HookManager& parent) : m_parent(&parent) {}
|
|
|
|
|
2014-06-16 21:42:12 +02:00
|
|
|
void add_hook(const String& hook_name, String group, HookFunc hook);
|
|
|
|
void remove_hooks(StringView group);
|
2012-04-14 03:17:09 +02:00
|
|
|
void run_hook(const String& hook_name, const String& param,
|
2013-01-17 13:44:14 +01:00
|
|
|
Context& context) const;
|
2011-11-25 15:26:29 +01:00
|
|
|
|
|
|
|
private:
|
2012-06-07 15:29:44 +02:00
|
|
|
HookManager()
|
|
|
|
: m_parent(nullptr) {}
|
|
|
|
// the only one allowed to construct a root hook manager
|
2012-11-22 13:50:29 +01:00
|
|
|
friend class GlobalHooks;
|
2012-06-07 15:29:44 +02:00
|
|
|
|
|
|
|
HookManager* m_parent;
|
2013-11-18 23:24:31 +01:00
|
|
|
std::unordered_map<String, id_map<HookFunc>> m_hook;
|
2011-11-25 15:26:29 +01:00
|
|
|
};
|
|
|
|
|
2012-11-22 13:50:29 +01:00
|
|
|
class GlobalHooks : public HookManager,
|
|
|
|
public Singleton<GlobalHooks>
|
2012-01-23 14:40:42 +01:00
|
|
|
{
|
2014-03-20 09:29:41 +01:00
|
|
|
public:
|
2014-06-21 13:08:19 +02:00
|
|
|
bool are_user_hooks_disabled() const;
|
2014-03-20 09:29:41 +01:00
|
|
|
|
2014-06-21 13:08:19 +02:00
|
|
|
void disable_user_hooks() { ++m_disabled; }
|
|
|
|
void enable_user_hooks() { --m_disabled; }
|
2014-03-20 09:29:41 +01:00
|
|
|
private:
|
|
|
|
int m_disabled = 0;
|
2012-01-23 14:40:42 +01:00
|
|
|
};
|
|
|
|
|
2011-11-25 15:26:29 +01:00
|
|
|
}
|
|
|
|
|
2012-04-03 14:01:01 +02:00
|
|
|
#endif // hook_manager_hh_INCLUDED
|
2011-11-25 15:26:29 +01:00
|
|
|
|