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