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