diff --git a/aegisub/src/auto4_lua.cpp b/aegisub/src/auto4_lua.cpp index c3c941901..fbdf30681 100644 --- a/aegisub/src/auto4_lua.cpp +++ b/aegisub/src/auto4_lua.cpp @@ -574,7 +574,7 @@ namespace Automation4 { luaL_unref(L, LUA_REGISTRYINDEX, myid); } - void LuaFeature::GetFeatureFunction(const char *function) + void LuaFeature::GetFeatureFunction(const char *function) const { // get this feature's function pointers lua_rawgeti(L, LUA_REGISTRYINDEX, myid); @@ -596,12 +596,16 @@ namespace Automation4 { LuaCommand::LuaCommand(lua_State *L) : LuaFeature(L) , display(check_wxstring(L, 1)) - , help(get_wxstring(L, 2)) , cmd_type(cmd::COMMAND_NORMAL) { lua_getfield(L, LUA_REGISTRYINDEX, "filename"); cmd_name = STD_STR(wxString::Format("automation/lua/%s/%s", get_wxstring(L, -1), check_wxstring(L, 1))); + if (lua_isstring(L, 2)) + help = get_wxstring(L, 2); + else if (lua_isfunction(L, 2)) + cmd_type |= cmd::COMMAND_DYNAMIC_HELP; + if (!lua_isfunction(L, 3)) luaL_error(L, "The macro processing function must be a function"); @@ -614,6 +618,11 @@ namespace Automation4 { // new table for containing the functions for this feature lua_newtable(L); + // store help string function + lua_pushstring(L, "help"); + lua_pushvalue(L, 2); + lua_rawset(L, -3); + // store processing function lua_pushstring(L, "run"); lua_pushvalue(L, 3); @@ -641,6 +650,17 @@ namespace Automation4 { LuaScript::GetScriptObject(L)->UnregisterCommand(this); } + wxString LuaCommand::StrHelp() const + { + if (!(cmd_type & cmd::COMMAND_DYNAMIC_HELP)) return help; + + GetFeatureFunction("help"); + lua_pcall(L, 0, 1, 0); + wxString result = get_wxstring(L, -1); + lua_pop(L, 1); + return result; + } + static int transform_selection(lua_State *L, const agi::Context *c) { std::set sel = c->selectionController->GetSelectedSet(); diff --git a/aegisub/src/auto4_lua.h b/aegisub/src/auto4_lua.h index 95caab948..bf92424b8 100644 --- a/aegisub/src/auto4_lua.h +++ b/aegisub/src/auto4_lua.h @@ -231,7 +231,7 @@ namespace Automation4 { void RegisterFeature(); void UnregisterFeature(); - void GetFeatureFunction(const char *function); + void GetFeatureFunction(const char *function) const; LuaFeature(lua_State *L); }; @@ -259,7 +259,7 @@ namespace Automation4 { const char* name() const { return cmd_name.c_str(); } wxString StrMenu(const agi::Context *) const { return display; } wxString StrDisplay(const agi::Context *) const { return display; } - wxString StrHelp() const { return help; } + wxString StrHelp() const; int Type() const { return cmd_type; }