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