HookManager are now hierarchical from window to buffer to global

window hooks also executes buffer hook which also execute global hooks
This commit is contained in:
Maxime Coste 2012-06-07 13:29:44 +00:00
parent 6a416abae2
commit 9bcfda3226
5 changed files with 19 additions and 5 deletions

View File

@ -4,7 +4,6 @@
#include "window.hh" #include "window.hh"
#include "assert.hh" #include "assert.hh"
#include "utils.hh" #include "utils.hh"
#include "hook_manager.hh"
#include "context.hh" #include "context.hh"
#include <algorithm> #include <algorithm>
@ -27,6 +26,7 @@ Buffer::Buffer(const String& name, Type type,
: m_name(name), m_type(type), : m_name(name), m_type(type),
m_history(1), m_history_cursor(m_history.begin()), m_history(1), m_history_cursor(m_history.begin()),
m_last_save_undo_index(0), m_last_save_undo_index(0),
m_hook_manager(GlobalHookManager::instance()),
m_option_manager(GlobalOptionManager::instance()) m_option_manager(GlobalOptionManager::instance())
{ {
BufferManager::instance().register_buffer(this); BufferManager::instance().register_buffer(this);
@ -34,9 +34,9 @@ Buffer::Buffer(const String& name, Type type,
apply_modification(Modification::make_insert(begin(), initial_content)); apply_modification(Modification::make_insert(begin(), initial_content));
if (type == Type::NewFile) if (type == Type::NewFile)
GlobalHookManager::instance().run_hook("BufCreate", name, Context(*this)); m_hook_manager.run_hook("BufCreate", name, Context(*this));
else if (type == Type::File) else if (type == Type::File)
GlobalHookManager::instance().run_hook("BufOpen", name, Context(*this)); m_hook_manager.run_hook("BufOpen", name, Context(*this));
} }
Buffer::~Buffer() Buffer::~Buffer()

View File

@ -7,6 +7,7 @@
#include "line_and_column.hh" #include "line_and_column.hh"
#include "option_manager.hh" #include "option_manager.hh"
#include "hook_manager.hh"
#include "string.hh" #include "string.hh"
namespace Kakoune namespace Kakoune
@ -168,6 +169,7 @@ public:
const String& line_content(size_t l) const { return m_lines[l].content; } const String& line_content(size_t l) const { return m_lines[l].content; }
OptionManager& option_manager() { return m_option_manager; } OptionManager& option_manager() { return m_option_manager; }
HookManager& hook_manager() { return m_hook_manager; }
private: private:
friend class BufferIterator; friend class BufferIterator;
@ -208,6 +210,7 @@ private:
std::vector<BufferIterator*> m_iterators_to_update; std::vector<BufferIterator*> m_iterators_to_update;
OptionManager m_option_manager; OptionManager m_option_manager;
HookManager m_hook_manager;
}; };
inline Modification Modification::make_erase(BufferIterator begin, inline Modification Modification::make_erase(BufferIterator begin,

View File

@ -12,6 +12,9 @@ void HookManager::run_hook(const String& hook_name,
const String& param, const String& param,
const Context& context) const const Context& context) const
{ {
if (m_parent)
m_parent->run_hook(hook_name, param, context);
auto hook_list_it = m_hook.find(hook_name); auto hook_list_it = m_hook.find(hook_name);
if (hook_list_it == m_hook.end()) if (hook_list_it == m_hook.end())
return; return;

View File

@ -14,11 +14,19 @@ typedef std::function<void (const String&, const Context&)> HookFunc;
class HookManager class HookManager
{ {
public: public:
HookManager(HookManager& parent) : m_parent(&parent) {}
void add_hook(const String& hook_name, HookFunc hook); void add_hook(const String& hook_name, HookFunc hook);
void run_hook(const String& hook_name, const String& param, void run_hook(const String& hook_name, const String& param,
const Context& context) const; const Context& context) const;
private: private:
HookManager()
: m_parent(nullptr) {}
// the only one allowed to construct a root hook manager
friend class GlobalHookManager;
HookManager* m_parent;
std::unordered_map<String, std::vector<HookFunc>> m_hook; std::unordered_map<String, std::vector<HookFunc>> m_hook;
}; };

View File

@ -15,12 +15,12 @@ Window::Window(Buffer& buffer)
: Editor(buffer), : Editor(buffer),
m_position(0, 0), m_position(0, 0),
m_dimensions(0, 0), m_dimensions(0, 0),
m_hook_manager(buffer.hook_manager()),
m_option_manager(buffer.option_manager()) m_option_manager(buffer.option_manager())
{ {
HighlighterRegistry& registry = HighlighterRegistry::instance(); HighlighterRegistry& registry = HighlighterRegistry::instance();
GlobalHookManager::instance().run_hook("WinCreate", buffer.name(), m_hook_manager.run_hook("WinCreate", buffer.name(), Context(*this));
Context(*this));
registry.add_highlighter_to_window(*this, "expand_tabs", HighlighterParameters()); registry.add_highlighter_to_window(*this, "expand_tabs", HighlighterParameters());
registry.add_highlighter_to_window(*this, "highlight_selections", HighlighterParameters()); registry.add_highlighter_to_window(*this, "highlight_selections", HighlighterParameters());