From 7c8f75721b42cc1b47f2599cfbdc6577bd790ac1 Mon Sep 17 00:00:00 2001 From: Martin Rudalics Date: Mon, 23 Dec 2024 10:59:11 +0100 Subject: [PATCH] Make 'fit-frame-to-buffer' work around size hints (Bug#74866) * lisp/window.el (fit-frame-to-buffer-1): When 'frame-resize-pixelwise' is nil, round up requested sizes to avoid that lines get wrapped (Bug#74866). * doc/lispref/windows.texi (Resizing Windows): Mention that with size hints one may have to set 'frame-resize-pixelwise' to make 'fit-frame-to-buffer' fit the buffer exactly. (cherry picked from commit 6017c6a986fd958732facb1bb6ea2c040981b023) --- doc/lispref/windows.texi | 4 +++- lisp/window.el | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/doc/lispref/windows.texi b/doc/lispref/windows.texi index adc294e4c99..3ff78b599de 100644 --- a/doc/lispref/windows.texi +++ b/doc/lispref/windows.texi @@ -1166,7 +1166,9 @@ frame to its buffer using the command @code{fit-frame-to-buffer}. This command adjusts the size of @var{frame} to display the contents of its buffer exactly. @var{frame} can be any live frame and defaults to the selected one. Fitting is done only if @var{frame}'s root window is -live. +a live window. On window systems that use size hints, exact fitting can +be often achieved if and only if @code{frame-resize-pixelwise} +(@pxref{Frame Size}) is non-@code{nil}. The arguments @var{max-height}, @var{min-height}, @var{max-width} and @var{min-width}, if non-@code{nil}, specify bounds on the new body size diff --git a/lisp/window.el b/lisp/window.el index 8f3e101d34c..b205c1d3517 100644 --- a/lisp/window.el +++ b/lisp/window.el @@ -9879,6 +9879,26 @@ for `fit-frame-to-buffer'." ;; Move frame down. (setq top top-margin))))) ;; Apply our changes. + (unless frame-resize-pixelwise + ;; When 'frame-resize-pixelwise' is nil, a frame cannot be + ;; necessarily fit completely even if the window's calculated + ;; width and height are integral multiples of the frame's + ;; character width and height. The size hints Emacs produces + ;; are inept to handle that when the combined sizes of the + ;; frame's fringes, scroll bar and internal border are not an + ;; integral multiple of the frame's character width (Bug#74866). + ;; Consequently, the window manager will round sizes down and + ;; this may cause lines getting wrapped. To avoid that, round + ;; sizes up here which will, however, leave a blank space at the + ;; end of the longest line(s). + (setq text-minus-body-width + (+ text-minus-body-width + (- char-width + (% text-minus-body-width char-width)))) + (setq text-minus-body-height + (+ text-minus-body-height + (- char-height + (% text-minus-body-height char-height))))) (setq text-width (if width (+ width text-minus-body-width) -- 2.39.5