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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
|
/* $Id: undo.c,v 1.8 2000/11/05 01:59:20 amura Exp $ */
/*
* Undo support routine.
* The functions in this file
* are a general set of undo support utilities.
*/
/*
* $Log: undo.c,v $
* Revision 1.8 2000/11/05 01:59:20 amura
* ploblem with big undo is fixed
*
* Revision 1.7 2000/11/04 13:44:58 amura
* undo memory exception is more safety
*
* Revision 1.6 2000/09/01 19:43:10 amura
* change undo buffer memory allocation strategy for speed
*
* Revision 1.5 2000/07/22 20:49:38 amura
* more secure run insert
*
* Revision 1.4 2000/07/18 18:59:57 amura
* fixed never end loop on do_undo with arguments
*
* Revision 1.3 2000/07/16 15:47:06 amura
* undo bug on autofill fixed
*
* Revision 1.2 2000/06/27 01:49:45 amura
* import to CVS
*
* Revision 1.1 2000/06/01 05:36:42 amura
* Initial revision
*
*/
#include "config.h"
#include "def.h"
#ifdef UNDO
#include "undo.h"
#ifndef UBLOCK
#define UBLOCK 32
#endif
UNDO_DATA **undoptr;
UNDO_DATA **undostart;
UNDO_DATA **undobefore;
static int undofirst;
VOID
ublock_open(bp)
register BUFFER *bp;
{
if (undoptr != NULL)
panic("bug: ublock opening error");
undoptr = undostart = &bp->b_ustack[bp->b_utop];
undobefore = bp->b_ulast;
if ((curbp->b_flag&BFCHG) == 0) /* First change */
undofirst = TRUE;
else
undofirst = FALSE;
}
VOID
ublock_close(bp)
register BUFFER *bp;
{
if (undoptr == NULL)
return;
if (undoptr != undostart)
{
bp->b_ulast = undobefore;
ublock_clear(undoptr);
if (undofirst && *undostart!=NULL)
(*undostart)->u_type |= UDFIRST;
bp->b_utop++;
if (bp->b_utop > UNDOSIZE)
bp->b_utop = 0;
if (bp->b_ubottom == bp->b_utop) {
bp->b_ubottom++;
if (bp->b_ubottom > UNDOSIZE)
bp->b_ubottom = 0;
}
}
undoptr = NULL;
}
VOID
undo_clean(bp)
BUFFER *bp;
{
int i;
UNDO_DATA **undo = bp->b_ustack;
for (i=0; i<UNDOSIZE+1; i++)
ublock_clear(undo++);
undo_reset(bp);
}
VOID
ublock_clear(upp)
register UNDO_DATA **upp;
{
register UNDO_DATA *up,*upold;
if (upp == NULL)
return;
up = *upp;
while (up != NULL)
{
if (up->u_size)
free(up->u_buffer);
upold = up;
up = up->u_next;
free(upold);
}
*upp = NULL;
}
int
undo_balloc(undo, size)
register UNDO_DATA *undo;
register RSIZE size;
{
char *newbuffer;
/* For memory save, too big undo buffer is turncate */
if (undo->u_size<size && (size*4)<undo->u_size)
undo_bfree(undo);
if (undo->u_size < size)
{
size = (size + UBLOCK - 1) / UBLOCK * UBLOCK;
#ifdef MALLOCROUND
MALLOCROUND(size);
#endif
if (size < undo->u_size)
{
ewprintf("Undo buffer too BIG!");
ttwait();
undo_clean(curbp);
undoptr = NULL;
return FALSE;
}
if (undo->u_size) {
free(undo->u_buffer);
undo->u_size = 0;
}
newbuffer = malloc(size);
if (newbuffer == NULL)
{
ewprintf("Can't get %ld Bytes / Undo buffer clear", size);
ttwait();
undo_clean(curbp);
undoptr = NULL;
return FALSE;
}
undo->u_buffer = newbuffer;
undo->u_size = size;
}
return TRUE;
}
int
undo_bgrow(undo, size)
register UNDO_DATA *undo;
RSIZE size;
{
char *newbuffer;
RSIZE newsize = (undo->u_used + size);
if (newsize < size)
{
ewprintf("Undo buffer too BIG!");
ttwait();
undo_clean(curbp);
undoptr = NULL;
return FALSE;
}
if (newsize > undo->u_size)
{
size = (newsize + UBLOCK - 1) / UBLOCK * UBLOCK;
#ifdef MALLOCROUND
MALLOCROUND(size);
#endif
if (size < newsize)
size = newsize;
newbuffer = malloc(size);
if (newbuffer == NULL)
{
ewprintf("Can't get %ld Bytes / Undo buffer clear");
ttwait();
undo_clean(curbp);
undoptr = NULL;
return FALSE;
}
if (undo->u_size) {
bcopy(undo->u_buffer, newbuffer, undo->u_used);
free(undo->u_buffer);
}
undo->u_buffer = newbuffer;
undo->u_size = size;
}
return TRUE;
}
int
do_undo(f, n)
int f, n;
{
register char *p;
register int i;
register LINE* lp;
UNDO_DATA *undo,*undoend;
int firstcheck = FALSE;
extern int twiddle();
ewprintf("Undo!");
if (n < 0)
return FALSE;
undoptr = NULL;
while (n--)
{
if (curbp->b_utop == curbp->b_ubottom) {
ewprintf("No more undo data");
ttbeep();
return TRUE;
}
curbp->b_utop--;
if (curbp->b_utop < 0)
curbp->b_utop = UNDOSIZE;
undoend = NULL;
while (1)
{
undo = curbp->b_ustack[curbp->b_utop];
if (undo == undoend)
break;
while (undo->u_next != undoend)
undo = undo->u_next;
undoend = undo;
lp = lforw(curbp->b_linep);
for (i=undo->u_dotlno; i>0; i--)
{
if (lp == curbp->b_linep)
break;
lp = lforw(lp);
}
if (i != 0) {
ewprintf("undo data error : line missing %d:%d",
undo->u_dotlno, undo->u_doto);
undo_reset(curbp);
return FALSE;
}
if (llength(lp) < undo->u_doto) {
ewprintf("undo data error : offset missing %d:%d",
undo->u_dotlno, undo->u_doto);
undo_reset(curbp);
return FALSE;
}
curwp->w_dotp = lp;
curwp->w_doto = undo->u_doto;
if (undo->u_type & UDFIRST)
firstcheck = TRUE;
switch (undo->u_type & UDMASK)
{
case UDDEL:
case UDBS:
if (undo->u_size)
{
p = undo->u_buffer;
for (i=0; i<undo->u_used; i++,p++)
{
if (*p == '\n')
lnewline();
else
linsert(1, *p);
}
}
else
{
p = undo->u_code;
if (*p == '\n')
lnewline();
else
{
linsert(1, *p++);
if (*p)
linsert(1, *p);
}
}
if ((undo->u_type&UDMASK) == UDDEL)
{
lp = lforw(curbp->b_linep);
for (i=undo->u_dotlno; i>0; i--)
{
if (lp == curbp->b_linep)
break;
lp = lforw(lp);
}
curwp->w_dotp = lp;
curwp->w_doto = undo->u_doto;
}
break;
#if UDINSNL != UDINS
case UDINSNL:
#endif
case UDINS:
ldelete(undo->u_used, KNONE);
break;
case UDTWIDDLE:
twiddle(FFRAND, 1);
curwp->w_dotp = lp;
curwp->w_doto = undo->u_doto;
break;
case UDOVER:
i = 0;
while (curwp->w_doto < llength(lp) && i < undo->u_used)
lputc(lp, curwp->w_doto++, undo->u_buffer[i++]);
if (i != undo->u_used)
{
ewprintf("do_undo: overwrite data error");
return FALSE;
}
if (undo->u_code[0]) {
if (curwp->w_doto >= llength(lp))
{
ewprintf("do_undo: overwrite data error");
undo_reset(curbp);
return FALSE;
}
lputc(lp, curwp->w_doto, undo->u_code[0]);
}
curwp->w_doto = undo->u_doto;
lchange(WFEDIT);
break;
case UDREPL:
curwp->w_doto += undo->u_used;
if (llength(lp) < curwp->w_doto)
{
ewprintf("do_undo: replace data error");
undo_reset(curbp);
return FALSE;
}
lreplace(undo->u_used, undo->u_buffer, TRUE);
break;
case UDNONE:
default:
panic("bug: do_undo");
}
}
}
if (firstcheck) {
WINDOW *wp;
curbp->b_flag &= ~BFCHG;
for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
if (wp->w_bufp == curbp) {
wp->w_flag |= WFMODE;
if(wp != curwp) wp->w_flag |= WFHARD;
}
}
}
return TRUE;
}
#endif
|