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