(repeat-history-command): Bug fix. Someone forgot a car.
[emacs.git] / src / undo.c
blobc8ba54580cb350d581b9701468ab32a563dd95eb
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. */
22 #include "config.h"
23 #include "lisp.h"
24 #include "buffer.h"
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))
40 return;
42 if (current_buffer != XBUFFER (last_undo_buffer))
43 Fundo_boundary ();
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)
53 Lisp_Object elt;
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));
61 return;
65 lbeg = beg;
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)
75 int beg, length;
77 Lisp_Object lbeg, lend, sbeg;
79 if (EQ (current_buffer->undo_list, Qt))
80 return;
82 if (current_buffer != XBUFFER (last_undo_buffer))
83 Fundo_boundary ();
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);
91 else
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. */
97 if (PT != sbeg)
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)
111 int 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)
133 int beg, length;
134 Lisp_Object prop, value, buffer;
136 Lisp_Object lbeg, lend, entry;
137 struct buffer *obuf = current_buffer;
138 int boundary = 0;
140 if (EQ (current_buffer->undo_list, Qt))
141 return;
143 if (!EQ (buffer, last_undo_buffer))
144 boundary = 1;
145 last_undo_buffer = buffer;
147 /* Switch temporarily to the buffer that was changed. */
148 current_buffer = XBUFFER (buffer);
150 if (boundary)
151 Fundo_boundary ();
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.")
170 Lisp_Object tem;
171 if (EQ (current_buffer->undo_list, Qt))
172 return Qnil;
173 tem = Fcar (current_buffer->undo_list);
174 if (!NILP (tem))
175 current_buffer->undo_list = Fcons (Qnil, current_buffer->undo_list);
176 return Qnil;
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. */
185 Lisp_Object
186 truncate_undo_list (list, minsize, maxsize)
187 Lisp_Object list;
188 int minsize, maxsize;
190 Lisp_Object prev, next, last_boundary;
191 int size_so_far = 0;
193 prev = Qnil;
194 next = list;
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. */
210 prev = next;
211 next = XCONS (next)->cdr;
213 while (XTYPE (next) == Lisp_Cons
214 && ! NILP (XCONS (next)->car))
216 Lisp_Object elt;
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. */
230 prev = next;
231 next = XCONS (next)->cdr;
233 if (XTYPE (next) == Lisp_Cons)
234 last_boundary = prev;
236 while (XTYPE (next) == Lisp_Cons)
238 Lisp_Object elt;
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. */
245 if (NILP (elt))
247 if (size_so_far > maxsize)
248 break;
249 last_boundary = prev;
250 if (size_so_far > minsize)
251 break;
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. */
265 prev = next;
266 next = XCONS (next)->cdr;
269 /* If we scanned the whole list, it is short enough; don't change it. */
270 if (NILP (next))
271 return list;
273 /* Truncate at the boundary where we decided to truncate. */
274 if (!NILP (last_boundary))
276 XCONS (last_boundary)->cdr = Qnil;
277 return list;
279 else
280 return 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.")
286 (count, 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. */
292 Lisp_Object tem;
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. */
296 tem = Fcar (list);
297 if (NILP (tem))
298 list = Fcdr (list);
299 #endif
301 while (arg > 0)
303 while (1)
305 Lisp_Object next;
306 next = Fcar (list);
307 list = Fcdr (list);
308 /* Exit inner loop at undo boundary. */
309 if (NILP (next))
310 break;
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;
318 car = Fcar (next);
319 cdr = Fcdr (next);
320 if (EQ (car, Qt))
322 /* Element (t high . low) records previous modtime. */
323 Lisp_Object high, low;
324 int mod_time;
326 high = Fcar (cdr);
327 low = Fcdr (cdr);
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)
333 break;
334 #ifdef CLASH_DETECTION
335 Funlock_buffer ();
336 #endif /* CLASH_DETECTION */
337 Fset_buffer_modified_p (Qnil);
339 if (EQ (car, Qnil))
341 /* Element (t prop val beg . end) records property change. */
342 Lisp_Object beg, end, prop, val;
344 prop = Fcar (cdr);
345 cdr = Fcdr (cdr);
346 val = Fcar (cdr);
347 cdr = Fcdr (cdr);
348 beg = Fcar (cdr);
349 end = Fcdr (cdr);
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. */
356 Lisp_Object end;
358 if (XINT (car) < BEGV
359 || XINT (cdr) > ZV)
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. */
363 Fgoto_char (car);
364 Fdelete_region (car, cdr);
366 else if (XTYPE (car) == Lisp_String && XTYPE (cdr) == Lisp_Int)
368 /* Element (STRING . POS) means STRING was deleted. */
369 Lisp_Object membuf;
370 int pos = XINT (cdr);
372 membuf = car;
373 if (pos < 0)
375 if (-pos < BEGV || -pos > ZV)
376 error ("Changes to be undone are outside visible portion of buffer");
377 SET_PT (-pos);
378 Finsert (1, &membuf);
380 else
382 if (pos < BEGV || pos > ZV)
383 error ("Changes to be undone are outside visible portion of buffer");
384 SET_PT (pos);
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
392 situations. */
393 Finsert_before_markers (1, &membuf);
394 SET_PT (pos);
399 arg--;
402 return list;
405 syms_of_undo ()
407 defsubr (&Sprimitive_undo);
408 defsubr (&Sundo_boundary);