Unquote lambda functions.
[emacs.git] / lisp / calendar / diary-lib.el
blob640cb6f789edbb678860628919d10828a0848ca0
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 (setq list-diary-entries-hook
122 '(include-other-diary-files sort-diary-entries))
123 (setq 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 entry)
417 (defvar date)
418 (defvar number)
419 (defvar date-string)
420 (defvar original-date)
422 (defun diary-attrtype-convert (attrvalue type)
423 "Convert string ATTRVALUE to TYPE appropriate for a face description.
424 Valid TYPEs are: string, symbol, int, stringtnil, tnil."
425 (let (ret)
426 (setq ret (cond ((eq type 'string) attrvalue)
427 ((eq type 'symbol) (read attrvalue))
428 ((eq type 'int) (string-to-number attrvalue))
429 ((eq type 'stringtnil)
430 (cond ((string= "t" attrvalue) t)
431 ((string= "nil" attrvalue) nil)
432 (t attrvalue)))
433 ((eq type 'tnil)
434 (cond ((string= "t" attrvalue) t)
435 ((string= "nil" attrvalue) nil)))))
436 ; (message "(%s)[%s]=[%s]" (print type) attrvalue ret)
437 ret))
440 (defun diary-pull-attrs (entry fileglobattrs)
441 "Pull the face-related attributes off the entry, merge with the
442 fileglobattrs, and return the (possibly modified) entry and face
443 data in a list of attrname attrvalue values.
444 The entry will be modified to drop all tags that are used for face matching.
445 If entry is nil, then the fileglobattrs are being searched for,
446 the fileglobattrs variable is ignored, and
447 diary-glob-file-regexp-prefix is prepended to the regexps before each
448 search."
449 (save-excursion
450 (let (regexp regnum attrname attr-list attrname attrvalue type
451 ret-attr attr)
452 (if (null entry)
453 (progn
454 (setq ret-attr '()
455 attr-list diary-face-attrs)
456 (while attr-list
457 (goto-char (point-min))
458 (setq attr (car attr-list)
459 regexp (nth 0 attr)
460 regnum (nth 1 attr)
461 attrname (nth 2 attr)
462 type (nth 3 attr)
463 regexp (concat diary-glob-file-regexp-prefix regexp))
464 (setq attrvalue nil)
465 (if (re-search-forward regexp (point-max) t)
466 (setq attrvalue (match-string-no-properties regnum)))
467 (if (and attrvalue
468 (setq attrvalue (diary-attrtype-convert attrvalue type)))
469 (setq ret-attr (append ret-attr (list attrname attrvalue))))
470 (setq attr-list (cdr attr-list)))
471 (setq fileglobattrs ret-attr))
472 (progn
473 (setq ret-attr fileglobattrs
474 attr-list diary-face-attrs)
475 (while attr-list
476 (goto-char (point-min))
477 (setq attr (car attr-list)
478 regexp (nth 0 attr)
479 regnum (nth 1 attr)
480 attrname (nth 2 attr)
481 type (nth 3 attr))
482 (setq attrvalue nil)
483 (if (string-match regexp entry)
484 (progn
485 (setq attrvalue (match-string-no-properties regnum entry))
486 (setq entry (replace-match "" t t entry))))
487 (if (and attrvalue
488 (setq attrvalue (diary-attrtype-convert attrvalue type)))
489 (setq ret-attr (append ret-attr (list attrname attrvalue))))
490 (setq attr-list (cdr attr-list)))))
491 (list entry ret-attr))))
493 (defun diary-set-maybe-redraw (symbol value)
494 "Set SYMBOL's value to VALUE, and redraw the diary if necessary.
495 Redraws the diary if it is being displayed (note this is not the same as
496 just visiting the `diary-file'), and SYMBOL's value is to be changed."
497 (let ((oldvalue (eval symbol)))
498 (custom-set-default symbol value)
499 (and (not (equal value oldvalue))
500 (diary-live-p)
501 ;; Note this assumes diary was called without prefix arg.
502 (diary))))
504 ;; This can be removed once the kill/yank treatment of invisible text
505 ;; (see etc/TODO) is fixed. -- gm
506 (defcustom diary-header-line-flag t
507 "If non-nil, `simple-diary-display' will show a header line.
508 The format of the header is specified by `diary-header-line-format'."
509 :group 'diary
510 :type 'boolean
511 :initialize 'custom-initialize-default
512 ;; FIXME overkill.
513 :set 'diary-set-maybe-redraw
514 :version "22.1")
516 (defvar diary-selective-display nil)
518 (defcustom diary-header-line-format
519 '(:eval (calendar-string-spread
520 (list (if diary-selective-display
521 "Selective display active - press \"s\" in calendar \
522 before edit/copy"
523 "Diary"))
524 ?\s (frame-width)))
525 "Format of the header line displayed by `simple-diary-display'.
526 Only used if `diary-header-line-flag' is non-nil."
527 :group 'diary
528 :type 'sexp
529 :initialize 'custom-initialize-default
530 ;; FIXME overkill.
531 :set 'diary-set-maybe-redraw
532 :version "22.1")
534 (defvar diary-saved-point) ; internal
536 ;; The first version of this also checked for diary-selective-display
537 ;; in the non-fancy case. This was an attempt to distinguish between
538 ;; displaying the diary and just visiting the diary file. However,
539 ;; when using fancy diary, calling diary when there are no entries to
540 ;; display does not create the fancy buffer, nor does it switch on
541 ;; selective-display in the diary buffer. This means some
542 ;; customizations will not take effect, eg:
543 ;; http://lists.gnu.org/archive/html/emacs-pretest-bug/2007-03/msg00466.html
544 ;; So the check for selective-display was dropped. This means the
545 ;; diary will be displayed if one customizes a diary variable while
546 ;; just visiting the diary-file. This is i) unlikely, and ii) no great loss.
547 (defun diary-live-p ()
548 "Return non-nil if the diary is being displayed."
549 (or (get-buffer fancy-diary-buffer)
550 (and diary-file
551 (find-buffer-visiting (substitute-in-file-name diary-file)))))
553 (defcustom number-of-diary-entries 1
554 "Specifies how many days of diary entries are to be displayed initially.
555 This variable affects the diary display when the command \\[diary] is used,
556 or if the value of the variable `view-diary-entries-initially' is t. For
557 example, if the default value 1 is used, then only the current day's diary
558 entries will be displayed. If the value 2 is used, then both the current
559 day's and the next day's entries will be displayed.
561 The value can also be a vector such as [0 2 2 2 2 4 1]; this value
562 says to display no diary entries on Sunday, the entries for
563 the current date and the day after on Monday through Thursday,
564 Friday through Monday's entries on Friday, and only Saturday's
565 entries on Saturday.
567 This variable does not affect the diary display with the `d' command
568 from the calendar; in that case, the prefix argument controls the
569 number of days of diary entries displayed."
570 :type '(choice (integer :tag "Entries")
571 (vector :value [0 0 0 0 0 0 0]
572 (integer :tag "Sunday")
573 (integer :tag "Monday")
574 (integer :tag "Tuesday")
575 (integer :tag "Wednesday")
576 (integer :tag "Thursday")
577 (integer :tag "Friday")
578 (integer :tag "Saturday")))
579 :initialize 'custom-initialize-default
580 :set 'diary-set-maybe-redraw
581 :group 'diary)
584 (defvar diary-modify-entry-list-string-function nil
585 "Function applied to entry string before putting it into the entries list.
586 Can be used by programs integrating a diary list into other buffers (e.g.
587 org.el and planner.el) to modify the string or add properties to it.
588 The function takes a string argument and must return a string.")
590 (defun add-to-diary-list (date string specifier &optional marker
591 globcolor literal)
592 "Add an entry to `diary-entries-list'.
593 Do nothing if DATE or STRING is nil. DATE is the (MONTH DAY
594 YEAR) for which the entry applies; STRING is the text of the
595 entry as it will appear in the diary (i.e. with any format
596 strings such as \"%d\" expanded); SPECIFIER is the date part of
597 the entry as it appears in the diary-file; LITERAL is the entry
598 as it appears in the diary-file (i.e. before expansion). If
599 LITERAL is nil, it is taken to be the same as STRING.
601 The entry is added to the list as (DATE STRING SPECIFIER LOCATOR
602 GLOBCOLOR), where LOCATOR has the form (MARKER FILENAME LITERAL),
603 FILENAME being the file containing the diary entry."
604 (when (and date string)
605 (if diary-file-name-prefix
606 (let ((prefix (funcall diary-file-name-prefix-function
607 (buffer-file-name))))
608 (or (string= prefix "")
609 (setq string (format "[%s] %s" prefix string)))))
610 (and diary-modify-entry-list-string-function
611 (setq string (funcall diary-modify-entry-list-string-function
612 string)))
613 (setq diary-entries-list
614 (append diary-entries-list
615 (list (list date string specifier
616 (list marker (buffer-file-name) literal)
617 globcolor))))))
619 (define-obsolete-function-alias 'list-diary-entries 'diary-list-entries)
620 (defun diary-list-entries (date number &optional list-only)
621 "Create and display a buffer containing the relevant lines in `diary-file'.
622 The arguments are DATE and NUMBER; the entries selected are those
623 for NUMBER days starting with date DATE. The other entries are hidden
624 using selective display. If NUMBER is less than 1, this function does nothing.
626 Returns a list of all relevant diary entries found, if any, in order by date.
627 The list entries have the form ((MONTH DAY YEAR) STRING SPECIFIER) where
628 \(MONTH DAY YEAR) is the date of the entry, STRING is the entry text, and
629 SPECIFIER is the applicability. If the variable `diary-list-include-blanks'
630 is t, this list includes a dummy diary entry consisting of the empty string
631 for a date with no diary entries.
633 After the list is prepared, the hooks `nongregorian-diary-listing-hook',
634 `list-diary-entries-hook', `diary-display-hook', and `diary-hook' are run.
635 These hooks have the following distinct roles:
637 `nongregorian-diary-listing-hook' can cull dates from the diary
638 and each included file. Usually used for Hebrew or Islamic
639 diary entries in files. Applied to *each* file.
641 `list-diary-entries-hook' adds or manipulates diary entries from
642 external sources. Used, for example, to include diary entries
643 from other files or to sort the diary entries. Invoked *once* only,
644 before the display hook is run.
646 `diary-display-hook' does the actual display of information. If this is
647 nil, simple-diary-display will be used. Use add-hook to set this to
648 fancy-diary-display, if desired. If you want no diary display, use
649 add-hook to set this to ignore.
651 `diary-hook' is run last. This can be used for an appointment
652 notification function.
654 If LIST-ONLY is non-nil don't modify or display the buffer, only return a list."
655 (unless number
656 (setq number (if (vectorp number-of-diary-entries)
657 (aref number-of-diary-entries (calendar-day-of-week date))
658 number-of-diary-entries)))
659 (when (> number 0)
660 (let ((original-date date);; save for possible use in the hooks
661 diary-entries-list
662 file-glob-attrs
663 (date-string (calendar-date-string date))
664 (d-file (substitute-in-file-name diary-file)))
665 (message "Preparing diary...")
666 (save-excursion
667 (let ((diary-buffer (find-buffer-visiting d-file)))
668 (if (not diary-buffer)
669 (set-buffer (find-file-noselect d-file t))
670 (set-buffer diary-buffer)
671 (or (verify-visited-file-modtime diary-buffer)
672 (revert-buffer t t))))
673 ;; Setup things like the header-line-format and invisibility-spec.
674 (if (eq major-mode default-major-mode)
675 (diary-mode)
676 ;; This kludge is to make customizations to
677 ;; diary-header-line-flag after diary has been displayed
678 ;; take effect. Unconditionally calling (diary-mode)
679 ;; clobbers file local variables.
680 ;; http://lists.gnu.org/archive/html/emacs-pretest-bug/2007-03/msg00363.html
681 ;; http://lists.gnu.org/archive/html/emacs-pretest-bug/2007-04/msg00404.html
682 (if (eq major-mode 'diary-mode)
683 (setq header-line-format (and diary-header-line-flag
684 diary-header-line-format))))
685 ;; d-s-p is passed to the diary display function.
686 (let ((diary-saved-point (point)))
687 (save-excursion
688 (setq file-glob-attrs (nth 1 (diary-pull-attrs nil "")))
689 (with-syntax-table diary-syntax-table
690 (let ((mark (regexp-quote diary-nonmarking-symbol)))
691 (goto-char (point-min))
692 (unless list-only
693 (let ((ol (make-overlay (point-min) (point-max) nil t nil)))
694 (set (make-local-variable 'diary-selective-display) t)
695 (overlay-put ol 'invisible 'diary)
696 (overlay-put ol 'evaporate t)))
697 (dotimes (idummy number)
698 (let ((month (extract-calendar-month date))
699 (day (extract-calendar-day date))
700 (year (extract-calendar-year date))
701 (entry-found (list-sexp-diary-entries date)))
702 (dolist (date-form diary-date-forms)
703 (let*
704 ((backup (when (eq (car date-form) 'backup)
705 (setq date-form (cdr date-form))
707 (dayname
708 (format "%s\\|%s\\.?"
709 (calendar-day-name date)
710 (calendar-day-name date 'abbrev)))
711 (monthname
712 (format "\\*\\|%s\\|%s\\.?"
713 (calendar-month-name month)
714 (calendar-month-name month 'abbrev)))
715 (month (concat "\\*\\|0*" (int-to-string month)))
716 (day (concat "\\*\\|0*" (int-to-string day)))
717 (year
718 (concat
719 "\\*\\|0*" (int-to-string year)
720 (if abbreviated-calendar-year
721 (concat "\\|" (format "%02d" (% year 100)))
722 "")))
723 (regexp
724 (concat
725 "\\(\\`\\|\^M\\|\n\\)" mark "?\\("
726 (mapconcat 'eval date-form "\\)\\(?:")
727 "\\)"))
728 (case-fold-search t))
729 (goto-char (point-min))
730 (while (re-search-forward regexp nil t)
731 (if backup (re-search-backward "\\<" nil t))
732 (if (and (or (char-equal (preceding-char) ?\^M)
733 (char-equal (preceding-char) ?\n))
734 (not (looking-at " \\|\^I")))
735 ;; Diary entry that consists only of date.
736 (backward-char 1)
737 ;; Found a nonempty diary entry--make it
738 ;; visible and add it to the list.
739 (setq entry-found t)
740 (let ((entry-start (point))
741 date-start temp)
742 (re-search-backward "\^M\\|\n\\|\\`")
743 (setq date-start (point))
744 ;; When selective display (rather than
745 ;; overlays) was used, diary file used to
746 ;; start in a blank line and end in a
747 ;; newline. Now that neither of these
748 ;; need be true, 'move handles the latter
749 ;; and 1/2 kludge the former.
750 (re-search-forward
751 "\^M\\|\n" nil 'move
752 (if (and (bobp) (not (looking-at "\^M\\|\n")))
755 (while (looking-at " \\|\^I")
756 (re-search-forward "\^M\\|\n" nil 'move))
757 (unless (and (eobp) (not (bolp)))
758 (backward-char 1))
759 (unless list-only
760 (remove-overlays date-start (point)
761 'invisible 'diary))
762 (setq entry (buffer-substring entry-start (point))
763 temp (diary-pull-attrs entry file-glob-attrs)
764 entry (nth 0 temp))
765 (add-to-diary-list
766 date
767 entry
768 (buffer-substring
769 (1+ date-start) (1- entry-start))
770 (copy-marker entry-start) (nth 1 temp)))))))
771 (or entry-found
772 (not diary-list-include-blanks)
773 (add-to-diary-list date "" "" "" ""))
774 (setq date
775 (calendar-gregorian-from-absolute
776 (1+ (calendar-absolute-from-gregorian date))))
777 (setq entry-found nil)))))
778 (goto-char (point-min))
779 (run-hooks 'nongregorian-diary-listing-hook
780 'list-diary-entries-hook)
781 (unless list-only
782 (if diary-display-hook
783 (run-hooks 'diary-display-hook)
784 (simple-diary-display)))
785 (run-hooks 'diary-hook)
786 diary-entries-list))))))
788 (defun diary-unhide-everything ()
789 (kill-local-variable 'diary-selective-display)
790 (remove-overlays (point-min) (point-max) 'invisible 'diary)
791 (kill-local-variable 'mode-line-format))
793 (defun include-other-diary-files ()
794 "Include the diary entries from other diary files with those of diary-file.
795 This function is suitable for use in `list-diary-entries-hook';
796 it enables you to use shared diary files together with your own.
797 The files included are specified in the diaryfile by lines of this form:
798 #include \"filename\"
799 This is recursive; that is, #include directives in diary files thus included
800 are obeyed. You can change the `#include' to some other string by
801 changing the variable `diary-include-string'."
802 (goto-char (point-min))
803 (while (re-search-forward
804 (concat
805 "\\(?:\\`\\|\^M\\|\n\\)"
806 (regexp-quote diary-include-string)
807 " \"\\([^\"]*\\)\"")
808 nil t)
809 (let* ((diary-file (substitute-in-file-name
810 (match-string-no-properties 1)))
811 (diary-list-include-blanks nil)
812 (list-diary-entries-hook 'include-other-diary-files)
813 (diary-display-hook 'ignore)
814 (diary-hook nil))
815 (if (file-exists-p diary-file)
816 (if (file-readable-p diary-file)
817 (unwind-protect
818 (setq diary-entries-list
819 (append diary-entries-list
820 (diary-list-entries original-date number)))
821 (with-current-buffer (find-buffer-visiting diary-file)
822 (diary-unhide-everything)))
823 (beep)
824 (message "Can't read included diary file %s" diary-file)
825 (sleep-for 2))
826 (beep)
827 (message "Can't find included diary file %s" diary-file)
828 (sleep-for 2))))
829 (goto-char (point-min)))
831 (defun simple-diary-display ()
832 "Display the diary buffer if there are any relevant entries or holidays."
833 (let* ((holiday-list (if holidays-in-diary-buffer
834 (calendar-check-holidays original-date)))
835 (hol-string (format "%s%s%s"
836 date-string
837 (if holiday-list ": " "")
838 (mapconcat 'identity holiday-list "; ")))
839 (msg (format "No diary entries for %s" hol-string))
840 ;; If selected window is dedicated (to the calendar),
841 ;; need a new one to display the diary.
842 (pop-up-frames (window-dedicated-p (selected-window))))
843 (calendar-set-mode-line (format "Diary for %s" hol-string))
844 (if (or (not diary-entries-list)
845 (and (not (cdr diary-entries-list))
846 (string-equal (car (cdr (car diary-entries-list))) "")))
847 (if (< (length msg) (frame-width))
848 (message "%s" msg)
849 (set-buffer (get-buffer-create holiday-buffer))
850 (setq buffer-read-only nil)
851 (calendar-set-mode-line date-string)
852 (erase-buffer)
853 (insert (mapconcat 'identity holiday-list "\n"))
854 (goto-char (point-min))
855 (set-buffer-modified-p nil)
856 (setq buffer-read-only t)
857 (display-buffer holiday-buffer)
858 (message "No diary entries for %s" date-string))
859 (with-current-buffer
860 (find-buffer-visiting (substitute-in-file-name diary-file))
861 (let ((window (display-buffer (current-buffer))))
862 ;; d-s-p is passed from list-diary-entries.
863 (set-window-point window diary-saved-point)
864 (set-window-start window (point-min))))
865 (message "Preparing diary...done"))))
867 (defface diary-button '((((type pc) (class color))
868 (:foreground "lightblue")))
869 "Default face used for buttons."
870 :version "22.1"
871 :group 'diary)
872 ;; backward-compatibility alias
873 (put 'diary-button-face 'face-alias 'diary-button)
875 (define-button-type 'diary-entry
876 'action #'diary-goto-entry
877 'face 'diary-button)
879 (defun diary-goto-entry (button)
880 (let* ((locator (button-get button 'locator))
881 (marker (car locator))
882 markbuf file)
883 ;; If marker pointing to diary location is valid, use that.
884 (if (and marker (setq markbuf (marker-buffer marker)))
885 (progn
886 (pop-to-buffer markbuf)
887 (goto-char (marker-position marker)))
888 ;; Marker is invalid (eg buffer has been killed).
889 (or (and (setq file (cadr locator))
890 (file-exists-p file)
891 (find-file-other-window file)
892 (progn
893 (when (eq major-mode default-major-mode) (diary-mode))
894 (goto-char (point-min))
895 (if (re-search-forward (format "%s.*\\(%s\\)"
896 (regexp-quote (nth 2 locator))
897 (regexp-quote (nth 3 locator)))
898 nil t)
899 (goto-char (match-beginning 1)))))
900 (message "Unable to locate this diary entry")))))
902 (defun fancy-diary-display ()
903 "Prepare a diary buffer with relevant entries in a fancy, noneditable form.
904 This function is provided for optional use as the `diary-display-hook'."
905 (with-current-buffer ;; Turn off selective-display in the diary file's buffer.
906 (find-buffer-visiting (substitute-in-file-name diary-file))
907 (diary-unhide-everything))
908 (if (or (not diary-entries-list)
909 (and (not (cdr diary-entries-list))
910 (string-equal (car (cdr (car diary-entries-list))) "")))
911 (let* ((holiday-list (if holidays-in-diary-buffer
912 (calendar-check-holidays original-date)))
913 (msg (format "No diary entries for %s %s"
914 (concat date-string (if holiday-list ":" ""))
915 (mapconcat 'identity holiday-list "; "))))
916 (if (<= (length msg) (frame-width))
917 (message "%s" msg)
918 (set-buffer (get-buffer-create holiday-buffer))
919 (setq buffer-read-only nil)
920 (erase-buffer)
921 (insert (mapconcat 'identity holiday-list "\n"))
922 (goto-char (point-min))
923 (set-buffer-modified-p nil)
924 (setq buffer-read-only t)
925 (display-buffer holiday-buffer)
926 (message "No diary entries for %s" date-string)))
927 (with-current-buffer;; Prepare the fancy diary buffer.
928 (make-fancy-diary-buffer)
929 (setq buffer-read-only nil)
930 (let ((entry-list diary-entries-list)
931 (holiday-list)
932 (holiday-list-last-month 1)
933 (holiday-list-last-year 1)
934 (date (list 0 0 0)))
935 (while entry-list
936 (if (not (calendar-date-equal date (car (car entry-list))))
937 (progn
938 (setq date (car (car entry-list)))
939 (and holidays-in-diary-buffer
940 (calendar-date-compare
941 (list (list holiday-list-last-month
942 (calendar-last-day-of-month
943 holiday-list-last-month
944 holiday-list-last-year)
945 holiday-list-last-year))
946 (list date))
947 ;; We need to get the holidays for the next 3 months.
948 (setq holiday-list-last-month
949 (extract-calendar-month date))
950 (setq holiday-list-last-year
951 (extract-calendar-year date))
952 (progn
953 (increment-calendar-month
954 holiday-list-last-month holiday-list-last-year 1)
956 (setq holiday-list
957 (let ((displayed-month holiday-list-last-month)
958 (displayed-year holiday-list-last-year))
959 (calendar-holiday-list)))
960 (increment-calendar-month
961 holiday-list-last-month holiday-list-last-year 1))
962 (let* ((date-string (calendar-date-string date))
963 (date-holiday-list
964 (let ((h holiday-list)
965 (d))
966 ;; Make a list of all holidays for date.
967 (while h
968 (if (calendar-date-equal date (car (car h)))
969 (setq d (append d (cdr (car h)))))
970 (setq h (cdr h)))
971 d)))
972 (insert (if (= (point) (point-min)) "" ?\n) date-string)
973 (if date-holiday-list (insert ": "))
974 (let* ((l (current-column))
975 (longest 0))
976 (insert (mapconcat (lambda (x)
977 (if (< longest (length x))
978 (setq longest (length x)))
980 date-holiday-list
981 (concat "\n" (make-string l ? ))))
982 (insert ?\n (make-string (+ l longest) ?=) ?\n)))))
984 (setq entry (car (cdr (car entry-list))))
985 (if (< 0 (length entry))
986 (let ((this-entry (car entry-list))
987 this-loc)
988 (if (setq this-loc (nth 3 this-entry))
989 (insert-button (concat entry "\n")
990 ;; (MARKER FILENAME SPECIFIER LITERAL)
991 'locator (list (car this-loc)
992 (cadr this-loc)
993 (nth 2 this-entry)
994 (or (nth 2 this-loc)
995 (nth 1 this-entry)))
996 :type 'diary-entry)
997 (insert entry ?\n))
998 (save-excursion
999 (let* ((marks (nth 4 this-entry))
1000 (faceinfo marks)
1001 temp-face)
1002 (when marks
1003 (setq temp-face (make-symbol
1004 (apply
1005 'concat "temp-face-"
1006 (mapcar (lambda (sym)
1007 (if (stringp sym)
1009 (symbol-name sym)))
1010 marks))))
1011 (make-face temp-face)
1012 ;; Remove :face info from the marks,
1013 ;; copy the face info into temp-face
1014 (while (setq faceinfo (memq :face faceinfo))
1015 (copy-face (read (nth 1 faceinfo)) temp-face)
1016 (setcar faceinfo nil)
1017 (setcar (cdr faceinfo) nil))
1018 (setq marks (delq nil marks))
1019 ;; Apply the font aspects.
1020 (apply 'set-face-attribute temp-face nil marks)
1021 (search-backward entry)
1022 (overlay-put
1023 (make-overlay (match-beginning 0) (match-end 0))
1024 'face temp-face))))))
1025 (setq entry-list (cdr entry-list))))
1026 (set-buffer-modified-p nil)
1027 (goto-char (point-min))
1028 (setq buffer-read-only t)
1029 (display-buffer fancy-diary-buffer)
1030 (fancy-diary-display-mode)
1031 (calendar-set-mode-line date-string)
1032 (message "Preparing diary...done"))))
1034 (defun make-fancy-diary-buffer ()
1035 "Create and return the initial fancy diary buffer."
1036 (with-current-buffer (get-buffer-create fancy-diary-buffer)
1037 (setq buffer-read-only nil)
1038 (calendar-set-mode-line "Diary Entries")
1039 (erase-buffer)
1040 (set-buffer-modified-p nil)
1041 (setq buffer-read-only t)
1042 (get-buffer fancy-diary-buffer)))
1044 (defun print-diary-entries ()
1045 "Print a hard copy of the diary display.
1047 If the simple diary display is being used, prepare a temp buffer with the
1048 visible lines of the diary buffer, add a heading line composed from the mode
1049 line, print the temp buffer, and destroy it.
1051 If the fancy diary display is being used, just print the buffer.
1053 The hooks given by the variable `print-diary-entries-hook' are called to do
1054 the actual printing."
1055 (interactive)
1056 (if (bufferp (get-buffer fancy-diary-buffer))
1057 (with-current-buffer (get-buffer fancy-diary-buffer)
1058 (run-hooks 'print-diary-entries-hook))
1059 (let ((diary-buffer
1060 (find-buffer-visiting (substitute-in-file-name diary-file))))
1061 (if diary-buffer
1062 (let ((temp-buffer (get-buffer-create " *Printable Diary Entries*"))
1063 (heading))
1064 (with-current-buffer diary-buffer
1065 (setq heading
1066 (if (not (stringp mode-line-format))
1067 "All Diary Entries"
1068 (string-match "^-*\\([^-].*[^-]\\)-*$" mode-line-format)
1069 (match-string 1 mode-line-format)))
1070 (let ((start (point-min))
1071 end)
1072 (while
1073 (progn
1074 (setq end (next-single-char-property-change
1075 start 'invisible))
1076 (if (get-char-property start 'invisible)
1078 (with-current-buffer temp-buffer
1079 (insert-buffer-substring diary-buffer
1080 start (or end (point-max)))))
1081 (setq start end)
1082 (and end (< end (point-max))))))
1083 (set-buffer temp-buffer)
1084 (goto-char (point-min))
1085 (insert heading "\n"
1086 (make-string (length heading) ?=) "\n")
1087 (run-hooks 'print-diary-entries-hook)
1088 (kill-buffer temp-buffer)))
1089 (error "You don't have a diary buffer!")))))
1091 (define-obsolete-function-alias 'show-all-diary-entries 'diary-show-all-entries)
1092 (defun diary-show-all-entries ()
1093 "Show all of the diary entries in the diary file.
1094 This function gets rid of the selective display of the diary file so that
1095 all entries, not just some, are visible. If there is no diary buffer, one
1096 is created."
1097 (interactive)
1098 (let ((d-file (diary-check-diary-file))
1099 (pop-up-frames (window-dedicated-p (selected-window))))
1100 (with-current-buffer (or (find-buffer-visiting d-file)
1101 (find-file-noselect d-file t))
1102 (when (eq major-mode default-major-mode) (diary-mode))
1103 (diary-unhide-everything)
1104 (display-buffer (current-buffer)))))
1106 ;;;###autoload
1107 (defun diary-mail-entries (&optional ndays)
1108 "Send a mail message showing diary entries for next NDAYS days.
1109 If no prefix argument is given, NDAYS is set to `diary-mail-days'.
1110 Mail is sent to the address specified by `diary-mail-addr'.
1112 You can call `diary-mail-entries' every night using an at/cron job.
1113 For example, this script will run the program at 2am daily. Since
1114 `emacs -batch' does not load your `.emacs' file, you must ensure that
1115 all relevant variables are set, as done here.
1117 #!/bin/sh
1118 # diary-rem.sh -- repeatedly run the Emacs diary-reminder
1119 emacs -batch \\
1120 -eval \"(setq diary-mail-days 3 \\
1121 diary-file \\\"/path/to/diary.file\\\" \\
1122 european-calendar-style t \\
1123 diary-mail-addr \\\"user@host.name\\\" )\" \\
1124 -l diary-lib -f diary-mail-entries
1125 at -f diary-rem.sh 0200 tomorrow
1127 You may have to tweak the syntax of the `at' command to suit your
1128 system. Alternatively, you can specify a cron entry:
1129 0 1 * * * diary-rem.sh
1130 to run it every morning at 1am."
1131 (interactive "P")
1132 (if (string-equal diary-mail-addr "")
1133 (error "You must set `diary-mail-addr' to use this command")
1134 (let ((diary-display-hook 'fancy-diary-display))
1135 (diary-list-entries (calendar-current-date) (or ndays diary-mail-days)))
1136 (compose-mail diary-mail-addr
1137 (concat "Diary entries generated "
1138 (calendar-date-string (calendar-current-date))))
1139 (insert
1140 (if (get-buffer fancy-diary-buffer)
1141 (with-current-buffer fancy-diary-buffer (buffer-string))
1142 "No entries found"))
1143 (call-interactively (get mail-user-agent 'sendfunc))))
1145 (defun diary-name-pattern (string-array &optional abbrev-array paren)
1146 "Return a regexp matching the strings in the array STRING-ARRAY.
1147 If the optional argument ABBREV-ARRAY is present, then the function
1148 `calendar-abbrev-construct' is used to construct abbreviations from the
1149 two supplied arrays. The returned regexp will then also match these
1150 abbreviations, with or without final `.' characters. If the optional
1151 argument PAREN is non-nil, the regexp is surrounded by parentheses."
1152 (regexp-opt (append string-array
1153 (if abbrev-array
1154 (calendar-abbrev-construct abbrev-array
1155 string-array))
1156 (if abbrev-array
1157 (calendar-abbrev-construct abbrev-array
1158 string-array
1159 'period))
1160 nil)
1161 paren))
1163 (defvar marking-diary-entries nil
1164 "True during the marking of diary entries, nil otherwise.")
1166 (defvar marking-diary-entry nil
1167 "True during the marking of diary entries, if current entry is marking.")
1169 (defun mark-diary-entries (&optional redraw)
1170 "Mark days in the calendar window that have diary entries.
1171 Each entry in the diary file visible in the calendar window is
1172 marked. After the entries are marked, the hooks
1173 `nongregorian-diary-marking-hook' and `mark-diary-entries-hook'
1174 are run. If the optional argument REDRAW is non-nil (which is
1175 the case interactively, for example) then any existing diary
1176 marks are first removed. This is intended to deal with deleted
1177 diary entries."
1178 (interactive "p")
1179 ;; To remove any deleted diary entries. Do not redraw when:
1180 ;; i) processing #include diary files (else only get the marks from
1181 ;; the last #include file processed).
1182 ;; ii) called via calendar-redraw (since calendar has already been
1183 ;; erased).
1184 ;; Use of REDRAW handles both of these cases.
1185 (when (and redraw mark-diary-entries-in-calendar)
1186 (setq mark-diary-entries-in-calendar nil)
1187 (redraw-calendar))
1188 (let ((marking-diary-entries t)
1189 file-glob-attrs marks)
1190 (with-current-buffer (find-file-noselect (diary-check-diary-file) t)
1191 (save-excursion
1192 (when (eq major-mode default-major-mode) (diary-mode))
1193 (setq mark-diary-entries-in-calendar t)
1194 (message "Marking diary entries...")
1195 (setq file-glob-attrs (nth 1 (diary-pull-attrs nil '())))
1196 (with-syntax-table diary-syntax-table
1197 (dolist (date-form diary-date-forms)
1198 (if (eq (car date-form) 'backup)
1199 (setq date-form (cdr date-form))) ;; ignore 'backup directive
1200 (let* ((dayname
1201 (diary-name-pattern calendar-day-name-array
1202 calendar-day-abbrev-array))
1203 (monthname
1204 (format "%s\\|\\*"
1205 (diary-name-pattern calendar-month-name-array
1206 calendar-month-abbrev-array)))
1207 (month "[0-9]+\\|\\*")
1208 (day "[0-9]+\\|\\*")
1209 (year "[0-9]+\\|\\*")
1210 (l (length date-form))
1211 (d-name-pos (- l (length (memq 'dayname date-form))))
1212 (d-name-pos (if (/= l d-name-pos) (+ 2 d-name-pos)))
1213 (m-name-pos (- l (length (memq 'monthname date-form))))
1214 (m-name-pos (if (/= l m-name-pos) (+ 2 m-name-pos)))
1215 (d-pos (- l (length (memq 'day date-form))))
1216 (d-pos (if (/= l d-pos) (+ 2 d-pos)))
1217 (m-pos (- l (length (memq 'month date-form))))
1218 (m-pos (if (/= l m-pos) (+ 2 m-pos)))
1219 (y-pos (- l (length (memq 'year date-form))))
1220 (y-pos (if (/= l y-pos) (+ 2 y-pos)))
1221 (regexp
1222 (concat
1223 "\\(\\`\\|\^M\\|\n\\)\\("
1224 (mapconcat 'eval date-form "\\)\\(")
1225 "\\)"))
1226 (case-fold-search t))
1227 (goto-char (point-min))
1228 (while (re-search-forward regexp nil t)
1229 (let* ((dd-name
1230 (if d-name-pos
1231 (match-string-no-properties d-name-pos)))
1232 (mm-name
1233 (if m-name-pos
1234 (match-string-no-properties m-name-pos)))
1235 (mm (string-to-number
1236 (if m-pos
1237 (match-string-no-properties m-pos)
1238 "")))
1239 (dd (string-to-number
1240 (if d-pos
1241 (match-string-no-properties d-pos)
1242 "")))
1243 (y-str (if y-pos
1244 (match-string-no-properties y-pos)))
1245 (yy (if (not y-str)
1247 (if (and (= (length y-str) 2)
1248 abbreviated-calendar-year)
1249 (let* ((current-y
1250 (extract-calendar-year
1251 (calendar-current-date)))
1252 (y (+ (string-to-number y-str)
1253 (* 100
1254 (/ current-y 100)))))
1255 (if (> (- y current-y) 50)
1256 (- y 100)
1257 (if (> (- current-y y) 50)
1258 (+ y 100)
1259 y)))
1260 (string-to-number y-str)))))
1261 (let ((tmp (diary-pull-attrs (buffer-substring-no-properties
1262 (point) (line-end-position))
1263 file-glob-attrs)))
1264 (setq entry (nth 0 tmp)
1265 marks (nth 1 tmp)))
1266 (if dd-name
1267 (mark-calendar-days-named
1268 (cdr (assoc-string
1269 dd-name
1270 (calendar-make-alist
1271 calendar-day-name-array
1272 0 nil calendar-day-abbrev-array) t)) marks)
1273 (if mm-name
1274 (setq mm
1275 (if (string-equal mm-name "*") 0
1276 (cdr (assoc-string
1277 mm-name
1278 (calendar-make-alist
1279 calendar-month-name-array
1280 1 nil calendar-month-abbrev-array) t)))))
1281 (mark-calendar-date-pattern mm dd yy marks))))))
1282 (mark-sexp-diary-entries)
1283 (run-hooks 'nongregorian-diary-marking-hook
1284 'mark-diary-entries-hook))
1285 (message "Marking diary entries...done")))))
1287 (defun mark-sexp-diary-entries ()
1288 "Mark days in the calendar window that have sexp diary entries.
1289 Each entry in the diary file (or included files) visible in the calendar window
1290 is marked. See the documentation for the function `list-sexp-diary-entries'."
1291 (let* ((sexp-mark (regexp-quote sexp-diary-entry-symbol))
1292 (s-entry (concat "\\(\\`\\|\^M\\|\n\\)\\("
1293 sexp-mark "(\\)\\|\\("
1294 (regexp-quote diary-nonmarking-symbol)
1295 sexp-mark "(diary-remind\\)"))
1296 (file-glob-attrs (nth 1 (diary-pull-attrs nil '())))
1297 m y first-date last-date mark file-glob-attrs)
1298 (with-current-buffer calendar-buffer
1299 (setq m displayed-month)
1300 (setq y displayed-year))
1301 (increment-calendar-month m y -1)
1302 (setq first-date
1303 (calendar-absolute-from-gregorian (list m 1 y)))
1304 (increment-calendar-month m y 2)
1305 (setq last-date
1306 (calendar-absolute-from-gregorian
1307 (list m (calendar-last-day-of-month m y) y)))
1308 (goto-char (point-min))
1309 (while (re-search-forward s-entry nil t)
1310 (setq marking-diary-entry (char-equal (preceding-char) ?\())
1311 (re-search-backward "(")
1312 (let ((sexp-start (point))
1313 sexp entry entry-start line-start marks)
1314 (forward-sexp)
1315 (setq sexp (buffer-substring-no-properties sexp-start (point)))
1316 (save-excursion
1317 (re-search-backward "\^M\\|\n\\|\\`")
1318 (setq line-start (point)))
1319 (forward-char 1)
1320 (if (and (or (char-equal (preceding-char) ?\^M)
1321 (char-equal (preceding-char) ?\n))
1322 (not (looking-at " \\|\^I")))
1323 (progn;; Diary entry consists only of the sexp
1324 (backward-char 1)
1325 (setq entry ""))
1326 (setq entry-start (point))
1327 ;; Find end of entry
1328 (re-search-forward "\^M\\|\n" nil t)
1329 (while (looking-at " \\|\^I")
1330 (or (re-search-forward "\^M\\|\n" nil t)
1331 (re-search-forward "$" nil t)))
1332 (if (or (char-equal (preceding-char) ?\^M)
1333 (char-equal (preceding-char) ?\n))
1334 (backward-char 1))
1335 (setq entry (buffer-substring-no-properties entry-start (point)))
1336 (while (string-match "[\^M]" entry)
1337 (aset entry (match-beginning 0) ?\n )))
1338 (calendar-for-loop date from first-date to last-date do
1339 (if (setq mark (diary-sexp-entry sexp entry
1340 (calendar-gregorian-from-absolute date)))
1341 (progn
1342 (setq marks (diary-pull-attrs entry file-glob-attrs)
1343 marks (nth 1 (diary-pull-attrs entry file-glob-attrs)))
1344 (mark-visible-calendar-date
1345 (calendar-gregorian-from-absolute date)
1346 (if (< 0 (length marks))
1347 marks
1348 (if (consp mark)
1349 (car mark)))))))))))
1351 (defun mark-included-diary-files ()
1352 "Mark the diary entries from other diary files with those of the diary file.
1353 This function is suitable for use as the `mark-diary-entries-hook'; it enables
1354 you to use shared diary files together with your own. The files included are
1355 specified in the diary-file by lines of this form:
1356 #include \"filename\"
1357 This is recursive; that is, #include directives in diary files thus included
1358 are obeyed. You can change the `#include' to some other string by
1359 changing the variable `diary-include-string'."
1360 (goto-char (point-min))
1361 (while (re-search-forward
1362 (concat
1363 "\\(?:\\`\\|\^M\\|\n\\)"
1364 (regexp-quote diary-include-string)
1365 " \"\\([^\"]*\\)\"")
1366 nil t)
1367 (let* ((diary-file (substitute-in-file-name
1368 (match-string-no-properties 1)))
1369 (mark-diary-entries-hook 'mark-included-diary-files)
1370 (dbuff (find-buffer-visiting diary-file)))
1371 (if (file-exists-p diary-file)
1372 (if (file-readable-p diary-file)
1373 (progn
1374 (mark-diary-entries)
1375 (unless dbuff
1376 (kill-buffer (find-buffer-visiting diary-file))))
1377 (beep)
1378 (message "Can't read included diary file %s" diary-file)
1379 (sleep-for 2))
1380 (beep)
1381 (message "Can't find included diary file %s" diary-file)
1382 (sleep-for 2))))
1383 (goto-char (point-min)))
1385 (defun mark-calendar-days-named (dayname &optional color)
1386 "Mark all dates in the calendar window that are day DAYNAME of the week.
1387 0 means all Sundays, 1 means all Mondays, and so on."
1388 (with-current-buffer calendar-buffer
1389 (let ((prev-month displayed-month)
1390 (prev-year displayed-year)
1391 (succ-month displayed-month)
1392 (succ-year displayed-year)
1393 (last-day)
1394 (day))
1395 (increment-calendar-month succ-month succ-year 1)
1396 (increment-calendar-month prev-month prev-year -1)
1397 (setq day (calendar-absolute-from-gregorian
1398 (calendar-nth-named-day 1 dayname prev-month prev-year)))
1399 (setq last-day (calendar-absolute-from-gregorian
1400 (calendar-nth-named-day -1 dayname succ-month succ-year)))
1401 (while (<= day last-day)
1402 (mark-visible-calendar-date (calendar-gregorian-from-absolute day) color)
1403 (setq day (+ day 7))))))
1405 (defun mark-calendar-date-pattern (month day year &optional color)
1406 "Mark all dates in the calendar window that conform to MONTH/DAY/YEAR.
1407 A value of 0 in any position is a wildcard."
1408 (with-current-buffer calendar-buffer
1409 (let ((m displayed-month)
1410 (y displayed-year))
1411 (increment-calendar-month m y -1)
1412 (dotimes (idummy 3)
1413 (mark-calendar-month m y month day year color)
1414 (increment-calendar-month m y 1)))))
1416 (defun mark-calendar-month (month year p-month p-day p-year &optional color)
1417 "Mark dates in the MONTH/YEAR that conform to pattern P-MONTH/P_DAY/P-YEAR.
1418 A value of 0 in any position of the pattern is a wildcard."
1419 (if (or (and (= month p-month)
1420 (or (= p-year 0) (= year p-year)))
1421 (and (= p-month 0)
1422 (or (= p-year 0) (= year p-year))))
1423 (if (= p-day 0)
1424 (calendar-for-loop
1425 i from 1 to (calendar-last-day-of-month month year) do
1426 (mark-visible-calendar-date (list month i year) color))
1427 (mark-visible-calendar-date (list month p-day year) color))))
1429 (defun sort-diary-entries ()
1430 "Sort the list of diary entries by time of day."
1431 (setq diary-entries-list (sort diary-entries-list 'diary-entry-compare)))
1433 (defun diary-entry-compare (e1 e2)
1434 "Returns t if E1 is earlier than E2."
1435 (or (calendar-date-compare e1 e2)
1436 (and (calendar-date-equal (car e1) (car e2))
1437 (let* ((ts1 (cadr e1)) (t1 (diary-entry-time ts1))
1438 (ts2 (cadr e2)) (t2 (diary-entry-time ts2)))
1439 (or (< t1 t2)
1440 (and (= t1 t2)
1441 (string-lessp ts1 ts2)))))))
1443 (defun diary-entry-time (s)
1444 "Return time at the beginning of the string S as a military-style integer.
1445 For example, returns 1325 for 1:25pm.
1447 Returns `diary-unknown-time' (default value -9999) if no time is recognized.
1448 The recognized forms are XXXX, X:XX, or XX:XX (military time), and XXam,
1449 XXAM, XXpm, XXPM, XX:XXam, XX:XXAM XX:XXpm, or XX:XXPM. A period (.) can
1450 be used instead of a colon (:) to separate the hour and minute parts."
1451 (let ((case-fold-search nil))
1452 (cond ((string-match ; Military time
1453 "\\`[ \t\n\\^M]*\\([0-9]?[0-9]\\)[:.]?\\([0-9][0-9]\\)\\(\\>\\|[^ap]\\)"
1455 (+ (* 100 (string-to-number (match-string 1 s)))
1456 (string-to-number (match-string 2 s))))
1457 ((string-match ; Hour only XXam or XXpm
1458 "\\`[ \t\n\\^M]*\\([0-9]?[0-9]\\)\\([ap]\\)m\\>" s)
1459 (+ (* 100 (% (string-to-number (match-string 1 s)) 12))
1460 (if (equal ?a (downcase (aref s (match-beginning 2))))
1461 0 1200)))
1462 ((string-match ; Hour and minute XX:XXam or XX:XXpm
1463 "\\`[ \t\n\\^M]*\\([0-9]?[0-9]\\)[:.]\\([0-9][0-9]\\)\\([ap]\\)m\\>" s)
1464 (+ (* 100 (% (string-to-number (match-string 1 s)) 12))
1465 (string-to-number (match-string 2 s))
1466 (if (equal ?a (downcase (aref s (match-beginning 3))))
1467 0 1200)))
1468 (t diary-unknown-time)))) ; Unrecognizable
1470 ;; Unrecognizable
1472 (defun list-sexp-diary-entries (date)
1473 "Add sexp entries for DATE from the diary file to `diary-entries-list'.
1474 Also, Make them visible in the diary file. Returns t if any entries were
1475 found.
1477 Sexp diary entries must be prefaced by a `sexp-diary-entry-symbol' (normally
1478 `%%'). The form of a sexp diary entry is
1480 %%(SEXP) ENTRY
1482 Both ENTRY and DATE are globally available when the SEXP is evaluated. If the
1483 SEXP yields the value nil, the diary entry does not apply. If it yields a
1484 non-nil value, ENTRY will be taken to apply to DATE; if the non-nil value is a
1485 string, that string will be the diary entry in the fancy diary display.
1487 For example, the following diary entry will apply to the 21st of the month
1488 if it is a weekday and the Friday before if the 21st is on a weekend:
1490 &%%(let ((dayname (calendar-day-of-week date))
1491 (day (extract-calendar-day date)))
1493 (and (= day 21) (memq dayname '(1 2 3 4 5)))
1494 (and (memq day '(19 20)) (= dayname 5)))
1495 ) UIUC pay checks deposited
1497 A number of built-in functions are available for this type of diary entry:
1499 %%(diary-date MONTH DAY YEAR &optional MARK) text
1500 Entry applies if date is MONTH, DAY, YEAR if
1501 `european-calendar-style' is nil, and DAY, MONTH, YEAR if
1502 `european-calendar-style' is t. DAY, MONTH, and YEAR
1503 can be lists of integers, the constant t, or an integer.
1504 The constant t means all values. An optional parameter
1505 MARK specifies a face or single-character string to use
1506 when highlighting the day in the calendar.
1508 %%(diary-float MONTH DAYNAME N &optional DAY MARK) text
1509 Entry will appear on the Nth DAYNAME of MONTH.
1510 (DAYNAME=0 means Sunday, 1 means Monday, and so on;
1511 if N is negative it counts backward from the end of
1512 the month. MONTH can be a list of months, a single
1513 month, or t to specify all months. Optional DAY means
1514 Nth DAYNAME of MONTH on or after/before DAY. DAY defaults
1515 to 1 if N>0 and the last day of the month if N<0. An
1516 optional parameter MARK specifies a face or single-character
1517 string to use when highlighting the day in the calendar.
1519 %%(diary-block M1 D1 Y1 M2 D2 Y2 &optional MARK) text
1520 Entry will appear on dates between M1/D1/Y1 and M2/D2/Y2,
1521 inclusive. (If `european-calendar-style' is t, the
1522 order of the parameters should be changed to D1, M1, Y1,
1523 D2, M2, Y2.) An optional parameter MARK specifies a face
1524 or single-character string to use when highlighting the
1525 day in the calendar.
1527 %%(diary-anniversary MONTH DAY YEAR &optional MARK) text
1528 Entry will appear on anniversary dates of MONTH DAY, YEAR.
1529 (If `european-calendar-style' is t, the order of the
1530 parameters should be changed to DAY, MONTH, YEAR.) Text
1531 can contain %d or %d%s; %d will be replaced by the number
1532 of years since the MONTH DAY, YEAR and %s will be replaced
1533 by the ordinal ending of that number (that is, `st', `nd',
1534 `rd' or `th', as appropriate. The anniversary of February
1535 29 is considered to be March 1 in a non-leap year. An
1536 optional parameter MARK specifies a face or single-character
1537 string to use when highlighting the day in the calendar.
1539 %%(diary-cyclic N MONTH DAY YEAR &optional MARK) text
1540 Entry will appear every N days, starting MONTH DAY, YEAR.
1541 (If `european-calendar-style' is t, the order of the
1542 parameters should be changed to N, DAY, MONTH, YEAR.) Text
1543 can contain %d or %d%s; %d will be replaced by the number
1544 of repetitions since the MONTH DAY, YEAR and %s will
1545 be replaced by the ordinal ending of that number (that is,
1546 `st', `nd', `rd' or `th', as appropriate. An optional
1547 parameter MARK specifies a face or single-character string
1548 to use when highlighting the day in the calendar.
1550 %%(diary-remind SEXP DAYS &optional MARKING) text
1551 Entry is a reminder for diary sexp SEXP. DAYS is either a
1552 single number or a list of numbers indicating the number(s)
1553 of days before the event that the warning(s) should occur.
1554 If the current date is (one of) DAYS before the event
1555 indicated by EXPR, then a suitable message (as specified
1556 by `diary-remind-message') appears. In addition to the
1557 reminders beforehand, the diary entry also appears on
1558 the date itself. If optional MARKING is non-nil then the
1559 *reminders* are marked on the calendar. Marking of
1560 reminders is independent of whether the entry *itself* is
1561 a marking or nonmarking one.
1563 %%(diary-day-of-year)
1564 Diary entries giving the day of the year and the number of
1565 days remaining in the year will be made every day. Note
1566 that since there is no text, it makes sense only if the
1567 fancy diary display is used.
1569 %%(diary-iso-date)
1570 Diary entries giving the corresponding ISO commercial date
1571 will be made every day. Note that since there is no text,
1572 it makes sense only if the fancy diary display is used.
1574 %%(diary-french-date)
1575 Diary entries giving the corresponding French Revolutionary
1576 date will be made every day. Note that since there is no
1577 text, it makes sense only if the fancy diary display is used.
1579 %%(diary-islamic-date)
1580 Diary entries giving the corresponding Islamic date will be
1581 made every day. Note that since there is no text, it
1582 makes sense only if the fancy diary display is used.
1584 %%(diary-hebrew-date)
1585 Diary entries giving the corresponding Hebrew date will be
1586 made every day. Note that since there is no text, it
1587 makes sense only if the fancy diary display is used.
1589 %%(diary-astro-day-number) Diary entries giving the corresponding
1590 astronomical (Julian) day number will be made every day.
1591 Note that since there is no text, it makes sense only if the
1592 fancy diary display is used.
1594 %%(diary-julian-date) Diary entries giving the corresponding
1595 Julian date will be made every day. Note that since
1596 there is no text, it makes sense only if the fancy diary
1597 display is used.
1599 %%(diary-sunrise-sunset)
1600 Diary entries giving the local times of sunrise and sunset
1601 will be made every day. Note that since there is no text,
1602 it makes sense only if the fancy diary display is used.
1603 Floating point required.
1605 %%(diary-phases-of-moon)
1606 Diary entries giving the times of the phases of the moon
1607 will be when appropriate. Note that since there is no text,
1608 it makes sense only if the fancy diary display is used.
1609 Floating point required.
1611 %%(diary-yahrzeit MONTH DAY YEAR) text
1612 Text is assumed to be the name of the person; the date is
1613 the date of death on the *civil* calendar. The diary entry
1614 will appear on the proper Hebrew-date anniversary and on the
1615 day before. (If `european-calendar-style' is t, the order
1616 of the parameters should be changed to DAY, MONTH, YEAR.)
1618 %%(diary-rosh-hodesh)
1619 Diary entries will be made on the dates of Rosh Hodesh on
1620 the Hebrew calendar. Note that since there is no text, it
1621 makes sense only if the fancy diary display is used.
1623 %%(diary-parasha)
1624 Diary entries giving the weekly parasha will be made on
1625 every Saturday. Note that since there is no text, it
1626 makes sense only if the fancy diary display is used.
1628 %%(diary-omer)
1629 Diary entries giving the omer count will be made every day
1630 from Passover to Shavuot. Note that since there is no text,
1631 it makes sense only if the fancy diary display is used.
1633 Marking these entries is *extremely* time consuming, so these entries are
1634 best if they are nonmarking."
1635 (let ((s-entry (concat "\\(\\`\\|\^M\\|\n\\)"
1636 (regexp-quote diary-nonmarking-symbol)
1638 (regexp-quote sexp-diary-entry-symbol)
1639 "("))
1640 entry-found file-glob-attrs marks)
1641 (goto-char (point-min))
1642 (save-excursion
1643 (setq file-glob-attrs (nth 1 (diary-pull-attrs nil '()))))
1644 (while (re-search-forward s-entry nil t)
1645 (backward-char 1)
1646 (let ((sexp-start (point))
1647 sexp entry specifier entry-start line-start)
1648 (forward-sexp)
1649 (setq sexp (buffer-substring-no-properties sexp-start (point)))
1650 (save-excursion
1651 (re-search-backward "\^M\\|\n\\|\\`")
1652 (setq line-start (point)))
1653 (setq specifier
1654 (buffer-substring-no-properties (1+ line-start) (point))
1655 entry-start (1+ line-start))
1656 (forward-char 1)
1657 (if (and (or (char-equal (preceding-char) ?\^M)
1658 (char-equal (preceding-char) ?\n))
1659 (not (looking-at " \\|\^I")))
1660 (progn;; Diary entry consists only of the sexp
1661 (backward-char 1)
1662 (setq entry ""))
1663 (setq entry-start (point))
1664 (re-search-forward "\^M\\|\n" nil t)
1665 (while (looking-at " \\|\^I")
1666 (re-search-forward "\^M\\|\n" nil t))
1667 (backward-char 1)
1668 (setq entry (buffer-substring-no-properties entry-start (point)))
1669 (while (string-match "[\^M]" entry)
1670 (aset entry (match-beginning 0) ?\n )))
1671 (let ((diary-entry (diary-sexp-entry sexp entry date))
1672 temp literal)
1673 (setq literal entry ; before evaluation
1674 entry (if (consp diary-entry)
1675 (cdr diary-entry)
1676 diary-entry))
1677 (if diary-entry
1678 (progn
1679 (remove-overlays line-start (point) 'invisible 'diary)
1680 (if (< 0 (length entry))
1681 (setq temp (diary-pull-attrs entry file-glob-attrs)
1682 entry (nth 0 temp)
1683 marks (nth 1 temp)))))
1684 (add-to-diary-list date
1685 entry
1686 specifier
1687 (if entry-start (copy-marker entry-start)
1688 nil)
1689 marks
1690 literal)
1691 (setq entry-found (or entry-found diary-entry)))))
1692 entry-found))
1694 (defun diary-sexp-entry (sexp entry date)
1695 "Process a SEXP diary ENTRY for DATE."
1696 (let ((result (if calendar-debug-sexp
1697 (let ((stack-trace-on-error t))
1698 (eval (car (read-from-string sexp))))
1699 (condition-case nil
1700 (eval (car (read-from-string sexp)))
1701 (error
1702 (beep)
1703 (message "Bad sexp at line %d in %s: %s"
1704 (count-lines (point-min) (point))
1705 diary-file sexp)
1706 (sleep-for 2))))))
1707 (cond ((stringp result) result)
1708 ((and (consp result)
1709 (stringp (cdr result))) result)
1710 (result entry)
1711 (t nil))))
1713 (defun diary-date (month day year &optional mark)
1714 "Specific date(s) diary entry.
1715 Entry applies if date is MONTH, DAY, YEAR if `european-calendar-style' is nil,
1716 and DAY, MONTH, YEAR if `european-calendar-style' is t. DAY, MONTH, and YEAR
1717 can be lists of integers, the constant t, or an integer. The constant t means
1718 all values.
1720 An optional parameter MARK specifies a face or single-character string to
1721 use when highlighting the day in the calendar."
1722 (let ((dd (if european-calendar-style
1723 month
1724 day))
1725 (mm (if european-calendar-style
1727 month))
1728 (m (extract-calendar-month date))
1729 (y (extract-calendar-year date))
1730 (d (extract-calendar-day date)))
1731 (if (and
1732 (or (and (listp dd) (memq d dd))
1733 (equal d dd)
1734 (eq dd t))
1735 (or (and (listp mm) (memq m mm))
1736 (equal m mm)
1737 (eq mm t))
1738 (or (and (listp year) (memq y year))
1739 (equal y year)
1740 (eq year t)))
1741 (cons mark entry))))
1743 (defun diary-block (m1 d1 y1 m2 d2 y2 &optional mark)
1744 "Block diary entry.
1745 Entry applies if date is between, or on one of, two dates.
1746 The order of the parameters is
1747 M1, D1, Y1, M2, D2, Y2 if `european-calendar-style' is nil, and
1748 D1, M1, Y1, D2, M2, Y2 if `european-calendar-style' is t.
1750 An optional parameter MARK specifies a face or single-character string to
1751 use when highlighting the day in the calendar."
1753 (let ((date1 (calendar-absolute-from-gregorian
1754 (if european-calendar-style
1755 (list d1 m1 y1)
1756 (list m1 d1 y1))))
1757 (date2 (calendar-absolute-from-gregorian
1758 (if european-calendar-style
1759 (list d2 m2 y2)
1760 (list m2 d2 y2))))
1761 (d (calendar-absolute-from-gregorian date)))
1762 (if (and (<= date1 d) (<= d date2))
1763 (cons mark entry))))
1765 (defun diary-float (month dayname n &optional day mark)
1766 "Floating diary entry--entry applies if date is the nth dayname of month.
1767 Parameters are MONTH, DAYNAME, N. MONTH can be a list of months, the constant
1768 t, or an integer. The constant t means all months. If N is negative, count
1769 backward from the end of the month.
1771 An optional parameter DAY means the Nth DAYNAME on or after/before MONTH DAY.
1772 Optional MARK specifies a face or single-character string to use when
1773 highlighting the day in the calendar."
1774 ;; This is messy because the diary entry may apply, but the date on which it
1775 ;; is based can be in a different month/year. For example, asking for the
1776 ;; first Monday after December 30. For large values of |n| the problem is
1777 ;; more grotesque.
1778 (and (= dayname (calendar-day-of-week date))
1779 (let* ((m (extract-calendar-month date))
1780 (d (extract-calendar-day date))
1781 (y (extract-calendar-year date))
1782 (limit; last (n>0) or first (n<0) possible base date for entry
1783 (calendar-nth-named-absday (- n) dayname m y d))
1784 (last-abs (if (> n 0) limit (+ limit 6)))
1785 (first-abs (if (> n 0) (- limit 6) limit))
1786 (last (calendar-gregorian-from-absolute last-abs))
1787 (first (calendar-gregorian-from-absolute first-abs))
1788 ; m1, d1 is first possible base date
1789 (m1 (extract-calendar-month first))
1790 (d1 (extract-calendar-day first))
1791 (y1 (extract-calendar-year first))
1792 ; m2, d2 is last possible base date
1793 (m2 (extract-calendar-month last))
1794 (d2 (extract-calendar-day last))
1795 (y2 (extract-calendar-year last)))
1796 (if (or (and (= m1 m2) ; only possible base dates in one month
1797 (or (eq month t)
1798 (if (listp month)
1799 (memq m1 month)
1800 (= m1 month)))
1801 (let ((d (or day (if (> n 0)
1803 (calendar-last-day-of-month m1 y1)))))
1804 (and (<= d1 d) (<= d d2))))
1805 ;; only possible base dates straddle two months
1806 (and (or (< y1 y2)
1807 (and (= y1 y2) (< m1 m2)))
1809 ;; m1, d1 works as a base date
1810 (and
1811 (or (eq month t)
1812 (if (listp month)
1813 (memq m1 month)
1814 (= m1 month)))
1815 (<= d1 (or day (if (> n 0)
1817 (calendar-last-day-of-month m1 y1)))))
1818 ;; m2, d2 works as a base date
1819 (and (or (eq month t)
1820 (if (listp month)
1821 (memq m2 month)
1822 (= m2 month)))
1823 (<= (or day (if (> n 0)
1825 (calendar-last-day-of-month m2 y2)))
1826 d2)))))
1827 (cons mark entry)))))
1830 (defun diary-anniversary (month day &optional year mark)
1831 "Anniversary diary entry.
1832 Entry applies if date is the anniversary of MONTH, DAY, YEAR if
1833 `european-calendar-style' is nil, and DAY, MONTH, YEAR if
1834 `european-calendar-style' is t. Diary entry can contain `%d' or `%d%s'; the
1835 %d will be replaced by the number of years since the MONTH DAY, YEAR and the
1836 %s will be replaced by the ordinal ending of that number (that is, `st', `nd',
1837 `rd' or `th', as appropriate. The anniversary of February 29 is considered
1838 to be March 1 in non-leap years.
1840 An optional parameter MARK specifies a face or single-character string to
1841 use when highlighting the day in the calendar."
1842 (let* ((d (if european-calendar-style
1843 month
1844 day))
1845 (m (if european-calendar-style
1847 month))
1848 (y (extract-calendar-year date))
1849 (diff (if year (- y year) 100)))
1850 (if (and (= m 2) (= d 29) (not (calendar-leap-year-p y)))
1851 (setq m 3
1852 d 1))
1853 (if (and (> diff 0) (calendar-date-equal (list m d y) date))
1854 (cons mark (format entry diff (diary-ordinal-suffix diff))))))
1856 (defun diary-cyclic (n month day year &optional mark)
1857 "Cycle diary entry--entry applies every N days starting at MONTH, DAY, YEAR.
1858 If `european-calendar-style' is t, parameters are N, DAY, MONTH, YEAR.
1859 ENTRY can contain `%d' or `%d%s'; the %d will be replaced by the number of
1860 repetitions since the MONTH DAY, YEAR and %s will be replaced by the
1861 ordinal ending of that number (that is, `st', `nd', `rd' or `th', as
1862 appropriate.
1864 An optional parameter MARK specifies a face or single-character string to
1865 use when highlighting the day in the calendar."
1866 (let* ((d (if european-calendar-style
1867 month
1868 day))
1869 (m (if european-calendar-style
1871 month))
1872 (diff (- (calendar-absolute-from-gregorian date)
1873 (calendar-absolute-from-gregorian
1874 (list m d year))))
1875 (cycle (/ diff n)))
1876 (if (and (>= diff 0) (zerop (% diff n)))
1877 (cons mark (format entry cycle (diary-ordinal-suffix cycle))))))
1879 (defun diary-ordinal-suffix (n)
1880 "Ordinal suffix for N. (That is, `st', `nd', `rd', or `th', as appropriate.)"
1881 (if (or (memq (% n 100) '(11 12 13))
1882 (< 3 (% n 10)))
1883 "th"
1884 (aref ["th" "st" "nd" "rd"] (% n 10))))
1886 (defun diary-day-of-year ()
1887 "Day of year and number of days remaining in the year of date diary entry."
1888 (calendar-day-of-year-string date))
1890 (defun diary-remind (sexp days &optional marking)
1891 "Provide a reminder of a diary entry.
1892 SEXP is a diary-sexp. DAYS is either a single number or a list of numbers
1893 indicating the number(s) of days before the event that the warning(s) should
1894 occur on. If the current date is (one of) DAYS before the event indicated by
1895 SEXP, then a suitable message (as specified by `diary-remind-message' is
1896 returned.
1898 In addition to the reminders beforehand, the diary entry also appears on the
1899 date itself.
1901 A `diary-nonmarking-symbol' at the beginning of the line of the diary-remind
1902 entry specifies that the diary entry (not the reminder) is non-marking.
1903 Marking of reminders is independent of whether the entry itself is a marking
1904 or nonmarking; if optional parameter MARKING is non-nil then the reminders are
1905 marked on the calendar."
1906 (let ((diary-entry (eval sexp)))
1907 (cond
1908 ;; Diary entry applies on date
1909 ((and diary-entry
1910 (or (not marking-diary-entries) marking-diary-entry))
1911 diary-entry)
1912 ;; Diary entry may apply to `days' before date
1913 ((and (integerp days)
1914 (not diary-entry); Diary entry does not apply to date
1915 (or (not marking-diary-entries) marking))
1916 (let ((date (calendar-gregorian-from-absolute
1917 (+ (calendar-absolute-from-gregorian date) days))))
1918 (when (setq diary-entry (eval sexp)) ; re-evaluate with adjusted date
1919 ;; Discard any mark portion from diary-anniversary, etc.
1920 (if (consp diary-entry) (setq diary-entry (cdr diary-entry)))
1921 (mapconcat 'eval diary-remind-message ""))))
1922 ;; Diary entry may apply to one of a list of days before date
1923 ((and (listp days) days)
1924 (or (diary-remind sexp (car days) marking)
1925 (diary-remind sexp (cdr days) marking))))))
1927 (defun diary-redraw-calendar ()
1928 "If `calendar-buffer' is live and diary entries are marked, redraw it."
1929 (and mark-diary-entries-in-calendar
1930 (save-excursion
1931 (redraw-calendar)))
1932 ;; Return value suitable for `write-contents-functions'.
1933 nil)
1935 (defun make-diary-entry (string &optional nonmarking file)
1936 "Insert a diary entry STRING which may be NONMARKING in FILE.
1937 If omitted, NONMARKING defaults to nil and FILE defaults to
1938 `diary-file'."
1939 (let ((pop-up-frames (window-dedicated-p (selected-window))))
1940 (find-file-other-window (substitute-in-file-name (or file diary-file))))
1941 (when (eq major-mode default-major-mode) (diary-mode))
1942 (widen)
1943 (diary-unhide-everything)
1944 (goto-char (point-max))
1945 (when (let ((case-fold-search t))
1946 (search-backward "Local Variables:"
1947 (max (- (point-max) 3000) (point-min))
1949 (beginning-of-line)
1950 (insert "\n")
1951 (forward-line -1))
1952 (insert
1953 (if (bolp) "" "\n")
1954 (if nonmarking diary-nonmarking-symbol "")
1955 string " "))
1957 (defun insert-diary-entry (arg)
1958 "Insert a diary entry for the date indicated by point.
1959 Prefix arg will make the entry nonmarking."
1960 (interactive "P")
1961 (make-diary-entry (calendar-date-string (calendar-cursor-to-date t) t t)
1962 arg))
1964 (defun insert-weekly-diary-entry (arg)
1965 "Insert a weekly diary entry for the day of the week indicated by point.
1966 Prefix arg will make the entry nonmarking."
1967 (interactive "P")
1968 (make-diary-entry (calendar-day-name (calendar-cursor-to-date t))
1969 arg))
1971 (defun insert-monthly-diary-entry (arg)
1972 "Insert a monthly diary entry for the day of the month indicated by point.
1973 Prefix arg will make the entry nonmarking."
1974 (interactive "P")
1975 (let ((calendar-date-display-form
1976 (if european-calendar-style
1977 '(day " * ")
1978 '("* " day))))
1979 (make-diary-entry (calendar-date-string (calendar-cursor-to-date t) t)
1980 arg)))
1982 (defun insert-yearly-diary-entry (arg)
1983 "Insert an annual diary entry for the day of the year indicated by point.
1984 Prefix arg will make the entry nonmarking."
1985 (interactive "P")
1986 (let ((calendar-date-display-form
1987 (if european-calendar-style
1988 '(day " " monthname)
1989 '(monthname " " day))))
1990 (make-diary-entry (calendar-date-string (calendar-cursor-to-date t) t)
1991 arg)))
1993 (defun insert-anniversary-diary-entry (arg)
1994 "Insert an anniversary diary entry for the date given by point.
1995 Prefix arg will make the entry nonmarking."
1996 (interactive "P")
1997 (let ((calendar-date-display-form
1998 (if european-calendar-style
1999 '(day " " month " " year)
2000 '(month " " day " " year))))
2001 (make-diary-entry
2002 (format "%s(diary-anniversary %s)"
2003 sexp-diary-entry-symbol
2004 (calendar-date-string (calendar-cursor-to-date t) nil t))
2005 arg)))
2007 (defun insert-block-diary-entry (arg)
2008 "Insert a block diary entry for the days between the point and marked date.
2009 Prefix arg will make the entry nonmarking."
2010 (interactive "P")
2011 (let ((calendar-date-display-form
2012 (if european-calendar-style
2013 '(day " " month " " year)
2014 '(month " " day " " year)))
2015 (cursor (calendar-cursor-to-date t))
2016 (mark (or (car calendar-mark-ring)
2017 (error "No mark set in this buffer")))
2018 start end)
2019 (if (< (calendar-absolute-from-gregorian mark)
2020 (calendar-absolute-from-gregorian cursor))
2021 (setq start mark
2022 end cursor)
2023 (setq start cursor
2024 end mark))
2025 (make-diary-entry
2026 (format "%s(diary-block %s %s)"
2027 sexp-diary-entry-symbol
2028 (calendar-date-string start nil t)
2029 (calendar-date-string end nil t))
2030 arg)))
2032 (defun insert-cyclic-diary-entry (arg)
2033 "Insert a cyclic diary entry starting at the date given by point.
2034 Prefix arg will make the entry nonmarking."
2035 (interactive "P")
2036 (let ((calendar-date-display-form
2037 (if european-calendar-style
2038 '(day " " month " " year)
2039 '(month " " day " " year))))
2040 (make-diary-entry
2041 (format "%s(diary-cyclic %d %s)"
2042 sexp-diary-entry-symbol
2043 (calendar-read "Repeat every how many days: "
2044 (lambda (x) (> x 0)))
2045 (calendar-date-string (calendar-cursor-to-date t) nil t))
2046 arg)))
2048 (defvar diary-mode-map
2049 (let ((map (make-sparse-keymap)))
2050 (define-key map "\C-c\C-s" 'diary-show-all-entries)
2051 (define-key map "\C-c\C-q" 'quit-window)
2052 map)
2053 "Keymap for `diary-mode'.")
2055 ;;;###autoload
2056 (define-derived-mode diary-mode fundamental-mode "Diary"
2057 "Major mode for editing the diary file."
2058 (set (make-local-variable 'font-lock-defaults)
2059 '(diary-font-lock-keywords t))
2060 (add-to-invisibility-spec '(diary . nil))
2061 (add-hook 'after-save-hook 'diary-redraw-calendar nil t)
2062 (if diary-header-line-flag
2063 (setq header-line-format diary-header-line-format)))
2066 (defvar diary-fancy-date-pattern
2067 (concat
2068 (let ((dayname (diary-name-pattern calendar-day-name-array nil t))
2069 (monthname (diary-name-pattern calendar-month-name-array nil t))
2070 (day "[0-9]+")
2071 (month "[0-9]+")
2072 (year "-?[0-9]+"))
2073 (mapconcat 'eval calendar-date-display-form ""))
2074 ;; Optional ": holiday name" after the date.
2075 "\\(: .*\\)?")
2076 "Regular expression matching a date header in Fancy Diary.")
2078 (defconst diary-time-regexp
2079 ;; Accepted formats: 10:00 10.00 10h00 10h 10am 10:00am 10.00am
2080 ;; Use of "." as a separator annoyingly matches numbers, eg "123.45".
2081 ;; Hence often prefix this with "\\(^\\|\\s-\\)."
2082 (concat "[0-9]?[0-9]\\([AaPp][mM]\\|\\("
2083 "[Hh]\\([0-9][0-9]\\)?\\|[:.][0-9][0-9]"
2084 "\\)\\([AaPp][Mm]\\)?\\)")
2085 "Regular expression matching a time of day.")
2087 (defface diary-anniversary '((t :inherit font-lock-keyword-face))
2088 "Face used for anniversaries in the diary."
2089 :version "22.1"
2090 :group 'diary)
2092 (defface diary-time '((t :inherit font-lock-variable-name-face))
2093 "Face used for times of day in the diary."
2094 :version "22.1"
2095 :group 'diary)
2097 (defvar fancy-diary-font-lock-keywords
2098 (list
2099 (list
2100 ;; Any number of " other holiday name" lines, followed by "==" line.
2101 (concat diary-fancy-date-pattern "\\(\n +.*\\)*\n=+$")
2102 '(0 (progn (put-text-property (match-beginning 0) (match-end 0)
2103 'font-lock-multiline t)
2104 diary-face)))
2105 '("^.*\\([aA]nniversary\\|[bB]irthday\\).*$" . 'diary-anniversary)
2106 '("^.*Yahrzeit.*$" . font-lock-reference-face)
2107 '("^\\(Erev \\)?Rosh Hodesh.*" . font-lock-function-name-face)
2108 '("^Day.*omer.*$" . font-lock-builtin-face)
2109 '("^Parashat.*$" . font-lock-comment-face)
2110 `(,(format "\\(^\\|\\s-\\)%s\\(-%s\\)?" diary-time-regexp
2111 diary-time-regexp) . 'diary-time))
2112 "Keywords to highlight in fancy diary display")
2114 ;; If region looks like it might start or end in the middle of a
2115 ;; multiline pattern, extend the region to encompass the whole pattern.
2116 (defun diary-fancy-font-lock-fontify-region-function (beg end &optional verbose)
2117 "Function to use for `font-lock-fontify-region-function' in Fancy Diary.
2118 Needed to handle multiline keyword in `fancy-diary-font-lock-keywords'."
2119 (goto-char beg)
2120 (forward-line 0)
2121 (if (looking-at "=+$") (forward-line -1))
2122 (while (and (looking-at " +[^ ]")
2123 (zerop (forward-line -1))))
2124 ;; This check not essential.
2125 (if (looking-at diary-fancy-date-pattern)
2126 (setq beg (line-beginning-position)))
2127 (goto-char end)
2128 (forward-line 0)
2129 (while (and (looking-at " +[^ ]")
2130 (zerop (forward-line 1))))
2131 (if (looking-at "=+$")
2132 (setq end (line-beginning-position 2)))
2133 (font-lock-default-fontify-region beg end verbose))
2135 (define-derived-mode fancy-diary-display-mode fundamental-mode
2136 "Diary"
2137 "Major mode used while displaying diary entries using Fancy Display."
2138 (set (make-local-variable 'font-lock-defaults)
2139 '(fancy-diary-font-lock-keywords
2140 t nil nil nil
2141 (font-lock-fontify-region-function
2142 . diary-fancy-font-lock-fontify-region-function)))
2143 (local-set-key "q" 'quit-window))
2146 (defun diary-font-lock-sexps (limit)
2147 "Recognize sexp diary entry for font-locking."
2148 (if (re-search-forward
2149 (concat "^" (regexp-quote diary-nonmarking-symbol)
2150 "?\\(" (regexp-quote sexp-diary-entry-symbol) "\\)")
2151 limit t)
2152 (condition-case nil
2153 (save-restriction
2154 (narrow-to-region (point-min) limit)
2155 (let ((start (point)))
2156 (forward-sexp 1)
2157 (store-match-data (list start (point)))
2159 (error t))))
2161 (defun diary-font-lock-date-forms (month-array &optional symbol abbrev-array)
2162 "Create font-lock patterns for `diary-date-forms' using MONTH-ARRAY.
2163 If given, optional SYMBOL must be a prefix to entries.
2164 If optional ABBREV-ARRAY is present, the abbreviations constructed
2165 from this array by the function `calendar-abbrev-construct' are
2166 matched (with or without a final `.'), in addition to the full month
2167 names."
2168 (let ((dayname (diary-name-pattern calendar-day-name-array
2169 calendar-day-abbrev-array t))
2170 (monthname (format "\\(%s\\|\\*\\)"
2171 (diary-name-pattern month-array abbrev-array)))
2172 (month "\\([0-9]+\\|\\*\\)")
2173 (day "\\([0-9]+\\|\\*\\)")
2174 (year "-?\\([0-9]+\\|\\*\\)"))
2175 (mapcar (lambda (x)
2176 (cons
2177 (concat "^" (regexp-quote diary-nonmarking-symbol) "?"
2178 (if symbol (regexp-quote symbol) "") "\\("
2179 (mapconcat 'eval
2180 ;; If backup, omit first item (backup)
2181 ;; and last item (not part of date)
2182 (if (equal (car x) 'backup)
2183 (nreverse (cdr (reverse (cdr x))))
2186 ;; With backup, last item is not part of date
2187 (if (equal (car x) 'backup)
2188 (concat "\\)" (eval (car (reverse x))))
2189 "\\)"))
2190 '(1 diary-face)))
2191 diary-date-forms)))
2193 (defvar calendar-hebrew-month-name-array-leap-year)
2194 (defvar calendar-islamic-month-name-array)
2195 (defvar calendar-bahai-month-name-array)
2197 (defun diary-font-lock-keywords ()
2198 "Return a value for the variable `diary-font-lock-keywords'."
2199 (append
2200 (diary-font-lock-date-forms calendar-month-name-array
2201 nil calendar-month-abbrev-array)
2202 (when (or (memq 'mark-hebrew-diary-entries
2203 nongregorian-diary-marking-hook)
2204 (memq 'list-hebrew-diary-entries
2205 nongregorian-diary-listing-hook))
2206 (require 'cal-hebrew)
2207 (diary-font-lock-date-forms
2208 calendar-hebrew-month-name-array-leap-year hebrew-diary-entry-symbol))
2209 (when (or (memq 'mark-islamic-diary-entries
2210 nongregorian-diary-marking-hook)
2211 (memq 'list-islamic-diary-entries
2212 nongregorian-diary-listing-hook))
2213 (require 'cal-islam)
2214 (diary-font-lock-date-forms
2215 calendar-islamic-month-name-array islamic-diary-entry-symbol))
2216 (when (or (memq 'diary-bahai-mark-entries
2217 nongregorian-diary-marking-hook)
2218 (memq 'diary-bahai-list-entries
2219 nongregorian-diary-marking-hook))
2220 (require 'cal-bahai)
2221 (diary-font-lock-date-forms
2222 calendar-bahai-month-name-array bahai-diary-entry-symbol))
2223 (list
2224 (cons
2225 (format "^%s.*$" (regexp-quote diary-include-string))
2226 'font-lock-keyword-face)
2227 (cons
2228 (format "^%s?\\(%s\\)" (regexp-quote diary-nonmarking-symbol)
2229 (regexp-quote sexp-diary-entry-symbol))
2230 '(1 font-lock-reference-face))
2231 (cons
2232 (format "^%s" (regexp-quote diary-nonmarking-symbol))
2233 'font-lock-reference-face)
2234 (cons
2235 (format "^%s?%s" (regexp-quote diary-nonmarking-symbol)
2236 (regexp-opt (mapcar 'regexp-quote
2237 (list hebrew-diary-entry-symbol
2238 islamic-diary-entry-symbol
2239 bahai-diary-entry-symbol))
2241 '(1 font-lock-reference-face))
2242 '(diary-font-lock-sexps . font-lock-keyword-face)
2243 `(,(format "\\(^\\|\\s-\\)%s\\(-%s\\)?" diary-time-regexp
2244 diary-time-regexp)
2245 . 'diary-time))))
2247 (defvar diary-font-lock-keywords (diary-font-lock-keywords)
2248 "Forms to highlight in `diary-mode'.")
2250 ;; Following code from Dave Love <fx@gnu.org>.
2251 ;; Import Outlook-format appointments from mail messages in Gnus or
2252 ;; Rmail using command `diary-from-outlook'. This, or the specialized
2253 ;; functions `diary-from-outlook-gnus' and `diary-from-outlook-rmail',
2254 ;; could be run from hooks to notice appointments automatically (in
2255 ;; which case they will prompt about adding to the diary). The
2256 ;; message formats recognized are customizable through
2257 ;; `diary-outlook-formats'.
2259 ;; Dynamically bound.
2260 (defvar body)
2261 (defvar subject)
2263 (defun diary-from-outlook-internal (&optional test-only)
2264 "Snarf a diary entry from a message assumed to be from MS Outlook.
2265 Assumes `body' is bound to a string comprising the body of the message and
2266 `subject' is bound to a string comprising its subject.
2267 Arg TEST-ONLY non-nil means return non-nil if and only if the
2268 message contains an appointment, don't make a diary entry."
2269 (catch 'finished
2270 (let (format-string)
2271 (dotimes (i (length diary-outlook-formats))
2272 (when (eq 0 (string-match (car (nth i diary-outlook-formats))
2273 body))
2274 (unless test-only
2275 (setq format-string (cdr (nth i diary-outlook-formats)))
2276 (save-excursion
2277 (save-window-excursion
2278 ;; Fixme: References to optional fields in the format
2279 ;; are treated literally, not replaced by the empty
2280 ;; string. I think this is an Emacs bug.
2281 (make-diary-entry
2282 (format (replace-match (if (functionp format-string)
2283 (funcall format-string body)
2284 format-string)
2285 t nil (match-string 0 body))
2286 subject))
2287 (save-buffer))))
2288 (throw 'finished t))))
2289 nil))
2291 (defun diary-from-outlook (&optional noconfirm)
2292 "Maybe snarf diary entry from current Outlook-generated message.
2293 Currently knows about Gnus and Rmail modes. Unless the optional
2294 argument NOCONFIRM is non-nil (which is the case when this
2295 function is called interactively), then if an entry is found the
2296 user is asked to confirm its addition."
2297 (interactive "p")
2298 (let ((func (cond
2299 ((eq major-mode 'rmail-mode)
2300 #'diary-from-outlook-rmail)
2301 ((memq major-mode '(gnus-summary-mode gnus-article-mode))
2302 #'diary-from-outlook-gnus)
2303 (t (error "Don't know how to snarf in `%s'" major-mode)))))
2304 (funcall func noconfirm)))
2307 (defvar gnus-article-mime-handles)
2308 (defvar gnus-article-buffer)
2310 (autoload 'gnus-fetch-field "gnus-util")
2311 (autoload 'gnus-narrow-to-body "gnus")
2312 (autoload 'mm-get-part "mm-decode")
2314 (defun diary-from-outlook-gnus (&optional noconfirm)
2315 "Maybe snarf diary entry from Outlook-generated message in Gnus.
2316 Unless the optional argument NOCONFIRM is non-nil (which is the case when
2317 this function is called interactively), then if an entry is found the
2318 user is asked to confirm its addition.
2319 Add this function to `gnus-article-prepare-hook' to notice appointments
2320 automatically."
2321 (interactive "p")
2322 (with-current-buffer gnus-article-buffer
2323 (let ((subject (gnus-fetch-field "subject"))
2324 (body (if gnus-article-mime-handles
2325 ;; We're multipart. Don't get confused by part
2326 ;; buttons &c. Assume info is in first part.
2327 (mm-get-part (nth 1 gnus-article-mime-handles))
2328 (save-restriction
2329 (gnus-narrow-to-body)
2330 (buffer-string)))))
2331 (when (diary-from-outlook-internal t)
2332 (when (or noconfirm (y-or-n-p "Snarf diary entry? "))
2333 (diary-from-outlook-internal)
2334 (message "Diary entry added"))))))
2336 (custom-add-option 'gnus-article-prepare-hook 'diary-from-outlook-gnus)
2339 (defvar rmail-buffer)
2341 (defun diary-from-outlook-rmail (&optional noconfirm)
2342 "Maybe snarf diary entry from Outlook-generated message in Rmail.
2343 Unless the optional argument NOCONFIRM is non-nil (which is the case when
2344 this function is called interactively), then if an entry is found the
2345 user is asked to confirm its addition."
2346 (interactive "p")
2347 (with-current-buffer rmail-buffer
2348 (let ((subject (mail-fetch-field "subject"))
2349 (body (buffer-substring (save-excursion
2350 (rfc822-goto-eoh)
2351 (point))
2352 (point-max))))
2353 (when (diary-from-outlook-internal t)
2354 (when (or noconfirm (y-or-n-p "Snarf diary entry? "))
2355 (diary-from-outlook-internal)
2356 (message "Diary entry added"))))))
2359 (provide 'diary-lib)
2361 ;; arch-tag: 22dd506e-2e33-410d-9ae1-095a0c1b2010
2362 ;;; diary-lib.el ends here