diff options
| author | Albert Cervin <albert@acervin.com> | 2023-02-11 23:03:39 +0100 |
|---|---|---|
| committer | Albert Cervin <albert@acervin.com> | 2023-02-15 23:41:35 +0100 |
| commit | e45499816eab8abadbdd5bb6dd79b526a4ed6648 (patch) | |
| tree | 3cdcb0238aaae8ed1b3578e4ad71883f0702de3c /src/undo.h | |
| parent | c2976cea9bbca465712534b7e523783e2ccc6c6e (diff) | |
| download | dged-e45499816eab8abadbdd5bb6dd79b526a4ed6648.tar.gz dged-e45499816eab8abadbdd5bb6dd79b526a4ed6648.tar.xz dged-e45499816eab8abadbdd5bb6dd79b526a4ed6648.zip | |
Implement undo
This also fixes a bunch of valgrind errors
Diffstat (limited to 'src/undo.h')
| -rw-r--r-- | src/undo.h | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/src/undo.h b/src/undo.h new file mode 100644 index 0000000..42022c5 --- /dev/null +++ b/src/undo.h @@ -0,0 +1,67 @@ +#include <stdbool.h> +#include <stdint.h> + +enum undo_record_type { + Undo_Boundary = 1, + Undo_Add = 2, + Undo_Delete = 3, +}; + +struct position { + uint32_t row; + uint32_t col; +}; + +struct undo_boundary { + bool save_point; +}; + +struct undo_add { + struct position begin; + struct position end; +}; + +struct undo_delete { + struct position pos; + uint8_t *data; + uint32_t nbytes; +}; + +struct undo_record { + enum undo_record_type type; + + union { + struct undo_boundary boundary; + struct undo_add add; + struct undo_delete delete; + }; +}; + +#define INVALID_TOP -1 + +struct undo_stack { + struct undo_record *records; + uint32_t nrecords; + uint32_t top; + uint32_t capacity; + bool undo_in_progress; +}; + +void undo_init(struct undo_stack *undo, uint32_t initial_capacity); +void undo_clear(struct undo_stack *undo); +void undo_destroy(struct undo_stack *undo); + +uint32_t undo_push_boundary(struct undo_stack *undo, + struct undo_boundary boundary); + +uint32_t undo_push_add(struct undo_stack *undo, struct undo_add add); +uint32_t undo_push_delete(struct undo_stack *undo, struct undo_delete delete); + +void undo_begin(struct undo_stack *undo); +void undo_next(struct undo_stack *undo, struct undo_record **records_out, + uint32_t *nrecords_out); +void undo_end(struct undo_stack *undo); + +uint32_t undo_size(struct undo_stack *undo); +uint32_t undo_current_position(struct undo_stack *undo); +const char *undo_dump(struct undo_stack *undo); |
