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