]> code.delx.au - gnu-emacs/blob - src/undo.c
*** empty log message ***
[gnu-emacs] / src / undo.c
1 /* undo handling for GNU Emacs.
2 Copyright (C) 1990, 1993, 1994, 2000 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21
22 #include <config.h>
23 #include "lisp.h"
24 #include "buffer.h"
25 #include "commands.h"
26
27 /* Limits controlling how much undo information to keep. */
28
29 EMACS_INT undo_limit;
30 EMACS_INT undo_strong_limit;
31
32 Lisp_Object Vundo_outer_limit;
33
34 /* Function to call when undo_outer_limit is exceeded. */
35
36 Lisp_Object Vundo_outer_limit_function;
37
38 /* Last buffer for which undo information was recorded. */
39 Lisp_Object last_undo_buffer;
40
41 Lisp_Object Qinhibit_read_only;
42
43 /* Marker for function call undo list elements. */
44
45 Lisp_Object Qapply;
46
47 /* The first time a command records something for undo.
48 it also allocates the undo-boundary object
49 which will be added to the list at the end of the command.
50 This ensures we can't run out of space while trying to make
51 an undo-boundary. */
52 Lisp_Object pending_boundary;
53
54 /* Record point as it was at beginning of this command (if necessary)
55 And prepare the undo info for recording a change.
56 PT is the position of point that will naturally occur as a result of the
57 undo record that will be added just after this command terminates. */
58
59 static void
60 record_point (pt)
61 int pt;
62 {
63 int at_boundary;
64
65 /* Allocate a cons cell to be the undo boundary after this command. */
66 if (NILP (pending_boundary))
67 pending_boundary = Fcons (Qnil, Qnil);
68
69 if (!BUFFERP (last_undo_buffer)
70 || current_buffer != XBUFFER (last_undo_buffer))
71 Fundo_boundary ();
72 XSETBUFFER (last_undo_buffer, current_buffer);
73
74 if (CONSP (current_buffer->undo_list))
75 {
76 /* Set AT_BOUNDARY to 1 only when we have nothing other than
77 marker adjustment before undo boundary. */
78
79 Lisp_Object tail = current_buffer->undo_list, elt;
80
81 while (1)
82 {
83 if (NILP (tail))
84 elt = Qnil;
85 else
86 elt = XCAR (tail);
87 if (NILP (elt) || ! (CONSP (elt) && MARKERP (XCAR (elt))))
88 break;
89 tail = XCDR (tail);
90 }
91 at_boundary = NILP (elt);
92 }
93 else
94 at_boundary = 1;
95
96 if (MODIFF <= SAVE_MODIFF)
97 record_first_change ();
98
99 /* If we are just after an undo boundary, and
100 point wasn't at start of deleted range, record where it was. */
101 if (at_boundary
102 && last_point_position != pt
103 /* If we're called from batch mode, this could be nil. */
104 && BUFFERP (last_point_position_buffer)
105 && current_buffer == XBUFFER (last_point_position_buffer))
106 current_buffer->undo_list
107 = Fcons (make_number (last_point_position), current_buffer->undo_list);
108 }
109
110 /* Record an insertion that just happened or is about to happen,
111 for LENGTH characters at position BEG.
112 (It is possible to record an insertion before or after the fact
113 because we don't need to record the contents.) */
114
115 void
116 record_insert (beg, length)
117 int beg, length;
118 {
119 Lisp_Object lbeg, lend;
120
121 if (EQ (current_buffer->undo_list, Qt))
122 return;
123
124 record_point (beg);
125
126 /* If this is following another insertion and consecutive with it
127 in the buffer, combine the two. */
128 if (CONSP (current_buffer->undo_list))
129 {
130 Lisp_Object elt;
131 elt = XCAR (current_buffer->undo_list);
132 if (CONSP (elt)
133 && INTEGERP (XCAR (elt))
134 && INTEGERP (XCDR (elt))
135 && XINT (XCDR (elt)) == beg)
136 {
137 XSETCDR (elt, make_number (beg + length));
138 return;
139 }
140 }
141
142 XSETFASTINT (lbeg, beg);
143 XSETINT (lend, beg + length);
144 current_buffer->undo_list = Fcons (Fcons (lbeg, lend),
145 current_buffer->undo_list);
146 }
147
148 /* Record that a deletion is about to take place,
149 of the characters in STRING, at location BEG. */
150
151 void
152 record_delete (beg, string)
153 int beg;
154 Lisp_Object string;
155 {
156 Lisp_Object sbeg;
157
158 if (EQ (current_buffer->undo_list, Qt))
159 return;
160
161 if (PT == beg + SCHARS (string))
162 {
163 XSETINT (sbeg, -beg);
164 record_point (PT);
165 }
166 else
167 {
168 XSETFASTINT (sbeg, beg);
169 record_point (beg);
170 }
171
172 current_buffer->undo_list
173 = Fcons (Fcons (string, sbeg), current_buffer->undo_list);
174 }
175
176 /* Record the fact that MARKER is about to be adjusted by ADJUSTMENT.
177 This is done only when a marker points within text being deleted,
178 because that's the only case where an automatic marker adjustment
179 won't be inverted automatically by undoing the buffer modification. */
180
181 void
182 record_marker_adjustment (marker, adjustment)
183 Lisp_Object marker;
184 int adjustment;
185 {
186 if (EQ (current_buffer->undo_list, Qt))
187 return;
188
189 /* Allocate a cons cell to be the undo boundary after this command. */
190 if (NILP (pending_boundary))
191 pending_boundary = Fcons (Qnil, Qnil);
192
193 if (!BUFFERP (last_undo_buffer)
194 || current_buffer != XBUFFER (last_undo_buffer))
195 Fundo_boundary ();
196 XSETBUFFER (last_undo_buffer, current_buffer);
197
198 current_buffer->undo_list
199 = Fcons (Fcons (marker, make_number (adjustment)),
200 current_buffer->undo_list);
201 }
202
203 /* Record that a replacement is about to take place,
204 for LENGTH characters at location BEG.
205 The replacement must not change the number of characters. */
206
207 void
208 record_change (beg, length)
209 int beg, length;
210 {
211 record_delete (beg, make_buffer_string (beg, beg + length, 1));
212 record_insert (beg, length);
213 }
214 \f
215 /* Record that an unmodified buffer is about to be changed.
216 Record the file modification date so that when undoing this entry
217 we can tell whether it is obsolete because the file was saved again. */
218
219 void
220 record_first_change ()
221 {
222 Lisp_Object high, low;
223 struct buffer *base_buffer = current_buffer;
224
225 if (EQ (current_buffer->undo_list, Qt))
226 return;
227
228 if (!BUFFERP (last_undo_buffer)
229 || current_buffer != XBUFFER (last_undo_buffer))
230 Fundo_boundary ();
231 XSETBUFFER (last_undo_buffer, current_buffer);
232
233 if (base_buffer->base_buffer)
234 base_buffer = base_buffer->base_buffer;
235
236 XSETFASTINT (high, (base_buffer->modtime >> 16) & 0xffff);
237 XSETFASTINT (low, base_buffer->modtime & 0xffff);
238 current_buffer->undo_list = Fcons (Fcons (Qt, Fcons (high, low)), current_buffer->undo_list);
239 }
240
241 /* Record a change in property PROP (whose old value was VAL)
242 for LENGTH characters starting at position BEG in BUFFER. */
243
244 void
245 record_property_change (beg, length, prop, value, buffer)
246 int beg, length;
247 Lisp_Object prop, value, buffer;
248 {
249 Lisp_Object lbeg, lend, entry;
250 struct buffer *obuf = current_buffer;
251 int boundary = 0;
252
253 if (EQ (XBUFFER (buffer)->undo_list, Qt))
254 return;
255
256 /* Allocate a cons cell to be the undo boundary after this command. */
257 if (NILP (pending_boundary))
258 pending_boundary = Fcons (Qnil, Qnil);
259
260 if (!EQ (buffer, last_undo_buffer))
261 boundary = 1;
262 last_undo_buffer = buffer;
263
264 /* Switch temporarily to the buffer that was changed. */
265 current_buffer = XBUFFER (buffer);
266
267 if (boundary)
268 Fundo_boundary ();
269
270 if (MODIFF <= SAVE_MODIFF)
271 record_first_change ();
272
273 XSETINT (lbeg, beg);
274 XSETINT (lend, beg + length);
275 entry = Fcons (Qnil, Fcons (prop, Fcons (value, Fcons (lbeg, lend))));
276 current_buffer->undo_list = Fcons (entry, current_buffer->undo_list);
277
278 current_buffer = obuf;
279 }
280
281 DEFUN ("undo-boundary", Fundo_boundary, Sundo_boundary, 0, 0, 0,
282 doc: /* Mark a boundary between units of undo.
283 An undo command will stop at this point,
284 but another undo command will undo to the previous boundary. */)
285 ()
286 {
287 Lisp_Object tem;
288 if (EQ (current_buffer->undo_list, Qt))
289 return Qnil;
290 tem = Fcar (current_buffer->undo_list);
291 if (!NILP (tem))
292 {
293 /* One way or another, cons nil onto the front of the undo list. */
294 if (!NILP (pending_boundary))
295 {
296 /* If we have preallocated the cons cell to use here,
297 use that one. */
298 XSETCDR (pending_boundary, current_buffer->undo_list);
299 current_buffer->undo_list = pending_boundary;
300 pending_boundary = Qnil;
301 }
302 else
303 current_buffer->undo_list = Fcons (Qnil, current_buffer->undo_list);
304 }
305 return Qnil;
306 }
307
308 /* At garbage collection time, make an undo list shorter at the end,
309 returning the truncated list. How this is done depends on the
310 variables undo-limit, undo-strong-limit and undo-outer-limit.
311 In some cases this works by calling undo-outer-limit-function. */
312
313 void
314 truncate_undo_list (b)
315 struct buffer *b;
316 {
317 Lisp_Object list;
318 Lisp_Object prev, next, last_boundary;
319 int size_so_far = 0;
320
321 /* Make sure that calling undo-outer-limit-function
322 won't cause another GC. */
323 int count = inhibit_garbage_collection ();
324
325 /* Make the buffer current to get its local values of variables such
326 as undo_limit. Also so that Vundo_outer_limit_function can
327 tell which buffer to operate on. */
328 record_unwind_protect (set_buffer_if_live, Fcurrent_buffer ());
329 set_buffer_internal (b);
330
331 list = b->undo_list;
332
333 prev = Qnil;
334 next = list;
335 last_boundary = Qnil;
336
337 /* If the first element is an undo boundary, skip past it. */
338 if (CONSP (next) && NILP (XCAR (next)))
339 {
340 /* Add in the space occupied by this element and its chain link. */
341 size_so_far += sizeof (struct Lisp_Cons);
342
343 /* Advance to next element. */
344 prev = next;
345 next = XCDR (next);
346 }
347
348 /* Always preserve at least the most recent undo record
349 unless it is really horribly big.
350
351 Skip, skip, skip the undo, skip, skip, skip the undo,
352 Skip, skip, skip the undo, skip to the undo bound'ry. */
353
354 while (CONSP (next) && ! NILP (XCAR (next)))
355 {
356 Lisp_Object elt;
357 elt = XCAR (next);
358
359 /* Add in the space occupied by this element and its chain link. */
360 size_so_far += sizeof (struct Lisp_Cons);
361 if (CONSP (elt))
362 {
363 size_so_far += sizeof (struct Lisp_Cons);
364 if (STRINGP (XCAR (elt)))
365 size_so_far += (sizeof (struct Lisp_String) - 1
366 + SCHARS (XCAR (elt)));
367 }
368
369 /* Advance to next element. */
370 prev = next;
371 next = XCDR (next);
372 }
373
374 /* If by the first boundary we have already passed undo_outer_limit,
375 we're heading for memory full, so offer to clear out the list. */
376 if (INTEGERP (Vundo_outer_limit)
377 && size_so_far > XINT (Vundo_outer_limit)
378 && !NILP (Vundo_outer_limit_function))
379 {
380 Lisp_Object temp = last_undo_buffer;
381
382 /* Normally the function this calls is undo-outer-limit-truncate. */
383 if (! NILP (call1 (Vundo_outer_limit_function,
384 make_number (size_so_far))))
385 {
386 /* The function is responsible for making
387 any desired changes in buffer-undo-list. */
388 unbind_to (count, Qnil);
389 return;
390 }
391 /* That function probably used the minibuffer, and if so, that
392 changed last_undo_buffer. Change it back so that we don't
393 force next change to make an undo boundary here. */
394 last_undo_buffer = temp;
395 }
396
397 if (CONSP (next))
398 last_boundary = prev;
399
400 /* Keep additional undo data, if it fits in the limits. */
401 while (CONSP (next))
402 {
403 Lisp_Object elt;
404 elt = XCAR (next);
405
406 /* When we get to a boundary, decide whether to truncate
407 either before or after it. The lower threshold, undo_limit,
408 tells us to truncate after it. If its size pushes past
409 the higher threshold undo_strong_limit, we truncate before it. */
410 if (NILP (elt))
411 {
412 if (size_so_far > undo_strong_limit)
413 break;
414 last_boundary = prev;
415 if (size_so_far > undo_limit)
416 break;
417 }
418
419 /* Add in the space occupied by this element and its chain link. */
420 size_so_far += sizeof (struct Lisp_Cons);
421 if (CONSP (elt))
422 {
423 size_so_far += sizeof (struct Lisp_Cons);
424 if (STRINGP (XCAR (elt)))
425 size_so_far += (sizeof (struct Lisp_String) - 1
426 + SCHARS (XCAR (elt)));
427 }
428
429 /* Advance to next element. */
430 prev = next;
431 next = XCDR (next);
432 }
433
434 /* If we scanned the whole list, it is short enough; don't change it. */
435 if (NILP (next))
436 ;
437 /* Truncate at the boundary where we decided to truncate. */
438 else if (!NILP (last_boundary))
439 XSETCDR (last_boundary, Qnil);
440 /* There's nothing we decided to keep, so clear it out. */
441 else
442 b->undo_list = Qnil;
443
444 unbind_to (count, Qnil);
445 }
446 \f
447 DEFUN ("primitive-undo", Fprimitive_undo, Sprimitive_undo, 2, 2, 0,
448 doc: /* Undo N records from the front of the list LIST.
449 Return what remains of the list. */)
450 (n, list)
451 Lisp_Object n, list;
452 {
453 struct gcpro gcpro1, gcpro2;
454 Lisp_Object next;
455 int count = SPECPDL_INDEX ();
456 register int arg;
457
458 #if 0 /* This is a good feature, but would make undo-start
459 unable to do what is expected. */
460 Lisp_Object tem;
461
462 /* If the head of the list is a boundary, it is the boundary
463 preceding this command. Get rid of it and don't count it. */
464 tem = Fcar (list);
465 if (NILP (tem))
466 list = Fcdr (list);
467 #endif
468
469 CHECK_NUMBER (n);
470 arg = XINT (n);
471 next = Qnil;
472 GCPRO2 (next, list);
473
474 /* In a writable buffer, enable undoing read-only text that is so
475 because of text properties. */
476 if (NILP (current_buffer->read_only))
477 specbind (Qinhibit_read_only, Qt);
478
479 /* Don't let `intangible' properties interfere with undo. */
480 specbind (Qinhibit_point_motion_hooks, Qt);
481
482 while (arg > 0)
483 {
484 while (CONSP (list))
485 {
486 next = XCAR (list);
487 list = XCDR (list);
488 /* Exit inner loop at undo boundary. */
489 if (NILP (next))
490 break;
491 /* Handle an integer by setting point to that value. */
492 if (INTEGERP (next))
493 SET_PT (clip_to_bounds (BEGV, XINT (next), ZV));
494 else if (CONSP (next))
495 {
496 Lisp_Object car, cdr;
497
498 car = XCAR (next);
499 cdr = XCDR (next);
500 if (EQ (car, Qt))
501 {
502 /* Element (t high . low) records previous modtime. */
503 Lisp_Object high, low;
504 int mod_time;
505 struct buffer *base_buffer = current_buffer;
506
507 high = Fcar (cdr);
508 low = Fcdr (cdr);
509 mod_time = (XFASTINT (high) << 16) + XFASTINT (low);
510
511 if (current_buffer->base_buffer)
512 base_buffer = current_buffer->base_buffer;
513
514 /* If this records an obsolete save
515 (not matching the actual disk file)
516 then don't mark unmodified. */
517 if (mod_time != base_buffer->modtime)
518 continue;
519 #ifdef CLASH_DETECTION
520 Funlock_buffer ();
521 #endif /* CLASH_DETECTION */
522 Fset_buffer_modified_p (Qnil);
523 }
524 else if (EQ (car, Qnil))
525 {
526 /* Element (nil PROP VAL BEG . END) is property change. */
527 Lisp_Object beg, end, prop, val;
528
529 prop = Fcar (cdr);
530 cdr = Fcdr (cdr);
531 val = Fcar (cdr);
532 cdr = Fcdr (cdr);
533 beg = Fcar (cdr);
534 end = Fcdr (cdr);
535
536 Fput_text_property (beg, end, prop, val, Qnil);
537 }
538 else if (INTEGERP (car) && INTEGERP (cdr))
539 {
540 /* Element (BEG . END) means range was inserted. */
541
542 if (XINT (car) < BEGV
543 || XINT (cdr) > ZV)
544 error ("Changes to be undone are outside visible portion of buffer");
545 /* Set point first thing, so that undoing this undo
546 does not send point back to where it is now. */
547 Fgoto_char (car);
548 Fdelete_region (car, cdr);
549 }
550 else if (EQ (car, Qapply))
551 {
552 Lisp_Object oldlist = current_buffer->undo_list;
553 /* Element (apply FUNNAME . ARGS) means call FUNNAME to undo. */
554 car = Fcar (cdr);
555 if (INTEGERP (car))
556 {
557 /* Long format: (apply DELTA START END FUNNAME . ARGS). */
558 cdr = Fcdr (Fcdr (Fcdr (cdr)));
559 car = Fcar (cdr);
560 }
561 cdr = Fcdr (cdr);
562 apply1 (car, cdr);
563
564 /* Make sure this produces at least one undo entry,
565 so the test in `undo' for continuing an undo series
566 will work right. */
567 if (EQ (oldlist, current_buffer->undo_list))
568 current_buffer->undo_list
569 = Fcons (list3 (Qapply, Qcdr, Qnil), current_buffer->undo_list);
570 }
571 else if (STRINGP (car) && INTEGERP (cdr))
572 {
573 /* Element (STRING . POS) means STRING was deleted. */
574 Lisp_Object membuf;
575 int pos = XINT (cdr);
576
577 membuf = car;
578 if (pos < 0)
579 {
580 if (-pos < BEGV || -pos > ZV)
581 error ("Changes to be undone are outside visible portion of buffer");
582 SET_PT (-pos);
583 Finsert (1, &membuf);
584 }
585 else
586 {
587 if (pos < BEGV || pos > ZV)
588 error ("Changes to be undone are outside visible portion of buffer");
589 SET_PT (pos);
590
591 /* Now that we record marker adjustments
592 (caused by deletion) for undo,
593 we should always insert after markers,
594 so that undoing the marker adjustments
595 put the markers back in the right place. */
596 Finsert (1, &membuf);
597 SET_PT (pos);
598 }
599 }
600 else if (MARKERP (car) && INTEGERP (cdr))
601 {
602 /* (MARKER . INTEGER) means a marker MARKER
603 was adjusted by INTEGER. */
604 if (XMARKER (car)->buffer)
605 Fset_marker (car,
606 make_number (marker_position (car) - XINT (cdr)),
607 Fmarker_buffer (car));
608 }
609 }
610 }
611 arg--;
612 }
613
614 UNGCPRO;
615 return unbind_to (count, list);
616 }
617 \f
618 void
619 syms_of_undo ()
620 {
621 Qinhibit_read_only = intern ("inhibit-read-only");
622 staticpro (&Qinhibit_read_only);
623
624 Qapply = intern ("apply");
625 staticpro (&Qapply);
626
627 pending_boundary = Qnil;
628 staticpro (&pending_boundary);
629
630 defsubr (&Sprimitive_undo);
631 defsubr (&Sundo_boundary);
632
633 DEFVAR_INT ("undo-limit", &undo_limit,
634 doc: /* Keep no more undo information once it exceeds this size.
635 This limit is applied when garbage collection happens.
636 When a previous command increases the total undo list size past this
637 value, the earlier commands that came before it are forgotten.
638
639 The size is counted as the number of bytes occupied,
640 which includes both saved text and other data. */);
641 undo_limit = 20000;
642
643 DEFVAR_INT ("undo-strong-limit", &undo_strong_limit,
644 doc: /* Don't keep more than this much size of undo information.
645 This limit is applied when garbage collection happens.
646 When a previous command increases the total undo list size past this
647 value, that command and the earlier commands that came before it are forgotten.
648 However, the most recent buffer-modifying command's undo info
649 is never discarded for this reason.
650
651 The size is counted as the number of bytes occupied,
652 which includes both saved text and other data. */);
653 undo_strong_limit = 30000;
654
655 DEFVAR_LISP ("undo-outer-limit", &Vundo_outer_limit,
656 doc: /* Outer limit on size of undo information for one command.
657 At garbage collection time, if the current command has produced
658 more than this much undo information, it discards the info and displays
659 a warning. This is a last-ditch limit to prevent memory overflow.
660
661 The size is counted as the number of bytes occupied, which includes
662 both saved text and other data. A value of nil means no limit. In
663 this case, accumulating one huge undo entry could make Emacs crash as
664 a result of memory overflow.
665
666 In fact, this calls the function which is the value of
667 `undo-outer-limit-function' with one argument, the size.
668 The text above describes the behavior of the function
669 that variable usually specifies. */);
670 Vundo_outer_limit = make_number (3000000);
671
672 DEFVAR_LISP ("undo-outer-limit-function", &Vundo_outer_limit_function,
673 doc: /* Function to call when an undo list exceeds `undo-outer-limit'.
674 This function is called with one argument, the current undo list size
675 for the most recent command (since the last undo boundary).
676 If the function returns t, that means truncation has been fully handled.
677 If it returns nil, the other forms of truncation are done.
678
679 Garbage collection is inhibited around the call to this function,
680 so it must make sure not to do a lot of consing. */);
681 Vundo_outer_limit_function = Qnil;
682 }
683
684 /* arch-tag: d546ee01-4aed-4ffb-bb8b-eefaae50d38a
685 (do not change this comment) */