(STRING_MARKED_P, VECTOR_MARKED_P): Return boolean.
[bpt/emacs.git] / src / undo.c
1 /* undo handling for GNU Emacs.
2 Copyright (C) 1990, 1993, 1994, 2000 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
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.
10
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.
15
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. */
20
21
22 #include <config.h>
23 #include "lisp.h"
24 #include "buffer.h"
25 #include "commands.h"
26
27 /* Limits controlling how much undo information to keep. */
28
29 EMACS_INT undo_limit;
30 EMACS_INT undo_strong_limit;
31
32 Lisp_Object Vundo_outer_limit;
33
34 /* Function to call when undo_outer_limit is exceeded. */
35
36 Lisp_Object Vundo_outer_limit_function;
37
38 /* Last buffer for which undo information was recorded. */
39 Lisp_Object last_undo_buffer;
40
41 Lisp_Object Qinhibit_read_only;
42
43 /* The first time a command records something for undo.
44 it also allocates the undo-boundary object
45 which will be added to the list at the end of the command.
46 This ensures we can't run out of space while trying to make
47 an undo-boundary. */
48 Lisp_Object pending_boundary;
49
50 /* Record point as it was at beginning of this command (if necessary)
51 And prepare the undo info for recording a change.
52 PT is the position of point that will naturally occur as a result of the
53 undo record that will be added just after this command terminates. */
54
55 static void
56 record_point (pt)
57 int pt;
58 {
59 int at_boundary;
60
61 /* Allocate a cons cell to be the undo boundary after this command. */
62 if (NILP (pending_boundary))
63 pending_boundary = Fcons (Qnil, Qnil);
64
65 if (!BUFFERP (last_undo_buffer)
66 || current_buffer != XBUFFER (last_undo_buffer))
67 Fundo_boundary ();
68 XSETBUFFER (last_undo_buffer, current_buffer);
69
70 if (CONSP (current_buffer->undo_list))
71 {
72 /* Set AT_BOUNDARY to 1 only when we have nothing other than
73 marker adjustment before undo boundary. */
74
75 Lisp_Object tail = current_buffer->undo_list, elt;
76
77 while (1)
78 {
79 if (NILP (tail))
80 elt = Qnil;
81 else
82 elt = XCAR (tail);
83 if (NILP (elt) || ! (CONSP (elt) && MARKERP (XCAR (elt))))
84 break;
85 tail = XCDR (tail);
86 }
87 at_boundary = NILP (elt);
88 }
89 else
90 at_boundary = 1;
91
92 if (MODIFF <= SAVE_MODIFF)
93 record_first_change ();
94
95 /* If we are just after an undo boundary, and
96 point wasn't at start of deleted range, record where it was. */
97 if (at_boundary
98 && last_point_position != pt
99 /* If we're called from batch mode, this could be nil. */
100 && BUFFERP (last_point_position_buffer)
101 && current_buffer == XBUFFER (last_point_position_buffer))
102 current_buffer->undo_list
103 = Fcons (make_number (last_point_position), current_buffer->undo_list);
104 }
105
106 /* Record an insertion that just happened or is about to happen,
107 for LENGTH characters at position BEG.
108 (It is possible to record an insertion before or after the fact
109 because we don't need to record the contents.) */
110
111 void
112 record_insert (beg, length)
113 int beg, length;
114 {
115 Lisp_Object lbeg, lend;
116
117 if (EQ (current_buffer->undo_list, Qt))
118 return;
119
120 record_point (beg);
121
122 /* If this is following another insertion and consecutive with it
123 in the buffer, combine the two. */
124 if (CONSP (current_buffer->undo_list))
125 {
126 Lisp_Object elt;
127 elt = XCAR (current_buffer->undo_list);
128 if (CONSP (elt)
129 && INTEGERP (XCAR (elt))
130 && INTEGERP (XCDR (elt))
131 && XINT (XCDR (elt)) == beg)
132 {
133 XSETCDR (elt, make_number (beg + length));
134 return;
135 }
136 }
137
138 XSETFASTINT (lbeg, beg);
139 XSETINT (lend, beg + length);
140 current_buffer->undo_list = Fcons (Fcons (lbeg, lend),
141 current_buffer->undo_list);
142 }
143
144 /* Record that a deletion is about to take place,
145 of the characters in STRING, at location BEG. */
146
147 void
148 record_delete (beg, string)
149 int beg;
150 Lisp_Object string;
151 {
152 Lisp_Object sbeg;
153
154 if (EQ (current_buffer->undo_list, Qt))
155 return;
156
157 if (PT == beg + SCHARS (string))
158 {
159 XSETINT (sbeg, -beg);
160 record_point (PT);
161 }
162 else
163 {
164 XSETFASTINT (sbeg, beg);
165 record_point (beg);
166 }
167
168 current_buffer->undo_list
169 = Fcons (Fcons (string, sbeg), current_buffer->undo_list);
170 }
171
172 /* Record the fact that MARKER is about to be adjusted by ADJUSTMENT.
173 This is done only when a marker points within text being deleted,
174 because that's the only case where an automatic marker adjustment
175 won't be inverted automatically by undoing the buffer modification. */
176
177 void
178 record_marker_adjustment (marker, adjustment)
179 Lisp_Object marker;
180 int adjustment;
181 {
182 if (EQ (current_buffer->undo_list, Qt))
183 return;
184
185 /* Allocate a cons cell to be the undo boundary after this command. */
186 if (NILP (pending_boundary))
187 pending_boundary = Fcons (Qnil, Qnil);
188
189 if (!BUFFERP (last_undo_buffer)
190 || current_buffer != XBUFFER (last_undo_buffer))
191 Fundo_boundary ();
192 XSETBUFFER (last_undo_buffer, current_buffer);
193
194 current_buffer->undo_list
195 = Fcons (Fcons (marker, make_number (adjustment)),
196 current_buffer->undo_list);
197 }
198
199 /* Record that a replacement is about to take place,
200 for LENGTH characters at location BEG.
201 The replacement must not change the number of characters. */
202
203 void
204 record_change (beg, length)
205 int beg, length;
206 {
207 record_delete (beg, make_buffer_string (beg, beg + length, 1));
208 record_insert (beg, length);
209 }
210 \f
211 /* Record that an unmodified buffer is about to be changed.
212 Record the file modification date so that when undoing this entry
213 we can tell whether it is obsolete because the file was saved again. */
214
215 void
216 record_first_change ()
217 {
218 Lisp_Object high, low;
219 struct buffer *base_buffer = current_buffer;
220
221 if (EQ (current_buffer->undo_list, Qt))
222 return;
223
224 if (!BUFFERP (last_undo_buffer)
225 || current_buffer != XBUFFER (last_undo_buffer))
226 Fundo_boundary ();
227 XSETBUFFER (last_undo_buffer, current_buffer);
228
229 if (base_buffer->base_buffer)
230 base_buffer = base_buffer->base_buffer;
231
232 XSETFASTINT (high, (base_buffer->modtime >> 16) & 0xffff);
233 XSETFASTINT (low, base_buffer->modtime & 0xffff);
234 current_buffer->undo_list = Fcons (Fcons (Qt, Fcons (high, low)), current_buffer->undo_list);
235 }
236
237 /* Record a change in property PROP (whose old value was VAL)
238 for LENGTH characters starting at position BEG in BUFFER. */
239
240 void
241 record_property_change (beg, length, prop, value, buffer)
242 int beg, length;
243 Lisp_Object prop, value, buffer;
244 {
245 Lisp_Object lbeg, lend, entry;
246 struct buffer *obuf = current_buffer;
247 int boundary = 0;
248
249 if (EQ (XBUFFER (buffer)->undo_list, Qt))
250 return;
251
252 /* Allocate a cons cell to be the undo boundary after this command. */
253 if (NILP (pending_boundary))
254 pending_boundary = Fcons (Qnil, Qnil);
255
256 if (!EQ (buffer, last_undo_buffer))
257 boundary = 1;
258 last_undo_buffer = buffer;
259
260 /* Switch temporarily to the buffer that was changed. */
261 current_buffer = XBUFFER (buffer);
262
263 if (boundary)
264 Fundo_boundary ();
265
266 if (MODIFF <= SAVE_MODIFF)
267 record_first_change ();
268
269 XSETINT (lbeg, beg);
270 XSETINT (lend, beg + length);
271 entry = Fcons (Qnil, Fcons (prop, Fcons (value, Fcons (lbeg, lend))));
272 current_buffer->undo_list = Fcons (entry, current_buffer->undo_list);
273
274 current_buffer = obuf;
275 }
276
277 DEFUN ("undo-boundary", Fundo_boundary, Sundo_boundary, 0, 0, 0,
278 doc: /* Mark a boundary between units of undo.
279 An undo command will stop at this point,
280 but another undo command will undo to the previous boundary. */)
281 ()
282 {
283 Lisp_Object tem;
284 if (EQ (current_buffer->undo_list, Qt))
285 return Qnil;
286 tem = Fcar (current_buffer->undo_list);
287 if (!NILP (tem))
288 {
289 /* One way or another, cons nil onto the front of the undo list. */
290 if (!NILP (pending_boundary))
291 {
292 /* If we have preallocated the cons cell to use here,
293 use that one. */
294 XSETCDR (pending_boundary, current_buffer->undo_list);
295 current_buffer->undo_list = pending_boundary;
296 pending_boundary = Qnil;
297 }
298 else
299 current_buffer->undo_list = Fcons (Qnil, current_buffer->undo_list);
300 }
301 return Qnil;
302 }
303
304 /* At garbage collection time, make an undo list shorter at the end,
305 returning the truncated list. How this is done depends on the
306 variables undo-limit, undo-strong-limit and undo-outer-limit.
307 In some cases this works by calling undo-outer-limit-function. */
308
309 void
310 truncate_undo_list (b)
311 struct buffer *b;
312 {
313 Lisp_Object list;
314 Lisp_Object prev, next, last_boundary;
315 int size_so_far = 0;
316
317 /* Make sure that calling undo-outer-limit-function
318 won't cause another GC. */
319 int count = inhibit_garbage_collection ();
320
321 /* Make the buffer current to get its local values of variables such
322 as undo_limit. Also so that Vundo_outer_limit_function can
323 tell which buffer to operate on. */
324 record_unwind_protect (set_buffer_if_live, Fcurrent_buffer ());
325 set_buffer_internal (b);
326
327 list = b->undo_list;
328
329 prev = Qnil;
330 next = list;
331 last_boundary = Qnil;
332
333 /* If the first element is an undo boundary, skip past it. */
334 if (CONSP (next) && NILP (XCAR (next)))
335 {
336 /* Add in the space occupied by this element and its chain link. */
337 size_so_far += sizeof (struct Lisp_Cons);
338
339 /* Advance to next element. */
340 prev = next;
341 next = XCDR (next);
342 }
343
344 /* Always preserve at least the most recent undo record
345 unless it is really horribly big.
346
347 Skip, skip, skip the undo, skip, skip, skip the undo,
348 Skip, skip, skip the undo, skip to the undo bound'ry. */
349
350 while (CONSP (next) && ! NILP (XCAR (next)))
351 {
352 Lisp_Object elt;
353 elt = XCAR (next);
354
355 /* Add in the space occupied by this element and its chain link. */
356 size_so_far += sizeof (struct Lisp_Cons);
357 if (CONSP (elt))
358 {
359 size_so_far += sizeof (struct Lisp_Cons);
360 if (STRINGP (XCAR (elt)))
361 size_so_far += (sizeof (struct Lisp_String) - 1
362 + SCHARS (XCAR (elt)));
363 }
364
365 /* Advance to next element. */
366 prev = next;
367 next = XCDR (next);
368 }
369
370 /* If by the first boundary we have already passed undo_outer_limit,
371 we're heading for memory full, so offer to clear out the list. */
372 if (INTEGERP (Vundo_outer_limit)
373 && size_so_far > XINT (Vundo_outer_limit)
374 && !NILP (Vundo_outer_limit_function))
375 {
376 Lisp_Object temp = last_undo_buffer;
377
378 /* Normally the function this calls is undo-outer-limit-truncate. */
379 if (! NILP (call1 (Vundo_outer_limit_function,
380 make_number (size_so_far))))
381 {
382 /* The function is responsible for making
383 any desired changes in buffer-undo-list. */
384 unbind_to (count, Qnil);
385 return;
386 }
387 /* That function probably used the minibuffer, and if so, that
388 changed last_undo_buffer. Change it back so that we don't
389 force next change to make an undo boundary here. */
390 last_undo_buffer = temp;
391 }
392
393 if (CONSP (next))
394 last_boundary = prev;
395
396 /* Keep additional undo data, if it fits in the limits. */
397 while (CONSP (next))
398 {
399 Lisp_Object elt;
400 elt = XCAR (next);
401
402 /* When we get to a boundary, decide whether to truncate
403 either before or after it. The lower threshold, undo_limit,
404 tells us to truncate after it. If its size pushes past
405 the higher threshold undo_strong_limit, we truncate before it. */
406 if (NILP (elt))
407 {
408 if (size_so_far > undo_strong_limit)
409 break;
410 last_boundary = prev;
411 if (size_so_far > undo_limit)
412 break;
413 }
414
415 /* Add in the space occupied by this element and its chain link. */
416 size_so_far += sizeof (struct Lisp_Cons);
417 if (CONSP (elt))
418 {
419 size_so_far += sizeof (struct Lisp_Cons);
420 if (STRINGP (XCAR (elt)))
421 size_so_far += (sizeof (struct Lisp_String) - 1
422 + SCHARS (XCAR (elt)));
423 }
424
425 /* Advance to next element. */
426 prev = next;
427 next = XCDR (next);
428 }
429
430 /* If we scanned the whole list, it is short enough; don't change it. */
431 if (NILP (next))
432 ;
433 /* Truncate at the boundary where we decided to truncate. */
434 else if (!NILP (last_boundary))
435 XSETCDR (last_boundary, Qnil);
436 /* There's nothing we decided to keep, so clear it out. */
437 else
438 b->undo_list = Qnil;
439
440 unbind_to (count, Qnil);
441 }
442 \f
443 DEFUN ("primitive-undo", Fprimitive_undo, Sprimitive_undo, 2, 2, 0,
444 doc: /* Undo N records from the front of the list LIST.
445 Return what remains of the list. */)
446 (n, list)
447 Lisp_Object n, list;
448 {
449 struct gcpro gcpro1, gcpro2;
450 Lisp_Object next;
451 int count = SPECPDL_INDEX ();
452 register int arg;
453
454 #if 0 /* This is a good feature, but would make undo-start
455 unable to do what is expected. */
456 Lisp_Object tem;
457
458 /* If the head of the list is a boundary, it is the boundary
459 preceding this command. Get rid of it and don't count it. */
460 tem = Fcar (list);
461 if (NILP (tem))
462 list = Fcdr (list);
463 #endif
464
465 CHECK_NUMBER (n);
466 arg = XINT (n);
467 next = Qnil;
468 GCPRO2 (next, list);
469
470 /* In a writable buffer, enable undoing read-only text that is so
471 because of text properties. */
472 if (NILP (current_buffer->read_only))
473 specbind (Qinhibit_read_only, Qt);
474
475 /* Don't let `intangible' properties interfere with undo. */
476 specbind (Qinhibit_point_motion_hooks, Qt);
477
478 while (arg > 0)
479 {
480 while (CONSP (list))
481 {
482 next = XCAR (list);
483 list = XCDR (list);
484 /* Exit inner loop at undo boundary. */
485 if (NILP (next))
486 break;
487 /* Handle an integer by setting point to that value. */
488 if (INTEGERP (next))
489 SET_PT (clip_to_bounds (BEGV, XINT (next), ZV));
490 else if (CONSP (next))
491 {
492 Lisp_Object car, cdr;
493
494 car = XCAR (next);
495 cdr = XCDR (next);
496 if (EQ (car, Qt))
497 {
498 /* Element (t high . low) records previous modtime. */
499 Lisp_Object high, low;
500 int mod_time;
501 struct buffer *base_buffer = current_buffer;
502
503 high = Fcar (cdr);
504 low = Fcdr (cdr);
505 mod_time = (XFASTINT (high) << 16) + XFASTINT (low);
506
507 if (current_buffer->base_buffer)
508 base_buffer = current_buffer->base_buffer;
509
510 /* If this records an obsolete save
511 (not matching the actual disk file)
512 then don't mark unmodified. */
513 if (mod_time != base_buffer->modtime)
514 continue;
515 #ifdef CLASH_DETECTION
516 Funlock_buffer ();
517 #endif /* CLASH_DETECTION */
518 Fset_buffer_modified_p (Qnil);
519 }
520 else if (EQ (car, Qnil))
521 {
522 /* Element (nil prop val beg . end) is property change. */
523 Lisp_Object beg, end, prop, val;
524
525 prop = Fcar (cdr);
526 cdr = Fcdr (cdr);
527 val = Fcar (cdr);
528 cdr = Fcdr (cdr);
529 beg = Fcar (cdr);
530 end = Fcdr (cdr);
531
532 Fput_text_property (beg, end, prop, val, Qnil);
533 }
534 else if (INTEGERP (car) && INTEGERP (cdr))
535 {
536 /* Element (BEG . END) means range was inserted. */
537
538 if (XINT (car) < BEGV
539 || XINT (cdr) > ZV)
540 error ("Changes to be undone are outside visible portion of buffer");
541 /* Set point first thing, so that undoing this undo
542 does not send point back to where it is now. */
543 Fgoto_char (car);
544 Fdelete_region (car, cdr);
545 }
546 else if (STRINGP (car) && INTEGERP (cdr))
547 {
548 /* Element (STRING . POS) means STRING was deleted. */
549 Lisp_Object membuf;
550 int pos = XINT (cdr);
551
552 membuf = car;
553 if (pos < 0)
554 {
555 if (-pos < BEGV || -pos > ZV)
556 error ("Changes to be undone are outside visible portion of buffer");
557 SET_PT (-pos);
558 Finsert (1, &membuf);
559 }
560 else
561 {
562 if (pos < BEGV || pos > ZV)
563 error ("Changes to be undone are outside visible portion of buffer");
564 SET_PT (pos);
565
566 /* Now that we record marker adjustments
567 (caused by deletion) for undo,
568 we should always insert after markers,
569 so that undoing the marker adjustments
570 put the markers back in the right place. */
571 Finsert (1, &membuf);
572 SET_PT (pos);
573 }
574 }
575 else if (MARKERP (car) && INTEGERP (cdr))
576 {
577 /* (MARKER . INTEGER) means a marker MARKER
578 was adjusted by INTEGER. */
579 if (XMARKER (car)->buffer)
580 Fset_marker (car,
581 make_number (marker_position (car) - XINT (cdr)),
582 Fmarker_buffer (car));
583 }
584 }
585 }
586 arg--;
587 }
588
589 UNGCPRO;
590 return unbind_to (count, list);
591 }
592
593 void
594 syms_of_undo ()
595 {
596 Qinhibit_read_only = intern ("inhibit-read-only");
597 staticpro (&Qinhibit_read_only);
598
599 pending_boundary = Qnil;
600 staticpro (&pending_boundary);
601
602 defsubr (&Sprimitive_undo);
603 defsubr (&Sundo_boundary);
604
605 DEFVAR_INT ("undo-limit", &undo_limit,
606 doc: /* Keep no more undo information once it exceeds this size.
607 This limit is applied when garbage collection happens.
608 When a previous command increases the total undo list size past this
609 value, the earlier commands that came before it are forgotten.
610
611 The size is counted as the number of bytes occupied,
612 which includes both saved text and other data. */);
613 undo_limit = 20000;
614
615 DEFVAR_INT ("undo-strong-limit", &undo_strong_limit,
616 doc: /* Don't keep more than this much size of undo information.
617 This limit is applied when garbage collection happens.
618 When a previous command increases the total undo list size past this
619 value, that command and the earlier commands that came before it are forgotten.
620 However, the most recent buffer-modifying command's undo info
621 is never discarded for this reason.
622
623 The size is counted as the number of bytes occupied,
624 which includes both saved text and other data. */);
625 undo_strong_limit = 30000;
626
627 DEFVAR_LISP ("undo-outer-limit", &Vundo_outer_limit,
628 doc: /* Outer limit on size of undo information for one command.
629 At garbage collection time, if the current command has produced
630 more than this much undo information, it asks you whether to delete
631 the information. This is a last-ditch limit to prevent memory overflow.
632
633 The size is counted as the number of bytes occupied,
634 which includes both saved text and other data.
635
636 In fact, this calls the function which is the value of
637 `undo-outer-limit-function' with one argument, the size.
638 The text above describes the behavior of the function
639 that variable usually specifies. */);
640 Vundo_outer_limit = make_number (300000);
641
642 DEFVAR_LISP ("undo-outer-limit-function", &Vundo_outer_limit_function,
643 doc: /* Function to call when an undo list exceeds `undo-outer-limit'.
644 This function is called with one argument, the current undo list size
645 for the most recent command (since the last undo boundary).
646 If the function returns t, that means truncation has been fully handled.
647 If it returns nil, the other forms of truncation are done.
648
649 Garbage collection is inhibited around the call to this function,
650 so it must make sure not to do a lot of consing. */);
651 Vundo_outer_limit_function = Qnil;
652 }
653
654 /* arch-tag: d546ee01-4aed-4ffb-bb8b-eefaae50d38a
655 (do not change this comment) */