Update FSF's address.
[bpt/emacs.git] / lisp / calendar / diary-lib.el
CommitLineData
e4ca7ef9 1;;; diary-lib.el --- diary functions.
0808d911 2
b578f267
EN
3;; Copyright (C) 1989, 1990, 1992, 1993, 1994, 1995 Free Software
4;; 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
41;;;###autoload
42(defun diary (&optional arg)
43 "Generate the diary window for ARG days starting with the current date.
44If no argument is provided, the number of days of diary entries is governed
45by the variable `number-of-diary-entries'. This function is suitable for
46execution in a `.emacs' file."
47 (interactive "P")
48 (let ((d-file (substitute-in-file-name diary-file))
49 (date (calendar-current-date)))
50 (if (and d-file (file-exists-p d-file))
51 (if (file-readable-p d-file)
52 (list-diary-entries
53 date
54 (cond
55 (arg (prefix-numeric-value arg))
56 ((vectorp number-of-diary-entries)
57 (aref number-of-diary-entries (calendar-day-of-week date)))
58 (t number-of-diary-entries)))
59 (error "Your diary file is not readable!"))
60 (error "You don't have a diary file!"))))
61
62(defun view-diary-entries (arg)
63 "Prepare and display a buffer with diary entries.
64Searches the file named in `diary-file' for entries that
65match ARG days starting with the date indicated by the cursor position
66in the displayed three-month calendar."
67 (interactive "p")
68 (let ((d-file (substitute-in-file-name diary-file)))
69 (if (and d-file (file-exists-p d-file))
70 (if (file-readable-p d-file)
71 (list-diary-entries (calendar-cursor-to-date t) arg)
72 (error "Diary file is not readable!"))
73 (error "You don't have a diary file!"))))
74
75(defun view-other-diary-entries (arg diary-file)
76 "Prepare and display buffer of diary entries from an alternative diary file.
77Prompts for a file name and searches that file for entries that match ARG
78days starting with the date indicated by the cursor position in the displayed
79three-month calendar."
80 (interactive
81 (list (cond ((null current-prefix-arg) 1)
82 ((listp current-prefix-arg) (car current-prefix-arg))
83 (t current-prefix-arg))
84 (setq diary-file (read-file-name "Enter diary file name: "
85 default-directory nil t))))
86 (view-diary-entries arg))
87
88(autoload 'check-calendar-holidays "holidays"
89 "Check the list of holidays for any that occur on DATE.
90The value returned is a list of strings of relevant holiday descriptions.
91The holidays are those in the list `calendar-holidays'."
92 t)
93
94(autoload 'calendar-holiday-list "holidays"
95 "Form the list of holidays that occur on dates in the calendar window.
96The holidays are those in the list `calendar-holidays'."
97 t)
98
99(autoload 'diary-french-date "cal-french"
100 "French calendar equivalent of date diary entry."
101 t)
102
103(autoload 'diary-mayan-date "cal-mayan"
104 "Mayan calendar equivalent of date diary entry."
105 t)
106
c36baedb
ER
107(autoload 'diary-iso-date "cal-iso"
108 "ISO calendar equivalent of date diary entry."
109 t)
110
0808d911
ER
111(autoload 'diary-julian-date "cal-julian"
112 "Julian calendar equivalent of date diary entry."
113 t)
114
115(autoload 'diary-astro-day-number "cal-julian"
116 "Astronomical (Julian) day number diary entry."
117 t)
118
119(autoload 'diary-chinese-date "cal-chinese"
120 "Chinese calendar equivalent of date diary entry."
121 t)
122
123(autoload 'diary-islamic-date "cal-islamic"
124 "Islamic calendar equivalent of date diary entry."
125 t)
126
127(autoload 'list-islamic-diary-entries "cal-islamic"
128 "Add any Islamic date entries from the diary file to `diary-entries-list'."
129 t)
130
131(autoload 'mark-islamic-diary-entries "cal-islamic"
132 "Mark days in the calendar window that have Islamic date diary entries."
133 t)
134
135(autoload 'mark-islamic-calendar-date-pattern "cal-islamic"
136 "Mark dates in calendar window that conform to Islamic date MONTH/DAY/YEAR."
137 t)
138
139(autoload 'diary-hebrew-date "cal-hebrew"
140 "Hebrew calendar equivalent of date diary entry."
141 t)
142
143(autoload 'diary-omer "cal-hebrew"
144 "Omer count diary entry."
145 t)
146
147(autoload 'diary-yahrzeit "cal-hebrew"
148 "Yahrzeit diary entry--entry applies if date is yahrzeit or the day before."
149 t)
150
151(autoload 'diary-parasha "cal-hebrew"
152 "Parasha diary entry--entry applies if date is a Saturday."
153 t)
154
155(autoload 'diary-rosh-hodesh "cal-hebrew"
156 "Rosh Hodesh diary entry."
157 t)
158
159(autoload 'list-hebrew-diary-entries "cal-hebrew"
160 "Add any Hebrew date entries from the diary file to `diary-entries-list'."
161 t)
162
163(autoload 'mark-hebrew-diary-entries "cal-hebrew"
164 "Mark days in the calendar window that have Hebrew date diary entries."
165 t)
166
167(autoload 'mark-hebrew-calendar-date-pattern "cal-hebrew"
168 "Mark dates in calendar window that conform to Hebrew date MONTH/DAY/YEAR."
169 t)
170
171(autoload 'diary-coptic-date "cal-coptic"
172 "Coptic calendar equivalent of date diary entry."
173 t)
174
175(autoload 'diary-ethiopic-date "cal-coptic"
176 "Ethiopic calendar equivalent of date diary entry."
177 t)
178
179(autoload 'diary-phases-of-moon "lunar" "Moon phases diary entry." t)
180
181(autoload 'diary-sunrise-sunset "solar"
182 "Local time of sunrise and sunset as a diary entry."
183 t)
184
185(autoload 'diary-sabbath-candles "solar"
186 "Local time of candle lighting diary entry--applies if date is a Friday.
187No diary entry if there is no sunset on that date."
188 t)
189
190(defvar diary-syntax-table (copy-syntax-table (standard-syntax-table))
191 "The syntax table used when parsing dates in the diary file.
192It is the standard syntax table used in Fundamental mode, but with the
193syntax of `*' changed to be a word constituent.")
194
195(modify-syntax-entry ?* "w" diary-syntax-table)
196
197(defun list-diary-entries (date number)
198 "Create and display a buffer containing the relevant lines in diary-file.
199The arguments are DATE and NUMBER; the entries selected are those
200for NUMBER days starting with date DATE. The other entries are hidden
201using selective display.
202
203Returns a list of all relevant diary entries found, if any, in order by date.
204The list entries have the form ((month day year) string). If the variable
205`diary-list-include-blanks' is t, this list includes a dummy diary entry
206\(consisting of the empty string) for a date with no diary entries.
207
208After the list is prepared, the hooks `nongregorian-diary-listing-hook',
209`list-diary-entries-hook', `diary-display-hook', and `diary-hook' are run.
210These hooks have the following distinct roles:
211
212 `nongregorian-diary-listing-hook' can cull dates from the diary
213 and each included file. Usually used for Hebrew or Islamic
214 diary entries in files. Applied to *each* file.
215
216 `list-diary-entries-hook' adds or manipulates diary entries from
217 external sources. Used, for example, to include diary entries
218 from other files or to sort the diary entries. Invoked *once* only,
219 before the display hook is run.
220
221 `diary-display-hook' does the actual display of information. If this is
222 nil, simple-diary-display will be used. Use add-hook to set this to
223 fancy-diary-display, if desired. If you want no diary display, use
224 add-hook to set this to ignore.
225
226 `diary-hook' is run last. This can be used for an appointment
227 notification function."
228
229 (if (< 0 number)
230 (let* ((original-date date);; save for possible use in the hooks
231 (old-diary-syntax-table)
232 (diary-entries-list)
233 (date-string (calendar-date-string date))
234 (d-file (substitute-in-file-name diary-file)))
235 (message "Preparing diary...")
236 (save-excursion
cdfeb73f 237 (let ((diary-buffer (find-buffer-visiting d-file)))
0808d911
ER
238 (set-buffer (if diary-buffer
239 diary-buffer
240 (find-file-noselect d-file t))))
241 (setq selective-display t)
242 (setq selective-display-ellipses nil)
243 (setq old-diary-syntax-table (syntax-table))
244 (set-syntax-table diary-syntax-table)
245 (unwind-protect
246 (let ((buffer-read-only nil)
247 (diary-modified (buffer-modified-p))
248 (mark (regexp-quote diary-nonmarking-symbol)))
249 (goto-char (1- (point-max)))
250 (if (not (looking-at "\^M\\|\n"))
251 (progn
252 (forward-char 1)
253 (insert-string "\^M")))
254 (goto-char (point-min))
255 (if (not (looking-at "\^M\\|\n"))
256 (insert-string "\^M"))
257 (subst-char-in-region (point-min) (point-max) ?\n ?\^M t)
258 (calendar-for-loop i from 1 to number do
259 (let ((d diary-date-forms)
260 (month (extract-calendar-month date))
261 (day (extract-calendar-day date))
262 (year (extract-calendar-year date))
263 (entry-found (list-sexp-diary-entries date)))
264 (while d
265 (let*
266 ((date-form (if (equal (car (car d)) 'backup)
267 (cdr (car d))
268 (car d)))
269 (backup (equal (car (car d)) 'backup))
270 (dayname
271 (concat
272 (calendar-day-name date) "\\|"
273 (substring (calendar-day-name date) 0 3) ".?"))
274 (monthname
275 (concat
276 "\\*\\|"
277 (calendar-month-name month) "\\|"
278 (substring (calendar-month-name month) 0 3) ".?"))
279 (month (concat "\\*\\|0*" (int-to-string month)))
280 (day (concat "\\*\\|0*" (int-to-string day)))
281 (year
282 (concat
283 "\\*\\|0*" (int-to-string year)
284 (if abbreviated-calendar-year
285 (concat "\\|" (int-to-string (% year 100)))
286 "")))
287 (regexp
288 (concat
289 "\\(\\`\\|\^M\\|\n\\)" mark "?\\("
290 (mapconcat 'eval date-form "\\)\\(")
291 "\\)"))
292 (case-fold-search t))
293 (goto-char (point-min))
294 (while (re-search-forward regexp nil t)
295 (if backup (re-search-backward "\\<" nil t))
296 (if (and (or (char-equal (preceding-char) ?\^M)
297 (char-equal (preceding-char) ?\n))
298 (not (looking-at " \\|\^I")))
299 ;; Diary entry that consists only of date.
300 (backward-char 1)
301 ;; Found a nonempty diary entry--make it visible and
302 ;; add it to the list.
303 (setq entry-found t)
304 (let ((entry-start (point))
305 (date-start))
306 (re-search-backward "\^M\\|\n\\|\\`")
307 (setq date-start (point))
308 (re-search-forward "\^M\\|\n" nil t 2)
309 (while (looking-at " \\|\^I")
310 (re-search-forward "\^M\\|\n" nil t))
311 (backward-char 1)
312 (subst-char-in-region date-start
313 (point) ?\^M ?\n t)
314 (add-to-diary-list
e6c3bfbc
ER
315 date
316 (buffer-substring-no-properties
317 entry-start (point)))))))
0808d911
ER
318 (setq d (cdr d)))
319 (or entry-found
320 (not diary-list-include-blanks)
321 (setq diary-entries-list
322 (append diary-entries-list
323 (list (list date "")))))
324 (setq date
325 (calendar-gregorian-from-absolute
326 (1+ (calendar-absolute-from-gregorian date))))
327 (setq entry-found nil)))
328 (set-buffer-modified-p diary-modified))
329 (set-syntax-table old-diary-syntax-table))
330 (goto-char (point-min))
331 (run-hooks 'nongregorian-diary-listing-hook
332 'list-diary-entries-hook)
333 (if diary-display-hook
334 (run-hooks 'diary-display-hook)
335 (simple-diary-display))
336 (run-hooks 'diary-hook)
337 diary-entries-list))))
338
339(defun include-other-diary-files ()
340 "Include the diary entries from other diary files with those of diary-file.
341This function is suitable for use in `list-diary-entries-hook';
342it enables you to use shared diary files together with your own.
343The files included are specified in the diaryfile by lines of this form:
344 #include \"filename\"
345This is recursive; that is, #include directives in diary files thus included
346are obeyed. You can change the `#include' to some other string by
347changing the variable `diary-include-string'."
348 (goto-char (point-min))
349 (while (re-search-forward
350 (concat
351 "\\(\\`\\|\^M\\|\n\\)"
352 (regexp-quote diary-include-string)
353 " \"\\([^\"]*\\)\"")
354 nil t)
355 (let ((diary-file (substitute-in-file-name
e6c3bfbc
ER
356 (buffer-substring-no-properties
357 (match-beginning 2) (match-end 2))))
0808d911
ER
358 (diary-list-include-blanks nil)
359 (list-diary-entries-hook 'include-other-diary-files)
360 (diary-display-hook 'ignore)
361 (diary-hook nil))
362 (if (file-exists-p diary-file)
363 (if (file-readable-p diary-file)
364 (unwind-protect
365 (setq diary-entries-list
366 (append diary-entries-list
367 (list-diary-entries original-date number)))
cdfeb73f 368 (kill-buffer (find-buffer-visiting diary-file)))
0808d911
ER
369 (beep)
370 (message "Can't read included diary file %s" diary-file)
371 (sleep-for 2))
372 (beep)
373 (message "Can't find included diary file %s" diary-file)
374 (sleep-for 2))))
375 (goto-char (point-min)))
376
377(defun simple-diary-display ()
378 "Display the diary buffer if there are any relevant entries or holidays."
379 (let* ((holiday-list (if holidays-in-diary-buffer
380 (check-calendar-holidays original-date)))
381 (msg (format "No diary entries for %s %s"
382 (concat date-string (if holiday-list ":" ""))
383 (mapconcat 'identity holiday-list "; "))))
384 (if (or (not diary-entries-list)
385 (and (not (cdr diary-entries-list))
386 (string-equal (car (cdr (car diary-entries-list))) "")))
387 (if (<= (length msg) (frame-width))
388 (message msg)
389 (set-buffer (get-buffer-create holiday-buffer))
390 (setq buffer-read-only nil)
391 (calendar-set-mode-line date-string)
392 (erase-buffer)
393 (insert (mapconcat 'identity holiday-list "\n"))
394 (goto-char (point-min))
395 (set-buffer-modified-p nil)
396 (setq buffer-read-only t)
397 (display-buffer holiday-buffer)
398 (message "No diary entries for %s" date-string))
399 (calendar-set-mode-line
400 (concat "Diary for " date-string
401 (if holiday-list ": " "")
402 (mapconcat 'identity holiday-list "; ")))
cdfeb73f 403 (display-buffer (find-buffer-visiting d-file))
0808d911
ER
404 (message "Preparing diary...done"))))
405
406(defun fancy-diary-display ()
407 "Prepare a diary buffer with relevant entries in a fancy, noneditable form.
408This function is provided for optional use as the `diary-display-hook'."
409 (save-excursion;; Turn off selective-display in the diary file's buffer.
cdfeb73f 410 (set-buffer (find-buffer-visiting (substitute-in-file-name diary-file)))
0808d911
ER
411 (let ((diary-modified (buffer-modified-p)))
412 (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)
413 (setq selective-display nil)
414 (kill-local-variable 'mode-line-format)
415 (set-buffer-modified-p diary-modified)))
416 (if (or (not diary-entries-list)
417 (and (not (cdr diary-entries-list))
418 (string-equal (car (cdr (car diary-entries-list))) "")))
419 (let* ((holiday-list (if holidays-in-diary-buffer
420 (check-calendar-holidays original-date)))
421 (msg (format "No diary entries for %s %s"
422 (concat date-string (if holiday-list ":" ""))
423 (mapconcat 'identity holiday-list "; "))))
424 (if (<= (length msg) (frame-width))
425 (message msg)
426 (set-buffer (get-buffer-create holiday-buffer))
427 (setq buffer-read-only nil)
428 (calendar-set-mode-line date-string)
429 (erase-buffer)
430 (insert (mapconcat 'identity holiday-list "\n"))
431 (goto-char (point-min))
432 (set-buffer-modified-p nil)
433 (setq buffer-read-only t)
434 (display-buffer holiday-buffer)
435 (message "No diary entries for %s" date-string)))
436 (save-excursion;; Prepare the fancy diary buffer.
437 (set-buffer (make-fancy-diary-buffer))
438 (setq buffer-read-only nil)
439 (let ((entry-list diary-entries-list)
440 (holiday-list)
441 (holiday-list-last-month 1)
442 (holiday-list-last-year 1)
443 (date (list 0 0 0)))
444 (while entry-list
445 (if (not (calendar-date-equal date (car (car entry-list))))
446 (progn
447 (setq date (car (car entry-list)))
448 (and holidays-in-diary-buffer
449 (calendar-date-compare
450 (list (list holiday-list-last-month
451 (calendar-last-day-of-month
452 holiday-list-last-month
453 holiday-list-last-year)
454 holiday-list-last-year))
455 (list date))
456 ;; We need to get the holidays for the next 3 months.
457 (setq holiday-list-last-month
458 (extract-calendar-month date))
459 (setq holiday-list-last-year
460 (extract-calendar-year date))
461 (increment-calendar-month
462 holiday-list-last-month holiday-list-last-year 1)
463 (setq holiday-list
464 (let ((displayed-month holiday-list-last-month)
465 (displayed-year holiday-list-last-year))
466 (calendar-holiday-list)))
467 (increment-calendar-month
468 holiday-list-last-month holiday-list-last-year 1))
469 (let* ((date-string (calendar-date-string date))
470 (date-holiday-list
471 (let ((h holiday-list)
472 (d))
473 ;; Make a list of all holidays for date.
474 (while h
475 (if (calendar-date-equal date (car (car h)))
476 (setq d (append d (cdr (car h)))))
477 (setq h (cdr h)))
478 d)))
479 (insert (if (= (point) (point-min)) "" ?\n) date-string)
480 (if date-holiday-list (insert ": "))
481 (let ((l (current-column)))
482 (insert (mapconcat 'identity date-holiday-list
483 (concat "\n" (make-string l ? )))))
484 (let ((l (current-column)))
485 (insert ?\n (make-string l ?=) ?\n)))))
486 (if (< 0 (length (car (cdr (car entry-list)))))
487 (insert (car (cdr (car entry-list))) ?\n))
488 (setq entry-list (cdr entry-list))))
489 (set-buffer-modified-p nil)
490 (goto-char (point-min))
491 (setq buffer-read-only t)
492 (display-buffer fancy-diary-buffer)
493 (message "Preparing diary...done"))))
494
495(defun make-fancy-diary-buffer ()
496 "Create and return the initial fancy diary buffer."
497 (save-excursion
498 (set-buffer (get-buffer-create fancy-diary-buffer))
499 (setq buffer-read-only nil)
500 (make-local-variable 'mode-line-format)
501 (calendar-set-mode-line "Diary Entries")
502 (erase-buffer)
503 (set-buffer-modified-p nil)
504 (setq buffer-read-only t)
505 (get-buffer fancy-diary-buffer)))
506
507(defun print-diary-entries ()
508 "Print a hard copy of the diary display.
509
510If the simple diary display is being used, prepare a temp buffer with the
511visible lines of the diary buffer, add a heading line composed from the mode
512line, print the temp buffer, and destroy it.
513
514If the fancy diary display is being used, just print the buffer.
515
516The hooks given by the variable `print-diary-entries-hook' are called to do
517the actual printing."
518 (interactive)
519 (if (bufferp (get-buffer fancy-diary-buffer))
520 (save-excursion
521 (set-buffer (get-buffer fancy-diary-buffer))
522 (run-hooks 'print-diary-entries-hook))
523 (let ((diary-buffer
cdfeb73f 524 (find-buffer-visiting (substitute-in-file-name diary-file))))
0808d911
ER
525 (if diary-buffer
526 (let ((temp-buffer (get-buffer-create "*Printable Diary Entries*"))
527 (heading))
528 (save-excursion
529 (set-buffer diary-buffer)
530 (setq heading
531 (if (not (stringp mode-line-format))
532 "All Diary Entries"
533 (string-match "^-*\\([^-].*[^-]\\)-*$" mode-line-format)
534 (substring mode-line-format
535 (match-beginning 1) (match-end 1))))
536 (copy-to-buffer temp-buffer (point-min) (point-max))
537 (set-buffer temp-buffer)
538 (while (re-search-forward "\^M.*$" nil t)
539 (replace-match ""))
540 (goto-char (point-min))
541 (insert heading "\n"
542 (make-string (length heading) ?=) "\n")
543 (run-hooks 'print-diary-entries-hook)
544 (kill-buffer temp-buffer)))
545 (error "You don't have a diary buffer!")))))
546
547(defun show-all-diary-entries ()
548 "Show all of the diary entries in the diary file.
549This function gets rid of the selective display of the diary file so that
550all entries, not just some, are visible. If there is no diary buffer, one
551is created."
552 (interactive)
553 (let ((d-file (substitute-in-file-name diary-file)))
554 (if (and d-file (file-exists-p d-file))
555 (if (file-readable-p d-file)
556 (save-excursion
cdfeb73f 557 (let ((diary-buffer (find-buffer-visiting d-file)))
0808d911
ER
558 (set-buffer (if diary-buffer
559 diary-buffer
560 (find-file-noselect d-file t)))
561 (let ((buffer-read-only nil)
562 (diary-modified (buffer-modified-p)))
563 (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)
564 (setq selective-display nil)
565 (make-local-variable 'mode-line-format)
566 (setq mode-line-format default-mode-line-format)
567 (display-buffer (current-buffer))
568 (set-buffer-modified-p diary-modified))))
569 (error "Your diary file is not readable!"))
570 (error "You don't have a diary file!"))))
571
572(defun diary-name-pattern (string-array &optional fullname)
573 "Convert an STRING-ARRAY, an array of strings to a pattern.
574The pattern will match any of the strings, either entirely or abbreviated
575to three characters. An abbreviated form will match with or without a period;
576If the optional FULLNAME is t, abbreviations will not match, just the full
577name."
578 (let ((pattern ""))
579 (calendar-for-loop i from 0 to (1- (length string-array)) do
580 (setq pattern
581 (concat
582 pattern
583 (if (string-equal pattern "") "" "\\|")
584 (aref string-array i)
585 (if fullname
586 ""
587 (concat
588 "\\|"
589 (substring (aref string-array i) 0 3) ".?")))))
590 pattern))
591
592(defvar marking-diary-entries nil
593 "True during the marking of diary entries, nil otherwise.")
594
595(defvar marking-diary-entry nil
596 "True during the marking of diary entries, if current entry is marking.")
597
598(defun mark-diary-entries ()
599 "Mark days in the calendar window that have diary entries.
600Each entry in the diary file visible in the calendar window is marked.
601After the entries are marked, the hooks `nongregorian-diary-marking-hook' and
602`mark-diary-entries-hook' are run."
603 (interactive)
604 (setq mark-diary-entries-in-calendar t)
605 (let ((d-file (substitute-in-file-name diary-file))
606 (marking-diary-entries t))
607 (if (and d-file (file-exists-p d-file))
608 (if (file-readable-p d-file)
609 (save-excursion
610 (message "Marking diary entries...")
611 (set-buffer (find-file-noselect d-file t))
612 (let ((d diary-date-forms)
613 (old-diary-syntax-table))
614 (setq old-diary-syntax-table (syntax-table))
615 (set-syntax-table diary-syntax-table)
616 (while d
617 (let*
618 ((date-form (if (equal (car (car d)) 'backup)
619 (cdr (car d))
620 (car d)));; ignore 'backup directive
621 (dayname (diary-name-pattern calendar-day-name-array))
622 (monthname
623 (concat
624 (diary-name-pattern calendar-month-name-array)
625 "\\|\\*"))
626 (month "[0-9]+\\|\\*")
627 (day "[0-9]+\\|\\*")
628 (year "[0-9]+\\|\\*")
629 (l (length date-form))
630 (d-name-pos (- l (length (memq 'dayname date-form))))
631 (d-name-pos (if (/= l d-name-pos) (+ 2 d-name-pos)))
632 (m-name-pos (- l (length (memq 'monthname date-form))))
633 (m-name-pos (if (/= l m-name-pos) (+ 2 m-name-pos)))
634 (d-pos (- l (length (memq 'day date-form))))
635 (d-pos (if (/= l d-pos) (+ 2 d-pos)))
636 (m-pos (- l (length (memq 'month date-form))))
637 (m-pos (if (/= l m-pos) (+ 2 m-pos)))
638 (y-pos (- l (length (memq 'year date-form))))
639 (y-pos (if (/= l y-pos) (+ 2 y-pos)))
640 (regexp
641 (concat
642 "\\(\\`\\|\^M\\|\n\\)\\("
643 (mapconcat 'eval date-form "\\)\\(")
644 "\\)"))
645 (case-fold-search t))
646 (goto-char (point-min))
647 (while (re-search-forward regexp nil t)
648 (let* ((dd-name
649 (if d-name-pos
e6c3bfbc 650 (buffer-substring-no-properties
0808d911
ER
651 (match-beginning d-name-pos)
652 (match-end d-name-pos))))
653 (mm-name
654 (if m-name-pos
e6c3bfbc 655 (buffer-substring-no-properties
0808d911
ER
656 (match-beginning m-name-pos)
657 (match-end m-name-pos))))
658 (mm (string-to-int
659 (if m-pos
e6c3bfbc 660 (buffer-substring-no-properties
0808d911
ER
661 (match-beginning m-pos)
662 (match-end m-pos))
663 "")))
664 (dd (string-to-int
665 (if d-pos
e6c3bfbc 666 (buffer-substring-no-properties
0808d911
ER
667 (match-beginning d-pos)
668 (match-end d-pos))
669 "")))
670 (y-str (if y-pos
e6c3bfbc 671 (buffer-substring-no-properties
0808d911
ER
672 (match-beginning y-pos)
673 (match-end y-pos))))
674 (yy (if (not y-str)
675 0
676 (if (and (= (length y-str) 2)
677 abbreviated-calendar-year)
678 (let* ((current-y
679 (extract-calendar-year
680 (calendar-current-date)))
681 (y (+ (string-to-int y-str)
682 (* 100
683 (/ current-y 100)))))
684 (if (> (- y current-y) 50)
685 (- y 100)
686 (if (> (- current-y y) 50)
687 (+ y 100)
688 y)))
689 (string-to-int y-str)))))
690 (if dd-name
691 (mark-calendar-days-named
692 (cdr (assoc (capitalize (substring dd-name 0 3))
693 (calendar-make-alist
694 calendar-day-name-array
695 0
696 '(lambda (x) (substring x 0 3))))))
697 (if mm-name
698 (if (string-equal mm-name "*")
699 (setq mm 0)
700 (setq mm
701 (cdr (assoc
702 (capitalize
703 (substring mm-name 0 3))
704 (calendar-make-alist
705 calendar-month-name-array
706 1
707 '(lambda (x) (substring x 0 3)))
708 )))))
709 (mark-calendar-date-pattern mm dd yy))))
710 (setq d (cdr d))))
711 (mark-sexp-diary-entries)
712 (run-hooks 'nongregorian-diary-marking-hook
713 'mark-diary-entries-hook)
714 (set-syntax-table old-diary-syntax-table)
715 (message "Marking diary entries...done")))
716 (error "Your diary file is not readable!"))
717 (error "You don't have a diary file!"))))
718
719(defun mark-sexp-diary-entries ()
720 "Mark days in the calendar window that have sexp diary entries.
721Each entry in the diary file (or included files) visible in the calendar window
722is marked. See the documentation for the function `list-sexp-diary-entries'."
723 (let* ((sexp-mark (regexp-quote sexp-diary-entry-symbol))
724 (s-entry (concat "\\(\\`\\|\^M\\|\n\\)\\("
725 (regexp-quote sexp-mark) "(\\)\\|\\("
726 (regexp-quote diary-nonmarking-symbol)
727 (regexp-quote sexp-mark) "(diary-remind\\)"))
728 (m)
729 (y)
730 (first-date)
731 (last-date))
732 (save-excursion
733 (set-buffer calendar-buffer)
734 (setq m displayed-month)
735 (setq y displayed-year))
736 (increment-calendar-month m y -1)
737 (setq first-date
738 (calendar-absolute-from-gregorian (list m 1 y)))
739 (increment-calendar-month m y 2)
740 (setq last-date
741 (calendar-absolute-from-gregorian
742 (list m (calendar-last-day-of-month m y) y)))
743 (goto-char (point-min))
744 (while (re-search-forward s-entry nil t)
dbabfd55 745 (if (char-equal (preceding-char) ?\()
0808d911
ER
746 (setq marking-diary-entry t)
747 (setq marking-diary-entry nil))
748 (re-search-backward "(")
749 (let ((sexp-start (point))
750 (sexp)
751 (entry)
752 (entry-start)
753 (line-start))
754 (forward-sexp)
e6c3bfbc 755 (setq sexp (buffer-substring-no-properties sexp-start (point)))
0808d911
ER
756 (save-excursion
757 (re-search-backward "\^M\\|\n\\|\\`")
758 (setq line-start (point)))
759 (forward-char 1)
760 (if (and (or (char-equal (preceding-char) ?\^M)
761 (char-equal (preceding-char) ?\n))
762 (not (looking-at " \\|\^I")))
763 (progn;; Diary entry consists only of the sexp
764 (backward-char 1)
765 (setq entry ""))
766 (setq entry-start (point))
767 (re-search-forward "\^M\\|\n" nil t)
768 (while (looking-at " \\|\^I")
769 (re-search-forward "\^M\\|\n" nil t))
770 (backward-char 1)
e6c3bfbc 771 (setq entry (buffer-substring-no-properties entry-start (point)))
0808d911
ER
772 (while (string-match "[\^M]" entry)
773 (aset entry (match-beginning 0) ?\n )))
774 (calendar-for-loop date from first-date to last-date do
775 (if (diary-sexp-entry sexp entry
776 (calendar-gregorian-from-absolute date))
777 (mark-visible-calendar-date
778 (calendar-gregorian-from-absolute date))))))))
779
780(defun mark-included-diary-files ()
781 "Mark the diary entries from other diary files with those of the diary file.
782This function is suitable for use as the `mark-diary-entries-hook'; it enables
783you to use shared diary files together with your own. The files included are
784specified in the diary-file by lines of this form:
785 #include \"filename\"
786This is recursive; that is, #include directives in diary files thus included
787are obeyed. You can change the `#include' to some other string by
788changing the variable `diary-include-string'."
789 (goto-char (point-min))
790 (while (re-search-forward
791 (concat
792 "\\(\\`\\|\^M\\|\n\\)"
793 (regexp-quote diary-include-string)
794 " \"\\([^\"]*\\)\"")
795 nil t)
796 (let ((diary-file (substitute-in-file-name
e6c3bfbc
ER
797 (buffer-substring-no-properties
798 (match-beginning 2) (match-end 2))))
0808d911
ER
799 (mark-diary-entries-hook 'mark-included-diary-files))
800 (if (file-exists-p diary-file)
801 (if (file-readable-p diary-file)
802 (progn
803 (mark-diary-entries)
cdfeb73f 804 (kill-buffer (find-buffer-visiting diary-file)))
0808d911
ER
805 (beep)
806 (message "Can't read included diary file %s" diary-file)
807 (sleep-for 2))
808 (beep)
809 (message "Can't find included diary file %s" diary-file)
810 (sleep-for 2))))
811 (goto-char (point-min)))
812
813(defun mark-calendar-days-named (dayname)
814 "Mark all dates in the calendar window that are day DAYNAME of the week.
8150 means all Sundays, 1 means all Mondays, and so on."
816 (save-excursion
817 (set-buffer calendar-buffer)
818 (let ((prev-month displayed-month)
819 (prev-year displayed-year)
820 (succ-month displayed-month)
821 (succ-year displayed-year)
822 (last-day)
823 (day))
824 (increment-calendar-month succ-month succ-year 1)
825 (increment-calendar-month prev-month prev-year -1)
826 (setq day (calendar-absolute-from-gregorian
827 (calendar-nth-named-day 1 dayname prev-month prev-year)))
828 (setq last-day (calendar-absolute-from-gregorian
829 (calendar-nth-named-day -1 dayname succ-month succ-year)))
830 (while (<= day last-day)
831 (mark-visible-calendar-date (calendar-gregorian-from-absolute day))
832 (setq day (+ day 7))))))
833
834(defun mark-calendar-date-pattern (month day year)
835 "Mark all dates in the calendar window that conform to MONTH/DAY/YEAR.
836A value of 0 in any position is a wildcard."
837 (save-excursion
838 (set-buffer calendar-buffer)
839 (let ((m displayed-month)
840 (y displayed-year))
841 (increment-calendar-month m y -1)
842 (calendar-for-loop i from 0 to 2 do
843 (mark-calendar-month m y month day year)
844 (increment-calendar-month m y 1)))))
845
846(defun mark-calendar-month (month year p-month p-day p-year)
847 "Mark dates in the MONTH/YEAR that conform to pattern P-MONTH/P_DAY/P-YEAR.
848A value of 0 in any position of the pattern is a wildcard."
849 (if (or (and (= month p-month)
850 (or (= p-year 0) (= year p-year)))
851 (and (= p-month 0)
852 (or (= p-year 0) (= year p-year))))
853 (if (= p-day 0)
854 (calendar-for-loop
855 i from 1 to (calendar-last-day-of-month month year) do
856 (mark-visible-calendar-date (list month i year)))
857 (mark-visible-calendar-date (list month p-day year)))))
858
859(defun sort-diary-entries ()
860 "Sort the list of diary entries by time of day."
861 (setq diary-entries-list (sort diary-entries-list 'diary-entry-compare)))
862
863(defun diary-entry-compare (e1 e2)
864 "Returns t if E1 is earlier than E2."
865 (or (calendar-date-compare e1 e2)
866 (and (calendar-date-equal (car e1) (car e2))
867 (< (diary-entry-time (car (cdr e1)))
868 (diary-entry-time (car (cdr e2)))))))
869
870(defun diary-entry-time (s)
871 "Time at the beginning of the string S in a military-style integer.
872For example, returns 1325 for 1:25pm. Returns -9999 if no time is recognized.
873The recognized forms are XXXX or X:XX or XX:XX (military time), XXam or XXpm,
874and XX:XXam or XX:XXpm."
875 (cond ((string-match;; Military time
876 "^[ \t]*\\([0-9]?[0-9]\\):?\\([0-9][0-9]\\)\\(\\>\\|[^ap]\\)" s)
877 (+ (* 100 (string-to-int
878 (substring s (match-beginning 1) (match-end 1))))
879 (string-to-int (substring s (match-beginning 2) (match-end 2)))))
880 ((string-match;; Hour only XXam or XXpm
881 "^[ \t]*\\([0-9]?[0-9]\\)\\([ap]\\)m\\>" s)
882 (+ (* 100 (% (string-to-int
883 (substring s (match-beginning 1) (match-end 1)))
884 12))
885 (if (string-equal "a"
886 (substring s (match-beginning 2) (match-end 2)))
887 0 1200)))
888 ((string-match;; Hour and minute XX:XXam or XX:XXpm
889 "^[ \t]*\\([0-9]?[0-9]\\):\\([0-9][0-9]\\)\\([ap]\\)m\\>" s)
890 (+ (* 100 (% (string-to-int
891 (substring s (match-beginning 1) (match-end 1)))
892 12))
893 (string-to-int (substring s (match-beginning 2) (match-end 2)))
894 (if (string-equal "a"
895 (substring s (match-beginning 3) (match-end 3)))
896 0 1200)))
897 (t -9999)));; Unrecognizable
898
899(defun list-sexp-diary-entries (date)
900 "Add sexp entries for DATE from the diary file to `diary-entries-list'.
901Also, Make them visible in the diary file. Returns t if any entries were
902found.
903
904Sexp diary entries must be prefaced by a `sexp-diary-entry-symbol' (normally
905`%%'). The form of a sexp diary entry is
906
907 %%(SEXP) ENTRY
908
909Both ENTRY and DATE are globally available when the SEXP is evaluated. If the
910SEXP yields the value nil, the diary entry does not apply. If it yields a
911non-nil value, ENTRY will be taken to apply to DATE; if the non-nil value is a
912string, that string will be the diary entry in the fancy diary display.
913
914For example, the following diary entry will apply to the 21st of the month
915if it is a weekday and the Friday before if the 21st is on a weekend:
916
917 &%%(let ((dayname (calendar-day-of-week date))
918 (day (extract-calendar-day date)))
919 (or
920 (and (= day 21) (memq dayname '(1 2 3 4 5)))
921 (and (memq day '(19 20)) (= dayname 5)))
922 ) UIUC pay checks deposited
923
924A number of built-in functions are available for this type of diary entry:
925
926 %%(diary-date MONTH DAY YEAR) text
927 Entry applies if date is MONTH, DAY, YEAR if
928 `european-calendar-style' is nil, and DAY, MONTH, YEAR if
929 `european-calendar-style' is t. DAY, MONTH, and YEAR
930 can be lists of integers, the constant t, or an integer.
931 The constant t means all values.
932
933 %%(diary-float MONTH DAYNAME N) text
934 Entry will appear on the Nth DAYNAME of MONTH.
935 (DAYNAME=0 means Sunday, 1 means Monday, and so on;
936 if N is negative it counts backward from the end of
937 the month. MONTH can be a list of months, a single
938 month, or t to specify all months.
939
940 %%(diary-block M1 D1 Y1 M2 D2 Y2) text
941 Entry will appear on dates between M1/D1/Y1 and M2/D2/Y2,
942 inclusive. (If `european-calendar-style' is t, the
943 order of the parameters should be changed to D1, M1, Y1,
944 D2, M2, Y2.)
945
946 %%(diary-anniversary MONTH DAY YEAR) text
947 Entry will appear on anniversary dates of MONTH DAY, YEAR.
948 (If `european-calendar-style' is t, the order of the
949 parameters should be changed to DAY, MONTH, YEAR.) Text
950 can contain %d or %d%s; %d will be replaced by the number
951 of years since the MONTH DAY, YEAR and %s will be replaced
952 by the ordinal ending of that number (that is, `st', `nd',
953 `rd' or `th', as appropriate. The anniversary of February
954 29 is considered to be March 1 in a non-leap year.
955
956 %%(diary-cyclic N MONTH DAY YEAR) text
957 Entry will appear every N days, starting MONTH DAY, YEAR.
958 (If `european-calendar-style' is t, the order of the
959 parameters should be changed to N, DAY, MONTH, YEAR.) Text
960 can contain %d or %d%s; %d will be replaced by the number
961 of repetitions since the MONTH DAY, YEAR and %s will
962 be replaced by the ordinal ending of that number (that is,
963 `st', `nd', `rd' or `th', as appropriate.
964
965 %%(diary-remind SEXP DAYS &optional MARKING) text
966 Entry is a reminder for diary sexp SEXP. DAYS is either a
967 single number or a list of numbers indicating the number(s)
968 of days before the event that the warning(s) should occur.
969 If the current date is (one of) DAYS before the event
970 indicated by EXPR, then a suitable message (as specified
971 by `diary-remind-message') appears. In addition to the
972 reminders beforehand, the diary entry also appears on
973 the date itself. If optional MARKING is non-nil then the
974 *reminders* are marked on the calendar. Marking of
975 reminders is independent of whether the entry *itself* is
976 a marking or nonmarking one.
977
978 %%(diary-day-of-year)
979 Diary entries giving the day of the year and the number of
980 days remaining in the year will be made every day. Note
981 that since there is no text, it makes sense only if the
982 fancy diary display is used.
983
984 %%(diary-iso-date)
985 Diary entries giving the corresponding ISO commercial date
986 will be made every day. Note that since there is no text,
987 it makes sense only if the fancy diary display is used.
988
989 %%(diary-french-date)
990 Diary entries giving the corresponding French Revolutionary
991 date will be made every day. Note that since there is no
992 text, it makes sense only if the fancy diary display is used.
993
994 %%(diary-islamic-date)
995 Diary entries giving the corresponding Islamic date will be
996 made every day. Note that since there is no text, it
997 makes sense only if the fancy diary display is used.
998
999 %%(diary-hebrew-date)
1000 Diary entries giving the corresponding Hebrew date will be
1001 made every day. Note that since there is no text, it
1002 makes sense only if the fancy diary display is used.
1003
1004 %%(diary-astro-day-number) Diary entries giving the corresponding
1005 astronomical (Julian) day number will be made every day.
1006 Note that since there is no text, it makes sense only if the
1007 fancy diary display is used.
1008
1009 %%(diary-julian-date) Diary entries giving the corresponding
1010 Julian date will be made every day. Note that since
1011 there is no text, it makes sense only if the fancy diary
1012 display is used.
1013
1014 %%(diary-sunrise-sunset)
1015 Diary entries giving the local times of sunrise and sunset
1016 will be made every day. Note that since there is no text,
1017 it makes sense only if the fancy diary display is used.
1018 Floating point required.
1019
1020 %%(diary-phases-of-moon)
1021 Diary entries giving the times of the phases of the moon
1022 will be when appropriate. Note that since there is no text,
1023 it makes sense only if the fancy diary display is used.
1024 Floating point required.
1025
1026 %%(diary-yahrzeit MONTH DAY YEAR) text
1027 Text is assumed to be the name of the person; the date is
1028 the date of death on the *civil* calendar. The diary entry
1029 will appear on the proper Hebrew-date anniversary and on the
1030 day before. (If `european-calendar-style' is t, the order
1031 of the parameters should be changed to DAY, MONTH, YEAR.)
1032
1033 %%(diary-rosh-hodesh)
1034 Diary entries will be made on the dates of Rosh Hodesh on
1035 the Hebrew calendar. Note that since there is no text, it
1036 makes sense only if the fancy diary display is used.
1037
1038 %%(diary-parasha)
1039 Diary entries giving the weekly parasha will be made on
1040 every Saturday. Note that since there is no text, it
1041 makes sense only if the fancy diary display is used.
1042
1043 %%(diary-omer)
1044 Diary entries giving the omer count will be made every day
59ba214d 1045 from Passover to Shavuot. Note that since there is no text,
0808d911
ER
1046 it makes sense only if the fancy diary display is used.
1047
1048Marking these entries is *extremely* time consuming, so these entries are
1049best if they are nonmarking."
1050 (let* ((mark (regexp-quote diary-nonmarking-symbol))
1051 (sexp-mark (regexp-quote sexp-diary-entry-symbol))
1052 (s-entry (concat "\\(\\`\\|\^M\\|\n\\)" mark "?" sexp-mark "("))
1053 (entry-found))
1054 (goto-char (point-min))
1055 (while (re-search-forward s-entry nil t)
1056 (backward-char 1)
1057 (let ((sexp-start (point))
1058 (sexp)
1059 (entry)
1060 (entry-start)
1061 (line-start))
1062 (forward-sexp)
e6c3bfbc 1063 (setq sexp (buffer-substring-no-properties sexp-start (point)))
0808d911
ER
1064 (save-excursion
1065 (re-search-backward "\^M\\|\n\\|\\`")
1066 (setq line-start (point)))
1067 (forward-char 1)
1068 (if (and (or (char-equal (preceding-char) ?\^M)
1069 (char-equal (preceding-char) ?\n))
1070 (not (looking-at " \\|\^I")))
1071 (progn;; Diary entry consists only of the sexp
1072 (backward-char 1)
1073 (setq entry ""))
1074 (setq entry-start (point))
1075 (re-search-forward "\^M\\|\n" nil t)
1076 (while (looking-at " \\|\^I")
1077 (re-search-forward "\^M\\|\n" nil t))
1078 (backward-char 1)
e6c3bfbc 1079 (setq entry (buffer-substring-no-properties entry-start (point)))
0808d911
ER
1080 (while (string-match "[\^M]" entry)
1081 (aset entry (match-beginning 0) ?\n )))
1082 (let ((diary-entry (diary-sexp-entry sexp entry date)))
1083 (if diary-entry
1084 (subst-char-in-region line-start (point) ?\^M ?\n t))
1085 (add-to-diary-list date diary-entry)
1086 (setq entry-found (or entry-found diary-entry)))))
1087 entry-found))
1088
1089(defun diary-sexp-entry (sexp entry date)
1090 "Process a SEXP diary ENTRY for DATE."
1091 (let ((result (if calendar-debug-sexp
1092 (let ((stack-trace-on-error t))
1093 (eval (car (read-from-string sexp))))
1094 (condition-case nil
1095 (eval (car (read-from-string sexp)))
1096 (error
1097 (beep)
1098 (message "Bad sexp at line %d in %s: %s"
1099 (save-excursion
1100 (save-restriction
1101 (narrow-to-region 1 (point))
1102 (goto-char (point-min))
1103 (let ((lines 1))
1104 (while (re-search-forward "\n\\|\^M" nil t)
1105 (setq lines (1+ lines)))
1106 lines)))
1107 diary-file sexp)
1108 (sleep-for 2))))))
1109 (if (stringp result)
1110 result
1111 (if result
1112 entry
1113 nil))))
1114
1115(defun diary-date (month day year)
1116 "Specific date(s) diary entry.
1117Entry applies if date is MONTH, DAY, YEAR if `european-calendar-style' is nil,
1118and DAY, MONTH, YEAR if `european-calendar-style' is t. DAY, MONTH, and YEAR
1119can be lists of integers, the constant t, or an integer. The constant t means
1120all values."
1121 (let* ((dd (if european-calendar-style
1122 month
1123 day))
1124 (mm (if european-calendar-style
1125 day
1126 month))
1127 (m (extract-calendar-month date))
1128 (y (extract-calendar-year date))
1129 (d (extract-calendar-day date)))
1130 (if (and
1131 (or (and (listp dd) (memq d dd))
1132 (equal d dd)
1133 (eq dd t))
1134 (or (and (listp mm) (memq m mm))
1135 (equal m mm)
1136 (eq mm t))
1137 (or (and (listp year) (memq y year))
1138 (equal y year)
1139 (eq year t)))
1140 entry)))
1141
1142(defun diary-block (m1 d1 y1 m2 d2 y2)
1143 "Block diary entry.
1144Entry applies if date is between two dates. Order of the parameters is
1145M1, D1, Y1, M2, D2, Y2 `european-calendar-style' is nil, and
1146D1, M1, Y1, D2, M2, Y2 if `european-calendar-style' is t."
1147 (let ((date1 (calendar-absolute-from-gregorian
1148 (if european-calendar-style
1149 (list d1 m1 y1)
1150 (list m1 d1 y1))))
1151 (date2 (calendar-absolute-from-gregorian
1152 (if european-calendar-style
1153 (list d2 m2 y2)
1154 (list m2 d2 y2))))
1155 (d (calendar-absolute-from-gregorian date)))
1156 (if (and (<= date1 d) (<= d date2))
1157 entry)))
1158
1159(defun diary-float (month dayname n)
1160 "Floating diary entry--entry applies if date is the nth dayname of month.
1161Parameters are MONTH, DAYNAME, N. MONTH can be a list of months, the constant
1162t, or an integer. The constant t means all months. If N is negative, count
1163backward from the end of the month."
1164 (let ((m (extract-calendar-month date))
1165 (y (extract-calendar-year date)))
1166 (if (and
1167 (or (and (listp month) (memq m month))
1168 (equal m month)
1169 (eq month t))
1170 (calendar-date-equal date (calendar-nth-named-day n dayname m y)))
1171 entry)))
1172
1173(defun diary-anniversary (month day year)
1174 "Anniversary diary entry.
1175Entry applies if date is the anniversary of MONTH, DAY, YEAR if
1176`european-calendar-style' is nil, and DAY, MONTH, YEAR if
1177`european-calendar-style' is t. Diary entry can contain `%d' or `%d%s'; the
1178%d will be replaced by the number of years since the MONTH DAY, YEAR and the
1179%s will be replaced by the ordinal ending of that number (that is, `st', `nd',
1180`rd' or `th', as appropriate. The anniversary of February 29 is considered
1181to be March 1 in non-leap years."
1182 (let* ((d (if european-calendar-style
1183 month
1184 day))
1185 (m (if european-calendar-style
1186 day
1187 month))
1188 (y (extract-calendar-year date))
1189 (diff (- y year)))
1190 (if (and (= m 2) (= d 29) (not (calendar-leap-year-p y)))
1191 (setq m 3
1192 d 1))
1193 (if (and (> diff 0) (calendar-date-equal (list m d y) date))
1194 (format entry diff (diary-ordinal-suffix diff)))))
1195
1196(defun diary-cyclic (n month day year)
1197 "Cycle diary entry--entry applies every N days starting at MONTH, DAY, YEAR.
1198If `european-calendar-style' is t, parameters are N, DAY, MONTH, YEAR.
1199ENTRY can contain `%d' or `%d%s'; the %d will be replaced by the number of
1200years since the MONTH DAY, YEAR and the %s will be replaced by the ordinal
1201ending of that number (that is, `st', `nd', `rd' or `th', as appropriate."
1202 (let* ((d (if european-calendar-style
1203 month
1204 day))
1205 (m (if european-calendar-style
1206 day
1207 month))
1208 (diff (- (calendar-absolute-from-gregorian date)
1209 (calendar-absolute-from-gregorian
1210 (list m d year))))
1211 (cycle (/ diff n)))
1212 (if (and (>= diff 0) (zerop (% diff n)))
1213 (format entry cycle (diary-ordinal-suffix cycle)))))
1214
1215(defun diary-ordinal-suffix (n)
1216 "Ordinal suffix for N. (That is, `st', `nd', `rd', or `th', as appropriate.)"
1217 (if (or (memq (% n 100) '(11 12 13))
1218 (< 3 (% n 10)))
1219 "th"
1220 (aref ["th" "st" "nd" "rd"] (% n 10))))
1221
1222(defun diary-day-of-year ()
1223 "Day of year and number of days remaining in the year of date diary entry."
1224 (calendar-day-of-year-string date))
1225
1226(defvar diary-remind-message
1227 '("Reminder: Only "
1228 (if (= 0 (% days 7))
1229 (concat (int-to-string (/ days 7)) (if (= 7 days) " week" " weeks"))
1230 (concat (int-to-string days) (if (= 1 days) " day" " days")))
1231 " until "
1232 diary-entry)
1233 "*Pseudo-pattern giving form of reminder messages in the fancy diary
1234display.
1235
1236Used by the function `diary-remind', a pseudo-pattern is a list of
1237expressions that can involve the keywords `days' (a number), `date' (a list of
1238month, day, year), and `diary-entry' (a string).")
1239
1240(defun diary-remind (sexp days &optional marking)
1241 "Provide a reminder of a diary entry.
1242SEXP is a diary-sexp. DAYS is either a single number or a list of numbers
1243indicating the number(s) of days before the event that the warning(s) should
1244occur on. If the current date is (one of) DAYS before the event indicated by
1245SEXP, then a suitable message (as specified by `diary-remind-message' is
1246returned.
1247
1248In addition to the reminders beforehand, the diary entry also appears on
1249the date itself.
1250
1251If optional parameter MARKING is non-nil then the reminders are marked on the
1252calendar. Marking of reminders is independent of whether the entry itself is
1253a marking or nonmarking one."
1254 (let ((diary-entry))
1255 (if (or (not marking-diary-entries) marking)
1256 (cond
1257 ((integerp days)
1258 (let ((date (calendar-gregorian-from-absolute
1259 (+ (calendar-absolute-from-gregorian date) days))))
1260 (if (setq diary-entry (eval sexp))
1261 (setq diary-entry (mapconcat 'eval diary-remind-message "")))))
1262 ((and (listp days) days)
1263 (setq diary-entry (diary-remind sexp (car days) marking))
1264 (if (not diary-entry)
1265 (setq diary-entry (diary-remind sexp (cdr days) marking))))))
1266 (or diary-entry
1267 (and (or (not marking-diary-entries) marking-diary-entry)
1268 (eval sexp)))))
1269
1270(defun add-to-diary-list (date string)
1271 "Add the entry (DATE STRING) to `diary-entries-list'.
1272Do nothing if DATE or STRING is nil."
1273 (and date string
1274 (setq diary-entries-list
1275 (append diary-entries-list (list (list date string))))))
1276
1277(defun make-diary-entry (string &optional nonmarking file)
1278 "Insert a diary entry STRING which may be NONMARKING in FILE.
1279If omitted, NONMARKING defaults to nil and FILE defaults to diary-file."
1280 (find-file-other-window
1281 (substitute-in-file-name (if file file diary-file)))
1282 (goto-char (point-max))
1283 (insert
1284 (if (bolp) "" "\n")
1285 (if nonmarking diary-nonmarking-symbol "")
1286 string " "))
1287
1288(defun insert-diary-entry (arg)
1289 "Insert a diary entry for the date indicated by point.
1290Prefix arg will make the entry nonmarking."
1291 (interactive "P")
1292 (make-diary-entry (calendar-date-string (calendar-cursor-to-date t) t t)
1293 arg))
1294
1295(defun insert-weekly-diary-entry (arg)
1296 "Insert a weekly diary entry for the day of the week indicated by point.
1297Prefix arg will make the entry nonmarking."
1298 (interactive "P")
1299 (make-diary-entry (calendar-day-name (calendar-cursor-to-date t))
1300 arg))
1301
1302(defun insert-monthly-diary-entry (arg)
1303 "Insert a monthly diary entry for the day of the month indicated by point.
1304Prefix arg will make the entry nonmarking."
1305 (interactive "P")
1306 (let* ((calendar-date-display-form
1307 (if european-calendar-style
1308 '(day " * ")
1309 '("* " day))))
1310 (make-diary-entry (calendar-date-string (calendar-cursor-to-date t) t)
1311 arg)))
1312
1313(defun insert-yearly-diary-entry (arg)
1314 "Insert an annual diary entry for the day of the year indicated by point.
1315Prefix arg will make the entry nonmarking."
1316 (interactive "P")
1317 (let* ((calendar-date-display-form
1318 (if european-calendar-style
1319 '(day " " monthname)
1320 '(monthname " " day))))
1321 (make-diary-entry (calendar-date-string (calendar-cursor-to-date t) t)
1322 arg)))
1323
1324(defun insert-anniversary-diary-entry (arg)
1325 "Insert an anniversary diary entry for the date given by point.
1326Prefix arg will make the entry nonmarking."
1327 (interactive "P")
1328 (let* ((calendar-date-display-form
1329 (if european-calendar-style
1330 '(day " " month " " year)
1331 '(month " " day " " year))))
1332 (make-diary-entry
1333 (format "%s(diary-anniversary %s)"
1334 sexp-diary-entry-symbol
1335 (calendar-date-string (calendar-cursor-to-date t) nil t))
1336 arg)))
1337
1338(defun insert-block-diary-entry (arg)
1339 "Insert a block diary entry for the days between the point and marked date.
1340Prefix arg will make the entry nonmarking."
1341 (interactive "P")
1342 (let* ((calendar-date-display-form
1343 (if european-calendar-style
1344 '(day " " month " " year)
1345 '(month " " day " " year)))
1346 (cursor (calendar-cursor-to-date t))
1347 (mark (or (car calendar-mark-ring)
1348 (error "No mark set in this buffer")))
1349 (start)
1350 (end))
1351 (if (< (calendar-absolute-from-gregorian mark)
1352 (calendar-absolute-from-gregorian cursor))
1353 (setq start mark
1354 end cursor)
1355 (setq start cursor
1356 end mark))
1357 (make-diary-entry
1358 (format "%s(diary-block %s %s)"
1359 sexp-diary-entry-symbol
1360 (calendar-date-string start nil t)
1361 (calendar-date-string end nil t))
1362 arg)))
1363
1364(defun insert-cyclic-diary-entry (arg)
1365 "Insert a cyclic diary entry starting at the date given by point.
1366Prefix arg will make the entry nonmarking."
1367 (interactive "P")
1368 (let* ((calendar-date-display-form
1369 (if european-calendar-style
1370 '(day " " month " " year)
1371 '(month " " day " " year))))
1372 (make-diary-entry
1373 (format "%s(diary-cyclic %d %s)"
1374 sexp-diary-entry-symbol
1375 (calendar-read "Repeat every how many days: "
1376 '(lambda (x) (> x 0)))
1377 (calendar-date-string (calendar-cursor-to-date t) nil t))
1378 arg)))
1379
e4ca7ef9 1380(provide 'diary-lib)
0808d911 1381
e4ca7ef9 1382;;; diary-lib.el ends here