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