Fix dirty buffer after doing anything in title bar, OnCommand in lua stops c code, FKeys

This commit is contained in:
Krzosa Karol
2025-05-12 22:44:45 +02:00
parent 0c7a424f5e
commit 7eec390752
6 changed files with 94 additions and 37 deletions

View File

@@ -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,12 +293,46 @@ 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()
end

View File

@@ -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));
}

View File

@@ -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,12 +363,46 @@ 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()
end

View File

@@ -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) {

View File

@@ -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;
}
if (name != main.buffer->name) {
main.buffer->name = Intern(&GlobalInternTable, name);
}
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;
}
}
Int buffer_pos = XYToPos(main.buffer, {column, line});
Caret &caret = main.view->carets[0];
if (GetFront(caret) != buffer_pos) {
caret = MakeCaret(buffer_pos);
}
return;
}

View File

@@ -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