1 ;;; diary-lib.el --- diary functions
3 ;; Copyright (C) 1989, 1990, 1992, 1993, 1994, 1995, 2003, 2004, 2005
4 ;; Free Software Foundation, Inc.
6 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
7 ;; Maintainer: Glenn Morris <gmorris@ast.cam.ac.uk>
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
29 ;; This collection of functions implements the diary features as described
32 ;; Comments, corrections, and improvements should be sent to
33 ;; Edward M. Reingold Department of Computer Science
34 ;; (217) 333-6733 University of Illinois at Urbana-Champaign
35 ;; reingold@cs.uiuc.edu 1304 West Springfield Avenue
36 ;; Urbana, Illinois 61801
42 (defun diary-check-diary-file ()
43 "Check that the file specified by `diary-file' exists and is readable.
44 If so, return the expanded file name, otherwise signal an error."
45 (let ((d-file (substitute-in-file-name diary-file)))
46 (if (and d-file (file-exists-p d-file))
47 (if (file-readable-p d-file)
49 (error "Diary file `%s' is not readable" diary-file))
50 (error "Diary file `%s' does not exist" diary-file))))
53 (defun diary (&optional arg)
54 "Generate the diary window for ARG days starting with the current date.
55 If no argument is provided, the number of days of diary entries is governed
56 by the variable `number-of-diary-entries'. A value of ARG less than 1
57 does nothing. This function is suitable for execution in a `.emacs' file."
59 (diary-check-diary-file)
60 (let ((date (calendar-current-date)))
61 (diary-list-entries date (if arg (prefix-numeric-value arg)))))
63 (define-obsolete-function-alias 'view-diary-entries 'diary-view-entries)
64 (defun diary-view-entries (&optional arg)
65 "Prepare and display a buffer with diary entries.
66 Searches the file named in `diary-file' for entries that
67 match ARG days starting with the date indicated by the cursor position
68 in the displayed three-month calendar."
70 (diary-check-diary-file)
71 (diary-list-entries (calendar-cursor-to-date t) arg))
73 (defun view-other-diary-entries (arg d-file)
74 "Prepare and display buffer of diary entries from an alternative diary file.
75 Searches for entries that match ARG days, starting with the date indicated
76 by the cursor position in the displayed three-month calendar.
77 D-FILE specifies the file to use as the diary file."
79 (list (prefix-numeric-value current-prefix-arg)
80 (read-file-name "Enter diary file name: " default-directory nil t)))
81 (let ((diary-file d-file))
82 (view-diary-entries arg)))
84 (autoload 'check-calendar-holidays "holidays"
85 "Check the list of holidays for any that occur on DATE.
86 The value returned is a list of strings of relevant holiday descriptions.
87 The holidays are those in the list `calendar-holidays'.")
89 (autoload 'calendar-holiday-list "holidays"
90 "Form the list of holidays that occur on dates in the calendar window.
91 The holidays are those in the list `calendar-holidays'.")
93 (autoload 'diary-french-date "cal-french"
94 "French calendar equivalent of date diary entry.")
96 (autoload 'diary-mayan-date "cal-mayan"
97 "Mayan calendar equivalent of date diary entry.")
99 (autoload 'diary-iso-date "cal-iso"
100 "ISO calendar equivalent of date diary entry.")
102 (autoload 'diary-julian-date "cal-julian"
103 "Julian calendar equivalent of date diary entry.")
105 (autoload 'diary-astro-day-number "cal-julian"
106 "Astronomical (Julian) day number diary entry.")
108 (autoload 'diary-chinese-date "cal-china"
109 "Chinese calendar equivalent of date diary entry.")
111 (autoload 'diary-islamic-date "cal-islam"
112 "Islamic calendar equivalent of date diary entry.")
114 (autoload 'list-islamic-diary-entries "cal-islam"
115 "Add any Islamic date entries from the diary file to `diary-entries-list'.")
117 (autoload 'mark-islamic-diary-entries "cal-islam"
118 "Mark days in the calendar window that have Islamic date diary entries.")
120 (autoload 'mark-islamic-calendar-date-pattern "cal-islam"
121 "Mark dates in calendar window that conform to Islamic date MONTH/DAY/YEAR.")
123 (autoload 'diary-bahai-date "cal-bahai"
124 "Baha'i calendar equivalent of date diary entry."
127 (autoload 'list-bahai-diary-entries "cal-bahai"
128 "Add any Baha'i date entries from the diary file to `diary-entries-list'."
131 (autoload 'mark-bahai-diary-entries "cal-bahai"
132 "Mark days in the calendar window that have Baha'i date diary entries."
135 (autoload 'mark-bahai-calendar-date-pattern "cal-bahai"
136 "Mark dates in calendar window that conform to Baha'i date MONTH/DAY/YEAR."
139 (autoload 'diary-hebrew-date "cal-hebrew"
140 "Hebrew calendar equivalent of date diary entry.")
142 (autoload 'diary-omer "cal-hebrew"
143 "Omer count diary entry.")
145 (autoload 'diary-yahrzeit "cal-hebrew"
146 "Yahrzeit diary entry--entry applies if date is yahrzeit or the day before.")
148 (autoload 'diary-parasha "cal-hebrew"
149 "Parasha diary entry--entry applies if date is a Saturday.")
151 (autoload 'diary-rosh-hodesh "cal-hebrew"
152 "Rosh Hodesh diary entry.")
154 (autoload 'list-hebrew-diary-entries "cal-hebrew"
155 "Add any Hebrew date entries from the diary file to `diary-entries-list'.")
157 (autoload 'mark-hebrew-diary-entries "cal-hebrew"
158 "Mark days in the calendar window that have Hebrew date diary entries.")
160 (autoload 'mark-hebrew-calendar-date-pattern "cal-hebrew"
161 "Mark dates in calendar window that conform to Hebrew date MONTH/DAY/YEAR.")
163 (autoload 'diary-coptic-date "cal-coptic"
164 "Coptic calendar equivalent of date diary entry.")
166 (autoload 'diary-ethiopic-date "cal-coptic"
167 "Ethiopic calendar equivalent of date diary entry.")
169 (autoload 'diary-persian-date "cal-persia"
170 "Persian calendar equivalent of date diary entry.")
172 (autoload 'diary-phases-of-moon "lunar" "Moon phases diary entry.")
174 (autoload 'diary-sunrise-sunset "solar"
175 "Local time of sunrise and sunset as a diary entry.")
177 (autoload 'diary-sabbath-candles "solar"
178 "Local time of candle lighting diary entry--applies if date is a Friday.
179 No diary entry if there is no sunset on that date.")
181 (defvar diary-syntax-table
182 (let ((st (copy-syntax-table (standard-syntax-table))))
183 (modify-syntax-entry ?* "w" st)
184 (modify-syntax-entry ?: "w" st)
186 "The syntax table used when parsing dates in the diary file.
187 It is the standard syntax table used in Fundamental mode, but with the
188 syntax of `*' and `:' changed to be word constituents.")
190 (defvar diary-entries-list)
191 (defvar displayed-year)
192 (defvar displayed-month)
197 (defvar original-date)
199 (defun diary-attrtype-convert (attrvalue type)
200 "Convert string ATTRVALUE to TYPE appropriate for a face description.
201 Valid TYPEs are: string, symbol, int, stringtnil, tnil."
203 (setq ret (cond ((eq type 'string) attrvalue)
204 ((eq type 'symbol) (read attrvalue))
205 ((eq type 'int) (string-to-number attrvalue))
206 ((eq type 'stringtnil)
207 (cond ((string= "t" attrvalue) t)
208 ((string= "nil" attrvalue) nil)
211 (cond ((string= "t" attrvalue) t)
212 ((string= "nil" attrvalue) nil)))))
213 ; (message "(%s)[%s]=[%s]" (print type) attrvalue ret)
217 (defun diary-pull-attrs (entry fileglobattrs)
218 "Pull the face-related attributes off the entry, merge with the
219 fileglobattrs, and return the (possibly modified) entry and face
220 data in a list of attrname attrvalue values.
221 The entry will be modified to drop all tags that are used for face matching.
222 If entry is nil, then the fileglobattrs are being searched for,
223 the fileglobattrs variable is ignored, and
224 diary-glob-file-regexp-prefix is prepended to the regexps before each
227 (let (regexp regnum attrname attr-list attrname attrvalue type
232 attr-list diary-face-attrs)
234 (goto-char (point-min))
235 (setq attr (car attr-list)
238 attrname (nth 2 attr)
240 regexp (concat diary-glob-file-regexp-prefix regexp))
242 (if (re-search-forward regexp (point-max) t)
243 (setq attrvalue (match-string-no-properties regnum)))
245 (setq attrvalue (diary-attrtype-convert attrvalue type)))
246 (setq ret-attr (append ret-attr (list attrname attrvalue))))
247 (setq attr-list (cdr attr-list)))
248 (setq fileglobattrs ret-attr))
250 (setq ret-attr fileglobattrs
251 attr-list diary-face-attrs)
253 (goto-char (point-min))
254 (setq attr (car attr-list)
257 attrname (nth 2 attr)
260 (if (string-match regexp entry)
262 (setq attrvalue (match-string-no-properties regnum entry))
263 (setq entry (replace-match "" t t entry))))
265 (setq attrvalue (diary-attrtype-convert attrvalue type)))
266 (setq ret-attr (append ret-attr (list attrname attrvalue))))
267 (setq attr-list (cdr attr-list)))))
268 (list entry ret-attr))))
271 ;; This can be removed once the kill/yank treatment of invisible text
272 ;; (see etc/TODO) is fixed. -- gm
273 (defcustom diary-header-line-flag t
274 "*If non-nil, `simple-diary-display' will show a header line.
275 The format of the header is specified by `diary-header-line-format'."
280 (defcustom diary-header-line-format
281 '(:eval (calendar-string-spread
282 (list (if selective-display
283 "Selective display active - press \"s\" in calendar \
287 "*Format of the header line displayed by `simple-diary-display'.
288 Only used if `diary-header-line-flag' is non-nil."
293 (defvar diary-saved-point) ; internal
296 (defcustom number-of-diary-entries 1
297 "Specifies how many days of diary entries are to be displayed initially.
298 This variable affects the diary display when the command \\[diary] is used,
299 or if the value of the variable `view-diary-entries-initially' is t. For
300 example, if the default value 1 is used, then only the current day's diary
301 entries will be displayed. If the value 2 is used, then both the current
302 day's and the next day's entries will be displayed.
304 The value can also be a vector such as [0 2 2 2 2 4 1]; this value
305 says to display no diary entries on Sunday, the display the entries
306 for the current date and the day after on Monday through Thursday,
307 display Friday through Monday's entries on Friday, and display only
308 Saturday's entries on Saturday.
310 This variable does not affect the diary display with the `d' command
311 from the calendar; in that case, the prefix argument controls the
312 number of days of diary entries displayed."
313 :type '(choice (integer :tag "Entries")
314 (vector :value [0 0 0 0 0 0 0]
315 (integer :tag "Sunday")
316 (integer :tag "Monday")
317 (integer :tag "Tuesday")
318 (integer :tag "Wednesday")
319 (integer :tag "Thursday")
320 (integer :tag "Friday")
321 (integer :tag "Saturday")))
324 (define-obsolete-function-alias 'list-diary-entries 'diary-list-entries)
325 (defun diary-list-entries (date number)
326 "Create and display a buffer containing the relevant lines in `diary-file'.
327 The arguments are DATE and NUMBER; the entries selected are those
328 for NUMBER days starting with date DATE. The other entries are hidden
329 using selective display. If NUMBER is less than 1, this function does nothing.
331 Returns a list of all relevant diary entries found, if any, in order by date.
332 The list entries have the form ((month day year) string specifier) where
333 \(month day year) is the date of the entry, string is the entry text, and
334 specifier is the applicability. If the variable `diary-list-include-blanks'
335 is t, this list includes a dummy diary entry consisting of the empty string)
336 for a date with no diary entries.
338 After the list is prepared, the hooks `nongregorian-diary-listing-hook',
339 `list-diary-entries-hook', `diary-display-hook', and `diary-hook' are run.
340 These hooks have the following distinct roles:
342 `nongregorian-diary-listing-hook' can cull dates from the diary
343 and each included file. Usually used for Hebrew or Islamic
344 diary entries in files. Applied to *each* file.
346 `list-diary-entries-hook' adds or manipulates diary entries from
347 external sources. Used, for example, to include diary entries
348 from other files or to sort the diary entries. Invoked *once* only,
349 before the display hook is run.
351 `diary-display-hook' does the actual display of information. If this is
352 nil, simple-diary-display will be used. Use add-hook to set this to
353 fancy-diary-display, if desired. If you want no diary display, use
354 add-hook to set this to ignore.
356 `diary-hook' is run last. This can be used for an appointment
357 notification function."
359 (setq number (if (vectorp number-of-diary-entries)
360 (aref number-of-diary-entries (calendar-day-of-week date))
361 number-of-diary-entries)))
363 (let ((original-date date);; save for possible use in the hooks
366 (date-string (calendar-date-string date))
367 (d-file (substitute-in-file-name diary-file)))
368 (message "Preparing diary...")
370 (let ((diary-buffer (find-buffer-visiting d-file)))
371 (if (not diary-buffer)
372 (set-buffer (find-file-noselect d-file t))
373 (set-buffer diary-buffer)
374 (or (verify-visited-file-modtime diary-buffer)
375 (revert-buffer t t))))
376 ;; d-s-p is passed to the diary display function.
377 (let ((diary-saved-point (point)))
379 (setq file-glob-attrs (nth 1 (diary-pull-attrs nil "")))
380 (setq selective-display t)
381 (setq selective-display-ellipses nil)
382 (if diary-header-line-flag
383 (setq header-line-format diary-header-line-format))
384 (with-syntax-table diary-syntax-table
385 (let ((buffer-read-only nil)
386 (diary-modified (buffer-modified-p))
387 (mark (regexp-quote diary-nonmarking-symbol)))
388 ;; First and last characters must be ^M or \n for
389 ;; selective display to work properly
390 (goto-char (1- (point-max)))
391 (if (not (looking-at "\^M\\|\n"))
393 (goto-char (point-max))
395 (goto-char (point-min))
396 (if (not (looking-at "\^M\\|\n"))
398 (subst-char-in-region (point-min) (point-max) ?\n ?\^M t)
400 i from 1 to number do
401 (let ((month (extract-calendar-month date))
402 (day (extract-calendar-day date))
403 (year (extract-calendar-year date))
404 (entry-found (list-sexp-diary-entries date)))
405 (dolist (date-form diary-date-forms)
407 ((backup (when (eq (car date-form) 'backup)
408 (setq date-form (cdr date-form))
411 (format "%s\\|%s\\.?"
412 (calendar-day-name date)
413 (calendar-day-name date 'abbrev)))
415 (format "\\*\\|%s\\|%s\\.?"
416 (calendar-month-name month)
417 (calendar-month-name month 'abbrev)))
418 (month (concat "\\*\\|0*" (int-to-string month)))
419 (day (concat "\\*\\|0*" (int-to-string day)))
422 "\\*\\|0*" (int-to-string year)
423 (if abbreviated-calendar-year
424 (concat "\\|" (format "%02d" (% year 100)))
428 "\\(\\`\\|\^M\\|\n\\)" mark "?\\("
429 (mapconcat 'eval date-form "\\)\\(")
431 (case-fold-search t))
432 (goto-char (point-min))
433 (while (re-search-forward regexp nil t)
434 (if backup (re-search-backward "\\<" nil t))
435 (if (and (or (char-equal (preceding-char) ?\^M)
436 (char-equal (preceding-char) ?\n))
437 (not (looking-at " \\|\^I")))
438 ;; Diary entry that consists only of date.
440 ;; Found a nonempty diary entry--make it
441 ;; visible and add it to the list.
443 (let ((entry-start (point))
445 (re-search-backward "\^M\\|\n\\|\\`")
446 (setq date-start (point))
447 (re-search-forward "\^M\\|\n" nil t 2)
448 (while (looking-at " \\|\^I")
449 (re-search-forward "\^M\\|\n" nil t))
451 (subst-char-in-region date-start
453 (setq entry (buffer-substring entry-start (point))
454 temp (diary-pull-attrs entry file-glob-attrs)
460 (1+ date-start) (1- entry-start))
461 (copy-marker entry-start) (nth 1 temp)))))))
463 (not diary-list-include-blanks)
464 (setq diary-entries-list
465 (append diary-entries-list
466 (list (list date "" "" "" "")))))
468 (calendar-gregorian-from-absolute
469 (1+ (calendar-absolute-from-gregorian date))))
470 (setq entry-found nil)))
471 (set-buffer-modified-p diary-modified)))
472 (goto-char (point-min))
473 (run-hooks 'nongregorian-diary-listing-hook
474 'list-diary-entries-hook)
475 (if diary-display-hook
476 (run-hooks 'diary-display-hook)
477 (simple-diary-display))
478 (run-hooks 'diary-hook)
479 diary-entries-list))))))
481 (defun diary-unhide-everything ()
482 (setq selective-display nil)
483 (let ((inhibit-read-only t)
484 (modified (buffer-modified-p)))
485 (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)
486 (set-buffer-modified-p modified))
487 (kill-local-variable 'mode-line-format))
489 (defun include-other-diary-files ()
490 "Include the diary entries from other diary files with those of diary-file.
491 This function is suitable for use in `list-diary-entries-hook';
492 it enables you to use shared diary files together with your own.
493 The files included are specified in the diaryfile by lines of this form:
494 #include \"filename\"
495 This is recursive; that is, #include directives in diary files thus included
496 are obeyed. You can change the `#include' to some other string by
497 changing the variable `diary-include-string'."
498 (goto-char (point-min))
499 (while (re-search-forward
501 "\\(?:\\`\\|\^M\\|\n\\)"
502 (regexp-quote diary-include-string)
505 (let* ((diary-file (substitute-in-file-name
506 (match-string-no-properties 1)))
507 (diary-list-include-blanks nil)
508 (list-diary-entries-hook 'include-other-diary-files)
509 (diary-display-hook 'ignore)
511 (if (file-exists-p diary-file)
512 (if (file-readable-p diary-file)
514 (setq diary-entries-list
515 (append diary-entries-list
516 (list-diary-entries original-date number)))
517 (with-current-buffer (find-buffer-visiting diary-file)
518 (diary-unhide-everything)))
520 (message "Can't read included diary file %s" diary-file)
523 (message "Can't find included diary file %s" diary-file)
525 (goto-char (point-min)))
527 (defun simple-diary-display ()
528 "Display the diary buffer if there are any relevant entries or holidays."
529 (let* ((holiday-list (if holidays-in-diary-buffer
530 (check-calendar-holidays original-date)))
531 (hol-string (format "%s%s%s"
533 (if holiday-list ": " "")
534 (mapconcat 'identity holiday-list "; ")))
535 (msg (format "No diary entries for %s" hol-string))
536 ;; If selected window is dedicated (to the calendar),
537 ;; need a new one to display the diary.
538 (pop-up-frames (window-dedicated-p (selected-window))))
539 (calendar-set-mode-line (format "Diary for %s" hol-string))
540 (if (or (not diary-entries-list)
541 (and (not (cdr diary-entries-list))
542 (string-equal (car (cdr (car diary-entries-list))) "")))
543 (if (< (length msg) (frame-width))
545 (set-buffer (get-buffer-create holiday-buffer))
546 (setq buffer-read-only nil)
547 (calendar-set-mode-line date-string)
549 (insert (mapconcat 'identity holiday-list "\n"))
550 (goto-char (point-min))
551 (set-buffer-modified-p nil)
552 (setq buffer-read-only t)
553 (display-buffer holiday-buffer)
554 (message "No diary entries for %s" date-string))
556 (find-buffer-visiting (substitute-in-file-name diary-file))
557 (let ((window (display-buffer (current-buffer))))
558 ;; d-s-p is passed from list-diary-entries.
559 (set-window-point window diary-saved-point)
560 (set-window-start window (point-min))))
561 (message "Preparing diary...done"))))
563 (defface diary-button '((((type pc) (class color))
564 (:foreground "lightblue")))
565 "Default face used for buttons."
568 ;; backward-compatibility alias
569 (put 'diary-button-face 'face-alias 'diary-button)
571 (define-button-type 'diary-entry
572 'action #'diary-goto-entry
575 (defun diary-goto-entry (button)
576 (let ((marker (button-get button 'marker)))
578 (pop-to-buffer (marker-buffer marker))
579 (goto-char (marker-position marker)))))
581 (defun fancy-diary-display ()
582 "Prepare a diary buffer with relevant entries in a fancy, noneditable form.
583 This function is provided for optional use as the `diary-display-hook'."
584 (with-current-buffer ;; Turn off selective-display in the diary file's buffer.
585 (find-buffer-visiting (substitute-in-file-name diary-file))
586 (diary-unhide-everything))
587 (if (or (not diary-entries-list)
588 (and (not (cdr diary-entries-list))
589 (string-equal (car (cdr (car diary-entries-list))) "")))
590 (let* ((holiday-list (if holidays-in-diary-buffer
591 (check-calendar-holidays original-date)))
592 (msg (format "No diary entries for %s %s"
593 (concat date-string (if holiday-list ":" ""))
594 (mapconcat 'identity holiday-list "; "))))
595 (if (<= (length msg) (frame-width))
597 (set-buffer (get-buffer-create holiday-buffer))
598 (setq buffer-read-only nil)
600 (insert (mapconcat 'identity holiday-list "\n"))
601 (goto-char (point-min))
602 (set-buffer-modified-p nil)
603 (setq buffer-read-only t)
604 (display-buffer holiday-buffer)
605 (message "No diary entries for %s" date-string)))
606 (save-excursion;; Prepare the fancy diary buffer.
607 (set-buffer (make-fancy-diary-buffer))
608 (setq buffer-read-only nil)
609 (let ((entry-list diary-entries-list)
611 (holiday-list-last-month 1)
612 (holiday-list-last-year 1)
615 (if (not (calendar-date-equal date (car (car entry-list))))
617 (setq date (car (car entry-list)))
618 (and holidays-in-diary-buffer
619 (calendar-date-compare
620 (list (list holiday-list-last-month
621 (calendar-last-day-of-month
622 holiday-list-last-month
623 holiday-list-last-year)
624 holiday-list-last-year))
626 ;; We need to get the holidays for the next 3 months.
627 (setq holiday-list-last-month
628 (extract-calendar-month date))
629 (setq holiday-list-last-year
630 (extract-calendar-year date))
632 (increment-calendar-month
633 holiday-list-last-month holiday-list-last-year 1)
636 (let ((displayed-month holiday-list-last-month)
637 (displayed-year holiday-list-last-year))
638 (calendar-holiday-list)))
639 (increment-calendar-month
640 holiday-list-last-month holiday-list-last-year 1))
641 (let* ((date-string (calendar-date-string date))
643 (let ((h holiday-list)
645 ;; Make a list of all holidays for date.
647 (if (calendar-date-equal date (car (car h)))
648 (setq d (append d (cdr (car h)))))
651 (insert (if (= (point) (point-min)) "" ?\n) date-string)
652 (if date-holiday-list (insert ": "))
653 (let* ((l (current-column))
655 (insert (mapconcat (lambda (x)
656 (if (< longest (length x))
657 (setq longest (length x)))
660 (concat "\n" (make-string l ? ))))
661 (insert ?\n (make-string (+ l longest) ?=) ?\n)))))
663 (setq entry (car (cdr (car entry-list))))
664 (if (< 0 (length entry))
666 (if (nth 3 (car entry-list))
667 (insert-button (concat entry "\n")
668 'marker (nth 3 (car entry-list))
672 (let* ((marks (nth 4 (car entry-list)))
673 (temp-face (make-symbol
676 (mapcar '(lambda (sym)
682 (make-face temp-face)
683 ;; Remove :face info from the marks,
684 ;; copy the face info into temp-face
685 (while (setq faceinfo (memq :face faceinfo))
686 (copy-face (read (nth 1 faceinfo)) temp-face)
687 (setcar faceinfo nil)
688 (setcar (cdr faceinfo) nil))
689 (setq marks (delq nil marks))
690 ;; Apply the font aspects
691 (apply 'set-face-attribute temp-face nil marks)
692 (search-backward entry)
694 (make-overlay (match-beginning 0) (match-end 0))
696 (setq entry-list (cdr entry-list))))
697 (set-buffer-modified-p nil)
698 (goto-char (point-min))
699 (setq buffer-read-only t)
700 (display-buffer fancy-diary-buffer)
701 (fancy-diary-display-mode)
702 (calendar-set-mode-line date-string)
703 (message "Preparing diary...done"))))
705 (defun make-fancy-diary-buffer ()
706 "Create and return the initial fancy diary buffer."
708 (set-buffer (get-buffer-create fancy-diary-buffer))
709 (setq buffer-read-only nil)
710 (calendar-set-mode-line "Diary Entries")
712 (set-buffer-modified-p nil)
713 (setq buffer-read-only t)
714 (get-buffer fancy-diary-buffer)))
716 (defun print-diary-entries ()
717 "Print a hard copy of the diary display.
719 If the simple diary display is being used, prepare a temp buffer with the
720 visible lines of the diary buffer, add a heading line composed from the mode
721 line, print the temp buffer, and destroy it.
723 If the fancy diary display is being used, just print the buffer.
725 The hooks given by the variable `print-diary-entries-hook' are called to do
726 the actual printing."
728 (if (bufferp (get-buffer fancy-diary-buffer))
730 (set-buffer (get-buffer fancy-diary-buffer))
731 (run-hooks 'print-diary-entries-hook))
733 (find-buffer-visiting (substitute-in-file-name diary-file))))
735 (let ((temp-buffer (get-buffer-create "*Printable Diary Entries*"))
738 (set-buffer diary-buffer)
740 (if (not (stringp mode-line-format))
742 (string-match "^-*\\([^-].*[^-]\\)-*$" mode-line-format)
743 (substring mode-line-format
744 (match-beginning 1) (match-end 1))))
745 (copy-to-buffer temp-buffer (point-min) (point-max))
746 (set-buffer temp-buffer)
747 (while (re-search-forward "\^M.*$" nil t)
749 (goto-char (point-min))
751 (make-string (length heading) ?=) "\n")
752 (run-hooks 'print-diary-entries-hook)
753 (kill-buffer temp-buffer)))
754 (error "You don't have a diary buffer!")))))
756 (define-obsolete-function-alias 'show-all-diary-entries 'diary-show-all-entries)
757 (defun diary-show-all-entries ()
758 "Show all of the diary entries in the diary file.
759 This function gets rid of the selective display of the diary file so that
760 all entries, not just some, are visible. If there is no diary buffer, one
763 (let ((d-file (diary-check-diary-file))
764 (pop-up-frames (window-dedicated-p (selected-window))))
765 (with-current-buffer (or (find-buffer-visiting d-file)
766 (find-file-noselect d-file t))
767 (diary-unhide-everything)
768 (display-buffer (current-buffer)))))
770 (defcustom diary-mail-addr
771 (if (boundp 'user-mail-address) user-mail-address "")
772 "*Email address that `diary-mail-entries' will send email to."
777 (defcustom diary-mail-days 7
778 "*Default number of days for `diary-mail-entries' to check."
784 (defun diary-mail-entries (&optional ndays)
785 "Send a mail message showing diary entries for next NDAYS days.
786 If no prefix argument is given, NDAYS is set to `diary-mail-days'.
787 Mail is sent to the address specified by `diary-mail-addr'.
789 You can call `diary-mail-entries' every night using an at/cron job.
790 For example, this script will run the program at 2am daily. Since
791 `emacs -batch' does not load your `.emacs' file, you must ensure that
792 all relevant variables are set, as done here.
795 # diary-rem.sh -- repeatedly run the Emacs diary-reminder
797 -eval \"(setq diary-mail-days 3 \\
798 diary-file \\\"/path/to/diary.file\\\" \\
799 european-calendar-style t \\
800 diary-mail-addr \\\"user@host.name\\\" )\" \\
801 -l diary-lib -f diary-mail-entries
802 at -f diary-rem.sh 0200 tomorrow
804 You may have to tweak the syntax of the `at' command to suit your
805 system. Alternatively, you can specify a cron entry:
806 0 1 * * * diary-rem.sh
807 to run it every morning at 1am."
809 (if (string-equal diary-mail-addr "")
810 (error "You must set `diary-mail-addr' to use this command")
811 (let ((diary-display-hook 'fancy-diary-display))
812 (list-diary-entries (calendar-current-date) (or ndays diary-mail-days)))
813 (compose-mail diary-mail-addr
814 (concat "Diary entries generated "
815 (calendar-date-string (calendar-current-date))))
817 (if (get-buffer fancy-diary-buffer)
818 (with-current-buffer fancy-diary-buffer (buffer-string))
820 (call-interactively (get mail-user-agent 'sendfunc))))
822 (defun diary-name-pattern (string-array &optional abbrev-array paren)
823 "Return a regexp matching the strings in the array STRING-ARRAY.
824 If the optional argument ABBREV-ARRAY is present, then the function
825 `calendar-abbrev-construct' is used to construct abbreviations from the
826 two supplied arrays. The returned regexp will then also match these
827 abbreviations, with or without final `.' characters. If the optional
828 argument PAREN is non-nil, the regexp is surrounded by parentheses."
829 (regexp-opt (append string-array
831 (calendar-abbrev-construct abbrev-array
834 (calendar-abbrev-construct abbrev-array
840 (defvar marking-diary-entries nil
841 "True during the marking of diary entries, nil otherwise.")
843 (defvar marking-diary-entry nil
844 "True during the marking of diary entries, if current entry is marking.")
846 (defun mark-diary-entries (&optional redraw)
847 "Mark days in the calendar window that have diary entries.
848 Each entry in the diary file visible in the calendar window is
849 marked. After the entries are marked, the hooks
850 `nongregorian-diary-marking-hook' and `mark-diary-entries-hook'
851 are run. If the optional argument REDRAW is non-nil (which is
852 the case interactively, for example) then any existing diary
853 marks are first removed. This is intended to deal with deleted
856 ;; To remove any deleted diary entries. Do not redraw when:
857 ;; i) processing #include diary files (else only get the marks from
858 ;; the last #include file processed).
859 ;; ii) called via calendar-redraw (since calendar has already been
861 ;; Use of REDRAW handles both of these cases.
862 (when (and redraw mark-diary-entries-in-calendar)
863 (setq mark-diary-entries-in-calendar nil)
865 (let ((marking-diary-entries t)
866 file-glob-attrs marks)
867 (with-current-buffer (find-file-noselect (diary-check-diary-file) t)
869 (setq mark-diary-entries-in-calendar t)
870 (message "Marking diary entries...")
871 (setq file-glob-attrs (nth 1 (diary-pull-attrs nil '())))
872 (with-syntax-table diary-syntax-table
873 (dolist (date-form diary-date-forms)
874 (if (eq (car date-form) 'backup)
875 (setq date-form (cdr date-form))) ;; ignore 'backup directive
877 (diary-name-pattern calendar-day-name-array
878 calendar-day-abbrev-array))
881 (diary-name-pattern calendar-month-name-array
882 calendar-month-abbrev-array)))
883 (month "[0-9]+\\|\\*")
885 (year "[0-9]+\\|\\*")
886 (l (length date-form))
887 (d-name-pos (- l (length (memq 'dayname date-form))))
888 (d-name-pos (if (/= l d-name-pos) (+ 2 d-name-pos)))
889 (m-name-pos (- l (length (memq 'monthname date-form))))
890 (m-name-pos (if (/= l m-name-pos) (+ 2 m-name-pos)))
891 (d-pos (- l (length (memq 'day date-form))))
892 (d-pos (if (/= l d-pos) (+ 2 d-pos)))
893 (m-pos (- l (length (memq 'month date-form))))
894 (m-pos (if (/= l m-pos) (+ 2 m-pos)))
895 (y-pos (- l (length (memq 'year date-form))))
896 (y-pos (if (/= l y-pos) (+ 2 y-pos)))
899 "\\(\\`\\|\^M\\|\n\\)\\("
900 (mapconcat 'eval date-form "\\)\\(")
902 (case-fold-search t))
903 (goto-char (point-min))
904 (while (re-search-forward regexp nil t)
907 (match-string-no-properties d-name-pos)))
910 (match-string-no-properties m-name-pos)))
911 (mm (string-to-number
913 (match-string-no-properties m-pos)
915 (dd (string-to-number
917 (match-string-no-properties d-pos)
920 (match-string-no-properties y-pos)))
923 (if (and (= (length y-str) 2)
924 abbreviated-calendar-year)
926 (extract-calendar-year
927 (calendar-current-date)))
928 (y (+ (string-to-number y-str)
930 (/ current-y 100)))))
931 (if (> (- y current-y) 50)
933 (if (> (- current-y y) 50)
936 (string-to-number y-str)))))
937 (let ((tmp (diary-pull-attrs (buffer-substring-no-properties
938 (point) (line-end-position))
940 (setq entry (nth 0 tmp)
943 (mark-calendar-days-named
947 calendar-day-name-array
948 0 nil calendar-day-abbrev-array) t)) marks)
951 (if (string-equal mm-name "*") 0
955 calendar-month-name-array
956 1 nil calendar-month-abbrev-array) t)))))
957 (mark-calendar-date-pattern mm dd yy marks))))))
958 (mark-sexp-diary-entries)
959 (run-hooks 'nongregorian-diary-marking-hook
960 'mark-diary-entries-hook))
961 (message "Marking diary entries...done")))))
963 (defun mark-sexp-diary-entries ()
964 "Mark days in the calendar window that have sexp diary entries.
965 Each entry in the diary file (or included files) visible in the calendar window
966 is marked. See the documentation for the function `list-sexp-diary-entries'."
967 (let* ((sexp-mark (regexp-quote sexp-diary-entry-symbol))
968 (s-entry (concat "\\(\\`\\|\^M\\|\n\\)\\("
969 sexp-mark "(\\)\\|\\("
970 (regexp-quote diary-nonmarking-symbol)
971 sexp-mark "(diary-remind\\)"))
972 (file-glob-attrs (nth 1 (diary-pull-attrs nil '())))
973 m y first-date last-date mark file-glob-attrs)
974 (with-current-buffer calendar-buffer
975 (setq m displayed-month)
976 (setq y displayed-year))
977 (increment-calendar-month m y -1)
979 (calendar-absolute-from-gregorian (list m 1 y)))
980 (increment-calendar-month m y 2)
982 (calendar-absolute-from-gregorian
983 (list m (calendar-last-day-of-month m y) y)))
984 (goto-char (point-min))
985 (while (re-search-forward s-entry nil t)
986 (setq marking-diary-entry (char-equal (preceding-char) ?\())
987 (re-search-backward "(")
988 (let ((sexp-start (point))
989 sexp entry entry-start line-start marks)
991 (setq sexp (buffer-substring-no-properties sexp-start (point)))
993 (re-search-backward "\^M\\|\n\\|\\`")
994 (setq line-start (point)))
996 (if (and (or (char-equal (preceding-char) ?\^M)
997 (char-equal (preceding-char) ?\n))
998 (not (looking-at " \\|\^I")))
999 (progn;; Diary entry consists only of the sexp
1002 (setq entry-start (point))
1003 ;; Find end of entry
1004 (re-search-forward "\^M\\|\n" nil t)
1005 (while (looking-at " \\|\^I")
1006 (or (re-search-forward "\^M\\|\n" nil t)
1007 (re-search-forward "$" nil t)))
1008 (if (or (char-equal (preceding-char) ?\^M)
1009 (char-equal (preceding-char) ?\n))
1011 (setq entry (buffer-substring-no-properties entry-start (point)))
1012 (while (string-match "[\^M]" entry)
1013 (aset entry (match-beginning 0) ?\n )))
1014 (calendar-for-loop date from first-date to last-date do
1015 (if (setq mark (diary-sexp-entry sexp entry
1016 (calendar-gregorian-from-absolute date)))
1018 (setq marks (diary-pull-attrs entry file-glob-attrs)
1019 marks (nth 1 (diary-pull-attrs entry file-glob-attrs)))
1020 (mark-visible-calendar-date
1021 (calendar-gregorian-from-absolute date)
1022 (if (< 0 (length marks))
1025 (car mark)))))))))))
1027 (defun mark-included-diary-files ()
1028 "Mark the diary entries from other diary files with those of the diary file.
1029 This function is suitable for use as the `mark-diary-entries-hook'; it enables
1030 you to use shared diary files together with your own. The files included are
1031 specified in the diary-file by lines of this form:
1032 #include \"filename\"
1033 This is recursive; that is, #include directives in diary files thus included
1034 are obeyed. You can change the `#include' to some other string by
1035 changing the variable `diary-include-string'."
1036 (goto-char (point-min))
1037 (while (re-search-forward
1039 "\\(?:\\`\\|\^M\\|\n\\)"
1040 (regexp-quote diary-include-string)
1041 " \"\\([^\"]*\\)\"")
1043 (let* ((diary-file (substitute-in-file-name
1044 (match-string-no-properties 1)))
1045 (mark-diary-entries-hook 'mark-included-diary-files)
1046 (dbuff (find-buffer-visiting diary-file)))
1047 (if (file-exists-p diary-file)
1048 (if (file-readable-p diary-file)
1050 (mark-diary-entries)
1052 (kill-buffer (find-buffer-visiting diary-file))))
1054 (message "Can't read included diary file %s" diary-file)
1057 (message "Can't find included diary file %s" diary-file)
1059 (goto-char (point-min)))
1061 (defun mark-calendar-days-named (dayname &optional color)
1062 "Mark all dates in the calendar window that are day DAYNAME of the week.
1063 0 means all Sundays, 1 means all Mondays, and so on."
1064 (with-current-buffer calendar-buffer
1065 (let ((prev-month displayed-month)
1066 (prev-year displayed-year)
1067 (succ-month displayed-month)
1068 (succ-year displayed-year)
1071 (increment-calendar-month succ-month succ-year 1)
1072 (increment-calendar-month prev-month prev-year -1)
1073 (setq day (calendar-absolute-from-gregorian
1074 (calendar-nth-named-day 1 dayname prev-month prev-year)))
1075 (setq last-day (calendar-absolute-from-gregorian
1076 (calendar-nth-named-day -1 dayname succ-month succ-year)))
1077 (while (<= day last-day)
1078 (mark-visible-calendar-date (calendar-gregorian-from-absolute day) color)
1079 (setq day (+ day 7))))))
1081 (defun mark-calendar-date-pattern (month day year &optional color)
1082 "Mark all dates in the calendar window that conform to MONTH/DAY/YEAR.
1083 A value of 0 in any position is a wildcard."
1084 (with-current-buffer calendar-buffer
1085 (let ((m displayed-month)
1087 (increment-calendar-month m y -1)
1088 (calendar-for-loop i from 0 to 2 do
1089 (mark-calendar-month m y month day year color)
1090 (increment-calendar-month m y 1)))))
1092 (defun mark-calendar-month (month year p-month p-day p-year &optional color)
1093 "Mark dates in the MONTH/YEAR that conform to pattern P-MONTH/P_DAY/P-YEAR.
1094 A value of 0 in any position of the pattern is a wildcard."
1095 (if (or (and (= month p-month)
1096 (or (= p-year 0) (= year p-year)))
1098 (or (= p-year 0) (= year p-year))))
1101 i from 1 to (calendar-last-day-of-month month year) do
1102 (mark-visible-calendar-date (list month i year) color))
1103 (mark-visible-calendar-date (list month p-day year) color))))
1105 (defun sort-diary-entries ()
1106 "Sort the list of diary entries by time of day."
1107 (setq diary-entries-list (sort diary-entries-list 'diary-entry-compare)))
1109 (defun diary-entry-compare (e1 e2)
1110 "Returns t if E1 is earlier than E2."
1111 (or (calendar-date-compare e1 e2)
1112 (and (calendar-date-equal (car e1) (car e2))
1113 (let* ((ts1 (cadr e1)) (t1 (diary-entry-time ts1))
1114 (ts2 (cadr e2)) (t2 (diary-entry-time ts2)))
1117 (string-lessp ts1 ts2)))))))
1119 (defcustom diary-unknown-time
1121 "*Value returned by diary-entry-time when no time is found.
1122 The default value -9999 causes entries with no recognizable time to be placed
1123 before those with times; 9999 would place entries with no recognizable time
1124 after those with times."
1129 (defun diary-entry-time (s)
1130 "Return time at the beginning of the string S as a military-style integer.
1131 For example, returns 1325 for 1:25pm.
1133 Returns `diary-unknown-time' (default value -9999) if no time is recognized.
1134 The recognized forms are XXXX, X:XX, or XX:XX (military time), and XXam,
1135 XXAM, XXpm, XXPM, XX:XXam, XX:XXAM XX:XXpm, or XX:XXPM. A period (.) can
1136 be used instead of a colon (:) to separate the hour and minute parts."
1137 (let ((case-fold-search nil))
1138 (cond ((string-match ; Military time
1139 "\\`[ \t\n\\^M]*\\([0-9]?[0-9]\\)[:.]?\\([0-9][0-9]\\)\\(\\>\\|[^ap]\\)"
1141 (+ (* 100 (string-to-number (match-string 1 s)))
1142 (string-to-number (match-string 2 s))))
1143 ((string-match ; Hour only XXam or XXpm
1144 "\\`[ \t\n\\^M]*\\([0-9]?[0-9]\\)\\([ap]\\)m\\>" s)
1145 (+ (* 100 (% (string-to-number (match-string 1 s)) 12))
1146 (if (equal ?a (downcase (aref s (match-beginning 2))))
1148 ((string-match ; Hour and minute XX:XXam or XX:XXpm
1149 "\\`[ \t\n\\^M]*\\([0-9]?[0-9]\\)[:.]\\([0-9][0-9]\\)\\([ap]\\)m\\>" s)
1150 (+ (* 100 (% (string-to-number (match-string 1 s)) 12))
1151 (string-to-number (match-string 2 s))
1152 (if (equal ?a (downcase (aref s (match-beginning 3))))
1154 (t diary-unknown-time)))) ; Unrecognizable
1158 (defun list-sexp-diary-entries (date)
1159 "Add sexp entries for DATE from the diary file to `diary-entries-list'.
1160 Also, Make them visible in the diary file. Returns t if any entries were
1163 Sexp diary entries must be prefaced by a `sexp-diary-entry-symbol' (normally
1164 `%%'). The form of a sexp diary entry is
1168 Both ENTRY and DATE are globally available when the SEXP is evaluated. If the
1169 SEXP yields the value nil, the diary entry does not apply. If it yields a
1170 non-nil value, ENTRY will be taken to apply to DATE; if the non-nil value is a
1171 string, that string will be the diary entry in the fancy diary display.
1173 For example, the following diary entry will apply to the 21st of the month
1174 if it is a weekday and the Friday before if the 21st is on a weekend:
1176 &%%(let ((dayname (calendar-day-of-week date))
1177 (day (extract-calendar-day date)))
1179 (and (= day 21) (memq dayname '(1 2 3 4 5)))
1180 (and (memq day '(19 20)) (= dayname 5)))
1181 ) UIUC pay checks deposited
1183 A number of built-in functions are available for this type of diary entry:
1185 %%(diary-date MONTH DAY YEAR &optional MARK) text
1186 Entry applies if date is MONTH, DAY, YEAR if
1187 `european-calendar-style' is nil, and DAY, MONTH, YEAR if
1188 `european-calendar-style' is t. DAY, MONTH, and YEAR
1189 can be lists of integers, the constant t, or an integer.
1190 The constant t means all values. An optional parameter
1191 MARK specifies a face or single-character string to use
1192 when highlighting the day in the calendar.
1194 %%(diary-float MONTH DAYNAME N &optional DAY MARK) text
1195 Entry will appear on the Nth DAYNAME of MONTH.
1196 (DAYNAME=0 means Sunday, 1 means Monday, and so on;
1197 if N is negative it counts backward from the end of
1198 the month. MONTH can be a list of months, a single
1199 month, or t to specify all months. Optional DAY means
1200 Nth DAYNAME of MONTH on or after/before DAY. DAY defaults
1201 to 1 if N>0 and the last day of the month if N<0. An
1202 optional parameter MARK specifies a face or single-character
1203 string to use when highlighting the day in the calendar.
1205 %%(diary-block M1 D1 Y1 M2 D2 Y2 &optional MARK) text
1206 Entry will appear on dates between M1/D1/Y1 and M2/D2/Y2,
1207 inclusive. (If `european-calendar-style' is t, the
1208 order of the parameters should be changed to D1, M1, Y1,
1209 D2, M2, Y2.) An optional parameter MARK specifies a face
1210 or single-character string to use when highlighting the
1211 day in the calendar.
1213 %%(diary-anniversary MONTH DAY YEAR &optional MARK) text
1214 Entry will appear on anniversary dates of MONTH DAY, YEAR.
1215 (If `european-calendar-style' is t, the order of the
1216 parameters should be changed to DAY, MONTH, YEAR.) Text
1217 can contain %d or %d%s; %d will be replaced by the number
1218 of years since the MONTH DAY, YEAR and %s will be replaced
1219 by the ordinal ending of that number (that is, `st', `nd',
1220 `rd' or `th', as appropriate. The anniversary of February
1221 29 is considered to be March 1 in a non-leap year. An
1222 optional parameter MARK specifies a face or single-character
1223 string to use when highlighting the day in the calendar.
1225 %%(diary-cyclic N MONTH DAY YEAR &optional MARK) text
1226 Entry will appear every N days, starting MONTH DAY, YEAR.
1227 (If `european-calendar-style' is t, the order of the
1228 parameters should be changed to N, DAY, MONTH, YEAR.) Text
1229 can contain %d or %d%s; %d will be replaced by the number
1230 of repetitions since the MONTH DAY, YEAR and %s will
1231 be replaced by the ordinal ending of that number (that is,
1232 `st', `nd', `rd' or `th', as appropriate. An optional
1233 parameter MARK specifies a face or single-character string
1234 to use when highlighting the day in the calendar.
1236 %%(diary-remind SEXP DAYS &optional MARKING) text
1237 Entry is a reminder for diary sexp SEXP. DAYS is either a
1238 single number or a list of numbers indicating the number(s)
1239 of days before the event that the warning(s) should occur.
1240 If the current date is (one of) DAYS before the event
1241 indicated by EXPR, then a suitable message (as specified
1242 by `diary-remind-message') appears. In addition to the
1243 reminders beforehand, the diary entry also appears on
1244 the date itself. If optional MARKING is non-nil then the
1245 *reminders* are marked on the calendar. Marking of
1246 reminders is independent of whether the entry *itself* is
1247 a marking or nonmarking one.
1249 %%(diary-day-of-year)
1250 Diary entries giving the day of the year and the number of
1251 days remaining in the year will be made every day. Note
1252 that since there is no text, it makes sense only if the
1253 fancy diary display is used.
1256 Diary entries giving the corresponding ISO commercial date
1257 will be made every day. Note that since there is no text,
1258 it makes sense only if the fancy diary display is used.
1260 %%(diary-french-date)
1261 Diary entries giving the corresponding French Revolutionary
1262 date will be made every day. Note that since there is no
1263 text, it makes sense only if the fancy diary display is used.
1265 %%(diary-islamic-date)
1266 Diary entries giving the corresponding Islamic date will be
1267 made every day. Note that since there is no text, it
1268 makes sense only if the fancy diary display is used.
1270 %%(diary-hebrew-date)
1271 Diary entries giving the corresponding Hebrew date will be
1272 made every day. Note that since there is no text, it
1273 makes sense only if the fancy diary display is used.
1275 %%(diary-astro-day-number) Diary entries giving the corresponding
1276 astronomical (Julian) day number will be made every day.
1277 Note that since there is no text, it makes sense only if the
1278 fancy diary display is used.
1280 %%(diary-julian-date) Diary entries giving the corresponding
1281 Julian date will be made every day. Note that since
1282 there is no text, it makes sense only if the fancy diary
1285 %%(diary-sunrise-sunset)
1286 Diary entries giving the local times of sunrise and sunset
1287 will be made every day. Note that since there is no text,
1288 it makes sense only if the fancy diary display is used.
1289 Floating point required.
1291 %%(diary-phases-of-moon)
1292 Diary entries giving the times of the phases of the moon
1293 will be when appropriate. Note that since there is no text,
1294 it makes sense only if the fancy diary display is used.
1295 Floating point required.
1297 %%(diary-yahrzeit MONTH DAY YEAR) text
1298 Text is assumed to be the name of the person; the date is
1299 the date of death on the *civil* calendar. The diary entry
1300 will appear on the proper Hebrew-date anniversary and on the
1301 day before. (If `european-calendar-style' is t, the order
1302 of the parameters should be changed to DAY, MONTH, YEAR.)
1304 %%(diary-rosh-hodesh)
1305 Diary entries will be made on the dates of Rosh Hodesh on
1306 the Hebrew calendar. Note that since there is no text, it
1307 makes sense only if the fancy diary display is used.
1310 Diary entries giving the weekly parasha will be made on
1311 every Saturday. Note that since there is no text, it
1312 makes sense only if the fancy diary display is used.
1315 Diary entries giving the omer count will be made every day
1316 from Passover to Shavuot. Note that since there is no text,
1317 it makes sense only if the fancy diary display is used.
1319 Marking these entries is *extremely* time consuming, so these entries are
1320 best if they are nonmarking."
1321 (let ((s-entry (concat "\\(\\`\\|\^M\\|\n\\)"
1322 (regexp-quote diary-nonmarking-symbol)
1324 (regexp-quote sexp-diary-entry-symbol)
1326 entry-found file-glob-attrs marks)
1327 (goto-char (point-min))
1329 (setq file-glob-attrs (nth 1 (diary-pull-attrs nil '()))))
1330 (while (re-search-forward s-entry nil t)
1332 (let ((sexp-start (point))
1333 sexp entry specifier entry-start line-start)
1335 (setq sexp (buffer-substring-no-properties sexp-start (point)))
1337 (re-search-backward "\^M\\|\n\\|\\`")
1338 (setq line-start (point)))
1340 (buffer-substring-no-properties (1+ line-start) (point))
1341 entry-start (1+ line-start))
1343 (if (and (or (char-equal (preceding-char) ?\^M)
1344 (char-equal (preceding-char) ?\n))
1345 (not (looking-at " \\|\^I")))
1346 (progn;; Diary entry consists only of the sexp
1349 (setq entry-start (point))
1350 (re-search-forward "\^M\\|\n" nil t)
1351 (while (looking-at " \\|\^I")
1352 (re-search-forward "\^M\\|\n" nil t))
1354 (setq entry (buffer-substring-no-properties entry-start (point)))
1355 (while (string-match "[\^M]" entry)
1356 (aset entry (match-beginning 0) ?\n )))
1357 (let ((diary-entry (diary-sexp-entry sexp entry date))
1359 (setq entry (if (consp diary-entry)
1364 (subst-char-in-region line-start (point) ?\^M ?\n t)
1365 (if (< 0 (length entry))
1366 (setq temp (diary-pull-attrs entry file-glob-attrs)
1368 marks (nth 1 temp)))))
1369 (add-to-diary-list date
1372 (if entry-start (copy-marker entry-start)
1375 (setq entry-found (or entry-found diary-entry)))))
1378 (defun diary-sexp-entry (sexp entry date)
1379 "Process a SEXP diary ENTRY for DATE."
1380 (let ((result (if calendar-debug-sexp
1381 (let ((stack-trace-on-error t))
1382 (eval (car (read-from-string sexp))))
1384 (eval (car (read-from-string sexp)))
1387 (message "Bad sexp at line %d in %s: %s"
1388 (count-lines (point-min) (point))
1391 (cond ((stringp result) result)
1392 ((and (consp result)
1393 (stringp (cdr result))) result)
1397 (defun diary-date (month day year &optional mark)
1398 "Specific date(s) diary entry.
1399 Entry applies if date is MONTH, DAY, YEAR if `european-calendar-style' is nil,
1400 and DAY, MONTH, YEAR if `european-calendar-style' is t. DAY, MONTH, and YEAR
1401 can be lists of integers, the constant t, or an integer. The constant t means
1404 An optional parameter MARK specifies a face or single-character string to
1405 use when highlighting the day in the calendar."
1406 (let ((dd (if european-calendar-style
1409 (mm (if european-calendar-style
1412 (m (extract-calendar-month date))
1413 (y (extract-calendar-year date))
1414 (d (extract-calendar-day date)))
1416 (or (and (listp dd) (memq d dd))
1419 (or (and (listp mm) (memq m mm))
1422 (or (and (listp year) (memq y year))
1425 (cons mark entry))))
1427 (defun diary-block (m1 d1 y1 m2 d2 y2 &optional mark)
1429 Entry applies if date is between, or on one of, two dates.
1430 The order of the parameters is
1431 M1, D1, Y1, M2, D2, Y2 if `european-calendar-style' is nil, and
1432 D1, M1, Y1, D2, M2, Y2 if `european-calendar-style' is t.
1434 An optional parameter MARK specifies a face or single-character string to
1435 use when highlighting the day in the calendar."
1437 (let ((date1 (calendar-absolute-from-gregorian
1438 (if european-calendar-style
1441 (date2 (calendar-absolute-from-gregorian
1442 (if european-calendar-style
1445 (d (calendar-absolute-from-gregorian date)))
1446 (if (and (<= date1 d) (<= d date2))
1447 (cons mark entry))))
1449 (defun diary-float (month dayname n &optional day mark)
1450 "Floating diary entry--entry applies if date is the nth dayname of month.
1451 Parameters are MONTH, DAYNAME, N. MONTH can be a list of months, the constant
1452 t, or an integer. The constant t means all months. If N is negative, count
1453 backward from the end of the month.
1455 An optional parameter DAY means the Nth DAYNAME on or after/before MONTH DAY.
1456 Optional MARK specifies a face or single-character string to use when
1457 highlighting the day in the calendar."
1458 ;; This is messy because the diary entry may apply, but the date on which it
1459 ;; is based can be in a different month/year. For example, asking for the
1460 ;; first Monday after December 30. For large values of |n| the problem is
1462 (and (= dayname (calendar-day-of-week date))
1463 (let* ((m (extract-calendar-month date))
1464 (d (extract-calendar-day date))
1465 (y (extract-calendar-year date))
1466 (limit; last (n>0) or first (n<0) possible base date for entry
1467 (calendar-nth-named-absday (- n) dayname m y d))
1468 (last-abs (if (> n 0) limit (+ limit 6)))
1469 (first-abs (if (> n 0) (- limit 6) limit))
1470 (last (calendar-gregorian-from-absolute last-abs))
1471 (first (calendar-gregorian-from-absolute first-abs))
1472 ; m1, d1 is first possible base date
1473 (m1 (extract-calendar-month first))
1474 (d1 (extract-calendar-day first))
1475 (y1 (extract-calendar-year first))
1476 ; m2, d2 is last possible base date
1477 (m2 (extract-calendar-month last))
1478 (d2 (extract-calendar-day last))
1479 (y2 (extract-calendar-year last)))
1480 (if (or (and (= m1 m2) ; only possible base dates in one month
1485 (let ((d (or day (if (> n 0)
1487 (calendar-last-day-of-month m1 y1)))))
1488 (and (<= d1 d) (<= d d2))))
1489 ;; only possible base dates straddle two months
1491 (and (= y1 y2) (< m1 m2)))
1493 ;; m1, d1 works as a base date
1499 (<= d1 (or day (if (> n 0)
1501 (calendar-last-day-of-month m1 y1)))))
1502 ;; m2, d2 works as a base date
1503 (and (or (eq month t)
1507 (<= (or day (if (> n 0)
1509 (calendar-last-day-of-month m2 y2)))
1511 (cons mark entry)))))
1514 (defun diary-anniversary (month day year &optional mark)
1515 "Anniversary diary entry.
1516 Entry applies if date is the anniversary of MONTH, DAY, YEAR if
1517 `european-calendar-style' is nil, and DAY, MONTH, YEAR if
1518 `european-calendar-style' is t. Diary entry can contain `%d' or `%d%s'; the
1519 %d will be replaced by the number of years since the MONTH DAY, YEAR and the
1520 %s will be replaced by the ordinal ending of that number (that is, `st', `nd',
1521 `rd' or `th', as appropriate. The anniversary of February 29 is considered
1522 to be March 1 in non-leap years.
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 (y (extract-calendar-year date))
1534 (if (and (= m 2) (= d 29) (not (calendar-leap-year-p y)))
1537 (if (and (> diff 0) (calendar-date-equal (list m d y) date))
1538 (cons mark (format entry diff (diary-ordinal-suffix diff))))))
1540 (defun diary-cyclic (n month day year &optional mark)
1541 "Cycle diary entry--entry applies every N days starting at MONTH, DAY, YEAR.
1542 If `european-calendar-style' is t, parameters are N, DAY, MONTH, YEAR.
1543 ENTRY can contain `%d' or `%d%s'; the %d will be replaced by the number of
1544 repetitions since the MONTH DAY, YEAR and %s will be replaced by the
1545 ordinal ending of that number (that is, `st', `nd', `rd' or `th', as
1548 An optional parameter MARK specifies a face or single-character string to
1549 use when highlighting the day in the calendar."
1550 (let* ((d (if european-calendar-style
1553 (m (if european-calendar-style
1556 (diff (- (calendar-absolute-from-gregorian date)
1557 (calendar-absolute-from-gregorian
1560 (if (and (>= diff 0) (zerop (% diff n)))
1561 (cons mark (format entry cycle (diary-ordinal-suffix cycle))))))
1563 (defun diary-ordinal-suffix (n)
1564 "Ordinal suffix for N. (That is, `st', `nd', `rd', or `th', as appropriate.)"
1565 (if (or (memq (% n 100) '(11 12 13))
1568 (aref ["th" "st" "nd" "rd"] (% n 10))))
1570 (defun diary-day-of-year ()
1571 "Day of year and number of days remaining in the year of date diary entry."
1572 (calendar-day-of-year-string date))
1574 (defcustom diary-remind-message
1576 (if (= 0 (% days 7))
1577 (concat (int-to-string (/ days 7)) (if (= 7 days) " week" " weeks"))
1578 (concat (int-to-string days) (if (= 1 days) " day" " days")))
1581 "*Pseudo-pattern giving form of reminder messages in the fancy diary
1584 Used by the function `diary-remind', a pseudo-pattern is a list of
1585 expressions that can involve the keywords `days' (a number), `date' (a list of
1586 month, day, year), and `diary-entry' (a string)."
1590 (defun diary-remind (sexp days &optional marking)
1591 "Provide a reminder of a diary entry.
1592 SEXP is a diary-sexp. DAYS is either a single number or a list of numbers
1593 indicating the number(s) of days before the event that the warning(s) should
1594 occur on. If the current date is (one of) DAYS before the event indicated by
1595 SEXP, then a suitable message (as specified by `diary-remind-message' is
1598 In addition to the reminders beforehand, the diary entry also appears on the
1601 A `diary-nonmarking-symbol' at the beginning of the line of the diary-remind
1602 entry specifies that the diary entry (not the reminder) is non-marking.
1603 Marking of reminders is independent of whether the entry itself is a marking
1604 or nonmarking; if optional parameter MARKING is non-nil then the reminders are
1605 marked on the calendar."
1606 (let ((diary-entry (eval sexp)))
1608 ;; Diary entry applies on date
1610 (or (not marking-diary-entries) marking-diary-entry))
1612 ;; Diary entry may apply to `days' before date
1613 ((and (integerp days)
1614 (not diary-entry); Diary entry does not apply to date
1615 (or (not marking-diary-entries) marking))
1616 (let ((date (calendar-gregorian-from-absolute
1617 (+ (calendar-absolute-from-gregorian date) days))))
1618 (when (setq diary-entry (eval sexp)) ; re-evaluate with adjusted date
1619 ;; Discard any mark portion from diary-anniversary, etc.
1620 (if (consp diary-entry) (setq diary-entry (cdr diary-entry)))
1621 (mapconcat 'eval diary-remind-message ""))))
1622 ;; Diary entry may apply to one of a list of days before date
1623 ((and (listp days) days)
1624 (or (diary-remind sexp (car days) marking)
1625 (diary-remind sexp (cdr days) marking))))))
1627 (defvar diary-modify-entry-list-string-function nil
1628 "Function applied to entry string before putting it into the entries list.
1629 Can be used by programs integrating a diary list into other buffers (e.g.
1630 org.el and planner.el) to modify the string or add properties to it.
1631 The function takes a string argument and must return a string.")
1633 (defun add-to-diary-list (date string specifier &optional marker globcolor)
1634 "Add the entry (DATE STRING SPECIFIER MARKER GLOBCOLOR) to `diary-entries-list'.
1635 Do nothing if DATE or STRING is nil."
1636 (when (and date string)
1637 (if diary-file-name-prefix
1638 (let ((prefix (funcall diary-file-name-prefix-function
1639 (buffer-file-name))))
1640 (or (string= prefix "")
1641 (setq string (format "[%s] %s" prefix string)))))
1642 (and diary-modify-entry-list-string-function
1643 (setq string (funcall diary-modify-entry-list-string-function
1645 (setq diary-entries-list
1646 (append diary-entries-list
1647 (list (list date string specifier marker globcolor))))))
1649 (defun diary-redraw-calendar ()
1650 "If `calendar-buffer' is live and diary entries are marked, redraw it."
1651 (and mark-diary-entries-in-calendar
1654 ;; Return value suitable for `write-contents-functions'.
1657 (defun make-diary-entry (string &optional nonmarking file)
1658 "Insert a diary entry STRING which may be NONMARKING in FILE.
1659 If omitted, NONMARKING defaults to nil and FILE defaults to
1660 `diary-file'. Adds `diary-redraw-calendar' to
1661 `write-contents-functions' for FILE, so that the calendar will be
1662 redrawn with the new entry marked, if necessary."
1663 (let ((pop-up-frames (window-dedicated-p (selected-window))))
1664 (find-file-other-window (substitute-in-file-name (or file diary-file))))
1665 (add-hook 'after-save-hook 'diary-redraw-calendar nil t)
1667 (diary-unhide-everything)
1668 (goto-char (point-max))
1669 (when (let ((case-fold-search t))
1670 (search-backward "Local Variables:"
1671 (max (- (point-max) 3000) (point-min))
1678 (if nonmarking diary-nonmarking-symbol "")
1681 (defun insert-diary-entry (arg)
1682 "Insert a diary entry for the date indicated by point.
1683 Prefix arg will make the entry nonmarking."
1685 (make-diary-entry (calendar-date-string (calendar-cursor-to-date t) t t)
1688 (defun insert-weekly-diary-entry (arg)
1689 "Insert a weekly diary entry for the day of the week indicated by point.
1690 Prefix arg will make the entry nonmarking."
1692 (make-diary-entry (calendar-day-name (calendar-cursor-to-date t))
1695 (defun insert-monthly-diary-entry (arg)
1696 "Insert a monthly diary entry for the day of the month indicated by point.
1697 Prefix arg will make the entry nonmarking."
1699 (let ((calendar-date-display-form
1700 (if european-calendar-style
1703 (make-diary-entry (calendar-date-string (calendar-cursor-to-date t) t)
1706 (defun insert-yearly-diary-entry (arg)
1707 "Insert an annual diary entry for the day of the year indicated by point.
1708 Prefix arg will make the entry nonmarking."
1710 (let ((calendar-date-display-form
1711 (if european-calendar-style
1712 '(day " " monthname)
1713 '(monthname " " day))))
1714 (make-diary-entry (calendar-date-string (calendar-cursor-to-date t) t)
1717 (defun insert-anniversary-diary-entry (arg)
1718 "Insert an anniversary diary entry for the date given by point.
1719 Prefix arg will make the entry nonmarking."
1721 (let ((calendar-date-display-form
1722 (if european-calendar-style
1723 '(day " " month " " year)
1724 '(month " " day " " year))))
1726 (format "%s(diary-anniversary %s)"
1727 sexp-diary-entry-symbol
1728 (calendar-date-string (calendar-cursor-to-date t) nil t))
1731 (defun insert-block-diary-entry (arg)
1732 "Insert a block diary entry for the days between the point and marked date.
1733 Prefix arg will make the entry nonmarking."
1735 (let ((calendar-date-display-form
1736 (if european-calendar-style
1737 '(day " " month " " year)
1738 '(month " " day " " year)))
1739 (cursor (calendar-cursor-to-date t))
1740 (mark (or (car calendar-mark-ring)
1741 (error "No mark set in this buffer")))
1743 (if (< (calendar-absolute-from-gregorian mark)
1744 (calendar-absolute-from-gregorian cursor))
1750 (format "%s(diary-block %s %s)"
1751 sexp-diary-entry-symbol
1752 (calendar-date-string start nil t)
1753 (calendar-date-string end nil t))
1756 (defun insert-cyclic-diary-entry (arg)
1757 "Insert a cyclic diary entry starting at the date given by point.
1758 Prefix arg will make the entry nonmarking."
1760 (let ((calendar-date-display-form
1761 (if european-calendar-style
1762 '(day " " month " " year)
1763 '(month " " day " " year))))
1765 (format "%s(diary-cyclic %d %s)"
1766 sexp-diary-entry-symbol
1767 (calendar-read "Repeat every how many days: "
1768 (lambda (x) (> x 0)))
1769 (calendar-date-string (calendar-cursor-to-date t) nil t))
1772 (defvar diary-mode-map
1773 (let ((map (make-sparse-keymap)))
1774 (define-key map "\C-c\C-s" 'diary-show-all-entries)
1775 (define-key map "\C-c\C-q" 'quit-window)
1777 "Keymap for `diary-mode'.")
1780 (define-derived-mode diary-mode fundamental-mode "Diary"
1781 "Major mode for editing the diary file."
1782 (set (make-local-variable 'font-lock-defaults)
1783 '(diary-font-lock-keywords t))
1784 (add-to-invisibility-spec '(diary . nil))
1785 (add-hook 'after-save-hook 'diary-redraw-calendar nil t)
1786 (if diary-header-line-flag
1787 (setq header-line-format diary-header-line-format)))
1789 (define-derived-mode fancy-diary-display-mode fundamental-mode
1791 "Major mode used while displaying diary entries using Fancy Display."
1792 (set (make-local-variable 'font-lock-defaults)
1793 '(fancy-diary-font-lock-keywords t))
1794 (local-set-key "q" 'quit-window))
1797 (defvar fancy-diary-font-lock-keywords
1801 (let ((dayname (diary-name-pattern calendar-day-name-array nil t))
1802 (monthname (diary-name-pattern calendar-month-name-array nil t))
1806 (mapconcat 'eval calendar-date-display-form ""))
1807 "\\(\\(: .*\\)\\|\\(\n +.*\\)\\)*\n=+$")
1809 '("^.*anniversary.*$" . font-lock-keyword-face)
1810 '("^.*birthday.*$" . font-lock-keyword-face)
1811 '("^.*Yahrzeit.*$" . font-lock-reference-face)
1812 '("^\\(Erev \\)?Rosh Hodesh.*" . font-lock-function-name-face)
1813 '("^Day.*omer.*$" . font-lock-builtin-face)
1814 '("^Parashat.*$" . font-lock-comment-face)
1815 '("^[ \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\\)?\\)?"
1816 . font-lock-variable-name-face))
1817 "Keywords to highlight in fancy diary display")
1820 (defun diary-font-lock-sexps (limit)
1821 "Recognize sexp diary entry for font-locking."
1822 (if (re-search-forward
1823 (concat "^" (regexp-quote diary-nonmarking-symbol)
1824 "?\\(" (regexp-quote sexp-diary-entry-symbol) "\\)")
1828 (narrow-to-region (point-min) limit)
1829 (let ((start (point)))
1831 (store-match-data (list start (point)))
1835 (defun diary-font-lock-date-forms (month-array &optional symbol abbrev-array)
1836 "Create font-lock patterns for `diary-date-forms' using MONTH-ARRAY.
1837 If given, optional SYMBOL must be a prefix to entries.
1838 If optional ABBREV-ARRAY is present, the abbreviations constructed
1839 from this array by the function `calendar-abbrev-construct' are
1840 matched (with or without a final `.'), in addition to the full month
1842 (let ((dayname (diary-name-pattern calendar-day-name-array
1843 calendar-day-abbrev-array t))
1844 (monthname (format "\\(%s\\|\\*\\)"
1845 (diary-name-pattern month-array abbrev-array)))
1846 (month "\\([0-9]+\\|\\*\\)")
1847 (day "\\([0-9]+\\|\\*\\)")
1848 (year "-?\\([0-9]+\\|\\*\\)"))
1851 (concat "^" (regexp-quote diary-nonmarking-symbol) "?"
1852 (if symbol (regexp-quote symbol) "") "\\("
1854 ;; If backup, omit first item (backup)
1855 ;; and last item (not part of date)
1856 (if (equal (car x) 'backup)
1857 (nreverse (cdr (reverse (cdr x))))
1860 ;; With backup, last item is not part of date
1861 (if (equal (car x) 'backup)
1862 (concat "\\)" (eval (car (reverse x))))
1867 (eval-when-compile (require 'cal-hebrew)
1868 (require 'cal-islam))
1870 (defvar diary-font-lock-keywords
1872 (diary-font-lock-date-forms calendar-month-name-array
1873 nil calendar-month-abbrev-array)
1874 (when (or (memq 'mark-hebrew-diary-entries
1875 nongregorian-diary-marking-hook)
1876 (memq 'list-hebrew-diary-entries
1877 nongregorian-diary-listing-hook))
1878 (require 'cal-hebrew)
1879 (diary-font-lock-date-forms
1880 calendar-hebrew-month-name-array-leap-year
1881 hebrew-diary-entry-symbol))
1882 (when (or (memq 'mark-islamic-diary-entries
1883 nongregorian-diary-marking-hook)
1884 (memq 'list-islamic-diary-entries
1885 nongregorian-diary-listing-hook))
1886 (require 'cal-islam)
1887 (diary-font-lock-date-forms
1888 calendar-islamic-month-name-array
1889 islamic-diary-entry-symbol))
1892 (concat "^" (regexp-quote diary-include-string) ".*$")
1893 'font-lock-keyword-face)
1895 (concat "^" (regexp-quote diary-nonmarking-symbol)
1896 "?\\(" (regexp-quote sexp-diary-entry-symbol) "\\)")
1897 '(1 font-lock-reference-face))
1899 (concat "^" (regexp-quote diary-nonmarking-symbol))
1900 'font-lock-reference-face)
1902 (concat "^" (regexp-quote diary-nonmarking-symbol)
1903 "?\\(" (regexp-quote hebrew-diary-entry-symbol) "\\)")
1904 '(1 font-lock-reference-face))
1906 (concat "^" (regexp-quote diary-nonmarking-symbol)
1907 "?\\(" (regexp-quote islamic-diary-entry-symbol) "\\)")
1908 '(1 font-lock-reference-face))
1909 '(diary-font-lock-sexps . font-lock-keyword-face)
1910 '("[0-9]?[0-9]\\([:.]?[0-9][0-9]\\)?\\(am\\|pm\\|AM\\|PM\\)\\(-[0-9]?[0-9]\\([:.]?[0-9][0-9]\\)?\\(am\\|pm\\|AM\\|PM\\)\\)?"
1911 . font-lock-function-name-face)))
1912 "Forms to highlight in `diary-mode'.")
1915 ;; Following code from Dave Love <fx@gnu.org>.
1916 ;; Import Outlook-format appointments from mail messages in Gnus or
1917 ;; Rmail using command `diary-from-outlook'. This, or the specialized
1918 ;; functions `diary-from-outlook-gnus' and `diary-from-outlook-rmail',
1919 ;; could be run from hooks to notice appointments automatically (in
1920 ;; which case they will prompt about adding to the diary). The
1921 ;; message formats recognized are customizable through
1922 ;; `diary-outlook-formats'.
1924 (defcustom diary-outlook-formats
1926 ;; When: 11 October 2001 12:00-14:00 (GMT) Greenwich Mean Time : Dublin, ...
1927 ;; [Current UK format? The timezone is meaningless. Sometimes the
1928 ;; Where is missing.]
1929 ("When: \\([0-9]+ [[:alpha:]]+ [0-9]+\\) \
1932 \\(?:Where: \\([^\n]+\\)\n+\\)?
1933 \\*~\\*~\\*~\\*~\\*~\\*~\\*~\\*~\\*~\\*"
1934 . "\\1\n \\2 %s, \\3")
1935 ;; When: Tuesday, April 30, 2002 03:00 PM-03:30 PM (GMT) Greenwich Mean ...
1937 ("^When: [[:alpha:]]+, \\([[:alpha:]]+\\) \\([0-9][0-9]*\\), \\([0-9]\\{4\\}\\) \
1940 \\(?:Where: \\([^\n]+\\)\\)?\n+"
1941 . "\\2 \\1 \\3\n \\4 %s, \\5")
1943 ;; German format, apparently.
1944 "^Zeit: [^ ]+, +\\([0-9]+\\)\. +\\([[:upper:]][[:lower:]][[:lower:]]\\)[^ ]* +\\([0-9]+\\) +\\([^ ]+\\).*$"
1945 . "\\1 \\2 \\3\n \\4 %s"))
1946 "Alist of regexps matching message text and replacement text.
1948 The regexp must match the start of the message text containing an
1949 appointment, but need not include a leading `^'. If it matches the
1950 current message, a diary entry is made from the corresponding
1951 template. If the template is a string, it should be suitable for
1952 passing to `replace-match', and so will have occurrences of `\\D' to
1953 substitute the match for the Dth subexpression. It must also contain
1954 a single `%s' which will be replaced with the text of the message's
1955 Subject field. Any other `%' characters must be doubled, so that the
1956 template can be passed to `format'.
1958 If the template is actually a function, it is called with the message
1959 body text as argument, and may use `match-string' etc. to make a
1960 template following the rules above."
1961 :type '(alist :key-type (regexp :tag "Regexp matching time/place")
1963 (string :tag "Template for entry")
1964 (function :tag "Unary function providing template")))
1969 ;; Dynamically bound.
1973 (defun diary-from-outlook-internal (&optional test-only)
1974 "Snarf a diary entry from a message assumed to be from MS Outlook.
1975 Assumes `body' is bound to a string comprising the body of the message and
1976 `subject' is bound to a string comprising its subject.
1977 Arg TEST-ONLY non-nil means return non-nil if and only if the
1978 message contains an appointment, don't make a diary entry."
1980 (let (format-string)
1981 (dotimes (i (length diary-outlook-formats))
1982 (when (eq 0 (string-match (car (nth i diary-outlook-formats))
1985 (setq format-string (cdr (nth i diary-outlook-formats)))
1987 (save-window-excursion
1988 ;; Fixme: References to optional fields in the format
1989 ;; are treated literally, not replaced by the empty
1990 ;; string. I think this is an Emacs bug.
1992 (format (replace-match (if (functionp format-string)
1993 (funcall format-string body)
1995 t nil (match-string 0 body))
1998 (throw 'finished t))))
2001 (defun diary-from-outlook (&optional noconfirm)
2002 "Maybe snarf diary entry from current Outlook-generated message.
2003 Currently knows about Gnus and Rmail modes. Unless the optional
2004 argument NOCONFIRM is non-nil (which is the case when this
2005 function is called interactively), then if an entry is found the
2006 user is asked to confirm its addition."
2009 ((eq major-mode 'rmail-mode)
2010 #'diary-from-outlook-rmail)
2011 ((memq major-mode '(gnus-summary-mode gnus-article-mode))
2012 #'diary-from-outlook-gnus)
2013 (t (error "Don't know how to snarf in `%s'" major-mode)))))
2014 (funcall func noconfirm)))
2017 (defvar gnus-article-mime-handles)
2018 (defvar gnus-article-buffer)
2020 (autoload 'gnus-fetch-field "gnus-util")
2021 (autoload 'gnus-narrow-to-body "gnus")
2022 (autoload 'mm-get-part "mm-decode")
2024 (defun diary-from-outlook-gnus (&optional noconfirm)
2025 "Maybe snarf diary entry from Outlook-generated message in Gnus.
2026 Unless the optional argument NOCONFIRM is non-nil (which is the case when
2027 this function is called interactively), then if an entry is found the
2028 user is asked to confirm its addition.
2029 Add this function to `gnus-article-prepare-hook' to notice appointments
2032 (with-current-buffer gnus-article-buffer
2033 (let ((subject (gnus-fetch-field "subject"))
2034 (body (if gnus-article-mime-handles
2035 ;; We're multipart. Don't get confused by part
2036 ;; buttons &c. Assume info is in first part.
2037 (mm-get-part (nth 1 gnus-article-mime-handles))
2039 (gnus-narrow-to-body)
2041 (when (diary-from-outlook-internal t)
2042 (when (or noconfirm (y-or-n-p "Snarf diary entry? "))
2043 (diary-from-outlook-internal)
2044 (message "Diary entry added"))))))
2046 (custom-add-option 'gnus-article-prepare-hook 'diary-from-outlook-gnus)
2049 (defvar rmail-buffer)
2051 (defun diary-from-outlook-rmail (&optional noconfirm)
2052 "Maybe snarf diary entry from Outlook-generated message in Rmail.
2053 Unless the optional argument NOCONFIRM is non-nil (which is the case when
2054 this function is called interactively), then if an entry is found the
2055 user is asked to confirm its addition."
2057 (with-current-buffer rmail-buffer
2058 (let ((subject (mail-fetch-field "subject"))
2059 (body (buffer-substring (save-excursion
2063 (when (diary-from-outlook-internal t)
2064 (when (or noconfirm (y-or-n-p "Snarf diary entry? "))
2065 (diary-from-outlook-internal)
2066 (message "Diary entry added"))))))
2069 (provide 'diary-lib)
2071 ;; arch-tag: 22dd506e-2e33-410d-9ae1-095a0c1b2010
2072 ;;; diary-lib.el ends here