From a94b914e06431d5bee59cd585dde0a47d1eee3ae Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Mon, 12 May 2025 16:35:06 +0200 Subject: [PATCH] WorkDir concept --- data/init.lua | 4 +-- src/text_editor/commands.cpp | 43 +++++++++++++++++++++++++-- src/text_editor/commands_bindings.cpp | 23 -------------- src/text_editor/generated.cpp | 4 +-- src/text_editor/lua_api.cpp | 15 ++-------- src/text_editor/lua_api_generated.cpp | 4 +-- src/text_editor/management.cpp | 12 +++----- src/text_editor/text_editor.cpp | 6 ++-- src/text_editor/text_editor.h | 1 - src/text_editor/window.cpp | 7 ++--- 10 files changed, 59 insertions(+), 60 deletions(-) diff --git a/data/init.lua b/data/init.lua index ad459aa..dd0b8c3 100644 --- a/data/init.lua +++ b/data/init.lua @@ -238,7 +238,7 @@ function MatchExec(s, meta) return nil end - if s:match(".exe$") then + if s:match(".exe$") or s:match(".bat$") or s:match(".sh$") then return {kind = "exec_console", cmd = s, working_dir = GetMainDir()} end @@ -283,7 +283,7 @@ 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 = GetProjectDir(), kind = "console", cmd = "build.bat" } end + Cmd { working_dir = GetWorkDir(), kind = "console", cmd = "build.bat" } end end) function OnUpdate(e) diff --git a/src/text_editor/commands.cpp b/src/text_editor/commands.cpp index e8ca05d..e3e45b6 100644 --- a/src/text_editor/commands.cpp +++ b/src/text_editor/commands.cpp @@ -1235,10 +1235,26 @@ int Lua_Eval(lua_State *L) { return 0; } +void SetProjectFile(Buffer *buffer) { + WorkDir = ChopLastSlash(buffer->name); + LuaProjectBuffer = buffer; + LuaProjectBuffer->user_change_id = -1; +} + int Lua_SetProjectFile(lua_State *L) { BSet set = GetActiveMainSet(); - LuaProjectBuffer = set.buffer; - LuaProjectBuffer->user_change_id = -1; + SetProjectFile(set.buffer); + return 0; +} + +int Lua_SetWorkDir(lua_State *L) { + String dir = lua_tostring(L, -1); + if (dir.len == 0) { + BSet set = GetActiveMainSet(); + WorkDir = ChopLastSlash(set.buffer->name); + } else { + WorkDir = dir; + } return 0; } @@ -1305,3 +1321,26 @@ Window *SwitchWindow(int direction) { Window *result = GetOverlappingWindow(p, window); return result; } + +String16 GetSearchString(Window *window) { + if (!window->is_search_bar) { + if (window->search_bar_window.id == 0) { + return {}; + } + window = GetWindow(window->search_bar_window); + } + + View *view = GetView(window->active_view); + Buffer *buffer = GetBuffer(view->active_buffer); + String16 string = GetString(buffer); + return string; +} + +String16 FetchLoadWord(void) { + BSet active = GetActiveSet(); + Caret caret = active.view->carets[0]; + Range range = caret.range; + if (GetSize(caret.range) == 0) range = EncloseLoadWord(active.buffer, GetFront(caret)); + String16 string = GetString(active.buffer, range); + return string; +} \ No newline at end of file diff --git a/src/text_editor/commands_bindings.cpp b/src/text_editor/commands_bindings.cpp index 781a477..1e578fc 100644 --- a/src/text_editor/commands_bindings.cpp +++ b/src/text_editor/commands_bindings.cpp @@ -1,26 +1,3 @@ -String16 GetSearchString(Window *window) { - if (!window->is_search_bar) { - if (window->search_bar_window.id == 0) { - return {}; - } - window = GetWindow(window->search_bar_window); - } - - View *view = GetView(window->active_view); - Buffer *buffer = GetBuffer(view->active_buffer); - String16 string = GetString(buffer); - return string; -} - -String16 FetchLoadWord(void) { - BSet active = GetActiveSet(); - Caret caret = active.view->carets[0]; - Range range = caret.range; - if (GetSize(caret.range) == 0) range = EncloseLoadWord(active.buffer, GetFront(caret)); - String16 string = GetString(active.buffer, range); - return string; -} - void UpdateScroll(Window *window, bool update_caret_scrolling) { ProfileFunction(); BSet set = GetBSet(window); diff --git a/src/text_editor/generated.cpp b/src/text_editor/generated.cpp index 93fdda6..0be6b8a 100644 --- a/src/text_editor/generated.cpp +++ b/src/text_editor/generated.cpp @@ -308,7 +308,7 @@ function MatchExec(s, meta) return nil end - if s:match(".exe$") then + if s:match(".exe$") or s:match(".bat$") or s:match(".sh$") then return {kind = "exec_console", cmd = s, working_dir = GetMainDir()} end @@ -353,7 +353,7 @@ 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 = GetProjectDir(), kind = "console", cmd = "build.bat" } end + Cmd { working_dir = GetWorkDir(), kind = "console", cmd = "build.bat" } end end) function OnUpdate(e) diff --git a/src/text_editor/lua_api.cpp b/src/text_editor/lua_api.cpp index 5f5d6c1..4749f2f 100644 --- a/src/text_editor/lua_api.cpp +++ b/src/text_editor/lua_api.cpp @@ -70,16 +70,6 @@ int Lua_GetFilename(lua_State *L) { return 1; } -int Lua_GetProjectDir(lua_State *L) { - if (LuaProjectBuffer) { - String path = ChopLastSlash(LuaProjectBuffer->name); - lua_pushlstring(L, path.data, path.len); - } else { - lua_pushlstring(L, "no config", 9); - } - return 1; -} - int Lua_FileExists(lua_State *L) { String path = luaL_checkstring(L, 1); lua_pop(L, 1); @@ -88,9 +78,8 @@ int Lua_FileExists(lua_State *L) { return 1; } -int Lua_GetBaseDir(lua_State *L) { - String name = Command_GetBaseDir(); - lua_pushlstring(L, name.data, name.len); +int Lua_GetWorkDir(lua_State *L) { + lua_pushlstring(L, WorkDir.data, WorkDir.len); return 1; } diff --git a/src/text_editor/lua_api_generated.cpp b/src/text_editor/lua_api_generated.cpp index 59aa275..7c7e4c7 100644 --- a/src/text_editor/lua_api_generated.cpp +++ b/src/text_editor/lua_api_generated.cpp @@ -7,9 +7,8 @@ luaL_Reg LuaFunctions[] = { {"GetEntireBuffer", Lua_GetEntireBuffer}, {"GetClipboard", Lua_GetClipboard}, {"GetFilename", Lua_GetFilename}, - {"GetProjectDir", Lua_GetProjectDir}, {"FileExists", Lua_FileExists}, - {"GetBaseDir", Lua_GetBaseDir}, + {"GetWorkDir", Lua_GetWorkDir}, {"GetMainDir", Lua_GetMainDir}, {"SplitSize", Lua_SplitSize}, {"Play", Lua_Play}, @@ -29,6 +28,7 @@ luaL_Reg LuaFunctions[] = { {"ListBuffers", Lua_ListBuffers}, {"Eval", Lua_Eval}, {"SetProjectFile", Lua_SetProjectFile}, + {"SetWorkDir", Lua_SetWorkDir}, {"ListCommands", Lua_ListCommands}, {"GetBufferList", Lua_GetBufferList}, {NULL, NULL}, diff --git a/src/text_editor/management.cpp b/src/text_editor/management.cpp index 7d3efb6..d520d3a 100644 --- a/src/text_editor/management.cpp +++ b/src/text_editor/management.cpp @@ -38,6 +38,7 @@ Buffer *GCInfoBuffer; Buffer *EventBuffer; Buffer *ScratchBuffer; +String WorkDir; RandomSeed UniqueBufferNameSeed = {}; // lua @@ -72,9 +73,9 @@ void InitScratchBuffer() { View *null_view = CreateView(null_buffer->id); Assert(null_buffer->id == NullBufferID && null_view->id == NullViewID); - GCInfoBuffer = CreateBuffer(sys_allocator, GetUniqueBufferName(Command_GetBaseDir(), "gc")); - EventBuffer = CreateBuffer(sys_allocator, GetUniqueBufferName(Command_GetBaseDir(), "events")); - ScratchBuffer = BufferOpenFile(GetUniqueBufferName(Command_GetBaseDir(), "scratch")); + GCInfoBuffer = CreateBuffer(sys_allocator, GetUniqueBufferName(WorkDir, "gc")); + EventBuffer = CreateBuffer(sys_allocator, GetUniqueBufferName(WorkDir, "events")); + ScratchBuffer = BufferOpenFile(GetUniqueBufferName(WorkDir, "scratch")); EventBuffer->no_history = true; GCInfoBuffer->no_history = true; @@ -328,11 +329,6 @@ String Command_GetMainDir() { return name; } -String Command_GetBaseDir() { - Buffer *buffer = GetBuffer(NullBufferID); - return ChopLastSlash(buffer->name); -} - Int ConvertUTF8ToUTF16UnixLine(String string, char16_t *buffer, Int buffer_cap) { if (string.len == 0) { return 0; diff --git a/src/text_editor/text_editor.cpp b/src/text_editor/text_editor.cpp index d400c1d..245328c 100644 --- a/src/text_editor/text_editor.cpp +++ b/src/text_editor/text_editor.cpp @@ -285,7 +285,7 @@ void Windows_SetupVCVarsall(mco_coro *co) { View *view = NULL; { Scratch scratch; - String working_dir = Command_GetBaseDir(); + String working_dir = WorkDir; String buffer_name = GetUniqueBufferName(working_dir, "vcvarsall-"); String cmd = Format(scratch, "\"%.*s\" && set", FmtString(StyleVCVarsall)); view = Command_ExecHidden(buffer_name, cmd, working_dir); @@ -325,6 +325,7 @@ int main(int argc, char **argv) InitArena(&Perm); // Prototype(); + WorkDir = GetWorkingDir(Perm); { String sdl_config_path = SDL_GetPrefPath("krzosa", "text_editor"); if (sdl_config_path.len && sdl_config_path.data[sdl_config_path.len - 1] == '\\') { @@ -410,8 +411,7 @@ int main(int argc, char **argv) if (EndsWith(it, ".project.lua")) { if (!LuaProjectBuffer) { - LuaProjectBuffer = BufferOpenFile(it); - LuaProjectBuffer->user_change_id = -1; + SetProjectFile(BufferOpenFile(it)); } } else { Window *window = GetWindow({0}); diff --git a/src/text_editor/text_editor.h b/src/text_editor/text_editor.h index eb8c579..49e305a 100644 --- a/src/text_editor/text_editor.h +++ b/src/text_editor/text_editor.h @@ -120,7 +120,6 @@ Array Command_ReplaceEx(Allocator scratch, View *view, String16 string); void Command_Eval(String string); void Command_Eval(String16 string); String Command_GetMainDir(); -String Command_GetBaseDir(); void ReplaceWithoutMovingCarets(Buffer *buffer, Range range, String16 string); void Command_Copy(View *view); diff --git a/src/text_editor/window.cpp b/src/text_editor/window.cpp index 8d76f14..6dd40ff 100644 --- a/src/text_editor/window.cpp +++ b/src/text_editor/window.cpp @@ -14,7 +14,7 @@ Window *CreateSearchBar(WindowID parent_window_id) { static int BarCount; Allocator sys_allocator = GetSystemAllocator(); - String name = Format(sys_allocator, "%.*s/searchbar%d", FmtString(Command_GetBaseDir()), ++BarCount); + String name = Format(sys_allocator, "%.*s/searchbar%d", FmtString(WorkDir), ++BarCount); Buffer *b = CreateBuffer(sys_allocator, name); View *v = CreateView(b->id); @@ -37,7 +37,7 @@ Window *CreateTitlebar(WindowID parent_window_id) { static int TitlebarCount; Allocator sys_allocator = GetSystemAllocator(); - String name = Format(sys_allocator, "%.*s/titlebar%d", FmtString(Command_GetBaseDir()), ++TitlebarCount); + String name = Format(sys_allocator, "%.*s/titlebar%d", FmtString(WorkDir), ++TitlebarCount); Buffer *b = CreateBuffer(sys_allocator, name); View *v = CreateView(b->id); @@ -99,7 +99,6 @@ void SplitWindow(WindowSplitKind kind) { Window *active_window = GetActiveWindow(); SplitWindowEx(NULL, &WindowSplits, active_window, window, kind); ActiveWindow = window->id; - } void SetVisibility(WindowID window_id, bool v) { @@ -205,7 +204,7 @@ void InitWindows() { window->visible = false; window->z = 2; - Buffer *buffer = CreateBuffer(sys_allocator, GetUniqueBufferName(Command_GetBaseDir(), "debug")); + Buffer *buffer = CreateBuffer(sys_allocator, GetUniqueBufferName(WorkDir, "debug")); DebugBufferID = buffer->id; buffer->no_history = true;