/* undo.c: This file contains the undo routines for the ed line editor */
* Copyright (c) 1993 Andrew Moore, Talke Studio.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
static char *rcsid
= "@(#)$Id: undo.c,v 1.2 1993/12/14 16:19:56 alm Exp $";
#define USIZE 100 /* undo stack size */
undo_t
*ustack
= NULL
; /* undo stack */
long usize
= 0; /* stack size variable */
long u_p
= 0; /* undo stack pointer */
/* push_undo_stack: return pointer to intialized undo node */
push_undo_stack(type
, from
, to
)
#if defined(sun) || defined(NO_REALLOC_NULL)
(ustack
= (undo_t
*) malloc((usize
= USIZE
) * sizeof(undo_t
))) == NULL
) {
fprintf(stderr
, "%s\n", strerror(errno
));
sprintf(errmsg
, "out of memory");
(t
= (undo_t
*) realloc(ustack
, (usize
+= USIZE
) * sizeof(undo_t
))) != NULL
) {
ustack
[u_p
].t
= get_addressed_line_node(to
);
ustack
[u_p
].h
= get_addressed_line_node(from
);
/* out of memory - release undo stack */
fprintf(stderr
, "%s\n", strerror(errno
));
sprintf(errmsg
, "out of memory");
/* USWAP: swap undo nodes */
utmp = x, x = y, y = utmp; \
long u_current_addr
= -1; /* if >= 0, undo enabled */
long u_addr_last
= -1; /* if >= 0, undo enabled */
/* pop_undo_stack: undo last change to the editor buffer */
long o_current_addr
= current_addr
;
long o_addr_last
= addr_last
;
if (u_current_addr
== -1 || u_addr_last
== -1) {
sprintf(errmsg
, "nothing to undo");
get_addressed_line_node(0); /* this get_addressed_line_node last! */
for (n
= u_p
; n
-- > 0;) {
REQUE(ustack
[n
].h
->q_back
, ustack
[n
].t
->q_forw
);
REQUE(ustack
[n
].h
->q_back
, ustack
[n
].h
);
REQUE(ustack
[n
].t
, ustack
[n
].t
->q_forw
);
REQUE(ustack
[n
- 1].h
, ustack
[n
].h
->q_forw
);
REQUE(ustack
[n
].t
->q_back
, ustack
[n
- 1].t
);
REQUE(ustack
[n
].h
, ustack
[n
].t
);
/* reverse undo stack order */
for (n
= u_p
; n
-- > (u_p
+ 1)/ 2;)
USWAP(ustack
[n
], ustack
[u_p
- 1 - n
]);
current_addr
= u_current_addr
, u_current_addr
= o_current_addr
;
addr_last
= u_addr_last
, u_addr_last
= o_addr_last
;
/* clear_undo_stack: clear the undo stack */
if (ustack
[u_p
].type
== UDEL
) {
ep
= ustack
[u_p
].t
->q_forw
;
for (lp
= ustack
[u_p
].h
; lp
!= ep
; lp
= tl
) {
u_current_addr
= current_addr
;