summaryrefslogtreecommitdiff
path: root/txt.c
diff options
context:
space:
mode:
authorWormHeamer2026-01-02 00:22:17 -0500
committerWormHeamer2026-01-02 00:22:17 -0500
commit8fa897bdfdfef633e669cd980d856ea05ef83022 (patch)
treed20162d8fe8cf8343a53cb63c5ead463c7f4033c /txt.c
parentcff961a844962fcd61ee04bbe67777d442d0c76f (diff)
undo/redo
Diffstat (limited to 'txt.c')
-rw-r--r--txt.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/txt.c b/txt.c
index 0a24b22..3107b94 100644
--- a/txt.c
+++ b/txt.c
@@ -258,6 +258,49 @@ void txt_free(Txt *t) {
free(t->ptbl.v);
}
+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 (t->hist.i + 1 < t->hist.n) t->hist.n = t->hist.i + 1;
+ if (t->hist.n == TXT_HIST_MAX) {
+ free(t->hist.v[0].v);
+ MOVE(&t->hist.v[0], &t->hist.v[1], TXT_HIST_MAX - 1);
+ t->hist.i--;
+ t->hist.n--;
+ }
+ if (t->hist.i + 1 == t->hist.n) t->hist.i++;
+ 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.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++;
+ 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;
+}
+
+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;
+}
+
Str txt_collect_range(TxtLoc lo, TxtLoc hi, Arena *a) {
DYNARR(char) buf = { 0 };
while (lo.p < hi.p) {