summaryrefslogtreecommitdiff
path: root/runtime/lua/core/commands.lua
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/lua/core/commands.lua')
-rw-r--r--runtime/lua/core/commands.lua41
1 files changed, 41 insertions, 0 deletions
diff --git a/runtime/lua/core/commands.lua b/runtime/lua/core/commands.lua
new file mode 100644
index 0000000..2aa484d
--- /dev/null
+++ b/runtime/lua/core/commands.lua
@@ -0,0 +1,41 @@
+local M = {}
+
+function M.define(state, name, fn)
+ state.commands[name] = fn
+end
+
+function M.bind(state, key, command_name)
+ state.keymap[key] = command_name
+end
+
+function M.call(state, name, ...)
+ local fn = state.commands[name]
+ if not fn then
+ return false, "Unknown command: " .. tostring(name)
+ end
+
+ local ok, err = pcall(fn, ...)
+ if not ok then
+ return false, tostring(err)
+ end
+
+ return true
+end
+
+function M.bind_fn(state, key, fn)
+ if type(key) ~= "string" or key == "" then
+ return false, "key must be a non-empty string"
+ end
+ if type(fn) ~= "function" then
+ return false, "bind_fn requires a function"
+ end
+
+ local name = string.format("anon:%d", state.next_anon_command_id)
+ state.next_anon_command_id = state.next_anon_command_id + 1
+
+ state.commands[name] = fn
+ state.keymap[key] = name
+ return true
+end
+
+return M