diff --git a/aegisub/src/auto4_lua.cpp b/aegisub/src/auto4_lua.cpp index fbdf30681..a738a6ba5 100644 --- a/aegisub/src/auto4_lua.cpp +++ b/aegisub/src/auto4_lua.cpp @@ -596,16 +596,12 @@ 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"); @@ -618,11 +614,6 @@ 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); @@ -650,17 +641,6 @@ 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(); @@ -695,18 +675,25 @@ namespace Automation4 { LuaAssFile *subsobj = new LuaAssFile(L, c->ass); lua_pushinteger(L, transform_selection(L, c)); - int err = lua_pcall(L, 3, 1, 0); + int err = lua_pcall(L, 3, 2, 0); subsobj->ProcessingComplete(); - bool result = false; - if (err) + if (err) { wxLogWarning("Runtime error in Lua macro validation function:\n%s", get_wxstring(L, -1)); - else - result = !!lua_toboolean(L, -1); + lua_pop(L, 1); + return false; + } - // clean up stack (result or error message) - lua_pop(L, 1); + bool result = !!lua_toboolean(L, -2); + + wxString new_help_string(get_wxstring(L, -1)); + if (new_help_string.size()) { + help = new_help_string; + cmd_type |= cmd::COMMAND_DYNAMIC_HELP; + } + + lua_pop(L, 2); return result; } diff --git a/aegisub/src/auto4_lua.h b/aegisub/src/auto4_lua.h index bf92424b8..65cb55dd3 100644 --- a/aegisub/src/auto4_lua.h +++ b/aegisub/src/auto4_lua.h @@ -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; + wxString StrHelp() const { return help; } int Type() const { return cmd_type; } diff --git a/aegisub/src/menu.cpp b/aegisub/src/menu.cpp index 914d88e6b..9421c48a3 100644 --- a/aegisub/src/menu.cpp +++ b/aegisub/src/menu.cpp @@ -146,12 +146,12 @@ class CommandManager { void UpdateItem(std::pair const& item) { cmd::Command *c = cmd::get(item.first); int flags = c->Type(); + if (flags & cmd::COMMAND_VALIDATE) + item.second->Enable(c->Validate(context)); if (flags & cmd::COMMAND_DYNAMIC_NAME) UpdateItemName(item); if (flags & cmd::COMMAND_DYNAMIC_HELP) item.second->SetHelp(c->StrHelp()); - if (flags & cmd::COMMAND_VALIDATE) - item.second->Enable(c->Validate(context)); if (flags & cmd::COMMAND_RADIO || flags & cmd::COMMAND_TOGGLE) { bool check = c->IsActive(context); // Don't call Check(false) on radio items as this causes wxGtk to