Add auto4lua support for dynamic help strings

Rather than passing a string as the second argument to
aegisub.register_macro, they can now pass a function which will be
called (with no arguments) to get a help string.

Originally committed to SVN as r6327.
This commit is contained in:
Thomas Goyne 2012-01-20 21:33:39 +00:00
parent b0c1ef2d6b
commit a939732d9c
2 changed files with 24 additions and 4 deletions

View File

@ -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<AssDialogue*> sel = c->selectionController->GetSelectedSet();

View File

@ -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; }