diff options
-rw-r--r-- | buffer.c | 9 | ||||
-rw-r--r-- | command.c | 6 | ||||
-rw-r--r-- | main.c | 2 | ||||
-rw-r--r-- | menu.c | 17 | ||||
-rw-r--r-- | string32.c | 15 | ||||
-rw-r--r-- | ted.h | 6 |
6 files changed, 39 insertions, 16 deletions
@@ -137,6 +137,15 @@ static inline Settings *buffer_settings(TextBuffer *buffer) { return &buffer->ted->settings; } +// NOTE: this string will be invalidated when the line is edited!!! +// only use it briefly!! +static String32 buffer_get_line(TextBuffer *buffer, u32 line_number) { + Line *line = &buffer->lines[line_number]; + return (String32) { + .str = line->str, .len = line->len + }; +} + // Returns a simple checksum of the buffer. // This is only used for testing, and shouldn't be relied on. static u64 buffer_checksum(TextBuffer *buffer) { @@ -157,14 +157,12 @@ void command_execute(Ted *ted, Command c, i64 argument) { break; case MENU_OPEN: { TextBuffer *open_to = &ted->main_buffer; - String32 filename32 = {.str = buffer->lines[0].str, .len = buffer->lines[0].len}; - - char *filename_cstr = str32_to_utf8_cstr(filename32); + char *filename_cstr = str32_to_utf8_cstr(buffer_get_line(&ted->line_buffer, 0)); if (filename_cstr) { buffer_load_file(open_to, filename_cstr); buffer = open_to; if (buffer_haserr(open_to)) { - // @TODO: something better + // @TODO: something } free(filename_cstr); menu_close(ted, true); @@ -321,7 +321,7 @@ int main(int argc, char **argv) { case SDL_TEXTINPUT: { char *text = event.text.text; if (buffer - && key_modifier == 0) // unfortunately, some key combinations like ctrl+minus still register as a "-" text input event + && (key_modifier & ~KEY_MODIFIER_SHIFT) == 0) // unfortunately, some key combinations like ctrl+minus still register as a "-" text input event buffer_insert_utf8_at_cursor(buffer, text); } break; } @@ -31,8 +31,10 @@ static void menu_render(Ted *ted, Menu menu) { gl_color_rgba(colors[COLOR_MENU_BACKDROP]); rect_render(rect(V2(0, 0), V2(window_width, window_height))); glEnd(); + if (menu == MENU_OPEN) { + char *search_term = str32_to_utf8_cstr(buffer_get_line(&ted->line_buffer, 0)); char const *directory = "."; float padding = 20; float menu_x1 = window_width * 0.5f - 300; @@ -62,8 +64,21 @@ static void menu_render(Ted *ted, Menu menu) { char **files = fs_list_directory(directory); if (files) { + u32 nfiles = 0; for (char **p = files; *p; ++p) ++nfiles; + + if (search_term && *search_term) { + // filter files based on search term + u32 in, out = 0; + for (in = 0; in < nfiles; ++in) { + if (stristr(files[in], search_term)) { + files[out++] = files[in]; + } + } + nfiles = out; + } + qsort(files, nfiles, sizeof *files, str_qsort_case_insensitive_cmp); { // render file names @@ -79,5 +94,7 @@ static void menu_render(Ted *ted, Menu menu) { for (u32 i = 0; i < nfiles; ++i) free(files[i]); free(files); } + free(search_term); } + } @@ -91,18 +91,17 @@ size_t str32_count_char(String32 s, char32_t c) { // returns number of characters deleted from s size_t str32_remove_all_instances_of_char(String32 *s, char32_t c) { - bool increment = true; char32_t *str = s->str; size_t ndeleted = 0; - for (size_t i = 0; i < s->len; i += increment, increment = true) { - if (str[i] == c) { - --s->len; - if (i < s->len) { - str[i] = str[i+1]; - } + size_t len = s->len; + size_t out = 0; + for (size_t in = 0; in < len; ++in) { + if (str[in] == c) { ++ndeleted; - increment = false; + } else { + str[out++] = str[in]; } } + s->len = out; return ndeleted; } @@ -19,9 +19,9 @@ typedef struct { #define KEY_MODIFIER_CTRL_BIT 0 #define KEY_MODIFIER_SHIFT_BIT 1 #define KEY_MODIFIER_ALT_BIT 2 -#define KEY_MODIFIER_CTRL (1<<KEY_MODIFIER_CTRL_BIT) -#define KEY_MODIFIER_SHIFT (1<<KEY_MODIFIER_SHIFT_BIT) -#define KEY_MODIFIER_ALT (1<<KEY_MODIFIER_ALT_BIT) +#define KEY_MODIFIER_CTRL ((u32)1<<KEY_MODIFIER_CTRL_BIT) +#define KEY_MODIFIER_SHIFT ((u32)1<<KEY_MODIFIER_SHIFT_BIT) +#define KEY_MODIFIER_ALT ((u32)1<<KEY_MODIFIER_ALT_BIT) // ctrl+alt+c is encoded as SDL_SCANCODE_C << 3 | KEY_MODIFIER_CTRL | KEY_MODIFIER_ALT typedef struct KeyAction { |