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>
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)
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.
29 ;; This collection of functions implements the diary features as described
36 (defcustom diary-include-string
"#include"
37 "The string indicating inclusion of another file of diary entries.
38 See the documentation for the function `include-other-diary-files'."
42 (defcustom diary-list-include-blanks nil
43 "If nil, do not include days with no diary entry in the list of diary entries.
44 Such days will then not be shown in the fancy diary buffer, even if they
49 (defcustom diary-glob-file-regexp-prefix
"^\\#"
50 "Regular expression prepended to attribute-regexps for file-wide specifiers."
54 (defcustom diary-face
'diary
55 "Face name to use for diary entries."
58 (make-obsolete-variable 'diary-face
"customize the face `diary' instead."
61 (defcustom diary-face-attrs
62 '((" *\\[foreground:\\([-a-z]+\\)\\]$" 1 :foreground string
)
63 (" *\\[background:\\([-a-z]+\\)\\]$" 1 :background string
)
64 (" *\\[width:\\([-a-z]+\\)\\]$" 1 :width symbol
)
65 (" *\\[height:\\([-0-9a-z]+\\)\\]$" 1 :height int
)
66 (" *\\[weight:\\([-a-z]+\\)\\]$" 1 :weight symbol
)
67 (" *\\[slant:\\([-a-z]+\\)\\]$" 1 :slant symbol
)
68 (" *\\[underline:\\([-a-z]+\\)\\]$" 1 :underline stringtnil
)
69 (" *\\[overline:\\([-a-z]+\\)\\]$" 1 :overline stringtnil
)
70 (" *\\[strike-through:\\([-a-z]+\\)\\]$" 1 :strike-through stringtnil
)
71 (" *\\[inverse-video:\\([-a-z]+\\)\\]$" 1 :inverse-video tnil
)
72 (" *\\[face:\\([-0-9a-z]+\\)\\]$" 1 :face string
)
73 (" *\\[font:\\([-a-z0-9]+\\)\\]$" 1 :font string
)
75 ;;; (" *\\[box:\\([-a-z]+\\)\\]$" 1 :box)
76 ;;; (" *\\[stipple:\\([-a-z]+\\)\\]$" 1 :stipple)
78 "A list of (regexp regnum attr attrtype) lists where the
79 regexp says how to find the tag, the regnum says which
80 parenthetical sub-regexp this regexp looks for, and the attr says
81 which attribute of the face (or that this _is_ a face) is being
86 (defcustom diary-file-name-prefix nil
87 "Non-nil means prefix each diary entry with the name of the file defining it."
91 (defcustom diary-file-name-prefix-function
'identity
92 "The function that will take a diary file name and return the desired prefix."
96 (defcustom sexp-diary-entry-symbol
"%%"
97 "The string used to indicate a sexp diary entry in `diary-file'.
98 See the documentation for the function `list-sexp-diary-entries'."
102 (defcustom list-diary-entries-hook nil
103 "List of functions called after diary file is culled for relevant entries.
104 It is to be used for diary entries that are not found in the diary file.
106 A function `include-other-diary-files' is provided for use as the value of
107 this hook. This function enables you to use shared diary files together
108 with your own. The files included are specified in the diary file by lines
111 #include \"filename\"
113 This is recursive; that is, #include directives in files thus included are
114 obeyed. You can change the \"#include\" to some other string by changing
115 the variable `diary-include-string'. When you use `include-other-diary-files'
116 as part of the list-diary-entries-hook, you will probably also want to use the
117 function `mark-included-diary-files' as part of `mark-diary-entries-hook'.
119 For example, you could use
121 (setq list-diary-entries-hook
122 '(include-other-diary-files sort-diary-entries))
123 (setq diary-display-hook 'fancy-diary-display)
125 in your `.emacs' file to cause the fancy diary buffer to be displayed with
126 diary entries from various included files, each day's entries sorted into
127 lexicographic order."
129 :options
'(include-other-diary-files sort-diary-entries
)
132 (defcustom mark-diary-entries-hook nil
133 "List of functions called after marking diary entries in the calendar.
135 A function `mark-included-diary-files' is also provided for use as the
136 `mark-diary-entries-hook'; it enables you to use shared diary files together
137 with your own. The files included are specified in the diary file by lines
139 #include \"filename\"
140 This is recursive; that is, #include directives in files thus included are
141 obeyed. You can change the \"#include\" to some other string by changing the
142 variable `diary-include-string'. When you use `mark-included-diary-files' as
143 part of the mark-diary-entries-hook, you will probably also want to use the
144 function `include-other-diary-files' as part of `list-diary-entries-hook'."
146 :options
'(mark-included-diary-files)
149 (defcustom nongregorian-diary-listing-hook nil
150 "List of functions called for listing diary file and included files.
151 As the files are processed for diary entries, these functions are used
152 to cull relevant entries. You can use either or both of
153 `list-hebrew-diary-entries', `list-islamic-diary-entries' and
154 `diary-bahai-list-entries'. The documentation for these functions
155 describes the style of such diary entries."
157 :options
'(list-hebrew-diary-entries
158 list-islamic-diary-entries
159 diary-bahai-list-entries
)
162 (defcustom nongregorian-diary-marking-hook nil
163 "List of functions called for marking diary file and included files.
164 As the files are processed for diary entries, these functions are used
165 to cull relevant entries. You can use either or both of
166 `mark-hebrew-diary-entries', `mark-islamic-diary-entries' and
167 `mark-bahai-diary-entries'. The documentation for these functions
168 describes the style of such diary entries."
170 :options
'(mark-hebrew-diary-entries
171 mark-islamic-diary-entries
172 diary-bahai-mark-entries
)
175 (defcustom print-diary-entries-hook
'lpr-buffer
176 "List of functions called after a temporary diary buffer is prepared.
177 The buffer shows only the diary entries currently visible in the diary
178 buffer. The default just does the printing. Other uses might include, for
179 example, rearranging the lines into order by day and time, saving the buffer
180 instead of deleting it, or changing the function used to do the printing."
184 (defcustom diary-unknown-time -
9999
185 "Value returned by diary-entry-time when no time is found.
186 The default value -9999 causes entries with no recognizable time to be placed
187 before those with times; 9999 would place entries with no recognizable time
188 after those with times."
193 (defcustom diary-mail-addr
194 (if (boundp 'user-mail-address
) user-mail-address
"")
195 "Email address that `diary-mail-entries' will send email to."
200 (defcustom diary-mail-days
7
201 "Default number of days for `diary-mail-entries' to check."
206 (defcustom diary-remind-message
209 (concat (int-to-string (/ days
7)) (if (= 7 days
) " week" " weeks"))
210 (concat (int-to-string days
) (if (= 1 days
) " day" " days")))
213 "Pseudo-pattern giving form of reminder messages in the fancy diary display.
215 Used by the function `diary-remind', a pseudo-pattern is a list of
216 expressions that can involve the keywords `days' (a number), `date' (a list of
217 month, day, year), and `diary-entry' (a string)."
221 (defcustom diary-outlook-formats
223 ;; When: 11 October 2001 12:00-14:00 (GMT) Greenwich Mean Time : Dublin, ...
224 ;; [Current UK format? The timezone is meaningless. Sometimes the
225 ;; Where is missing.]
226 ("When: \\([0-9]+ [[:alpha:]]+ [0-9]+\\) \
229 \\(?:Where: \\([^\n]+\\)\n+\\)?
230 \\*~\\*~\\*~\\*~\\*~\\*~\\*~\\*~\\*~\\*"
231 .
"\\1\n \\2 %s, \\3")
232 ;; When: Tuesday, April 30, 2002 03:00 PM-03:30 PM (GMT) Greenwich Mean ...
234 ("^When: [[:alpha:]]+, \\([[:alpha:]]+\\) \\([0-9][0-9]*\\), \\([0-9]\\{4\\}\\) \
237 \\(?:Where: \\([^\n]+\\)\\)?\n+"
238 .
"\\2 \\1 \\3\n \\4 %s, \\5")
240 ;; German format, apparently.
241 "^Zeit: [^ ]+, +\\([0-9]+\\)\. +\\([[:upper:]][[:lower:]][[:lower:]]\\)[^ ]* +\\([0-9]+\\) +\\([^ ]+\\).*$"
242 .
"\\1 \\2 \\3\n \\4 %s"))
243 "Alist of regexps matching message text and replacement text.
245 The regexp must match the start of the message text containing an
246 appointment, but need not include a leading `^'. If it matches the
247 current message, a diary entry is made from the corresponding
248 template. If the template is a string, it should be suitable for
249 passing to `replace-match', and so will have occurrences of `\\D' to
250 substitute the match for the Dth subexpression. It must also contain
251 a single `%s' which will be replaced with the text of the message's
252 Subject field. Any other `%' characters must be doubled, so that the
253 template can be passed to `format'.
255 If the template is actually a function, it is called with the message
256 body text as argument, and may use `match-string' etc. to make a
257 template following the rules above."
258 :type
'(alist :key-type
(regexp :tag
"Regexp matching time/place")
260 (string :tag
"Template for entry")
262 "Unary function providing template")))
266 ;;; More user options below and in calendar.el.
269 (defun diary-check-diary-file ()
270 "Check that the file specified by `diary-file' exists and is readable.
271 If so, return the expanded file name, otherwise signal an error."
272 (let ((d-file (substitute-in-file-name diary-file
)))
273 (if (and d-file
(file-exists-p d-file
))
274 (if (file-readable-p d-file
)
276 (error "Diary file `%s' is not readable" diary-file
))
277 (error "Diary file `%s' does not exist" diary-file
))))
280 (defun diary (&optional arg
)
281 "Generate the diary window for ARG days starting with the current date.
282 If no argument is provided, the number of days of diary entries is governed
283 by the variable `number-of-diary-entries'. A value of ARG less than 1
284 does nothing. This function is suitable for execution in a `.emacs' file."
286 (diary-check-diary-file)
287 (let ((date (calendar-current-date)))
288 (diary-list-entries date
(if arg
(prefix-numeric-value arg
)))))
290 (define-obsolete-function-alias 'view-diary-entries
'diary-view-entries
)
291 (defun diary-view-entries (&optional arg
)
292 "Prepare and display a buffer with diary entries.
293 Searches the file named in `diary-file' for entries that
294 match ARG days starting with the date indicated by the cursor position
295 in the displayed three-month calendar."
297 (diary-check-diary-file)
298 (diary-list-entries (calendar-cursor-to-date t
) arg
))
300 (defun view-other-diary-entries (arg d-file
)
301 "Prepare and display buffer of diary entries from an alternative diary file.
302 Searches for entries that match ARG days, starting with the date indicated
303 by the cursor position in the displayed three-month calendar.
304 D-FILE specifies the file to use as the diary file."
306 (list (prefix-numeric-value current-prefix-arg
)
307 (read-file-name "Enter diary file name: " default-directory nil t
)))
308 (let ((diary-file d-file
))
309 (diary-view-entries arg
)))
311 (autoload 'calendar-check-holidays
"holidays"
312 "Check the list of holidays for any that occur on DATE.
313 The value returned is a list of strings of relevant holiday descriptions.
314 The holidays are those in the list `calendar-holidays'.")
316 (autoload 'calendar-holiday-list
"holidays"
317 "Form the list of holidays that occur on dates in the calendar window.
318 The holidays are those in the list `calendar-holidays'.")
320 (autoload 'diary-french-date
"cal-french"
321 "French calendar equivalent of date diary entry.")
323 (autoload 'diary-mayan-date
"cal-mayan"
324 "Mayan calendar equivalent of date diary entry.")
326 (autoload 'diary-iso-date
"cal-iso"
327 "ISO calendar equivalent of date diary entry.")
329 (autoload 'diary-julian-date
"cal-julian"
330 "Julian calendar equivalent of date diary entry.")
332 (autoload 'diary-astro-day-number
"cal-julian"
333 "Astronomical (Julian) day number diary entry.")
335 (autoload 'diary-chinese-date
"cal-china"
336 "Chinese calendar equivalent of date diary entry.")
338 (autoload 'diary-islamic-date
"cal-islam"
339 "Islamic calendar equivalent of date diary entry.")
341 (autoload 'list-islamic-diary-entries
"cal-islam"
342 "Add any Islamic date entries from the diary file to `diary-entries-list'.")
344 (autoload 'mark-islamic-diary-entries
"cal-islam"
345 "Mark days in the calendar window that have Islamic date diary entries.")
347 (autoload 'mark-islamic-calendar-date-pattern
"cal-islam"
348 "Mark dates in calendar window that conform to Islamic date MONTH/DAY/YEAR.")
350 (autoload 'diary-bahai-date
"cal-bahai"
351 "Baha'i calendar equivalent of date diary entry.")
353 (autoload 'diary-bahai-list-entries
"cal-bahai"
354 "Add any Baha'i date entries from the diary file to `diary-entries-list'.")
356 (autoload 'diary-bahai-mark-entries
"cal-bahai"
357 "Mark days in the calendar window that have Baha'i date diary entries.")
359 (autoload 'calendar-bahai-mark-date-pattern
"cal-bahai"
360 "Mark dates in calendar window that conform to Baha'i date MONTH/DAY/YEAR.")
362 (autoload 'diary-hebrew-date
"cal-hebrew"
363 "Hebrew calendar equivalent of date diary entry.")
365 (autoload 'diary-omer
"cal-hebrew"
366 "Omer count diary entry.")
368 (autoload 'diary-yahrzeit
"cal-hebrew"
369 "Yahrzeit diary entry--entry applies if date is yahrzeit or the day before.")
371 (autoload 'diary-parasha
"cal-hebrew"
372 "Parasha diary entry--entry applies if date is a Saturday.")
374 (autoload 'diary-rosh-hodesh
"cal-hebrew"
375 "Rosh Hodesh diary entry.")
377 (autoload 'list-hebrew-diary-entries
"cal-hebrew"
378 "Add any Hebrew date entries from the diary file to `diary-entries-list'.")
380 (autoload 'mark-hebrew-diary-entries
"cal-hebrew"
381 "Mark days in the calendar window that have Hebrew date diary entries.")
383 (autoload 'mark-hebrew-calendar-date-pattern
"cal-hebrew"
384 "Mark dates in calendar window that conform to Hebrew date MONTH/DAY/YEAR.")
386 (autoload 'diary-coptic-date
"cal-coptic"
387 "Coptic calendar equivalent of date diary entry.")
389 (autoload 'diary-ethiopic-date
"cal-coptic"
390 "Ethiopic calendar equivalent of date diary entry.")
392 (autoload 'diary-persian-date
"cal-persia"
393 "Persian calendar equivalent of date diary entry.")
395 (autoload 'diary-phases-of-moon
"lunar" "Moon phases diary entry.")
397 (autoload 'diary-sunrise-sunset
"solar"
398 "Local time of sunrise and sunset as a diary entry.")
400 (autoload 'diary-sabbath-candles
"solar"
401 "Local time of candle lighting diary entry--applies if date is a Friday.
402 No diary entry if there is no sunset on that date.")
404 (defvar diary-syntax-table
405 (let ((st (copy-syntax-table (standard-syntax-table))))
406 (modify-syntax-entry ?
* "w" st
)
407 (modify-syntax-entry ?
: "w" st
)
409 "The syntax table used when parsing dates in the diary file.
410 It is the standard syntax table used in Fundamental mode, but with the
411 syntax of `*' and `:' changed to be word constituents.")
413 (defvar diary-entries-list
)
414 (defvar displayed-year
)
415 (defvar displayed-month
)
420 (defvar original-date
)
422 (defun diary-attrtype-convert (attrvalue type
)
423 "Convert string ATTRVALUE to TYPE appropriate for a face description.
424 Valid TYPEs are: string, symbol, int, stringtnil, tnil."
426 (setq ret
(cond ((eq type
'string
) attrvalue
)
427 ((eq type
'symbol
) (read attrvalue
))
428 ((eq type
'int
) (string-to-number attrvalue
))
429 ((eq type
'stringtnil
)
430 (cond ((string= "t" attrvalue
) t
)
431 ((string= "nil" attrvalue
) nil
)
434 (cond ((string= "t" attrvalue
) t
)
435 ((string= "nil" attrvalue
) nil
)))))
436 ; (message "(%s)[%s]=[%s]" (print type) attrvalue ret)
440 (defun diary-pull-attrs (entry fileglobattrs
)
441 "Pull the face-related attributes off the entry, merge with the
442 fileglobattrs, and return the (possibly modified) entry and face
443 data in a list of attrname attrvalue values.
444 The entry will be modified to drop all tags that are used for face matching.
445 If entry is nil, then the fileglobattrs are being searched for,
446 the fileglobattrs variable is ignored, and
447 diary-glob-file-regexp-prefix is prepended to the regexps before each
450 (let (regexp regnum attrname attr-list attrname attrvalue type
455 attr-list diary-face-attrs
)
457 (goto-char (point-min))
458 (setq attr
(car attr-list
)
461 attrname
(nth 2 attr
)
463 regexp
(concat diary-glob-file-regexp-prefix regexp
))
465 (if (re-search-forward regexp
(point-max) t
)
466 (setq attrvalue
(match-string-no-properties regnum
)))
468 (setq attrvalue
(diary-attrtype-convert attrvalue type
)))
469 (setq ret-attr
(append ret-attr
(list attrname attrvalue
))))
470 (setq attr-list
(cdr attr-list
)))
471 (setq fileglobattrs ret-attr
))
473 (setq ret-attr fileglobattrs
474 attr-list diary-face-attrs
)
476 (goto-char (point-min))
477 (setq attr
(car attr-list
)
480 attrname
(nth 2 attr
)
483 (if (string-match regexp entry
)
485 (setq attrvalue
(match-string-no-properties regnum entry
))
486 (setq entry
(replace-match "" t t entry
))))
488 (setq attrvalue
(diary-attrtype-convert attrvalue type
)))
489 (setq ret-attr
(append ret-attr
(list attrname attrvalue
))))
490 (setq attr-list
(cdr attr-list
)))))
491 (list entry ret-attr
))))
493 (defun diary-set-maybe-redraw (symbol value
)
494 "Set SYMBOL's value to VALUE, and redraw the diary if necessary.
495 Redraws the diary if it is being displayed (note this is not the same as
496 just visiting the `diary-file'), and SYMBOL's value is to be changed."
497 (let ((oldvalue (eval symbol
)))
498 (custom-set-default symbol value
)
499 (and (not (equal value oldvalue
))
501 ;; Note this assumes diary was called without prefix arg.
504 ;; This can be removed once the kill/yank treatment of invisible text
505 ;; (see etc/TODO) is fixed. -- gm
506 (defcustom diary-header-line-flag t
507 "If non-nil, `simple-diary-display' will show a header line.
508 The format of the header is specified by `diary-header-line-format'."
511 :initialize
'custom-initialize-default
513 :set
'diary-set-maybe-redraw
516 (defvar diary-selective-display nil
)
518 (defcustom diary-header-line-format
519 '(:eval
(calendar-string-spread
520 (list (if diary-selective-display
521 "Selective display active - press \"s\" in calendar \
525 "Format of the header line displayed by `simple-diary-display'.
526 Only used if `diary-header-line-flag' is non-nil."
529 :initialize
'custom-initialize-default
531 :set
'diary-set-maybe-redraw
534 (defvar diary-saved-point
) ; internal
536 ;; The first version of this also checked for diary-selective-display
537 ;; in the non-fancy case. This was an attempt to distinguish between
538 ;; displaying the diary and just visiting the diary file. However,
539 ;; when using fancy diary, calling diary when there are no entries to
540 ;; display does not create the fancy buffer, nor does it switch on
541 ;; selective-display in the diary buffer. This means some
542 ;; customizations will not take effect, eg:
543 ;; http://lists.gnu.org/archive/html/emacs-pretest-bug/2007-03/msg00466.html
544 ;; So the check for selective-display was dropped. This means the
545 ;; diary will be displayed if one customizes a diary variable while
546 ;; just visiting the diary-file. This is i) unlikely, and ii) no great loss.
547 (defun diary-live-p ()
548 "Return non-nil if the diary is being displayed."
549 (or (get-buffer fancy-diary-buffer
)
551 (find-buffer-visiting (substitute-in-file-name diary-file
)))))
553 (defcustom number-of-diary-entries
1
554 "Specifies how many days of diary entries are to be displayed initially.
555 This variable affects the diary display when the command \\[diary] is used,
556 or if the value of the variable `view-diary-entries-initially' is t. For
557 example, if the default value 1 is used, then only the current day's diary
558 entries will be displayed. If the value 2 is used, then both the current
559 day's and the next day's entries will be displayed.
561 The value can also be a vector such as [0 2 2 2 2 4 1]; this value
562 says to display no diary entries on Sunday, the entries for
563 the current date and the day after on Monday through Thursday,
564 Friday through Monday's entries on Friday, and only Saturday's
567 This variable does not affect the diary display with the `d' command
568 from the calendar; in that case, the prefix argument controls the
569 number of days of diary entries displayed."
570 :type
'(choice (integer :tag
"Entries")
571 (vector :value
[0 0 0 0 0 0 0]
572 (integer :tag
"Sunday")
573 (integer :tag
"Monday")
574 (integer :tag
"Tuesday")
575 (integer :tag
"Wednesday")
576 (integer :tag
"Thursday")
577 (integer :tag
"Friday")
578 (integer :tag
"Saturday")))
579 :initialize
'custom-initialize-default
580 :set
'diary-set-maybe-redraw
584 (defvar diary-modify-entry-list-string-function nil
585 "Function applied to entry string before putting it into the entries list.
586 Can be used by programs integrating a diary list into other buffers (e.g.
587 org.el and planner.el) to modify the string or add properties to it.
588 The function takes a string argument and must return a string.")
590 (defun add-to-diary-list (date string specifier
&optional marker
592 "Add an entry to `diary-entries-list'.
593 Do nothing if DATE or STRING is nil. DATE is the (MONTH DAY
594 YEAR) for which the entry applies; STRING is the text of the
595 entry as it will appear in the diary (i.e. with any format
596 strings such as \"%d\" expanded); SPECIFIER is the date part of
597 the entry as it appears in the diary-file; LITERAL is the entry
598 as it appears in the diary-file (i.e. before expansion). If
599 LITERAL is nil, it is taken to be the same as STRING.
601 The entry is added to the list as (DATE STRING SPECIFIER LOCATOR
602 GLOBCOLOR), where LOCATOR has the form (MARKER FILENAME LITERAL),
603 FILENAME being the file containing the diary entry."
604 (when (and date string
)
605 (if diary-file-name-prefix
606 (let ((prefix (funcall diary-file-name-prefix-function
607 (buffer-file-name))))
608 (or (string= prefix
"")
609 (setq string
(format "[%s] %s" prefix string
)))))
610 (and diary-modify-entry-list-string-function
611 (setq string
(funcall diary-modify-entry-list-string-function
613 (setq diary-entries-list
614 (append diary-entries-list
615 (list (list date string specifier
616 (list marker
(buffer-file-name) literal
)
619 (define-obsolete-function-alias 'list-diary-entries
'diary-list-entries
)
620 (defun diary-list-entries (date number
&optional list-only
)
621 "Create and display a buffer containing the relevant lines in `diary-file'.
622 The arguments are DATE and NUMBER; the entries selected are those
623 for NUMBER days starting with date DATE. The other entries are hidden
624 using selective display. If NUMBER is less than 1, this function does nothing.
626 Returns a list of all relevant diary entries found, if any, in order by date.
627 The list entries have the form ((MONTH DAY YEAR) STRING SPECIFIER) where
628 \(MONTH DAY YEAR) is the date of the entry, STRING is the entry text, and
629 SPECIFIER is the applicability. If the variable `diary-list-include-blanks'
630 is t, this list includes a dummy diary entry consisting of the empty string
631 for a date with no diary entries.
633 After the list is prepared, the hooks `nongregorian-diary-listing-hook',
634 `list-diary-entries-hook', `diary-display-hook', and `diary-hook' are run.
635 These hooks have the following distinct roles:
637 `nongregorian-diary-listing-hook' can cull dates from the diary
638 and each included file. Usually used for Hebrew or Islamic
639 diary entries in files. Applied to *each* file.
641 `list-diary-entries-hook' adds or manipulates diary entries from
642 external sources. Used, for example, to include diary entries
643 from other files or to sort the diary entries. Invoked *once* only,
644 before the display hook is run.
646 `diary-display-hook' does the actual display of information. If this is
647 nil, simple-diary-display will be used. Use add-hook to set this to
648 fancy-diary-display, if desired. If you want no diary display, use
649 add-hook to set this to ignore.
651 `diary-hook' is run last. This can be used for an appointment
652 notification function.
654 If LIST-ONLY is non-nil don't modify or display the buffer, only return a list."
656 (setq number
(if (vectorp number-of-diary-entries
)
657 (aref number-of-diary-entries
(calendar-day-of-week date
))
658 number-of-diary-entries
)))
660 (let ((original-date date
);; save for possible use in the hooks
663 (date-string (calendar-date-string date
))
664 (d-file (substitute-in-file-name diary-file
)))
665 (message "Preparing diary...")
667 (let ((diary-buffer (find-buffer-visiting d-file
)))
668 (if (not diary-buffer
)
669 (set-buffer (find-file-noselect d-file t
))
670 (set-buffer diary-buffer
)
671 (or (verify-visited-file-modtime diary-buffer
)
672 (revert-buffer t t
))))
673 ;; Setup things like the header-line-format and invisibility-spec.
674 (if (eq major-mode default-major-mode
)
676 ;; This kludge is to make customizations to
677 ;; diary-header-line-flag after diary has been displayed
678 ;; take effect. Unconditionally calling (diary-mode)
679 ;; clobbers file local variables.
680 ;; http://lists.gnu.org/archive/html/emacs-pretest-bug/2007-03/msg00363.html
681 ;; http://lists.gnu.org/archive/html/emacs-pretest-bug/2007-04/msg00404.html
682 (if (eq major-mode
'diary-mode
)
683 (setq header-line-format
(and diary-header-line-flag
684 diary-header-line-format
))))
685 ;; d-s-p is passed to the diary display function.
686 (let ((diary-saved-point (point)))
688 (setq file-glob-attrs
(nth 1 (diary-pull-attrs nil
"")))
689 (with-syntax-table diary-syntax-table
690 (let ((mark (regexp-quote diary-nonmarking-symbol
)))
691 (goto-char (point-min))
693 (let ((ol (make-overlay (point-min) (point-max) nil t nil
)))
694 (set (make-local-variable 'diary-selective-display
) t
)
695 (overlay-put ol
'invisible
'diary
)
696 (overlay-put ol
'evaporate t
)))
697 (dotimes (idummy number
)
698 (let ((month (extract-calendar-month date
))
699 (day (extract-calendar-day date
))
700 (year (extract-calendar-year date
))
701 (entry-found (list-sexp-diary-entries date
)))
702 (dolist (date-form diary-date-forms
)
704 ((backup (when (eq (car date-form
) 'backup
)
705 (setq date-form
(cdr date-form
))
708 (format "%s\\|%s\\.?"
709 (calendar-day-name date
)
710 (calendar-day-name date
'abbrev
)))
712 (format "\\*\\|%s\\|%s\\.?"
713 (calendar-month-name month
)
714 (calendar-month-name month
'abbrev
)))
715 (month (concat "\\*\\|0*" (int-to-string month
)))
716 (day (concat "\\*\\|0*" (int-to-string day
)))
719 "\\*\\|0*" (int-to-string year
)
720 (if abbreviated-calendar-year
721 (concat "\\|" (format "%02d" (% year
100)))
725 "\\(\\`\\|\^M\\|\n\\)" mark
"?\\("
726 (mapconcat 'eval date-form
"\\)\\(?:")
728 (case-fold-search t
))
729 (goto-char (point-min))
730 (while (re-search-forward regexp nil t
)
731 (if backup
(re-search-backward "\\<" nil t
))
732 (if (and (or (char-equal (preceding-char) ?\^M
)
733 (char-equal (preceding-char) ?
\n))
734 (not (looking-at " \\|\^I")))
735 ;; Diary entry that consists only of date.
737 ;; Found a nonempty diary entry--make it
738 ;; visible and add it to the list.
740 (let ((entry-start (point))
742 (re-search-backward "\^M\\|\n\\|\\`")
743 (setq date-start
(point))
744 ;; When selective display (rather than
745 ;; overlays) was used, diary file used to
746 ;; start in a blank line and end in a
747 ;; newline. Now that neither of these
748 ;; need be true, 'move handles the latter
749 ;; and 1/2 kludge the former.
752 (if (and (bobp) (not (looking-at "\^M\\|\n")))
755 (while (looking-at " \\|\^I")
756 (re-search-forward "\^M\\|\n" nil
'move
))
757 (unless (and (eobp) (not (bolp)))
760 (remove-overlays date-start
(point)
762 (setq entry
(buffer-substring entry-start
(point))
763 temp
(diary-pull-attrs entry file-glob-attrs
)
769 (1+ date-start
) (1- entry-start
))
770 (copy-marker entry-start
) (nth 1 temp
)))))))
772 (not diary-list-include-blanks
)
773 (add-to-diary-list date
"" "" "" ""))
775 (calendar-gregorian-from-absolute
776 (1+ (calendar-absolute-from-gregorian date
))))
777 (setq entry-found nil
)))))
778 (goto-char (point-min))
779 (run-hooks 'nongregorian-diary-listing-hook
780 'list-diary-entries-hook
)
782 (if diary-display-hook
783 (run-hooks 'diary-display-hook
)
784 (simple-diary-display)))
785 (run-hooks 'diary-hook
)
786 diary-entries-list
))))))
788 (defun diary-unhide-everything ()
789 (kill-local-variable 'diary-selective-display
)
790 (remove-overlays (point-min) (point-max) 'invisible
'diary
)
791 (kill-local-variable 'mode-line-format
))
793 (defun include-other-diary-files ()
794 "Include the diary entries from other diary files with those of diary-file.
795 This function is suitable for use in `list-diary-entries-hook';
796 it enables you to use shared diary files together with your own.
797 The files included are specified in the diaryfile by lines of this form:
798 #include \"filename\"
799 This is recursive; that is, #include directives in diary files thus included
800 are obeyed. You can change the `#include' to some other string by
801 changing the variable `diary-include-string'."
802 (goto-char (point-min))
803 (while (re-search-forward
805 "\\(?:\\`\\|\^M\\|\n\\)"
806 (regexp-quote diary-include-string
)
809 (let* ((diary-file (substitute-in-file-name
810 (match-string-no-properties 1)))
811 (diary-list-include-blanks nil
)
812 (list-diary-entries-hook 'include-other-diary-files
)
813 (diary-display-hook 'ignore
)
815 (if (file-exists-p diary-file
)
816 (if (file-readable-p diary-file
)
818 (setq diary-entries-list
819 (append diary-entries-list
820 (diary-list-entries original-date number
)))
821 (with-current-buffer (find-buffer-visiting diary-file
)
822 (diary-unhide-everything)))
824 (message "Can't read included diary file %s" diary-file
)
827 (message "Can't find included diary file %s" diary-file
)
829 (goto-char (point-min)))
831 (defun simple-diary-display ()
832 "Display the diary buffer if there are any relevant entries or holidays."
833 (let* ((holiday-list (if holidays-in-diary-buffer
834 (calendar-check-holidays original-date
)))
835 (hol-string (format "%s%s%s"
837 (if holiday-list
": " "")
838 (mapconcat 'identity holiday-list
"; ")))
839 (msg (format "No diary entries for %s" hol-string
))
840 ;; If selected window is dedicated (to the calendar),
841 ;; need a new one to display the diary.
842 (pop-up-frames (window-dedicated-p (selected-window))))
843 (calendar-set-mode-line (format "Diary for %s" hol-string
))
844 (if (or (not diary-entries-list
)
845 (and (not (cdr diary-entries-list
))
846 (string-equal (car (cdr (car diary-entries-list
))) "")))
847 (if (< (length msg
) (frame-width))
849 (set-buffer (get-buffer-create holiday-buffer
))
850 (setq buffer-read-only nil
)
851 (calendar-set-mode-line date-string
)
853 (insert (mapconcat 'identity holiday-list
"\n"))
854 (goto-char (point-min))
855 (set-buffer-modified-p nil
)
856 (setq buffer-read-only t
)
857 (display-buffer holiday-buffer
)
858 (message "No diary entries for %s" date-string
))
860 (find-buffer-visiting (substitute-in-file-name diary-file
))
861 (let ((window (display-buffer (current-buffer))))
862 ;; d-s-p is passed from list-diary-entries.
863 (set-window-point window diary-saved-point
)
864 (set-window-start window
(point-min))))
865 (message "Preparing diary...done"))))
867 (defface diary-button
'((((type pc
) (class color
))
868 (:foreground
"lightblue")))
869 "Default face used for buttons."
872 ;; backward-compatibility alias
873 (put 'diary-button-face
'face-alias
'diary-button
)
875 (define-button-type 'diary-entry
876 'action
#'diary-goto-entry
879 (defun diary-goto-entry (button)
880 (let* ((locator (button-get button
'locator
))
881 (marker (car locator
))
883 ;; If marker pointing to diary location is valid, use that.
884 (if (and marker
(setq markbuf
(marker-buffer marker
)))
886 (pop-to-buffer markbuf
)
887 (goto-char (marker-position marker
)))
888 ;; Marker is invalid (eg buffer has been killed).
889 (or (and (setq file
(cadr locator
))
891 (find-file-other-window file
)
893 (when (eq major-mode default-major-mode
) (diary-mode))
894 (goto-char (point-min))
895 (if (re-search-forward (format "%s.*\\(%s\\)"
896 (regexp-quote (nth 2 locator
))
897 (regexp-quote (nth 3 locator
)))
899 (goto-char (match-beginning 1)))))
900 (message "Unable to locate this diary entry")))))
902 (defun fancy-diary-display ()
903 "Prepare a diary buffer with relevant entries in a fancy, noneditable form.
904 This function is provided for optional use as the `diary-display-hook'."
905 (with-current-buffer ;; Turn off selective-display in the diary file's buffer.
906 (find-buffer-visiting (substitute-in-file-name diary-file
))
907 (diary-unhide-everything))
908 (if (or (not diary-entries-list
)
909 (and (not (cdr diary-entries-list
))
910 (string-equal (car (cdr (car diary-entries-list
))) "")))
911 (let* ((holiday-list (if holidays-in-diary-buffer
912 (calendar-check-holidays original-date
)))
913 (msg (format "No diary entries for %s %s"
914 (concat date-string
(if holiday-list
":" ""))
915 (mapconcat 'identity holiday-list
"; "))))
916 (if (<= (length msg
) (frame-width))
918 (set-buffer (get-buffer-create holiday-buffer
))
919 (setq buffer-read-only nil
)
921 (insert (mapconcat 'identity holiday-list
"\n"))
922 (goto-char (point-min))
923 (set-buffer-modified-p nil
)
924 (setq buffer-read-only t
)
925 (display-buffer holiday-buffer
)
926 (message "No diary entries for %s" date-string
)))
927 (with-current-buffer;; Prepare the fancy diary buffer.
928 (make-fancy-diary-buffer)
929 (setq buffer-read-only nil
)
930 (let ((entry-list diary-entries-list
)
932 (holiday-list-last-month 1)
933 (holiday-list-last-year 1)
936 (if (not (calendar-date-equal date
(car (car entry-list
))))
938 (setq date
(car (car entry-list
)))
939 (and holidays-in-diary-buffer
940 (calendar-date-compare
941 (list (list holiday-list-last-month
942 (calendar-last-day-of-month
943 holiday-list-last-month
944 holiday-list-last-year
)
945 holiday-list-last-year
))
947 ;; We need to get the holidays for the next 3 months.
948 (setq holiday-list-last-month
949 (extract-calendar-month date
))
950 (setq holiday-list-last-year
951 (extract-calendar-year date
))
953 (increment-calendar-month
954 holiday-list-last-month holiday-list-last-year
1)
957 (let ((displayed-month holiday-list-last-month
)
958 (displayed-year holiday-list-last-year
))
959 (calendar-holiday-list)))
960 (increment-calendar-month
961 holiday-list-last-month holiday-list-last-year
1))
962 (let* ((date-string (calendar-date-string date
))
964 (let ((h holiday-list
)
966 ;; Make a list of all holidays for date.
968 (if (calendar-date-equal date
(car (car h
)))
969 (setq d
(append d
(cdr (car h
)))))
972 (insert (if (= (point) (point-min)) "" ?
\n) date-string
)
973 (if date-holiday-list
(insert ": "))
974 (let* ((l (current-column))
976 (insert (mapconcat (lambda (x)
977 (if (< longest
(length x
))
978 (setq longest
(length x
)))
981 (concat "\n" (make-string l ?
))))
982 (insert ?
\n (make-string (+ l longest
) ?
=) ?
\n)))))
984 (setq entry
(car (cdr (car entry-list
))))
985 (if (< 0 (length entry
))
986 (let ((this-entry (car entry-list
))
988 (if (setq this-loc
(nth 3 this-entry
))
989 (insert-button (concat entry
"\n")
990 ;; (MARKER FILENAME SPECIFIER LITERAL)
991 'locator
(list (car this-loc
)
999 (let* ((marks (nth 4 this-entry
))
1003 (setq temp-face
(make-symbol
1005 'concat
"temp-face-"
1006 (mapcar (lambda (sym)
1011 (make-face temp-face
)
1012 ;; Remove :face info from the marks,
1013 ;; copy the face info into temp-face
1014 (while (setq faceinfo
(memq :face faceinfo
))
1015 (copy-face (read (nth 1 faceinfo
)) temp-face
)
1016 (setcar faceinfo nil
)
1017 (setcar (cdr faceinfo
) nil
))
1018 (setq marks
(delq nil marks
))
1019 ;; Apply the font aspects.
1020 (apply 'set-face-attribute temp-face nil marks
)
1021 (search-backward entry
)
1023 (make-overlay (match-beginning 0) (match-end 0))
1024 'face temp-face
))))))
1025 (setq entry-list
(cdr entry-list
))))
1026 (set-buffer-modified-p nil
)
1027 (goto-char (point-min))
1028 (setq buffer-read-only t
)
1029 (display-buffer fancy-diary-buffer
)
1030 (fancy-diary-display-mode)
1031 (calendar-set-mode-line date-string
)
1032 (message "Preparing diary...done"))))
1034 (defun make-fancy-diary-buffer ()
1035 "Create and return the initial fancy diary buffer."
1036 (with-current-buffer (get-buffer-create fancy-diary-buffer
)
1037 (setq buffer-read-only nil
)
1038 (calendar-set-mode-line "Diary Entries")
1040 (set-buffer-modified-p nil
)
1041 (setq buffer-read-only t
)
1042 (get-buffer fancy-diary-buffer
)))
1044 (defun print-diary-entries ()
1045 "Print a hard copy of the diary display.
1047 If the simple diary display is being used, prepare a temp buffer with the
1048 visible lines of the diary buffer, add a heading line composed from the mode
1049 line, print the temp buffer, and destroy it.
1051 If the fancy diary display is being used, just print the buffer.
1053 The hooks given by the variable `print-diary-entries-hook' are called to do
1054 the actual printing."
1056 (if (bufferp (get-buffer fancy-diary-buffer
))
1057 (with-current-buffer (get-buffer fancy-diary-buffer
)
1058 (run-hooks 'print-diary-entries-hook
))
1060 (find-buffer-visiting (substitute-in-file-name diary-file
))))
1062 (let ((temp-buffer (get-buffer-create " *Printable Diary Entries*"))
1064 (with-current-buffer diary-buffer
1066 (if (not (stringp mode-line-format
))
1068 (string-match "^-*\\([^-].*[^-]\\)-*$" mode-line-format
)
1069 (match-string 1 mode-line-format
)))
1070 (let ((start (point-min))
1074 (setq end
(next-single-char-property-change
1076 (if (get-char-property start
'invisible
)
1078 (with-current-buffer temp-buffer
1079 (insert-buffer-substring diary-buffer
1080 start
(or end
(point-max)))))
1082 (and end
(< end
(point-max))))))
1083 (set-buffer temp-buffer
)
1084 (goto-char (point-min))
1085 (insert heading
"\n"
1086 (make-string (length heading
) ?
=) "\n")
1087 (run-hooks 'print-diary-entries-hook
)
1088 (kill-buffer temp-buffer
)))
1089 (error "You don't have a diary buffer!")))))
1091 (define-obsolete-function-alias 'show-all-diary-entries
'diary-show-all-entries
)
1092 (defun diary-show-all-entries ()
1093 "Show all of the diary entries in the diary file.
1094 This function gets rid of the selective display of the diary file so that
1095 all entries, not just some, are visible. If there is no diary buffer, one
1098 (let ((d-file (diary-check-diary-file))
1099 (pop-up-frames (window-dedicated-p (selected-window))))
1100 (with-current-buffer (or (find-buffer-visiting d-file
)
1101 (find-file-noselect d-file t
))
1102 (when (eq major-mode default-major-mode
) (diary-mode))
1103 (diary-unhide-everything)
1104 (display-buffer (current-buffer)))))
1107 (defun diary-mail-entries (&optional ndays
)
1108 "Send a mail message showing diary entries for next NDAYS days.
1109 If no prefix argument is given, NDAYS is set to `diary-mail-days'.
1110 Mail is sent to the address specified by `diary-mail-addr'.
1112 You can call `diary-mail-entries' every night using an at/cron job.
1113 For example, this script will run the program at 2am daily. Since
1114 `emacs -batch' does not load your `.emacs' file, you must ensure that
1115 all relevant variables are set, as done here.
1118 # diary-rem.sh -- repeatedly run the Emacs diary-reminder
1120 -eval \"(setq diary-mail-days 3 \\
1121 diary-file \\\"/path/to/diary.file\\\" \\
1122 european-calendar-style t \\
1123 diary-mail-addr \\\"user@host.name\\\" )\" \\
1124 -l diary-lib -f diary-mail-entries
1125 at -f diary-rem.sh 0200 tomorrow
1127 You may have to tweak the syntax of the `at' command to suit your
1128 system. Alternatively, you can specify a cron entry:
1129 0 1 * * * diary-rem.sh
1130 to run it every morning at 1am."
1132 (if (string-equal diary-mail-addr
"")
1133 (error "You must set `diary-mail-addr' to use this command")
1134 (let ((diary-display-hook 'fancy-diary-display
))
1135 (diary-list-entries (calendar-current-date) (or ndays diary-mail-days
)))
1136 (compose-mail diary-mail-addr
1137 (concat "Diary entries generated "
1138 (calendar-date-string (calendar-current-date))))
1140 (if (get-buffer fancy-diary-buffer
)
1141 (with-current-buffer fancy-diary-buffer
(buffer-string))
1142 "No entries found"))
1143 (call-interactively (get mail-user-agent
'sendfunc
))))
1145 (defun diary-name-pattern (string-array &optional abbrev-array paren
)
1146 "Return a regexp matching the strings in the array STRING-ARRAY.
1147 If the optional argument ABBREV-ARRAY is present, then the function
1148 `calendar-abbrev-construct' is used to construct abbreviations from the
1149 two supplied arrays. The returned regexp will then also match these
1150 abbreviations, with or without final `.' characters. If the optional
1151 argument PAREN is non-nil, the regexp is surrounded by parentheses."
1152 (regexp-opt (append string-array
1154 (calendar-abbrev-construct abbrev-array
1157 (calendar-abbrev-construct abbrev-array
1163 (defvar marking-diary-entries nil
1164 "True during the marking of diary entries, nil otherwise.")
1166 (defvar marking-diary-entry nil
1167 "True during the marking of diary entries, if current entry is marking.")
1169 (defun mark-diary-entries (&optional redraw
)
1170 "Mark days in the calendar window that have diary entries.
1171 Each entry in the diary file visible in the calendar window is
1172 marked. After the entries are marked, the hooks
1173 `nongregorian-diary-marking-hook' and `mark-diary-entries-hook'
1174 are run. If the optional argument REDRAW is non-nil (which is
1175 the case interactively, for example) then any existing diary
1176 marks are first removed. This is intended to deal with deleted
1179 ;; To remove any deleted diary entries. Do not redraw when:
1180 ;; i) processing #include diary files (else only get the marks from
1181 ;; the last #include file processed).
1182 ;; ii) called via calendar-redraw (since calendar has already been
1184 ;; Use of REDRAW handles both of these cases.
1185 (when (and redraw mark-diary-entries-in-calendar
)
1186 (setq mark-diary-entries-in-calendar nil
)
1188 (let ((marking-diary-entries t
)
1189 file-glob-attrs marks
)
1190 (with-current-buffer (find-file-noselect (diary-check-diary-file) t
)
1192 (when (eq major-mode default-major-mode
) (diary-mode))
1193 (setq mark-diary-entries-in-calendar t
)
1194 (message "Marking diary entries...")
1195 (setq file-glob-attrs
(nth 1 (diary-pull-attrs nil
'())))
1196 (with-syntax-table diary-syntax-table
1197 (dolist (date-form diary-date-forms
)
1198 (if (eq (car date-form
) 'backup
)
1199 (setq date-form
(cdr date-form
))) ;; ignore 'backup directive
1201 (diary-name-pattern calendar-day-name-array
1202 calendar-day-abbrev-array
))
1205 (diary-name-pattern calendar-month-name-array
1206 calendar-month-abbrev-array
)))
1207 (month "[0-9]+\\|\\*")
1208 (day "[0-9]+\\|\\*")
1209 (year "[0-9]+\\|\\*")
1210 (l (length date-form
))
1211 (d-name-pos (- l
(length (memq 'dayname date-form
))))
1212 (d-name-pos (if (/= l d-name-pos
) (+ 2 d-name-pos
)))
1213 (m-name-pos (- l
(length (memq 'monthname date-form
))))
1214 (m-name-pos (if (/= l m-name-pos
) (+ 2 m-name-pos
)))
1215 (d-pos (- l
(length (memq 'day date-form
))))
1216 (d-pos (if (/= l d-pos
) (+ 2 d-pos
)))
1217 (m-pos (- l
(length (memq 'month date-form
))))
1218 (m-pos (if (/= l m-pos
) (+ 2 m-pos
)))
1219 (y-pos (- l
(length (memq 'year date-form
))))
1220 (y-pos (if (/= l y-pos
) (+ 2 y-pos
)))
1223 "\\(\\`\\|\^M\\|\n\\)\\("
1224 (mapconcat 'eval date-form
"\\)\\(")
1226 (case-fold-search t
))
1227 (goto-char (point-min))
1228 (while (re-search-forward regexp nil t
)
1231 (match-string-no-properties d-name-pos
)))
1234 (match-string-no-properties m-name-pos
)))
1235 (mm (string-to-number
1237 (match-string-no-properties m-pos
)
1239 (dd (string-to-number
1241 (match-string-no-properties d-pos
)
1244 (match-string-no-properties y-pos
)))
1247 (if (and (= (length y-str
) 2)
1248 abbreviated-calendar-year
)
1250 (extract-calendar-year
1251 (calendar-current-date)))
1252 (y (+ (string-to-number y-str
)
1254 (/ current-y
100)))))
1255 (if (> (- y current-y
) 50)
1257 (if (> (- current-y y
) 50)
1260 (string-to-number y-str
)))))
1261 (let ((tmp (diary-pull-attrs (buffer-substring-no-properties
1262 (point) (line-end-position))
1264 (setq entry
(nth 0 tmp
)
1267 (mark-calendar-days-named
1270 (calendar-make-alist
1271 calendar-day-name-array
1272 0 nil calendar-day-abbrev-array
) t
)) marks
)
1275 (if (string-equal mm-name
"*") 0
1278 (calendar-make-alist
1279 calendar-month-name-array
1280 1 nil calendar-month-abbrev-array
) t
)))))
1281 (mark-calendar-date-pattern mm dd yy marks
))))))
1282 (mark-sexp-diary-entries)
1283 (run-hooks 'nongregorian-diary-marking-hook
1284 'mark-diary-entries-hook
))
1285 (message "Marking diary entries...done")))))
1287 (defun mark-sexp-diary-entries ()
1288 "Mark days in the calendar window that have sexp diary entries.
1289 Each entry in the diary file (or included files) visible in the calendar window
1290 is marked. See the documentation for the function `list-sexp-diary-entries'."
1291 (let* ((sexp-mark (regexp-quote sexp-diary-entry-symbol
))
1292 (s-entry (concat "\\(\\`\\|\^M\\|\n\\)\\("
1293 sexp-mark
"(\\)\\|\\("
1294 (regexp-quote diary-nonmarking-symbol
)
1295 sexp-mark
"(diary-remind\\)"))
1296 (file-glob-attrs (nth 1 (diary-pull-attrs nil
'())))
1297 m y first-date last-date mark file-glob-attrs
)
1298 (with-current-buffer calendar-buffer
1299 (setq m displayed-month
)
1300 (setq y displayed-year
))
1301 (increment-calendar-month m y -
1)
1303 (calendar-absolute-from-gregorian (list m
1 y
)))
1304 (increment-calendar-month m y
2)
1306 (calendar-absolute-from-gregorian
1307 (list m
(calendar-last-day-of-month m y
) y
)))
1308 (goto-char (point-min))
1309 (while (re-search-forward s-entry nil t
)
1310 (setq marking-diary-entry
(char-equal (preceding-char) ?\
())
1311 (re-search-backward "(")
1312 (let ((sexp-start (point))
1313 sexp entry entry-start line-start marks
)
1315 (setq sexp
(buffer-substring-no-properties sexp-start
(point)))
1317 (re-search-backward "\^M\\|\n\\|\\`")
1318 (setq line-start
(point)))
1320 (if (and (or (char-equal (preceding-char) ?\^M
)
1321 (char-equal (preceding-char) ?
\n))
1322 (not (looking-at " \\|\^I")))
1323 (progn;; Diary entry consists only of the sexp
1326 (setq entry-start
(point))
1327 ;; Find end of entry
1328 (re-search-forward "\^M\\|\n" nil t
)
1329 (while (looking-at " \\|\^I")
1330 (or (re-search-forward "\^M\\|\n" nil t
)
1331 (re-search-forward "$" nil t
)))
1332 (if (or (char-equal (preceding-char) ?\^M
)
1333 (char-equal (preceding-char) ?
\n))
1335 (setq entry
(buffer-substring-no-properties entry-start
(point)))
1336 (while (string-match "[\^M]" entry
)
1337 (aset entry
(match-beginning 0) ?
\n )))
1338 (calendar-for-loop date from first-date to last-date do
1339 (if (setq mark
(diary-sexp-entry sexp entry
1340 (calendar-gregorian-from-absolute date
)))
1342 (setq marks
(diary-pull-attrs entry file-glob-attrs
)
1343 marks
(nth 1 (diary-pull-attrs entry file-glob-attrs
)))
1344 (mark-visible-calendar-date
1345 (calendar-gregorian-from-absolute date
)
1346 (if (< 0 (length marks
))
1349 (car mark
)))))))))))
1351 (defun mark-included-diary-files ()
1352 "Mark the diary entries from other diary files with those of the diary file.
1353 This function is suitable for use as the `mark-diary-entries-hook'; it enables
1354 you to use shared diary files together with your own. The files included are
1355 specified in the diary-file by lines of this form:
1356 #include \"filename\"
1357 This is recursive; that is, #include directives in diary files thus included
1358 are obeyed. You can change the `#include' to some other string by
1359 changing the variable `diary-include-string'."
1360 (goto-char (point-min))
1361 (while (re-search-forward
1363 "\\(?:\\`\\|\^M\\|\n\\)"
1364 (regexp-quote diary-include-string
)
1365 " \"\\([^\"]*\\)\"")
1367 (let* ((diary-file (substitute-in-file-name
1368 (match-string-no-properties 1)))
1369 (mark-diary-entries-hook 'mark-included-diary-files
)
1370 (dbuff (find-buffer-visiting diary-file
)))
1371 (if (file-exists-p diary-file
)
1372 (if (file-readable-p diary-file
)
1374 (mark-diary-entries)
1376 (kill-buffer (find-buffer-visiting diary-file
))))
1378 (message "Can't read included diary file %s" diary-file
)
1381 (message "Can't find included diary file %s" diary-file
)
1383 (goto-char (point-min)))
1385 (defun mark-calendar-days-named (dayname &optional color
)
1386 "Mark all dates in the calendar window that are day DAYNAME of the week.
1387 0 means all Sundays, 1 means all Mondays, and so on."
1388 (with-current-buffer calendar-buffer
1389 (let ((prev-month displayed-month
)
1390 (prev-year displayed-year
)
1391 (succ-month displayed-month
)
1392 (succ-year displayed-year
)
1395 (increment-calendar-month succ-month succ-year
1)
1396 (increment-calendar-month prev-month prev-year -
1)
1397 (setq day
(calendar-absolute-from-gregorian
1398 (calendar-nth-named-day 1 dayname prev-month prev-year
)))
1399 (setq last-day
(calendar-absolute-from-gregorian
1400 (calendar-nth-named-day -
1 dayname succ-month succ-year
)))
1401 (while (<= day last-day
)
1402 (mark-visible-calendar-date (calendar-gregorian-from-absolute day
) color
)
1403 (setq day
(+ day
7))))))
1405 (defun mark-calendar-date-pattern (month day year
&optional color
)
1406 "Mark all dates in the calendar window that conform to MONTH/DAY/YEAR.
1407 A value of 0 in any position is a wildcard."
1408 (with-current-buffer calendar-buffer
1409 (let ((m displayed-month
)
1411 (increment-calendar-month m y -
1)
1413 (mark-calendar-month m y month day year color
)
1414 (increment-calendar-month m y
1)))))
1416 (defun mark-calendar-month (month year p-month p-day p-year
&optional color
)
1417 "Mark dates in the MONTH/YEAR that conform to pattern P-MONTH/P_DAY/P-YEAR.
1418 A value of 0 in any position of the pattern is a wildcard."
1419 (if (or (and (= month p-month
)
1420 (or (= p-year
0) (= year p-year
)))
1422 (or (= p-year
0) (= year p-year
))))
1425 i from
1 to
(calendar-last-day-of-month month year
) do
1426 (mark-visible-calendar-date (list month i year
) color
))
1427 (mark-visible-calendar-date (list month p-day year
) color
))))
1429 (defun sort-diary-entries ()
1430 "Sort the list of diary entries by time of day."
1431 (setq diary-entries-list
(sort diary-entries-list
'diary-entry-compare
)))
1433 (defun diary-entry-compare (e1 e2
)
1434 "Returns t if E1 is earlier than E2."
1435 (or (calendar-date-compare e1 e2
)
1436 (and (calendar-date-equal (car e1
) (car e2
))
1437 (let* ((ts1 (cadr e1
)) (t1 (diary-entry-time ts1
))
1438 (ts2 (cadr e2
)) (t2 (diary-entry-time ts2
)))
1441 (string-lessp ts1 ts2
)))))))
1443 (defun diary-entry-time (s)
1444 "Return time at the beginning of the string S as a military-style integer.
1445 For example, returns 1325 for 1:25pm.
1447 Returns `diary-unknown-time' (default value -9999) if no time is recognized.
1448 The recognized forms are XXXX, X:XX, or XX:XX (military time), and XXam,
1449 XXAM, XXpm, XXPM, XX:XXam, XX:XXAM XX:XXpm, or XX:XXPM. A period (.) can
1450 be used instead of a colon (:) to separate the hour and minute parts."
1451 (let ((case-fold-search nil
))
1452 (cond ((string-match ; Military time
1453 "\\`[ \t\n\\^M]*\\([0-9]?[0-9]\\)[:.]?\\([0-9][0-9]\\)\\(\\>\\|[^ap]\\)"
1455 (+ (* 100 (string-to-number (match-string 1 s
)))
1456 (string-to-number (match-string 2 s
))))
1457 ((string-match ; Hour only XXam or XXpm
1458 "\\`[ \t\n\\^M]*\\([0-9]?[0-9]\\)\\([ap]\\)m\\>" s
)
1459 (+ (* 100 (%
(string-to-number (match-string 1 s
)) 12))
1460 (if (equal ?a
(downcase (aref s
(match-beginning 2))))
1462 ((string-match ; Hour and minute XX:XXam or XX:XXpm
1463 "\\`[ \t\n\\^M]*\\([0-9]?[0-9]\\)[:.]\\([0-9][0-9]\\)\\([ap]\\)m\\>" s
)
1464 (+ (* 100 (%
(string-to-number (match-string 1 s
)) 12))
1465 (string-to-number (match-string 2 s
))
1466 (if (equal ?a
(downcase (aref s
(match-beginning 3))))
1468 (t diary-unknown-time
)))) ; Unrecognizable
1470 (defun list-sexp-diary-entries (date)
1471 "Add sexp entries for DATE from the diary file to `diary-entries-list'.
1472 Also, Make them visible in the diary file. Returns t if any entries were
1475 Sexp diary entries must be prefaced by a `sexp-diary-entry-symbol' (normally
1476 `%%'). The form of a sexp diary entry is
1480 Both ENTRY and DATE are globally available when the SEXP is evaluated. If the
1481 SEXP yields the value nil, the diary entry does not apply. If it yields a
1482 non-nil value, ENTRY will be taken to apply to DATE; if the non-nil value is a
1483 string, that string will be the diary entry in the fancy diary display.
1485 For example, the following diary entry will apply to the 21st of the month
1486 if it is a weekday and the Friday before if the 21st is on a weekend:
1488 &%%(let ((dayname (calendar-day-of-week date))
1489 (day (extract-calendar-day date)))
1491 (and (= day 21) (memq dayname '(1 2 3 4 5)))
1492 (and (memq day '(19 20)) (= dayname 5)))
1493 ) UIUC pay checks deposited
1495 A number of built-in functions are available for this type of diary entry:
1497 %%(diary-date MONTH DAY YEAR &optional MARK) text
1498 Entry applies if date is MONTH, DAY, YEAR if
1499 `european-calendar-style' is nil, and DAY, MONTH, YEAR if
1500 `european-calendar-style' is t. DAY, MONTH, and YEAR
1501 can be lists of integers, the constant t, or an integer.
1502 The constant t means all values. An optional parameter
1503 MARK specifies a face or single-character string to use
1504 when highlighting the day in the calendar.
1506 %%(diary-float MONTH DAYNAME N &optional DAY MARK) text
1507 Entry will appear on the Nth DAYNAME of MONTH.
1508 (DAYNAME=0 means Sunday, 1 means Monday, and so on;
1509 if N is negative it counts backward from the end of
1510 the month. MONTH can be a list of months, a single
1511 month, or t to specify all months. Optional DAY means
1512 Nth DAYNAME of MONTH on or after/before DAY. DAY defaults
1513 to 1 if N>0 and the last day of the month if N<0. An
1514 optional parameter MARK specifies a face or single-character
1515 string to use when highlighting the day in the calendar.
1517 %%(diary-block M1 D1 Y1 M2 D2 Y2 &optional MARK) text
1518 Entry will appear on dates between M1/D1/Y1 and M2/D2/Y2,
1519 inclusive. (If `european-calendar-style' is t, the
1520 order of the parameters should be changed to D1, M1, Y1,
1521 D2, M2, Y2.) An optional parameter MARK specifies a face
1522 or single-character string to use when highlighting the
1523 day in the calendar.
1525 %%(diary-anniversary MONTH DAY YEAR &optional MARK) text
1526 Entry will appear on anniversary dates of MONTH DAY, YEAR.
1527 (If `european-calendar-style' is t, the order of the
1528 parameters should be changed to DAY, MONTH, YEAR.) Text
1529 can contain %d or %d%s; %d will be replaced by the number
1530 of years since the MONTH DAY, YEAR and %s will be replaced
1531 by the ordinal ending of that number (that is, `st', `nd',
1532 `rd' or `th', as appropriate. The anniversary of February
1533 29 is considered to be March 1 in a non-leap year. An
1534 optional parameter MARK specifies a face or single-character
1535 string to use when highlighting the day in the calendar.
1537 %%(diary-cyclic N MONTH DAY YEAR &optional MARK) text
1538 Entry will appear every N days, starting MONTH DAY, YEAR.
1539 (If `european-calendar-style' is t, the order of the
1540 parameters should be changed to N, DAY, MONTH, YEAR.) Text
1541 can contain %d or %d%s; %d will be replaced by the number
1542 of repetitions since the MONTH DAY, YEAR and %s will
1543 be replaced by the ordinal ending of that number (that is,
1544 `st', `nd', `rd' or `th', as appropriate. An optional
1545 parameter MARK specifies a face or single-character string
1546 to use when highlighting the day in the calendar.
1548 %%(diary-remind SEXP DAYS &optional MARKING) text
1549 Entry is a reminder for diary sexp SEXP. DAYS is either a
1550 single number or a list of numbers indicating the number(s)
1551 of days before the event that the warning(s) should occur.
1552 If the current date is (one of) DAYS before the event
1553 indicated by EXPR, then a suitable message (as specified
1554 by `diary-remind-message') appears. In addition to the
1555 reminders beforehand, the diary entry also appears on
1556 the date itself. If optional MARKING is non-nil then the
1557 *reminders* are marked on the calendar. Marking of
1558 reminders is independent of whether the entry *itself* is
1559 a marking or nonmarking one.
1561 %%(diary-day-of-year)
1562 Diary entries giving the day of the year and the number of
1563 days remaining in the year will be made every day. Note
1564 that since there is no text, it makes sense only if the
1565 fancy diary display is used.
1568 Diary entries giving the corresponding ISO commercial date
1569 will be made every day. Note that since there is no text,
1570 it makes sense only if the fancy diary display is used.
1572 %%(diary-french-date)
1573 Diary entries giving the corresponding French Revolutionary
1574 date will be made every day. Note that since there is no
1575 text, it makes sense only if the fancy diary display is used.
1577 %%(diary-islamic-date)
1578 Diary entries giving the corresponding Islamic date will be
1579 made every day. Note that since there is no text, it
1580 makes sense only if the fancy diary display is used.
1582 %%(diary-hebrew-date)
1583 Diary entries giving the corresponding Hebrew date will be
1584 made every day. Note that since there is no text, it
1585 makes sense only if the fancy diary display is used.
1587 %%(diary-astro-day-number) Diary entries giving the corresponding
1588 astronomical (Julian) day number will be made every day.
1589 Note that since there is no text, it makes sense only if the
1590 fancy diary display is used.
1592 %%(diary-julian-date) Diary entries giving the corresponding
1593 Julian date will be made every day. Note that since
1594 there is no text, it makes sense only if the fancy diary
1597 %%(diary-sunrise-sunset)
1598 Diary entries giving the local times of sunrise and sunset
1599 will be made every day. Note that since there is no text,
1600 it makes sense only if the fancy diary display is used.
1601 Floating point required.
1603 %%(diary-phases-of-moon)
1604 Diary entries giving the times of the phases of the moon
1605 will be when appropriate. Note that since there is no text,
1606 it makes sense only if the fancy diary display is used.
1607 Floating point required.
1609 %%(diary-yahrzeit MONTH DAY YEAR) text
1610 Text is assumed to be the name of the person; the date is
1611 the date of death on the *civil* calendar. The diary entry
1612 will appear on the proper Hebrew-date anniversary and on the
1613 day before. (If `european-calendar-style' is t, the order
1614 of the parameters should be changed to DAY, MONTH, YEAR.)
1616 %%(diary-rosh-hodesh)
1617 Diary entries will be made on the dates of Rosh Hodesh on
1618 the Hebrew calendar. Note that since there is no text, it
1619 makes sense only if the fancy diary display is used.
1622 Diary entries giving the weekly parasha will be made on
1623 every Saturday. Note that since there is no text, it
1624 makes sense only if the fancy diary display is used.
1627 Diary entries giving the omer count will be made every day
1628 from Passover to Shavuot. Note that since there is no text,
1629 it makes sense only if the fancy diary display is used.
1631 Marking these entries is *extremely* time consuming, so these entries are
1632 best if they are nonmarking."
1633 (let ((s-entry (concat "\\(\\`\\|\^M\\|\n\\)"
1634 (regexp-quote diary-nonmarking-symbol
)
1636 (regexp-quote sexp-diary-entry-symbol
)
1638 entry-found file-glob-attrs marks
)
1639 (goto-char (point-min))
1641 (setq file-glob-attrs
(nth 1 (diary-pull-attrs nil
'()))))
1642 (while (re-search-forward s-entry nil t
)
1644 (let ((sexp-start (point))
1645 sexp entry specifier entry-start line-start
)
1647 (setq sexp
(buffer-substring-no-properties sexp-start
(point)))
1649 (re-search-backward "\^M\\|\n\\|\\`")
1650 (setq line-start
(point)))
1652 (buffer-substring-no-properties (1+ line-start
) (point))
1653 entry-start
(1+ line-start
))
1655 (if (and (or (char-equal (preceding-char) ?\^M
)
1656 (char-equal (preceding-char) ?
\n))
1657 (not (looking-at " \\|\^I")))
1658 (progn;; Diary entry consists only of the sexp
1661 (setq entry-start
(point))
1662 (re-search-forward "\^M\\|\n" nil t
)
1663 (while (looking-at " \\|\^I")
1664 (re-search-forward "\^M\\|\n" nil t
))
1666 (setq entry
(buffer-substring-no-properties entry-start
(point)))
1667 (while (string-match "[\^M]" entry
)
1668 (aset entry
(match-beginning 0) ?
\n )))
1669 (let ((diary-entry (diary-sexp-entry sexp entry date
))
1671 (setq literal entry
; before evaluation
1672 entry
(if (consp diary-entry
)
1677 (remove-overlays line-start
(point) 'invisible
'diary
)
1678 (if (< 0 (length entry
))
1679 (setq temp
(diary-pull-attrs entry file-glob-attrs
)
1681 marks
(nth 1 temp
)))))
1682 (add-to-diary-list date
1685 (if entry-start
(copy-marker entry-start
)
1689 (setq entry-found
(or entry-found diary-entry
)))))
1692 (defun diary-sexp-entry (sexp entry date
)
1693 "Process a SEXP diary ENTRY for DATE."
1694 (let ((result (if calendar-debug-sexp
1695 (let ((stack-trace-on-error t
))
1696 (eval (car (read-from-string sexp
))))
1698 (eval (car (read-from-string sexp
)))
1701 (message "Bad sexp at line %d in %s: %s"
1702 (count-lines (point-min) (point))
1705 (cond ((stringp result
) result
)
1706 ((and (consp result
)
1707 (stringp (cdr result
))) result
)
1711 (defun diary-date (month day year
&optional mark
)
1712 "Specific date(s) diary entry.
1713 Entry applies if date is MONTH, DAY, YEAR if `european-calendar-style' is nil,
1714 and DAY, MONTH, YEAR if `european-calendar-style' is t. DAY, MONTH, and YEAR
1715 can be lists of integers, the constant t, or an integer. The constant t means
1718 An optional parameter MARK specifies a face or single-character string to
1719 use when highlighting the day in the calendar."
1720 (let ((dd (if european-calendar-style
1723 (mm (if european-calendar-style
1726 (m (extract-calendar-month date
))
1727 (y (extract-calendar-year date
))
1728 (d (extract-calendar-day date
)))
1730 (or (and (listp dd
) (memq d dd
))
1733 (or (and (listp mm
) (memq m mm
))
1736 (or (and (listp year
) (memq y year
))
1739 (cons mark entry
))))
1741 (defun diary-block (m1 d1 y1 m2 d2 y2
&optional mark
)
1743 Entry applies if date is between, or on one of, two dates.
1744 The order of the parameters is
1745 M1, D1, Y1, M2, D2, Y2 if `european-calendar-style' is nil, and
1746 D1, M1, Y1, D2, M2, Y2 if `european-calendar-style' is t.
1748 An optional parameter MARK specifies a face or single-character string to
1749 use when highlighting the day in the calendar."
1751 (let ((date1 (calendar-absolute-from-gregorian
1752 (if european-calendar-style
1755 (date2 (calendar-absolute-from-gregorian
1756 (if european-calendar-style
1759 (d (calendar-absolute-from-gregorian date
)))
1760 (if (and (<= date1 d
) (<= d date2
))
1761 (cons mark entry
))))
1763 (defun diary-float (month dayname n
&optional day mark
)
1764 "Floating diary entry--entry applies if date is the nth dayname of month.
1765 Parameters are MONTH, DAYNAME, N. MONTH can be a list of months, the constant
1766 t, or an integer. The constant t means all months. If N is negative, count
1767 backward from the end of the month.
1769 An optional parameter DAY means the Nth DAYNAME on or after/before MONTH DAY.
1770 Optional MARK specifies a face or single-character string to use when
1771 highlighting the day in the calendar."
1772 ;; This is messy because the diary entry may apply, but the date on which it
1773 ;; is based can be in a different month/year. For example, asking for the
1774 ;; first Monday after December 30. For large values of |n| the problem is
1776 (and (= dayname
(calendar-day-of-week date
))
1777 (let* ((m (extract-calendar-month date
))
1778 (d (extract-calendar-day date
))
1779 (y (extract-calendar-year date
))
1780 (limit; last (n>0) or first (n<0) possible base date for entry
1781 (calendar-nth-named-absday (- n
) dayname m y d
))
1782 (last-abs (if (> n
0) limit
(+ limit
6)))
1783 (first-abs (if (> n
0) (- limit
6) limit
))
1784 (last (calendar-gregorian-from-absolute last-abs
))
1785 (first (calendar-gregorian-from-absolute first-abs
))
1786 ; m1, d1 is first possible base date
1787 (m1 (extract-calendar-month first
))
1788 (d1 (extract-calendar-day first
))
1789 (y1 (extract-calendar-year first
))
1790 ; m2, d2 is last possible base date
1791 (m2 (extract-calendar-month last
))
1792 (d2 (extract-calendar-day last
))
1793 (y2 (extract-calendar-year last
)))
1794 (if (or (and (= m1 m2
) ; only possible base dates in one month
1799 (let ((d (or day
(if (> n
0)
1801 (calendar-last-day-of-month m1 y1
)))))
1802 (and (<= d1 d
) (<= d d2
))))
1803 ;; only possible base dates straddle two months
1805 (and (= y1 y2
) (< m1 m2
)))
1807 ;; m1, d1 works as a base date
1813 (<= d1
(or day
(if (> n
0)
1815 (calendar-last-day-of-month m1 y1
)))))
1816 ;; m2, d2 works as a base date
1817 (and (or (eq month t
)
1821 (<= (or day
(if (> n
0)
1823 (calendar-last-day-of-month m2 y2
)))
1825 (cons mark entry
)))))
1828 (defun diary-anniversary (month day
&optional year mark
)
1829 "Anniversary diary entry.
1830 Entry applies if date is the anniversary of MONTH, DAY, YEAR if
1831 `european-calendar-style' is nil, and DAY, MONTH, YEAR if
1832 `european-calendar-style' is t. Diary entry can contain `%d' or `%d%s'; the
1833 %d will be replaced by the number of years since the MONTH DAY, YEAR and the
1834 %s will be replaced by the ordinal ending of that number (that is, `st', `nd',
1835 `rd' or `th', as appropriate. The anniversary of February 29 is considered
1836 to be March 1 in non-leap years.
1838 An optional parameter MARK specifies a face or single-character string to
1839 use when highlighting the day in the calendar."
1840 (let* ((d (if european-calendar-style
1843 (m (if european-calendar-style
1846 (y (extract-calendar-year date
))
1847 (diff (if year
(- y year
) 100)))
1848 (if (and (= m
2) (= d
29) (not (calendar-leap-year-p y
)))
1851 (if (and (> diff
0) (calendar-date-equal (list m d y
) date
))
1852 (cons mark
(format entry diff
(diary-ordinal-suffix diff
))))))
1854 (defun diary-cyclic (n month day year
&optional mark
)
1855 "Cycle diary entry--entry applies every N days starting at MONTH, DAY, YEAR.
1856 If `european-calendar-style' is t, parameters are N, DAY, MONTH, YEAR.
1857 ENTRY can contain `%d' or `%d%s'; the %d will be replaced by the number of
1858 repetitions since the MONTH DAY, YEAR and %s will be replaced by the
1859 ordinal ending of that number (that is, `st', `nd', `rd' or `th', as
1862 An optional parameter MARK specifies a face or single-character string to
1863 use when highlighting the day in the calendar."
1864 (let* ((d (if european-calendar-style
1867 (m (if european-calendar-style
1870 (diff (- (calendar-absolute-from-gregorian date
)
1871 (calendar-absolute-from-gregorian
1874 (if (and (>= diff
0) (zerop (% diff n
)))
1875 (cons mark
(format entry cycle
(diary-ordinal-suffix cycle
))))))
1877 (defun diary-ordinal-suffix (n)
1878 "Ordinal suffix for N. (That is, `st', `nd', `rd', or `th', as appropriate.)"
1879 (if (or (memq (% n
100) '(11 12 13))
1882 (aref ["th" "st" "nd" "rd"] (% n
10))))
1884 (defun diary-day-of-year ()
1885 "Day of year and number of days remaining in the year of date diary entry."
1886 (calendar-day-of-year-string date
))
1888 (defun diary-remind (sexp days
&optional marking
)
1889 "Provide a reminder of a diary entry.
1890 SEXP is a diary-sexp. DAYS is either a single number or a list of numbers
1891 indicating the number(s) of days before the event that the warning(s) should
1892 occur on. If the current date is (one of) DAYS before the event indicated by
1893 SEXP, then a suitable message (as specified by `diary-remind-message' is
1896 In addition to the reminders beforehand, the diary entry also appears on the
1899 A `diary-nonmarking-symbol' at the beginning of the line of the diary-remind
1900 entry specifies that the diary entry (not the reminder) is non-marking.
1901 Marking of reminders is independent of whether the entry itself is a marking
1902 or nonmarking; if optional parameter MARKING is non-nil then the reminders are
1903 marked on the calendar."
1904 (let ((diary-entry (eval sexp
)))
1906 ;; Diary entry applies on date
1908 (or (not marking-diary-entries
) marking-diary-entry
))
1910 ;; Diary entry may apply to `days' before date
1911 ((and (integerp days
)
1912 (not diary-entry
); Diary entry does not apply to date
1913 (or (not marking-diary-entries
) marking
))
1914 (let ((date (calendar-gregorian-from-absolute
1915 (+ (calendar-absolute-from-gregorian date
) days
))))
1916 (when (setq diary-entry
(eval sexp
)) ; re-evaluate with adjusted date
1917 ;; Discard any mark portion from diary-anniversary, etc.
1918 (if (consp diary-entry
) (setq diary-entry
(cdr diary-entry
)))
1919 (mapconcat 'eval diary-remind-message
""))))
1920 ;; Diary entry may apply to one of a list of days before date
1921 ((and (listp days
) days
)
1922 (or (diary-remind sexp
(car days
) marking
)
1923 (diary-remind sexp
(cdr days
) marking
))))))
1925 (defun diary-redraw-calendar ()
1926 "If `calendar-buffer' is live and diary entries are marked, redraw it."
1927 (and mark-diary-entries-in-calendar
1930 ;; Return value suitable for `write-contents-functions'.
1933 (defun make-diary-entry (string &optional nonmarking file
)
1934 "Insert a diary entry STRING which may be NONMARKING in FILE.
1935 If omitted, NONMARKING defaults to nil and FILE defaults to
1937 (let ((pop-up-frames (window-dedicated-p (selected-window))))
1938 (find-file-other-window (substitute-in-file-name (or file diary-file
))))
1939 (when (eq major-mode default-major-mode
) (diary-mode))
1941 (diary-unhide-everything)
1942 (goto-char (point-max))
1943 (when (let ((case-fold-search t
))
1944 (search-backward "Local Variables:"
1945 (max (- (point-max) 3000) (point-min))
1952 (if nonmarking diary-nonmarking-symbol
"")
1955 (defun insert-diary-entry (arg)
1956 "Insert a diary entry for the date indicated by point.
1957 Prefix arg will make the entry nonmarking."
1959 (make-diary-entry (calendar-date-string (calendar-cursor-to-date t
) t t
)
1962 (defun insert-weekly-diary-entry (arg)
1963 "Insert a weekly diary entry for the day of the week indicated by point.
1964 Prefix arg will make the entry nonmarking."
1966 (make-diary-entry (calendar-day-name (calendar-cursor-to-date t
))
1969 (defun insert-monthly-diary-entry (arg)
1970 "Insert a monthly diary entry for the day of the month indicated by point.
1971 Prefix arg will make the entry nonmarking."
1973 (let ((calendar-date-display-form
1974 (if european-calendar-style
1977 (make-diary-entry (calendar-date-string (calendar-cursor-to-date t
) t
)
1980 (defun insert-yearly-diary-entry (arg)
1981 "Insert an annual diary entry for the day of the year indicated by point.
1982 Prefix arg will make the entry nonmarking."
1984 (let ((calendar-date-display-form
1985 (if european-calendar-style
1986 '(day " " monthname
)
1987 '(monthname " " day
))))
1988 (make-diary-entry (calendar-date-string (calendar-cursor-to-date t
) t
)
1991 (defun insert-anniversary-diary-entry (arg)
1992 "Insert an anniversary diary entry for the date given by point.
1993 Prefix arg will make the entry nonmarking."
1995 (let ((calendar-date-display-form
1996 (if european-calendar-style
1997 '(day " " month
" " year
)
1998 '(month " " day
" " year
))))
2000 (format "%s(diary-anniversary %s)"
2001 sexp-diary-entry-symbol
2002 (calendar-date-string (calendar-cursor-to-date t
) nil t
))
2005 (defun insert-block-diary-entry (arg)
2006 "Insert a block diary entry for the days between the point and marked date.
2007 Prefix arg will make the entry nonmarking."
2009 (let ((calendar-date-display-form
2010 (if european-calendar-style
2011 '(day " " month
" " year
)
2012 '(month " " day
" " year
)))
2013 (cursor (calendar-cursor-to-date t
))
2014 (mark (or (car calendar-mark-ring
)
2015 (error "No mark set in this buffer")))
2017 (if (< (calendar-absolute-from-gregorian mark
)
2018 (calendar-absolute-from-gregorian cursor
))
2024 (format "%s(diary-block %s %s)"
2025 sexp-diary-entry-symbol
2026 (calendar-date-string start nil t
)
2027 (calendar-date-string end nil t
))
2030 (defun insert-cyclic-diary-entry (arg)
2031 "Insert a cyclic diary entry starting at the date given by point.
2032 Prefix arg will make the entry nonmarking."
2034 (let ((calendar-date-display-form
2035 (if european-calendar-style
2036 '(day " " month
" " year
)
2037 '(month " " day
" " year
))))
2039 (format "%s(diary-cyclic %d %s)"
2040 sexp-diary-entry-symbol
2041 (calendar-read "Repeat every how many days: "
2042 (lambda (x) (> x
0)))
2043 (calendar-date-string (calendar-cursor-to-date t
) nil t
))
2046 (defvar diary-mode-map
2047 (let ((map (make-sparse-keymap)))
2048 (define-key map
"\C-c\C-s" 'diary-show-all-entries
)
2049 (define-key map
"\C-c\C-q" 'quit-window
)
2051 "Keymap for `diary-mode'.")
2054 (define-derived-mode diary-mode fundamental-mode
"Diary"
2055 "Major mode for editing the diary file."
2056 (set (make-local-variable 'font-lock-defaults
)
2057 '(diary-font-lock-keywords t
))
2058 (add-to-invisibility-spec '(diary . nil
))
2059 (add-hook 'after-save-hook
'diary-redraw-calendar nil t
)
2060 (if diary-header-line-flag
2061 (setq header-line-format diary-header-line-format
)))
2064 (defvar diary-fancy-date-pattern
2066 (let ((dayname (diary-name-pattern calendar-day-name-array nil t
))
2067 (monthname (diary-name-pattern calendar-month-name-array nil t
))
2071 (mapconcat 'eval calendar-date-display-form
""))
2072 ;; Optional ": holiday name" after the date.
2074 "Regular expression matching a date header in Fancy Diary.")
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 (defface diary-anniversary
'((t :inherit font-lock-keyword-face
))
2086 "Face used for anniversaries in the diary."
2090 (defface diary-time
'((t :inherit font-lock-variable-name-face
))
2091 "Face used for times of day in the diary."
2095 (defvar fancy-diary-font-lock-keywords
2098 ;; Any number of " other holiday name" lines, followed by "==" line.
2099 (concat diary-fancy-date-pattern
"\\(\n +.*\\)*\n=+$")
2100 '(0 (progn (put-text-property (match-beginning 0) (match-end 0)
2101 'font-lock-multiline t
)
2103 '("^.*\\([aA]nniversary\\|[bB]irthday\\).*$" .
'diary-anniversary
)
2104 '("^.*Yahrzeit.*$" . font-lock-reference-face
)
2105 '("^\\(Erev \\)?Rosh Hodesh.*" . font-lock-function-name-face
)
2106 '("^Day.*omer.*$" . font-lock-builtin-face
)
2107 '("^Parashat.*$" . font-lock-comment-face
)
2108 `(,(format "\\(^\\|\\s-\\)%s\\(-%s\\)?" diary-time-regexp
2109 diary-time-regexp
) .
'diary-time
))
2110 "Keywords to highlight in fancy diary display")
2112 ;; If region looks like it might start or end in the middle of a
2113 ;; multiline pattern, extend the region to encompass the whole pattern.
2114 (defun diary-fancy-font-lock-fontify-region-function (beg end
&optional verbose
)
2115 "Function to use for `font-lock-fontify-region-function' in Fancy Diary.
2116 Needed to handle multiline keyword in `fancy-diary-font-lock-keywords'."
2119 (if (looking-at "=+$") (forward-line -
1))
2120 (while (and (looking-at " +[^ ]")
2121 (zerop (forward-line -
1))))
2122 ;; This check not essential.
2123 (if (looking-at diary-fancy-date-pattern
)
2124 (setq beg
(line-beginning-position)))
2127 (while (and (looking-at " +[^ ]")
2128 (zerop (forward-line 1))))
2129 (if (looking-at "=+$")
2130 (setq end
(line-beginning-position 2)))
2131 (font-lock-default-fontify-region beg end verbose
))
2133 (define-derived-mode fancy-diary-display-mode fundamental-mode
2135 "Major mode used while displaying diary entries using Fancy Display."
2136 (set (make-local-variable 'font-lock-defaults
)
2137 '(fancy-diary-font-lock-keywords
2139 (font-lock-fontify-region-function
2140 . diary-fancy-font-lock-fontify-region-function
)))
2141 (local-set-key "q" 'quit-window
))
2144 (defun diary-font-lock-sexps (limit)
2145 "Recognize sexp diary entry for font-locking."
2146 (if (re-search-forward
2147 (concat "^" (regexp-quote diary-nonmarking-symbol
)
2148 "?\\(" (regexp-quote sexp-diary-entry-symbol
) "\\)")
2152 (narrow-to-region (point-min) limit
)
2153 (let ((start (point)))
2155 (store-match-data (list start
(point)))
2159 (defun diary-font-lock-date-forms (month-array &optional symbol abbrev-array
)
2160 "Create font-lock patterns for `diary-date-forms' using MONTH-ARRAY.
2161 If given, optional SYMBOL must be a prefix to entries.
2162 If optional ABBREV-ARRAY is present, the abbreviations constructed
2163 from this array by the function `calendar-abbrev-construct' are
2164 matched (with or without a final `.'), in addition to the full month
2166 (let ((dayname (diary-name-pattern calendar-day-name-array
2167 calendar-day-abbrev-array t
))
2168 (monthname (format "\\(%s\\|\\*\\)"
2169 (diary-name-pattern month-array abbrev-array
)))
2170 (month "\\([0-9]+\\|\\*\\)")
2171 (day "\\([0-9]+\\|\\*\\)")
2172 (year "-?\\([0-9]+\\|\\*\\)"))
2175 (concat "^" (regexp-quote diary-nonmarking-symbol
) "?"
2176 (if symbol
(regexp-quote symbol
) "") "\\("
2178 ;; If backup, omit first item (backup)
2179 ;; and last item (not part of date)
2180 (if (equal (car x
) 'backup
)
2181 (nreverse (cdr (reverse (cdr x
))))
2184 ;; With backup, last item is not part of date
2185 (if (equal (car x
) 'backup
)
2186 (concat "\\)" (eval (car (reverse x
))))
2191 (defvar calendar-hebrew-month-name-array-leap-year
)
2192 (defvar calendar-islamic-month-name-array
)
2193 (defvar calendar-bahai-month-name-array
)
2195 (defun diary-font-lock-keywords ()
2196 "Return a value for the variable `diary-font-lock-keywords'."
2198 (diary-font-lock-date-forms calendar-month-name-array
2199 nil calendar-month-abbrev-array
)
2200 (when (or (memq 'mark-hebrew-diary-entries
2201 nongregorian-diary-marking-hook
)
2202 (memq 'list-hebrew-diary-entries
2203 nongregorian-diary-listing-hook
))
2204 (require 'cal-hebrew
)
2205 (diary-font-lock-date-forms
2206 calendar-hebrew-month-name-array-leap-year hebrew-diary-entry-symbol
))
2207 (when (or (memq 'mark-islamic-diary-entries
2208 nongregorian-diary-marking-hook
)
2209 (memq 'list-islamic-diary-entries
2210 nongregorian-diary-listing-hook
))
2211 (require 'cal-islam
)
2212 (diary-font-lock-date-forms
2213 calendar-islamic-month-name-array islamic-diary-entry-symbol
))
2214 (when (or (memq 'diary-bahai-mark-entries
2215 nongregorian-diary-marking-hook
)
2216 (memq 'diary-bahai-list-entries
2217 nongregorian-diary-marking-hook
))
2218 (require 'cal-bahai
)
2219 (diary-font-lock-date-forms
2220 calendar-bahai-month-name-array bahai-diary-entry-symbol
))
2223 (format "^%s.*$" (regexp-quote diary-include-string
))
2224 'font-lock-keyword-face
)
2226 (format "^%s?\\(%s\\)" (regexp-quote diary-nonmarking-symbol
)
2227 (regexp-quote sexp-diary-entry-symbol
))
2228 '(1 font-lock-reference-face
))
2230 (format "^%s" (regexp-quote diary-nonmarking-symbol
))
2231 'font-lock-reference-face
)
2233 (format "^%s?%s" (regexp-quote diary-nonmarking-symbol
)
2234 (regexp-opt (mapcar 'regexp-quote
2235 (list hebrew-diary-entry-symbol
2236 islamic-diary-entry-symbol
2237 bahai-diary-entry-symbol
))
2239 '(1 font-lock-reference-face
))
2240 '(diary-font-lock-sexps . font-lock-keyword-face
)
2241 `(,(format "\\(^\\|\\s-\\)%s\\(-%s\\)?" diary-time-regexp
2245 (defvar diary-font-lock-keywords
(diary-font-lock-keywords)
2246 "Forms to highlight in `diary-mode'.")
2248 ;; Following code from Dave Love <fx@gnu.org>.
2249 ;; Import Outlook-format appointments from mail messages in Gnus or
2250 ;; Rmail using command `diary-from-outlook'. This, or the specialized
2251 ;; functions `diary-from-outlook-gnus' and `diary-from-outlook-rmail',
2252 ;; could be run from hooks to notice appointments automatically (in
2253 ;; which case they will prompt about adding to the diary). The
2254 ;; message formats recognized are customizable through
2255 ;; `diary-outlook-formats'.
2257 ;; Dynamically bound.
2261 (defun diary-from-outlook-internal (&optional test-only
)
2262 "Snarf a diary entry from a message assumed to be from MS Outlook.
2263 Assumes `body' is bound to a string comprising the body of the message and
2264 `subject' is bound to a string comprising its subject.
2265 Arg TEST-ONLY non-nil means return non-nil if and only if the
2266 message contains an appointment, don't make a diary entry."
2268 (let (format-string)
2269 (dotimes (i (length diary-outlook-formats
))
2270 (when (eq 0 (string-match (car (nth i diary-outlook-formats
))
2273 (setq format-string
(cdr (nth i diary-outlook-formats
)))
2275 (save-window-excursion
2276 ;; Fixme: References to optional fields in the format
2277 ;; are treated literally, not replaced by the empty
2278 ;; string. I think this is an Emacs bug.
2280 (format (replace-match (if (functionp format-string
)
2281 (funcall format-string body
)
2283 t nil
(match-string 0 body
))
2286 (throw 'finished t
))))
2289 (defun diary-from-outlook (&optional noconfirm
)
2290 "Maybe snarf diary entry from current Outlook-generated message.
2291 Currently knows about Gnus and Rmail modes. Unless the optional
2292 argument NOCONFIRM is non-nil (which is the case when this
2293 function is called interactively), then if an entry is found the
2294 user is asked to confirm its addition."
2297 ((eq major-mode
'rmail-mode
)
2298 #'diary-from-outlook-rmail
)
2299 ((memq major-mode
'(gnus-summary-mode gnus-article-mode
))
2300 #'diary-from-outlook-gnus
)
2301 (t (error "Don't know how to snarf in `%s'" major-mode
)))))
2302 (funcall func noconfirm
)))
2305 (defvar gnus-article-mime-handles
)
2306 (defvar gnus-article-buffer
)
2308 (autoload 'gnus-fetch-field
"gnus-util")
2309 (autoload 'gnus-narrow-to-body
"gnus")
2310 (autoload 'mm-get-part
"mm-decode")
2312 (defun diary-from-outlook-gnus (&optional noconfirm
)
2313 "Maybe snarf diary entry from Outlook-generated message in Gnus.
2314 Unless the optional argument NOCONFIRM is non-nil (which is the case when
2315 this function is called interactively), then if an entry is found the
2316 user is asked to confirm its addition.
2317 Add this function to `gnus-article-prepare-hook' to notice appointments
2320 (with-current-buffer gnus-article-buffer
2321 (let ((subject (gnus-fetch-field "subject"))
2322 (body (if gnus-article-mime-handles
2323 ;; We're multipart. Don't get confused by part
2324 ;; buttons &c. Assume info is in first part.
2325 (mm-get-part (nth 1 gnus-article-mime-handles
))
2327 (gnus-narrow-to-body)
2329 (when (diary-from-outlook-internal t
)
2330 (when (or noconfirm
(y-or-n-p "Snarf diary entry? "))
2331 (diary-from-outlook-internal)
2332 (message "Diary entry added"))))))
2334 (custom-add-option 'gnus-article-prepare-hook
'diary-from-outlook-gnus
)
2337 (defvar rmail-buffer
)
2339 (defun diary-from-outlook-rmail (&optional noconfirm
)
2340 "Maybe snarf diary entry from Outlook-generated message in Rmail.
2341 Unless the optional argument NOCONFIRM is non-nil (which is the case when
2342 this function is called interactively), then if an entry is found the
2343 user is asked to confirm its addition."
2345 (with-current-buffer rmail-buffer
2346 (let ((subject (mail-fetch-field "subject"))
2347 (body (buffer-substring (save-excursion
2351 (when (diary-from-outlook-internal t
)
2352 (when (or noconfirm
(y-or-n-p "Snarf diary entry? "))
2353 (diary-from-outlook-internal)
2354 (message "Diary entry added"))))))
2357 (provide 'diary-lib
)
2359 ;; arch-tag: 22dd506e-2e33-410d-9ae1-095a0c1b2010
2360 ;;; diary-lib.el ends here