summaryrefslogtreecommitdiff
path: root/usr.bin/mg/undo.c
blob: 6e87d9cc63227e46d723a3309f5e207167d15c0b (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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
/* $OpenBSD: undo.c,v 1.58 2016/09/05 08:10:58 lum Exp $ */
/*
 * This file is in the public domain
 */

#include <sys/queue.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "def.h"
#include "kbd.h"

#define MAX_FREE_RECORDS	32

/*
 * Local variables
 */
static struct undoq		 undo_free;
static int			 undo_free_num;
static int			 boundary_flag = TRUE;
static int			 undo_enable_flag = TRUE;

/*
 * Local functions
 */
static int find_dot(struct line *, int);
static int find_lo(int, struct line **, int *, int *);
static struct undo_rec *new_undo_record(void);
static int drop_oldest_undo_record(void);

/*
 * find_dot, find_lo()
 *
 * Find an absolute dot in the buffer from a line/offset pair, and vice-versa.
 *
 * Since lines can be deleted while they are referenced by undo record, we
 * need to have an absolute dot to have something reliable.
 */
static int
find_dot(struct line *lp, int off)
{
	int	 count = 0;
	struct line	*p;

	for (p = curbp->b_headp; p != lp; p = lforw(p)) {
		if (count != 0) {
			if (p == curbp->b_headp) {
				dobeep();
				ewprintf("Error: Undo stuff called with a"
				    "nonexistent line");
				return (FALSE);
			}
		}
		count += llength(p) + 1;
	}
	count += off;

	return (count);
}

static int
find_lo(int pos, struct line **olp, int *offset, int *lnum)
{
	struct line *p;
	int lineno;

	p = curbp->b_headp;
	lineno = 0;
	while (pos > llength(p)) {
		pos -= llength(p) + 1;
		if ((p = lforw(p)) == curbp->b_headp) {
			*olp = NULL;
			*offset = 0;
			return (FALSE);
		}
		lineno++;
	}
	*olp = p;
	*offset = pos;
	*lnum = lineno;

	return (TRUE);
}

static struct undo_rec *
new_undo_record(void)
{
	struct undo_rec *rec;

	rec = TAILQ_FIRST(&undo_free);
	if (rec != NULL) {
		/* Remove it from the free-list */
		TAILQ_REMOVE(&undo_free, rec, next);
		undo_free_num--;
	} else {
		if ((rec = malloc(sizeof(*rec))) == NULL)
			panic("Out of memory in undo code (record)");
	}
	memset(rec, 0, sizeof(struct undo_rec));

	return (rec);
}

void
free_undo_record(struct undo_rec *rec)
{
	static int initialised = 0;

	/*
	 * On the first run, do initialisation of the free list.
	 */
	if (initialised == 0) {
		TAILQ_INIT(&undo_free);
		initialised = 1;
	}
	free(rec->content);
	rec->content = NULL;
	if (undo_free_num >= MAX_FREE_RECORDS) {
		free(rec);
		return;
	}
	undo_free_num++;

	TAILQ_INSERT_HEAD(&undo_free, rec, next);
}

/*
 * Drop the oldest undo record in our list. Return 1 if we could remove it,
 * 0 if the undo list was empty.
 */
static int
drop_oldest_undo_record(void)
{
	struct undo_rec *rec;

	rec = TAILQ_LAST(&curbp->b_undo, undoq);
	if (rec != NULL) {
		undo_free_num--;
		TAILQ_REMOVE(&curbp->b_undo, rec, next);
		free_undo_record(rec);
		return (1);
	}
	return (0);
}

static int
lastrectype(void)
{
	struct undo_rec *rec;

	if ((rec = TAILQ_FIRST(&curbp->b_undo)) != NULL)
		return (rec->type);
	return (0);
}

/*
 * Returns TRUE if undo is enabled, FALSE otherwise.
 */
int
undo_enabled(void)
{
	return (undo_enable_flag);
}

/*
 * undo_enable: toggle undo_enable.
 * Returns the previous value of the flag.
 */
int
undo_enable(int f, int n)
{
	int pon = undo_enable_flag;

	if (f & (FFARG | FFRAND))
		undo_enable_flag = n > 0;
	else
		undo_enable_flag = !undo_enable_flag;

	if (!(f & FFRAND))
		ewprintf("Undo %sabled", undo_enable_flag ? "en" : "dis");

	return (pon);
}

/*
 * If undo is enabled, then:
 *  Toggle undo boundary recording.
 *  If called with an argument, (n > 0) => enable. Otherwise disable.
 * In either case, add an undo boundary
 * If undo is disabled, this function has no effect.
 */
int
undo_boundary_enable(int f, int n)
{
	int bon = boundary_flag;

	if (!undo_enable_flag)
		return (FALSE);

	undo_add_boundary(FFRAND, 1);

	if (f & (FFARG | FFRAND))
		boundary_flag = n > 0;
	else
		boundary_flag = !boundary_flag;

	if (!(f & FFRAND))
		ewprintf("Undo boundaries %sabled",
		    boundary_flag ? "en" : "dis");

	return (bon);
}

/*
 * Record an undo boundary, unless boundary_flag == FALSE.
 * Does nothing if previous undo entry is already a boundary or 'modified' flag.
 */
int
undo_add_boundary(int f, int n)
{
	struct undo_rec *rec;
	int last;

	if (boundary_flag == FALSE)
		return (FALSE);

	last = lastrectype();
	if (last == BOUNDARY || last == MODIFIED)
		return (TRUE);

	rec = new_undo_record();
	rec->type = BOUNDARY;

	TAILQ_INSERT_HEAD(&curbp->b_undo, rec, next);

	return (TRUE);
}

/*
 * Record an undo "modified" boundary
 */
void
undo_add_modified(void)
{
	struct undo_rec *rec, *trec;

	TAILQ_FOREACH_SAFE(rec, &curbp->b_undo, next, trec)
		if (rec->type == MODIFIED) {
			TAILQ_REMOVE(&curbp->b_undo, rec, next);
			free_undo_record(rec);
		}

	rec = new_undo_record();
	rec->type = MODIFIED;

	TAILQ_INSERT_HEAD(&curbp->b_undo, rec, next);

	return;
}

int
undo_add_insert(struct line *lp, int offset, int size)
{
	struct region	reg;
	struct	undo_rec *rec;
	int	pos;

	if (!undo_enable_flag)
		return (TRUE);

	memset(&reg, 0, sizeof(reg));
	reg.r_linep = lp;
	reg.r_offset = offset;
	reg.r_size = size;

	pos = find_dot(lp, offset);

	/*
	 * We try to reuse the last undo record to `compress' things.
	 */
	rec = TAILQ_FIRST(&curbp->b_undo);
	if (rec != NULL && rec->type == INSERT) {
		if (rec->pos + rec->region.r_size == pos) {
			rec->region.r_size += reg.r_size;
			return (TRUE);
		}
	}

	/*
	 * We couldn't reuse the last undo record, so prepare a new one.
	 */
	rec = new_undo_record();
	rec->pos = pos;
	rec->type = INSERT;
	memmove(&rec->region, &reg, sizeof(struct region));
	rec->content = NULL;

	undo_add_boundary(FFRAND, 1);

	TAILQ_INSERT_HEAD(&curbp->b_undo, rec, next);

	return (TRUE);
}

/*
 * This of course must be done _before_ the actual deletion is done.
 */
int
undo_add_delete(struct line *lp, int offset, int size, int isreg)
{
	struct region	reg;
	struct	undo_rec *rec;
	int	pos;

	if (!undo_enable_flag)
		return (TRUE);

	memset(&reg, 0, sizeof(reg));
	reg.r_linep = lp;
	reg.r_offset = offset;
	reg.r_size = size;

	pos = find_dot(lp, offset);

	if (offset == llength(lp))	/* if it's a newline... */
		undo_add_boundary(FFRAND, 1);
	else if ((rec = TAILQ_FIRST(&curbp->b_undo)) != NULL) {
		/*
		 * Separate this command from the previous one if we're not
		 * just before the previous record...
		 */
		if (!isreg && rec->type == DELETE) {
			if (rec->pos - rec->region.r_size != pos)
				undo_add_boundary(FFRAND, 1);
		}
	}
	rec = new_undo_record();
	rec->pos = pos;
	if (isreg)
		rec->type = DELREG;
	else
		rec->type = DELETE;
	memmove(&rec->region, &reg, sizeof(struct region));
	do {
		rec->content = malloc(reg.r_size + 1);
	} while ((rec->content == NULL) && drop_oldest_undo_record());

	if (rec->content == NULL)
		panic("Out of memory");

	region_get_data(&reg, rec->content, reg.r_size);

	if (isreg || lastrectype() != DELETE)
		undo_add_boundary(FFRAND, 1);

	TAILQ_INSERT_HEAD(&curbp->b_undo, rec, next);

	return (TRUE);
}

/*
 * This of course must be called before the change takes place.
 */
int
undo_add_change(struct line *lp, int offset, int size)
{
	if (!undo_enable_flag)
		return (TRUE);
	undo_add_boundary(FFRAND, 1);
	boundary_flag = FALSE;
	undo_add_delete(lp, offset, size, 0);
	undo_add_insert(lp, offset, size);
	boundary_flag = TRUE;
	undo_add_boundary(FFRAND, 1);

	return (TRUE);
}

/*
 * Show the undo records for the current buffer in a new buffer.
 */
/* ARGSUSED */
int
undo_dump(int f, int n)
{
	struct	 undo_rec *rec;
	struct buffer	*bp;
	struct mgwin	*wp;
	char	 buf[4096], tmp[1024];
	int	 num;

	/*
	 * Prepare the buffer for insertion.
	 */
	if ((bp = bfind("*undo*", TRUE)) == NULL)
		return (FALSE);
	bp->b_flag |= BFREADONLY;
	bclear(bp);
	if ((wp = popbuf(bp, WNONE)) == NULL)
		return (FALSE);

	for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
		if (wp->w_bufp == bp) {
			wp->w_dotp = bp->b_headp;
			wp->w_doto = 0;
		}
	}

	num = 0;
	TAILQ_FOREACH(rec, &curbp->b_undo, next) {
		num++;
		snprintf(buf, sizeof(buf),
		    "%d:\t %s at %d ", num,
		    (rec->type == DELETE) ? "DELETE":
		    (rec->type == DELREG) ? "DELREGION":
		    (rec->type == INSERT) ? "INSERT":
		    (rec->type == BOUNDARY) ? "----" :
		    (rec->type == MODIFIED) ? "MODIFIED": "UNKNOWN",
		    rec->pos);

		if (rec->content) {
			(void)strlcat(buf, "\"", sizeof(buf));
			snprintf(tmp, sizeof(tmp), "%.*s", rec->region.r_size,
			    rec->content);
			(void)strlcat(buf, tmp, sizeof(buf));
			(void)strlcat(buf, "\"", sizeof(buf));
		}
		snprintf(tmp, sizeof(tmp), " [%d]", rec->region.r_size);
		if (strlcat(buf, tmp, sizeof(buf)) >= sizeof(buf)) {
			dobeep();
			ewprintf("Undo record too large. Aborted.");
			return (FALSE);
		}
		addlinef(bp, "%s", buf);
	}
	for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
		if (wp->w_bufp == bp) {
			wp->w_dotline = num+1;
			wp->w_rflag |= WFFULL;
		}
	}
	return (TRUE);
}

/*
 * After the user did action1, then action2, then action3:
 *
 *	[action3] <--- Undoptr
 *	[action2]
 *	[action1]
 *	 ------
 *	 [undo]
 *
 * After undo:
 *
 *	[undo of action3]
 *	[action2] <--- Undoptr
 *	[action1]
 *	 ------
 *	 [undo]
 *
 * After another undo:
 *
 *
 *	[undo of action2]
 *	[undo of action3]
 *	[action1]  <--- Undoptr
 *	 ------
 *	 [undo]
 *
 * Note that the "undo of actionX" have no special meaning. Only when
 * we undo a deletion, the insertion will be recorded just as if it
 * was typed on the keyboard. Resulting in the inverse operation being
 * saved in the list.
 *
 * If undoptr reaches the bottom of the list, or if we moved between
 * two undo actions, we make it point back at the topmost record. This is
 * how we handle redoing.
 */
/* ARGSUSED */
int
undo(int f, int n)
{
	struct undo_rec	*ptr, *nptr;
	int		 done, rval;
	struct line	*lp;
	int		 offset, save;
	static int	 nulled = FALSE;
	int		 lineno;

	if (n < 0)
		return (FALSE);

	ptr = curbp->b_undoptr;

	/* first invocation, make ptr point back to the top of the list */
	if ((ptr == NULL && nulled == TRUE) ||  rptcount == 0) {
		ptr = TAILQ_FIRST(&curbp->b_undo);
		nulled = TRUE;
	}

	rval = TRUE;
	while (n--) {
		/* if we have a spurious boundary, free it and move on.... */
		while (ptr && ptr->type == BOUNDARY) {
			nptr = TAILQ_NEXT(ptr, next);
			TAILQ_REMOVE(&curbp->b_undo, ptr, next);
			free_undo_record(ptr);
			ptr = nptr;
		}
		/*
		 * Ptr is NULL, but on the next run, it will point to the
		 * top again, redoing all stuff done in the buffer since
		 * its creation.
		 */
		if (ptr == NULL) {
			dobeep();
			ewprintf("No further undo information");
			rval = FALSE;
			nulled = TRUE;
			break;
		}
		nulled = FALSE;

		/*
		 * Loop while we don't get a boundary specifying we've
		 * finished the current action...
		 */

		undo_add_boundary(FFRAND, 1);

		save = boundary_flag;
		boundary_flag = FALSE;

		done = 0;
		do {
			/*
			 * Move to where this has to apply
			 *
			 * Boundaries (and the modified flag)  are put as
			 * position 0 (to save lookup time in find_dot)
			 * so we must not move there...
			 */
			if (ptr->type != BOUNDARY && ptr->type != MODIFIED) {
				if (find_lo(ptr->pos, &lp,
				    &offset, &lineno) == FALSE) {
					dobeep();
					ewprintf("Internal error in Undo!");
					rval = FALSE;
					break;
				}
				curwp->w_dotp = lp;
				curwp->w_doto = offset;
				curwp->w_markline = curwp->w_dotline;
				curwp->w_dotline = lineno;
			}

			/*
			 * Do operation^-1
			 */
			switch (ptr->type) {
			case INSERT:
				ldelete(ptr->region.r_size, KNONE);
				break;
			case DELETE:
				lp = curwp->w_dotp;
				offset = curwp->w_doto;
				region_put_data(ptr->content,
				    ptr->region.r_size);
				curwp->w_dotp = lp;
				curwp->w_doto = offset;
				break;
			case DELREG:
				region_put_data(ptr->content,
				    ptr->region.r_size);
				break;
			case BOUNDARY:
				done = 1;
				break;
			case MODIFIED:
				curbp->b_flag &= ~BFCHG;
				break;
			default:
				break;
			}

			/* And move to next record */
			ptr = TAILQ_NEXT(ptr, next);
		} while (ptr != NULL && !done);

		boundary_flag = save;
		undo_add_boundary(FFRAND, 1);

		ewprintf("Undo!");
	}

	curbp->b_undoptr = ptr;

	return (rval);
}