Compare commits

...

3 Commits

Author SHA1 Message Date
Krzosa Karol
05d5de4b7d RemedyBG integration plugin 2026-01-10 14:03:00 +01:00
Krzosa Karol
e575569d8a Misc 2026-01-10 10:36:09 +01:00
Krzosa Karol
4c9c0e5210 Continuing the hook refactor, BeforeSaveBuffer 2026-01-10 10:28:12 +01:00
11 changed files with 2354 additions and 76 deletions

View File

@@ -1142,6 +1142,7 @@ API void InitBuffer(Allocator allocator, Buffer *buffer, BufferID id = {}, Strin
API void DeinitBuffer(Buffer *buffer) {
Allocator allocator = buffer->line_starts.allocator;
Dealloc(allocator, buffer->data);
Dealloc(&buffer->hooks);
Dealloc(&buffer->line_starts);
DeallocHistoryArray(&buffer->undo_stack);
DeallocHistoryArray(&buffer->redo_stack);
@@ -1548,7 +1549,18 @@ void ReopenBuffer(Buffer *buffer) {
buffer->dirty = false;
}
void BasicSaveBuffer(Buffer *buffer) {
void SaveBuffer(Buffer *buffer) {
ProfileFunction();
For (GlobalHooks) {
if (it.kind == HookKind_BeforeSavingBuffer) {
ProfileScopeEx(it.name);
HookParam param = {};
param.buffer = buffer;
it.function(param);
}
}
Scratch scratch;
String string = AllocCharString(scratch, buffer);
bool success = WriteFile(buffer->name, string);
@@ -1561,11 +1573,3 @@ void BasicSaveBuffer(Buffer *buffer) {
ReportWarningf("Failed to save file with name: %S", buffer->name);
}
}
void SaveBuffer(Buffer *buffer) {
if (TrimTrailingWhitespace) {
TrimWhitespace(buffer);
}
BasicSaveBuffer(buffer);
}

View File

@@ -291,6 +291,12 @@ void TrimWhitespace(Buffer *buffer, bool trim_lines_with_caret) {
view->update_scroll = false;
}
void HOOK_TrimTrailingWhitespace(HookParam param) {
if (TrimTrailingWhitespace) {
TrimWhitespace(param.buffer, false);
}
} RegisterHook(HOOK_TrimTrailingWhitespace, HookKind_BeforeSavingBuffer, "", "Cleans trailing whitespace before saving to file");
void ConvertLineEndingsToLF(Buffer *buffer, bool trim_lines_with_caret = false) {
Scratch scratch;
@@ -486,7 +492,7 @@ void CMD_SaveAll(HookParam param) {
For(Buffers) {
// NOTE: file_mod_time is only set when buffer got read or written to disk already so should be saved
if (it->file_mod_time && it->dirty) {
BasicSaveBuffer(it);
SaveBuffer(it);
}
}
} RegisterCommand(CMD_SaveAll, "ctrl-shift-s");
@@ -1106,6 +1112,12 @@ String Coro_CloseAllEx(mco_coro *co) {
void Coro_Quit(mco_coro *co) {
String res = Coro_CloseAllEx(co);
if (res != "Cancel") {
For (GlobalHooks) {
if (it.kind == HookKind_AppQuit) {
ProfileScopeEx(it.name);
it.function({});
}
}
AppIsRunning = false;
}
}
@@ -1114,9 +1126,15 @@ void CMD_Quit(HookParam param) {
CoRemove("Coro_Quit");
CoData *data = CoAdd(Coro_Quit);
CoResume(data);
} RegisterHook(CMD_Quit, HookKind_AppQuit, "", "Ask user which files he would like to save and exit");
} RegisterCommand(CMD_Quit, "", "Ask user which files he would like to save and exit");
void CMD_QuitWithoutSaving(HookParam param) {
For (GlobalHooks) {
if (it.kind == HookKind_AppQuit) {
ProfileScopeEx(it.name);
it.function({});
}
}
AppIsRunning = false;
} RegisterCommand(CMD_QuitWithoutSaving, "", "Self explanatory");

File diff suppressed because it is too large Load Diff

View File

@@ -34,6 +34,8 @@
#include "draw.cpp"
#include "test/tests.cpp"
#include "remedybg_plugin.cpp"
#if OS_WASM
EM_JS(void, JS_SetMouseCursor, (const char *cursor_str), {
document.getElementById("canvas").style.cursor = UTF8ToString(cursor_str);
@@ -425,12 +427,7 @@ void OnCommand(Event event) {
}
if (event.kind == EVENT_QUIT) {
For (GlobalHooks) {
if (it.kind == HookKind_AppQuit) {
ProfileScopeEx(it.name);
it.function({});
}
}
CMD_Quit({});
}
IF_DEBUG(AssertRanges(main.view->carets));
@@ -511,6 +508,7 @@ void GarbageCollect() {
RawAppendf(GCInfoBuffer, "View %d %S\n", (int)it->id.id, buffer ? buffer->name : String{"NULL"});
remove_item = true;
Dealloc(&it->hooks);
Dealloc(it);
}

View File

@@ -25,6 +25,66 @@ struct HistoryEntry {
double time;
};
enum HookKind {
HookKind_Invalid,
HookKind_AppInit,
HookKind_AppUpdate,
HookKind_AppQuit,
// Should we have commands like PostCommand, PreCommand? what would be the purpose?
HookKind_Command,
// Currently we are only basically allowing control over non-layouted windows.
// How can this be expanded?
// - Changing the layout algorithm: this seems like a decent one
// - What beside that?
HookKind_HandleWindowLayout,
// Move the special window rendering to hooks?
// HookKind_RenderWindow,
// HookKind_CreateWindow,
// How to handle formatters for multiple languages and other saving rules???
HookKind_BeforeSavingBuffer,
HookKind_CreateBuffer,
// HookKind_BufferSave,
// HookKind_AfterBufferSave,
// HookKind_ResolveOpen,
// HookKind_BeforeBufferOpen,
// HookKind_BufferOpen,
// HookKind_AfterBufferOpen,
HookKind_CreateView,
};
// Global hooks, per (window, view, buffer) hooks
struct HookParam {
union {
Buffer *buffer;
struct {
Window *window;
Rect2I *rect;
int16_t wx;
int16_t wy;
} layout;
};
};
typedef void HookFunction(HookParam param);
struct Hook {
HookKind kind;
String name;
String docs;
HookFunction *function;
String binding;
struct Trigger *trigger;
};
struct Buffer {
BufferID id;
String name;
@@ -44,6 +104,7 @@ struct Buffer {
Array<HistoryEntry> undo_stack;
Array<HistoryEntry> redo_stack;
int edit_phase;
Array<Hook> hooks;
struct {
unsigned no_history : 1;
unsigned no_line_starts : 1;
@@ -57,58 +118,6 @@ struct Buffer {
};
};
enum HookKind {
HookKind_Invalid,
HookKind_AppInit,
HookKind_AppUpdate,
HookKind_AppQuit,
// Should we have commands like PostCommand, PreCommand? what would be the purpose?
HookKind_Command,
// Currently we are only basically allowing control over non-layouted windows.
// How can this be expanded?
// - Changing the layout algorithm: this seems like a decent one
// - What beside that?
HookKind_LayoutWindow,
HookKind_RenderWindow
HookKind_BeforeBufferSave,
HookKind_BufferSave,
HookKind_AfterBufferSave,
HookKind_BeforeBufferOpen,
HookKind_BufferOpen,
HookKind_AfterBufferOpen,
HookKind_ViewUpdate,
};
// Global hooks, per (window, view, buffer) hooks
struct HookParam {
union {
struct {
Window *window;
Rect2I *rect;
int16_t wx;
int16_t wy;
} layout;
};
};
typedef void HookFunction(HookParam param);
struct Hook {
HookKind kind;
String name;
String docs;
HookFunction *function;
String binding;
struct Trigger *trigger;
};
struct View {
ViewID id;
BufferID active_buffer;

View File

@@ -140,7 +140,7 @@ void LayoutWindows(int16_t wx, int16_t wy) {
ForItem (n, Windows) {
ForItem (hook, n->hooks) {
if (hook.kind == HookKind_LayoutWindow) {
if (hook.kind == HookKind_HandleWindowLayout) {
ProfileScopeEx(it.name);
HookParam param = {};
param.layout.window = n;

View File

@@ -35,5 +35,5 @@ void InitBuildWindow(HookParam param) {
window->visible = false;
window->lose_visibility_on_escape = true;
window->jump_history = false;
AddHook(&window->hooks, HookKind_LayoutWindow, "LayoutBuildWindow", "", LayoutBuildWindow);
AddHook(&window->hooks, HookKind_HandleWindowLayout, "LayoutBuildWindow", "", LayoutBuildWindow);
} RegisterHook(InitBuildWindow, HookKind_AppInit, "", "Init the build window");

View File

@@ -248,5 +248,5 @@ void InitCommandWindow(HookParam param) {
window->sync_visibility_with_focus = true;
window->lose_focus_on_escape = true;
window->jump_history = false;
AddHook(&window->hooks, HookKind_LayoutWindow, "LayoutCommandWindow", "", LayoutCommandWindow);
AddHook(&window->hooks, HookKind_HandleWindowLayout, "LayoutCommandWindow", "", LayoutCommandWindow);
} RegisterHook(InitCommandWindow, HookKind_AppInit, "", "Init command window");

View File

@@ -78,7 +78,7 @@ void InitDebugWindow(HookParam param) {
window->active_view = view->id;
window->visible = false;
AddHook(&window->hooks, HookKind_LayoutWindow, "@todo: LayoutDebugWindow", "", LayoutDebugWindow);
AddHook(&window->hooks, HookKind_HandleWindowLayout, "@todo: LayoutDebugWindow", "", LayoutDebugWindow);
} RegisterHook(InitDebugWindow, HookKind_AppInit, "", "Init the debug window");
void CMD_ToggleDebug(HookParam param) {

View File

@@ -99,5 +99,5 @@ void InitSearchWindow(HookParam param) {
AddCommand(&view->hooks, "SearchAll", "alt-enter", CMD_SearchAll);
AddCommand(&view->hooks, "SearchPrevInSearch", "shift-enter", CMD_SearchPrevInSearch);
AddCommand(&view->hooks, "SearchNextInSearch", "enter", CMD_SearchNextInSearch);
AddHook(&window->hooks, HookKind_LayoutWindow, "LayoutSearchWindow", "", LayoutSearchWindow);
AddHook(&window->hooks, HookKind_HandleWindowLayout, "LayoutSearchWindow", "", LayoutSearchWindow);
} RegisterHook(InitSearchWindow, HookKind_AppInit, "", "Init the search window");

View File

@@ -77,7 +77,7 @@ void LayoutStatusWindow(HookParam param) {
}
Int barsize = GetExpandingBarSize(p.window);
p.window->document_rect = p.window->total_rect = CutBottom(p.rect, barsize);
} RegisterHook(LayoutStatusWindow, HookKind_LayoutWindow, "", "Layout the status window");
} RegisterHook(LayoutStatusWindow, HookKind_HandleWindowLayout, "", "Layout the status window");
void StatusWindowInit(HookParam param) {
Window *window = CreateWind();
@@ -95,5 +95,5 @@ void StatusWindowInit(HookParam param) {
window->primary = false;
window->jump_history = false;
window->lose_focus_on_escape = true;
AddHook(&window->hooks, HookKind_LayoutWindow, "@todo LayoutStatusWindow", "", LayoutStatusWindow);
AddHook(&window->hooks, HookKind_HandleWindowLayout, "@todo LayoutStatusWindow", "", LayoutStatusWindow);
} RegisterHook(StatusWindowInit, HookKind_AppInit, "", "Init the status window");