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