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 /* Last buffer for which undo information was recorded. */
28 Lisp_Object last_undo_buffer
;
30 Lisp_Object Qinhibit_read_only
;
32 /* The first time a command records something for undo.
33 it also allocates the undo-boundary object
34 which will be added to the list at the end of the command.
35 This ensures we can't run out of space while trying to make
37 Lisp_Object pending_boundary
;
39 /* Record point as it was at beginning of this command (if necessary)
40 And prepare the undo info for recording a change.
41 PT is the position of point that will naturally occur as a result of the
42 undo record that will be added just after this command terminates. */
49 /* Allocate a cons cell to be the undo boundary after this command. */
50 if (NILP (pending_boundary
))
51 pending_boundary
= Fcons (Qnil
, Qnil
);
53 if (!BUFFERP (last_undo_buffer
)
54 || current_buffer
!= XBUFFER (last_undo_buffer
))
56 XSETBUFFER (last_undo_buffer
, current_buffer
);
58 if (CONSP (current_buffer
->undo_list
))
60 /* Set AT_BOUNDARY to 1 only when we have nothing other than
61 marker adjustment before undo boundary. */
63 Lisp_Object tail
= current_buffer
->undo_list
, elt
;
71 if (NILP (elt
) || ! (CONSP (elt
) && MARKERP (XCAR (elt
))))
75 at_boundary
= NILP (elt
);
80 if (MODIFF
<= SAVE_MODIFF
)
81 record_first_change ();
83 /* If we are just after an undo boundary, and
84 point wasn't at start of deleted range, record where it was. */
86 && last_point_position
!= pt
87 /* If we're called from batch mode, this could be nil. */
88 && BUFFERP (last_point_position_buffer
)
89 && current_buffer
== XBUFFER (last_point_position_buffer
))
90 current_buffer
->undo_list
91 = Fcons (make_number (last_point_position
), current_buffer
->undo_list
);
94 /* Record an insertion that just happened or is about to happen,
95 for LENGTH characters at position BEG.
96 (It is possible to record an insertion before or after the fact
97 because we don't need to record the contents.) */
100 record_insert (beg
, length
)
103 Lisp_Object lbeg
, lend
;
105 if (EQ (current_buffer
->undo_list
, Qt
))
110 /* If this is following another insertion and consecutive with it
111 in the buffer, combine the two. */
112 if (CONSP (current_buffer
->undo_list
))
115 elt
= XCAR (current_buffer
->undo_list
);
117 && INTEGERP (XCAR (elt
))
118 && INTEGERP (XCDR (elt
))
119 && XINT (XCDR (elt
)) == beg
)
121 XSETCDR (elt
, make_number (beg
+ length
));
126 XSETFASTINT (lbeg
, beg
);
127 XSETINT (lend
, beg
+ length
);
128 current_buffer
->undo_list
= Fcons (Fcons (lbeg
, lend
),
129 current_buffer
->undo_list
);
132 /* Record that a deletion is about to take place,
133 of the characters in STRING, at location BEG. */
136 record_delete (beg
, string
)
142 if (EQ (current_buffer
->undo_list
, Qt
))
145 if (PT
== beg
+ SCHARS (string
))
147 XSETINT (sbeg
, -beg
);
152 XSETFASTINT (sbeg
, beg
);
156 current_buffer
->undo_list
157 = Fcons (Fcons (string
, sbeg
), current_buffer
->undo_list
);
160 /* Record the fact that MARKER is about to be adjusted by ADJUSTMENT.
161 This is done only when a marker points within text being deleted,
162 because that's the only case where an automatic marker adjustment
163 won't be inverted automatically by undoing the buffer modification. */
166 record_marker_adjustment (marker
, adjustment
)
170 if (EQ (current_buffer
->undo_list
, Qt
))
173 /* Allocate a cons cell to be the undo boundary after this command. */
174 if (NILP (pending_boundary
))
175 pending_boundary
= Fcons (Qnil
, Qnil
);
177 if (!BUFFERP (last_undo_buffer
)
178 || current_buffer
!= XBUFFER (last_undo_buffer
))
180 XSETBUFFER (last_undo_buffer
, current_buffer
);
182 current_buffer
->undo_list
183 = Fcons (Fcons (marker
, make_number (adjustment
)),
184 current_buffer
->undo_list
);
187 /* Record that a replacement is about to take place,
188 for LENGTH characters at location BEG.
189 The replacement must not change the number of characters. */
192 record_change (beg
, length
)
195 record_delete (beg
, make_buffer_string (beg
, beg
+ length
, 1));
196 record_insert (beg
, length
);
199 /* Record that an unmodified buffer is about to be changed.
200 Record the file modification date so that when undoing this entry
201 we can tell whether it is obsolete because the file was saved again. */
204 record_first_change ()
206 Lisp_Object high
, low
;
207 struct buffer
*base_buffer
= current_buffer
;
209 if (EQ (current_buffer
->undo_list
, Qt
))
212 if (!BUFFERP (last_undo_buffer
)
213 || current_buffer
!= XBUFFER (last_undo_buffer
))
215 XSETBUFFER (last_undo_buffer
, current_buffer
);
217 if (base_buffer
->base_buffer
)
218 base_buffer
= base_buffer
->base_buffer
;
220 XSETFASTINT (high
, (base_buffer
->modtime
>> 16) & 0xffff);
221 XSETFASTINT (low
, base_buffer
->modtime
& 0xffff);
222 current_buffer
->undo_list
= Fcons (Fcons (Qt
, Fcons (high
, low
)), current_buffer
->undo_list
);
225 /* Record a change in property PROP (whose old value was VAL)
226 for LENGTH characters starting at position BEG in BUFFER. */
229 record_property_change (beg
, length
, prop
, value
, buffer
)
231 Lisp_Object prop
, value
, buffer
;
233 Lisp_Object lbeg
, lend
, entry
;
234 struct buffer
*obuf
= current_buffer
;
237 if (EQ (XBUFFER (buffer
)->undo_list
, Qt
))
240 /* Allocate a cons cell to be the undo boundary after this command. */
241 if (NILP (pending_boundary
))
242 pending_boundary
= Fcons (Qnil
, Qnil
);
244 if (!EQ (buffer
, last_undo_buffer
))
246 last_undo_buffer
= buffer
;
248 /* Switch temporarily to the buffer that was changed. */
249 current_buffer
= XBUFFER (buffer
);
254 if (MODIFF
<= SAVE_MODIFF
)
255 record_first_change ();
258 XSETINT (lend
, beg
+ length
);
259 entry
= Fcons (Qnil
, Fcons (prop
, Fcons (value
, Fcons (lbeg
, lend
))));
260 current_buffer
->undo_list
= Fcons (entry
, current_buffer
->undo_list
);
262 current_buffer
= obuf
;
265 DEFUN ("undo-boundary", Fundo_boundary
, Sundo_boundary
, 0, 0, 0,
266 doc
: /* Mark a boundary between units of undo.
267 An undo command will stop at this point,
268 but another undo command will undo to the previous boundary. */)
272 if (EQ (current_buffer
->undo_list
, Qt
))
274 tem
= Fcar (current_buffer
->undo_list
);
277 /* One way or another, cons nil onto the front of the undo list. */
278 if (!NILP (pending_boundary
))
280 /* If we have preallocated the cons cell to use here,
282 XSETCDR (pending_boundary
, current_buffer
->undo_list
);
283 current_buffer
->undo_list
= pending_boundary
;
284 pending_boundary
= Qnil
;
287 current_buffer
->undo_list
= Fcons (Qnil
, current_buffer
->undo_list
);
292 /* At garbage collection time, make an undo list shorter at the end,
293 returning the truncated list.
294 MINSIZE and MAXSIZE are the limits on size allowed, as described below.
295 In practice, these are the values of undo-limit and
296 undo-strong-limit. */
299 truncate_undo_list (list
, minsize
, maxsize
)
301 int minsize
, maxsize
;
303 Lisp_Object prev
, next
, last_boundary
;
308 last_boundary
= Qnil
;
310 /* Always preserve at least the most recent undo record.
311 If the first element is an undo boundary, skip past it.
313 Skip, skip, skip the undo, skip, skip, skip the undo,
314 Skip, skip, skip the undo, skip to the undo bound'ry.
315 (Get it? "Skip to my Loo?") */
316 if (CONSP (next
) && NILP (XCAR (next
)))
318 /* Add in the space occupied by this element and its chain link. */
319 size_so_far
+= sizeof (struct Lisp_Cons
);
321 /* Advance to next element. */
325 while (CONSP (next
) && ! NILP (XCAR (next
)))
330 /* Add in the space occupied by this element and its chain link. */
331 size_so_far
+= sizeof (struct Lisp_Cons
);
334 size_so_far
+= sizeof (struct Lisp_Cons
);
335 if (STRINGP (XCAR (elt
)))
336 size_so_far
+= (sizeof (struct Lisp_String
) - 1
337 + SCHARS (XCAR (elt
)));
340 /* Advance to next element. */
345 last_boundary
= prev
;
352 /* When we get to a boundary, decide whether to truncate
353 either before or after it. The lower threshold, MINSIZE,
354 tells us to truncate after it. If its size pushes past
355 the higher threshold MAXSIZE as well, we truncate before it. */
358 if (size_so_far
> maxsize
)
360 last_boundary
= prev
;
361 if (size_so_far
> minsize
)
365 /* Add in the space occupied by this element and its chain link. */
366 size_so_far
+= sizeof (struct Lisp_Cons
);
369 size_so_far
+= sizeof (struct Lisp_Cons
);
370 if (STRINGP (XCAR (elt
)))
371 size_so_far
+= (sizeof (struct Lisp_String
) - 1
372 + SCHARS (XCAR (elt
)));
375 /* Advance to next element. */
380 /* If we scanned the whole list, it is short enough; don't change it. */
384 /* Truncate at the boundary where we decided to truncate. */
385 if (!NILP (last_boundary
))
387 XSETCDR (last_boundary
, Qnil
);
394 DEFUN ("primitive-undo", Fprimitive_undo
, Sprimitive_undo
, 2, 2, 0,
395 doc
: /* Undo N records from the front of the list LIST.
396 Return what remains of the list. */)
400 struct gcpro gcpro1
, gcpro2
;
402 int count
= SPECPDL_INDEX ();
405 #if 0 /* This is a good feature, but would make undo-start
406 unable to do what is expected. */
409 /* If the head of the list is a boundary, it is the boundary
410 preceding this command. Get rid of it and don't count it. */
421 /* In a writable buffer, enable undoing read-only text that is so
422 because of text properties. */
423 if (NILP (current_buffer
->read_only
))
424 specbind (Qinhibit_read_only
, Qt
);
426 /* Don't let `intangible' properties interfere with undo. */
427 specbind (Qinhibit_point_motion_hooks
, Qt
);
435 /* Exit inner loop at undo boundary. */
438 /* Handle an integer by setting point to that value. */
440 SET_PT (clip_to_bounds (BEGV
, XINT (next
), ZV
));
441 else if (CONSP (next
))
443 Lisp_Object car
, cdr
;
449 /* Element (t high . low) records previous modtime. */
450 Lisp_Object high
, low
;
452 struct buffer
*base_buffer
= current_buffer
;
456 mod_time
= (XFASTINT (high
) << 16) + XFASTINT (low
);
458 if (current_buffer
->base_buffer
)
459 base_buffer
= current_buffer
->base_buffer
;
461 /* If this records an obsolete save
462 (not matching the actual disk file)
463 then don't mark unmodified. */
464 if (mod_time
!= base_buffer
->modtime
)
466 #ifdef CLASH_DETECTION
468 #endif /* CLASH_DETECTION */
469 Fset_buffer_modified_p (Qnil
);
471 else if (EQ (car
, Qnil
))
473 /* Element (nil prop val beg . end) is property change. */
474 Lisp_Object beg
, end
, prop
, val
;
483 Fput_text_property (beg
, end
, prop
, val
, Qnil
);
485 else if (INTEGERP (car
) && INTEGERP (cdr
))
487 /* Element (BEG . END) means range was inserted. */
489 if (XINT (car
) < BEGV
491 error ("Changes to be undone are outside visible portion of buffer");
492 /* Set point first thing, so that undoing this undo
493 does not send point back to where it is now. */
495 Fdelete_region (car
, cdr
);
497 else if (STRINGP (car
) && INTEGERP (cdr
))
499 /* Element (STRING . POS) means STRING was deleted. */
501 int pos
= XINT (cdr
);
506 if (-pos
< BEGV
|| -pos
> ZV
)
507 error ("Changes to be undone are outside visible portion of buffer");
509 Finsert (1, &membuf
);
513 if (pos
< BEGV
|| pos
> ZV
)
514 error ("Changes to be undone are outside visible portion of buffer");
517 /* Now that we record marker adjustments
518 (caused by deletion) for undo,
519 we should always insert after markers,
520 so that undoing the marker adjustments
521 put the markers back in the right place. */
522 Finsert (1, &membuf
);
526 else if (MARKERP (car
) && INTEGERP (cdr
))
528 /* (MARKER . INTEGER) means a marker MARKER
529 was adjusted by INTEGER. */
530 if (XMARKER (car
)->buffer
)
532 make_number (marker_position (car
) - XINT (cdr
)),
533 Fmarker_buffer (car
));
541 return unbind_to (count
, list
);
547 Qinhibit_read_only
= intern ("inhibit-read-only");
548 staticpro (&Qinhibit_read_only
);
550 pending_boundary
= Qnil
;
551 staticpro (&pending_boundary
);
553 defsubr (&Sprimitive_undo
);
554 defsubr (&Sundo_boundary
);