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