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