macro.lua (626B)
1 local Macros = { } 2 3 local function doMacro( key ) 4 local macro = Macros[ key ] 5 6 if macro then 7 local ok, err = xpcall( macro, debug.traceback ) 8 if not ok then 9 mud.print( "\n#s> macro callback failed: %s", err ) 10 end 11 end 12 end 13 14 function mud.macro( key, callback, disabled ) 15 enforce( key, "key", "string" ) 16 enforce( callback, "callback", "function", "string" ) 17 18 if type( callback ) == "string" then 19 local command = callback 20 21 callback = function() 22 mud.input( command ) 23 end 24 end 25 26 assert( not Macros[ key ], "macro `%s' already registered" % key ) 27 28 Macros[ key ] = callback 29 end 30 31 return { 32 doMacro = doMacro, 33 }