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