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