Skip to content

Commit

Permalink
allow functions for tempAlias and tempKey
Browse files Browse the repository at this point in the history
  • Loading branch information
Edru2 committed Oct 16, 2020
1 parent df7b49a commit 0ad5398
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 12 deletions.
12 changes: 12 additions & 0 deletions src/TAlias.cpp
Expand Up @@ -35,6 +35,7 @@ TAlias::TAlias(TAlias* parent, Host* pHost)
, mModuleMember(false)
, mModuleMasterFolder(false)
, exportItem(true)
, mRegisteredAnonymousLuaFunction(false)
{
}

Expand All @@ -46,6 +47,7 @@ TAlias::TAlias(const QString& name, Host* pHost)
, mModuleMember(false)
, mModuleMasterFolder(false)
, exportItem(true)
, mRegisteredAnonymousLuaFunction(false)
{
}

Expand Down Expand Up @@ -339,5 +341,15 @@ void TAlias::execute()
return;
}
}

if (mRegisteredAnonymousLuaFunction) {
mpHost->mLuaInterpreter.call_luafunction(this);
return;
}

if (mScript.isEmpty()) {
return;
}

mpHost->mLuaInterpreter.call(mFuncName, mName);
}
1 change: 1 addition & 0 deletions src/TAlias.h
Expand Up @@ -77,6 +77,7 @@ class TAlias : public Tree<TAlias>
bool mModuleMasterFolder;
QString mFuncName;
bool exportItem;
bool mRegisteredAnonymousLuaFunction;
};

#endif // MUDLET_TALIAS_H
14 changes: 13 additions & 1 deletion src/TKey.cpp
Expand Up @@ -36,6 +36,7 @@ TKey::TKey(TKey* parent, Host* pHost)
, mModuleMember(false)
, mKeyCode()
, mKeyModifier()
, mRegisteredAnonymousLuaFunction(false)
{
}

Expand All @@ -49,6 +50,7 @@ TKey::TKey(QString name, Host* pHost)
, mModuleMember(false)
, mKeyCode()
, mKeyModifier()
, mRegisteredAnonymousLuaFunction(false)
{
}

Expand Down Expand Up @@ -158,7 +160,7 @@ void TKey::compile()
}
}

bool TKey::setScript(QString& script)
bool TKey::setScript(const QString& script)
{
mScript = script;
mNeedsToBeCompiled = true;
Expand Down Expand Up @@ -192,5 +194,15 @@ void TKey::execute()
return;
}
}

if (mRegisteredAnonymousLuaFunction) {
mpHost->mLuaInterpreter.call_luafunction(this);
return;
}

if (mScript.isEmpty()) {
return;
}

mpHost->mLuaInterpreter.call(mFuncName, mName);
}
3 changes: 2 additions & 1 deletion src/TKey.h
Expand Up @@ -55,7 +55,7 @@ class TKey : public Tree<TKey>
bool compileScript();
void execute();
QString getScript() { return mScript; }
bool setScript(QString& script);
bool setScript(const QString& script);
void setCommand(QString command) { mCommand = command; }
QString getCommand() { return mCommand; }

Expand All @@ -64,6 +64,7 @@ class TKey : public Tree<TKey>

bool exportItem;
bool mModuleMasterFolder;
bool mRegisteredAnonymousLuaFunction;

private:
TKey() = default;
Expand Down
61 changes: 52 additions & 9 deletions src/TLuaInterpreter.cpp
Expand Up @@ -8231,15 +8231,37 @@ int TLuaInterpreter::tempAlias(lua_State* L)
}
QString regex{lua_tostring(L, 1)};



Host& host = getHostFromLua(L);
TLuaInterpreter* pLuaInterpreter = host.getLuaInterpreter();

if (lua_isfunction(L, 2)) {

int result = pLuaInterpreter->startTempAlias(regex, QString());
if (result == -1) {
lua_pushnumber(L, -1);
return 2;
}

TAlias* alias = host.getAliasUnit()->getAlias(result);
Q_ASSERT_X(alias,
"TLuaInterpreter::tempAlias(...)",
"Got a positive result from LuaInterpreter::startTempAlias(...) but that failed to produce pointer to it from Host::mAliasUnit::getAlias(...)");
alias->mRegisteredAnonymousLuaFunction = true;
lua_pushlightuserdata(L, alias);
lua_pushvalue(L, 2);
lua_settable(L, LUA_REGISTRYINDEX);
lua_pushnumber(L, result);
return 1;
}

if (!lua_isstring(L, 2)) {
lua_pushfstring(L, "tempAlias: bad argument #2 type (lua script as string expected, got %s!)",
luaL_typename(L, 2));
lua_pushfstring(L, "tempAlias: bad argument #2 type (lua script as string or function expected, got %s!)", luaL_typename(L, 2));
return lua_error(L);
}
QString script{lua_tostring(L, 2)};

Host& host = getHostFromLua(L);
TLuaInterpreter* pLuaInterpreter = host.getLuaInterpreter();
lua_pushnumber(L, pLuaInterpreter->startTempAlias(regex, script));
return 1;
}
Expand Down Expand Up @@ -8681,14 +8703,35 @@ int TLuaInterpreter::tempKey(lua_State* L)
}
int keyCode = lua_tointeger(L, argIndex);

if (!lua_isstring(L, ++argIndex)) {
lua_pushfstring(L, "tempKey: bad argument #%d type (lua script as string expected, got %s!)", argIndex, luaL_typename(L, argIndex));
Host& host = getHostFromLua(L);
TLuaInterpreter* pLuaInterpreter = host.getLuaInterpreter();

if (lua_isfunction(L, ++argIndex)) {

int result = pLuaInterpreter->startTempKey(keyModifier, keyCode, QString());
if (result == -1) {
lua_pushnumber(L, -1);
return 2;
}

TKey* key = host.getKeyUnit()->getKey(result);
Q_ASSERT_X(key,
"TLuaInterpreter::tempKey(...)",
"Got a positive result from LuaInterpreter::startTempKey(...) but that failed to produce pointer to it from Host::mKeyUnit::getKey(...)");
key->mRegisteredAnonymousLuaFunction = true;
lua_pushlightuserdata(L, key);
lua_pushvalue(L, argIndex);
lua_settable(L, LUA_REGISTRYINDEX);
lua_pushnumber(L, result);
return 1;
}

if (!lua_isstring(L, argIndex)) {
lua_pushfstring(L, "tempKey: bad argument #%d type (lua script as string or function expected, got %s!)", argIndex, luaL_typename(L, argIndex));
return lua_error(L);
}
QString luaFunction{lua_tostring(L, argIndex)};

Host& host = getHostFromLua(L);
TLuaInterpreter* pLuaInterpreter = host.getLuaInterpreter();
int timerID = pLuaInterpreter->startTempKey(keyModifier, keyCode, luaFunction);
lua_pushnumber(L, timerID);
return 1;
Expand Down Expand Up @@ -17311,7 +17354,7 @@ int TLuaInterpreter::startPermKey(QString& name, QString& parent, int& keycode,
}

// No documentation available in wiki - internal function
int TLuaInterpreter::startTempKey(int& modifier, int& keycode, QString& function)
int TLuaInterpreter::startTempKey(int& modifier, int& keycode, const QString& function)
{
TKey* pT;
pT = new TKey("a", mpHost);
Expand Down
2 changes: 1 addition & 1 deletion src/TLuaInterpreter.h
Expand Up @@ -126,7 +126,7 @@ class TLuaInterpreter : public QThread

QPair<int, QString> startTempTimer(double timeout, const QString& function, const bool repeating = false);
int startTempAlias(const QString&, const QString&);
int startTempKey(int&, int&, QString&);
int startTempKey(int&, int&, const QString&);
int startTempTrigger(const QString& regex, const QString& function, int expiryCount = -1);
int startTempBeginOfLineTrigger(const QString&, const QString&, int expiryCount = -1);
int startTempExactMatchTrigger(const QString&, const QString&, int expiryCount = -1);
Expand Down

0 comments on commit 0ad5398

Please sign in to comment.