(adjust_markers): Don't be confused by the gap
[bpt/emacs.git] / src / undo.c
CommitLineData
c6953be1 1/* undo handling for GNU Emacs.
3a22ee35 2 Copyright (C) 1990, 1993, 1994 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
c6953be1
JB
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.) */
43
44record_insert (beg, length)
53480e99 45 int beg, length;
c6953be1
JB
46{
47 Lisp_Object lbeg, lend;
48
bdbe6f28
RS
49 if (EQ (current_buffer->undo_list, Qt))
50 return;
51
c58632fc
RS
52 /* Allocate a cons cell to be the undo boundary after this command. */
53 if (NILP (pending_boundary))
54 pending_boundary = Fcons (Qnil, Qnil);
55
c6953be1
JB
56 if (current_buffer != XBUFFER (last_undo_buffer))
57 Fundo_boundary ();
552bdbcf 58 XSETBUFFER (last_undo_buffer, current_buffer);
c6953be1 59
ad9cdce4 60 if (MODIFF <= SAVE_MODIFF)
c6953be1
JB
61 record_first_change ();
62
63 /* If this is following another insertion and consecutive with it
64 in the buffer, combine the two. */
38c0d37c 65 if (CONSP (current_buffer->undo_list))
c6953be1
JB
66 {
67 Lisp_Object elt;
68 elt = XCONS (current_buffer->undo_list)->car;
38c0d37c
KH
69 if (CONSP (elt)
70 && INTEGERP (XCONS (elt)->car)
71 && INTEGERP (XCONS (elt)->cdr)
53480e99 72 && XINT (XCONS (elt)->cdr) == beg)
c6953be1 73 {
53480e99 74 XSETINT (XCONS (elt)->cdr, beg + length);
c6953be1
JB
75 return;
76 }
77 }
78
53480e99
KH
79 XSETFASTINT (lbeg, beg);
80 XSETINT (lend, beg + length);
213861c7
JB
81 current_buffer->undo_list = Fcons (Fcons (lbeg, lend),
82 current_buffer->undo_list);
c6953be1
JB
83}
84
85/* Record that a deletion is about to take place,
86 for LENGTH characters at location BEG. */
87
88record_delete (beg, length)
89 int beg, length;
90{
91 Lisp_Object lbeg, lend, sbeg;
e7a8b791 92 int at_boundary;
c6953be1 93
bdbe6f28
RS
94 if (EQ (current_buffer->undo_list, Qt))
95 return;
96
c58632fc
RS
97 /* Allocate a cons cell to be the undo boundary after this command. */
98 if (NILP (pending_boundary))
99 pending_boundary = Fcons (Qnil, Qnil);
100
c6953be1
JB
101 if (current_buffer != XBUFFER (last_undo_buffer))
102 Fundo_boundary ();
552bdbcf 103 XSETBUFFER (last_undo_buffer, current_buffer);
c6953be1 104
e7a8b791
RS
105 at_boundary = (CONSP (current_buffer->undo_list)
106 && NILP (XCONS (current_buffer->undo_list)->car));
107
ad9cdce4 108 if (MODIFF <= SAVE_MODIFF)
c6953be1
JB
109 record_first_change ();
110
6ec8bbd2 111 if (PT == beg + length)
552bdbcf 112 XSETINT (sbeg, -beg);
c6953be1 113 else
28b2b116
KH
114 XSETFASTINT (sbeg, beg);
115 XSETFASTINT (lbeg, beg);
116 XSETFASTINT (lend, beg + length);
350bce56 117
e7a8b791
RS
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))
350bce56 123 current_buffer->undo_list
4e665715 124 = Fcons (make_number (last_point_position), current_buffer->undo_list);
350bce56 125
c6953be1
JB
126 current_buffer->undo_list
127 = Fcons (Fcons (Fbuffer_substring (lbeg, lend), sbeg),
128 current_buffer->undo_list);
129}
130
714bced9
RS
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. */
135
136record_marker_adjustment (marker, adjustment)
137 Lisp_Object marker;
138 int adjustment;
139{
140 if (EQ (current_buffer->undo_list, Qt))
141 return;
142
143 /* Allocate a cons cell to be the undo boundary after this command. */
144 if (NILP (pending_boundary))
145 pending_boundary = Fcons (Qnil, Qnil);
146
147 if (current_buffer != XBUFFER (last_undo_buffer))
148 Fundo_boundary ();
149 XSETBUFFER (last_undo_buffer, current_buffer);
150
151 current_buffer->undo_list
152 = Fcons (Fcons (marker, make_number (adjustment)),
153 current_buffer->undo_list);
154}
155
c6953be1
JB
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. */
159
160record_change (beg, length)
161 int beg, length;
162{
163 record_delete (beg, length);
164 record_insert (beg, length);
165}
166\f
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. */
170
171record_first_change ()
172{
173 Lisp_Object high, low;
ad9cdce4 174 struct buffer *base_buffer = current_buffer;
0736cafe
RS
175
176 if (EQ (current_buffer->undo_list, Qt))
177 return;
178
179 if (current_buffer != XBUFFER (last_undo_buffer))
180 Fundo_boundary ();
552bdbcf 181 XSETBUFFER (last_undo_buffer, current_buffer);
0736cafe 182
ad9cdce4
RS
183 if (base_buffer->base_buffer)
184 base_buffer = base_buffer->base_buffer;
185
186 XSETFASTINT (high, (base_buffer->modtime >> 16) & 0xffff);
187 XSETFASTINT (low, base_buffer->modtime & 0xffff);
c6953be1
JB
188 current_buffer->undo_list = Fcons (Fcons (Qt, Fcons (high, low)), current_buffer->undo_list);
189}
190
da9319d5
RS
191/* Record a change in property PROP (whose old value was VAL)
192 for LENGTH characters starting at position BEG in BUFFER. */
193
194record_property_change (beg, length, prop, value, buffer)
195 int beg, length;
196 Lisp_Object prop, value, buffer;
197{
198 Lisp_Object lbeg, lend, entry;
199 struct buffer *obuf = current_buffer;
200 int boundary = 0;
201
0736cafe 202 if (EQ (XBUFFER (buffer)->undo_list, Qt))
bdbe6f28
RS
203 return;
204
c58632fc
RS
205 /* Allocate a cons cell to be the undo boundary after this command. */
206 if (NILP (pending_boundary))
207 pending_boundary = Fcons (Qnil, Qnil);
208
da9319d5
RS
209 if (!EQ (buffer, last_undo_buffer))
210 boundary = 1;
211 last_undo_buffer = buffer;
212
da9319d5
RS
213 /* Switch temporarily to the buffer that was changed. */
214 current_buffer = XBUFFER (buffer);
215
216 if (boundary)
217 Fundo_boundary ();
218
ad9cdce4 219 if (MODIFF <= SAVE_MODIFF)
da9319d5
RS
220 record_first_change ();
221
552bdbcf
KH
222 XSETINT (lbeg, beg);
223 XSETINT (lend, beg + length);
da9319d5
RS
224 entry = Fcons (Qnil, Fcons (prop, Fcons (value, Fcons (lbeg, lend))));
225 current_buffer->undo_list = Fcons (entry, current_buffer->undo_list);
226
227 current_buffer = obuf;
228}
229
c6953be1
JB
230DEFUN ("undo-boundary", Fundo_boundary, Sundo_boundary, 0, 0, 0,
231 "Mark a boundary between units of undo.\n\
232An undo command will stop at this point,\n\
233but another undo command will undo to the previous boundary.")
234 ()
235{
236 Lisp_Object tem;
237 if (EQ (current_buffer->undo_list, Qt))
238 return Qnil;
239 tem = Fcar (current_buffer->undo_list);
265a9e55 240 if (!NILP (tem))
c58632fc
RS
241 {
242 /* One way or another, cons nil onto the front of the undo list. */
243 if (!NILP (pending_boundary))
244 {
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;
250 }
251 else
252 current_buffer->undo_list = Fcons (Qnil, current_buffer->undo_list);
253 }
c6953be1
JB
254 return Qnil;
255}
256
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.
f06cd136
JB
260 In practice, these are the values of undo-limit and
261 undo-strong-limit. */
c6953be1
JB
262
263Lisp_Object
264truncate_undo_list (list, minsize, maxsize)
265 Lisp_Object list;
266 int minsize, maxsize;
267{
268 Lisp_Object prev, next, last_boundary;
269 int size_so_far = 0;
270
271 prev = Qnil;
272 next = list;
273 last_boundary = Qnil;
274
275 /* Always preserve at least the most recent undo record.
181a18b1
JB
276 If the first element is an undo boundary, skip past it.
277
278 Skip, skip, skip the undo, skip, skip, skip the undo,
07627b5d
JB
279 Skip, skip, skip the undo, skip to the undo bound'ry.
280 (Get it? "Skip to my Loo?") */
38c0d37c 281 if (CONSP (next) && NILP (XCONS (next)->car))
c6953be1
JB
282 {
283 /* Add in the space occupied by this element and its chain link. */
284 size_so_far += sizeof (struct Lisp_Cons);
285
286 /* Advance to next element. */
287 prev = next;
288 next = XCONS (next)->cdr;
289 }
38c0d37c 290 while (CONSP (next) && ! NILP (XCONS (next)->car))
c6953be1
JB
291 {
292 Lisp_Object elt;
293 elt = XCONS (next)->car;
294
295 /* Add in the space occupied by this element and its chain link. */
296 size_so_far += sizeof (struct Lisp_Cons);
38c0d37c 297 if (CONSP (elt))
c6953be1
JB
298 {
299 size_so_far += sizeof (struct Lisp_Cons);
38c0d37c 300 if (STRINGP (XCONS (elt)->car))
c6953be1
JB
301 size_so_far += (sizeof (struct Lisp_String) - 1
302 + XSTRING (XCONS (elt)->car)->size);
303 }
304
305 /* Advance to next element. */
306 prev = next;
307 next = XCONS (next)->cdr;
308 }
38c0d37c 309 if (CONSP (next))
c6953be1
JB
310 last_boundary = prev;
311
38c0d37c 312 while (CONSP (next))
c6953be1
JB
313 {
314 Lisp_Object elt;
315 elt = XCONS (next)->car;
316
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. */
265a9e55 321 if (NILP (elt))
c6953be1
JB
322 {
323 if (size_so_far > maxsize)
324 break;
325 last_boundary = prev;
326 if (size_so_far > minsize)
327 break;
328 }
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);
38c0d37c 335 if (STRINGP (XCONS (elt)->car))
c6953be1
JB
336 size_so_far += (sizeof (struct Lisp_String) - 1
337 + XSTRING (XCONS (elt)->car)->size);
338 }
339
340 /* Advance to next element. */
341 prev = next;
342 next = XCONS (next)->cdr;
343 }
344
345 /* If we scanned the whole list, it is short enough; don't change it. */
265a9e55 346 if (NILP (next))
c6953be1
JB
347 return list;
348
349 /* Truncate at the boundary where we decided to truncate. */
265a9e55 350 if (!NILP (last_boundary))
c6953be1
JB
351 {
352 XCONS (last_boundary)->cdr = Qnil;
353 return list;
354 }
355 else
356 return Qnil;
357}
358\f
359DEFUN ("primitive-undo", Fprimitive_undo, Sprimitive_undo, 2, 2, 0,
360 "Undo N records from the front of the list LIST.\n\
361Return what remains of the list.")
063fb61f
RS
362 (n, list)
363 Lisp_Object n, list;
c6953be1 364{
de65837b
KH
365 struct gcpro gcpro1, gcpro2;
366 Lisp_Object next;
f87a68b3 367 int count = specpdl_ptr - specpdl;
de65837b 368 register int arg;
c6953be1
JB
369#if 0 /* This is a good feature, but would make undo-start
370 unable to do what is expected. */
371 Lisp_Object tem;
372
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);
265a9e55 376 if (NILP (tem))
c6953be1
JB
377 list = Fcdr (list);
378#endif
379
de65837b
KH
380 CHECK_NUMBER (n, 0);
381 arg = XINT (n);
382 next = Qnil;
383 GCPRO2 (next, list);
384
f87a68b3
RS
385 /* Don't let read-only properties interfere with undo. */
386 if (NILP (current_buffer->read_only))
387 specbind (Qinhibit_read_only, Qt);
388
c6953be1
JB
389 while (arg > 0)
390 {
391 while (1)
392 {
c6953be1
JB
393 next = Fcar (list);
394 list = Fcdr (list);
350bce56 395 /* Exit inner loop at undo boundary. */
265a9e55 396 if (NILP (next))
c6953be1 397 break;
350bce56 398 /* Handle an integer by setting point to that value. */
38c0d37c 399 if (INTEGERP (next))
350bce56 400 SET_PT (clip_to_bounds (BEGV, XINT (next), ZV));
38c0d37c 401 else if (CONSP (next))
c6953be1 402 {
350bce56
RS
403 Lisp_Object car, cdr;
404
405 car = Fcar (next);
406 cdr = Fcdr (next);
407 if (EQ (car, Qt))
c6953be1 408 {
350bce56
RS
409 /* Element (t high . low) records previous modtime. */
410 Lisp_Object high, low;
411 int mod_time;
ad9cdce4 412 struct buffer *base_buffer = current_buffer;
350bce56
RS
413
414 high = Fcar (cdr);
415 low = Fcdr (cdr);
d8552b2f 416 mod_time = (XFASTINT (high) << 16) + XFASTINT (low);
ad9cdce4
RS
417
418 if (current_buffer->base_buffer)
419 base_buffer = current_buffer->base_buffer;
420
350bce56
RS
421 /* If this records an obsolete save
422 (not matching the actual disk file)
423 then don't mark unmodified. */
ad9cdce4 424 if (mod_time != base_buffer->modtime)
103dcb38 425 continue;
e6dd6080 426#ifdef CLASH_DETECTION
350bce56 427 Funlock_buffer ();
e6dd6080 428#endif /* CLASH_DETECTION */
350bce56 429 Fset_buffer_modified_p (Qnil);
c6953be1 430 }
d8552b2f
RS
431#ifdef USE_TEXT_PROPERTIES
432 else if (EQ (car, Qnil))
da9319d5 433 {
d8552b2f 434 /* Element (nil prop val beg . end) is property change. */
da9319d5
RS
435 Lisp_Object beg, end, prop, val;
436
437 prop = Fcar (cdr);
438 cdr = Fcdr (cdr);
439 val = Fcar (cdr);
440 cdr = Fcdr (cdr);
441 beg = Fcar (cdr);
442 end = Fcdr (cdr);
443
444 Fput_text_property (beg, end, prop, val, Qnil);
445 }
d8552b2f 446#endif /* USE_TEXT_PROPERTIES */
38c0d37c 447 else if (INTEGERP (car) && INTEGERP (cdr))
c6953be1 448 {
350bce56
RS
449 /* Element (BEG . END) means range was inserted. */
450 Lisp_Object end;
451
452 if (XINT (car) < BEGV
453 || XINT (cdr) > ZV)
c6953be1 454 error ("Changes to be undone are outside visible portion of buffer");
f28f04cc
RS
455 /* Set point first thing, so that undoing this undo
456 does not send point back to where it is now. */
350bce56 457 Fgoto_char (car);
f28f04cc 458 Fdelete_region (car, cdr);
350bce56 459 }
38c0d37c 460 else if (STRINGP (car) && INTEGERP (cdr))
350bce56
RS
461 {
462 /* Element (STRING . POS) means STRING was deleted. */
463 Lisp_Object membuf;
464 int pos = XINT (cdr);
465
466 membuf = car;
467 if (pos < 0)
468 {
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);
473 }
474 else
475 {
476 if (pos < BEGV || pos > ZV)
477 error ("Changes to be undone are outside visible portion of buffer");
478 SET_PT (pos);
479
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);
489 }
c6953be1 490 }
714bced9
RS
491 else if (MARKERP (car) && INTEGERP (cdr))
492 {
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));
499 }
c6953be1
JB
500 }
501 }
502 arg--;
503 }
504
de65837b 505 UNGCPRO;
f87a68b3 506 return unbind_to (count, list);
c6953be1
JB
507}
508
509syms_of_undo ()
510{
f87a68b3
RS
511 Qinhibit_read_only = intern ("inhibit-read-only");
512 staticpro (&Qinhibit_read_only);
513
c58632fc
RS
514 pending_boundary = Qnil;
515 staticpro (&pending_boundary);
516
c6953be1
JB
517 defsubr (&Sprimitive_undo);
518 defsubr (&Sundo_boundary);
519}