-
Notifications
You must be signed in to change notification settings - Fork 1
/
undo.c
248 lines (202 loc) · 5 KB
/
undo.c
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/* undo.c - undo functions
* Copyright (C) 1988-2018 Sean MacLennan
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* along with this project; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "buff.h"
/** @addtogroup zedit
* @{
*/
#if defined(UNDO) && UNDO
#include <stdlib.h>
#include <string.h>
/* We cannot use marks here since pages may come and go and the marks
* become useless. Use an offset instead.
*/
struct undo {
struct undo *prev;
Byte *data;
unsigned long offset;
struct mark mrk;
int size;
};
#define is_insert(u) ((u)->data == NULL)
static struct undo *new_undo(struct buff *buff, void **tail,
int insert, int size)
{
struct undo *undo = (struct undo *)calloc(1, sizeof(struct undo));
if (!undo)
return NULL;
undo->size = size;
undo->offset = blocation(buff);
bmrktopnt(buff, &undo->mrk);
if (!insert) {
undo->data = (Byte *)malloc(size);
if (!undo->data) {
free(undo);
return NULL;
}
}
undo->prev = *tail;
*tail = undo;
return undo;
}
static void free_undo(void **tail)
{
struct undo *undo = (struct undo *)*tail;
if (undo) {
*tail = undo->prev;
free(undo->data);
free(undo);
}
}
static int add_clumped(struct buff *buff, struct undo *undo, int size)
{
int left;
struct mark *mrk = &undo->mrk;
/* move the undo mark */
left = mrk->mpage->plen - mrk->moffset;
if (left >= size)
mrk->moffset += size;
else {
size -= left;
while (size > 0) {
mrk->mpage = mrk->mpage->nextp;
mrk->moffset = 0;
if (!mrk->mpage)
return 0;
if (mrk->mpage->plen >= (unsigned)size) {
mrk->moffset = size;
size = 0;
} else
size -= mrk->mpage->plen;
}
}
return bisatmrk(buff, mrk);
}
/* Always within current buffer page... may not be current mrk page */
static int del_clumped(struct buff *buff, struct undo *undo, int size)
{
struct mark *mrk = &undo->mrk;
if (bisatmrk(buff, mrk))
return 1;
/* Move mark back */
if (mrk->moffset >= (unsigned)size) {
mrk->moffset -= size;
} else if (mrk->mpage->prevp) { /* paranoia */
mrk->mpage = mrk->mpage->prevp;
mrk->moffset = mrk->mpage->plen - size;
}
if (bisatmrk(buff, mrk))
return -1;
return 0;
}
/* Exports */
void undo_add(struct buff *buff, int size)
{
struct undo *undo = (struct undo *)buff->undo_tail;
if (buff->in_undo)
return;
if (undo && is_insert(undo) && add_clumped(buff, undo, size)) {
/* clump with last undo */
undo->size += size;
undo->offset += size;
} else
/* need a new undo */
undo = new_undo(buff, &buff->undo_tail, 1, size);
bmrktopnt(buff, &undo->mrk);
}
static void undo_append(struct undo *undo, Byte *data)
{
Byte *buf = (Byte *)realloc(undo->data, undo->size + 1);
if (!buf)
return;
buf[undo->size++] = *data;
undo->data = buf;
}
static void undo_prepend(struct undo *undo, Byte *data)
{
Byte *buf = (Byte *)realloc(undo->data, undo->size + 1);
if (!buf)
return;
/* Shift the old */
memmove(buf + 1, buf, undo->size);
/* Move in the new */
*buf = *data;
undo->data = buf;
undo->size++;
}
/* Size is always within the current page. */
void undo_del(struct buff *buff, int size)
{
struct undo *undo = (struct undo *)buff->undo_tail;
if (buff->in_undo)
return;
if (size == 0) /* this can happen on page boundaries */
return;
if (undo && !is_insert(undo))
switch (del_clumped(buff, undo, size)) {
case 1: /* delete forward */
undo_append(undo, buff->curcptr);
return;
case -1: /* delete previous */
undo_prepend(undo, buff->curcptr);
undo->offset--;
return;
}
/* need a new undo */
undo = new_undo(buff, &buff->undo_tail, 0, size);
if (undo == NULL)
return;
memcpy(undo->data, buff->curcptr, size);
}
void undo_clear(struct buff *buff)
{
while (buff->undo_tail)
free_undo(&buff->undo_tail);
}
int do_undo(struct buff *buff)
{
struct undo *undo;
int i;
if (!buff->undo_tail)
return 1;
undo = (struct undo *)buff->undo_tail;
buff->in_undo = 1;
boffset(buff, undo->offset);
if (is_insert(undo)) {
bmove(buff, -undo->size);
bdelete(buff, undo->size);
free_undo(&buff->undo_tail);
} else {
unsigned long offset = undo->offset;
struct mark *tmark = bcremark(buff);
if (!tmark)
return 1;
do {
for (i = 0; i < undo->size; ++i)
binsert(buff, undo->data[i]);
free_undo(&buff->undo_tail);
undo = (struct undo *)buff->undo_tail;
} while (undo && !is_insert(undo) && undo->offset == offset);
bpnttomrk(buff, tmark);
bdelmark(tmark);
}
buff->in_undo = 0;
return 0;
}
#endif
/* @} */