From c139c44503d337cdcf87e23dde4d4fbcad70841a Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Sat, 10 May 2025 08:12:49 +0200 Subject: [PATCH] "New" command better, GetUniqueBufferName no longer big numbers --- src/text_editor/commands.cpp | 31 +++++++++++++++++++++++++++ src/text_editor/commands_bindings.cpp | 3 +++ src/text_editor/lua_api.cpp | 15 ------------- src/text_editor/lua_api_generated.cpp | 2 +- src/text_editor/management.cpp | 19 +++++++++------- src/text_editor/text_editor.h | 1 + src/text_editor/title_bar.cpp | 2 +- src/text_editor/todo.txt | 1 - 8 files changed, 48 insertions(+), 26 deletions(-) diff --git a/src/text_editor/commands.cpp b/src/text_editor/commands.cpp index 5797924..83c2e91 100644 --- a/src/text_editor/commands.cpp +++ b/src/text_editor/commands.cpp @@ -954,6 +954,37 @@ int Lua_Reopen(lua_State *L) { return 0; } +void New(Window *window, String name = "") { + View *view = GetView(window->active_view); + Buffer *buffer = GetBuffer(view->active_buffer); + + Scratch scratch; + String dir = GetDir(buffer); + if (name != "") { + if (!IsAbsolute(name)) { + name = Format(scratch, "%.*s/%.*s", FmtString(dir), FmtString(name)); + } + name = GetAbsolutePath(scratch, name); + } else { + name = GetUniqueBufferName(dir, "new"); + } + WindowOpenBufferView(window, name); +} + +void Command_New(String name = "") { + BSet main = GetActiveMainSet(); + New(main.window, name); +} + +int Lua_New(lua_State *L) { + Scratch scratch; + String name = lua_tostring(L, 1); + lua_pop(L, 1); + Command_New(name); + return 0; +} + + void Command_ToggleFullscreen() { if (IsInFullscreen) { SDL_SetWindowSize(SDLWindow, FullScreenSizeX, FullScreenSizeY); diff --git a/src/text_editor/commands_bindings.cpp b/src/text_editor/commands_bindings.cpp index 86f20f9..1f76865 100644 --- a/src/text_editor/commands_bindings.cpp +++ b/src/text_editor/commands_bindings.cpp @@ -659,6 +659,9 @@ void OnCommand(Event event) { } } + if (CtrlPress(SDLK_N)) { + Command_New(); + } if (CtrlPress(SDLK_S)) { SaveBuffer(active.buffer); diff --git a/src/text_editor/lua_api.cpp b/src/text_editor/lua_api.cpp index 9bef680..5f5d6c1 100644 --- a/src/text_editor/lua_api.cpp +++ b/src/text_editor/lua_api.cpp @@ -30,21 +30,6 @@ int Lua_GetLoadWord(lua_State *L) { return 1; } -int Lua_New(lua_State *L) { - Scratch scratch; - String name = luaL_checkstring(L, 1); - lua_pop(L, 1); - - BSet main = GetActiveMainSet(); - if (!IsAbsolute(name)) { - String dir = GetDir(main.buffer); - name = Format(scratch, "%.*s/%.*s", FmtString(dir), FmtString(name)); - } - WindowOpenBufferView(main.window, name); - - return 0; -} - int Lua_BufferExists(lua_State *L) { String string = lua_tostring(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 e5c5d8e..8f81678 100644 --- a/src/text_editor/lua_api_generated.cpp +++ b/src/text_editor/lua_api_generated.cpp @@ -2,7 +2,6 @@ luaL_Reg LuaFunctions[] = { {"Print", Lua_Print}, {"Kill", Lua_Kill}, {"GetLoadWord", Lua_GetLoadWord}, - {"New", Lua_New}, {"BufferExists", Lua_BufferExists}, {"GetSelection", Lua_GetSelection}, {"GetEntireBuffer", Lua_GetEntireBuffer}, @@ -20,6 +19,7 @@ luaL_Reg LuaFunctions[] = { {"GetBufferNameByID", Lua_GetBufferNameByID}, {"Save", Lua_Save}, {"Reopen", Lua_Reopen}, + {"New", Lua_New}, {"ToggleFullscreen", Lua_ToggleFullscreen}, {"GetCFiles", Lua_GetCFiles}, {"C", Lua_C}, diff --git a/src/text_editor/management.cpp b/src/text_editor/management.cpp index a9bcaee..9a904b9 100644 --- a/src/text_editor/management.cpp +++ b/src/text_editor/management.cpp @@ -49,13 +49,16 @@ Array SavedClipboardCarets; String GetUniqueBufferName(String working_dir, String prepend_name) { - if (UniqueBufferNameSeed.a == 0) { - double value = get_time_in_micros(); - UniqueBufferNameSeed.a = *(uint64_t *)&value; - } Scratch scratch; - uint64_t number = GetRandomU64(&UniqueBufferNameSeed); - String buffer_name = Format(scratch, "%.*s/%.*s%llu.log", FmtString(working_dir), FmtString(prepend_name), number); + String buffer_name = {}; + for (int i = 1; i < INT_MAX; i += 1) { + buffer_name = Format(scratch, "%.*s/%.*s%d.log", FmtString(working_dir), FmtString(prepend_name), i); + buffer_name = GetAbsolutePath(scratch, buffer_name); + Buffer *exists = FindBuffer(buffer_name); + if (!exists) { + break; + } + } buffer_name = Intern(&GlobalInternTable, buffer_name); return buffer_name; } @@ -102,7 +105,7 @@ inline Buffer *GetBuffer(BufferID id) { return FirstBuffer; } -inline Buffer *GetBufferStrict(BufferID id) { +inline Buffer *FindBuffer(BufferID id) { for (Buffer *it = FirstBuffer; it; it = it->next) { if (it->id == id) return it; } @@ -116,7 +119,7 @@ inline Buffer *GetBuffer(String name) { return FirstBuffer; } -inline Buffer *GetBufferStrict(String name) { +inline Buffer *FindBuffer(String name) { for (Buffer *it = FirstBuffer; it; it = it->next) { if (it->name == name) return it; } diff --git a/src/text_editor/text_editor.h b/src/text_editor/text_editor.h index a4ba82c..561189a 100644 --- a/src/text_editor/text_editor.h +++ b/src/text_editor/text_editor.h @@ -138,6 +138,7 @@ void Command_Appendf(View *view, const char *fmt, ...); Buffer *CreateBuffer(Allocator allocator, String name, Int size = 4096); View *CreateView(BufferID active_buffer); void ReopenBuffer(Buffer *buffer); +inline Buffer *FindBuffer(String name); inline bool operator==(WindowID a, WindowID b) { return a.id == b.id; } inline bool operator==(BufferID a, BufferID b) { return a.id == b.id; } diff --git a/src/text_editor/title_bar.cpp b/src/text_editor/title_bar.cpp index 3396e6e..01696a9 100644 --- a/src/text_editor/title_bar.cpp +++ b/src/text_editor/title_bar.cpp @@ -48,7 +48,7 @@ void ReplaceTitleBarData(Window *window) { String name = ToString(scratch, buffer_name); if (name != main.buffer->name) { name = GetAbsolutePath(scratch, name); - if (GetBufferStrict(name)) { + if (FindBuffer(name)) { title.window->title_bar_last_buffer_change_id = title.buffer->change_id; ReportConsolef("there is already buffer with name: %.*s", FmtString(name)); return; diff --git a/src/text_editor/todo.txt b/src/text_editor/todo.txt index f1b23ed..15840e3 100644 --- a/src/text_editor/todo.txt +++ b/src/text_editor/todo.txt @@ -15,7 +15,6 @@ - LoadWord, EncloseWord configurable? - dump text editor state to file, restore state - help menu popup when for example in process buffer, on tile bar buffer and stuff like that -- report errors (try showing a window) - proper lister - Setting size of splits