diff --git a/src/text_editor/commands.cpp b/src/text_editor/commands.cpp index 6747898..dbeee19 100644 --- a/src/text_editor/commands.cpp +++ b/src/text_editor/commands.cpp @@ -76,29 +76,6 @@ Int ScreenSpaceToBufferPosErrorOutOfBounds(Window *window, View *view, Buffer *b return result; } -void Command_ListBuffers() { - BSet main = GetActiveMainSet(); - CheckpointBeforeGoto(main.window); - - Scratch scratch; - Array strings = {scratch}; - for (Buffer *it = FirstBuffer; it; it = it->next) { - String string = Format(scratch, "%.*s", FmtString(it->name)); - Add(&strings, string); - } - String result = Merge(scratch, strings, "\n"); - String16 string16 = ToString16(scratch, result); - - String buffer_name = GetUniqueBufferName(GetDir(main.buffer), "buffer_list"); - View *new_view = WindowOpenBufferView(main.window, buffer_name); - Buffer *new_buffer = GetBuffer(new_view->active_buffer); - new_buffer->gc = true; - - Command_SelectEntireBuffer(new_view); - Command_Replace(new_view, string16); - Command_SelectRangeOneCursor(new_view, {}); -} - void MouseLoadWord(Event event, void (*cmd_function)(String16 string)) { Vec2I mouse = MouseVec2I(); BSet active = GetActiveSet(); diff --git a/src/text_editor/commands_bindings.cpp b/src/text_editor/commands_bindings.cpp index 23319dd..38cb40a 100644 --- a/src/text_editor/commands_bindings.cpp +++ b/src/text_editor/commands_bindings.cpp @@ -412,6 +412,7 @@ void OnCommand(Event event) { if (CtrlPress(SDLK_P)) { + void Command_ListBuffers(); Command_ListBuffers(); } diff --git a/src/text_editor/lua_api.cpp b/src/text_editor/lua_api.cpp index ba65d63..2cc1fcb 100644 --- a/src/text_editor/lua_api.cpp +++ b/src/text_editor/lua_api.cpp @@ -40,23 +40,41 @@ View *Command_ExecHidden(String buffer_name, String cmd, String working_dir) { return view; } -void Command_BeginConsoleJump(BSet *set) { +void Command_BeginJump(BSet *set, BufferID buffer_id = NullBufferID) { CheckpointBeforeGoto(set->window); - set->buffer = GetBuffer(NullBufferID); + set->buffer = GetBuffer(buffer_id); set->view = WindowOpenBufferView(set->window, set->buffer->name); } -void Command_EndConsoleJump(BSet set) { +void Command_BeginJumpCommandBuffer(BSet *set) { + String current_dir = Command_GetDir(); + Command_BeginJump(set, set->window->command_buffer); + set->buffer->name = GetUniqueBufferName(current_dir, WindowBufferName); + Command_SelectEntireBuffer(set->view); + Command_Replace(set->view, u""); +} + +void Command_EndJump(BSet set) { Int pos = XYToPos(set.buffer, {0, set.buffer->line_starts.len - 1}); set.view->carets[0] = MakeCaret(pos); UpdateScroll(set.window, true); } +void Command_ListBuffers() { + BSet main = GetActiveMainSet(); + Command_BeginJumpCommandBuffer(&main); + for (Buffer *it = FirstBuffer; it; it = it->next) { + Command_Appendf(main.view, "%.*s\n", FmtString(it->name)); + } + Command_EndJump(main); + ActiveWindow = main.window->id; +} + BSet Command_Exec(String cmd, String working_dir) { BSet set = GetActiveMainSet(); - Command_BeginConsoleJump(&set); + Command_BeginJumpCommandBuffer(&set); Exec(set.view->id, true, cmd, working_dir); - Command_EndConsoleJump(set); + Command_EndJump(set); ActiveWindow = set.window->id; return set; } @@ -83,12 +101,12 @@ BSet Command_Open(String path) { Int col = strtoll(col_string.data, NULL, 10); if (IsDir(file_path)) { - Command_BeginConsoleJump(&main); + Command_BeginJumpCommandBuffer(&main); Command_Appendf(main.view, "%.*s/..\n", FmtString(file_path)); for (FileIter it = IterateFiles(scratch, file_path); IsValid(it); Advance(&it)) { Command_Appendf(main.view, "%.*s\n", FmtString(it.absolute_path)); } - Command_EndConsoleJump(main); + Command_EndJump(main); ActiveWindow = main.window->id; } else { CheckpointBeforeGoto(main.window); @@ -179,17 +197,16 @@ int Lua_Cmd(lua_State *L) { String destination = lua_tostring(L, -1); lua_pop(L, 1); - BSet set = {}; if (destination == "console") { - set = GetConsoleSet(); + BSet set = GetConsoleSet(); + Command_BeginJump(&set); + Exec(set.view->id, true, cmd, working_dir); + Command_EndJump(set); } else { - set = GetActiveMainSet(); - } - - Command_BeginConsoleJump(&set); - Exec(set.view->id, true, cmd, working_dir); - Command_EndConsoleJump(set); - if (destination != "console") { + BSet set = GetActiveMainSet(); + Command_BeginJumpCommandBuffer(&set); + Exec(set.view->id, true, cmd, working_dir); + Command_EndJump(set); ActiveWindow = set.window->id; } @@ -260,14 +277,14 @@ int Lua_SetProjectFile(lua_State *L) { int Lua_ListCommands(lua_State *L) { BSet main = GetActiveMainSet(); - Command_BeginConsoleJump(&main); + Command_BeginJump(&main); for (int i = 0; LuaFunctions[i].name != NULL; i += 1) { Command_Appendf(main.view, "%20s() ", LuaFunctions[i].name); if (((i + 1) % 6) == 0) { Command_Appendf(main.view, "\n"); } } - Command_EndConsoleJump(main); + Command_EndJump(main); ActiveWindow = main.window->id; return 0; } diff --git a/src/text_editor/management.cpp b/src/text_editor/management.cpp index 5baacf2..78be7d4 100644 --- a/src/text_editor/management.cpp +++ b/src/text_editor/management.cpp @@ -146,12 +146,16 @@ Buffer *CreateTempBuffer(Allocator allocator, Int size = 4096) { return result; } -Window *CreateWindow() { +String WindowBufferName = "window_buffer"; +Window *CreateWindow(bool create_command_buffer = true) { Window *w = AllocType(Perm, Window); w->visible = true; w->draw_scrollbar = StyleDrawScrollbar; w->draw_line_numbers = StyleDrawLineNumbers; w->id = AllocWindowID(w); + if (create_command_buffer) { + w->command_buffer = CreateBuffer(GetSystemAllocator(), GetUniqueBufferName(WorkingDir, WindowBufferName), 4096*2)->id; + } DLL_QUEUE_ADD(FirstWindow, LastWindow, w); WindowCount += 1; return w; diff --git a/src/text_editor/text_editor.h b/src/text_editor/text_editor.h index 04254b1..f34bf77 100644 --- a/src/text_editor/text_editor.h +++ b/src/text_editor/text_editor.h @@ -42,6 +42,7 @@ struct Window { Array goto_redo; ViewID active_goto_list; + BufferID command_buffer; double mouse_scroller_offset; int z; diff --git a/src/text_editor/todo.txt b/src/text_editor/todo.txt index 907e83c..7f4c87e 100644 --- a/src/text_editor/todo.txt +++ b/src/text_editor/todo.txt @@ -1,9 +1,9 @@ !!As little lua code as possible, but lua code should be powerful just in case of quick edits - maybe we could allow user to change window titles which would make them special in some way. A:/text_editor/+test_buffer:1:1:ADE | -- Fix B: not opening +- need a buffer for every window that will be cleared on command and replaced with new content + - buffer_list shouldn't be a thing - Scroll the console properly - commands for scrolling: center, cursor_at_bottom_of_screen, cursor_at_top -- need a buffer for every window that will be cleared on command and replaced with new content -------------- buffer = make_buffer() diff --git a/src/text_editor/window.cpp b/src/text_editor/window.cpp index 248d25c..3538bb1 100644 --- a/src/text_editor/window.cpp +++ b/src/text_editor/window.cpp @@ -7,7 +7,7 @@ Array GetWindowZOrder(Allocator allocator) { } Window *CreateSearchBar(WindowID parent_window_id) { - Window *window = CreateWindow(); + Window *window = CreateWindow(false); window->draw_scrollbar = false; window->deactivate_on_escape = true; window->is_search_bar = true; @@ -30,7 +30,7 @@ Window *CreateSearchBar(WindowID parent_window_id) { } Window *CreateTitlebar(WindowID parent_window_id) { - Window *window = CreateWindow(); + Window *window = CreateWindow(false); window->draw_scrollbar = false; window->deactivate_on_escape = true; window->is_title_bar = true;