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