Begin search buffer

This commit is contained in:
Krzosa Karol
2024-07-24 17:07:10 +02:00
parent d1b4053205
commit 754d6cd3bd
4 changed files with 92 additions and 15 deletions

View File

@@ -218,8 +218,8 @@ void ReloadFont(Int font_size) {
} }
void HandleGlobalCommands() { void HandleGlobalCommands() {
Window *command_window = GetWindow(CommandWindowID);
if (CtrlPress(KEY_P)) { if (CtrlPress(KEY_P)) {
Window *command_window = GetWindow(CommandWindowID);
if (command_window->visible) { if (command_window->visible) {
SetActiveWindow(GetLastActiveWindow()); SetActiveWindow(GetLastActiveWindow());
} else { } else {
@@ -227,6 +227,15 @@ void HandleGlobalCommands() {
} }
} }
if (CtrlPress(KEY_F)) {
Window *search_window = GetWindow(SearchWindowID);
if (search_window->visible) {
SetActiveWindow(GetLastActiveWindow());
} else {
SetActiveWindow(search_window->id);
}
}
if (CtrlPress(KEY_MINUS)) { if (CtrlPress(KEY_MINUS)) {
Int font_size = Clamp(FontSize - 1, (Int)4, (Int)100); Int font_size = Clamp(FontSize - 1, (Int)4, (Int)100);
ReloadFont(font_size); ReloadFont(font_size);

View File

@@ -173,8 +173,12 @@ void HandleActiveWindowBindings(Window *window) {
} }
if (Press(KEY_ESCAPE)) { if (Press(KEY_ESCAPE)) {
if (window->deactivate_on_escape) {
SetActiveWindow(GetLastActiveWindow());
} else {
view.carets.len = 1; view.carets.len = 1;
} }
}
if (CtrlAltPress(KEY_DOWN)) { if (CtrlAltPress(KEY_DOWN)) {
Command_DuplicateLine(&view, DIR_DOWN); Command_DuplicateLine(&view, DIR_DOWN);
@@ -320,23 +324,28 @@ void HandleActiveWindowBindings(Window *window) {
Command_MoveCursorsToSide(window, DIR_RIGHT); Command_MoveCursorsToSide(window, DIR_RIGHT);
} }
bool search = false;
if (Press(KEY_TAB)) { if (Press(KEY_TAB)) {
Command_Replace(&view, L" "); Command_Replace(&view, L" ");
search = true;
} }
if (CtrlPress(KEY_BACKSPACE)) { if (CtrlPress(KEY_BACKSPACE)) {
Command_Delete(&view, DIR_LEFT, CTRL_PRESSED); Command_Delete(&view, DIR_LEFT, CTRL_PRESSED);
search = true;
} else if (Press(KEY_BACKSPACE)) { } else if (Press(KEY_BACKSPACE)) {
Command_Delete(&view, DIR_LEFT); Command_Delete(&view, DIR_LEFT);
search = true;
} }
if (CtrlPress(KEY_DELETE)) { if (CtrlPress(KEY_DELETE)) {
Command_Delete(&view, DIR_RIGHT, CTRL_PRESSED); Command_Delete(&view, DIR_RIGHT, CTRL_PRESSED);
search = true;
} else if (Press(KEY_DELETE)) { } else if (Press(KEY_DELETE)) {
Command_Delete(&view, DIR_RIGHT); Command_Delete(&view, DIR_RIGHT);
search = true;
} }
bool search = false;
for (int c = GetCharPressed(); c; c = GetCharPressed()) { for (int c = GetCharPressed(); c; c = GetCharPressed()) {
// we interpret 2 byte sequences as 1 byte when rendering but we still // we interpret 2 byte sequences as 1 byte when rendering but we still
// want to read them properly. // want to read them properly.
@@ -347,6 +356,50 @@ void HandleActiveWindowBindings(Window *window) {
search = true; search = true;
} }
if (Press(KEY_F3)) {
Buffer *search_buffer = GetBuffer("*search*");
String16 search_string = GetString(*search_buffer);
Caret caret = view.carets[0];
Int pos = caret.range.min + 1;
String16 medium = GetString(*buffer, {pos, INT64_MAX});
Int index = 0;
if (Seek(medium, search_string, &index)) {
view.carets[0] = MakeCaret(pos + index + search_string.len, pos + index);
} else {
medium = GetString(*buffer);
if (Seek(medium, search_string, &index)) {
view.carets[0] = MakeCaret(index + search_string.len, index);
}
}
}
if (Press(KEY_ENTER)) {
search = true;
}
if (window->id.id == SearchWindowID.id && search) {
Window *seek_window = GetWindow(GetLastActiveWindow());
View *seek_view = GetView(seek_window->active_view);
Buffer *seek_buffer = GetBuffer(seek_view->buffer_id);
Caret caret = seek_view->carets[0];
Int pos = caret.range.min;
// @todo: copy paste
String16 needle = GetString(*buffer);
String16 medium = GetString(*seek_buffer, {pos, INT64_MAX});
Int index = 0;
if (Seek(medium, needle, &index)) {
seek_view->carets[0] = MakeCaret(pos + index + needle.len, pos + index);
} else {
medium = GetString(*seek_buffer);
if (Seek(medium, needle, &index)) {
seek_view->carets[0] = MakeCaret(index + needle.len, index);
}
}
}
if (window->fuzzy_search && search) { if (window->fuzzy_search && search) {
Scratch scratch; Scratch scratch;
String16 first_line_string = GetLineStringWithoutNL(*buffer, 0); String16 first_line_string = GetLineStringWithoutNL(*buffer, 0);
@@ -372,6 +425,7 @@ void HandleActiveWindowBindings(Window *window) {
if (Press(KEY_ENTER)) { if (Press(KEY_ENTER)) {
Command_EvalLua(&view); Command_EvalLua(&view);
} }
} else if (window->id.id == SearchWindowID.id) {
} else { } else {
if (CtrlPress(KEY_ENTER)) { if (CtrlPress(KEY_ENTER)) {
Command_MoveCursorsToSide(window, DIR_RIGHT); Command_MoveCursorsToSide(window, DIR_RIGHT);

View File

@@ -31,11 +31,8 @@
#include "lua_api.cpp" #include "lua_api.cpp"
/* /*
- update info bar until '|' if tghere is no sign delete whole
- Save file (utf16->utf8) - Save file (utf16->utf8)
- resize windows - resize windows
- list files and open
- file dock on left side - file dock on left side
- We can actually combine this with command window and lua, it's just going to be a buffer of - We can actually combine this with command window and lua, it's just going to be a buffer of
- open "asd/asd/asd/asd" - open "asd/asd/asd/asd"
@@ -80,10 +77,6 @@ int main(void) {
// Create null // Create null
{ {
Buffer *buffer = CreateBuffer(sys_allocator, "*scratch*"); Buffer *buffer = CreateBuffer(sys_allocator, "*scratch*");
// View *view = CreateView(buffer->id);
// Window *window = CreateWindow();
// window->visible = false;
// AddView(window, view->id);
} }
{ {
@@ -114,6 +107,7 @@ int main(void) {
w->draw_scrollbar = false; w->draw_scrollbar = false;
w->draw_line_numbers = false; w->draw_line_numbers = false;
w->dont_save_in_active_window_history = true; w->dont_save_in_active_window_history = true;
w->deactivate_on_escape = true;
Buffer *b = CreateBuffer(sys_allocator, "*infobar*"); Buffer *b = CreateBuffer(sys_allocator, "*infobar*");
b->no_history = true; b->no_history = true;
View *v = CreateView(b->id); View *v = CreateView(b->id);
@@ -130,6 +124,7 @@ int main(void) {
w->execute_line = true; w->execute_line = true;
w->invisible_when_inactive = true; w->invisible_when_inactive = true;
w->dont_save_in_active_window_history = true; w->dont_save_in_active_window_history = true;
w->deactivate_on_escape = true;
Buffer *b = CreateBuffer(sys_allocator, "*commands*"); Buffer *b = CreateBuffer(sys_allocator, "*commands*");
View *v = CreateView(b->id); View *v = CreateView(b->id);
AddView(w, v->id); AddView(w, v->id);
@@ -138,6 +133,20 @@ int main(void) {
Command_EvalLua(v, L"open \"./\""); Command_EvalLua(v, L"open \"./\"");
} }
{
Window *w = CreateWindow();
w->draw_scrollbar = false;
w->draw_line_numbers = false;
w->visible = false;
w->dont_save_in_active_window_history = true;
w->invisible_when_inactive = true;
w->deactivate_on_escape = true;
Buffer *b = CreateBuffer(sys_allocator, "*search*");
View *v = CreateView(b->id);
AddView(w, v->id);
SearchWindowID = w->id;
}
while (!WindowShouldClose()) { while (!WindowShouldClose()) {
ProfileScope(game_loop); ProfileScope(game_loop);
FrameID += 1; FrameID += 1;
@@ -145,6 +154,14 @@ int main(void) {
Rect2I screen_rect = GetScreenRect(); Rect2I screen_rect = GetScreenRect();
Rect2I infobar_rect = CutBottom(&screen_rect, (Int)FontLineSpacing); Rect2I infobar_rect = CutBottom(&screen_rect, (Int)FontLineSpacing);
float line_numbers_size = MeasureTextEx(MainFont, "12345", (float)FontSize, (float)FontSpacing).x; float line_numbers_size = MeasureTextEx(MainFont, "12345", (float)FontSize, (float)FontSpacing).x;
{
int i = 5;
if (Windows[i].visible) {
Rect2I rect = CutBottom(&screen_rect, FontLineSpacing);
Windows[i].total_rect = rect;
Windows[i].document_rect = Windows[i].total_rect;
}
}
{ {
int i = 0; int i = 0;
Windows[i].total_rect = CutLeft(&screen_rect, (Int)((double)GetSize(screen_rect).x * 0.33)); Windows[i].total_rect = CutLeft(&screen_rect, (Int)((double)GetSize(screen_rect).x * 0.33));
@@ -166,11 +183,6 @@ int main(void) {
if (Windows[i].draw_scrollbar) Windows[i].scrollbar_rect = CutRight(&Windows[i].document_rect, 10); if (Windows[i].draw_scrollbar) Windows[i].scrollbar_rect = CutRight(&Windows[i].document_rect, 10);
if (Windows[i].draw_line_numbers) Windows[i].line_numbers_rect = CutLeft(&Windows[i].document_rect, (Int)line_numbers_size); if (Windows[i].draw_line_numbers) Windows[i].line_numbers_rect = CutLeft(&Windows[i].document_rect, (Int)line_numbers_size);
} }
// {
// int i = 3;
// Windows[i].total_rect = CutLeft(&infobar_rect, GetSize(infobar_rect).x / 2);
// Windows[i].document_rect = Windows[i].total_rect;
// }
{ {
int i = 3; int i = 3;
Windows[i].total_rect = infobar_rect; Windows[i].total_rect = infobar_rect;

View File

@@ -70,6 +70,7 @@ struct Window {
bool execute_line : 1; bool execute_line : 1;
bool invisible_when_inactive : 1; bool invisible_when_inactive : 1;
bool dont_save_in_active_window_history : 1; bool dont_save_in_active_window_history : 1;
bool deactivate_on_escape : 1;
}; };
}; };
@@ -89,6 +90,7 @@ BufferID NullBufferID = {0};
ViewID NullViewID = {0}; ViewID NullViewID = {0};
WindowID CommandWindowID = {0}; WindowID CommandWindowID = {0};
WindowID InfoBarWindowID = {0}; WindowID InfoBarWindowID = {0};
WindowID SearchWindowID = {0};
Array<Buffer> Buffers = {}; Array<Buffer> Buffers = {};
Array<View> Views = {}; Array<View> Views = {};