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