From f63735266929ca4cb6fbd177f28e037acca68aa4 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Sat, 20 Dec 2025 11:37:02 +0100 Subject: [PATCH] RegisterVariable --- src/basic/basic_head.h | 1 + src/text_editor/globals.cpp | 143 +++++++++------------------------- src/text_editor/lua.cpp | 10 ++- src/text_editor/text_editor.h | 70 ++++++++++++++++- 4 files changed, 114 insertions(+), 110 deletions(-) diff --git a/src/basic/basic_head.h b/src/basic/basic_head.h index 79b3246..c09e3fe 100644 --- a/src/basic/basic_head.h +++ b/src/basic/basic_head.h @@ -102,6 +102,7 @@ using S32 = int32_t; using S64 = int64_t; using Int = S64; using UInt = U64; +using Float = double; template T Min(T a, T b) { diff --git a/src/text_editor/globals.cpp b/src/text_editor/globals.cpp index b5abfed..c7963f6 100644 --- a/src/text_editor/globals.cpp +++ b/src/text_editor/globals.cpp @@ -77,6 +77,12 @@ String Intern(InternTable *table, String string) { // optimize worst offenders (like event text) InternTable GlobalInternTable; +bool SkipRemainingCommands; +Array CommandFunctions; +Array LuaFunctions; +Array TestFunctions; +Array Variables; + /////////////////////////////// // CONFIG Color GruvboxDark0Hard = {0x1d, 0x20, 0x21, 0xff}; @@ -116,109 +122,34 @@ Color GruvboxFadedBlue = {0x07, 0x66, 0x78, 0xff}; Color GruvboxFadedPurple = {0x8f, 0x3f, 0x71, 0xff}; Color GruvboxFadedAqua = {0x42, 0x7b, 0x58, 0xff}; Color GruvboxFadedOrange = {0xaf, 0x3a, 0x03, 0xff}; -Color ColorText = GruvboxDark0Hard; -Color ColorLoadTextHighlight = {0x00, 0x00, 0x00, 0x0F}; -Color ColorBackground = GruvboxLight0Hard; -Color ColorInactiveWindow = {0x00, 0x00, 0x00, 0x0F}; -Color ColorTextLineNumbers = GruvboxDark4; -Color ColorLineHighlight = GruvboxLight0Soft; -Color ColorMainCaret = GruvboxDark0Hard; -Color ColorSubCaret = GruvboxGray245; -Color ColorSelection = GruvboxLight1; -Color ColorWhitespaceDuringSelection = GruvboxLight4; -Color ColorMouseUnderline = GruvboxDark0Hard; -Color ColorCaretUnderline = GruvboxGray245; -Color ColorFuzzySearchLineHighlight = GruvboxDark0; -Color ColorScrollbarBackground = GruvboxLight2; -Color ColorScrollbarScroller = GruvboxLight1; -Color ColorScrollbarScrollerSelected = GruvboxLight0Hard; -Color ColorTitleBarText = GruvboxDark2; -Color ColorTitleBarBackground = GruvboxLight1; -Color ColorTitleBarActiveBackground = {0xfe, 0xfe, 0xfe, 0xfe}; -Color ColorTitleBarSelection = GruvboxLight3; -Color ColorResizerBackground = GruvboxLight0Hard; -Color ColorResizerOutline = GruvboxLight3; -Int StyleWaitForEvents = 1; -Int StyleDrawLineNumbers = 1; -Int StyleDrawScrollbar = 1; -Int StyleIndentSize = 4; -Int StyleFontSize = 15; -Int StyleFontFilter = 0; -String StyleFont = "/home/krz/text_editor/package/CascadiaMono.ttf"; -String StyleVCVarsall = "C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Auxiliary/Build/vcvars64.bat"; -double StyleUndoMergeTimeout = 0.3; - -void ReloadStyle() { - ColorText = GetColor("Text", ColorText); - ColorLoadTextHighlight = GetColor("LoadTextHighlight", ColorLoadTextHighlight); - ColorBackground = GetColor("Background", ColorBackground); - ColorInactiveWindow = GetColor("InactiveWindow", ColorInactiveWindow); - ColorTextLineNumbers = GetColor("TextLineNumbers", ColorTextLineNumbers); - ColorLineHighlight = GetColor("LineHighlight", ColorLineHighlight); - ColorMainCaret = GetColor("MainCaret", ColorMainCaret); - ColorSubCaret = GetColor("SubCaret", ColorSubCaret); - ColorSelection = GetColor("Selection", ColorSelection); - ColorWhitespaceDuringSelection = GetColor("WhitespaceDuringSelection", ColorWhitespaceDuringSelection); - ColorMouseUnderline = GetColor("MouseUnderline", ColorMouseUnderline); - ColorCaretUnderline = GetColor("CaretUnderline", ColorCaretUnderline); - ColorFuzzySearchLineHighlight = GetColor("FuzzySearchLineHighlight", ColorFuzzySearchLineHighlight); - ColorScrollbarBackground = GetColor("ScrollbarBackground", ColorScrollbarBackground); - ColorScrollbarScroller = GetColor("ScrollbarScroller", ColorScrollbarScroller); - ColorScrollbarScrollerSelected = GetColor("ScrollbarScrollerSelected", ColorScrollbarScrollerSelected); - ColorTitleBarText = GetColor("TitleBarText", ColorTitleBarText); - ColorTitleBarBackground = GetColor("TitleBarBackground", ColorTitleBarBackground); - ColorTitleBarActiveBackground = GetColor("TitleBarActiveBackground", ColorTitleBarActiveBackground); - ColorTitleBarSelection = GetColor("TitleBarSelection", ColorTitleBarSelection); - ColorResizerBackground = GetColor("ResizerBackground", ColorResizerBackground); - ColorResizerOutline = GetColor("ResizerOutline", ColorResizerOutline); - StyleWaitForEvents = GetStyleInt("WaitForEvents", StyleWaitForEvents); - StyleDrawLineNumbers = GetStyleInt("DrawLineNumbers", StyleDrawLineNumbers); - StyleDrawScrollbar = GetStyleInt("DrawScrollbar", StyleDrawScrollbar); - StyleIndentSize = GetStyleInt("IndentSize", StyleIndentSize); - StyleFontSize = GetStyleInt("FontSize", StyleFontSize); - StyleFontFilter = GetStyleInt("FontFilter", StyleFontFilter); - StyleFont = GetStyleString("Font", StyleFont); - StyleVCVarsall = GetStyleString("VCVarsall", StyleVCVarsall); - StyleUndoMergeTimeout = GetStyleFloat("UndoMergeTimeout", StyleUndoMergeTimeout); -} - -typedef void Function(); -typedef void PFunction(void *param); -typedef int LuaFunction(lua_State *state); -struct FunctionData { String name; Function *function; }; -struct LuaFunctionData { String name; LuaFunction *function; }; -struct CommandData { String name; String binding; Function *function; struct Trigger *trigger; }; -struct PFunctionData { String name; PFunction *function; }; - -bool SkipRemainingCommands; -Array CommandFunctions; -Array LuaFunctions; -Array TestFunctions; - -struct Register_Function { - Register_Function(Array *functions, String name, Function *f) { - int64_t pos = 0; - if (Seek(name, "_", &pos, 0)) { - name = Skip(name, pos + 1); - } - Add(functions, {name, f}); - } -}; -#define RegisterFunction(functions, name) Register_Function RF__##name(functions, #name, name) - -struct Register_Lua { Register_Lua(LuaFunction *function, String name) { if (StartsWith(name, "Lua_")) name = Skip(name, 4); Add(&LuaFunctions, {name, function}); } }; -#define RegisterLua(NAME) Register_Lua RL_##NAME(NAME, #NAME) - -struct Register_Command { Register_Command(Function *function, String name, String binding) { if (StartsWith(name, "Command_")) name = Skip(name, 8); Add(&CommandFunctions, {name, binding, function}); } }; -#define RegisterCommand(name, binding) Register_Command RC__##name(name, #name, binding) - -struct Register_Hook { Register_Hook(Array *functions, PFunction *function, String name) { if (StartsWith(name, "Hook_")) name = Skip(name, 5); Add(functions, {name, function}); } }; -#define RegisterHook(functions, name) Register_Hook RC__##name(functions, (PFunction *)name, #name) - -const int DIR_RIGHT = 0; -const int DIR_LEFT = 1; -const int DIR_DOWN = 2; -const int DIR_UP = 3; -const int DIR_COUNT = 4; -const bool CTRL_PRESSED = true; -const bool SHIFT_PRESS = true; \ No newline at end of file +RegisterVariable(Color, ColorText, GruvboxDark0Hard); +RegisterVariable(Color, ColorLoadTextHighlight, {0x00, 0x00, 0x00, 0x0F}); +RegisterVariable(Color, ColorBackground, GruvboxLight0Hard); +RegisterVariable(Color, ColorInactiveWindow, {0x00, 0x00, 0x00, 0x0F}); +RegisterVariable(Color, ColorTextLineNumbers, GruvboxDark4); +RegisterVariable(Color, ColorLineHighlight, GruvboxLight0Soft); +RegisterVariable(Color, ColorMainCaret, GruvboxDark0Hard); +RegisterVariable(Color, ColorSubCaret, GruvboxGray245); +RegisterVariable(Color, ColorSelection, GruvboxLight1); +RegisterVariable(Color, ColorWhitespaceDuringSelection, GruvboxLight4); +RegisterVariable(Color, ColorMouseUnderline, GruvboxDark0Hard); +RegisterVariable(Color, ColorCaretUnderline, GruvboxGray245); +RegisterVariable(Color, ColorFuzzySearchLineHighlight, GruvboxDark0); +RegisterVariable(Color, ColorScrollbarBackground, GruvboxLight2); +RegisterVariable(Color, ColorScrollbarScroller, GruvboxLight1); +RegisterVariable(Color, ColorScrollbarScrollerSelected, GruvboxLight0Hard); +RegisterVariable(Color, ColorTitleBarText, GruvboxDark2); +RegisterVariable(Color, ColorTitleBarBackground, GruvboxLight1); +RegisterVariable(Color, ColorTitleBarActiveBackground, {0xfe, 0xfe, 0xfe, 0xfe}); +RegisterVariable(Color, ColorTitleBarSelection, GruvboxLight3); +RegisterVariable(Color, ColorResizerBackground, GruvboxLight0Hard); +RegisterVariable(Color, ColorResizerOutline, GruvboxLight3); +RegisterVariable(Int, StyleWaitForEvents, 1); +RegisterVariable(Int, StyleDrawLineNumbers, 1); +RegisterVariable(Int, StyleDrawScrollbar, 1); +RegisterVariable(Int, StyleIndentSize, 4); +RegisterVariable(Int, StyleFontSize, 15); +RegisterVariable(Int, StyleFontFilter, 0); +RegisterVariable(String, StyleFont, "/home/krz/text_editor/package/CascadiaMono.ttf"); +RegisterVariable(String, StyleVCVarsall, "C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Auxiliary/Build/vcvars64.bat"); +RegisterVariable(Float, StyleUndoMergeTimeout, 0.3); diff --git a/src/text_editor/lua.cpp b/src/text_editor/lua.cpp index 8b95f1d..27d669c 100644 --- a/src/text_editor/lua.cpp +++ b/src/text_editor/lua.cpp @@ -86,7 +86,6 @@ API String GetFieldString(lua_State *L, String name) { return result; } -void ReloadStyle(); extern String BaseLuaConfig; API void LoadLuaBuffer(Buffer *lua_buffer) { @@ -123,7 +122,13 @@ API void ReloadLuaConfigs(bool reload) { LoadLuaBuffer(LuaConfigBuffer); LoadLuaBuffer(LuaProjectBuffer); - ReloadStyle(); + For (Variables) { + if (it.type == VariableType_Color) it.color[0] = GetColor(Skip(it.name, 5), it.color[0]); + else if (it.type == VariableType_Int) it.i[0] = GetStyleInt(Skip(it.name, 5), it.i[0]); + else if (it.type == VariableType_String) it.string[0] = GetStyleString(Skip(it.name, 5), it.string[0]); + else if (it.type == VariableType_Float) it.f[0] = GetStyleFloat(Skip(it.name, 5), it.f[0]); + else Assert(!"Invalid codepath"); + } ReloadFont(StyleFont, (U32)StyleFontSize); For(Windows) { it->draw_scrollbar = StyleDrawScrollbar; @@ -227,7 +232,6 @@ OnOpenResult CallOnOpen(Allocator allocator, String path, String meta) { result.working_dir = working_dir; result.file_path = file_path; if (!IsAbsolute(result.file_path)) { - String GetMainDir(); String dir = GetMainDir(); result.file_path = Format(allocator, "%S/%S", dir, result.file_path); } diff --git a/src/text_editor/text_editor.h b/src/text_editor/text_editor.h index 01310a1..cfdd6b8 100644 --- a/src/text_editor/text_editor.h +++ b/src/text_editor/text_editor.h @@ -75,6 +75,7 @@ Scroller ComputeScrollerRect(Window *window); BSet Open(String path, String meta = ""); BSet Open(String16 path, String meta = ""); void UpdateScroll(Window *window, bool update_caret_scrolling); +String GetMainDir(); void SelectEntireBuffer(View *view); void Replace(View *view, String16 string); @@ -120,4 +121,71 @@ Buffer *BufferOpenFile(String path); #define IF_DEBUG(x) x #else #define IF_DEBUG(x) -#endif \ No newline at end of file +#endif + + +typedef void Function(); +typedef void PFunction(void *param); +typedef int LuaFunction(lua_State *state); +struct FunctionData { String name; Function *function; }; +struct LuaFunctionData { String name; LuaFunction *function; }; +struct CommandData { String name; String binding; Function *function; struct Trigger *trigger; }; +struct PFunctionData { String name; PFunction *function; }; + + +struct Register_Function { + Register_Function(Array *functions, String name, Function *f) { + int64_t pos = 0; + if (Seek(name, "_", &pos, 0)) { + name = Skip(name, pos + 1); + } + Add(functions, {name, f}); + } +}; +#define RegisterFunction(functions, name) Register_Function RF__##name(functions, #name, name) + +struct Register_Lua { Register_Lua(Array *funcs, LuaFunction *function, String name) { if (StartsWith(name, "Lua_")) name = Skip(name, 4); Add(funcs, {name, function}); } }; +#define RegisterLua(NAME) Register_Lua RL_##NAME(&LuaFunctions, NAME, #NAME) + +struct Register_Command { Register_Command(Array *fucs, Function *function, String name, String binding) { if (StartsWith(name, "Command_")) name = Skip(name, 8); Add(fucs, {name, binding, function}); } }; +#define RegisterCommand(name, binding) Register_Command RC__##name(&CommandFunctions, name, #name, binding) + +struct Register_Hook { Register_Hook(Array *functions, PFunction *function, String name) { if (StartsWith(name, "Hook_")) name = Skip(name, 5); Add(functions, {name, function}); } }; +#define RegisterHook(functions, name) Register_Hook RC__##name(functions, (PFunction *)name, #name) + +const int DIR_RIGHT = 0; +const int DIR_LEFT = 1; +const int DIR_DOWN = 2; +const int DIR_UP = 3; +const int DIR_COUNT = 4; +const bool CTRL_PRESSED = true; +const bool SHIFT_PRESS = true; + +enum VariableType { + VariableType_Invalid, + VariableType_Color, + VariableType_String, + VariableType_Int, + VariableType_Float, +}; + +struct Variable { + VariableType type; + String name; + union { + void *addr; + Color *color; + Int *i; + double *f; + String *string; + }; +}; + +struct Register_Variable { + Register_Variable(Array *variables, VariableType type, String name, void *addr) { + Add(variables, {type, name, addr}); + } +}; +#define RegisterVariable(type, name, ...) \ + type name = __VA_ARGS__; \ + Register_Variable var_##name(&Variables, VariableType_##type, #name, &name)