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