OnCommand and OnInit

This commit is contained in:
Krzosa Karol
2024-08-15 10:03:44 +02:00
parent d4ac1dd817
commit b5772fa52c
7 changed files with 90 additions and 10 deletions

1
.gitignore vendored
View File

@@ -6,3 +6,4 @@ src/external/SDL/
build/
*.rdbg
*.spall
*.sublime-workspace

View File

@@ -185,6 +185,19 @@ KEY_RIGHT = 1073741903
KEY_LEFT = 1073741904
KEY_Q = 113
KEY_F1 = 0x4000003a
KEY_F2 = 0x4000003b
KEY_F3 = 0x4000003c
KEY_F4 = 0x4000003d
KEY_F5 = 0x4000003e
KEY_F6 = 0x4000003f
KEY_F7 = 0x40000040
KEY_F8 = 0x40000041
KEY_F9 = 0x40000042
KEY_F10 = 0x40000043
KEY_F11 = 0x40000044
KEY_F12 = 0x40000045
function SkipLineAndColumn(s)
local line, col = "1", "1"
@@ -355,21 +368,28 @@ Coroutines = {}
function AddCo(f)
local i = #Coroutines + 1
Coroutines[i] = coroutine.create(f)
coroutine.resume(Coroutines[i])
return Coroutines[i]
end
function OnUpdate()
function OnUpdate(e)
local new_co_list = {}
for key, co in pairs(Coroutines) do
local status = coroutine.status(co)
if status ~= "dead" then
coroutine.resume(co)
coroutine.resume(co, e)
new_co_list[#new_co_list + 1] = co
end
end
Coroutines = new_co_list
end
function OnCommand(e)
end
function OnInit()
end
)==";
void GenerateConfig() {

View File

@@ -264,6 +264,9 @@ void GlobalCommand(Event event) {
BSet active = GetActiveSet();
Int buffer_change_id = active.buffer->change_id;
void CallOnCommand(Event * event);
CallOnCommand(&event);
if (event.kind == EVENT_DROP_FILE) {
WindowOpenBufferView(active.window, event.text);
}

View File

@@ -75,6 +75,19 @@ KEY_RIGHT = 1073741903
KEY_LEFT = 1073741904
KEY_Q = 113
KEY_F1 = 0x4000003a
KEY_F2 = 0x4000003b
KEY_F3 = 0x4000003c
KEY_F4 = 0x4000003d
KEY_F5 = 0x4000003e
KEY_F6 = 0x4000003f
KEY_F7 = 0x40000040
KEY_F8 = 0x40000041
KEY_F9 = 0x40000042
KEY_F10 = 0x40000043
KEY_F11 = 0x40000044
KEY_F12 = 0x40000045
function SkipLineAndColumn(s)
local line, col = "1", "1"
@@ -245,21 +258,28 @@ Coroutines = {}
function AddCo(f)
local i = #Coroutines + 1
Coroutines[i] = coroutine.create(f)
coroutine.resume(Coroutines[i])
return Coroutines[i]
end
function OnUpdate()
function OnUpdate(e)
local new_co_list = {}
for key, co in pairs(Coroutines) do
local status = coroutine.status(co)
if status ~= "dead" then
coroutine.resume(co)
coroutine.resume(co, e)
new_co_list[#new_co_list + 1] = co
end
end
Coroutines = new_co_list
end
function OnCommand(e)
end
function OnInit()
end
)==";
void ReloadStyle() {

View File

@@ -370,7 +370,7 @@ int LoadLuaBuffer(Buffer *lua_buffer) {
return true;
}
void UpdateLua() {
void ReloadLuaConfigs() {
int base_config_reload = LoadLuaBuffer(LuaConfigBuffer);
int project_config_reload = LoadLuaBuffer(LuaProjectBuffer);
@@ -385,13 +385,38 @@ void UpdateLua() {
window->draw_line_numbers = StyleDrawLineNumbers;
}
}
}
if (luaL_dostring(LuaState, "OnUpdate()") != 0) {
void CallOnCommand(Event *event) {
lua_getglobal(LuaState, "OnCommand");
PushEvent(LuaState, event);
if (lua_pcall(LuaState, 1, 0, 0) != 0) {
const char *error_message = lua_tostring(LuaState, -1);
ReportWarningf("OnUpdate failed! %s", error_message);
ReportWarningf("Failed the call to OnCommand! %s", error_message);
lua_pop(LuaState, 1);
return;
}
// bool command_handled = lua_toboolean(LuaState, -1);
// return command_handled;
}
void CallLuaOnUpdate(Event *event) {
lua_getglobal(LuaState, "OnUpdate");
PushEvent(LuaState, event);
if (lua_pcall(LuaState, 1, 0, 0) != 0) {
const char *error_message = lua_tostring(LuaState, -1);
ReportWarningf("Failed the call to OnUpdate! %s", error_message);
lua_pop(LuaState, 1);
return;
}
}
void CallLuaOnInit() {
if (luaL_dostring(LuaState, "OnInit()") != LUA_OK) {
const char *error_message = lua_tostring(LuaState, -1);
ReportErrorf("Error in OnInit! %s", error_message);
lua_pop(LuaState, 1);
}
}
void InitLuaConfig() {
@@ -433,7 +458,8 @@ void InitLuaConfig() {
lua_buffer->user_change_id = -1; // if we loaded from file this should force to read
LuaConfigBuffer = lua_buffer;
UpdateLua();
ReloadLuaConfigs();
CallLuaOnInit();
}
//

View File

@@ -166,7 +166,8 @@ void Update(Event event) {
GlobalCommand(event);
For(Windows) if (it.o->is_title_bar) ReplaceTitleBarData(it.o);
UpdateProcesses();
UpdateLua();
ReloadLuaConfigs();
CallLuaOnUpdate(&event);
UpdateDebugBuffer();
GarbageCollect();

View File

@@ -0,0 +1,9 @@
{
"folders":
[
{
"path": ".",
"folder_exclude_patterns": ["src/external/SDL/src", "src/external/SDL/test", "src/external/SDL/cmake", "src/external/SDL/mingw", "src/external/SDL/docs", "src/external/SDL/build-scripts", "src/external/SDL/Xcode", "src/external/SDL/VisualC"],
}
],
}