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