Subject: Re: bug#39822: 27.0.90; Cannot set *Completions* buffer height using display-buffer-alist

From: Juri Linkov <[email-masked]>
Date: Fri, 13 Mar 2020 00:54:35 +0200

>> It seems better to handle the temp-buffer setup in the buffer display action
>> functions (currently there are two such functions that handle temp-buffer:
>> 'display-buffer--maybe-at-bottom' and 'display-buffer-in-direction'),
>> if this will help to get rid of monstrous macro 'with-displayed-buffer-window'.
>
> Then go for it.

Here is the first step, this patch seems to keep the original behavior,
but I need your help to finish it.  Could you confirm that calls of
window-preserve-size at the end of with-displayed-buffer-window are
not needed anymore after this patch is applied, because there are the
same calls of window-preserve-size at the end of window--display-buffer
that are called later after buffer contents is filled
by after-display-function in the middle of window--display-buffer:

diff --git a/lisp/window.el b/lisp/window.el
index fc1e7d4a76..5be9d0ee83 100644
--- a/lisp/window.el
+++ b/lisp/window.el
@@ -7052,6 +7052,11 @@ window--display-buffer
         (set-window-dedicated-p window display-buffer-mark-dedicated))))
     (when (memq type '(window frame tab))
       (set-window-prev-buffers window nil))
+
+    ;; Is this the right place to call former body of with-displayed-buffer-window?
+    (when (functionp (cdr (assq 'after-display-function alist)))
+      (funcall (cdr (assq 'after-display-function alist))))
+
     (let ((quit-restore (window-parameter window 'quit-restore))
 	  (height (cdr (assq 'window-height alist)))
 	  (width (cdr (assq 'window-width alist)))
diff --git a/lisp/dired.el b/lisp/dired.el
index a4de51f609..a76d223bed 100644
--- a/lisp/dired.el
+++ b/lisp/dired.el
@@ -3465,23 +3465,28 @@ dired-mark-pop-up
       (with-displayed-buffer-window
        buffer
        (cons 'display-buffer-below-selected
-	     '((window-height . fit-window-to-buffer)
-	       (preserve-size . (nil . t))))
+             `((window-height . fit-window-to-buffer)
+               (preserve-size . (nil . t))
+               (after-display-function
+                .
+                ;; Handle (t FILE) just like (FILE), here.  That value is
+                ;; used (only in some cases), to mean just one file that was
+                ;; marked, rather than the current line file.
+                ,#'(lambda ()
+                    (with-current-buffer buffer
+                      (let ((inhibit-read-only t)
+                            (inhibit-modification-hooks t))
+                        (dired-format-columns-of-files
+                         (if (eq (car files) t) (cdr files) files))
+                        (remove-text-properties (point-min) (point-max)
+                                                '(mouse-face nil help-echo nil))
+                        (setq tab-line-exclude nil)))))))
        #'(lambda (window _value)
 	   (with-selected-window window
 	     (unwind-protect
 		 (apply function args)
 	       (when (window-live-p window)
-		 (quit-restore-window window 'kill)))))
-       ;; Handle (t FILE) just like (FILE), here.  That value is
-       ;; used (only in some cases), to mean just one file that was
-       ;; marked, rather than the current line file.
-       (with-current-buffer buffer
-	 (dired-format-columns-of-files
-	  (if (eq (car files) t) (cdr files) files))
-	 (remove-text-properties (point-min) (point-max)
-				 '(mouse-face nil help-echo nil))
-	 (setq tab-line-exclude nil))))))
+		 (quit-restore-window window 'kill)))))))))
 
 (defun dired-format-columns-of-files (files)
   (let ((beg (point)))