1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
/* undo.c: This file contains the undo routines for the ed line editor */
/* ed line editor.
Copyright (C) 1993, 1994 Andrew Moore, Talke Studio
All Rights Reserved
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef lint
static char *rcsid = "@(#)$Id: undo.c,v 1.7 1994/11/13 04:25:44 alm Exp $";
#endif /* not lint */
#include "ed.h"
#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 */
undo_t *
push_undo_stack (type, from, to)
int type;
long from;
long to;
{
undo_t *t;
if ((t = ustack) == NULL &&
(t = ustack = (undo_t *) malloc ((usize = USIZE) * sizeof (undo_t))) == NULL)
{
fprintf (stderr, "%s\n", strerror (errno));
sprintf (errmsg, "Out of memory");
return NULL;
}
else if (u_p >= usize &&
(t = (undo_t *) realloc (ustack, (usize += USIZE) * sizeof (undo_t))) == NULL)
{
/* out of memory - release undo stack */
fprintf (stderr, "%s\n", strerror (errno));
sprintf (errmsg, "Out of memory");
clear_undo_stack ();
free (ustack);
ustack = NULL;
usize = 0;
return NULL;
}
ustack = t;
ustack[u_p].type = type;
ustack[u_p].t = get_addressed_line_node (to);
ustack[u_p].h = get_addressed_line_node (from);
return ustack + u_p++;
}
/* USWAP: swap undo nodes */
#define USWAP(x, y) \
do \
{ \
undo_t utmp; \
utmp = (x), (x) = (y), (y) = utmp; \
} \
while (0)
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 */
int
pop_undo_stack ()
{
long n;
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");
return ERR;
}
else if (u_p)
modified = 1;
get_addressed_line_node (0); /* this get_addressed_line_node last! */
SPL1 ();
for (n = u_p; n-- > 0;)
{
switch (ustack[n].type)
{
case UADD:
REQUE (ustack[n].h->q_back, ustack[n].t->q_forw);
break;
case UDEL:
REQUE (ustack[n].h->q_back, ustack[n].h);
REQUE (ustack[n].t, ustack[n].t->q_forw);
break;
case UMOV:
case VMOV:
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);
n--;
break;
default:
/*NOTREACHED */
;
}
ustack[n].type ^= 1;
}
/* reverse undo stack order */
for (n = u_p; n-- > (u_p + 1) / 2;)
USWAP (ustack[n], ustack[u_p - 1 - n]);
if (isglobal)
clear_active_list ();
current_addr = u_current_addr, u_current_addr = o_current_addr;
addr_last = u_addr_last, u_addr_last = o_addr_last;
SPL0 ();
return 0;
}
/* clear_undo_stack: clear the undo stack */
void
clear_undo_stack ()
{
line_t *lp, *ep, *tl;
while (u_p--)
if (ustack[u_p].type == UDEL)
{
ep = ustack[u_p].t->q_forw;
for (lp = ustack[u_p].h; lp != ep; lp = tl)
{
unmark_line_node (lp);
tl = lp->q_forw;
free (lp);
}
}
u_p = 0;
u_current_addr = current_addr;
u_addr_last = addr_last;
}
|