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