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