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