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 /* The first time a command records something for undo.
44 it also allocates the undo-boundary object
45 which will be added to the list at the end of the command.
46 This ensures we can't run out of space while trying to make
48 Lisp_Object pending_boundary
;
50 /* Record point as it was at beginning of this command (if necessary)
51 And prepare the undo info for recording a change.
52 PT is the position of point that will naturally occur as a result of the
53 undo record that will be added just after this command terminates. */
61 /* Allocate a cons cell to be the undo boundary after this command. */
62 if (NILP (pending_boundary
))
63 pending_boundary
= Fcons (Qnil
, Qnil
);
65 if (!BUFFERP (last_undo_buffer
)
66 || current_buffer
!= XBUFFER (last_undo_buffer
))
68 XSETBUFFER (last_undo_buffer
, current_buffer
);
70 if (CONSP (current_buffer
->undo_list
))
72 /* Set AT_BOUNDARY to 1 only when we have nothing other than
73 marker adjustment before undo boundary. */
75 Lisp_Object tail
= current_buffer
->undo_list
, elt
;
83 if (NILP (elt
) || ! (CONSP (elt
) && MARKERP (XCAR (elt
))))
87 at_boundary
= NILP (elt
);
92 if (MODIFF
<= SAVE_MODIFF
)
93 record_first_change ();
95 /* If we are just after an undo boundary, and
96 point wasn't at start of deleted range, record where it was. */
98 && last_point_position
!= pt
99 /* If we're called from batch mode, this could be nil. */
100 && BUFFERP (last_point_position_buffer
)
101 && current_buffer
== XBUFFER (last_point_position_buffer
))
102 current_buffer
->undo_list
103 = Fcons (make_number (last_point_position
), current_buffer
->undo_list
);
106 /* Record an insertion that just happened or is about to happen,
107 for LENGTH characters at position BEG.
108 (It is possible to record an insertion before or after the fact
109 because we don't need to record the contents.) */
112 record_insert (beg
, length
)
115 Lisp_Object lbeg
, lend
;
117 if (EQ (current_buffer
->undo_list
, Qt
))
122 /* If this is following another insertion and consecutive with it
123 in the buffer, combine the two. */
124 if (CONSP (current_buffer
->undo_list
))
127 elt
= XCAR (current_buffer
->undo_list
);
129 && INTEGERP (XCAR (elt
))
130 && INTEGERP (XCDR (elt
))
131 && XINT (XCDR (elt
)) == beg
)
133 XSETCDR (elt
, make_number (beg
+ length
));
138 XSETFASTINT (lbeg
, beg
);
139 XSETINT (lend
, beg
+ length
);
140 current_buffer
->undo_list
= Fcons (Fcons (lbeg
, lend
),
141 current_buffer
->undo_list
);
144 /* Record that a deletion is about to take place,
145 of the characters in STRING, at location BEG. */
148 record_delete (beg
, string
)
154 if (EQ (current_buffer
->undo_list
, Qt
))
157 if (PT
== beg
+ SCHARS (string
))
159 XSETINT (sbeg
, -beg
);
164 XSETFASTINT (sbeg
, beg
);
168 current_buffer
->undo_list
169 = Fcons (Fcons (string
, sbeg
), current_buffer
->undo_list
);
172 /* Record the fact that MARKER is about to be adjusted by ADJUSTMENT.
173 This is done only when a marker points within text being deleted,
174 because that's the only case where an automatic marker adjustment
175 won't be inverted automatically by undoing the buffer modification. */
178 record_marker_adjustment (marker
, adjustment
)
182 if (EQ (current_buffer
->undo_list
, Qt
))
185 /* Allocate a cons cell to be the undo boundary after this command. */
186 if (NILP (pending_boundary
))
187 pending_boundary
= Fcons (Qnil
, Qnil
);
189 if (!BUFFERP (last_undo_buffer
)
190 || current_buffer
!= XBUFFER (last_undo_buffer
))
192 XSETBUFFER (last_undo_buffer
, current_buffer
);
194 current_buffer
->undo_list
195 = Fcons (Fcons (marker
, make_number (adjustment
)),
196 current_buffer
->undo_list
);
199 /* Record that a replacement is about to take place,
200 for LENGTH characters at location BEG.
201 The replacement must not change the number of characters. */
204 record_change (beg
, length
)
207 record_delete (beg
, make_buffer_string (beg
, beg
+ length
, 1));
208 record_insert (beg
, length
);
211 /* Record that an unmodified buffer is about to be changed.
212 Record the file modification date so that when undoing this entry
213 we can tell whether it is obsolete because the file was saved again. */
216 record_first_change ()
218 Lisp_Object high
, low
;
219 struct buffer
*base_buffer
= current_buffer
;
221 if (EQ (current_buffer
->undo_list
, Qt
))
224 if (!BUFFERP (last_undo_buffer
)
225 || current_buffer
!= XBUFFER (last_undo_buffer
))
227 XSETBUFFER (last_undo_buffer
, current_buffer
);
229 if (base_buffer
->base_buffer
)
230 base_buffer
= base_buffer
->base_buffer
;
232 XSETFASTINT (high
, (base_buffer
->modtime
>> 16) & 0xffff);
233 XSETFASTINT (low
, base_buffer
->modtime
& 0xffff);
234 current_buffer
->undo_list
= Fcons (Fcons (Qt
, Fcons (high
, low
)), current_buffer
->undo_list
);
237 /* Record a change in property PROP (whose old value was VAL)
238 for LENGTH characters starting at position BEG in BUFFER. */
241 record_property_change (beg
, length
, prop
, value
, buffer
)
243 Lisp_Object prop
, value
, buffer
;
245 Lisp_Object lbeg
, lend
, entry
;
246 struct buffer
*obuf
= current_buffer
;
249 if (EQ (XBUFFER (buffer
)->undo_list
, Qt
))
252 /* Allocate a cons cell to be the undo boundary after this command. */
253 if (NILP (pending_boundary
))
254 pending_boundary
= Fcons (Qnil
, Qnil
);
256 if (!EQ (buffer
, last_undo_buffer
))
258 last_undo_buffer
= buffer
;
260 /* Switch temporarily to the buffer that was changed. */
261 current_buffer
= XBUFFER (buffer
);
266 if (MODIFF
<= SAVE_MODIFF
)
267 record_first_change ();
270 XSETINT (lend
, beg
+ length
);
271 entry
= Fcons (Qnil
, Fcons (prop
, Fcons (value
, Fcons (lbeg
, lend
))));
272 current_buffer
->undo_list
= Fcons (entry
, current_buffer
->undo_list
);
274 current_buffer
= obuf
;
277 DEFUN ("undo-boundary", Fundo_boundary
, Sundo_boundary
, 0, 0, 0,
278 doc
: /* Mark a boundary between units of undo.
279 An undo command will stop at this point,
280 but another undo command will undo to the previous boundary. */)
284 if (EQ (current_buffer
->undo_list
, Qt
))
286 tem
= Fcar (current_buffer
->undo_list
);
289 /* One way or another, cons nil onto the front of the undo list. */
290 if (!NILP (pending_boundary
))
292 /* If we have preallocated the cons cell to use here,
294 XSETCDR (pending_boundary
, current_buffer
->undo_list
);
295 current_buffer
->undo_list
= pending_boundary
;
296 pending_boundary
= Qnil
;
299 current_buffer
->undo_list
= Fcons (Qnil
, current_buffer
->undo_list
);
304 /* At garbage collection time, make an undo list shorter at the end,
305 returning the truncated list. How this is done depends on the
306 variables undo-limit, undo-strong-limit and undo-outer-limit.
307 In some cases this works by calling undo-outer-limit-function. */
310 truncate_undo_list (b
)
314 Lisp_Object prev
, next
, last_boundary
;
317 /* Make sure that calling undo-outer-limit-function
318 won't cause another GC. */
319 int count
= inhibit_garbage_collection ();
321 /* Make the buffer current to get its local values of variables such
322 as undo_limit. Also so that Vundo_outer_limit_function can
323 tell which buffer to operate on. */
324 record_unwind_protect (set_buffer_if_live
, Fcurrent_buffer ());
325 set_buffer_internal (b
);
331 last_boundary
= Qnil
;
333 /* If the first element is an undo boundary, skip past it. */
334 if (CONSP (next
) && NILP (XCAR (next
)))
336 /* Add in the space occupied by this element and its chain link. */
337 size_so_far
+= sizeof (struct Lisp_Cons
);
339 /* Advance to next element. */
344 /* Always preserve at least the most recent undo record
345 unless it is really horribly big.
347 Skip, skip, skip the undo, skip, skip, skip the undo,
348 Skip, skip, skip the undo, skip to the undo bound'ry. */
350 while (CONSP (next
) && ! NILP (XCAR (next
)))
355 /* Add in the space occupied by this element and its chain link. */
356 size_so_far
+= sizeof (struct Lisp_Cons
);
359 size_so_far
+= sizeof (struct Lisp_Cons
);
360 if (STRINGP (XCAR (elt
)))
361 size_so_far
+= (sizeof (struct Lisp_String
) - 1
362 + SCHARS (XCAR (elt
)));
365 /* Advance to next element. */
370 /* If by the first boundary we have already passed undo_outer_limit,
371 we're heading for memory full, so offer to clear out the list. */
372 if (INTEGERP (Vundo_outer_limit
)
373 && size_so_far
> XINT (Vundo_outer_limit
)
374 && !NILP (Vundo_outer_limit_function
))
376 Lisp_Object temp
= last_undo_buffer
;
378 /* Normally the function this calls is undo-outer-limit-truncate. */
379 if (! NILP (call1 (Vundo_outer_limit_function
,
380 make_number (size_so_far
))))
382 /* The function is responsible for making
383 any desired changes in buffer-undo-list. */
384 unbind_to (count
, Qnil
);
387 /* That function probably used the minibuffer, and if so, that
388 changed last_undo_buffer. Change it back so that we don't
389 force next change to make an undo boundary here. */
390 last_undo_buffer
= temp
;
394 last_boundary
= prev
;
396 /* Keep additional undo data, if it fits in the limits. */
402 /* When we get to a boundary, decide whether to truncate
403 either before or after it. The lower threshold, undo_limit,
404 tells us to truncate after it. If its size pushes past
405 the higher threshold undo_strong_limit, we truncate before it. */
408 if (size_so_far
> undo_strong_limit
)
410 last_boundary
= prev
;
411 if (size_so_far
> undo_limit
)
415 /* Add in the space occupied by this element and its chain link. */
416 size_so_far
+= sizeof (struct Lisp_Cons
);
419 size_so_far
+= sizeof (struct Lisp_Cons
);
420 if (STRINGP (XCAR (elt
)))
421 size_so_far
+= (sizeof (struct Lisp_String
) - 1
422 + SCHARS (XCAR (elt
)));
425 /* Advance to next element. */
430 /* If we scanned the whole list, it is short enough; don't change it. */
433 /* Truncate at the boundary where we decided to truncate. */
434 else if (!NILP (last_boundary
))
435 XSETCDR (last_boundary
, Qnil
);
436 /* There's nothing we decided to keep, so clear it out. */
440 unbind_to (count
, Qnil
);
443 DEFUN ("primitive-undo", Fprimitive_undo
, Sprimitive_undo
, 2, 2, 0,
444 doc
: /* Undo N records from the front of the list LIST.
445 Return what remains of the list. */)
449 struct gcpro gcpro1
, gcpro2
;
451 int count
= SPECPDL_INDEX ();
454 #if 0 /* This is a good feature, but would make undo-start
455 unable to do what is expected. */
458 /* If the head of the list is a boundary, it is the boundary
459 preceding this command. Get rid of it and don't count it. */
470 /* In a writable buffer, enable undoing read-only text that is so
471 because of text properties. */
472 if (NILP (current_buffer
->read_only
))
473 specbind (Qinhibit_read_only
, Qt
);
475 /* Don't let `intangible' properties interfere with undo. */
476 specbind (Qinhibit_point_motion_hooks
, Qt
);
484 /* Exit inner loop at undo boundary. */
487 /* Handle an integer by setting point to that value. */
489 SET_PT (clip_to_bounds (BEGV
, XINT (next
), ZV
));
490 else if (CONSP (next
))
492 Lisp_Object car
, cdr
;
498 /* Element (t high . low) records previous modtime. */
499 Lisp_Object high
, low
;
501 struct buffer
*base_buffer
= current_buffer
;
505 mod_time
= (XFASTINT (high
) << 16) + XFASTINT (low
);
507 if (current_buffer
->base_buffer
)
508 base_buffer
= current_buffer
->base_buffer
;
510 /* If this records an obsolete save
511 (not matching the actual disk file)
512 then don't mark unmodified. */
513 if (mod_time
!= base_buffer
->modtime
)
515 #ifdef CLASH_DETECTION
517 #endif /* CLASH_DETECTION */
518 Fset_buffer_modified_p (Qnil
);
520 else if (EQ (car
, Qnil
))
522 /* Element (nil prop val beg . end) is property change. */
523 Lisp_Object beg
, end
, prop
, val
;
532 Fput_text_property (beg
, end
, prop
, val
, Qnil
);
534 else if (INTEGERP (car
) && INTEGERP (cdr
))
536 /* Element (BEG . END) means range was inserted. */
538 if (XINT (car
) < BEGV
540 error ("Changes to be undone are outside visible portion of buffer");
541 /* Set point first thing, so that undoing this undo
542 does not send point back to where it is now. */
544 Fdelete_region (car
, cdr
);
546 else if (STRINGP (car
) && INTEGERP (cdr
))
548 /* Element (STRING . POS) means STRING was deleted. */
550 int pos
= XINT (cdr
);
555 if (-pos
< BEGV
|| -pos
> ZV
)
556 error ("Changes to be undone are outside visible portion of buffer");
558 Finsert (1, &membuf
);
562 if (pos
< BEGV
|| pos
> ZV
)
563 error ("Changes to be undone are outside visible portion of buffer");
566 /* Now that we record marker adjustments
567 (caused by deletion) for undo,
568 we should always insert after markers,
569 so that undoing the marker adjustments
570 put the markers back in the right place. */
571 Finsert (1, &membuf
);
575 else if (MARKERP (car
) && INTEGERP (cdr
))
577 /* (MARKER . INTEGER) means a marker MARKER
578 was adjusted by INTEGER. */
579 if (XMARKER (car
)->buffer
)
581 make_number (marker_position (car
) - XINT (cdr
)),
582 Fmarker_buffer (car
));
590 return unbind_to (count
, list
);
596 Qinhibit_read_only
= intern ("inhibit-read-only");
597 staticpro (&Qinhibit_read_only
);
599 pending_boundary
= Qnil
;
600 staticpro (&pending_boundary
);
602 defsubr (&Sprimitive_undo
);
603 defsubr (&Sundo_boundary
);
605 DEFVAR_INT ("undo-limit", &undo_limit
,
606 doc
: /* Keep no more undo information once it exceeds this size.
607 This limit is applied when garbage collection happens.
608 When a previous command increases the total undo list size past this
609 value, the earlier commands that came before it are forgotten.
611 The size is counted as the number of bytes occupied,
612 which includes both saved text and other data. */);
615 DEFVAR_INT ("undo-strong-limit", &undo_strong_limit
,
616 doc
: /* Don't keep more than this much size of undo information.
617 This limit is applied when garbage collection happens.
618 When a previous command increases the total undo list size past this
619 value, that command and the earlier commands that came before it are forgotten.
620 However, the most recent buffer-modifying command's undo info
621 is never discarded for this reason.
623 The size is counted as the number of bytes occupied,
624 which includes both saved text and other data. */);
625 undo_strong_limit
= 30000;
627 DEFVAR_LISP ("undo-outer-limit", &Vundo_outer_limit
,
628 doc
: /* Outer limit on size of undo information for one command.
629 At garbage collection time, if the current command has produced
630 more than this much undo information, it asks you whether to delete
631 the information. This is a last-ditch limit to prevent memory overflow.
633 The size is counted as the number of bytes occupied,
634 which includes both saved text and other data.
636 In fact, this calls the function which is the value of
637 `undo-outer-limit-function' with one argument, the size.
638 The text above describes the behavior of the function
639 that variable usually specifies. */);
640 Vundo_outer_limit
= make_number (300000);
642 DEFVAR_LISP ("undo-outer-limit-function", &Vundo_outer_limit_function
,
643 doc
: /* Function to call when an undo list exceeds `undo-outer-limit'.
644 This function is called with one argument, the current undo list size
645 for the most recent command (since the last undo boundary).
646 If the function returns t, that means truncation has been fully handled.
647 If it returns nil, the other forms of truncation are done.
649 Garbage collection is inhibited around the call to this function,
650 so it must make sure not to do a lot of consing. */);
651 Vundo_outer_limit_function
= Qnil
;
654 /* arch-tag: d546ee01-4aed-4ffb-bb8b-eefaae50d38a
655 (do not change this comment) */