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