summaryrefslogtreecommitdiff
path: root/src/dged/buffer.c
diff options
context:
space:
mode:
authorAlbert Cervin <albert@acervin.com>2025-11-26 21:31:39 +0100
committerAlbert Cervin <albert@acervin.com>2025-11-26 21:42:27 +0100
commit7096e5f7f2dd6e00d1440990cde88fd28feca5a1 (patch)
treec76372130418e244316b3fb52c9b51103baf49ad /src/dged/buffer.c
parent8de2328d2e712fc892d6f02d92feb88fa857e85b (diff)
downloaddged-7096e5f7f2dd6e00d1440990cde88fd28feca5a1.tar.gz
dged-7096e5f7f2dd6e00d1440990cde88fd28feca5a1.tar.xz
dged-7096e5f7f2dd6e00d1440990cde88fd28feca5a1.zip
Add proper undo to buffer reloading
It now creates an undo entry for the whole file.
Diffstat (limited to 'src/dged/buffer.c')
-rw-r--r--src/dged/buffer.c27
1 files changed, 25 insertions, 2 deletions
diff --git a/src/dged/buffer.c b/src/dged/buffer.c
index fed8f87..c3f5655 100644
--- a/src/dged/buffer.c
+++ b/src/dged/buffer.c
@@ -234,8 +234,6 @@ static void buffer_read_from_file(struct buffer *b) {
free(fullname);
return;
}
-
- undo_push_boundary(&b->undo, (struct undo_boundary){.save_point = true});
}
static void write_line_lf(struct text_chunk *chunk, void *userdata) {
@@ -388,6 +386,7 @@ struct buffer buffer_from_file(const char *path) {
char *full_path = to_abspath(path);
struct buffer b = create_internal(basename((char *)path), full_path);
buffer_read_from_file(&b);
+ undo_push_boundary(&b.undo, (struct undo_boundary){.save_point = true});
dispatch_hook(&g_create_hooks, struct create_hook, &b);
@@ -470,6 +469,8 @@ void buffer_set_filename(struct buffer *buffer, const char *filename) {
buffer->modified = true;
}
+static void *text_alloc(size_t sz) { return calloc(sz, 1); }
+
void buffer_reload(struct buffer *buffer) {
if (buffer->filename == NULL) {
return;
@@ -484,8 +485,26 @@ void buffer_reload(struct buffer *buffer) {
if (sb.st_mtim.tv_sec != buffer->last_write.tv_sec ||
sb.st_mtim.tv_nsec != buffer->last_write.tv_nsec) {
+
+ undo_push_boundary(&buffer->undo,
+ (struct undo_boundary){.save_point = false});
+
+ struct text_chunk txt = buffer_text(buffer);
+ undo_push_delete(
+ &buffer->undo,
+ (struct undo_delete){.data = txt.text,
+ .nbytes = txt.nbytes,
+ .pos = ((struct position){.col = 0, .row = 0})});
text_clear(buffer->text);
+
buffer_read_from_file(buffer);
+ undo_push_add(
+ &buffer->undo,
+ (struct undo_add){.begin = (struct position){.col = 0, .row = 0},
+ .end = (struct position){
+ .col = 0, .row = buffer_num_lines(buffer)}});
+ undo_push_boundary(&buffer->undo,
+ (struct undo_boundary){.save_point = true});
dispatch_hook(&buffer->hooks->reload_hooks, struct reload_hook, buffer);
}
}
@@ -1081,6 +1100,10 @@ struct text_chunk buffer_line(struct buffer *buffer, uint32_t line) {
return text_get_line(buffer->text, line);
}
+struct text_chunk buffer_text(struct buffer *buffer) {
+ return text_get(buffer->text, text_alloc);
+}
+
struct text_chunk buffer_region(struct buffer *buffer, struct region region) {
struct location begin_bytes =
buffer_location_to_byte_coords(buffer, region.begin);