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.) */
44 record_insert (beg
, length
)
47 Lisp_Object lbeg
, lend
;
49 if (EQ (current_buffer
->undo_list
, Qt
))
52 /* Allocate a cons cell to be the undo boundary after this command. */
53 if (NILP (pending_boundary
))
54 pending_boundary
= Fcons (Qnil
, Qnil
);
56 if (current_buffer
!= XBUFFER (last_undo_buffer
))
58 XSETBUFFER (last_undo_buffer
, current_buffer
);
60 if (MODIFF
<= SAVE_MODIFF
)
61 record_first_change ();
63 /* If this is following another insertion and consecutive with it
64 in the buffer, combine the two. */
65 if (CONSP (current_buffer
->undo_list
))
68 elt
= XCONS (current_buffer
->undo_list
)->car
;
70 && INTEGERP (XCONS (elt
)->car
)
71 && INTEGERP (XCONS (elt
)->cdr
)
72 && XINT (XCONS (elt
)->cdr
) == beg
)
74 XSETINT (XCONS (elt
)->cdr
, beg
+ length
);
79 XSETFASTINT (lbeg
, beg
);
80 XSETINT (lend
, beg
+ length
);
81 current_buffer
->undo_list
= Fcons (Fcons (lbeg
, lend
),
82 current_buffer
->undo_list
);
85 /* Record that a deletion is about to take place,
86 for LENGTH characters at location BEG. */
88 record_delete (beg
, length
)
91 Lisp_Object lbeg
, lend
, sbeg
;
94 if (EQ (current_buffer
->undo_list
, Qt
))
97 /* Allocate a cons cell to be the undo boundary after this command. */
98 if (NILP (pending_boundary
))
99 pending_boundary
= Fcons (Qnil
, Qnil
);
101 if (current_buffer
!= XBUFFER (last_undo_buffer
))
103 XSETBUFFER (last_undo_buffer
, current_buffer
);
105 at_boundary
= (CONSP (current_buffer
->undo_list
)
106 && NILP (XCONS (current_buffer
->undo_list
)->car
));
108 if (MODIFF
<= SAVE_MODIFF
)
109 record_first_change ();
111 if (point
== beg
+ length
)
112 XSETINT (sbeg
, -beg
);
114 XSETFASTINT (sbeg
, beg
);
115 XSETFASTINT (lbeg
, beg
);
116 XSETFASTINT (lend
, beg
+ length
);
118 /* If we are just after an undo boundary, and
119 point wasn't at start of deleted range, record where it was. */
121 && last_point_position
!= XFASTINT (sbeg
)
122 && current_buffer
== XBUFFER (last_point_position_buffer
))
123 current_buffer
->undo_list
124 = Fcons (make_number (last_point_position
), current_buffer
->undo_list
);
126 current_buffer
->undo_list
127 = Fcons (Fcons (Fbuffer_substring (lbeg
, lend
), sbeg
),
128 current_buffer
->undo_list
);
131 /* Record the fact that MARKER is about to be adjusted by ADJUSTMENT.
132 This is done only when a marker points within text being deleted,
133 because that's the only case where an automatic marker adjustment
134 won't be inverted automatically by undoing the buffer modification. */
136 record_marker_adjustment (marker
, adjustment
)
140 if (EQ (current_buffer
->undo_list
, Qt
))
143 /* Allocate a cons cell to be the undo boundary after this command. */
144 if (NILP (pending_boundary
))
145 pending_boundary
= Fcons (Qnil
, Qnil
);
147 if (current_buffer
!= XBUFFER (last_undo_buffer
))
149 XSETBUFFER (last_undo_buffer
, current_buffer
);
151 current_buffer
->undo_list
152 = Fcons (Fcons (marker
, make_number (adjustment
)),
153 current_buffer
->undo_list
);
156 /* Record that a replacement is about to take place,
157 for LENGTH characters at location BEG.
158 The replacement does not change the number of characters. */
160 record_change (beg
, length
)
163 record_delete (beg
, length
);
164 record_insert (beg
, length
);
167 /* Record that an unmodified buffer is about to be changed.
168 Record the file modification date so that when undoing this entry
169 we can tell whether it is obsolete because the file was saved again. */
171 record_first_change ()
173 Lisp_Object high
, low
;
174 struct buffer
*base_buffer
= current_buffer
;
176 if (EQ (current_buffer
->undo_list
, Qt
))
179 if (current_buffer
!= XBUFFER (last_undo_buffer
))
181 XSETBUFFER (last_undo_buffer
, current_buffer
);
183 if (base_buffer
->base_buffer
)
184 base_buffer
= base_buffer
->base_buffer
;
186 XSETFASTINT (high
, (base_buffer
->modtime
>> 16) & 0xffff);
187 XSETFASTINT (low
, base_buffer
->modtime
& 0xffff);
188 current_buffer
->undo_list
= Fcons (Fcons (Qt
, Fcons (high
, low
)), current_buffer
->undo_list
);
191 /* Record a change in property PROP (whose old value was VAL)
192 for LENGTH characters starting at position BEG in BUFFER. */
194 record_property_change (beg
, length
, prop
, value
, buffer
)
196 Lisp_Object prop
, value
, buffer
;
198 Lisp_Object lbeg
, lend
, entry
;
199 struct buffer
*obuf
= current_buffer
;
202 if (EQ (XBUFFER (buffer
)->undo_list
, Qt
))
205 /* Allocate a cons cell to be the undo boundary after this command. */
206 if (NILP (pending_boundary
))
207 pending_boundary
= Fcons (Qnil
, Qnil
);
209 if (!EQ (buffer
, last_undo_buffer
))
211 last_undo_buffer
= buffer
;
213 /* Switch temporarily to the buffer that was changed. */
214 current_buffer
= XBUFFER (buffer
);
219 if (MODIFF
<= SAVE_MODIFF
)
220 record_first_change ();
223 XSETINT (lend
, beg
+ length
);
224 entry
= Fcons (Qnil
, Fcons (prop
, Fcons (value
, Fcons (lbeg
, lend
))));
225 current_buffer
->undo_list
= Fcons (entry
, current_buffer
->undo_list
);
227 current_buffer
= obuf
;
230 DEFUN ("undo-boundary", Fundo_boundary
, Sundo_boundary
, 0, 0, 0,
231 "Mark a boundary between units of undo.\n\
232 An undo command will stop at this point,\n\
233 but another undo command will undo to the previous boundary.")
237 if (EQ (current_buffer
->undo_list
, Qt
))
239 tem
= Fcar (current_buffer
->undo_list
);
242 /* One way or another, cons nil onto the front of the undo list. */
243 if (!NILP (pending_boundary
))
245 /* If we have preallocated the cons cell to use here,
247 XCONS (pending_boundary
)->cdr
= current_buffer
->undo_list
;
248 current_buffer
->undo_list
= pending_boundary
;
249 pending_boundary
= Qnil
;
252 current_buffer
->undo_list
= Fcons (Qnil
, current_buffer
->undo_list
);
257 /* At garbage collection time, make an undo list shorter at the end,
258 returning the truncated list.
259 MINSIZE and MAXSIZE are the limits on size allowed, as described below.
260 In practice, these are the values of undo-limit and
261 undo-strong-limit. */
264 truncate_undo_list (list
, minsize
, maxsize
)
266 int minsize
, maxsize
;
268 Lisp_Object prev
, next
, last_boundary
;
273 last_boundary
= Qnil
;
275 /* Always preserve at least the most recent undo record.
276 If the first element is an undo boundary, skip past it.
278 Skip, skip, skip the undo, skip, skip, skip the undo,
279 Skip, skip, skip the undo, skip to the undo bound'ry.
280 (Get it? "Skip to my Loo?") */
281 if (CONSP (next
) && NILP (XCONS (next
)->car
))
283 /* Add in the space occupied by this element and its chain link. */
284 size_so_far
+= sizeof (struct Lisp_Cons
);
286 /* Advance to next element. */
288 next
= XCONS (next
)->cdr
;
290 while (CONSP (next
) && ! NILP (XCONS (next
)->car
))
293 elt
= XCONS (next
)->car
;
295 /* Add in the space occupied by this element and its chain link. */
296 size_so_far
+= sizeof (struct Lisp_Cons
);
299 size_so_far
+= sizeof (struct Lisp_Cons
);
300 if (STRINGP (XCONS (elt
)->car
))
301 size_so_far
+= (sizeof (struct Lisp_String
) - 1
302 + XSTRING (XCONS (elt
)->car
)->size
);
305 /* Advance to next element. */
307 next
= XCONS (next
)->cdr
;
310 last_boundary
= prev
;
315 elt
= XCONS (next
)->car
;
317 /* When we get to a boundary, decide whether to truncate
318 either before or after it. The lower threshold, MINSIZE,
319 tells us to truncate after it. If its size pushes past
320 the higher threshold MAXSIZE as well, we truncate before it. */
323 if (size_so_far
> maxsize
)
325 last_boundary
= prev
;
326 if (size_so_far
> minsize
)
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 (XCONS (elt
)->car
))
336 size_so_far
+= (sizeof (struct Lisp_String
) - 1
337 + XSTRING (XCONS (elt
)->car
)->size
);
340 /* Advance to next element. */
342 next
= XCONS (next
)->cdr
;
345 /* If we scanned the whole list, it is short enough; don't change it. */
349 /* Truncate at the boundary where we decided to truncate. */
350 if (!NILP (last_boundary
))
352 XCONS (last_boundary
)->cdr
= Qnil
;
359 DEFUN ("primitive-undo", Fprimitive_undo
, Sprimitive_undo
, 2, 2, 0,
360 "Undo N records from the front of the list LIST.\n\
361 Return what remains of the list.")
365 struct gcpro gcpro1
, gcpro2
;
367 int count
= specpdl_ptr
- specpdl
;
369 #if 0 /* This is a good feature, but would make undo-start
370 unable to do what is expected. */
373 /* If the head of the list is a boundary, it is the boundary
374 preceding this command. Get rid of it and don't count it. */
385 /* Don't let read-only properties interfere with undo. */
386 if (NILP (current_buffer
->read_only
))
387 specbind (Qinhibit_read_only
, Qt
);
395 /* Exit inner loop at undo boundary. */
398 /* Handle an integer by setting point to that value. */
400 SET_PT (clip_to_bounds (BEGV
, XINT (next
), ZV
));
401 else if (CONSP (next
))
403 Lisp_Object car
, cdr
;
409 /* Element (t high . low) records previous modtime. */
410 Lisp_Object high
, low
;
412 struct buffer
*base_buffer
= current_buffer
;
416 mod_time
= (XFASTINT (high
) << 16) + XFASTINT (low
);
418 if (current_buffer
->base_buffer
)
419 base_buffer
= current_buffer
->base_buffer
;
421 /* If this records an obsolete save
422 (not matching the actual disk file)
423 then don't mark unmodified. */
424 if (mod_time
!= base_buffer
->modtime
)
426 #ifdef CLASH_DETECTION
428 #endif /* CLASH_DETECTION */
429 Fset_buffer_modified_p (Qnil
);
431 #ifdef USE_TEXT_PROPERTIES
432 else if (EQ (car
, Qnil
))
434 /* Element (nil prop val beg . end) is property change. */
435 Lisp_Object beg
, end
, prop
, val
;
444 Fput_text_property (beg
, end
, prop
, val
, Qnil
);
446 #endif /* USE_TEXT_PROPERTIES */
447 else if (INTEGERP (car
) && INTEGERP (cdr
))
449 /* Element (BEG . END) means range was inserted. */
452 if (XINT (car
) < BEGV
454 error ("Changes to be undone are outside visible portion of buffer");
455 /* Set point first thing, so that undoing this undo
456 does not send point back to where it is now. */
458 Fdelete_region (car
, cdr
);
460 else if (STRINGP (car
) && INTEGERP (cdr
))
462 /* Element (STRING . POS) means STRING was deleted. */
464 int pos
= XINT (cdr
);
469 if (-pos
< BEGV
|| -pos
> ZV
)
470 error ("Changes to be undone are outside visible portion of buffer");
472 Finsert (1, &membuf
);
476 if (pos
< BEGV
|| pos
> ZV
)
477 error ("Changes to be undone are outside visible portion of buffer");
480 /* Insert before markers so that if the mark is
481 currently on the boundary of this deletion, it
482 ends up on the other side of the now-undeleted
483 text from point. Since undo doesn't even keep
484 track of the mark, this isn't really necessary,
485 but it may lead to better behavior in certain
487 Finsert_before_markers (1, &membuf
);
491 else if (MARKERP (car
) && INTEGERP (cdr
))
493 /* (MARKER . INTEGER) means a marker MARKER
494 was adjusted by INTEGER. */
495 if (XMARKER (car
)->buffer
)
497 make_number (marker_position (car
) - XINT (cdr
)),
498 Fmarker_buffer (car
));
506 return unbind_to (count
, list
);
511 Qinhibit_read_only
= intern ("inhibit-read-only");
512 staticpro (&Qinhibit_read_only
);
514 pending_boundary
= Qnil
;
515 staticpro (&pending_boundary
);
517 defsubr (&Sprimitive_undo
);
518 defsubr (&Sundo_boundary
);