Maintaining Undo Lists

This section describes how to enable and disable undo information for a given buffer. It also explains how the undo list is truncated automatically so it doesn’t get too big.

Recording of undo information in a newly created buffer is normally enabled to start with; but if the buffer name starts with a space, the undo recording is initially disabled. You can explicitly enable or disable undo recording with the following two functions, or by setting buffer-undo-list yourself.

Command: buffer-enable-undo &optional buffer-or-name

This command enables recording undo information for buffer buffer-or-name, so that subsequent changes can be undone. If no argument is supplied, then the current buffer is used. This function does nothing if undo recording is already enabled in the buffer. It returns nil.

In an interactive call, buffer-or-name is the current buffer. You cannot specify any other buffer.

Command: buffer-disable-undo &optional buffer-or-name

This function discards the undo list of buffer-or-name, and disables further recording of undo information. As a result, it is no longer possible to undo either previous changes or any subsequent changes. If the undo list of buffer-or-name is already disabled, this function has no effect.

In an interactive call, BUFFER-OR-NAME is the current buffer. You cannot specify any other buffer. This function returns nil.

As editing continues, undo lists get longer and longer. To prevent them from using up all available memory space, garbage collection trims them back to size limits you can set. (For this purpose, the size of an undo list measures the cons cells that make up the list, plus the strings of deleted text.) Three variables control the range of acceptable sizes: undo-limit, undo-strong-limit and undo-outer-limit. In these variables, size is counted as the number of bytes occupied, which includes both saved text and other data.

User Option: undo-limit

This is the soft limit for the acceptable size of an undo list. The change group at which this size is exceeded is the last one kept.

User Option: undo-strong-limit

This is the upper limit for the acceptable size of an undo list. The change group at which this size is exceeded is discarded itself (along with all older change groups). There is one exception: the very latest change group is only discarded if it exceeds undo-outer-limit.

User Option: undo-outer-limit

If at garbage collection time the undo info for the current command exceeds this limit, Emacs discards the info and displays a warning. This is a last ditch limit to prevent memory overflow.

User Option: undo-ask-before-discard

If this variable is non-nil, when the undo info exceeds undo-outer-limit, Emacs asks in the echo area whether to discard the info. The default value is nil, which means to discard it automatically.

This option is mainly intended for debugging. Garbage collection is inhibited while the question is asked, which means that Emacs might leak memory if the user waits too long before answering the question.

Copyright © 1990-1996, 1998-2021 Free Software Foundation, Inc.
Licensed under the GNU GPL license.
https://www.gnu.org/software/emacs/manual/html_node/elisp/Maintaining-Undo.html