[go: up one dir, main page]

Menu

[bde3dc]: / source / undo.c  Maximize  Restore  History

Download this file

498 lines (429 with data), 15.6 kB

  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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*******************************************************************************
* *
* undo.c -- Nirvana Editor undo command *
* *
* Copyright (C) 1999 Mark Edel *
* *
* This 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 of the License, or (at your option) any later *
* version. In addition, you may distribute version of this program linked to *
* Motif or Open Motif. See README for details. *
* *
* This software 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 *
* software; if not, write to the Free Software Foundation, Inc., 59 Temple *
* Place, Suite 330, Boston, MA 02111-1307 USA *
* *
* Nirvana Text Editor *
* May 10, 1991 *
* *
* Written by Mark Edel *
* *
*******************************************************************************/
#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif
#include "undo.h"
#include "textBuf.h"
#include "text.h"
#include "nedit.h"
#include "search.h"
#include "window.h"
#include "file.h"
#include "userCmds.h"
#include "preferences.h"
#include "../util/nedit_malloc.h"
#include <string.h>
#ifdef VMS
#include "../util/VMSparam.h"
#else
#ifndef __MVS__
#include <sys/param.h>
#endif
#endif /*VMS*/
#include <Xm/Xm.h>
#include <Xm/Text.h>
#ifdef HAVE_DEBUG_H
#include "../debug.h"
#endif
#define FORWARD 1
#define REVERSE 2
static void addUndoItem(WindowInfo *window, UndoInfo *undo);
static void addRedoItem(WindowInfo *window, UndoInfo *redo);
static void removeUndoItem(WindowInfo *window);
static void removeRedoItem(WindowInfo *window);
static void appendDeletedText(WindowInfo *window, const char *deletedText,
int deletedLen, int direction);
static void trimUndoList(WindowInfo *window, int maxLength);
static int determineUndoType(int nInserted, int nDeleted);
static void freeUndoRecord(UndoInfo *undo);
void Undo(WindowInfo *window)
{
UndoInfo *undo = window->undo;
int restoredTextLength;
/* return if nothing to undo */
if (undo == NULL)
return;
/* BufReplace will eventually call SaveUndoInformation. This is mostly
good because it makes accumulating redo operations easier, however
SaveUndoInformation needs to know that it is being called in the context
of an undo. The inUndo field in the undo record indicates that this
record is in the process of being undone. */
undo->inUndo = True;
/* use the saved undo information to reverse changes */
BufReplace(window->buffer, undo->startPos, undo->endPos,
(undo->oldText != NULL ? undo->oldText : ""));
restoredTextLength = undo->oldText != NULL ? strlen(undo->oldText) : 0;
if (!window->buffer->primary.selected || GetPrefUndoModifiesSelection()) {
/* position the cursor in the focus pane after the changed text
to show the user where the undo was done */
TextSetCursorPos(window->lastFocus, undo->startPos +
restoredTextLength);
}
if (GetPrefUndoModifiesSelection()) {
if (restoredTextLength > 0) {
BufSelect(window->buffer, undo->startPos, undo->startPos +
restoredTextLength);
}
else {
BufUnselect(window->buffer);
}
}
MakeSelectionVisible(window, window->lastFocus);
/* restore the file's unmodified status if the file was unmodified
when the change being undone was originally made. Also, remove
the backup file, since the text in the buffer is now identical to
the original file */
if (undo->restoresToSaved) {
SetWindowModified(window, False);
RemoveBackupFile(window);
}
/* free the undo record and remove it from the chain */
removeUndoItem(window);
}
void Redo(WindowInfo *window)
{
UndoInfo *redo = window->redo;
int restoredTextLength;
/* return if nothing to redo */
if (window->redo == NULL)
return;
/* BufReplace will eventually call SaveUndoInformation. To indicate
to SaveUndoInformation that this is the context of a redo operation,
we set the inUndo indicator in the redo record */
redo->inUndo = True;
/* use the saved redo information to reverse changes */
BufReplace(window->buffer, redo->startPos, redo->endPos,
(redo->oldText != NULL ? redo->oldText : ""));
restoredTextLength = redo->oldText != NULL ? strlen(redo->oldText) : 0;
if (!window->buffer->primary.selected || GetPrefUndoModifiesSelection()) {
/* position the cursor in the focus pane after the changed text
to show the user where the undo was done */
TextSetCursorPos(window->lastFocus, redo->startPos +
restoredTextLength);
}
if (GetPrefUndoModifiesSelection()) {
if (restoredTextLength > 0) {
BufSelect(window->buffer, redo->startPos, redo->startPos +
restoredTextLength);
}
else {
BufUnselect(window->buffer);
}
}
MakeSelectionVisible(window, window->lastFocus);
/* restore the file's unmodified status if the file was unmodified
when the change being redone was originally made. Also, remove
the backup file, since the text in the buffer is now identical to
the original file */
if (redo->restoresToSaved) {
SetWindowModified(window, False);
RemoveBackupFile(window);
}
/* remove the redo record from the chain and free it */
removeRedoItem(window);
}
/*
** SaveUndoInformation stores away the changes made to the text buffer. As a
** side effect, it also increments the autoSave operation and character counts
** since it needs to do the classification anyhow.
**
** Note: This routine must be kept efficient. It is called for every
** character typed.
*/
void SaveUndoInformation(WindowInfo *window, int pos, int nInserted,
int nDeleted, const char *deletedText)
{
int newType, oldType;
UndoInfo *u, *undo = window->undo;
int isUndo = (undo != NULL && undo->inUndo);
int isRedo = (window->redo != NULL && window->redo->inUndo);
/* redo operations become invalid once the user begins typing or does
other editing. If this is not a redo or undo operation and a redo
list still exists, clear it and dim the redo menu item */
if (!(isUndo || isRedo) && window->redo != NULL)
ClearRedoList(window);
/* figure out what kind of editing operation this is, and recall
what the last one was */
newType = determineUndoType(nInserted, nDeleted);
if (newType == UNDO_NOOP)
return;
oldType = (undo == NULL || isUndo) ? UNDO_NOOP : undo->type;
/*
** Check for continuations of single character operations. These are
** accumulated so a whole insertion or deletion can be undone, rather
** than just the last character that the user typed. If the window
** is currently in an unmodified state, don't accumulate operations
** across the save, so the user can undo back to the unmodified state.
*/
if (window->fileChanged) {
/* normal sequential character insertion */
if ( ((oldType == ONE_CHAR_INSERT || oldType == ONE_CHAR_REPLACE)
&& newType == ONE_CHAR_INSERT) && (pos == undo->endPos)) {
undo->endPos++;
window->autoSaveCharCount++;
return;
}
/* overstrike mode replacement */
if ((oldType == ONE_CHAR_REPLACE && newType == ONE_CHAR_REPLACE) &&
(pos == undo->endPos)) {
appendDeletedText(window, deletedText, nDeleted, FORWARD);
undo->endPos++;
window->autoSaveCharCount++;
return;
}
/* forward delete */
if ((oldType==ONE_CHAR_DELETE && newType==ONE_CHAR_DELETE) &&
(pos==undo->startPos)) {
appendDeletedText(window, deletedText, nDeleted, FORWARD);
return;
}
/* reverse delete */
if ((oldType==ONE_CHAR_DELETE && newType==ONE_CHAR_DELETE) &&
(pos == undo->startPos-1)) {
appendDeletedText(window, deletedText, nDeleted, REVERSE);
undo->startPos--;
undo->endPos--;
return;
}
}
/*
** The user has started a new operation, create a new undo record
** and save the new undo data.
*/
undo = (UndoInfo *)NEditMalloc(sizeof(UndoInfo));
undo->oldLen = 0;
undo->oldText = NULL;
undo->type = newType;
undo->inUndo = False;
undo->restoresToSaved = False;
undo->startPos = pos;
undo->endPos = pos + nInserted;
/* if text was deleted, save it */
if (nDeleted > 0) {
undo->oldLen = nDeleted + 1; /* +1 is for null at end */
undo->oldText = (char*)NEditMalloc(nDeleted + 1);
strcpy(undo->oldText, deletedText);
}
/* increment the operation count for the autosave feature */
window->autoSaveOpCount++;
/* if the window is currently unmodified, remove the previous
restoresToSaved marker, and set it on this record */
if (!window->fileChanged) {
undo->restoresToSaved = True;
for (u=window->undo; u!=NULL; u=u->next)
u->restoresToSaved = False;
for (u=window->redo; u!=NULL; u=u->next)
u->restoresToSaved = False;
}
/* Add the new record to the undo list unless SaveUndoInfo is
saving information generated by an Undo operation itself, in
which case, add the new record to the redo list. */
if (isUndo)
addRedoItem(window, undo);
else
addUndoItem(window, undo);
}
/*
** ClearUndoList, ClearRedoList
**
** Functions for clearing all of the information off of the undo or redo
** lists and adjusting the edit menu accordingly
*/
void ClearUndoList(WindowInfo *window)
{
while (window->undo != NULL)
removeUndoItem(window);
}
void ClearRedoList(WindowInfo *window)
{
while (window->redo != NULL)
removeRedoItem(window);
}
/*
** Add an undo record (already allocated by the caller) to the window's undo
** list if the item pushes the undo operation or character counts past the
** limits, trim the undo list to an acceptable length.
*/
static void addUndoItem(WindowInfo *window, UndoInfo *undo)
{
/* Make the undo menu item sensitive now that there's something to undo */
if (window->undo == NULL) {
SetSensitive(window, window->undoItem, True);
SetBGMenuUndoSensitivity(window, True);
}
/* Add the item to the beginning of the list */
undo->next = window->undo;
window->undo = undo;
/* Increment the operation and memory counts */
window->undoOpCount++;
window->undoMemUsed += undo->oldLen;
/* Trim the list if it exceeds any of the limits */
if (window->undoOpCount > UNDO_OP_LIMIT)
trimUndoList(window, UNDO_OP_TRIMTO);
if (window->undoMemUsed > UNDO_WORRY_LIMIT)
trimUndoList(window, UNDO_WORRY_TRIMTO);
if (window->undoMemUsed > UNDO_PURGE_LIMIT)
trimUndoList(window, UNDO_PURGE_TRIMTO);
}
/*
** Add an item (already allocated by the caller) to the window's redo list.
*/
static void addRedoItem(WindowInfo *window, UndoInfo *redo)
{
/* Make the redo menu item sensitive now that there's something to redo */
if (window->redo == NULL) {
SetSensitive(window, window->redoItem, True);
SetBGMenuRedoSensitivity(window, True);
}
/* Add the item to the beginning of the list */
redo->next = window->redo;
window->redo = redo;
}
/*
** Pop (remove and free) the current (front) undo record from the undo list
*/
static void removeUndoItem(WindowInfo *window)
{
UndoInfo *undo = window->undo;
if (undo == NULL)
return;
/* Decrement the operation and memory counts */
window->undoOpCount--;
window->undoMemUsed -= undo->oldLen;
/* Remove and free the item */
window->undo = undo->next;
freeUndoRecord(undo);
/* if there are no more undo records left, dim the Undo menu item */
if (window->undo == NULL) {
SetSensitive(window, window->undoItem, False);
SetBGMenuUndoSensitivity(window, False);
}
}
/*
** Pop (remove and free) the current (front) redo record from the redo list
*/
static void removeRedoItem(WindowInfo *window)
{
UndoInfo *redo = window->redo;
/* Remove and free the item */
window->redo = redo->next;
freeUndoRecord(redo);
/* if there are no more redo records left, dim the Redo menu item */
if (window->redo == NULL) {
SetSensitive(window, window->redoItem, False);
SetBGMenuRedoSensitivity(window, False);
}
}
/*
** Add deleted text to the beginning or end
** of the text saved for undoing the last operation. This routine is intended
** for continuing of a string of one character deletes or replaces, but will
** work with more than one character.
*/
static void appendDeletedText(WindowInfo *window, const char *deletedText,
int deletedLen, int direction)
{
UndoInfo *undo = window->undo;
char *comboText;
/* re-allocate, adding space for the new character(s) */
comboText = (char*)NEditMalloc(undo->oldLen + deletedLen);
/* copy the new character and the already deleted text to the new memory */
if (direction == FORWARD) {
strcpy(comboText, undo->oldText);
strcat(comboText, deletedText);
} else {
strcpy(comboText, deletedText);
strcat(comboText, undo->oldText);
}
/* keep track of the additional memory now used by the undo list */
window->undoMemUsed++;
/* free the old saved text and attach the new */
NEditFree(undo->oldText);
undo->oldText = comboText;
undo->oldLen += deletedLen;
}
/*
** Trim records off of the END of the undo list to reduce it to length
** maxLength
*/
static void trimUndoList(WindowInfo *window, int maxLength)
{
int i;
UndoInfo *u, *lastRec;
if (window->undo == NULL)
return;
/* Find last item on the list to leave intact */
for (i=1, u=window->undo; i<maxLength && u!=NULL; i++, u=u->next);
if (u == NULL)
return;
/* Trim off all subsequent entries */
lastRec = u;
while (lastRec->next != NULL) {
u = lastRec->next;
lastRec->next = u->next;
window->undoOpCount--;
window->undoMemUsed -= u->oldLen;
freeUndoRecord(u);
}
}
static int determineUndoType(int nInserted, int nDeleted)
{
int textDeleted, textInserted;
textDeleted = (nDeleted > 0);
textInserted = (nInserted > 0);
if (textInserted && !textDeleted) {
/* Insert */
if (nInserted == 1)
return ONE_CHAR_INSERT;
else
return BLOCK_INSERT;
} else if (textInserted && textDeleted) {
/* Replace */
if (nInserted == 1)
return ONE_CHAR_REPLACE;
else
return BLOCK_REPLACE;
} else if (!textInserted && textDeleted) {
/* Delete */
if (nDeleted == 1)
return ONE_CHAR_DELETE;
else
return BLOCK_DELETE;
} else {
/* Nothing deleted or inserted */
return UNDO_NOOP;
}
}
static void freeUndoRecord(UndoInfo *undo)
{
if (undo == NULL)
return;
NEditFree(undo->oldText);
NEditFree(undo);
}