(calc-embedded-mode-vars): Rename `calc-complement-signed-mode' to
[emacs.git] / src / undo.c
blob8dcd393444cb5bfb9f5427477239800b730f15b3
1 /* undo handling for GNU Emacs.
2 Copyright (C) 1990, 1993, 1994, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008, 2009 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 of the License, or
10 (at your option) 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. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
22 #include <setjmp.h>
23 #include "lisp.h"
24 #include "buffer.h"
25 #include "commands.h"
26 #include "window.h"
28 /* Limits controlling how much undo information to keep. */
30 EMACS_INT undo_limit;
31 EMACS_INT undo_strong_limit;
33 Lisp_Object Vundo_outer_limit;
35 /* Function to call when undo_outer_limit is exceeded. */
37 Lisp_Object Vundo_outer_limit_function;
39 /* Last buffer for which undo information was recorded. */
40 /* BEWARE: This is not traced by the GC, so never dereference it! */
41 struct buffer *last_undo_buffer;
43 /* Position of point last time we inserted a boundary. */
44 struct buffer *last_boundary_buffer;
45 EMACS_INT last_boundary_position;
47 Lisp_Object Qinhibit_read_only;
49 /* Marker for function call undo list elements. */
51 Lisp_Object Qapply;
53 /* The first time a command records something for undo.
54 it also allocates the undo-boundary object
55 which will be added to the list at the end of the command.
56 This ensures we can't run out of space while trying to make
57 an undo-boundary. */
58 Lisp_Object pending_boundary;
60 /* Nonzero means do not record point in record_point. */
62 int undo_inhibit_record_point;
64 /* Record point as it was at beginning of this command (if necessary)
65 and prepare the undo info for recording a change.
66 PT is the position of point that will naturally occur as a result of the
67 undo record that will be added just after this command terminates. */
69 static void
70 record_point (pt)
71 int pt;
73 int at_boundary;
75 /* Don't record position of pt when undo_inhibit_record_point holds. */
76 if (undo_inhibit_record_point)
77 return;
79 /* Allocate a cons cell to be the undo boundary after this command. */
80 if (NILP (pending_boundary))
81 pending_boundary = Fcons (Qnil, Qnil);
83 if ((current_buffer != last_undo_buffer)
84 /* Don't call Fundo_boundary for the first change. Otherwise we
85 risk overwriting last_boundary_position in Fundo_boundary with
86 PT of the current buffer and as a consequence not insert an
87 undo boundary because last_boundary_position will equal pt in
88 the test at the end of the present function (Bug#731). */
89 && (MODIFF > SAVE_MODIFF))
90 Fundo_boundary ();
91 last_undo_buffer = current_buffer;
93 if (CONSP (current_buffer->undo_list))
95 /* Set AT_BOUNDARY to 1 only when we have nothing other than
96 marker adjustment before undo boundary. */
98 Lisp_Object tail = current_buffer->undo_list, elt;
100 while (1)
102 if (NILP (tail))
103 elt = Qnil;
104 else
105 elt = XCAR (tail);
106 if (NILP (elt) || ! (CONSP (elt) && MARKERP (XCAR (elt))))
107 break;
108 tail = XCDR (tail);
110 at_boundary = NILP (elt);
112 else
113 at_boundary = 1;
115 if (MODIFF <= SAVE_MODIFF)
116 record_first_change ();
118 /* If we are just after an undo boundary, and
119 point wasn't at start of deleted range, record where it was. */
120 if (at_boundary
121 && current_buffer == last_boundary_buffer
122 && last_boundary_position != pt)
123 current_buffer->undo_list
124 = Fcons (make_number (last_boundary_position), current_buffer->undo_list);
127 /* Record an insertion that just happened or is about to happen,
128 for LENGTH characters at position BEG.
129 (It is possible to record an insertion before or after the fact
130 because we don't need to record the contents.) */
132 void
133 record_insert (beg, length)
134 int beg, length;
136 Lisp_Object lbeg, lend;
138 if (EQ (current_buffer->undo_list, Qt))
139 return;
141 record_point (beg);
143 /* If this is following another insertion and consecutive with it
144 in the buffer, combine the two. */
145 if (CONSP (current_buffer->undo_list))
147 Lisp_Object elt;
148 elt = XCAR (current_buffer->undo_list);
149 if (CONSP (elt)
150 && INTEGERP (XCAR (elt))
151 && INTEGERP (XCDR (elt))
152 && XINT (XCDR (elt)) == beg)
154 XSETCDR (elt, make_number (beg + length));
155 return;
159 XSETFASTINT (lbeg, beg);
160 XSETINT (lend, beg + length);
161 current_buffer->undo_list = Fcons (Fcons (lbeg, lend),
162 current_buffer->undo_list);
165 /* Record that a deletion is about to take place,
166 of the characters in STRING, at location BEG. */
168 void
169 record_delete (beg, string)
170 int beg;
171 Lisp_Object string;
173 Lisp_Object sbeg;
175 if (EQ (current_buffer->undo_list, Qt))
176 return;
178 if (PT == beg + SCHARS (string))
180 XSETINT (sbeg, -beg);
181 record_point (PT);
183 else
185 XSETFASTINT (sbeg, beg);
186 record_point (beg);
189 current_buffer->undo_list
190 = Fcons (Fcons (string, sbeg), current_buffer->undo_list);
193 /* Record the fact that MARKER is about to be adjusted by ADJUSTMENT.
194 This is done only when a marker points within text being deleted,
195 because that's the only case where an automatic marker adjustment
196 won't be inverted automatically by undoing the buffer modification. */
198 void
199 record_marker_adjustment (marker, adjustment)
200 Lisp_Object marker;
201 int adjustment;
203 if (EQ (current_buffer->undo_list, Qt))
204 return;
206 /* Allocate a cons cell to be the undo boundary after this command. */
207 if (NILP (pending_boundary))
208 pending_boundary = Fcons (Qnil, Qnil);
210 if (current_buffer != last_undo_buffer)
211 Fundo_boundary ();
212 last_undo_buffer = current_buffer;
214 current_buffer->undo_list
215 = Fcons (Fcons (marker, make_number (adjustment)),
216 current_buffer->undo_list);
219 /* Record that a replacement is about to take place,
220 for LENGTH characters at location BEG.
221 The replacement must not change the number of characters. */
223 void
224 record_change (beg, length)
225 int beg, length;
227 record_delete (beg, make_buffer_string (beg, beg + length, 1));
228 record_insert (beg, length);
231 /* Record that an unmodified buffer is about to be changed.
232 Record the file modification date so that when undoing this entry
233 we can tell whether it is obsolete because the file was saved again. */
235 void
236 record_first_change ()
238 Lisp_Object high, low;
239 struct buffer *base_buffer = current_buffer;
241 if (EQ (current_buffer->undo_list, Qt))
242 return;
244 if (current_buffer != last_undo_buffer)
245 Fundo_boundary ();
246 last_undo_buffer = current_buffer;
248 if (base_buffer->base_buffer)
249 base_buffer = base_buffer->base_buffer;
251 XSETFASTINT (high, (base_buffer->modtime >> 16) & 0xffff);
252 XSETFASTINT (low, base_buffer->modtime & 0xffff);
253 current_buffer->undo_list = Fcons (Fcons (Qt, Fcons (high, low)), current_buffer->undo_list);
256 /* Record a change in property PROP (whose old value was VAL)
257 for LENGTH characters starting at position BEG in BUFFER. */
259 void
260 record_property_change (beg, length, prop, value, buffer)
261 int beg, length;
262 Lisp_Object prop, value, buffer;
264 Lisp_Object lbeg, lend, entry;
265 struct buffer *obuf = current_buffer, *buf = XBUFFER (buffer);
266 int boundary = 0;
268 if (EQ (buf->undo_list, Qt))
269 return;
271 /* Allocate a cons cell to be the undo boundary after this command. */
272 if (NILP (pending_boundary))
273 pending_boundary = Fcons (Qnil, Qnil);
275 if (buf != last_undo_buffer)
276 boundary = 1;
277 last_undo_buffer = buf;
279 /* Switch temporarily to the buffer that was changed. */
280 current_buffer = buf;
282 if (boundary)
283 Fundo_boundary ();
285 if (MODIFF <= SAVE_MODIFF)
286 record_first_change ();
288 XSETINT (lbeg, beg);
289 XSETINT (lend, beg + length);
290 entry = Fcons (Qnil, Fcons (prop, Fcons (value, Fcons (lbeg, lend))));
291 current_buffer->undo_list = Fcons (entry, current_buffer->undo_list);
293 current_buffer = obuf;
296 DEFUN ("undo-boundary", Fundo_boundary, Sundo_boundary, 0, 0, 0,
297 doc: /* Mark a boundary between units of undo.
298 An undo command will stop at this point,
299 but another undo command will undo to the previous boundary. */)
302 Lisp_Object tem;
303 if (EQ (current_buffer->undo_list, Qt))
304 return Qnil;
305 tem = Fcar (current_buffer->undo_list);
306 if (!NILP (tem))
308 /* One way or another, cons nil onto the front of the undo list. */
309 if (!NILP (pending_boundary))
311 /* If we have preallocated the cons cell to use here,
312 use that one. */
313 XSETCDR (pending_boundary, current_buffer->undo_list);
314 current_buffer->undo_list = pending_boundary;
315 pending_boundary = Qnil;
317 else
318 current_buffer->undo_list = Fcons (Qnil, current_buffer->undo_list);
320 last_boundary_position = PT;
321 last_boundary_buffer = current_buffer;
322 return Qnil;
325 /* At garbage collection time, make an undo list shorter at the end,
326 returning the truncated list. How this is done depends on the
327 variables undo-limit, undo-strong-limit and undo-outer-limit.
328 In some cases this works by calling undo-outer-limit-function. */
330 void
331 truncate_undo_list (b)
332 struct buffer *b;
334 Lisp_Object list;
335 Lisp_Object prev, next, last_boundary;
336 int size_so_far = 0;
338 /* Make sure that calling undo-outer-limit-function
339 won't cause another GC. */
340 int count = inhibit_garbage_collection ();
342 /* Make the buffer current to get its local values of variables such
343 as undo_limit. Also so that Vundo_outer_limit_function can
344 tell which buffer to operate on. */
345 record_unwind_protect (set_buffer_if_live, Fcurrent_buffer ());
346 set_buffer_internal (b);
348 list = b->undo_list;
350 prev = Qnil;
351 next = list;
352 last_boundary = Qnil;
354 /* If the first element is an undo boundary, skip past it. */
355 if (CONSP (next) && NILP (XCAR (next)))
357 /* Add in the space occupied by this element and its chain link. */
358 size_so_far += sizeof (struct Lisp_Cons);
360 /* Advance to next element. */
361 prev = next;
362 next = XCDR (next);
365 /* Always preserve at least the most recent undo record
366 unless it is really horribly big.
368 Skip, skip, skip the undo, skip, skip, skip the undo,
369 Skip, skip, skip the undo, skip to the undo bound'ry. */
371 while (CONSP (next) && ! NILP (XCAR (next)))
373 Lisp_Object elt;
374 elt = XCAR (next);
376 /* Add in the space occupied by this element and its chain link. */
377 size_so_far += sizeof (struct Lisp_Cons);
378 if (CONSP (elt))
380 size_so_far += sizeof (struct Lisp_Cons);
381 if (STRINGP (XCAR (elt)))
382 size_so_far += (sizeof (struct Lisp_String) - 1
383 + SCHARS (XCAR (elt)));
386 /* Advance to next element. */
387 prev = next;
388 next = XCDR (next);
391 /* If by the first boundary we have already passed undo_outer_limit,
392 we're heading for memory full, so offer to clear out the list. */
393 if (INTEGERP (Vundo_outer_limit)
394 && size_so_far > XINT (Vundo_outer_limit)
395 && !NILP (Vundo_outer_limit_function))
397 Lisp_Object tem;
398 struct buffer *temp = last_undo_buffer;
400 /* Normally the function this calls is undo-outer-limit-truncate. */
401 tem = call1 (Vundo_outer_limit_function, make_number (size_so_far));
402 if (! NILP (tem))
404 /* The function is responsible for making
405 any desired changes in buffer-undo-list. */
406 unbind_to (count, Qnil);
407 return;
409 /* That function probably used the minibuffer, and if so, that
410 changed last_undo_buffer. Change it back so that we don't
411 force next change to make an undo boundary here. */
412 last_undo_buffer = temp;
415 if (CONSP (next))
416 last_boundary = prev;
418 /* Keep additional undo data, if it fits in the limits. */
419 while (CONSP (next))
421 Lisp_Object elt;
422 elt = XCAR (next);
424 /* When we get to a boundary, decide whether to truncate
425 either before or after it. The lower threshold, undo_limit,
426 tells us to truncate after it. If its size pushes past
427 the higher threshold undo_strong_limit, we truncate before it. */
428 if (NILP (elt))
430 if (size_so_far > undo_strong_limit)
431 break;
432 last_boundary = prev;
433 if (size_so_far > undo_limit)
434 break;
437 /* Add in the space occupied by this element and its chain link. */
438 size_so_far += sizeof (struct Lisp_Cons);
439 if (CONSP (elt))
441 size_so_far += sizeof (struct Lisp_Cons);
442 if (STRINGP (XCAR (elt)))
443 size_so_far += (sizeof (struct Lisp_String) - 1
444 + SCHARS (XCAR (elt)));
447 /* Advance to next element. */
448 prev = next;
449 next = XCDR (next);
452 /* If we scanned the whole list, it is short enough; don't change it. */
453 if (NILP (next))
455 /* Truncate at the boundary where we decided to truncate. */
456 else if (!NILP (last_boundary))
457 XSETCDR (last_boundary, Qnil);
458 /* There's nothing we decided to keep, so clear it out. */
459 else
460 b->undo_list = Qnil;
462 unbind_to (count, Qnil);
465 DEFUN ("primitive-undo", Fprimitive_undo, Sprimitive_undo, 2, 2, 0,
466 doc: /* Undo N records from the front of the list LIST.
467 Return what remains of the list. */)
468 (n, list)
469 Lisp_Object n, list;
471 struct gcpro gcpro1, gcpro2;
472 Lisp_Object next;
473 int count = SPECPDL_INDEX ();
474 register int arg;
475 Lisp_Object oldlist;
476 int did_apply = 0;
478 #if 0 /* This is a good feature, but would make undo-start
479 unable to do what is expected. */
480 Lisp_Object tem;
482 /* If the head of the list is a boundary, it is the boundary
483 preceding this command. Get rid of it and don't count it. */
484 tem = Fcar (list);
485 if (NILP (tem))
486 list = Fcdr (list);
487 #endif
489 CHECK_NUMBER (n);
490 arg = XINT (n);
491 next = Qnil;
492 GCPRO2 (next, list);
493 /* I don't think we need to gcpro oldlist, as we use it only
494 to check for EQ. ++kfs */
496 /* In a writable buffer, enable undoing read-only text that is so
497 because of text properties. */
498 if (NILP (current_buffer->read_only))
499 specbind (Qinhibit_read_only, Qt);
501 /* Don't let `intangible' properties interfere with undo. */
502 specbind (Qinhibit_point_motion_hooks, Qt);
504 oldlist = current_buffer->undo_list;
506 while (arg > 0)
508 while (CONSP (list))
510 next = XCAR (list);
511 list = XCDR (list);
512 /* Exit inner loop at undo boundary. */
513 if (NILP (next))
514 break;
515 /* Handle an integer by setting point to that value. */
516 if (INTEGERP (next))
517 SET_PT (clip_to_bounds (BEGV, XINT (next), ZV));
518 else if (CONSP (next))
520 Lisp_Object car, cdr;
522 car = XCAR (next);
523 cdr = XCDR (next);
524 if (EQ (car, Qt))
526 /* Element (t high . low) records previous modtime. */
527 Lisp_Object high, low;
528 int mod_time;
529 struct buffer *base_buffer = current_buffer;
531 high = Fcar (cdr);
532 low = Fcdr (cdr);
533 mod_time = (XFASTINT (high) << 16) + XFASTINT (low);
535 if (current_buffer->base_buffer)
536 base_buffer = current_buffer->base_buffer;
538 /* If this records an obsolete save
539 (not matching the actual disk file)
540 then don't mark unmodified. */
541 if (mod_time != base_buffer->modtime)
542 continue;
543 #ifdef CLASH_DETECTION
544 Funlock_buffer ();
545 #endif /* CLASH_DETECTION */
546 Fset_buffer_modified_p (Qnil);
548 else if (EQ (car, Qnil))
550 /* Element (nil PROP VAL BEG . END) is property change. */
551 Lisp_Object beg, end, prop, val;
553 prop = Fcar (cdr);
554 cdr = Fcdr (cdr);
555 val = Fcar (cdr);
556 cdr = Fcdr (cdr);
557 beg = Fcar (cdr);
558 end = Fcdr (cdr);
560 if (XINT (beg) < BEGV || XINT (end) > ZV)
561 error ("Changes to be undone are outside visible portion of buffer");
562 Fput_text_property (beg, end, prop, val, Qnil);
564 else if (INTEGERP (car) && INTEGERP (cdr))
566 /* Element (BEG . END) means range was inserted. */
568 if (XINT (car) < BEGV
569 || XINT (cdr) > ZV)
570 error ("Changes to be undone are outside visible portion of buffer");
571 /* Set point first thing, so that undoing this undo
572 does not send point back to where it is now. */
573 Fgoto_char (car);
574 Fdelete_region (car, cdr);
576 else if (EQ (car, Qapply))
578 /* Element (apply FUN . ARGS) means call FUN to undo. */
579 struct buffer *save_buffer = current_buffer;
581 car = Fcar (cdr);
582 cdr = Fcdr (cdr);
583 if (INTEGERP (car))
585 /* Long format: (apply DELTA START END FUN . ARGS). */
586 Lisp_Object delta = car;
587 Lisp_Object start = Fcar (cdr);
588 Lisp_Object end = Fcar (Fcdr (cdr));
589 Lisp_Object start_mark = Fcopy_marker (start, Qnil);
590 Lisp_Object end_mark = Fcopy_marker (end, Qt);
592 cdr = Fcdr (Fcdr (cdr));
593 apply1 (Fcar (cdr), Fcdr (cdr));
595 /* Check that the function did what the entry said it
596 would do. */
597 if (!EQ (start, Fmarker_position (start_mark))
598 || (XINT (delta) + XINT (end)
599 != marker_position (end_mark)))
600 error ("Changes to be undone by function different than announced");
601 Fset_marker (start_mark, Qnil, Qnil);
602 Fset_marker (end_mark, Qnil, Qnil);
604 else
605 apply1 (car, cdr);
607 if (save_buffer != current_buffer)
608 error ("Undo function switched buffer");
609 did_apply = 1;
611 else if (STRINGP (car) && INTEGERP (cdr))
613 /* Element (STRING . POS) means STRING was deleted. */
614 Lisp_Object membuf;
615 int pos = XINT (cdr);
617 membuf = car;
618 if (pos < 0)
620 if (-pos < BEGV || -pos > ZV)
621 error ("Changes to be undone are outside visible portion of buffer");
622 SET_PT (-pos);
623 Finsert (1, &membuf);
625 else
627 if (pos < BEGV || pos > ZV)
628 error ("Changes to be undone are outside visible portion of buffer");
629 SET_PT (pos);
631 /* Now that we record marker adjustments
632 (caused by deletion) for undo,
633 we should always insert after markers,
634 so that undoing the marker adjustments
635 put the markers back in the right place. */
636 Finsert (1, &membuf);
637 SET_PT (pos);
640 else if (MARKERP (car) && INTEGERP (cdr))
642 /* (MARKER . INTEGER) means a marker MARKER
643 was adjusted by INTEGER. */
644 if (XMARKER (car)->buffer)
645 Fset_marker (car,
646 make_number (marker_position (car) - XINT (cdr)),
647 Fmarker_buffer (car));
651 arg--;
655 /* Make sure an apply entry produces at least one undo entry,
656 so the test in `undo' for continuing an undo series
657 will work right. */
658 if (did_apply
659 && EQ (oldlist, current_buffer->undo_list))
660 current_buffer->undo_list
661 = Fcons (list3 (Qapply, Qcdr, Qnil), current_buffer->undo_list);
663 UNGCPRO;
664 return unbind_to (count, list);
667 void
668 syms_of_undo ()
670 Qinhibit_read_only = intern_c_string ("inhibit-read-only");
671 staticpro (&Qinhibit_read_only);
673 Qapply = intern_c_string ("apply");
674 staticpro (&Qapply);
676 pending_boundary = Qnil;
677 staticpro (&pending_boundary);
679 last_undo_buffer = NULL;
680 last_boundary_buffer = NULL;
682 defsubr (&Sprimitive_undo);
683 defsubr (&Sundo_boundary);
685 DEFVAR_INT ("undo-limit", &undo_limit,
686 doc: /* Keep no more undo information once it exceeds this size.
687 This limit is applied when garbage collection happens.
688 When a previous command increases the total undo list size past this
689 value, the earlier commands that came before it are forgotten.
691 The size is counted as the number of bytes occupied,
692 which includes both saved text and other data. */);
693 undo_limit = 80000;
695 DEFVAR_INT ("undo-strong-limit", &undo_strong_limit,
696 doc: /* Don't keep more than this much size of undo information.
697 This limit is applied when garbage collection happens.
698 When a previous command increases the total undo list size past this
699 value, that command and the earlier commands that came before it are forgotten.
700 However, the most recent buffer-modifying command's undo info
701 is never discarded for this reason.
703 The size is counted as the number of bytes occupied,
704 which includes both saved text and other data. */);
705 undo_strong_limit = 120000;
707 DEFVAR_LISP ("undo-outer-limit", &Vundo_outer_limit,
708 doc: /* Outer limit on size of undo information for one command.
709 At garbage collection time, if the current command has produced
710 more than this much undo information, it discards the info and displays
711 a warning. This is a last-ditch limit to prevent memory overflow.
713 The size is counted as the number of bytes occupied, which includes
714 both saved text and other data. A value of nil means no limit. In
715 this case, accumulating one huge undo entry could make Emacs crash as
716 a result of memory overflow.
718 In fact, this calls the function which is the value of
719 `undo-outer-limit-function' with one argument, the size.
720 The text above describes the behavior of the function
721 that variable usually specifies. */);
722 Vundo_outer_limit = make_number (12000000);
724 DEFVAR_LISP ("undo-outer-limit-function", &Vundo_outer_limit_function,
725 doc: /* Function to call when an undo list exceeds `undo-outer-limit'.
726 This function is called with one argument, the current undo list size
727 for the most recent command (since the last undo boundary).
728 If the function returns t, that means truncation has been fully handled.
729 If it returns nil, the other forms of truncation are done.
731 Garbage collection is inhibited around the call to this function,
732 so it must make sure not to do a lot of consing. */);
733 Vundo_outer_limit_function = Qnil;
735 DEFVAR_BOOL ("undo-inhibit-record-point", &undo_inhibit_record_point,
736 doc: /* Non-nil means do not record `point' in `buffer-undo-list'. */);
737 undo_inhibit_record_point = 0;
740 /* arch-tag: d546ee01-4aed-4ffb-bb8b-eefaae50d38a
741 (do not change this comment) */