WorkDir concept

This commit is contained in:
Krzosa Karol
2025-05-12 16:35:06 +02:00
parent fd842a1f70
commit a94b914e06
10 changed files with 59 additions and 60 deletions

View File

@@ -238,7 +238,7 @@ function MatchExec(s, meta)
return nil return nil
end 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()} return {kind = "exec_console", cmd = s, working_dir = GetMainDir()}
end end
@@ -283,7 +283,7 @@ table.insert(OnCommandCallbacks, function (e)
if e.key == SDLK_F and e.ctrl == 1 and e.shift == 1 then if e.key == SDLK_F and e.ctrl == 1 and e.shift == 1 then
C("git grep -n "..GetLoadWord().." :/") end C("git grep -n "..GetLoadWord().." :/") end
if e.key == SDLK_B and e.ctrl == 1 then 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) end)
function OnUpdate(e) function OnUpdate(e)

View File

@@ -1235,10 +1235,26 @@ int Lua_Eval(lua_State *L) {
return 0; return 0;
} }
void SetProjectFile(Buffer *buffer) {
WorkDir = ChopLastSlash(buffer->name);
LuaProjectBuffer = buffer;
LuaProjectBuffer->user_change_id = -1;
}
int Lua_SetProjectFile(lua_State *L) { int Lua_SetProjectFile(lua_State *L) {
BSet set = GetActiveMainSet(); BSet set = GetActiveMainSet();
LuaProjectBuffer = set.buffer; SetProjectFile(set.buffer);
LuaProjectBuffer->user_change_id = -1; 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; return 0;
} }
@@ -1305,3 +1321,26 @@ Window *SwitchWindow(int direction) {
Window *result = GetOverlappingWindow(p, window); Window *result = GetOverlappingWindow(p, window);
return result; 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;
}

View File

@@ -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) { void UpdateScroll(Window *window, bool update_caret_scrolling) {
ProfileFunction(); ProfileFunction();
BSet set = GetBSet(window); BSet set = GetBSet(window);

View File

@@ -308,7 +308,7 @@ function MatchExec(s, meta)
return nil return nil
end 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()} return {kind = "exec_console", cmd = s, working_dir = GetMainDir()}
end end
@@ -353,7 +353,7 @@ table.insert(OnCommandCallbacks, function (e)
if e.key == SDLK_F and e.ctrl == 1 and e.shift == 1 then if e.key == SDLK_F and e.ctrl == 1 and e.shift == 1 then
C("git grep -n "..GetLoadWord().." :/") end C("git grep -n "..GetLoadWord().." :/") end
if e.key == SDLK_B and e.ctrl == 1 then 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) end)
function OnUpdate(e) function OnUpdate(e)

View File

@@ -70,16 +70,6 @@ int Lua_GetFilename(lua_State *L) {
return 1; 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) { int Lua_FileExists(lua_State *L) {
String path = luaL_checkstring(L, 1); String path = luaL_checkstring(L, 1);
lua_pop(L, 1); lua_pop(L, 1);
@@ -88,9 +78,8 @@ int Lua_FileExists(lua_State *L) {
return 1; return 1;
} }
int Lua_GetBaseDir(lua_State *L) { int Lua_GetWorkDir(lua_State *L) {
String name = Command_GetBaseDir(); lua_pushlstring(L, WorkDir.data, WorkDir.len);
lua_pushlstring(L, name.data, name.len);
return 1; return 1;
} }

View File

@@ -7,9 +7,8 @@ luaL_Reg LuaFunctions[] = {
{"GetEntireBuffer", Lua_GetEntireBuffer}, {"GetEntireBuffer", Lua_GetEntireBuffer},
{"GetClipboard", Lua_GetClipboard}, {"GetClipboard", Lua_GetClipboard},
{"GetFilename", Lua_GetFilename}, {"GetFilename", Lua_GetFilename},
{"GetProjectDir", Lua_GetProjectDir},
{"FileExists", Lua_FileExists}, {"FileExists", Lua_FileExists},
{"GetBaseDir", Lua_GetBaseDir}, {"GetWorkDir", Lua_GetWorkDir},
{"GetMainDir", Lua_GetMainDir}, {"GetMainDir", Lua_GetMainDir},
{"SplitSize", Lua_SplitSize}, {"SplitSize", Lua_SplitSize},
{"Play", Lua_Play}, {"Play", Lua_Play},
@@ -29,6 +28,7 @@ luaL_Reg LuaFunctions[] = {
{"ListBuffers", Lua_ListBuffers}, {"ListBuffers", Lua_ListBuffers},
{"Eval", Lua_Eval}, {"Eval", Lua_Eval},
{"SetProjectFile", Lua_SetProjectFile}, {"SetProjectFile", Lua_SetProjectFile},
{"SetWorkDir", Lua_SetWorkDir},
{"ListCommands", Lua_ListCommands}, {"ListCommands", Lua_ListCommands},
{"GetBufferList", Lua_GetBufferList}, {"GetBufferList", Lua_GetBufferList},
{NULL, NULL}, {NULL, NULL},

View File

@@ -38,6 +38,7 @@ Buffer *GCInfoBuffer;
Buffer *EventBuffer; Buffer *EventBuffer;
Buffer *ScratchBuffer; Buffer *ScratchBuffer;
String WorkDir;
RandomSeed UniqueBufferNameSeed = {}; RandomSeed UniqueBufferNameSeed = {};
// lua // lua
@@ -72,9 +73,9 @@ void InitScratchBuffer() {
View *null_view = CreateView(null_buffer->id); View *null_view = CreateView(null_buffer->id);
Assert(null_buffer->id == NullBufferID && null_view->id == NullViewID); Assert(null_buffer->id == NullBufferID && null_view->id == NullViewID);
GCInfoBuffer = CreateBuffer(sys_allocator, GetUniqueBufferName(Command_GetBaseDir(), "gc")); GCInfoBuffer = CreateBuffer(sys_allocator, GetUniqueBufferName(WorkDir, "gc"));
EventBuffer = CreateBuffer(sys_allocator, GetUniqueBufferName(Command_GetBaseDir(), "events")); EventBuffer = CreateBuffer(sys_allocator, GetUniqueBufferName(WorkDir, "events"));
ScratchBuffer = BufferOpenFile(GetUniqueBufferName(Command_GetBaseDir(), "scratch")); ScratchBuffer = BufferOpenFile(GetUniqueBufferName(WorkDir, "scratch"));
EventBuffer->no_history = true; EventBuffer->no_history = true;
GCInfoBuffer->no_history = true; GCInfoBuffer->no_history = true;
@@ -328,11 +329,6 @@ String Command_GetMainDir() {
return name; return name;
} }
String Command_GetBaseDir() {
Buffer *buffer = GetBuffer(NullBufferID);
return ChopLastSlash(buffer->name);
}
Int ConvertUTF8ToUTF16UnixLine(String string, char16_t *buffer, Int buffer_cap) { Int ConvertUTF8ToUTF16UnixLine(String string, char16_t *buffer, Int buffer_cap) {
if (string.len == 0) { if (string.len == 0) {
return 0; return 0;

View File

@@ -285,7 +285,7 @@ void Windows_SetupVCVarsall(mco_coro *co) {
View *view = NULL; View *view = NULL;
{ {
Scratch scratch; Scratch scratch;
String working_dir = Command_GetBaseDir(); String working_dir = WorkDir;
String buffer_name = GetUniqueBufferName(working_dir, "vcvarsall-"); String buffer_name = GetUniqueBufferName(working_dir, "vcvarsall-");
String cmd = Format(scratch, "\"%.*s\" && set", FmtString(StyleVCVarsall)); String cmd = Format(scratch, "\"%.*s\" && set", FmtString(StyleVCVarsall));
view = Command_ExecHidden(buffer_name, cmd, working_dir); view = Command_ExecHidden(buffer_name, cmd, working_dir);
@@ -325,6 +325,7 @@ int main(int argc, char **argv)
InitArena(&Perm); InitArena(&Perm);
// Prototype(); // Prototype();
WorkDir = GetWorkingDir(Perm);
{ {
String sdl_config_path = SDL_GetPrefPath("krzosa", "text_editor"); String sdl_config_path = SDL_GetPrefPath("krzosa", "text_editor");
if (sdl_config_path.len && sdl_config_path.data[sdl_config_path.len - 1] == '\\') { 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 (EndsWith(it, ".project.lua")) {
if (!LuaProjectBuffer) { if (!LuaProjectBuffer) {
LuaProjectBuffer = BufferOpenFile(it); SetProjectFile(BufferOpenFile(it));
LuaProjectBuffer->user_change_id = -1;
} }
} else { } else {
Window *window = GetWindow({0}); Window *window = GetWindow({0});

View File

@@ -120,7 +120,6 @@ Array<Edit> Command_ReplaceEx(Allocator scratch, View *view, String16 string);
void Command_Eval(String string); void Command_Eval(String string);
void Command_Eval(String16 string); void Command_Eval(String16 string);
String Command_GetMainDir(); String Command_GetMainDir();
String Command_GetBaseDir();
void ReplaceWithoutMovingCarets(Buffer *buffer, Range range, String16 string); void ReplaceWithoutMovingCarets(Buffer *buffer, Range range, String16 string);
void Command_Copy(View *view); void Command_Copy(View *view);

View File

@@ -14,7 +14,7 @@ Window *CreateSearchBar(WindowID parent_window_id) {
static int BarCount; static int BarCount;
Allocator sys_allocator = GetSystemAllocator(); 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); Buffer *b = CreateBuffer(sys_allocator, name);
View *v = CreateView(b->id); View *v = CreateView(b->id);
@@ -37,7 +37,7 @@ Window *CreateTitlebar(WindowID parent_window_id) {
static int TitlebarCount; static int TitlebarCount;
Allocator sys_allocator = GetSystemAllocator(); 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); Buffer *b = CreateBuffer(sys_allocator, name);
View *v = CreateView(b->id); View *v = CreateView(b->id);
@@ -99,7 +99,6 @@ void SplitWindow(WindowSplitKind kind) {
Window *active_window = GetActiveWindow(); Window *active_window = GetActiveWindow();
SplitWindowEx(NULL, &WindowSplits, active_window, window, kind); SplitWindowEx(NULL, &WindowSplits, active_window, window, kind);
ActiveWindow = window->id; ActiveWindow = window->id;
} }
void SetVisibility(WindowID window_id, bool v) { void SetVisibility(WindowID window_id, bool v) {
@@ -205,7 +204,7 @@ void InitWindows() {
window->visible = false; window->visible = false;
window->z = 2; window->z = 2;
Buffer *buffer = CreateBuffer(sys_allocator, GetUniqueBufferName(Command_GetBaseDir(), "debug")); Buffer *buffer = CreateBuffer(sys_allocator, GetUniqueBufferName(WorkDir, "debug"));
DebugBufferID = buffer->id; DebugBufferID = buffer->id;
buffer->no_history = true; buffer->no_history = true;