From 7391d9be671cd303fdebbf12bb9bc26958d13bf4 Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Sun, 14 Mar 2021 17:17:25 -0400 Subject: fix bug where modifications weren't detected also fix find scroll bug REALLY for real this time! --- buffer.c | 52 ++++++++++++++++++++++++++++------------------------ build.c | 32 ++++++++++++++++---------------- command.c | 16 ++++++++++------ ted.c | 10 ++++++++-- ted.h | 1 + 5 files changed, 63 insertions(+), 48 deletions(-) diff --git a/buffer.c b/buffer.c index c4143c9..bdf0c01 100644 --- a/buffer.c +++ b/buffer.c @@ -593,7 +593,8 @@ static bool buffer_edit_split(TextBuffer *buffer) { if (buffer->chaining_edits) return false; double curr_time = time_get_seconds(); double undo_time_cutoff = buffer_settings(buffer)->undo_save_time; // only keep around edits for this long (in seconds). - return curr_time - last_edit->time > undo_time_cutoff; + return last_edit->time <= timespec_to_seconds(buffer->last_write_time) // last edit happened before buffer write (we need to split this so that undo_history_write_pos works) + || curr_time - last_edit->time > undo_time_cutoff; } // removes the last edit in the undo history if it doesn't do anything @@ -1350,7 +1351,7 @@ BufferPos buffer_insert_text_at_pos(TextBuffer *buffer, BufferPos pos, String32 i64 where_in_last_edit = last_edit ? buffer_pos_diff(buffer, last_edit->pos, pos) : -1; // create a new edit, rather than adding to the old one if: bool create_new_edit = where_in_last_edit < 0 || where_in_last_edit > last_edit->new_len // insertion is happening outside the previous edit, - || buffer_edit_split(buffer); // or enough time has elapsed to warrant a new one. + || buffer_edit_split(buffer); // or enough time has elapsed/etc to warrant a new one. if (create_new_edit) { // create a new edit for this insertion @@ -1950,6 +1951,8 @@ void buffer_paste(TextBuffer *buffer) { Status buffer_load_file(TextBuffer *buffer, char const *filename) { FILE *fp = fopen(filename, "rb"); bool success = true; + Line *lines = NULL; + u32 nlines = 0, lines_capacity = 0; if (fp) { fseek(fp, 0, SEEK_END); long file_pos = ftell(fp); @@ -1964,10 +1967,10 @@ Status buffer_load_file(TextBuffer *buffer, char const *filename) { } else { u8 *file_contents = buffer_calloc(buffer, 1, file_size); if (file_contents) { - u32 lines_capacity = 4; - Line *lines = buffer_calloc(buffer, lines_capacity, sizeof *buffer->lines); // initial lines + lines_capacity = 4; + lines = buffer_calloc(buffer, lines_capacity, sizeof *buffer->lines); // initial lines if (lines) { - u32 nlines = 1; + nlines = 1; size_t bytes_read = fread(file_contents, 1, file_size, fp); if (bytes_read == file_size) { char32_t c = 0; @@ -2000,25 +2003,6 @@ Status buffer_load_file(TextBuffer *buffer, char const *filename) { buffer_line_append_char(buffer, line, c); } } - if (success) { - char *filename_copy = buffer_strdup(buffer, filename); - if (!filename_copy) success = false; - if (success) { - // everything is good - buffer_clear(buffer); - buffer->lines = lines; - buffer->nlines = nlines; - buffer->frame_earliest_line_modified = 0; - buffer->frame_latest_line_modified = nlines - 1; - buffer->lines_capacity = lines_capacity; - buffer->filename = filename_copy; - buffer->last_write_time = time_last_modified(buffer->filename); - if (!(fs_path_permission(filename) & FS_PERMISSION_WRITE)) { - // can't write to this file; make the buffer view only. - buffer->view_only = true; - } - } - } } else { success = false; } @@ -2044,6 +2028,25 @@ Status buffer_load_file(TextBuffer *buffer, char const *filename) { buffer_seterr(buffer, "Couldn't open file %s: %s.", filename, strerror(errno)); success = false; } + if (success) { + char *filename_copy = buffer_strdup(buffer, filename); + if (!filename_copy) success = false; + if (success) { + // everything is good + buffer_clear(buffer); + buffer->lines = lines; + buffer->nlines = nlines; + buffer->frame_earliest_line_modified = 0; + buffer->frame_latest_line_modified = nlines - 1; + buffer->lines_capacity = lines_capacity; + buffer->filename = filename_copy; + buffer->last_write_time = time_last_modified(buffer->filename); + if (!(fs_path_permission(filename) & FS_PERMISSION_WRITE)) { + // can't write to this file; make the buffer view only. + buffer->view_only = true; + } + } + } return success; } @@ -2088,6 +2091,7 @@ void buffer_new_file(TextBuffer *buffer, char const *filename) { // whether there are any unsaved changes. bool buffer_save(TextBuffer *buffer) { Settings const *settings = buffer_settings(buffer); + if (!buffer->is_line_buffer && buffer->filename) { if (buffer->view_only) { buffer_seterr(buffer, "Can't save view-only file."); diff --git a/build.c b/build.c index 7f8e9bf..cc6899d 100644 --- a/build.c +++ b/build.c @@ -22,22 +22,22 @@ static void build_start_with_command(Ted *ted, char const *command) { build_stop(ted); } build_clear(ted); // clear errors from previous build - ted_save_all(ted); - - if (process_run(&ted->build_process, command)) { - ted->building = true; - ted->build_shown = true; - TextBuffer *build_buffer = &ted->build_buffer; - // new empty build output buffer - buffer_new_file(build_buffer, NULL); - build_buffer->store_undo_events = false; // don't need undo events for build output buffer - char32_t text[] = {'$', ' '}; - buffer_insert_text_at_cursor(build_buffer, str32(text, 2)); - buffer_insert_utf8_at_cursor(build_buffer, command); - buffer_insert_char_at_cursor(build_buffer, '\n'); - build_buffer->view_only = true; - } else { - ted_seterr(ted, "Couldn't start build: %s", process_geterr(&ted->build_process)); + if (ted_save_all(ted)) { + if (process_run(&ted->build_process, command)) { + ted->building = true; + ted->build_shown = true; + TextBuffer *build_buffer = &ted->build_buffer; + // new empty build output buffer + buffer_new_file(build_buffer, NULL); + build_buffer->store_undo_events = false; // don't need undo events for build output buffer + char32_t text[] = {'$', ' '}; + buffer_insert_text_at_cursor(build_buffer, str32(text, 2)); + buffer_insert_utf8_at_cursor(build_buffer, command); + buffer_insert_char_at_cursor(build_buffer, '\n'); + build_buffer->view_only = true; + } else { + ted_seterr(ted, "Couldn't start build: %s", process_geterr(&ted->build_process)); + } } } diff --git a/command.c b/command.c index 310c76d..402686c 100644 --- a/command.c +++ b/command.c @@ -329,12 +329,16 @@ void command_execute(Ted *ted, Command c, i64 argument) { ted->autocomplete = false; } else if (ted->menu) { menu_escape(ted); - } else if (ted->find) { - find_close(ted); - } else if (ted->build_shown) { - build_stop(ted); - } else if (buffer) { - buffer_disable_selection(buffer); + } else { + if (ted->find) { + find_close(ted); + } + if (ted->build_shown) { + build_stop(ted); + } + if (buffer) { + buffer_disable_selection(buffer); + } } break; diff --git a/ted.c b/ted.c index 9e4fe1b..bd3ac46 100644 --- a/ted.c +++ b/ted.c @@ -106,9 +106,13 @@ static void ted_load_fonts(Ted *ted) { // sets the active buffer to this buffer, and updates active_node, etc. accordingly // you can pass NULL to buffer to make it so no buffer is active. void ted_switch_to_buffer(Ted *ted, TextBuffer *buffer) { + TextBuffer *search_buffer = find_search_buffer(ted); ted->active_buffer = buffer; ted->autocomplete = false; - if (ted->find) find_update(ted, true); + if (buffer != search_buffer) { + if (ted->find) + find_update(ted, true); // make sure find results are for this file + } if (buffer >= ted->buffers && buffer < ted->buffers + TED_MAX_BUFFERS) { ted->prev_active_buffer = buffer; @@ -303,8 +307,10 @@ static bool ted_save_all(Ted *ted) { success = false; // we haven't saved this buffer yet; we've just opened the "save as" menu. break; } else { - if (!buffer_save(buffer)) + if (!buffer_save(buffer)) { success = false; + ted_seterr_to_buferr(ted, buffer); + } } } } diff --git a/ted.h b/ted.h index 3628c54..a4bf0ca 100644 --- a/ted.h +++ b/ted.h @@ -342,3 +342,4 @@ typedef struct Ted { void command_execute(Ted *ted, Command c, i64 argument); void ted_switch_to_buffer(Ted *ted, TextBuffer *buffer); +static TextBuffer *find_search_buffer(Ted *ted); -- cgit v1.2.3