summaryrefslogtreecommitdiff
path: root/src/dged/undo.c
blob: ad167c3c2d2c6d1b7ca6bf6b015590caac78930c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "undo.h"
#include "string.h"
#include "vec.h"

#include <stdio.h>
#include <stdlib.h>

void undo_init(struct undo_stack *undo, uint32_t initial_capacity) {
  undo->top = INVALID_TOP;
  undo->undo_in_progress = false;
  VEC_INIT(&undo->records, initial_capacity);
}

void undo_clear(struct undo_stack *undo) {
  undo->top = INVALID_TOP;
  VEC_CLEAR(&undo->records);
}

void undo_destroy(struct undo_stack *undo) {
  VEC_FOR_EACH(&undo->records, struct undo_record * rec) {
    if (rec->type == Undo_Delete && rec->data.delete.data != NULL &&
        rec->data.delete.nbytes > 0) {
      free(rec->data.delete.data);
    }
  }

  undo_clear(undo);

  VEC_DESTROY(&undo->records);
}

uint32_t undo_push_boundary(struct undo_stack *undo,
                            struct undo_boundary boundary) {

  // we can only have one save point
  if (boundary.save_point) {
    VEC_FOR_EACH(&undo->records, struct undo_record * rec) {
      if (rec->type == Undo_Boundary && rec->data.boundary.save_point) {
        rec->data.boundary.save_point = false;
      }
    }
  }

  VEC_APPEND(&undo->records, struct undo_record * rec);
  rec->type = Undo_Boundary;
  rec->data.boundary = boundary;

  if (!undo->undo_in_progress) {
    undo->top = VEC_SIZE(&undo->records) - 1;
  }

  return VEC_SIZE(&undo->records) - 1;
}

bool pos_equal(struct position *a, struct position *b) {
  return a->row == b->row && a->col == b->col;
}

uint32_t undo_push_add(struct undo_stack *undo, struct undo_add add) {

  // "compress"
  if (!VEC_EMPTY(&undo->records) &&
      VEC_BACK(&undo->records)->type == Undo_Add &&
      pos_equal(&VEC_BACK(&undo->records)->data.add.end, &add.begin)) {
    VEC_BACK(&undo->records)->data.add.end = add.end;
  } else {
    VEC_APPEND(&undo->records, struct undo_record * rec);
    rec->type = Undo_Add;
    rec->data.add = add;
  }

  if (!undo->undo_in_progress) {
    undo->top = VEC_SIZE(&undo->records) - 1;
  }

  return VEC_SIZE(&undo->records) - 1;
}

uint32_t undo_push_delete(struct undo_stack *undo, struct undo_delete delete) {
  VEC_APPEND(&undo->records, struct undo_record * rec);
  rec->type = Undo_Delete;
  rec->data.delete = delete;

  if (!undo->undo_in_progress) {
    undo->top = VEC_SIZE(&undo->records) - 1;
  }

  return VEC_SIZE(&undo->records) - 1;
}

void undo_begin(struct undo_stack *undo) { undo->undo_in_progress = true; }

void undo_next(struct undo_stack *undo, struct undo_record **records_out,
               uint32_t *nrecords_out) {
  *nrecords_out = 0;
  *records_out = NULL;

  if (VEC_EMPTY(&undo->records)) {
    return;
  }

  if (undo->top == INVALID_TOP) {
    // reset back to the top (redo)
    undo->top = VEC_SIZE(&undo->records) - 1;
  }

  uint32_t nrecords = 1;
  struct undo_record *current = &VEC_ENTRIES(&undo->records)[undo->top];

  // skip any leading boundaries
  while (undo->top > 0 && current->type == Undo_Boundary) {
    ++nrecords;
    --undo->top;
    current = &VEC_ENTRIES(&undo->records)[undo->top];
  }

  // find the next boundary
  while (undo->top > 0 && current->type != Undo_Boundary) {
    ++nrecords;
    --undo->top;
    current = &VEC_ENTRIES(&undo->records)[undo->top];
  }

  if (nrecords > 0) {
    *records_out = calloc(nrecords, sizeof(struct undo_record));
    *nrecords_out = nrecords;

    struct undo_record *dest = *records_out;

    // copy backwards
    for (uint32_t reci = undo->top + nrecords, outi = 0; reci > undo->top;
         --reci, ++outi) {
      dest[outi] = VEC_ENTRIES(&undo->records)[reci - 1];
    }
  }

  if (undo->top > 0) {
    --undo->top;
  } else {
    undo->top = INVALID_TOP;
  }
}

void undo_end(struct undo_stack *undo) { undo->undo_in_progress = false; }

uint32_t undo_size(struct undo_stack *undo) { return VEC_SIZE(&undo->records); }
uint32_t undo_current_position(struct undo_stack *undo) { return undo->top; }

size_t rec_to_str(struct undo_record *rec, char *buffer, size_t n) {
  switch (rec->type) {
  case Undo_Add:
    return snprintf(buffer, n, "add { begin: (%d, %d) end: (%d, %d)}",
                    rec->data.add.begin.row, rec->data.add.begin.col,
                    rec->data.add.end.row, rec->data.add.end.col);
  case Undo_Delete:
    return snprintf(buffer, n, "delete { pos: (%d, %d), ptr: 0x%p, nbytes: %d}",
                    rec->data.delete.pos.row, rec->data.delete.pos.col,
                    (void *)rec->data.delete.data, rec->data.delete.nbytes);
  default:
    return snprintf(buffer, n, "boundary { save_point: %s }",
                    rec->data.boundary.save_point ? "yes" : "no");
  }
}

const char *undo_dump(struct undo_stack *undo) {
  uint32_t left = 8192;
  const char *buf = malloc(left);
  char *pos = (char *)buf;
  pos[0] = '\0';

  char rec_buf[256];
  VEC_FOR_EACH_INDEXED(&undo->records, struct undo_record * rec, reci) {
    rec_to_str(rec, rec_buf, 256);
    uint32_t written = snprintf(pos, left, "%d: [%s]%s\n", reci, rec_buf,
                                reci == undo->top ? " <- top" : "");
    left = written > left ? 0 : left - written;
    pos += written;
  }

  return buf;
}