1 /* undo handling for GNU Emacs.
2 Copyright (C) 1990, 1993, 1994, 2000 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
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)
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.
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. */
27 /* Limits controlling how much undo information to keep. */
30 EMACS_INT undo_strong_limit
;
32 Lisp_Object Vundo_outer_limit
;
34 /* Function to call when undo_outer_limit is exceeded. */
36 Lisp_Object Vundo_outer_limit_function
;
38 /* Last buffer for which undo information was recorded. */
39 Lisp_Object last_undo_buffer
;
41 Lisp_Object Qinhibit_read_only
;
43 /* Marker for function call undo list elements. */
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
52 Lisp_Object pending_boundary
;
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. */
65 /* Allocate a cons cell to be the undo boundary after this command. */
66 if (NILP (pending_boundary
))
67 pending_boundary
= Fcons (Qnil
, Qnil
);
69 if (!BUFFERP (last_undo_buffer
)
70 || current_buffer
!= XBUFFER (last_undo_buffer
))
72 XSETBUFFER (last_undo_buffer
, current_buffer
);
74 if (CONSP (current_buffer
->undo_list
))
76 /* Set AT_BOUNDARY to 1 only when we have nothing other than
77 marker adjustment before undo boundary. */
79 Lisp_Object tail
= current_buffer
->undo_list
, elt
;
87 if (NILP (elt
) || ! (CONSP (elt
) && MARKERP (XCAR (elt
))))
91 at_boundary
= NILP (elt
);
96 if (MODIFF
<= SAVE_MODIFF
)
97 record_first_change ();
99 /* If we are just after an undo boundary, and
100 point wasn't at start of deleted range, record where it was. */
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
);
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.) */
116 record_insert (beg
, length
)
119 Lisp_Object lbeg
, lend
;
121 if (EQ (current_buffer
->undo_list
, Qt
))
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
))
131 elt
= XCAR (current_buffer
->undo_list
);
133 && INTEGERP (XCAR (elt
))
134 && INTEGERP (XCDR (elt
))
135 && XINT (XCDR (elt
)) == beg
)
137 XSETCDR (elt
, make_number (beg
+ length
));
142 XSETFASTINT (lbeg
, beg
);
143 XSETINT (lend
, beg
+ length
);
144 current_buffer
->undo_list
= Fcons (Fcons (lbeg
, lend
),
145 current_buffer
->undo_list
);
148 /* Record that a deletion is about to take place,
149 of the characters in STRING, at location BEG. */
152 record_delete (beg
, string
)
158 if (EQ (current_buffer
->undo_list
, Qt
))
161 if (PT
== beg
+ SCHARS (string
))
163 XSETINT (sbeg
, -beg
);
168 XSETFASTINT (sbeg
, beg
);
172 current_buffer
->undo_list
173 = Fcons (Fcons (string
, sbeg
), current_buffer
->undo_list
);
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. */
182 record_marker_adjustment (marker
, adjustment
)
186 if (EQ (current_buffer
->undo_list
, Qt
))
189 /* Allocate a cons cell to be the undo boundary after this command. */
190 if (NILP (pending_boundary
))
191 pending_boundary
= Fcons (Qnil
, Qnil
);
193 if (!BUFFERP (last_undo_buffer
)
194 || current_buffer
!= XBUFFER (last_undo_buffer
))
196 XSETBUFFER (last_undo_buffer
, current_buffer
);
198 current_buffer
->undo_list
199 = Fcons (Fcons (marker
, make_number (adjustment
)),
200 current_buffer
->undo_list
);
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. */
208 record_change (beg
, length
)
211 record_delete (beg
, make_buffer_string (beg
, beg
+ length
, 1));
212 record_insert (beg
, length
);
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. */
220 record_first_change ()
222 Lisp_Object high
, low
;
223 struct buffer
*base_buffer
= current_buffer
;
225 if (EQ (current_buffer
->undo_list
, Qt
))
228 if (!BUFFERP (last_undo_buffer
)
229 || current_buffer
!= XBUFFER (last_undo_buffer
))
231 XSETBUFFER (last_undo_buffer
, current_buffer
);
233 if (base_buffer
->base_buffer
)
234 base_buffer
= base_buffer
->base_buffer
;
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
);
241 /* Record a change in property PROP (whose old value was VAL)
242 for LENGTH characters starting at position BEG in BUFFER. */
245 record_property_change (beg
, length
, prop
, value
, buffer
)
247 Lisp_Object prop
, value
, buffer
;
249 Lisp_Object lbeg
, lend
, entry
;
250 struct buffer
*obuf
= current_buffer
;
253 if (EQ (XBUFFER (buffer
)->undo_list
, Qt
))
256 /* Allocate a cons cell to be the undo boundary after this command. */
257 if (NILP (pending_boundary
))
258 pending_boundary
= Fcons (Qnil
, Qnil
);
260 if (!EQ (buffer
, last_undo_buffer
))
262 last_undo_buffer
= buffer
;
264 /* Switch temporarily to the buffer that was changed. */
265 current_buffer
= XBUFFER (buffer
);
270 if (MODIFF
<= SAVE_MODIFF
)
271 record_first_change ();
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
);
278 current_buffer
= obuf
;
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. */)
288 if (EQ (current_buffer
->undo_list
, Qt
))
290 tem
= Fcar (current_buffer
->undo_list
);
293 /* One way or another, cons nil onto the front of the undo list. */
294 if (!NILP (pending_boundary
))
296 /* If we have preallocated the cons cell to use here,
298 XSETCDR (pending_boundary
, current_buffer
->undo_list
);
299 current_buffer
->undo_list
= pending_boundary
;
300 pending_boundary
= Qnil
;
303 current_buffer
->undo_list
= Fcons (Qnil
, current_buffer
->undo_list
);
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. */
314 truncate_undo_list (b
)
318 Lisp_Object prev
, next
, last_boundary
;
321 /* Make sure that calling undo-outer-limit-function
322 won't cause another GC. */
323 int count
= inhibit_garbage_collection ();
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
);
335 last_boundary
= Qnil
;
337 /* If the first element is an undo boundary, skip past it. */
338 if (CONSP (next
) && NILP (XCAR (next
)))
340 /* Add in the space occupied by this element and its chain link. */
341 size_so_far
+= sizeof (struct Lisp_Cons
);
343 /* Advance to next element. */
348 /* Always preserve at least the most recent undo record
349 unless it is really horribly big.
351 Skip, skip, skip the undo, skip, skip, skip the undo,
352 Skip, skip, skip the undo, skip to the undo bound'ry. */
354 while (CONSP (next
) && ! NILP (XCAR (next
)))
359 /* Add in the space occupied by this element and its chain link. */
360 size_so_far
+= sizeof (struct Lisp_Cons
);
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
)));
369 /* Advance to next element. */
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
))
380 Lisp_Object temp
= last_undo_buffer
;
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
))))
386 /* The function is responsible for making
387 any desired changes in buffer-undo-list. */
388 unbind_to (count
, Qnil
);
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
;
398 last_boundary
= prev
;
400 /* Keep additional undo data, if it fits in the limits. */
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. */
412 if (size_so_far
> undo_strong_limit
)
414 last_boundary
= prev
;
415 if (size_so_far
> undo_limit
)
419 /* Add in the space occupied by this element and its chain link. */
420 size_so_far
+= sizeof (struct Lisp_Cons
);
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
)));
429 /* Advance to next element. */
434 /* If we scanned the whole list, it is short enough; don't change it. */
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. */
444 unbind_to (count
, Qnil
);
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. */)
453 struct gcpro gcpro1
, gcpro2
;
455 int count
= SPECPDL_INDEX ();
458 #if 0 /* This is a good feature, but would make undo-start
459 unable to do what is expected. */
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. */
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
);
479 /* Don't let `intangible' properties interfere with undo. */
480 specbind (Qinhibit_point_motion_hooks
, Qt
);
488 /* Exit inner loop at undo boundary. */
491 /* Handle an integer by setting point to that value. */
493 SET_PT (clip_to_bounds (BEGV
, XINT (next
), ZV
));
494 else if (CONSP (next
))
496 Lisp_Object car
, cdr
;
502 /* Element (t high . low) records previous modtime. */
503 Lisp_Object high
, low
;
505 struct buffer
*base_buffer
= current_buffer
;
509 mod_time
= (XFASTINT (high
) << 16) + XFASTINT (low
);
511 if (current_buffer
->base_buffer
)
512 base_buffer
= current_buffer
->base_buffer
;
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
)
519 #ifdef CLASH_DETECTION
521 #endif /* CLASH_DETECTION */
522 Fset_buffer_modified_p (Qnil
);
524 else if (EQ (car
, Qnil
))
526 /* Element (nil PROP VAL BEG . END) is property change. */
527 Lisp_Object beg
, end
, prop
, val
;
536 Fput_text_property (beg
, end
, prop
, val
, Qnil
);
538 else if (INTEGERP (car
) && INTEGERP (cdr
))
540 /* Element (BEG . END) means range was inserted. */
542 if (XINT (car
) < BEGV
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. */
548 Fdelete_region (car
, cdr
);
550 else if (EQ (car
, Qapply
))
552 Lisp_Object oldlist
= current_buffer
->undo_list
;
553 /* Element (apply FUNNAME . ARGS) means call FUNNAME to undo. */
557 /* Long format: (apply DELTA START END FUNNAME . ARGS). */
558 cdr
= Fcdr (Fcdr (Fcdr (cdr
)));
563 /* Make sure this produces at least one undo entry,
564 so the test in `undo' for continuing an undo series
566 if (EQ (oldlist
, current_buffer
->undo_list
))
567 current_buffer
->undo_list
568 = Fcons (list2 (Qcdr
, Qnil
), current_buffer
->undo_list
);
570 else if (STRINGP (car
) && INTEGERP (cdr
))
572 /* Element (STRING . POS) means STRING was deleted. */
574 int pos
= XINT (cdr
);
579 if (-pos
< BEGV
|| -pos
> ZV
)
580 error ("Changes to be undone are outside visible portion of buffer");
582 Finsert (1, &membuf
);
586 if (pos
< BEGV
|| pos
> ZV
)
587 error ("Changes to be undone are outside visible portion of buffer");
590 /* Now that we record marker adjustments
591 (caused by deletion) for undo,
592 we should always insert after markers,
593 so that undoing the marker adjustments
594 put the markers back in the right place. */
595 Finsert (1, &membuf
);
599 else if (MARKERP (car
) && INTEGERP (cdr
))
601 /* (MARKER . INTEGER) means a marker MARKER
602 was adjusted by INTEGER. */
603 if (XMARKER (car
)->buffer
)
605 make_number (marker_position (car
) - XINT (cdr
)),
606 Fmarker_buffer (car
));
614 return unbind_to (count
, list
);
620 Qinhibit_read_only
= intern ("inhibit-read-only");
621 staticpro (&Qinhibit_read_only
);
623 Qapply
= intern ("apply");
626 pending_boundary
= Qnil
;
627 staticpro (&pending_boundary
);
629 defsubr (&Sprimitive_undo
);
630 defsubr (&Sundo_boundary
);
632 DEFVAR_INT ("undo-limit", &undo_limit
,
633 doc
: /* Keep no more undo information once it exceeds this size.
634 This limit is applied when garbage collection happens.
635 When a previous command increases the total undo list size past this
636 value, the earlier commands that came before it are forgotten.
638 The size is counted as the number of bytes occupied,
639 which includes both saved text and other data. */);
642 DEFVAR_INT ("undo-strong-limit", &undo_strong_limit
,
643 doc
: /* Don't keep more than this much size of undo information.
644 This limit is applied when garbage collection happens.
645 When a previous command increases the total undo list size past this
646 value, that command and the earlier commands that came before it are forgotten.
647 However, the most recent buffer-modifying command's undo info
648 is never discarded for this reason.
650 The size is counted as the number of bytes occupied,
651 which includes both saved text and other data. */);
652 undo_strong_limit
= 30000;
654 DEFVAR_LISP ("undo-outer-limit", &Vundo_outer_limit
,
655 doc
: /* Outer limit on size of undo information for one command.
656 At garbage collection time, if the current command has produced
657 more than this much undo information, it discards the info and displays
658 a warning. This is a last-ditch limit to prevent memory overflow.
660 The size is counted as the number of bytes occupied, which includes
661 both saved text and other data. A value of nil means no limit. In
662 this case, accumulating one huge undo entry could make Emacs crash as
663 a result of memory overflow.
665 In fact, this calls the function which is the value of
666 `undo-outer-limit-function' with one argument, the size.
667 The text above describes the behavior of the function
668 that variable usually specifies. */);
669 Vundo_outer_limit
= make_number (3000000);
671 DEFVAR_LISP ("undo-outer-limit-function", &Vundo_outer_limit_function
,
672 doc
: /* Function to call when an undo list exceeds `undo-outer-limit'.
673 This function is called with one argument, the current undo list size
674 for the most recent command (since the last undo boundary).
675 If the function returns t, that means truncation has been fully handled.
676 If it returns nil, the other forms of truncation are done.
678 Garbage collection is inhibited around the call to this function,
679 so it must make sure not to do a lot of consing. */);
680 Vundo_outer_limit_function
= Qnil
;
683 /* arch-tag: d546ee01-4aed-4ffb-bb8b-eefaae50d38a
684 (do not change this comment) */