diff --git a/src/text_editor/commands.cpp b/src/text_editor/commands.cpp index e1dd193..bcf5036 100644 --- a/src/text_editor/commands.cpp +++ b/src/text_editor/commands.cpp @@ -162,6 +162,29 @@ void MouseExecWord(Event event) { } } +void Command_ListBuffers() { + Window *window = GetActiveMainWindow(); + View *view = GetView(window->active_view); + Buffer *buffer = GetBuffer(view->active_buffer); + CheckpointBeforeGoto(window->id); + + Scratch scratch; + Array strings = {scratch}; + For(Buffers) { + String string = Format(scratch, "%.*s id=%d is_dir=%d", FmtString(it.o->name), (int)it.id, it.o->is_directory); + Add(&strings, string); + } + String result = Merge(scratch, strings, "\n"); + String16 string16 = ToString16(scratch, result); + + String buffer_name = GetUniqueBufferName(scratch, GetDir(buffer), "+list_buffers-"); + View *new_view = WindowOpenBufferView(window, buffer_name); + + Command_SelectEntireBuffer(new_view); + Command_Replace(new_view, string16); + Command_SelectRangeOneCursor(new_view, {}); +} + void MouseLoadWord(Event event) { Vec2I mouse = MouseVec2I(); Window *window = GetActiveWindow(); @@ -419,6 +442,10 @@ bool GlobalCommand(Event event) { } } + if (Ctrl(SDLK_P)) { + Command_ListBuffers(); + } + if (CtrlShift(SDLK_BACKSLASH)) { AddRowWindow(); } else if (Ctrl(SDLK_BACKSLASH)) { diff --git a/src/text_editor/lua_api.cpp b/src/text_editor/lua_api.cpp index f483468..de874dc 100644 --- a/src/text_editor/lua_api.cpp +++ b/src/text_editor/lua_api.cpp @@ -28,13 +28,6 @@ String FieldString(lua_State *L, String name) { return result; } -RandomSeed UniqueBufferNameSeed = {13}; -String GetUniqueBufferName(Allocator allocator, String working_dir, String prepend_name) { - uint64_t number = GetRandomU64(&UniqueBufferNameSeed); - String buffer_name = Format(allocator, "%.*s/%.*s%llu", FmtString(working_dir), FmtString(prepend_name), number); - return buffer_name; -} - void ExecInNewBuffer(String cmd, String working_dir) { Scratch scratch; CheckpointBeforeGoto(GetActiveMainWindowID()); @@ -144,27 +137,7 @@ int Lua_Print(lua_State *L) { } int Lua_ListBuffers(lua_State *L) { - Window *window = GetActiveMainWindow(); - View *view = GetView(window->active_view); - Buffer *buffer = GetBuffer(view->active_buffer); - CheckpointBeforeGoto(window->id); - - Scratch scratch; - Array strings = {scratch}; - For(Buffers) { - String string = Format(scratch, "%.*s id=%d is_dir=%d", FmtString(it.o->name), (int)it.id, it.o->is_directory); - Add(&strings, string); - } - String result = Merge(scratch, strings, "\n"); - String16 string16 = ToString16(scratch, result); - - String buffer_name = GetUniqueBufferName(scratch, GetDir(buffer), "+list_buffers-"); - View *new_view = WindowOpenBufferView(window, buffer_name); - - Command_SelectEntireBuffer(new_view); - Command_Replace(new_view, string16); - Command_SelectRangeOneCursor(new_view, {}); - + Command_ListBuffers(); return 0; } diff --git a/src/text_editor/management.cpp b/src/text_editor/management.cpp index 25c28a0..d0b4044 100644 --- a/src/text_editor/management.cpp +++ b/src/text_editor/management.cpp @@ -186,6 +186,13 @@ WindowID GetActiveMainWindowID() { return GetActiveMainWindow()->id; } +RandomSeed UniqueBufferNameSeed = {13}; +String GetUniqueBufferName(Allocator allocator, String working_dir, String prepend_name) { + uint64_t number = GetRandomU64(&UniqueBufferNameSeed); + String buffer_name = Format(allocator, "%.*s/%.*s%llu", FmtString(working_dir), FmtString(prepend_name), number); + return buffer_name; +} + String GetActiveMainWindowBufferName() { Window *window = GetActiveMainWindow(); View *view = GetView(window->active_view); diff --git a/src/text_editor/todo.txt b/src/text_editor/todo.txt index 54489dc..b9359bf 100644 --- a/src/text_editor/todo.txt +++ b/src/text_editor/todo.txt @@ -1,8 +1,6 @@ - garbage collect the buffers, views - store them in free list on destroy -- Remove console and command window, provide alternatives but unify the interface? -- We can create 2 buffers with same name -- window splitting leaves a whitegap at the end - PageUp for some reason stops at 2 line before last line +- adding items to directory should create files on save - it should ask the user (syntax: dir/ | file) - apply clang format - apply clang format on save