* lisp.h (struct Lisp_Symbol): Replace field "name" with a lisp
[bpt/emacs.git] / src / undo.c
CommitLineData
c6953be1 1/* undo handling for GNU Emacs.
392e96d4 2 Copyright (C) 1990, 1993, 1994, 2000 Free Software Foundation, Inc.
c6953be1
JB
3
4This file is part of GNU Emacs.
5
3b7ad313
EN
6GNU Emacs is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
c6953be1 11GNU Emacs is distributed in the hope that it will be useful,
3b7ad313
EN
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Emacs; see the file COPYING. If not, write to
18the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
c6953be1
JB
20
21
18160b98 22#include <config.h>
c6953be1
JB
23#include "lisp.h"
24#include "buffer.h"
4e665715 25#include "commands.h"
c6953be1
JB
26
27/* Last buffer for which undo information was recorded. */
28Lisp_Object last_undo_buffer;
29
f87a68b3
RS
30Lisp_Object Qinhibit_read_only;
31
c58632fc
RS
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. */
37Lisp_Object pending_boundary;
38
6396140a
SM
39/* Record point as it was at beginning of this command (if necessary)
40 And prepare the undo info for recording a change.
41 PT is the position of point that will naturally occur as a result of the
42 undo record that will be added just after this command terminates. */
c6953be1 43
6396140a
SM
44static void
45record_point (pt)
c6953be1 46{
6396140a 47 int at_boundary;
bdbe6f28 48
c58632fc
RS
49 /* Allocate a cons cell to be the undo boundary after this command. */
50 if (NILP (pending_boundary))
51 pending_boundary = Fcons (Qnil, Qnil);
52
8801a864
KR
53 if (!BUFFERP (last_undo_buffer)
54 || current_buffer != XBUFFER (last_undo_buffer))
c6953be1 55 Fundo_boundary ();
552bdbcf 56 XSETBUFFER (last_undo_buffer, current_buffer);
c6953be1 57
6396140a
SM
58 if (CONSP (current_buffer->undo_list))
59 {
60 /* Set AT_BOUNDARY to 1 only when we have nothing other than
61 marker adjustment before undo boundary. */
62
63 Lisp_Object tail = current_buffer->undo_list, elt;
64
65 while (1)
66 {
67 if (NILP (tail))
68 elt = Qnil;
69 else
70 elt = XCAR (tail);
71 if (NILP (elt) || ! (CONSP (elt) && MARKERP (XCAR (elt))))
72 break;
73 tail = XCDR (tail);
74 }
75 at_boundary = NILP (elt);
76 }
77 else
78 at_boundary = 1;
79
ad9cdce4 80 if (MODIFF <= SAVE_MODIFF)
c6953be1
JB
81 record_first_change ();
82
6396140a
SM
83 /* If we are just after an undo boundary, and
84 point wasn't at start of deleted range, record where it was. */
85 if (at_boundary
86 && last_point_position != pt
87 /* If we're called from batch mode, this could be nil. */
88 && BUFFERP (last_point_position_buffer)
89 && current_buffer == XBUFFER (last_point_position_buffer))
90 current_buffer->undo_list
91 = Fcons (make_number (last_point_position), current_buffer->undo_list);
92}
93
94/* Record an insertion that just happened or is about to happen,
95 for LENGTH characters at position BEG.
96 (It is possible to record an insertion before or after the fact
97 because we don't need to record the contents.) */
98
99void
100record_insert (beg, length)
101 int beg, length;
102{
103 Lisp_Object lbeg, lend;
104
105 if (EQ (current_buffer->undo_list, Qt))
106 return;
107
108 record_point (beg);
109
c6953be1
JB
110 /* If this is following another insertion and consecutive with it
111 in the buffer, combine the two. */
38c0d37c 112 if (CONSP (current_buffer->undo_list))
c6953be1
JB
113 {
114 Lisp_Object elt;
c1d497be 115 elt = XCAR (current_buffer->undo_list);
38c0d37c 116 if (CONSP (elt)
c1d497be
KR
117 && INTEGERP (XCAR (elt))
118 && INTEGERP (XCDR (elt))
119 && XINT (XCDR (elt)) == beg)
c6953be1 120 {
f3fbd155 121 XSETCDR (elt, make_number (beg + length));
c6953be1
JB
122 return;
123 }
124 }
125
53480e99
KH
126 XSETFASTINT (lbeg, beg);
127 XSETINT (lend, beg + length);
213861c7
JB
128 current_buffer->undo_list = Fcons (Fcons (lbeg, lend),
129 current_buffer->undo_list);
c6953be1
JB
130}
131
132/* Record that a deletion is about to take place,
e928d437 133 of the characters in STRING, at location BEG. */
c6953be1 134
ff1aa840 135void
e928d437
RS
136record_delete (beg, string)
137 int beg;
138 Lisp_Object string;
c6953be1 139{
e928d437 140 Lisp_Object sbeg;
c6953be1 141
bdbe6f28
RS
142 if (EQ (current_buffer->undo_list, Qt))
143 return;
144
6396140a 145 if (PT == beg + XSTRING (string)->size)
cbc1b668 146 {
6396140a
SM
147 XSETINT (sbeg, -beg);
148 record_point (PT);
cbc1b668
KH
149 }
150 else
6396140a
SM
151 {
152 XSETFASTINT (sbeg, beg);
153 record_point (beg);
154 }
350bce56 155
c6953be1 156 current_buffer->undo_list
e928d437 157 = Fcons (Fcons (string, sbeg), current_buffer->undo_list);
c6953be1
JB
158}
159
714bced9
RS
160/* Record the fact that MARKER is about to be adjusted by ADJUSTMENT.
161 This is done only when a marker points within text being deleted,
162 because that's the only case where an automatic marker adjustment
163 won't be inverted automatically by undoing the buffer modification. */
164
ff1aa840 165void
714bced9
RS
166record_marker_adjustment (marker, adjustment)
167 Lisp_Object marker;
168 int adjustment;
169{
170 if (EQ (current_buffer->undo_list, Qt))
171 return;
172
173 /* Allocate a cons cell to be the undo boundary after this command. */
174 if (NILP (pending_boundary))
175 pending_boundary = Fcons (Qnil, Qnil);
176
2f33f38a
GM
177 if (!BUFFERP (last_undo_buffer)
178 || current_buffer != XBUFFER (last_undo_buffer))
714bced9
RS
179 Fundo_boundary ();
180 XSETBUFFER (last_undo_buffer, current_buffer);
181
182 current_buffer->undo_list
183 = Fcons (Fcons (marker, make_number (adjustment)),
184 current_buffer->undo_list);
185}
186
c6953be1
JB
187/* Record that a replacement is about to take place,
188 for LENGTH characters at location BEG.
e928d437 189 The replacement must not change the number of characters. */
c6953be1 190
ff1aa840 191void
c6953be1
JB
192record_change (beg, length)
193 int beg, length;
194{
e928d437 195 record_delete (beg, make_buffer_string (beg, beg + length, 1));
c6953be1
JB
196 record_insert (beg, length);
197}
198\f
199/* Record that an unmodified buffer is about to be changed.
200 Record the file modification date so that when undoing this entry
201 we can tell whether it is obsolete because the file was saved again. */
202
90dd3e4f 203void
c6953be1
JB
204record_first_change ()
205{
206 Lisp_Object high, low;
ad9cdce4 207 struct buffer *base_buffer = current_buffer;
0736cafe
RS
208
209 if (EQ (current_buffer->undo_list, Qt))
210 return;
211
2f33f38a
GM
212 if (!BUFFERP (last_undo_buffer)
213 || current_buffer != XBUFFER (last_undo_buffer))
0736cafe 214 Fundo_boundary ();
552bdbcf 215 XSETBUFFER (last_undo_buffer, current_buffer);
0736cafe 216
ad9cdce4
RS
217 if (base_buffer->base_buffer)
218 base_buffer = base_buffer->base_buffer;
219
220 XSETFASTINT (high, (base_buffer->modtime >> 16) & 0xffff);
221 XSETFASTINT (low, base_buffer->modtime & 0xffff);
c6953be1
JB
222 current_buffer->undo_list = Fcons (Fcons (Qt, Fcons (high, low)), current_buffer->undo_list);
223}
224
da9319d5
RS
225/* Record a change in property PROP (whose old value was VAL)
226 for LENGTH characters starting at position BEG in BUFFER. */
227
90dd3e4f 228void
da9319d5
RS
229record_property_change (beg, length, prop, value, buffer)
230 int beg, length;
231 Lisp_Object prop, value, buffer;
232{
233 Lisp_Object lbeg, lend, entry;
234 struct buffer *obuf = current_buffer;
235 int boundary = 0;
236
0736cafe 237 if (EQ (XBUFFER (buffer)->undo_list, Qt))
bdbe6f28
RS
238 return;
239
c58632fc
RS
240 /* Allocate a cons cell to be the undo boundary after this command. */
241 if (NILP (pending_boundary))
242 pending_boundary = Fcons (Qnil, Qnil);
243
da9319d5
RS
244 if (!EQ (buffer, last_undo_buffer))
245 boundary = 1;
246 last_undo_buffer = buffer;
247
da9319d5
RS
248 /* Switch temporarily to the buffer that was changed. */
249 current_buffer = XBUFFER (buffer);
250
251 if (boundary)
252 Fundo_boundary ();
253
ad9cdce4 254 if (MODIFF <= SAVE_MODIFF)
da9319d5
RS
255 record_first_change ();
256
552bdbcf
KH
257 XSETINT (lbeg, beg);
258 XSETINT (lend, beg + length);
da9319d5
RS
259 entry = Fcons (Qnil, Fcons (prop, Fcons (value, Fcons (lbeg, lend))));
260 current_buffer->undo_list = Fcons (entry, current_buffer->undo_list);
261
262 current_buffer = obuf;
263}
264
c6953be1 265DEFUN ("undo-boundary", Fundo_boundary, Sundo_boundary, 0, 0, 0,
8c1a1077
PJ
266 doc: /* Mark a boundary between units of undo.
267An undo command will stop at this point,
268but another undo command will undo to the previous boundary. */)
269 ()
c6953be1
JB
270{
271 Lisp_Object tem;
272 if (EQ (current_buffer->undo_list, Qt))
273 return Qnil;
274 tem = Fcar (current_buffer->undo_list);
265a9e55 275 if (!NILP (tem))
c58632fc
RS
276 {
277 /* One way or another, cons nil onto the front of the undo list. */
278 if (!NILP (pending_boundary))
279 {
280 /* If we have preallocated the cons cell to use here,
281 use that one. */
f3fbd155 282 XSETCDR (pending_boundary, current_buffer->undo_list);
c58632fc
RS
283 current_buffer->undo_list = pending_boundary;
284 pending_boundary = Qnil;
285 }
286 else
287 current_buffer->undo_list = Fcons (Qnil, current_buffer->undo_list);
288 }
c6953be1
JB
289 return Qnil;
290}
291
292/* At garbage collection time, make an undo list shorter at the end,
293 returning the truncated list.
294 MINSIZE and MAXSIZE are the limits on size allowed, as described below.
f06cd136
JB
295 In practice, these are the values of undo-limit and
296 undo-strong-limit. */
c6953be1
JB
297
298Lisp_Object
299truncate_undo_list (list, minsize, maxsize)
300 Lisp_Object list;
301 int minsize, maxsize;
302{
303 Lisp_Object prev, next, last_boundary;
304 int size_so_far = 0;
305
306 prev = Qnil;
307 next = list;
308 last_boundary = Qnil;
309
310 /* Always preserve at least the most recent undo record.
181a18b1
JB
311 If the first element is an undo boundary, skip past it.
312
313 Skip, skip, skip the undo, skip, skip, skip the undo,
07627b5d
JB
314 Skip, skip, skip the undo, skip to the undo bound'ry.
315 (Get it? "Skip to my Loo?") */
c1d497be 316 if (CONSP (next) && NILP (XCAR (next)))
c6953be1
JB
317 {
318 /* Add in the space occupied by this element and its chain link. */
319 size_so_far += sizeof (struct Lisp_Cons);
320
321 /* Advance to next element. */
322 prev = next;
c1d497be 323 next = XCDR (next);
c6953be1 324 }
c1d497be 325 while (CONSP (next) && ! NILP (XCAR (next)))
c6953be1
JB
326 {
327 Lisp_Object elt;
c1d497be 328 elt = XCAR (next);
c6953be1
JB
329
330 /* Add in the space occupied by this element and its chain link. */
331 size_so_far += sizeof (struct Lisp_Cons);
38c0d37c 332 if (CONSP (elt))
c6953be1
JB
333 {
334 size_so_far += sizeof (struct Lisp_Cons);
c1d497be 335 if (STRINGP (XCAR (elt)))
c6953be1 336 size_so_far += (sizeof (struct Lisp_String) - 1
c1d497be 337 + XSTRING (XCAR (elt))->size);
c6953be1
JB
338 }
339
340 /* Advance to next element. */
341 prev = next;
c1d497be 342 next = XCDR (next);
c6953be1 343 }
38c0d37c 344 if (CONSP (next))
c6953be1
JB
345 last_boundary = prev;
346
38c0d37c 347 while (CONSP (next))
c6953be1
JB
348 {
349 Lisp_Object elt;
c1d497be 350 elt = XCAR (next);
c6953be1
JB
351
352 /* When we get to a boundary, decide whether to truncate
353 either before or after it. The lower threshold, MINSIZE,
354 tells us to truncate after it. If its size pushes past
355 the higher threshold MAXSIZE as well, we truncate before it. */
265a9e55 356 if (NILP (elt))
c6953be1
JB
357 {
358 if (size_so_far > maxsize)
359 break;
360 last_boundary = prev;
361 if (size_so_far > minsize)
362 break;
363 }
364
365 /* Add in the space occupied by this element and its chain link. */
366 size_so_far += sizeof (struct Lisp_Cons);
38c0d37c 367 if (CONSP (elt))
c6953be1
JB
368 {
369 size_so_far += sizeof (struct Lisp_Cons);
c1d497be 370 if (STRINGP (XCAR (elt)))
c6953be1 371 size_so_far += (sizeof (struct Lisp_String) - 1
c1d497be 372 + XSTRING (XCAR (elt))->size);
c6953be1
JB
373 }
374
375 /* Advance to next element. */
376 prev = next;
c1d497be 377 next = XCDR (next);
c6953be1
JB
378 }
379
380 /* If we scanned the whole list, it is short enough; don't change it. */
265a9e55 381 if (NILP (next))
c6953be1
JB
382 return list;
383
384 /* Truncate at the boundary where we decided to truncate. */
265a9e55 385 if (!NILP (last_boundary))
c6953be1 386 {
f3fbd155 387 XSETCDR (last_boundary, Qnil);
c6953be1
JB
388 return list;
389 }
390 else
391 return Qnil;
392}
393\f
394DEFUN ("primitive-undo", Fprimitive_undo, Sprimitive_undo, 2, 2, 0,
8c1a1077
PJ
395 doc: /* Undo N records from the front of the list LIST.
396Return what remains of the list. */)
397 (n, list)
063fb61f 398 Lisp_Object n, list;
c6953be1 399{
de65837b
KH
400 struct gcpro gcpro1, gcpro2;
401 Lisp_Object next;
8c757fd7 402 int count = BINDING_STACK_SIZE ();
de65837b 403 register int arg;
8c757fd7 404
c6953be1
JB
405#if 0 /* This is a good feature, but would make undo-start
406 unable to do what is expected. */
407 Lisp_Object tem;
408
409 /* If the head of the list is a boundary, it is the boundary
410 preceding this command. Get rid of it and don't count it. */
411 tem = Fcar (list);
265a9e55 412 if (NILP (tem))
c6953be1
JB
413 list = Fcdr (list);
414#endif
415
b7826503 416 CHECK_NUMBER (n);
de65837b
KH
417 arg = XINT (n);
418 next = Qnil;
419 GCPRO2 (next, list);
420
38d56be3
GM
421 /* In a writable buffer, enable undoing read-only text that is so
422 because of text properties. */
423 if (NILP (current_buffer->read_only))
f87a68b3
RS
424 specbind (Qinhibit_read_only, Qt);
425
8c757fd7
GM
426 /* Don't let `intangible' properties interfere with undo. */
427 specbind (Qinhibit_point_motion_hooks, Qt);
428
c6953be1
JB
429 while (arg > 0)
430 {
c3b09bbf 431 while (CONSP (list))
c6953be1 432 {
c3b09bbf
SM
433 next = XCAR (list);
434 list = XCDR (list);
350bce56 435 /* Exit inner loop at undo boundary. */
265a9e55 436 if (NILP (next))
c6953be1 437 break;
350bce56 438 /* Handle an integer by setting point to that value. */
38c0d37c 439 if (INTEGERP (next))
350bce56 440 SET_PT (clip_to_bounds (BEGV, XINT (next), ZV));
38c0d37c 441 else if (CONSP (next))
c6953be1 442 {
350bce56
RS
443 Lisp_Object car, cdr;
444
c3b09bbf
SM
445 car = XCAR (next);
446 cdr = XCDR (next);
350bce56 447 if (EQ (car, Qt))
c6953be1 448 {
350bce56
RS
449 /* Element (t high . low) records previous modtime. */
450 Lisp_Object high, low;
451 int mod_time;
ad9cdce4 452 struct buffer *base_buffer = current_buffer;
350bce56
RS
453
454 high = Fcar (cdr);
455 low = Fcdr (cdr);
d8552b2f 456 mod_time = (XFASTINT (high) << 16) + XFASTINT (low);
ad9cdce4
RS
457
458 if (current_buffer->base_buffer)
459 base_buffer = current_buffer->base_buffer;
460
350bce56
RS
461 /* If this records an obsolete save
462 (not matching the actual disk file)
463 then don't mark unmodified. */
ad9cdce4 464 if (mod_time != base_buffer->modtime)
103dcb38 465 continue;
e6dd6080 466#ifdef CLASH_DETECTION
350bce56 467 Funlock_buffer ();
e6dd6080 468#endif /* CLASH_DETECTION */
350bce56 469 Fset_buffer_modified_p (Qnil);
c6953be1 470 }
d8552b2f 471 else if (EQ (car, Qnil))
da9319d5 472 {
d8552b2f 473 /* Element (nil prop val beg . end) is property change. */
da9319d5
RS
474 Lisp_Object beg, end, prop, val;
475
476 prop = Fcar (cdr);
477 cdr = Fcdr (cdr);
478 val = Fcar (cdr);
479 cdr = Fcdr (cdr);
480 beg = Fcar (cdr);
481 end = Fcdr (cdr);
482
483 Fput_text_property (beg, end, prop, val, Qnil);
484 }
38c0d37c 485 else if (INTEGERP (car) && INTEGERP (cdr))
c6953be1 486 {
350bce56 487 /* Element (BEG . END) means range was inserted. */
350bce56
RS
488
489 if (XINT (car) < BEGV
490 || XINT (cdr) > ZV)
c6953be1 491 error ("Changes to be undone are outside visible portion of buffer");
f28f04cc
RS
492 /* Set point first thing, so that undoing this undo
493 does not send point back to where it is now. */
350bce56 494 Fgoto_char (car);
f28f04cc 495 Fdelete_region (car, cdr);
350bce56 496 }
38c0d37c 497 else if (STRINGP (car) && INTEGERP (cdr))
350bce56
RS
498 {
499 /* Element (STRING . POS) means STRING was deleted. */
500 Lisp_Object membuf;
501 int pos = XINT (cdr);
502
503 membuf = car;
504 if (pos < 0)
505 {
506 if (-pos < BEGV || -pos > ZV)
507 error ("Changes to be undone are outside visible portion of buffer");
508 SET_PT (-pos);
509 Finsert (1, &membuf);
510 }
511 else
512 {
513 if (pos < BEGV || pos > ZV)
514 error ("Changes to be undone are outside visible portion of buffer");
515 SET_PT (pos);
516
b2adc409
RS
517 /* Now that we record marker adjustments
518 (caused by deletion) for undo,
519 we should always insert after markers,
520 so that undoing the marker adjustments
521 put the markers back in the right place. */
522 Finsert (1, &membuf);
350bce56
RS
523 SET_PT (pos);
524 }
c6953be1 525 }
714bced9
RS
526 else if (MARKERP (car) && INTEGERP (cdr))
527 {
528 /* (MARKER . INTEGER) means a marker MARKER
529 was adjusted by INTEGER. */
530 if (XMARKER (car)->buffer)
531 Fset_marker (car,
532 make_number (marker_position (car) - XINT (cdr)),
533 Fmarker_buffer (car));
534 }
c6953be1
JB
535 }
536 }
537 arg--;
538 }
539
de65837b 540 UNGCPRO;
f87a68b3 541 return unbind_to (count, list);
c6953be1
JB
542}
543
dfcf069d 544void
c6953be1
JB
545syms_of_undo ()
546{
f87a68b3
RS
547 Qinhibit_read_only = intern ("inhibit-read-only");
548 staticpro (&Qinhibit_read_only);
549
c58632fc
RS
550 pending_boundary = Qnil;
551 staticpro (&pending_boundary);
552
c6953be1
JB
553 defsubr (&Sprimitive_undo);
554 defsubr (&Sundo_boundary);
555}