From c67db4e4952888cc33415fb8b73a310d8fb630de Mon Sep 17 00:00:00 2001 From: krzosa Date: Tue, 23 Dec 2025 08:52:42 +0100 Subject: [PATCH] Implement new search window --- src/backup/todo.txt | 2 ++ src/text_editor/buffer.h | 1 + src/text_editor/commands.cpp | 12 +++++++++- src/text_editor/globals.cpp | 2 +- src/text_editor/text_editor.cpp | 4 +++- src/text_editor/window.h | 3 ++- src/text_editor/window_search.cpp | 39 +++++++++++++++++++++++++++---- src/text_editor/window_status.cpp | 3 +-- 8 files changed, 55 insertions(+), 11 deletions(-) diff --git a/src/backup/todo.txt b/src/backup/todo.txt index c5e5f24..d753a5f 100644 --- a/src/backup/todo.txt +++ b/src/backup/todo.txt @@ -1,7 +1,9 @@ - What precise workflow do I need for me to be viable to use this? - From a user (novice) point of view, how does it look like? +- Show what process/coroutines are running and allow to kill (active process buffer?) - ctrl + p like in VSCode (without special buffers) + - I THINK WE NEED A SPECIAL VIEW, this is too slow - Move to the top so that it can be progressively expanded using coroutines by appending at the end - Maybe 2 windows? - Guide on the first page for new users with links to configs, tutorials diff --git a/src/text_editor/buffer.h b/src/text_editor/buffer.h index 4736a26..dec92b6 100644 --- a/src/text_editor/buffer.h +++ b/src/text_editor/buffer.h @@ -18,6 +18,7 @@ struct HistoryEntry { struct Buffer { BufferID id; String name; + Int begin_frame_change_id; Int change_id; Int user_change_id; Int file_mod_time; diff --git a/src/text_editor/commands.cpp b/src/text_editor/commands.cpp index 9f24302..5da5588 100644 --- a/src/text_editor/commands.cpp +++ b/src/text_editor/commands.cpp @@ -1063,6 +1063,8 @@ void Command_NewLine() { BSet active = GetBSet(ActiveWindowID); if (active.window->id == CommandWindowID) { CommandWindowOpen(active); + } else if (active.window->id == SearchWindowID) { + SearchWindowFindNext(true); } else { IdentedNewLine(active.view); } @@ -1108,7 +1110,6 @@ void Command_ClearCarets() { active.view->carets.len = 1; active.view->carets[0] = MakeCaret(GetFront(active.view->carets[0])); - // @todo: move to hook post command??? if (active.window->lose_focus_on_escape && active.window->id == ActiveWindowID) { if (active.window->layout) { // @@ -1116,4 +1117,13 @@ void Command_ClearCarets() { ActiveWindowID = LastActiveLayoutWindowID; } } + + For (Windows) { + if (it->lose_visibility_on_escape && it->visible) { + if (ActiveWindowID == it->id) { + ActiveWindowID = LastActiveLayoutWindowID; + } + it->visible = false; + } + } } RegisterCommand(Command_ClearCarets, "escape"); diff --git a/src/text_editor/globals.cpp b/src/text_editor/globals.cpp index 33c425e..c8be602 100644 --- a/src/text_editor/globals.cpp +++ b/src/text_editor/globals.cpp @@ -27,7 +27,7 @@ BufferID DebugBufferID; WindowID CommandWindowID; WindowID StatusBarWindowID; -WindowID SearchBarWindowID; +WindowID SearchWindowID; ViewID SearchViewID; BufferID SearchBufferID; diff --git a/src/text_editor/text_editor.cpp b/src/text_editor/text_editor.cpp index 5a325e1..9e29701 100644 --- a/src/text_editor/text_editor.cpp +++ b/src/text_editor/text_editor.cpp @@ -388,7 +388,6 @@ void OnCommand(Event event) { BSet main = GetBSet(LastActiveLayoutWindowID); BSet active = GetBSet(ActiveWindowID); - Int buffer_change_id = active.buffer->change_id; For (CommandFunctions) { if (it.trigger && MatchEvent(it.trigger, &event)) { @@ -514,12 +513,15 @@ void Update(Event event) { view->update_scroll = true; } + OnCommand(event); StatusWindowUpdate(); DebugWindowUpdate(); CommandWindowUpdate(); + SearchWindowUpdate(); UpdateProcesses(); CoUpdate(&event); + For (Buffers) it->begin_frame_change_id = it->change_id; // after last place we modify { ProfileScope(WindowEndOfFrameVisibilityAndLastActive); diff --git a/src/text_editor/window.h b/src/text_editor/window.h index 613b76c..428eed8 100644 --- a/src/text_editor/window.h +++ b/src/text_editor/window.h @@ -15,7 +15,7 @@ struct Window { double mouse_scroller_offset; int z; double weight; - Int status_bar_last_buffer_change_id; + Caret search_bar_anchor; Array goto_history; Array goto_redo; @@ -33,6 +33,7 @@ struct Window { bool close : 1; bool sync_visibility_with_focus : 1; bool lose_focus_on_escape : 1; + bool lose_visibility_on_escape : 1; bool jump_history : 1; bool eval_command : 1; }; diff --git a/src/text_editor/window_search.cpp b/src/text_editor/window_search.cpp index 5208073..439610d 100644 --- a/src/text_editor/window_search.cpp +++ b/src/text_editor/window_search.cpp @@ -1,6 +1,6 @@ void SearchWindowInit() { Window *window = CreateWind(); - SearchBarWindowID = window->id; + SearchWindowID = window->id; Buffer *buffer = CreateBuffer(SysAllocator, "search_bar"); buffer->special = true; SearchBufferID = buffer->id; @@ -14,11 +14,11 @@ void SearchWindowInit() { window->draw_line_highlight = false; window->layout = false; window->visible = false; - window->lose_focus_on_escape = true; + window->lose_visibility_on_escape = true; } void SearchWindowLayout(Rect2I *rect, Int wx, Int wy) { - Window *n = GetWindow(SearchBarWindowID); + Window *n = GetWindow(SearchWindowID); Rect2I copy_rect = *rect; if (!n->visible) { rect = ©_rect; @@ -28,6 +28,35 @@ void SearchWindowLayout(Rect2I *rect, Int wx, Int wy) { } void Command_Search() { - Window *window = GetWindow(SearchBarWindowID); - window->visible = !window->visible; + BSet set = GetBSet(SearchWindowID); + set.window->visible = true; + ActiveWindowID = SearchWindowID; + SelectEntireBuffer(set.view); } RegisterCommand(Command_Search, "ctrl-f"); + +void SearchWindowFindNext(bool forward = true) { + BSet main = GetBSet(LastActiveLayoutWindowID); + BSet set = GetBSet(SearchWindowID); + String16 seek = GetString(set.buffer, GetRange(set.buffer)); + Find(main.view, seek, forward); + main.window->search_bar_anchor = main.view->carets[0]; +} + +void Command_SearchNext() { + SearchWindowFindNext(true); +} RegisterCommand(Command_SearchNext, "f3"); + +void Command_SearchPrev() { + SearchWindowFindNext(false); +} RegisterCommand(Command_SearchPrev, "shift-f3 | shift-enter"); + +void SearchWindowUpdate() { + BSet active = GetBSet(ActiveWindowID); + if (active.window->id == SearchWindowID && active.buffer->begin_frame_change_id != active.buffer->change_id) { + BSet main = GetBSet(LastActiveLayoutWindowID); + BSet set = GetBSet(SearchWindowID); + main.view->carets[0] = main.window->search_bar_anchor; + String16 seek = GetString(set.buffer, GetRange(set.buffer)); + Find(main.view, seek, true); + } +} diff --git a/src/text_editor/window_status.cpp b/src/text_editor/window_status.cpp index eeb9c6a..9b114fc 100644 --- a/src/text_editor/window_status.cpp +++ b/src/text_editor/window_status.cpp @@ -42,7 +42,7 @@ void StatusWindowUpdate() { // Parse the title and line if (title.window->id == ActiveWindowID) { - if (title.buffer->change_id == title.window->status_bar_last_buffer_change_id) { + if (title.buffer->change_id == title.buffer->begin_frame_change_id) { return; } String16 buffer_name = GetString(title.buffer, replace_range); @@ -63,7 +63,6 @@ void StatusWindowUpdate() { if (GetFront(caret) != buffer_pos) { caret = MakeCaret(buffer_pos); } - title.window->status_bar_last_buffer_change_id = title.buffer->change_id; return; }