Annotation of src/usr.bin/mg/undo.c, Revision 1.57
1.57 ! mmcc 1: /* $OpenBSD: undo.c,v 1.56 2015/03/19 21:22:15 bcallah Exp $ */
1.1 vincent 2: /*
1.45 kjell 3: * This file is in the public domain
1.1 vincent 4: */
1.56 bcallah 5:
6: #include <sys/queue.h>
7: #include <signal.h>
8: #include <stdio.h>
9: #include <stdlib.h>
10: #include <string.h>
1.1 vincent 11:
12: #include "def.h"
13: #include "kbd.h"
14:
15: #define MAX_FREE_RECORDS 32
16:
17: /*
18: * Local variables
19: */
1.50 oga 20: static struct undoq undo_free;
1.7 vincent 21: static int undo_free_num;
1.42 kjell 22: static int boundary_flag = TRUE;
1.43 deraadt 23: static int undo_enable_flag = TRUE;
1.1 vincent 24:
25: /*
26: * Local functions
27: */
1.35 deraadt 28: static int find_dot(struct line *, int);
1.39 kjell 29: static int find_lo(int, struct line **, int *, int *);
1.1 vincent 30: static struct undo_rec *new_undo_record(void);
31: static int drop_oldest_undo_record(void);
32:
1.9 vincent 33: /*
1.24 vincent 34: * find_dot, find_lo()
1.9 vincent 35: *
36: * Find an absolute dot in the buffer from a line/offset pair, and vice-versa.
37: *
1.13 deraadt 38: * Since lines can be deleted while they are referenced by undo record, we
1.9 vincent 39: * need to have an absolute dot to have something reliable.
40: */
1.1 vincent 41: static int
1.35 deraadt 42: find_dot(struct line *lp, int off)
1.1 vincent 43: {
1.25 db 44: int count = 0;
1.35 deraadt 45: struct line *p;
1.1 vincent 46:
1.41 kjell 47: for (p = curbp->b_headp; p != lp; p = lforw(p)) {
1.1 vincent 48: if (count != 0) {
1.41 kjell 49: if (p == curbp->b_headp) {
1.55 lum 50: dobeep();
1.1 vincent 51: ewprintf("Error: Undo stuff called with a"
1.9 vincent 52: "nonexistent line");
1.11 vincent 53: return (FALSE);
1.1 vincent 54: }
55: }
56: count += llength(p) + 1;
57: }
58: count += off;
59:
1.11 vincent 60: return (count);
1.1 vincent 61: }
62:
63: static int
1.39 kjell 64: find_lo(int pos, struct line **olp, int *offset, int *lnum)
1.1 vincent 65: {
1.35 deraadt 66: struct line *p;
1.39 kjell 67: int lineno;
1.1 vincent 68:
1.41 kjell 69: p = curbp->b_headp;
1.39 kjell 70: lineno = 0;
1.9 vincent 71: while (pos > llength(p)) {
1.1 vincent 72: pos -= llength(p) + 1;
1.41 kjell 73: if ((p = lforw(p)) == curbp->b_headp) {
1.1 vincent 74: *olp = NULL;
75: *offset = 0;
1.11 vincent 76: return (FALSE);
1.1 vincent 77: }
1.39 kjell 78: lineno++;
1.1 vincent 79: }
80: *olp = p;
81: *offset = pos;
1.39 kjell 82: *lnum = lineno;
1.1 vincent 83:
1.11 vincent 84: return (TRUE);
1.1 vincent 85: }
86:
87: static struct undo_rec *
88: new_undo_record(void)
89: {
90: struct undo_rec *rec;
91:
1.50 oga 92: rec = TAILQ_FIRST(&undo_free);
1.9 vincent 93: if (rec != NULL) {
1.50 oga 94: /* Remove it from the free-list */
95: TAILQ_REMOVE(&undo_free, rec, next);
1.9 vincent 96: undo_free_num--;
97: } else {
1.25 db 98: if ((rec = malloc(sizeof(*rec))) == NULL)
1.1 vincent 99: panic("Out of memory in undo code (record)");
100: }
101: memset(rec, 0, sizeof(struct undo_rec));
1.9 vincent 102:
1.11 vincent 103: return (rec);
1.1 vincent 104: }
105:
1.7 vincent 106: void
1.1 vincent 107: free_undo_record(struct undo_rec *rec)
108: {
1.9 vincent 109: static int initialised = 0;
110:
111: /*
112: * On the first run, do initialisation of the free list.
113: */
114: if (initialised == 0) {
1.50 oga 115: TAILQ_INIT(&undo_free);
1.9 vincent 116: initialised = 1;
117: }
1.57 ! mmcc 118: free(rec->content);
! 119: rec->content = NULL;
1.1 vincent 120: if (undo_free_num >= MAX_FREE_RECORDS) {
121: free(rec);
122: return;
123: }
124: undo_free_num++;
1.9 vincent 125:
1.50 oga 126: TAILQ_INSERT_HEAD(&undo_free, rec, next);
1.1 vincent 127: }
128:
129: /*
130: * Drop the oldest undo record in our list. Return 1 if we could remove it,
1.25 db 131: * 0 if the undo list was empty.
1.1 vincent 132: */
133: static int
134: drop_oldest_undo_record(void)
135: {
136: struct undo_rec *rec;
137:
1.50 oga 138: rec = TAILQ_LAST(&curbp->b_undo, undoq);
1.1 vincent 139: if (rec != NULL) {
140: undo_free_num--;
1.50 oga 141: TAILQ_REMOVE(&curbp->b_undo, rec, next);
1.1 vincent 142: free_undo_record(rec);
1.11 vincent 143: return (1);
1.1 vincent 144: }
1.11 vincent 145: return (0);
1.1 vincent 146: }
1.9 vincent 147:
1.44 kjell 148: static int
1.22 vincent 149: lastrectype(void)
1.1 vincent 150: {
1.9 vincent 151: struct undo_rec *rec;
152:
1.50 oga 153: if ((rec = TAILQ_FIRST(&curbp->b_undo)) != NULL)
1.22 vincent 154: return (rec->type);
1.11 vincent 155: return (0);
1.1 vincent 156: }
157:
1.30 kjell 158: /*
1.42 kjell 159: * Returns TRUE if undo is enabled, FALSE otherwise.
160: */
161: int
162: undo_enabled(void)
163: {
164: return (undo_enable_flag);
165: }
166:
167: /*
1.47 kjell 168: * undo_enable: toggle undo_enable.
169: * Returns the previous value of the flag.
1.30 kjell 170: */
1.1 vincent 171: int
1.47 kjell 172: undo_enable(int f, int n)
1.1 vincent 173: {
1.42 kjell 174: int pon = undo_enable_flag;
1.22 vincent 175:
1.47 kjell 176: if (f & (FFARG | FFRAND))
177: undo_enable_flag = n > 0;
178: else
179: undo_enable_flag = !undo_enable_flag;
180:
181: if (!(f & FFRAND))
182: ewprintf("Undo %sabled", undo_enable_flag ? "en" : "dis");
183:
1.42 kjell 184: return (pon);
1.5 vincent 185: }
186:
1.30 kjell 187: /*
188: * If undo is enabled, then:
1.47 kjell 189: * Toggle undo boundary recording.
190: * If called with an argument, (n > 0) => enable. Otherwise disable.
191: * In either case, add an undo boundary
1.30 kjell 192: * If undo is disabled, this function has no effect.
193: */
1.47 kjell 194: int
195: undo_boundary_enable(int f, int n)
196: {
197: int bon = boundary_flag;
1.42 kjell 198:
1.47 kjell 199: if (!undo_enable_flag)
200: return (FALSE);
201:
202: undo_add_boundary(FFRAND, 1);
203:
204: if (f & (FFARG | FFRAND))
205: boundary_flag = n > 0;
206: else
207: boundary_flag = !boundary_flag;
208:
209: if (!(f & FFRAND))
210: ewprintf("Undo boundaries %sabled",
211: boundary_flag ? "en" : "dis");
212:
213: return (bon);
1.28 kjell 214: }
215:
1.30 kjell 216: /*
1.42 kjell 217: * Record an undo boundary, unless boundary_flag == FALSE.
1.40 kjell 218: * Does nothing if previous undo entry is already a boundary or 'modified' flag.
1.30 kjell 219: */
1.47 kjell 220: int
221: undo_add_boundary(int f, int n)
1.5 vincent 222: {
223: struct undo_rec *rec;
1.40 kjell 224: int last;
1.5 vincent 225:
1.42 kjell 226: if (boundary_flag == FALSE)
1.47 kjell 227: return (FALSE);
1.12 vincent 228:
1.40 kjell 229: last = lastrectype();
230: if (last == BOUNDARY || last == MODIFIED)
1.47 kjell 231: return (TRUE);
1.31 deraadt 232:
1.5 vincent 233: rec = new_undo_record();
1.9 vincent 234: rec->type = BOUNDARY;
1.5 vincent 235:
1.50 oga 236: TAILQ_INSERT_HEAD(&curbp->b_undo, rec, next);
1.9 vincent 237:
1.47 kjell 238: return (TRUE);
1.1 vincent 239: }
240:
1.40 kjell 241: /*
242: * Record an undo "modified" boundary
243: */
244: void
245: undo_add_modified(void)
246: {
1.52 florian 247: struct undo_rec *rec, *trec;
248:
249: TAILQ_FOREACH_SAFE(rec, &curbp->b_undo, next, trec)
250: if (rec->type == MODIFIED) {
251: TAILQ_REMOVE(&curbp->b_undo, rec, next);
252: free_undo_record(rec);
253: }
1.40 kjell 254:
255: rec = new_undo_record();
256: rec->type = MODIFIED;
257:
1.50 oga 258: TAILQ_INSERT_HEAD(&curbp->b_undo, rec, next);
1.40 kjell 259:
260: return;
261: }
262:
1.1 vincent 263: int
1.35 deraadt 264: undo_add_insert(struct line *lp, int offset, int size)
1.1 vincent 265: {
1.35 deraadt 266: struct region reg;
1.25 db 267: struct undo_rec *rec;
268: int pos;
1.9 vincent 269:
1.47 kjell 270: if (!undo_enable_flag)
1.11 vincent 271: return (TRUE);
1.1 vincent 272: reg.r_linep = lp;
273: reg.r_offset = offset;
274: reg.r_size = size;
1.9 vincent 275:
1.24 vincent 276: pos = find_dot(lp, offset);
1.9 vincent 277:
1.1 vincent 278: /*
279: * We try to reuse the last undo record to `compress' things.
1.13 deraadt 280: */
1.50 oga 281: rec = TAILQ_FIRST(&curbp->b_undo);
1.23 vincent 282: if (rec != NULL && rec->type == INSERT) {
283: if (rec->pos + rec->region.r_size == pos) {
284: rec->region.r_size += reg.r_size;
285: return (TRUE);
1.1 vincent 286: }
287: }
1.9 vincent 288:
1.1 vincent 289: /*
1.25 db 290: * We couldn't reuse the last undo record, so prepare a new one.
1.1 vincent 291: */
292: rec = new_undo_record();
1.9 vincent 293: rec->pos = pos;
1.1 vincent 294: rec->type = INSERT;
1.35 deraadt 295: memmove(&rec->region, ®, sizeof(struct region));
1.1 vincent 296: rec->content = NULL;
1.9 vincent 297:
1.47 kjell 298: undo_add_boundary(FFRAND, 1);
1.9 vincent 299:
1.50 oga 300: TAILQ_INSERT_HEAD(&curbp->b_undo, rec, next);
1.1 vincent 301:
1.11 vincent 302: return (TRUE);
1.1 vincent 303: }
304:
305: /*
1.25 db 306: * This of course must be done _before_ the actual deletion is done.
1.1 vincent 307: */
308: int
1.49 kjell 309: undo_add_delete(struct line *lp, int offset, int size, int isreg)
1.1 vincent 310: {
1.35 deraadt 311: struct region reg;
1.25 db 312: struct undo_rec *rec;
313: int pos;
1.1 vincent 314:
1.47 kjell 315: if (!undo_enable_flag)
1.11 vincent 316: return (TRUE);
1.1 vincent 317:
318: reg.r_linep = lp;
319: reg.r_offset = offset;
320: reg.r_size = size;
321:
1.24 vincent 322: pos = find_dot(lp, offset);
1.3 vincent 323:
1.10 vincent 324: if (offset == llength(lp)) /* if it's a newline... */
1.47 kjell 325: undo_add_boundary(FFRAND, 1);
1.50 oga 326: else if ((rec = TAILQ_FIRST(&curbp->b_undo)) != NULL) {
1.9 vincent 327: /*
328: * Separate this command from the previous one if we're not
329: * just before the previous record...
330: */
1.49 kjell 331: if (!isreg && rec->type == DELETE) {
1.9 vincent 332: if (rec->pos - rec->region.r_size != pos)
1.47 kjell 333: undo_add_boundary(FFRAND, 1);
1.23 vincent 334: }
1.3 vincent 335: }
1.1 vincent 336: rec = new_undo_record();
337: rec->pos = pos;
1.49 kjell 338: if (isreg)
339: rec->type = DELREG;
340: else
341: rec->type = DELETE;
1.35 deraadt 342: memmove(&rec->region, ®, sizeof(struct region));
1.1 vincent 343: do {
344: rec->content = malloc(reg.r_size + 1);
345: } while ((rec->content == NULL) && drop_oldest_undo_record());
1.9 vincent 346:
1.1 vincent 347: if (rec->content == NULL)
348: panic("Out of memory");
1.9 vincent 349:
1.1 vincent 350: region_get_data(®, rec->content, reg.r_size);
351:
1.49 kjell 352: if (isreg || lastrectype() != DELETE)
1.47 kjell 353: undo_add_boundary(FFRAND, 1);
1.22 vincent 354:
1.50 oga 355: TAILQ_INSERT_HEAD(&curbp->b_undo, rec, next);
1.1 vincent 356:
1.11 vincent 357: return (TRUE);
1.1 vincent 358: }
359:
360: /*
1.25 db 361: * This of course must be called before the change takes place.
1.1 vincent 362: */
363: int
1.35 deraadt 364: undo_add_change(struct line *lp, int offset, int size)
1.1 vincent 365: {
1.47 kjell 366: if (!undo_enable_flag)
1.11 vincent 367: return (TRUE);
1.47 kjell 368: undo_add_boundary(FFRAND, 1);
1.42 kjell 369: boundary_flag = FALSE;
1.49 kjell 370: undo_add_delete(lp, offset, size, 0);
1.12 vincent 371: undo_add_insert(lp, offset, size);
1.42 kjell 372: boundary_flag = TRUE;
1.47 kjell 373: undo_add_boundary(FFRAND, 1);
1.9 vincent 374:
1.11 vincent 375: return (TRUE);
1.8 vincent 376: }
377:
378: /*
379: * Show the undo records for the current buffer in a new buffer.
380: */
1.32 kjell 381: /* ARGSUSED */
1.8 vincent 382: int
1.18 vincent 383: undo_dump(int f, int n)
1.8 vincent 384: {
1.25 db 385: struct undo_rec *rec;
1.35 deraadt 386: struct buffer *bp;
387: struct mgwin *wp;
1.25 db 388: char buf[4096], tmp[1024];
389: int num;
1.8 vincent 390:
391: /*
392: * Prepare the buffer for insertion.
393: */
394: if ((bp = bfind("*undo*", TRUE)) == NULL)
1.11 vincent 395: return (FALSE);
1.9 vincent 396: bp->b_flag |= BFREADONLY;
1.8 vincent 397: bclear(bp);
1.54 lum 398: if ((wp = popbuf(bp, WNONE)) == NULL)
399: return (FALSE);
1.8 vincent 400:
1.10 vincent 401: for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
1.8 vincent 402: if (wp->w_bufp == bp) {
1.41 kjell 403: wp->w_dotp = bp->b_headp;
1.8 vincent 404: wp->w_doto = 0;
405: }
1.10 vincent 406: }
1.8 vincent 407:
408: num = 0;
1.50 oga 409: TAILQ_FOREACH(rec, &curbp->b_undo, next) {
1.8 vincent 410: num++;
1.25 db 411: snprintf(buf, sizeof(buf),
1.38 kjell 412: "%d:\t %s at %d ", num,
1.8 vincent 413: (rec->type == DELETE) ? "DELETE":
1.49 kjell 414: (rec->type == DELREG) ? "DELREGION":
1.8 vincent 415: (rec->type == INSERT) ? "INSERT":
1.40 kjell 416: (rec->type == BOUNDARY) ? "----" :
417: (rec->type == MODIFIED) ? "MODIFIED": "UNKNOWN",
1.8 vincent 418: rec->pos);
1.10 vincent 419:
1.12 vincent 420: if (rec->content) {
1.38 kjell 421: (void)strlcat(buf, "\"", sizeof(buf));
1.25 db 422: snprintf(tmp, sizeof(tmp), "%.*s", rec->region.r_size,
1.8 vincent 423: rec->content);
1.38 kjell 424: (void)strlcat(buf, tmp, sizeof(buf));
425: (void)strlcat(buf, "\"", sizeof(buf));
1.8 vincent 426: }
1.25 db 427: snprintf(tmp, sizeof(tmp), " [%d]", rec->region.r_size);
1.33 kjell 428: if (strlcat(buf, tmp, sizeof(buf)) >= sizeof(buf)) {
1.55 lum 429: dobeep();
1.33 kjell 430: ewprintf("Undo record too large. Aborted.");
431: return (FALSE);
432: }
1.17 deraadt 433: addlinef(bp, "%s", buf);
1.53 florian 434: }
435: for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
436: if (wp->w_bufp == bp) {
437: wp->w_dotline = num+1;
438: wp->w_rflag |= WFFULL;
439: }
1.8 vincent 440: }
1.11 vincent 441: return (TRUE);
1.1 vincent 442: }
443:
1.9 vincent 444: /*
1.25 db 445: * After the user did action1, then action2, then action3:
1.9 vincent 446: *
447: * [action3] <--- Undoptr
448: * [action2]
449: * [action1]
450: * ------
451: * [undo]
452: *
453: * After undo:
454: *
455: * [undo of action3]
456: * [action2] <--- Undoptr
457: * [action1]
458: * ------
459: * [undo]
1.13 deraadt 460: *
1.9 vincent 461: * After another undo:
462: *
463: *
464: * [undo of action2]
465: * [undo of action3]
466: * [action1] <--- Undoptr
467: * ------
468: * [undo]
1.13 deraadt 469: *
1.25 db 470: * Note that the "undo of actionX" have no special meaning. Only when
471: * we undo a deletion, the insertion will be recorded just as if it
1.10 vincent 472: * was typed on the keyboard. Resulting in the inverse operation being
1.9 vincent 473: * saved in the list.
474: *
475: * If undoptr reaches the bottom of the list, or if we moved between
476: * two undo actions, we make it point back at the topmost record. This is
477: * how we handle redoing.
478: */
1.32 kjell 479: /* ARGSUSED */
1.1 vincent 480: int
1.4 vincent 481: undo(int f, int n)
1.1 vincent 482: {
1.25 db 483: struct undo_rec *ptr, *nptr;
1.31 deraadt 484: int done, rval;
1.36 kjell 485: struct line *lp;
1.51 florian 486: int offset, save;
1.28 kjell 487: static int nulled = FALSE;
1.39 kjell 488: int lineno;
1.23 vincent 489:
1.46 kjell 490: if (n < 0)
491: return (FALSE);
492:
1.29 kjell 493: ptr = curbp->b_undoptr;
1.9 vincent 494:
1.46 kjell 495: /* first invocation, make ptr point back to the top of the list */
496: if ((ptr == NULL && nulled == TRUE) || rptcount == 0) {
1.50 oga 497: ptr = TAILQ_FIRST(&curbp->b_undo);
1.28 kjell 498: nulled = TRUE;
499: }
1.1 vincent 500:
1.9 vincent 501: rval = TRUE;
1.10 vincent 502: while (n--) {
1.9 vincent 503: /* if we have a spurious boundary, free it and move on.... */
504: while (ptr && ptr->type == BOUNDARY) {
1.50 oga 505: nptr = TAILQ_NEXT(ptr, next);
506: TAILQ_REMOVE(&curbp->b_undo, ptr, next);
1.9 vincent 507: free_undo_record(ptr);
508: ptr = nptr;
1.4 vincent 509: }
510: /*
1.9 vincent 511: * Ptr is NULL, but on the next run, it will point to the
512: * top again, redoing all stuff done in the buffer since
513: * its creation.
1.4 vincent 514: */
1.9 vincent 515: if (ptr == NULL) {
1.55 lum 516: dobeep();
1.11 vincent 517: ewprintf("No further undo information");
1.9 vincent 518: rval = FALSE;
1.28 kjell 519: nulled = TRUE;
1.4 vincent 520: break;
521: }
1.28 kjell 522: nulled = FALSE;
1.6 vincent 523:
1.13 deraadt 524: /*
1.9 vincent 525: * Loop while we don't get a boundary specifying we've
526: * finished the current action...
527: */
1.22 vincent 528:
1.47 kjell 529: undo_add_boundary(FFRAND, 1);
1.22 vincent 530:
1.42 kjell 531: save = boundary_flag;
532: boundary_flag = FALSE;
1.22 vincent 533:
1.9 vincent 534: done = 0;
535: do {
536: /*
537: * Move to where this has to apply
538: *
1.40 kjell 539: * Boundaries (and the modified flag) are put as
540: * position 0 (to save lookup time in find_dot)
541: * so we must not move there...
1.9 vincent 542: */
1.40 kjell 543: if (ptr->type != BOUNDARY && ptr->type != MODIFIED) {
1.24 vincent 544: if (find_lo(ptr->pos, &lp,
1.39 kjell 545: &offset, &lineno) == FALSE) {
1.55 lum 546: dobeep();
1.9 vincent 547: ewprintf("Internal error in Undo!");
548: rval = FALSE;
549: break;
550: }
551: curwp->w_dotp = lp;
552: curwp->w_doto = offset;
1.39 kjell 553: curwp->w_markline = curwp->w_dotline;
554: curwp->w_dotline = lineno;
1.9 vincent 555: }
556:
557: /*
558: * Do operation^-1
559: */
560: switch (ptr->type) {
561: case INSERT:
1.34 kjell 562: ldelete(ptr->region.r_size, KNONE);
1.9 vincent 563: break;
564: case DELETE:
1.46 kjell 565: lp = curwp->w_dotp;
566: offset = curwp->w_doto;
1.13 deraadt 567: region_put_data(ptr->content,
1.9 vincent 568: ptr->region.r_size);
1.46 kjell 569: curwp->w_dotp = lp;
570: curwp->w_doto = offset;
1.49 kjell 571: break;
572: case DELREG:
573: region_put_data(ptr->content,
574: ptr->region.r_size);
1.9 vincent 575: break;
576: case BOUNDARY:
577: done = 1;
1.40 kjell 578: break;
579: case MODIFIED:
580: curbp->b_flag &= ~BFCHG;
1.9 vincent 581: break;
582: default:
583: break;
584: }
585:
586: /* And move to next record */
1.50 oga 587: ptr = TAILQ_NEXT(ptr, next);
1.9 vincent 588: } while (ptr != NULL && !done);
1.22 vincent 589:
1.42 kjell 590: boundary_flag = save;
1.47 kjell 591: undo_add_boundary(FFRAND, 1);
1.9 vincent 592:
593: ewprintf("Undo!");
1.1 vincent 594: }
1.51 florian 595:
1.29 kjell 596: curbp->b_undoptr = ptr;
1.9 vincent 597:
1.13 deraadt 598: return (rval);
1.1 vincent 599: }