From f7f2aafc56706e38566dac898e1e1ddbb2a99376 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Tue, 13 May 2025 09:04:52 +0200 Subject: [PATCH] FKey function, fix goto build, GetLine, remedybg --- data/init.lua | 4 +++- src/build_tool/core/cmd.c | 1 - src/text_editor/commands.cpp | 18 ++++++++++++++++++ src/text_editor/commands_bindings.cpp | 10 +++++----- src/text_editor/generated.cpp | 5 ++++- src/text_editor/lua_api.cpp | 9 +++++++++ src/text_editor/lua_api_generated.cpp | 2 ++ src/text_editor/text_editor.cpp | 2 +- src/text_editor/todo.txt | 6 +----- 9 files changed, 43 insertions(+), 14 deletions(-) diff --git a/data/init.lua b/data/init.lua index 80c29af..c91665f 100644 --- a/data/init.lua +++ b/data/init.lua @@ -309,7 +309,9 @@ 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] } + local cmdline = FKey[i] + if type(cmdline) == "function" then cmdline = FKey[i]() end + Cmd { working_dir = GetWorkDir(), kind = "console", cmd = cmdline } return true end end diff --git a/src/build_tool/core/cmd.c b/src/build_tool/core/cmd.c index 68cc10a..6115247 100644 --- a/src/build_tool/core/cmd.c +++ b/src/build_tool/core/cmd.c @@ -1,4 +1,3 @@ - CmdParser MakeCmdParser(MA_Arena *arena, int argc, char **argv, const char *custom_help) { CmdParser result = {argc, argv, arena, custom_help}; return result; diff --git a/src/text_editor/commands.cpp b/src/text_editor/commands.cpp index 76f689c..b8f8bf4 100644 --- a/src/text_editor/commands.cpp +++ b/src/text_editor/commands.cpp @@ -1214,11 +1214,27 @@ void Command_ListBuffers() { Command_Appendf(main.view, "%.*s\n", FmtString(it->name)); } } + int Lua_ListBuffers(lua_State *L) { Command_ListBuffers(); return 0; } +void Command_ListViews() { + BSet main = GetActiveMainSet(); + ActiveWindow = main.window->id; + JumpGarbageBuffer(&main); + for (View *it = FirstView; it; it = it->next) { + Buffer *buffer = GetBuffer(it->active_buffer); + Command_Appendf(main.view, "%d %.*s\n", (int)it->id.id, FmtString(buffer->name)); + } +} + +int Lua_ListViews(lua_State *L) { + Command_ListViews(); + return 0; +} + void Command_Eval(String string) { if (luaL_dostring(LuaState, string.data) != LUA_OK) { const char *error_message = lua_tostring(LuaState, -1); @@ -1226,10 +1242,12 @@ void Command_Eval(String string) { lua_pop(LuaState, 1); } } + void Command_Eval(String16 string) { Scratch scratch; Command_Eval(ToString(scratch, string)); } + int Lua_Eval(lua_State *L) { String string = lua_tostring(L, 1); lua_pop(L, 1); diff --git a/src/text_editor/commands_bindings.cpp b/src/text_editor/commands_bindings.cpp index fde05a0..708a032 100644 --- a/src/text_editor/commands_bindings.cpp +++ b/src/text_editor/commands_bindings.cpp @@ -284,10 +284,10 @@ void OnCommand(Event event) { } - if (CtrlPress(SDLK_P)) { - Command_ListCode(); - } else if (CtrlAltPress(SDLK_P)) { + if (CtrlAltPress(SDLK_P)) { Command_ListBuffers(); + } else if (CtrlPress(SDLK_P)) { + Command_ListCode(); } if (CtrlShiftPress(SDLK_BACKSLASH)) { @@ -528,9 +528,9 @@ void OnCommand(Event event) { } if (CtrlPress(SDLK_E)) { - Command_GotoNextInList(active.window, -1); - } else if (AltPress(SDLK_E)) { Command_GotoNextInList(active.window, 1); + } else if (AltPress(SDLK_E)) { + Command_GotoNextInList(active.window, -1); } if (CtrlPress(SDLK_F)) { diff --git a/src/text_editor/generated.cpp b/src/text_editor/generated.cpp index bb50472..2a92556 100644 --- a/src/text_editor/generated.cpp +++ b/src/text_editor/generated.cpp @@ -379,7 +379,10 @@ 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] } + local cmdline = FKey[i] + if type(cmdline) == "function" then cmdline = FKey[i]() end + Print(cmdline) + Cmd { working_dir = GetWorkDir(), kind = "console", cmd = cmdline } return true end end diff --git a/src/text_editor/lua_api.cpp b/src/text_editor/lua_api.cpp index d643b56..3795735 100644 --- a/src/text_editor/lua_api.cpp +++ b/src/text_editor/lua_api.cpp @@ -70,6 +70,15 @@ int Lua_GetFilename(lua_State *L) { return 1; } +int Lua_GetLine(lua_State *L) { + BSet main = GetActiveMainSet(); + Caret caret = main.view->carets[0]; + Int front = GetFront(caret); + Int line = PosToLine(main.buffer, front); + lua_pushinteger(L, line + 1); + return 1; +} + int Lua_FileExists(lua_State *L) { String path = luaL_checkstring(L, 1); lua_pop(L, 1); diff --git a/src/text_editor/lua_api_generated.cpp b/src/text_editor/lua_api_generated.cpp index 550fef2..7c1392e 100644 --- a/src/text_editor/lua_api_generated.cpp +++ b/src/text_editor/lua_api_generated.cpp @@ -7,6 +7,7 @@ luaL_Reg LuaFunctions[] = { {"GetEntireBuffer", Lua_GetEntireBuffer}, {"GetClipboard", Lua_GetClipboard}, {"GetFilename", Lua_GetFilename}, + {"GetLine", Lua_GetLine}, {"FileExists", Lua_FileExists}, {"GetWorkDir", Lua_GetWorkDir}, {"GetMainDir", Lua_GetMainDir}, @@ -26,6 +27,7 @@ luaL_Reg LuaFunctions[] = { {"Open", Lua_Open}, {"Cmd", Lua_Cmd}, {"ListBuffers", Lua_ListBuffers}, + {"ListViews", Lua_ListViews}, {"Eval", Lua_Eval}, {"SetProjectFile", Lua_SetProjectFile}, {"SetWorkDir", Lua_SetWorkDir}, diff --git a/src/text_editor/text_editor.cpp b/src/text_editor/text_editor.cpp index 245328c..6a32c3f 100644 --- a/src/text_editor/text_editor.cpp +++ b/src/text_editor/text_editor.cpp @@ -486,4 +486,4 @@ end_of_editor_loop:; EndProfiler(); return 0; -} \ No newline at end of file +} diff --git a/src/text_editor/todo.txt b/src/text_editor/todo.txt index b9da0a8..753dd1c 100644 --- a/src/text_editor/todo.txt +++ b/src/text_editor/todo.txt @@ -5,8 +5,8 @@ - Delete directory/file on disk command - 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 - Lua namespaces for different kinds of commands (by ids, using main_set, using active_set)? +- Some decl/function indexing in fuzzy format - Set window layout using project file - Split command @@ -15,10 +15,6 @@ - Open buffer using id - Set active window using id - Fix jump scroll, the query ends up the last line on screen, kind of wacky -- Use project file as working dir instead of scratch buffer path, separate project dir and project file -- Remedybg integration - - GetLine() - - in lua start debugging, jump to line, start debugger on file - save all relevant buffers and build - maybe most of the bindings should be in lua, but actual code in C