From 5214ce61acb367e51227c8bba51d70b679fd0efb Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Tue, 30 Dec 2025 12:56:36 +0100 Subject: [PATCH] Hook code cleanup --- src/backup/todo.txt | 1 - src/text_editor/commands.cpp | 10 +--- src/text_editor/window_command.cpp | 66 ++++++++++++----------- src/text_editor/window_search.cpp | 84 +++++++++++++----------------- 4 files changed, 70 insertions(+), 91 deletions(-) diff --git a/src/backup/todo.txt b/src/backup/todo.txt index 5b750d0..594fb37 100644 --- a/src/backup/todo.txt +++ b/src/backup/todo.txt @@ -11,7 +11,6 @@ How to go about search/replace, opening code and other considerations Use session 2 - Need configs I can't change browser or vcvarsall currently, maybe syntax like :Set InternetBrowser "firefox" -- BUILD annoying: Console or use the other window / the window which is already being used - Tutorial - When jumping should center the view!!! diff --git a/src/text_editor/commands.cpp b/src/text_editor/commands.cpp index 9b5dc3c..407dc87 100644 --- a/src/text_editor/commands.cpp +++ b/src/text_editor/commands.cpp @@ -402,7 +402,7 @@ BSet ExecBuild(String cmd) { BSet main = GetBSet(LastActiveLayoutWindowID); SelectRange(build.view, Range{}); ResetBuffer(build.buffer); - Exec(build.view->id, false, cmd, WorkDir); // NOTE: IN CASE WE MOVE THIS TO CONSOLE WINDOW, MAKE SURE TO SWITCH HERE the scrolling + Exec(build.view->id, true, cmd, WorkDir); main.window->active_goto_list = build.view->id; main.window->goto_list_pos = 0; return build; @@ -949,9 +949,6 @@ void Command_MakeFontSmaller() { void Command_Open() { BSet active = GetBSet(ActiveWindowID); - if (active.window->id == CommandWindowID) { - return; - } Open(FetchLoadWord(active.view)); } RegisterCommand(Command_Open, "ctrl-q"); @@ -1172,12 +1169,9 @@ void Command_InsertNewLineDown() { } RegisterCommand(Command_InsertNewLineDown, "ctrl-enter"); void Command_NewLine() { - if (ActiveWindowID == CommandWindowID || ActiveWindowID == SearchWindowID) { - return; - } BSet active = GetBSet(ActiveWindowID); IdentedNewLine(active.view); -} RegisterCommand(Command_NewLine, "enter"); +} RegisterCommand(Command_NewLine, "enter | shift-enter"); void Command_CreateCaretOnNextFind() { BSet active = GetBSet(ActiveWindowID); diff --git a/src/text_editor/window_command.cpp b/src/text_editor/window_command.cpp index 7407146..3636422 100644 --- a/src/text_editor/window_command.cpp +++ b/src/text_editor/window_command.cpp @@ -1,32 +1,3 @@ -void CommandWindowInit() { - Window *window = CreateWind(); - CommandWindowID = window->id; - Buffer *buffer = CreateBuffer(SysAllocator, GetUniqueBufferName(WorkDir, "command_bar")); - buffer->special = true; - View *view = CreateView(buffer->id); - view->special = true; - window->active_view = view->id; - window->draw_line_numbers = false; - window->draw_scrollbar = false; - window->draw_darker = true; - window->draw_line_highlight = true; - window->layout = false; - window->visible = false; - window->sync_visibility_with_focus = true; - window->lose_focus_on_escape = true; - window->jump_history = false; -} - -void CommandWindowLayout(Rect2I *rect, Int wx, Int wy) { - Window *n = GetWindow(CommandWindowID); - Rect2I copy_rect = *rect; - if (!n->visible) { - rect = ©_rect; - } - Int barsize = Clamp((Int)n->font->line_spacing*10, (Int)0, (Int)wx - 100); - n->document_rect = n->total_rect = CutBottom(rect, barsize); -} - int32_t FuzzyRate(String16 string, String16 with) { ProfileFunction(); if (with.len == 0) return 0; @@ -175,12 +146,39 @@ void OpenCommand(BSet active) { Open(string); } -void Command_OpenCommand() { - if (ActiveWindowID != CommandWindowID) { - return; - } +void Command_CommandWindowOpen() { BSet active = GetBSet(ActiveWindowID); BSet main = GetBSet(LastActiveLayoutWindowID); NextActiveWindowID = main.window->id; OpenCommand(active); -} RegisterCommand(Command_OpenCommand, "ctrl-q | enter"); +} + +void CommandWindowLayout(Rect2I *rect, Int wx, Int wy) { + Window *n = GetWindow(CommandWindowID); + Rect2I copy_rect = *rect; + if (!n->visible) { + rect = ©_rect; + } + Int barsize = Clamp((Int)n->font->line_spacing*10, (Int)0, (Int)wx - 100); + n->document_rect = n->total_rect = CutBottom(rect, barsize); +} + +void CommandWindowInit() { + Window *window = CreateWind(); + CommandWindowID = window->id; + Buffer *buffer = CreateBuffer(SysAllocator, GetUniqueBufferName(WorkDir, "command_bar")); + buffer->special = true; + View *view = CreateView(buffer->id); + view->special = true; + window->active_view = view->id; + window->draw_line_numbers = false; + window->draw_scrollbar = false; + window->draw_darker = true; + window->draw_line_highlight = true; + window->layout = false; + window->visible = false; + window->sync_visibility_with_focus = true; + window->lose_focus_on_escape = true; + window->jump_history = false; + AddHook(&view->hooks, "Open", "ctrl-q | enter", Command_CommandWindowOpen); +} diff --git a/src/text_editor/window_search.cpp b/src/text_editor/window_search.cpp index 37e730a..5f68d55 100644 --- a/src/text_editor/window_search.cpp +++ b/src/text_editor/window_search.cpp @@ -1,34 +1,3 @@ -void SearchWindowInit() { - Window *window = CreateWind(); - SearchWindowID = window->id; - Buffer *buffer = CreateBuffer(SysAllocator, GetUniqueBufferName(WorkDir, "search")); - buffer->special = true; - SearchBufferID = buffer->id; - View *view = CreateView(buffer->id); - view->special = true; - SearchViewID = view->id; - window->active_view = view->id; - window->draw_line_numbers = false; - window->draw_scrollbar = false; - window->draw_darker = true; - window->draw_line_highlight = false; - window->layout = false; - window->visible = false; - window->lose_visibility_on_escape = true; - window->jump_history = false; -} - -void SearchWindowLayout(Rect2I *rect, Int wx, Int wy) { - Window *n = GetWindow(SearchWindowID); - Rect2I copy_rect = *rect; - if (!n->visible) { - rect = ©_rect; - } - Int barsize = GetExpandingBarSize(n); - n->document_rect = n->total_rect = CutBottom(rect, barsize); - n->line_numbers_rect = CutLeft(&n->document_rect, n->font->char_spacing * 6); -} - void Command_Search() { BSet main = GetBSet(ActiveWindowID); String16 string = {}; @@ -54,18 +23,12 @@ void SearchWindowFindNext(bool forward = true) { } void Command_SearchNextInSearch() { - if (ActiveWindowID != SearchWindowID) { - return; - } SearchWindowFindNext(true); -} RegisterCommand(Command_SearchNextInSearch, "enter"); +} void Command_SearchPrevInSearch() { - if (ActiveWindowID != SearchWindowID) { - return; - } SearchWindowFindNext(false); -} RegisterCommand(Command_SearchPrevInSearch, "shift-enter"); +} void Command_SearchNext() { SearchWindowFindNext(true); @@ -96,15 +59,6 @@ void Command_SearchAll() { set.window->visible = false; } RegisterCommand(Command_SearchAll, "alt-f3"); -void Command_SearchAllInSearch() { - if (ActiveWindowID != SearchWindowID) { - return; - } - SearchAll(); - BSet set = GetBSet(SearchWindowID); - set.window->visible = false; -} RegisterCommand(Command_SearchAllInSearch, "alt-enter"); - void Command_ToggleCaseSensitiveSearch() { SearchCaseSensitive = !SearchCaseSensitive; } RegisterCommand(Command_ToggleCaseSensitiveSearch, "alt-c"); @@ -123,3 +77,37 @@ void SearchWindowUpdate() { Find(main.view, seek, true); } } + +void SearchWindowLayout(Rect2I *rect, Int wx, Int wy) { + Window *n = GetWindow(SearchWindowID); + Rect2I copy_rect = *rect; + if (!n->visible) { + rect = ©_rect; + } + Int barsize = GetExpandingBarSize(n); + n->document_rect = n->total_rect = CutBottom(rect, barsize); + n->line_numbers_rect = CutLeft(&n->document_rect, n->font->char_spacing * 6); +} + +void SearchWindowInit() { + Window *window = CreateWind(); + SearchWindowID = window->id; + Buffer *buffer = CreateBuffer(SysAllocator, GetUniqueBufferName(WorkDir, "search")); + buffer->special = true; + SearchBufferID = buffer->id; + View *view = CreateView(buffer->id); + view->special = true; + SearchViewID = view->id; + window->active_view = view->id; + window->draw_line_numbers = false; + window->draw_scrollbar = false; + window->draw_darker = true; + window->draw_line_highlight = false; + window->layout = false; + window->visible = false; + window->lose_visibility_on_escape = true; + window->jump_history = false; + AddHook(&view->hooks, "SearchAll", "alt-enter", Command_SearchAll); + AddHook(&view->hooks, "SearchPrevInSearch", "shift-enter", Command_SearchPrevInSearch); + AddHook(&view->hooks, "SearchNextInSearch", "enter", Command_SearchNextInSearch); +}