File: undo.c

package info (click to toggle)
wily 0.13.33-3
  • links: PTS
  • area: main
  • in suites: slink
  • size: 1,500 kB
  • ctags: 1,720
  • sloc: ansic: 12,830; sh: 245; makefile: 188
file content (350 lines) | stat: -rw-r--r-- 6,763 bytes parent folder | download | duplicates (2)
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include "wily.h"
#include "text.h"

/* We currently manage our Undo chain as a linked list,
 * and separately alloc small buffers every time.
 * If we cared about efficiency, we'd make all the u->s
 * entries share bigger chunks of Runes.
 *
 * For now, it doesn't seem to be a problem, and simpler
 * is better.
 */
 
struct Undo {
	Undo	*next;
	Range	r;			/* where to replace */
	Rstring	s;			/* what to replace with */
	ulong	alloced;		/* bytes alloced at s.p0 */
};

static Bool undoing = false;	/* prevent recording undo ops */
static Range illegal_range = {10,1};

static Undo *	reverse	(Text*, Range, Rstring);
static Bool 	append	(Text*, Range r, Rstring s);
static void 	reset	(Undo**);
static void	save_state	(Text *);
static void	update_state	(Text *);
static Range	shift	(Text *, Undo **, Undo **, Bool );

static void
text_rmtool(Text*t, char*s)	{
	if(t->data)
		tag_rmtool(data_tag(t->data), s);
}

static void
text_addtool(Text*t, char*s)	{
	tag_addtool(data_tag(t->data), s);
}

/* Record information allowing us to undo the replacement of
 * 'r' in 't' with 's'.  This replacement hasn't happened yet.
 */
void
undo_record
(Text *t, Range r, Rstring s)
{
	Undo	*u;

	assert(ROK(r));
	assert(RSOK(s));
	assert(r.p1 <= t->length);

	if(undoing || t->undoing == NoUndo)
		return;

	save_state(t);

	if(!append(t, r, s)) {
		/* remove old t->undone trail */
		reset(&t->undone);

		u = reverse(t, r, s);
		u->next = t->did;
		t->did = u;
	}
	t->undoing = MoreUndo;

	update_state(t);
}

/* Undo the top operation in the undo stack, and move it to the redo
 * stack.  Keep going to mark if appropriate.
 * Return the range that was inserted.
 */
Range
undo_undo(Text *t, Bool all)
{
	Range r;
	
	if (!t->did)
		return illegal_range;

	save_state(t);
	
	do {
		r = shift(t, &t->did, &t->undone, true);
	} while (all && t->did && t->did != t->mark);
	
	update_state(t);
	return (all ? illegal_range : r);
}

/* The mirror of undo_undo */
Range
undo_redo(Text *t, Bool all)
{
	Range r;
	Undo *tmp;
	
	save_state(t);
	
	tmp = t->did;
	t->did = t->undone;
	t->undone = tmp;
	
	r = undo_undo(t, all);
	
	tmp = t->did;
	t->did = t->undone;
	t->undone = tmp;
	
	update_state(t);
	return r;
}

/* Throw away all the undo information. */
void
undo_free(Text*t) {
	reset(&t->did);
	reset(&t->undone);
}

/* Throw away all the undo information, leave in NoUndo state. */
void
undo_reset(Text*t)
{
	undo_free(t);
	t->undoing = NoUndo;
	text_rmtool(t, "Put");
	text_rmtool(t, "Undo");
	text_rmtool(t, "Redo");
}

/* Start undoing on this Text. */
void
undo_start(Text*t)
{
	t->undoing = StartUndo;
}

/*
 * Make sure we don't merge whatever's currently on top of the undo
 * stack with the next operation.
 */
void
undo_break(Text*t)
{
	if(t->undoing==MoreUndo)
		t->undoing = StartUndo;
}

/* Remember this point in the Undo history
 * for future reference.
 */
void
undo_mark(Text*t)
{
	t->mark = t->did;
	t->undoing = StartUndo;
}

/* Are we at the same point in the history as we marked earlier?
 */
Bool
undo_atmark(Text*t)
{
	return t->mark == t->did;
}

/*********************************************************
	INTERNAL STUFF
*********************************************************/
static void	tag		(Text *, Bool , Bool , char *);
static Bool	undo_eq(Undo*u, Range r, Rstring s);

/* 
 * We are about to replace the text at 'r' with 's'.  If we can record
 * the undo information for this by modifying t->undo, and/or t->undone,
 * do so, and return true.  Otherwise, return false.
 */
static Bool
append(Text *t, Range r, Rstring s)
{
	Undo 	*u;
	Rune	*buf;
	int		rlen, slen;

	if (undo_eq(t->did, r, s)) {
		/*
		 * What we're about to do is exactly what would happen by
		 * an Undo operation.  Therefore, we just move the front of
		 * the undo queue to the front of the redo queue.
		 */
		save_state(t);
		shift(t, &t->did, &t->undone, false);
		update_state(t);
		
		return true;
	}
	if (!(t->did && t->undoing==MoreUndo && t->did != t->mark))
		return false;

	u = t->did;

	if(u->r.p1 == r.p0 && RLEN(r)==0 && RSLEN(u->s)==0) {
		u->r.p1 += RSLEN(s);
		return true;
	}
	if (RLEN(u->r)==0 && RSLEN(s)==0 && u->r.p0 == r.p1) {
		slen = RSLEN(u->s);
		rlen = RLEN(r); 
		buf = salloc ( (slen + rlen) * sizeof(Rune) );
		text_copy( t, r, buf);
		memcpy(buf + rlen, u->s.r0, slen*sizeof(Rune));
		free(u->s.r0);
		u->s.r0 = buf;
		u->s.r1 = buf + slen + rlen;
		u->r.p0 = u->r.p1 = r.p0;
		return true;
	}
	return false;
}

/* Free all the nodes in the list, set the head of the list to 0.*/

static void
reset(Undo**head) {
	Undo	*u, *next;

	for(u = *head; u; u = next){
		next = u->next;
		free(u->s.r0);
		free(u);
	}
	*head = 0;
}

/*
 * We use save_state() and update_state() to compare our undo state
 * before and after operations.  These two functions use 'did' and
 * 'undone'
 */
static Undo	*did, *undone;
static Bool	state_count = 0;
static void
save_state(Text *t)
{
	if(state_count++)
		return;
	did = t->did;
	undone = t->undone;
}
static void
update_state(Text *t)
{
	if(--state_count)
		return;
	if(t->needsbackup) {
		tag(t, (Bool)did, (Bool)t->did, "Undo");
		tag(t, (Bool)undone, (Bool)t->undone, "Redo");
		tag(t, t->did == t->mark, did==t->mark, "Put");
	}
}

/*
 * If 'change_text', do the replacement at 'from'.
 *
 * Reverse the top of 'from', add it to 'to'.
 *
 * Returns the range of any text inserted, or gibberish if !change_text
 */
static Range
shift(Text *t, Undo **from, Undo **to, Bool change_text)
{
	Range	r;
	Undo	*u;
	Undo	*rev;

	u = *from;
	assert(u);
	*from = u->next;

	rev = reverse(t, u->r, u->s);

	if(change_text){
		undoing=true;
		r = text_replace(t, u->r, u->s);
		undoing = false;
	}

	free(u->s.r0);
	free(u);

	rev->next = *to;
	*to = rev;

	return r;
}

/*If the state has changed to (true/false), (add/remove) 's'
 * in all the tags associated with 't'
 */
static void
tag(Text *t, Bool before, Bool after, char *s)
{
	if(before!=after)
		(after? text_addtool : text_rmtool)(t,s);
}

/*
 * Compare two rune strings.  Return a number >0, <0 or 0 to indicate if
 * s1 is lexically after, before or equal to s2
 */
int
rstrcmp(Rstring s1, Rstring s2)
{
	Rune	*p1, *p2;
	int		diff;

	for (	p1=s1.r0, p2 = s2.r0;  p1<s1.r1 && p2 < s2.r1; p1++, p2++)
		if( (diff = *p1 - *p2) )
			return diff;
	return RSLEN(s1) - RSLEN(s2);
}

static Bool
undo_eq(Undo*u, Range r, Rstring s)
{
	return u && 
		r.p0 == u->r.p0 && r.p1 == u->r.p1 &&
		 !rstrcmp(u->s, s);
}

/* Return undo entry to undo the replacement of 'r' in 't' with 's'
 */
static Undo *
reverse(Text*t, Range r, Rstring s)
{
	Undo	*u;

	u = NEW(Undo);
	u->r.p0 = r.p0;
	u->r.p1 = r.p0 + RSLEN(s);
	u->alloced = RLEN(r);
	u->s.r0 = (Rune*)salloc(u->alloced*sizeof(Rune));
	text_copy(t, r, u->s.r0);
	u->s.r1 = u->s.r0 + RLEN(r);
	return u;
}