(entry): Declare for compiler part-way through.
[emacs.git] / lisp / calendar / diary-lib.el
blobe84f08a475edf2742d637501707b48b86bdbeec2
1 ;;; diary-lib.el --- diary functions
3 ;; Copyright (C) 1989, 1990, 1992, 1993, 1994, 1995, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
7 ;; Maintainer: Glenn Morris <rgm@gnu.org>
8 ;; Keywords: calendar
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 3, or (at your option)
15 ;; any later version.
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.
27 ;;; Commentary:
29 ;; This collection of functions implements the diary features as described
30 ;; in calendar.el.
32 ;;; Code:
34 (require 'calendar)
36 (defcustom diary-include-string "#include"
37 "The string indicating inclusion of another file of diary entries.
38 See the documentation for the function `include-other-diary-files'."
39 :type 'string
40 :group 'diary)
42 (defcustom diary-list-include-blanks nil
43 "If nil, do not include days with no diary entry in the list of diary entries.
44 Such days will then not be shown in the fancy diary buffer, even if they
45 are holidays."
46 :type 'boolean
47 :group 'diary)
49 (defcustom diary-glob-file-regexp-prefix "^\\#"
50 "Regular expression prepended to attribute-regexps for file-wide specifiers."
51 :type 'regexp
52 :group 'diary)
54 (defcustom diary-face 'diary
55 "Face name to use for diary entries."
56 :type 'face
57 :group 'diary)
58 (make-obsolete-variable 'diary-face "customize the face `diary' instead."
59 "23.1")
61 (defcustom diary-face-attrs
62 '((" *\\[foreground:\\([-a-z]+\\)\\]$" 1 :foreground string)
63 (" *\\[background:\\([-a-z]+\\)\\]$" 1 :background string)
64 (" *\\[width:\\([-a-z]+\\)\\]$" 1 :width symbol)
65 (" *\\[height:\\([-0-9a-z]+\\)\\]$" 1 :height int)
66 (" *\\[weight:\\([-a-z]+\\)\\]$" 1 :weight symbol)
67 (" *\\[slant:\\([-a-z]+\\)\\]$" 1 :slant symbol)
68 (" *\\[underline:\\([-a-z]+\\)\\]$" 1 :underline stringtnil)
69 (" *\\[overline:\\([-a-z]+\\)\\]$" 1 :overline stringtnil)
70 (" *\\[strike-through:\\([-a-z]+\\)\\]$" 1 :strike-through stringtnil)
71 (" *\\[inverse-video:\\([-a-z]+\\)\\]$" 1 :inverse-video tnil)
72 (" *\\[face:\\([-0-9a-z]+\\)\\]$" 1 :face string)
73 (" *\\[font:\\([-a-z0-9]+\\)\\]$" 1 :font string)
74 ;; Unsupported.
75 ;;; (" *\\[box:\\([-a-z]+\\)\\]$" 1 :box)
76 ;;; (" *\\[stipple:\\([-a-z]+\\)\\]$" 1 :stipple)
78 "A list of (regexp regnum attr attrtype) lists where the
79 regexp says how to find the tag, the regnum says which
80 parenthetical sub-regexp this regexp looks for, and the attr says
81 which attribute of the face (or that this _is_ a face) is being
82 modified."
83 :type 'sexp
84 :group 'diary)
86 (defcustom diary-file-name-prefix nil
87 "Non-nil means prefix each diary entry with the name of the file defining it."
88 :type 'boolean
89 :group 'diary)
91 (defcustom diary-file-name-prefix-function 'identity
92 "The function that will take a diary file name and return the desired prefix."
93 :type 'function
94 :group 'diary)
96 (defcustom sexp-diary-entry-symbol "%%"
97 "The string used to indicate a sexp diary entry in `diary-file'.
98 See the documentation for the function `list-sexp-diary-entries'."
99 :type 'string
100 :group 'diary)
102 (defcustom list-diary-entries-hook nil
103 "List of functions called after diary file is culled for relevant entries.
104 It is to be used for diary entries that are not found in the diary file.
106 A function `include-other-diary-files' is provided for use as the value of
107 this hook. This function enables you to use shared diary files together
108 with your own. The files included are specified in the diary file by lines
109 of the form
111 #include \"filename\"
113 This is recursive; that is, #include directives in files thus included are
114 obeyed. You can change the \"#include\" to some other string by changing
115 the variable `diary-include-string'. When you use `include-other-diary-files'
116 as part of the list-diary-entries-hook, you will probably also want to use the
117 function `mark-included-diary-files' as part of `mark-diary-entries-hook'.
119 For example, you could use
121 (add-hook 'list-diary-entries-hook 'include-other-diary-files)
122 (add-hook 'list-diary-entries-hook 'sort-diary-entries)
123 (add-hook 'diary-display-hook 'fancy-diary-display)
125 in your `.emacs' file to cause the fancy diary buffer to be displayed with
126 diary entries from various included files, each day's entries sorted into
127 lexicographic order."
128 :type 'hook
129 :options '(include-other-diary-files sort-diary-entries)
130 :group 'diary)
132 (defcustom mark-diary-entries-hook nil
133 "List of functions called after marking diary entries in the calendar.
135 A function `mark-included-diary-files' is also provided for use as the
136 `mark-diary-entries-hook'; it enables you to use shared diary files together
137 with your own. The files included are specified in the diary file by lines
138 of the form
139 #include \"filename\"
140 This is recursive; that is, #include directives in files thus included are
141 obeyed. You can change the \"#include\" to some other string by changing the
142 variable `diary-include-string'. When you use `mark-included-diary-files' as
143 part of the mark-diary-entries-hook, you will probably also want to use the
144 function `include-other-diary-files' as part of `list-diary-entries-hook'."
145 :type 'hook
146 :options '(mark-included-diary-files)
147 :group 'diary)
149 (defcustom nongregorian-diary-listing-hook nil
150 "List of functions called for listing diary file and included files.
151 As the files are processed for diary entries, these functions are used
152 to cull relevant entries. You can use either or both of
153 `list-hebrew-diary-entries', `list-islamic-diary-entries' and
154 `diary-bahai-list-entries'. The documentation for these functions
155 describes the style of such diary entries."
156 :type 'hook
157 :options '(list-hebrew-diary-entries
158 list-islamic-diary-entries
159 diary-bahai-list-entries)
160 :group 'diary)
162 (defcustom nongregorian-diary-marking-hook nil
163 "List of functions called for marking diary file and included files.
164 As the files are processed for diary entries, these functions are used
165 to cull relevant entries. You can use either or both of
166 `mark-hebrew-diary-entries', `mark-islamic-diary-entries' and
167 `mark-bahai-diary-entries'. The documentation for these functions
168 describes the style of such diary entries."
169 :type 'hook
170 :options '(mark-hebrew-diary-entries
171 mark-islamic-diary-entries
172 diary-bahai-mark-entries)
173 :group 'diary)
175 (defcustom print-diary-entries-hook 'lpr-buffer
176 "List of functions called after a temporary diary buffer is prepared.
177 The buffer shows only the diary entries currently visible in the diary
178 buffer. The default just does the printing. Other uses might include, for
179 example, rearranging the lines into order by day and time, saving the buffer
180 instead of deleting it, or changing the function used to do the printing."
181 :type 'hook
182 :group 'diary)
184 (defcustom diary-unknown-time -9999
185 "Value returned by diary-entry-time when no time is found.
186 The default value -9999 causes entries with no recognizable time to be placed
187 before those with times; 9999 would place entries with no recognizable time
188 after those with times."
189 :type 'integer
190 :group 'diary
191 :version "20.3")
193 (defcustom diary-mail-addr
194 (if (boundp 'user-mail-address) user-mail-address "")
195 "Email address that `diary-mail-entries' will send email to."
196 :group 'diary
197 :type 'string
198 :version "20.3")
200 (defcustom diary-mail-days 7
201 "Default number of days for `diary-mail-entries' to check."
202 :group 'diary
203 :type 'integer
204 :version "20.3")
206 (defcustom diary-remind-message
207 '("Reminder: Only "
208 (if (= 0 (% days 7))
209 (concat (int-to-string (/ days 7)) (if (= 7 days) " week" " weeks"))
210 (concat (int-to-string days) (if (= 1 days) " day" " days")))
211 " until "
212 diary-entry)
213 "Pseudo-pattern giving form of reminder messages in the fancy diary display.
215 Used by the function `diary-remind', a pseudo-pattern is a list of
216 expressions that can involve the keywords `days' (a number), `date' (a list of
217 month, day, year), and `diary-entry' (a string)."
218 :type 'sexp
219 :group 'diary)
221 (defcustom diary-outlook-formats
223 ;; When: 11 October 2001 12:00-14:00 (GMT) Greenwich Mean Time : Dublin, ...
224 ;; [Current UK format? The timezone is meaningless. Sometimes the
225 ;; Where is missing.]
226 ("When: \\([0-9]+ [[:alpha:]]+ [0-9]+\\) \
227 \\([^ ]+\\) [^\n]+
228 \[^\n]+
229 \\(?:Where: \\([^\n]+\\)\n+\\)?
230 \\*~\\*~\\*~\\*~\\*~\\*~\\*~\\*~\\*~\\*"
231 . "\\1\n \\2 %s, \\3")
232 ;; When: Tuesday, April 30, 2002 03:00 PM-03:30 PM (GMT) Greenwich Mean ...
233 ;; [Old UK format?]
234 ("^When: [[:alpha:]]+, \\([[:alpha:]]+\\) \\([0-9][0-9]*\\), \\([0-9]\\{4\\}\\) \
235 \\([^ ]+\\) [^\n]+
236 \[^\n]+
237 \\(?:Where: \\([^\n]+\\)\\)?\n+"
238 . "\\2 \\1 \\3\n \\4 %s, \\5")
240 ;; German format, apparently.
241 "^Zeit: [^ ]+, +\\([0-9]+\\)\. +\\([[:upper:]][[:lower:]][[:lower:]]\\)[^ ]* +\\([0-9]+\\) +\\([^ ]+\\).*$"
242 . "\\1 \\2 \\3\n \\4 %s"))
243 "Alist of regexps matching message text and replacement text.
245 The regexp must match the start of the message text containing an
246 appointment, but need not include a leading `^'. If it matches the
247 current message, a diary entry is made from the corresponding
248 template. If the template is a string, it should be suitable for
249 passing to `replace-match', and so will have occurrences of `\\D' to
250 substitute the match for the Dth subexpression. It must also contain
251 a single `%s' which will be replaced with the text of the message's
252 Subject field. Any other `%' characters must be doubled, so that the
253 template can be passed to `format'.
255 If the template is actually a function, it is called with the message
256 body text as argument, and may use `match-string' etc. to make a
257 template following the rules above."
258 :type '(alist :key-type (regexp :tag "Regexp matching time/place")
259 :value-type (choice
260 (string :tag "Template for entry")
261 (function :tag
262 "Unary function providing template")))
263 :version "22.1"
264 :group 'diary)
266 ;;; More user options below and in calendar.el.
269 (defun diary-check-diary-file ()
270 "Check that the file specified by `diary-file' exists and is readable.
271 If so, return the expanded file name, otherwise signal an error."
272 (let ((d-file (substitute-in-file-name diary-file)))
273 (if (and d-file (file-exists-p d-file))
274 (if (file-readable-p d-file)
275 d-file
276 (error "Diary file `%s' is not readable" diary-file))
277 (error "Diary file `%s' does not exist" diary-file))))
279 ;;;###autoload
280 (defun diary (&optional arg)
281 "Generate the diary window for ARG days starting with the current date.
282 If no argument is provided, the number of days of diary entries is governed
283 by the variable `number-of-diary-entries'. A value of ARG less than 1
284 does nothing. This function is suitable for execution in a `.emacs' file."
285 (interactive "P")
286 (diary-check-diary-file)
287 (let ((date (calendar-current-date)))
288 (diary-list-entries date (if arg (prefix-numeric-value arg)))))
290 (define-obsolete-function-alias 'view-diary-entries 'diary-view-entries)
291 (defun diary-view-entries (&optional arg)
292 "Prepare and display a buffer with diary entries.
293 Searches the file named in `diary-file' for entries that
294 match ARG days starting with the date indicated by the cursor position
295 in the displayed three-month calendar."
296 (interactive "p")
297 (diary-check-diary-file)
298 (diary-list-entries (calendar-cursor-to-date t) arg))
300 (defun view-other-diary-entries (arg d-file)
301 "Prepare and display buffer of diary entries from an alternative diary file.
302 Searches for entries that match ARG days, starting with the date indicated
303 by the cursor position in the displayed three-month calendar.
304 D-FILE specifies the file to use as the diary file."
305 (interactive
306 (list (prefix-numeric-value current-prefix-arg)
307 (read-file-name "Enter diary file name: " default-directory nil t)))
308 (let ((diary-file d-file))
309 (diary-view-entries arg)))
311 (autoload 'calendar-check-holidays "holidays"
312 "Check the list of holidays for any that occur on DATE.
313 The value returned is a list of strings of relevant holiday descriptions.
314 The holidays are those in the list `calendar-holidays'.")
316 (autoload 'calendar-holiday-list "holidays"
317 "Form the list of holidays that occur on dates in the calendar window.
318 The holidays are those in the list `calendar-holidays'.")
320 (autoload 'diary-french-date "cal-french"
321 "French calendar equivalent of date diary entry.")
323 (autoload 'diary-mayan-date "cal-mayan"
324 "Mayan calendar equivalent of date diary entry.")
326 (autoload 'diary-iso-date "cal-iso"
327 "ISO calendar equivalent of date diary entry.")
329 (autoload 'diary-julian-date "cal-julian"
330 "Julian calendar equivalent of date diary entry.")
332 (autoload 'diary-astro-day-number "cal-julian"
333 "Astronomical (Julian) day number diary entry.")
335 (autoload 'diary-chinese-date "cal-china"
336 "Chinese calendar equivalent of date diary entry.")
338 (autoload 'diary-islamic-date "cal-islam"
339 "Islamic calendar equivalent of date diary entry.")
341 (autoload 'list-islamic-diary-entries "cal-islam"
342 "Add any Islamic date entries from the diary file to `diary-entries-list'.")
344 (autoload 'mark-islamic-diary-entries "cal-islam"
345 "Mark days in the calendar window that have Islamic date diary entries.")
347 (autoload 'mark-islamic-calendar-date-pattern "cal-islam"
348 "Mark dates in calendar window that conform to Islamic date MONTH/DAY/YEAR.")
350 (autoload 'diary-bahai-date "cal-bahai"
351 "Baha'i calendar equivalent of date diary entry.")
353 (autoload 'diary-bahai-list-entries "cal-bahai"
354 "Add any Baha'i date entries from the diary file to `diary-entries-list'.")
356 (autoload 'diary-bahai-mark-entries "cal-bahai"
357 "Mark days in the calendar window that have Baha'i date diary entries.")
359 (autoload 'calendar-bahai-mark-date-pattern "cal-bahai"
360 "Mark dates in calendar window that conform to Baha'i date MONTH/DAY/YEAR.")
362 (autoload 'diary-hebrew-date "cal-hebrew"
363 "Hebrew calendar equivalent of date diary entry.")
365 (autoload 'diary-omer "cal-hebrew"
366 "Omer count diary entry.")
368 (autoload 'diary-yahrzeit "cal-hebrew"
369 "Yahrzeit diary entry--entry applies if date is yahrzeit or the day before.")
371 (autoload 'diary-parasha "cal-hebrew"
372 "Parasha diary entry--entry applies if date is a Saturday.")
374 (autoload 'diary-rosh-hodesh "cal-hebrew"
375 "Rosh Hodesh diary entry.")
377 (autoload 'list-hebrew-diary-entries "cal-hebrew"
378 "Add any Hebrew date entries from the diary file to `diary-entries-list'.")
380 (autoload 'mark-hebrew-diary-entries "cal-hebrew"
381 "Mark days in the calendar window that have Hebrew date diary entries.")
383 (autoload 'mark-hebrew-calendar-date-pattern "cal-hebrew"
384 "Mark dates in calendar window that conform to Hebrew date MONTH/DAY/YEAR.")
386 (autoload 'diary-coptic-date "cal-coptic"
387 "Coptic calendar equivalent of date diary entry.")
389 (autoload 'diary-ethiopic-date "cal-coptic"
390 "Ethiopic calendar equivalent of date diary entry.")
392 (autoload 'diary-persian-date "cal-persia"
393 "Persian calendar equivalent of date diary entry.")
395 (autoload 'diary-phases-of-moon "lunar" "Moon phases diary entry.")
397 (autoload 'diary-sunrise-sunset "solar"
398 "Local time of sunrise and sunset as a diary entry.")
400 (autoload 'diary-sabbath-candles "solar"
401 "Local time of candle lighting diary entry--applies if date is a Friday.
402 No diary entry if there is no sunset on that date.")
404 (defvar diary-syntax-table
405 (let ((st (copy-syntax-table (standard-syntax-table))))
406 (modify-syntax-entry ?* "w" st)
407 (modify-syntax-entry ?: "w" st)
409 "The syntax table used when parsing dates in the diary file.
410 It is the standard syntax table used in Fundamental mode, but with the
411 syntax of `*' and `:' changed to be word constituents.")
413 (defvar diary-entries-list)
414 (defvar displayed-year)
415 (defvar displayed-month)
416 (defvar date)
417 (defvar number)
418 (defvar date-string)
419 (defvar original-date)
421 (defun diary-attrtype-convert (attrvalue type)
422 "Convert string ATTRVALUE to TYPE appropriate for a face description.
423 Valid TYPEs are: string, symbol, int, stringtnil, tnil."
424 (let (ret)
425 (setq ret (cond ((eq type 'string) attrvalue)
426 ((eq type 'symbol) (read attrvalue))
427 ((eq type 'int) (string-to-number attrvalue))
428 ((eq type 'stringtnil)
429 (cond ((string= "t" attrvalue) t)
430 ((string= "nil" attrvalue) nil)
431 (t attrvalue)))
432 ((eq type 'tnil)
433 (cond ((string= "t" attrvalue) t)
434 ((string= "nil" attrvalue) nil)))))
435 ; (message "(%s)[%s]=[%s]" (print type) attrvalue ret)
436 ret))
439 (defun diary-pull-attrs (entry fileglobattrs)
440 "Pull the face-related attributes off the entry, merge with the
441 fileglobattrs, and return the (possibly modified) entry and face
442 data in a list of attrname attrvalue values.
443 The entry will be modified to drop all tags that are used for face matching.
444 If entry is nil, then the fileglobattrs are being searched for,
445 the fileglobattrs variable is ignored, and
446 diary-glob-file-regexp-prefix is prepended to the regexps before each
447 search."
448 (save-excursion
449 (let (regexp regnum attrname attr-list attrname attrvalue type
450 ret-attr attr)
451 (if (null entry)
452 (progn
453 (setq ret-attr '()
454 attr-list diary-face-attrs)
455 (while attr-list
456 (goto-char (point-min))
457 (setq attr (car attr-list)
458 regexp (nth 0 attr)
459 regnum (nth 1 attr)
460 attrname (nth 2 attr)
461 type (nth 3 attr)
462 regexp (concat diary-glob-file-regexp-prefix regexp))
463 (setq attrvalue nil)
464 (if (re-search-forward regexp (point-max) t)
465 (setq attrvalue (match-string-no-properties regnum)))
466 (if (and attrvalue
467 (setq attrvalue (diary-attrtype-convert attrvalue type)))
468 (setq ret-attr (append ret-attr (list attrname attrvalue))))
469 (setq attr-list (cdr attr-list)))
470 (setq fileglobattrs ret-attr))
471 (progn
472 (setq ret-attr fileglobattrs
473 attr-list diary-face-attrs)
474 (while attr-list
475 (goto-char (point-min))
476 (setq attr (car attr-list)
477 regexp (nth 0 attr)
478 regnum (nth 1 attr)
479 attrname (nth 2 attr)
480 type (nth 3 attr))
481 (setq attrvalue nil)
482 (if (string-match regexp entry)
483 (progn
484 (setq attrvalue (match-string-no-properties regnum entry))
485 (setq entry (replace-match "" t t entry))))
486 (if (and attrvalue
487 (setq attrvalue (diary-attrtype-convert attrvalue type)))
488 (setq ret-attr (append ret-attr (list attrname attrvalue))))
489 (setq attr-list (cdr attr-list)))))
490 (list entry ret-attr))))
492 (defun diary-set-maybe-redraw (symbol value)
493 "Set SYMBOL's value to VALUE, and redraw the diary if necessary.
494 Redraws the diary if it is being displayed (note this is not the same as
495 just visiting the `diary-file'), and SYMBOL's value is to be changed."
496 (let ((oldvalue (eval symbol)))
497 (custom-set-default symbol value)
498 (and (not (equal value oldvalue))
499 (diary-live-p)
500 ;; Note this assumes diary was called without prefix arg.
501 (diary))))
503 ;; This can be removed once the kill/yank treatment of invisible text
504 ;; (see etc/TODO) is fixed. -- gm
505 (defcustom diary-header-line-flag t
506 "If non-nil, `simple-diary-display' will show a header line.
507 The format of the header is specified by `diary-header-line-format'."
508 :group 'diary
509 :type 'boolean
510 :initialize 'custom-initialize-default
511 ;; FIXME overkill.
512 :set 'diary-set-maybe-redraw
513 :version "22.1")
515 (defvar diary-selective-display nil)
517 (defcustom diary-header-line-format
518 '(:eval (calendar-string-spread
519 (list (if diary-selective-display
520 "Selective display active - press \"s\" in calendar \
521 before edit/copy"
522 "Diary"))
523 ?\s (frame-width)))
524 "Format of the header line displayed by `simple-diary-display'.
525 Only used if `diary-header-line-flag' is non-nil."
526 :group 'diary
527 :type 'sexp
528 :initialize 'custom-initialize-default
529 ;; FIXME overkill.
530 :set 'diary-set-maybe-redraw
531 :version "22.1")
533 (defvar diary-saved-point) ; internal
535 ;; The first version of this also checked for diary-selective-display
536 ;; in the non-fancy case. This was an attempt to distinguish between
537 ;; displaying the diary and just visiting the diary file. However,
538 ;; when using fancy diary, calling diary when there are no entries to
539 ;; display does not create the fancy buffer, nor does it switch on
540 ;; selective-display in the diary buffer. This means some
541 ;; customizations will not take effect, eg:
542 ;; http://lists.gnu.org/archive/html/emacs-pretest-bug/2007-03/msg00466.html
543 ;; So the check for selective-display was dropped. This means the
544 ;; diary will be displayed if one customizes a diary variable while
545 ;; just visiting the diary-file. This is i) unlikely, and ii) no great loss.
546 (defun diary-live-p ()
547 "Return non-nil if the diary is being displayed."
548 (or (get-buffer fancy-diary-buffer)
549 (and diary-file
550 (find-buffer-visiting (substitute-in-file-name diary-file)))))
552 (defcustom number-of-diary-entries 1
553 "Specifies how many days of diary entries are to be displayed initially.
554 This variable affects the diary display when the command \\[diary] is used,
555 or if the value of the variable `view-diary-entries-initially' is t. For
556 example, if the default value 1 is used, then only the current day's diary
557 entries will be displayed. If the value 2 is used, then both the current
558 day's and the next day's entries will be displayed.
560 The value can also be a vector such as [0 2 2 2 2 4 1]; this value
561 says to display no diary entries on Sunday, the entries for
562 the current date and the day after on Monday through Thursday,
563 Friday through Monday's entries on Friday, and only Saturday's
564 entries on Saturday.
566 This variable does not affect the diary display with the `d' command
567 from the calendar; in that case, the prefix argument controls the
568 number of days of diary entries displayed."
569 :type '(choice (integer :tag "Entries")
570 (vector :value [0 0 0 0 0 0 0]
571 (integer :tag "Sunday")
572 (integer :tag "Monday")
573 (integer :tag "Tuesday")
574 (integer :tag "Wednesday")
575 (integer :tag "Thursday")
576 (integer :tag "Friday")
577 (integer :tag "Saturday")))
578 :initialize 'custom-initialize-default
579 :set 'diary-set-maybe-redraw
580 :group 'diary)
583 (defvar diary-modify-entry-list-string-function nil
584 "Function applied to entry string before putting it into the entries list.
585 Can be used by programs integrating a diary list into other buffers (e.g.
586 org.el and planner.el) to modify the string or add properties to it.
587 The function takes a string argument and must return a string.")
589 (defun add-to-diary-list (date string specifier &optional marker
590 globcolor literal)
591 "Add an entry to `diary-entries-list'.
592 Do nothing if DATE or STRING is nil. DATE is the (MONTH DAY
593 YEAR) for which the entry applies; STRING is the text of the
594 entry as it will appear in the diary (i.e. with any format
595 strings such as \"%d\" expanded); SPECIFIER is the date part of
596 the entry as it appears in the diary-file; LITERAL is the entry
597 as it appears in the diary-file (i.e. before expansion). If
598 LITERAL is nil, it is taken to be the same as STRING.
600 The entry is added to the list as (DATE STRING SPECIFIER LOCATOR
601 GLOBCOLOR), where LOCATOR has the form (MARKER FILENAME LITERAL),
602 FILENAME being the file containing the diary entry."
603 (when (and date string)
604 (if diary-file-name-prefix
605 (let ((prefix (funcall diary-file-name-prefix-function
606 (buffer-file-name))))
607 (or (string= prefix "")
608 (setq string (format "[%s] %s" prefix string)))))
609 (and diary-modify-entry-list-string-function
610 (setq string (funcall diary-modify-entry-list-string-function
611 string)))
612 (setq diary-entries-list
613 (append diary-entries-list
614 (list (list date string specifier
615 (list marker (buffer-file-name) literal)
616 globcolor))))))
618 (define-obsolete-function-alias 'list-diary-entries 'diary-list-entries)
619 (defun diary-list-entries (date number &optional list-only)
620 "Create and display a buffer containing the relevant lines in `diary-file'.
621 The arguments are DATE and NUMBER; the entries selected are those
622 for NUMBER days starting with date DATE. The other entries are hidden
623 using selective display. If NUMBER is less than 1, this function does nothing.
625 Returns a list of all relevant diary entries found, if any, in order by date.
626 The list entries have the form ((MONTH DAY YEAR) STRING SPECIFIER) where
627 \(MONTH DAY YEAR) is the date of the entry, STRING is the entry text, and
628 SPECIFIER is the applicability. If the variable `diary-list-include-blanks'
629 is t, this list includes a dummy diary entry consisting of the empty string
630 for a date with no diary entries.
632 After the list is prepared, the hooks `nongregorian-diary-listing-hook',
633 `list-diary-entries-hook', `diary-display-hook', and `diary-hook' are run.
634 These hooks have the following distinct roles:
636 `nongregorian-diary-listing-hook' can cull dates from the diary
637 and each included file. Usually used for Hebrew or Islamic
638 diary entries in files. Applied to *each* file.
640 `list-diary-entries-hook' adds or manipulates diary entries from
641 external sources. Used, for example, to include diary entries
642 from other files or to sort the diary entries. Invoked *once* only,
643 before the display hook is run.
645 `diary-display-hook' does the actual display of information. If this is
646 nil, simple-diary-display will be used. Use add-hook to set this to
647 fancy-diary-display, if desired. If you want no diary display, use
648 add-hook to set this to ignore.
650 `diary-hook' is run last. This can be used for an appointment
651 notification function.
653 If LIST-ONLY is non-nil don't modify or display the buffer, only return a list."
654 (unless number
655 (setq number (if (vectorp number-of-diary-entries)
656 (aref number-of-diary-entries (calendar-day-of-week date))
657 number-of-diary-entries)))
658 (when (> number 0)
659 (let ((original-date date);; save for possible use in the hooks
660 diary-entries-list
661 file-glob-attrs
662 (date-string (calendar-date-string date))
663 (d-file (substitute-in-file-name diary-file)))
664 (message "Preparing diary...")
665 (save-excursion
666 (let ((diary-buffer (find-buffer-visiting d-file)))
667 (if (not diary-buffer)
668 (set-buffer (find-file-noselect d-file t))
669 (set-buffer diary-buffer)
670 (or (verify-visited-file-modtime diary-buffer)
671 (revert-buffer t t))))
672 ;; Setup things like the header-line-format and invisibility-spec.
673 (if (eq major-mode default-major-mode)
674 (diary-mode)
675 ;; This kludge is to make customizations to
676 ;; diary-header-line-flag after diary has been displayed
677 ;; take effect. Unconditionally calling (diary-mode)
678 ;; clobbers file local variables.
679 ;; http://lists.gnu.org/archive/html/emacs-pretest-bug/2007-03/msg00363.html
680 ;; http://lists.gnu.org/archive/html/emacs-pretest-bug/2007-04/msg00404.html
681 (if (eq major-mode 'diary-mode)
682 (setq header-line-format (and diary-header-line-flag
683 diary-header-line-format))))
684 ;; d-s-p is passed to the diary display function.
685 (let ((diary-saved-point (point)))
686 (save-excursion
687 (setq file-glob-attrs (nth 1 (diary-pull-attrs nil "")))
688 (with-syntax-table diary-syntax-table
689 (let ((mark (regexp-quote diary-nonmarking-symbol)))
690 (goto-char (point-min))
691 (unless list-only
692 (let ((ol (make-overlay (point-min) (point-max) nil t nil)))
693 (set (make-local-variable 'diary-selective-display) t)
694 (overlay-put ol 'invisible 'diary)
695 (overlay-put ol 'evaporate t)))
696 (dotimes (idummy number)
697 (let ((month (extract-calendar-month date))
698 (day (extract-calendar-day date))
699 (year (extract-calendar-year date))
700 (entry-found (list-sexp-diary-entries date)))
701 (dolist (date-form diary-date-forms)
702 (let*
703 ((backup (when (eq (car date-form) 'backup)
704 (setq date-form (cdr date-form))
706 (dayname
707 (format "%s\\|%s\\.?"
708 (calendar-day-name date)
709 (calendar-day-name date 'abbrev)))
710 (monthname
711 (format "\\*\\|%s\\|%s\\.?"
712 (calendar-month-name month)
713 (calendar-month-name month 'abbrev)))
714 (month (concat "\\*\\|0*" (int-to-string month)))
715 (day (concat "\\*\\|0*" (int-to-string day)))
716 (year
717 (concat
718 "\\*\\|0*" (int-to-string year)
719 (if abbreviated-calendar-year
720 (concat "\\|" (format "%02d" (% year 100)))
721 "")))
722 (regexp
723 (concat
724 "^" mark "?\\("
725 (mapconcat 'eval date-form "\\)\\(?:")
726 "\\)"))
727 (case-fold-search t))
728 (goto-char (point-min))
729 (while (re-search-forward regexp nil t)
730 (if backup (re-search-backward "\\<" nil t))
731 (if (and (bolp) (not (looking-at "[ \t]")))
732 ;; Diary entry that consists only of date.
733 (backward-char 1)
734 ;; Found a nonempty diary entry--make it
735 ;; visible and add it to the list.
736 (setq entry-found t)
737 (if (looking-at "[ \t]*\n[ \t]") (forward-line 1))
738 (let ((entry-start (point))
739 date-start temp)
740 (setq date-start
741 (line-end-position
742 (if (and (bolp) (> number 1)) -1 0)))
743 (forward-line 1)
744 (while (looking-at "[ \t]")
745 (forward-line 1))
746 (unless (and (eobp) (not (bolp)))
747 (backward-char 1))
748 (unless list-only
749 (remove-overlays date-start (point)
750 'invisible 'diary))
751 ; FIXME free variable entry?
752 (setq entry (buffer-substring entry-start (point))
753 temp (diary-pull-attrs entry file-glob-attrs)
754 entry (nth 0 temp))
755 (add-to-diary-list
756 date
757 entry
758 (buffer-substring
759 (1+ date-start) (1- entry-start))
760 (copy-marker entry-start) (nth 1 temp)))))))
761 (or entry-found
762 (not diary-list-include-blanks)
763 (add-to-diary-list date "" "" "" ""))
764 (setq date
765 (calendar-gregorian-from-absolute
766 (1+ (calendar-absolute-from-gregorian date))))
767 (setq entry-found nil)))))
768 (goto-char (point-min))
769 (run-hooks 'nongregorian-diary-listing-hook
770 'list-diary-entries-hook)
771 (unless list-only
772 (if diary-display-hook
773 (run-hooks 'diary-display-hook)
774 (simple-diary-display)))
775 (run-hooks 'diary-hook)
776 diary-entries-list))))))
778 (defun diary-unhide-everything ()
779 (kill-local-variable 'diary-selective-display)
780 (remove-overlays (point-min) (point-max) 'invisible 'diary)
781 (kill-local-variable 'mode-line-format))
783 (defun include-other-diary-files ()
784 "Include the diary entries from other diary files with those of diary-file.
785 This function is suitable for use in `list-diary-entries-hook';
786 it enables you to use shared diary files together with your own.
787 The files included are specified in the diaryfile by lines of this form:
788 #include \"filename\"
789 This is recursive; that is, #include directives in diary files thus included
790 are obeyed. You can change the `#include' to some other string by
791 changing the variable `diary-include-string'."
792 (goto-char (point-min))
793 (while (re-search-forward
794 (concat
796 (regexp-quote diary-include-string)
797 " \"\\([^\"]*\\)\"")
798 nil t)
799 (let* ((diary-file (substitute-in-file-name
800 (match-string-no-properties 1)))
801 (diary-list-include-blanks nil)
802 (list-diary-entries-hook 'include-other-diary-files)
803 (diary-display-hook 'ignore)
804 (diary-hook nil))
805 (if (file-exists-p diary-file)
806 (if (file-readable-p diary-file)
807 (unwind-protect
808 (setq diary-entries-list
809 (append diary-entries-list
810 (diary-list-entries original-date number)))
811 (with-current-buffer (find-buffer-visiting diary-file)
812 (diary-unhide-everything)))
813 (beep)
814 (message "Can't read included diary file %s" diary-file)
815 (sleep-for 2))
816 (beep)
817 (message "Can't find included diary file %s" diary-file)
818 (sleep-for 2))))
819 (goto-char (point-min)))
821 (defun simple-diary-display ()
822 "Display the diary buffer if there are any relevant entries or holidays."
823 (let* ((holiday-list (if holidays-in-diary-buffer
824 (calendar-check-holidays original-date)))
825 (hol-string (format "%s%s%s"
826 date-string
827 (if holiday-list ": " "")
828 (mapconcat 'identity holiday-list "; ")))
829 (msg (format "No diary entries for %s" hol-string))
830 ;; If selected window is dedicated (to the calendar),
831 ;; need a new one to display the diary.
832 (pop-up-frames (or pop-up-frames
833 (window-dedicated-p (selected-window)))))
834 (calendar-set-mode-line (format "Diary for %s" hol-string))
835 (if (or (not diary-entries-list)
836 (and (not (cdr diary-entries-list))
837 (string-equal (car (cdr (car diary-entries-list))) "")))
838 (if (< (length msg) (frame-width))
839 (message "%s" msg)
840 (set-buffer (get-buffer-create holiday-buffer))
841 (setq buffer-read-only nil)
842 (calendar-set-mode-line date-string)
843 (erase-buffer)
844 (insert (mapconcat 'identity holiday-list "\n"))
845 (goto-char (point-min))
846 (set-buffer-modified-p nil)
847 (setq buffer-read-only t)
848 (display-buffer holiday-buffer)
849 (message "No diary entries for %s" date-string))
850 (with-current-buffer
851 (find-buffer-visiting (substitute-in-file-name diary-file))
852 (let ((window (display-buffer (current-buffer))))
853 ;; d-s-p is passed from list-diary-entries.
854 (set-window-point window diary-saved-point)
855 (set-window-start window (point-min))))
856 (message "Preparing diary...done"))))
858 (defface diary-button '((((type pc) (class color))
859 (:foreground "lightblue")))
860 "Default face used for buttons."
861 :version "22.1"
862 :group 'diary)
863 ;; backward-compatibility alias
864 (put 'diary-button-face 'face-alias 'diary-button)
866 (define-button-type 'diary-entry
867 'action #'diary-goto-entry
868 'face 'diary-button)
870 (defun diary-goto-entry (button)
871 (let* ((locator (button-get button 'locator))
872 (marker (car locator))
873 markbuf file)
874 ;; If marker pointing to diary location is valid, use that.
875 (if (and marker (setq markbuf (marker-buffer marker)))
876 (progn
877 (pop-to-buffer markbuf)
878 (goto-char (marker-position marker)))
879 ;; Marker is invalid (eg buffer has been killed).
880 (or (and (setq file (cadr locator))
881 (file-exists-p file)
882 (find-file-other-window file)
883 (progn
884 (when (eq major-mode default-major-mode) (diary-mode))
885 (goto-char (point-min))
886 (if (re-search-forward (format "%s.*\\(%s\\)"
887 (regexp-quote (nth 2 locator))
888 (regexp-quote (nth 3 locator)))
889 nil t)
890 (goto-char (match-beginning 1)))))
891 (message "Unable to locate this diary entry")))))
893 (defun fancy-diary-display ()
894 "Prepare a diary buffer with relevant entries in a fancy, noneditable form.
895 This function is provided for optional use as the `diary-display-hook'."
896 (with-current-buffer ;; Turn off selective-display in the diary file's buffer.
897 (find-buffer-visiting (substitute-in-file-name diary-file))
898 (diary-unhide-everything))
899 (if (or (not diary-entries-list)
900 (and (not (cdr diary-entries-list))
901 (string-equal (car (cdr (car diary-entries-list))) "")))
902 (let* ((holiday-list (if holidays-in-diary-buffer
903 (calendar-check-holidays original-date)))
904 (msg (format "No diary entries for %s %s"
905 (concat date-string (if holiday-list ":" ""))
906 (mapconcat 'identity holiday-list "; "))))
907 (if (<= (length msg) (frame-width))
908 (message "%s" msg)
909 (set-buffer (get-buffer-create holiday-buffer))
910 (setq buffer-read-only nil)
911 (erase-buffer)
912 (insert (mapconcat 'identity holiday-list "\n"))
913 (goto-char (point-min))
914 (set-buffer-modified-p nil)
915 (setq buffer-read-only t)
916 (display-buffer holiday-buffer)
917 (message "No diary entries for %s" date-string)))
918 (with-current-buffer;; Prepare the fancy diary buffer.
919 (make-fancy-diary-buffer)
920 (setq buffer-read-only nil)
921 (let ((entry-list diary-entries-list)
922 (holiday-list)
923 (holiday-list-last-month 1)
924 (holiday-list-last-year 1)
925 (date (list 0 0 0)))
926 (while entry-list
927 (if (not (calendar-date-equal date (car (car entry-list))))
928 (progn
929 (setq date (car (car entry-list)))
930 (and holidays-in-diary-buffer
931 (calendar-date-compare
932 (list (list holiday-list-last-month
933 (calendar-last-day-of-month
934 holiday-list-last-month
935 holiday-list-last-year)
936 holiday-list-last-year))
937 (list date))
938 ;; We need to get the holidays for the next 3 months.
939 (setq holiday-list-last-month
940 (extract-calendar-month date))
941 (setq holiday-list-last-year
942 (extract-calendar-year date))
943 (progn
944 (increment-calendar-month
945 holiday-list-last-month holiday-list-last-year 1)
947 (setq holiday-list
948 (let ((displayed-month holiday-list-last-month)
949 (displayed-year holiday-list-last-year))
950 (calendar-holiday-list)))
951 (increment-calendar-month
952 holiday-list-last-month holiday-list-last-year 1))
953 (let* ((date-string (calendar-date-string date))
954 (date-holiday-list
955 (let ((h holiday-list)
956 (d))
957 ;; Make a list of all holidays for date.
958 (while h
959 (if (calendar-date-equal date (car (car h)))
960 (setq d (append d (cdr (car h)))))
961 (setq h (cdr h)))
962 d)))
963 (insert (if (= (point) (point-min)) "" ?\n) date-string)
964 (if date-holiday-list (insert ": "))
965 (let* ((l (current-column))
966 (longest 0))
967 (insert (mapconcat (lambda (x)
968 (if (< longest (length x))
969 (setq longest (length x)))
971 date-holiday-list
972 (concat "\n" (make-string l ? ))))
973 (insert ?\n (make-string (+ l longest) ?=) ?\n)))))
975 ;; FIXME free variable entry?
976 (setq entry (car (cdr (car entry-list))))
977 (if (< 0 (length entry))
978 (let ((this-entry (car entry-list))
979 this-loc)
980 (if (setq this-loc (nth 3 this-entry))
981 (insert-button (concat entry "\n")
982 ;; (MARKER FILENAME SPECIFIER LITERAL)
983 'locator (list (car this-loc)
984 (cadr this-loc)
985 (nth 2 this-entry)
986 (or (nth 2 this-loc)
987 (nth 1 this-entry)))
988 :type 'diary-entry)
989 ; FIXME free variable entry?
990 (insert entry ?\n))
991 (save-excursion
992 (let* ((marks (nth 4 this-entry))
993 (faceinfo marks)
994 temp-face)
995 (when marks
996 (setq temp-face (make-symbol
997 (apply
998 'concat "temp-face-"
999 (mapcar (lambda (sym)
1000 (if (stringp sym)
1002 (symbol-name sym)))
1003 marks))))
1004 (make-face temp-face)
1005 ;; Remove :face info from the marks,
1006 ;; copy the face info into temp-face
1007 (while (setq faceinfo (memq :face faceinfo))
1008 (copy-face (read (nth 1 faceinfo)) temp-face)
1009 (setcar faceinfo nil)
1010 (setcar (cdr faceinfo) nil))
1011 (setq marks (delq nil marks))
1012 ;; Apply the font aspects.
1013 (apply 'set-face-attribute temp-face nil marks)
1014 (search-backward entry)
1015 (overlay-put
1016 (make-overlay (match-beginning 0) (match-end 0))
1017 'face temp-face))))))
1018 (setq entry-list (cdr entry-list))))
1019 (set-buffer-modified-p nil)
1020 (goto-char (point-min))
1021 (setq buffer-read-only t)
1022 (display-buffer fancy-diary-buffer)
1023 (fancy-diary-display-mode)
1024 (calendar-set-mode-line date-string)
1025 (message "Preparing diary...done"))))
1027 (defun make-fancy-diary-buffer ()
1028 "Create and return the initial fancy diary buffer."
1029 (with-current-buffer (get-buffer-create fancy-diary-buffer)
1030 (setq buffer-read-only nil)
1031 (calendar-set-mode-line "Diary Entries")
1032 (erase-buffer)
1033 (set-buffer-modified-p nil)
1034 (setq buffer-read-only t)
1035 (get-buffer fancy-diary-buffer)))
1037 (defun print-diary-entries ()
1038 "Print a hard copy of the diary display.
1040 If the simple diary display is being used, prepare a temp buffer with the
1041 visible lines of the diary buffer, add a heading line composed from the mode
1042 line, print the temp buffer, and destroy it.
1044 If the fancy diary display is being used, just print the buffer.
1046 The hooks given by the variable `print-diary-entries-hook' are called to do
1047 the actual printing."
1048 (interactive)
1049 (if (bufferp (get-buffer fancy-diary-buffer))
1050 (with-current-buffer (get-buffer fancy-diary-buffer)
1051 (run-hooks 'print-diary-entries-hook))
1052 (let ((diary-buffer
1053 (find-buffer-visiting (substitute-in-file-name diary-file))))
1054 (if diary-buffer
1055 (let ((temp-buffer (get-buffer-create " *Printable Diary Entries*"))
1056 (heading))
1057 (with-current-buffer diary-buffer
1058 (setq heading
1059 (if (not (stringp mode-line-format))
1060 "All Diary Entries"
1061 (string-match "^-*\\([^-].*[^-]\\)-*$" mode-line-format)
1062 (match-string 1 mode-line-format)))
1063 (let ((start (point-min))
1064 end)
1065 (while
1066 (progn
1067 (setq end (next-single-char-property-change
1068 start 'invisible))
1069 (if (get-char-property start 'invisible)
1071 (with-current-buffer temp-buffer
1072 (insert-buffer-substring diary-buffer
1073 start (or end (point-max)))))
1074 (setq start end)
1075 (and end (< end (point-max))))))
1076 (set-buffer temp-buffer)
1077 (goto-char (point-min))
1078 (insert heading "\n"
1079 (make-string (length heading) ?=) "\n")
1080 (run-hooks 'print-diary-entries-hook)
1081 (kill-buffer temp-buffer)))
1082 (error "You don't have a diary buffer!")))))
1084 (define-obsolete-function-alias 'show-all-diary-entries 'diary-show-all-entries)
1085 (defun diary-show-all-entries ()
1086 "Show all of the diary entries in the diary file.
1087 This function gets rid of the selective display of the diary file so that
1088 all entries, not just some, are visible. If there is no diary buffer, one
1089 is created."
1090 (interactive)
1091 (let ((d-file (diary-check-diary-file))
1092 (pop-up-frames (window-dedicated-p (selected-window))))
1093 (with-current-buffer (or (find-buffer-visiting d-file)
1094 (find-file-noselect d-file t))
1095 (when (eq major-mode default-major-mode) (diary-mode))
1096 (diary-unhide-everything)
1097 (display-buffer (current-buffer)))))
1099 ;;;###autoload
1100 (defun diary-mail-entries (&optional ndays)
1101 "Send a mail message showing diary entries for next NDAYS days.
1102 If no prefix argument is given, NDAYS is set to `diary-mail-days'.
1103 Mail is sent to the address specified by `diary-mail-addr'.
1105 You can call `diary-mail-entries' every night using an at/cron job.
1106 For example, this script will run the program at 2am daily. Since
1107 `emacs -batch' does not load your `.emacs' file, you must ensure that
1108 all relevant variables are set, as done here.
1110 #!/bin/sh
1111 # diary-rem.sh -- repeatedly run the Emacs diary-reminder
1112 emacs -batch \\
1113 -eval \"(setq diary-mail-days 3 \\
1114 diary-file \\\"/path/to/diary.file\\\" \\
1115 european-calendar-style t \\
1116 diary-mail-addr \\\"user@host.name\\\" )\" \\
1117 -l diary-lib -f diary-mail-entries
1118 at -f diary-rem.sh 0200 tomorrow
1120 You may have to tweak the syntax of the `at' command to suit your
1121 system. Alternatively, you can specify a cron entry:
1122 0 1 * * * diary-rem.sh
1123 to run it every morning at 1am."
1124 (interactive "P")
1125 (if (string-equal diary-mail-addr "")
1126 (error "You must set `diary-mail-addr' to use this command")
1127 (let ((diary-display-hook 'fancy-diary-display))
1128 (diary-list-entries (calendar-current-date) (or ndays diary-mail-days)))
1129 (compose-mail diary-mail-addr
1130 (concat "Diary entries generated "
1131 (calendar-date-string (calendar-current-date))))
1132 (insert
1133 (if (get-buffer fancy-diary-buffer)
1134 (with-current-buffer fancy-diary-buffer (buffer-string))
1135 "No entries found"))
1136 (call-interactively (get mail-user-agent 'sendfunc))))
1138 (defun diary-name-pattern (string-array &optional abbrev-array paren)
1139 "Return a regexp matching the strings in the array STRING-ARRAY.
1140 If the optional argument ABBREV-ARRAY is present, then the function
1141 `calendar-abbrev-construct' is used to construct abbreviations from the
1142 two supplied arrays. The returned regexp will then also match these
1143 abbreviations, with or without final `.' characters. If the optional
1144 argument PAREN is non-nil, the regexp is surrounded by parentheses."
1145 (regexp-opt (append string-array
1146 (if abbrev-array
1147 (calendar-abbrev-construct abbrev-array
1148 string-array))
1149 (if abbrev-array
1150 (calendar-abbrev-construct abbrev-array
1151 string-array
1152 'period))
1153 nil)
1154 paren))
1156 (defvar marking-diary-entries nil
1157 "True during the marking of diary entries, nil otherwise.")
1159 (defvar marking-diary-entry nil
1160 "True during the marking of diary entries, if current entry is marking.")
1162 (defun mark-diary-entries (&optional redraw)
1163 "Mark days in the calendar window that have diary entries.
1164 Each entry in the diary file visible in the calendar window is
1165 marked. After the entries are marked, the hooks
1166 `nongregorian-diary-marking-hook' and `mark-diary-entries-hook'
1167 are run. If the optional argument REDRAW is non-nil (which is
1168 the case interactively, for example) then any existing diary
1169 marks are first removed. This is intended to deal with deleted
1170 diary entries."
1171 (interactive "p")
1172 ;; To remove any deleted diary entries. Do not redraw when:
1173 ;; i) processing #include diary files (else only get the marks from
1174 ;; the last #include file processed).
1175 ;; ii) called via calendar-redraw (since calendar has already been
1176 ;; erased).
1177 ;; Use of REDRAW handles both of these cases.
1178 (when (and redraw mark-diary-entries-in-calendar)
1179 (setq mark-diary-entries-in-calendar nil)
1180 (redraw-calendar))
1181 (let ((marking-diary-entries t)
1182 file-glob-attrs marks)
1183 (with-current-buffer (find-file-noselect (diary-check-diary-file) t)
1184 (save-excursion
1185 (when (eq major-mode default-major-mode) (diary-mode))
1186 (setq mark-diary-entries-in-calendar t)
1187 (message "Marking diary entries...")
1188 (setq file-glob-attrs (nth 1 (diary-pull-attrs nil '())))
1189 (with-syntax-table diary-syntax-table
1190 (dolist (date-form diary-date-forms)
1191 (if (eq (car date-form) 'backup)
1192 (setq date-form (cdr date-form))) ;; ignore 'backup directive
1193 (let* ((dayname
1194 (diary-name-pattern calendar-day-name-array
1195 calendar-day-abbrev-array))
1196 (monthname
1197 (format "%s\\|\\*"
1198 (diary-name-pattern calendar-month-name-array
1199 calendar-month-abbrev-array)))
1200 (month "[0-9]+\\|\\*")
1201 (day "[0-9]+\\|\\*")
1202 (year "[0-9]+\\|\\*")
1203 (l (length date-form))
1204 (d-name-pos (- l (length (memq 'dayname date-form))))
1205 (d-name-pos (if (/= l d-name-pos) (+ 1 d-name-pos)))
1206 (m-name-pos (- l (length (memq 'monthname date-form))))
1207 (m-name-pos (if (/= l m-name-pos) (+ 1 m-name-pos)))
1208 (d-pos (- l (length (memq 'day date-form))))
1209 (d-pos (if (/= l d-pos) (+ 1 d-pos)))
1210 (m-pos (- l (length (memq 'month date-form))))
1211 (m-pos (if (/= l m-pos) (+ 1 m-pos)))
1212 (y-pos (- l (length (memq 'year date-form))))
1213 (y-pos (if (/= l y-pos) (+ 1 y-pos)))
1214 (regexp
1215 (concat
1216 "^\\("
1217 (mapconcat 'eval date-form "\\)\\(")
1218 "\\)"))
1219 (case-fold-search t))
1220 (goto-char (point-min))
1221 (while (re-search-forward regexp nil t)
1222 (let* ((dd-name
1223 (if d-name-pos
1224 (match-string-no-properties d-name-pos)))
1225 (mm-name
1226 (if m-name-pos
1227 (match-string-no-properties m-name-pos)))
1228 (mm (string-to-number
1229 (if m-pos
1230 (match-string-no-properties m-pos)
1231 "")))
1232 (dd (string-to-number
1233 (if d-pos
1234 (match-string-no-properties d-pos)
1235 "")))
1236 (y-str (if y-pos
1237 (match-string-no-properties y-pos)))
1238 (yy (if (not y-str)
1240 (if (and (= (length y-str) 2)
1241 abbreviated-calendar-year)
1242 (let* ((current-y
1243 (extract-calendar-year
1244 (calendar-current-date)))
1245 (y (+ (string-to-number y-str)
1246 (* 100
1247 (/ current-y 100)))))
1248 (if (> (- y current-y) 50)
1249 (- y 100)
1250 (if (> (- current-y y) 50)
1251 (+ y 100)
1252 y)))
1253 (string-to-number y-str)))))
1254 (let ((tmp (diary-pull-attrs (buffer-substring-no-properties
1255 (point) (line-end-position))
1256 file-glob-attrs)))
1257 ;; FIXME free variable entry.
1258 (setq entry (nth 0 tmp)
1259 marks (nth 1 tmp)))
1260 (if dd-name
1261 (mark-calendar-days-named
1262 (cdr (assoc-string
1263 dd-name
1264 (calendar-make-alist
1265 calendar-day-name-array
1266 0 nil calendar-day-abbrev-array) t)) marks)
1267 (if mm-name
1268 (setq mm
1269 (if (string-equal mm-name "*") 0
1270 (cdr (assoc-string
1271 mm-name
1272 (calendar-make-alist
1273 calendar-month-name-array
1274 1 nil calendar-month-abbrev-array) t)))))
1275 (mark-calendar-date-pattern mm dd yy marks))))))
1276 (mark-sexp-diary-entries)
1277 (run-hooks 'nongregorian-diary-marking-hook
1278 'mark-diary-entries-hook))
1279 (message "Marking diary entries...done")))))
1281 (defun mark-sexp-diary-entries ()
1282 "Mark days in the calendar window that have sexp diary entries.
1283 Each entry in the diary file (or included files) visible in the calendar window
1284 is marked. See the documentation for the function `list-sexp-diary-entries'."
1285 (let* ((sexp-mark (regexp-quote sexp-diary-entry-symbol))
1286 (s-entry (concat "^\\("
1287 sexp-mark "(\\)\\|\\("
1288 (regexp-quote diary-nonmarking-symbol)
1289 sexp-mark "(diary-remind\\)"))
1290 (file-glob-attrs (nth 1 (diary-pull-attrs nil '())))
1291 m y first-date last-date mark file-glob-attrs)
1292 (with-current-buffer calendar-buffer
1293 (setq m displayed-month)
1294 (setq y displayed-year))
1295 (increment-calendar-month m y -1)
1296 (setq first-date
1297 (calendar-absolute-from-gregorian (list m 1 y)))
1298 (increment-calendar-month m y 2)
1299 (setq last-date
1300 (calendar-absolute-from-gregorian
1301 (list m (calendar-last-day-of-month m y) y)))
1302 (goto-char (point-min))
1303 (while (re-search-forward s-entry nil t)
1304 (setq marking-diary-entry (char-equal (preceding-char) ?\())
1305 (re-search-backward "(")
1306 (let ((sexp-start (point))
1307 sexp entry entry-start marks)
1308 (forward-sexp)
1309 (setq sexp (buffer-substring-no-properties sexp-start (point)))
1310 (forward-char 1)
1311 (if (and (bolp) (not (looking-at "[ \t]")))
1312 (progn;; Diary entry consists only of the sexp
1313 (backward-char 1)
1314 (setq entry ""))
1315 (setq entry-start (point))
1316 ;; Find end of entry
1317 (forward-line 1)
1318 (while (looking-at "[ \t]")
1319 (forward-line 1))
1320 (if (bolp) (backward-char 1))
1321 (setq entry (buffer-substring-no-properties entry-start (point))))
1322 (calendar-for-loop date from first-date to last-date do
1323 (if (setq mark (diary-sexp-entry sexp entry
1324 (calendar-gregorian-from-absolute date)))
1325 (progn
1326 (setq marks (diary-pull-attrs entry file-glob-attrs)
1327 marks (nth 1 (diary-pull-attrs entry file-glob-attrs)))
1328 (mark-visible-calendar-date
1329 (calendar-gregorian-from-absolute date)
1330 (if (< 0 (length marks))
1331 marks
1332 (if (consp mark)
1333 (car mark)))))))))))
1335 (defun mark-included-diary-files ()
1336 "Mark the diary entries from other diary files with those of the diary file.
1337 This function is suitable for use as the `mark-diary-entries-hook'; it enables
1338 you to use shared diary files together with your own. The files included are
1339 specified in the diary-file by lines of this form:
1340 #include \"filename\"
1341 This is recursive; that is, #include directives in diary files thus included
1342 are obeyed. You can change the `#include' to some other string by
1343 changing the variable `diary-include-string'."
1344 (goto-char (point-min))
1345 (while (re-search-forward
1346 (concat
1348 (regexp-quote diary-include-string)
1349 " \"\\([^\"]*\\)\"")
1350 nil t)
1351 (let* ((diary-file (substitute-in-file-name
1352 (match-string-no-properties 1)))
1353 (mark-diary-entries-hook 'mark-included-diary-files)
1354 (dbuff (find-buffer-visiting diary-file)))
1355 (if (file-exists-p diary-file)
1356 (if (file-readable-p diary-file)
1357 (progn
1358 (mark-diary-entries)
1359 (unless dbuff
1360 (kill-buffer (find-buffer-visiting diary-file))))
1361 (beep)
1362 (message "Can't read included diary file %s" diary-file)
1363 (sleep-for 2))
1364 (beep)
1365 (message "Can't find included diary file %s" diary-file)
1366 (sleep-for 2))))
1367 (goto-char (point-min)))
1369 (defun mark-calendar-days-named (dayname &optional color)
1370 "Mark all dates in the calendar window that are day DAYNAME of the week.
1371 0 means all Sundays, 1 means all Mondays, and so on."
1372 (with-current-buffer calendar-buffer
1373 (let ((prev-month displayed-month)
1374 (prev-year displayed-year)
1375 (succ-month displayed-month)
1376 (succ-year displayed-year)
1377 (last-day)
1378 (day))
1379 (increment-calendar-month succ-month succ-year 1)
1380 (increment-calendar-month prev-month prev-year -1)
1381 (setq day (calendar-absolute-from-gregorian
1382 (calendar-nth-named-day 1 dayname prev-month prev-year)))
1383 (setq last-day (calendar-absolute-from-gregorian
1384 (calendar-nth-named-day -1 dayname succ-month succ-year)))
1385 (while (<= day last-day)
1386 (mark-visible-calendar-date (calendar-gregorian-from-absolute day) color)
1387 (setq day (+ day 7))))))
1389 (defun mark-calendar-date-pattern (month day year &optional color)
1390 "Mark all dates in the calendar window that conform to MONTH/DAY/YEAR.
1391 A value of 0 in any position is a wildcard."
1392 (with-current-buffer calendar-buffer
1393 (let ((m displayed-month)
1394 (y displayed-year))
1395 (increment-calendar-month m y -1)
1396 (dotimes (idummy 3)
1397 (mark-calendar-month m y month day year color)
1398 (increment-calendar-month m y 1)))))
1400 (defun mark-calendar-month (month year p-month p-day p-year &optional color)
1401 "Mark dates in the MONTH/YEAR that conform to pattern P-MONTH/P_DAY/P-YEAR.
1402 A value of 0 in any position of the pattern is a wildcard."
1403 (if (or (and (= month p-month)
1404 (or (= p-year 0) (= year p-year)))
1405 (and (= p-month 0)
1406 (or (= p-year 0) (= year p-year))))
1407 (if (= p-day 0)
1408 (calendar-for-loop
1409 i from 1 to (calendar-last-day-of-month month year) do
1410 (mark-visible-calendar-date (list month i year) color))
1411 (mark-visible-calendar-date (list month p-day year) color))))
1413 (defun sort-diary-entries ()
1414 "Sort the list of diary entries by time of day."
1415 (setq diary-entries-list (sort diary-entries-list 'diary-entry-compare)))
1417 (defun diary-entry-compare (e1 e2)
1418 "Returns t if E1 is earlier than E2."
1419 (or (calendar-date-compare e1 e2)
1420 (and (calendar-date-equal (car e1) (car e2))
1421 (let* ((ts1 (cadr e1)) (t1 (diary-entry-time ts1))
1422 (ts2 (cadr e2)) (t2 (diary-entry-time ts2)))
1423 (or (< t1 t2)
1424 (and (= t1 t2)
1425 (string-lessp ts1 ts2)))))))
1427 (defun diary-entry-time (s)
1428 "Return time at the beginning of the string S as a military-style integer.
1429 For example, returns 1325 for 1:25pm.
1431 Returns `diary-unknown-time' (default value -9999) if no time is recognized.
1432 The recognized forms are XXXX, X:XX, or XX:XX (military time), and XXam,
1433 XXAM, XXpm, XXPM, XX:XXam, XX:XXAM XX:XXpm, or XX:XXPM. A period (.) can
1434 be used instead of a colon (:) to separate the hour and minute parts."
1435 (let ((case-fold-search nil))
1436 (cond ((string-match ; Military time
1437 "\\`[ \t\n]*\\([0-9]?[0-9]\\)[:.]?\\([0-9][0-9]\\)\\(\\>\\|[^ap]\\)"
1439 (+ (* 100 (string-to-number (match-string 1 s)))
1440 (string-to-number (match-string 2 s))))
1441 ((string-match ; Hour only XXam or XXpm
1442 "\\`[ \t\n]*\\([0-9]?[0-9]\\)\\([ap]\\)m\\>" s)
1443 (+ (* 100 (% (string-to-number (match-string 1 s)) 12))
1444 (if (equal ?a (downcase (aref s (match-beginning 2))))
1445 0 1200)))
1446 ((string-match ; Hour and minute XX:XXam or XX:XXpm
1447 "\\`[ \t\n]*\\([0-9]?[0-9]\\)[:.]\\([0-9][0-9]\\)\\([ap]\\)m\\>" s)
1448 (+ (* 100 (% (string-to-number (match-string 1 s)) 12))
1449 (string-to-number (match-string 2 s))
1450 (if (equal ?a (downcase (aref s (match-beginning 3))))
1451 0 1200)))
1452 (t diary-unknown-time)))) ; Unrecognizable
1454 (defun list-sexp-diary-entries (date)
1455 "Add sexp entries for DATE from the diary file to `diary-entries-list'.
1456 Also, Make them visible in the diary file. Returns t if any entries were
1457 found.
1459 Sexp diary entries must be prefaced by a `sexp-diary-entry-symbol' (normally
1460 `%%'). The form of a sexp diary entry is
1462 %%(SEXP) ENTRY
1464 Both ENTRY and DATE are globally available when the SEXP is evaluated. If the
1465 SEXP yields the value nil, the diary entry does not apply. If it yields a
1466 non-nil value, ENTRY will be taken to apply to DATE; if the non-nil value is a
1467 string, that string will be the diary entry in the fancy diary display.
1469 For example, the following diary entry will apply to the 21st of the month
1470 if it is a weekday and the Friday before if the 21st is on a weekend:
1472 &%%(let ((dayname (calendar-day-of-week date))
1473 (day (extract-calendar-day date)))
1475 (and (= day 21) (memq dayname '(1 2 3 4 5)))
1476 (and (memq day '(19 20)) (= dayname 5)))
1477 ) UIUC pay checks deposited
1479 A number of built-in functions are available for this type of diary entry:
1481 %%(diary-date MONTH DAY YEAR &optional MARK) text
1482 Entry applies if date is MONTH, DAY, YEAR if
1483 `european-calendar-style' is nil, and DAY, MONTH, YEAR if
1484 `european-calendar-style' is t. DAY, MONTH, and YEAR
1485 can be lists of integers, the constant t, or an integer.
1486 The constant t means all values. An optional parameter
1487 MARK specifies a face or single-character string to use
1488 when highlighting the day in the calendar.
1490 %%(diary-float MONTH DAYNAME N &optional DAY MARK) text
1491 Entry will appear on the Nth DAYNAME of MONTH.
1492 (DAYNAME=0 means Sunday, 1 means Monday, and so on;
1493 if N is negative it counts backward from the end of
1494 the month. MONTH can be a list of months, a single
1495 month, or t to specify all months. Optional DAY means
1496 Nth DAYNAME of MONTH on or after/before DAY. DAY defaults
1497 to 1 if N>0 and the last day of the month if N<0. An
1498 optional parameter MARK specifies a face or single-character
1499 string to use when highlighting the day in the calendar.
1501 %%(diary-block M1 D1 Y1 M2 D2 Y2 &optional MARK) text
1502 Entry will appear on dates between M1/D1/Y1 and M2/D2/Y2,
1503 inclusive. (If `european-calendar-style' is t, the
1504 order of the parameters should be changed to D1, M1, Y1,
1505 D2, M2, Y2.) An optional parameter MARK specifies a face
1506 or single-character string to use when highlighting the
1507 day in the calendar.
1509 %%(diary-anniversary MONTH DAY YEAR &optional MARK) text
1510 Entry will appear on anniversary dates of MONTH DAY, YEAR.
1511 (If `european-calendar-style' is t, the order of the
1512 parameters should be changed to DAY, MONTH, YEAR.) Text
1513 can contain %d or %d%s; %d will be replaced by the number
1514 of years since the MONTH DAY, YEAR and %s will be replaced
1515 by the ordinal ending of that number (that is, `st', `nd',
1516 `rd' or `th', as appropriate. The anniversary of February
1517 29 is considered to be March 1 in a non-leap year. An
1518 optional parameter MARK specifies a face or single-character
1519 string to use when highlighting the day in the calendar.
1521 %%(diary-cyclic N MONTH DAY YEAR &optional MARK) text
1522 Entry will appear every N days, starting MONTH DAY, YEAR.
1523 (If `european-calendar-style' is t, the order of the
1524 parameters should be changed to N, DAY, MONTH, YEAR.) Text
1525 can contain %d or %d%s; %d will be replaced by the number
1526 of repetitions since the MONTH DAY, YEAR and %s will
1527 be replaced by the ordinal ending of that number (that is,
1528 `st', `nd', `rd' or `th', as appropriate. An optional
1529 parameter MARK specifies a face or single-character string
1530 to use when highlighting the day in the calendar.
1532 %%(diary-remind SEXP DAYS &optional MARKING) text
1533 Entry is a reminder for diary sexp SEXP. DAYS is either a
1534 single number or a list of numbers indicating the number(s)
1535 of days before the event that the warning(s) should occur.
1536 If the current date is (one of) DAYS before the event
1537 indicated by EXPR, then a suitable message (as specified
1538 by `diary-remind-message') appears. In addition to the
1539 reminders beforehand, the diary entry also appears on
1540 the date itself. If optional MARKING is non-nil then the
1541 *reminders* are marked on the calendar. Marking of
1542 reminders is independent of whether the entry *itself* is
1543 a marking or nonmarking one.
1545 %%(diary-day-of-year)
1546 Diary entries giving the day of the year and the number of
1547 days remaining in the year will be made every day. Note
1548 that since there is no text, it makes sense only if the
1549 fancy diary display is used.
1551 %%(diary-iso-date)
1552 Diary entries giving the corresponding ISO commercial date
1553 will be made every day. Note that since there is no text,
1554 it makes sense only if the fancy diary display is used.
1556 %%(diary-french-date)
1557 Diary entries giving the corresponding French Revolutionary
1558 date will be made every day. Note that since there is no
1559 text, it makes sense only if the fancy diary display is used.
1561 %%(diary-islamic-date)
1562 Diary entries giving the corresponding Islamic date will be
1563 made every day. Note that since there is no text, it
1564 makes sense only if the fancy diary display is used.
1566 %%(diary-hebrew-date)
1567 Diary entries giving the corresponding Hebrew date will be
1568 made every day. Note that since there is no text, it
1569 makes sense only if the fancy diary display is used.
1571 %%(diary-astro-day-number) Diary entries giving the corresponding
1572 astronomical (Julian) day number will be made every day.
1573 Note that since there is no text, it makes sense only if the
1574 fancy diary display is used.
1576 %%(diary-julian-date) Diary entries giving the corresponding
1577 Julian date will be made every day. Note that since
1578 there is no text, it makes sense only if the fancy diary
1579 display is used.
1581 %%(diary-sunrise-sunset)
1582 Diary entries giving the local times of sunrise and sunset
1583 will be made every day. Note that since there is no text,
1584 it makes sense only if the fancy diary display is used.
1585 Floating point required.
1587 %%(diary-phases-of-moon)
1588 Diary entries giving the times of the phases of the moon
1589 will be when appropriate. Note that since there is no text,
1590 it makes sense only if the fancy diary display is used.
1591 Floating point required.
1593 %%(diary-yahrzeit MONTH DAY YEAR) text
1594 Text is assumed to be the name of the person; the date is
1595 the date of death on the *civil* calendar. The diary entry
1596 will appear on the proper Hebrew-date anniversary and on the
1597 day before. (If `european-calendar-style' is t, the order
1598 of the parameters should be changed to DAY, MONTH, YEAR.)
1600 %%(diary-rosh-hodesh)
1601 Diary entries will be made on the dates of Rosh Hodesh on
1602 the Hebrew calendar. Note that since there is no text, it
1603 makes sense only if the fancy diary display is used.
1605 %%(diary-parasha)
1606 Diary entries giving the weekly parasha will be made on
1607 every Saturday. Note that since there is no text, it
1608 makes sense only if the fancy diary display is used.
1610 %%(diary-omer)
1611 Diary entries giving the omer count will be made every day
1612 from Passover to Shavuot. Note that since there is no text,
1613 it makes sense only if the fancy diary display is used.
1615 Marking these entries is *extremely* time consuming, so these entries are
1616 best if they are nonmarking."
1617 (let ((s-entry (concat "^"
1618 (regexp-quote diary-nonmarking-symbol)
1620 (regexp-quote sexp-diary-entry-symbol)
1621 "("))
1622 entry-found file-glob-attrs marks)
1623 (goto-char (point-min))
1624 (save-excursion
1625 (setq file-glob-attrs (nth 1 (diary-pull-attrs nil '()))))
1626 (while (re-search-forward s-entry nil t)
1627 (backward-char 1)
1628 (let ((sexp-start (point))
1629 sexp entry specifier entry-start line-start)
1630 (forward-sexp)
1631 (setq sexp (buffer-substring-no-properties sexp-start (point)))
1632 (setq line-start (line-end-position 0))
1633 (setq specifier
1634 (buffer-substring-no-properties (1+ line-start) (point))
1635 entry-start (1+ line-start))
1636 (forward-char 1)
1637 (if (and (bolp) (not (looking-at "[ \t]")))
1638 (progn;; Diary entry consists only of the sexp
1639 (backward-char 1)
1640 (setq entry ""))
1641 (setq entry-start (point))
1642 (forward-line 1)
1643 (while (looking-at "[ \t]")
1644 (forward-line 1))
1645 (backward-char 1)
1646 (setq entry (buffer-substring-no-properties entry-start (point))))
1647 (let ((diary-entry (diary-sexp-entry sexp entry date))
1648 temp literal)
1649 (setq literal entry ; before evaluation
1650 entry (if (consp diary-entry)
1651 (cdr diary-entry)
1652 diary-entry))
1653 (if diary-entry
1654 (progn
1655 (remove-overlays line-start (point) 'invisible 'diary)
1656 (if (< 0 (length entry))
1657 (setq temp (diary-pull-attrs entry file-glob-attrs)
1658 entry (nth 0 temp)
1659 marks (nth 1 temp)))))
1660 (add-to-diary-list date
1661 entry
1662 specifier
1663 (if entry-start (copy-marker entry-start)
1664 nil)
1665 marks
1666 literal)
1667 (setq entry-found (or entry-found diary-entry)))))
1668 entry-found))
1670 (defun diary-sexp-entry (sexp entry date)
1671 "Process a SEXP diary ENTRY for DATE."
1672 (let ((result (if calendar-debug-sexp
1673 (let ((stack-trace-on-error t))
1674 (eval (car (read-from-string sexp))))
1675 (condition-case nil
1676 (eval (car (read-from-string sexp)))
1677 (error
1678 (beep)
1679 (message "Bad sexp at line %d in %s: %s"
1680 (count-lines (point-min) (point))
1681 diary-file sexp)
1682 (sleep-for 2))))))
1683 (cond ((stringp result) result)
1684 ((and (consp result)
1685 (stringp (cdr result))) result)
1686 (result entry)
1687 (t nil))))
1689 (defvar entry)
1691 ;; To be called from diary-sexp-entry, where DATE, ENTRY are bound.
1692 (defun diary-date (month day year &optional mark)
1693 "Specific date(s) diary entry.
1694 Entry applies if date is MONTH, DAY, YEAR if `european-calendar-style' is nil,
1695 and DAY, MONTH, YEAR if `european-calendar-style' is t. DAY, MONTH, and YEAR
1696 can be lists of integers, the constant t, or an integer. The constant t means
1697 all values.
1699 An optional parameter MARK specifies a face or single-character string to
1700 use when highlighting the day in the calendar."
1701 (let ((dd (if european-calendar-style
1702 month
1703 day))
1704 (mm (if european-calendar-style
1706 month))
1707 (m (extract-calendar-month date))
1708 (y (extract-calendar-year date))
1709 (d (extract-calendar-day date)))
1710 (if (and
1711 (or (and (listp dd) (memq d dd))
1712 (equal d dd)
1713 (eq dd t))
1714 (or (and (listp mm) (memq m mm))
1715 (equal m mm)
1716 (eq mm t))
1717 (or (and (listp year) (memq y year))
1718 (equal y year)
1719 (eq year t)))
1720 (cons mark entry))))
1722 ;; To be called from diary-sexp-entry, where DATE, ENTRY are bound.
1723 (defun diary-block (m1 d1 y1 m2 d2 y2 &optional mark)
1724 "Block diary entry.
1725 Entry applies if date is between, or on one of, two dates.
1726 The order of the parameters is
1727 M1, D1, Y1, M2, D2, Y2 if `european-calendar-style' is nil, and
1728 D1, M1, Y1, D2, M2, Y2 if `european-calendar-style' is t.
1730 An optional parameter MARK specifies a face or single-character string to
1731 use when highlighting the day in the calendar."
1733 (let ((date1 (calendar-absolute-from-gregorian
1734 (if european-calendar-style
1735 (list d1 m1 y1)
1736 (list m1 d1 y1))))
1737 (date2 (calendar-absolute-from-gregorian
1738 (if european-calendar-style
1739 (list d2 m2 y2)
1740 (list m2 d2 y2))))
1741 (d (calendar-absolute-from-gregorian date)))
1742 (if (and (<= date1 d) (<= d date2))
1743 (cons mark entry))))
1745 ;; To be called from diary-sexp-entry, where DATE, ENTRY are bound.
1746 (defun diary-float (month dayname n &optional day mark)
1747 "Floating diary entry--entry applies if date is the nth dayname of month.
1748 Parameters are MONTH, DAYNAME, N. MONTH can be a list of months, the constant
1749 t, or an integer. The constant t means all months. If N is negative, count
1750 backward from the end of the month.
1752 An optional parameter DAY means the Nth DAYNAME on or after/before MONTH DAY.
1753 Optional MARK specifies a face or single-character string to use when
1754 highlighting the day in the calendar."
1755 ;; This is messy because the diary entry may apply, but the date on which it
1756 ;; is based can be in a different month/year. For example, asking for the
1757 ;; first Monday after December 30. For large values of |n| the problem is
1758 ;; more grotesque.
1759 (and (= dayname (calendar-day-of-week date))
1760 (let* ((m (extract-calendar-month date))
1761 (d (extract-calendar-day date))
1762 (y (extract-calendar-year date))
1763 (limit; last (n>0) or first (n<0) possible base date for entry
1764 (calendar-nth-named-absday (- n) dayname m y d))
1765 (last-abs (if (> n 0) limit (+ limit 6)))
1766 (first-abs (if (> n 0) (- limit 6) limit))
1767 (last (calendar-gregorian-from-absolute last-abs))
1768 (first (calendar-gregorian-from-absolute first-abs))
1769 ; m1, d1 is first possible base date
1770 (m1 (extract-calendar-month first))
1771 (d1 (extract-calendar-day first))
1772 (y1 (extract-calendar-year first))
1773 ; m2, d2 is last possible base date
1774 (m2 (extract-calendar-month last))
1775 (d2 (extract-calendar-day last))
1776 (y2 (extract-calendar-year last)))
1777 (if (or (and (= m1 m2) ; only possible base dates in one month
1778 (or (eq month t)
1779 (if (listp month)
1780 (memq m1 month)
1781 (= m1 month)))
1782 (let ((d (or day (if (> n 0)
1784 (calendar-last-day-of-month m1 y1)))))
1785 (and (<= d1 d) (<= d d2))))
1786 ;; only possible base dates straddle two months
1787 (and (or (< y1 y2)
1788 (and (= y1 y2) (< m1 m2)))
1790 ;; m1, d1 works as a base date
1791 (and
1792 (or (eq month t)
1793 (if (listp month)
1794 (memq m1 month)
1795 (= m1 month)))
1796 (<= d1 (or day (if (> n 0)
1798 (calendar-last-day-of-month m1 y1)))))
1799 ;; m2, d2 works as a base date
1800 (and (or (eq month t)
1801 (if (listp month)
1802 (memq m2 month)
1803 (= m2 month)))
1804 (<= (or day (if (> n 0)
1806 (calendar-last-day-of-month m2 y2)))
1807 d2)))))
1808 (cons mark entry)))))
1810 ;; To be called from diary-sexp-entry, where DATE, ENTRY are bound.
1811 (defun diary-anniversary (month day &optional year mark)
1812 "Anniversary diary entry.
1813 Entry applies if date is the anniversary of MONTH, DAY, YEAR if
1814 `european-calendar-style' is nil, and DAY, MONTH, YEAR if
1815 `european-calendar-style' is t. Diary entry can contain `%d' or `%d%s'; the
1816 %d will be replaced by the number of years since the MONTH DAY, YEAR and the
1817 %s will be replaced by the ordinal ending of that number (that is, `st', `nd',
1818 `rd' or `th', as appropriate. The anniversary of February 29 is considered
1819 to be March 1 in non-leap years.
1821 An optional parameter MARK specifies a face or single-character string to
1822 use when highlighting the day in the calendar."
1823 (let* ((d (if european-calendar-style
1824 month
1825 day))
1826 (m (if european-calendar-style
1828 month))
1829 (y (extract-calendar-year date))
1830 (diff (if year (- y year) 100)))
1831 (if (and (= m 2) (= d 29) (not (calendar-leap-year-p y)))
1832 (setq m 3
1833 d 1))
1834 (if (and (> diff 0) (calendar-date-equal (list m d y) date))
1835 (cons mark (format entry diff (diary-ordinal-suffix diff))))))
1837 ;; To be called from diary-sexp-entry, where DATE, ENTRY are bound.
1838 (defun diary-cyclic (n month day year &optional mark)
1839 "Cycle diary entry--entry applies every N days starting at MONTH, DAY, YEAR.
1840 If `european-calendar-style' is t, parameters are N, DAY, MONTH, YEAR.
1841 ENTRY can contain `%d' or `%d%s'; the %d will be replaced by the number of
1842 repetitions since the MONTH DAY, YEAR and %s will be replaced by the
1843 ordinal ending of that number (that is, `st', `nd', `rd' or `th', as
1844 appropriate.
1846 An optional parameter MARK specifies a face or single-character string to
1847 use when highlighting the day in the calendar."
1848 (let* ((d (if european-calendar-style
1849 month
1850 day))
1851 (m (if european-calendar-style
1853 month))
1854 (diff (- (calendar-absolute-from-gregorian date)
1855 (calendar-absolute-from-gregorian
1856 (list m d year))))
1857 (cycle (/ diff n)))
1858 (if (and (>= diff 0) (zerop (% diff n)))
1859 (cons mark (format entry cycle (diary-ordinal-suffix cycle))))))
1861 (defun diary-ordinal-suffix (n)
1862 "Ordinal suffix for N. (That is, `st', `nd', `rd', or `th', as appropriate.)"
1863 (if (or (memq (% n 100) '(11 12 13))
1864 (< 3 (% n 10)))
1865 "th"
1866 (aref ["th" "st" "nd" "rd"] (% n 10))))
1868 (defun diary-day-of-year ()
1869 "Day of year and number of days remaining in the year of date diary entry."
1870 (calendar-day-of-year-string date))
1872 (defun diary-remind (sexp days &optional marking)
1873 "Provide a reminder of a diary entry.
1874 SEXP is a diary-sexp. DAYS is either a single number or a list of numbers
1875 indicating the number(s) of days before the event that the warning(s) should
1876 occur on. If the current date is (one of) DAYS before the event indicated by
1877 SEXP, then a suitable message (as specified by `diary-remind-message' is
1878 returned.
1880 In addition to the reminders beforehand, the diary entry also appears on the
1881 date itself.
1883 A `diary-nonmarking-symbol' at the beginning of the line of the diary-remind
1884 entry specifies that the diary entry (not the reminder) is non-marking.
1885 Marking of reminders is independent of whether the entry itself is a marking
1886 or nonmarking; if optional parameter MARKING is non-nil then the reminders are
1887 marked on the calendar."
1888 (let ((diary-entry (eval sexp)))
1889 (cond
1890 ;; Diary entry applies on date
1891 ((and diary-entry
1892 (or (not marking-diary-entries) marking-diary-entry))
1893 diary-entry)
1894 ;; Diary entry may apply to `days' before date
1895 ((and (integerp days)
1896 (not diary-entry); Diary entry does not apply to date
1897 (or (not marking-diary-entries) marking))
1898 (let ((date (calendar-gregorian-from-absolute
1899 (+ (calendar-absolute-from-gregorian date) days))))
1900 (when (setq diary-entry (eval sexp)) ; re-evaluate with adjusted date
1901 ;; Discard any mark portion from diary-anniversary, etc.
1902 (if (consp diary-entry) (setq diary-entry (cdr diary-entry)))
1903 (mapconcat 'eval diary-remind-message ""))))
1904 ;; Diary entry may apply to one of a list of days before date
1905 ((and (listp days) days)
1906 (or (diary-remind sexp (car days) marking)
1907 (diary-remind sexp (cdr days) marking))))))
1909 (defun diary-redraw-calendar ()
1910 "If `calendar-buffer' is live and diary entries are marked, redraw it."
1911 (and mark-diary-entries-in-calendar
1912 (save-excursion
1913 (redraw-calendar)))
1914 ;; Return value suitable for `write-contents-functions'.
1915 nil)
1917 (defun make-diary-entry (string &optional nonmarking file)
1918 "Insert a diary entry STRING which may be NONMARKING in FILE.
1919 If omitted, NONMARKING defaults to nil and FILE defaults to
1920 `diary-file'."
1921 (let ((pop-up-frames (window-dedicated-p (selected-window))))
1922 (find-file-other-window (substitute-in-file-name (or file diary-file))))
1923 (when (eq major-mode default-major-mode) (diary-mode))
1924 (widen)
1925 (diary-unhide-everything)
1926 (goto-char (point-max))
1927 (when (let ((case-fold-search t))
1928 (search-backward "Local Variables:"
1929 (max (- (point-max) 3000) (point-min))
1931 (beginning-of-line)
1932 (insert "\n")
1933 (forward-line -1))
1934 (insert
1935 (if (bolp) "" "\n")
1936 (if nonmarking diary-nonmarking-symbol "")
1937 string " "))
1939 (defun insert-diary-entry (arg)
1940 "Insert a diary entry for the date indicated by point.
1941 Prefix arg will make the entry nonmarking."
1942 (interactive "P")
1943 (make-diary-entry (calendar-date-string (calendar-cursor-to-date t) t t)
1944 arg))
1946 (defun insert-weekly-diary-entry (arg)
1947 "Insert a weekly diary entry for the day of the week indicated by point.
1948 Prefix arg will make the entry nonmarking."
1949 (interactive "P")
1950 (make-diary-entry (calendar-day-name (calendar-cursor-to-date t))
1951 arg))
1953 (defun insert-monthly-diary-entry (arg)
1954 "Insert a monthly diary entry for the day of the month indicated by point.
1955 Prefix arg will make the entry nonmarking."
1956 (interactive "P")
1957 (let ((calendar-date-display-form
1958 (if european-calendar-style
1959 '(day " * ")
1960 '("* " day))))
1961 (make-diary-entry (calendar-date-string (calendar-cursor-to-date t) t)
1962 arg)))
1964 (defun insert-yearly-diary-entry (arg)
1965 "Insert an annual diary entry for the day of the year indicated by point.
1966 Prefix arg will make the entry nonmarking."
1967 (interactive "P")
1968 (let ((calendar-date-display-form
1969 (if european-calendar-style
1970 '(day " " monthname)
1971 '(monthname " " day))))
1972 (make-diary-entry (calendar-date-string (calendar-cursor-to-date t) t)
1973 arg)))
1975 (defun insert-anniversary-diary-entry (arg)
1976 "Insert an anniversary diary entry for the date given by point.
1977 Prefix arg will make the entry nonmarking."
1978 (interactive "P")
1979 (let ((calendar-date-display-form
1980 (if european-calendar-style
1981 '(day " " month " " year)
1982 '(month " " day " " year))))
1983 (make-diary-entry
1984 (format "%s(diary-anniversary %s)"
1985 sexp-diary-entry-symbol
1986 (calendar-date-string (calendar-cursor-to-date t) nil t))
1987 arg)))
1989 (defun insert-block-diary-entry (arg)
1990 "Insert a block diary entry for the days between the point and marked date.
1991 Prefix arg will make the entry nonmarking."
1992 (interactive "P")
1993 (let ((calendar-date-display-form
1994 (if european-calendar-style
1995 '(day " " month " " year)
1996 '(month " " day " " year)))
1997 (cursor (calendar-cursor-to-date t))
1998 (mark (or (car calendar-mark-ring)
1999 (error "No mark set in this buffer")))
2000 start end)
2001 (if (< (calendar-absolute-from-gregorian mark)
2002 (calendar-absolute-from-gregorian cursor))
2003 (setq start mark
2004 end cursor)
2005 (setq start cursor
2006 end mark))
2007 (make-diary-entry
2008 (format "%s(diary-block %s %s)"
2009 sexp-diary-entry-symbol
2010 (calendar-date-string start nil t)
2011 (calendar-date-string end nil t))
2012 arg)))
2014 (defun insert-cyclic-diary-entry (arg)
2015 "Insert a cyclic diary entry starting at the date given by point.
2016 Prefix arg will make the entry nonmarking."
2017 (interactive "P")
2018 (let ((calendar-date-display-form
2019 (if european-calendar-style
2020 '(day " " month " " year)
2021 '(month " " day " " year))))
2022 (make-diary-entry
2023 (format "%s(diary-cyclic %d %s)"
2024 sexp-diary-entry-symbol
2025 (calendar-read "Repeat every how many days: "
2026 (lambda (x) (> x 0)))
2027 (calendar-date-string (calendar-cursor-to-date t) nil t))
2028 arg)))
2030 (defvar diary-mode-map
2031 (let ((map (make-sparse-keymap)))
2032 (define-key map "\C-c\C-s" 'diary-show-all-entries)
2033 (define-key map "\C-c\C-q" 'quit-window)
2034 map)
2035 "Keymap for `diary-mode'.")
2037 ;;;###autoload
2038 (define-derived-mode diary-mode fundamental-mode "Diary"
2039 "Major mode for editing the diary file."
2040 (set (make-local-variable 'font-lock-defaults)
2041 '(diary-font-lock-keywords t))
2042 (add-to-invisibility-spec '(diary . nil))
2043 (add-hook 'after-save-hook 'diary-redraw-calendar nil t)
2044 (if diary-header-line-flag
2045 (setq header-line-format diary-header-line-format)))
2048 (defvar diary-fancy-date-pattern
2049 (concat
2050 (let ((dayname (diary-name-pattern calendar-day-name-array nil t))
2051 (monthname (diary-name-pattern calendar-month-name-array nil t))
2052 (day "[0-9]+")
2053 (month "[0-9]+")
2054 (year "-?[0-9]+"))
2055 (mapconcat 'eval calendar-date-display-form ""))
2056 ;; Optional ": holiday name" after the date.
2057 "\\(: .*\\)?")
2058 "Regular expression matching a date header in Fancy Diary.")
2060 (defconst diary-time-regexp
2061 ;; Accepted formats: 10:00 10.00 10h00 10h 10am 10:00am 10.00am
2062 ;; Use of "." as a separator annoyingly matches numbers, eg "123.45".
2063 ;; Hence often prefix this with "\\(^\\|\\s-\\)."
2064 (concat "[0-9]?[0-9]\\([AaPp][mM]\\|\\("
2065 "[Hh]\\([0-9][0-9]\\)?\\|[:.][0-9][0-9]"
2066 "\\)\\([AaPp][Mm]\\)?\\)")
2067 "Regular expression matching a time of day.")
2069 (defface diary-anniversary '((t :inherit font-lock-keyword-face))
2070 "Face used for anniversaries in the diary."
2071 :version "22.1"
2072 :group 'diary)
2074 (defface diary-time '((t :inherit font-lock-variable-name-face))
2075 "Face used for times of day in the diary."
2076 :version "22.1"
2077 :group 'diary)
2079 (defvar fancy-diary-font-lock-keywords
2080 (list
2081 (list
2082 ;; Any number of " other holiday name" lines, followed by "==" line.
2083 (concat diary-fancy-date-pattern "\\(\n +.*\\)*\n=+$")
2084 '(0 (progn (put-text-property (match-beginning 0) (match-end 0)
2085 'font-lock-multiline t)
2086 diary-face)))
2087 '("^.*\\([aA]nniversary\\|[bB]irthday\\).*$" . 'diary-anniversary)
2088 '("^.*Yahrzeit.*$" . font-lock-reference-face)
2089 '("^\\(Erev \\)?Rosh Hodesh.*" . font-lock-function-name-face)
2090 '("^Day.*omer.*$" . font-lock-builtin-face)
2091 '("^Parashat.*$" . font-lock-comment-face)
2092 `(,(format "\\(^\\|\\s-\\)%s\\(-%s\\)?" diary-time-regexp
2093 diary-time-regexp) . 'diary-time))
2094 "Keywords to highlight in fancy diary display")
2096 ;; If region looks like it might start or end in the middle of a
2097 ;; multiline pattern, extend the region to encompass the whole pattern.
2098 (defun diary-fancy-font-lock-fontify-region-function (beg end &optional verbose)
2099 "Function to use for `font-lock-fontify-region-function' in Fancy Diary.
2100 Needed to handle multiline keyword in `fancy-diary-font-lock-keywords'."
2101 (goto-char beg)
2102 (forward-line 0)
2103 (if (looking-at "=+$") (forward-line -1))
2104 (while (and (looking-at " +[^ ]")
2105 (zerop (forward-line -1))))
2106 ;; This check not essential.
2107 (if (looking-at diary-fancy-date-pattern)
2108 (setq beg (line-beginning-position)))
2109 (goto-char end)
2110 (forward-line 0)
2111 (while (and (looking-at " +[^ ]")
2112 (zerop (forward-line 1))))
2113 (if (looking-at "=+$")
2114 (setq end (line-beginning-position 2)))
2115 (font-lock-default-fontify-region beg end verbose))
2117 (define-derived-mode fancy-diary-display-mode fundamental-mode
2118 "Diary"
2119 "Major mode used while displaying diary entries using Fancy Display."
2120 (set (make-local-variable 'font-lock-defaults)
2121 '(fancy-diary-font-lock-keywords
2122 t nil nil nil
2123 (font-lock-fontify-region-function
2124 . diary-fancy-font-lock-fontify-region-function)))
2125 (local-set-key "q" 'quit-window))
2128 (defun diary-font-lock-sexps (limit)
2129 "Recognize sexp diary entry for font-locking."
2130 (if (re-search-forward
2131 (concat "^" (regexp-quote diary-nonmarking-symbol)
2132 "?\\(" (regexp-quote sexp-diary-entry-symbol) "\\)")
2133 limit t)
2134 (condition-case nil
2135 (save-restriction
2136 (narrow-to-region (point-min) limit)
2137 (let ((start (point)))
2138 (forward-sexp 1)
2139 (store-match-data (list start (point)))
2141 (error t))))
2143 (defun diary-font-lock-date-forms (month-array &optional symbol abbrev-array)
2144 "Create font-lock patterns for `diary-date-forms' using MONTH-ARRAY.
2145 If given, optional SYMBOL must be a prefix to entries.
2146 If optional ABBREV-ARRAY is present, the abbreviations constructed
2147 from this array by the function `calendar-abbrev-construct' are
2148 matched (with or without a final `.'), in addition to the full month
2149 names."
2150 (let ((dayname (diary-name-pattern calendar-day-name-array
2151 calendar-day-abbrev-array t))
2152 (monthname (format "\\(%s\\|\\*\\)"
2153 (diary-name-pattern month-array abbrev-array)))
2154 (month "\\([0-9]+\\|\\*\\)")
2155 (day "\\([0-9]+\\|\\*\\)")
2156 (year "-?\\([0-9]+\\|\\*\\)"))
2157 (mapcar (lambda (x)
2158 (cons
2159 (concat "^" (regexp-quote diary-nonmarking-symbol) "?"
2160 (if symbol (regexp-quote symbol) "") "\\("
2161 (mapconcat 'eval
2162 ;; If backup, omit first item (backup)
2163 ;; and last item (not part of date)
2164 (if (equal (car x) 'backup)
2165 (nreverse (cdr (reverse (cdr x))))
2168 ;; With backup, last item is not part of date
2169 (if (equal (car x) 'backup)
2170 (concat "\\)" (eval (car (reverse x))))
2171 "\\)"))
2172 '(1 diary-face)))
2173 diary-date-forms)))
2175 (defvar calendar-hebrew-month-name-array-leap-year)
2176 (defvar calendar-islamic-month-name-array)
2177 (defvar calendar-bahai-month-name-array)
2179 (defun diary-font-lock-keywords ()
2180 "Return a value for the variable `diary-font-lock-keywords'."
2181 (append
2182 (diary-font-lock-date-forms calendar-month-name-array
2183 nil calendar-month-abbrev-array)
2184 (when (or (memq 'mark-hebrew-diary-entries
2185 nongregorian-diary-marking-hook)
2186 (memq 'list-hebrew-diary-entries
2187 nongregorian-diary-listing-hook))
2188 (require 'cal-hebrew)
2189 (diary-font-lock-date-forms
2190 calendar-hebrew-month-name-array-leap-year hebrew-diary-entry-symbol))
2191 (when (or (memq 'mark-islamic-diary-entries
2192 nongregorian-diary-marking-hook)
2193 (memq 'list-islamic-diary-entries
2194 nongregorian-diary-listing-hook))
2195 (require 'cal-islam)
2196 (diary-font-lock-date-forms
2197 calendar-islamic-month-name-array islamic-diary-entry-symbol))
2198 (when (or (memq 'diary-bahai-mark-entries
2199 nongregorian-diary-marking-hook)
2200 (memq 'diary-bahai-list-entries
2201 nongregorian-diary-marking-hook))
2202 (require 'cal-bahai)
2203 (diary-font-lock-date-forms
2204 calendar-bahai-month-name-array bahai-diary-entry-symbol))
2205 (list
2206 (cons
2207 (format "^%s.*$" (regexp-quote diary-include-string))
2208 'font-lock-keyword-face)
2209 (cons
2210 (format "^%s?\\(%s\\)" (regexp-quote diary-nonmarking-symbol)
2211 (regexp-quote sexp-diary-entry-symbol))
2212 '(1 font-lock-reference-face))
2213 (cons
2214 (format "^%s" (regexp-quote diary-nonmarking-symbol))
2215 'font-lock-reference-face)
2216 (cons
2217 (format "^%s?%s" (regexp-quote diary-nonmarking-symbol)
2218 (regexp-opt (mapcar 'regexp-quote
2219 (list hebrew-diary-entry-symbol
2220 islamic-diary-entry-symbol
2221 bahai-diary-entry-symbol))
2223 '(1 font-lock-reference-face))
2224 '(diary-font-lock-sexps . font-lock-keyword-face)
2225 `(,(format "\\(^\\|\\s-\\)%s\\(-%s\\)?" diary-time-regexp
2226 diary-time-regexp)
2227 . 'diary-time))))
2229 (defvar diary-font-lock-keywords (diary-font-lock-keywords)
2230 "Forms to highlight in `diary-mode'.")
2232 ;; Following code from Dave Love <fx@gnu.org>.
2233 ;; Import Outlook-format appointments from mail messages in Gnus or
2234 ;; Rmail using command `diary-from-outlook'. This, or the specialized
2235 ;; functions `diary-from-outlook-gnus' and `diary-from-outlook-rmail',
2236 ;; could be run from hooks to notice appointments automatically (in
2237 ;; which case they will prompt about adding to the diary). The
2238 ;; message formats recognized are customizable through
2239 ;; `diary-outlook-formats'.
2241 ;; Dynamically bound.
2242 (defvar subject)
2244 (defun diary-from-outlook-internal (&optional test-only)
2245 "Snarf a diary entry from a message assumed to be from MS Outlook.
2246 Assumes `body' is bound to a string comprising the body of the message and
2247 `subject' is bound to a string comprising its subject.
2248 Arg TEST-ONLY non-nil means return non-nil if and only if the
2249 message contains an appointment, don't make a diary entry."
2250 (catch 'finished
2251 (let (format-string)
2252 (dotimes (i (length diary-outlook-formats))
2253 (when (eq 0 (string-match (car (nth i diary-outlook-formats))
2254 body))
2255 (unless test-only
2256 (setq format-string (cdr (nth i diary-outlook-formats)))
2257 (save-excursion
2258 (save-window-excursion
2259 ;; Fixme: References to optional fields in the format
2260 ;; are treated literally, not replaced by the empty
2261 ;; string. I think this is an Emacs bug.
2262 (make-diary-entry
2263 (format (replace-match (if (functionp format-string)
2264 (funcall format-string body)
2265 format-string)
2266 t nil (match-string 0 body))
2267 subject))
2268 (save-buffer))))
2269 (throw 'finished t))))
2270 nil))
2272 (defun diary-from-outlook (&optional noconfirm)
2273 "Maybe snarf diary entry from current Outlook-generated message.
2274 Currently knows about Gnus and Rmail modes. Unless the optional
2275 argument NOCONFIRM is non-nil (which is the case when this
2276 function is called interactively), then if an entry is found the
2277 user is asked to confirm its addition."
2278 (interactive "p")
2279 (let ((func (cond
2280 ((eq major-mode 'rmail-mode)
2281 #'diary-from-outlook-rmail)
2282 ((memq major-mode '(gnus-summary-mode gnus-article-mode))
2283 #'diary-from-outlook-gnus)
2284 (t (error "Don't know how to snarf in `%s'" major-mode)))))
2285 (funcall func noconfirm)))
2288 (defvar gnus-article-mime-handles)
2289 (defvar gnus-article-buffer)
2291 (autoload 'gnus-fetch-field "gnus-util")
2292 (autoload 'gnus-narrow-to-body "gnus")
2293 (autoload 'mm-get-part "mm-decode")
2295 (defun diary-from-outlook-gnus (&optional noconfirm)
2296 "Maybe snarf diary entry from Outlook-generated message in Gnus.
2297 Unless the optional argument NOCONFIRM is non-nil (which is the case when
2298 this function is called interactively), then if an entry is found the
2299 user is asked to confirm its addition.
2300 Add this function to `gnus-article-prepare-hook' to notice appointments
2301 automatically."
2302 (interactive "p")
2303 (with-current-buffer gnus-article-buffer
2304 (let ((subject (gnus-fetch-field "subject"))
2305 (body (if gnus-article-mime-handles
2306 ;; We're multipart. Don't get confused by part
2307 ;; buttons &c. Assume info is in first part.
2308 (mm-get-part (nth 1 gnus-article-mime-handles))
2309 (save-restriction
2310 (gnus-narrow-to-body)
2311 (buffer-string)))))
2312 (when (diary-from-outlook-internal t)
2313 (when (or noconfirm (y-or-n-p "Snarf diary entry? "))
2314 (diary-from-outlook-internal)
2315 (message "Diary entry added"))))))
2317 (custom-add-option 'gnus-article-prepare-hook 'diary-from-outlook-gnus)
2320 (defvar rmail-buffer)
2322 (defun diary-from-outlook-rmail (&optional noconfirm)
2323 "Maybe snarf diary entry from Outlook-generated message in Rmail.
2324 Unless the optional argument NOCONFIRM is non-nil (which is the case when
2325 this function is called interactively), then if an entry is found the
2326 user is asked to confirm its addition."
2327 (interactive "p")
2328 (with-current-buffer rmail-buffer
2329 (let ((subject (mail-fetch-field "subject"))
2330 (body (buffer-substring (save-excursion
2331 (rfc822-goto-eoh)
2332 (point))
2333 (point-max))))
2334 (when (diary-from-outlook-internal t)
2335 (when (or noconfirm (y-or-n-p "Snarf diary entry? "))
2336 (diary-from-outlook-internal)
2337 (message "Diary entry added"))))))
2340 (provide 'diary-lib)
2342 ;; arch-tag: 22dd506e-2e33-410d-9ae1-095a0c1b2010
2343 ;;; diary-lib.el ends here