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 (current_buffer
!= XBUFFER (last_undo_buffer
))
59 XSETBUFFER (last_undo_buffer
, current_buffer
);
61 if (MODIFF
<= SAVE_MODIFF
)
62 record_first_change ();
64 /* If this is following another insertion and consecutive with it
65 in the buffer, combine the two. */
66 if (CONSP (current_buffer
->undo_list
))
69 elt
= XCONS (current_buffer
->undo_list
)->car
;
71 && INTEGERP (XCONS (elt
)->car
)
72 && INTEGERP (XCONS (elt
)->cdr
)
73 && XINT (XCONS (elt
)->cdr
) == beg
)
75 XSETINT (XCONS (elt
)->cdr
, beg
+ length
);
80 XSETFASTINT (lbeg
, beg
);
81 XSETINT (lend
, beg
+ length
);
82 current_buffer
->undo_list
= Fcons (Fcons (lbeg
, lend
),
83 current_buffer
->undo_list
);
86 /* Record that a deletion is about to take place,
87 of the characters in STRING, at location BEG. */
90 record_delete (beg
, string
)
97 if (EQ (current_buffer
->undo_list
, Qt
))
100 /* Allocate a cons cell to be the undo boundary after this command. */
101 if (NILP (pending_boundary
))
102 pending_boundary
= Fcons (Qnil
, Qnil
);
104 if (current_buffer
!= XBUFFER (last_undo_buffer
))
106 XSETBUFFER (last_undo_buffer
, current_buffer
);
108 at_boundary
= (CONSP (current_buffer
->undo_list
)
109 && NILP (XCONS (current_buffer
->undo_list
)->car
));
111 if (MODIFF
<= SAVE_MODIFF
)
112 record_first_change ();
114 if (PT
== beg
+ XSTRING (string
)->size
)
115 XSETINT (sbeg
, -beg
);
117 XSETFASTINT (sbeg
, beg
);
119 /* If we are just after an undo boundary, and
120 point wasn't at start of deleted range, record where it was. */
122 && last_point_position
!= XFASTINT (sbeg
)
123 && current_buffer
== XBUFFER (last_point_position_buffer
))
124 current_buffer
->undo_list
125 = Fcons (make_number (last_point_position
), current_buffer
->undo_list
);
127 current_buffer
->undo_list
128 = Fcons (Fcons (string
, sbeg
), 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. */
137 record_marker_adjustment (marker
, adjustment
)
141 if (EQ (current_buffer
->undo_list
, Qt
))
144 /* Allocate a cons cell to be the undo boundary after this command. */
145 if (NILP (pending_boundary
))
146 pending_boundary
= Fcons (Qnil
, Qnil
);
148 if (current_buffer
!= XBUFFER (last_undo_buffer
))
150 XSETBUFFER (last_undo_buffer
, current_buffer
);
152 current_buffer
->undo_list
153 = Fcons (Fcons (marker
, make_number (adjustment
)),
154 current_buffer
->undo_list
);
157 /* Record that a replacement is about to take place,
158 for LENGTH characters at location BEG.
159 The replacement must not change the number of characters. */
162 record_change (beg
, length
)
165 record_delete (beg
, make_buffer_string (beg
, beg
+ length
, 1));
166 record_insert (beg
, length
);
169 /* Record that an unmodified buffer is about to be changed.
170 Record the file modification date so that when undoing this entry
171 we can tell whether it is obsolete because the file was saved again. */
174 record_first_change ()
176 Lisp_Object high
, low
;
177 struct buffer
*base_buffer
= current_buffer
;
179 if (EQ (current_buffer
->undo_list
, Qt
))
182 if (current_buffer
!= XBUFFER (last_undo_buffer
))
184 XSETBUFFER (last_undo_buffer
, current_buffer
);
186 if (base_buffer
->base_buffer
)
187 base_buffer
= base_buffer
->base_buffer
;
189 XSETFASTINT (high
, (base_buffer
->modtime
>> 16) & 0xffff);
190 XSETFASTINT (low
, base_buffer
->modtime
& 0xffff);
191 current_buffer
->undo_list
= Fcons (Fcons (Qt
, Fcons (high
, low
)), current_buffer
->undo_list
);
194 /* Record a change in property PROP (whose old value was VAL)
195 for LENGTH characters starting at position BEG in BUFFER. */
198 record_property_change (beg
, length
, prop
, value
, buffer
)
200 Lisp_Object prop
, value
, buffer
;
202 Lisp_Object lbeg
, lend
, entry
;
203 struct buffer
*obuf
= current_buffer
;
206 if (EQ (XBUFFER (buffer
)->undo_list
, Qt
))
209 /* Allocate a cons cell to be the undo boundary after this command. */
210 if (NILP (pending_boundary
))
211 pending_boundary
= Fcons (Qnil
, Qnil
);
213 if (!EQ (buffer
, last_undo_buffer
))
215 last_undo_buffer
= buffer
;
217 /* Switch temporarily to the buffer that was changed. */
218 current_buffer
= XBUFFER (buffer
);
223 if (MODIFF
<= SAVE_MODIFF
)
224 record_first_change ();
227 XSETINT (lend
, beg
+ length
);
228 entry
= Fcons (Qnil
, Fcons (prop
, Fcons (value
, Fcons (lbeg
, lend
))));
229 current_buffer
->undo_list
= Fcons (entry
, current_buffer
->undo_list
);
231 current_buffer
= obuf
;
234 DEFUN ("undo-boundary", Fundo_boundary
, Sundo_boundary
, 0, 0, 0,
235 "Mark a boundary between units of undo.\n\
236 An undo command will stop at this point,\n\
237 but another undo command will undo to the previous boundary.")
241 if (EQ (current_buffer
->undo_list
, Qt
))
243 tem
= Fcar (current_buffer
->undo_list
);
246 /* One way or another, cons nil onto the front of the undo list. */
247 if (!NILP (pending_boundary
))
249 /* If we have preallocated the cons cell to use here,
251 XCONS (pending_boundary
)->cdr
= current_buffer
->undo_list
;
252 current_buffer
->undo_list
= pending_boundary
;
253 pending_boundary
= Qnil
;
256 current_buffer
->undo_list
= Fcons (Qnil
, current_buffer
->undo_list
);
261 /* At garbage collection time, make an undo list shorter at the end,
262 returning the truncated list.
263 MINSIZE and MAXSIZE are the limits on size allowed, as described below.
264 In practice, these are the values of undo-limit and
265 undo-strong-limit. */
268 truncate_undo_list (list
, minsize
, maxsize
)
270 int minsize
, maxsize
;
272 Lisp_Object prev
, next
, last_boundary
;
277 last_boundary
= Qnil
;
279 /* Always preserve at least the most recent undo record.
280 If the first element is an undo boundary, skip past it.
282 Skip, skip, skip the undo, skip, skip, skip the undo,
283 Skip, skip, skip the undo, skip to the undo bound'ry.
284 (Get it? "Skip to my Loo?") */
285 if (CONSP (next
) && NILP (XCONS (next
)->car
))
287 /* Add in the space occupied by this element and its chain link. */
288 size_so_far
+= sizeof (struct Lisp_Cons
);
290 /* Advance to next element. */
292 next
= XCONS (next
)->cdr
;
294 while (CONSP (next
) && ! NILP (XCONS (next
)->car
))
297 elt
= XCONS (next
)->car
;
299 /* Add in the space occupied by this element and its chain link. */
300 size_so_far
+= sizeof (struct Lisp_Cons
);
303 size_so_far
+= sizeof (struct Lisp_Cons
);
304 if (STRINGP (XCONS (elt
)->car
))
305 size_so_far
+= (sizeof (struct Lisp_String
) - 1
306 + XSTRING (XCONS (elt
)->car
)->size
);
309 /* Advance to next element. */
311 next
= XCONS (next
)->cdr
;
314 last_boundary
= prev
;
319 elt
= XCONS (next
)->car
;
321 /* When we get to a boundary, decide whether to truncate
322 either before or after it. The lower threshold, MINSIZE,
323 tells us to truncate after it. If its size pushes past
324 the higher threshold MAXSIZE as well, we truncate before it. */
327 if (size_so_far
> maxsize
)
329 last_boundary
= prev
;
330 if (size_so_far
> minsize
)
334 /* Add in the space occupied by this element and its chain link. */
335 size_so_far
+= sizeof (struct Lisp_Cons
);
338 size_so_far
+= sizeof (struct Lisp_Cons
);
339 if (STRINGP (XCONS (elt
)->car
))
340 size_so_far
+= (sizeof (struct Lisp_String
) - 1
341 + XSTRING (XCONS (elt
)->car
)->size
);
344 /* Advance to next element. */
346 next
= XCONS (next
)->cdr
;
349 /* If we scanned the whole list, it is short enough; don't change it. */
353 /* Truncate at the boundary where we decided to truncate. */
354 if (!NILP (last_boundary
))
356 XCONS (last_boundary
)->cdr
= Qnil
;
363 DEFUN ("primitive-undo", Fprimitive_undo
, Sprimitive_undo
, 2, 2, 0,
364 "Undo N records from the front of the list LIST.\n\
365 Return what remains of the list.")
369 struct gcpro gcpro1
, gcpro2
;
371 int count
= specpdl_ptr
- specpdl
;
373 #if 0 /* This is a good feature, but would make undo-start
374 unable to do what is expected. */
377 /* If the head of the list is a boundary, it is the boundary
378 preceding this command. Get rid of it and don't count it. */
389 /* Don't let read-only properties interfere with undo. */
390 if (NILP (current_buffer
->read_only
))
391 specbind (Qinhibit_read_only
, Qt
);
399 /* Exit inner loop at undo boundary. */
402 /* Handle an integer by setting point to that value. */
404 SET_PT (clip_to_bounds (BEGV
, XINT (next
), ZV
));
405 else if (CONSP (next
))
407 Lisp_Object car
, cdr
;
413 /* Element (t high . low) records previous modtime. */
414 Lisp_Object high
, low
;
416 struct buffer
*base_buffer
= current_buffer
;
420 mod_time
= (XFASTINT (high
) << 16) + XFASTINT (low
);
422 if (current_buffer
->base_buffer
)
423 base_buffer
= current_buffer
->base_buffer
;
425 /* If this records an obsolete save
426 (not matching the actual disk file)
427 then don't mark unmodified. */
428 if (mod_time
!= base_buffer
->modtime
)
430 #ifdef CLASH_DETECTION
432 #endif /* CLASH_DETECTION */
433 Fset_buffer_modified_p (Qnil
);
435 #ifdef USE_TEXT_PROPERTIES
436 else if (EQ (car
, Qnil
))
438 /* Element (nil prop val beg . end) is property change. */
439 Lisp_Object beg
, end
, prop
, val
;
448 Fput_text_property (beg
, end
, prop
, val
, Qnil
);
450 #endif /* USE_TEXT_PROPERTIES */
451 else if (INTEGERP (car
) && INTEGERP (cdr
))
453 /* Element (BEG . END) means range was inserted. */
456 if (XINT (car
) < BEGV
458 error ("Changes to be undone are outside visible portion of buffer");
459 /* Set point first thing, so that undoing this undo
460 does not send point back to where it is now. */
462 Fdelete_region (car
, cdr
);
464 else if (STRINGP (car
) && INTEGERP (cdr
))
466 /* Element (STRING . POS) means STRING was deleted. */
468 int pos
= XINT (cdr
);
473 if (-pos
< BEGV
|| -pos
> ZV
)
474 error ("Changes to be undone are outside visible portion of buffer");
476 Finsert (1, &membuf
);
480 if (pos
< BEGV
|| pos
> ZV
)
481 error ("Changes to be undone are outside visible portion of buffer");
484 /* Now that we record marker adjustments
485 (caused by deletion) for undo,
486 we should always insert after markers,
487 so that undoing the marker adjustments
488 put the markers back in the right place. */
489 Finsert (1, &membuf
);
493 else if (MARKERP (car
) && INTEGERP (cdr
))
495 /* (MARKER . INTEGER) means a marker MARKER
496 was adjusted by INTEGER. */
497 if (XMARKER (car
)->buffer
)
499 make_number (marker_position (car
) - XINT (cdr
)),
500 Fmarker_buffer (car
));
508 return unbind_to (count
, list
);
513 Qinhibit_read_only
= intern ("inhibit-read-only");
514 staticpro (&Qinhibit_read_only
);
516 pending_boundary
= Qnil
;
517 staticpro (&pending_boundary
);
519 defsubr (&Sprimitive_undo
);
520 defsubr (&Sundo_boundary
);