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