summaryrefslogtreecommitdiff
path: root/src/hook_manager.hh
blob: 44fb91077d56d9dd0766f1dbecb68b80cba03085 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#ifndef hook_manager_hh_INCLUDED
#define hook_manager_hh_INCLUDED

#include "hash_map.hh"
#include "completion.hh"
#include "safe_ptr.hh"

namespace Kakoune
{

class Context;
class Regex;

enum class HookFlags
{
    None = 0,
    Always = 1 << 0
};
constexpr bool with_bit_ops(Meta::Type<HookFlags>) { return true; }

class HookManager : public SafeCountable
{
public:
    HookManager(HookManager& parent);
    ~HookManager();

    void add_hook(StringView hook_name, String group, HookFlags flags,
                  Regex filter, String commands);
    void remove_hooks(StringView group);
    CandidateList complete_hook_group(StringView prefix, ByteCount pos_in_token);
    void run_hook(StringView hook_name, StringView param,
                  Context& context) const;

private:
    HookManager();
    // the only one allowed to construct a root hook manager
    friend class Scope;

    struct Hook;

    SafePtr<HookManager> m_parent;
    HashMap<String, Vector<std::unique_ptr<Hook>, MemoryDomain::Hooks>, MemoryDomain::Hooks> m_hooks;

    mutable Vector<std::pair<StringView, StringView>, MemoryDomain::Hooks> m_running_hooks;
    mutable Vector<std::unique_ptr<Hook>, MemoryDomain::Hooks> m_hooks_trash;
};

}

#endif // hook_manager_hh_INCLUDED