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