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 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 XSETCDR (elt
, make_number (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 (BUFFERP (last_undo_buffer
)
106 && current_buffer
!= XBUFFER (last_undo_buffer
))
108 XSETBUFFER (last_undo_buffer
, current_buffer
);
110 if (CONSP (current_buffer
->undo_list
))
112 /* Set AT_BOUNDARY to 1 only when we have nothing other than
113 marker adjustment before undo boundary. */
115 Lisp_Object tail
= current_buffer
->undo_list
, elt
;
123 if (NILP (elt
) || ! (CONSP (elt
) && MARKERP (XCAR (elt
))))
127 at_boundary
= NILP (elt
);
132 if (MODIFF
<= SAVE_MODIFF
)
133 record_first_change ();
135 if (PT
== beg
+ XSTRING (string
)->size
)
136 XSETINT (sbeg
, -beg
);
138 XSETFASTINT (sbeg
, beg
);
140 /* If we are just after an undo boundary, and
141 point wasn't at start of deleted range, record where it was. */
143 && last_point_position
!= XFASTINT (sbeg
)
144 /* If we're called from batch mode, this could be nil. */
145 && BUFFERP (last_point_position_buffer
)
146 && current_buffer
== XBUFFER (last_point_position_buffer
))
147 current_buffer
->undo_list
148 = Fcons (make_number (last_point_position
), current_buffer
->undo_list
);
150 current_buffer
->undo_list
151 = Fcons (Fcons (string
, sbeg
), current_buffer
->undo_list
);
154 /* Record the fact that MARKER is about to be adjusted by ADJUSTMENT.
155 This is done only when a marker points within text being deleted,
156 because that's the only case where an automatic marker adjustment
157 won't be inverted automatically by undoing the buffer modification. */
160 record_marker_adjustment (marker
, adjustment
)
164 if (EQ (current_buffer
->undo_list
, Qt
))
167 /* Allocate a cons cell to be the undo boundary after this command. */
168 if (NILP (pending_boundary
))
169 pending_boundary
= Fcons (Qnil
, Qnil
);
171 if (!BUFFERP (last_undo_buffer
)
172 || current_buffer
!= XBUFFER (last_undo_buffer
))
174 XSETBUFFER (last_undo_buffer
, current_buffer
);
176 current_buffer
->undo_list
177 = Fcons (Fcons (marker
, make_number (adjustment
)),
178 current_buffer
->undo_list
);
181 /* Record that a replacement is about to take place,
182 for LENGTH characters at location BEG.
183 The replacement must not change the number of characters. */
186 record_change (beg
, length
)
189 record_delete (beg
, make_buffer_string (beg
, beg
+ length
, 1));
190 record_insert (beg
, length
);
193 /* Record that an unmodified buffer is about to be changed.
194 Record the file modification date so that when undoing this entry
195 we can tell whether it is obsolete because the file was saved again. */
198 record_first_change ()
200 Lisp_Object high
, low
;
201 struct buffer
*base_buffer
= current_buffer
;
203 if (EQ (current_buffer
->undo_list
, Qt
))
206 if (!BUFFERP (last_undo_buffer
)
207 || current_buffer
!= XBUFFER (last_undo_buffer
))
209 XSETBUFFER (last_undo_buffer
, current_buffer
);
211 if (base_buffer
->base_buffer
)
212 base_buffer
= base_buffer
->base_buffer
;
214 XSETFASTINT (high
, (base_buffer
->modtime
>> 16) & 0xffff);
215 XSETFASTINT (low
, base_buffer
->modtime
& 0xffff);
216 current_buffer
->undo_list
= Fcons (Fcons (Qt
, Fcons (high
, low
)), current_buffer
->undo_list
);
219 /* Record a change in property PROP (whose old value was VAL)
220 for LENGTH characters starting at position BEG in BUFFER. */
223 record_property_change (beg
, length
, prop
, value
, buffer
)
225 Lisp_Object prop
, value
, buffer
;
227 Lisp_Object lbeg
, lend
, entry
;
228 struct buffer
*obuf
= current_buffer
;
231 if (EQ (XBUFFER (buffer
)->undo_list
, Qt
))
234 /* Allocate a cons cell to be the undo boundary after this command. */
235 if (NILP (pending_boundary
))
236 pending_boundary
= Fcons (Qnil
, Qnil
);
238 if (!EQ (buffer
, last_undo_buffer
))
240 last_undo_buffer
= buffer
;
242 /* Switch temporarily to the buffer that was changed. */
243 current_buffer
= XBUFFER (buffer
);
248 if (MODIFF
<= SAVE_MODIFF
)
249 record_first_change ();
252 XSETINT (lend
, beg
+ length
);
253 entry
= Fcons (Qnil
, Fcons (prop
, Fcons (value
, Fcons (lbeg
, lend
))));
254 current_buffer
->undo_list
= Fcons (entry
, current_buffer
->undo_list
);
256 current_buffer
= obuf
;
259 DEFUN ("undo-boundary", Fundo_boundary
, Sundo_boundary
, 0, 0, 0,
260 doc
: /* Mark a boundary between units of undo.
261 An undo command will stop at this point,
262 but another undo command will undo to the previous boundary. */)
266 if (EQ (current_buffer
->undo_list
, Qt
))
268 tem
= Fcar (current_buffer
->undo_list
);
271 /* One way or another, cons nil onto the front of the undo list. */
272 if (!NILP (pending_boundary
))
274 /* If we have preallocated the cons cell to use here,
276 XSETCDR (pending_boundary
, current_buffer
->undo_list
);
277 current_buffer
->undo_list
= pending_boundary
;
278 pending_boundary
= Qnil
;
281 current_buffer
->undo_list
= Fcons (Qnil
, current_buffer
->undo_list
);
286 /* At garbage collection time, make an undo list shorter at the end,
287 returning the truncated list.
288 MINSIZE and MAXSIZE are the limits on size allowed, as described below.
289 In practice, these are the values of undo-limit and
290 undo-strong-limit. */
293 truncate_undo_list (list
, minsize
, maxsize
)
295 int minsize
, maxsize
;
297 Lisp_Object prev
, next
, last_boundary
;
302 last_boundary
= Qnil
;
304 /* Always preserve at least the most recent undo record.
305 If the first element is an undo boundary, skip past it.
307 Skip, skip, skip the undo, skip, skip, skip the undo,
308 Skip, skip, skip the undo, skip to the undo bound'ry.
309 (Get it? "Skip to my Loo?") */
310 if (CONSP (next
) && NILP (XCAR (next
)))
312 /* Add in the space occupied by this element and its chain link. */
313 size_so_far
+= sizeof (struct Lisp_Cons
);
315 /* Advance to next element. */
319 while (CONSP (next
) && ! NILP (XCAR (next
)))
324 /* Add in the space occupied by this element and its chain link. */
325 size_so_far
+= sizeof (struct Lisp_Cons
);
328 size_so_far
+= sizeof (struct Lisp_Cons
);
329 if (STRINGP (XCAR (elt
)))
330 size_so_far
+= (sizeof (struct Lisp_String
) - 1
331 + XSTRING (XCAR (elt
))->size
);
334 /* Advance to next element. */
339 last_boundary
= prev
;
346 /* When we get to a boundary, decide whether to truncate
347 either before or after it. The lower threshold, MINSIZE,
348 tells us to truncate after it. If its size pushes past
349 the higher threshold MAXSIZE as well, we truncate before it. */
352 if (size_so_far
> maxsize
)
354 last_boundary
= prev
;
355 if (size_so_far
> minsize
)
359 /* Add in the space occupied by this element and its chain link. */
360 size_so_far
+= sizeof (struct Lisp_Cons
);
363 size_so_far
+= sizeof (struct Lisp_Cons
);
364 if (STRINGP (XCAR (elt
)))
365 size_so_far
+= (sizeof (struct Lisp_String
) - 1
366 + XSTRING (XCAR (elt
))->size
);
369 /* Advance to next element. */
374 /* If we scanned the whole list, it is short enough; don't change it. */
378 /* Truncate at the boundary where we decided to truncate. */
379 if (!NILP (last_boundary
))
381 XSETCDR (last_boundary
, Qnil
);
388 DEFUN ("primitive-undo", Fprimitive_undo
, Sprimitive_undo
, 2, 2, 0,
389 doc
: /* Undo N records from the front of the list LIST.
390 Return what remains of the list. */)
394 struct gcpro gcpro1
, gcpro2
;
396 int count
= BINDING_STACK_SIZE ();
399 #if 0 /* This is a good feature, but would make undo-start
400 unable to do what is expected. */
403 /* If the head of the list is a boundary, it is the boundary
404 preceding this command. Get rid of it and don't count it. */
415 /* In a writable buffer, enable undoing read-only text that is so
416 because of text properties. */
417 if (NILP (current_buffer
->read_only
))
418 specbind (Qinhibit_read_only
, Qt
);
420 /* Don't let `intangible' properties interfere with undo. */
421 specbind (Qinhibit_point_motion_hooks
, Qt
);
429 /* Exit inner loop at undo boundary. */
432 /* Handle an integer by setting point to that value. */
434 SET_PT (clip_to_bounds (BEGV
, XINT (next
), ZV
));
435 else if (CONSP (next
))
437 Lisp_Object car
, cdr
;
443 /* Element (t high . low) records previous modtime. */
444 Lisp_Object high
, low
;
446 struct buffer
*base_buffer
= current_buffer
;
450 mod_time
= (XFASTINT (high
) << 16) + XFASTINT (low
);
452 if (current_buffer
->base_buffer
)
453 base_buffer
= current_buffer
->base_buffer
;
455 /* If this records an obsolete save
456 (not matching the actual disk file)
457 then don't mark unmodified. */
458 if (mod_time
!= base_buffer
->modtime
)
460 #ifdef CLASH_DETECTION
462 #endif /* CLASH_DETECTION */
463 Fset_buffer_modified_p (Qnil
);
465 else if (EQ (car
, Qnil
))
467 /* Element (nil prop val beg . end) is property change. */
468 Lisp_Object beg
, end
, prop
, val
;
477 Fput_text_property (beg
, end
, prop
, val
, Qnil
);
479 else if (INTEGERP (car
) && INTEGERP (cdr
))
481 /* Element (BEG . END) means range was inserted. */
483 if (XINT (car
) < BEGV
485 error ("Changes to be undone are outside visible portion of buffer");
486 /* Set point first thing, so that undoing this undo
487 does not send point back to where it is now. */
489 Fdelete_region (car
, cdr
);
491 else if (STRINGP (car
) && INTEGERP (cdr
))
493 /* Element (STRING . POS) means STRING was deleted. */
495 int pos
= XINT (cdr
);
500 if (-pos
< BEGV
|| -pos
> ZV
)
501 error ("Changes to be undone are outside visible portion of buffer");
503 Finsert (1, &membuf
);
507 if (pos
< BEGV
|| pos
> ZV
)
508 error ("Changes to be undone are outside visible portion of buffer");
511 /* Now that we record marker adjustments
512 (caused by deletion) for undo,
513 we should always insert after markers,
514 so that undoing the marker adjustments
515 put the markers back in the right place. */
516 Finsert (1, &membuf
);
520 else if (MARKERP (car
) && INTEGERP (cdr
))
522 /* (MARKER . INTEGER) means a marker MARKER
523 was adjusted by INTEGER. */
524 if (XMARKER (car
)->buffer
)
526 make_number (marker_position (car
) - XINT (cdr
)),
527 Fmarker_buffer (car
));
535 return unbind_to (count
, list
);
541 Qinhibit_read_only
= intern ("inhibit-read-only");
542 staticpro (&Qinhibit_read_only
);
544 pending_boundary
= Qnil
;
545 staticpro (&pending_boundary
);
547 defsubr (&Sprimitive_undo
);
548 defsubr (&Sundo_boundary
);