emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/compat 3457cbfcad 1/2: compat-27: Add with-minibuffer-s


From: ELPA Syncer
Subject: [elpa] externals/compat 3457cbfcad 1/2: compat-27: Add with-minibuffer-selected-window
Date: Sun, 15 Jan 2023 06:57:26 -0500 (EST)

branch: externals/compat
commit 3457cbfcadea30702b7a56fd7d84a767fa7b2996
Author: Daniel Mendler <mail@daniel-mendler.de>
Commit: Daniel Mendler <mail@daniel-mendler.de>

    compat-27: Add with-minibuffer-selected-window
---
 NEWS.org        |  1 +
 compat-27.el    | 12 ++++++++++++
 compat-tests.el | 15 +++++++++++++++
 compat.texi     |  7 +++++++
 4 files changed, 35 insertions(+)

diff --git a/NEWS.org b/NEWS.org
index f8f83c957a..4dd85e9657 100644
--- a/NEWS.org
+++ b/NEWS.org
@@ -3,6 +3,7 @@
 * Development
 
 - compat-27: Add ~fixnump~ and ~bignump~.
+- compat-27: Add ~with-minibuffer-selected-window~.
 - compat-27: Add generalized variables for ~decoded-time-*~.
 - compat-28: Add ~macroexp-warn-and-return~.
 - compat-28: Add ~subr-native-elisp-p~.
diff --git a/compat-27.el b/compat-27.el
index fe74e2aa41..dabe83f984 100644
--- a/compat-27.el
+++ b/compat-27.el
@@ -340,6 +340,18 @@ the number of seconds east of Greenwich."
   (gv-define-setter decoded-time-dst (v x)     `(setcar (nthcdr 7 ,x) ,v))
   (gv-define-setter decoded-time-zone (v x)    `(setcar (nthcdr 8 ,x) ,v)))
 
+;;;; Defined in minibuffer.el
+
+(compat-defmacro with-minibuffer-selected-window (&rest body)
+  "Execute the forms in BODY from the minibuffer in its original window.
+When used in a minibuffer window, select the window selected just before
+the minibuffer was activated, and execute the forms."
+  (declare (indent 0) (debug t))
+  `(let ((window (minibuffer-selected-window)))
+     (when window
+       (with-selected-window window
+         ,@body))))
+
 ;;;; Defined in image.el
 
 (compat-defun image--set-property (image property value) ;; <OK>
diff --git a/compat-tests.el b/compat-tests.el
index 5d7423e91a..b22af597a8 100644
--- a/compat-tests.el
+++ b/compat-tests.el
@@ -64,6 +64,21 @@
     (setq list (funcall sym list "first" 1 #'string=))
     (should (eq (compat-call plist-get list "first" #'string=) 1))))
 
+(ert-deftest with-minibuffer-selected-window ()
+  (let (ran)
+    (should-not (minibuffer-selected-window))
+    (should-not (with-minibuffer-selected-window
+                  (setq ran t)))
+    (should-not ran)
+    (unwind-protect
+        (progn
+          (advice-add #'minibuffer-selected-window :override #'selected-window)
+          (should-equal 'result (with-minibuffer-selected-window
+                                  (setq ran t)
+                                  'result))
+          (should ran))
+      (advice-remove #'minibuffer-selected-window #'selected-window))))
+
 (ert-deftest fixnump ()
   (should (fixnump 0))
   (should (fixnump most-negative-fixnum))
diff --git a/compat.texi b/compat.texi
index 6d0734f866..dfee2b02f4 100644
--- a/compat.texi
+++ b/compat.texi
@@ -859,6 +859,13 @@ The function @code{window-swap-states}.
 The following functions and macros implemented in 27.1, and are provided
 by Compat:
 
+@c based on lisp/minibuffer.el
+@defmac with-minibuffer-selected-window &rest body
+Execute the forms in @var{body} from the minibuffer in its original
+window. When used in a minibuffer window, select the window selected
+just before the minibuffer was activated, and execute the forms.
+@end defmac
+
 @c copied from lispref/numbers.texi
 @defun bignump object
 This predicate tests whether its argument is a large integer, and



reply via email to

[Prev in Thread] Current Thread [Next in Thread]