]> code.delx.au - gnu-emacs/blob - lisp/calendar/diary-lib.el
Use defcustom.
[gnu-emacs] / lisp / calendar / diary-lib.el
1 ;;; diary-lib.el --- diary functions.
2
3 ;; Copyright (C) 1989, 1990, 1992, 1993, 1994, 1995 Free Software
4 ;; Foundation, Inc.
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
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.
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.
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!"))))
61
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!"))))
74
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))
87
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'."
92 t)
93
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'."
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
107 (autoload 'diary-iso-date "cal-iso"
108 "ISO calendar equivalent of date diary entry."
109 t)
110
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-china"
120 "Chinese calendar equivalent of date diary entry."
121 t)
122
123 (autoload 'diary-islamic-date "cal-islam"
124 "Islamic calendar equivalent of date diary entry."
125 t)
126
127 (autoload 'list-islamic-diary-entries "cal-islam"
128 "Add any Islamic date entries from the diary file to `diary-entries-list'."
129 t)
130
131 (autoload 'mark-islamic-diary-entries "cal-islam"
132 "Mark days in the calendar window that have Islamic date diary entries."
133 t)
134
135 (autoload 'mark-islamic-calendar-date-pattern "cal-islam"
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-persian-date "cal-persia"
180 "Persian calendar equivalent of date diary entry."
181 t)
182
183 (autoload 'diary-phases-of-moon "lunar" "Moon phases diary entry." t)
184
185 (autoload 'diary-sunrise-sunset "solar"
186 "Local time of sunrise and sunset as a diary entry."
187 t)
188
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."
192 t)
193
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.")
198
199 (modify-syntax-entry ?* "w" diary-syntax-table)
200
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.
206
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.
211
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:
215
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.
219
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.
224
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.
229
230 `diary-hook' is run last. This can be used for an appointment
231 notification function."
232
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))))
344
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)))
382
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"))))
411
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)))
492 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"))))
504
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)))
516
517 (defun print-diary-entries ()
518 "Print a hard copy of the diary display.
519
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.
523
524 If the fancy diary display is being used, just print the buffer.
525
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!")))))
556
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!"))))
581
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
596 ""
597 (concat
598 "\\|"
599 (substring (aref string-array i) 0 3) ".?")))))
600 pattern))
601
602 (defvar marking-diary-entries nil
603 "True during the marking of diary entries, nil otherwise.")
604
605 (defvar marking-diary-entry nil
606 "True during the marking of diary entries, if current entry is marking.")
607
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)
685 0
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
705 0
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
716 1
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!"))))
728
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\\)"))
738 (m)
739 (y)
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))))))))
789
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)))
822
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))))))
843
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)))))
855
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)))))
868
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)))
872
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)))))))
879
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
908
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.
913
914 Sexp diary entries must be prefaced by a `sexp-diary-entry-symbol' (normally
915 `%%'). The form of a sexp diary entry is
916
917 %%(SEXP) ENTRY
918
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.
923
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:
926
927 &%%(let ((dayname (calendar-day-of-week date))
928 (day (extract-calendar-day date)))
929 (or
930 (and (= day 21) (memq dayname '(1 2 3 4 5)))
931 (and (memq day '(19 20)) (= dayname 5)))
932 ) UIUC pay checks deposited
933
934 A number of built-in functions are available for this type of diary entry:
935
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.
942
943 %%(diary-float MONTH DAYNAME N) 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.
949
950 %%(diary-block M1 D1 Y1 M2 D2 Y2) text
951 Entry will appear on dates between M1/D1/Y1 and M2/D2/Y2,
952 inclusive. (If `european-calendar-style' is t, the
953 order of the parameters should be changed to D1, M1, Y1,
954 D2, M2, Y2.)
955
956 %%(diary-anniversary MONTH DAY YEAR) text
957 Entry will appear on anniversary dates of MONTH DAY, YEAR.
958 (If `european-calendar-style' is t, the order of the
959 parameters should be changed to DAY, MONTH, YEAR.) Text
960 can contain %d or %d%s; %d will be replaced by the number
961 of years since the MONTH DAY, YEAR and %s will be replaced
962 by the ordinal ending of that number (that is, `st', `nd',
963 `rd' or `th', as appropriate. The anniversary of February
964 29 is considered to be March 1 in a non-leap year.
965
966 %%(diary-cyclic N MONTH DAY YEAR) text
967 Entry will appear every N days, starting MONTH DAY, YEAR.
968 (If `european-calendar-style' is t, the order of the
969 parameters should be changed to N, DAY, MONTH, YEAR.) Text
970 can contain %d or %d%s; %d will be replaced by the number
971 of repetitions since the MONTH DAY, YEAR and %s will
972 be replaced by the ordinal ending of that number (that is,
973 `st', `nd', `rd' or `th', as appropriate.
974
975 %%(diary-remind SEXP DAYS &optional MARKING) text
976 Entry is a reminder for diary sexp SEXP. DAYS is either a
977 single number or a list of numbers indicating the number(s)
978 of days before the event that the warning(s) should occur.
979 If the current date is (one of) DAYS before the event
980 indicated by EXPR, then a suitable message (as specified
981 by `diary-remind-message') appears. In addition to the
982 reminders beforehand, the diary entry also appears on
983 the date itself. If optional MARKING is non-nil then the
984 *reminders* are marked on the calendar. Marking of
985 reminders is independent of whether the entry *itself* is
986 a marking or nonmarking one.
987
988 %%(diary-day-of-year)
989 Diary entries giving the day of the year and the number of
990 days remaining in the year will be made every day. Note
991 that since there is no text, it makes sense only if the
992 fancy diary display is used.
993
994 %%(diary-iso-date)
995 Diary entries giving the corresponding ISO commercial date
996 will be made every day. Note that since there is no text,
997 it makes sense only if the fancy diary display is used.
998
999 %%(diary-french-date)
1000 Diary entries giving the corresponding French Revolutionary
1001 date will be made every day. Note that since there is no
1002 text, it makes sense only if the fancy diary display is used.
1003
1004 %%(diary-islamic-date)
1005 Diary entries giving the corresponding Islamic date will be
1006 made every day. Note that since there is no text, it
1007 makes sense only if the fancy diary display is used.
1008
1009 %%(diary-hebrew-date)
1010 Diary entries giving the corresponding Hebrew date will be
1011 made every day. Note that since there is no text, it
1012 makes sense only if the fancy diary display is used.
1013
1014 %%(diary-astro-day-number) Diary entries giving the corresponding
1015 astronomical (Julian) day number will be made every day.
1016 Note that since there is no text, it makes sense only if the
1017 fancy diary display is used.
1018
1019 %%(diary-julian-date) Diary entries giving the corresponding
1020 Julian date will be made every day. Note that since
1021 there is no text, it makes sense only if the fancy diary
1022 display is used.
1023
1024 %%(diary-sunrise-sunset)
1025 Diary entries giving the local times of sunrise and sunset
1026 will be made every day. Note that since there is no text,
1027 it makes sense only if the fancy diary display is used.
1028 Floating point required.
1029
1030 %%(diary-phases-of-moon)
1031 Diary entries giving the times of the phases of the moon
1032 will be when appropriate. Note that since there is no text,
1033 it makes sense only if the fancy diary display is used.
1034 Floating point required.
1035
1036 %%(diary-yahrzeit MONTH DAY YEAR) text
1037 Text is assumed to be the name of the person; the date is
1038 the date of death on the *civil* calendar. The diary entry
1039 will appear on the proper Hebrew-date anniversary and on the
1040 day before. (If `european-calendar-style' is t, the order
1041 of the parameters should be changed to DAY, MONTH, YEAR.)
1042
1043 %%(diary-rosh-hodesh)
1044 Diary entries will be made on the dates of Rosh Hodesh on
1045 the Hebrew calendar. Note that since there is no text, it
1046 makes sense only if the fancy diary display is used.
1047
1048 %%(diary-parasha)
1049 Diary entries giving the weekly parasha will be made on
1050 every Saturday. Note that since there is no text, it
1051 makes sense only if the fancy diary display is used.
1052
1053 %%(diary-omer)
1054 Diary entries giving the omer count will be made every day
1055 from Passover to Shavuot. Note that since there is no text,
1056 it makes sense only if the fancy diary display is used.
1057
1058 Marking these entries is *extremely* time consuming, so these entries are
1059 best if they are nonmarking."
1060 (let* ((mark (regexp-quote diary-nonmarking-symbol))
1061 (sexp-mark (regexp-quote sexp-diary-entry-symbol))
1062 (s-entry (concat "\\(\\`\\|\^M\\|\n\\)" mark "?" sexp-mark "("))
1063 (entry-found))
1064 (goto-char (point-min))
1065 (while (re-search-forward s-entry nil t)
1066 (backward-char 1)
1067 (let ((sexp-start (point))
1068 (sexp)
1069 (entry)
1070 (entry-start)
1071 (line-start))
1072 (forward-sexp)
1073 (setq sexp (buffer-substring-no-properties sexp-start (point)))
1074 (save-excursion
1075 (re-search-backward "\^M\\|\n\\|\\`")
1076 (setq line-start (point)))
1077 (forward-char 1)
1078 (if (and (or (char-equal (preceding-char) ?\^M)
1079 (char-equal (preceding-char) ?\n))
1080 (not (looking-at " \\|\^I")))
1081 (progn;; Diary entry consists only of the sexp
1082 (backward-char 1)
1083 (setq entry ""))
1084 (setq entry-start (point))
1085 (re-search-forward "\^M\\|\n" nil t)
1086 (while (looking-at " \\|\^I")
1087 (re-search-forward "\^M\\|\n" nil t))
1088 (backward-char 1)
1089 (setq entry (buffer-substring-no-properties entry-start (point)))
1090 (while (string-match "[\^M]" entry)
1091 (aset entry (match-beginning 0) ?\n )))
1092 (let ((diary-entry (diary-sexp-entry sexp entry date)))
1093 (if diary-entry
1094 (subst-char-in-region line-start (point) ?\^M ?\n t))
1095 (add-to-diary-list date diary-entry)
1096 (setq entry-found (or entry-found diary-entry)))))
1097 entry-found))
1098
1099 (defun diary-sexp-entry (sexp entry date)
1100 "Process a SEXP diary ENTRY for DATE."
1101 (let ((result (if calendar-debug-sexp
1102 (let ((stack-trace-on-error t))
1103 (eval (car (read-from-string sexp))))
1104 (condition-case nil
1105 (eval (car (read-from-string sexp)))
1106 (error
1107 (beep)
1108 (message "Bad sexp at line %d in %s: %s"
1109 (save-excursion
1110 (save-restriction
1111 (narrow-to-region 1 (point))
1112 (goto-char (point-min))
1113 (let ((lines 1))
1114 (while (re-search-forward "\n\\|\^M" nil t)
1115 (setq lines (1+ lines)))
1116 lines)))
1117 diary-file sexp)
1118 (sleep-for 2))))))
1119 (if (stringp result)
1120 result
1121 (if result
1122 entry
1123 nil))))
1124
1125 (defun diary-date (month day year)
1126 "Specific date(s) diary entry.
1127 Entry applies if date is MONTH, DAY, YEAR if `european-calendar-style' is nil,
1128 and DAY, MONTH, YEAR if `european-calendar-style' is t. DAY, MONTH, and YEAR
1129 can be lists of integers, the constant t, or an integer. The constant t means
1130 all values."
1131 (let* ((dd (if european-calendar-style
1132 month
1133 day))
1134 (mm (if european-calendar-style
1135 day
1136 month))
1137 (m (extract-calendar-month date))
1138 (y (extract-calendar-year date))
1139 (d (extract-calendar-day date)))
1140 (if (and
1141 (or (and (listp dd) (memq d dd))
1142 (equal d dd)
1143 (eq dd t))
1144 (or (and (listp mm) (memq m mm))
1145 (equal m mm)
1146 (eq mm t))
1147 (or (and (listp year) (memq y year))
1148 (equal y year)
1149 (eq year t)))
1150 entry)))
1151
1152 (defun diary-block (m1 d1 y1 m2 d2 y2)
1153 "Block diary entry.
1154 Entry applies if date is between two dates. Order of the parameters is
1155 M1, D1, Y1, M2, D2, Y2 `european-calendar-style' is nil, and
1156 D1, M1, Y1, D2, M2, Y2 if `european-calendar-style' is t."
1157 (let ((date1 (calendar-absolute-from-gregorian
1158 (if european-calendar-style
1159 (list d1 m1 y1)
1160 (list m1 d1 y1))))
1161 (date2 (calendar-absolute-from-gregorian
1162 (if european-calendar-style
1163 (list d2 m2 y2)
1164 (list m2 d2 y2))))
1165 (d (calendar-absolute-from-gregorian date)))
1166 (if (and (<= date1 d) (<= d date2))
1167 entry)))
1168
1169 (defun diary-float (month dayname n)
1170 "Floating diary entry--entry applies if date is the nth dayname of month.
1171 Parameters are MONTH, DAYNAME, N. MONTH can be a list of months, the constant
1172 t, or an integer. The constant t means all months. If N is negative, count
1173 backward from the end of the month."
1174 (let ((m (extract-calendar-month date))
1175 (y (extract-calendar-year date)))
1176 (if (and
1177 (or (and (listp month) (memq m month))
1178 (equal m month)
1179 (eq month t))
1180 (calendar-date-equal date (calendar-nth-named-day n dayname m y)))
1181 entry)))
1182
1183 (defun diary-anniversary (month day year)
1184 "Anniversary diary entry.
1185 Entry applies if date is the anniversary of MONTH, DAY, YEAR if
1186 `european-calendar-style' is nil, and DAY, MONTH, YEAR if
1187 `european-calendar-style' is t. Diary entry can contain `%d' or `%d%s'; the
1188 %d will be replaced by the number of years since the MONTH DAY, YEAR and the
1189 %s will be replaced by the ordinal ending of that number (that is, `st', `nd',
1190 `rd' or `th', as appropriate. The anniversary of February 29 is considered
1191 to be March 1 in non-leap years."
1192 (let* ((d (if european-calendar-style
1193 month
1194 day))
1195 (m (if european-calendar-style
1196 day
1197 month))
1198 (y (extract-calendar-year date))
1199 (diff (- y year)))
1200 (if (and (= m 2) (= d 29) (not (calendar-leap-year-p y)))
1201 (setq m 3
1202 d 1))
1203 (if (and (> diff 0) (calendar-date-equal (list m d y) date))
1204 (format entry diff (diary-ordinal-suffix diff)))))
1205
1206 (defun diary-cyclic (n month day year)
1207 "Cycle diary entry--entry applies every N days starting at MONTH, DAY, YEAR.
1208 If `european-calendar-style' is t, parameters are N, DAY, MONTH, YEAR.
1209 ENTRY can contain `%d' or `%d%s'; the %d will be replaced by the number of
1210 years since the MONTH DAY, YEAR and the %s will be replaced by the ordinal
1211 ending of that number (that is, `st', `nd', `rd' or `th', as appropriate."
1212 (let* ((d (if european-calendar-style
1213 month
1214 day))
1215 (m (if european-calendar-style
1216 day
1217 month))
1218 (diff (- (calendar-absolute-from-gregorian date)
1219 (calendar-absolute-from-gregorian
1220 (list m d year))))
1221 (cycle (/ diff n)))
1222 (if (and (>= diff 0) (zerop (% diff n)))
1223 (format entry cycle (diary-ordinal-suffix cycle)))))
1224
1225 (defun diary-ordinal-suffix (n)
1226 "Ordinal suffix for N. (That is, `st', `nd', `rd', or `th', as appropriate.)"
1227 (if (or (memq (% n 100) '(11 12 13))
1228 (< 3 (% n 10)))
1229 "th"
1230 (aref ["th" "st" "nd" "rd"] (% n 10))))
1231
1232 (defun diary-day-of-year ()
1233 "Day of year and number of days remaining in the year of date diary entry."
1234 (calendar-day-of-year-string date))
1235
1236 (defcustom diary-remind-message
1237 '("Reminder: Only "
1238 (if (= 0 (% days 7))
1239 (concat (int-to-string (/ days 7)) (if (= 7 days) " week" " weeks"))
1240 (concat (int-to-string days) (if (= 1 days) " day" " days")))
1241 " until "
1242 diary-entry)
1243 "*Pseudo-pattern giving form of reminder messages in the fancy diary
1244 display.
1245
1246 Used by the function `diary-remind', a pseudo-pattern is a list of
1247 expressions that can involve the keywords `days' (a number), `date' (a list of
1248 month, day, year), and `diary-entry' (a string)."
1249 :type 'sexp
1250 :group 'diary)
1251
1252 (defun diary-remind (sexp days &optional marking)
1253 "Provide a reminder of a diary entry.
1254 SEXP is a diary-sexp. DAYS is either a single number or a list of numbers
1255 indicating the number(s) of days before the event that the warning(s) should
1256 occur on. If the current date is (one of) DAYS before the event indicated by
1257 SEXP, then a suitable message (as specified by `diary-remind-message' is
1258 returned.
1259
1260 In addition to the reminders beforehand, the diary entry also appears on
1261 the date itself.
1262
1263 If optional parameter MARKING is non-nil then the reminders are marked on the
1264 calendar. Marking of reminders is independent of whether the entry itself is
1265 a marking or nonmarking one."
1266 (let ((diary-entry))
1267 (if (or (not marking-diary-entries) marking)
1268 (cond
1269 ((integerp days)
1270 (let ((date (calendar-gregorian-from-absolute
1271 (+ (calendar-absolute-from-gregorian date) days))))
1272 (if (setq diary-entry (eval sexp))
1273 (setq diary-entry (mapconcat 'eval diary-remind-message "")))))
1274 ((and (listp days) days)
1275 (setq diary-entry (diary-remind sexp (car days) marking))
1276 (if (not diary-entry)
1277 (setq diary-entry (diary-remind sexp (cdr days) marking))))))
1278 (or diary-entry
1279 (and (or (not marking-diary-entries) marking-diary-entry)
1280 (eval sexp)))))
1281
1282 (defun add-to-diary-list (date string)
1283 "Add the entry (DATE STRING) to `diary-entries-list'.
1284 Do nothing if DATE or STRING is nil."
1285 (and date string
1286 (setq diary-entries-list
1287 (append diary-entries-list (list (list date string))))))
1288
1289 (defun make-diary-entry (string &optional nonmarking file)
1290 "Insert a diary entry STRING which may be NONMARKING in FILE.
1291 If omitted, NONMARKING defaults to nil and FILE defaults to diary-file."
1292 (find-file-other-window
1293 (substitute-in-file-name (if file file diary-file)))
1294 (goto-char (point-max))
1295 (insert
1296 (if (bolp) "" "\n")
1297 (if nonmarking diary-nonmarking-symbol "")
1298 string " "))
1299
1300 (defun insert-diary-entry (arg)
1301 "Insert a diary entry for the date indicated by point.
1302 Prefix arg will make the entry nonmarking."
1303 (interactive "P")
1304 (make-diary-entry (calendar-date-string (calendar-cursor-to-date t) t t)
1305 arg))
1306
1307 (defun insert-weekly-diary-entry (arg)
1308 "Insert a weekly diary entry for the day of the week indicated by point.
1309 Prefix arg will make the entry nonmarking."
1310 (interactive "P")
1311 (make-diary-entry (calendar-day-name (calendar-cursor-to-date t))
1312 arg))
1313
1314 (defun insert-monthly-diary-entry (arg)
1315 "Insert a monthly diary entry for the day of the month indicated by point.
1316 Prefix arg will make the entry nonmarking."
1317 (interactive "P")
1318 (let* ((calendar-date-display-form
1319 (if european-calendar-style
1320 '(day " * ")
1321 '("* " day))))
1322 (make-diary-entry (calendar-date-string (calendar-cursor-to-date t) t)
1323 arg)))
1324
1325 (defun insert-yearly-diary-entry (arg)
1326 "Insert an annual diary entry for the day of the year indicated by point.
1327 Prefix arg will make the entry nonmarking."
1328 (interactive "P")
1329 (let* ((calendar-date-display-form
1330 (if european-calendar-style
1331 '(day " " monthname)
1332 '(monthname " " day))))
1333 (make-diary-entry (calendar-date-string (calendar-cursor-to-date t) t)
1334 arg)))
1335
1336 (defun insert-anniversary-diary-entry (arg)
1337 "Insert an anniversary diary entry for the date given by point.
1338 Prefix arg will make the entry nonmarking."
1339 (interactive "P")
1340 (let* ((calendar-date-display-form
1341 (if european-calendar-style
1342 '(day " " month " " year)
1343 '(month " " day " " year))))
1344 (make-diary-entry
1345 (format "%s(diary-anniversary %s)"
1346 sexp-diary-entry-symbol
1347 (calendar-date-string (calendar-cursor-to-date t) nil t))
1348 arg)))
1349
1350 (defun insert-block-diary-entry (arg)
1351 "Insert a block diary entry for the days between the point and marked date.
1352 Prefix arg will make the entry nonmarking."
1353 (interactive "P")
1354 (let* ((calendar-date-display-form
1355 (if european-calendar-style
1356 '(day " " month " " year)
1357 '(month " " day " " year)))
1358 (cursor (calendar-cursor-to-date t))
1359 (mark (or (car calendar-mark-ring)
1360 (error "No mark set in this buffer")))
1361 (start)
1362 (end))
1363 (if (< (calendar-absolute-from-gregorian mark)
1364 (calendar-absolute-from-gregorian cursor))
1365 (setq start mark
1366 end cursor)
1367 (setq start cursor
1368 end mark))
1369 (make-diary-entry
1370 (format "%s(diary-block %s %s)"
1371 sexp-diary-entry-symbol
1372 (calendar-date-string start nil t)
1373 (calendar-date-string end nil t))
1374 arg)))
1375
1376 (defun insert-cyclic-diary-entry (arg)
1377 "Insert a cyclic diary entry starting at the date given by point.
1378 Prefix arg will make the entry nonmarking."
1379 (interactive "P")
1380 (let* ((calendar-date-display-form
1381 (if european-calendar-style
1382 '(day " " month " " year)
1383 '(month " " day " " year))))
1384 (make-diary-entry
1385 (format "%s(diary-cyclic %d %s)"
1386 sexp-diary-entry-symbol
1387 (calendar-read "Repeat every how many days: "
1388 '(lambda (x) (> x 0)))
1389 (calendar-date-string (calendar-cursor-to-date t) nil t))
1390 arg)))
1391
1392 (provide 'diary-lib)
1393
1394 ;;; diary-lib.el ends here