0
|
1 /* undo handling for XEmacs.
|
|
2 Copyright (C) 1990, 1992, 1993, 1994 Free Software Foundation, Inc.
|
|
3
|
|
4 This file is part of XEmacs.
|
|
5
|
|
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
|
|
9 later version.
|
|
10
|
|
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
|
|
14 for more details.
|
|
15
|
|
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. */
|
|
20
|
|
21 /* Synched up with: FSF 19.28. */
|
|
22
|
|
23 /* This file has been Mule-ized. */
|
|
24
|
|
25 #include <config.h>
|
|
26 #include "lisp.h"
|
|
27 #include "buffer.h"
|
|
28 #include "extents.h"
|
|
29
|
|
30 /* Maintained in event-stream.c */
|
|
31 extern Bufpos last_point_position;
|
|
32 extern Lisp_Object last_point_position_buffer;
|
|
33
|
|
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
|
|
36 an undo or not. */
|
|
37 int inside_undo;
|
|
38
|
|
39 /* Last buffer for which undo information was recorded. */
|
|
40 static Lisp_Object last_undo_buffer;
|
|
41
|
|
42 Lisp_Object Qinhibit_read_only;
|
|
43
|
|
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
|
|
48 an undo-boundary. */
|
|
49 Lisp_Object pending_boundary;
|
|
50
|
|
51 static void
|
|
52 undo_boundary (struct buffer *b)
|
|
53 {
|
|
54 Lisp_Object tem = Fcar (b->undo_list);
|
|
55 if (!NILP (tem))
|
|
56 {
|
|
57 /* One way or another, cons nil onto the front of the undo list. */
|
|
58 if (CONSP (pending_boundary))
|
|
59 {
|
|
60 /* If we have preallocated the cons cell to use here,
|
|
61 use that one. */
|
|
62 XCDR (pending_boundary) = b->undo_list;
|
|
63 b->undo_list = pending_boundary;
|
|
64 pending_boundary = Qnil;
|
|
65 }
|
|
66 else
|
|
67 b->undo_list = Fcons (Qnil, b->undo_list);
|
|
68 }
|
|
69 }
|
|
70
|
|
71
|
|
72 static int
|
|
73 undo_prelude (struct buffer *b, int hack_pending_boundary)
|
|
74 {
|
|
75 if (EQ (b->undo_list, Qt))
|
|
76 return (0);
|
|
77
|
|
78 if (NILP (last_undo_buffer) || b != XBUFFER (last_undo_buffer))
|
|
79 {
|
|
80 undo_boundary (b);
|
|
81 XSETBUFFER (last_undo_buffer, b);
|
|
82 }
|
185
|
83
|
0
|
84 /* Allocate a cons cell to be the undo boundary after this command. */
|
|
85 if (hack_pending_boundary && NILP (pending_boundary))
|
|
86 pending_boundary = Fcons (Qnil, Qnil);
|
|
87
|
|
88 if (BUF_MODIFF (b) <= BUF_SAVE_MODIFF (b))
|
|
89 {
|
|
90 /* Record that an unmodified buffer is about to be changed.
|
|
91 Record the file modification date so that when undoing this entry
|
|
92 we can tell whether it is obsolete because the file was saved again. */
|
|
93 b->undo_list
|
|
94 = Fcons (Fcons (Qt,
|
|
95 Fcons (make_int ((b->modtime >> 16) & 0xffff),
|
|
96 make_int (b->modtime & 0xffff))),
|
|
97 b->undo_list);
|
|
98 }
|
|
99 return (1);
|
|
100 }
|
|
101
|
|
102
|
|
103
|
|
104 static Lisp_Object
|
|
105 restore_inside_undo (Lisp_Object val)
|
|
106 {
|
|
107 inside_undo = XINT (val);
|
|
108 return val;
|
|
109 }
|
|
110
|
|
111
|
|
112 /* Record an insertion that just happened or is about to happen,
|
|
113 for LENGTH characters at position BEG.
|
|
114 (It is possible to record an insertion before or after the fact
|
|
115 because we don't need to record the contents.) */
|
|
116
|
|
117 void
|
|
118 record_insert (struct buffer *b, Bufpos beg, Charcount length)
|
|
119 {
|
|
120 if (!undo_prelude (b, 1))
|
|
121 return;
|
|
122
|
|
123 /* If this is following another insertion and consecutive with it
|
|
124 in the buffer, combine the two. */
|
|
125 if (CONSP (b->undo_list))
|
|
126 {
|
|
127 Lisp_Object elt;
|
|
128 elt = XCAR (b->undo_list);
|
|
129 if (CONSP (elt)
|
|
130 && INTP (XCAR (elt))
|
|
131 && INTP (XCDR (elt))
|
|
132 && XINT (XCDR (elt)) == beg)
|
|
133 {
|
|
134 XCDR (elt) = make_int (beg + length);
|
|
135 return;
|
|
136 }
|
|
137 }
|
|
138
|
185
|
139 b->undo_list = Fcons (Fcons (make_int (beg),
|
0
|
140 make_int (beg + length)),
|
|
141 b->undo_list);
|
|
142 }
|
|
143
|
|
144 /* Record that a deletion is about to take place,
|
|
145 for LENGTH characters at location BEG. */
|
|
146
|
|
147 void
|
|
148 record_delete (struct buffer *b, Bufpos beg, Charcount length)
|
|
149 {
|
|
150 /* This function can GC */
|
|
151 Lisp_Object sbeg;
|
|
152 int at_boundary;
|
|
153
|
|
154 if (!undo_prelude (b, 1))
|
|
155 return;
|
|
156
|
|
157 at_boundary = (CONSP (b->undo_list)
|
|
158 && NILP (XCAR (b->undo_list)));
|
|
159
|
|
160 if (BUF_PT (b) == beg + length)
|
|
161 sbeg = make_int (-beg);
|
|
162 else
|
|
163 sbeg = make_int (beg);
|
|
164
|
185
|
165 /* If we are just after an undo boundary, and
|
0
|
166 point wasn't at start of deleted range, record where it was. */
|
|
167 if (at_boundary
|
|
168 && BUFFERP (last_point_position_buffer)
|
|
169 && b == XBUFFER (last_point_position_buffer)
|
|
170 && last_point_position != XINT (sbeg))
|
|
171 b->undo_list = Fcons (make_int (last_point_position), b->undo_list);
|
|
172
|
|
173 b->undo_list = Fcons (Fcons (make_string_from_buffer (b, beg,
|
|
174 length),
|
|
175 sbeg),
|
|
176 b->undo_list);
|
|
177 }
|
|
178
|
|
179 /* Record that a replacement is about to take place,
|
|
180 for LENGTH characters at location BEG.
|
|
181 The replacement does not change the number of characters. */
|
|
182
|
|
183 void
|
|
184 record_change (struct buffer *b, Bufpos beg, Charcount length)
|
|
185 {
|
|
186 record_delete (b, beg, length);
|
|
187 record_insert (b, beg, length);
|
|
188 }
|
|
189
|
|
190 /* Record that an EXTENT is about to be attached or detached in its buffer.
|
|
191 This works much like a deletion or insertion, except that there's no string.
|
|
192 The tricky part is that the buffer we operate on comes from EXTENT.
|
|
193 Most extent changes happen as a side effect of string insertion and
|
|
194 deletion; this call is solely for Fdetach_extent() and Finsert_extent().
|
|
195 */
|
|
196 void
|
|
197 record_extent (Lisp_Object extent, int attached)
|
|
198 {
|
167
|
199 Lisp_Object obj = Fextent_object (extent);
|
0
|
200
|
167
|
201 if (BUFFERP (obj))
|
|
202 {
|
|
203 Lisp_Object token;
|
|
204 struct buffer *b = XBUFFER (obj);
|
|
205 if (!undo_prelude (b, 1))
|
|
206 return;
|
|
207 if (attached)
|
|
208 token = extent;
|
|
209 else
|
|
210 token = list3 (extent, Fextent_start_position (extent),
|
|
211 Fextent_end_position (extent));
|
|
212 b->undo_list = Fcons (token, b->undo_list);
|
|
213 }
|
0
|
214 else
|
167
|
215 return;
|
0
|
216 }
|
|
217
|
|
218 #if 0 /* FSFmacs */
|
|
219 /* Record a change in property PROP (whose old value was VAL)
|
|
220 for LENGTH characters starting at position BEG in BUFFER. */
|
|
221
|
|
222 record_property_change (Bufpos beg, Charcount length,
|
|
223 Lisp_Object prop, Lisp_Object value,
|
|
224 Lisp_Object buffer)
|
|
225 {
|
|
226 Lisp_Object lbeg, lend, entry;
|
|
227 struct buffer *b = XBUFFER (buffer);
|
|
228
|
|
229 if (!undo_prelude (b, 1))
|
|
230 return;
|
|
231
|
|
232 lbeg = make_int (beg);
|
|
233 lend = make_int (beg + length);
|
|
234 entry = Fcons (Qnil, Fcons (prop, Fcons (value, Fcons (lbeg, lend))));
|
|
235 b->undo_list = Fcons (entry, b->undo_list);
|
|
236 }
|
|
237 #endif /* FSFmacs */
|
|
238
|
|
239
|
20
|
240 DEFUN ("undo-boundary", Fundo_boundary, 0, 0, 0, /*
|
0
|
241 Mark a boundary between units of undo.
|
|
242 An undo command will stop at this point,
|
|
243 but another undo command will undo to the previous boundary.
|
20
|
244 */
|
|
245 ())
|
0
|
246 {
|
|
247 if (EQ (current_buffer->undo_list, Qt))
|
|
248 return Qnil;
|
|
249 undo_boundary (current_buffer);
|
|
250 return Qnil;
|
|
251 }
|
|
252
|
|
253 /* At garbage collection time, make an undo list shorter at the end,
|
|
254 returning the truncated list.
|
|
255 MINSIZE and MAXSIZE are the limits on size allowed, as described below.
|
|
256 In practice, these are the values of undo-threshold and
|
|
257 undo-high-threshold. */
|
|
258
|
|
259 Lisp_Object
|
|
260 truncate_undo_list (Lisp_Object list, int minsize, int maxsize)
|
|
261 {
|
|
262 Lisp_Object prev, next, last_boundary;
|
|
263 int size_so_far = 0;
|
|
264
|
|
265 if (!(minsize > 0 || maxsize > 0))
|
|
266 return list;
|
|
267
|
|
268 prev = Qnil;
|
|
269 next = list;
|
|
270 last_boundary = Qnil;
|
|
271
|
|
272 if (!CONSP (list))
|
|
273 return (list);
|
|
274
|
|
275 /* Always preserve at least the most recent undo record.
|
|
276 If the first element is an undo boundary, skip past it. */
|
|
277 if (CONSP (next)
|
|
278 && NILP (XCAR (next)))
|
|
279 {
|
|
280 /* Add in the space occupied by this element and its chain link. */
|
|
281 size_so_far += sizeof (struct Lisp_Cons);
|
|
282
|
|
283 /* Advance to next element. */
|
|
284 prev = next;
|
|
285 next = XCDR (next);
|
|
286 }
|
|
287 while (CONSP (next)
|
|
288 && !NILP (XCAR (next)))
|
|
289 {
|
|
290 Lisp_Object elt;
|
|
291 elt = XCAR (next);
|
|
292
|
|
293 /* Add in the space occupied by this element and its chain link. */
|
|
294 size_so_far += sizeof (struct Lisp_Cons);
|
|
295 if (CONSP (elt))
|
|
296 {
|
|
297 size_so_far += sizeof (struct Lisp_Cons);
|
|
298 if (STRINGP (XCAR (elt)))
|
|
299 size_so_far += (sizeof (struct Lisp_String) - 1
|
16
|
300 + XSTRING_LENGTH (XCAR (elt)));
|
0
|
301 }
|
|
302
|
|
303 /* Advance to next element. */
|
|
304 prev = next;
|
|
305 next = XCDR (next);
|
|
306 }
|
|
307 if (CONSP (next))
|
|
308 last_boundary = prev;
|
|
309
|
|
310 while (CONSP (next))
|
|
311 {
|
|
312 Lisp_Object elt;
|
|
313 elt = XCAR (next);
|
|
314
|
|
315 /* When we get to a boundary, decide whether to truncate
|
|
316 either before or after it. The lower threshold, MINSIZE,
|
|
317 tells us to truncate after it. If its size pushes past
|
|
318 the higher threshold MAXSIZE as well, we truncate before it. */
|
|
319 if (NILP (elt))
|
|
320 {
|
|
321 if (size_so_far > maxsize && maxsize > 0)
|
|
322 break;
|
|
323 last_boundary = prev;
|
|
324 if (size_so_far > minsize && minsize > 0)
|
|
325 break;
|
|
326 }
|
|
327
|
|
328 /* Add in the space occupied by this element and its chain link. */
|
|
329 size_so_far += sizeof (struct Lisp_Cons);
|
|
330 if (CONSP (elt))
|
|
331 {
|
|
332 size_so_far += sizeof (struct Lisp_Cons);
|
|
333 if (STRINGP (XCAR (elt)))
|
|
334 size_so_far += (sizeof (struct Lisp_String) - 1
|
16
|
335 + XSTRING_LENGTH (XCAR (elt)));
|
0
|
336 }
|
|
337
|
|
338 /* Advance to next element. */
|
|
339 prev = next;
|
|
340 next = XCDR (next);
|
|
341 }
|
|
342
|
|
343 /* If we scanned the whole list, it is short enough; don't change it. */
|
|
344 if (NILP (next))
|
|
345 return list;
|
|
346
|
|
347 /* Truncate at the boundary where we decided to truncate. */
|
|
348 if (!NILP (last_boundary))
|
|
349 {
|
|
350 XCDR (last_boundary) = Qnil;
|
|
351 return list;
|
|
352 }
|
|
353 else
|
|
354 return Qnil;
|
|
355 }
|
|
356
|
20
|
357 DEFUN ("primitive-undo", Fprimitive_undo, 2, 2, 0, /*
|
0
|
358 Undo COUNT records from the front of the list LIST.
|
|
359 Return what remains of the list.
|
20
|
360 */
|
|
361 (count, list))
|
0
|
362 {
|
|
363 struct gcpro gcpro1, gcpro2;
|
|
364 Lisp_Object next = Qnil;
|
|
365 /* This function can GC */
|
|
366 int arg;
|
|
367 int speccount = specpdl_depth ();
|
|
368
|
|
369 record_unwind_protect (restore_inside_undo, make_int (inside_undo));
|
|
370 inside_undo = 1;
|
|
371
|
|
372 #if 0 /* This is a good feature, but would make undo-start
|
|
373 unable to do what is expected. */
|
|
374 Lisp_Object tem;
|
|
375
|
|
376 /* If the head of the list is a boundary, it is the boundary
|
|
377 preceding this command. Get rid of it and don't count it. */
|
|
378 tem = Fcar (list);
|
|
379 if (NILP (tem))
|
|
380 list = Fcdr (list);
|
|
381 #endif
|
|
382
|
|
383 CHECK_INT (count);
|
|
384 arg = XINT (count);
|
|
385 next = Qnil;
|
|
386 GCPRO2 (next, list);
|
|
387
|
|
388 /* Don't let read-only properties interfere with undo. */
|
|
389 if (NILP (current_buffer->read_only))
|
|
390 specbind (Qinhibit_read_only, Qt);
|
|
391
|
|
392 while (arg > 0)
|
|
393 {
|
|
394 while (1)
|
|
395 {
|
|
396 if (NILP (list))
|
|
397 break;
|
|
398 else if (!CONSP (list))
|
|
399 goto rotten;
|
|
400 next = XCAR (list);
|
|
401 list = XCDR (list);
|
|
402 /* Exit inner loop at undo boundary. */
|
|
403 if (NILP (next))
|
|
404 break;
|
|
405 /* Handle an integer by setting point to that value. */
|
|
406 else if (INTP (next))
|
|
407 BUF_SET_PT (current_buffer,
|
|
408 bufpos_clip_to_bounds (BUF_BEGV (current_buffer),
|
|
409 XINT (next),
|
|
410 BUF_ZV (current_buffer)));
|
|
411 else if (CONSP (next))
|
|
412 {
|
|
413 Lisp_Object car = XCAR (next);
|
|
414 Lisp_Object cdr = XCDR (next);
|
|
415
|
|
416 if (EQ (car, Qt))
|
|
417 {
|
|
418 /* Element (t high . low) records previous modtime. */
|
|
419 Lisp_Object high, low;
|
|
420 int mod_time;
|
|
421 if (!CONSP (cdr)) goto rotten;
|
|
422 high = XCAR (cdr);
|
|
423 low = XCDR (cdr);
|
|
424 if (!INTP (high) || !INTP (low)) goto rotten;
|
|
425 mod_time = (XINT (high) << 16) + XINT (low);
|
|
426 /* If this records an obsolete save
|
|
427 (not matching the actual disk file)
|
|
428 then don't mark unmodified. */
|
|
429 if (mod_time != current_buffer->modtime)
|
|
430 break;
|
|
431 #ifdef CLASH_DETECTION
|
|
432 Funlock_buffer ();
|
|
433 #endif /* CLASH_DETECTION */
|
223
|
434 /* may GC under ENERGIZE: */
|
|
435 Fset_buffer_modified_p (Qnil, Qnil);
|
0
|
436 }
|
|
437 else if (EXTENTP (car))
|
|
438 {
|
|
439 /* Element (extent start end) means that EXTENT was
|
|
440 detached, and we need to reattach it. */
|
|
441 Lisp_Object extent_obj, start, end;
|
185
|
442
|
0
|
443 extent_obj = car;
|
|
444 start = Fcar (cdr);
|
|
445 end = Fcar (Fcdr (cdr));
|
|
446
|
|
447 if (!INTP (start) || !INTP (end))
|
|
448 goto rotten;
|
|
449 Fset_extent_endpoints (extent_obj, start, end,
|
|
450 Fcurrent_buffer ());
|
|
451 }
|
|
452 #if 0 /* FSFmacs */
|
|
453 else if (EQ (car, Qnil))
|
|
454 {
|
|
455 /* Element (nil prop val beg . end) is property change. */
|
|
456 Lisp_Object beg, end, prop, val;
|
|
457
|
|
458 prop = Fcar (cdr);
|
|
459 cdr = Fcdr (cdr);
|
|
460 val = Fcar (cdr);
|
|
461 cdr = Fcdr (cdr);
|
|
462 beg = Fcar (cdr);
|
|
463 end = Fcdr (cdr);
|
|
464
|
|
465 Fput_text_property (beg, end, prop, val, Qnil);
|
|
466 }
|
|
467 #endif /* FSFmacs */
|
|
468 else if (INTP (car) && INTP (cdr))
|
|
469 {
|
|
470 /* Element (BEG . END) means range was inserted. */
|
|
471
|
|
472 if (XINT (car) < BUF_BEGV (current_buffer)
|
|
473 || XINT (cdr) > BUF_ZV (current_buffer))
|
|
474 error ("Changes to be undone are outside visible portion of buffer");
|
|
475 /* Set point first thing, so that undoing this undo
|
|
476 does not send point back to where it is now. */
|
223
|
477 Fgoto_char (car, Qnil);
|
|
478 Fdelete_region (car, cdr, Qnil);
|
0
|
479 }
|
|
480 else if (STRINGP (car) && INTP (cdr))
|
|
481 {
|
|
482 /* Element (STRING . POS) means STRING was deleted. */
|
|
483 Lisp_Object membuf = car;
|
|
484 int pos = XINT (cdr);
|
|
485
|
|
486 if (pos < 0)
|
|
487 {
|
|
488 if (-pos < BUF_BEGV (current_buffer) || -pos > BUF_ZV (current_buffer))
|
|
489 error ("Changes to be undone are outside visible portion of buffer");
|
|
490 BUF_SET_PT (current_buffer, -pos);
|
|
491 Finsert (1, &membuf);
|
|
492 }
|
|
493 else
|
|
494 {
|
|
495 if (pos < BUF_BEGV (current_buffer) || pos > BUF_ZV (current_buffer))
|
|
496 error ("Changes to be undone are outside visible portion of buffer");
|
|
497 BUF_SET_PT (current_buffer, pos);
|
|
498
|
|
499 /* Insert before markers so that if the mark is
|
|
500 currently on the boundary of this deletion, it
|
|
501 ends up on the other side of the now-undeleted
|
|
502 text from point. Since undo doesn't even keep
|
|
503 track of the mark, this isn't really necessary,
|
|
504 but it may lead to better behavior in certain
|
|
505 situations.
|
185
|
506
|
0
|
507 I'm doubtful that this is safe; you could mess
|
|
508 up the process-output mark in shell buffers, so
|
|
509 until I hear a compelling reason for this change,
|
|
510 I'm leaving it out. -jwz
|
|
511 */
|
|
512 /* Finsert_before_markers (1, &membuf); */
|
|
513 Finsert (1, &membuf);
|
|
514 BUF_SET_PT (current_buffer, pos);
|
|
515 }
|
|
516 }
|
|
517 else
|
|
518 {
|
|
519 goto rotten;
|
|
520 }
|
|
521 }
|
|
522 else if (EXTENTP (next))
|
|
523 Fdetach_extent (next);
|
|
524 else
|
|
525 {
|
|
526 rotten:
|
|
527 signal_simple_continuable_error
|
|
528 ("Something rotten in the state of undo:", next);
|
|
529 }
|
|
530 }
|
|
531 arg--;
|
|
532 }
|
|
533
|
|
534 UNGCPRO;
|
|
535 return unbind_to (speccount, list);
|
|
536 }
|
|
537
|
|
538 void
|
|
539 syms_of_undo (void)
|
|
540 {
|
20
|
541 DEFSUBR (Fprimitive_undo);
|
|
542 DEFSUBR (Fundo_boundary);
|
0
|
543 defsymbol (&Qinhibit_read_only, "inhibit-read-only");
|
|
544 }
|
|
545
|
|
546 void
|
|
547 vars_of_undo (void)
|
|
548 {
|
|
549 inside_undo = 0;
|
|
550 pending_boundary = Qnil;
|
|
551 staticpro (&pending_boundary);
|
|
552 last_undo_buffer = Qnil;
|
|
553 staticpro (&last_undo_buffer);
|
|
554 }
|