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