Subject: Re: bug#74924: 29.3; Buffer showing manpage jumps back to beginning

From: Juri Linkov <[email-masked]>
Date: Mon, 06 Jan 2025 21:18:10 +0200

> This code from man.el should be setq-localʼing Man-columns
> unconditionally, I think (itʼs nil by default):
>
>     (when (or window-system
> 	      (not (or (getenv "MANWIDTH") (getenv "COLUMNS"))))
>       ;; Since the page buffer is displayed beforehand,
>       ;; we can select its window and get the window/frame width.
>       (setq-local Man-columns (Man-columns))
>       (setenv "COLUMNS" (number-to-string Man-columns)))

The problem is that the number returned by '(Man-columns)'
is inapplicable in case of "MANWIDTH".

So I will add a check for 'Man-columns' like below.

However, before pushing the patch, let's solve another problem
that the manpage window jumps back to the beginning
even on a window system.  Here is the reproducible test case:

1. emacs -Q
2. M-x man RET man RET
3. C-M-v (scroll-other-window)
4. Reduce the width of the frame
5. manpage window jumps back to the beginning

So unless Martin has objections, I will also change
'with-current-buffer' to 'with-selected-window':

diff --git a/lisp/man.el b/lisp/man.el
index 29c3dec501c..ba4f01122e2 100644
--- a/lisp/man.el
+++ b/lisp/man.el
@@ -1300,8 +1300,9 @@ Man--window-state-change
 (defun Man-fit-to-window (window)
   "Adjust width of the buffer to fit columns into WINDOW boundaries."
   (when (window-live-p window)
-    (with-current-buffer (window-buffer window)
+    (with-selected-window window
       (when (and (derived-mode-p 'Man-mode)
+                 Man-columns
                  (not (eq Man-columns (Man-columns))))
         (let ((proc (get-buffer-process (current-buffer))))
           (unless (and proc (not (eq (process-status proc) 'exit)))