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