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 /* If we're called from batch mode, this could be nil. */
144 && BUFFERP (last_point_position_buffer
)
145 && current_buffer
== XBUFFER (last_point_position_buffer
))
146 current_buffer
->undo_list
147 = Fcons (make_number (last_point_position
), current_buffer
->undo_list
);
149 current_buffer
->undo_list
150 = Fcons (Fcons (string
, sbeg
), current_buffer
->undo_list
);
153 /* Record the fact that MARKER is about to be adjusted by ADJUSTMENT.
154 This is done only when a marker points within text being deleted,
155 because that's the only case where an automatic marker adjustment
156 won't be inverted automatically by undoing the buffer modification. */
159 record_marker_adjustment (marker
, adjustment
)
163 if (EQ (current_buffer
->undo_list
, Qt
))
166 /* Allocate a cons cell to be the undo boundary after this command. */
167 if (NILP (pending_boundary
))
168 pending_boundary
= Fcons (Qnil
, Qnil
);
170 if (!BUFFERP (last_undo_buffer
)
171 || current_buffer
!= XBUFFER (last_undo_buffer
))
173 XSETBUFFER (last_undo_buffer
, current_buffer
);
175 current_buffer
->undo_list
176 = Fcons (Fcons (marker
, make_number (adjustment
)),
177 current_buffer
->undo_list
);
180 /* Record that a replacement is about to take place,
181 for LENGTH characters at location BEG.
182 The replacement must not change the number of characters. */
185 record_change (beg
, length
)
188 record_delete (beg
, make_buffer_string (beg
, beg
+ length
, 1));
189 record_insert (beg
, length
);
192 /* Record that an unmodified buffer is about to be changed.
193 Record the file modification date so that when undoing this entry
194 we can tell whether it is obsolete because the file was saved again. */
197 record_first_change ()
199 Lisp_Object high
, low
;
200 struct buffer
*base_buffer
= current_buffer
;
202 if (EQ (current_buffer
->undo_list
, Qt
))
205 if (!BUFFERP (last_undo_buffer
)
206 || current_buffer
!= XBUFFER (last_undo_buffer
))
208 XSETBUFFER (last_undo_buffer
, current_buffer
);
210 if (base_buffer
->base_buffer
)
211 base_buffer
= base_buffer
->base_buffer
;
213 XSETFASTINT (high
, (base_buffer
->modtime
>> 16) & 0xffff);
214 XSETFASTINT (low
, base_buffer
->modtime
& 0xffff);
215 current_buffer
->undo_list
= Fcons (Fcons (Qt
, Fcons (high
, low
)), current_buffer
->undo_list
);
218 /* Record a change in property PROP (whose old value was VAL)
219 for LENGTH characters starting at position BEG in BUFFER. */
222 record_property_change (beg
, length
, prop
, value
, buffer
)
224 Lisp_Object prop
, value
, buffer
;
226 Lisp_Object lbeg
, lend
, entry
;
227 struct buffer
*obuf
= current_buffer
;
230 if (EQ (XBUFFER (buffer
)->undo_list
, Qt
))
233 /* Allocate a cons cell to be the undo boundary after this command. */
234 if (NILP (pending_boundary
))
235 pending_boundary
= Fcons (Qnil
, Qnil
);
237 if (!EQ (buffer
, last_undo_buffer
))
239 last_undo_buffer
= buffer
;
241 /* Switch temporarily to the buffer that was changed. */
242 current_buffer
= XBUFFER (buffer
);
247 if (MODIFF
<= SAVE_MODIFF
)
248 record_first_change ();
251 XSETINT (lend
, beg
+ length
);
252 entry
= Fcons (Qnil
, Fcons (prop
, Fcons (value
, Fcons (lbeg
, lend
))));
253 current_buffer
->undo_list
= Fcons (entry
, current_buffer
->undo_list
);
255 current_buffer
= obuf
;
258 DEFUN ("undo-boundary", Fundo_boundary
, Sundo_boundary
, 0, 0, 0,
259 "Mark a boundary between units of undo.\n\
260 An undo command will stop at this point,\n\
261 but another undo command will undo to the previous boundary.")
265 if (EQ (current_buffer
->undo_list
, Qt
))
267 tem
= Fcar (current_buffer
->undo_list
);
270 /* One way or another, cons nil onto the front of the undo list. */
271 if (!NILP (pending_boundary
))
273 /* If we have preallocated the cons cell to use here,
275 XCDR (pending_boundary
) = current_buffer
->undo_list
;
276 current_buffer
->undo_list
= pending_boundary
;
277 pending_boundary
= Qnil
;
280 current_buffer
->undo_list
= Fcons (Qnil
, current_buffer
->undo_list
);
285 /* At garbage collection time, make an undo list shorter at the end,
286 returning the truncated list.
287 MINSIZE and MAXSIZE are the limits on size allowed, as described below.
288 In practice, these are the values of undo-limit and
289 undo-strong-limit. */
292 truncate_undo_list (list
, minsize
, maxsize
)
294 int minsize
, maxsize
;
296 Lisp_Object prev
, next
, last_boundary
;
301 last_boundary
= Qnil
;
303 /* Always preserve at least the most recent undo record.
304 If the first element is an undo boundary, skip past it.
306 Skip, skip, skip the undo, skip, skip, skip the undo,
307 Skip, skip, skip the undo, skip to the undo bound'ry.
308 (Get it? "Skip to my Loo?") */
309 if (CONSP (next
) && NILP (XCAR (next
)))
311 /* Add in the space occupied by this element and its chain link. */
312 size_so_far
+= sizeof (struct Lisp_Cons
);
314 /* Advance to next element. */
318 while (CONSP (next
) && ! NILP (XCAR (next
)))
323 /* Add in the space occupied by this element and its chain link. */
324 size_so_far
+= sizeof (struct Lisp_Cons
);
327 size_so_far
+= sizeof (struct Lisp_Cons
);
328 if (STRINGP (XCAR (elt
)))
329 size_so_far
+= (sizeof (struct Lisp_String
) - 1
330 + XSTRING (XCAR (elt
))->size
);
333 /* Advance to next element. */
338 last_boundary
= prev
;
345 /* When we get to a boundary, decide whether to truncate
346 either before or after it. The lower threshold, MINSIZE,
347 tells us to truncate after it. If its size pushes past
348 the higher threshold MAXSIZE as well, we truncate before it. */
351 if (size_so_far
> maxsize
)
353 last_boundary
= prev
;
354 if (size_so_far
> minsize
)
358 /* Add in the space occupied by this element and its chain link. */
359 size_so_far
+= sizeof (struct Lisp_Cons
);
362 size_so_far
+= sizeof (struct Lisp_Cons
);
363 if (STRINGP (XCAR (elt
)))
364 size_so_far
+= (sizeof (struct Lisp_String
) - 1
365 + XSTRING (XCAR (elt
))->size
);
368 /* Advance to next element. */
373 /* If we scanned the whole list, it is short enough; don't change it. */
377 /* Truncate at the boundary where we decided to truncate. */
378 if (!NILP (last_boundary
))
380 XCDR (last_boundary
) = Qnil
;
387 DEFUN ("primitive-undo", Fprimitive_undo
, Sprimitive_undo
, 2, 2, 0,
388 "Undo N records from the front of the list LIST.\n\
389 Return what remains of the list.")
393 struct gcpro gcpro1
, gcpro2
;
395 int count
= specpdl_ptr
- specpdl
;
397 #if 0 /* This is a good feature, but would make undo-start
398 unable to do what is expected. */
401 /* If the head of the list is a boundary, it is the boundary
402 preceding this command. Get rid of it and don't count it. */
413 /* Don't let read-only properties interfere with undo. */
414 if (NILP (current_buffer
->read_only
))
415 specbind (Qinhibit_read_only
, Qt
);
423 /* Exit inner loop at undo boundary. */
426 /* Handle an integer by setting point to that value. */
428 SET_PT (clip_to_bounds (BEGV
, XINT (next
), ZV
));
429 else if (CONSP (next
))
431 Lisp_Object car
, cdr
;
437 /* Element (t high . low) records previous modtime. */
438 Lisp_Object high
, low
;
440 struct buffer
*base_buffer
= current_buffer
;
444 mod_time
= (XFASTINT (high
) << 16) + XFASTINT (low
);
446 if (current_buffer
->base_buffer
)
447 base_buffer
= current_buffer
->base_buffer
;
449 /* If this records an obsolete save
450 (not matching the actual disk file)
451 then don't mark unmodified. */
452 if (mod_time
!= base_buffer
->modtime
)
454 #ifdef CLASH_DETECTION
456 #endif /* CLASH_DETECTION */
457 Fset_buffer_modified_p (Qnil
);
459 else if (EQ (car
, Qnil
))
461 /* Element (nil prop val beg . end) is property change. */
462 Lisp_Object beg
, end
, prop
, val
;
471 Fput_text_property (beg
, end
, prop
, val
, Qnil
);
473 else if (INTEGERP (car
) && INTEGERP (cdr
))
475 /* Element (BEG . END) means range was inserted. */
478 if (XINT (car
) < BEGV
480 error ("Changes to be undone are outside visible portion of buffer");
481 /* Set point first thing, so that undoing this undo
482 does not send point back to where it is now. */
484 Fdelete_region (car
, cdr
);
486 else if (STRINGP (car
) && INTEGERP (cdr
))
488 /* Element (STRING . POS) means STRING was deleted. */
490 int pos
= XINT (cdr
);
495 if (-pos
< BEGV
|| -pos
> ZV
)
496 error ("Changes to be undone are outside visible portion of buffer");
498 Finsert (1, &membuf
);
502 if (pos
< BEGV
|| pos
> ZV
)
503 error ("Changes to be undone are outside visible portion of buffer");
506 /* Now that we record marker adjustments
507 (caused by deletion) for undo,
508 we should always insert after markers,
509 so that undoing the marker adjustments
510 put the markers back in the right place. */
511 Finsert (1, &membuf
);
515 else if (MARKERP (car
) && INTEGERP (cdr
))
517 /* (MARKER . INTEGER) means a marker MARKER
518 was adjusted by INTEGER. */
519 if (XMARKER (car
)->buffer
)
521 make_number (marker_position (car
) - XINT (cdr
)),
522 Fmarker_buffer (car
));
530 return unbind_to (count
, list
);
536 Qinhibit_read_only
= intern ("inhibit-read-only");
537 staticpro (&Qinhibit_read_only
);
539 pending_boundary
= Qnil
;
540 staticpro (&pending_boundary
);
542 defsubr (&Sprimitive_undo
);
543 defsubr (&Sundo_boundary
);