]> git.eshelyaron.com Git - emacs.git/commitdiff
New variable 'undo-inhibit-region'.
authorEshel Yaron <me@eshelyaron.com>
Tue, 12 Dec 2023 13:17:33 +0000 (14:17 +0100)
committerEshel Yaron <me@eshelyaron.com>
Tue, 12 Dec 2023 13:42:35 +0000 (14:42 +0100)
This buffer-local variable allows Lisp code to tell the next 'undo'
invocation to ignore an active region.

* lisp/simple.el (undo-inhibit-region): New variable.
(undo): Use it.
* etc/NEWS: Announce it.
* doc/lispref/text.texi (Undo): Mention it.

doc/lispref/text.texi
etc/NEWS
lisp/simple.el

index 7ee2b06891dbd419505ef3a1c27d03958ca6ffe4..9f9c7ed0783e098b892cc821b85a0f40d68674ca 100644 (file)
@@ -1524,11 +1524,14 @@ This macro removes all the undo boundaries inserted during the
 execution of @var{body} so that it can be undone as a single step.
 @end defmac
 
+@vindex undo-inhibit-region
 Some commands leave the region active after execution in such a way that
 it interferes with selective undo of that command.  To make @code{undo}
 ignore the active region when invoked immediately after such a command,
 set the property @code{undo-inhibit-region} of the command's function
-symbol to a non-@code{nil} value.  @xref{Standard Properties}.
+symbol to a non-@code{nil} value.  @xref{Standard Properties}.  You can
+also set the buffer-local variable @code{undo-inhibit-region} from any
+Lisp function to make the next @code{undo} ignore an active region.
 
 @node Maintaining Undo
 @section Maintaining Undo Lists
index a4edd6f189187ed3fa18d4d33ca3e4a0062c2247..93d8a70d55b6aad84d785b7c01e163fb3127ac0d 100644 (file)
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1310,6 +1310,15 @@ from which you are switching.  You can check for this variable in your
 'read-buffer-function' to determine if the caller intends to switch to
 the buffer that this function reads.
 
++++
+** New buffer-local variable 'undo-inhibit-region'.
+Lisp code can set this to non-nil to tell the next 'undo' command to
+ignore an active region.  This allows functions to select a region
+without restricting a subsequent 'undo'.  For commands that activate
+the region and never want to restrict 'undo' to that region, it is
+preferable to use the existing 'undo-inhibit-region' symbol property
+instead of this variable.
+
 ** New function 'merge-ordered-lists'.
 Mostly used internally to do a kind of topological sort of
 inheritance hierarchies.
index ca1f326a81b17d6eedc5d0687827209138dab36c..82fbfe05e1f4462916b3122ef43ded2a3f09dcb2 100644 (file)
@@ -3438,6 +3438,20 @@ undo record: if we undo from 4, `pending-undo-list' will be at 3,
   "Within a run of consecutive undo commands, list remaining to be undone.
 If t, we undid all the way to the end of it.")
 
+(defvar-local undo-inhibit-region nil
+  "Whether to inhibit the restriction of \\[undo] to the active region.
+
+If this is non-nil, the next invocation of \\[undo] operates on
+the entire buffer even if the region is active.  \\[undo] sets
+this variable back to nil after consulting it.
+
+Functions that set the active region can use this variable to
+have a subsequent \\[undo] undo other changes outside the region.
+
+For commands that set the active region and never want to
+restrict \\[undo] to that region, it is preferable to use the
+`undo-inhibit-region' symbol property instead of this variable.")
+
 (defun undo--last-change-was-undo-p (undo-list)
   (while (and (consp undo-list) (eq (car undo-list) nil))
     (setq undo-list (cdr undo-list)))
@@ -3466,13 +3480,16 @@ as an argument limits undo to changes within the current region."
                        (recent-auto-save-p)))
          ;; Allow certain commands to inhibit an immediately following
          ;; undo-in-region.
-         (inhibit-region (and (symbolp last-command)
-                              (get last-command 'undo-inhibit-region)))
+         (inhibit-region (or undo-inhibit-region
+                             (and (symbolp last-command)
+                                  (get last-command 'undo-inhibit-region))))
         message)
     ;; If we get an error in undo-start,
     ;; the next command should not be a "consecutive undo".
     ;; So set `this-command' to something other than `undo'.
-    (setq this-command 'undo-start)
+    (setq this-command 'undo-start
+          ;; Restore region restrictions for subsequent invocations.
+          undo-inhibit-region nil)
     ;; Here we decide whether to break the undo chain.  If the
     ;; previous command is `undo', we don't call `undo-start', i.e.,
     ;; don't break the undo chain.