1 /* undo handling for GNU Emacs.
2 Copyright (C) 1990, 1993, 1994 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 an insertion that just happened or is about to happen,
40 for LENGTH characters at position BEG.
41 (It is possible to record an insertion before or after the fact
42 because we don't need to record the contents.) */
45 record_insert (beg
, length
)
48 Lisp_Object lbeg
, lend
;
50 if (EQ (current_buffer
->undo_list
, Qt
))
53 /* Allocate a cons cell to be the undo boundary after this command. */
54 if (NILP (pending_boundary
))
55 pending_boundary
= Fcons (Qnil
, Qnil
);
57 if (!BUFFERP (last_undo_buffer
)
58 || current_buffer
!= XBUFFER (last_undo_buffer
))
60 XSETBUFFER (last_undo_buffer
, current_buffer
);
62 if (MODIFF
<= SAVE_MODIFF
)
63 record_first_change ();
65 /* If this is following another insertion and consecutive with it
66 in the buffer, combine the two. */
67 if (CONSP (current_buffer
->undo_list
))
70 elt
= XCAR (current_buffer
->undo_list
);
72 && INTEGERP (XCAR (elt
))
73 && INTEGERP (XCDR (elt
))
74 && XINT (XCDR (elt
)) == beg
)
76 XSETINT (XCDR (elt
), beg
+ length
);
81 XSETFASTINT (lbeg
, beg
);
82 XSETINT (lend
, beg
+ length
);
83 current_buffer
->undo_list
= Fcons (Fcons (lbeg
, lend
),
84 current_buffer
->undo_list
);
87 /* Record that a deletion is about to take place,
88 of the characters in STRING, at location BEG. */
91 record_delete (beg
, string
)
98 if (EQ (current_buffer
->undo_list
, Qt
))
101 /* Allocate a cons cell to be the undo boundary after this command. */
102 if (NILP (pending_boundary
))
103 pending_boundary
= Fcons (Qnil
, Qnil
);
105 if (current_buffer
!= XBUFFER (last_undo_buffer
))
107 XSETBUFFER (last_undo_buffer
, current_buffer
);
109 if (CONSP (current_buffer
->undo_list
))
111 /* Set AT_BOUNDARY to 1 only when we have nothing other than
112 marker adjustment before undo boundary. */
114 Lisp_Object tail
= current_buffer
->undo_list
, elt
;
122 if (NILP (elt
) || ! (CONSP (elt
) && MARKERP (XCAR (elt
))))
126 at_boundary
= NILP (elt
);
131 if (MODIFF
<= SAVE_MODIFF
)
132 record_first_change ();
134 if (PT
== beg
+ XSTRING (string
)->size
)
135 XSETINT (sbeg
, -beg
);
137 XSETFASTINT (sbeg
, beg
);
139 /* If we are just after an undo boundary, and
140 point wasn't at start of deleted range, record where it was. */
142 && last_point_position
!= XFASTINT (sbeg
)
143 && current_buffer
== XBUFFER (last_point_position_buffer
))
144 current_buffer
->undo_list
145 = Fcons (make_number (last_point_position
), current_buffer
->undo_list
);
147 current_buffer
->undo_list
148 = Fcons (Fcons (string
, sbeg
), current_buffer
->undo_list
);
151 /* Record the fact that MARKER is about to be adjusted by ADJUSTMENT.
152 This is done only when a marker points within text being deleted,
153 because that's the only case where an automatic marker adjustment
154 won't be inverted automatically by undoing the buffer modification. */
157 record_marker_adjustment (marker
, adjustment
)
161 if (EQ (current_buffer
->undo_list
, Qt
))
164 /* Allocate a cons cell to be the undo boundary after this command. */
165 if (NILP (pending_boundary
))
166 pending_boundary
= Fcons (Qnil
, Qnil
);
168 if (current_buffer
!= XBUFFER (last_undo_buffer
))
170 XSETBUFFER (last_undo_buffer
, current_buffer
);
172 current_buffer
->undo_list
173 = Fcons (Fcons (marker
, make_number (adjustment
)),
174 current_buffer
->undo_list
);
177 /* Record that a replacement is about to take place,
178 for LENGTH characters at location BEG.
179 The replacement must not change the number of characters. */
182 record_change (beg
, length
)
185 record_delete (beg
, make_buffer_string (beg
, beg
+ length
, 1));
186 record_insert (beg
, length
);
189 /* Record that an unmodified buffer is about to be changed.
190 Record the file modification date so that when undoing this entry
191 we can tell whether it is obsolete because the file was saved again. */
194 record_first_change ()
196 Lisp_Object high
, low
;
197 struct buffer
*base_buffer
= current_buffer
;
199 if (EQ (current_buffer
->undo_list
, Qt
))
202 if (current_buffer
!= XBUFFER (last_undo_buffer
))
204 XSETBUFFER (last_undo_buffer
, current_buffer
);
206 if (base_buffer
->base_buffer
)
207 base_buffer
= base_buffer
->base_buffer
;
209 XSETFASTINT (high
, (base_buffer
->modtime
>> 16) & 0xffff);
210 XSETFASTINT (low
, base_buffer
->modtime
& 0xffff);
211 current_buffer
->undo_list
= Fcons (Fcons (Qt
, Fcons (high
, low
)), current_buffer
->undo_list
);
214 /* Record a change in property PROP (whose old value was VAL)
215 for LENGTH characters starting at position BEG in BUFFER. */
218 record_property_change (beg
, length
, prop
, value
, buffer
)
220 Lisp_Object prop
, value
, buffer
;
222 Lisp_Object lbeg
, lend
, entry
;
223 struct buffer
*obuf
= current_buffer
;
226 if (EQ (XBUFFER (buffer
)->undo_list
, Qt
))
229 /* Allocate a cons cell to be the undo boundary after this command. */
230 if (NILP (pending_boundary
))
231 pending_boundary
= Fcons (Qnil
, Qnil
);
233 if (!EQ (buffer
, last_undo_buffer
))
235 last_undo_buffer
= buffer
;
237 /* Switch temporarily to the buffer that was changed. */
238 current_buffer
= XBUFFER (buffer
);
243 if (MODIFF
<= SAVE_MODIFF
)
244 record_first_change ();
247 XSETINT (lend
, beg
+ length
);
248 entry
= Fcons (Qnil
, Fcons (prop
, Fcons (value
, Fcons (lbeg
, lend
))));
249 current_buffer
->undo_list
= Fcons (entry
, current_buffer
->undo_list
);
251 current_buffer
= obuf
;
254 DEFUN ("undo-boundary", Fundo_boundary
, Sundo_boundary
, 0, 0, 0,
255 "Mark a boundary between units of undo.\n\
256 An undo command will stop at this point,\n\
257 but another undo command will undo to the previous boundary.")
261 if (EQ (current_buffer
->undo_list
, Qt
))
263 tem
= Fcar (current_buffer
->undo_list
);
266 /* One way or another, cons nil onto the front of the undo list. */
267 if (!NILP (pending_boundary
))
269 /* If we have preallocated the cons cell to use here,
271 XCDR (pending_boundary
) = current_buffer
->undo_list
;
272 current_buffer
->undo_list
= pending_boundary
;
273 pending_boundary
= Qnil
;
276 current_buffer
->undo_list
= Fcons (Qnil
, current_buffer
->undo_list
);
281 /* At garbage collection time, make an undo list shorter at the end,
282 returning the truncated list.
283 MINSIZE and MAXSIZE are the limits on size allowed, as described below.
284 In practice, these are the values of undo-limit and
285 undo-strong-limit. */
288 truncate_undo_list (list
, minsize
, maxsize
)
290 int minsize
, maxsize
;
292 Lisp_Object prev
, next
, last_boundary
;
297 last_boundary
= Qnil
;
299 /* Always preserve at least the most recent undo record.
300 If the first element is an undo boundary, skip past it.
302 Skip, skip, skip the undo, skip, skip, skip the undo,
303 Skip, skip, skip the undo, skip to the undo bound'ry.
304 (Get it? "Skip to my Loo?") */
305 if (CONSP (next
) && NILP (XCAR (next
)))
307 /* Add in the space occupied by this element and its chain link. */
308 size_so_far
+= sizeof (struct Lisp_Cons
);
310 /* Advance to next element. */
314 while (CONSP (next
) && ! NILP (XCAR (next
)))
319 /* Add in the space occupied by this element and its chain link. */
320 size_so_far
+= sizeof (struct Lisp_Cons
);
323 size_so_far
+= sizeof (struct Lisp_Cons
);
324 if (STRINGP (XCAR (elt
)))
325 size_so_far
+= (sizeof (struct Lisp_String
) - 1
326 + XSTRING (XCAR (elt
))->size
);
329 /* Advance to next element. */
334 last_boundary
= prev
;
341 /* When we get to a boundary, decide whether to truncate
342 either before or after it. The lower threshold, MINSIZE,
343 tells us to truncate after it. If its size pushes past
344 the higher threshold MAXSIZE as well, we truncate before it. */
347 if (size_so_far
> maxsize
)
349 last_boundary
= prev
;
350 if (size_so_far
> minsize
)
354 /* Add in the space occupied by this element and its chain link. */
355 size_so_far
+= sizeof (struct Lisp_Cons
);
358 size_so_far
+= sizeof (struct Lisp_Cons
);
359 if (STRINGP (XCAR (elt
)))
360 size_so_far
+= (sizeof (struct Lisp_String
) - 1
361 + XSTRING (XCAR (elt
))->size
);
364 /* Advance to next element. */
369 /* If we scanned the whole list, it is short enough; don't change it. */
373 /* Truncate at the boundary where we decided to truncate. */
374 if (!NILP (last_boundary
))
376 XCDR (last_boundary
) = Qnil
;
383 DEFUN ("primitive-undo", Fprimitive_undo
, Sprimitive_undo
, 2, 2, 0,
384 "Undo N records from the front of the list LIST.\n\
385 Return what remains of the list.")
389 struct gcpro gcpro1
, gcpro2
;
391 int count
= specpdl_ptr
- specpdl
;
393 #if 0 /* This is a good feature, but would make undo-start
394 unable to do what is expected. */
397 /* If the head of the list is a boundary, it is the boundary
398 preceding this command. Get rid of it and don't count it. */
409 /* Don't let read-only properties interfere with undo. */
410 if (NILP (current_buffer
->read_only
))
411 specbind (Qinhibit_read_only
, Qt
);
419 /* Exit inner loop at undo boundary. */
422 /* Handle an integer by setting point to that value. */
424 SET_PT (clip_to_bounds (BEGV
, XINT (next
), ZV
));
425 else if (CONSP (next
))
427 Lisp_Object car
, cdr
;
433 /* Element (t high . low) records previous modtime. */
434 Lisp_Object high
, low
;
436 struct buffer
*base_buffer
= current_buffer
;
440 mod_time
= (XFASTINT (high
) << 16) + XFASTINT (low
);
442 if (current_buffer
->base_buffer
)
443 base_buffer
= current_buffer
->base_buffer
;
445 /* If this records an obsolete save
446 (not matching the actual disk file)
447 then don't mark unmodified. */
448 if (mod_time
!= base_buffer
->modtime
)
450 #ifdef CLASH_DETECTION
452 #endif /* CLASH_DETECTION */
453 Fset_buffer_modified_p (Qnil
);
455 else if (EQ (car
, Qnil
))
457 /* Element (nil prop val beg . end) is property change. */
458 Lisp_Object beg
, end
, prop
, val
;
467 Fput_text_property (beg
, end
, prop
, val
, Qnil
);
469 else if (INTEGERP (car
) && INTEGERP (cdr
))
471 /* Element (BEG . END) means range was inserted. */
474 if (XINT (car
) < BEGV
476 error ("Changes to be undone are outside visible portion of buffer");
477 /* Set point first thing, so that undoing this undo
478 does not send point back to where it is now. */
480 Fdelete_region (car
, cdr
);
482 else if (STRINGP (car
) && INTEGERP (cdr
))
484 /* Element (STRING . POS) means STRING was deleted. */
486 int pos
= XINT (cdr
);
491 if (-pos
< BEGV
|| -pos
> ZV
)
492 error ("Changes to be undone are outside visible portion of buffer");
494 Finsert (1, &membuf
);
498 if (pos
< BEGV
|| pos
> ZV
)
499 error ("Changes to be undone are outside visible portion of buffer");
502 /* Now that we record marker adjustments
503 (caused by deletion) for undo,
504 we should always insert after markers,
505 so that undoing the marker adjustments
506 put the markers back in the right place. */
507 Finsert (1, &membuf
);
511 else if (MARKERP (car
) && INTEGERP (cdr
))
513 /* (MARKER . INTEGER) means a marker MARKER
514 was adjusted by INTEGER. */
515 if (XMARKER (car
)->buffer
)
517 make_number (marker_position (car
) - XINT (cdr
)),
518 Fmarker_buffer (car
));
526 return unbind_to (count
, list
);
532 Qinhibit_read_only
= intern ("inhibit-read-only");
533 staticpro (&Qinhibit_read_only
);
535 pending_boundary
= Qnil
;
536 staticpro (&pending_boundary
);
538 defsubr (&Sprimitive_undo
);
539 defsubr (&Sundo_boundary
);