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