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