From 7eec39075213432eebd029487c14b6c5ed2e0032 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Mon, 12 May 2025 22:44:45 +0200 Subject: [PATCH] Fix dirty buffer after doing anything in title bar, OnCommand in lua stops c code, FKeys --- data/init.lua | 44 +++++++++++++++++++++------ src/text_editor/commands_bindings.cpp | 18 ++++++----- src/text_editor/generated.cpp | 44 +++++++++++++++++++++------ src/text_editor/lua_api.cpp | 7 +++-- src/text_editor/title_bar.cpp | 16 +++++----- src/text_editor/todo.txt | 2 -- 6 files changed, 94 insertions(+), 37 deletions(-) diff --git a/data/init.lua b/data/init.lua index 07ae061..80c29af 100644 --- a/data/init.lua +++ b/data/init.lua @@ -281,14 +281,6 @@ function AddCo(f) return Coroutines[i] end -OnCommandCallbacks = {} -table.insert(OnCommandCallbacks, function (e) - if e.key == SDLK_F and e.ctrl == 1 and e.shift == 1 then - C("git grep -n "..GetLoadWord().." :/") end - if e.key == SDLK_B and e.ctrl == 1 then - Cmd { working_dir = GetWorkDir(), kind = "console", cmd = "build.bat" } end -end) - function OnUpdate(e) local new_co_list = {} for key, co in pairs(Coroutines) do @@ -301,11 +293,45 @@ function OnUpdate(e) Coroutines = new_co_list end + +function KeybindsBasic(e) + if e.key == SDLK_F and e.ctrl == 1 and e.shift == 1 then + C("git grep -n "..GetLoadWord().." :/") + return true + end + return false +end + +FKey = {"build.bat", "", "", "", "", "", "", "", "", "", "", ""} +FKeySDLK = {SDLK_F1, SDLK_F2, SDLK_F3, SDLK_F4, SDLK_F5, SDLK_F6, SDLK_F7, SDLK_F8, SDLK_F9, SDLK_F10, SDLK_F11, SDLK_F12} + +function KeybindsFKeys(e) + for i = #FKey,1,-1 do + if FKey[i] ~= "" then + if e.key == FKeySDLK[i] then + Cmd { working_dir = GetWorkDir(), kind = "console", cmd = FKey[i] } + return true + end + end + end + return false +end + +BuiltinOnCommandCallbacks = { + KeybindsBasic, + KeybindsFKeys, +} +OnCommandCallbacks = BuiltinOnCommandCallbacks + function OnCommand(e) for i = #OnCommandCallbacks,1,-1 do on_command = OnCommandCallbacks[i] - on_command(e) + local result = on_command(e) + if result then + return true + end end + return false end function OnInit() diff --git a/src/text_editor/commands_bindings.cpp b/src/text_editor/commands_bindings.cpp index 3235592..fde05a0 100644 --- a/src/text_editor/commands_bindings.cpp +++ b/src/text_editor/commands_bindings.cpp @@ -340,8 +340,13 @@ void OnCommand(Event event) { BSet active = GetActiveSet(); Int buffer_change_id = active.buffer->change_id; - void CallOnCommand(Event * event); - CallOnCommand(&event); + bool skip = CallOnCommand(&event); + if (skip) { + // :OnCommandEnding + MergeCarets(active.buffer, &active.view->carets); + IF_DEBUG(AssertRanges(active.view->carets)); + return; + } @@ -522,15 +527,13 @@ void OnCommand(Event event) { Command_SelectRangeOneCursor(main.view, caret); } - if (ShiftPress(SDLK_F4)) { + if (CtrlPress(SDLK_E)) { Command_GotoNextInList(active.window, -1); - } else if (Press(SDLK_F4)) { + } else if (AltPress(SDLK_E)) { Command_GotoNextInList(active.window, 1); } - if (CtrlShiftPress(SDLK_F)) { - // because using lua thing - } else if (CtrlPress(SDLK_F)) { + if (CtrlPress(SDLK_F)) { if (!active.window->is_search_bar) { BSet search = GetBSet(main.window->search_bar_window); String16 string = GetString(main.buffer, main.view->carets[0].range); @@ -659,6 +662,7 @@ void OnCommand(Event event) { Command_Find(main.view, GetSearchString(main.window), true); } + // :OnCommandEnding MergeCarets(active.buffer, &active.view->carets); IF_DEBUG(AssertRanges(active.view->carets)); } diff --git a/src/text_editor/generated.cpp b/src/text_editor/generated.cpp index cbced8c..bb50472 100644 --- a/src/text_editor/generated.cpp +++ b/src/text_editor/generated.cpp @@ -351,14 +351,6 @@ function AddCo(f) return Coroutines[i] end -OnCommandCallbacks = {} -table.insert(OnCommandCallbacks, function (e) - if e.key == SDLK_F and e.ctrl == 1 and e.shift == 1 then - C("git grep -n "..GetLoadWord().." :/") end - if e.key == SDLK_B and e.ctrl == 1 then - Cmd { working_dir = GetWorkDir(), kind = "console", cmd = "build.bat" } end -end) - function OnUpdate(e) local new_co_list = {} for key, co in pairs(Coroutines) do @@ -371,11 +363,45 @@ function OnUpdate(e) Coroutines = new_co_list end + +function KeybindsBasic(e) + if e.key == SDLK_F and e.ctrl == 1 and e.shift == 1 then + C("git grep -n "..GetLoadWord().." :/") + return true + end + return false +end + +FKey = {"build.bat", "", "", "", "", "", "", "", "", "", "", ""} +FKeySDLK = {SDLK_F1, SDLK_F2, SDLK_F3, SDLK_F4, SDLK_F5, SDLK_F6, SDLK_F7, SDLK_F8, SDLK_F9, SDLK_F10, SDLK_F11, SDLK_F12} + +function KeybindsFKeys(e) + for i = #FKey,1,-1 do + if FKey[i] ~= "" then + if e.key == FKeySDLK[i] then + Cmd { working_dir = GetWorkDir(), kind = "console", cmd = FKey[i] } + return true + end + end + end + return false +end + +BuiltinOnCommandCallbacks = { + KeybindsBasic, + KeybindsFKeys, +} +OnCommandCallbacks = BuiltinOnCommandCallbacks + function OnCommand(e) for i = #OnCommandCallbacks,1,-1 do on_command = OnCommandCallbacks[i] - on_command(e) + local result = on_command(e) + if result then + return true + end end + return false end function OnInit() diff --git a/src/text_editor/lua_api.cpp b/src/text_editor/lua_api.cpp index d6fceb6..d643b56 100644 --- a/src/text_editor/lua_api.cpp +++ b/src/text_editor/lua_api.cpp @@ -337,10 +337,13 @@ void CallOnSave(BufferID buffer_id) { CallLuaFunc("OnSave", 1, 0); } -void CallOnCommand(Event *event) { +bool CallOnCommand(Event *event) { lua_getglobal(LuaState, "OnCommand"); PushEvent(LuaState, event); - CallLuaFunc("OnCommand", 1, 0); + CallLuaFunc("OnCommand", 1, 1); + bool result = lua_toboolean(LuaState, -1); + lua_pop(LuaState, 1); + return result; } void CallLuaOnUpdate(Event *event) { diff --git a/src/text_editor/title_bar.cpp b/src/text_editor/title_bar.cpp index 01696a9..6427ba3 100644 --- a/src/text_editor/title_bar.cpp +++ b/src/text_editor/title_bar.cpp @@ -45,6 +45,7 @@ void ReplaceTitleBarData(Window *window) { column = 0; } + bool name_changed = false; String name = ToString(scratch, buffer_name); if (name != main.buffer->name) { name = GetAbsolutePath(scratch, name); @@ -54,21 +55,20 @@ void ReplaceTitleBarData(Window *window) { return; } - main.buffer->name = Intern(&GlobalInternTable, name); + if (name != main.buffer->name) { + main.buffer->name = Intern(&GlobalInternTable, name); + title.window->title_bar_last_buffer_change_id = title.buffer->change_id; + main.buffer->file_mod_time = 0; + main.buffer->changed_on_disk = false; + main.buffer->dirty = true; + } } Int buffer_pos = XYToPos(main.buffer, {column, line}); - Caret &caret = main.view->carets[0]; if (GetFront(caret) != buffer_pos) { caret = MakeCaret(buffer_pos); } - - title.window->title_bar_last_buffer_change_id = title.buffer->change_id; - main.buffer->file_mod_time = 0; - main.buffer->changed_on_disk = false; - main.buffer->dirty = true; - return; } diff --git a/src/text_editor/todo.txt b/src/text_editor/todo.txt index faae5ef..b9da0a8 100644 --- a/src/text_editor/todo.txt +++ b/src/text_editor/todo.txt @@ -6,9 +6,7 @@ - Check. Convert more commands to taking buffer instead of view - Check. Rewrite more commands to use already implemented commands? - Lua OnCommand should be able to comunicate that we don't want C handling and do only the Lua handling -- Easily programmable F1-F12 commands - Lua namespaces for different kinds of commands (by ids, using main_set, using active_set)? -- List all files in project, exclude by lua pattern - Set window layout using project file - Split command