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