(timeout-event-p): Function deleted.
[emacs.git] / src / undo.c
blobd2cc1d2dcd4537cdfe516f6dac18fda87a2e3a78
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)
9 any later version.
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. */
22 #include <config.h>
23 #include "lisp.h"
24 #include "buffer.h"
25 #include "commands.h"
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
36 an undo-boundary. */
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)
45 int beg, length;
47 Lisp_Object lbeg, lend;
49 if (EQ (current_buffer->undo_list, Qt))
50 return;
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))
57 Fundo_boundary ();
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))
67 Lisp_Object elt;
68 elt = XCONS (current_buffer->undo_list)->car;
69 if (CONSP (elt)
70 && INTEGERP (XCONS (elt)->car)
71 && INTEGERP (XCONS (elt)->cdr)
72 && XINT (XCONS (elt)->cdr) == beg)
74 XSETINT (XCONS (elt)->cdr, beg + length);
75 return;
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)
89 int beg, length;
91 Lisp_Object lbeg, lend, sbeg;
92 int at_boundary;
94 if (EQ (current_buffer->undo_list, Qt))
95 return;
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))
102 Fundo_boundary ();
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);
113 else
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. */
120 if (at_boundary
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)
137 Lisp_Object marker;
138 int adjustment;
140 if (EQ (current_buffer->undo_list, Qt))
141 return;
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))
148 Fundo_boundary ();
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)
161 int 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))
177 return;
179 if (current_buffer != XBUFFER (last_undo_buffer))
180 Fundo_boundary ();
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)
195 int beg, length;
196 Lisp_Object prop, value, buffer;
198 Lisp_Object lbeg, lend, entry;
199 struct buffer *obuf = current_buffer;
200 int boundary = 0;
202 if (EQ (XBUFFER (buffer)->undo_list, Qt))
203 return;
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))
210 boundary = 1;
211 last_undo_buffer = buffer;
213 /* Switch temporarily to the buffer that was changed. */
214 current_buffer = XBUFFER (buffer);
216 if (boundary)
217 Fundo_boundary ();
219 if (MODIFF <= SAVE_MODIFF)
220 record_first_change ();
222 XSETINT (lbeg, beg);
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.")
236 Lisp_Object tem;
237 if (EQ (current_buffer->undo_list, Qt))
238 return Qnil;
239 tem = Fcar (current_buffer->undo_list);
240 if (!NILP (tem))
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,
246 use that one. */
247 XCONS (pending_boundary)->cdr = current_buffer->undo_list;
248 current_buffer->undo_list = pending_boundary;
249 pending_boundary = Qnil;
251 else
252 current_buffer->undo_list = Fcons (Qnil, current_buffer->undo_list);
254 return Qnil;
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. */
263 Lisp_Object
264 truncate_undo_list (list, minsize, maxsize)
265 Lisp_Object list;
266 int minsize, maxsize;
268 Lisp_Object prev, next, last_boundary;
269 int size_so_far = 0;
271 prev = Qnil;
272 next = list;
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. */
287 prev = next;
288 next = XCONS (next)->cdr;
290 while (CONSP (next) && ! NILP (XCONS (next)->car))
292 Lisp_Object elt;
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);
297 if (CONSP (elt))
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. */
306 prev = next;
307 next = XCONS (next)->cdr;
309 if (CONSP (next))
310 last_boundary = prev;
312 while (CONSP (next))
314 Lisp_Object elt;
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. */
321 if (NILP (elt))
323 if (size_so_far > maxsize)
324 break;
325 last_boundary = prev;
326 if (size_so_far > minsize)
327 break;
330 /* Add in the space occupied by this element and its chain link. */
331 size_so_far += sizeof (struct Lisp_Cons);
332 if (CONSP (elt))
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. */
341 prev = next;
342 next = XCONS (next)->cdr;
345 /* If we scanned the whole list, it is short enough; don't change it. */
346 if (NILP (next))
347 return list;
349 /* Truncate at the boundary where we decided to truncate. */
350 if (!NILP (last_boundary))
352 XCONS (last_boundary)->cdr = Qnil;
353 return list;
355 else
356 return 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.")
362 (n, list)
363 Lisp_Object n, list;
365 struct gcpro gcpro1, gcpro2;
366 Lisp_Object next;
367 int count = specpdl_ptr - specpdl;
368 register int arg;
369 #if 0 /* This is a good feature, but would make undo-start
370 unable to do what is expected. */
371 Lisp_Object tem;
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. */
375 tem = Fcar (list);
376 if (NILP (tem))
377 list = Fcdr (list);
378 #endif
380 CHECK_NUMBER (n, 0);
381 arg = XINT (n);
382 next = Qnil;
383 GCPRO2 (next, list);
385 /* Don't let read-only properties interfere with undo. */
386 if (NILP (current_buffer->read_only))
387 specbind (Qinhibit_read_only, Qt);
389 while (arg > 0)
391 while (1)
393 next = Fcar (list);
394 list = Fcdr (list);
395 /* Exit inner loop at undo boundary. */
396 if (NILP (next))
397 break;
398 /* Handle an integer by setting point to that value. */
399 if (INTEGERP (next))
400 SET_PT (clip_to_bounds (BEGV, XINT (next), ZV));
401 else if (CONSP (next))
403 Lisp_Object car, cdr;
405 car = Fcar (next);
406 cdr = Fcdr (next);
407 if (EQ (car, Qt))
409 /* Element (t high . low) records previous modtime. */
410 Lisp_Object high, low;
411 int mod_time;
412 struct buffer *base_buffer = current_buffer;
414 high = Fcar (cdr);
415 low = Fcdr (cdr);
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)
425 continue;
426 #ifdef CLASH_DETECTION
427 Funlock_buffer ();
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;
437 prop = Fcar (cdr);
438 cdr = Fcdr (cdr);
439 val = Fcar (cdr);
440 cdr = Fcdr (cdr);
441 beg = Fcar (cdr);
442 end = Fcdr (cdr);
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. */
450 Lisp_Object end;
452 if (XINT (car) < BEGV
453 || XINT (cdr) > ZV)
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. */
457 Fgoto_char (car);
458 Fdelete_region (car, cdr);
460 else if (STRINGP (car) && INTEGERP (cdr))
462 /* Element (STRING . POS) means STRING was deleted. */
463 Lisp_Object membuf;
464 int pos = XINT (cdr);
466 membuf = car;
467 if (pos < 0)
469 if (-pos < BEGV || -pos > ZV)
470 error ("Changes to be undone are outside visible portion of buffer");
471 SET_PT (-pos);
472 Finsert (1, &membuf);
474 else
476 if (pos < BEGV || pos > ZV)
477 error ("Changes to be undone are outside visible portion of buffer");
478 SET_PT (pos);
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
486 situations. */
487 Finsert_before_markers (1, &membuf);
488 SET_PT (pos);
491 else if (MARKERP (car) && INTEGERP (cdr))
493 /* (MARKER . INTEGER) means a marker MARKER
494 was adjusted by INTEGER. */
495 if (XMARKER (car)->buffer)
496 Fset_marker (car,
497 make_number (marker_position (car) - XINT (cdr)),
498 Fmarker_buffer (car));
502 arg--;
505 UNGCPRO;
506 return unbind_to (count, list);
509 syms_of_undo ()
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);