]> code.delx.au - gnu-emacs/blob - src/undo.c
Merge branch 'emacs-25' of git.sv.gnu.org:/srv/git/emacs into emacs-25
[gnu-emacs] / src / undo.c
1 /* undo handling for GNU Emacs.
2 Copyright (C) 1990, 1993-1994, 2000-2015 Free Software Foundation,
3 Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
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
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20
21 #include <config.h>
22
23 #include "lisp.h"
24 #include "buffer.h"
25 #include "keyboard.h"
26
27 /* The first time a command records something for undo.
28 it also allocates the undo-boundary object
29 which will be added to the list at the end of the command.
30 This ensures we can't run out of space while trying to make
31 an undo-boundary. */
32 static Lisp_Object pending_boundary;
33
34 /* Record point as it was at beginning of this command (if necessary)
35 and prepare the undo info for recording a change.
36 /* Prepare the undo info for recording a change. */
37 static void
38 prepare_record ()
39 {
40 /* Allocate a cons cell to be the undo boundary after this command. */
41 if (NILP (pending_boundary))
42 pending_boundary = Fcons (Qnil, Qnil);
43
44 if (MODIFF <= SAVE_MODIFF)
45 record_first_change ();
46 }
47
48 /* Record point as it was at beginning of this command.
49 PT is the position of point that will naturally occur as a result of the
50 undo record that will be added just after this command terminates. */
51 static void
52 record_point (ptrdiff_t pt)
53 {
54 /* Don't record position of pt when undo_inhibit_record_point holds. */
55 if (undo_inhibit_record_point)
56 return;
57
58 bool at_boundary;
59
60 at_boundary = ! CONSP (BVAR (current_buffer, undo_list))
61 || NILP (XCAR (BVAR (current_buffer, undo_list)));
62
63 prepare_record();
64
65 /* If we are just after an undo boundary, and
66 point wasn't at start of deleted range, record where it was. */
67 if (at_boundary){
68 bset_undo_list (current_buffer,
69 Fcons (make_number (pt),
70 BVAR (current_buffer, undo_list)));
71 }
72 }
73
74 /* Record an insertion that just happened or is about to happen,
75 for LENGTH characters at position BEG.
76 (It is possible to record an insertion before or after the fact
77 because we don't need to record the contents.) */
78
79 void
80 record_insert (ptrdiff_t beg, ptrdiff_t length)
81 {
82 Lisp_Object lbeg, lend;
83
84 if (EQ (BVAR (current_buffer, undo_list), Qt))
85 return;
86
87 prepare_record ();
88
89 /* If this is following another insertion and consecutive with it
90 in the buffer, combine the two. */
91 if (CONSP (BVAR (current_buffer, undo_list)))
92 {
93 Lisp_Object elt;
94 elt = XCAR (BVAR (current_buffer, undo_list));
95 if (CONSP (elt)
96 && INTEGERP (XCAR (elt))
97 && INTEGERP (XCDR (elt))
98 && XINT (XCDR (elt)) == beg)
99 {
100 XSETCDR (elt, make_number (beg + length));
101 return;
102 }
103 }
104
105 XSETFASTINT (lbeg, beg);
106 XSETINT (lend, beg + length);
107 bset_undo_list (current_buffer,
108 Fcons (Fcons (lbeg, lend), BVAR (current_buffer, undo_list)));
109 }
110
111 /* Record the fact that markers in the region of FROM, TO are about to
112 be adjusted. This is done only when a marker points within text
113 being deleted, because that's the only case where an automatic
114 marker adjustment won't be inverted automatically by undoing the
115 buffer modification. */
116
117 static void
118 record_marker_adjustments (ptrdiff_t from, ptrdiff_t to)
119 {
120 Lisp_Object marker;
121 register struct Lisp_Marker *m;
122 register ptrdiff_t charpos, adjustment;
123
124 /* Allocate a cons cell to be the undo boundary after this command. */
125 if (NILP (pending_boundary))
126 pending_boundary = Fcons (Qnil, Qnil);
127
128 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
129 {
130 charpos = m->charpos;
131 eassert (charpos <= Z);
132
133 if (from <= charpos && charpos <= to)
134 {
135 /* insertion_type nil markers will end up at the beginning of
136 the re-inserted text after undoing a deletion, and must be
137 adjusted to move them to the correct place.
138
139 insertion_type t markers will automatically move forward
140 upon re-inserting the deleted text, so we have to arrange
141 for them to move backward to the correct position. */
142 adjustment = (m->insertion_type ? to : from) - charpos;
143
144 if (adjustment)
145 {
146 XSETMISC (marker, m);
147 bset_undo_list
148 (current_buffer,
149 Fcons (Fcons (marker, make_number (adjustment)),
150 BVAR (current_buffer, undo_list)));
151 }
152 }
153 }
154 }
155
156 /* Record that a deletion is about to take place, of the characters in
157 STRING, at location BEG. Optionally record adjustments for markers
158 in the region STRING occupies in the current buffer. */
159 void
160 record_delete (ptrdiff_t beg, Lisp_Object string, bool record_markers)
161 {
162 Lisp_Object sbeg;
163
164 if (EQ (BVAR (current_buffer, undo_list), Qt))
165 return;
166
167 if (point_before_last_command_or_undo != beg &&
168 buffer_before_last_command_or_undo == current_buffer)
169 {
170 record_point (point_before_last_command_or_undo);
171 }
172
173 if (PT == beg + SCHARS (string))
174 {
175 XSETINT (sbeg, -beg);
176 prepare_record ();
177 }
178 else
179 {
180 XSETFASTINT (sbeg, beg);
181 prepare_record ();
182 }
183
184 /* primitive-undo assumes marker adjustments are recorded
185 immediately before the deletion is recorded. See bug 16818
186 discussion. */
187 if (record_markers)
188 record_marker_adjustments (beg, beg + SCHARS (string));
189
190 bset_undo_list
191 (current_buffer,
192 Fcons (Fcons (string, sbeg), BVAR (current_buffer, undo_list)));
193 }
194
195 /* Record that a replacement is about to take place,
196 for LENGTH characters at location BEG.
197 The replacement must not change the number of characters. */
198
199 void
200 record_change (ptrdiff_t beg, ptrdiff_t length)
201 {
202 record_delete (beg, make_buffer_string (beg, beg + length, true), false);
203 record_insert (beg, length);
204 }
205 \f
206 /* Record that an unmodified buffer is about to be changed.
207 Record the file modification date so that when undoing this entry
208 we can tell whether it is obsolete because the file was saved again. */
209
210 void
211 record_first_change (void)
212 {
213 struct buffer *base_buffer = current_buffer;
214
215 if (EQ (BVAR (current_buffer, undo_list), Qt))
216 return;
217
218 if (base_buffer->base_buffer)
219 base_buffer = base_buffer->base_buffer;
220
221 bset_undo_list (current_buffer,
222 Fcons (Fcons (Qt, Fvisited_file_modtime ()),
223 BVAR (current_buffer, undo_list)));
224 }
225
226 /* Record a change in property PROP (whose old value was VAL)
227 for LENGTH characters starting at position BEG in BUFFER. */
228
229 void
230 record_property_change (ptrdiff_t beg, ptrdiff_t length,
231 Lisp_Object prop, Lisp_Object value,
232 Lisp_Object buffer)
233 {
234 Lisp_Object lbeg, lend, entry;
235 struct buffer *buf = XBUFFER (buffer);
236
237 if (EQ (BVAR (buf, undo_list), Qt))
238 return;
239
240 /* Allocate a cons cell to be the undo boundary after this command. */
241 if (NILP (pending_boundary))
242 pending_boundary = Fcons (Qnil, Qnil);
243
244 if (MODIFF <= SAVE_MODIFF)
245 record_first_change ();
246
247 XSETINT (lbeg, beg);
248 XSETINT (lend, beg + length);
249 entry = Fcons (Qnil, Fcons (prop, Fcons (value, Fcons (lbeg, lend))));
250 bset_undo_list (current_buffer,
251 Fcons (entry, BVAR (current_buffer, undo_list)));
252 }
253
254 DEFUN ("undo-boundary", Fundo_boundary, Sundo_boundary, 0, 0, 0,
255 doc: /* Mark a boundary between units of undo.
256 An undo command will stop at this point,
257 but another undo command will undo to the previous boundary. */)
258 (void)
259 {
260 Lisp_Object tem;
261 if (EQ (BVAR (current_buffer, undo_list), Qt))
262 return Qnil;
263 tem = Fcar (BVAR (current_buffer, undo_list));
264 if (!NILP (tem))
265 {
266 /* One way or another, cons nil onto the front of the undo list. */
267 if (!NILP (pending_boundary))
268 {
269 /* If we have preallocated the cons cell to use here,
270 use that one. */
271 XSETCDR (pending_boundary, BVAR (current_buffer, undo_list));
272 bset_undo_list (current_buffer, pending_boundary);
273 pending_boundary = Qnil;
274 }
275 else
276 bset_undo_list (current_buffer,
277 Fcons (Qnil, BVAR (current_buffer, undo_list)));
278 }
279
280 Fset (Qundo_auto__last_boundary_cause, Qexplicit);
281 point_before_last_command_or_undo = PT;
282 buffer_before_last_command_or_undo = current_buffer;
283
284 return Qnil;
285 }
286
287 /* At garbage collection time, make an undo list shorter at the end,
288 returning the truncated list. How this is done depends on the
289 variables undo-limit, undo-strong-limit and undo-outer-limit.
290 In some cases this works by calling undo-outer-limit-function. */
291
292 void
293 truncate_undo_list (struct buffer *b)
294 {
295 Lisp_Object list;
296 Lisp_Object prev, next, last_boundary;
297 EMACS_INT size_so_far = 0;
298
299 /* Make sure that calling undo-outer-limit-function
300 won't cause another GC. */
301 ptrdiff_t count = inhibit_garbage_collection ();
302
303 /* Make the buffer current to get its local values of variables such
304 as undo_limit. Also so that Vundo_outer_limit_function can
305 tell which buffer to operate on. */
306 record_unwind_current_buffer ();
307 set_buffer_internal (b);
308
309 list = BVAR (b, undo_list);
310
311 prev = Qnil;
312 next = list;
313 last_boundary = Qnil;
314
315 /* If the first element is an undo boundary, skip past it. */
316 if (CONSP (next) && NILP (XCAR (next)))
317 {
318 /* Add in the space occupied by this element and its chain link. */
319 size_so_far += sizeof (struct Lisp_Cons);
320
321 /* Advance to next element. */
322 prev = next;
323 next = XCDR (next);
324 }
325
326 /* Always preserve at least the most recent undo record
327 unless it is really horribly big.
328
329 Skip, skip, skip the undo, skip, skip, skip the undo,
330 Skip, skip, skip the undo, skip to the undo bound'ry. */
331
332 while (CONSP (next) && ! NILP (XCAR (next)))
333 {
334 Lisp_Object elt;
335 elt = XCAR (next);
336
337 /* Add in the space occupied by this element and its chain link. */
338 size_so_far += sizeof (struct Lisp_Cons);
339 if (CONSP (elt))
340 {
341 size_so_far += sizeof (struct Lisp_Cons);
342 if (STRINGP (XCAR (elt)))
343 size_so_far += (sizeof (struct Lisp_String) - 1
344 + SCHARS (XCAR (elt)));
345 }
346
347 /* Advance to next element. */
348 prev = next;
349 next = XCDR (next);
350 }
351
352 /* If by the first boundary we have already passed undo_outer_limit,
353 we're heading for memory full, so offer to clear out the list. */
354 if (INTEGERP (Vundo_outer_limit)
355 && size_so_far > XINT (Vundo_outer_limit)
356 && !NILP (Vundo_outer_limit_function))
357 {
358 Lisp_Object tem;
359
360 /* Normally the function this calls is undo-outer-limit-truncate. */
361 tem = call1 (Vundo_outer_limit_function, make_number (size_so_far));
362 if (! NILP (tem))
363 {
364 /* The function is responsible for making
365 any desired changes in buffer-undo-list. */
366 unbind_to (count, Qnil);
367 return;
368 }
369 }
370
371 if (CONSP (next))
372 last_boundary = prev;
373
374 /* Keep additional undo data, if it fits in the limits. */
375 while (CONSP (next))
376 {
377 Lisp_Object elt;
378 elt = XCAR (next);
379
380 /* When we get to a boundary, decide whether to truncate
381 either before or after it. The lower threshold, undo_limit,
382 tells us to truncate after it. If its size pushes past
383 the higher threshold undo_strong_limit, we truncate before it. */
384 if (NILP (elt))
385 {
386 if (size_so_far > undo_strong_limit)
387 break;
388 last_boundary = prev;
389 if (size_so_far > undo_limit)
390 break;
391 }
392
393 /* Add in the space occupied by this element and its chain link. */
394 size_so_far += sizeof (struct Lisp_Cons);
395 if (CONSP (elt))
396 {
397 size_so_far += sizeof (struct Lisp_Cons);
398 if (STRINGP (XCAR (elt)))
399 size_so_far += (sizeof (struct Lisp_String) - 1
400 + SCHARS (XCAR (elt)));
401 }
402
403 /* Advance to next element. */
404 prev = next;
405 next = XCDR (next);
406 }
407
408 /* If we scanned the whole list, it is short enough; don't change it. */
409 if (NILP (next))
410 ;
411 /* Truncate at the boundary where we decided to truncate. */
412 else if (!NILP (last_boundary))
413 XSETCDR (last_boundary, Qnil);
414 /* There's nothing we decided to keep, so clear it out. */
415 else
416 bset_undo_list (b, Qnil);
417
418 unbind_to (count, Qnil);
419 }
420
421 \f
422 void
423 syms_of_undo (void)
424 {
425 DEFSYM (Qinhibit_read_only, "inhibit-read-only");
426 DEFSYM (Qundo_auto__last_boundary_cause, "undo-auto--last-boundary-cause");
427 DEFSYM (Qexplicit, "explicit");
428
429 /* Marker for function call undo list elements. */
430 DEFSYM (Qapply, "apply");
431
432 pending_boundary = Qnil;
433 staticpro (&pending_boundary);
434
435 defsubr (&Sundo_boundary);
436
437 DEFVAR_INT ("undo-limit", undo_limit,
438 doc: /* Keep no more undo information once it exceeds this size.
439 This limit is applied when garbage collection happens.
440 When a previous command increases the total undo list size past this
441 value, the earlier commands that came before it are forgotten.
442
443 The size is counted as the number of bytes occupied,
444 which includes both saved text and other data. */);
445 undo_limit = 80000;
446
447 DEFVAR_INT ("undo-strong-limit", undo_strong_limit,
448 doc: /* Don't keep more than this much size of undo information.
449 This limit is applied when garbage collection happens.
450 When a previous command increases the total undo list size past this
451 value, that command and the earlier commands that came before it are forgotten.
452 However, the most recent buffer-modifying command's undo info
453 is never discarded for this reason.
454
455 The size is counted as the number of bytes occupied,
456 which includes both saved text and other data. */);
457 undo_strong_limit = 120000;
458
459 DEFVAR_LISP ("undo-outer-limit", Vundo_outer_limit,
460 doc: /* Outer limit on size of undo information for one command.
461 At garbage collection time, if the current command has produced
462 more than this much undo information, it discards the info and displays
463 a warning. This is a last-ditch limit to prevent memory overflow.
464
465 The size is counted as the number of bytes occupied, which includes
466 both saved text and other data. A value of nil means no limit. In
467 this case, accumulating one huge undo entry could make Emacs crash as
468 a result of memory overflow.
469
470 In fact, this calls the function which is the value of
471 `undo-outer-limit-function' with one argument, the size.
472 The text above describes the behavior of the function
473 that variable usually specifies. */);
474 Vundo_outer_limit = make_number (12000000);
475
476 DEFVAR_LISP ("undo-outer-limit-function", Vundo_outer_limit_function,
477 doc: /* Function to call when an undo list exceeds `undo-outer-limit'.
478 This function is called with one argument, the current undo list size
479 for the most recent command (since the last undo boundary).
480 If the function returns t, that means truncation has been fully handled.
481 If it returns nil, the other forms of truncation are done.
482
483 Garbage collection is inhibited around the call to this function,
484 so it must make sure not to do a lot of consing. */);
485 Vundo_outer_limit_function = Qnil;
486
487 DEFVAR_BOOL ("undo-inhibit-record-point", undo_inhibit_record_point,
488 doc: /* Non-nil means do not record `point' in `buffer-undo-list'. */);
489 undo_inhibit_record_point = false;
490 }