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