From 4f2a012a5dbad755bd621a3308a306afd4589fd5 Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Wed, 10 Feb 2021 10:17:06 -0500 Subject: make find+replace more convenient --- command.c | 13 ++++++++++++- command.h | 2 ++ find.c | 54 +++++++++++++++++++++++++++--------------------------- main.c | 1 + ted.cfg | 1 + 5 files changed, 43 insertions(+), 28 deletions(-) diff --git a/command.c b/command.c index 19c291e..1d628b0 100644 --- a/command.c +++ b/command.c @@ -98,7 +98,18 @@ void command_execute(Ted *ted, Command c, i64 argument) { break; case CMD_TAB: - buffer_insert_char_at_cursor(buffer, '\t'); + if (ted->replace && ted->active_buffer == &ted->find_buffer) { + ted->active_buffer = &ted->replace_buffer; + buffer_select_all(ted->active_buffer); + } else { + buffer_insert_char_at_cursor(buffer, '\t'); + } + break; + case CMD_BACKTAB: + if (ted->replace && ted->active_buffer == &ted->replace_buffer) { + ted->active_buffer = &ted->find_buffer; + buffer_select_all(ted->active_buffer); + } break; case CMD_NEWLINE: case CMD_NEWLINE_BACK: diff --git a/command.h b/command.h index fcdde38..e366f17 100644 --- a/command.h +++ b/command.h @@ -26,6 +26,7 @@ ENUM_U16 { // insertion CMD_TAB, // insert '\t' + CMD_BACKTAB, CMD_NEWLINE, // insert '\n' + autoindent -- also used to submit line buffers CMD_NEWLINE_BACK, @@ -98,6 +99,7 @@ static CommandName const command_names[CMD_COUNT] = { {"page-up", CMD_PAGE_UP}, {"page-down", CMD_PAGE_DOWN}, {"tab", CMD_TAB}, + {"backtab", CMD_BACKTAB}, {"newline", CMD_NEWLINE}, {"newline-back", CMD_NEWLINE_BACK}, {"backspace", CMD_BACKSPACE}, diff --git a/find.c b/find.c index 6b237f2..48a9299 100644 --- a/find.c +++ b/find.c @@ -58,25 +58,6 @@ static void find_free_pattern(Ted *ted) { arr_clear(ted->find_results); } -static void find_open(Ted *ted, bool replace) { - if (!ted->find && ted->active_buffer) { - ted->prev_active_buffer = ted->active_buffer; - ted->active_buffer = &ted->find_buffer; - ted->find = true; - buffer_clear(&ted->find_buffer); - } - if (!ted->replace && replace) { - ted->replace = true; - buffer_clear(&ted->replace_buffer); - } -} - -static void find_close(Ted *ted) { - ted->find = false; - ted->active_buffer = ted->prev_active_buffer; - find_free_pattern(ted); -} - static float find_menu_height(Ted *ted) { Font *font = ted->font; float char_height = text_font_char_height(font); @@ -130,10 +111,11 @@ static WarnUnusedResult bool find_match(Ted *ted, BufferPos *pos, u32 *match_sta } // check if the search term needs to be recompiled -static void find_update(Ted *ted) { +static void find_update(Ted *ted, bool force) { TextBuffer *find_buffer = &ted->find_buffer; u32 flags = find_compilation_flags(ted); - if (!find_buffer->modified // check if buffer has been modified, + if (!force + && !find_buffer->modified // check if buffer has been modified, && ted->find_flags == flags) // or checkboxes have been (un)checked return; ted->find_flags = flags; @@ -202,6 +184,9 @@ static void find_next_in_direction(Ted *ted, int direction) { // returns true if successful static bool find_replace_match(Ted *ted, u32 match_idx) { + find_update(ted, false); + if (!ted->find_code) return false; + bool success = false; FindResult match = ted->find_results[match_idx]; TextBuffer *buffer = ted->prev_active_buffer; @@ -268,16 +253,12 @@ static void find_replace(Ted *ted) { // go to next find result static void find_next(Ted *ted) { - find_update(ted); - if (!ted->find_code) return; - if (ted->replace) find_replace(ted); find_next_in_direction(ted, +1); } static void find_prev(Ted *ted) { - find_update(ted); find_next_in_direction(ted, -1); } @@ -296,7 +277,7 @@ static void find_replace_all(Ted *ted) { if (!find_replace_match(ted, i)) break; } - find_update(ted); + find_update(ted, true); } } } @@ -363,7 +344,7 @@ static void find_menu_frame(Ted *ted) { find_replace_all(ted); } - find_update(ted); + find_update(ted, false); arr_foreach_ptr(ted->find_results, FindResult, result) { // highlight matches BufferPos p1 = result->start, p2 = result->end; @@ -450,3 +431,22 @@ static void find_menu_frame(Ted *ted) { gl_geometry_draw(); } + +static void find_open(Ted *ted, bool replace) { + if (!ted->find && ted->active_buffer) { + ted->prev_active_buffer = ted->active_buffer; + ted->active_buffer = &ted->find_buffer; + ted->find = true; + buffer_select_all(ted->active_buffer); + } + if (!ted->replace && replace) { + ted->replace = true; + } + find_update(ted, true); +} + +static void find_close(Ted *ted) { + ted->find = false; + ted->active_buffer = ted->prev_active_buffer; + find_free_pattern(ted); +} diff --git a/main.c b/main.c index 7d13359..4036a1f 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,7 @@ // @TODO: // - chained undos (replace all = one undo action) // - highlight matching parentheses +// - indent/dedent region // - split // - completion // - view-only diff --git a/ted.cfg b/ted.cfg index 24d2e56..60b9929 100644 --- a/ted.cfg +++ b/ted.cfg @@ -54,6 +54,7 @@ Ctrl+a = :select-all # insertion Tab = :tab +Shift+Tab = :backtab Enter = :newline # same as :newline for normal text insertion, but goes to the previous search result instead of the next one Shift+Enter = :newline-back -- cgit v1.2.3