Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/ted_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,39 @@ void reserve_line_cap(Line *ln, size_t x) {
}
}

void search_fwd(Buffer* buf, const char* pat) {
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);
Comment thread
rayworks marked this conversation as resolved.

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) {
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
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 = gi_to_wi(x, curr_line);
buf->cursor.y = y_pos;

recalc_cur(buf);
}
}

1 change: 1 addition & 0 deletions src/ted_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 12 additions & 0 deletions src/ted_commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -86,6 +96,7 @@ struct {
{"next" , next },
{"prev" , prev },
{"close" , close_buffer},
{"search" , search},
{NULL, NULL}
};

Expand All @@ -99,6 +110,7 @@ Hints hints[] = {
{"next" , "" },
{"prev" , "" },
{"close" , "" },
{"search" , "<keyword>" },
{NULL, NULL}
};

Expand Down
3 changes: 3 additions & 0 deletions src/ted_input.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down