1 /* undo handling for GNU Emacs.
2 Copyright (C) 1990 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is distributed in the hope that it will be useful,
7 but WITHOUT ANY WARRANTY. No author or distributor
8 accepts responsibility to anyone for the consequences of using it
9 or for whether it serves any particular purpose or works at all,
10 unless he says so in writing. Refer to the GNU Emacs General Public
11 License for full details.
13 Everyone is granted permission to copy, modify and redistribute
14 GNU Emacs, but only under the conditions described in the
15 GNU Emacs General Public License. A copy of this license is
16 supposed to have been given to you along with GNU Emacs so you
17 can know your rights and responsibilities. It should be in a
18 file named COPYING. Among other things, the copyright notice
19 and this notice must be preserved on all copies. */
26 /* Last buffer for which undo information was recorded. */
27 Lisp_Object last_undo_buffer
;
29 /* Record an insertion that just happened or is about to happen,
30 for LENGTH characters at position BEG.
31 (It is possible to record an insertion before or after the fact
32 because we don't need to record the contents.) */
34 record_insert (beg
, length
)
35 Lisp_Object beg
, length
;
37 Lisp_Object lbeg
, lend
;
39 if (EQ (current_buffer
->undo_list
, Qt
))
42 if (current_buffer
!= XBUFFER (last_undo_buffer
))
44 XSET (last_undo_buffer
, Lisp_Buffer
, current_buffer
);
46 if (MODIFF
<= current_buffer
->save_modified
)
47 record_first_change ();
49 /* If this is following another insertion and consecutive with it
50 in the buffer, combine the two. */
51 if (XTYPE (current_buffer
->undo_list
) == Lisp_Cons
)
54 elt
= XCONS (current_buffer
->undo_list
)->car
;
55 if (XTYPE (elt
) == Lisp_Cons
56 && XTYPE (XCONS (elt
)->car
) == Lisp_Int
57 && XTYPE (XCONS (elt
)->cdr
) == Lisp_Int
58 && XINT (XCONS (elt
)->cdr
) == XINT (beg
))
60 XSETINT (XCONS (elt
)->cdr
, XINT (beg
) + XINT (length
));
66 XSET (lend
, Lisp_Int
, XINT (beg
) + XINT (length
));
67 current_buffer
->undo_list
= Fcons (Fcons (lbeg
, lend
),
68 current_buffer
->undo_list
);
71 /* Record that a deletion is about to take place,
72 for LENGTH characters at location BEG. */
74 record_delete (beg
, length
)
77 Lisp_Object lbeg
, lend
, sbeg
;
79 if (EQ (current_buffer
->undo_list
, Qt
))
82 if (current_buffer
!= XBUFFER (last_undo_buffer
))
84 XSET (last_undo_buffer
, Lisp_Buffer
, current_buffer
);
86 if (MODIFF
<= current_buffer
->save_modified
)
87 record_first_change ();
89 if (point
== beg
+ length
)
90 XSET (sbeg
, Lisp_Int
, -beg
);
92 XFASTINT (sbeg
) = beg
;
93 XFASTINT (lbeg
) = beg
;
94 XFASTINT (lend
) = beg
+ length
;
96 /* If point isn't at start of deleted range, record where it is. */
98 current_buffer
->undo_list
99 = Fcons (make_number (PT
), current_buffer
->undo_list
);
101 current_buffer
->undo_list
102 = Fcons (Fcons (Fbuffer_substring (lbeg
, lend
), sbeg
),
103 current_buffer
->undo_list
);
106 /* Record that a replacement is about to take place,
107 for LENGTH characters at location BEG.
108 The replacement does not change the number of characters. */
110 record_change (beg
, length
)
113 record_delete (beg
, length
);
114 record_insert (beg
, length
);
117 /* Record that an unmodified buffer is about to be changed.
118 Record the file modification date so that when undoing this entry
119 we can tell whether it is obsolete because the file was saved again. */
121 record_first_change ()
123 Lisp_Object high
, low
;
124 XFASTINT (high
) = (current_buffer
->modtime
>> 16) & 0xffff;
125 XFASTINT (low
) = current_buffer
->modtime
& 0xffff;
126 current_buffer
->undo_list
= Fcons (Fcons (Qt
, Fcons (high
, low
)), current_buffer
->undo_list
);
129 /* Record a change in property PROP (whose old value was VAL)
130 for LENGTH characters starting at position BEG in BUFFER. */
132 record_property_change (beg
, length
, prop
, value
, buffer
)
134 Lisp_Object prop
, value
, buffer
;
136 Lisp_Object lbeg
, lend
, entry
;
137 struct buffer
*obuf
= current_buffer
;
140 if (EQ (current_buffer
->undo_list
, Qt
))
143 if (!EQ (buffer
, last_undo_buffer
))
145 last_undo_buffer
= buffer
;
147 /* Switch temporarily to the buffer that was changed. */
148 current_buffer
= XBUFFER (buffer
);
153 if (MODIFF
<= current_buffer
->save_modified
)
154 record_first_change ();
156 XSET (lbeg
, Lisp_Int
, beg
);
157 XSET (lend
, Lisp_Int
, beg
+ length
);
158 entry
= Fcons (Qnil
, Fcons (prop
, Fcons (value
, Fcons (lbeg
, lend
))));
159 current_buffer
->undo_list
= Fcons (entry
, current_buffer
->undo_list
);
161 current_buffer
= obuf
;
164 DEFUN ("undo-boundary", Fundo_boundary
, Sundo_boundary
, 0, 0, 0,
165 "Mark a boundary between units of undo.\n\
166 An undo command will stop at this point,\n\
167 but another undo command will undo to the previous boundary.")
171 if (EQ (current_buffer
->undo_list
, Qt
))
173 tem
= Fcar (current_buffer
->undo_list
);
175 current_buffer
->undo_list
= Fcons (Qnil
, current_buffer
->undo_list
);
179 /* At garbage collection time, make an undo list shorter at the end,
180 returning the truncated list.
181 MINSIZE and MAXSIZE are the limits on size allowed, as described below.
182 In practice, these are the values of undo-limit and
183 undo-strong-limit. */
186 truncate_undo_list (list
, minsize
, maxsize
)
188 int minsize
, maxsize
;
190 Lisp_Object prev
, next
, last_boundary
;
195 last_boundary
= Qnil
;
197 /* Always preserve at least the most recent undo record.
198 If the first element is an undo boundary, skip past it.
200 Skip, skip, skip the undo, skip, skip, skip the undo,
201 Skip, skip, skip the undo, skip to the undo bound'ry.
202 (Get it? "Skip to my Loo?") */
203 if (XTYPE (next
) == Lisp_Cons
204 && NILP (XCONS (next
)->car
))
206 /* Add in the space occupied by this element and its chain link. */
207 size_so_far
+= sizeof (struct Lisp_Cons
);
209 /* Advance to next element. */
211 next
= XCONS (next
)->cdr
;
213 while (XTYPE (next
) == Lisp_Cons
214 && ! NILP (XCONS (next
)->car
))
217 elt
= XCONS (next
)->car
;
219 /* Add in the space occupied by this element and its chain link. */
220 size_so_far
+= sizeof (struct Lisp_Cons
);
221 if (XTYPE (elt
) == Lisp_Cons
)
223 size_so_far
+= sizeof (struct Lisp_Cons
);
224 if (XTYPE (XCONS (elt
)->car
) == Lisp_String
)
225 size_so_far
+= (sizeof (struct Lisp_String
) - 1
226 + XSTRING (XCONS (elt
)->car
)->size
);
229 /* Advance to next element. */
231 next
= XCONS (next
)->cdr
;
233 if (XTYPE (next
) == Lisp_Cons
)
234 last_boundary
= prev
;
236 while (XTYPE (next
) == Lisp_Cons
)
239 elt
= XCONS (next
)->car
;
241 /* When we get to a boundary, decide whether to truncate
242 either before or after it. The lower threshold, MINSIZE,
243 tells us to truncate after it. If its size pushes past
244 the higher threshold MAXSIZE as well, we truncate before it. */
247 if (size_so_far
> maxsize
)
249 last_boundary
= prev
;
250 if (size_so_far
> minsize
)
254 /* Add in the space occupied by this element and its chain link. */
255 size_so_far
+= sizeof (struct Lisp_Cons
);
256 if (XTYPE (elt
) == Lisp_Cons
)
258 size_so_far
+= sizeof (struct Lisp_Cons
);
259 if (XTYPE (XCONS (elt
)->car
) == Lisp_String
)
260 size_so_far
+= (sizeof (struct Lisp_String
) - 1
261 + XSTRING (XCONS (elt
)->car
)->size
);
264 /* Advance to next element. */
266 next
= XCONS (next
)->cdr
;
269 /* If we scanned the whole list, it is short enough; don't change it. */
273 /* Truncate at the boundary where we decided to truncate. */
274 if (!NILP (last_boundary
))
276 XCONS (last_boundary
)->cdr
= Qnil
;
283 DEFUN ("primitive-undo", Fprimitive_undo
, Sprimitive_undo
, 2, 2, 0,
284 "Undo N records from the front of the list LIST.\n\
285 Return what remains of the list.")
287 Lisp_Object count
, list
;
289 register int arg
= XINT (count
);
290 #if 0 /* This is a good feature, but would make undo-start
291 unable to do what is expected. */
294 /* If the head of the list is a boundary, it is the boundary
295 preceding this command. Get rid of it and don't count it. */
308 /* Exit inner loop at undo boundary. */
311 /* Handle an integer by setting point to that value. */
312 if (XTYPE (next
) == Lisp_Int
)
313 SET_PT (clip_to_bounds (BEGV
, XINT (next
), ZV
));
314 else if (XTYPE (next
) == Lisp_Cons
)
316 Lisp_Object car
, cdr
;
322 /* Element (t high . low) records previous modtime. */
323 Lisp_Object high
, low
;
328 mod_time
= (high
<< 16) + low
;
329 /* If this records an obsolete save
330 (not matching the actual disk file)
331 then don't mark unmodified. */
332 if (mod_time
!= current_buffer
->modtime
)
334 #ifdef CLASH_DETECTION
336 #endif /* CLASH_DETECTION */
337 Fset_buffer_modified_p (Qnil
);
341 /* Element (t prop val beg . end) records property change. */
342 Lisp_Object beg
, end
, prop
, val
;
351 Fput_text_property (beg
, end
, prop
, val
, Qnil
);
353 else if (XTYPE (car
) == Lisp_Int
&& XTYPE (cdr
) == Lisp_Int
)
355 /* Element (BEG . END) means range was inserted. */
358 if (XINT (car
) < BEGV
360 error ("Changes to be undone are outside visible portion of buffer");
361 /* Set point first thing, so that undoing this undo
362 does not send point back to where it is now. */
364 Fdelete_region (car
, cdr
);
366 else if (XTYPE (car
) == Lisp_String
&& XTYPE (cdr
) == Lisp_Int
)
368 /* Element (STRING . POS) means STRING was deleted. */
370 int pos
= XINT (cdr
);
375 if (-pos
< BEGV
|| -pos
> ZV
)
376 error ("Changes to be undone are outside visible portion of buffer");
378 Finsert (1, &membuf
);
382 if (pos
< BEGV
|| pos
> ZV
)
383 error ("Changes to be undone are outside visible portion of buffer");
386 /* Insert before markers so that if the mark is
387 currently on the boundary of this deletion, it
388 ends up on the other side of the now-undeleted
389 text from point. Since undo doesn't even keep
390 track of the mark, this isn't really necessary,
391 but it may lead to better behavior in certain
393 Finsert_before_markers (1, &membuf
);
407 defsubr (&Sprimitive_undo
);
408 defsubr (&Sundo_boundary
);