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