1 ;;; diary-lib.el --- diary functions
3 ;; Copyright (C) 1989, 1990, 1992, 1993, 1994, 1995 Free Software
6 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
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)
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.
28 ;; This collection of functions implements the diary features as described
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
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."
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
)
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."
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 d-file
)
76 "Prepare and display buffer of diary entries from an alternative diary file.
77 Prompts for a file name and searches that file for entries that match ARG
78 days starting with the date indicated by the cursor position in the displayed
79 three-month calendar."
81 (list (cond ((null current-prefix-arg
) 1)
82 ((listp current-prefix-arg
) (car current-prefix-arg
))
83 (t current-prefix-arg
))
84 (read-file-name "Enter diary file name: " default-directory nil t
)))
85 (let ((diary-file d-file
))
86 (view-diary-entries arg
)))
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'.")
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'.")
97 (autoload 'diary-french-date
"cal-french"
98 "French calendar equivalent of date diary entry.")
100 (autoload 'diary-mayan-date
"cal-mayan"
101 "Mayan calendar equivalent of date diary entry.")
103 (autoload 'diary-iso-date
"cal-iso"
104 "ISO calendar equivalent of date diary entry.")
106 (autoload 'diary-julian-date
"cal-julian"
107 "Julian calendar equivalent of date diary entry.")
109 (autoload 'diary-astro-day-number
"cal-julian"
110 "Astronomical (Julian) day number diary entry.")
112 (autoload 'diary-chinese-date
"cal-china"
113 "Chinese calendar equivalent of date diary entry.")
115 (autoload 'diary-islamic-date
"cal-islam"
116 "Islamic calendar equivalent of date diary entry.")
118 (autoload 'list-islamic-diary-entries
"cal-islam"
119 "Add any Islamic date entries from the diary file to `diary-entries-list'.")
121 (autoload 'mark-islamic-diary-entries
"cal-islam"
122 "Mark days in the calendar window that have Islamic date diary entries.")
124 (autoload 'mark-islamic-calendar-date-pattern
"cal-islam"
125 "Mark dates in calendar window that conform to Islamic date MONTH/DAY/YEAR.")
127 (autoload 'diary-hebrew-date
"cal-hebrew"
128 "Hebrew calendar equivalent of date diary entry.")
130 (autoload 'diary-omer
"cal-hebrew"
131 "Omer count diary entry.")
133 (autoload 'diary-yahrzeit
"cal-hebrew"
134 "Yahrzeit diary entry--entry applies if date is yahrzeit or the day before.")
136 (autoload 'diary-parasha
"cal-hebrew"
137 "Parasha diary entry--entry applies if date is a Saturday.")
139 (autoload 'diary-rosh-hodesh
"cal-hebrew"
140 "Rosh Hodesh diary entry.")
142 (autoload 'list-hebrew-diary-entries
"cal-hebrew"
143 "Add any Hebrew date entries from the diary file to `diary-entries-list'.")
145 (autoload 'mark-hebrew-diary-entries
"cal-hebrew"
146 "Mark days in the calendar window that have Hebrew date diary entries.")
148 (autoload 'mark-hebrew-calendar-date-pattern
"cal-hebrew"
149 "Mark dates in calendar window that conform to Hebrew date MONTH/DAY/YEAR.")
151 (autoload 'diary-coptic-date
"cal-coptic"
152 "Coptic calendar equivalent of date diary entry.")
154 (autoload 'diary-ethiopic-date
"cal-coptic"
155 "Ethiopic calendar equivalent of date diary entry.")
157 (autoload 'diary-persian-date
"cal-persia"
158 "Persian calendar equivalent of date diary entry.")
160 (autoload 'diary-phases-of-moon
"lunar" "Moon phases diary entry.")
162 (autoload 'diary-sunrise-sunset
"solar"
163 "Local time of sunrise and sunset as a diary entry.")
165 (autoload 'diary-sabbath-candles
"solar"
166 "Local time of candle lighting diary entry--applies if date is a Friday.
167 No diary entry if there is no sunset on that date.")
169 (defvar diary-syntax-table
(copy-syntax-table (standard-syntax-table))
170 "The syntax table used when parsing dates in the diary file.
171 It is the standard syntax table used in Fundamental mode, but with the
172 syntax of `*' changed to be a word constituent.")
174 (modify-syntax-entry ?
* "w" diary-syntax-table
)
175 (modify-syntax-entry ?
: "w" diary-syntax-table
)
177 (defvar diary-modified
)
178 (defvar diary-entries-list
)
179 (defvar displayed-year
)
180 (defvar displayed-month
)
186 (defvar original-date
)
188 (defun diary-attrtype-convert (attrvalue type
)
189 "Convert the attrvalue from a string to the appropriate type for using
190 in a face description"
192 (setq ret
(cond ((eq type
'string
) attrvalue
)
193 ((eq type
'symbol
) (read attrvalue
))
194 ((eq type
'int
) (string-to-int attrvalue
))
195 ((eq type
'stringtnil
)
196 (cond ((string= "t" attrvalue
) t
)
197 ((string= "nil" attrvalue
) nil
)
200 (cond ((string= "t" attrvalue
) t
)
201 ((string= "nil" attrvalue
) nil
)))))
202 ; (message "(%s)[%s]=[%s]" (print type) attrvalue ret)
206 (defun diary-pull-attrs (entry fileglobattrs
)
207 "Pull the face-related attributes off the entry, merge with the
208 fileglobattrs, and return the (possibly modified) entry and face
209 data in a list of attrname attrvalue values.
210 The entry will be modified to drop all tags that are used for face matching.
211 If entry is nil, then the fileglobattrs are being searched for,
212 the fileglobattrs variable is ignored, and
213 diary-glob-file-regexp-prefix is prepended to the regexps before each
216 (let (regexp regnum attrname attr-list attrname attrvalue type
)
220 attr-list diary-face-attrs
)
222 (goto-char (point-min))
223 (setq attr
(car attr-list
)
226 attrname
(nth 2 attr
)
228 regexp
(concat diary-glob-file-regexp-prefix regexp
))
230 (if (re-search-forward regexp
(point-max) t
)
231 (setq attrvalue
(buffer-substring-no-properties
232 (match-beginning regnum
)
233 (match-end regnum
))))
235 (setq attrvalue
(diary-attrtype-convert attrvalue type
)))
236 (setq ret-attr
(append ret-attr
(list attrname attrvalue
))))
237 (setq attr-list
(cdr attr-list
)))
238 (setq fileglobattrs ret-attr
))
240 (setq ret-attr fileglobattrs
241 attr-list diary-face-attrs
)
243 (goto-char (point-min))
244 (setq attr
(car attr-list
)
247 attrname
(nth 2 attr
)
250 (if (string-match regexp entry
)
252 (setq attrvalue
(substring-no-properties entry
253 (match-beginning regnum
)
255 (setq entry
(replace-match "" t t entry
))))
257 (setq attrvalue
(diary-attrtype-convert attrvalue type
)))
258 (setq ret-attr
(append ret-attr
(list attrname attrvalue
))))
259 (setq attr-list
(cdr attr-list
)))))))
260 (list entry ret-attr
))
264 (defun list-diary-entries (date number
)
265 "Create and display a buffer containing the relevant lines in diary-file.
266 The arguments are DATE and NUMBER; the entries selected are those
267 for NUMBER days starting with date DATE. The other entries are hidden
268 using selective display.
270 Returns a list of all relevant diary entries found, if any, in order by date.
271 The list entries have the form ((month day year) string specifier) where
272 \(month day year) is the date of the entry, string is the entry text, and
273 specifier is the applicability. If the variable `diary-list-include-blanks'
274 is t, this list includes a dummy diary entry consisting of the empty string)
275 for a date with no diary entries.
277 After the list is prepared, the hooks `nongregorian-diary-listing-hook',
278 `list-diary-entries-hook', `diary-display-hook', and `diary-hook' are run.
279 These hooks have the following distinct roles:
281 `nongregorian-diary-listing-hook' can cull dates from the diary
282 and each included file. Usually used for Hebrew or Islamic
283 diary entries in files. Applied to *each* file.
285 `list-diary-entries-hook' adds or manipulates diary entries from
286 external sources. Used, for example, to include diary entries
287 from other files or to sort the diary entries. Invoked *once* only,
288 before the display hook is run.
290 `diary-display-hook' does the actual display of information. If this is
291 nil, simple-diary-display will be used. Use add-hook to set this to
292 fancy-diary-display, if desired. If you want no diary display, use
293 add-hook to set this to ignore.
295 `diary-hook' is run last. This can be used for an appointment
296 notification function."
299 (let* ((original-date date
);; save for possible use in the hooks
300 old-diary-syntax-table
303 (date-string (calendar-date-string date
))
304 (d-file (substitute-in-file-name diary-file
)))
305 (message "Preparing diary...")
307 (let ((diary-buffer (find-buffer-visiting d-file
)))
308 (if (not diary-buffer
)
309 (set-buffer (find-file-noselect d-file t
))
310 (set-buffer diary-buffer
)
311 (or (verify-visited-file-modtime diary-buffer
)
312 (revert-buffer t t
))))
313 (setq file-glob-attrs
(nth 1 (diary-pull-attrs nil
"")))
314 (setq selective-display t
)
315 (setq selective-display-ellipses nil
)
316 (setq old-diary-syntax-table
(syntax-table))
317 (set-syntax-table diary-syntax-table
)
319 (let ((buffer-read-only nil
)
320 (diary-modified (buffer-modified-p))
321 (mark (regexp-quote diary-nonmarking-symbol
)))
322 ;; First and last characters must be ^M or \n for
323 ;; selective display to work properly
324 (goto-char (1- (point-max)))
325 (if (not (looking-at "\^M\\|\n"))
327 (goto-char (point-max))
329 (goto-char (point-min))
330 (if (not (looking-at "\^M\\|\n"))
332 (subst-char-in-region (point-min) (point-max) ?
\n ?\^M t
)
333 (calendar-for-loop i from
1 to number do
334 (let ((d diary-date-forms
)
335 (month (extract-calendar-month date
))
336 (day (extract-calendar-day date
))
337 (year (extract-calendar-year date
))
338 (entry-found (list-sexp-diary-entries date
)))
341 ((date-form (if (equal (car (car d
)) 'backup
)
344 (backup (equal (car (car d
)) 'backup
))
347 (calendar-day-name date
) "\\|"
348 (substring (calendar-day-name date
) 0 3) ".?"))
352 (calendar-month-name month
) "\\|"
353 (substring (calendar-month-name month
) 0 3) ".?"))
354 (month (concat "\\*\\|0*" (int-to-string month
)))
355 (day (concat "\\*\\|0*" (int-to-string day
)))
358 "\\*\\|0*" (int-to-string year
)
359 (if abbreviated-calendar-year
360 (concat "\\|" (format "%02d" (% year
100)))
364 "\\(\\`\\|\^M\\|\n\\)" mark
"?\\("
365 (mapconcat 'eval date-form
"\\)\\(")
367 (case-fold-search t
))
368 (goto-char (point-min))
369 (while (re-search-forward regexp nil t
)
370 (if backup
(re-search-backward "\\<" nil t
))
371 (if (and (or (char-equal (preceding-char) ?\^M
)
372 (char-equal (preceding-char) ?
\n))
373 (not (looking-at " \\|\^I")))
374 ;; Diary entry that consists only of date.
376 ;; Found a nonempty diary entry--make it visible and
377 ;; add it to the list.
379 (let ((entry-start (point))
381 (re-search-backward "\^M\\|\n\\|\\`")
382 (setq date-start
(point))
383 (re-search-forward "\^M\\|\n" nil t
2)
384 (while (looking-at " \\|\^I")
385 (re-search-forward "\^M\\|\n" nil t
))
387 (subst-char-in-region date-start
389 (setq entry
(buffer-substring entry-start
(point))
390 temp
(diary-pull-attrs entry file-glob-attrs
)
397 (1+ date-start
) (1- entry-start
))
398 (copy-marker entry-start
) marks
)))))
401 (not diary-list-include-blanks
)
402 (setq diary-entries-list
403 (append diary-entries-list
404 (list (list date
"" "" "" "")))))
406 (calendar-gregorian-from-absolute
407 (1+ (calendar-absolute-from-gregorian date
))))
408 (setq entry-found nil
)))
409 (set-buffer-modified-p diary-modified
))
410 (set-syntax-table old-diary-syntax-table
))
411 (goto-char (point-min))
412 (run-hooks 'nongregorian-diary-listing-hook
413 'list-diary-entries-hook
)
414 (if diary-display-hook
415 (run-hooks 'diary-display-hook
)
416 (simple-diary-display))
417 (run-hooks 'diary-hook
)
418 diary-entries-list
))))
420 (defun include-other-diary-files ()
421 "Include the diary entries from other diary files with those of diary-file.
422 This function is suitable for use in `list-diary-entries-hook';
423 it enables you to use shared diary files together with your own.
424 The files included are specified in the diaryfile by lines of this form:
425 #include \"filename\"
426 This is recursive; that is, #include directives in diary files thus included
427 are obeyed. You can change the `#include' to some other string by
428 changing the variable `diary-include-string'."
429 (goto-char (point-min))
430 (while (re-search-forward
432 "\\(\\`\\|\^M\\|\n\\)"
433 (regexp-quote diary-include-string
)
436 (let* ((diary-file (substitute-in-file-name
437 (buffer-substring-no-properties
438 (match-beginning 2) (match-end 2))))
439 (diary-list-include-blanks nil
)
440 (list-diary-entries-hook 'include-other-diary-files
)
441 (diary-display-hook 'ignore
)
443 (d-buffer (find-buffer-visiting diary-file
))
444 (diary-modified (if d-buffer
446 (set-buffer d-buffer
)
447 (buffer-modified-p)))))
448 (if (file-exists-p diary-file
)
449 (if (file-readable-p diary-file
)
451 (setq diary-entries-list
452 (append diary-entries-list
453 (list-diary-entries original-date number
)))
455 (set-buffer (find-buffer-visiting diary-file
))
456 (let ((inhibit-read-only t
))
457 (subst-char-in-region (point-min) (point-max) ?\^M ?
\n t
))
458 (setq selective-display nil
)
459 (set-buffer-modified-p diary-modified
)))
461 (message "Can't read included diary file %s" diary-file
)
464 (message "Can't find included diary file %s" diary-file
)
466 (goto-char (point-min)))
468 (defun simple-diary-display ()
469 "Display the diary buffer if there are any relevant entries or holidays."
470 (let* ((holiday-list (if holidays-in-diary-buffer
471 (check-calendar-holidays original-date
)))
472 (msg (format "No diary entries for %s %s"
473 (concat date-string
(if holiday-list
":" ""))
474 (mapconcat 'identity holiday-list
"; "))))
475 (calendar-set-mode-line
476 (concat "Diary for " date-string
477 (if holiday-list
": " "")
478 (mapconcat 'identity holiday-list
"; ")))
479 (if (or (not diary-entries-list
)
480 (and (not (cdr diary-entries-list
))
481 (string-equal (car (cdr (car diary-entries-list
))) "")))
482 (if (<= (length msg
) (frame-width))
484 (set-buffer (get-buffer-create holiday-buffer
))
485 (setq buffer-read-only nil
)
486 (calendar-set-mode-line date-string
)
488 (insert (mapconcat 'identity holiday-list
"\n"))
489 (goto-char (point-min))
490 (set-buffer-modified-p nil
)
491 (setq buffer-read-only t
)
492 (display-buffer holiday-buffer
)
493 (message "No diary entries for %s" date-string
))
494 (display-buffer (find-buffer-visiting d-file
))
495 (message "Preparing diary...done"))))
497 (defface diary-button-face
'((((type pc
) (class color
))
498 (:foreground
"lightblue")))
499 "Default face used for buttons."
503 (define-button-type 'diary-entry
504 'action
#'diary-goto-entry
505 'face
#'diary-button-face
)
507 (defun diary-goto-entry (button)
508 (let ((marker (button-get button
'marker
)))
510 (pop-to-buffer (marker-buffer marker
))
511 (goto-char (marker-position marker
)))))
513 (defun fancy-diary-display ()
514 "Prepare a diary buffer with relevant entries in a fancy, noneditable form.
515 This function is provided for optional use as the `diary-display-hook'."
516 (save-excursion;; Turn off selective-display in the diary file's buffer.
517 (set-buffer (find-buffer-visiting (substitute-in-file-name diary-file
)))
518 (let ((diary-modified (buffer-modified-p)))
519 (subst-char-in-region (point-min) (point-max) ?\^M ?
\n t
)
520 (setq selective-display nil
)
521 (kill-local-variable 'mode-line-format
)
522 (set-buffer-modified-p diary-modified
)))
523 (if (or (not diary-entries-list
)
524 (and (not (cdr diary-entries-list
))
525 (string-equal (car (cdr (car diary-entries-list
))) "")))
526 (let* ((holiday-list (if holidays-in-diary-buffer
527 (check-calendar-holidays original-date
)))
528 (msg (format "No diary entries for %s %s"
529 (concat date-string
(if holiday-list
":" ""))
530 (mapconcat 'identity holiday-list
"; "))))
531 (if (<= (length msg
) (frame-width))
533 (set-buffer (get-buffer-create holiday-buffer
))
534 (setq buffer-read-only nil
)
535 (calendar-set-mode-line date-string
)
537 (insert (mapconcat 'identity holiday-list
"\n"))
538 (goto-char (point-min))
539 (set-buffer-modified-p nil
)
540 (setq buffer-read-only t
)
541 (display-buffer holiday-buffer
)
542 (message "No diary entries for %s" date-string
)))
543 (save-excursion;; Prepare the fancy diary buffer.
544 (set-buffer (make-fancy-diary-buffer))
545 (setq buffer-read-only nil
)
546 (let ((entry-list diary-entries-list
)
548 (holiday-list-last-month 1)
549 (holiday-list-last-year 1)
552 (if (not (calendar-date-equal date
(car (car entry-list
))))
554 (setq date
(car (car entry-list
)))
555 (and holidays-in-diary-buffer
556 (calendar-date-compare
557 (list (list holiday-list-last-month
558 (calendar-last-day-of-month
559 holiday-list-last-month
560 holiday-list-last-year
)
561 holiday-list-last-year
))
563 ;; We need to get the holidays for the next 3 months.
564 (setq holiday-list-last-month
565 (extract-calendar-month date
))
566 (setq holiday-list-last-year
567 (extract-calendar-year date
))
568 (increment-calendar-month
569 holiday-list-last-month holiday-list-last-year
1)
571 (let ((displayed-month holiday-list-last-month
)
572 (displayed-year holiday-list-last-year
))
573 (calendar-holiday-list)))
574 (increment-calendar-month
575 holiday-list-last-month holiday-list-last-year
1))
576 (let* ((date-string (calendar-date-string date
))
578 (let ((h holiday-list
)
580 ;; Make a list of all holidays for date.
582 (if (calendar-date-equal date
(car (car h
)))
583 (setq d
(append d
(cdr (car h
)))))
586 (insert (if (= (point) (point-min)) "" ?
\n) date-string
)
587 (if date-holiday-list
(insert ": "))
588 (let* ((l (current-column))
590 (insert (mapconcat (lambda (x)
591 (if (< longest
(length x
))
592 (setq longest
(length x
)))
595 (concat "\n" (make-string l ?
))))
596 (insert ?
\n (make-string (+ l longest
) ?
=) ?
\n)))))
598 (setq entry
(car (cdr (car entry-list
))))
599 (if (< 0 (length entry
))
601 (if (nth 3 (car entry-list
))
602 (insert-button (concat entry
"\n")
603 'marker
(nth 3 (car entry-list
))
607 (setq marks
(nth 4 (car entry-list
)))
608 (setq temp-face
(make-symbol (apply 'concat
"temp-face-" (mapcar '(lambda (sym) (if (not (stringp sym
)) (symbol-name sym
) sym
)) marks
))))
609 (make-face temp-face
)
610 ;; Remove :face info from the marks, copy the face info into temp-face
611 (setq faceinfo marks
)
612 (while (setq faceinfo
(memq :face faceinfo
))
613 (copy-face (read (nth 1 faceinfo
)) temp-face
)
614 (setcar faceinfo nil
)
615 (setcar (cdr faceinfo
) nil
))
616 (setq marks
(delq nil marks
))
617 ;; Apply the font aspects
618 (apply 'set-face-attribute temp-face nil marks
)
619 (search-backward entry
)
621 (make-overlay (match-beginning 0) (match-end 0)) 'face temp-face
))
623 (setq entry-list
(cdr entry-list
))))
624 (set-buffer-modified-p nil
)
625 (goto-char (point-min))
626 (setq buffer-read-only t
)
627 (display-buffer fancy-diary-buffer
)
628 (fancy-diary-display-mode)
629 (message "Preparing diary...done"))))
631 (defun make-fancy-diary-buffer ()
632 "Create and return the initial fancy diary buffer."
634 (set-buffer (get-buffer-create fancy-diary-buffer
))
635 (setq buffer-read-only nil
)
636 (make-local-variable 'mode-line-format
)
637 (calendar-set-mode-line "Diary Entries")
639 (set-buffer-modified-p nil
)
640 (setq buffer-read-only t
)
641 (get-buffer fancy-diary-buffer
)))
643 (defun print-diary-entries ()
644 "Print a hard copy of the diary display.
646 If the simple diary display is being used, prepare a temp buffer with the
647 visible lines of the diary buffer, add a heading line composed from the mode
648 line, print the temp buffer, and destroy it.
650 If the fancy diary display is being used, just print the buffer.
652 The hooks given by the variable `print-diary-entries-hook' are called to do
653 the actual printing."
655 (if (bufferp (get-buffer fancy-diary-buffer
))
657 (set-buffer (get-buffer fancy-diary-buffer
))
658 (run-hooks 'print-diary-entries-hook
))
660 (find-buffer-visiting (substitute-in-file-name diary-file
))))
662 (let ((temp-buffer (get-buffer-create "*Printable Diary Entries*"))
665 (set-buffer diary-buffer
)
667 (if (not (stringp mode-line-format
))
669 (string-match "^-*\\([^-].*[^-]\\)-*$" mode-line-format
)
670 (substring mode-line-format
671 (match-beginning 1) (match-end 1))))
672 (copy-to-buffer temp-buffer
(point-min) (point-max))
673 (set-buffer temp-buffer
)
674 (while (re-search-forward "\^M.*$" nil t
)
676 (goto-char (point-min))
678 (make-string (length heading
) ?
=) "\n")
679 (run-hooks 'print-diary-entries-hook
)
680 (kill-buffer temp-buffer
)))
681 (error "You don't have a diary buffer!")))))
683 (defun show-all-diary-entries ()
684 "Show all of the diary entries in the diary file.
685 This function gets rid of the selective display of the diary file so that
686 all entries, not just some, are visible. If there is no diary buffer, one
689 (let ((d-file (substitute-in-file-name diary-file
)))
690 (if (and d-file
(file-exists-p d-file
))
691 (if (file-readable-p d-file
)
693 (let ((diary-buffer (find-buffer-visiting d-file
)))
694 (set-buffer (if diary-buffer
696 (find-file-noselect d-file t
)))
697 (let ((buffer-read-only nil
)
698 (diary-modified (buffer-modified-p)))
699 (subst-char-in-region (point-min) (point-max) ?\^M ?
\n t
)
700 (setq selective-display nil
)
701 (make-local-variable 'mode-line-format
)
702 (setq mode-line-format default-mode-line-format
)
703 (display-buffer (current-buffer))
704 (set-buffer-modified-p diary-modified
))))
705 (error "Your diary file is not readable!"))
706 (error "You don't have a diary file!"))))
710 (defcustom diary-mail-addr
711 (if (boundp 'user-mail-address
) user-mail-address nil
)
712 "*Email address that `diary-mail-entries' will send email to."
714 :type
'(choice string
(const nil
))
717 (defcustom diary-mail-days
7
718 "*Number of days for `diary-mail-entries' to check."
724 (defun diary-mail-entries (&optional ndays
)
725 "Send a mail message showing diary entries for next NDAYS days.
726 If no prefix argument is given, NDAYS is set to `diary-mail-days'.
728 You can call `diary-mail-entries' every night using an at/cron job.
729 For example, this script will run the program at 2am daily. Since
730 `emacs -batch' does not load your `.emacs' file, you must ensure that
731 all relevant variables are set, as done here.
734 # diary-rem.sh -- repeatedly run the Emacs diary-reminder
736 -eval \"(setq diary-mail-days 3 \\
737 european-calendar-style t \\
738 diary-mail-addr \\\"user@host.name\\\" )\" \\
739 -l diary-lib -f diary-mail-entries
740 at -f diary-rem.sh 0200 tomorrow
742 You may have to tweak the syntax of the `at' command to suit your
743 system. Alternatively, you can specify a cron entry:
744 0 1 * * * diary-rem.sh
745 to run it every morning at 1am."
747 (let* ((diary-display-hook 'fancy-diary-display
)
748 (text (progn (list-diary-entries (calendar-current-date)
749 (if ndays ndays diary-mail-days
))
750 (set-buffer fancy-diary-buffer
)
751 (buffer-substring (point-min) (point-max)))))
752 (compose-mail diary-mail-addr
753 (if (string-equal text
"")
755 (concat "Diary entries generated "
756 (calendar-date-string (calendar-current-date)))))
758 (funcall (get mail-user-agent
'sendfunc
))))
761 (defun diary-name-pattern (string-array &optional fullname
)
762 "Convert a STRING-ARRAY, an array of strings to a pattern.
763 The pattern will match any of the strings, either entirely or abbreviated
764 to three characters. An abbreviated form will match with or without a period;
765 If the optional FULLNAME is t, abbreviations will not match, just the full
768 (calendar-for-loop i from
0 to
(1- (length string-array
)) do
772 (if (string-equal pattern
"") "" "\\|")
773 (aref string-array i
)
778 (substring (aref string-array i
) 0 3) ".?")))))
781 (defvar marking-diary-entries nil
782 "True during the marking of diary entries, nil otherwise.")
784 (defvar marking-diary-entry nil
785 "True during the marking of diary entries, if current entry is marking.")
787 (defun mark-diary-entries ()
788 "Mark days in the calendar window that have diary entries.
789 Each entry in the diary file visible in the calendar window is marked.
790 After the entries are marked, the hooks `nongregorian-diary-marking-hook' and
791 `mark-diary-entries-hook' are run."
793 (setq mark-diary-entries-in-calendar t
)
794 (let (file-glob-attrs
796 (d-file (substitute-in-file-name diary-file
))
797 (marking-diary-entries t
))
798 (if (and d-file
(file-exists-p d-file
))
799 (if (file-readable-p d-file
)
801 (message "Marking diary entries...")
802 (set-buffer (find-file-noselect d-file t
))
803 (setq file-glob-attrs
(nth 1 (diary-pull-attrs nil
'())))
804 (let ((d diary-date-forms
)
805 (old-diary-syntax-table))
806 (setq old-diary-syntax-table
(syntax-table))
807 (set-syntax-table diary-syntax-table
)
810 ((date-form (if (equal (car (car d
)) 'backup
)
812 (car d
)));; ignore 'backup directive
813 (dayname (diary-name-pattern calendar-day-name-array
))
816 (diary-name-pattern calendar-month-name-array
)
818 (month "[0-9]+\\|\\*")
820 (year "[0-9]+\\|\\*")
821 (l (length date-form
))
822 (d-name-pos (- l
(length (memq 'dayname date-form
))))
823 (d-name-pos (if (/= l d-name-pos
) (+ 2 d-name-pos
)))
824 (m-name-pos (- l
(length (memq 'monthname date-form
))))
825 (m-name-pos (if (/= l m-name-pos
) (+ 2 m-name-pos
)))
826 (d-pos (- l
(length (memq 'day date-form
))))
827 (d-pos (if (/= l d-pos
) (+ 2 d-pos
)))
828 (m-pos (- l
(length (memq 'month date-form
))))
829 (m-pos (if (/= l m-pos
) (+ 2 m-pos
)))
830 (y-pos (- l
(length (memq 'year date-form
))))
831 (y-pos (if (/= l y-pos
) (+ 2 y-pos
)))
834 "\\(\\`\\|\^M\\|\n\\)\\("
835 (mapconcat 'eval date-form
"\\)\\(")
837 (case-fold-search t
))
838 (goto-char (point-min))
839 (while (re-search-forward regexp nil t
)
842 (buffer-substring-no-properties
843 (match-beginning d-name-pos
)
844 (match-end d-name-pos
))))
847 (buffer-substring-no-properties
848 (match-beginning m-name-pos
)
849 (match-end m-name-pos
))))
852 (buffer-substring-no-properties
853 (match-beginning m-pos
)
858 (buffer-substring-no-properties
859 (match-beginning d-pos
)
863 (buffer-substring-no-properties
864 (match-beginning y-pos
)
868 (if (and (= (length y-str
) 2)
869 abbreviated-calendar-year
)
871 (extract-calendar-year
872 (calendar-current-date)))
873 (y (+ (string-to-int y-str
)
875 (/ current-y
100)))))
876 (if (> (- y current-y
) 50)
878 (if (> (- current-y y
) 50)
881 (string-to-int y-str
))))
883 (setq entry
(buffer-substring-no-properties (point) (line-end-position))
884 temp
(diary-pull-attrs entry file-glob-attrs
)
886 marks
(nth 1 temp
))))
888 (mark-calendar-days-named
889 (cdr (assoc-ignore-case
890 (substring dd-name
0 3)
892 calendar-day-name-array
894 (lambda (x) (substring x
0 3))))) marks
)
896 (if (string-equal mm-name
"*")
899 (cdr (assoc-ignore-case
900 (substring mm-name
0 3)
902 calendar-month-name-array
904 (lambda (x) (substring x
0 3)))
906 (mark-calendar-date-pattern mm dd yy marks
))))
908 (mark-sexp-diary-entries)
909 (run-hooks 'nongregorian-diary-marking-hook
910 'mark-diary-entries-hook
)
911 (set-syntax-table old-diary-syntax-table
)
912 (message "Marking diary entries...done")))
913 (error "Your diary file is not readable!"))
914 (error "You don't have a diary file!"))))
916 (defun mark-sexp-diary-entries ()
917 "Mark days in the calendar window that have sexp diary entries.
918 Each entry in the diary file (or included files) visible in the calendar window
919 is marked. See the documentation for the function `list-sexp-diary-entries'."
920 (let* ((sexp-mark (regexp-quote sexp-diary-entry-symbol
))
921 (s-entry (concat "\\(\\`\\|\^M\\|\n\\)\\("
922 (regexp-quote sexp-mark
) "(\\)\\|\\("
923 (regexp-quote diary-nonmarking-symbol
)
924 (regexp-quote sexp-mark
) "(diary-remind\\)"))
931 (setq file-glob-attrs
(nth 1 (diary-pull-attrs nil
'())))
933 (set-buffer calendar-buffer
)
934 (setq m displayed-month
)
935 (setq y displayed-year
))
936 (increment-calendar-month m y -
1)
938 (calendar-absolute-from-gregorian (list m
1 y
)))
939 (increment-calendar-month m y
2)
941 (calendar-absolute-from-gregorian
942 (list m
(calendar-last-day-of-month m y
) y
)))
943 (goto-char (point-min))
944 (while (re-search-forward s-entry nil t
)
945 (if (char-equal (preceding-char) ?\
()
946 (setq marking-diary-entry t
)
947 (setq marking-diary-entry nil
))
948 (re-search-backward "(")
949 (let ((sexp-start (point))
955 (setq sexp
(buffer-substring-no-properties sexp-start
(point)))
957 (re-search-backward "\^M\\|\n\\|\\`")
958 (setq line-start
(point)))
960 (if (and (or (char-equal (preceding-char) ?\^M
)
961 (char-equal (preceding-char) ?
\n))
962 (not (looking-at " \\|\^I")))
963 (progn;; Diary entry consists only of the sexp
966 (setq entry-start
(point))
968 (re-search-forward "\^M\\|\n" nil t
)
969 (while (looking-at " \\|\^I")
970 (or (re-search-forward "\^M\\|\n" nil t
)
971 (re-search-forward "$" nil t
)))
972 (if (or (char-equal (preceding-char) ?\^M
)
973 (char-equal (preceding-char) ?
\n))
975 (setq entry
(buffer-substring-no-properties entry-start
(point)))
976 (while (string-match "[\^M]" entry
)
977 (aset entry
(match-beginning 0) ?
\n )))
978 (calendar-for-loop date from first-date to last-date do
979 (if (setq mark
(diary-sexp-entry sexp entry
980 (calendar-gregorian-from-absolute date
)))
982 (setq marks
(diary-pull-attrs entry file-glob-attrs
)
983 temp
(diary-pull-attrs entry file-glob-attrs
)
985 (mark-visible-calendar-date
986 (calendar-gregorian-from-absolute date
)
987 (if (< 0 (length marks
))
992 (defun mark-included-diary-files ()
993 "Mark the diary entries from other diary files with those of the diary file.
994 This function is suitable for use as the `mark-diary-entries-hook'; it enables
995 you to use shared diary files together with your own. The files included are
996 specified in the diary-file by lines of this form:
997 #include \"filename\"
998 This is recursive; that is, #include directives in diary files thus included
999 are obeyed. You can change the `#include' to some other string by
1000 changing the variable `diary-include-string'."
1001 (goto-char (point-min))
1002 (while (re-search-forward
1004 "\\(\\`\\|\^M\\|\n\\)"
1005 (regexp-quote diary-include-string
)
1006 " \"\\([^\"]*\\)\"")
1008 (let ((diary-file (substitute-in-file-name
1009 (buffer-substring-no-properties
1010 (match-beginning 2) (match-end 2))))
1011 (mark-diary-entries-hook 'mark-included-diary-files
))
1012 (if (file-exists-p diary-file
)
1013 (if (file-readable-p diary-file
)
1015 (mark-diary-entries)
1016 (kill-buffer (find-buffer-visiting diary-file
)))
1018 (message "Can't read included diary file %s" diary-file
)
1021 (message "Can't find included diary file %s" diary-file
)
1023 (goto-char (point-min)))
1025 (defun mark-calendar-days-named (dayname &optional color
)
1026 "Mark all dates in the calendar window that are day DAYNAME of the week.
1027 0 means all Sundays, 1 means all Mondays, and so on."
1029 (set-buffer calendar-buffer
)
1030 (let ((prev-month displayed-month
)
1031 (prev-year displayed-year
)
1032 (succ-month displayed-month
)
1033 (succ-year displayed-year
)
1036 (increment-calendar-month succ-month succ-year
1)
1037 (increment-calendar-month prev-month prev-year -
1)
1038 (setq day
(calendar-absolute-from-gregorian
1039 (calendar-nth-named-day 1 dayname prev-month prev-year
)))
1040 (setq last-day
(calendar-absolute-from-gregorian
1041 (calendar-nth-named-day -
1 dayname succ-month succ-year
)))
1042 (while (<= day last-day
)
1043 (mark-visible-calendar-date (calendar-gregorian-from-absolute day
) color
)
1044 (setq day
(+ day
7))))))
1046 (defun mark-calendar-date-pattern (month day year
&optional color
)
1047 "Mark all dates in the calendar window that conform to MONTH/DAY/YEAR.
1048 A value of 0 in any position is a wildcard."
1050 (set-buffer calendar-buffer
)
1051 (let ((m displayed-month
)
1053 (increment-calendar-month m y -
1)
1054 (calendar-for-loop i from
0 to
2 do
1055 (mark-calendar-month m y month day year color
)
1056 (increment-calendar-month m y
1)))))
1058 (defun mark-calendar-month (month year p-month p-day p-year
&optional color
)
1059 "Mark dates in the MONTH/YEAR that conform to pattern P-MONTH/P_DAY/P-YEAR.
1060 A value of 0 in any position of the pattern is a wildcard."
1061 (if (or (and (= month p-month
)
1062 (or (= p-year
0) (= year p-year
)))
1064 (or (= p-year
0) (= year p-year
))))
1067 i from
1 to
(calendar-last-day-of-month month year
) do
1068 (mark-visible-calendar-date (list month i year
) color
))
1069 (mark-visible-calendar-date (list month p-day year
) color
))))
1071 (defun sort-diary-entries ()
1072 "Sort the list of diary entries by time of day."
1073 (setq diary-entries-list
(sort diary-entries-list
'diary-entry-compare
)))
1075 (defun diary-entry-compare (e1 e2
)
1076 "Returns t if E1 is earlier than E2."
1077 (or (calendar-date-compare e1 e2
)
1078 (and (calendar-date-equal (car e1
) (car e2
))
1079 (let* ((ts1 (cadr e1
)) (t1 (diary-entry-time ts1
))
1080 (ts2 (cadr e2
)) (t2 (diary-entry-time ts2
)))
1083 (string-lessp ts1 ts2
)))))))
1085 (defcustom diary-unknown-time
1087 "*Value returned by diary-entry-time when no time is found.
1088 The default value -9999 causes entries with no recognizable time to be placed
1089 before those with times; 9999 would place entries with no recognizable time
1090 after those with times."
1095 (defun diary-entry-time (s)
1096 "Return time at the beginning of the string S as a military-style integer.
1097 For example, returns 1325 for 1:25pm.
1098 Returns `diary-unknown-time' (default value -9999) if no time is recognized. The recognized forms are XXXX, X:XX, or
1099 XX:XX (military time), and XXam, XXAM, XXpm, XXPM, XX:XXam, XX:XXAM XX:XXpm,
1101 (let ((case-fold-search nil
))
1102 (cond ((string-match ; Military time
1103 "\\`[ \t\n\\^M]*\\([0-9]?[0-9]\\):?\\([0-9][0-9]\\)\\(\\>\\|[^ap]\\)" s
)
1104 (+ (* 100 (string-to-int
1105 (substring s
(match-beginning 1) (match-end 1))))
1106 (string-to-int (substring s
(match-beginning 2) (match-end 2)))))
1107 ((string-match ; Hour only XXam or XXpm
1108 "\\`[ \t\n\\^M]*\\([0-9]?[0-9]\\)\\([ap]\\)m\\>" s
)
1109 (+ (* 100 (%
(string-to-int
1110 (substring s
(match-beginning 1) (match-end 1)))
1112 (if (equal ?a
(downcase (aref s
(match-beginning 2))))
1114 ((string-match ; Hour and minute XX:XXam or XX:XXpm
1115 "\\`[ \t\n\\^M]*\\([0-9]?[0-9]\\):\\([0-9][0-9]\\)\\([ap]\\)m\\>" s
)
1116 (+ (* 100 (%
(string-to-int
1117 (substring s
(match-beginning 1) (match-end 1)))
1119 (string-to-int (substring s
(match-beginning 2) (match-end 2)))
1120 (if (equal ?a
(downcase (aref s
(match-beginning 3))))
1122 (t diary-unknown-time
)))) ; Unrecognizable
1124 (defun list-sexp-diary-entries (date)
1125 "Add sexp entries for DATE from the diary file to `diary-entries-list'.
1126 Also, Make them visible in the diary file. Returns t if any entries were
1129 Sexp diary entries must be prefaced by a `sexp-diary-entry-symbol' (normally
1130 `%%'). The form of a sexp diary entry is
1134 Both ENTRY and DATE are globally available when the SEXP is evaluated. If the
1135 SEXP yields the value nil, the diary entry does not apply. If it yields a
1136 non-nil value, ENTRY will be taken to apply to DATE; if the non-nil value is a
1137 string, that string will be the diary entry in the fancy diary display.
1139 For example, the following diary entry will apply to the 21st of the month
1140 if it is a weekday and the Friday before if the 21st is on a weekend:
1142 &%%(let ((dayname (calendar-day-of-week date))
1143 (day (extract-calendar-day date)))
1145 (and (= day 21) (memq dayname '(1 2 3 4 5)))
1146 (and (memq day '(19 20)) (= dayname 5)))
1147 ) UIUC pay checks deposited
1149 A number of built-in functions are available for this type of diary entry:
1151 %%(diary-date MONTH DAY YEAR &optional MARK) text
1152 Entry applies if date is MONTH, DAY, YEAR if
1153 `european-calendar-style' is nil, and DAY, MONTH, YEAR if
1154 `european-calendar-style' is t. DAY, MONTH, and YEAR
1155 can be lists of integers, the constant t, or an integer.
1156 The constant t means all values. An optional parameter
1157 MARK specifies a face or single-character string to use
1158 when highlighting the day in the calendar.
1160 %%(diary-float MONTH DAYNAME N &optional DAY MARK) text
1161 Entry will appear on the Nth DAYNAME of MONTH.
1162 (DAYNAME=0 means Sunday, 1 means Monday, and so on;
1163 if N is negative it counts backward from the end of
1164 the month. MONTH can be a list of months, a single
1165 month, or t to specify all months. Optional DAY means
1166 Nth DAYNAME of MONTH on or after/before DAY. DAY defaults
1167 to 1 if N>0 and the last day of the month if N<0. An
1168 optional parameter MARK specifies a face or single-character
1169 string to use when highlighting the day in the calendar.
1171 %%(diary-block M1 D1 Y1 M2 D2 Y2 &optional MARK) text
1172 Entry will appear on dates between M1/D1/Y1 and M2/D2/Y2,
1173 inclusive. (If `european-calendar-style' is t, the
1174 order of the parameters should be changed to D1, M1, Y1,
1175 D2, M2, Y2.) An optional parameter MARK specifies a face
1176 or single-character string to use when highlighting the
1177 day in the calendar.
1179 %%(diary-anniversary MONTH DAY YEAR &optional MARK) text
1180 Entry will appear on anniversary dates of MONTH DAY, YEAR.
1181 (If `european-calendar-style' is t, the order of the
1182 parameters should be changed to DAY, MONTH, YEAR.) Text
1183 can contain %d or %d%s; %d will be replaced by the number
1184 of years since the MONTH DAY, YEAR and %s will be replaced
1185 by the ordinal ending of that number (that is, `st', `nd',
1186 `rd' or `th', as appropriate. The anniversary of February
1187 29 is considered to be March 1 in a non-leap year. An
1188 optional parameter MARK specifies a face or single-character
1189 string to use when highlighting the day in the calendar.
1191 %%(diary-cyclic N MONTH DAY YEAR &optional MARK) text
1192 Entry will appear every N days, starting MONTH DAY, YEAR.
1193 (If `european-calendar-style' is t, the order of the
1194 parameters should be changed to N, DAY, MONTH, YEAR.) Text
1195 can contain %d or %d%s; %d will be replaced by the number
1196 of repetitions since the MONTH DAY, YEAR and %s will
1197 be replaced by the ordinal ending of that number (that is,
1198 `st', `nd', `rd' or `th', as appropriate. An optional
1199 parameter MARK specifies a face or single-character string
1200 to use when highlighting the day in the calendar.
1202 %%(diary-remind SEXP DAYS &optional MARKING) text
1203 Entry is a reminder for diary sexp SEXP. DAYS is either a
1204 single number or a list of numbers indicating the number(s)
1205 of days before the event that the warning(s) should occur.
1206 If the current date is (one of) DAYS before the event
1207 indicated by EXPR, then a suitable message (as specified
1208 by `diary-remind-message') appears. In addition to the
1209 reminders beforehand, the diary entry also appears on
1210 the date itself. If optional MARKING is non-nil then the
1211 *reminders* are marked on the calendar. Marking of
1212 reminders is independent of whether the entry *itself* is
1213 a marking or nonmarking one.
1215 %%(diary-day-of-year)
1216 Diary entries giving the day of the year and the number of
1217 days remaining in the year will be made every day. Note
1218 that since there is no text, it makes sense only if the
1219 fancy diary display is used.
1222 Diary entries giving the corresponding ISO commercial date
1223 will be made every day. Note that since there is no text,
1224 it makes sense only if the fancy diary display is used.
1226 %%(diary-french-date)
1227 Diary entries giving the corresponding French Revolutionary
1228 date will be made every day. Note that since there is no
1229 text, it makes sense only if the fancy diary display is used.
1231 %%(diary-islamic-date)
1232 Diary entries giving the corresponding Islamic date will be
1233 made every day. Note that since there is no text, it
1234 makes sense only if the fancy diary display is used.
1236 %%(diary-hebrew-date)
1237 Diary entries giving the corresponding Hebrew date will be
1238 made every day. Note that since there is no text, it
1239 makes sense only if the fancy diary display is used.
1241 %%(diary-astro-day-number) Diary entries giving the corresponding
1242 astronomical (Julian) day number will be made every day.
1243 Note that since there is no text, it makes sense only if the
1244 fancy diary display is used.
1246 %%(diary-julian-date) Diary entries giving the corresponding
1247 Julian date will be made every day. Note that since
1248 there is no text, it makes sense only if the fancy diary
1251 %%(diary-sunrise-sunset)
1252 Diary entries giving the local times of sunrise and sunset
1253 will be made every day. Note that since there is no text,
1254 it makes sense only if the fancy diary display is used.
1255 Floating point required.
1257 %%(diary-phases-of-moon)
1258 Diary entries giving the times of the phases of the moon
1259 will be when appropriate. Note that since there is no text,
1260 it makes sense only if the fancy diary display is used.
1261 Floating point required.
1263 %%(diary-yahrzeit MONTH DAY YEAR) text
1264 Text is assumed to be the name of the person; the date is
1265 the date of death on the *civil* calendar. The diary entry
1266 will appear on the proper Hebrew-date anniversary and on the
1267 day before. (If `european-calendar-style' is t, the order
1268 of the parameters should be changed to DAY, MONTH, YEAR.)
1270 %%(diary-rosh-hodesh)
1271 Diary entries will be made on the dates of Rosh Hodesh on
1272 the Hebrew calendar. Note that since there is no text, it
1273 makes sense only if the fancy diary display is used.
1276 Diary entries giving the weekly parasha will be made on
1277 every Saturday. Note that since there is no text, it
1278 makes sense only if the fancy diary display is used.
1281 Diary entries giving the omer count will be made every day
1282 from Passover to Shavuot. Note that since there is no text,
1283 it makes sense only if the fancy diary display is used.
1285 Marking these entries is *extremely* time consuming, so these entries are
1286 best if they are nonmarking."
1287 (let* ((mark (regexp-quote diary-nonmarking-symbol
))
1288 (sexp-mark (regexp-quote sexp-diary-entry-symbol
))
1289 (s-entry (concat "\\(\\`\\|\^M\\|\n\\)" mark
"?" sexp-mark
"("))
1293 (goto-char (point-min))
1295 (setq file-glob-attrs
(nth 1 (diary-pull-attrs nil
'()))))
1296 (while (re-search-forward s-entry nil t
)
1298 (let ((sexp-start (point))
1305 (setq sexp
(buffer-substring-no-properties sexp-start
(point)))
1307 (re-search-backward "\^M\\|\n\\|\\`")
1308 (setq line-start
(point)))
1310 (buffer-substring-no-properties (1+ line-start
) (point))
1311 entry-start
(1+ line-start
))
1313 (if (and (or (char-equal (preceding-char) ?\^M
)
1314 (char-equal (preceding-char) ?
\n))
1315 (not (looking-at " \\|\^I")))
1316 (progn;; Diary entry consists only of the sexp
1319 (setq entry-start
(point))
1320 (re-search-forward "\^M\\|\n" nil t
)
1321 (while (looking-at " \\|\^I")
1322 (re-search-forward "\^M\\|\n" nil t
))
1324 (setq entry
(buffer-substring-no-properties entry-start
(point)))
1325 (while (string-match "[\^M]" entry
)
1326 (aset entry
(match-beginning 0) ?
\n )))
1327 (let ((diary-entry (diary-sexp-entry sexp entry date
)))
1328 (setq entry
(if (consp diary-entry
)
1333 (subst-char-in-region line-start
(point) ?\^M ?
\n t
)
1334 (if (< 0 (length entry
))
1335 (setq temp
(diary-pull-attrs entry file-glob-attrs
)
1337 marks
(nth 1 temp
)))))
1338 (add-to-diary-list date
1341 (if entry-start
(copy-marker entry-start
)
1344 (setq entry-found
(or entry-found diary-entry
)))))
1347 (defun diary-sexp-entry (sexp entry date
)
1348 "Process a SEXP diary ENTRY for DATE."
1349 (let ((result (if calendar-debug-sexp
1350 (let ((stack-trace-on-error t
))
1351 (eval (car (read-from-string sexp
))))
1353 (eval (car (read-from-string sexp
)))
1356 (message "Bad sexp at line %d in %s: %s"
1359 (narrow-to-region 1 (point))
1360 (goto-char (point-min))
1362 (while (re-search-forward "\n\\|\^M" nil t
)
1363 (setq lines
(1+ lines
)))
1367 (cond ((stringp result
) result
)
1368 ((and (consp result
)
1369 (stringp (cdr result
))) result
)
1373 (defun diary-date (month day year
&optional mark
)
1374 "Specific date(s) diary entry.
1375 Entry applies if date is MONTH, DAY, YEAR if `european-calendar-style' is nil,
1376 and DAY, MONTH, YEAR if `european-calendar-style' is t. DAY, MONTH, and YEAR
1377 can be lists of integers, the constant t, or an integer. The constant t means
1380 An optional parameter MARK specifies a face or single-character string to
1381 use when highlighting the day in the calendar."
1382 (let* ((dd (if european-calendar-style
1385 (mm (if european-calendar-style
1388 (m (extract-calendar-month date
))
1389 (y (extract-calendar-year date
))
1390 (d (extract-calendar-day date
)))
1392 (or (and (listp dd
) (memq d dd
))
1395 (or (and (listp mm
) (memq m mm
))
1398 (or (and (listp year
) (memq y year
))
1401 (cons mark entry
))))
1403 (defun diary-block (m1 d1 y1 m2 d2 y2
&optional mark
)
1405 Entry applies if date is between, or on one of, two dates.
1406 The order of the parameters is
1407 M1, D1, Y1, M2, D2, Y2 if `european-calendar-style' is nil, and
1408 D1, M1, Y1, D2, M2, Y2 if `european-calendar-style' is t.
1410 An optional parameter MARK specifies a face or single-character string to
1411 use when highlighting the day in the calendar."
1413 (let ((date1 (calendar-absolute-from-gregorian
1414 (if european-calendar-style
1417 (date2 (calendar-absolute-from-gregorian
1418 (if european-calendar-style
1421 (d (calendar-absolute-from-gregorian date
)))
1422 (if (and (<= date1 d
) (<= d date2
))
1423 (cons mark entry
))))
1425 (defun diary-float (month dayname n
&optional day mark
)
1426 "Floating diary entry--entry applies if date is the nth dayname of month.
1427 Parameters are MONTH, DAYNAME, N. MONTH can be a list of months, the constant
1428 t, or an integer. The constant t means all months. If N is negative, count
1429 backward from the end of the month.
1431 An optional parameter DAY means the Nth DAYNAME on or after/before MONTH DAY.
1432 Optional MARK specifies a face or single-character string to use when
1433 highlighting the day in the calendar."
1434 ;; This is messy because the diary entry may apply, but the date on which it
1435 ;; is based can be in a different month/year. For example, asking for the
1436 ;; first Monday after December 30. For large values of |n| the problem is
1438 (and (= dayname
(calendar-day-of-week date
))
1439 (let* ((m (extract-calendar-month date
))
1440 (d (extract-calendar-day date
))
1441 (y (extract-calendar-year date
))
1442 (limit; last (n>0) or first (n<0) possible base date for entry
1443 (calendar-nth-named-absday (- n
) dayname m y d
))
1444 (last-abs (if (> n
0) limit
(+ limit
6)))
1445 (first-abs (if (> n
0) (- limit
6) limit
))
1446 (last (calendar-gregorian-from-absolute last-abs
))
1447 (first (calendar-gregorian-from-absolute first-abs
))
1448 ; m1, d1 is first possible base date
1449 (m1 (extract-calendar-month first
))
1450 (d1 (extract-calendar-day first
))
1451 (y1 (extract-calendar-year first
))
1452 ; m2, d2 is last possible base date
1453 (m2 (extract-calendar-month last
))
1454 (d2 (extract-calendar-day last
))
1455 (y2 (extract-calendar-year last
)))
1456 (if (or (and (= m1 m2
) ; only possible base dates in one month
1461 (let ((d (or day
(if (> n
0)
1463 (calendar-last-day-of-month m1 y1
)))))
1464 (and (<= d1 d
) (<= d d2
))))
1465 ;; only possible base dates straddle two months
1467 (and (= y1 y2
) (< m1 m2
)))
1469 ;; m1, d1 works as a base date
1475 (<= d1
(or day
(if (> n
0)
1477 (calendar-last-day-of-month m1 y1
)))))
1478 ;; m2, d2 works as a base date
1479 (and (or (eq month t
)
1483 (<= (or day
(if (> n
0)
1485 (calendar-last-day-of-month m2 y2
)))
1487 (cons mark entry
)))))
1490 (defun diary-anniversary (month day year
&optional mark
)
1491 "Anniversary diary entry.
1492 Entry applies if date is the anniversary of MONTH, DAY, YEAR if
1493 `european-calendar-style' is nil, and DAY, MONTH, YEAR if
1494 `european-calendar-style' is t. Diary entry can contain `%d' or `%d%s'; the
1495 %d will be replaced by the number of years since the MONTH DAY, YEAR and the
1496 %s will be replaced by the ordinal ending of that number (that is, `st', `nd',
1497 `rd' or `th', as appropriate. The anniversary of February 29 is considered
1498 to be March 1 in non-leap years.
1500 An optional parameter MARK specifies a face or single-character string to
1501 use when highlighting the day in the calendar."
1502 (let* ((d (if european-calendar-style
1505 (m (if european-calendar-style
1508 (y (extract-calendar-year date
))
1510 (if (and (= m
2) (= d
29) (not (calendar-leap-year-p y
)))
1513 (if (and (> diff
0) (calendar-date-equal (list m d y
) date
))
1514 (cons mark
(format entry diff
(diary-ordinal-suffix diff
))))))
1516 (defun diary-cyclic (n month day year
&optional mark
)
1517 "Cycle diary entry--entry applies every N days starting at MONTH, DAY, YEAR.
1518 If `european-calendar-style' is t, parameters are N, DAY, MONTH, YEAR.
1519 ENTRY can contain `%d' or `%d%s'; the %d will be replaced by the number of
1520 repetitions since the MONTH DAY, YEAR and %s will be replaced by the
1521 ordinal ending of that number (that is, `st', `nd', `rd' or `th', as
1524 An optional parameter MARK specifies a face or single-character string to
1525 use when highlighting the day in the calendar."
1526 (let* ((d (if european-calendar-style
1529 (m (if european-calendar-style
1532 (diff (- (calendar-absolute-from-gregorian date
)
1533 (calendar-absolute-from-gregorian
1536 (if (and (>= diff
0) (zerop (% diff n
)))
1537 (cons mark
(format entry cycle
(diary-ordinal-suffix cycle
))))))
1539 (defun diary-ordinal-suffix (n)
1540 "Ordinal suffix for N. (That is, `st', `nd', `rd', or `th', as appropriate.)"
1541 (if (or (memq (% n
100) '(11 12 13))
1544 (aref ["th" "st" "nd" "rd"] (% n
10))))
1546 (defun diary-day-of-year ()
1547 "Day of year and number of days remaining in the year of date diary entry."
1548 (calendar-day-of-year-string date
))
1550 (defcustom diary-remind-message
1552 (if (= 0 (% days
7))
1553 (concat (int-to-string (/ days
7)) (if (= 7 days
) " week" " weeks"))
1554 (concat (int-to-string days
) (if (= 1 days
) " day" " days")))
1557 "*Pseudo-pattern giving form of reminder messages in the fancy diary
1560 Used by the function `diary-remind', a pseudo-pattern is a list of
1561 expressions that can involve the keywords `days' (a number), `date' (a list of
1562 month, day, year), and `diary-entry' (a string)."
1566 (defun diary-remind (sexp days
&optional marking
)
1567 "Provide a reminder of a diary entry.
1568 SEXP is a diary-sexp. DAYS is either a single number or a list of numbers
1569 indicating the number(s) of days before the event that the warning(s) should
1570 occur on. If the current date is (one of) DAYS before the event indicated by
1571 SEXP, then a suitable message (as specified by `diary-remind-message' is
1574 In addition to the reminders beforehand, the diary entry also appears on the
1577 A `diary-nonmarking-symbol' at the beginning of the line of the diary-remind
1578 entry specifies that the diary entry (not the reminder) is non-marking.
1579 Marking of reminders is independent of whether the entry itself is a marking
1580 or nonmarking; if optional parameter MARKING is non-nil then the reminders are
1581 marked on the calendar."
1582 (let ((diary-entry (eval sexp
)))
1584 ;; Diary entry applies on date
1586 (or (not marking-diary-entries
) marking-diary-entry
))
1588 ;; Diary entry may apply to `days' before date
1589 ((and (integerp days
)
1590 (not diary-entry
); Diary entry does not apply to date
1591 (or (not marking-diary-entries
) marking
))
1592 (let ((date (calendar-gregorian-from-absolute
1593 (+ (calendar-absolute-from-gregorian date
) days
))))
1594 (if (setq diary-entry
(eval sexp
))
1595 (mapconcat 'eval diary-remind-message
""))))
1596 ;; Diary entry may apply to one of a list of days before date
1597 ((and (listp days
) days
)
1598 (or (diary-remind sexp
(car days
) marking
)
1599 (diary-remind sexp
(cdr days
) marking
))))))
1601 (defun add-to-diary-list (date string specifier marker
&optional globcolor
)
1602 "Add the entry (DATE STRING SPECIFIER MARKER GLOBCOLOR) to `diary-entries-list'.
1603 Do nothing if DATE or STRING is nil."
1605 (if (and diary-file-name-prefix
1606 (setq prefix
(concat "[" (funcall diary-file-name-prefix-function
(buffer-file-name)) "] "))
1607 (not (string= prefix
"[] ")))
1608 (setq string
(concat prefix string
))
1610 (setq diary-entries-list
1611 (append diary-entries-list
1612 (list (list date string specifier marker globcolor
))))))
1614 (defun make-diary-entry (string &optional nonmarking file
)
1615 "Insert a diary entry STRING which may be NONMARKING in FILE.
1616 If omitted, NONMARKING defaults to nil and FILE defaults to diary-file."
1617 (find-file-other-window
1618 (substitute-in-file-name (if file file diary-file
)))
1620 (goto-char (point-max))
1621 (when (let ((case-fold-search t
))
1622 (search-backward "Local Variables:"
1623 (max (- (point-max) 3000) (point-min))
1630 (if nonmarking diary-nonmarking-symbol
"")
1633 (defun insert-diary-entry (arg)
1634 "Insert a diary entry for the date indicated by point.
1635 Prefix arg will make the entry nonmarking."
1637 (make-diary-entry (calendar-date-string (calendar-cursor-to-date t
) t t
)
1640 (defun insert-weekly-diary-entry (arg)
1641 "Insert a weekly diary entry for the day of the week indicated by point.
1642 Prefix arg will make the entry nonmarking."
1644 (make-diary-entry (calendar-day-name (calendar-cursor-to-date t
))
1647 (defun insert-monthly-diary-entry (arg)
1648 "Insert a monthly diary entry for the day of the month indicated by point.
1649 Prefix arg will make the entry nonmarking."
1651 (let* ((calendar-date-display-form
1652 (if european-calendar-style
1655 (make-diary-entry (calendar-date-string (calendar-cursor-to-date t
) t
)
1658 (defun insert-yearly-diary-entry (arg)
1659 "Insert an annual diary entry for the day of the year indicated by point.
1660 Prefix arg will make the entry nonmarking."
1662 (let* ((calendar-date-display-form
1663 (if european-calendar-style
1664 '(day " " monthname
)
1665 '(monthname " " day
))))
1666 (make-diary-entry (calendar-date-string (calendar-cursor-to-date t
) t
)
1669 (defun insert-anniversary-diary-entry (arg)
1670 "Insert an anniversary diary entry for the date given by point.
1671 Prefix arg will make the entry nonmarking."
1673 (let* ((calendar-date-display-form
1674 (if european-calendar-style
1675 '(day " " month
" " year
)
1676 '(month " " day
" " year
))))
1678 (format "%s(diary-anniversary %s)"
1679 sexp-diary-entry-symbol
1680 (calendar-date-string (calendar-cursor-to-date t
) nil t
))
1683 (defun insert-block-diary-entry (arg)
1684 "Insert a block diary entry for the days between the point and marked date.
1685 Prefix arg will make the entry nonmarking."
1687 (let* ((calendar-date-display-form
1688 (if european-calendar-style
1689 '(day " " month
" " year
)
1690 '(month " " day
" " year
)))
1691 (cursor (calendar-cursor-to-date t
))
1692 (mark (or (car calendar-mark-ring
)
1693 (error "No mark set in this buffer")))
1696 (if (< (calendar-absolute-from-gregorian mark
)
1697 (calendar-absolute-from-gregorian cursor
))
1703 (format "%s(diary-block %s %s)"
1704 sexp-diary-entry-symbol
1705 (calendar-date-string start nil t
)
1706 (calendar-date-string end nil t
))
1709 (defun insert-cyclic-diary-entry (arg)
1710 "Insert a cyclic diary entry starting at the date given by point.
1711 Prefix arg will make the entry nonmarking."
1713 (let* ((calendar-date-display-form
1714 (if european-calendar-style
1715 '(day " " month
" " year
)
1716 '(month " " day
" " year
))))
1718 (format "%s(diary-cyclic %d %s)"
1719 sexp-diary-entry-symbol
1720 (calendar-read "Repeat every how many days: "
1721 (lambda (x) (> x
0)))
1722 (calendar-date-string (calendar-cursor-to-date t
) nil t
))
1726 (define-derived-mode diary-mode text-mode
1728 "Major mode for editing the diary file."
1729 (set (make-local-variable 'font-lock-defaults
)
1730 '(diary-font-lock-keywords t
)))
1732 (define-derived-mode fancy-diary-display-mode text-mode
1734 "Major mode used while displaying diary entries using Fancy Display."
1735 (set (make-local-variable 'font-lock-defaults
)
1736 '(fancy-diary-font-lock-keywords t
)))
1739 (defvar fancy-diary-font-lock-keywords
1745 (diary-name-pattern calendar-day-name-array t
)
1749 (diary-name-pattern calendar-month-name-array t
)
1754 (mapconcat 'eval calendar-date-display-form
""))
1755 "\\(\\(: .*\\)\\|\\(\n +.*\\)\\)*\n=+$")
1757 '("^.*anniversary.*$" . font-lock-keyword-face
)
1758 '("^.*birthday.*$" . font-lock-keyword-face
)
1759 '("^.*Yahrzeit.*$" . font-lock-reference-face
)
1760 '("^\\(Erev \\)?Rosh Hodesh.*" . font-lock-function-name-face
)
1761 '("^Day.*omer.*$" . font-lock-builtin-face
)
1762 '("^Parashat.*$" . font-lock-comment-face
)
1763 '("^[ \t]*[0-9]?[0-9]\\(:?[0-9][0-9]\\)?\\(am\\|pm\\|AM\\|PM\\)?\\(-[0-9]?[0-9]\\(:?[0-9][0-9]\\)?\\(am\\|pm\\|AM\\|PM\\)?\\)?"
1764 . font-lock-variable-name-face
))
1765 "Keywords to highlight in fancy diary display")
1768 (defun font-lock-diary-sexps (limit)
1769 "Recognize sexp diary entry for font-locking."
1770 (if (re-search-forward
1771 (concat "^" (regexp-quote diary-nonmarking-symbol
)
1772 "?\\(" (regexp-quote sexp-diary-entry-symbol
) "\\)")
1776 (narrow-to-region (point-min) limit
)
1777 (let ((start (point)))
1779 (store-match-data (list start
(point)))
1783 (defun font-lock-diary-date-forms (month-list &optional symbol noabbrev
)
1784 "Create a list of font-lock patterns for `diary-date-forms' with MONTH-LIST.
1785 If given, optional SYMBOL must be a prefix to entries.
1786 If optional NOABBREV is t, do not allow abbreviations in names."
1788 (concat "\\(" (diary-name-pattern calendar-day-name-array
) "\\)"))
1789 (monthname (concat "\\("
1790 (diary-name-pattern month-list noabbrev
)
1792 (month "\\([0-9]+\\|\\*\\)")
1793 (day "\\([0-9]+\\|\\*\\)")
1794 (year "-?\\([0-9]+\\|\\*\\)"))
1795 (mapcar '(lambda (x)
1797 (concat "^" (regexp-quote diary-nonmarking-symbol
) "?"
1798 (if symbol
(regexp-quote symbol
) "") "\\("
1800 ;; If backup, omit first item (backup)
1801 ;; and last item (not part of date)
1802 (if (equal (car x
) 'backup
)
1803 (reverse (cdr (reverse (cdr x
))))
1806 ;; With backup, last item is not part of date
1807 (if (equal (car x
) 'backup
)
1808 (concat "\\)" (eval (car (reverse x
))))
1813 (defvar diary-font-lock-keywords
1815 (font-lock-diary-date-forms calendar-month-name-array
)
1816 (if (or (memq 'mark-hebrew-diary-entries
1817 nongregorian-diary-marking-hook
)
1818 (memq 'list-hebrew-diary-entries
1819 nongregorian-diary-listing-hook
))
1821 (require 'cal-hebrew
)
1822 (font-lock-diary-date-forms
1823 calendar-hebrew-month-name-array-leap-year
1824 hebrew-diary-entry-symbol t
)))
1825 (if (or (memq 'mark-islamic-diary-entries
1826 nongregorian-diary-marking-hook
)
1827 (memq 'list-islamic-diary-entries
1828 nongregorian-diary-listing-hook
))
1830 (require 'cal-islamic
)
1831 (font-lock-diary-date-forms
1832 calendar-islamic-month-name-array-leap-year
1833 islamic-diary-entry-symbol t
)))
1836 (concat "^" (regexp-quote diary-include-string
) ".*$")
1837 'font-lock-keyword-face
)
1839 (concat "^" (regexp-quote diary-nonmarking-symbol
)
1840 "?\\(" (regexp-quote sexp-diary-entry-symbol
) "\\)")
1841 '(1 font-lock-reference-face
))
1843 (concat "^" (regexp-quote diary-nonmarking-symbol
))
1844 'font-lock-reference-face
)
1846 (concat "^" (regexp-quote diary-nonmarking-symbol
)
1847 "?\\(" (regexp-quote hebrew-diary-entry-symbol
) "\\)")
1848 '(1 font-lock-reference-face
))
1850 (concat "^" (regexp-quote diary-nonmarking-symbol
)
1851 "?\\(" (regexp-quote islamic-diary-entry-symbol
) "\\)")
1852 '(1 font-lock-reference-face
))
1853 '(font-lock-diary-sexps . font-lock-keyword-face
)
1854 '("[0-9]?[0-9]\\(:?[0-9][0-9]\\)?\\(am\\|pm\\|AM\\|PM\\)\\(-[0-9]?[0-9]\\(:?[0-9][0-9]\\)?\\(am\\|pm\\|AM\\|PM\\)\\)?"
1855 . font-lock-function-name-face
)))
1856 "Forms to highlight in diary-mode")
1859 (provide 'diary-lib
)
1861 ;;; diary-lib.el ends here