my-nvim-setup/lua/core/hooks.lua

36 lines
634 B
Lua
Raw Normal View History

2021-11-13 16:29:31 +00:00
local hooks, M = {}, {}
2021-11-14 07:13:36 +00:00
local allowed_hooks = {
2021-10-02 06:15:50 +01:00
"install_plugins",
"setup_mappings",
"ready",
}
2021-10-02 06:15:50 +01:00
local function has_value(tab, val)
for _, value in ipairs(tab) do
if value == val then
return true
end
end
end
M.add = function(name, fn)
2021-10-02 06:15:50 +01:00
if not (has_value(allowed_hooks, name)) then
2021-11-13 16:29:31 +00:00
print("Custom lua uses unallowed hook " .. name)
2021-10-02 06:15:50 +01:00
end
if hooks[name] == nil then
hooks[name] = {}
end
table.insert(hooks[name], fn)
end
M.run = function(name, args)
2021-11-13 16:29:31 +00:00
if hooks[name] ~= nil then
for _, hook in pairs(hooks[name]) do
hook(args)
2021-08-31 15:20:57 +01:00
end
2021-10-02 06:15:50 +01:00
end
2021-08-31 15:20:57 +01:00
end
2021-10-02 06:15:50 +01:00
return M