diff options
author | Leo Tenenbaum <pommicket@gmail.com> | 2021-02-09 09:37:41 -0500 |
---|---|---|
committer | Leo Tenenbaum <pommicket@gmail.com> | 2021-02-09 09:37:41 -0500 |
commit | f9afc00d15f934d8509f3eeb7c62b01ca84ca204 (patch) | |
tree | 779ee27d67666fd3d99794bb37a2f086e06bee6b /buffer.c | |
parent | 91c98a180acab2152f4c60070a4bac3a98d9d486 (diff) |
improve buffer_unsaved_changes
so that making an edit then undoing it doesn't count as making a change
Diffstat (limited to 'buffer.c')
-rw-r--r-- | buffer.c | 23 |
1 files changed, 12 insertions, 11 deletions
@@ -34,6 +34,9 @@ static void buffer_clear_redo_history(TextBuffer *buffer) { buffer_edit_free(edit); } arr_clear(buffer->redo_history); + // if the write pos is in the redo history, + if (buffer->undo_history_write_pos > arr_len(buffer->undo_history)) + buffer->undo_history_write_pos = U32_MAX; // get rid of it } static void buffer_clear_undo_history(TextBuffer *buffer) { @@ -41,8 +44,13 @@ static void buffer_clear_undo_history(TextBuffer *buffer) { buffer_edit_free(edit); } arr_clear(buffer->undo_history); + buffer->undo_history_write_pos = U32_MAX; } +static void buffer_clear_undo_redo(TextBuffer *buffer) { + buffer_clear_undo_history(buffer); + buffer_clear_redo_history(buffer); +} bool buffer_empty(TextBuffer *buffer) { return buffer->nlines == 1 && buffer->lines[0].len == 0; @@ -56,12 +64,6 @@ bool buffer_is_untitled(TextBuffer *buffer) { return streq(buffer->filename, TED_UNTITLED); } -// clear all undo and redo events -void buffer_clear_undo_redo(TextBuffer *buffer) { - buffer_clear_undo_history(buffer); - buffer_clear_redo_history(buffer); -} - // add this edit to the undo history static void buffer_append_edit(TextBuffer *buffer, BufferEdit const *edit) { // whenever an edit is made, clear the redo history @@ -138,7 +140,7 @@ static bool buffer_pos_valid(TextBuffer *buffer, BufferPos p) { bool buffer_unsaved_changes(TextBuffer *buffer) { if (buffer_is_untitled(buffer) && buffer_empty(buffer)) return false; // don't worry about empty untitled buffers - return buffer->modified; + return arr_len(buffer->undo_history) != buffer->undo_history_write_pos; } // code point at position. @@ -1885,11 +1887,10 @@ bool buffer_save(TextBuffer *buffer) { if (!buffer_haserr(buffer)) buffer_seterr(buffer, "Couldn't close file %s.", buffer->filename); } - bool success = !buffer_haserr(buffer); - if (success) { - buffer->modified = false; - } buffer->last_write_time = time_last_modified(buffer->filename); + bool success = !buffer_haserr(buffer); + if (success) + buffer->undo_history_write_pos = arr_len(buffer->undo_history); return success; } else { buffer_seterr(buffer, "Couldn't open file %s for writing: %s.", buffer->filename, strerror(errno)); |