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

From: martin rudalics <[email-masked]>
Date: Thu, 10 Jul 2025 16:42:04 +0200

> Martin, any comments to the patch, with emphasis on its safety?

If the patch is this one

diff --git a/lisp/calendar/calendar.el b/lisp/calendar/calendar.el
index 917624c489e..8b7aa71db93 100644
--- a/lisp/calendar/calendar.el
+++ b/lisp/calendar/calendar.el
@@ -1168,8 +1168,9 @@ calendar-in-read-only-buffer
             buffer-undo-list t)
       (erase-buffer)
       (display-buffer ,buffer)
-     ,@body
-     (goto-char (point-min))
+     (with-selected-window (get-buffer-window ,buffer)
+       ,@body
+       (goto-char (point-min)))
       (set-buffer-modified-p nil)
       (setq buffer-read-only t)))
Switch to BUFFER
then I would write it as

diff --git a/lisp/calendar/calendar.el b/lisp/calendar/calendar.el
index 917624c489e..67764eb0b26 100644
--- a/lisp/calendar/calendar.el
+++ b/lisp/calendar/calendar.el
@@ -1167,9 +1167,11 @@ calendar-in-read-only-buffer
       (setq buffer-read-only nil
             buffer-undo-list t)
       (erase-buffer)
-     (display-buffer ,buffer)
-     ,@body
-     (goto-char (point-min))
+     (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)))

which selects the correct window if there is one.

But I doubt that it is a good idea to use 'set-buffer' here and talk
about "Switch to BUFFER" in the first place.

martin