Subject: 27.0.50; Marking diary entries from included files

From: Stephen Berman <[email-masked]>
Date: Sun, 18 Nov 2018 23:57:47 +0100

0. Save the following files (this can all be done from emacs -Q but this
is easier and doesn't affect the bug):
~/.emacs with this content:
----------------------------------------------------------------
(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(calendar-mark-diary-entries-flag t)
 '(diary-mark-entries-hook '(diary-mark-included-diary-files))
)
(custom-set-faces
 ;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 )
----------------------------------------------------------------
~/.emacs.d/diary with this content:
----------------------------------------------------------------
#include "~/.emacs.d/todo/test.todo"
Nov 16, 2018 test
----------------------------------------------------------------
~/.emacs.d/todo/test.todo with this content:
----------------------------------------------------------------
(("cat1" . [1 0 0 0]) ("cat2" . [1 1 0 0]))
--==-- cat1
[Nov 18, 2018] Not a diary item

==--== DONE 
--==-- cat2
* *, * Diary item

==--== DONE 
----------------------------------------------------------------

1. Start emacs without -Q

2. M-x calendar
=> The Calendar is displayed with all dates marked red as diary entries
(using the default diary face), as it should be, since the file
~/.emacs.d/todo/test.todo is included as a diary file and in the todo item
"* *, * Diary item" "* *, *" means this is diary item for every day.

3. M-x todo-show
=> The window the Calendar was displayed in now shows a Todo mode buffer
displaying cat1 of the above file:
[Nov 18, 2018] Not a diary item

4. M-x calendar
=> The same window now displays the Calendar again, but now only November
16, 2018 (the date of the item in ~/.emacs.d/diary) is marked as a diary
entry, instead of every date.

The reason is that diary-mark-entries does not take narrowing of
included files into account (todo-mode files are narrowed to the current
category: if after step 3 above you type `f' in the todo-mode buffer, it
then displays the next category, and now doing step 4 does show the
Calendar with all dates marked as diary items).

The following patch fixes this.  (Only nine years after bug#5093; better
late than never.  But I guess too late for 26.2?)

diff --git a/lisp/calendar/diary-lib.el b/lisp/calendar/diary-lib.el
index acf4b20d77..1fa1aaacd3 100644
--- a/lisp/calendar/diary-lib.el
+++ b/lisp/calendar/diary-lib.el
@@ -1412,13 +1412,15 @@ diary-mark-entries
           (setq file-glob-attrs (nth 1 (diary-pull-attrs nil '())))
           (with-syntax-table diary-syntax-table
             (save-excursion
-              (diary-mark-entries-1 'calendar-mark-date-pattern)
-              (diary-mark-sexp-entries)
-              ;; Although it looks like mark-entries-hook runs every time,
-              ;; diary-mark-included-diary-files binds it to nil
-              ;; (essentially) when it runs in included files.
-              (run-hooks 'diary-nongregorian-marking-hook
-                         'diary-mark-entries-hook))))
+              (save-restriction
+                (widen)
+                (diary-mark-entries-1 'calendar-mark-date-pattern)
+                (diary-mark-sexp-entries)
+                ;; Although it looks like mark-entries-hook runs every time,
+                ;; diary-mark-included-diary-files binds it to nil
+                ;; (essentially) when it runs in included files.
+                (run-hooks 'diary-nongregorian-marking-hook
+                           'diary-mark-entries-hook)))))
       (and temp-buff (buffer-name temp-buff) (kill-buffer temp-buff)))
     (or d-incp (message "Marking diary entries...done"))))

In GNU Emacs 27.0.50 (build 13, x86_64-pc-linux-gnu, GTK+ Version 3.22.30)
 of 2018-11-18 built on rosalinde
Repository revision: 5d140800398287c20230dda79162a7c40016d88e
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.12001000
System Description: 8.3