From cebb280fe546190e55e071b6adbf37cd47950f34 Mon Sep 17 00:00:00 2001 From: Collin Williams <96917990+bluedragon1221@users.noreply.github.com> Date: Mon, 11 May 2026 15:07:55 -0500 Subject: initial commit --- runtime/lua/core/hooks.lua | 61 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 runtime/lua/core/hooks.lua (limited to 'runtime/lua/core/hooks.lua') diff --git a/runtime/lua/core/hooks.lua b/runtime/lua/core/hooks.lua new file mode 100644 index 0000000..9623e9c --- /dev/null +++ b/runtime/lua/core/hooks.lua @@ -0,0 +1,61 @@ +local M = {} + +function M.new_store() + return {} +end + +function M.add(store, event_name, fn) + if type(event_name) ~= "string" or event_name == "" then + return false, "event name must be a non-empty string" + end + if type(fn) ~= "function" then + return false, "hook must be a function" + end + + local bucket = store[event_name] + if not bucket then + bucket = {} + store[event_name] = bucket + end + + bucket[#bucket + 1] = fn + return true +end + +function M.run(store, event_name, ...) + local bucket = store[event_name] + if not bucket then + return true + end + + local ok_all = true + for i = 1, #bucket do + local ok, err = pcall(bucket[i], ...) + if not ok then + ok_all = false + print(string.format("lemacs hook error [%s]: %s", event_name, tostring(err))) + end + end + + return ok_all +end + +function M.run_first_truthy(store, event_name, ...) + local bucket = store[event_name] + if not bucket then + return false + end + + for i = 1, #bucket do + local ok, result = pcall(bucket[i], ...) + if not ok then + print(string.format("lemacs hook error [%s]: %s", event_name, tostring(result))) + elseif result then + return true + end + end + + return false +end + +return M -- cgit v1.3.1