Home | History | Annotate | Line # | Download | only in ed
undo.c revision 1.3
      1  1.3  thorpej /*	$NetBSD: undo.c,v 1.3 1997/07/20 06:35:42 thorpej Exp $	*/
      2  1.2      cgd 
      3  1.1      alm /* undo.c: This file contains the undo routines for the ed line editor */
      4  1.1      alm /*-
      5  1.1      alm  * Copyright (c) 1993 Andrew Moore, Talke Studio.
      6  1.1      alm  * All rights reserved.
      7  1.1      alm  *
      8  1.1      alm  * Redistribution and use in source and binary forms, with or without
      9  1.1      alm  * modification, are permitted provided that the following conditions
     10  1.1      alm  * are met:
     11  1.1      alm  * 1. Redistributions of source code must retain the above copyright
     12  1.1      alm  *    notice, this list of conditions and the following disclaimer.
     13  1.1      alm  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1      alm  *    notice, this list of conditions and the following disclaimer in the
     15  1.1      alm  *    documentation and/or other materials provided with the distribution.
     16  1.1      alm  *
     17  1.1      alm  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     18  1.1      alm  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19  1.1      alm  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20  1.1      alm  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     21  1.1      alm  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  1.1      alm  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23  1.1      alm  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24  1.1      alm  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25  1.1      alm  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26  1.1      alm  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  1.1      alm  * SUCH DAMAGE.
     28  1.1      alm  */
     29  1.1      alm 
     30  1.3  thorpej #include <sys/cdefs.h>
     31  1.1      alm #ifndef lint
     32  1.2      cgd #if 0
     33  1.1      alm static char *rcsid = "@(#)undo.c,v 1.1 1994/02/01 00:34:44 alm Exp";
     34  1.2      cgd #else
     35  1.3  thorpej __RCSID("$NetBSD: undo.c,v 1.3 1997/07/20 06:35:42 thorpej Exp $");
     36  1.2      cgd #endif
     37  1.1      alm #endif /* not lint */
     38  1.1      alm 
     39  1.1      alm #include "ed.h"
     40  1.1      alm 
     41  1.1      alm 
     42  1.1      alm #define USIZE 100				/* undo stack size */
     43  1.1      alm undo_t *ustack = NULL;				/* undo stack */
     44  1.1      alm long usize = 0;					/* stack size variable */
     45  1.1      alm long u_p = 0;					/* undo stack pointer */
     46  1.1      alm 
     47  1.1      alm /* push_undo_stack: return pointer to intialized undo node */
     48  1.1      alm undo_t *
     49  1.1      alm push_undo_stack(type, from, to)
     50  1.1      alm 	int type;
     51  1.1      alm 	long from;
     52  1.1      alm 	long to;
     53  1.1      alm {
     54  1.1      alm 	undo_t *t;
     55  1.1      alm 
     56  1.1      alm #if defined(sun) || defined(NO_REALLOC_NULL)
     57  1.1      alm 	if (ustack == NULL &&
     58  1.1      alm 	    (ustack = (undo_t *) malloc((usize = USIZE) * sizeof(undo_t))) == NULL) {
     59  1.1      alm 		fprintf(stderr, "%s\n", strerror(errno));
     60  1.1      alm 		sprintf(errmsg, "out of memory");
     61  1.1      alm 		return NULL;
     62  1.1      alm 	}
     63  1.1      alm #endif
     64  1.1      alm 	t = ustack;
     65  1.1      alm 	if (u_p < usize ||
     66  1.1      alm 	    (t = (undo_t *) realloc(ustack, (usize += USIZE) * sizeof(undo_t))) != NULL) {
     67  1.1      alm 		ustack = t;
     68  1.1      alm 		ustack[u_p].type = type;
     69  1.1      alm 		ustack[u_p].t = get_addressed_line_node(to);
     70  1.1      alm 		ustack[u_p].h = get_addressed_line_node(from);
     71  1.1      alm 		return ustack + u_p++;
     72  1.1      alm 	}
     73  1.1      alm 	/* out of memory - release undo stack */
     74  1.1      alm 	fprintf(stderr, "%s\n", strerror(errno));
     75  1.1      alm 	sprintf(errmsg, "out of memory");
     76  1.1      alm 	clear_undo_stack();
     77  1.1      alm 	free(ustack);
     78  1.1      alm 	ustack = NULL;
     79  1.1      alm 	usize = 0;
     80  1.1      alm 	return NULL;
     81  1.1      alm }
     82  1.1      alm 
     83  1.1      alm 
     84  1.1      alm /* USWAP: swap undo nodes */
     85  1.1      alm #define USWAP(x,y) { \
     86  1.1      alm 	undo_t utmp; \
     87  1.1      alm 	utmp = x, x = y, y = utmp; \
     88  1.1      alm }
     89  1.1      alm 
     90  1.1      alm 
     91  1.1      alm long u_current_addr = -1;	/* if >= 0, undo enabled */
     92  1.1      alm long u_addr_last = -1;		/* if >= 0, undo enabled */
     93  1.1      alm 
     94  1.1      alm /* pop_undo_stack: undo last change to the editor buffer */
     95  1.1      alm int
     96  1.1      alm pop_undo_stack()
     97  1.1      alm {
     98  1.1      alm 	long n;
     99  1.1      alm 	long o_current_addr = current_addr;
    100  1.1      alm 	long o_addr_last = addr_last;
    101  1.1      alm 
    102  1.1      alm 	if (u_current_addr == -1 || u_addr_last == -1) {
    103  1.1      alm 		sprintf(errmsg, "nothing to undo");
    104  1.1      alm 		return ERR;
    105  1.1      alm 	} else if (u_p)
    106  1.1      alm 		modified = 1;
    107  1.1      alm 	get_addressed_line_node(0);	/* this get_addressed_line_node last! */
    108  1.1      alm 	SPL1();
    109  1.1      alm 	for (n = u_p; n-- > 0;) {
    110  1.1      alm 		switch(ustack[n].type) {
    111  1.1      alm 		case UADD:
    112  1.1      alm 			REQUE(ustack[n].h->q_back, ustack[n].t->q_forw);
    113  1.1      alm 			break;
    114  1.1      alm 		case UDEL:
    115  1.1      alm 			REQUE(ustack[n].h->q_back, ustack[n].h);
    116  1.1      alm 			REQUE(ustack[n].t, ustack[n].t->q_forw);
    117  1.1      alm 			break;
    118  1.1      alm 		case UMOV:
    119  1.1      alm 		case VMOV:
    120  1.1      alm 			REQUE(ustack[n - 1].h, ustack[n].h->q_forw);
    121  1.1      alm 			REQUE(ustack[n].t->q_back, ustack[n - 1].t);
    122  1.1      alm 			REQUE(ustack[n].h, ustack[n].t);
    123  1.1      alm 			n--;
    124  1.1      alm 			break;
    125  1.1      alm 		default:
    126  1.1      alm 			/*NOTREACHED*/
    127  1.1      alm 			;
    128  1.1      alm 		}
    129  1.1      alm 		ustack[n].type ^= 1;
    130  1.1      alm 	}
    131  1.1      alm 	/* reverse undo stack order */
    132  1.1      alm 	for (n = u_p; n-- > (u_p + 1)/ 2;)
    133  1.1      alm 		USWAP(ustack[n], ustack[u_p - 1 - n]);
    134  1.1      alm 	if (isglobal)
    135  1.1      alm 		clear_active_list();
    136  1.1      alm 	current_addr = u_current_addr, u_current_addr = o_current_addr;
    137  1.1      alm 	addr_last = u_addr_last, u_addr_last = o_addr_last;
    138  1.1      alm 	SPL0();
    139  1.1      alm 	return 0;
    140  1.1      alm }
    141  1.1      alm 
    142  1.1      alm 
    143  1.1      alm /* clear_undo_stack: clear the undo stack */
    144  1.1      alm void
    145  1.1      alm clear_undo_stack()
    146  1.1      alm {
    147  1.1      alm 	line_t *lp, *ep, *tl;
    148  1.1      alm 
    149  1.1      alm 	while (u_p--)
    150  1.1      alm 		if (ustack[u_p].type == UDEL) {
    151  1.1      alm 			ep = ustack[u_p].t->q_forw;
    152  1.1      alm 			for (lp = ustack[u_p].h; lp != ep; lp = tl) {
    153  1.1      alm 				unmark_line_node(lp);
    154  1.1      alm 				tl = lp->q_forw;
    155  1.1      alm 				free(lp);
    156  1.1      alm 			}
    157  1.1      alm 		}
    158  1.1      alm 	u_p = 0;
    159  1.1      alm 	u_current_addr = current_addr;
    160  1.1      alm 	u_addr_last = addr_last;
    161  1.1      alm }
    162