From b5772fa52cf2271ad2fd7a84ea48a3c4aeb41150 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Thu, 15 Aug 2024 10:03:44 +0200 Subject: [PATCH] OnCommand and OnInit --- .gitignore | 3 ++- build_file.cpp | 24 +++++++++++++++++-- src/text_editor/commands_bindings.cpp | 3 +++ src/text_editor/generated.cpp | 24 +++++++++++++++++-- src/text_editor/lua_api.cpp | 34 +++++++++++++++++++++++---- src/text_editor/text_editor.cpp | 3 ++- text_editor.sublime-project | 9 +++++++ 7 files changed, 90 insertions(+), 10 deletions(-) create mode 100644 text_editor.sublime-project diff --git a/.gitignore b/.gitignore index 3d1fd53..4adc1e7 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ x64/Release src/external/SDL/ build/ *.rdbg -*.spall \ No newline at end of file +*.spall +*.sublime-workspace \ No newline at end of file diff --git a/build_file.cpp b/build_file.cpp index ad3f78a..dcb88c7 100644 --- a/build_file.cpp +++ b/build_file.cpp @@ -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() { diff --git a/src/text_editor/commands_bindings.cpp b/src/text_editor/commands_bindings.cpp index 30e9d1d..f0e993a 100644 --- a/src/text_editor/commands_bindings.cpp +++ b/src/text_editor/commands_bindings.cpp @@ -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); } diff --git a/src/text_editor/generated.cpp b/src/text_editor/generated.cpp index e2d84df..6995864 100644 --- a/src/text_editor/generated.cpp +++ b/src/text_editor/generated.cpp @@ -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() { diff --git a/src/text_editor/lua_api.cpp b/src/text_editor/lua_api.cpp index 29ef552..a19cdd5 100644 --- a/src/text_editor/lua_api.cpp +++ b/src/text_editor/lua_api.cpp @@ -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(); } // diff --git a/src/text_editor/text_editor.cpp b/src/text_editor/text_editor.cpp index 7bc4057..35700e5 100644 --- a/src/text_editor/text_editor.cpp +++ b/src/text_editor/text_editor.cpp @@ -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(); diff --git a/text_editor.sublime-project b/text_editor.sublime-project new file mode 100644 index 0000000..e246f3f --- /dev/null +++ b/text_editor.sublime-project @@ -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"], + } + ], +}