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

From: Stephen Berman <[email-masked]>
Date: Sat, 05 Jul 2025 00:20:32 +0200

On Fri, 04 Jul 2025 21:14:52 +0200 Manuel Giraud <[email-masked]> wrote:

> Hi,
>
> When viewing diary entry from the calendar, the mode line could be set
> erroneously.  The recipe:
>
>           - Have a file "/tmp/diary" with the following content:
>
> July 4, 2025 A bitter day for many
>
>           - emacs -Q
>           - M-: (setopt diary-file "/tmp/diary") <RET>
>           - M-: (toggle-frame-fullscreen) <RET>
>           - M-: (calendar) <RET>
>           - g d
>           - 2025 <RET>
>           - July <RET>
>           - 4 <RET>
>           - d
>
> Observe that the date in the mode line of the diary view buffer is not
> centered and maybe partly out of sight.  This seems to come from the
> fact that the call to `window-edges' in `calendar-set-mode-line' does
> not return correct values but I can't figure out why.

I think it's because both times `window-edges' is called in
`diary-fancy-display' the selected window is the one displaying the
Calendar, whose `window-width' is the full screen width due to
`toggle-frame-fullscreen', while the window displaying the Fancy Diary
is half as wide.  The following patch seems to fix the problem for me:

diff --git a/lisp/calendar/diary-lib.el b/lisp/calendar/diary-lib.el
index 8fb6fadfe4c..3b0275d4cee 100644
--- a/lisp/calendar/diary-lib.el
+++ b/lisp/calendar/diary-lib.el
@@ -1058,7 +1058,8 @@ diary-fancy-display
   (unless (car (diary-display-no-entries)) ; no entries
     ;; Prepare the fancy diary buffer.
     (calendar-in-read-only-buffer diary-fancy-buffer
-      (calendar-set-mode-line "Diary Entries")
+      (with-selected-window (get-buffer-window diary-fancy-buffer)
+        (calendar-set-mode-line "Diary Entries"))
       (let ((holiday-list-last-month 1)
             (holiday-list-last-year 1)
             (date (list 0 0 0))
@@ -1139,7 +1140,8 @@ diary-fancy-display
       (if (eq major-mode 'diary-fancy-display-mode)
           (run-hooks 'diary-fancy-display-mode-hook)
         (diary-fancy-display-mode))
-      (calendar-set-mode-line diary--date-string))))
+      (with-selected-window (get-buffer-window diary-fancy-buffer)
+        (calendar-set-mode-line diary--date-string)))))
 
 ;; FIXME modernize?
 (defun diary-print-entries ()

Steve Berman