Subject: Re: bug#78957: 31.0.50; Wrong mode-line in fancy diary

From: Manuel Giraud <[email-masked]>
Date: Sat, 26 Jul 2025 19:27:16 +0200

Eli Zaretskii <[email-masked]> writes:

> Ping!  Can we please make some progress with this?

Ok.  So I've tried again this time starting from a "make extraclean"
with the attached patch and it works as expected for diary entries,
moonphases, etc.

>From 3ef79d0fb04199fb64c6ced52ff1687a3d779fc4 Mon Sep 17 00:00:00 2001
From: Manuel Giraud <[email-masked]>
Date: Sat, 5 Jul 2025 10:56:00 +0200
Subject: [PATCH] Fix mode-line string width in diary view

Bug#78957

* lisp/calendar/calendar.el (calendar-in-read-only-buffer):
Select the buffer window because, for example,
`calendar-set-mode-line' needs it.
---
 lisp/calendar/calendar.el | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)

diff --git a/lisp/calendar/calendar.el b/lisp/calendar/calendar.el
index 917624c489e..04bbc2cb8a4 100644
--- a/lisp/calendar/calendar.el
+++ b/lisp/calendar/calendar.el
@@ -1161,17 +1161,20 @@ calendar-in-read-only-buffer
 First creates or erases BUFFER as needed.  Leaves BUFFER read-only,
 with disabled undo.  Leaves point at `point-min', displays BUFFER."
   (declare (indent 1) (debug t))
-  `(progn
-     (set-buffer (get-buffer-create ,buffer))
-     (or (derived-mode-p 'special-mode) (special-mode))
-     (setq buffer-read-only nil
-           buffer-undo-list t)
-     (erase-buffer)
-     (display-buffer ,buffer)
-     ,@body
-     (goto-char (point-min))
-     (set-buffer-modified-p nil)
-     (setq buffer-read-only t)))
+  (let ((window (gensym)))
+    `(progn
+       (set-buffer (get-buffer-create ,buffer))
+       (or (derived-mode-p 'special-mode) (special-mode))
+       (setq buffer-read-only nil
+             buffer-undo-list t)
+       (erase-buffer)
+       (let ((,window (display-buffer ,buffer)))
+         (when ,window
+	   (with-selected-window ,window
+	     ,@body
+	     (goto-char (point-min)))))
+       (set-buffer-modified-p nil)
+       (setq buffer-read-only t))))
 
 ;; The following are in-line for speed; they can be called thousands of times
 ;; when looking up holidays or processing the diary.  Here, for example, are
-- 
2.50.1

-- 
Manuel Giraud