*** empty log message ***
[bpt/emacs.git] / src / undo.c
CommitLineData
c6953be1 1/* undo handling for GNU Emacs.
429ab54e 2 Copyright (C) 1990, 1993, 1994, 2000, 2001, 2002, 2003, 2004,
8cabe764 3 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
c6953be1
JB
4
5This file is part of GNU Emacs.
6
3b7ad313
EN
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
684d6f5b 9the Free Software Foundation; either version 3, or (at your option)
3b7ad313
EN
10any later version.
11
c6953be1 12GNU Emacs is distributed in the hope that it will be useful,
3b7ad313
EN
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
4fc5845f
LK
19the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20Boston, MA 02110-1301, USA. */
c6953be1
JB
21
22
18160b98 23#include <config.h>
c6953be1
JB
24#include "lisp.h"
25#include "buffer.h"
4e665715 26#include "commands.h"
91e25f5e 27#include "window.h"
c6953be1 28
137e23ea
RS
29/* Limits controlling how much undo information to keep. */
30
31EMACS_INT undo_limit;
32EMACS_INT undo_strong_limit;
81c1cf71
RS
33
34Lisp_Object Vundo_outer_limit;
137e23ea
RS
35
36/* Function to call when undo_outer_limit is exceeded. */
37
38Lisp_Object Vundo_outer_limit_function;
39
c6953be1 40/* Last buffer for which undo information was recorded. */
4591d6cb
SM
41/* BEWARE: This is not traced by the GC, so never dereference it! */
42struct buffer *last_undo_buffer;
43
44/* Position of point last time we inserted a boundary. */
45struct buffer *last_boundary_buffer;
46EMACS_INT last_boundary_position;
c6953be1 47
f87a68b3
RS
48Lisp_Object Qinhibit_read_only;
49
49be18c9
KS
50/* Marker for function call undo list elements. */
51
52Lisp_Object Qapply;
53
c58632fc
RS
54/* The first time a command records something for undo.
55 it also allocates the undo-boundary object
56 which will be added to the list at the end of the command.
57 This ensures we can't run out of space while trying to make
58 an undo-boundary. */
59Lisp_Object pending_boundary;
60
8abe0f97
MR
61/* Nonzero means do not record point in record_point. */
62
63int undo_inhibit_record_point;
64
6396140a 65/* Record point as it was at beginning of this command (if necessary)
8abe0f97 66 and prepare the undo info for recording a change.
6396140a
SM
67 PT is the position of point that will naturally occur as a result of the
68 undo record that will be added just after this command terminates. */
c6953be1 69
6396140a
SM
70static void
71record_point (pt)
f45bedd4 72 int pt;
c6953be1 73{
6396140a 74 int at_boundary;
bdbe6f28 75
4591d6cb 76 /* Don't record position of pt when undo_inhibit_record_point holds. */
8abe0f97
MR
77 if (undo_inhibit_record_point)
78 return;
79
c58632fc
RS
80 /* Allocate a cons cell to be the undo boundary after this command. */
81 if (NILP (pending_boundary))
82 pending_boundary = Fcons (Qnil, Qnil);
83
4591d6cb 84 if (current_buffer != last_undo_buffer)
c6953be1 85 Fundo_boundary ();
4591d6cb 86 last_undo_buffer = current_buffer;
c6953be1 87
6396140a
SM
88 if (CONSP (current_buffer->undo_list))
89 {
90 /* Set AT_BOUNDARY to 1 only when we have nothing other than
91 marker adjustment before undo boundary. */
92
93 Lisp_Object tail = current_buffer->undo_list, elt;
94
95 while (1)
96 {
97 if (NILP (tail))
98 elt = Qnil;
99 else
100 elt = XCAR (tail);
101 if (NILP (elt) || ! (CONSP (elt) && MARKERP (XCAR (elt))))
102 break;
103 tail = XCDR (tail);
104 }
105 at_boundary = NILP (elt);
106 }
107 else
108 at_boundary = 1;
109
ad9cdce4 110 if (MODIFF <= SAVE_MODIFF)
c6953be1
JB
111 record_first_change ();
112
177c0ea7 113 /* If we are just after an undo boundary, and
6396140a
SM
114 point wasn't at start of deleted range, record where it was. */
115 if (at_boundary
4591d6cb
SM
116 && current_buffer == last_boundary_buffer
117 && last_boundary_position != pt)
118 current_buffer->undo_list
119 = Fcons (make_number (last_boundary_position), current_buffer->undo_list);
6396140a
SM
120}
121
122/* Record an insertion that just happened or is about to happen,
123 for LENGTH characters at position BEG.
124 (It is possible to record an insertion before or after the fact
125 because we don't need to record the contents.) */
126
127void
128record_insert (beg, length)
129 int beg, length;
130{
131 Lisp_Object lbeg, lend;
132
133 if (EQ (current_buffer->undo_list, Qt))
134 return;
135
136 record_point (beg);
137
c6953be1
JB
138 /* If this is following another insertion and consecutive with it
139 in the buffer, combine the two. */
38c0d37c 140 if (CONSP (current_buffer->undo_list))
c6953be1
JB
141 {
142 Lisp_Object elt;
c1d497be 143 elt = XCAR (current_buffer->undo_list);
38c0d37c 144 if (CONSP (elt)
c1d497be
KR
145 && INTEGERP (XCAR (elt))
146 && INTEGERP (XCDR (elt))
147 && XINT (XCDR (elt)) == beg)
c6953be1 148 {
f3fbd155 149 XSETCDR (elt, make_number (beg + length));
c6953be1
JB
150 return;
151 }
152 }
153
53480e99
KH
154 XSETFASTINT (lbeg, beg);
155 XSETINT (lend, beg + length);
213861c7
JB
156 current_buffer->undo_list = Fcons (Fcons (lbeg, lend),
157 current_buffer->undo_list);
c6953be1
JB
158}
159
160/* Record that a deletion is about to take place,
e928d437 161 of the characters in STRING, at location BEG. */
c6953be1 162
ff1aa840 163void
e928d437
RS
164record_delete (beg, string)
165 int beg;
166 Lisp_Object string;
c6953be1 167{
e928d437 168 Lisp_Object sbeg;
c6953be1 169
bdbe6f28
RS
170 if (EQ (current_buffer->undo_list, Qt))
171 return;
172
d5db4077 173 if (PT == beg + SCHARS (string))
cbc1b668 174 {
6396140a
SM
175 XSETINT (sbeg, -beg);
176 record_point (PT);
cbc1b668
KH
177 }
178 else
6396140a
SM
179 {
180 XSETFASTINT (sbeg, beg);
181 record_point (beg);
182 }
350bce56 183
c6953be1 184 current_buffer->undo_list
e928d437 185 = Fcons (Fcons (string, sbeg), current_buffer->undo_list);
c6953be1
JB
186}
187
714bced9
RS
188/* Record the fact that MARKER is about to be adjusted by ADJUSTMENT.
189 This is done only when a marker points within text being deleted,
190 because that's the only case where an automatic marker adjustment
191 won't be inverted automatically by undoing the buffer modification. */
192
ff1aa840 193void
714bced9
RS
194record_marker_adjustment (marker, adjustment)
195 Lisp_Object marker;
196 int adjustment;
197{
198 if (EQ (current_buffer->undo_list, Qt))
199 return;
200
201 /* Allocate a cons cell to be the undo boundary after this command. */
202 if (NILP (pending_boundary))
203 pending_boundary = Fcons (Qnil, Qnil);
204
4591d6cb 205 if (current_buffer != last_undo_buffer)
714bced9 206 Fundo_boundary ();
4591d6cb 207 last_undo_buffer = current_buffer;
714bced9
RS
208
209 current_buffer->undo_list
210 = Fcons (Fcons (marker, make_number (adjustment)),
211 current_buffer->undo_list);
212}
213
c6953be1
JB
214/* Record that a replacement is about to take place,
215 for LENGTH characters at location BEG.
e928d437 216 The replacement must not change the number of characters. */
c6953be1 217
ff1aa840 218void
c6953be1
JB
219record_change (beg, length)
220 int beg, length;
221{
e928d437 222 record_delete (beg, make_buffer_string (beg, beg + length, 1));
c6953be1
JB
223 record_insert (beg, length);
224}
225\f
226/* Record that an unmodified buffer is about to be changed.
227 Record the file modification date so that when undoing this entry
228 we can tell whether it is obsolete because the file was saved again. */
229
90dd3e4f 230void
c6953be1
JB
231record_first_change ()
232{
233 Lisp_Object high, low;
ad9cdce4 234 struct buffer *base_buffer = current_buffer;
0736cafe
RS
235
236 if (EQ (current_buffer->undo_list, Qt))
237 return;
238
4591d6cb 239 if (current_buffer != last_undo_buffer)
0736cafe 240 Fundo_boundary ();
4591d6cb 241 last_undo_buffer = current_buffer;
0736cafe 242
ad9cdce4
RS
243 if (base_buffer->base_buffer)
244 base_buffer = base_buffer->base_buffer;
245
246 XSETFASTINT (high, (base_buffer->modtime >> 16) & 0xffff);
247 XSETFASTINT (low, base_buffer->modtime & 0xffff);
c6953be1
JB
248 current_buffer->undo_list = Fcons (Fcons (Qt, Fcons (high, low)), current_buffer->undo_list);
249}
250
da9319d5
RS
251/* Record a change in property PROP (whose old value was VAL)
252 for LENGTH characters starting at position BEG in BUFFER. */
253
90dd3e4f 254void
da9319d5
RS
255record_property_change (beg, length, prop, value, buffer)
256 int beg, length;
257 Lisp_Object prop, value, buffer;
258{
259 Lisp_Object lbeg, lend, entry;
4591d6cb 260 struct buffer *obuf = current_buffer, *buf = XBUFFER (buffer);
da9319d5
RS
261 int boundary = 0;
262
4591d6cb 263 if (EQ (buf->undo_list, Qt))
bdbe6f28
RS
264 return;
265
c58632fc
RS
266 /* Allocate a cons cell to be the undo boundary after this command. */
267 if (NILP (pending_boundary))
268 pending_boundary = Fcons (Qnil, Qnil);
269
4591d6cb 270 if (buf != last_undo_buffer)
da9319d5 271 boundary = 1;
4591d6cb 272 last_undo_buffer = buf;
da9319d5 273
da9319d5 274 /* Switch temporarily to the buffer that was changed. */
4591d6cb 275 current_buffer = buf;
da9319d5
RS
276
277 if (boundary)
278 Fundo_boundary ();
279
ad9cdce4 280 if (MODIFF <= SAVE_MODIFF)
da9319d5
RS
281 record_first_change ();
282
552bdbcf
KH
283 XSETINT (lbeg, beg);
284 XSETINT (lend, beg + length);
da9319d5
RS
285 entry = Fcons (Qnil, Fcons (prop, Fcons (value, Fcons (lbeg, lend))));
286 current_buffer->undo_list = Fcons (entry, current_buffer->undo_list);
287
288 current_buffer = obuf;
289}
290
c6953be1 291DEFUN ("undo-boundary", Fundo_boundary, Sundo_boundary, 0, 0, 0,
8c1a1077
PJ
292 doc: /* Mark a boundary between units of undo.
293An undo command will stop at this point,
294but another undo command will undo to the previous boundary. */)
295 ()
c6953be1
JB
296{
297 Lisp_Object tem;
298 if (EQ (current_buffer->undo_list, Qt))
299 return Qnil;
300 tem = Fcar (current_buffer->undo_list);
265a9e55 301 if (!NILP (tem))
c58632fc
RS
302 {
303 /* One way or another, cons nil onto the front of the undo list. */
304 if (!NILP (pending_boundary))
305 {
306 /* If we have preallocated the cons cell to use here,
307 use that one. */
f3fbd155 308 XSETCDR (pending_boundary, current_buffer->undo_list);
c58632fc
RS
309 current_buffer->undo_list = pending_boundary;
310 pending_boundary = Qnil;
311 }
312 else
313 current_buffer->undo_list = Fcons (Qnil, current_buffer->undo_list);
314 }
4591d6cb
SM
315 last_boundary_position = PT;
316 last_boundary_buffer = current_buffer;
c6953be1
JB
317 return Qnil;
318}
319
320/* At garbage collection time, make an undo list shorter at the end,
137e23ea
RS
321 returning the truncated list. How this is done depends on the
322 variables undo-limit, undo-strong-limit and undo-outer-limit.
323 In some cases this works by calling undo-outer-limit-function. */
324
325void
326truncate_undo_list (b)
327 struct buffer *b;
c6953be1 328{
137e23ea 329 Lisp_Object list;
c6953be1
JB
330 Lisp_Object prev, next, last_boundary;
331 int size_so_far = 0;
332
137e23ea
RS
333 /* Make sure that calling undo-outer-limit-function
334 won't cause another GC. */
335 int count = inhibit_garbage_collection ();
336
337 /* Make the buffer current to get its local values of variables such
338 as undo_limit. Also so that Vundo_outer_limit_function can
339 tell which buffer to operate on. */
340 record_unwind_protect (set_buffer_if_live, Fcurrent_buffer ());
341 set_buffer_internal (b);
342
343 list = b->undo_list;
344
c6953be1
JB
345 prev = Qnil;
346 next = list;
347 last_boundary = Qnil;
348
137e23ea 349 /* If the first element is an undo boundary, skip past it. */
c1d497be 350 if (CONSP (next) && NILP (XCAR (next)))
c6953be1
JB
351 {
352 /* Add in the space occupied by this element and its chain link. */
353 size_so_far += sizeof (struct Lisp_Cons);
354
355 /* Advance to next element. */
356 prev = next;
c1d497be 357 next = XCDR (next);
c6953be1 358 }
e3d5ca1e 359
137e23ea
RS
360 /* Always preserve at least the most recent undo record
361 unless it is really horribly big.
362
363 Skip, skip, skip the undo, skip, skip, skip the undo,
364 Skip, skip, skip the undo, skip to the undo bound'ry. */
365
c1d497be 366 while (CONSP (next) && ! NILP (XCAR (next)))
c6953be1
JB
367 {
368 Lisp_Object elt;
c1d497be 369 elt = XCAR (next);
c6953be1
JB
370
371 /* Add in the space occupied by this element and its chain link. */
372 size_so_far += sizeof (struct Lisp_Cons);
38c0d37c 373 if (CONSP (elt))
c6953be1
JB
374 {
375 size_so_far += sizeof (struct Lisp_Cons);
c1d497be 376 if (STRINGP (XCAR (elt)))
c6953be1 377 size_so_far += (sizeof (struct Lisp_String) - 1
d5db4077 378 + SCHARS (XCAR (elt)));
c6953be1
JB
379 }
380
381 /* Advance to next element. */
382 prev = next;
c1d497be 383 next = XCDR (next);
c6953be1 384 }
e3d5ca1e 385
137e23ea
RS
386 /* If by the first boundary we have already passed undo_outer_limit,
387 we're heading for memory full, so offer to clear out the list. */
81c1cf71
RS
388 if (INTEGERP (Vundo_outer_limit)
389 && size_so_far > XINT (Vundo_outer_limit)
137e23ea
RS
390 && !NILP (Vundo_outer_limit_function))
391 {
4591d6cb
SM
392 Lisp_Object tem;
393 struct buffer *temp = last_undo_buffer;
137e23ea
RS
394
395 /* Normally the function this calls is undo-outer-limit-truncate. */
88fde92a
KR
396 tem = call1 (Vundo_outer_limit_function, make_number (size_so_far));
397 if (! NILP (tem))
137e23ea
RS
398 {
399 /* The function is responsible for making
400 any desired changes in buffer-undo-list. */
401 unbind_to (count, Qnil);
402 return;
403 }
404 /* That function probably used the minibuffer, and if so, that
405 changed last_undo_buffer. Change it back so that we don't
406 force next change to make an undo boundary here. */
407 last_undo_buffer = temp;
408 }
409
38c0d37c 410 if (CONSP (next))
c6953be1
JB
411 last_boundary = prev;
412
137e23ea 413 /* Keep additional undo data, if it fits in the limits. */
38c0d37c 414 while (CONSP (next))
c6953be1
JB
415 {
416 Lisp_Object elt;
c1d497be 417 elt = XCAR (next);
c6953be1
JB
418
419 /* When we get to a boundary, decide whether to truncate
137e23ea 420 either before or after it. The lower threshold, undo_limit,
c6953be1 421 tells us to truncate after it. If its size pushes past
137e23ea 422 the higher threshold undo_strong_limit, we truncate before it. */
265a9e55 423 if (NILP (elt))
c6953be1 424 {
137e23ea 425 if (size_so_far > undo_strong_limit)
c6953be1
JB
426 break;
427 last_boundary = prev;
137e23ea 428 if (size_so_far > undo_limit)
c6953be1
JB
429 break;
430 }
431
432 /* Add in the space occupied by this element and its chain link. */
433 size_so_far += sizeof (struct Lisp_Cons);
38c0d37c 434 if (CONSP (elt))
c6953be1
JB
435 {
436 size_so_far += sizeof (struct Lisp_Cons);
c1d497be 437 if (STRINGP (XCAR (elt)))
c6953be1 438 size_so_far += (sizeof (struct Lisp_String) - 1
d5db4077 439 + SCHARS (XCAR (elt)));
c6953be1
JB
440 }
441
442 /* Advance to next element. */
443 prev = next;
c1d497be 444 next = XCDR (next);
c6953be1
JB
445 }
446
447 /* If we scanned the whole list, it is short enough; don't change it. */
265a9e55 448 if (NILP (next))
137e23ea 449 ;
c6953be1 450 /* Truncate at the boundary where we decided to truncate. */
137e23ea
RS
451 else if (!NILP (last_boundary))
452 XSETCDR (last_boundary, Qnil);
453 /* There's nothing we decided to keep, so clear it out. */
c6953be1 454 else
137e23ea
RS
455 b->undo_list = Qnil;
456
457 unbind_to (count, Qnil);
c6953be1
JB
458}
459\f
460DEFUN ("primitive-undo", Fprimitive_undo, Sprimitive_undo, 2, 2, 0,
8c1a1077
PJ
461 doc: /* Undo N records from the front of the list LIST.
462Return what remains of the list. */)
463 (n, list)
063fb61f 464 Lisp_Object n, list;
c6953be1 465{
de65837b
KH
466 struct gcpro gcpro1, gcpro2;
467 Lisp_Object next;
331379bf 468 int count = SPECPDL_INDEX ();
de65837b 469 register int arg;
4ac03187
KS
470 Lisp_Object oldlist;
471 int did_apply = 0;
177c0ea7 472
c6953be1
JB
473#if 0 /* This is a good feature, but would make undo-start
474 unable to do what is expected. */
475 Lisp_Object tem;
476
477 /* If the head of the list is a boundary, it is the boundary
478 preceding this command. Get rid of it and don't count it. */
479 tem = Fcar (list);
265a9e55 480 if (NILP (tem))
c6953be1
JB
481 list = Fcdr (list);
482#endif
483
b7826503 484 CHECK_NUMBER (n);
de65837b
KH
485 arg = XINT (n);
486 next = Qnil;
487 GCPRO2 (next, list);
4ac03187
KS
488 /* I don't think we need to gcpro oldlist, as we use it only
489 to check for EQ. ++kfs */
de65837b 490
38d56be3
GM
491 /* In a writable buffer, enable undoing read-only text that is so
492 because of text properties. */
493 if (NILP (current_buffer->read_only))
f87a68b3
RS
494 specbind (Qinhibit_read_only, Qt);
495
8c757fd7
GM
496 /* Don't let `intangible' properties interfere with undo. */
497 specbind (Qinhibit_point_motion_hooks, Qt);
498
4ac03187
KS
499 oldlist = current_buffer->undo_list;
500
c6953be1
JB
501 while (arg > 0)
502 {
c3b09bbf 503 while (CONSP (list))
c6953be1 504 {
c3b09bbf
SM
505 next = XCAR (list);
506 list = XCDR (list);
350bce56 507 /* Exit inner loop at undo boundary. */
265a9e55 508 if (NILP (next))
c6953be1 509 break;
350bce56 510 /* Handle an integer by setting point to that value. */
38c0d37c 511 if (INTEGERP (next))
350bce56 512 SET_PT (clip_to_bounds (BEGV, XINT (next), ZV));
38c0d37c 513 else if (CONSP (next))
c6953be1 514 {
350bce56
RS
515 Lisp_Object car, cdr;
516
c3b09bbf
SM
517 car = XCAR (next);
518 cdr = XCDR (next);
350bce56 519 if (EQ (car, Qt))
c6953be1 520 {
350bce56
RS
521 /* Element (t high . low) records previous modtime. */
522 Lisp_Object high, low;
523 int mod_time;
ad9cdce4 524 struct buffer *base_buffer = current_buffer;
350bce56
RS
525
526 high = Fcar (cdr);
527 low = Fcdr (cdr);
d8552b2f 528 mod_time = (XFASTINT (high) << 16) + XFASTINT (low);
ad9cdce4
RS
529
530 if (current_buffer->base_buffer)
531 base_buffer = current_buffer->base_buffer;
532
350bce56
RS
533 /* If this records an obsolete save
534 (not matching the actual disk file)
535 then don't mark unmodified. */
ad9cdce4 536 if (mod_time != base_buffer->modtime)
103dcb38 537 continue;
e6dd6080 538#ifdef CLASH_DETECTION
350bce56 539 Funlock_buffer ();
e6dd6080 540#endif /* CLASH_DETECTION */
350bce56 541 Fset_buffer_modified_p (Qnil);
c6953be1 542 }
d8552b2f 543 else if (EQ (car, Qnil))
da9319d5 544 {
6887bce5 545 /* Element (nil PROP VAL BEG . END) is property change. */
da9319d5
RS
546 Lisp_Object beg, end, prop, val;
547
548 prop = Fcar (cdr);
549 cdr = Fcdr (cdr);
550 val = Fcar (cdr);
551 cdr = Fcdr (cdr);
552 beg = Fcar (cdr);
553 end = Fcdr (cdr);
554
9e568684
CY
555 if (XINT (beg) < BEGV || XINT (end) > ZV)
556 error ("Changes to be undone are outside visible portion of buffer");
da9319d5
RS
557 Fput_text_property (beg, end, prop, val, Qnil);
558 }
38c0d37c 559 else if (INTEGERP (car) && INTEGERP (cdr))
c6953be1 560 {
350bce56 561 /* Element (BEG . END) means range was inserted. */
350bce56
RS
562
563 if (XINT (car) < BEGV
564 || XINT (cdr) > ZV)
c6953be1 565 error ("Changes to be undone are outside visible portion of buffer");
f28f04cc
RS
566 /* Set point first thing, so that undoing this undo
567 does not send point back to where it is now. */
350bce56 568 Fgoto_char (car);
f28f04cc 569 Fdelete_region (car, cdr);
350bce56 570 }
49be18c9 571 else if (EQ (car, Qapply))
6887bce5 572 {
3419757d 573 /* Element (apply FUN . ARGS) means call FUN to undo. */
a7a39468
KS
574 struct buffer *save_buffer = current_buffer;
575
49be18c9 576 car = Fcar (cdr);
3419757d 577 cdr = Fcdr (cdr);
49be18c9
KS
578 if (INTEGERP (car))
579 {
3419757d
SM
580 /* Long format: (apply DELTA START END FUN . ARGS). */
581 Lisp_Object delta = car;
582 Lisp_Object start = Fcar (cdr);
583 Lisp_Object end = Fcar (Fcdr (cdr));
584 Lisp_Object start_mark = Fcopy_marker (start, Qnil);
585 Lisp_Object end_mark = Fcopy_marker (end, Qt);
586
587 cdr = Fcdr (Fcdr (cdr));
588 apply1 (Fcar (cdr), Fcdr (cdr));
589
590 /* Check that the function did what the entry said it
591 would do. */
592 if (!EQ (start, Fmarker_position (start_mark))
593 || (XINT (delta) + XINT (end)
594 != marker_position (end_mark)))
595 error ("Changes to be undone by function different than announced");
596 Fset_marker (start_mark, Qnil, Qnil);
597 Fset_marker (end_mark, Qnil, Qnil);
49be18c9 598 }
3419757d
SM
599 else
600 apply1 (car, cdr);
a7a39468
KS
601
602 if (save_buffer != current_buffer)
603 error ("Undo function switched buffer");
4ac03187 604 did_apply = 1;
6887bce5 605 }
38c0d37c 606 else if (STRINGP (car) && INTEGERP (cdr))
350bce56
RS
607 {
608 /* Element (STRING . POS) means STRING was deleted. */
609 Lisp_Object membuf;
610 int pos = XINT (cdr);
611
612 membuf = car;
613 if (pos < 0)
614 {
615 if (-pos < BEGV || -pos > ZV)
616 error ("Changes to be undone are outside visible portion of buffer");
617 SET_PT (-pos);
618 Finsert (1, &membuf);
619 }
620 else
621 {
622 if (pos < BEGV || pos > ZV)
623 error ("Changes to be undone are outside visible portion of buffer");
624 SET_PT (pos);
625
b2adc409
RS
626 /* Now that we record marker adjustments
627 (caused by deletion) for undo,
628 we should always insert after markers,
629 so that undoing the marker adjustments
630 put the markers back in the right place. */
631 Finsert (1, &membuf);
350bce56
RS
632 SET_PT (pos);
633 }
c6953be1 634 }
714bced9
RS
635 else if (MARKERP (car) && INTEGERP (cdr))
636 {
637 /* (MARKER . INTEGER) means a marker MARKER
638 was adjusted by INTEGER. */
639 if (XMARKER (car)->buffer)
640 Fset_marker (car,
641 make_number (marker_position (car) - XINT (cdr)),
642 Fmarker_buffer (car));
643 }
c6953be1
JB
644 }
645 }
646 arg--;
647 }
648
4ac03187
KS
649
650 /* Make sure an apply entry produces at least one undo entry,
651 so the test in `undo' for continuing an undo series
652 will work right. */
653 if (did_apply
654 && EQ (oldlist, current_buffer->undo_list))
655 current_buffer->undo_list
656 = Fcons (list3 (Qapply, Qcdr, Qnil), current_buffer->undo_list);
657
de65837b 658 UNGCPRO;
f87a68b3 659 return unbind_to (count, list);
c6953be1 660}
6887bce5 661\f
dfcf069d 662void
c6953be1
JB
663syms_of_undo ()
664{
f87a68b3
RS
665 Qinhibit_read_only = intern ("inhibit-read-only");
666 staticpro (&Qinhibit_read_only);
667
49be18c9
KS
668 Qapply = intern ("apply");
669 staticpro (&Qapply);
670
c58632fc
RS
671 pending_boundary = Qnil;
672 staticpro (&pending_boundary);
673
4591d6cb
SM
674 last_undo_buffer = NULL;
675 last_boundary_buffer = NULL;
676
c6953be1
JB
677 defsubr (&Sprimitive_undo);
678 defsubr (&Sundo_boundary);
137e23ea
RS
679
680 DEFVAR_INT ("undo-limit", &undo_limit,
681 doc: /* Keep no more undo information once it exceeds this size.
682This limit is applied when garbage collection happens.
683When a previous command increases the total undo list size past this
684value, the earlier commands that came before it are forgotten.
685
686The size is counted as the number of bytes occupied,
687which includes both saved text and other data. */);
688 undo_limit = 20000;
689
690 DEFVAR_INT ("undo-strong-limit", &undo_strong_limit,
691 doc: /* Don't keep more than this much size of undo information.
692This limit is applied when garbage collection happens.
693When a previous command increases the total undo list size past this
694value, that command and the earlier commands that came before it are forgotten.
695However, the most recent buffer-modifying command's undo info
696is never discarded for this reason.
697
698The size is counted as the number of bytes occupied,
699which includes both saved text and other data. */);
700 undo_strong_limit = 30000;
701
81c1cf71 702 DEFVAR_LISP ("undo-outer-limit", &Vundo_outer_limit,
137e23ea
RS
703 doc: /* Outer limit on size of undo information for one command.
704At garbage collection time, if the current command has produced
62d776fd
LT
705more than this much undo information, it discards the info and displays
706a warning. This is a last-ditch limit to prevent memory overflow.
137e23ea 707
62d776fd
LT
708The size is counted as the number of bytes occupied, which includes
709both saved text and other data. A value of nil means no limit. In
710this case, accumulating one huge undo entry could make Emacs crash as
711a result of memory overflow.
137e23ea
RS
712
713In fact, this calls the function which is the value of
714`undo-outer-limit-function' with one argument, the size.
715The text above describes the behavior of the function
716that variable usually specifies. */);
6de38aa3 717 Vundo_outer_limit = make_number (3000000);
137e23ea
RS
718
719 DEFVAR_LISP ("undo-outer-limit-function", &Vundo_outer_limit_function,
720 doc: /* Function to call when an undo list exceeds `undo-outer-limit'.
721This function is called with one argument, the current undo list size
722for the most recent command (since the last undo boundary).
723If the function returns t, that means truncation has been fully handled.
724If it returns nil, the other forms of truncation are done.
725
726Garbage collection is inhibited around the call to this function,
727so it must make sure not to do a lot of consing. */);
728 Vundo_outer_limit_function = Qnil;
8abe0f97
MR
729
730 DEFVAR_BOOL ("undo-inhibit-record-point", &undo_inhibit_record_point,
731 doc: /* Non-nil means do not record `point' in `buffer-undo-list'. */);
732 undo_inhibit_record_point = 0;
c6953be1 733}
ab5796a9
MB
734
735/* arch-tag: d546ee01-4aed-4ffb-bb8b-eefaae50d38a
736 (do not change this comment) */