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