summaryrefslogtreecommitdiff
path: root/txt.c
diff options
context:
space:
mode:
Diffstat (limited to 'txt.c')
-rw-r--r--txt.c41
1 files changed, 25 insertions, 16 deletions
diff --git a/txt.c b/txt.c
index 3107b94..a7bf447 100644
--- a/txt.c
+++ b/txt.c
@@ -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) {