Subject: [PATCH] Use display-buffer to make the calc popup

From: Viktor Obeling <[email-masked]>
Date: Sun, 12 Jul 2020 21:41:24 +0200

>From e18d2f85504669e24c9f060c5b36e14c60ba6957 Mon Sep 17 00:00:00 2001
From: Viktor Obeling <[email-masked]>
Date: Sun, 12 Jul 2020 17:58:28 +0200
Subject: [PATCH] Use display-buffer to make the calc popup

* lisp/calc/calc.el (calc--popup-buffer, calc):
Allows the user to more easily customize the buffer placement by
using display-buffer-alist.

Copyright-paperwork-exempt: yes
---
 lisp/calc/calc.el | 40 +++++++++++++++++++++-------------------
 1 file changed, 21 insertions(+), 19 deletions(-)

diff --git a/lisp/calc/calc.el b/lisp/calc/calc.el
index 4e4fb67173..5f3b2877d3 100644
--- a/lisp/calc/calc.el
+++ b/lisp/calc/calc.el
@@ -1394,6 +1394,19 @@ calc-create-buffer
     (require 'calc-ext)
     (calc-set-language calc-language calc-language-option t)))
 
+(defun calc--popup-buffer (buffer alist)
+  "Action function for `display-buffer'."
+  (let ((w (get-largest-window)))
+    (when (and pop-up-windows
+               (> (window-height w)
+                  (+ window-min-height calc-window-height 2)))
+      (setq w (split-window w
+                            (- (window-height w)
+                               calc-window-height 2)
+                            nil))
+      (set-window-buffer w buffer)
+      (select-window w))))
+
 ;;;###autoload
 (defun calc (&optional arg full-display interactive)
   "The Emacs Calculator.  Full documentation is listed under `calc-mode'."
@@ -1412,25 +1425,14 @@ calc
 	(calc-quit)
       (calc-create-buffer)
       (setq calc-was-keypad-mode nil)
-      (if (or (eq full-display t)
-              (and (null full-display) calc-full-mode))
-          (switch-to-buffer (current-buffer) t)
-        (if (get-buffer-window (current-buffer))
-            (select-window (get-buffer-window (current-buffer)))
-          (if calc-window-hook
-              (run-hooks 'calc-window-hook)
-            (let ((w (get-largest-window)))
-              (if (and pop-up-windows
-                       (> (window-height w)
-                          (+ window-min-height calc-window-height 2)))
-                  (progn
-                    (setq w (split-window w
-                                          (- (window-height w)
-                                             calc-window-height 2)
-                                          nil))
-                    (set-window-buffer w (current-buffer))
-                    (select-window w))
-                (pop-to-buffer (current-buffer)))))))
+      (cond ((or (eq full-display t)
+                 (and (null full-display) calc-full-mode))
+             (switch-to-buffer (current-buffer) t))
+            ((get-buffer-window (current-buffer))
+             (select-window (get-buffer-window (current-buffer))))
+            (calc-window-hook
+             (run-hooks 'calc-window-hook))
+            (t (pop-to-buffer (current-buffer) (cons #'calc--popup-buffer nil))))
       (with-current-buffer (calc-trail-buffer)
         (and calc-display-trail
              (= (window-width) (frame-width))
-- 
2.26.2

--
Viktor Obeling