1 /* undo handling for XEmacs.
2 Copyright (C) 1990, 1992, 1993, 1994 Free Software Foundation, Inc.
4 This file is part of XEmacs.
6 XEmacs is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with XEmacs; 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. */
21 /* Synched up with: FSF 19.28. */
23 /* This file has been Mule-ized. */
30 /* Maintained in event-stream.c */
31 extern Bufpos last_point_position;
32 extern Lisp_Object last_point_position_buffer;
34 /* Extent code needs to know about undo because the behavior of insert()
35 with regard to extents varies depending on whether we are inside
39 /* Last buffer for which undo information was recorded. */
40 static Lisp_Object last_undo_buffer;
42 Lisp_Object Qinhibit_read_only;
44 /* The first time a command records something for undo.
45 it also allocates the undo-boundary object
46 which will be added to the list at the end of the command.
47 This ensures we can't run out of space while trying to make
49 static Lisp_Object pending_boundary;
52 undo_boundary (struct buffer *b)
54 Lisp_Object tem = Fcar (b->undo_list);
57 /* One way or another, cons nil onto the front of the undo list. */
58 if (CONSP (pending_boundary))
60 /* If we have preallocated the cons cell to use here,
62 XCDR (pending_boundary) = b->undo_list;
63 b->undo_list = pending_boundary;
64 pending_boundary = Qnil;
67 b->undo_list = Fcons (Qnil, b->undo_list);
73 undo_prelude (struct buffer *b, int hack_pending_boundary)
75 if (EQ (b->undo_list, Qt))
78 if (NILP (last_undo_buffer)
79 || (BUFFER_BASE_BUFFER (b)
80 != BUFFER_BASE_BUFFER (XBUFFER (last_undo_buffer))))
83 XSETBUFFER (last_undo_buffer, b);
86 /* Allocate a cons cell to be the undo boundary after this command. */
87 if (hack_pending_boundary && NILP (pending_boundary))
88 pending_boundary = Fcons (Qnil, Qnil);
90 if (BUF_MODIFF (b) <= BUF_SAVE_MODIFF (b))
92 /* Record that an unmodified buffer is about to be changed.
93 Record the file modification date so that when undoing this
94 entry we can tell whether it is obsolete because the file was
98 Fcons (make_int ((b->modtime >> 16) & 0xffff),
99 make_int (b->modtime & 0xffff))),
108 restore_inside_undo (Lisp_Object val)
110 inside_undo = XINT (val);
115 /* Record an insertion that just happened or is about to happen,
116 for LENGTH characters at position BEG.
117 (It is possible to record an insertion before or after the fact
118 because we don't need to record the contents.) */
121 record_insert (struct buffer *b, Bufpos beg, Charcount length)
123 if (!undo_prelude (b, 1))
126 /* If this is following another insertion and consecutive with it
127 in the buffer, combine the two. */
128 if (CONSP (b->undo_list))
131 elt = XCAR (b->undo_list);
135 && XINT (XCDR (elt)) == beg)
137 XCDR (elt) = make_int (beg + length);
142 b->undo_list = Fcons (Fcons (make_int (beg),
143 make_int (beg + length)),
147 /* Record that a deletion is about to take place,
148 for LENGTH characters at location BEG. */
151 record_delete (struct buffer *b, Bufpos beg, Charcount length)
153 /* This function can GC */
157 if (!undo_prelude (b, 1))
160 at_boundary = (CONSP (b->undo_list)
161 && NILP (XCAR (b->undo_list)));
163 if (BUF_PT (b) == beg + length)
164 sbeg = make_int (-beg);
166 sbeg = make_int (beg);
168 /* If we are just after an undo boundary, and
169 point wasn't at start of deleted range, record where it was. */
171 && BUFFERP (last_point_position_buffer)
172 && b == XBUFFER (last_point_position_buffer)
173 && last_point_position != XINT (sbeg))
174 b->undo_list = Fcons (make_int (last_point_position), b->undo_list);
176 b->undo_list = Fcons (Fcons (make_string_from_buffer (b, beg,
182 /* Record that a replacement is about to take place,
183 for LENGTH characters at location BEG.
184 The replacement does not change the number of characters. */
187 record_change (struct buffer *b, Bufpos beg, Charcount length)
189 record_delete (b, beg, length);
190 record_insert (b, beg, length);
193 /* Record that an EXTENT is about to be attached or detached in its buffer.
194 This works much like a deletion or insertion, except that there's no string.
195 The tricky part is that the buffer we operate on comes from EXTENT.
196 Most extent changes happen as a side effect of string insertion and
197 deletion; this call is solely for Fdetach_extent() and Finsert_extent().
200 record_extent (Lisp_Object extent, int attached)
202 Lisp_Object obj = Fextent_object (extent);
207 struct buffer *b = XBUFFER (obj);
208 if (!undo_prelude (b, 1))
213 token = list3 (extent, Fextent_start_position (extent),
214 Fextent_end_position (extent));
215 b->undo_list = Fcons (token, b->undo_list);
222 /* Record a change in property PROP (whose old value was VAL)
223 for LENGTH characters starting at position BEG in BUFFER. */
225 record_property_change (Bufpos beg, Charcount length,
226 Lisp_Object prop, Lisp_Object value,
229 Lisp_Object lbeg, lend, entry;
230 struct buffer *b = XBUFFER (buffer);
232 if (!undo_prelude (b, 1))
235 lbeg = make_int (beg);
236 lend = make_int (beg + length);
237 entry = Fcons (Qnil, Fcons (prop, Fcons (value, Fcons (lbeg, lend))));
238 b->undo_list = Fcons (entry, b->undo_list);
243 DEFUN ("undo-boundary", Fundo_boundary, 0, 0, 0, /*
244 Mark a boundary between units of undo.
245 An undo command will stop at this point,
246 but another undo command will undo to the previous boundary.
250 if (EQ (current_buffer->undo_list, Qt))
252 undo_boundary (current_buffer);
256 /* At garbage collection time, make an undo list shorter at the end,
257 returning the truncated list.
258 MINSIZE and MAXSIZE are the limits on size allowed, as described below.
259 In practice, these are the values of undo-threshold and
260 undo-high-threshold. */
263 truncate_undo_list (Lisp_Object list, int minsize, int maxsize)
265 Lisp_Object prev, next, last_boundary;
268 if (!(minsize > 0 || maxsize > 0))
273 last_boundary = Qnil;
278 /* Always preserve at least the most recent undo record.
279 If the first element is an undo boundary, skip past it. */
281 && NILP (XCAR (next)))
283 /* Add in the space occupied by this element and its chain link. */
284 size_so_far += sizeof (Lisp_Cons);
286 /* Advance to next element. */
291 && !NILP (XCAR (next)))
296 /* Add in the space occupied by this element and its chain link. */
297 size_so_far += sizeof (Lisp_Cons);
300 size_so_far += sizeof (Lisp_Cons);
301 if (STRINGP (XCAR (elt)))
302 size_so_far += (sizeof (Lisp_String) - 1
303 + XSTRING_LENGTH (XCAR (elt)));
306 /* Advance to next element. */
311 last_boundary = prev;
318 /* When we get to a boundary, decide whether to truncate
319 either before or after it. The lower threshold, MINSIZE,
320 tells us to truncate after it. If its size pushes past
321 the higher threshold MAXSIZE as well, we truncate before it. */
324 if (size_so_far > maxsize && maxsize > 0)
326 last_boundary = prev;
327 if (size_so_far > minsize && minsize > 0)
331 /* Add in the space occupied by this element and its chain link. */
332 size_so_far += sizeof (Lisp_Cons);
335 size_so_far += sizeof (Lisp_Cons);
336 if (STRINGP (XCAR (elt)))
337 size_so_far += (sizeof (Lisp_String) - 1
338 + XSTRING_LENGTH (XCAR (elt)));
341 /* Advance to next element. */
346 /* If we scanned the whole list, it is short enough; don't change it. */
350 /* Truncate at the boundary where we decided to truncate. */
351 if (!NILP (last_boundary))
353 XCDR (last_boundary) = Qnil;
360 DEFUN ("primitive-undo", Fprimitive_undo, 2, 2, 0, /*
361 Undo COUNT records from the front of the list LIST.
362 Return what remains of the list.
366 struct gcpro gcpro1, gcpro2;
367 Lisp_Object next = Qnil;
368 /* This function can GC */
370 int speccount = specpdl_depth ();
372 record_unwind_protect (restore_inside_undo, make_int (inside_undo));
375 #if 0 /* This is a good feature, but would make undo-start
376 unable to do what is expected. */
379 /* If the head of the list is a boundary, it is the boundary
380 preceding this command. Get rid of it and don't count it. */
391 /* Don't let read-only properties interfere with undo. */
392 if (NILP (current_buffer->read_only))
393 specbind (Qinhibit_read_only, Qt);
401 else if (!CONSP (list))
405 /* Exit inner loop at undo boundary. */
408 /* Handle an integer by setting point to that value. */
409 else if (INTP (next))
410 BUF_SET_PT (current_buffer,
411 bufpos_clip_to_bounds (BUF_BEGV (current_buffer),
413 BUF_ZV (current_buffer)));
414 else if (CONSP (next))
416 Lisp_Object car = XCAR (next);
417 Lisp_Object cdr = XCDR (next);
421 /* Element (t high . low) records previous modtime. */
422 Lisp_Object high, low;
424 if (!CONSP (cdr)) goto rotten;
427 if (!INTP (high) || !INTP (low)) goto rotten;
428 mod_time = (XINT (high) << 16) + XINT (low);
429 /* If this records an obsolete save
430 (not matching the actual disk file)
431 then don't mark unmodified. */
432 if (mod_time != current_buffer->modtime)
434 #ifdef CLASH_DETECTION
436 #endif /* CLASH_DETECTION */
437 /* may GC under ENERGIZE: */
438 Fset_buffer_modified_p (Qnil, Qnil);
440 else if (EXTENTP (car))
442 /* Element (extent start end) means that EXTENT was
443 detached, and we need to reattach it. */
444 Lisp_Object extent_obj, start, end;
448 end = Fcar (Fcdr (cdr));
450 if (!INTP (start) || !INTP (end))
452 Fset_extent_endpoints (extent_obj, start, end,
456 else if (EQ (car, Qnil))
458 /* Element (nil prop val beg . end) is property change. */
459 Lisp_Object beg, end, prop, val;
468 Fput_text_property (beg, end, prop, val, Qnil);
471 else if (INTP (car) && INTP (cdr))
473 /* Element (BEG . END) means range was inserted. */
475 if (XINT (car) < BUF_BEGV (current_buffer)
476 || XINT (cdr) > BUF_ZV (current_buffer))
477 error ("Changes to be undone are outside visible portion of buffer");
478 /* Set point first thing, so that undoing this undo
479 does not send point back to where it is now. */
480 Fgoto_char (car, Qnil);
481 Fdelete_region (car, cdr, Qnil);
483 else if (STRINGP (car) && INTP (cdr))
485 /* Element (STRING . POS) means STRING was deleted. */
486 Lisp_Object membuf = car;
487 int pos = XINT (cdr);
491 if (-pos < BUF_BEGV (current_buffer) || -pos > BUF_ZV (current_buffer))
492 error ("Changes to be undone are outside visible portion of buffer");
493 BUF_SET_PT (current_buffer, -pos);
494 Finsert (1, &membuf);
498 if (pos < BUF_BEGV (current_buffer) || pos > BUF_ZV (current_buffer))
499 error ("Changes to be undone are outside visible portion of buffer");
500 BUF_SET_PT (current_buffer, pos);
502 /* Insert before markers so that if the mark is
503 currently on the boundary of this deletion, it
504 ends up on the other side of the now-undeleted
505 text from point. Since undo doesn't even keep
506 track of the mark, this isn't really necessary,
507 but it may lead to better behavior in certain
510 I'm doubtful that this is safe; you could mess
511 up the process-output mark in shell buffers, so
512 until I hear a compelling reason for this change,
513 I'm leaving it out. -jwz
515 /* Finsert_before_markers (1, &membuf); */
516 Finsert (1, &membuf);
517 BUF_SET_PT (current_buffer, pos);
525 else if (EXTENTP (next))
526 Fdetach_extent (next);
530 signal_simple_continuable_error
531 ("Something rotten in the state of undo", next);
538 return unbind_to (speccount, list);
544 DEFSUBR (Fprimitive_undo);
545 DEFSUBR (Fundo_boundary);
546 defsymbol (&Qinhibit_read_only, "inhibit-read-only");
550 reinit_vars_of_undo (void)
558 reinit_vars_of_undo ();
560 pending_boundary = Qnil;
561 staticpro (&pending_boundary);
562 last_undo_buffer = Qnil;
563 staticpro (&last_undo_buffer);