1 /* undo handling for GNU Emacs.
2 Copyright (C) 1990 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is distributed in the hope that it will be useful,
7 but WITHOUT ANY WARRANTY. No author or distributor
8 accepts responsibility to anyone for the consequences of using it
9 or for whether it serves any particular purpose or works at all,
10 unless he says so in writing. Refer to the GNU Emacs General Public
11 License for full details.
13 Everyone is granted permission to copy, modify and redistribute
14 GNU Emacs, but only under the conditions described in the
15 GNU Emacs General Public License. A copy of this license is
16 supposed to have been given to you along with GNU Emacs so you
17 can know your rights and responsibilities. It should be in a
18 file named COPYING. Among other things, the copyright notice
19 and this notice must be preserved on all copies. */
26 /* Last buffer for which undo information was recorded. */
27 Lisp_Object last_undo_buffer
;
29 /* Record an insertion that just happened or is about to happen,
30 for LENGTH characters at position BEG.
31 (It is possible to record an insertion before or after the fact
32 because we don't need to record the contents.) */
34 record_insert (beg
, length
)
35 Lisp_Object beg
, length
;
37 Lisp_Object lbeg
, lend
;
39 if (current_buffer
!= XBUFFER (last_undo_buffer
))
41 XSET (last_undo_buffer
, Lisp_Buffer
, current_buffer
);
43 if (EQ (current_buffer
->undo_list
, Qt
))
45 if (MODIFF
<= current_buffer
->save_modified
)
46 record_first_change ();
48 /* If this is following another insertion and consecutive with it
49 in the buffer, combine the two. */
50 if (XTYPE (current_buffer
->undo_list
) == Lisp_Cons
)
53 elt
= XCONS (current_buffer
->undo_list
)->car
;
54 if (XTYPE (elt
) == Lisp_Cons
55 && XTYPE (XCONS (elt
)->car
) == Lisp_Int
56 && XTYPE (XCONS (elt
)->cdr
) == Lisp_Int
57 && XINT (XCONS (elt
)->cdr
) == XINT (beg
))
59 XSETINT (XCONS (elt
)->cdr
, XINT (beg
) + XINT (length
));
65 XSET (lend
, Lisp_Int
, XINT (beg
) + XINT (length
));
66 current_buffer
->undo_list
= Fcons (Fcons (lbeg
, lend
),
67 current_buffer
->undo_list
);
70 /* Record that a deletion is about to take place,
71 for LENGTH characters at location BEG. */
73 record_delete (beg
, length
)
76 Lisp_Object lbeg
, lend
, sbeg
;
78 if (current_buffer
!= XBUFFER (last_undo_buffer
))
80 XSET (last_undo_buffer
, Lisp_Buffer
, current_buffer
);
82 if (EQ (current_buffer
->undo_list
, Qt
))
84 if (MODIFF
<= current_buffer
->save_modified
)
85 record_first_change ();
87 if (point
== beg
+ length
)
88 XSET (sbeg
, Lisp_Int
, -beg
);
90 XFASTINT (sbeg
) = beg
;
91 XFASTINT (lbeg
) = beg
;
92 XFASTINT (lend
) = beg
+ length
;
94 /* If point isn't at start of deleted range, record where it is. */
96 current_buffer
->undo_list
97 = Fcons (make_number (PT
), current_buffer
->undo_list
);
99 current_buffer
->undo_list
100 = Fcons (Fcons (Fbuffer_substring (lbeg
, lend
), sbeg
),
101 current_buffer
->undo_list
);
104 /* Record that a replacement is about to take place,
105 for LENGTH characters at location BEG.
106 The replacement does not change the number of characters. */
108 record_change (beg
, length
)
111 record_delete (beg
, length
);
112 record_insert (beg
, length
);
115 /* Record that an unmodified buffer is about to be changed.
116 Record the file modification date so that when undoing this entry
117 we can tell whether it is obsolete because the file was saved again. */
119 record_first_change ()
121 Lisp_Object high
, low
;
122 XFASTINT (high
) = (current_buffer
->modtime
>> 16) & 0xffff;
123 XFASTINT (low
) = current_buffer
->modtime
& 0xffff;
124 current_buffer
->undo_list
= Fcons (Fcons (Qt
, Fcons (high
, low
)), current_buffer
->undo_list
);
127 DEFUN ("undo-boundary", Fundo_boundary
, Sundo_boundary
, 0, 0, 0,
128 "Mark a boundary between units of undo.\n\
129 An undo command will stop at this point,\n\
130 but another undo command will undo to the previous boundary.")
134 if (EQ (current_buffer
->undo_list
, Qt
))
136 tem
= Fcar (current_buffer
->undo_list
);
138 current_buffer
->undo_list
= Fcons (Qnil
, current_buffer
->undo_list
);
142 /* At garbage collection time, make an undo list shorter at the end,
143 returning the truncated list.
144 MINSIZE and MAXSIZE are the limits on size allowed, as described below.
145 In practice, these are the values of undo-limit and
146 undo-strong-limit. */
149 truncate_undo_list (list
, minsize
, maxsize
)
151 int minsize
, maxsize
;
153 Lisp_Object prev
, next
, last_boundary
;
158 last_boundary
= Qnil
;
160 /* Always preserve at least the most recent undo record.
161 If the first element is an undo boundary, skip past it.
163 Skip, skip, skip the undo, skip, skip, skip the undo,
164 Skip, skip, skip the undo, skip to the undo bound'ry.
165 (Get it? "Skip to my Loo?") */
166 if (XTYPE (next
) == Lisp_Cons
167 && NILP (XCONS (next
)->car
))
169 /* Add in the space occupied by this element and its chain link. */
170 size_so_far
+= sizeof (struct Lisp_Cons
);
172 /* Advance to next element. */
174 next
= XCONS (next
)->cdr
;
176 while (XTYPE (next
) == Lisp_Cons
177 && ! NILP (XCONS (next
)->car
))
180 elt
= XCONS (next
)->car
;
182 /* Add in the space occupied by this element and its chain link. */
183 size_so_far
+= sizeof (struct Lisp_Cons
);
184 if (XTYPE (elt
) == Lisp_Cons
)
186 size_so_far
+= sizeof (struct Lisp_Cons
);
187 if (XTYPE (XCONS (elt
)->car
) == Lisp_String
)
188 size_so_far
+= (sizeof (struct Lisp_String
) - 1
189 + XSTRING (XCONS (elt
)->car
)->size
);
192 /* Advance to next element. */
194 next
= XCONS (next
)->cdr
;
196 if (XTYPE (next
) == Lisp_Cons
)
197 last_boundary
= prev
;
199 while (XTYPE (next
) == Lisp_Cons
)
202 elt
= XCONS (next
)->car
;
204 /* When we get to a boundary, decide whether to truncate
205 either before or after it. The lower threshold, MINSIZE,
206 tells us to truncate after it. If its size pushes past
207 the higher threshold MAXSIZE as well, we truncate before it. */
210 if (size_so_far
> maxsize
)
212 last_boundary
= prev
;
213 if (size_so_far
> minsize
)
217 /* Add in the space occupied by this element and its chain link. */
218 size_so_far
+= sizeof (struct Lisp_Cons
);
219 if (XTYPE (elt
) == Lisp_Cons
)
221 size_so_far
+= sizeof (struct Lisp_Cons
);
222 if (XTYPE (XCONS (elt
)->car
) == Lisp_String
)
223 size_so_far
+= (sizeof (struct Lisp_String
) - 1
224 + XSTRING (XCONS (elt
)->car
)->size
);
227 /* Advance to next element. */
229 next
= XCONS (next
)->cdr
;
232 /* If we scanned the whole list, it is short enough; don't change it. */
236 /* Truncate at the boundary where we decided to truncate. */
237 if (!NILP (last_boundary
))
239 XCONS (last_boundary
)->cdr
= Qnil
;
246 DEFUN ("primitive-undo", Fprimitive_undo
, Sprimitive_undo
, 2, 2, 0,
247 "Undo N records from the front of the list LIST.\n\
248 Return what remains of the list.")
250 Lisp_Object count
, list
;
252 register int arg
= XINT (count
);
253 #if 0 /* This is a good feature, but would make undo-start
254 unable to do what is expected. */
257 /* If the head of the list is a boundary, it is the boundary
258 preceding this command. Get rid of it and don't count it. */
271 /* Exit inner loop at undo boundary. */
274 /* Handle an integer by setting point to that value. */
275 if (XTYPE (next
) == Lisp_Int
)
276 SET_PT (clip_to_bounds (BEGV
, XINT (next
), ZV
));
277 else if (XTYPE (next
) == Lisp_Cons
)
279 Lisp_Object car
, cdr
;
285 /* Element (t high . low) records previous modtime. */
286 Lisp_Object high
, low
;
291 mod_time
= (high
<< 16) + low
;
292 /* If this records an obsolete save
293 (not matching the actual disk file)
294 then don't mark unmodified. */
295 if (mod_time
!= current_buffer
->modtime
)
297 #ifdef CLASH_DETECTION
299 #endif /* CLASH_DETECTION */
300 Fset_buffer_modified_p (Qnil
);
302 else if (XTYPE (car
) == Lisp_Int
&& XTYPE (cdr
) == Lisp_Int
)
304 /* Element (BEG . END) means range was inserted. */
307 if (XINT (car
) < BEGV
309 error ("Changes to be undone are outside visible portion of buffer");
310 /* Set point first thing, so that undoing this undo
311 does not send point back to where it is now. */
313 Fdelete_region (car
, cdr
);
315 else if (XTYPE (car
) == Lisp_String
&& XTYPE (cdr
) == Lisp_Int
)
317 /* Element (STRING . POS) means STRING was deleted. */
319 int pos
= XINT (cdr
);
324 if (-pos
< BEGV
|| -pos
> ZV
)
325 error ("Changes to be undone are outside visible portion of buffer");
327 Finsert (1, &membuf
);
331 if (pos
< BEGV
|| pos
> ZV
)
332 error ("Changes to be undone are outside visible portion of buffer");
335 /* Insert before markers so that if the mark is
336 currently on the boundary of this deletion, it
337 ends up on the other side of the now-undeleted
338 text from point. Since undo doesn't even keep
339 track of the mark, this isn't really necessary,
340 but it may lead to better behavior in certain
342 Finsert_before_markers (1, &membuf
);
356 defsubr (&Sprimitive_undo
);
357 defsubr (&Sundo_boundary
);