From 85ca1a6a9e755ec5fcebad45e91e52792ef8bb8f Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Sun, 14 Dec 2025 17:32:48 +0100 Subject: [PATCH] RegisterCommand --- src/backup/todo.txt | 5 ----- src/text_editor/commands.cpp | 35 ++++++++++++++++++----------------- src/text_editor/globals.cpp | 17 ++++++++++++++++- 3 files changed, 34 insertions(+), 23 deletions(-) diff --git a/src/backup/todo.txt b/src/backup/todo.txt index 0e80367..155bed4 100644 --- a/src/backup/todo.txt +++ b/src/backup/todo.txt @@ -26,11 +26,6 @@ Splits: - move titlebar, search to splits? -Linux -- Add backtrace -- Use DPI well, use as much resolution but scale with DPI - - Commands TODO: - Search - Ctrl + F diff --git a/src/text_editor/commands.cpp b/src/text_editor/commands.cpp index 833d1b9..2f1ef6f 100644 --- a/src/text_editor/commands.cpp +++ b/src/text_editor/commands.cpp @@ -705,7 +705,7 @@ void SaveBuffer(Buffer *buffer) { void Command_Save() { BSet active = GetBSet(LastActiveLayoutWindowID); SaveBuffer(active.buffer); -} +} RegisterCommand(Command_Save); int Lua_Save(lua_State *L) { Command_Save(); @@ -718,7 +718,7 @@ void Command_SaveAll() { SaveBuffer(it); } } -} +} RegisterCommand(Command_SaveAll); int Lua_SaveAll(lua_State *L) { Command_SaveAll(); @@ -992,7 +992,7 @@ void Command_Reopen() { BSet main = GetBSet(LastActiveLayoutWindowID); ReopenBuffer(main.buffer); NextActiveWindowID = main.window->id; -} +} RegisterCommand(Command_Reopen); int Lua_Reopen(lua_State *L) { Command_Reopen(); @@ -1016,15 +1016,16 @@ void New(Window *window, String name = "") { WindowOpenBufferView(window, name); } -void Command_New(String name = "") { +void Command_New() { BSet main = GetBSet(LastActiveLayoutWindowID); - New(main.window, name); -} + New(main.window, ""); +} RegisterCommand(Command_New); int Lua_New(lua_State *L) { String name = lua_tostring(L, 1); lua_pop(L, 1); - Command_New(name); + BSet main = GetBSet(LastActiveLayoutWindowID); + New(main.window, name); return 0; } @@ -1070,7 +1071,7 @@ void Command_ToggleFullscreen() { } IsInFullscreen = !IsInFullscreen; -} +} RegisterCommand(Command_ToggleFullscreen); int Lua_ToggleFullscreen(lua_State *L) { Command_ToggleFullscreen(); @@ -1102,7 +1103,7 @@ void Command_ListCode() { main.view->fuzzy_search = true; main.view->update_scroll = true; SelectRange(main.view, GetBufferEndAsRange(main.buffer)); -} +} RegisterCommand(Command_ListCode); int Lua_ListCode(lua_State *L) { Command_ListCode(); @@ -1252,7 +1253,7 @@ void Command_ShowBufferList() { } command_bar.view->update_scroll = true; SelectRange(command_bar.view, GetBufferEndAsRange(command_bar.buffer)); -} +} RegisterCommand(Command_ShowBufferList); void Command_ShowCommandList() { BSet command_bar = GetBSet(CommandBarWindowID); @@ -1265,7 +1266,7 @@ void Command_ShowCommandList() { command_bar.view->update_scroll = true; SelectRange(command_bar.view, GetBufferEndAsRange(command_bar.buffer)); -} +} RegisterCommand(Command_ShowCommandList); void Command_ListViews() { BSet command_bar = GetBSet(CommandBarWindowID); @@ -1279,7 +1280,7 @@ void Command_ListViews() { command_bar.view->fuzzy_search = true; command_bar.view->update_scroll = true; SelectRange(command_bar.view, GetBufferEndAsRange(command_bar.buffer)); -} +} RegisterCommand(Command_ListViews); int Lua_ListViews(lua_State *L) { Command_ListViews(); @@ -1316,7 +1317,7 @@ void SetProjectFile(Buffer *buffer) { void Command_SetProjectFile() { BSet main = GetBSet(LastActiveLayoutWindowID); SetProjectFile(main.buffer); -} +} RegisterCommand(Command_SetProjectFile); void Command_SetWorkDir() { String dir = lua_tostring(LuaState, -1); @@ -1331,7 +1332,7 @@ void Command_SetWorkDir() { void Command_SetProject() { Command_SetWorkDir(); Command_SetProjectFile(); -} +} RegisterCommand(Command_SetProject); int Lua_SetProjectFile(lua_State *L) { Command_SetProjectFile(); @@ -1419,12 +1420,12 @@ String16 FetchLoadWord(void) { void Command_ToggleDebug() { Window *window = GetWindow(DebugWindowID); window->visible = !window->visible; -} +} RegisterCommand(Command_ToggleDebug); void Command_KillProcess() { BSet main = GetBSet(LastActiveLayoutWindowID); KillProcess(main.view); -} +} RegisterCommand(Command_KillProcess); int Lua_KillProcess(lua_State *L) { Command_KillProcess(); @@ -1434,7 +1435,7 @@ int Lua_KillProcess(lua_State *L) { void Command_KillWindow() { BSet main = GetBSet(LastActiveLayoutWindowID); main.window->kill = true; -} +} RegisterCommand(Command_KillWindow); int Lua_KillWindow(lua_State *L) { Command_KillWindow(); diff --git a/src/text_editor/globals.cpp b/src/text_editor/globals.cpp index d334c05..5f1af01 100644 --- a/src/text_editor/globals.cpp +++ b/src/text_editor/globals.cpp @@ -184,4 +184,19 @@ void ReloadStyle() { StyleFont = GetStyleString("Font", StyleFont); StyleVCVarsall = GetStyleString("VCVarsall", StyleVCVarsall); StyleUndoMergeTimeout = GetStyleFloat("UndoMergeTimeout", StyleUndoMergeTimeout); -} \ No newline at end of file +} + +typedef void CommandFunction(void); +Array CommandFunctions; + +#define STRINGIFY_(x) x +#define STRINGIFY(x) STRINGIFY_(x) +#define CONCAT_(a, b) a ## b +#define CONCAT(a, b) CONCAT_(a, b) + +struct Register_Command { + Register_Command(CommandFunction *f) { + Add(&CommandFunctions, f); + } +}; +#define RegisterCommand(NAME) Register_Command CONCAT(COMMAND, __COUNTER__)(NAME) \ No newline at end of file