(diary-header-line-flag, diary-header-line-format): New variables.
[bpt/emacs.git] / lisp / calendar / diary-lib.el
CommitLineData
3afbc435 1;;; diary-lib.el --- diary functions
0808d911 2
c87a1f38
GM
3;; Copyright (C) 1989, 1990, 1992, 1993, 1994, 1995, 2003
4;; Free Software Foundation, Inc.
0808d911
ER
5
6;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
7;; Keywords: calendar
8
9;; This file is part of GNU Emacs.
10
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation; either version 2, or (at your option)
14;; any later version.
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
b578f267
EN
22;; along with GNU Emacs; see the file COPYING. If not, write to the
23;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24;; Boston, MA 02111-1307, USA.
0808d911
ER
25
26;;; Commentary:
27
28;; This collection of functions implements the diary features as described
29;; in calendar.el.
30
31;; Comments, corrections, and improvements should be sent to
32;; Edward M. Reingold Department of Computer Science
33;; (217) 333-6733 University of Illinois at Urbana-Champaign
34;; reingold@cs.uiuc.edu 1304 West Springfield Avenue
35;; Urbana, Illinois 61801
36
37;;; Code:
38
39(require 'calendar)
40
c87a1f38
GM
41(defun diary-check-diary-file ()
42 "Check that the file specified by `diary-file' exists and is readable.
43If so, return the expanded file name, otherwise signal an error."
44 (let ((d-file (substitute-in-file-name diary-file)))
45 (if (and d-file (file-exists-p d-file))
46 (if (file-readable-p d-file)
47 d-file
48 (error "Diary file `%s' is not readable" diary-file))
49 (error "Diary file `%s' does not exist" diary-file))))
50
0808d911
ER
51;;;###autoload
52(defun diary (&optional arg)
53 "Generate the diary window for ARG days starting with the current date.
54If no argument is provided, the number of days of diary entries is governed
55by the variable `number-of-diary-entries'. This function is suitable for
56execution in a `.emacs' file."
57 (interactive "P")
c87a1f38
GM
58 (diary-check-diary-file)
59 (let ((date (calendar-current-date)))
60 (list-diary-entries
61 date
62 (cond (arg (prefix-numeric-value arg))
63 ((vectorp number-of-diary-entries)
64 (aref number-of-diary-entries (calendar-day-of-week date)))
65 (t number-of-diary-entries)))))
0808d911
ER
66
67(defun view-diary-entries (arg)
68 "Prepare and display a buffer with diary entries.
69Searches the file named in `diary-file' for entries that
70match ARG days starting with the date indicated by the cursor position
71in the displayed three-month calendar."
72 (interactive "p")
c87a1f38
GM
73 (diary-check-diary-file)
74 (list-diary-entries (calendar-cursor-to-date t) arg))
0808d911 75
892e6825 76(defun view-other-diary-entries (arg d-file)
0808d911 77 "Prepare and display buffer of diary entries from an alternative diary file.
c87a1f38
GM
78Searches for entries that match ARG days, starting with the date indicated
79by the cursor position in the displayed three-month calendar.
80D-FILE specifies the file to use as the diary file."
0808d911 81 (interactive
c87a1f38 82 (list (if arg (prefix-numeric-value arg) 1)
892e6825
ER
83 (read-file-name "Enter diary file name: " default-directory nil t)))
84 (let ((diary-file d-file))
85 (view-diary-entries arg)))
0808d911
ER
86
87(autoload 'check-calendar-holidays "holidays"
88 "Check the list of holidays for any that occur on DATE.
89The value returned is a list of strings of relevant holiday descriptions.
fd4a98f0 90The holidays are those in the list `calendar-holidays'.")
0808d911
ER
91
92(autoload 'calendar-holiday-list "holidays"
93 "Form the list of holidays that occur on dates in the calendar window.
fd4a98f0 94The holidays are those in the list `calendar-holidays'.")
0808d911
ER
95
96(autoload 'diary-french-date "cal-french"
fd4a98f0 97 "French calendar equivalent of date diary entry.")
0808d911
ER
98
99(autoload 'diary-mayan-date "cal-mayan"
fd4a98f0 100 "Mayan calendar equivalent of date diary entry.")
0808d911 101
c36baedb 102(autoload 'diary-iso-date "cal-iso"
fd4a98f0 103 "ISO calendar equivalent of date diary entry.")
c36baedb 104
0808d911 105(autoload 'diary-julian-date "cal-julian"
fd4a98f0 106 "Julian calendar equivalent of date diary entry.")
0808d911
ER
107
108(autoload 'diary-astro-day-number "cal-julian"
fd4a98f0 109 "Astronomical (Julian) day number diary entry.")
0808d911 110
327b6868 111(autoload 'diary-chinese-date "cal-china"
fd4a98f0 112 "Chinese calendar equivalent of date diary entry.")
0808d911 113
327b6868 114(autoload 'diary-islamic-date "cal-islam"
fd4a98f0 115 "Islamic calendar equivalent of date diary entry.")
0808d911 116
327b6868 117(autoload 'list-islamic-diary-entries "cal-islam"
fd4a98f0 118 "Add any Islamic date entries from the diary file to `diary-entries-list'.")
0808d911 119
327b6868 120(autoload 'mark-islamic-diary-entries "cal-islam"
fd4a98f0 121 "Mark days in the calendar window that have Islamic date diary entries.")
0808d911 122
327b6868 123(autoload 'mark-islamic-calendar-date-pattern "cal-islam"
fd4a98f0 124 "Mark dates in calendar window that conform to Islamic date MONTH/DAY/YEAR.")
0808d911
ER
125
126(autoload 'diary-hebrew-date "cal-hebrew"
fd4a98f0 127 "Hebrew calendar equivalent of date diary entry.")
0808d911
ER
128
129(autoload 'diary-omer "cal-hebrew"
fd4a98f0 130 "Omer count diary entry.")
0808d911
ER
131
132(autoload 'diary-yahrzeit "cal-hebrew"
fd4a98f0 133 "Yahrzeit diary entry--entry applies if date is yahrzeit or the day before.")
0808d911
ER
134
135(autoload 'diary-parasha "cal-hebrew"
fd4a98f0 136 "Parasha diary entry--entry applies if date is a Saturday.")
0808d911
ER
137
138(autoload 'diary-rosh-hodesh "cal-hebrew"
fd4a98f0 139 "Rosh Hodesh diary entry.")
0808d911
ER
140
141(autoload 'list-hebrew-diary-entries "cal-hebrew"
fd4a98f0 142 "Add any Hebrew date entries from the diary file to `diary-entries-list'.")
0808d911
ER
143
144(autoload 'mark-hebrew-diary-entries "cal-hebrew"
fd4a98f0 145 "Mark days in the calendar window that have Hebrew date diary entries.")
0808d911
ER
146
147(autoload 'mark-hebrew-calendar-date-pattern "cal-hebrew"
fd4a98f0 148 "Mark dates in calendar window that conform to Hebrew date MONTH/DAY/YEAR.")
0808d911
ER
149
150(autoload 'diary-coptic-date "cal-coptic"
fd4a98f0 151 "Coptic calendar equivalent of date diary entry.")
0808d911
ER
152
153(autoload 'diary-ethiopic-date "cal-coptic"
fd4a98f0 154 "Ethiopic calendar equivalent of date diary entry.")
0808d911 155
a45eee72 156(autoload 'diary-persian-date "cal-persia"
fd4a98f0 157 "Persian calendar equivalent of date diary entry.")
72fb78ce 158
fd4a98f0 159(autoload 'diary-phases-of-moon "lunar" "Moon phases diary entry.")
0808d911
ER
160
161(autoload 'diary-sunrise-sunset "solar"
fd4a98f0 162 "Local time of sunrise and sunset as a diary entry.")
0808d911
ER
163
164(autoload 'diary-sabbath-candles "solar"
165 "Local time of candle lighting diary entry--applies if date is a Friday.
fd4a98f0 166No diary entry if there is no sunset on that date.")
0808d911
ER
167
168(defvar diary-syntax-table (copy-syntax-table (standard-syntax-table))
169 "The syntax table used when parsing dates in the diary file.
170It is the standard syntax table used in Fundamental mode, but with the
c87a1f38 171syntax of `*' and `:' changed to be word constituents.")
0808d911
ER
172
173(modify-syntax-entry ?* "w" diary-syntax-table)
cfd1d0ec 174(modify-syntax-entry ?: "w" diary-syntax-table)
0808d911 175
ee58da1b
SS
176(defvar diary-entries-list)
177(defvar displayed-year)
178(defvar displayed-month)
179(defvar entry)
180(defvar date)
181(defvar number)
182(defvar date-string)
ee58da1b
SS
183(defvar original-date)
184
c47a201a 185(defun diary-attrtype-convert (attrvalue type)
c87a1f38
GM
186 "Convert string ATTRVALUE to TYPE appropriate for a face description.
187Valid TYPEs are: string, symbol, int, stringtnil, tnil."
c47a201a
JB
188 (let (ret)
189 (setq ret (cond ((eq type 'string) attrvalue)
190 ((eq type 'symbol) (read attrvalue))
191 ((eq type 'int) (string-to-int attrvalue))
192 ((eq type 'stringtnil)
193 (cond ((string= "t" attrvalue) t)
194 ((string= "nil" attrvalue) nil)
195 (t attrvalue)))
196 ((eq type 'tnil)
197 (cond ((string= "t" attrvalue) t)
198 ((string= "nil" attrvalue) nil)))))
199; (message "(%s)[%s]=[%s]" (print type) attrvalue ret)
200 ret))
4e80f517 201
c47a201a
JB
202
203(defun diary-pull-attrs (entry fileglobattrs)
4e80f517
SS
204 "Pull the face-related attributes off the entry, merge with the
205fileglobattrs, and return the (possibly modified) entry and face
206data in a list of attrname attrvalue values.
c47a201a 207The entry will be modified to drop all tags that are used for face matching.
4e80f517
SS
208If entry is nil, then the fileglobattrs are being searched for,
209the fileglobattrs variable is ignored, and
210diary-glob-file-regexp-prefix is prepended to the regexps before each
c47a201a
JB
211search."
212 (save-excursion
61d094de
GM
213 (let (regexp regnum attrname attr-list attrname attrvalue type
214 ret-attr attr)
c47a201a
JB
215 (if (null entry)
216 (progn
217 (setq ret-attr '()
218 attr-list diary-face-attrs)
219 (while attr-list
220 (goto-char (point-min))
221 (setq attr (car attr-list)
222 regexp (nth 0 attr)
223 regnum (nth 1 attr)
224 attrname (nth 2 attr)
225 type (nth 3 attr)
226 regexp (concat diary-glob-file-regexp-prefix regexp))
227 (setq attrvalue nil)
228 (if (re-search-forward regexp (point-max) t)
229 (setq attrvalue (buffer-substring-no-properties
230 (match-beginning regnum)
231 (match-end regnum))))
232 (if (and attrvalue
233 (setq attrvalue (diary-attrtype-convert attrvalue type)))
234 (setq ret-attr (append ret-attr (list attrname attrvalue))))
235 (setq attr-list (cdr attr-list)))
236 (setq fileglobattrs ret-attr))
237 (progn
238 (setq ret-attr fileglobattrs
239 attr-list diary-face-attrs)
240 (while attr-list
241 (goto-char (point-min))
242 (setq attr (car attr-list)
243 regexp (nth 0 attr)
244 regnum (nth 1 attr)
245 attrname (nth 2 attr)
246 type (nth 3 attr))
247 (setq attrvalue nil)
248 (if (string-match regexp entry)
4e80f517 249 (progn
c47a201a
JB
250 (setq attrvalue (substring-no-properties entry
251 (match-beginning regnum)
252 (match-end regnum)))
253 (setq entry (replace-match "" t t entry))))
254 (if (and attrvalue
255 (setq attrvalue (diary-attrtype-convert attrvalue type)))
256 (setq ret-attr (append ret-attr (list attrname attrvalue))))
61d094de
GM
257 (setq attr-list (cdr attr-list)))))
258 (list entry ret-attr))))
4e80f517
SS
259
260
3bd9ca85
GM
261;; This can be removed once the kill/yank treatment of invisible text
262;; (see etc/TODO) is fixed. -- gm
263(defcustom diary-header-line-flag t
264 "*If non-nil, `simple-diary-display' will show a header line.
265The format of the header is specified by `diary-header-line-format'."
266 :group 'diary
267 :type 'boolean
268 :version "21.4")
269
270(defcustom diary-header-line-format
271 '(:eval (calendar-string-spread
272 (list (if selective-display
273 "Selective display active - press \"s\" in calendar \
274before edit/copy"
275 "Diary"))
276 ?\ (frame-width)))
277 "*Format of the header line displayed by `simple-diary-display'.
278Only used if `diary-header-line-flag' is non-nil."
279 :group 'diary
280 :type 'sexp
281 :version "21.4")
c47a201a 282
0808d911
ER
283(defun list-diary-entries (date number)
284 "Create and display a buffer containing the relevant lines in diary-file.
285The arguments are DATE and NUMBER; the entries selected are those
286for NUMBER days starting with date DATE. The other entries are hidden
287using selective display.
288
289Returns a list of all relevant diary entries found, if any, in order by date.
a14723d9
KH
290The list entries have the form ((month day year) string specifier) where
291\(month day year) is the date of the entry, string is the entry text, and
292specifier is the applicability. If the variable `diary-list-include-blanks'
293is t, this list includes a dummy diary entry consisting of the empty string)
294for a date with no diary entries.
0808d911
ER
295
296After the list is prepared, the hooks `nongregorian-diary-listing-hook',
297`list-diary-entries-hook', `diary-display-hook', and `diary-hook' are run.
298These hooks have the following distinct roles:
299
300 `nongregorian-diary-listing-hook' can cull dates from the diary
301 and each included file. Usually used for Hebrew or Islamic
302 diary entries in files. Applied to *each* file.
303
304 `list-diary-entries-hook' adds or manipulates diary entries from
305 external sources. Used, for example, to include diary entries
306 from other files or to sort the diary entries. Invoked *once* only,
307 before the display hook is run.
308
309 `diary-display-hook' does the actual display of information. If this is
310 nil, simple-diary-display will be used. Use add-hook to set this to
311 fancy-diary-display, if desired. If you want no diary display, use
312 add-hook to set this to ignore.
313
314 `diary-hook' is run last. This can be used for an appointment
315 notification function."
316
317 (if (< 0 number)
c87a1f38
GM
318 (let ((original-date date);; save for possible use in the hooks
319 old-diary-syntax-table
320 diary-entries-list
321 file-glob-attrs
322 (date-string (calendar-date-string date))
323 (d-file (substitute-in-file-name diary-file)))
0808d911
ER
324 (message "Preparing diary...")
325 (save-excursion
cdfeb73f 326 (let ((diary-buffer (find-buffer-visiting d-file)))
ca67b44e
RS
327 (if (not diary-buffer)
328 (set-buffer (find-file-noselect d-file t))
329 (set-buffer diary-buffer)
330 (or (verify-visited-file-modtime diary-buffer)
331 (revert-buffer t t))))
c47a201a 332 (setq file-glob-attrs (nth 1 (diary-pull-attrs nil "")))
0808d911
ER
333 (setq selective-display t)
334 (setq selective-display-ellipses nil)
3bd9ca85
GM
335 (if diary-header-line-flag
336 (setq header-line-format diary-header-line-format))
0808d911
ER
337 (setq old-diary-syntax-table (syntax-table))
338 (set-syntax-table diary-syntax-table)
339 (unwind-protect
340 (let ((buffer-read-only nil)
341 (diary-modified (buffer-modified-p))
342 (mark (regexp-quote diary-nonmarking-symbol)))
7d3ff869
GM
343 ;; First and last characters must be ^M or \n for
344 ;; selective display to work properly
0808d911
ER
345 (goto-char (1- (point-max)))
346 (if (not (looking-at "\^M\\|\n"))
347 (progn
7d3ff869 348 (goto-char (point-max))
4cf95c44 349 (insert "\^M")))
0808d911
ER
350 (goto-char (point-min))
351 (if (not (looking-at "\^M\\|\n"))
4cf95c44 352 (insert "\^M"))
0808d911
ER
353 (subst-char-in-region (point-min) (point-max) ?\n ?\^M t)
354 (calendar-for-loop i from 1 to number do
355 (let ((d diary-date-forms)
356 (month (extract-calendar-month date))
357 (day (extract-calendar-day date))
358 (year (extract-calendar-year date))
359 (entry-found (list-sexp-diary-entries date)))
360 (while d
361 (let*
362 ((date-form (if (equal (car (car d)) 'backup)
363 (cdr (car d))
364 (car d)))
365 (backup (equal (car (car d)) 'backup))
366 (dayname
ca2a5950
GM
367 (format "%s\\|%s\\.?"
368 (calendar-day-name date)
369 (calendar-day-name date 'abbrev)))
0808d911 370 (monthname
ca2a5950
GM
371 (format "\\*\\|%s\\|%s\\.?"
372 (calendar-month-name month)
373 (calendar-month-name month 'abbrev)))
0808d911
ER
374 (month (concat "\\*\\|0*" (int-to-string month)))
375 (day (concat "\\*\\|0*" (int-to-string day)))
376 (year
377 (concat
378 "\\*\\|0*" (int-to-string year)
379 (if abbreviated-calendar-year
e65fc107 380 (concat "\\|" (format "%02d" (% year 100)))
0808d911
ER
381 "")))
382 (regexp
383 (concat
384 "\\(\\`\\|\^M\\|\n\\)" mark "?\\("
385 (mapconcat 'eval date-form "\\)\\(")
386 "\\)"))
387 (case-fold-search t))
388 (goto-char (point-min))
389 (while (re-search-forward regexp nil t)
390 (if backup (re-search-backward "\\<" nil t))
391 (if (and (or (char-equal (preceding-char) ?\^M)
392 (char-equal (preceding-char) ?\n))
393 (not (looking-at " \\|\^I")))
394 ;; Diary entry that consists only of date.
395 (backward-char 1)
396 ;; Found a nonempty diary entry--make it visible and
397 ;; add it to the list.
398 (setq entry-found t)
399 (let ((entry-start (point))
61d094de 400 date-start temp)
0808d911
ER
401 (re-search-backward "\^M\\|\n\\|\\`")
402 (setq date-start (point))
403 (re-search-forward "\^M\\|\n" nil t 2)
404 (while (looking-at " \\|\^I")
405 (re-search-forward "\^M\\|\n" nil t))
406 (backward-char 1)
407 (subst-char-in-region date-start
408 (point) ?\^M ?\n t)
c47a201a
JB
409 (setq entry (buffer-substring entry-start (point))
410 temp (diary-pull-attrs entry file-glob-attrs)
61d094de 411 entry (nth 0 temp))
0808d911 412 (add-to-diary-list
a14723d9 413 date
c47a201a 414 entry
c1b0dc0c 415 (buffer-substring
86432f81 416 (1+ date-start) (1- entry-start))
61d094de 417 (copy-marker entry-start) (nth 1 temp))))))
0808d911
ER
418 (setq d (cdr d)))
419 (or entry-found
420 (not diary-list-include-blanks)
4dd618ff 421 (setq diary-entries-list
0808d911 422 (append diary-entries-list
c47a201a 423 (list (list date "" "" "" "")))))
0808d911
ER
424 (setq date
425 (calendar-gregorian-from-absolute
426 (1+ (calendar-absolute-from-gregorian date))))
427 (setq entry-found nil)))
428 (set-buffer-modified-p diary-modified))
429 (set-syntax-table old-diary-syntax-table))
430 (goto-char (point-min))
431 (run-hooks 'nongregorian-diary-listing-hook
432 'list-diary-entries-hook)
433 (if diary-display-hook
434 (run-hooks 'diary-display-hook)
435 (simple-diary-display))
436 (run-hooks 'diary-hook)
437 diary-entries-list))))
438
439(defun include-other-diary-files ()
440 "Include the diary entries from other diary files with those of diary-file.
441This function is suitable for use in `list-diary-entries-hook';
442it enables you to use shared diary files together with your own.
443The files included are specified in the diaryfile by lines of this form:
444 #include \"filename\"
445This is recursive; that is, #include directives in diary files thus included
446are obeyed. You can change the `#include' to some other string by
447changing the variable `diary-include-string'."
448 (goto-char (point-min))
449 (while (re-search-forward
450 (concat
451 "\\(\\`\\|\^M\\|\n\\)"
452 (regexp-quote diary-include-string)
453 " \"\\([^\"]*\\)\"")
454 nil t)
cb8325bf
GM
455 (let* ((diary-file (substitute-in-file-name
456 (buffer-substring-no-properties
457 (match-beginning 2) (match-end 2))))
458 (diary-list-include-blanks nil)
459 (list-diary-entries-hook 'include-other-diary-files)
460 (diary-display-hook 'ignore)
461 (diary-hook nil)
462 (d-buffer (find-buffer-visiting diary-file))
463 (diary-modified (if d-buffer
464 (save-excursion
465 (set-buffer d-buffer)
466 (buffer-modified-p)))))
0808d911
ER
467 (if (file-exists-p diary-file)
468 (if (file-readable-p diary-file)
469 (unwind-protect
470 (setq diary-entries-list
471 (append diary-entries-list
472 (list-diary-entries original-date number)))
8b431855
GM
473 (save-excursion
474 (set-buffer (find-buffer-visiting diary-file))
73e19344
RS
475 (let ((inhibit-read-only t))
476 (subst-char-in-region (point-min) (point-max) ?\^M ?\n t))
8b431855
GM
477 (setq selective-display nil)
478 (set-buffer-modified-p diary-modified)))
0808d911
ER
479 (beep)
480 (message "Can't read included diary file %s" diary-file)
481 (sleep-for 2))
482 (beep)
483 (message "Can't find included diary file %s" diary-file)
484 (sleep-for 2))))
485 (goto-char (point-min)))
486
487(defun simple-diary-display ()
488 "Display the diary buffer if there are any relevant entries or holidays."
489 (let* ((holiday-list (if holidays-in-diary-buffer
490 (check-calendar-holidays original-date)))
8ec6d48a
GM
491 (hol-string (format "%s%s%s"
492 date-string
493 (if holiday-list ": " "")
494 (mapconcat 'identity holiday-list "; ")))
495 (msg (format "No diary entries for %s" hol-string))
496 ;; If selected window is dedicated (to the calendar),
497 ;; need a new one to display the diary.
498 (pop-up-frames (window-dedicated-p (selected-window))))
499 (calendar-set-mode-line (format "Diary for %s" hol-string))
0808d911
ER
500 (if (or (not diary-entries-list)
501 (and (not (cdr diary-entries-list))
502 (string-equal (car (cdr (car diary-entries-list))) "")))
8ec6d48a 503 (if (< (length msg) (frame-width))
b3099c46 504 (message "%s" msg)
0808d911
ER
505 (set-buffer (get-buffer-create holiday-buffer))
506 (setq buffer-read-only nil)
507 (calendar-set-mode-line date-string)
508 (erase-buffer)
509 (insert (mapconcat 'identity holiday-list "\n"))
510 (goto-char (point-min))
511 (set-buffer-modified-p nil)
512 (setq buffer-read-only t)
513 (display-buffer holiday-buffer)
514 (message "No diary entries for %s" date-string))
c87a1f38
GM
515 (display-buffer (find-buffer-visiting
516 (substitute-in-file-name diary-file)))
0808d911
ER
517 (message "Preparing diary...done"))))
518
86432f81
MR
519(defface diary-button-face '((((type pc) (class color))
520 (:foreground "lightblue")))
ebea495a
MR
521 "Default face used for buttons."
522 :version "21.4"
523 :group 'diary)
86432f81
MR
524
525(define-button-type 'diary-entry
526 'action #'diary-goto-entry
527 'face #'diary-button-face)
528
529(defun diary-goto-entry (button)
530 (let ((marker (button-get button 'marker)))
531 (when marker
532 (pop-to-buffer (marker-buffer marker))
533 (goto-char (marker-position marker)))))
534
0808d911
ER
535(defun fancy-diary-display ()
536 "Prepare a diary buffer with relevant entries in a fancy, noneditable form.
537This function is provided for optional use as the `diary-display-hook'."
538 (save-excursion;; Turn off selective-display in the diary file's buffer.
cdfeb73f 539 (set-buffer (find-buffer-visiting (substitute-in-file-name diary-file)))
0808d911
ER
540 (let ((diary-modified (buffer-modified-p)))
541 (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)
542 (setq selective-display nil)
543 (kill-local-variable 'mode-line-format)
544 (set-buffer-modified-p diary-modified)))
545 (if (or (not diary-entries-list)
546 (and (not (cdr diary-entries-list))
547 (string-equal (car (cdr (car diary-entries-list))) "")))
548 (let* ((holiday-list (if holidays-in-diary-buffer
549 (check-calendar-holidays original-date)))
550 (msg (format "No diary entries for %s %s"
551 (concat date-string (if holiday-list ":" ""))
552 (mapconcat 'identity holiday-list "; "))))
553 (if (<= (length msg) (frame-width))
b3099c46 554 (message "%s" msg)
0808d911
ER
555 (set-buffer (get-buffer-create holiday-buffer))
556 (setq buffer-read-only nil)
557 (calendar-set-mode-line date-string)
558 (erase-buffer)
559 (insert (mapconcat 'identity holiday-list "\n"))
560 (goto-char (point-min))
561 (set-buffer-modified-p nil)
562 (setq buffer-read-only t)
563 (display-buffer holiday-buffer)
564 (message "No diary entries for %s" date-string)))
565 (save-excursion;; Prepare the fancy diary buffer.
566 (set-buffer (make-fancy-diary-buffer))
567 (setq buffer-read-only nil)
568 (let ((entry-list diary-entries-list)
569 (holiday-list)
570 (holiday-list-last-month 1)
571 (holiday-list-last-year 1)
572 (date (list 0 0 0)))
573 (while entry-list
574 (if (not (calendar-date-equal date (car (car entry-list))))
575 (progn
576 (setq date (car (car entry-list)))
577 (and holidays-in-diary-buffer
578 (calendar-date-compare
579 (list (list holiday-list-last-month
580 (calendar-last-day-of-month
581 holiday-list-last-month
582 holiday-list-last-year)
583 holiday-list-last-year))
584 (list date))
585 ;; We need to get the holidays for the next 3 months.
586 (setq holiday-list-last-month
587 (extract-calendar-month date))
588 (setq holiday-list-last-year
589 (extract-calendar-year date))
590 (increment-calendar-month
591 holiday-list-last-month holiday-list-last-year 1)
592 (setq holiday-list
593 (let ((displayed-month holiday-list-last-month)
594 (displayed-year holiday-list-last-year))
595 (calendar-holiday-list)))
596 (increment-calendar-month
597 holiday-list-last-month holiday-list-last-year 1))
598 (let* ((date-string (calendar-date-string date))
599 (date-holiday-list
600 (let ((h holiday-list)
601 (d))
602 ;; Make a list of all holidays for date.
603 (while h
604 (if (calendar-date-equal date (car (car h)))
605 (setq d (append d (cdr (car h)))))
606 (setq h (cdr h)))
607 d)))
608 (insert (if (= (point) (point-min)) "" ?\n) date-string)
609 (if date-holiday-list (insert ": "))
72fb78ce
ER
610 (let* ((l (current-column))
611 (longest 0))
86855ebd
SM
612 (insert (mapconcat (lambda (x)
613 (if (< longest (length x))
614 (setq longest (length x)))
615 x)
72fb78ce
ER
616 date-holiday-list
617 (concat "\n" (make-string l ? ))))
618 (insert ?\n (make-string (+ l longest) ?=) ?\n)))))
c47a201a
JB
619
620 (setq entry (car (cdr (car entry-list))))
621 (if (< 0 (length entry))
622 (progn
623 (if (nth 3 (car entry-list))
624 (insert-button (concat entry "\n")
625 'marker (nth 3 (car entry-list))
626 :type 'diary-entry)
627 (insert entry ?\n))
628 (save-excursion
61d094de
GM
629 (let* ((marks (nth 4 (car entry-list)))
630 (temp-face (make-symbol
631 (apply
632 'concat "temp-face-"
633 (mapcar '(lambda (sym)
634 (if (stringp sym)
635 sym
636 (symbol-name sym)))
637 marks))))
638 faceinfo)
639 ;; Remove :face info from the marks,
640 ;; copy the face info into temp-face
641 (setq faceinfo marks)
642 (while (setq faceinfo (memq :face faceinfo))
643 (copy-face (read (nth 1 faceinfo)) temp-face)
644 (setcar faceinfo nil)
645 (setcar (cdr faceinfo) nil))
646 (setq marks (delq nil marks))
c47a201a 647 ;; Apply the font aspects
61d094de
GM
648 (apply 'set-face-attribute temp-face nil marks)
649 (search-backward entry)
650 (overlay-put
651 (make-overlay (match-beginning 0) (match-end 0))
652 'face temp-face)))))
c47a201a 653 (setq entry-list (cdr entry-list))))
0808d911
ER
654 (set-buffer-modified-p nil)
655 (goto-char (point-min))
656 (setq buffer-read-only t)
657 (display-buffer fancy-diary-buffer)
86432f81 658 (fancy-diary-display-mode)
0808d911
ER
659 (message "Preparing diary...done"))))
660
661(defun make-fancy-diary-buffer ()
662 "Create and return the initial fancy diary buffer."
663 (save-excursion
664 (set-buffer (get-buffer-create fancy-diary-buffer))
665 (setq buffer-read-only nil)
0808d911
ER
666 (calendar-set-mode-line "Diary Entries")
667 (erase-buffer)
668 (set-buffer-modified-p nil)
669 (setq buffer-read-only t)
670 (get-buffer fancy-diary-buffer)))
671
672(defun print-diary-entries ()
673 "Print a hard copy of the diary display.
674
675If the simple diary display is being used, prepare a temp buffer with the
676visible lines of the diary buffer, add a heading line composed from the mode
677line, print the temp buffer, and destroy it.
678
679If the fancy diary display is being used, just print the buffer.
680
681The hooks given by the variable `print-diary-entries-hook' are called to do
682the actual printing."
683 (interactive)
684 (if (bufferp (get-buffer fancy-diary-buffer))
685 (save-excursion
686 (set-buffer (get-buffer fancy-diary-buffer))
687 (run-hooks 'print-diary-entries-hook))
688 (let ((diary-buffer
cdfeb73f 689 (find-buffer-visiting (substitute-in-file-name diary-file))))
0808d911
ER
690 (if diary-buffer
691 (let ((temp-buffer (get-buffer-create "*Printable Diary Entries*"))
692 (heading))
693 (save-excursion
694 (set-buffer diary-buffer)
695 (setq heading
696 (if (not (stringp mode-line-format))
697 "All Diary Entries"
698 (string-match "^-*\\([^-].*[^-]\\)-*$" mode-line-format)
699 (substring mode-line-format
700 (match-beginning 1) (match-end 1))))
701 (copy-to-buffer temp-buffer (point-min) (point-max))
702 (set-buffer temp-buffer)
703 (while (re-search-forward "\^M.*$" nil t)
704 (replace-match ""))
705 (goto-char (point-min))
706 (insert heading "\n"
707 (make-string (length heading) ?=) "\n")
708 (run-hooks 'print-diary-entries-hook)
709 (kill-buffer temp-buffer)))
710 (error "You don't have a diary buffer!")))))
711
712(defun show-all-diary-entries ()
713 "Show all of the diary entries in the diary file.
714This function gets rid of the selective display of the diary file so that
715all entries, not just some, are visible. If there is no diary buffer, one
716is created."
717 (interactive)
c87a1f38
GM
718 (let ((d-file (diary-check-diary-file)))
719 (save-excursion
720 (set-buffer (or (find-buffer-visiting d-file)
721 (find-file-noselect d-file t)))
722 (let ((buffer-read-only nil)
723 (diary-modified (buffer-modified-p)))
724 (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)
725 (setq selective-display nil
726 mode-line-format default-mode-line-format)
727 (display-buffer (current-buffer))
728 (set-buffer-modified-p diary-modified)))))
8ffbfaa9 729
4dd618ff 730(defcustom diary-mail-addr
c87a1f38 731 (if (boundp 'user-mail-address) user-mail-address "")
8ffbfaa9
RS
732 "*Email address that `diary-mail-entries' will send email to."
733 :group 'diary
c87a1f38 734 :type 'string
cd32a7ba 735 :version "20.3")
8ffbfaa9
RS
736
737(defcustom diary-mail-days 7
c87a1f38 738 "*Default number of days for `diary-mail-entries' to check."
8ffbfaa9 739 :group 'diary
cd32a7ba
DN
740 :type 'integer
741 :version "20.3")
8ffbfaa9 742
f91adf29 743;;;###autoload
8ffbfaa9
RS
744(defun diary-mail-entries (&optional ndays)
745 "Send a mail message showing diary entries for next NDAYS days.
746If no prefix argument is given, NDAYS is set to `diary-mail-days'.
c87a1f38 747Mail is sent to the address specified by `diary-mail-addr'.
8ffbfaa9
RS
748
749You can call `diary-mail-entries' every night using an at/cron job.
750For example, this script will run the program at 2am daily. Since
751`emacs -batch' does not load your `.emacs' file, you must ensure that
752all relevant variables are set, as done here.
753
754#!/bin/sh
755# diary-rem.sh -- repeatedly run the Emacs diary-reminder
756emacs -batch \\
757-eval \"(setq diary-mail-days 3 \\
c87a1f38 758 diary-file \\\"/path/to/diary.file\\\" \\
8ffbfaa9
RS
759 european-calendar-style t \\
760 diary-mail-addr \\\"user@host.name\\\" )\" \\
4dd618ff 761-l diary-lib -f diary-mail-entries
8ffbfaa9
RS
762at -f diary-rem.sh 0200 tomorrow
763
764You may have to tweak the syntax of the `at' command to suit your
765system. Alternatively, you can specify a cron entry:
7660 1 * * * diary-rem.sh
767to run it every morning at 1am."
d56aaa64 768 (interactive "P")
c87a1f38
GM
769 (if (string-equal diary-mail-addr "")
770 (error "You must set `diary-mail-addr' to use this command")
771 (let ((diary-display-hook 'fancy-diary-display))
772 (list-diary-entries (calendar-current-date) (or ndays diary-mail-days)))
773 (compose-mail diary-mail-addr
774 (concat "Diary entries generated "
775 (calendar-date-string (calendar-current-date))))
776 (insert
777 (if (get-buffer fancy-diary-buffer)
778 (save-excursion
779 (set-buffer fancy-diary-buffer)
780 (buffer-substring (point-min) (point-max)))
781 "No entries found"))
782 (call-interactively (get mail-user-agent 'sendfunc))))
d56aaa64 783
ca2a5950
GM
784(defun diary-name-pattern (string-array &optional abbrev-array paren)
785 "Return a regexp matching the strings in the array STRING-ARRAY.
786If the optional argument ABBREV-ARRAY is present, then the function
787`calendar-abbrev-construct' is used to construct abbreviations from the
788two supplied arrays. The returned regexp will then also match these
789abbreviations, with or without final `.' characters. If the optional
790argument PAREN is non-nil, the regexp is surrounded by parentheses."
791 (regexp-opt (append string-array
792 (if abbrev-array
793 (calendar-abbrev-construct abbrev-array
794 string-array))
795 (if abbrev-array
796 (calendar-abbrev-construct abbrev-array
797 string-array
798 'period))
799 nil)
800 paren))
0808d911
ER
801
802(defvar marking-diary-entries nil
803 "True during the marking of diary entries, nil otherwise.")
804
805(defvar marking-diary-entry nil
806 "True during the marking of diary entries, if current entry is marking.")
807
808(defun mark-diary-entries ()
809 "Mark days in the calendar window that have diary entries.
810Each entry in the diary file visible in the calendar window is marked.
811After the entries are marked, the hooks `nongregorian-diary-marking-hook' and
812`mark-diary-entries-hook' are run."
813 (interactive)
814 (setq mark-diary-entries-in-calendar t)
c87a1f38
GM
815 (let ((marking-diary-entries t)
816 file-glob-attrs marks)
817 (save-excursion
818 (set-buffer (find-file-noselect (diary-check-diary-file) t))
819 (message "Marking diary entries...")
820 (setq file-glob-attrs (nth 1 (diary-pull-attrs nil '())))
821 (let ((d diary-date-forms)
822 (old-diary-syntax-table (syntax-table))
823 temp)
824 (set-syntax-table diary-syntax-table)
825 (while d
826 (let* ((date-form (if (equal (car (car d)) 'backup)
827 (cdr (car d))
828 (car d)));; ignore 'backup directive
ca2a5950
GM
829 (dayname
830 (diary-name-pattern calendar-day-name-array
831 calendar-day-abbrev-array))
c87a1f38 832 (monthname
ca2a5950
GM
833 (format "%s\\|\\*"
834 (diary-name-pattern calendar-month-name-array
835 calendar-month-abbrev-array)))
c87a1f38
GM
836 (month "[0-9]+\\|\\*")
837 (day "[0-9]+\\|\\*")
838 (year "[0-9]+\\|\\*")
839 (l (length date-form))
840 (d-name-pos (- l (length (memq 'dayname date-form))))
841 (d-name-pos (if (/= l d-name-pos) (+ 2 d-name-pos)))
842 (m-name-pos (- l (length (memq 'monthname date-form))))
843 (m-name-pos (if (/= l m-name-pos) (+ 2 m-name-pos)))
844 (d-pos (- l (length (memq 'day date-form))))
845 (d-pos (if (/= l d-pos) (+ 2 d-pos)))
846 (m-pos (- l (length (memq 'month date-form))))
847 (m-pos (if (/= l m-pos) (+ 2 m-pos)))
848 (y-pos (- l (length (memq 'year date-form))))
849 (y-pos (if (/= l y-pos) (+ 2 y-pos)))
850 (regexp
851 (concat
852 "\\(\\`\\|\^M\\|\n\\)\\("
853 (mapconcat 'eval date-form "\\)\\(")
854 "\\)"))
855 (case-fold-search t))
856 (goto-char (point-min))
857 (while (re-search-forward regexp nil t)
858 (let* ((dd-name
859 (if d-name-pos
860 (buffer-substring-no-properties
861 (match-beginning d-name-pos)
862 (match-end d-name-pos))))
863 (mm-name
864 (if m-name-pos
865 (buffer-substring-no-properties
866 (match-beginning m-name-pos)
867 (match-end m-name-pos))))
868 (mm (string-to-int
869 (if m-pos
870 (buffer-substring-no-properties
871 (match-beginning m-pos)
872 (match-end m-pos))
873 "")))
874 (dd (string-to-int
875 (if d-pos
876 (buffer-substring-no-properties
877 (match-beginning d-pos)
878 (match-end d-pos))
879 "")))
880 (y-str (if y-pos
881 (buffer-substring-no-properties
882 (match-beginning y-pos)
883 (match-end y-pos))))
884 (yy (if (not y-str)
885 0
886 (if (and (= (length y-str) 2)
887 abbreviated-calendar-year)
888 (let* ((current-y
889 (extract-calendar-year
890 (calendar-current-date)))
891 (y (+ (string-to-int y-str)
892 (* 100
893 (/ current-y 100)))))
894 (if (> (- y current-y) 50)
895 (- y 100)
896 (if (> (- current-y y) 50)
897 (+ y 100)
898 y)))
899 (string-to-int y-str))))
900 (save-excursion
901 (setq entry (buffer-substring-no-properties
902 (point) (line-end-position))
903 temp (diary-pull-attrs entry file-glob-attrs)
904 entry (nth 0 temp)
905 marks (nth 1 temp))))
906 (if dd-name
907 (mark-calendar-days-named
908 (cdr (assoc-ignore-case
ca2a5950 909 dd-name
c87a1f38
GM
910 (calendar-make-alist
911 calendar-day-name-array
ca2a5950 912 0 nil calendar-day-abbrev-array))) marks)
c87a1f38 913 (if mm-name
ca2a5950
GM
914 (setq mm
915 (if (string-equal mm-name "*") 0
c87a1f38 916 (cdr (assoc-ignore-case
ca2a5950 917 mm-name
c87a1f38
GM
918 (calendar-make-alist
919 calendar-month-name-array
ca2a5950 920 1 nil calendar-month-abbrev-array))))))
c87a1f38
GM
921 (mark-calendar-date-pattern mm dd yy marks))))
922 (setq d (cdr d))))
923 (mark-sexp-diary-entries)
924 (run-hooks 'nongregorian-diary-marking-hook
925 'mark-diary-entries-hook)
926 (set-syntax-table old-diary-syntax-table)
927 (message "Marking diary entries...done")))))
0808d911
ER
928
929(defun mark-sexp-diary-entries ()
930 "Mark days in the calendar window that have sexp diary entries.
931Each entry in the diary file (or included files) visible in the calendar window
932is marked. See the documentation for the function `list-sexp-diary-entries'."
933 (let* ((sexp-mark (regexp-quote sexp-diary-entry-symbol))
934 (s-entry (concat "\\(\\`\\|\^M\\|\n\\)\\("
c87a1f38 935 sexp-mark "(\\)\\|\\("
0808d911 936 (regexp-quote diary-nonmarking-symbol)
c87a1f38
GM
937 sexp-mark "(diary-remind\\)"))
938 (file-glob-attrs (nth 1 (diary-pull-attrs nil '())))
939 m y first-date last-date mark file-glob-attrs)
0808d911
ER
940 (save-excursion
941 (set-buffer calendar-buffer)
942 (setq m displayed-month)
943 (setq y displayed-year))
944 (increment-calendar-month m y -1)
945 (setq first-date
946 (calendar-absolute-from-gregorian (list m 1 y)))
947 (increment-calendar-month m y 2)
948 (setq last-date
949 (calendar-absolute-from-gregorian
950 (list m (calendar-last-day-of-month m y) y)))
951 (goto-char (point-min))
952 (while (re-search-forward s-entry nil t)
c87a1f38 953 (setq marking-diary-entry (char-equal (preceding-char) ?\())
0808d911
ER
954 (re-search-backward "(")
955 (let ((sexp-start (point))
61d094de 956 sexp entry entry-start line-start marks)
0808d911 957 (forward-sexp)
e6c3bfbc 958 (setq sexp (buffer-substring-no-properties sexp-start (point)))
0808d911
ER
959 (save-excursion
960 (re-search-backward "\^M\\|\n\\|\\`")
961 (setq line-start (point)))
962 (forward-char 1)
963 (if (and (or (char-equal (preceding-char) ?\^M)
964 (char-equal (preceding-char) ?\n))
965 (not (looking-at " \\|\^I")))
966 (progn;; Diary entry consists only of the sexp
967 (backward-char 1)
968 (setq entry ""))
969 (setq entry-start (point))
7ff02048 970 ;; Find end of entry
0808d911
ER
971 (re-search-forward "\^M\\|\n" nil t)
972 (while (looking-at " \\|\^I")
94d1817d
KH
973 (or (re-search-forward "\^M\\|\n" nil t)
974 (re-search-forward "$" nil t)))
7ff02048
KH
975 (if (or (char-equal (preceding-char) ?\^M)
976 (char-equal (preceding-char) ?\n))
977 (backward-char 1))
e6c3bfbc 978 (setq entry (buffer-substring-no-properties entry-start (point)))
0808d911
ER
979 (while (string-match "[\^M]" entry)
980 (aset entry (match-beginning 0) ?\n )))
981 (calendar-for-loop date from first-date to last-date do
fd4a98f0
RS
982 (if (setq mark (diary-sexp-entry sexp entry
983 (calendar-gregorian-from-absolute date)))
c47a201a
JB
984 (progn
985 (setq marks (diary-pull-attrs entry file-glob-attrs)
61d094de 986 marks (nth 1 (diary-pull-attrs entry file-glob-attrs)))
c47a201a 987 (mark-visible-calendar-date
4e80f517 988 (calendar-gregorian-from-absolute date)
c47a201a
JB
989 (if (< 0 (length marks))
990 marks
991 (if (consp mark)
992 (car mark)))))))))))
0808d911
ER
993
994(defun mark-included-diary-files ()
995 "Mark the diary entries from other diary files with those of the diary file.
996This function is suitable for use as the `mark-diary-entries-hook'; it enables
997you to use shared diary files together with your own. The files included are
998specified in the diary-file by lines of this form:
999 #include \"filename\"
1000This is recursive; that is, #include directives in diary files thus included
1001are obeyed. You can change the `#include' to some other string by
1002changing the variable `diary-include-string'."
1003 (goto-char (point-min))
1004 (while (re-search-forward
1005 (concat
1006 "\\(\\`\\|\^M\\|\n\\)"
1007 (regexp-quote diary-include-string)
1008 " \"\\([^\"]*\\)\"")
1009 nil t)
1010 (let ((diary-file (substitute-in-file-name
e6c3bfbc
ER
1011 (buffer-substring-no-properties
1012 (match-beginning 2) (match-end 2))))
0808d911
ER
1013 (mark-diary-entries-hook 'mark-included-diary-files))
1014 (if (file-exists-p diary-file)
1015 (if (file-readable-p diary-file)
1016 (progn
1017 (mark-diary-entries)
cdfeb73f 1018 (kill-buffer (find-buffer-visiting diary-file)))
0808d911
ER
1019 (beep)
1020 (message "Can't read included diary file %s" diary-file)
1021 (sleep-for 2))
1022 (beep)
1023 (message "Can't find included diary file %s" diary-file)
1024 (sleep-for 2))))
1025 (goto-char (point-min)))
1026
c47a201a 1027(defun mark-calendar-days-named (dayname &optional color)
0808d911
ER
1028 "Mark all dates in the calendar window that are day DAYNAME of the week.
10290 means all Sundays, 1 means all Mondays, and so on."
1030 (save-excursion
1031 (set-buffer calendar-buffer)
1032 (let ((prev-month displayed-month)
1033 (prev-year displayed-year)
1034 (succ-month displayed-month)
1035 (succ-year displayed-year)
1036 (last-day)
1037 (day))
1038 (increment-calendar-month succ-month succ-year 1)
1039 (increment-calendar-month prev-month prev-year -1)
1040 (setq day (calendar-absolute-from-gregorian
1041 (calendar-nth-named-day 1 dayname prev-month prev-year)))
1042 (setq last-day (calendar-absolute-from-gregorian
1043 (calendar-nth-named-day -1 dayname succ-month succ-year)))
1044 (while (<= day last-day)
c47a201a 1045 (mark-visible-calendar-date (calendar-gregorian-from-absolute day) color)
0808d911
ER
1046 (setq day (+ day 7))))))
1047
c47a201a 1048(defun mark-calendar-date-pattern (month day year &optional color)
0808d911
ER
1049 "Mark all dates in the calendar window that conform to MONTH/DAY/YEAR.
1050A value of 0 in any position is a wildcard."
1051 (save-excursion
1052 (set-buffer calendar-buffer)
1053 (let ((m displayed-month)
1054 (y displayed-year))
1055 (increment-calendar-month m y -1)
1056 (calendar-for-loop i from 0 to 2 do
c47a201a 1057 (mark-calendar-month m y month day year color)
0808d911
ER
1058 (increment-calendar-month m y 1)))))
1059
c47a201a 1060(defun mark-calendar-month (month year p-month p-day p-year &optional color)
0808d911
ER
1061 "Mark dates in the MONTH/YEAR that conform to pattern P-MONTH/P_DAY/P-YEAR.
1062A value of 0 in any position of the pattern is a wildcard."
1063 (if (or (and (= month p-month)
1064 (or (= p-year 0) (= year p-year)))
1065 (and (= p-month 0)
1066 (or (= p-year 0) (= year p-year))))
1067 (if (= p-day 0)
1068 (calendar-for-loop
1069 i from 1 to (calendar-last-day-of-month month year) do
c47a201a
JB
1070 (mark-visible-calendar-date (list month i year) color))
1071 (mark-visible-calendar-date (list month p-day year) color))))
0808d911
ER
1072
1073(defun sort-diary-entries ()
1074 "Sort the list of diary entries by time of day."
1075 (setq diary-entries-list (sort diary-entries-list 'diary-entry-compare)))
1076
1077(defun diary-entry-compare (e1 e2)
1078 "Returns t if E1 is earlier than E2."
1079 (or (calendar-date-compare e1 e2)
1080 (and (calendar-date-equal (car e1) (car e2))
4dd618ff
SS
1081 (let* ((ts1 (cadr e1)) (t1 (diary-entry-time ts1))
1082 (ts2 (cadr e2)) (t2 (diary-entry-time ts2)))
1083 (or (< t1 t2)
1084 (and (= t1 t2)
1085 (string-lessp ts1 ts2)))))))
0808d911 1086
a14723d9
KH
1087(defcustom diary-unknown-time
1088 -9999
1089 "*Value returned by diary-entry-time when no time is found.
1090The default value -9999 causes entries with no recognizable time to be placed
1091before those with times; 9999 would place entries with no recognizable time
1092after those with times."
1093 :type 'integer
cd32a7ba
DN
1094 :group 'diary
1095 :version "20.3")
4dd618ff 1096
0808d911 1097(defun diary-entry-time (s)
fd4a98f0
RS
1098 "Return time at the beginning of the string S as a military-style integer.
1099For example, returns 1325 for 1:25pm.
1100Returns `diary-unknown-time' (default value -9999) if no time is recognized. The recognized forms are XXXX, X:XX, or
a14723d9
KH
1101XX:XX (military time), and XXam, XXAM, XXpm, XXPM, XX:XXam, XX:XXAM XX:XXpm,
1102or XX:XXPM."
0488694c 1103 (let ((case-fold-search nil))
4dd618ff 1104 (cond ((string-match ; Military time
619fdf3d 1105 "\\`[ \t\n\\^M]*\\([0-9]?[0-9]\\):?\\([0-9][0-9]\\)\\(\\>\\|[^ap]\\)" s)
0488694c
RS
1106 (+ (* 100 (string-to-int
1107 (substring s (match-beginning 1) (match-end 1))))
1108 (string-to-int (substring s (match-beginning 2) (match-end 2)))))
4dd618ff 1109 ((string-match ; Hour only XXam or XXpm
619fdf3d 1110 "\\`[ \t\n\\^M]*\\([0-9]?[0-9]\\)\\([ap]\\)m\\>" s)
0488694c
RS
1111 (+ (* 100 (% (string-to-int
1112 (substring s (match-beginning 1) (match-end 1)))
1113 12))
1114 (if (equal ?a (downcase (aref s (match-beginning 2))))
1115 0 1200)))
4dd618ff 1116 ((string-match ; Hour and minute XX:XXam or XX:XXpm
619fdf3d 1117 "\\`[ \t\n\\^M]*\\([0-9]?[0-9]\\):\\([0-9][0-9]\\)\\([ap]\\)m\\>" s)
0488694c
RS
1118 (+ (* 100 (% (string-to-int
1119 (substring s (match-beginning 1) (match-end 1)))
1120 12))
1121 (string-to-int (substring s (match-beginning 2) (match-end 2)))
1122 (if (equal ?a (downcase (aref s (match-beginning 3))))
1123 0 1200)))
4dd618ff 1124 (t diary-unknown-time)))) ; Unrecognizable
619fdf3d 1125
0808d911
ER
1126(defun list-sexp-diary-entries (date)
1127 "Add sexp entries for DATE from the diary file to `diary-entries-list'.
1128Also, Make them visible in the diary file. Returns t if any entries were
1129found.
1130
1131Sexp diary entries must be prefaced by a `sexp-diary-entry-symbol' (normally
1132`%%'). The form of a sexp diary entry is
1133
1134 %%(SEXP) ENTRY
1135
1136Both ENTRY and DATE are globally available when the SEXP is evaluated. If the
1137SEXP yields the value nil, the diary entry does not apply. If it yields a
1138non-nil value, ENTRY will be taken to apply to DATE; if the non-nil value is a
1139string, that string will be the diary entry in the fancy diary display.
1140
1141For example, the following diary entry will apply to the 21st of the month
1142if it is a weekday and the Friday before if the 21st is on a weekend:
1143
1144 &%%(let ((dayname (calendar-day-of-week date))
1145 (day (extract-calendar-day date)))
1146 (or
1147 (and (= day 21) (memq dayname '(1 2 3 4 5)))
1148 (and (memq day '(19 20)) (= dayname 5)))
1149 ) UIUC pay checks deposited
1150
1151A number of built-in functions are available for this type of diary entry:
1152
fd4a98f0 1153 %%(diary-date MONTH DAY YEAR &optional MARK) text
0808d911
ER
1154 Entry applies if date is MONTH, DAY, YEAR if
1155 `european-calendar-style' is nil, and DAY, MONTH, YEAR if
1156 `european-calendar-style' is t. DAY, MONTH, and YEAR
1157 can be lists of integers, the constant t, or an integer.
ee58da1b 1158 The constant t means all values. An optional parameter
fd4a98f0
RS
1159 MARK specifies a face or single-character string to use
1160 when highlighting the day in the calendar.
0808d911 1161
fd4a98f0 1162 %%(diary-float MONTH DAYNAME N &optional DAY MARK) text
0808d911
ER
1163 Entry will appear on the Nth DAYNAME of MONTH.
1164 (DAYNAME=0 means Sunday, 1 means Monday, and so on;
1165 if N is negative it counts backward from the end of
1166 the month. MONTH can be a list of months, a single
9b58d144
RS
1167 month, or t to specify all months. Optional DAY means
1168 Nth DAYNAME of MONTH on or after/before DAY. DAY defaults
ee58da1b
SS
1169 to 1 if N>0 and the last day of the month if N<0. An
1170 optional parameter MARK specifies a face or single-character
fd4a98f0 1171 string to use when highlighting the day in the calendar.
0808d911 1172
fd4a98f0 1173 %%(diary-block M1 D1 Y1 M2 D2 Y2 &optional MARK) text
0808d911
ER
1174 Entry will appear on dates between M1/D1/Y1 and M2/D2/Y2,
1175 inclusive. (If `european-calendar-style' is t, the
1176 order of the parameters should be changed to D1, M1, Y1,
ee58da1b
SS
1177 D2, M2, Y2.) An optional parameter MARK specifies a face
1178 or single-character string to use when highlighting the
fd4a98f0 1179 day in the calendar.
0808d911 1180
fd4a98f0 1181 %%(diary-anniversary MONTH DAY YEAR &optional MARK) text
0808d911
ER
1182 Entry will appear on anniversary dates of MONTH DAY, YEAR.
1183 (If `european-calendar-style' is t, the order of the
1184 parameters should be changed to DAY, MONTH, YEAR.) Text
1185 can contain %d or %d%s; %d will be replaced by the number
1186 of years since the MONTH DAY, YEAR and %s will be replaced
1187 by the ordinal ending of that number (that is, `st', `nd',
1188 `rd' or `th', as appropriate. The anniversary of February
ee58da1b
SS
1189 29 is considered to be March 1 in a non-leap year. An
1190 optional parameter MARK specifies a face or single-character
fd4a98f0 1191 string to use when highlighting the day in the calendar.
0808d911 1192
fd4a98f0 1193 %%(diary-cyclic N MONTH DAY YEAR &optional MARK) text
0808d911
ER
1194 Entry will appear every N days, starting MONTH DAY, YEAR.
1195 (If `european-calendar-style' is t, the order of the
1196 parameters should be changed to N, DAY, MONTH, YEAR.) Text
1197 can contain %d or %d%s; %d will be replaced by the number
1198 of repetitions since the MONTH DAY, YEAR and %s will
1199 be replaced by the ordinal ending of that number (that is,
ee58da1b
SS
1200 `st', `nd', `rd' or `th', as appropriate. An optional
1201 parameter MARK specifies a face or single-character string
fd4a98f0 1202 to use when highlighting the day in the calendar.
0808d911
ER
1203
1204 %%(diary-remind SEXP DAYS &optional MARKING) text
1205 Entry is a reminder for diary sexp SEXP. DAYS is either a
1206 single number or a list of numbers indicating the number(s)
1207 of days before the event that the warning(s) should occur.
1208 If the current date is (one of) DAYS before the event
1209 indicated by EXPR, then a suitable message (as specified
1210 by `diary-remind-message') appears. In addition to the
1211 reminders beforehand, the diary entry also appears on
1212 the date itself. If optional MARKING is non-nil then the
1213 *reminders* are marked on the calendar. Marking of
1214 reminders is independent of whether the entry *itself* is
1215 a marking or nonmarking one.
1216
1217 %%(diary-day-of-year)
1218 Diary entries giving the day of the year and the number of
1219 days remaining in the year will be made every day. Note
1220 that since there is no text, it makes sense only if the
1221 fancy diary display is used.
1222
1223 %%(diary-iso-date)
1224 Diary entries giving the corresponding ISO commercial date
1225 will be made every day. Note that since there is no text,
1226 it makes sense only if the fancy diary display is used.
1227
1228 %%(diary-french-date)
1229 Diary entries giving the corresponding French Revolutionary
1230 date will be made every day. Note that since there is no
1231 text, it makes sense only if the fancy diary display is used.
1232
1233 %%(diary-islamic-date)
1234 Diary entries giving the corresponding Islamic date will be
1235 made every day. Note that since there is no text, it
1236 makes sense only if the fancy diary display is used.
1237
1238 %%(diary-hebrew-date)
1239 Diary entries giving the corresponding Hebrew date will be
1240 made every day. Note that since there is no text, it
1241 makes sense only if the fancy diary display is used.
1242
1243 %%(diary-astro-day-number) Diary entries giving the corresponding
1244 astronomical (Julian) day number will be made every day.
1245 Note that since there is no text, it makes sense only if the
1246 fancy diary display is used.
1247
1248 %%(diary-julian-date) Diary entries giving the corresponding
1249 Julian date will be made every day. Note that since
1250 there is no text, it makes sense only if the fancy diary
1251 display is used.
1252
1253 %%(diary-sunrise-sunset)
1254 Diary entries giving the local times of sunrise and sunset
1255 will be made every day. Note that since there is no text,
1256 it makes sense only if the fancy diary display is used.
1257 Floating point required.
1258
1259 %%(diary-phases-of-moon)
1260 Diary entries giving the times of the phases of the moon
1261 will be when appropriate. Note that since there is no text,
1262 it makes sense only if the fancy diary display is used.
1263 Floating point required.
1264
1265 %%(diary-yahrzeit MONTH DAY YEAR) text
1266 Text is assumed to be the name of the person; the date is
1267 the date of death on the *civil* calendar. The diary entry
1268 will appear on the proper Hebrew-date anniversary and on the
1269 day before. (If `european-calendar-style' is t, the order
1270 of the parameters should be changed to DAY, MONTH, YEAR.)
4dd618ff 1271
0808d911
ER
1272 %%(diary-rosh-hodesh)
1273 Diary entries will be made on the dates of Rosh Hodesh on
1274 the Hebrew calendar. Note that since there is no text, it
1275 makes sense only if the fancy diary display is used.
1276
1277 %%(diary-parasha)
1278 Diary entries giving the weekly parasha will be made on
1279 every Saturday. Note that since there is no text, it
1280 makes sense only if the fancy diary display is used.
1281
1282 %%(diary-omer)
1283 Diary entries giving the omer count will be made every day
59ba214d 1284 from Passover to Shavuot. Note that since there is no text,
0808d911
ER
1285 it makes sense only if the fancy diary display is used.
1286
1287Marking these entries is *extremely* time consuming, so these entries are
1288best if they are nonmarking."
c87a1f38
GM
1289 (let ((s-entry (concat "\\(\\`\\|\^M\\|\n\\)"
1290 (regexp-quote diary-nonmarking-symbol)
1291 "?"
1292 (regexp-quote sexp-diary-entry-symbol)
1293 "("))
1294 entry-found file-glob-attrs marks)
0808d911 1295 (goto-char (point-min))
c47a201a
JB
1296 (save-excursion
1297 (setq file-glob-attrs (nth 1 (diary-pull-attrs nil '()))))
0808d911
ER
1298 (while (re-search-forward s-entry nil t)
1299 (backward-char 1)
1300 (let ((sexp-start (point))
c87a1f38 1301 sexp entry specifier entry-start line-start)
0808d911 1302 (forward-sexp)
e6c3bfbc 1303 (setq sexp (buffer-substring-no-properties sexp-start (point)))
0808d911
ER
1304 (save-excursion
1305 (re-search-backward "\^M\\|\n\\|\\`")
1306 (setq line-start (point)))
a14723d9 1307 (setq specifier
86432f81
MR
1308 (buffer-substring-no-properties (1+ line-start) (point))
1309 entry-start (1+ line-start))
0808d911
ER
1310 (forward-char 1)
1311 (if (and (or (char-equal (preceding-char) ?\^M)
1312 (char-equal (preceding-char) ?\n))
1313 (not (looking-at " \\|\^I")))
1314 (progn;; Diary entry consists only of the sexp
1315 (backward-char 1)
1316 (setq entry ""))
1317 (setq entry-start (point))
1318 (re-search-forward "\^M\\|\n" nil t)
1319 (while (looking-at " \\|\^I")
1320 (re-search-forward "\^M\\|\n" nil t))
1321 (backward-char 1)
e6c3bfbc 1322 (setq entry (buffer-substring-no-properties entry-start (point)))
0808d911
ER
1323 (while (string-match "[\^M]" entry)
1324 (aset entry (match-beginning 0) ?\n )))
61d094de
GM
1325 (let ((diary-entry (diary-sexp-entry sexp entry date))
1326 temp)
c47a201a
JB
1327 (setq entry (if (consp diary-entry)
1328 (cdr diary-entry)
1329 diary-entry))
0808d911 1330 (if diary-entry
c47a201a
JB
1331 (progn
1332 (subst-char-in-region line-start (point) ?\^M ?\n t)
1333 (if (< 0 (length entry))
1334 (setq temp (diary-pull-attrs entry file-glob-attrs)
1335 entry (nth 0 temp)
1336 marks (nth 1 temp)))))
1337 (add-to-diary-list date
1338 entry
86432f81
MR
1339 specifier
1340 (if entry-start (copy-marker entry-start)
4e80f517 1341 nil)
c47a201a 1342 marks)
fd4a98f0 1343 (setq entry-found (or entry-found diary-entry)))))
0808d911
ER
1344 entry-found))
1345
1346(defun diary-sexp-entry (sexp entry date)
1347 "Process a SEXP diary ENTRY for DATE."
1348 (let ((result (if calendar-debug-sexp
1349 (let ((stack-trace-on-error t))
1350 (eval (car (read-from-string sexp))))
1351 (condition-case nil
1352 (eval (car (read-from-string sexp)))
1353 (error
1354 (beep)
1355 (message "Bad sexp at line %d in %s: %s"
1356 (save-excursion
1357 (save-restriction
1358 (narrow-to-region 1 (point))
1359 (goto-char (point-min))
1360 (let ((lines 1))
1361 (while (re-search-forward "\n\\|\^M" nil t)
1362 (setq lines (1+ lines)))
1363 lines)))
1364 diary-file sexp)
1365 (sleep-for 2))))))
fd4a98f0
RS
1366 (cond ((stringp result) result)
1367 ((and (consp result)
1368 (stringp (cdr result))) result)
1369 (result entry)
1370 (t nil))))
0808d911 1371
fd4a98f0 1372(defun diary-date (month day year &optional mark)
0808d911
ER
1373 "Specific date(s) diary entry.
1374Entry applies if date is MONTH, DAY, YEAR if `european-calendar-style' is nil,
1375and DAY, MONTH, YEAR if `european-calendar-style' is t. DAY, MONTH, and YEAR
1376can be lists of integers, the constant t, or an integer. The constant t means
fd4a98f0
RS
1377all values.
1378
ee58da1b 1379An optional parameter MARK specifies a face or single-character string to
fd4a98f0 1380use when highlighting the day in the calendar."
c87a1f38 1381 (let ((dd (if european-calendar-style
0808d911
ER
1382 month
1383 day))
c87a1f38 1384 (mm (if european-calendar-style
0808d911
ER
1385 day
1386 month))
c87a1f38
GM
1387 (m (extract-calendar-month date))
1388 (y (extract-calendar-year date))
1389 (d (extract-calendar-day date)))
0808d911
ER
1390 (if (and
1391 (or (and (listp dd) (memq d dd))
1392 (equal d dd)
1393 (eq dd t))
1394 (or (and (listp mm) (memq m mm))
1395 (equal m mm)
1396 (eq mm t))
1397 (or (and (listp year) (memq y year))
1398 (equal y year)
1399 (eq year t)))
86432f81 1400 (cons mark entry))))
0808d911 1401
fd4a98f0 1402(defun diary-block (m1 d1 y1 m2 d2 y2 &optional mark)
0808d911 1403 "Block diary entry.
5860ba6e
RS
1404Entry applies if date is between, or on one of, two dates.
1405The order of the parameters is
666333c0 1406M1, D1, Y1, M2, D2, Y2 if `european-calendar-style' is nil, and
fd4a98f0
RS
1407D1, M1, Y1, D2, M2, Y2 if `european-calendar-style' is t.
1408
ee58da1b 1409An optional parameter MARK specifies a face or single-character string to
fd4a98f0
RS
1410use when highlighting the day in the calendar."
1411
0808d911
ER
1412 (let ((date1 (calendar-absolute-from-gregorian
1413 (if european-calendar-style
1414 (list d1 m1 y1)
1415 (list m1 d1 y1))))
1416 (date2 (calendar-absolute-from-gregorian
1417 (if european-calendar-style
1418 (list d2 m2 y2)
1419 (list m2 d2 y2))))
1420 (d (calendar-absolute-from-gregorian date)))
1421 (if (and (<= date1 d) (<= d date2))
fd4a98f0 1422 (cons mark entry))))
0808d911 1423
fd4a98f0 1424(defun diary-float (month dayname n &optional day mark)
0808d911
ER
1425 "Floating diary entry--entry applies if date is the nth dayname of month.
1426Parameters are MONTH, DAYNAME, N. MONTH can be a list of months, the constant
1427t, or an integer. The constant t means all months. If N is negative, count
9b58d144
RS
1428backward from the end of the month.
1429
fd4a98f0 1430An optional parameter DAY means the Nth DAYNAME on or after/before MONTH DAY.
ee58da1b 1431Optional MARK specifies a face or single-character string to use when
fd4a98f0 1432highlighting the day in the calendar."
9b58d144
RS
1433;; This is messy because the diary entry may apply, but the date on which it
1434;; is based can be in a different month/year. For example, asking for the
1435;; first Monday after December 30. For large values of |n| the problem is
1436;; more grotesque.
1437 (and (= dayname (calendar-day-of-week date))
1438 (let* ((m (extract-calendar-month date))
1439 (d (extract-calendar-day date))
1440 (y (extract-calendar-year date))
1441 (limit; last (n>0) or first (n<0) possible base date for entry
1442 (calendar-nth-named-absday (- n) dayname m y d))
1443 (last-abs (if (> n 0) limit (+ limit 6)))
1444 (first-abs (if (> n 0) (- limit 6) limit))
1445 (last (calendar-gregorian-from-absolute last-abs))
1446 (first (calendar-gregorian-from-absolute first-abs))
1447 ; m1, d1 is first possible base date
1448 (m1 (extract-calendar-month first))
1449 (d1 (extract-calendar-day first))
1450 (y1 (extract-calendar-year first))
1451 ; m2, d2 is last possible base date
1452 (m2 (extract-calendar-month last))
1453 (d2 (extract-calendar-day last))
1454 (y2 (extract-calendar-year last)))
a2faecac 1455 (if (or (and (= m1 m2) ; only possible base dates in one month
d56aaa64
GM
1456 (or (eq month t)
1457 (if (listp month)
1458 (memq m1 month)
1459 (= m1 month)))
9f596b33
RS
1460 (let ((d (or day (if (> n 0)
1461 1
1462 (calendar-last-day-of-month m1 y1)))))
1463 (and (<= d1 d) (<= d d2))))
1464 ;; only possible base dates straddle two months
b5972d79
RS
1465 (and (or (< y1 y2)
1466 (and (= y1 y2) (< m1 m2)))
9f596b33 1467 (or
a2faecac 1468 ;; m1, d1 works as a base date
9f596b33 1469 (and
d56aaa64
GM
1470 (or (eq month t)
1471 (if (listp month)
1472 (memq m1 month)
1473 (= m1 month)))
9f596b33
RS
1474 (<= d1 (or day (if (> n 0)
1475 1
1476 (calendar-last-day-of-month m1 y1)))))
a2faecac 1477 ;; m2, d2 works as a base date
d56aaa64
GM
1478 (and (or (eq month t)
1479 (if (listp month)
1480 (memq m2 month)
1481 (= m2 month)))
9f596b33
RS
1482 (<= (or day (if (> n 0)
1483 1
1484 (calendar-last-day-of-month m2 y2)))
1485 d2)))))
fd4a98f0 1486 (cons mark entry)))))
0808d911 1487
d56aaa64 1488
fd4a98f0 1489(defun diary-anniversary (month day year &optional mark)
0808d911
ER
1490 "Anniversary diary entry.
1491Entry applies if date is the anniversary of MONTH, DAY, YEAR if
1492`european-calendar-style' is nil, and DAY, MONTH, YEAR if
1493`european-calendar-style' is t. Diary entry can contain `%d' or `%d%s'; the
1494%d will be replaced by the number of years since the MONTH DAY, YEAR and the
1495%s will be replaced by the ordinal ending of that number (that is, `st', `nd',
1496`rd' or `th', as appropriate. The anniversary of February 29 is considered
fd4a98f0
RS
1497to be March 1 in non-leap years.
1498
ee58da1b 1499An optional parameter MARK specifies a face or single-character string to
fd4a98f0 1500use when highlighting the day in the calendar."
0808d911
ER
1501 (let* ((d (if european-calendar-style
1502 month
1503 day))
1504 (m (if european-calendar-style
1505 day
1506 month))
1507 (y (extract-calendar-year date))
1508 (diff (- y year)))
1509 (if (and (= m 2) (= d 29) (not (calendar-leap-year-p y)))
1510 (setq m 3
1511 d 1))
1512 (if (and (> diff 0) (calendar-date-equal (list m d y) date))
fd4a98f0 1513 (cons mark (format entry diff (diary-ordinal-suffix diff))))))
0808d911 1514
fd4a98f0 1515(defun diary-cyclic (n month day year &optional mark)
0808d911
ER
1516 "Cycle diary entry--entry applies every N days starting at MONTH, DAY, YEAR.
1517If `european-calendar-style' is t, parameters are N, DAY, MONTH, YEAR.
1518ENTRY can contain `%d' or `%d%s'; the %d will be replaced by the number of
d861718a
GM
1519repetitions since the MONTH DAY, YEAR and %s will be replaced by the
1520ordinal ending of that number (that is, `st', `nd', `rd' or `th', as
fd4a98f0
RS
1521appropriate.
1522
ee58da1b 1523An optional parameter MARK specifies a face or single-character string to
fd4a98f0 1524use when highlighting the day in the calendar."
0808d911
ER
1525 (let* ((d (if european-calendar-style
1526 month
1527 day))
1528 (m (if european-calendar-style
1529 day
1530 month))
1531 (diff (- (calendar-absolute-from-gregorian date)
1532 (calendar-absolute-from-gregorian
1533 (list m d year))))
1534 (cycle (/ diff n)))
1535 (if (and (>= diff 0) (zerop (% diff n)))
fd4a98f0 1536 (cons mark (format entry cycle (diary-ordinal-suffix cycle))))))
0808d911
ER
1537
1538(defun diary-ordinal-suffix (n)
1539 "Ordinal suffix for N. (That is, `st', `nd', `rd', or `th', as appropriate.)"
1540 (if (or (memq (% n 100) '(11 12 13))
1541 (< 3 (% n 10)))
1542 "th"
1543 (aref ["th" "st" "nd" "rd"] (% n 10))))
1544
1545(defun diary-day-of-year ()
1546 "Day of year and number of days remaining in the year of date diary entry."
1547 (calendar-day-of-year-string date))
1548
5e11a170 1549(defcustom diary-remind-message
0808d911
ER
1550 '("Reminder: Only "
1551 (if (= 0 (% days 7))
1552 (concat (int-to-string (/ days 7)) (if (= 7 days) " week" " weeks"))
1553 (concat (int-to-string days) (if (= 1 days) " day" " days")))
1554 " until "
1555 diary-entry)
1556 "*Pseudo-pattern giving form of reminder messages in the fancy diary
1557display.
4dd618ff 1558
0808d911
ER
1559Used by the function `diary-remind', a pseudo-pattern is a list of
1560expressions that can involve the keywords `days' (a number), `date' (a list of
5e11a170
RS
1561month, day, year), and `diary-entry' (a string)."
1562 :type 'sexp
1563 :group 'diary)
0808d911
ER
1564
1565(defun diary-remind (sexp days &optional marking)
1566 "Provide a reminder of a diary entry.
1567SEXP is a diary-sexp. DAYS is either a single number or a list of numbers
1568indicating the number(s) of days before the event that the warning(s) should
1569occur on. If the current date is (one of) DAYS before the event indicated by
1570SEXP, then a suitable message (as specified by `diary-remind-message' is
1571returned.
1572
17b7580f
KH
1573In addition to the reminders beforehand, the diary entry also appears on the
1574date itself.
1575
1576A `diary-nonmarking-symbol' at the beginning of the line of the diary-remind
1577entry specifies that the diary entry (not the reminder) is non-marking.
1578Marking of reminders is independent of whether the entry itself is a marking
1579or nonmarking; if optional parameter MARKING is non-nil then the reminders are
1580marked on the calendar."
1581 (let ((diary-entry (eval sexp)))
1582 (cond
1583 ;; Diary entry applies on date
1584 ((and diary-entry
1585 (or (not marking-diary-entries) marking-diary-entry))
1586 diary-entry)
1587 ;; Diary entry may apply to `days' before date
1588 ((and (integerp days)
1589 (not diary-entry); Diary entry does not apply to date
1590 (or (not marking-diary-entries) marking))
1591 (let ((date (calendar-gregorian-from-absolute
1592 (+ (calendar-absolute-from-gregorian date) days))))
1593 (if (setq diary-entry (eval sexp))
1594 (mapconcat 'eval diary-remind-message ""))))
1595 ;; Diary entry may apply to one of a list of days before date
1596 ((and (listp days) days)
1597 (or (diary-remind sexp (car days) marking)
1598 (diary-remind sexp (cdr days) marking))))))
0808d911 1599
c47a201a
JB
1600(defun add-to-diary-list (date string specifier marker &optional globcolor)
1601 "Add the entry (DATE STRING SPECIFIER MARKER GLOBCOLOR) to `diary-entries-list'.
0808d911 1602Do nothing if DATE or STRING is nil."
61d094de
GM
1603 (when (and date string)
1604 (if diary-file-name-prefix
1605 (let ((prefix (funcall diary-file-name-prefix-function
1606 (buffer-file-name))))
1607 (or (string= prefix "")
1608 (setq string (format "[%s] %s" prefix string)))))
1609 (setq diary-entries-list
1610 (append diary-entries-list
1611 (list (list date string specifier marker globcolor))))))
0808d911
ER
1612
1613(defun make-diary-entry (string &optional nonmarking file)
1614 "Insert a diary entry STRING which may be NONMARKING in FILE.
c87a1f38 1615If omitted, NONMARKING defaults to nil and FILE defaults to `diary-file'."
8ec6d48a
GM
1616 (let ((pop-up-frames (window-dedicated-p (selected-window))))
1617 (find-file-other-window (substitute-in-file-name (or file diary-file))))
b8f2671f 1618 (widen)
0808d911 1619 (goto-char (point-max))
b8f2671f
JB
1620 (when (let ((case-fold-search t))
1621 (search-backward "Local Variables:"
1622 (max (- (point-max) 3000) (point-min))
1623 t))
1624 (beginning-of-line)
1625 (insert "\n")
1626 (previous-line 1))
0808d911
ER
1627 (insert
1628 (if (bolp) "" "\n")
1629 (if nonmarking diary-nonmarking-symbol "")
1630 string " "))
1631
1632(defun insert-diary-entry (arg)
1633 "Insert a diary entry for the date indicated by point.
1634Prefix arg will make the entry nonmarking."
1635 (interactive "P")
1636 (make-diary-entry (calendar-date-string (calendar-cursor-to-date t) t t)
1637 arg))
1638
1639(defun insert-weekly-diary-entry (arg)
1640 "Insert a weekly diary entry for the day of the week indicated by point.
1641Prefix arg will make the entry nonmarking."
1642 (interactive "P")
1643 (make-diary-entry (calendar-day-name (calendar-cursor-to-date t))
1644 arg))
1645
1646(defun insert-monthly-diary-entry (arg)
1647 "Insert a monthly diary entry for the day of the month indicated by point.
1648Prefix arg will make the entry nonmarking."
1649 (interactive "P")
c87a1f38
GM
1650 (let ((calendar-date-display-form
1651 (if european-calendar-style
1652 '(day " * ")
1653 '("* " day))))
0808d911
ER
1654 (make-diary-entry (calendar-date-string (calendar-cursor-to-date t) t)
1655 arg)))
1656
1657(defun insert-yearly-diary-entry (arg)
1658 "Insert an annual diary entry for the day of the year indicated by point.
1659Prefix arg will make the entry nonmarking."
1660 (interactive "P")
c87a1f38
GM
1661 (let ((calendar-date-display-form
1662 (if european-calendar-style
1663 '(day " " monthname)
1664 '(monthname " " day))))
0808d911
ER
1665 (make-diary-entry (calendar-date-string (calendar-cursor-to-date t) t)
1666 arg)))
1667
1668(defun insert-anniversary-diary-entry (arg)
1669 "Insert an anniversary diary entry for the date given by point.
1670Prefix arg will make the entry nonmarking."
1671 (interactive "P")
c87a1f38
GM
1672 (let ((calendar-date-display-form
1673 (if european-calendar-style
1674 '(day " " month " " year)
1675 '(month " " day " " year))))
0808d911
ER
1676 (make-diary-entry
1677 (format "%s(diary-anniversary %s)"
1678 sexp-diary-entry-symbol
1679 (calendar-date-string (calendar-cursor-to-date t) nil t))
1680 arg)))
1681
1682(defun insert-block-diary-entry (arg)
1683 "Insert a block diary entry for the days between the point and marked date.
1684Prefix arg will make the entry nonmarking."
1685 (interactive "P")
c87a1f38
GM
1686 (let ((calendar-date-display-form
1687 (if european-calendar-style
1688 '(day " " month " " year)
1689 '(month " " day " " year)))
0808d911
ER
1690 (cursor (calendar-cursor-to-date t))
1691 (mark (or (car calendar-mark-ring)
1692 (error "No mark set in this buffer")))
c87a1f38 1693 start end)
0808d911
ER
1694 (if (< (calendar-absolute-from-gregorian mark)
1695 (calendar-absolute-from-gregorian cursor))
1696 (setq start mark
1697 end cursor)
1698 (setq start cursor
1699 end mark))
1700 (make-diary-entry
1701 (format "%s(diary-block %s %s)"
1702 sexp-diary-entry-symbol
1703 (calendar-date-string start nil t)
1704 (calendar-date-string end nil t))
1705 arg)))
1706
1707(defun insert-cyclic-diary-entry (arg)
1708 "Insert a cyclic diary entry starting at the date given by point.
1709Prefix arg will make the entry nonmarking."
1710 (interactive "P")
c87a1f38
GM
1711 (let ((calendar-date-display-form
1712 (if european-calendar-style
1713 '(day " " month " " year)
1714 '(month " " day " " year))))
0808d911
ER
1715 (make-diary-entry
1716 (format "%s(diary-cyclic %d %s)"
1717 sexp-diary-entry-symbol
1718 (calendar-read "Repeat every how many days: "
86855ebd 1719 (lambda (x) (> x 0)))
0808d911
ER
1720 (calendar-date-string (calendar-cursor-to-date t) nil t))
1721 arg)))
1722
86432f81
MR
1723;;;###autoload
1724(define-derived-mode diary-mode text-mode
1725 "Diary"
1726 "Major mode for editing the diary file."
1727 (set (make-local-variable 'font-lock-defaults)
1728 '(diary-font-lock-keywords t)))
1729
1730(define-derived-mode fancy-diary-display-mode text-mode
1731 "Diary"
1732 "Major mode used while displaying diary entries using Fancy Display."
1733 (set (make-local-variable 'font-lock-defaults)
4e80f517
SS
1734 '(fancy-diary-font-lock-keywords t))
1735 (define-key (current-local-map) "q" 'quit-window))
86432f81
MR
1736
1737
1738(defvar fancy-diary-font-lock-keywords
1739 (list
1740 (cons
1741 (concat
ca2a5950
GM
1742 (let ((dayname (diary-name-pattern calendar-day-name-array nil t))
1743 (monthname (diary-name-pattern calendar-month-name-array nil t))
86432f81 1744 (day "[0-9]+")
e440b20e 1745 (month "[0-9]+")
86432f81
MR
1746 (year "-?[0-9]+"))
1747 (mapconcat 'eval calendar-date-display-form ""))
1748 "\\(\\(: .*\\)\\|\\(\n +.*\\)\\)*\n=+$")
1749 'diary-face)
1750 '("^.*anniversary.*$" . font-lock-keyword-face)
1751 '("^.*birthday.*$" . font-lock-keyword-face)
1752 '("^.*Yahrzeit.*$" . font-lock-reference-face)
1753 '("^\\(Erev \\)?Rosh Hodesh.*" . font-lock-function-name-face)
1754 '("^Day.*omer.*$" . font-lock-builtin-face)
1755 '("^Parashat.*$" . font-lock-comment-face)
1756 '("^[ \t]*[0-9]?[0-9]\\(:?[0-9][0-9]\\)?\\(am\\|pm\\|AM\\|PM\\)?\\(-[0-9]?[0-9]\\(:?[0-9][0-9]\\)?\\(am\\|pm\\|AM\\|PM\\)?\\)?"
1757 . font-lock-variable-name-face))
1758 "Keywords to highlight in fancy diary display")
1759
1760
1761(defun font-lock-diary-sexps (limit)
1762 "Recognize sexp diary entry for font-locking."
1763 (if (re-search-forward
1764 (concat "^" (regexp-quote diary-nonmarking-symbol)
1765 "?\\(" (regexp-quote sexp-diary-entry-symbol) "\\)")
1766 limit t)
1767 (condition-case nil
1768 (save-restriction
1769 (narrow-to-region (point-min) limit)
1770 (let ((start (point)))
1771 (forward-sexp 1)
1772 (store-match-data (list start (point)))
1773 t))
1774 (error t))))
1775
ca2a5950
GM
1776(defun font-lock-diary-date-forms (month-array &optional symbol abbrev-array)
1777 "Create font-lock patterns for `diary-date-forms' using MONTH-ARRAY.
86432f81 1778If given, optional SYMBOL must be a prefix to entries.
ca2a5950
GM
1779If optional ABBREV-ARRAY is present, the abbreviations constructed
1780from this array by the function `calendar-abbrev-construct' are
1781matched (with or without a final `.'), in addition to the full month
1782names."
1783 (let ((dayname (diary-name-pattern calendar-day-name-array
1784 calendar-day-abbrev-array t))
1785 (monthname (format "\\(%s\\|\\*\\)"
1786 (diary-name-pattern month-array abbrev-array)))
c87a1f38
GM
1787 (month "\\([0-9]+\\|\\*\\)")
1788 (day "\\([0-9]+\\|\\*\\)")
1789 (year "-?\\([0-9]+\\|\\*\\)"))
86432f81
MR
1790 (mapcar '(lambda (x)
1791 (cons
1792 (concat "^" (regexp-quote diary-nonmarking-symbol) "?"
1793 (if symbol (regexp-quote symbol) "") "\\("
1794 (mapconcat 'eval
1795 ;; If backup, omit first item (backup)
1796 ;; and last item (not part of date)
1797 (if (equal (car x) 'backup)
1798 (reverse (cdr (reverse (cdr x))))
1799 x)
1800 "")
1801 ;; With backup, last item is not part of date
1802 (if (equal (car x) 'backup)
1803 (concat "\\)" (eval (car (reverse x))))
1804 "\\)"))
1805 '(1 diary-face)))
1806 diary-date-forms)))
1807
ca2a5950
GM
1808(eval-when-compile (require 'cal-hebrew)
1809 (require 'cal-islam))
1810
86432f81
MR
1811(defvar diary-font-lock-keywords
1812 (append
ca2a5950
GM
1813 (font-lock-diary-date-forms calendar-month-name-array
1814 nil calendar-month-abbrev-array)
c87a1f38
GM
1815 (when (or (memq 'mark-hebrew-diary-entries
1816 nongregorian-diary-marking-hook)
1817 (memq 'list-hebrew-diary-entries
1818 nongregorian-diary-listing-hook))
1819 (require 'cal-hebrew)
1820 (font-lock-diary-date-forms
1821 calendar-hebrew-month-name-array-leap-year
ca2a5950 1822 hebrew-diary-entry-symbol))
c87a1f38
GM
1823 (when (or (memq 'mark-islamic-diary-entries
1824 nongregorian-diary-marking-hook)
1825 (memq 'list-islamic-diary-entries
1826 nongregorian-diary-listing-hook))
1827 (require 'cal-islam)
1828 (font-lock-diary-date-forms
1829 calendar-islamic-month-name-array
ca2a5950 1830 islamic-diary-entry-symbol))
86432f81
MR
1831 (list
1832 (cons
1833 (concat "^" (regexp-quote diary-include-string) ".*$")
1834 'font-lock-keyword-face)
1835 (cons
1836 (concat "^" (regexp-quote diary-nonmarking-symbol)
1837 "?\\(" (regexp-quote sexp-diary-entry-symbol) "\\)")
1838 '(1 font-lock-reference-face))
1839 (cons
1840 (concat "^" (regexp-quote diary-nonmarking-symbol))
1841 'font-lock-reference-face)
1842 (cons
1843 (concat "^" (regexp-quote diary-nonmarking-symbol)
1844 "?\\(" (regexp-quote hebrew-diary-entry-symbol) "\\)")
1845 '(1 font-lock-reference-face))
1846 (cons
1847 (concat "^" (regexp-quote diary-nonmarking-symbol)
1848 "?\\(" (regexp-quote islamic-diary-entry-symbol) "\\)")
1849 '(1 font-lock-reference-face))
1850 '(font-lock-diary-sexps . font-lock-keyword-face)
1851 '("[0-9]?[0-9]\\(:?[0-9][0-9]\\)?\\(am\\|pm\\|AM\\|PM\\)\\(-[0-9]?[0-9]\\(:?[0-9][0-9]\\)?\\(am\\|pm\\|AM\\|PM\\)\\)?"
1852 . font-lock-function-name-face)))
1853 "Forms to highlight in diary-mode")
a1506d29 1854
86432f81 1855
e4ca7ef9 1856(provide 'diary-lib)
0808d911 1857
ab5796a9 1858;;; arch-tag: 22dd506e-2e33-410d-9ae1-095a0c1b2010
e4ca7ef9 1859;;; diary-lib.el ends here