"New" command better, GetUniqueBufferName no longer big numbers

This commit is contained in:
Krzosa Karol
2025-05-10 08:12:49 +02:00
parent 8370a2ae67
commit c139c44503
8 changed files with 48 additions and 26 deletions

View File

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

View File

@@ -659,6 +659,9 @@ void OnCommand(Event event) {
}
}
if (CtrlPress(SDLK_N)) {
Command_New();
}
if (CtrlPress(SDLK_S)) {
SaveBuffer(active.buffer);

View File

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

View File

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

View File

@@ -49,13 +49,16 @@ Array<String16> 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;
}

View File

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

View File

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

View File

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