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