1 ;;; diary-lib.el --- diary functions
3 ;; Copyright (C) 1989, 1990, 1992, 1993, 1994, 1995, 2003
4 ;; Free Software Foundation, Inc.
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
41 (defun diary-check-diary-file ()
42 "Check that the file specified by `diary-file' exists and is readable.
43 If so, return the expanded file name, otherwise signal an error."
44 (let ((d-file (substitute-in-file-name diary-file
)))
45 (if (and d-file
(file-exists-p d-file
))
46 (if (file-readable-p d-file
)
48 (error "Diary file `%s' is not readable" diary-file
))
49 (error "Diary file `%s' does not exist" diary-file
))))
52 (defun diary (&optional arg
)
53 "Generate the diary window for ARG days starting with the current date.
54 If no argument is provided, the number of days of diary entries is governed
55 by the variable `number-of-diary-entries'. This function is suitable for
56 execution in a `.emacs' file."
58 (diary-check-diary-file)
59 (let ((date (calendar-current-date)))
62 (cond (arg (prefix-numeric-value arg
))
63 ((vectorp number-of-diary-entries
)
64 (aref number-of-diary-entries
(calendar-day-of-week date
)))
65 (t number-of-diary-entries
)))))
67 (defun view-diary-entries (arg)
68 "Prepare and display a buffer with diary entries.
69 Searches the file named in `diary-file' for entries that
70 match ARG days starting with the date indicated by the cursor position
71 in the displayed three-month calendar."
73 (diary-check-diary-file)
74 (list-diary-entries (calendar-cursor-to-date t
) arg
))
76 (defun view-other-diary-entries (arg d-file
)
77 "Prepare and display buffer of diary entries from an alternative diary file.
78 Searches for entries that match ARG days, starting with the date indicated
79 by the cursor position in the displayed three-month calendar.
80 D-FILE specifies the file to use as the diary file."
82 (list (if arg
(prefix-numeric-value arg
) 1)
83 (read-file-name "Enter diary file name: " default-directory nil t
)))
84 (let ((diary-file d-file
))
85 (view-diary-entries arg
)))
87 (autoload 'check-calendar-holidays
"holidays"
88 "Check the list of holidays for any that occur on DATE.
89 The value returned is a list of strings of relevant holiday descriptions.
90 The holidays are those in the list `calendar-holidays'.")
92 (autoload 'calendar-holiday-list
"holidays"
93 "Form the list of holidays that occur on dates in the calendar window.
94 The holidays are those in the list `calendar-holidays'.")
96 (autoload 'diary-french-date
"cal-french"
97 "French calendar equivalent of date diary entry.")
99 (autoload 'diary-mayan-date
"cal-mayan"
100 "Mayan calendar equivalent of date diary entry.")
102 (autoload 'diary-iso-date
"cal-iso"
103 "ISO calendar equivalent of date diary entry.")
105 (autoload 'diary-julian-date
"cal-julian"
106 "Julian calendar equivalent of date diary entry.")
108 (autoload 'diary-astro-day-number
"cal-julian"
109 "Astronomical (Julian) day number diary entry.")
111 (autoload 'diary-chinese-date
"cal-china"
112 "Chinese calendar equivalent of date diary entry.")
114 (autoload 'diary-islamic-date
"cal-islam"
115 "Islamic calendar equivalent of date diary entry.")
117 (autoload 'list-islamic-diary-entries
"cal-islam"
118 "Add any Islamic date entries from the diary file to `diary-entries-list'.")
120 (autoload 'mark-islamic-diary-entries
"cal-islam"
121 "Mark days in the calendar window that have Islamic date diary entries.")
123 (autoload 'mark-islamic-calendar-date-pattern
"cal-islam"
124 "Mark dates in calendar window that conform to Islamic date MONTH/DAY/YEAR.")
126 (autoload 'diary-hebrew-date
"cal-hebrew"
127 "Hebrew calendar equivalent of date diary entry.")
129 (autoload 'diary-omer
"cal-hebrew"
130 "Omer count diary entry.")
132 (autoload 'diary-yahrzeit
"cal-hebrew"
133 "Yahrzeit diary entry--entry applies if date is yahrzeit or the day before.")
135 (autoload 'diary-parasha
"cal-hebrew"
136 "Parasha diary entry--entry applies if date is a Saturday.")
138 (autoload 'diary-rosh-hodesh
"cal-hebrew"
139 "Rosh Hodesh diary entry.")
141 (autoload 'list-hebrew-diary-entries
"cal-hebrew"
142 "Add any Hebrew date entries from the diary file to `diary-entries-list'.")
144 (autoload 'mark-hebrew-diary-entries
"cal-hebrew"
145 "Mark days in the calendar window that have Hebrew date diary entries.")
147 (autoload 'mark-hebrew-calendar-date-pattern
"cal-hebrew"
148 "Mark dates in calendar window that conform to Hebrew date MONTH/DAY/YEAR.")
150 (autoload 'diary-coptic-date
"cal-coptic"
151 "Coptic calendar equivalent of date diary entry.")
153 (autoload 'diary-ethiopic-date
"cal-coptic"
154 "Ethiopic calendar equivalent of date diary entry.")
156 (autoload 'diary-persian-date
"cal-persia"
157 "Persian calendar equivalent of date diary entry.")
159 (autoload 'diary-phases-of-moon
"lunar" "Moon phases diary entry.")
161 (autoload 'diary-sunrise-sunset
"solar"
162 "Local time of sunrise and sunset as a diary entry.")
164 (autoload 'diary-sabbath-candles
"solar"
165 "Local time of candle lighting diary entry--applies if date is a Friday.
166 No diary entry if there is no sunset on that date.")
168 (defvar diary-syntax-table
(copy-syntax-table (standard-syntax-table))
169 "The syntax table used when parsing dates in the diary file.
170 It is the standard syntax table used in Fundamental mode, but with the
171 syntax of `*' and `:' changed to be word constituents.")
173 (modify-syntax-entry ?
* "w" diary-syntax-table
)
174 (modify-syntax-entry ?
: "w" diary-syntax-table
)
176 (defvar diary-entries-list
)
177 (defvar displayed-year
)
178 (defvar displayed-month
)
183 (defvar original-date
)
185 (defun diary-attrtype-convert (attrvalue type
)
186 "Convert string ATTRVALUE to TYPE appropriate for a face description.
187 Valid TYPEs are: string, symbol, int, stringtnil, tnil."
189 (setq ret
(cond ((eq type
'string
) attrvalue
)
190 ((eq type
'symbol
) (read attrvalue
))
191 ((eq type
'int
) (string-to-int attrvalue
))
192 ((eq type
'stringtnil
)
193 (cond ((string= "t" attrvalue
) t
)
194 ((string= "nil" attrvalue
) nil
)
197 (cond ((string= "t" attrvalue
) t
)
198 ((string= "nil" attrvalue
) nil
)))))
199 ; (message "(%s)[%s]=[%s]" (print type) attrvalue ret)
203 (defun diary-pull-attrs (entry fileglobattrs
)
204 "Pull the face-related attributes off the entry, merge with the
205 fileglobattrs, and return the (possibly modified) entry and face
206 data in a list of attrname attrvalue values.
207 The entry will be modified to drop all tags that are used for face matching.
208 If entry is nil, then the fileglobattrs are being searched for,
209 the fileglobattrs variable is ignored, and
210 diary-glob-file-regexp-prefix is prepended to the regexps before each
213 (let (regexp regnum attrname attr-list attrname attrvalue type
218 attr-list diary-face-attrs
)
220 (goto-char (point-min))
221 (setq attr
(car attr-list
)
224 attrname
(nth 2 attr
)
226 regexp
(concat diary-glob-file-regexp-prefix regexp
))
228 (if (re-search-forward regexp
(point-max) t
)
229 (setq attrvalue
(buffer-substring-no-properties
230 (match-beginning regnum
)
231 (match-end regnum
))))
233 (setq attrvalue
(diary-attrtype-convert attrvalue type
)))
234 (setq ret-attr
(append ret-attr
(list attrname attrvalue
))))
235 (setq attr-list
(cdr attr-list
)))
236 (setq fileglobattrs ret-attr
))
238 (setq ret-attr fileglobattrs
239 attr-list diary-face-attrs
)
241 (goto-char (point-min))
242 (setq attr
(car attr-list
)
245 attrname
(nth 2 attr
)
248 (if (string-match regexp entry
)
250 (setq attrvalue
(substring-no-properties entry
251 (match-beginning regnum
)
253 (setq entry
(replace-match "" t t entry
))))
255 (setq attrvalue
(diary-attrtype-convert attrvalue type
)))
256 (setq ret-attr
(append ret-attr
(list attrname attrvalue
))))
257 (setq attr-list
(cdr attr-list
)))))
258 (list entry ret-attr
))))
262 (defun list-diary-entries (date number
)
263 "Create and display a buffer containing the relevant lines in diary-file.
264 The arguments are DATE and NUMBER; the entries selected are those
265 for NUMBER days starting with date DATE. The other entries are hidden
266 using selective display.
268 Returns a list of all relevant diary entries found, if any, in order by date.
269 The list entries have the form ((month day year) string specifier) where
270 \(month day year) is the date of the entry, string is the entry text, and
271 specifier is the applicability. If the variable `diary-list-include-blanks'
272 is t, this list includes a dummy diary entry consisting of the empty string)
273 for a date with no diary entries.
275 After the list is prepared, the hooks `nongregorian-diary-listing-hook',
276 `list-diary-entries-hook', `diary-display-hook', and `diary-hook' are run.
277 These hooks have the following distinct roles:
279 `nongregorian-diary-listing-hook' can cull dates from the diary
280 and each included file. Usually used for Hebrew or Islamic
281 diary entries in files. Applied to *each* file.
283 `list-diary-entries-hook' adds or manipulates diary entries from
284 external sources. Used, for example, to include diary entries
285 from other files or to sort the diary entries. Invoked *once* only,
286 before the display hook is run.
288 `diary-display-hook' does the actual display of information. If this is
289 nil, simple-diary-display will be used. Use add-hook to set this to
290 fancy-diary-display, if desired. If you want no diary display, use
291 add-hook to set this to ignore.
293 `diary-hook' is run last. This can be used for an appointment
294 notification function."
297 (let ((original-date date
);; save for possible use in the hooks
298 old-diary-syntax-table
301 (date-string (calendar-date-string date
))
302 (d-file (substitute-in-file-name diary-file
)))
303 (message "Preparing diary...")
305 (let ((diary-buffer (find-buffer-visiting d-file
)))
306 (if (not diary-buffer
)
307 (set-buffer (find-file-noselect d-file t
))
308 (set-buffer diary-buffer
)
309 (or (verify-visited-file-modtime diary-buffer
)
310 (revert-buffer t t
))))
311 (setq file-glob-attrs
(nth 1 (diary-pull-attrs nil
"")))
312 (setq selective-display t
)
313 (setq selective-display-ellipses nil
)
314 (setq old-diary-syntax-table
(syntax-table))
315 (set-syntax-table diary-syntax-table
)
317 (let ((buffer-read-only nil
)
318 (diary-modified (buffer-modified-p))
319 (mark (regexp-quote diary-nonmarking-symbol
)))
320 ;; First and last characters must be ^M or \n for
321 ;; selective display to work properly
322 (goto-char (1- (point-max)))
323 (if (not (looking-at "\^M\\|\n"))
325 (goto-char (point-max))
327 (goto-char (point-min))
328 (if (not (looking-at "\^M\\|\n"))
330 (subst-char-in-region (point-min) (point-max) ?
\n ?\^M t
)
331 (calendar-for-loop i from
1 to number do
332 (let ((d diary-date-forms
)
333 (month (extract-calendar-month date
))
334 (day (extract-calendar-day date
))
335 (year (extract-calendar-year date
))
336 (entry-found (list-sexp-diary-entries date
)))
339 ((date-form (if (equal (car (car d
)) 'backup
)
342 (backup (equal (car (car d
)) 'backup
))
344 (format "%s\\|%s\\.?"
345 (calendar-day-name date
)
346 (calendar-day-name date
'abbrev
)))
348 (format "\\*\\|%s\\|%s\\.?"
349 (calendar-month-name month
)
350 (calendar-month-name month
'abbrev
)))
351 (month (concat "\\*\\|0*" (int-to-string month
)))
352 (day (concat "\\*\\|0*" (int-to-string day
)))
355 "\\*\\|0*" (int-to-string year
)
356 (if abbreviated-calendar-year
357 (concat "\\|" (format "%02d" (% year
100)))
361 "\\(\\`\\|\^M\\|\n\\)" mark
"?\\("
362 (mapconcat 'eval date-form
"\\)\\(")
364 (case-fold-search t
))
365 (goto-char (point-min))
366 (while (re-search-forward regexp nil t
)
367 (if backup
(re-search-backward "\\<" nil t
))
368 (if (and (or (char-equal (preceding-char) ?\^M
)
369 (char-equal (preceding-char) ?
\n))
370 (not (looking-at " \\|\^I")))
371 ;; Diary entry that consists only of date.
373 ;; Found a nonempty diary entry--make it visible and
374 ;; add it to the list.
376 (let ((entry-start (point))
378 (re-search-backward "\^M\\|\n\\|\\`")
379 (setq date-start
(point))
380 (re-search-forward "\^M\\|\n" nil t
2)
381 (while (looking-at " \\|\^I")
382 (re-search-forward "\^M\\|\n" nil t
))
384 (subst-char-in-region date-start
386 (setq entry
(buffer-substring entry-start
(point))
387 temp
(diary-pull-attrs entry file-glob-attrs
)
393 (1+ date-start
) (1- entry-start
))
394 (copy-marker entry-start
) (nth 1 temp
))))))
397 (not diary-list-include-blanks
)
398 (setq diary-entries-list
399 (append diary-entries-list
400 (list (list date
"" "" "" "")))))
402 (calendar-gregorian-from-absolute
403 (1+ (calendar-absolute-from-gregorian date
))))
404 (setq entry-found nil
)))
405 (set-buffer-modified-p diary-modified
))
406 (set-syntax-table old-diary-syntax-table
))
407 (goto-char (point-min))
408 (run-hooks 'nongregorian-diary-listing-hook
409 'list-diary-entries-hook
)
410 (if diary-display-hook
411 (run-hooks 'diary-display-hook
)
412 (simple-diary-display))
413 (run-hooks 'diary-hook
)
414 diary-entries-list
))))
416 (defun include-other-diary-files ()
417 "Include the diary entries from other diary files with those of diary-file.
418 This function is suitable for use in `list-diary-entries-hook';
419 it enables you to use shared diary files together with your own.
420 The files included are specified in the diaryfile by lines of this form:
421 #include \"filename\"
422 This is recursive; that is, #include directives in diary files thus included
423 are obeyed. You can change the `#include' to some other string by
424 changing the variable `diary-include-string'."
425 (goto-char (point-min))
426 (while (re-search-forward
428 "\\(\\`\\|\^M\\|\n\\)"
429 (regexp-quote diary-include-string
)
432 (let* ((diary-file (substitute-in-file-name
433 (buffer-substring-no-properties
434 (match-beginning 2) (match-end 2))))
435 (diary-list-include-blanks nil
)
436 (list-diary-entries-hook 'include-other-diary-files
)
437 (diary-display-hook 'ignore
)
439 (d-buffer (find-buffer-visiting diary-file
))
440 (diary-modified (if d-buffer
442 (set-buffer d-buffer
)
443 (buffer-modified-p)))))
444 (if (file-exists-p diary-file
)
445 (if (file-readable-p diary-file
)
447 (setq diary-entries-list
448 (append diary-entries-list
449 (list-diary-entries original-date number
)))
451 (set-buffer (find-buffer-visiting diary-file
))
452 (let ((inhibit-read-only t
))
453 (subst-char-in-region (point-min) (point-max) ?\^M ?
\n t
))
454 (setq selective-display nil
)
455 (set-buffer-modified-p diary-modified
)))
457 (message "Can't read included diary file %s" diary-file
)
460 (message "Can't find included diary file %s" diary-file
)
462 (goto-char (point-min)))
464 (defun simple-diary-display ()
465 "Display the diary buffer if there are any relevant entries or holidays."
466 (let* ((holiday-list (if holidays-in-diary-buffer
467 (check-calendar-holidays original-date
)))
468 (hol-string (format "%s%s%s"
470 (if holiday-list
": " "")
471 (mapconcat 'identity holiday-list
"; ")))
472 (msg (format "No diary entries for %s" hol-string
))
473 ;; If selected window is dedicated (to the calendar),
474 ;; need a new one to display the diary.
475 (pop-up-frames (window-dedicated-p (selected-window))))
476 (calendar-set-mode-line (format "Diary for %s" hol-string
))
477 (if (or (not diary-entries-list
)
478 (and (not (cdr diary-entries-list
))
479 (string-equal (car (cdr (car diary-entries-list
))) "")))
480 (if (< (length msg
) (frame-width))
482 (set-buffer (get-buffer-create holiday-buffer
))
483 (setq buffer-read-only nil
)
484 (calendar-set-mode-line date-string
)
486 (insert (mapconcat 'identity holiday-list
"\n"))
487 (goto-char (point-min))
488 (set-buffer-modified-p nil
)
489 (setq buffer-read-only t
)
490 (display-buffer holiday-buffer
)
491 (message "No diary entries for %s" date-string
))
492 (display-buffer (find-buffer-visiting
493 (substitute-in-file-name diary-file
)))
494 (message "Preparing diary...done"))))
496 (defface diary-button-face
'((((type pc
) (class color
))
497 (:foreground
"lightblue")))
498 "Default face used for buttons."
502 (define-button-type 'diary-entry
503 'action
#'diary-goto-entry
504 'face
#'diary-button-face
)
506 (defun diary-goto-entry (button)
507 (let ((marker (button-get button
'marker
)))
509 (pop-to-buffer (marker-buffer marker
))
510 (goto-char (marker-position marker
)))))
512 (defun fancy-diary-display ()
513 "Prepare a diary buffer with relevant entries in a fancy, noneditable form.
514 This function is provided for optional use as the `diary-display-hook'."
515 (save-excursion;; Turn off selective-display in the diary file's buffer.
516 (set-buffer (find-buffer-visiting (substitute-in-file-name diary-file
)))
517 (let ((diary-modified (buffer-modified-p)))
518 (subst-char-in-region (point-min) (point-max) ?\^M ?
\n t
)
519 (setq selective-display nil
)
520 (kill-local-variable 'mode-line-format
)
521 (set-buffer-modified-p diary-modified
)))
522 (if (or (not diary-entries-list
)
523 (and (not (cdr diary-entries-list
))
524 (string-equal (car (cdr (car diary-entries-list
))) "")))
525 (let* ((holiday-list (if holidays-in-diary-buffer
526 (check-calendar-holidays original-date
)))
527 (msg (format "No diary entries for %s %s"
528 (concat date-string
(if holiday-list
":" ""))
529 (mapconcat 'identity holiday-list
"; "))))
530 (if (<= (length msg
) (frame-width))
532 (set-buffer (get-buffer-create holiday-buffer
))
533 (setq buffer-read-only nil
)
534 (calendar-set-mode-line date-string
)
536 (insert (mapconcat 'identity holiday-list
"\n"))
537 (goto-char (point-min))
538 (set-buffer-modified-p nil
)
539 (setq buffer-read-only t
)
540 (display-buffer holiday-buffer
)
541 (message "No diary entries for %s" date-string
)))
542 (save-excursion;; Prepare the fancy diary buffer.
543 (set-buffer (make-fancy-diary-buffer))
544 (setq buffer-read-only nil
)
545 (let ((entry-list diary-entries-list
)
547 (holiday-list-last-month 1)
548 (holiday-list-last-year 1)
551 (if (not (calendar-date-equal date
(car (car entry-list
))))
553 (setq date
(car (car entry-list
)))
554 (and holidays-in-diary-buffer
555 (calendar-date-compare
556 (list (list holiday-list-last-month
557 (calendar-last-day-of-month
558 holiday-list-last-month
559 holiday-list-last-year
)
560 holiday-list-last-year
))
562 ;; We need to get the holidays for the next 3 months.
563 (setq holiday-list-last-month
564 (extract-calendar-month date
))
565 (setq holiday-list-last-year
566 (extract-calendar-year date
))
567 (increment-calendar-month
568 holiday-list-last-month holiday-list-last-year
1)
570 (let ((displayed-month holiday-list-last-month
)
571 (displayed-year holiday-list-last-year
))
572 (calendar-holiday-list)))
573 (increment-calendar-month
574 holiday-list-last-month holiday-list-last-year
1))
575 (let* ((date-string (calendar-date-string date
))
577 (let ((h holiday-list
)
579 ;; Make a list of all holidays for date.
581 (if (calendar-date-equal date
(car (car h
)))
582 (setq d
(append d
(cdr (car h
)))))
585 (insert (if (= (point) (point-min)) "" ?
\n) date-string
)
586 (if date-holiday-list
(insert ": "))
587 (let* ((l (current-column))
589 (insert (mapconcat (lambda (x)
590 (if (< longest
(length x
))
591 (setq longest
(length x
)))
594 (concat "\n" (make-string l ?
))))
595 (insert ?
\n (make-string (+ l longest
) ?
=) ?
\n)))))
597 (setq entry
(car (cdr (car entry-list
))))
598 (if (< 0 (length entry
))
600 (if (nth 3 (car entry-list
))
601 (insert-button (concat entry
"\n")
602 'marker
(nth 3 (car entry-list
))
606 (let* ((marks (nth 4 (car entry-list
)))
607 (temp-face (make-symbol
610 (mapcar '(lambda (sym)
616 ;; Remove :face info from the marks,
617 ;; copy the face info into temp-face
618 (setq faceinfo marks
)
619 (while (setq faceinfo
(memq :face faceinfo
))
620 (copy-face (read (nth 1 faceinfo
)) temp-face
)
621 (setcar faceinfo nil
)
622 (setcar (cdr faceinfo
) nil
))
623 (setq marks
(delq nil marks
))
624 ;; Apply the font aspects
625 (apply 'set-face-attribute temp-face nil marks
)
626 (search-backward entry
)
628 (make-overlay (match-beginning 0) (match-end 0))
630 (setq entry-list
(cdr entry-list
))))
631 (set-buffer-modified-p nil
)
632 (goto-char (point-min))
633 (setq buffer-read-only t
)
634 (display-buffer fancy-diary-buffer
)
635 (fancy-diary-display-mode)
636 (message "Preparing diary...done"))))
638 (defun make-fancy-diary-buffer ()
639 "Create and return the initial fancy diary buffer."
641 (set-buffer (get-buffer-create fancy-diary-buffer
))
642 (setq buffer-read-only nil
)
643 (calendar-set-mode-line "Diary Entries")
645 (set-buffer-modified-p nil
)
646 (setq buffer-read-only t
)
647 (get-buffer fancy-diary-buffer
)))
649 (defun print-diary-entries ()
650 "Print a hard copy of the diary display.
652 If the simple diary display is being used, prepare a temp buffer with the
653 visible lines of the diary buffer, add a heading line composed from the mode
654 line, print the temp buffer, and destroy it.
656 If the fancy diary display is being used, just print the buffer.
658 The hooks given by the variable `print-diary-entries-hook' are called to do
659 the actual printing."
661 (if (bufferp (get-buffer fancy-diary-buffer
))
663 (set-buffer (get-buffer fancy-diary-buffer
))
664 (run-hooks 'print-diary-entries-hook
))
666 (find-buffer-visiting (substitute-in-file-name diary-file
))))
668 (let ((temp-buffer (get-buffer-create "*Printable Diary Entries*"))
671 (set-buffer diary-buffer
)
673 (if (not (stringp mode-line-format
))
675 (string-match "^-*\\([^-].*[^-]\\)-*$" mode-line-format
)
676 (substring mode-line-format
677 (match-beginning 1) (match-end 1))))
678 (copy-to-buffer temp-buffer
(point-min) (point-max))
679 (set-buffer temp-buffer
)
680 (while (re-search-forward "\^M.*$" nil t
)
682 (goto-char (point-min))
684 (make-string (length heading
) ?
=) "\n")
685 (run-hooks 'print-diary-entries-hook
)
686 (kill-buffer temp-buffer
)))
687 (error "You don't have a diary buffer!")))))
689 (defun show-all-diary-entries ()
690 "Show all of the diary entries in the diary file.
691 This function gets rid of the selective display of the diary file so that
692 all entries, not just some, are visible. If there is no diary buffer, one
695 (let ((d-file (diary-check-diary-file)))
697 (set-buffer (or (find-buffer-visiting d-file
)
698 (find-file-noselect d-file t
)))
699 (let ((buffer-read-only nil
)
700 (diary-modified (buffer-modified-p)))
701 (subst-char-in-region (point-min) (point-max) ?\^M ?
\n t
)
702 (setq selective-display nil
703 mode-line-format default-mode-line-format
)
704 (display-buffer (current-buffer))
705 (set-buffer-modified-p diary-modified
)))))
707 (defcustom diary-mail-addr
708 (if (boundp 'user-mail-address
) user-mail-address
"")
709 "*Email address that `diary-mail-entries' will send email to."
714 (defcustom diary-mail-days
7
715 "*Default number of days for `diary-mail-entries' to check."
721 (defun diary-mail-entries (&optional ndays
)
722 "Send a mail message showing diary entries for next NDAYS days.
723 If no prefix argument is given, NDAYS is set to `diary-mail-days'.
724 Mail is sent to the address specified by `diary-mail-addr'.
726 You can call `diary-mail-entries' every night using an at/cron job.
727 For example, this script will run the program at 2am daily. Since
728 `emacs -batch' does not load your `.emacs' file, you must ensure that
729 all relevant variables are set, as done here.
732 # diary-rem.sh -- repeatedly run the Emacs diary-reminder
734 -eval \"(setq diary-mail-days 3 \\
735 diary-file \\\"/path/to/diary.file\\\" \\
736 european-calendar-style t \\
737 diary-mail-addr \\\"user@host.name\\\" )\" \\
738 -l diary-lib -f diary-mail-entries
739 at -f diary-rem.sh 0200 tomorrow
741 You may have to tweak the syntax of the `at' command to suit your
742 system. Alternatively, you can specify a cron entry:
743 0 1 * * * diary-rem.sh
744 to run it every morning at 1am."
746 (if (string-equal diary-mail-addr
"")
747 (error "You must set `diary-mail-addr' to use this command")
748 (let ((diary-display-hook 'fancy-diary-display
))
749 (list-diary-entries (calendar-current-date) (or ndays diary-mail-days
)))
750 (compose-mail diary-mail-addr
751 (concat "Diary entries generated "
752 (calendar-date-string (calendar-current-date))))
754 (if (get-buffer fancy-diary-buffer
)
756 (set-buffer fancy-diary-buffer
)
757 (buffer-substring (point-min) (point-max)))
759 (call-interactively (get mail-user-agent
'sendfunc
))))
761 (defun diary-name-pattern (string-array &optional abbrev-array paren
)
762 "Return a regexp matching the strings in the array STRING-ARRAY.
763 If the optional argument ABBREV-ARRAY is present, then the function
764 `calendar-abbrev-construct' is used to construct abbreviations from the
765 two supplied arrays. The returned regexp will then also match these
766 abbreviations, with or without final `.' characters. If the optional
767 argument PAREN is non-nil, the regexp is surrounded by parentheses."
768 (regexp-opt (append string-array
770 (calendar-abbrev-construct abbrev-array
773 (calendar-abbrev-construct abbrev-array
779 (defvar marking-diary-entries nil
780 "True during the marking of diary entries, nil otherwise.")
782 (defvar marking-diary-entry nil
783 "True during the marking of diary entries, if current entry is marking.")
785 (defun mark-diary-entries ()
786 "Mark days in the calendar window that have diary entries.
787 Each entry in the diary file visible in the calendar window is marked.
788 After the entries are marked, the hooks `nongregorian-diary-marking-hook' and
789 `mark-diary-entries-hook' are run."
791 (setq mark-diary-entries-in-calendar t
)
792 (let ((marking-diary-entries t
)
793 file-glob-attrs marks
)
795 (set-buffer (find-file-noselect (diary-check-diary-file) t
))
796 (message "Marking diary entries...")
797 (setq file-glob-attrs
(nth 1 (diary-pull-attrs nil
'())))
798 (let ((d diary-date-forms
)
799 (old-diary-syntax-table (syntax-table))
801 (set-syntax-table diary-syntax-table
)
803 (let* ((date-form (if (equal (car (car d
)) 'backup
)
805 (car d
)));; ignore 'backup directive
807 (diary-name-pattern calendar-day-name-array
808 calendar-day-abbrev-array
))
811 (diary-name-pattern calendar-month-name-array
812 calendar-month-abbrev-array
)))
813 (month "[0-9]+\\|\\*")
815 (year "[0-9]+\\|\\*")
816 (l (length date-form
))
817 (d-name-pos (- l
(length (memq 'dayname date-form
))))
818 (d-name-pos (if (/= l d-name-pos
) (+ 2 d-name-pos
)))
819 (m-name-pos (- l
(length (memq 'monthname date-form
))))
820 (m-name-pos (if (/= l m-name-pos
) (+ 2 m-name-pos
)))
821 (d-pos (- l
(length (memq 'day date-form
))))
822 (d-pos (if (/= l d-pos
) (+ 2 d-pos
)))
823 (m-pos (- l
(length (memq 'month date-form
))))
824 (m-pos (if (/= l m-pos
) (+ 2 m-pos
)))
825 (y-pos (- l
(length (memq 'year date-form
))))
826 (y-pos (if (/= l y-pos
) (+ 2 y-pos
)))
829 "\\(\\`\\|\^M\\|\n\\)\\("
830 (mapconcat 'eval date-form
"\\)\\(")
832 (case-fold-search t
))
833 (goto-char (point-min))
834 (while (re-search-forward regexp nil t
)
837 (buffer-substring-no-properties
838 (match-beginning d-name-pos
)
839 (match-end d-name-pos
))))
842 (buffer-substring-no-properties
843 (match-beginning m-name-pos
)
844 (match-end m-name-pos
))))
847 (buffer-substring-no-properties
848 (match-beginning m-pos
)
853 (buffer-substring-no-properties
854 (match-beginning d-pos
)
858 (buffer-substring-no-properties
859 (match-beginning y-pos
)
863 (if (and (= (length y-str
) 2)
864 abbreviated-calendar-year
)
866 (extract-calendar-year
867 (calendar-current-date)))
868 (y (+ (string-to-int y-str
)
870 (/ current-y
100)))))
871 (if (> (- y current-y
) 50)
873 (if (> (- current-y y
) 50)
876 (string-to-int y-str
))))
878 (setq entry
(buffer-substring-no-properties
879 (point) (line-end-position))
880 temp
(diary-pull-attrs entry file-glob-attrs
)
882 marks
(nth 1 temp
))))
884 (mark-calendar-days-named
885 (cdr (assoc-ignore-case
888 calendar-day-name-array
889 0 nil calendar-day-abbrev-array
))) marks
)
892 (if (string-equal mm-name
"*") 0
893 (cdr (assoc-ignore-case
896 calendar-month-name-array
897 1 nil calendar-month-abbrev-array
))))))
898 (mark-calendar-date-pattern mm dd yy marks
))))
900 (mark-sexp-diary-entries)
901 (run-hooks 'nongregorian-diary-marking-hook
902 'mark-diary-entries-hook
)
903 (set-syntax-table old-diary-syntax-table
)
904 (message "Marking diary entries...done")))))
906 (defun mark-sexp-diary-entries ()
907 "Mark days in the calendar window that have sexp diary entries.
908 Each entry in the diary file (or included files) visible in the calendar window
909 is marked. See the documentation for the function `list-sexp-diary-entries'."
910 (let* ((sexp-mark (regexp-quote sexp-diary-entry-symbol
))
911 (s-entry (concat "\\(\\`\\|\^M\\|\n\\)\\("
912 sexp-mark
"(\\)\\|\\("
913 (regexp-quote diary-nonmarking-symbol
)
914 sexp-mark
"(diary-remind\\)"))
915 (file-glob-attrs (nth 1 (diary-pull-attrs nil
'())))
916 m y first-date last-date mark file-glob-attrs
)
918 (set-buffer calendar-buffer
)
919 (setq m displayed-month
)
920 (setq y displayed-year
))
921 (increment-calendar-month m y -
1)
923 (calendar-absolute-from-gregorian (list m
1 y
)))
924 (increment-calendar-month m y
2)
926 (calendar-absolute-from-gregorian
927 (list m
(calendar-last-day-of-month m y
) y
)))
928 (goto-char (point-min))
929 (while (re-search-forward s-entry nil t
)
930 (setq marking-diary-entry
(char-equal (preceding-char) ?\
())
931 (re-search-backward "(")
932 (let ((sexp-start (point))
933 sexp entry entry-start line-start marks
)
935 (setq sexp
(buffer-substring-no-properties sexp-start
(point)))
937 (re-search-backward "\^M\\|\n\\|\\`")
938 (setq line-start
(point)))
940 (if (and (or (char-equal (preceding-char) ?\^M
)
941 (char-equal (preceding-char) ?
\n))
942 (not (looking-at " \\|\^I")))
943 (progn;; Diary entry consists only of the sexp
946 (setq entry-start
(point))
948 (re-search-forward "\^M\\|\n" nil t
)
949 (while (looking-at " \\|\^I")
950 (or (re-search-forward "\^M\\|\n" nil t
)
951 (re-search-forward "$" nil t
)))
952 (if (or (char-equal (preceding-char) ?\^M
)
953 (char-equal (preceding-char) ?
\n))
955 (setq entry
(buffer-substring-no-properties entry-start
(point)))
956 (while (string-match "[\^M]" entry
)
957 (aset entry
(match-beginning 0) ?
\n )))
958 (calendar-for-loop date from first-date to last-date do
959 (if (setq mark
(diary-sexp-entry sexp entry
960 (calendar-gregorian-from-absolute date
)))
962 (setq marks
(diary-pull-attrs entry file-glob-attrs
)
963 marks
(nth 1 (diary-pull-attrs entry file-glob-attrs
)))
964 (mark-visible-calendar-date
965 (calendar-gregorian-from-absolute date
)
966 (if (< 0 (length marks
))
971 (defun mark-included-diary-files ()
972 "Mark the diary entries from other diary files with those of the diary file.
973 This function is suitable for use as the `mark-diary-entries-hook'; it enables
974 you to use shared diary files together with your own. The files included are
975 specified in the diary-file by lines of this form:
976 #include \"filename\"
977 This is recursive; that is, #include directives in diary files thus included
978 are obeyed. You can change the `#include' to some other string by
979 changing the variable `diary-include-string'."
980 (goto-char (point-min))
981 (while (re-search-forward
983 "\\(\\`\\|\^M\\|\n\\)"
984 (regexp-quote diary-include-string
)
987 (let ((diary-file (substitute-in-file-name
988 (buffer-substring-no-properties
989 (match-beginning 2) (match-end 2))))
990 (mark-diary-entries-hook 'mark-included-diary-files
))
991 (if (file-exists-p diary-file
)
992 (if (file-readable-p diary-file
)
995 (kill-buffer (find-buffer-visiting diary-file
)))
997 (message "Can't read included diary file %s" diary-file
)
1000 (message "Can't find included diary file %s" diary-file
)
1002 (goto-char (point-min)))
1004 (defun mark-calendar-days-named (dayname &optional color
)
1005 "Mark all dates in the calendar window that are day DAYNAME of the week.
1006 0 means all Sundays, 1 means all Mondays, and so on."
1008 (set-buffer calendar-buffer
)
1009 (let ((prev-month displayed-month
)
1010 (prev-year displayed-year
)
1011 (succ-month displayed-month
)
1012 (succ-year displayed-year
)
1015 (increment-calendar-month succ-month succ-year
1)
1016 (increment-calendar-month prev-month prev-year -
1)
1017 (setq day
(calendar-absolute-from-gregorian
1018 (calendar-nth-named-day 1 dayname prev-month prev-year
)))
1019 (setq last-day
(calendar-absolute-from-gregorian
1020 (calendar-nth-named-day -
1 dayname succ-month succ-year
)))
1021 (while (<= day last-day
)
1022 (mark-visible-calendar-date (calendar-gregorian-from-absolute day
) color
)
1023 (setq day
(+ day
7))))))
1025 (defun mark-calendar-date-pattern (month day year
&optional color
)
1026 "Mark all dates in the calendar window that conform to MONTH/DAY/YEAR.
1027 A value of 0 in any position is a wildcard."
1029 (set-buffer calendar-buffer
)
1030 (let ((m displayed-month
)
1032 (increment-calendar-month m y -
1)
1033 (calendar-for-loop i from
0 to
2 do
1034 (mark-calendar-month m y month day year color
)
1035 (increment-calendar-month m y
1)))))
1037 (defun mark-calendar-month (month year p-month p-day p-year
&optional color
)
1038 "Mark dates in the MONTH/YEAR that conform to pattern P-MONTH/P_DAY/P-YEAR.
1039 A value of 0 in any position of the pattern is a wildcard."
1040 (if (or (and (= month p-month
)
1041 (or (= p-year
0) (= year p-year
)))
1043 (or (= p-year
0) (= year p-year
))))
1046 i from
1 to
(calendar-last-day-of-month month year
) do
1047 (mark-visible-calendar-date (list month i year
) color
))
1048 (mark-visible-calendar-date (list month p-day year
) color
))))
1050 (defun sort-diary-entries ()
1051 "Sort the list of diary entries by time of day."
1052 (setq diary-entries-list
(sort diary-entries-list
'diary-entry-compare
)))
1054 (defun diary-entry-compare (e1 e2
)
1055 "Returns t if E1 is earlier than E2."
1056 (or (calendar-date-compare e1 e2
)
1057 (and (calendar-date-equal (car e1
) (car e2
))
1058 (let* ((ts1 (cadr e1
)) (t1 (diary-entry-time ts1
))
1059 (ts2 (cadr e2
)) (t2 (diary-entry-time ts2
)))
1062 (string-lessp ts1 ts2
)))))))
1064 (defcustom diary-unknown-time
1066 "*Value returned by diary-entry-time when no time is found.
1067 The default value -9999 causes entries with no recognizable time to be placed
1068 before those with times; 9999 would place entries with no recognizable time
1069 after those with times."
1074 (defun diary-entry-time (s)
1075 "Return time at the beginning of the string S as a military-style integer.
1076 For example, returns 1325 for 1:25pm.
1077 Returns `diary-unknown-time' (default value -9999) if no time is recognized. The recognized forms are XXXX, X:XX, or
1078 XX:XX (military time), and XXam, XXAM, XXpm, XXPM, XX:XXam, XX:XXAM XX:XXpm,
1080 (let ((case-fold-search nil
))
1081 (cond ((string-match ; Military time
1082 "\\`[ \t\n\\^M]*\\([0-9]?[0-9]\\):?\\([0-9][0-9]\\)\\(\\>\\|[^ap]\\)" s
)
1083 (+ (* 100 (string-to-int
1084 (substring s
(match-beginning 1) (match-end 1))))
1085 (string-to-int (substring s
(match-beginning 2) (match-end 2)))))
1086 ((string-match ; Hour only XXam or XXpm
1087 "\\`[ \t\n\\^M]*\\([0-9]?[0-9]\\)\\([ap]\\)m\\>" s
)
1088 (+ (* 100 (%
(string-to-int
1089 (substring s
(match-beginning 1) (match-end 1)))
1091 (if (equal ?a
(downcase (aref s
(match-beginning 2))))
1093 ((string-match ; Hour and minute XX:XXam or XX:XXpm
1094 "\\`[ \t\n\\^M]*\\([0-9]?[0-9]\\):\\([0-9][0-9]\\)\\([ap]\\)m\\>" s
)
1095 (+ (* 100 (%
(string-to-int
1096 (substring s
(match-beginning 1) (match-end 1)))
1098 (string-to-int (substring s
(match-beginning 2) (match-end 2)))
1099 (if (equal ?a
(downcase (aref s
(match-beginning 3))))
1101 (t diary-unknown-time
)))) ; Unrecognizable
1103 (defun list-sexp-diary-entries (date)
1104 "Add sexp entries for DATE from the diary file to `diary-entries-list'.
1105 Also, Make them visible in the diary file. Returns t if any entries were
1108 Sexp diary entries must be prefaced by a `sexp-diary-entry-symbol' (normally
1109 `%%'). The form of a sexp diary entry is
1113 Both ENTRY and DATE are globally available when the SEXP is evaluated. If the
1114 SEXP yields the value nil, the diary entry does not apply. If it yields a
1115 non-nil value, ENTRY will be taken to apply to DATE; if the non-nil value is a
1116 string, that string will be the diary entry in the fancy diary display.
1118 For example, the following diary entry will apply to the 21st of the month
1119 if it is a weekday and the Friday before if the 21st is on a weekend:
1121 &%%(let ((dayname (calendar-day-of-week date))
1122 (day (extract-calendar-day date)))
1124 (and (= day 21) (memq dayname '(1 2 3 4 5)))
1125 (and (memq day '(19 20)) (= dayname 5)))
1126 ) UIUC pay checks deposited
1128 A number of built-in functions are available for this type of diary entry:
1130 %%(diary-date MONTH DAY YEAR &optional MARK) text
1131 Entry applies if date is MONTH, DAY, YEAR if
1132 `european-calendar-style' is nil, and DAY, MONTH, YEAR if
1133 `european-calendar-style' is t. DAY, MONTH, and YEAR
1134 can be lists of integers, the constant t, or an integer.
1135 The constant t means all values. An optional parameter
1136 MARK specifies a face or single-character string to use
1137 when highlighting the day in the calendar.
1139 %%(diary-float MONTH DAYNAME N &optional DAY MARK) text
1140 Entry will appear on the Nth DAYNAME of MONTH.
1141 (DAYNAME=0 means Sunday, 1 means Monday, and so on;
1142 if N is negative it counts backward from the end of
1143 the month. MONTH can be a list of months, a single
1144 month, or t to specify all months. Optional DAY means
1145 Nth DAYNAME of MONTH on or after/before DAY. DAY defaults
1146 to 1 if N>0 and the last day of the month if N<0. An
1147 optional parameter MARK specifies a face or single-character
1148 string to use when highlighting the day in the calendar.
1150 %%(diary-block M1 D1 Y1 M2 D2 Y2 &optional MARK) text
1151 Entry will appear on dates between M1/D1/Y1 and M2/D2/Y2,
1152 inclusive. (If `european-calendar-style' is t, the
1153 order of the parameters should be changed to D1, M1, Y1,
1154 D2, M2, Y2.) An optional parameter MARK specifies a face
1155 or single-character string to use when highlighting the
1156 day in the calendar.
1158 %%(diary-anniversary MONTH DAY YEAR &optional MARK) text
1159 Entry will appear on anniversary dates of MONTH DAY, YEAR.
1160 (If `european-calendar-style' is t, the order of the
1161 parameters should be changed to DAY, MONTH, YEAR.) Text
1162 can contain %d or %d%s; %d will be replaced by the number
1163 of years since the MONTH DAY, YEAR and %s will be replaced
1164 by the ordinal ending of that number (that is, `st', `nd',
1165 `rd' or `th', as appropriate. The anniversary of February
1166 29 is considered to be March 1 in a non-leap year. An
1167 optional parameter MARK specifies a face or single-character
1168 string to use when highlighting the day in the calendar.
1170 %%(diary-cyclic N MONTH DAY YEAR &optional MARK) text
1171 Entry will appear every N days, starting MONTH DAY, YEAR.
1172 (If `european-calendar-style' is t, the order of the
1173 parameters should be changed to N, DAY, MONTH, YEAR.) Text
1174 can contain %d or %d%s; %d will be replaced by the number
1175 of repetitions since the MONTH DAY, YEAR and %s will
1176 be replaced by the ordinal ending of that number (that is,
1177 `st', `nd', `rd' or `th', as appropriate. An optional
1178 parameter MARK specifies a face or single-character string
1179 to use when highlighting the day in the calendar.
1181 %%(diary-remind SEXP DAYS &optional MARKING) text
1182 Entry is a reminder for diary sexp SEXP. DAYS is either a
1183 single number or a list of numbers indicating the number(s)
1184 of days before the event that the warning(s) should occur.
1185 If the current date is (one of) DAYS before the event
1186 indicated by EXPR, then a suitable message (as specified
1187 by `diary-remind-message') appears. In addition to the
1188 reminders beforehand, the diary entry also appears on
1189 the date itself. If optional MARKING is non-nil then the
1190 *reminders* are marked on the calendar. Marking of
1191 reminders is independent of whether the entry *itself* is
1192 a marking or nonmarking one.
1194 %%(diary-day-of-year)
1195 Diary entries giving the day of the year and the number of
1196 days remaining in the year will be made every day. Note
1197 that since there is no text, it makes sense only if the
1198 fancy diary display is used.
1201 Diary entries giving the corresponding ISO commercial date
1202 will be made every day. Note that since there is no text,
1203 it makes sense only if the fancy diary display is used.
1205 %%(diary-french-date)
1206 Diary entries giving the corresponding French Revolutionary
1207 date will be made every day. Note that since there is no
1208 text, it makes sense only if the fancy diary display is used.
1210 %%(diary-islamic-date)
1211 Diary entries giving the corresponding Islamic date will be
1212 made every day. Note that since there is no text, it
1213 makes sense only if the fancy diary display is used.
1215 %%(diary-hebrew-date)
1216 Diary entries giving the corresponding Hebrew date will be
1217 made every day. Note that since there is no text, it
1218 makes sense only if the fancy diary display is used.
1220 %%(diary-astro-day-number) Diary entries giving the corresponding
1221 astronomical (Julian) day number will be made every day.
1222 Note that since there is no text, it makes sense only if the
1223 fancy diary display is used.
1225 %%(diary-julian-date) Diary entries giving the corresponding
1226 Julian date will be made every day. Note that since
1227 there is no text, it makes sense only if the fancy diary
1230 %%(diary-sunrise-sunset)
1231 Diary entries giving the local times of sunrise and sunset
1232 will be made every day. Note that since there is no text,
1233 it makes sense only if the fancy diary display is used.
1234 Floating point required.
1236 %%(diary-phases-of-moon)
1237 Diary entries giving the times of the phases of the moon
1238 will be when appropriate. Note that since there is no text,
1239 it makes sense only if the fancy diary display is used.
1240 Floating point required.
1242 %%(diary-yahrzeit MONTH DAY YEAR) text
1243 Text is assumed to be the name of the person; the date is
1244 the date of death on the *civil* calendar. The diary entry
1245 will appear on the proper Hebrew-date anniversary and on the
1246 day before. (If `european-calendar-style' is t, the order
1247 of the parameters should be changed to DAY, MONTH, YEAR.)
1249 %%(diary-rosh-hodesh)
1250 Diary entries will be made on the dates of Rosh Hodesh on
1251 the Hebrew calendar. Note that since there is no text, it
1252 makes sense only if the fancy diary display is used.
1255 Diary entries giving the weekly parasha will be made on
1256 every Saturday. Note that since there is no text, it
1257 makes sense only if the fancy diary display is used.
1260 Diary entries giving the omer count will be made every day
1261 from Passover to Shavuot. Note that since there is no text,
1262 it makes sense only if the fancy diary display is used.
1264 Marking these entries is *extremely* time consuming, so these entries are
1265 best if they are nonmarking."
1266 (let ((s-entry (concat "\\(\\`\\|\^M\\|\n\\)"
1267 (regexp-quote diary-nonmarking-symbol
)
1269 (regexp-quote sexp-diary-entry-symbol
)
1271 entry-found file-glob-attrs marks
)
1272 (goto-char (point-min))
1274 (setq file-glob-attrs
(nth 1 (diary-pull-attrs nil
'()))))
1275 (while (re-search-forward s-entry nil t
)
1277 (let ((sexp-start (point))
1278 sexp entry specifier entry-start line-start
)
1280 (setq sexp
(buffer-substring-no-properties sexp-start
(point)))
1282 (re-search-backward "\^M\\|\n\\|\\`")
1283 (setq line-start
(point)))
1285 (buffer-substring-no-properties (1+ line-start
) (point))
1286 entry-start
(1+ line-start
))
1288 (if (and (or (char-equal (preceding-char) ?\^M
)
1289 (char-equal (preceding-char) ?
\n))
1290 (not (looking-at " \\|\^I")))
1291 (progn;; Diary entry consists only of the sexp
1294 (setq entry-start
(point))
1295 (re-search-forward "\^M\\|\n" nil t
)
1296 (while (looking-at " \\|\^I")
1297 (re-search-forward "\^M\\|\n" nil t
))
1299 (setq entry
(buffer-substring-no-properties entry-start
(point)))
1300 (while (string-match "[\^M]" entry
)
1301 (aset entry
(match-beginning 0) ?
\n )))
1302 (let ((diary-entry (diary-sexp-entry sexp entry date
))
1304 (setq entry
(if (consp diary-entry
)
1309 (subst-char-in-region line-start
(point) ?\^M ?
\n t
)
1310 (if (< 0 (length entry
))
1311 (setq temp
(diary-pull-attrs entry file-glob-attrs
)
1313 marks
(nth 1 temp
)))))
1314 (add-to-diary-list date
1317 (if entry-start
(copy-marker entry-start
)
1320 (setq entry-found
(or entry-found diary-entry
)))))
1323 (defun diary-sexp-entry (sexp entry date
)
1324 "Process a SEXP diary ENTRY for DATE."
1325 (let ((result (if calendar-debug-sexp
1326 (let ((stack-trace-on-error t
))
1327 (eval (car (read-from-string sexp
))))
1329 (eval (car (read-from-string sexp
)))
1332 (message "Bad sexp at line %d in %s: %s"
1335 (narrow-to-region 1 (point))
1336 (goto-char (point-min))
1338 (while (re-search-forward "\n\\|\^M" nil t
)
1339 (setq lines
(1+ lines
)))
1343 (cond ((stringp result
) result
)
1344 ((and (consp result
)
1345 (stringp (cdr result
))) result
)
1349 (defun diary-date (month day year
&optional mark
)
1350 "Specific date(s) diary entry.
1351 Entry applies if date is MONTH, DAY, YEAR if `european-calendar-style' is nil,
1352 and DAY, MONTH, YEAR if `european-calendar-style' is t. DAY, MONTH, and YEAR
1353 can be lists of integers, the constant t, or an integer. The constant t means
1356 An optional parameter MARK specifies a face or single-character string to
1357 use when highlighting the day in the calendar."
1358 (let ((dd (if european-calendar-style
1361 (mm (if european-calendar-style
1364 (m (extract-calendar-month date
))
1365 (y (extract-calendar-year date
))
1366 (d (extract-calendar-day date
)))
1368 (or (and (listp dd
) (memq d dd
))
1371 (or (and (listp mm
) (memq m mm
))
1374 (or (and (listp year
) (memq y year
))
1377 (cons mark entry
))))
1379 (defun diary-block (m1 d1 y1 m2 d2 y2
&optional mark
)
1381 Entry applies if date is between, or on one of, two dates.
1382 The order of the parameters is
1383 M1, D1, Y1, M2, D2, Y2 if `european-calendar-style' is nil, and
1384 D1, M1, Y1, D2, M2, Y2 if `european-calendar-style' is t.
1386 An optional parameter MARK specifies a face or single-character string to
1387 use when highlighting the day in the calendar."
1389 (let ((date1 (calendar-absolute-from-gregorian
1390 (if european-calendar-style
1393 (date2 (calendar-absolute-from-gregorian
1394 (if european-calendar-style
1397 (d (calendar-absolute-from-gregorian date
)))
1398 (if (and (<= date1 d
) (<= d date2
))
1399 (cons mark entry
))))
1401 (defun diary-float (month dayname n
&optional day mark
)
1402 "Floating diary entry--entry applies if date is the nth dayname of month.
1403 Parameters are MONTH, DAYNAME, N. MONTH can be a list of months, the constant
1404 t, or an integer. The constant t means all months. If N is negative, count
1405 backward from the end of the month.
1407 An optional parameter DAY means the Nth DAYNAME on or after/before MONTH DAY.
1408 Optional MARK specifies a face or single-character string to use when
1409 highlighting the day in the calendar."
1410 ;; This is messy because the diary entry may apply, but the date on which it
1411 ;; is based can be in a different month/year. For example, asking for the
1412 ;; first Monday after December 30. For large values of |n| the problem is
1414 (and (= dayname
(calendar-day-of-week date
))
1415 (let* ((m (extract-calendar-month date
))
1416 (d (extract-calendar-day date
))
1417 (y (extract-calendar-year date
))
1418 (limit; last (n>0) or first (n<0) possible base date for entry
1419 (calendar-nth-named-absday (- n
) dayname m y d
))
1420 (last-abs (if (> n
0) limit
(+ limit
6)))
1421 (first-abs (if (> n
0) (- limit
6) limit
))
1422 (last (calendar-gregorian-from-absolute last-abs
))
1423 (first (calendar-gregorian-from-absolute first-abs
))
1424 ; m1, d1 is first possible base date
1425 (m1 (extract-calendar-month first
))
1426 (d1 (extract-calendar-day first
))
1427 (y1 (extract-calendar-year first
))
1428 ; m2, d2 is last possible base date
1429 (m2 (extract-calendar-month last
))
1430 (d2 (extract-calendar-day last
))
1431 (y2 (extract-calendar-year last
)))
1432 (if (or (and (= m1 m2
) ; only possible base dates in one month
1437 (let ((d (or day
(if (> n
0)
1439 (calendar-last-day-of-month m1 y1
)))))
1440 (and (<= d1 d
) (<= d d2
))))
1441 ;; only possible base dates straddle two months
1443 (and (= y1 y2
) (< m1 m2
)))
1445 ;; m1, d1 works as a base date
1451 (<= d1
(or day
(if (> n
0)
1453 (calendar-last-day-of-month m1 y1
)))))
1454 ;; m2, d2 works as a base date
1455 (and (or (eq month t
)
1459 (<= (or day
(if (> n
0)
1461 (calendar-last-day-of-month m2 y2
)))
1463 (cons mark entry
)))))
1466 (defun diary-anniversary (month day year
&optional mark
)
1467 "Anniversary diary entry.
1468 Entry applies if date is the anniversary of MONTH, DAY, YEAR if
1469 `european-calendar-style' is nil, and DAY, MONTH, YEAR if
1470 `european-calendar-style' is t. Diary entry can contain `%d' or `%d%s'; the
1471 %d will be replaced by the number of years since the MONTH DAY, YEAR and the
1472 %s will be replaced by the ordinal ending of that number (that is, `st', `nd',
1473 `rd' or `th', as appropriate. The anniversary of February 29 is considered
1474 to be March 1 in non-leap years.
1476 An optional parameter MARK specifies a face or single-character string to
1477 use when highlighting the day in the calendar."
1478 (let* ((d (if european-calendar-style
1481 (m (if european-calendar-style
1484 (y (extract-calendar-year date
))
1486 (if (and (= m
2) (= d
29) (not (calendar-leap-year-p y
)))
1489 (if (and (> diff
0) (calendar-date-equal (list m d y
) date
))
1490 (cons mark
(format entry diff
(diary-ordinal-suffix diff
))))))
1492 (defun diary-cyclic (n month day year
&optional mark
)
1493 "Cycle diary entry--entry applies every N days starting at MONTH, DAY, YEAR.
1494 If `european-calendar-style' is t, parameters are N, DAY, MONTH, YEAR.
1495 ENTRY can contain `%d' or `%d%s'; the %d will be replaced by the number of
1496 repetitions since the MONTH DAY, YEAR and %s will be replaced by the
1497 ordinal ending of that number (that is, `st', `nd', `rd' or `th', as
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 (diff (- (calendar-absolute-from-gregorian date
)
1509 (calendar-absolute-from-gregorian
1512 (if (and (>= diff
0) (zerop (% diff n
)))
1513 (cons mark
(format entry cycle
(diary-ordinal-suffix cycle
))))))
1515 (defun diary-ordinal-suffix (n)
1516 "Ordinal suffix for N. (That is, `st', `nd', `rd', or `th', as appropriate.)"
1517 (if (or (memq (% n
100) '(11 12 13))
1520 (aref ["th" "st" "nd" "rd"] (% n
10))))
1522 (defun diary-day-of-year ()
1523 "Day of year and number of days remaining in the year of date diary entry."
1524 (calendar-day-of-year-string date
))
1526 (defcustom diary-remind-message
1528 (if (= 0 (% days
7))
1529 (concat (int-to-string (/ days
7)) (if (= 7 days
) " week" " weeks"))
1530 (concat (int-to-string days
) (if (= 1 days
) " day" " days")))
1533 "*Pseudo-pattern giving form of reminder messages in the fancy diary
1536 Used by the function `diary-remind', a pseudo-pattern is a list of
1537 expressions that can involve the keywords `days' (a number), `date' (a list of
1538 month, day, year), and `diary-entry' (a string)."
1542 (defun diary-remind (sexp days
&optional marking
)
1543 "Provide a reminder of a diary entry.
1544 SEXP is a diary-sexp. DAYS is either a single number or a list of numbers
1545 indicating the number(s) of days before the event that the warning(s) should
1546 occur on. If the current date is (one of) DAYS before the event indicated by
1547 SEXP, then a suitable message (as specified by `diary-remind-message' is
1550 In addition to the reminders beforehand, the diary entry also appears on the
1553 A `diary-nonmarking-symbol' at the beginning of the line of the diary-remind
1554 entry specifies that the diary entry (not the reminder) is non-marking.
1555 Marking of reminders is independent of whether the entry itself is a marking
1556 or nonmarking; if optional parameter MARKING is non-nil then the reminders are
1557 marked on the calendar."
1558 (let ((diary-entry (eval sexp
)))
1560 ;; Diary entry applies on date
1562 (or (not marking-diary-entries
) marking-diary-entry
))
1564 ;; Diary entry may apply to `days' before date
1565 ((and (integerp days
)
1566 (not diary-entry
); Diary entry does not apply to date
1567 (or (not marking-diary-entries
) marking
))
1568 (let ((date (calendar-gregorian-from-absolute
1569 (+ (calendar-absolute-from-gregorian date
) days
))))
1570 (if (setq diary-entry
(eval sexp
))
1571 (mapconcat 'eval diary-remind-message
""))))
1572 ;; Diary entry may apply to one of a list of days before date
1573 ((and (listp days
) days
)
1574 (or (diary-remind sexp
(car days
) marking
)
1575 (diary-remind sexp
(cdr days
) marking
))))))
1577 (defun add-to-diary-list (date string specifier marker
&optional globcolor
)
1578 "Add the entry (DATE STRING SPECIFIER MARKER GLOBCOLOR) to `diary-entries-list'.
1579 Do nothing if DATE or STRING is nil."
1580 (when (and date string
)
1581 (if diary-file-name-prefix
1582 (let ((prefix (funcall diary-file-name-prefix-function
1583 (buffer-file-name))))
1584 (or (string= prefix
"")
1585 (setq string
(format "[%s] %s" prefix string
)))))
1586 (setq diary-entries-list
1587 (append diary-entries-list
1588 (list (list date string specifier marker globcolor
))))))
1590 (defun make-diary-entry (string &optional nonmarking file
)
1591 "Insert a diary entry STRING which may be NONMARKING in FILE.
1592 If omitted, NONMARKING defaults to nil and FILE defaults to `diary-file'."
1593 (let ((pop-up-frames (window-dedicated-p (selected-window))))
1594 (find-file-other-window (substitute-in-file-name (or file diary-file
))))
1596 (goto-char (point-max))
1597 (when (let ((case-fold-search t
))
1598 (search-backward "Local Variables:"
1599 (max (- (point-max) 3000) (point-min))
1606 (if nonmarking diary-nonmarking-symbol
"")
1609 (defun insert-diary-entry (arg)
1610 "Insert a diary entry for the date indicated by point.
1611 Prefix arg will make the entry nonmarking."
1613 (make-diary-entry (calendar-date-string (calendar-cursor-to-date t
) t t
)
1616 (defun insert-weekly-diary-entry (arg)
1617 "Insert a weekly diary entry for the day of the week indicated by point.
1618 Prefix arg will make the entry nonmarking."
1620 (make-diary-entry (calendar-day-name (calendar-cursor-to-date t
))
1623 (defun insert-monthly-diary-entry (arg)
1624 "Insert a monthly diary entry for the day of the month indicated by point.
1625 Prefix arg will make the entry nonmarking."
1627 (let ((calendar-date-display-form
1628 (if european-calendar-style
1631 (make-diary-entry (calendar-date-string (calendar-cursor-to-date t
) t
)
1634 (defun insert-yearly-diary-entry (arg)
1635 "Insert an annual diary entry for the day of the year indicated by point.
1636 Prefix arg will make the entry nonmarking."
1638 (let ((calendar-date-display-form
1639 (if european-calendar-style
1640 '(day " " monthname
)
1641 '(monthname " " day
))))
1642 (make-diary-entry (calendar-date-string (calendar-cursor-to-date t
) t
)
1645 (defun insert-anniversary-diary-entry (arg)
1646 "Insert an anniversary diary entry for the date given by point.
1647 Prefix arg will make the entry nonmarking."
1649 (let ((calendar-date-display-form
1650 (if european-calendar-style
1651 '(day " " month
" " year
)
1652 '(month " " day
" " year
))))
1654 (format "%s(diary-anniversary %s)"
1655 sexp-diary-entry-symbol
1656 (calendar-date-string (calendar-cursor-to-date t
) nil t
))
1659 (defun insert-block-diary-entry (arg)
1660 "Insert a block diary entry for the days between the point and marked date.
1661 Prefix arg will make the entry nonmarking."
1663 (let ((calendar-date-display-form
1664 (if european-calendar-style
1665 '(day " " month
" " year
)
1666 '(month " " day
" " year
)))
1667 (cursor (calendar-cursor-to-date t
))
1668 (mark (or (car calendar-mark-ring
)
1669 (error "No mark set in this buffer")))
1671 (if (< (calendar-absolute-from-gregorian mark
)
1672 (calendar-absolute-from-gregorian cursor
))
1678 (format "%s(diary-block %s %s)"
1679 sexp-diary-entry-symbol
1680 (calendar-date-string start nil t
)
1681 (calendar-date-string end nil t
))
1684 (defun insert-cyclic-diary-entry (arg)
1685 "Insert a cyclic diary entry starting at the date given by point.
1686 Prefix arg will make the entry nonmarking."
1688 (let ((calendar-date-display-form
1689 (if european-calendar-style
1690 '(day " " month
" " year
)
1691 '(month " " day
" " year
))))
1693 (format "%s(diary-cyclic %d %s)"
1694 sexp-diary-entry-symbol
1695 (calendar-read "Repeat every how many days: "
1696 (lambda (x) (> x
0)))
1697 (calendar-date-string (calendar-cursor-to-date t
) nil t
))
1701 (define-derived-mode diary-mode text-mode
1703 "Major mode for editing the diary file."
1704 (set (make-local-variable 'font-lock-defaults
)
1705 '(diary-font-lock-keywords t
)))
1707 (define-derived-mode fancy-diary-display-mode text-mode
1709 "Major mode used while displaying diary entries using Fancy Display."
1710 (set (make-local-variable 'font-lock-defaults
)
1711 '(fancy-diary-font-lock-keywords t
))
1712 (define-key (current-local-map) "q" 'quit-window
))
1715 (defvar fancy-diary-font-lock-keywords
1719 (let ((dayname (diary-name-pattern calendar-day-name-array nil t
))
1720 (monthname (diary-name-pattern calendar-month-name-array nil t
))
1724 (mapconcat 'eval calendar-date-display-form
""))
1725 "\\(\\(: .*\\)\\|\\(\n +.*\\)\\)*\n=+$")
1727 '("^.*anniversary.*$" . font-lock-keyword-face
)
1728 '("^.*birthday.*$" . font-lock-keyword-face
)
1729 '("^.*Yahrzeit.*$" . font-lock-reference-face
)
1730 '("^\\(Erev \\)?Rosh Hodesh.*" . font-lock-function-name-face
)
1731 '("^Day.*omer.*$" . font-lock-builtin-face
)
1732 '("^Parashat.*$" . font-lock-comment-face
)
1733 '("^[ \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\\)?\\)?"
1734 . font-lock-variable-name-face
))
1735 "Keywords to highlight in fancy diary display")
1738 (defun font-lock-diary-sexps (limit)
1739 "Recognize sexp diary entry for font-locking."
1740 (if (re-search-forward
1741 (concat "^" (regexp-quote diary-nonmarking-symbol
)
1742 "?\\(" (regexp-quote sexp-diary-entry-symbol
) "\\)")
1746 (narrow-to-region (point-min) limit
)
1747 (let ((start (point)))
1749 (store-match-data (list start
(point)))
1753 (defun font-lock-diary-date-forms (month-array &optional symbol abbrev-array
)
1754 "Create font-lock patterns for `diary-date-forms' using MONTH-ARRAY.
1755 If given, optional SYMBOL must be a prefix to entries.
1756 If optional ABBREV-ARRAY is present, the abbreviations constructed
1757 from this array by the function `calendar-abbrev-construct' are
1758 matched (with or without a final `.'), in addition to the full month
1760 (let ((dayname (diary-name-pattern calendar-day-name-array
1761 calendar-day-abbrev-array t
))
1762 (monthname (format "\\(%s\\|\\*\\)"
1763 (diary-name-pattern month-array abbrev-array
)))
1764 (month "\\([0-9]+\\|\\*\\)")
1765 (day "\\([0-9]+\\|\\*\\)")
1766 (year "-?\\([0-9]+\\|\\*\\)"))
1767 (mapcar '(lambda (x)
1769 (concat "^" (regexp-quote diary-nonmarking-symbol
) "?"
1770 (if symbol
(regexp-quote symbol
) "") "\\("
1772 ;; If backup, omit first item (backup)
1773 ;; and last item (not part of date)
1774 (if (equal (car x
) 'backup
)
1775 (reverse (cdr (reverse (cdr x
))))
1778 ;; With backup, last item is not part of date
1779 (if (equal (car x
) 'backup
)
1780 (concat "\\)" (eval (car (reverse x
))))
1785 (eval-when-compile (require 'cal-hebrew
)
1786 (require 'cal-islam
))
1788 (defvar diary-font-lock-keywords
1790 (font-lock-diary-date-forms calendar-month-name-array
1791 nil calendar-month-abbrev-array
)
1792 (when (or (memq 'mark-hebrew-diary-entries
1793 nongregorian-diary-marking-hook
)
1794 (memq 'list-hebrew-diary-entries
1795 nongregorian-diary-listing-hook
))
1796 (require 'cal-hebrew
)
1797 (font-lock-diary-date-forms
1798 calendar-hebrew-month-name-array-leap-year
1799 hebrew-diary-entry-symbol
))
1800 (when (or (memq 'mark-islamic-diary-entries
1801 nongregorian-diary-marking-hook
)
1802 (memq 'list-islamic-diary-entries
1803 nongregorian-diary-listing-hook
))
1804 (require 'cal-islam
)
1805 (font-lock-diary-date-forms
1806 calendar-islamic-month-name-array
1807 islamic-diary-entry-symbol
))
1810 (concat "^" (regexp-quote diary-include-string
) ".*$")
1811 'font-lock-keyword-face
)
1813 (concat "^" (regexp-quote diary-nonmarking-symbol
)
1814 "?\\(" (regexp-quote sexp-diary-entry-symbol
) "\\)")
1815 '(1 font-lock-reference-face
))
1817 (concat "^" (regexp-quote diary-nonmarking-symbol
))
1818 'font-lock-reference-face
)
1820 (concat "^" (regexp-quote diary-nonmarking-symbol
)
1821 "?\\(" (regexp-quote hebrew-diary-entry-symbol
) "\\)")
1822 '(1 font-lock-reference-face
))
1824 (concat "^" (regexp-quote diary-nonmarking-symbol
)
1825 "?\\(" (regexp-quote islamic-diary-entry-symbol
) "\\)")
1826 '(1 font-lock-reference-face
))
1827 '(font-lock-diary-sexps . font-lock-keyword-face
)
1828 '("[0-9]?[0-9]\\(:?[0-9][0-9]\\)?\\(am\\|pm\\|AM\\|PM\\)\\(-[0-9]?[0-9]\\(:?[0-9][0-9]\\)?\\(am\\|pm\\|AM\\|PM\\)\\)?"
1829 . font-lock-function-name-face
)))
1830 "Forms to highlight in diary-mode")
1833 (provide 'diary-lib
)
1835 ;;; diary-lib.el ends here