diff options
| author | Akshay Nair <phenax5@gmail.com> | 2025-03-22 00:51:46 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2025-03-22 00:51:50 +0530 |
| commit | f09f1aa704f06472ec134b40e2f69bf3279c3f06 (patch) | |
| tree | f292716c90d16ade8d31b2b2d70da8481d1e8294 /src/LuaRuntime.cpp | |
| parent | 498135054a168bd839f2ee8ebb1ba245d72a8f3b (diff) | |
| download | null-browser-f09f1aa704f06472ec134b40e2f69bf3279c3f06.tar.gz null-browser-f09f1aa704f06472ec134b40e2f69bf3279c3f06.zip | |
Add web.keymap.set + test keymaps config.lua
Diffstat (limited to 'src/LuaRuntime.cpp')
| -rw-r--r-- | src/LuaRuntime.cpp | 47 |
1 files changed, 46 insertions, 1 deletions
diff --git a/src/LuaRuntime.cpp b/src/LuaRuntime.cpp index e9a30b1..f83ee48 100644 --- a/src/LuaRuntime.cpp +++ b/src/LuaRuntime.cpp @@ -22,6 +22,14 @@ LuaRuntime::LuaRuntime() { }; luaL_newlib(state, weblib); lua_setglobal(state, web_global_name); + + lua_getglobal(state, web_global_name); + luaL_Reg keymaplib[] = { + {"set", &LuaRuntime::lua_addKeymap}, + }; + luaL_newlib(state, keymaplib); + lua_setfield(state, -2, "keymap"); + lua_pop(state, 1); } void LuaRuntime::startEventLoop() { @@ -51,7 +59,7 @@ void LuaRuntime::stopEventLoop() { void LuaRuntime::evaluate(QString code) { eventLoop->queueTask([this, code]() { - if (luaL_dostring(state, code.toStdString().c_str())) { + if (luaL_dostring(state, code.toStdString().c_str()) != LUA_OK) { auto value = lua_tostring(state, -1); lua_pop(state, 1); @@ -102,6 +110,43 @@ int LuaRuntime::lua_onUrlTabOpen(lua_State *state) { return 1; } +int LuaRuntime::lua_addKeymap(lua_State *state) { + const char *mode = lua_tostring(state, 1); + const char *keyseq = lua_tostring(state, 2); + qDebug() << "Adduing" << mode << keyseq; + + qDebug() << "---" << lua_isfunction(state, 3); + + lua_pushvalue(state, 3); + int functionRef = luaL_ref(state, LUA_REGISTRYINDEX); + auto action = [state, functionRef]() { + qDebug() << "Hello"; + + lua_rawgeti(state, LUA_REGISTRYINDEX, functionRef); + if (lua_pcall(state, 0, 0, 0) != LUA_OK) { + const char *error = lua_tostring(state, -1); + qDebug() << "Error calling Lua function:" << error; + lua_pop(state, 1); + } else { + qDebug() << "Done"; + } + lua_pop(state, 1); + }; + + auto runtime = LuaRuntime::instance(); + emit runtime->keymapAddRequested(mode, keyseq, action); + + return 1; +} + +void LuaRuntime::loadFile(QString path) { + qDebug() << "Loading: " << path; + if (luaL_dofile(state, path.toStdString().c_str()) != LUA_OK) { + qDebug() << "Load file error:" << lua_tostring(state, -1); + lua_pop(state, 1); + } +} + LuaRuntime::~LuaRuntime() { stopEventLoop(); lua_close(state); |
