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