Rewrote calendar-mouse-holidays, calendar-mouse-view-diary-entries,
[bpt/emacs.git] / lisp / calendar / diary-lib.el
CommitLineData
e4ca7ef9 1;;; diary-lib.el --- diary functions.
0808d911 2
b578f267
EN
3;; Copyright (C) 1989, 1990, 1992, 1993, 1994, 1995 Free Software
4;; Foundation, Inc.
0808d911
ER
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
b578f267
EN
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.
0808d911
ER
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.
44If no argument is provided, the number of days of diary entries is governed
45by the variable `number-of-diary-entries'. This function is suitable for
46execution 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.
64Searches the file named in `diary-file' for entries that
65match ARG days starting with the date indicated by the cursor position
66in 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
892e6825 75(defun view-other-diary-entries (arg d-file)
0808d911
ER
76 "Prepare and display buffer of diary entries from an alternative diary file.
77Prompts for a file name and searches that file for entries that match ARG
78days starting with the date indicated by the cursor position in the displayed
79three-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))
892e6825
ER
84 (read-file-name "Enter diary file name: " default-directory nil t)))
85 (let ((diary-file d-file))
86 (view-diary-entries arg)))
0808d911
ER
87
88(autoload 'check-calendar-holidays "holidays"
89 "Check the list of holidays for any that occur on DATE.
90The value returned is a list of strings of relevant holiday descriptions.
91The 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.
96The 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
c36baedb
ER
107(autoload 'diary-iso-date "cal-iso"
108 "ISO calendar equivalent of date diary entry."
109 t)
110
0808d911
ER
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
327b6868 119(autoload 'diary-chinese-date "cal-china"
0808d911
ER
120 "Chinese calendar equivalent of date diary entry."
121 t)
122
327b6868 123(autoload 'diary-islamic-date "cal-islam"
0808d911
ER
124 "Islamic calendar equivalent of date diary entry."
125 t)
126
327b6868 127(autoload 'list-islamic-diary-entries "cal-islam"
0808d911
ER
128 "Add any Islamic date entries from the diary file to `diary-entries-list'."
129 t)
130
327b6868 131(autoload 'mark-islamic-diary-entries "cal-islam"
0808d911
ER
132 "Mark days in the calendar window that have Islamic date diary entries."
133 t)
134
327b6868 135(autoload 'mark-islamic-calendar-date-pattern "cal-islam"
0808d911
ER
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
a45eee72 179(autoload 'diary-persian-date "cal-persia"
72fb78ce
ER
180 "Persian calendar equivalent of date diary entry."
181 t)
182
0808d911
ER
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.
191No 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.
196It is the standard syntax table used in Fundamental mode, but with the
197syntax 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.
203The arguments are DATE and NUMBER; the entries selected are those
204for NUMBER days starting with date DATE. The other entries are hidden
205using selective display.
206
207Returns a list of all relevant diary entries found, if any, in order by date.
a14723d9
KH
208The 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
210specifier is the applicability. If the variable `diary-list-include-blanks'
211is t, this list includes a dummy diary entry consisting of the empty string)
212for a date with no diary entries.
0808d911
ER
213
214After the list is prepared, the hooks `nongregorian-diary-listing-hook',
215`list-diary-entries-hook', `diary-display-hook', and `diary-hook' are run.
216These 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
cdfeb73f 243 (let ((diary-buffer (find-buffer-visiting d-file)))
ca67b44e
RS
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))))
0808d911
ER
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
a14723d9
KH
323 date
324 (buffer-substring-no-properties
325 entry-start (point))
326 (buffer-substring-no-properties
327 (1+ date-start) (1- entry-start)))))))
0808d911
ER
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
a14723d9 333 (list (list date "" "")))))
0808d911
ER
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.
351This function is suitable for use in `list-diary-entries-hook';
352it enables you to use shared diary files together with your own.
353The files included are specified in the diaryfile by lines of this form:
354 #include \"filename\"
355This is recursive; that is, #include directives in diary files thus included
356are obeyed. You can change the `#include' to some other string by
357changing 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
e6c3bfbc
ER
366 (buffer-substring-no-properties
367 (match-beginning 2) (match-end 2))))
0808d911
ER
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)))
cdfeb73f 378 (kill-buffer (find-buffer-visiting diary-file)))
0808d911
ER
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))
b3099c46 398 (message "%s" msg)
0808d911
ER
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 "; ")))
cdfeb73f 413 (display-buffer (find-buffer-visiting d-file))
0808d911
ER
414 (message "Preparing diary...done"))))
415
416(defun fancy-diary-display ()
417 "Prepare a diary buffer with relevant entries in a fancy, noneditable form.
418This function is provided for optional use as the `diary-display-hook'."
419 (save-excursion;; Turn off selective-display in the diary file's buffer.
cdfeb73f 420 (set-buffer (find-buffer-visiting (substitute-in-file-name diary-file)))
0808d911
ER
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))
b3099c46 435 (message "%s" msg)
0808d911
ER
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 ": "))
72fb78ce
ER
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)))))
0808d911
ER
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
524If the simple diary display is being used, prepare a temp buffer with the
525visible lines of the diary buffer, add a heading line composed from the mode
526line, print the temp buffer, and destroy it.
527
528If the fancy diary display is being used, just print the buffer.
529
530The hooks given by the variable `print-diary-entries-hook' are called to do
531the 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
cdfeb73f 538 (find-buffer-visiting (substitute-in-file-name diary-file))))
0808d911
ER
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.
563This function gets rid of the selective display of the diary file so that
564all entries, not just some, are visible. If there is no diary buffer, one
565is 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
cdfeb73f 571 (let ((diary-buffer (find-buffer-visiting d-file)))
0808d911
ER
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
8ffbfaa9
RS
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
cd32a7ba
DN
592 :type 'string
593 :version "20.3")
8ffbfaa9
RS
594
595(defcustom diary-mail-days 7
596 "*Number of days for `diary-mail-entries' to check."
597 :group 'diary
cd32a7ba
DN
598 :type 'integer
599 :version "20.3")
8ffbfaa9 600
f91adf29 601;;;###autoload
8ffbfaa9
RS
602(defun diary-mail-entries (&optional ndays)
603 "Send a mail message showing diary entries for next NDAYS days.
604If no prefix argument is given, NDAYS is set to `diary-mail-days'.
605
606You can call `diary-mail-entries' every night using an at/cron job.
607For example, this script will run the program at 2am daily. Since
608`emacs -batch' does not load your `.emacs' file, you must ensure that
609all relevant variables are set, as done here.
610
611#!/bin/sh
612# diary-rem.sh -- repeatedly run the Emacs diary-reminder
613emacs -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
618at -f diary-rem.sh 0200 tomorrow
619
620You may have to tweak the syntax of the `at' command to suit your
621system. Alternatively, you can specify a cron entry:
6220 1 * * * diary-rem.sh
623to 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 ")
23c81514 642 (insert (format-time-string "%a %d %b %Y" (current-time)))
8ffbfaa9
RS
643 (mail-text)
644 (insert text)
645 (mail-send-and-exit nil)))
646
0808d911
ER
647(defun diary-name-pattern (string-array &optional fullname)
648 "Convert an STRING-ARRAY, an array of strings to a pattern.
649The pattern will match any of the strings, either entirely or abbreviated
650to three characters. An abbreviated form will match with or without a period;
651If the optional FULLNAME is t, abbreviations will not match, just the full
652name."
653 (let ((pattern ""))
654 (calendar-for-loop i from 0 to (1- (length string-array)) do
655 (setq pattern
656 (concat
657 pattern
658 (if (string-equal pattern "") "" "\\|")
659 (aref string-array i)
660 (if fullname
661 ""
662 (concat
663 "\\|"
664 (substring (aref string-array i) 0 3) ".?")))))
665 pattern))
666
667(defvar marking-diary-entries nil
668 "True during the marking of diary entries, nil otherwise.")
669
670(defvar marking-diary-entry nil
671 "True during the marking of diary entries, if current entry is marking.")
672
673(defun mark-diary-entries ()
674 "Mark days in the calendar window that have diary entries.
675Each entry in the diary file visible in the calendar window is marked.
676After the entries are marked, the hooks `nongregorian-diary-marking-hook' and
677`mark-diary-entries-hook' are run."
678 (interactive)
679 (setq mark-diary-entries-in-calendar t)
680 (let ((d-file (substitute-in-file-name diary-file))
681 (marking-diary-entries t))
682 (if (and d-file (file-exists-p d-file))
683 (if (file-readable-p d-file)
684 (save-excursion
685 (message "Marking diary entries...")
686 (set-buffer (find-file-noselect d-file t))
687 (let ((d diary-date-forms)
688 (old-diary-syntax-table))
689 (setq old-diary-syntax-table (syntax-table))
690 (set-syntax-table diary-syntax-table)
691 (while d
692 (let*
693 ((date-form (if (equal (car (car d)) 'backup)
694 (cdr (car d))
695 (car d)));; ignore 'backup directive
696 (dayname (diary-name-pattern calendar-day-name-array))
697 (monthname
698 (concat
699 (diary-name-pattern calendar-month-name-array)
700 "\\|\\*"))
701 (month "[0-9]+\\|\\*")
702 (day "[0-9]+\\|\\*")
703 (year "[0-9]+\\|\\*")
704 (l (length date-form))
705 (d-name-pos (- l (length (memq 'dayname date-form))))
706 (d-name-pos (if (/= l d-name-pos) (+ 2 d-name-pos)))
707 (m-name-pos (- l (length (memq 'monthname date-form))))
708 (m-name-pos (if (/= l m-name-pos) (+ 2 m-name-pos)))
709 (d-pos (- l (length (memq 'day date-form))))
710 (d-pos (if (/= l d-pos) (+ 2 d-pos)))
711 (m-pos (- l (length (memq 'month date-form))))
712 (m-pos (if (/= l m-pos) (+ 2 m-pos)))
713 (y-pos (- l (length (memq 'year date-form))))
714 (y-pos (if (/= l y-pos) (+ 2 y-pos)))
715 (regexp
716 (concat
717 "\\(\\`\\|\^M\\|\n\\)\\("
718 (mapconcat 'eval date-form "\\)\\(")
719 "\\)"))
720 (case-fold-search t))
721 (goto-char (point-min))
722 (while (re-search-forward regexp nil t)
723 (let* ((dd-name
724 (if d-name-pos
e6c3bfbc 725 (buffer-substring-no-properties
0808d911
ER
726 (match-beginning d-name-pos)
727 (match-end d-name-pos))))
728 (mm-name
729 (if m-name-pos
e6c3bfbc 730 (buffer-substring-no-properties
0808d911
ER
731 (match-beginning m-name-pos)
732 (match-end m-name-pos))))
733 (mm (string-to-int
734 (if m-pos
e6c3bfbc 735 (buffer-substring-no-properties
0808d911
ER
736 (match-beginning m-pos)
737 (match-end m-pos))
738 "")))
739 (dd (string-to-int
740 (if d-pos
e6c3bfbc 741 (buffer-substring-no-properties
0808d911
ER
742 (match-beginning d-pos)
743 (match-end d-pos))
744 "")))
745 (y-str (if y-pos
e6c3bfbc 746 (buffer-substring-no-properties
0808d911
ER
747 (match-beginning y-pos)
748 (match-end y-pos))))
749 (yy (if (not y-str)
750 0
751 (if (and (= (length y-str) 2)
752 abbreviated-calendar-year)
753 (let* ((current-y
754 (extract-calendar-year
755 (calendar-current-date)))
756 (y (+ (string-to-int y-str)
757 (* 100
758 (/ current-y 100)))))
759 (if (> (- y current-y) 50)
760 (- y 100)
761 (if (> (- current-y y) 50)
762 (+ y 100)
763 y)))
764 (string-to-int y-str)))))
765 (if dd-name
766 (mark-calendar-days-named
767 (cdr (assoc (capitalize (substring dd-name 0 3))
768 (calendar-make-alist
769 calendar-day-name-array
770 0
771 '(lambda (x) (substring x 0 3))))))
772 (if mm-name
773 (if (string-equal mm-name "*")
774 (setq mm 0)
775 (setq mm
776 (cdr (assoc
777 (capitalize
778 (substring mm-name 0 3))
779 (calendar-make-alist
780 calendar-month-name-array
781 1
782 '(lambda (x) (substring x 0 3)))
783 )))))
784 (mark-calendar-date-pattern mm dd yy))))
785 (setq d (cdr d))))
786 (mark-sexp-diary-entries)
787 (run-hooks 'nongregorian-diary-marking-hook
788 'mark-diary-entries-hook)
789 (set-syntax-table old-diary-syntax-table)
790 (message "Marking diary entries...done")))
791 (error "Your diary file is not readable!"))
792 (error "You don't have a diary file!"))))
793
794(defun mark-sexp-diary-entries ()
795 "Mark days in the calendar window that have sexp diary entries.
796Each entry in the diary file (or included files) visible in the calendar window
797is marked. See the documentation for the function `list-sexp-diary-entries'."
798 (let* ((sexp-mark (regexp-quote sexp-diary-entry-symbol))
799 (s-entry (concat "\\(\\`\\|\^M\\|\n\\)\\("
800 (regexp-quote sexp-mark) "(\\)\\|\\("
801 (regexp-quote diary-nonmarking-symbol)
802 (regexp-quote sexp-mark) "(diary-remind\\)"))
803 (m)
804 (y)
805 (first-date)
806 (last-date))
807 (save-excursion
808 (set-buffer calendar-buffer)
809 (setq m displayed-month)
810 (setq y displayed-year))
811 (increment-calendar-month m y -1)
812 (setq first-date
813 (calendar-absolute-from-gregorian (list m 1 y)))
814 (increment-calendar-month m y 2)
815 (setq last-date
816 (calendar-absolute-from-gregorian
817 (list m (calendar-last-day-of-month m y) y)))
818 (goto-char (point-min))
819 (while (re-search-forward s-entry nil t)
dbabfd55 820 (if (char-equal (preceding-char) ?\()
0808d911
ER
821 (setq marking-diary-entry t)
822 (setq marking-diary-entry nil))
823 (re-search-backward "(")
824 (let ((sexp-start (point))
825 (sexp)
826 (entry)
827 (entry-start)
828 (line-start))
829 (forward-sexp)
e6c3bfbc 830 (setq sexp (buffer-substring-no-properties sexp-start (point)))
0808d911
ER
831 (save-excursion
832 (re-search-backward "\^M\\|\n\\|\\`")
833 (setq line-start (point)))
834 (forward-char 1)
835 (if (and (or (char-equal (preceding-char) ?\^M)
836 (char-equal (preceding-char) ?\n))
837 (not (looking-at " \\|\^I")))
838 (progn;; Diary entry consists only of the sexp
839 (backward-char 1)
840 (setq entry ""))
841 (setq entry-start (point))
842 (re-search-forward "\^M\\|\n" nil t)
843 (while (looking-at " \\|\^I")
844 (re-search-forward "\^M\\|\n" nil t))
845 (backward-char 1)
e6c3bfbc 846 (setq entry (buffer-substring-no-properties entry-start (point)))
0808d911
ER
847 (while (string-match "[\^M]" entry)
848 (aset entry (match-beginning 0) ?\n )))
849 (calendar-for-loop date from first-date to last-date do
850 (if (diary-sexp-entry sexp entry
851 (calendar-gregorian-from-absolute date))
852 (mark-visible-calendar-date
853 (calendar-gregorian-from-absolute date))))))))
854
855(defun mark-included-diary-files ()
856 "Mark the diary entries from other diary files with those of the diary file.
857This function is suitable for use as the `mark-diary-entries-hook'; it enables
858you to use shared diary files together with your own. The files included are
859specified in the diary-file by lines of this form:
860 #include \"filename\"
861This is recursive; that is, #include directives in diary files thus included
862are obeyed. You can change the `#include' to some other string by
863changing the variable `diary-include-string'."
864 (goto-char (point-min))
865 (while (re-search-forward
866 (concat
867 "\\(\\`\\|\^M\\|\n\\)"
868 (regexp-quote diary-include-string)
869 " \"\\([^\"]*\\)\"")
870 nil t)
871 (let ((diary-file (substitute-in-file-name
e6c3bfbc
ER
872 (buffer-substring-no-properties
873 (match-beginning 2) (match-end 2))))
0808d911
ER
874 (mark-diary-entries-hook 'mark-included-diary-files))
875 (if (file-exists-p diary-file)
876 (if (file-readable-p diary-file)
877 (progn
878 (mark-diary-entries)
cdfeb73f 879 (kill-buffer (find-buffer-visiting diary-file)))
0808d911
ER
880 (beep)
881 (message "Can't read included diary file %s" diary-file)
882 (sleep-for 2))
883 (beep)
884 (message "Can't find included diary file %s" diary-file)
885 (sleep-for 2))))
886 (goto-char (point-min)))
887
888(defun mark-calendar-days-named (dayname)
889 "Mark all dates in the calendar window that are day DAYNAME of the week.
8900 means all Sundays, 1 means all Mondays, and so on."
891 (save-excursion
892 (set-buffer calendar-buffer)
893 (let ((prev-month displayed-month)
894 (prev-year displayed-year)
895 (succ-month displayed-month)
896 (succ-year displayed-year)
897 (last-day)
898 (day))
899 (increment-calendar-month succ-month succ-year 1)
900 (increment-calendar-month prev-month prev-year -1)
901 (setq day (calendar-absolute-from-gregorian
902 (calendar-nth-named-day 1 dayname prev-month prev-year)))
903 (setq last-day (calendar-absolute-from-gregorian
904 (calendar-nth-named-day -1 dayname succ-month succ-year)))
905 (while (<= day last-day)
906 (mark-visible-calendar-date (calendar-gregorian-from-absolute day))
907 (setq day (+ day 7))))))
908
909(defun mark-calendar-date-pattern (month day year)
910 "Mark all dates in the calendar window that conform to MONTH/DAY/YEAR.
911A value of 0 in any position is a wildcard."
912 (save-excursion
913 (set-buffer calendar-buffer)
914 (let ((m displayed-month)
915 (y displayed-year))
916 (increment-calendar-month m y -1)
917 (calendar-for-loop i from 0 to 2 do
918 (mark-calendar-month m y month day year)
919 (increment-calendar-month m y 1)))))
920
921(defun mark-calendar-month (month year p-month p-day p-year)
922 "Mark dates in the MONTH/YEAR that conform to pattern P-MONTH/P_DAY/P-YEAR.
923A value of 0 in any position of the pattern is a wildcard."
924 (if (or (and (= month p-month)
925 (or (= p-year 0) (= year p-year)))
926 (and (= p-month 0)
927 (or (= p-year 0) (= year p-year))))
928 (if (= p-day 0)
929 (calendar-for-loop
930 i from 1 to (calendar-last-day-of-month month year) do
931 (mark-visible-calendar-date (list month i year)))
932 (mark-visible-calendar-date (list month p-day year)))))
933
934(defun sort-diary-entries ()
935 "Sort the list of diary entries by time of day."
936 (setq diary-entries-list (sort diary-entries-list 'diary-entry-compare)))
937
938(defun diary-entry-compare (e1 e2)
939 "Returns t if E1 is earlier than E2."
940 (or (calendar-date-compare e1 e2)
941 (and (calendar-date-equal (car e1) (car e2))
942 (< (diary-entry-time (car (cdr e1)))
943 (diary-entry-time (car (cdr e2)))))))
944
a14723d9
KH
945(defcustom diary-unknown-time
946 -9999
947 "*Value returned by diary-entry-time when no time is found.
948The default value -9999 causes entries with no recognizable time to be placed
949before those with times; 9999 would place entries with no recognizable time
950after those with times."
951 :type 'integer
cd32a7ba
DN
952 :group 'diary
953 :version "20.3")
a14723d9 954
0808d911 955(defun diary-entry-time (s)
a14723d9
KH
956 "Time at the beginning of the string S in a military-style integer. For
957example, returns 1325 for 1:25pm. Returns `diary-unknown-time' (default value
958-9999) if no time is recognized. The recognized forms are XXXX, X:XX, or
959XX:XX (military time), and XXam, XXAM, XXpm, XXPM, XX:XXam, XX:XXAM XX:XXpm,
960or XX:XXPM."
0488694c
RS
961 (let ((case-fold-search nil))
962 (cond ((string-match;; Military time
963 "^[ \t]*\\([0-9]?[0-9]\\):?\\([0-9][0-9]\\)\\(\\>\\|[^ap]\\)" s)
964 (+ (* 100 (string-to-int
965 (substring s (match-beginning 1) (match-end 1))))
966 (string-to-int (substring s (match-beginning 2) (match-end 2)))))
967 ((string-match;; Hour only XXam or XXpm
968 "^[ \t]*\\([0-9]?[0-9]\\)\\([ap]\\)m\\>" s)
969 (+ (* 100 (% (string-to-int
970 (substring s (match-beginning 1) (match-end 1)))
971 12))
972 (if (equal ?a (downcase (aref s (match-beginning 2))))
973 0 1200)))
974 ((string-match;; Hour and minute XX:XXam or XX:XXpm
975 "^[ \t]*\\([0-9]?[0-9]\\):\\([0-9][0-9]\\)\\([ap]\\)m\\>" s)
976 (+ (* 100 (% (string-to-int
977 (substring s (match-beginning 1) (match-end 1)))
978 12))
979 (string-to-int (substring s (match-beginning 2) (match-end 2)))
980 (if (equal ?a (downcase (aref s (match-beginning 3))))
981 0 1200)))
a14723d9 982 (t diary-unknown-time))));; Unrecognizable
0808d911
ER
983
984(defun list-sexp-diary-entries (date)
985 "Add sexp entries for DATE from the diary file to `diary-entries-list'.
986Also, Make them visible in the diary file. Returns t if any entries were
987found.
988
989Sexp diary entries must be prefaced by a `sexp-diary-entry-symbol' (normally
990`%%'). The form of a sexp diary entry is
991
992 %%(SEXP) ENTRY
993
994Both ENTRY and DATE are globally available when the SEXP is evaluated. If the
995SEXP yields the value nil, the diary entry does not apply. If it yields a
996non-nil value, ENTRY will be taken to apply to DATE; if the non-nil value is a
997string, that string will be the diary entry in the fancy diary display.
998
999For example, the following diary entry will apply to the 21st of the month
1000if it is a weekday and the Friday before if the 21st is on a weekend:
1001
1002 &%%(let ((dayname (calendar-day-of-week date))
1003 (day (extract-calendar-day date)))
1004 (or
1005 (and (= day 21) (memq dayname '(1 2 3 4 5)))
1006 (and (memq day '(19 20)) (= dayname 5)))
1007 ) UIUC pay checks deposited
1008
1009A number of built-in functions are available for this type of diary entry:
1010
1011 %%(diary-date MONTH DAY YEAR) text
1012 Entry applies if date is MONTH, DAY, YEAR if
1013 `european-calendar-style' is nil, and DAY, MONTH, YEAR if
1014 `european-calendar-style' is t. DAY, MONTH, and YEAR
1015 can be lists of integers, the constant t, or an integer.
1016 The constant t means all values.
1017
9b58d144 1018 %%(diary-float MONTH DAYNAME N &optional DAY) text
0808d911
ER
1019 Entry will appear on the Nth DAYNAME of MONTH.
1020 (DAYNAME=0 means Sunday, 1 means Monday, and so on;
1021 if N is negative it counts backward from the end of
1022 the month. MONTH can be a list of months, a single
9b58d144
RS
1023 month, or t to specify all months. Optional DAY means
1024 Nth DAYNAME of MONTH on or after/before DAY. DAY defaults
1025 to 1 if N>0 and the last day of the month if N<0.
0808d911
ER
1026
1027 %%(diary-block M1 D1 Y1 M2 D2 Y2) text
1028 Entry will appear on dates between M1/D1/Y1 and M2/D2/Y2,
1029 inclusive. (If `european-calendar-style' is t, the
1030 order of the parameters should be changed to D1, M1, Y1,
1031 D2, M2, Y2.)
1032
1033 %%(diary-anniversary MONTH DAY YEAR) text
1034 Entry will appear on anniversary dates of MONTH DAY, YEAR.
1035 (If `european-calendar-style' is t, the order of the
1036 parameters should be changed to DAY, MONTH, YEAR.) Text
1037 can contain %d or %d%s; %d will be replaced by the number
1038 of years since the MONTH DAY, YEAR and %s will be replaced
1039 by the ordinal ending of that number (that is, `st', `nd',
1040 `rd' or `th', as appropriate. The anniversary of February
1041 29 is considered to be March 1 in a non-leap year.
1042
1043 %%(diary-cyclic N MONTH DAY YEAR) text
1044 Entry will appear every N days, starting MONTH DAY, YEAR.
1045 (If `european-calendar-style' is t, the order of the
1046 parameters should be changed to N, DAY, MONTH, YEAR.) Text
1047 can contain %d or %d%s; %d will be replaced by the number
1048 of repetitions since the MONTH DAY, YEAR and %s will
1049 be replaced by the ordinal ending of that number (that is,
1050 `st', `nd', `rd' or `th', as appropriate.
1051
1052 %%(diary-remind SEXP DAYS &optional MARKING) text
1053 Entry is a reminder for diary sexp SEXP. DAYS is either a
1054 single number or a list of numbers indicating the number(s)
1055 of days before the event that the warning(s) should occur.
1056 If the current date is (one of) DAYS before the event
1057 indicated by EXPR, then a suitable message (as specified
1058 by `diary-remind-message') appears. In addition to the
1059 reminders beforehand, the diary entry also appears on
1060 the date itself. If optional MARKING is non-nil then the
1061 *reminders* are marked on the calendar. Marking of
1062 reminders is independent of whether the entry *itself* is
1063 a marking or nonmarking one.
1064
1065 %%(diary-day-of-year)
1066 Diary entries giving the day of the year and the number of
1067 days remaining in the year will be made every day. Note
1068 that since there is no text, it makes sense only if the
1069 fancy diary display is used.
1070
1071 %%(diary-iso-date)
1072 Diary entries giving the corresponding ISO commercial date
1073 will be made every day. Note that since there is no text,
1074 it makes sense only if the fancy diary display is used.
1075
1076 %%(diary-french-date)
1077 Diary entries giving the corresponding French Revolutionary
1078 date will be made every day. Note that since there is no
1079 text, it makes sense only if the fancy diary display is used.
1080
1081 %%(diary-islamic-date)
1082 Diary entries giving the corresponding Islamic date will be
1083 made every day. Note that since there is no text, it
1084 makes sense only if the fancy diary display is used.
1085
1086 %%(diary-hebrew-date)
1087 Diary entries giving the corresponding Hebrew date will be
1088 made every day. Note that since there is no text, it
1089 makes sense only if the fancy diary display is used.
1090
1091 %%(diary-astro-day-number) Diary entries giving the corresponding
1092 astronomical (Julian) day number will be made every day.
1093 Note that since there is no text, it makes sense only if the
1094 fancy diary display is used.
1095
1096 %%(diary-julian-date) Diary entries giving the corresponding
1097 Julian date will be made every day. Note that since
1098 there is no text, it makes sense only if the fancy diary
1099 display is used.
1100
1101 %%(diary-sunrise-sunset)
1102 Diary entries giving the local times of sunrise and sunset
1103 will be made every day. Note that since there is no text,
1104 it makes sense only if the fancy diary display is used.
1105 Floating point required.
1106
1107 %%(diary-phases-of-moon)
1108 Diary entries giving the times of the phases of the moon
1109 will be when appropriate. Note that since there is no text,
1110 it makes sense only if the fancy diary display is used.
1111 Floating point required.
1112
1113 %%(diary-yahrzeit MONTH DAY YEAR) text
1114 Text is assumed to be the name of the person; the date is
1115 the date of death on the *civil* calendar. The diary entry
1116 will appear on the proper Hebrew-date anniversary and on the
1117 day before. (If `european-calendar-style' is t, the order
1118 of the parameters should be changed to DAY, MONTH, YEAR.)
1119
1120 %%(diary-rosh-hodesh)
1121 Diary entries will be made on the dates of Rosh Hodesh on
1122 the Hebrew calendar. Note that since there is no text, it
1123 makes sense only if the fancy diary display is used.
1124
1125 %%(diary-parasha)
1126 Diary entries giving the weekly parasha will be made on
1127 every Saturday. Note that since there is no text, it
1128 makes sense only if the fancy diary display is used.
1129
1130 %%(diary-omer)
1131 Diary entries giving the omer count will be made every day
59ba214d 1132 from Passover to Shavuot. Note that since there is no text,
0808d911
ER
1133 it makes sense only if the fancy diary display is used.
1134
1135Marking these entries is *extremely* time consuming, so these entries are
1136best if they are nonmarking."
1137 (let* ((mark (regexp-quote diary-nonmarking-symbol))
1138 (sexp-mark (regexp-quote sexp-diary-entry-symbol))
1139 (s-entry (concat "\\(\\`\\|\^M\\|\n\\)" mark "?" sexp-mark "("))
1140 (entry-found))
1141 (goto-char (point-min))
1142 (while (re-search-forward s-entry nil t)
1143 (backward-char 1)
1144 (let ((sexp-start (point))
1145 (sexp)
1146 (entry)
a14723d9 1147 (specifier)
0808d911
ER
1148 (entry-start)
1149 (line-start))
1150 (forward-sexp)
e6c3bfbc 1151 (setq sexp (buffer-substring-no-properties sexp-start (point)))
0808d911
ER
1152 (save-excursion
1153 (re-search-backward "\^M\\|\n\\|\\`")
1154 (setq line-start (point)))
a14723d9
KH
1155 (setq specifier
1156 (buffer-substring-no-properties (1+ line-start) (point)))
0808d911
ER
1157 (forward-char 1)
1158 (if (and (or (char-equal (preceding-char) ?\^M)
1159 (char-equal (preceding-char) ?\n))
1160 (not (looking-at " \\|\^I")))
1161 (progn;; Diary entry consists only of the sexp
1162 (backward-char 1)
1163 (setq entry ""))
1164 (setq entry-start (point))
1165 (re-search-forward "\^M\\|\n" nil t)
1166 (while (looking-at " \\|\^I")
1167 (re-search-forward "\^M\\|\n" nil t))
1168 (backward-char 1)
e6c3bfbc 1169 (setq entry (buffer-substring-no-properties entry-start (point)))
0808d911
ER
1170 (while (string-match "[\^M]" entry)
1171 (aset entry (match-beginning 0) ?\n )))
1172 (let ((diary-entry (diary-sexp-entry sexp entry date)))
1173 (if diary-entry
1174 (subst-char-in-region line-start (point) ?\^M ?\n t))
a14723d9 1175 (add-to-diary-list date diary-entry specifier)
0808d911
ER
1176 (setq entry-found (or entry-found diary-entry)))))
1177 entry-found))
1178
1179(defun diary-sexp-entry (sexp entry date)
1180 "Process a SEXP diary ENTRY for DATE."
1181 (let ((result (if calendar-debug-sexp
1182 (let ((stack-trace-on-error t))
1183 (eval (car (read-from-string sexp))))
1184 (condition-case nil
1185 (eval (car (read-from-string sexp)))
1186 (error
1187 (beep)
1188 (message "Bad sexp at line %d in %s: %s"
1189 (save-excursion
1190 (save-restriction
1191 (narrow-to-region 1 (point))
1192 (goto-char (point-min))
1193 (let ((lines 1))
1194 (while (re-search-forward "\n\\|\^M" nil t)
1195 (setq lines (1+ lines)))
1196 lines)))
1197 diary-file sexp)
1198 (sleep-for 2))))))
1199 (if (stringp result)
1200 result
1201 (if result
1202 entry
1203 nil))))
1204
1205(defun diary-date (month day year)
1206 "Specific date(s) diary entry.
1207Entry applies if date is MONTH, DAY, YEAR if `european-calendar-style' is nil,
1208and DAY, MONTH, YEAR if `european-calendar-style' is t. DAY, MONTH, and YEAR
1209can be lists of integers, the constant t, or an integer. The constant t means
1210all values."
1211 (let* ((dd (if european-calendar-style
1212 month
1213 day))
1214 (mm (if european-calendar-style
1215 day
1216 month))
1217 (m (extract-calendar-month date))
1218 (y (extract-calendar-year date))
1219 (d (extract-calendar-day date)))
1220 (if (and
1221 (or (and (listp dd) (memq d dd))
1222 (equal d dd)
1223 (eq dd t))
1224 (or (and (listp mm) (memq m mm))
1225 (equal m mm)
1226 (eq mm t))
1227 (or (and (listp year) (memq y year))
1228 (equal y year)
1229 (eq year t)))
1230 entry)))
1231
1232(defun diary-block (m1 d1 y1 m2 d2 y2)
1233 "Block diary entry.
1234Entry applies if date is between two dates. Order of the parameters is
1235M1, D1, Y1, M2, D2, Y2 `european-calendar-style' is nil, and
1236D1, M1, Y1, D2, M2, Y2 if `european-calendar-style' is t."
1237 (let ((date1 (calendar-absolute-from-gregorian
1238 (if european-calendar-style
1239 (list d1 m1 y1)
1240 (list m1 d1 y1))))
1241 (date2 (calendar-absolute-from-gregorian
1242 (if european-calendar-style
1243 (list d2 m2 y2)
1244 (list m2 d2 y2))))
1245 (d (calendar-absolute-from-gregorian date)))
1246 (if (and (<= date1 d) (<= d date2))
1247 entry)))
1248
9b58d144 1249(defun diary-float (month dayname n &optional day)
0808d911
ER
1250 "Floating diary entry--entry applies if date is the nth dayname of month.
1251Parameters are MONTH, DAYNAME, N. MONTH can be a list of months, the constant
1252t, or an integer. The constant t means all months. If N is negative, count
9b58d144
RS
1253backward from the end of the month.
1254
1255An optional parameter DAY means the Nth DAYNAME on or after/before MONTH DAY."
1256;; This is messy because the diary entry may apply, but the date on which it
1257;; is based can be in a different month/year. For example, asking for the
1258;; first Monday after December 30. For large values of |n| the problem is
1259;; more grotesque.
1260 (and (= dayname (calendar-day-of-week date))
1261 (let* ((m (extract-calendar-month date))
1262 (d (extract-calendar-day date))
1263 (y (extract-calendar-year date))
1264 (limit; last (n>0) or first (n<0) possible base date for entry
1265 (calendar-nth-named-absday (- n) dayname m y d))
1266 (last-abs (if (> n 0) limit (+ limit 6)))
1267 (first-abs (if (> n 0) (- limit 6) limit))
1268 (last (calendar-gregorian-from-absolute last-abs))
1269 (first (calendar-gregorian-from-absolute first-abs))
1270 ; m1, d1 is first possible base date
1271 (m1 (extract-calendar-month first))
1272 (d1 (extract-calendar-day first))
1273 (y1 (extract-calendar-year first))
1274 ; m2, d2 is last possible base date
1275 (m2 (extract-calendar-month last))
1276 (d2 (extract-calendar-day last))
1277 (y2 (extract-calendar-year last)))
9f596b33
RS
1278 (if (or (and (= m1 m2) ; only possible base dates in one month
1279 (or (and (listp month) (memq m1 month))
1280 (eq month t)
1281 (= m1 month))
1282 (let ((d (or day (if (> n 0)
1283 1
1284 (calendar-last-day-of-month m1 y1)))))
1285 (and (<= d1 d) (<= d d2))))
1286 ;; only possible base dates straddle two months
1287 (and (< m1 m2)
1288 (or
1289 ;; m1, d1 works is a base date
1290 (and
1291 (or (and (listp month) (memq m1 month))
1292 (eq month t)
1293 (= m1 month))
1294 (<= d1 (or day (if (> n 0)
1295 1
1296 (calendar-last-day-of-month m1 y1)))))
1297 ;; m2, d2 works is a base date
1298 (and (or (and (listp month) (memq m2 month))
1299 (eq month t)
1300 (= m2 month))
1301 (<= (or day (if (> n 0)
1302 1
1303 (calendar-last-day-of-month m2 y2)))
1304 d2)))))
1305 entry))))
0808d911
ER
1306
1307(defun diary-anniversary (month day year)
1308 "Anniversary diary entry.
1309Entry applies if date is the anniversary of MONTH, DAY, YEAR if
1310`european-calendar-style' is nil, and DAY, MONTH, YEAR if
1311`european-calendar-style' is t. Diary entry can contain `%d' or `%d%s'; the
1312%d will be replaced by the number of years since the MONTH DAY, YEAR and the
1313%s will be replaced by the ordinal ending of that number (that is, `st', `nd',
1314`rd' or `th', as appropriate. The anniversary of February 29 is considered
1315to be March 1 in non-leap years."
1316 (let* ((d (if european-calendar-style
1317 month
1318 day))
1319 (m (if european-calendar-style
1320 day
1321 month))
1322 (y (extract-calendar-year date))
1323 (diff (- y year)))
1324 (if (and (= m 2) (= d 29) (not (calendar-leap-year-p y)))
1325 (setq m 3
1326 d 1))
1327 (if (and (> diff 0) (calendar-date-equal (list m d y) date))
1328 (format entry diff (diary-ordinal-suffix diff)))))
1329
1330(defun diary-cyclic (n month day year)
1331 "Cycle diary entry--entry applies every N days starting at MONTH, DAY, YEAR.
1332If `european-calendar-style' is t, parameters are N, DAY, MONTH, YEAR.
1333ENTRY can contain `%d' or `%d%s'; the %d will be replaced by the number of
1334years since the MONTH DAY, YEAR and the %s will be replaced by the ordinal
1335ending of that number (that is, `st', `nd', `rd' or `th', as appropriate."
1336 (let* ((d (if european-calendar-style
1337 month
1338 day))
1339 (m (if european-calendar-style
1340 day
1341 month))
1342 (diff (- (calendar-absolute-from-gregorian date)
1343 (calendar-absolute-from-gregorian
1344 (list m d year))))
1345 (cycle (/ diff n)))
1346 (if (and (>= diff 0) (zerop (% diff n)))
1347 (format entry cycle (diary-ordinal-suffix cycle)))))
1348
1349(defun diary-ordinal-suffix (n)
1350 "Ordinal suffix for N. (That is, `st', `nd', `rd', or `th', as appropriate.)"
1351 (if (or (memq (% n 100) '(11 12 13))
1352 (< 3 (% n 10)))
1353 "th"
1354 (aref ["th" "st" "nd" "rd"] (% n 10))))
1355
1356(defun diary-day-of-year ()
1357 "Day of year and number of days remaining in the year of date diary entry."
1358 (calendar-day-of-year-string date))
1359
5e11a170 1360(defcustom diary-remind-message
0808d911
ER
1361 '("Reminder: Only "
1362 (if (= 0 (% days 7))
1363 (concat (int-to-string (/ days 7)) (if (= 7 days) " week" " weeks"))
1364 (concat (int-to-string days) (if (= 1 days) " day" " days")))
1365 " until "
1366 diary-entry)
1367 "*Pseudo-pattern giving form of reminder messages in the fancy diary
1368display.
1369
1370Used by the function `diary-remind', a pseudo-pattern is a list of
1371expressions that can involve the keywords `days' (a number), `date' (a list of
5e11a170
RS
1372month, day, year), and `diary-entry' (a string)."
1373 :type 'sexp
1374 :group 'diary)
0808d911
ER
1375
1376(defun diary-remind (sexp days &optional marking)
1377 "Provide a reminder of a diary entry.
1378SEXP is a diary-sexp. DAYS is either a single number or a list of numbers
1379indicating the number(s) of days before the event that the warning(s) should
1380occur on. If the current date is (one of) DAYS before the event indicated by
1381SEXP, then a suitable message (as specified by `diary-remind-message' is
1382returned.
1383
1384In addition to the reminders beforehand, the diary entry also appears on
1385the date itself.
1386
1387If optional parameter MARKING is non-nil then the reminders are marked on the
1388calendar. Marking of reminders is independent of whether the entry itself is
1389a marking or nonmarking one."
1390 (let ((diary-entry))
1391 (if (or (not marking-diary-entries) marking)
1392 (cond
1393 ((integerp days)
1394 (let ((date (calendar-gregorian-from-absolute
1395 (+ (calendar-absolute-from-gregorian date) days))))
1396 (if (setq diary-entry (eval sexp))
1397 (setq diary-entry (mapconcat 'eval diary-remind-message "")))))
1398 ((and (listp days) days)
1399 (setq diary-entry (diary-remind sexp (car days) marking))
1400 (if (not diary-entry)
1401 (setq diary-entry (diary-remind sexp (cdr days) marking))))))
1402 (or diary-entry
1403 (and (or (not marking-diary-entries) marking-diary-entry)
1404 (eval sexp)))))
1405
a14723d9
KH
1406(defun add-to-diary-list (date string specifier)
1407 "Add the entry (DATE STRING SPECIFIER) to `diary-entries-list'.
0808d911
ER
1408Do nothing if DATE or STRING is nil."
1409 (and date string
1410 (setq diary-entries-list
a14723d9 1411 (append diary-entries-list (list (list date string specifier))))))
0808d911
ER
1412
1413(defun make-diary-entry (string &optional nonmarking file)
1414 "Insert a diary entry STRING which may be NONMARKING in FILE.
1415If omitted, NONMARKING defaults to nil and FILE defaults to diary-file."
1416 (find-file-other-window
1417 (substitute-in-file-name (if file file diary-file)))
1418 (goto-char (point-max))
1419 (insert
1420 (if (bolp) "" "\n")
1421 (if nonmarking diary-nonmarking-symbol "")
1422 string " "))
1423
1424(defun insert-diary-entry (arg)
1425 "Insert a diary entry for the date indicated by point.
1426Prefix arg will make the entry nonmarking."
1427 (interactive "P")
1428 (make-diary-entry (calendar-date-string (calendar-cursor-to-date t) t t)
1429 arg))
1430
1431(defun insert-weekly-diary-entry (arg)
1432 "Insert a weekly diary entry for the day of the week indicated by point.
1433Prefix arg will make the entry nonmarking."
1434 (interactive "P")
1435 (make-diary-entry (calendar-day-name (calendar-cursor-to-date t))
1436 arg))
1437
1438(defun insert-monthly-diary-entry (arg)
1439 "Insert a monthly diary entry for the day of the month indicated by point.
1440Prefix arg will make the entry nonmarking."
1441 (interactive "P")
1442 (let* ((calendar-date-display-form
1443 (if european-calendar-style
1444 '(day " * ")
1445 '("* " day))))
1446 (make-diary-entry (calendar-date-string (calendar-cursor-to-date t) t)
1447 arg)))
1448
1449(defun insert-yearly-diary-entry (arg)
1450 "Insert an annual diary entry for the day of the year indicated by point.
1451Prefix arg will make the entry nonmarking."
1452 (interactive "P")
1453 (let* ((calendar-date-display-form
1454 (if european-calendar-style
1455 '(day " " monthname)
1456 '(monthname " " day))))
1457 (make-diary-entry (calendar-date-string (calendar-cursor-to-date t) t)
1458 arg)))
1459
1460(defun insert-anniversary-diary-entry (arg)
1461 "Insert an anniversary diary entry for the date given by point.
1462Prefix arg will make the entry nonmarking."
1463 (interactive "P")
1464 (let* ((calendar-date-display-form
1465 (if european-calendar-style
1466 '(day " " month " " year)
1467 '(month " " day " " year))))
1468 (make-diary-entry
1469 (format "%s(diary-anniversary %s)"
1470 sexp-diary-entry-symbol
1471 (calendar-date-string (calendar-cursor-to-date t) nil t))
1472 arg)))
1473
1474(defun insert-block-diary-entry (arg)
1475 "Insert a block diary entry for the days between the point and marked date.
1476Prefix arg will make the entry nonmarking."
1477 (interactive "P")
1478 (let* ((calendar-date-display-form
1479 (if european-calendar-style
1480 '(day " " month " " year)
1481 '(month " " day " " year)))
1482 (cursor (calendar-cursor-to-date t))
1483 (mark (or (car calendar-mark-ring)
1484 (error "No mark set in this buffer")))
1485 (start)
1486 (end))
1487 (if (< (calendar-absolute-from-gregorian mark)
1488 (calendar-absolute-from-gregorian cursor))
1489 (setq start mark
1490 end cursor)
1491 (setq start cursor
1492 end mark))
1493 (make-diary-entry
1494 (format "%s(diary-block %s %s)"
1495 sexp-diary-entry-symbol
1496 (calendar-date-string start nil t)
1497 (calendar-date-string end nil t))
1498 arg)))
1499
1500(defun insert-cyclic-diary-entry (arg)
1501 "Insert a cyclic diary entry starting at the date given by point.
1502Prefix arg will make the entry nonmarking."
1503 (interactive "P")
1504 (let* ((calendar-date-display-form
1505 (if european-calendar-style
1506 '(day " " month " " year)
1507 '(month " " day " " year))))
1508 (make-diary-entry
1509 (format "%s(diary-cyclic %d %s)"
1510 sexp-diary-entry-symbol
1511 (calendar-read "Repeat every how many days: "
1512 '(lambda (x) (> x 0)))
1513 (calendar-date-string (calendar-cursor-to-date t) nil t))
1514 arg)))
1515
e4ca7ef9 1516(provide 'diary-lib)
0808d911 1517
e4ca7ef9 1518;;; diary-lib.el ends here