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