Subject: Re: bug#44080: 27.1; Display behavior of overlays `after-string` in resizable minibuffer frames

From: Clemens <[email-masked]>
Date: Sat, 24 Oct 2020 15:08:19 +0200

> The customization is done here in cus-start.el
 >
 >               (resize-mini-frames
 >                frames (choice
 >                        (const :tag "Never" nil)
 >                        (const :tag "Fit frame to buffer" t)
 >                        (function :tag "User-defined function"))
 >                 "27.1")

Thanks, now I know how that is done, I should have grepped for it.

I have attached an updated patch where I implemented your suggestions.
For now I updated the tag to 27.2. Let me know if anything else could be 
improved and thanks for giving me the opportunity to contribute a little 
to my favorite software :)
>From fa40d19d2eb6339a30d396e51201781fc8a639ca Mon Sep 17 00:00:00 2001
From: Clemens Radermacher <[email-masked]>
Date: Sat, 24 Oct 2020 14:49:45 +0200
Subject: [PATCH] Don't skip empty lines when fitting mini frame to buffer
 (bug#44080)

* lisp/window.el: (fit-mini-frame-to-buffer,
window--resize-mini-frame, fit-frame-to-buffer,
fit-frame-to-buffer-1): Fit to mini frames without skipping leading or
trailing empty lines.
* src/frame.c: (resize-mini-frames): Update docstring.
* lisp/cus-start.el: Update for customize.
* doc/lispref/minibuf.texi: (resize-mini-frames) Update description.
---
 doc/lispref/minibuf.texi |  6 ++++--
 etc/NEWS                 |  6 ++++++
 lisp/cus-start.el        |  4 ++--
 lisp/window.el           | 24 +++++++++++++++++++++---
 src/frame.c              |  2 +-
 5 files changed, 34 insertions(+), 8 deletions(-)

diff --git a/doc/lispref/minibuf.texi b/doc/lispref/minibuf.texi
index d00acd0ded..e5a0233b3c 100644
--- a/doc/lispref/minibuf.texi
+++ b/doc/lispref/minibuf.texi
@@ -2501,8 +2501,10 @@ frame is the buffer whose contents will be shown the next time that
 window is redisplayed.  The function is expected to fit the frame to
 the buffer in some appropriate way.
 
-Any other non-@code{nil} value means to resize minibuffer-only frames
-by calling @code{fit-frame-to-buffer} (@pxref{Resizing Windows}).
+Any other non-@code{nil} value means to resize minibuffer-only frames by
+calling @code{fit-mini-frame-to-buffer}, a function that behaves like
+@code{fit-frame-to-buffer} (@pxref{Resizing Windows}) but does not strip
+leading or trailing empty lines from the buffer text.
 @end defopt

diff --git a/etc/NEWS b/etc/NEWS
index 2aed575159..861ce6ff6f 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -274,6 +274,12 @@ of the next command to be displayed in a new window.
 It's bound to the command 'other-frame-prefix' that requests the buffer
 of the next command to be displayed in a new frame.
 
++++
+*** Change behavior of user option 'resize-mini-frames'.
+If set to non-nil resize the mini frame using the new function
+'fit-mini-frame-to-buffer' which won't skip leading or trailing empty
+lines of the buffer.
+
 ** Tab Bars
 
 *** The key prefix 'C-x t t' displays next command buffer in a new tab.
diff --git a/lisp/cus-start.el b/lisp/cus-start.el
index 3fd6ac031c..6927b6df6b 100644
--- a/lisp/cus-start.el
+++ b/lisp/cus-start.el
@@ -324,9 +324,9 @@ (default-frame-alist frames
              (resize-mini-frames
               frames (choice
                       (const :tag "Never" nil)
-                      (const :tag "Fit frame to buffer" t)
+                      (const :tag "Fit mini frame to buffer" t)
                       (function :tag "User-defined function"))
-               "27.1")
+               "27.2")
              (menu-bar-mode frames boolean nil
 			    ;; FIXME?
                             ;; :initialize custom-initialize-default
diff --git a/lisp/window.el b/lisp/window.el
index 1fcfffcb53..865f6fdd5c 100644
--- a/lisp/window.el
+++ b/lisp/window.el
@@ -3432,7 +3432,7 @@ (defun window--resize-mini-frame (frame)
   "Resize minibuffer-only frame FRAME."
   (if (functionp resize-mini-frames)
       (funcall resize-mini-frames frame)
-    (fit-frame-to-buffer frame)))
+    (fit-mini-frame-to-buffer frame)))
 
 (defun window--sanitize-window-sizes (horizontal)
   "Assert that all windows on selected frame are large enough.
@@ -8925,6 +8925,14 @@ (defun window--sanitize-margin (margin left right)
 
 (declare-function tool-bar-height "xdisp.c" (&optional frame pixelwise))
 
+(defun fit-mini-frame-to-buffer (&optional frame)
+  "Adjust size of minibuffer FRAME to display its contents.
+FRAME should be a minibuffer-only frame and defaults to the
+selected one.  Unlike `fit-frame-to-buffer' FRAME will fit to the
+contents of its buffer with any leading or trailing empty lines
+included."
+  (fit-frame-to-buffer-1 frame))
+
 (defun fit-frame-to-buffer (&optional frame max-height min-height max-width min-width only)
   "Adjust size of FRAME to display the contents of its buffer exactly.
 FRAME can be any live frame and defaults to the selected one.
@@ -8943,8 +8951,18 @@ (defun fit-frame-to-buffer (&optional frame max-height min-height max-width min-
 The new position and size of FRAME can be additionally determined
 by customizing the options `fit-frame-to-buffer-sizes' and
 `fit-frame-to-buffer-margins' or setting the corresponding
-parameters of FRAME."
+parameters of FRAME.
+
+Any leading or trailing empty lines of the buffer content are not
+considered."
   (interactive)
+  (fit-frame-to-buffer-1 frame max-height min-height max-width min-width only t t))
+
+(defun fit-frame-to-buffer-1 (&optional frame max-height min-height max-width min-width only from to)
+  "Helper function for `fit-frame-to-buffer'.
+FROM and TO are the buffer positions to determine the size to fit
+to, see `window-text-pixel-size'.  The remaining arguments are as
+for `fit-frame-to-buffer'."
   (unless (fboundp 'display-monitor-attributes-list)
     (user-error "Cannot resize frame in non-graphic Emacs"))
   (setq frame (window-normalize-frame frame))
@@ -9079,7 +9097,7 @@ (defun fit-frame-to-buffer (&optional frame max-height min-height max-width min-
            ;; Note: Currently, for a new frame the sizes of the header
            ;; and mode line may be estimated incorrectly
            (size
-            (window-text-pixel-size window t t max-width max-height))
+            (window-text-pixel-size window from to max-width max-height))
            (width (max (car size) min-width))
            (height (max (cdr size) min-height)))
       ;; Don't change height or width when the window's size is fixed
diff --git a/src/frame.c b/src/frame.c
index 0b707c2af8..797d885d43 100644
--- a/src/frame.c
+++ b/src/frame.c
@@ -6182,7 +6182,7 @@ window of that frame is the buffer whose text will be eventually shown
 in the minibuffer window.
 
 Any other non-nil value means to resize minibuffer-only frames by
-calling `fit-frame-to-buffer'.  */);
+calling `fit-mini-frame-to-buffer'.  */);
   resize_mini_frames = Qnil;
 
   DEFVAR_LISP ("focus-follows-mouse", focus_follows_mouse,
-- 
2.17.1