kakoune

Fork of kakoune, an acceptable text editor
git clone git://git.laack.co/kakoune.git
Log | Files | Refs

hook_manager.hh (4083B)


      1 #ifndef hook_manager_hh_INCLUDED
      2 #define hook_manager_hh_INCLUDED
      3 
      4 #include "completion.hh"
      5 #include "safe_ptr.hh"
      6 #include "meta.hh"
      7 #include "enum.hh"
      8 #include "array.hh"
      9 #include "unique_ptr.hh"
     10 
     11 namespace Kakoune
     12 {
     13 
     14 class Context;
     15 class Regex;
     16 
     17 enum class Hook
     18 {
     19     BufCreate,
     20     BufNewFile,
     21     BufOpenFile,
     22     BufClose,
     23     BufWritePost,
     24     BufReload,
     25     BufWritePre,
     26     BufOpenFifo,
     27     BufCloseFifo,
     28     BufReadFifo,
     29     BufSetOption,
     30     ClientCreate,
     31     ClientClose,
     32     ClientRenamed,
     33     SessionRenamed,
     34     InsertChar,
     35     InsertDelete,
     36     InsertIdle,
     37     InsertKey,
     38     InsertMove,
     39     InsertCompletionHide,
     40     InsertCompletionShow,
     41     KakBegin,
     42     KakEnd,
     43     FocusIn,
     44     FocusOut,
     45     GlobalSetOption,
     46     RuntimeError,
     47     PromptIdle,
     48     NormalIdle,
     49     NextKeyIdle,
     50     NormalKey,
     51     ModeChange,
     52     EnterDirectory,
     53     RawKey,
     54     RegisterModified,
     55     WinClose,
     56     WinCreate,
     57     WinDisplay,
     58     WinResize,
     59     WinSetOption,
     60     ModuleLoaded,
     61     User
     62 };
     63 
     64 constexpr auto enum_desc(Meta::Type<Hook>)
     65 {
     66     return make_array<EnumDesc<Hook>>({
     67         {Hook::BufCreate, "BufCreate"},
     68         {Hook::BufNewFile, "BufNewFile"},
     69         {Hook::BufOpenFile, "BufOpenFile"},
     70         {Hook::BufClose, "BufClose"},
     71         {Hook::BufWritePost, "BufWritePost"},
     72         {Hook::BufReload, "BufReload"},
     73         {Hook::BufWritePre, "BufWritePre"},
     74         {Hook::BufOpenFifo, "BufOpenFifo"},
     75         {Hook::BufCloseFifo, "BufCloseFifo"},
     76         {Hook::BufReadFifo, "BufReadFifo"},
     77         {Hook::BufSetOption, "BufSetOption"},
     78         {Hook::ClientCreate, "ClientCreate"},
     79         {Hook::ClientClose, "ClientClose"},
     80         {Hook::ClientRenamed, "ClientRenamed"},
     81         {Hook::SessionRenamed, "SessionRenamed"},
     82         {Hook::InsertChar, "InsertChar"},
     83         {Hook::InsertDelete, "InsertDelete"},
     84         {Hook::InsertIdle, "InsertIdle"},
     85         {Hook::InsertKey, "InsertKey"},
     86         {Hook::InsertMove, "InsertMove"},
     87         {Hook::InsertCompletionHide, "InsertCompletionHide"},
     88         {Hook::InsertCompletionShow, "InsertCompletionShow"},
     89         {Hook::KakBegin, "KakBegin"},
     90         {Hook::KakEnd, "KakEnd"},
     91         {Hook::FocusIn, "FocusIn"},
     92         {Hook::FocusOut, "FocusOut"},
     93         {Hook::GlobalSetOption, "GlobalSetOption"},
     94         {Hook::RuntimeError, "RuntimeError"},
     95         {Hook::PromptIdle, "PromptIdle"},
     96         {Hook::NormalIdle, "NormalIdle"},
     97         {Hook::NextKeyIdle, "NextKeyIdle"},
     98         {Hook::NormalKey, "NormalKey"},
     99         {Hook::ModeChange, "ModeChange"},
    100         {Hook::EnterDirectory, "EnterDirectory"},
    101         {Hook::RawKey, "RawKey"},
    102         {Hook::RegisterModified, "RegisterModified"},
    103         {Hook::WinClose, "WinClose"},
    104         {Hook::WinCreate, "WinCreate"},
    105         {Hook::WinDisplay, "WinDisplay"},
    106         {Hook::WinResize, "WinResize"},
    107         {Hook::WinSetOption, "WinSetOption"},
    108         {Hook::ModuleLoaded, "ModuleLoaded"},
    109         {Hook::User, "User"}
    110     });
    111 }
    112 
    113 enum class HookFlags
    114 {
    115     None = 0,
    116     Always = 1 << 0,
    117     Once = 1 << 1
    118 };
    119 constexpr bool with_bit_ops(Meta::Type<HookFlags>) { return true; }
    120 
    121 class HookManager : public SafeCountable
    122 {
    123 public:
    124     HookManager(HookManager& parent);
    125     ~HookManager();
    126 
    127     void reparent(HookManager& parent) { m_parent = &parent; }
    128 
    129     void add_hook(Hook hook, String group, HookFlags flags,
    130                   Regex filter, String commands, Context& context);
    131     void remove_hooks(const Regex& regex);
    132     CandidateList complete_hook_group(StringView prefix, ByteCount pos_in_token);
    133     void run_hook(Hook hook, StringView param, Context& context);
    134 
    135 private:
    136     struct HookData;
    137 
    138     HookManager();
    139     // the only one allowed to construct a root hook manager
    140     friend class Scope;
    141 
    142     SafePtr<HookManager> m_parent;
    143     Array<Vector<UniquePtr<HookData>, MemoryDomain::Hooks>, enum_desc(Meta::Type<Hook>{}).size()> m_hooks;
    144 
    145     mutable Vector<std::pair<Hook, StringView>, MemoryDomain::Hooks> m_running_hooks;
    146     mutable Vector<UniquePtr<HookData>, MemoryDomain::Hooks> m_hooks_trash;
    147 };
    148 
    149 }
    150 
    151 #endif // hook_manager_hh_INCLUDED