Partially undo the dispatcher split, it needs to happen more gradually
[emacs.git] / src / undo.c
blob22ebc143355ca4cc962f0d44cc5a945d17294903
1 /* undo handling for GNU Emacs.
2 Copyright (C) 1990, 1993, 1994, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
23 #include <config.h>
24 #include "lisp.h"
25 #include "buffer.h"
26 #include "commands.h"
27 #include "window.h"
29 /* Limits controlling how much undo information to keep. */
31 EMACS_INT undo_limit;
32 EMACS_INT undo_strong_limit;
34 Lisp_Object Vundo_outer_limit;
36 /* Function to call when undo_outer_limit is exceeded. */
38 Lisp_Object Vundo_outer_limit_function;
40 /* Last buffer for which undo information was recorded. */
41 /* BEWARE: This is not traced by the GC, so never dereference it! */
42 struct buffer *last_undo_buffer;
44 /* Position of point last time we inserted a boundary. */
45 struct buffer *last_boundary_buffer;
46 EMACS_INT last_boundary_position;
48 Lisp_Object Qinhibit_read_only;
50 /* Marker for function call undo list elements. */
52 Lisp_Object Qapply;
54 /* The first time a command records something for undo.
55 it also allocates the undo-boundary object
56 which will be added to the list at the end of the command.
57 This ensures we can't run out of space while trying to make
58 an undo-boundary. */
59 Lisp_Object pending_boundary;
61 /* Nonzero means do not record point in record_point. */
63 int undo_inhibit_record_point;
65 /* Record point as it was at beginning of this command (if necessary)
66 and prepare the undo info for recording a change.
67 PT is the position of point that will naturally occur as a result of the
68 undo record that will be added just after this command terminates. */
70 static void
71 record_point (pt)
72 int pt;
74 int at_boundary;
76 /* Don't record position of pt when undo_inhibit_record_point holds. */
77 if (undo_inhibit_record_point)
78 return;
80 /* Allocate a cons cell to be the undo boundary after this command. */
81 if (NILP (pending_boundary))
82 pending_boundary = Fcons (Qnil, Qnil);
84 if (current_buffer != last_undo_buffer)
85 Fundo_boundary ();
86 last_undo_buffer = current_buffer;
88 if (CONSP (current_buffer->undo_list))
90 /* Set AT_BOUNDARY to 1 only when we have nothing other than
91 marker adjustment before undo boundary. */
93 Lisp_Object tail = current_buffer->undo_list, elt;
95 while (1)
97 if (NILP (tail))
98 elt = Qnil;
99 else
100 elt = XCAR (tail);
101 if (NILP (elt) || ! (CONSP (elt) && MARKERP (XCAR (elt))))
102 break;
103 tail = XCDR (tail);
105 at_boundary = NILP (elt);
107 else
108 at_boundary = 1;
110 if (MODIFF <= SAVE_MODIFF)
111 record_first_change ();
113 /* If we are just after an undo boundary, and
114 point wasn't at start of deleted range, record where it was. */
115 if (at_boundary
116 && current_buffer == last_boundary_buffer
117 && last_boundary_position != pt)
118 current_buffer->undo_list
119 = Fcons (make_number (last_boundary_position), current_buffer->undo_list);
122 /* Record an insertion that just happened or is about to happen,
123 for LENGTH characters at position BEG.
124 (It is possible to record an insertion before or after the fact
125 because we don't need to record the contents.) */
127 void
128 record_insert (beg, length)
129 int beg, length;
131 Lisp_Object lbeg, lend;
133 if (EQ (current_buffer->undo_list, Qt))
134 return;
136 record_point (beg);
138 /* If this is following another insertion and consecutive with it
139 in the buffer, combine the two. */
140 if (CONSP (current_buffer->undo_list))
142 Lisp_Object elt;
143 elt = XCAR (current_buffer->undo_list);
144 if (CONSP (elt)
145 && INTEGERP (XCAR (elt))
146 && INTEGERP (XCDR (elt))
147 && XINT (XCDR (elt)) == beg)
149 XSETCDR (elt, make_number (beg + length));
150 return;
154 XSETFASTINT (lbeg, beg);
155 XSETINT (lend, beg + length);
156 current_buffer->undo_list = Fcons (Fcons (lbeg, lend),
157 current_buffer->undo_list);
160 /* Record that a deletion is about to take place,
161 of the characters in STRING, at location BEG. */
163 void
164 record_delete (beg, string)
165 int beg;
166 Lisp_Object string;
168 Lisp_Object sbeg;
170 if (EQ (current_buffer->undo_list, Qt))
171 return;
173 if (PT == beg + SCHARS (string))
175 XSETINT (sbeg, -beg);
176 record_point (PT);
178 else
180 XSETFASTINT (sbeg, beg);
181 record_point (beg);
184 current_buffer->undo_list
185 = Fcons (Fcons (string, sbeg), current_buffer->undo_list);
188 /* Record the fact that MARKER is about to be adjusted by ADJUSTMENT.
189 This is done only when a marker points within text being deleted,
190 because that's the only case where an automatic marker adjustment
191 won't be inverted automatically by undoing the buffer modification. */
193 void
194 record_marker_adjustment (marker, adjustment)
195 Lisp_Object marker;
196 int adjustment;
198 if (EQ (current_buffer->undo_list, Qt))
199 return;
201 /* Allocate a cons cell to be the undo boundary after this command. */
202 if (NILP (pending_boundary))
203 pending_boundary = Fcons (Qnil, Qnil);
205 if (current_buffer != last_undo_buffer)
206 Fundo_boundary ();
207 last_undo_buffer = current_buffer;
209 current_buffer->undo_list
210 = Fcons (Fcons (marker, make_number (adjustment)),
211 current_buffer->undo_list);
214 /* Record that a replacement is about to take place,
215 for LENGTH characters at location BEG.
216 The replacement must not change the number of characters. */
218 void
219 record_change (beg, length)
220 int beg, length;
222 record_delete (beg, make_buffer_string (beg, beg + length, 1));
223 record_insert (beg, length);
226 /* Record that an unmodified buffer is about to be changed.
227 Record the file modification date so that when undoing this entry
228 we can tell whether it is obsolete because the file was saved again. */
230 void
231 record_first_change ()
233 Lisp_Object high, low;
234 struct buffer *base_buffer = current_buffer;
236 if (EQ (current_buffer->undo_list, Qt))
237 return;
239 if (current_buffer != last_undo_buffer)
240 Fundo_boundary ();
241 last_undo_buffer = current_buffer;
243 if (base_buffer->base_buffer)
244 base_buffer = base_buffer->base_buffer;
246 XSETFASTINT (high, (base_buffer->modtime >> 16) & 0xffff);
247 XSETFASTINT (low, base_buffer->modtime & 0xffff);
248 current_buffer->undo_list = Fcons (Fcons (Qt, Fcons (high, low)), current_buffer->undo_list);
251 /* Record a change in property PROP (whose old value was VAL)
252 for LENGTH characters starting at position BEG in BUFFER. */
254 void
255 record_property_change (beg, length, prop, value, buffer)
256 int beg, length;
257 Lisp_Object prop, value, buffer;
259 Lisp_Object lbeg, lend, entry;
260 struct buffer *obuf = current_buffer, *buf = XBUFFER (buffer);
261 int boundary = 0;
263 if (EQ (buf->undo_list, Qt))
264 return;
266 /* Allocate a cons cell to be the undo boundary after this command. */
267 if (NILP (pending_boundary))
268 pending_boundary = Fcons (Qnil, Qnil);
270 if (buf != last_undo_buffer)
271 boundary = 1;
272 last_undo_buffer = buf;
274 /* Switch temporarily to the buffer that was changed. */
275 current_buffer = buf;
277 if (boundary)
278 Fundo_boundary ();
280 if (MODIFF <= SAVE_MODIFF)
281 record_first_change ();
283 XSETINT (lbeg, beg);
284 XSETINT (lend, beg + length);
285 entry = Fcons (Qnil, Fcons (prop, Fcons (value, Fcons (lbeg, lend))));
286 current_buffer->undo_list = Fcons (entry, current_buffer->undo_list);
288 current_buffer = obuf;
291 DEFUN ("undo-boundary", Fundo_boundary, Sundo_boundary, 0, 0, 0,
292 doc: /* Mark a boundary between units of undo.
293 An undo command will stop at this point,
294 but another undo command will undo to the previous boundary. */)
297 Lisp_Object tem;
298 if (EQ (current_buffer->undo_list, Qt))
299 return Qnil;
300 tem = Fcar (current_buffer->undo_list);
301 if (!NILP (tem))
303 /* One way or another, cons nil onto the front of the undo list. */
304 if (!NILP (pending_boundary))
306 /* If we have preallocated the cons cell to use here,
307 use that one. */
308 XSETCDR (pending_boundary, current_buffer->undo_list);
309 current_buffer->undo_list = pending_boundary;
310 pending_boundary = Qnil;
312 else
313 current_buffer->undo_list = Fcons (Qnil, current_buffer->undo_list);
315 last_boundary_position = PT;
316 last_boundary_buffer = current_buffer;
317 return Qnil;
320 /* At garbage collection time, make an undo list shorter at the end,
321 returning the truncated list. How this is done depends on the
322 variables undo-limit, undo-strong-limit and undo-outer-limit.
323 In some cases this works by calling undo-outer-limit-function. */
325 void
326 truncate_undo_list (b)
327 struct buffer *b;
329 Lisp_Object list;
330 Lisp_Object prev, next, last_boundary;
331 int size_so_far = 0;
333 /* Make sure that calling undo-outer-limit-function
334 won't cause another GC. */
335 int count = inhibit_garbage_collection ();
337 /* Make the buffer current to get its local values of variables such
338 as undo_limit. Also so that Vundo_outer_limit_function can
339 tell which buffer to operate on. */
340 record_unwind_protect (set_buffer_if_live, Fcurrent_buffer ());
341 set_buffer_internal (b);
343 list = b->undo_list;
345 prev = Qnil;
346 next = list;
347 last_boundary = Qnil;
349 /* If the first element is an undo boundary, skip past it. */
350 if (CONSP (next) && NILP (XCAR (next)))
352 /* Add in the space occupied by this element and its chain link. */
353 size_so_far += sizeof (struct Lisp_Cons);
355 /* Advance to next element. */
356 prev = next;
357 next = XCDR (next);
360 /* Always preserve at least the most recent undo record
361 unless it is really horribly big.
363 Skip, skip, skip the undo, skip, skip, skip the undo,
364 Skip, skip, skip the undo, skip to the undo bound'ry. */
366 while (CONSP (next) && ! NILP (XCAR (next)))
368 Lisp_Object elt;
369 elt = XCAR (next);
371 /* Add in the space occupied by this element and its chain link. */
372 size_so_far += sizeof (struct Lisp_Cons);
373 if (CONSP (elt))
375 size_so_far += sizeof (struct Lisp_Cons);
376 if (STRINGP (XCAR (elt)))
377 size_so_far += (sizeof (struct Lisp_String) - 1
378 + SCHARS (XCAR (elt)));
381 /* Advance to next element. */
382 prev = next;
383 next = XCDR (next);
386 /* If by the first boundary we have already passed undo_outer_limit,
387 we're heading for memory full, so offer to clear out the list. */
388 if (INTEGERP (Vundo_outer_limit)
389 && size_so_far > XINT (Vundo_outer_limit)
390 && !NILP (Vundo_outer_limit_function))
392 Lisp_Object tem;
393 struct buffer *temp = last_undo_buffer;
395 /* Normally the function this calls is undo-outer-limit-truncate. */
396 tem = call1 (Vundo_outer_limit_function, make_number (size_so_far));
397 if (! NILP (tem))
399 /* The function is responsible for making
400 any desired changes in buffer-undo-list. */
401 unbind_to (count, Qnil);
402 return;
404 /* That function probably used the minibuffer, and if so, that
405 changed last_undo_buffer. Change it back so that we don't
406 force next change to make an undo boundary here. */
407 last_undo_buffer = temp;
410 if (CONSP (next))
411 last_boundary = prev;
413 /* Keep additional undo data, if it fits in the limits. */
414 while (CONSP (next))
416 Lisp_Object elt;
417 elt = XCAR (next);
419 /* When we get to a boundary, decide whether to truncate
420 either before or after it. The lower threshold, undo_limit,
421 tells us to truncate after it. If its size pushes past
422 the higher threshold undo_strong_limit, we truncate before it. */
423 if (NILP (elt))
425 if (size_so_far > undo_strong_limit)
426 break;
427 last_boundary = prev;
428 if (size_so_far > undo_limit)
429 break;
432 /* Add in the space occupied by this element and its chain link. */
433 size_so_far += sizeof (struct Lisp_Cons);
434 if (CONSP (elt))
436 size_so_far += sizeof (struct Lisp_Cons);
437 if (STRINGP (XCAR (elt)))
438 size_so_far += (sizeof (struct Lisp_String) - 1
439 + SCHARS (XCAR (elt)));
442 /* Advance to next element. */
443 prev = next;
444 next = XCDR (next);
447 /* If we scanned the whole list, it is short enough; don't change it. */
448 if (NILP (next))
450 /* Truncate at the boundary where we decided to truncate. */
451 else if (!NILP (last_boundary))
452 XSETCDR (last_boundary, Qnil);
453 /* There's nothing we decided to keep, so clear it out. */
454 else
455 b->undo_list = Qnil;
457 unbind_to (count, Qnil);
460 DEFUN ("primitive-undo", Fprimitive_undo, Sprimitive_undo, 2, 2, 0,
461 doc: /* Undo N records from the front of the list LIST.
462 Return what remains of the list. */)
463 (n, list)
464 Lisp_Object n, list;
466 struct gcpro gcpro1, gcpro2;
467 Lisp_Object next;
468 int count = SPECPDL_INDEX ();
469 register int arg;
470 Lisp_Object oldlist;
471 int did_apply = 0;
473 #if 0 /* This is a good feature, but would make undo-start
474 unable to do what is expected. */
475 Lisp_Object tem;
477 /* If the head of the list is a boundary, it is the boundary
478 preceding this command. Get rid of it and don't count it. */
479 tem = Fcar (list);
480 if (NILP (tem))
481 list = Fcdr (list);
482 #endif
484 CHECK_NUMBER (n);
485 arg = XINT (n);
486 next = Qnil;
487 GCPRO2 (next, list);
488 /* I don't think we need to gcpro oldlist, as we use it only
489 to check for EQ. ++kfs */
491 /* In a writable buffer, enable undoing read-only text that is so
492 because of text properties. */
493 if (NILP (current_buffer->read_only))
494 specbind (Qinhibit_read_only, Qt);
496 /* Don't let `intangible' properties interfere with undo. */
497 specbind (Qinhibit_point_motion_hooks, Qt);
499 oldlist = current_buffer->undo_list;
501 while (arg > 0)
503 while (CONSP (list))
505 next = XCAR (list);
506 list = XCDR (list);
507 /* Exit inner loop at undo boundary. */
508 if (NILP (next))
509 break;
510 /* Handle an integer by setting point to that value. */
511 if (INTEGERP (next))
512 SET_PT (clip_to_bounds (BEGV, XINT (next), ZV));
513 else if (CONSP (next))
515 Lisp_Object car, cdr;
517 car = XCAR (next);
518 cdr = XCDR (next);
519 if (EQ (car, Qt))
521 /* Element (t high . low) records previous modtime. */
522 Lisp_Object high, low;
523 int mod_time;
524 struct buffer *base_buffer = current_buffer;
526 high = Fcar (cdr);
527 low = Fcdr (cdr);
528 mod_time = (XFASTINT (high) << 16) + XFASTINT (low);
530 if (current_buffer->base_buffer)
531 base_buffer = current_buffer->base_buffer;
533 /* If this records an obsolete save
534 (not matching the actual disk file)
535 then don't mark unmodified. */
536 if (mod_time != base_buffer->modtime)
537 continue;
538 #ifdef CLASH_DETECTION
539 Funlock_buffer ();
540 #endif /* CLASH_DETECTION */
541 Fset_buffer_modified_p (Qnil);
543 else if (EQ (car, Qnil))
545 /* Element (nil PROP VAL BEG . END) is property change. */
546 Lisp_Object beg, end, prop, val;
548 prop = Fcar (cdr);
549 cdr = Fcdr (cdr);
550 val = Fcar (cdr);
551 cdr = Fcdr (cdr);
552 beg = Fcar (cdr);
553 end = Fcdr (cdr);
555 if (XINT (beg) < BEGV || XINT (end) > ZV)
556 error ("Changes to be undone are outside visible portion of buffer");
557 Fput_text_property (beg, end, prop, val, Qnil);
559 else if (INTEGERP (car) && INTEGERP (cdr))
561 /* Element (BEG . END) means range was inserted. */
563 if (XINT (car) < BEGV
564 || XINT (cdr) > ZV)
565 error ("Changes to be undone are outside visible portion of buffer");
566 /* Set point first thing, so that undoing this undo
567 does not send point back to where it is now. */
568 Fgoto_char (car);
569 Fdelete_region (car, cdr);
571 else if (EQ (car, Qapply))
573 /* Element (apply FUN . ARGS) means call FUN to undo. */
574 struct buffer *save_buffer = current_buffer;
576 car = Fcar (cdr);
577 cdr = Fcdr (cdr);
578 if (INTEGERP (car))
580 /* Long format: (apply DELTA START END FUN . ARGS). */
581 Lisp_Object delta = car;
582 Lisp_Object start = Fcar (cdr);
583 Lisp_Object end = Fcar (Fcdr (cdr));
584 Lisp_Object start_mark = Fcopy_marker (start, Qnil);
585 Lisp_Object end_mark = Fcopy_marker (end, Qt);
587 cdr = Fcdr (Fcdr (cdr));
588 apply1 (Fcar (cdr), Fcdr (cdr));
590 /* Check that the function did what the entry said it
591 would do. */
592 if (!EQ (start, Fmarker_position (start_mark))
593 || (XINT (delta) + XINT (end)
594 != marker_position (end_mark)))
595 error ("Changes to be undone by function different than announced");
596 Fset_marker (start_mark, Qnil, Qnil);
597 Fset_marker (end_mark, Qnil, Qnil);
599 else
600 apply1 (car, cdr);
602 if (save_buffer != current_buffer)
603 error ("Undo function switched buffer");
604 did_apply = 1;
606 else if (STRINGP (car) && INTEGERP (cdr))
608 /* Element (STRING . POS) means STRING was deleted. */
609 Lisp_Object membuf;
610 int pos = XINT (cdr);
612 membuf = car;
613 if (pos < 0)
615 if (-pos < BEGV || -pos > ZV)
616 error ("Changes to be undone are outside visible portion of buffer");
617 SET_PT (-pos);
618 Finsert (1, &membuf);
620 else
622 if (pos < BEGV || pos > ZV)
623 error ("Changes to be undone are outside visible portion of buffer");
624 SET_PT (pos);
626 /* Now that we record marker adjustments
627 (caused by deletion) for undo,
628 we should always insert after markers,
629 so that undoing the marker adjustments
630 put the markers back in the right place. */
631 Finsert (1, &membuf);
632 SET_PT (pos);
635 else if (MARKERP (car) && INTEGERP (cdr))
637 /* (MARKER . INTEGER) means a marker MARKER
638 was adjusted by INTEGER. */
639 if (XMARKER (car)->buffer)
640 Fset_marker (car,
641 make_number (marker_position (car) - XINT (cdr)),
642 Fmarker_buffer (car));
646 arg--;
650 /* Make sure an apply entry produces at least one undo entry,
651 so the test in `undo' for continuing an undo series
652 will work right. */
653 if (did_apply
654 && EQ (oldlist, current_buffer->undo_list))
655 current_buffer->undo_list
656 = Fcons (list3 (Qapply, Qcdr, Qnil), current_buffer->undo_list);
658 UNGCPRO;
659 return unbind_to (count, list);
662 void
663 syms_of_undo ()
665 Qinhibit_read_only = intern ("inhibit-read-only");
666 staticpro (&Qinhibit_read_only);
668 Qapply = intern ("apply");
669 staticpro (&Qapply);
671 pending_boundary = Qnil;
672 staticpro (&pending_boundary);
674 last_undo_buffer = NULL;
675 last_boundary_buffer = NULL;
677 defsubr (&Sprimitive_undo);
678 defsubr (&Sundo_boundary);
680 DEFVAR_INT ("undo-limit", &undo_limit,
681 doc: /* Keep no more undo information once it exceeds this size.
682 This limit is applied when garbage collection happens.
683 When a previous command increases the total undo list size past this
684 value, the earlier commands that came before it are forgotten.
686 The size is counted as the number of bytes occupied,
687 which includes both saved text and other data. */);
688 undo_limit = 20000;
690 DEFVAR_INT ("undo-strong-limit", &undo_strong_limit,
691 doc: /* Don't keep more than this much size of undo information.
692 This limit is applied when garbage collection happens.
693 When a previous command increases the total undo list size past this
694 value, that command and the earlier commands that came before it are forgotten.
695 However, the most recent buffer-modifying command's undo info
696 is never discarded for this reason.
698 The size is counted as the number of bytes occupied,
699 which includes both saved text and other data. */);
700 undo_strong_limit = 30000;
702 DEFVAR_LISP ("undo-outer-limit", &Vundo_outer_limit,
703 doc: /* Outer limit on size of undo information for one command.
704 At garbage collection time, if the current command has produced
705 more than this much undo information, it discards the info and displays
706 a warning. This is a last-ditch limit to prevent memory overflow.
708 The size is counted as the number of bytes occupied, which includes
709 both saved text and other data. A value of nil means no limit. In
710 this case, accumulating one huge undo entry could make Emacs crash as
711 a result of memory overflow.
713 In fact, this calls the function which is the value of
714 `undo-outer-limit-function' with one argument, the size.
715 The text above describes the behavior of the function
716 that variable usually specifies. */);
717 Vundo_outer_limit = make_number (3000000);
719 DEFVAR_LISP ("undo-outer-limit-function", &Vundo_outer_limit_function,
720 doc: /* Function to call when an undo list exceeds `undo-outer-limit'.
721 This function is called with one argument, the current undo list size
722 for the most recent command (since the last undo boundary).
723 If the function returns t, that means truncation has been fully handled.
724 If it returns nil, the other forms of truncation are done.
726 Garbage collection is inhibited around the call to this function,
727 so it must make sure not to do a lot of consing. */);
728 Vundo_outer_limit_function = Qnil;
730 DEFVAR_BOOL ("undo-inhibit-record-point", &undo_inhibit_record_point,
731 doc: /* Non-nil means do not record `point' in `buffer-undo-list'. */);
732 undo_inhibit_record_point = 0;
735 /* arch-tag: d546ee01-4aed-4ffb-bb8b-eefaae50d38a
736 (do not change this comment) */