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

From: Stephen Berman <[email-masked]>
Date: Sat, 05 Jul 2025 12:02:52 +0200

On Sat, 05 Jul 2025 10:53:11 +0200 Manuel Giraud <[email-masked]> wrote:

> Stephen Berman <[email-masked]> writes:
>
>> 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)))))
>
> Thanks!  That works as expected.  I thought that the set-buffer (from
> calendar-in-read-only-buffer) was enough but apparently not.
>
> While here, I will complete your patch for other calls of
> calendar-set-mode-line (lunar phases,...)

I hope you haven't spent time on that yet, because it occurred to me
that the following patch might suffice:

diff --git a/lisp/calendar/calendar.el b/lisp/calendar/calendar.el
index 917624c489e..6b23cbcd1d4 100644
--- a/lisp/calendar/calendar.el
+++ b/lisp/calendar/calendar.el
@@ -1167,7 +1167,7 @@ calendar-in-read-only-buffer
      (setq buffer-read-only nil
            buffer-undo-list t)
      (erase-buffer)
-     (display-buffer ,buffer)
+     (select-window (display-buffer ,buffer))
      ,@body
      (goto-char (point-min))
      (set-buffer-modified-p nil)

It seems to DTRT for all users of `calendar-in-read-only-buffer' (after
byte-compiling), if I haven't overlooked anything.

Steve Berman