diff options
| author | WormHeamer | 2026-01-02 01:03:41 -0500 |
|---|---|---|
| committer | WormHeamer | 2026-01-02 01:03:41 -0500 |
| commit | 92d528ad2f406f85933c2a410594e9fc3da2c91e (patch) | |
| tree | 3db6420994f3487dafded21a4790528d2916bb57 | |
| parent | df49050cf32373ea2679ae71a659d0cdc5663e04 (diff) | |
set and unset dirty flag as appropriate on undo/redo
| -rw-r--r-- | main.c | 2 | ||||
| -rw-r--r-- | txt.c | 41 | ||||
| -rw-r--r-- | txt.h | 6 |
3 files changed, 30 insertions, 19 deletions
@@ -524,7 +524,7 @@ void draw_buf(EditBuf *eb) { VuiAttr a = i == e.bufi ? sel : norm; Str p = basename(b->path); x += vui_aprintf(x, y, a, " %.*s", (int)p.n, p.s); - if (b->type == ED_BUF_FILE && b->txt->dirty) { + if (b->type == ED_BUF_FILE && b->txt->ptbl.dirty) { x += vui_putsa(x, y, "* ", a); } else { vui_chra(x++, y, ' ', a); @@ -127,7 +127,7 @@ loop: goto loop; } ASSERT(txt_valid_loc(l)); - l.t->dirty = 1; + l.t->ptbl.dirty = 1; return resolve_loc(l); } @@ -237,6 +237,14 @@ void txt_load_empty(Txt *b) { txt_insert_piece(b, 0, TXT_ADD, 0, b->len); } +static inline int txt_hist_unchanged(Txt *t) { + if (t->hist.n < 1) return 0; + TxtPieceTbl *pt = &t->hist.v[t->hist.i]; + if (t->ptbl.n != pt->n) return 0; + if (memcmp(pt->v, t->ptbl.v, sizeof(TxtPiece) * pt->n)) return 0; + return 1; +} + int txt_save(Txt *t, const char *path) { FILE *f = fopen(path, "wb"); if (!f) return -1; @@ -247,7 +255,9 @@ int txt_save(Txt *t, const char *path) { } int e = ferror(f); fclose(f); - t->dirty = 0; + t->ptbl.dirty = 0; + for (u32 i = 0; i < t->hist.n; i++) t->hist.v[i].dirty = 1; + if (txt_hist_unchanged(t)) t->hist.v[t->hist.i].dirty = 0; return e ? -1 : 0; } @@ -259,12 +269,9 @@ void txt_free(Txt *t) { } void txt_hist_push(Txt *t, TxtLoc cur) { - if (t->hist.n > 0) { - TxtPieceTbl *pt = &t->hist.v[t->hist.i]; - if (t->ptbl.n == pt->n && !memcmp(pt->v, t->ptbl.v, sizeof(TxtPiece) * pt->n)) { - t->hist.cur[t->hist.i] = cur; - return; - } + if (txt_hist_unchanged(t)) { + t->hist.cur[t->hist.i] = cur; + return; } if (t->hist.i + 1 < t->hist.n) t->hist.n = t->hist.i + 1; if (t->hist.n == TXT_HIST_MAX) { @@ -277,28 +284,30 @@ void txt_hist_push(Txt *t, TxtLoc cur) { DA_FIT(&t->hist.v[t->hist.i], t->ptbl.n); memcpy(t->hist.v[t->hist.i].v, t->ptbl.v, sizeof(TxtPiece) * t->ptbl.n); t->hist.v[t->hist.i].n = t->ptbl.n; + t->hist.v[t->hist.i].dirty = t->ptbl.dirty; t->hist.cur[t->hist.i] = cur; t->hist.n++; } -int txt_hist_fwd(Txt *t, TxtLoc *cur) { - if (t->hist.i + 1 >= t->hist.n) return 0; - t->hist.i++; +int txt_hist_set(Txt *t, TxtLoc *cur) { t->ptbl.n = t->hist.v[t->hist.i].n; DA_FIT(&t->ptbl, t->ptbl.n); memcpy(t->ptbl.v, t->hist.v[t->hist.i].v, sizeof(TxtPiece) * t->ptbl.n); + t->ptbl.dirty = t->hist.v[t->hist.i].dirty; *cur = t->hist.cur[t->hist.i]; return 1; } +int txt_hist_fwd(Txt *t, TxtLoc *cur) { + if (t->hist.i + 1 >= t->hist.n) return 0; + t->hist.i++; + return txt_hist_set(t, cur); +} + int txt_hist_back(Txt *t, TxtLoc *cur) { if (t->hist.i == 0) return 0; t->hist.i--; - t->ptbl.n = t->hist.v[t->hist.i].n; - DA_FIT(&t->ptbl, t->ptbl.n); - memcpy(t->ptbl.v, t->hist.v[t->hist.i].v, sizeof(TxtPiece) * t->ptbl.n); - *cur = t->hist.cur[t->hist.i]; - return 1; + return txt_hist_set(t, cur); } Str txt_collect_range(TxtLoc lo, TxtLoc hi, Arena *a) { @@ -22,7 +22,10 @@ typedef struct { u32 n, c; } TxtBuf; -typedef DYNARR(TxtPiece) TxtPieceTbl; +typedef struct { + DYNARR(TxtPiece); + int dirty; +} TxtPieceTbl; typedef struct { struct Txt *t; @@ -40,7 +43,6 @@ typedef struct Txt { TxtHist hist; TxtBuf buf[2]; u32 len; - int dirty; } Txt; /* text buffer manipulation */ |
