From 434465b82c544646117741c3509ae64f8ac0083d Mon Sep 17 00:00:00 2001 From: rayworks Date: Tue, 12 Aug 2025 17:55:20 +0800 Subject: [PATCH 1/5] feat: implement forward search functionality - Implement search_fwd() function in ted_buffer.c * Search from current cursor position forward through buffer * Move cursor to first match found - Add search command to ted_commands.c * Prompt user for search keyword * Integrate with search_fwd() function - Add ctrl+r keybinding for search in ted_input.c - Add command hints for search functionality --- src/ted_buffer.c | 25 +++++++++++++++++++++++++ src/ted_buffer.h | 1 + src/ted_commands.c | 12 ++++++++++++ src/ted_input.c | 3 +++ 4 files changed, 41 insertions(+) diff --git a/src/ted_buffer.c b/src/ted_buffer.c index feb4f67..2d32442 100644 --- a/src/ted_buffer.c +++ b/src/ted_buffer.c @@ -250,3 +250,28 @@ void reserve_line_cap(Line *ln, size_t x) { } } +void search_fwd(Buffer* buf, const char* pat) { + size_t x_offset = buf->cursor.x_width; + size_t y_pos = buf->cursor.y; + char* curr_line = buf->lines[y_pos].data; + char* sub = strstr(curr_line + x_offset, pat); + + if (sub == NULL) { + y_pos++; + for (; y_pos < buf->num_lines; y_pos++) { + curr_line = buf->lines[y_pos].data; + sub = strstr(curr_line, pat); + if (sub != NULL) { + break; + } + } + } + + if (sub != NULL) { + size_t x_pos = wi_to_gi(sub - curr_line, curr_line); + buf->cursor.x_width = gi_to_wi(x_pos, curr_line); + buf->cursor.y = y_pos; + recalc_cur(buf); + } +} + diff --git a/src/ted_buffer.h b/src/ted_buffer.h index 4113f98..73c1fdb 100644 --- a/src/ted_buffer.h +++ b/src/ted_buffer.h @@ -73,6 +73,7 @@ bool modify_buffer(Buffer *buf); void add_char(Grapheme c, size_t x, Line *ln); void remove_char(size_t x, Line *ln); void reserve_line_cap(Line *ln, size_t x); +void search_fwd(Buffer* buf, const char* pat); #endif diff --git a/src/ted_commands.c b/src/ted_commands.c index e802776..6657ef6 100644 --- a/src/ted_commands.c +++ b/src/ted_commands.c @@ -73,6 +73,16 @@ DEF_COMMAND(close_buffer, { buffer_close(); }) +DEF_COMMAND(search, { + char msg[MSG_SZ] = "Keyword to be searched: "; + prompt_hints(msg, "input the keyword", NULL); + if (0 == strcmp("", msg)) { + return; + } + + search_fwd(&SEL_BUF, msg); +}) + struct { const char *name; void (*function)(char *words); @@ -86,6 +96,7 @@ struct { {"next" , next }, {"prev" , prev }, {"close" , close_buffer}, + {"search" , search}, {NULL, NULL} }; @@ -99,6 +110,7 @@ Hints hints[] = { {"next" , "" }, {"prev" , "" }, {"close" , "" }, + {"search" , "" }, {NULL, NULL} }; diff --git a/src/ted_input.c b/src/ted_input.c index 5710a5c..c81cdd2 100644 --- a/src/ted_input.c +++ b/src/ted_input.c @@ -23,6 +23,9 @@ void process_keypress(int c) { case ctrl('c'): { parse_command("close"); break; + } case ctrl('r'): { + parse_command("search"); + break; } case ctrl('z'): { parse_command("prev"); break; From 2f5e758df2430635ff9f53641c8e17dfe516b4ca Mon Sep 17 00:00:00 2001 From: rayworks Date: Tue, 19 Aug 2025 09:04:53 +0800 Subject: [PATCH 2/5] fix (search): calculate the cursor position (x coordinate) correctly --- src/ted_buffer.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/ted_buffer.c b/src/ted_buffer.c index 2d32442..1f54d08 100644 --- a/src/ted_buffer.c +++ b/src/ted_buffer.c @@ -251,7 +251,7 @@ void reserve_line_cap(Line *ln, size_t x) { } void search_fwd(Buffer* buf, const char* pat) { - size_t x_offset = buf->cursor.x_width; + size_t x_offset = buf->cursor.x_bytes; size_t y_pos = buf->cursor.y; char* curr_line = buf->lines[y_pos].data; char* sub = strstr(curr_line + x_offset, pat); @@ -268,8 +268,16 @@ void search_fwd(Buffer* buf, const char* pat) { } if (sub != NULL) { - size_t x_pos = wi_to_gi(sub - curr_line, curr_line); - buf->cursor.x_width = gi_to_wi(x_pos, curr_line); + buf->cursor.x_bytes = sub - curr_line; + + // calc visual width position + char *temp_ptr = curr_line; + buf->cursor.x_width = 0; + while (temp_ptr < sub) { + Grapheme g = get_next_grapheme(&temp_ptr, SIZE_MAX); + buf->cursor.x_width += grapheme_width(g); + } + buf->cursor.y = y_pos; recalc_cur(buf); } From 5e6f70b346d1b170627819565f820b13ff03c96b Mon Sep 17 00:00:00 2001 From: rayworks Date: Tue, 19 Aug 2025 16:37:53 +0800 Subject: [PATCH 3/5] refactor: use wi_to_gi() helper for cursor x_width calculation --- src/ted_buffer.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/ted_buffer.c b/src/ted_buffer.c index 1f54d08..1fca694 100644 --- a/src/ted_buffer.c +++ b/src/ted_buffer.c @@ -271,13 +271,7 @@ void search_fwd(Buffer* buf, const char* pat) { buf->cursor.x_bytes = sub - curr_line; // calc visual width position - char *temp_ptr = curr_line; - buf->cursor.x_width = 0; - while (temp_ptr < sub) { - Grapheme g = get_next_grapheme(&temp_ptr, SIZE_MAX); - buf->cursor.x_width += grapheme_width(g); - } - + buf->cursor.x_width = wi_to_gi(buf->cursor.x_bytes, curr_line); buf->cursor.y = y_pos; recalc_cur(buf); } From 90f2a5c3a8037bc577309cd97fb46be44a0e87ca Mon Sep 17 00:00:00 2001 From: rayworks Date: Wed, 20 Aug 2025 10:11:48 +0800 Subject: [PATCH 4/5] fix: update the calculation logic for cursor position --- src/ted_buffer.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/ted_buffer.c b/src/ted_buffer.c index 1fca694..eee6dfa 100644 --- a/src/ted_buffer.c +++ b/src/ted_buffer.c @@ -270,9 +270,18 @@ void search_fwd(Buffer* buf, const char* pat) { if (sub != NULL) { buf->cursor.x_bytes = sub - curr_line; + // the index of the grapheme cluster the cursor is at + size_t x = 0; + // calc visual width position - buf->cursor.x_width = wi_to_gi(buf->cursor.x_bytes, curr_line); + char *temp_ptr = curr_line; + while (temp_ptr < sub) { + Grapheme g = get_next_grapheme(&temp_ptr, SIZE_MAX); + x += grapheme_width(g); + } + buf->cursor.x_width = wi_to_gi(x, curr_line); buf->cursor.y = y_pos; + recalc_cur(buf); } } From 3fd7f730040866c734718117ad7ea7144f7d9816 Mon Sep 17 00:00:00 2001 From: rayworks Date: Thu, 21 Aug 2025 07:22:11 +0800 Subject: [PATCH 5/5] fix: use the correct function to convert grapheme index to width index --- src/ted_buffer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ted_buffer.c b/src/ted_buffer.c index eee6dfa..98e1023 100644 --- a/src/ted_buffer.c +++ b/src/ted_buffer.c @@ -279,7 +279,7 @@ void search_fwd(Buffer* buf, const char* pat) { Grapheme g = get_next_grapheme(&temp_ptr, SIZE_MAX); x += grapheme_width(g); } - buf->cursor.x_width = wi_to_gi(x, curr_line); + buf->cursor.x_width = gi_to_wi(x, curr_line); buf->cursor.y = y_pos; recalc_cur(buf);