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