From 30932b0d7c6d6b3bf298554cc6394cbcdb6d7839 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Wed, 24 Jul 2024 08:02:46 +0200 Subject: [PATCH] Listing open windows, views and buffers --- src/text_editor/commands_window.cpp | 6 +- src/text_editor/lua_api.cpp | 108 ++++++++++++++++++++++++++-- src/text_editor/text_editor.cpp | 26 +++---- src/text_editor/text_editor.h | 3 +- 4 files changed, 116 insertions(+), 27 deletions(-) diff --git a/src/text_editor/commands_window.cpp b/src/text_editor/commands_window.cpp index b9e3d0e..11c0320 100644 --- a/src/text_editor/commands_window.cpp +++ b/src/text_editor/commands_window.cpp @@ -549,8 +549,10 @@ void ReplaceInfobarData() { name = buffer->name; } - Scratch scratch; - String s = Format(scratch, "line: %5lld col: %5lld name: %.*s", (long long)xy.line + 1ll, (long long)xy.col + 1ll, FmtString(name)); + Scratch scratch; + String error = {}; + if (InfoBarErrorMessage.len) error = Format(scratch, "| error: %.*s", FmtString(InfoBarErrorMessage)); + String s = Format(scratch, "line: %5lld col: %5lld name: %.*s %.*s", (long long)xy.line + 1ll, (long long)xy.col + 1ll, FmtString(name), FmtString(error)); String16 string = ToString16(scratch, s); ReplaceText(buffer, {0, buffer->len}, string); } \ No newline at end of file diff --git a/src/text_editor/lua_api.cpp b/src/text_editor/lua_api.cpp index 0ff9fff..1935f2f 100644 --- a/src/text_editor/lua_api.cpp +++ b/src/text_editor/lua_api.cpp @@ -1,29 +1,123 @@ lua_State *LuaState = NULL; int LuaOpenFile(lua_State *L) { - const char *text = luaL_checkstring(L, 1); - - Window *window = GetWindow(LastActiveWindow); - View *view = ViewOpenFile(window, text); + const char *text = luaL_checkstring(L, 1); + Window *window = GetWindow(LastActiveWindow); + View *view = ViewOpenFile(window, text); SetActiveWindow(window->id); return 0; // number of results } +// struct ResolvedSet { +// Window *window; +// View *view; +// Buffer *buffer; +// }; + +// ResolvedSet GetActiveWindow() { +// if (ActiveWindow.id == CommandWindowID.id) { +// Window *window = GetWindow(LastActiveWindow); +// View *view = GetView(window->active_view); +// SetActiveWindow(window->id); +// Buffer *buffer = GetBuffer(view->buffer_id); +// return {window, view, buffer}; +// } + +// Window *window = GetWindow(ActiveWindow); +// View *view = GetView(window->active_view); +// Buffer *buffer = GetBuffer(view->buffer_id); +// return {window, view, buffer}; +// } + +int LuaListOpenBuffers(lua_State *L) { + Window *window = GetWindow(LastActiveWindow); + View *view = GetView(window->active_view); + SetActiveWindow(window->id); + Buffer *buffer = GetBuffer(view->buffer_id); + + Scratch scratch; + Array strings = {scratch}; + For(Buffers) { + String string = Format(scratch, "%.*s\n", FmtString(it.name)); + Add(&strings, ToString16(scratch, string)); + } + String16 string = Merge(scratch, strings, L"\n"); + Command_Replace(view, string); + return 0; +} + +int LuaListViews(lua_State *L) { + Window *window = GetWindow(LastActiveWindow); + View *view = GetView(window->active_view); + SetActiveWindow(window->id); + Buffer *buffer = GetBuffer(view->buffer_id); + + Scratch scratch; + Array strings = {scratch}; + For(Views) { + Buffer *buffer = GetBuffer(it.buffer_id); + String string = Format(scratch, "view = %lld buffer = %lld name = %.*s", (long long)it.id.id, (long long)buffer->id.id, FmtString(buffer->name)); + Add(&strings, ToString16(scratch, string)); + } + String16 string = Merge(scratch, strings, L"\n"); + Command_Replace(view, string); + return 0; +} + +int LuaListWindows(lua_State *L) { + Window *window = GetWindow(LastActiveWindow); + View *view = GetView(window->active_view); + SetActiveWindow(window->id); + Buffer *buffer = GetBuffer(view->buffer_id); + + Scratch scratch; + Array strings = {scratch}; + For(Windows) { + View *view = GetActiveView(&it); + Buffer *buffer = GetBuffer(view->buffer_id); + String string = Format(scratch, "window = %lld active_view = %lld buffer_name = %.*s", (long long)it.id.id, (long long)it.active_view.id, FmtString(buffer->name)); + Add(&strings, ToString16(scratch, string)); + ForItem(child_view_id, it.views) { + View *child_view = GetView(child_view_id); + Buffer *child_buffer = GetBuffer(child_view->buffer_id); + String child_string = Format(scratch, " view = %lld buffer = %lld name = %.*s", (long long)child_view->id.id, (long long)child_buffer->id.id, FmtString(child_buffer->name)); + Add(&strings, ToString16(scratch, child_string)); + } + } + String16 string = Merge(scratch, strings, L"\n"); + Command_Replace(view, string); + return 0; +} + void InitLua() { LuaState = luaL_newstate(); luaL_openlibs(LuaState); lua_pushcfunction(LuaState, LuaOpenFile); lua_setglobal(LuaState, "open"); + + lua_pushcfunction(LuaState, LuaListOpenBuffers); + lua_setglobal(LuaState, "list_buffers"); + + lua_pushcfunction(LuaState, LuaListViews); + lua_setglobal(LuaState, "list_views"); + + lua_pushcfunction(LuaState, LuaListWindows); + lua_setglobal(LuaState, "list_windows"); +} + +// @todo: also log to buffer or something +void SetInfoBarErrorMessage(String string) { + Allocator sys = GetSystemAllocator(); + if (InfoBarErrorMessage.data) Dealloc(sys, &InfoBarErrorMessage.data); + if (string.len) InfoBarErrorMessage = Copy(sys, string); } void EvalString(String16 string16) { - if (!LuaState) InitLua(); Scratch scratch; String string = ToString(scratch, string16); if (luaL_dostring(LuaState, string.data) != LUA_OK) { - // @todo: const char *text = lua_tostring(LuaState, -1); - printf("lua error: %s\n", text); + SetInfoBarErrorMessage(text); } } \ No newline at end of file diff --git a/src/text_editor/text_editor.cpp b/src/text_editor/text_editor.cpp index da6dcdf..1d06909 100644 --- a/src/text_editor/text_editor.cpp +++ b/src/text_editor/text_editor.cpp @@ -30,10 +30,10 @@ #include "lua_api.cpp" /* -- replace info bar with 2 windows - Save file (utf16->utf8) - reuse buffers!! - resize windows +- Change font size - command window - maybe use lua and have there be lua commands that you choose with cursor - open "asd/asd/asd/asd" @@ -80,6 +80,7 @@ int main(void) { FontLineSpacing = FontSize; MainFont = LoadFontEx("c:\\Windows\\Fonts\\consola.ttf", (int)FontSize, NULL, 500); FontCharSpacing = GetCharSpacing(MainFont, FontSize, FontSpacing); + InitLua(); Allocator sys_allocator = GetSystemAllocator(); // Create null @@ -123,16 +124,7 @@ int main(void) { AddView(w, v->id); InfoBarWindowID = w->id; } - { - Window *w = CreateWindow(); - w->draw_scrollbar = false; - w->draw_line_numbers = false; - w->execute_line = true; - Buffer *b = CreateBuffer(sys_allocator, "*execbar*"); - View *v = CreateView(b->id); - AddView(w, v->id); - ExecBarWindowID = w->id; - } + { Window *w = CreateWindow(); w->draw_scrollbar = false; @@ -179,18 +171,18 @@ int main(void) { if (Windows[i].draw_scrollbar) Windows[i].scrollbar_rect = CutRight(&Windows[i].document_rect, 10); if (Windows[i].draw_line_numbers) Windows[i].line_numbers_rect = CutLeft(&Windows[i].document_rect, (Int)line_numbers_size); } + // { + // int i = 3; + // Windows[i].total_rect = CutLeft(&infobar_rect, GetSize(infobar_rect).x / 2); + // Windows[i].document_rect = Windows[i].total_rect; + // } { int i = 3; - Windows[i].total_rect = CutLeft(&infobar_rect, GetSize(infobar_rect).x / 2); - Windows[i].document_rect = Windows[i].total_rect; - } - { - int i = 4; Windows[i].total_rect = infobar_rect; Windows[i].document_rect = Windows[i].total_rect; } { - int i = 5; + int i = 4; Rect2 screen_rect = GetScreenRectF(); Vec2 size = GetSize(screen_rect); CutTop(&screen_rect, size.y * 0.05f); diff --git a/src/text_editor/text_editor.h b/src/text_editor/text_editor.h index 2d871bb..f9733b6 100644 --- a/src/text_editor/text_editor.h +++ b/src/text_editor/text_editor.h @@ -86,7 +86,7 @@ BufferID NullBufferID = {0}; ViewID NullViewID = {0}; WindowID CommandWindowID = {0}; WindowID InfoBarWindowID = {0}; -WindowID ExecBarWindowID = {0}; +// WindowID ExecBarWindowID = {0}; Array Buffers = {}; Array Views = {}; @@ -107,6 +107,7 @@ Int LastFrameIDWhenScrolled; WindowID LastActiveWindow; Int LastFrameIDWhenSwitchedActiveWindow; +String InfoBarErrorMessage; void EvalString(String16 string16); Rect2I GetVisibleCells(Window &window);