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
37 (load "diary-loaddefs" nil
'quiet
))
39 (defcustom diary-include-string
"#include"
40 "The string indicating inclusion of another file of diary entries.
41 See the documentation for the function `include-other-diary-files'."
45 (defcustom diary-list-include-blanks nil
46 "If nil, do not include days with no diary entry in the list of diary entries.
47 Such days will then not be shown in the fancy diary buffer, even if they
52 (defcustom diary-face
'diary
53 "Face name to use for diary entries."
56 (make-obsolete-variable 'diary-face
"customize the face `diary' instead."
59 ;; Face markup of calendar and diary displays: Any entry line that
60 ;; ends with [foo:value] where foo is a face attribute (except :box
61 ;; :stipple) or with [face:blah] tags, will have these values applied
62 ;; to the calendar and fancy diary displays. These attributes "stack"
63 ;; on calendar displays. File-wide attributes can be defined as
64 ;; follows: the first line matching "^# [tag:value]" defines the value
65 ;; for that particular tag.
66 (defcustom diary-face-attrs
67 '((" *\\[foreground:\\([-a-z]+\\)\\]$" 1 :foreground string
)
68 (" *\\[background:\\([-a-z]+\\)\\]$" 1 :background string
)
69 (" *\\[width:\\([-a-z]+\\)\\]$" 1 :width symbol
)
70 (" *\\[height:\\([-0-9a-z]+\\)\\]$" 1 :height int
)
71 (" *\\[weight:\\([-a-z]+\\)\\]$" 1 :weight symbol
)
72 (" *\\[slant:\\([-a-z]+\\)\\]$" 1 :slant symbol
)
73 (" *\\[underline:\\([-a-z]+\\)\\]$" 1 :underline stringtnil
)
74 (" *\\[overline:\\([-a-z]+\\)\\]$" 1 :overline stringtnil
)
75 (" *\\[strike-through:\\([-a-z]+\\)\\]$" 1 :strike-through stringtnil
)
76 (" *\\[inverse-video:\\([-a-z]+\\)\\]$" 1 :inverse-video tnil
)
77 (" *\\[face:\\([-0-9a-z]+\\)\\]$" 1 :face string
)
78 (" *\\[font:\\([-a-z0-9]+\\)\\]$" 1 :font string
)
80 ;;; (" *\\[box:\\([-a-z]+\\)\\]$" 1 :box)
81 ;;; (" *\\[stipple:\\([-a-z]+\\)\\]$" 1 :stipple)
83 "Alist of (REGEXP SUBEXP ATTRIBUTE TYPE) elements.
84 This is used by `diary-pull-attrs' to fontify certain diary
85 elements. REGEXP is a regular expression to for, and SUBEXP is
86 the numbered sub-expression to extract. `diary-glob-file-regexp-prefix'
87 is prepended to REGEXP for file-wide specifiers. ATTRIBUTE
88 specifies which face attribute (e.g. `:foreground') to modify, or
89 that this is a face (`:face') to apply. TYPE is the type of
90 attribute being applied. Available TYPES (see `diary-attrtype-convert')
91 are: `string', `symbol', `int', `tnil',`stringtnil.'"
92 :type
'(repeat (list (string :tag
"Regular expression")
93 (integer :tag
"Sub-expression")
94 (symbol :tag
"Attribute (e.g. :foreground)")
95 (choice (const string
:tag
"A string")
96 (const symbol
:tag
"A symbol")
97 (const int
:tag
"An integer")
98 (const tnil
:tag
"`t' or `nil'")
100 :tag
"A string, `t', or `nil'"))))
103 (defcustom diary-glob-file-regexp-prefix
"^\\#"
104 "Regular expression prepended to `diary-face-attrs' for file-wide specifiers."
108 (defcustom diary-file-name-prefix nil
109 "Non-nil means prefix each diary entry with the name of the file defining it."
113 (defcustom diary-file-name-prefix-function
'identity
114 "The function that will take a diary file name and return the desired prefix."
118 (defcustom sexp-diary-entry-symbol
"%%"
119 "The string used to indicate a sexp diary entry in `diary-file'.
120 See the documentation for the function `list-sexp-diary-entries'."
124 (defcustom list-diary-entries-hook nil
125 "List of functions called after diary file is culled for relevant entries.
126 It is to be used for diary entries that are not found in the diary file.
128 A function `include-other-diary-files' is provided for use as the value of
129 this hook. This function enables you to use shared diary files together
130 with your own. The files included are specified in the diary file by lines
133 #include \"filename\"
135 This is recursive; that is, #include directives in files thus included are
136 obeyed. You can change the \"#include\" to some other string by changing
137 the variable `diary-include-string'. When you use `include-other-diary-files'
138 as part of the list-diary-entries-hook, you will probably also want to use the
139 function `mark-included-diary-files' as part of `mark-diary-entries-hook'.
141 For example, you could use
143 (add-hook 'list-diary-entries-hook 'include-other-diary-files)
144 (add-hook 'list-diary-entries-hook 'sort-diary-entries)
145 (add-hook 'diary-display-hook 'fancy-diary-display)
147 in your `.emacs' file to cause the fancy diary buffer to be displayed with
148 diary entries from various included files, each day's entries sorted into
149 lexicographic order."
151 :options
'(include-other-diary-files sort-diary-entries
)
154 (defcustom mark-diary-entries-hook nil
155 "List of functions called after marking diary entries in the calendar.
157 A function `mark-included-diary-files' is also provided for use as the
158 `mark-diary-entries-hook'; it enables you to use shared diary files together
159 with your own. The files included are specified in the diary file by lines
161 #include \"filename\"
162 This is recursive; that is, #include directives in files thus included are
163 obeyed. You can change the \"#include\" to some other string by changing the
164 variable `diary-include-string'. When you use `mark-included-diary-files' as
165 part of the mark-diary-entries-hook, you will probably also want to use the
166 function `include-other-diary-files' as part of `list-diary-entries-hook'."
168 :options
'(mark-included-diary-files)
171 (defcustom nongregorian-diary-listing-hook nil
172 "List of functions called for listing diary file and included files.
173 As the files are processed for diary entries, these functions are used
174 to cull relevant entries. You can use either or both of
175 `list-hebrew-diary-entries', `list-islamic-diary-entries' and
176 `diary-bahai-list-entries'. The documentation for these functions
177 describes the style of such diary entries."
179 :options
'(list-hebrew-diary-entries
180 list-islamic-diary-entries
181 diary-bahai-list-entries
)
184 (defcustom nongregorian-diary-marking-hook nil
185 "List of functions called for marking diary file and included files.
186 As the files are processed for diary entries, these functions are used
187 to cull relevant entries. You can use either or both of
188 `mark-hebrew-diary-entries', `mark-islamic-diary-entries' and
189 `bahai-mark-diary-entries'. The documentation for these functions
190 describes the style of such diary entries."
192 :options
'(mark-hebrew-diary-entries
193 mark-islamic-diary-entries
194 diary-bahai-mark-entries
)
197 (defcustom print-diary-entries-hook
'lpr-buffer
198 "List of functions called after a temporary diary buffer is prepared.
199 The buffer shows only the diary entries currently visible in the diary
200 buffer. The default just does the printing. Other uses might include, for
201 example, rearranging the lines into order by day and time, saving the buffer
202 instead of deleting it, or changing the function used to do the printing."
206 (defcustom diary-unknown-time -
9999
207 "Value returned by `diary-entry-time' when no time is found.
208 The default value -9999 causes entries with no recognizable time to be placed
209 before those with times; 9999 would place entries with no recognizable time
210 after those with times."
215 (defcustom diary-mail-addr
216 (if (boundp 'user-mail-address
) user-mail-address
"")
217 "Email address that `diary-mail-entries' will send email to."
222 (defcustom diary-mail-days
7
223 "Default number of days for `diary-mail-entries' to check."
228 (defcustom diary-remind-message
230 (if (zerop (% days
7))
231 (concat (int-to-string (/ days
7)) (if (= 7 days
) " week" " weeks"))
232 (concat (int-to-string days
) (if (= 1 days
) " day" " days")))
235 "Pseudo-pattern giving form of reminder messages in the fancy diary display.
237 Used by the function `diary-remind', a pseudo-pattern is a list of
238 expressions that can involve the keywords `days' (a number), `date' (a list of
239 month, day, year), and `diary-entry' (a string)."
243 (defcustom diary-outlook-formats
245 ;; When: 11 October 2001 12:00-14:00 (GMT) Greenwich Mean Time : Dublin, ...
246 ;; [Current UK format? The timezone is meaningless. Sometimes the
247 ;; Where is missing.]
248 ("When: \\([0-9]+ [[:alpha:]]+ [0-9]+\\) \
251 \\(?:Where: \\([^\n]+\\)\n+\\)?
252 \\*~\\*~\\*~\\*~\\*~\\*~\\*~\\*~\\*~\\*"
253 .
"\\1\n \\2 %s, \\3")
254 ;; When: Tuesday, April 30, 2002 03:00 PM-03:30 PM (GMT) Greenwich Mean ...
256 ("^When: [[:alpha:]]+, \\([[:alpha:]]+\\) \\([0-9][0-9]*\\), \\([0-9]\\{4\\}\\) \
259 \\(?:Where: \\([^\n]+\\)\\)?\n+"
260 .
"\\2 \\1 \\3\n \\4 %s, \\5")
262 ;; German format, apparently.
263 "^Zeit: [^ ]+, +\\([0-9]+\\)\. +\\([[:upper:]][[:lower:]][[:lower:]]\\)[^ ]* +\\([0-9]+\\) +\\([^ ]+\\).*$"
264 .
"\\1 \\2 \\3\n \\4 %s"))
265 "Alist of regexps matching message text and replacement text.
267 The regexp must match the start of the message text containing an
268 appointment, but need not include a leading `^'. If it matches the
269 current message, a diary entry is made from the corresponding
270 template. If the template is a string, it should be suitable for
271 passing to `replace-match', and so will have occurrences of `\\D' to
272 substitute the match for the Dth subexpression. It must also contain
273 a single `%s' which will be replaced with the text of the message's
274 Subject field. Any other `%' characters must be doubled, so that the
275 template can be passed to `format'.
277 If the template is actually a function, it is called with the message
278 body text as argument, and may use `match-string' etc. to make a
279 template following the rules above."
280 :type
'(alist :key-type
(regexp :tag
"Regexp matching time/place")
282 (string :tag
"Template for entry")
284 "Unary function providing template")))
288 ;;; More user options below and in calendar.el.
291 (defun diary-check-diary-file ()
292 "Check that the file specified by `diary-file' exists and is readable.
293 If so, return the expanded file name, otherwise signal an error."
294 (let ((d-file (substitute-in-file-name diary-file
)))
295 (if (and d-file
(file-exists-p d-file
))
296 (if (file-readable-p d-file
)
298 (error "Diary file `%s' is not readable" diary-file
))
299 (error "Diary file `%s' does not exist" diary-file
))))
302 (defun diary (&optional arg
)
303 "Generate the diary window for ARG days starting with the current date.
304 If no argument is provided, the number of days of diary entries is governed
305 by the variable `number-of-diary-entries'. A value of ARG less than 1
306 does nothing. This function is suitable for execution in a `.emacs' file."
308 (diary-check-diary-file)
309 (let ((date (calendar-current-date)))
310 (diary-list-entries date
(if arg
(prefix-numeric-value arg
)))))
312 (define-obsolete-function-alias 'view-diary-entries
'diary-view-entries
)
314 (defun diary-view-entries (&optional arg
)
315 "Prepare and display a buffer with diary entries.
316 Searches the file named in `diary-file' for entries that
317 match ARG days starting with the date indicated by the cursor position
318 in the displayed three-month calendar."
320 (diary-check-diary-file)
321 (diary-list-entries (calendar-cursor-to-date t
) arg
))
324 (defun view-other-diary-entries (arg d-file
)
325 "Prepare and display buffer of diary entries from an alternative diary file.
326 Searches for entries that match ARG days, starting with the date indicated
327 by the cursor position in the displayed three-month calendar.
328 D-FILE specifies the file to use as the diary file."
330 (list (prefix-numeric-value current-prefix-arg
)
331 (read-file-name "Enter diary file name: " default-directory nil t
)))
332 (let ((diary-file d-file
))
333 (diary-view-entries arg
)))
335 (defvar diary-syntax-table
336 (let ((st (copy-syntax-table (standard-syntax-table))))
337 (modify-syntax-entry ?
* "w" st
)
338 (modify-syntax-entry ?
: "w" st
)
340 "The syntax table used when parsing dates in the diary file.
341 It is the standard syntax table used in Fundamental mode, but with the
342 syntax of `*' and `:' changed to be word constituents.")
344 (defun diary-attrtype-convert (attrvalue type
)
345 "Convert string ATTRVALUE to TYPE appropriate for a face description.
346 Valid TYPEs are: string, symbol, int, stringtnil, tnil."
347 (cond ((eq type
'string
) attrvalue
)
348 ((eq type
'symbol
) (read attrvalue
)) ; FIXME intern-soft?
349 ((eq type
'int
) (string-to-number attrvalue
))
350 ((eq type
'stringtnil
)
351 (cond ((string-equal "t" attrvalue
) t
)
352 ((string-equal "nil" attrvalue
) nil
)
354 ((eq type
'tnil
) (string-equal "t" attrvalue
))))
356 (defun diary-pull-attrs (entry fileglobattrs
)
357 "Search for matches for regexps from `diary-face-attrs'.
358 If ENTRY is nil, searches from the start of the current buffer, and
359 prepends all regexps with `diary-glob-file-regexp-prefix'.
360 If ENTRY is a string, search for matches in that string, and remove them.
361 Returns a list of ENTRY followed by (ATTRIBUTE VALUE) pairs.
362 When ENTRY is non-nil, FILEGLOBATTRS forms the start of the (ATTRIBUTE VALUE)
364 (let (regexp regnum attrname attrname attrvalue type ret-attr
)
367 (dolist (attr diary-face-attrs
)
368 ;; FIXME inefficient searching.
369 (goto-char (point-min))
370 (setq regexp
(concat diary-glob-file-regexp-prefix
(car attr
))
372 attrname
(nth 2 attr
)
374 attrvalue
(if (re-search-forward regexp nil t
)
375 (match-string-no-properties regnum
)))
377 (setq attrvalue
(diary-attrtype-convert attrvalue type
))
378 (setq ret-attr
(append ret-attr
379 (list attrname attrvalue
))))))
380 (setq ret-attr fileglobattrs
)
381 (dolist (attr diary-face-attrs
)
382 (setq regexp
(car attr
)
384 attrname
(nth 2 attr
)
387 ;; FIXME multiple matches?
388 (if (string-match regexp entry
)
389 (setq attrvalue
(match-string-no-properties regnum entry
)
390 entry
(replace-match "" t t entry
)))
392 (setq attrvalue
(diary-attrtype-convert attrvalue type
))
393 (setq ret-attr
(append ret-attr
(list attrname attrvalue
))))))
394 (list entry ret-attr
)))
397 (defun diary-set-maybe-redraw (symbol value
)
398 "Set SYMBOL's value to VALUE, and redraw the diary if necessary.
399 Redraws the diary if it is being displayed (note this is not the same as
400 just visiting the `diary-file'), and SYMBOL's value is to be changed."
401 (let ((oldvalue (eval symbol
)))
402 (custom-set-default symbol value
)
403 (and (not (equal value oldvalue
))
405 ;; Note this assumes diary was called without prefix arg.
408 ;; This can be removed once the kill/yank treatment of invisible text
409 ;; (see etc/TODO) is fixed. -- gm
410 (defcustom diary-header-line-flag t
411 "Non-nil means `simple-diary-display' will show a header line.
412 The format of the header is specified by `diary-header-line-format'."
415 :initialize
'custom-initialize-default
417 :set
'diary-set-maybe-redraw
420 (defvar diary-selective-display nil
)
422 (defcustom diary-header-line-format
423 '(:eval
(calendar-string-spread
424 (list (if diary-selective-display
425 "Selective display active - press \"s\" in calendar \
429 "Format of the header line displayed by `simple-diary-display'.
430 Only used if `diary-header-line-flag' is non-nil."
433 :initialize
'custom-initialize-default
435 :set
'diary-set-maybe-redraw
438 ;; The first version of this also checked for diary-selective-display
439 ;; in the non-fancy case. This was an attempt to distinguish between
440 ;; displaying the diary and just visiting the diary file. However,
441 ;; when using fancy diary, calling diary when there are no entries to
442 ;; display does not create the fancy buffer, nor does it switch on
443 ;; selective-display in the diary buffer. This means some
444 ;; customizations will not take effect, eg:
445 ;; http://lists.gnu.org/archive/html/emacs-pretest-bug/2007-03/msg00466.html
446 ;; So the check for selective-display was dropped. This means the
447 ;; diary will be displayed if one customizes a diary variable while
448 ;; just visiting the diary-file. This is i) unlikely, and ii) no great loss.
450 (defun diary-live-p ()
451 "Return non-nil if the diary is being displayed."
452 (or (get-buffer fancy-diary-buffer
)
454 (find-buffer-visiting (substitute-in-file-name diary-file
)))))
456 (defcustom number-of-diary-entries
1
457 "Specifies how many days of diary entries are to be displayed initially.
458 This variable affects the diary display when the command \\[diary] is used,
459 or if the value of the variable `view-diary-entries-initially' is t. For
460 example, if the default value 1 is used, then only the current day's diary
461 entries will be displayed. If the value 2 is used, then both the current
462 day's and the next day's entries will be displayed.
464 The value can also be a vector such as [0 2 2 2 2 4 1]; this value
465 says to display no diary entries on Sunday, the entries for
466 the current date and the day after on Monday through Thursday,
467 Friday through Monday's entries on Friday, and only Saturday's
470 This variable does not affect the diary display with the `d' command
471 from the calendar; in that case, the prefix argument controls the
472 number of days of diary entries displayed."
473 :type
'(choice (integer :tag
"Entries")
474 (vector :value
[0 0 0 0 0 0 0]
475 (integer :tag
"Sunday")
476 (integer :tag
"Monday")
477 (integer :tag
"Tuesday")
478 (integer :tag
"Wednesday")
479 (integer :tag
"Thursday")
480 (integer :tag
"Friday")
481 (integer :tag
"Saturday")))
482 :initialize
'custom-initialize-default
483 :set
'diary-set-maybe-redraw
487 (defvar diary-modify-entry-list-string-function nil
488 "Function applied to entry string before putting it into the entries list.
489 Can be used by programs integrating a diary list into other buffers (e.g.
490 org.el and planner.el) to modify the string or add properties to it.
491 The function takes a string argument and must return a string.")
493 (defvar diary-entries-list
) ; bound in diary-list-entries
495 (defun add-to-diary-list (date string specifier
&optional marker
497 "Add an entry to `diary-entries-list'.
498 Do nothing if DATE or STRING is nil. DATE is the (MONTH DAY
499 YEAR) for which the entry applies; STRING is the text of the
500 entry as it will appear in the diary (i.e. with any format
501 strings such as \"%d\" expanded); SPECIFIER is the date part of
502 the entry as it appears in the diary-file; LITERAL is the entry
503 as it appears in the diary-file (i.e. before expansion). If
504 LITERAL is nil, it is taken to be the same as STRING.
506 The entry is added to the list as (DATE STRING SPECIFIER LOCATOR
507 GLOBCOLOR), where LOCATOR has the form (MARKER FILENAME LITERAL),
508 FILENAME being the file containing the diary entry."
509 (when (and date string
)
510 (if diary-file-name-prefix
511 (let ((prefix (funcall diary-file-name-prefix-function
512 (buffer-file-name))))
513 (or (string-equal prefix
"")
514 (setq string
(format "[%s] %s" prefix string
)))))
515 (and diary-modify-entry-list-string-function
516 (setq string
(funcall diary-modify-entry-list-string-function
518 (setq diary-entries-list
519 (append diary-entries-list
520 (list (list date string specifier
521 (list marker
(buffer-file-name) literal
)
524 (define-obsolete-function-alias 'list-diary-entries
'diary-list-entries
)
525 (defun diary-list-entries (date number
&optional list-only
)
526 "Create and display a buffer containing the relevant lines in `diary-file'.
527 The arguments are DATE and NUMBER; the entries selected are those
528 for NUMBER days starting with date DATE. The other entries are hidden
529 using selective display. If NUMBER is less than 1, this function does nothing.
531 Returns a list of all relevant diary entries found, if any, in order by date.
532 The list entries have the form ((MONTH DAY YEAR) STRING SPECIFIER) where
533 \(MONTH DAY YEAR) is the date of the entry, STRING is the entry text, and
534 SPECIFIER is the applicability. If the variable `diary-list-include-blanks'
535 is t, this list includes a dummy diary entry consisting of the empty string
536 for a date with no diary entries.
538 After the list is prepared, the hooks `nongregorian-diary-listing-hook',
539 `list-diary-entries-hook', `diary-display-hook', and `diary-hook' are run.
540 These hooks have the following distinct roles:
542 `nongregorian-diary-listing-hook' can cull dates from the diary
543 and each included file. Usually used for Hebrew or Islamic
544 diary entries in files. Applied to *each* file.
546 `list-diary-entries-hook' adds or manipulates diary entries from
547 external sources. Used, for example, to include diary entries
548 from other files or to sort the diary entries. Invoked *once* only,
549 before the display hook is run.
551 `diary-display-hook' does the actual display of information. If this is
552 nil, `simple-diary-display' will be used. Use `add-hook' to use
553 `fancy-diary-display', if desired. If you want no diary display, use
554 add-hook to set this to `ignore'.
556 `diary-hook' is run last. This can be used for an appointment
557 notification function.
559 Functions called by these hooks may use the variables ORIGINAL-DATE
560 and NUMBER, which are the arguments with which this function was called.
561 Note that hook functions should _not_ use DATE, but ORIGINAL-DATE.
562 \(Sexp diary entries may use DATE - see `list-sexp-diary-entries'.)
564 If LIST-ONLY is non-nil don't modify or display the buffer, only return a list."
566 (setq number
(if (vectorp number-of-diary-entries
)
567 (aref number-of-diary-entries
(calendar-day-of-week date
))
568 number-of-diary-entries
)))
570 (let ((original-date date
) ; save for possible use in the hooks
573 (date-string (calendar-date-string date
))
574 (d-file (substitute-in-file-name diary-file
)))
575 (message "Preparing diary...")
577 (let ((diary-buffer (find-buffer-visiting d-file
)))
578 (if (not diary-buffer
)
579 (set-buffer (find-file-noselect d-file t
))
580 (set-buffer diary-buffer
)
581 (or (verify-visited-file-modtime diary-buffer
)
582 (revert-buffer t t
))))
583 ;; Setup things like the header-line-format and invisibility-spec.
584 (if (eq major-mode default-major-mode
)
586 ;; This kludge is to make customizations to
587 ;; diary-header-line-flag after diary has been displayed
588 ;; take effect. Unconditionally calling (diary-mode)
589 ;; clobbers file local variables.
590 ;; http://lists.gnu.org/archive/html/emacs-pretest-bug/2007-03/msg00363.html
591 ;; http://lists.gnu.org/archive/html/emacs-pretest-bug/2007-04/msg00404.html
592 (if (eq major-mode
'diary-mode
)
593 (setq header-line-format
(and diary-header-line-flag
594 diary-header-line-format
))))
595 ;; d-s-p is passed to the diary display function.
596 (let ((diary-saved-point (point)))
598 (setq file-glob-attrs
(nth 1 (diary-pull-attrs nil
"")))
599 (with-syntax-table diary-syntax-table
600 (let ((mark (regexp-quote diary-nonmarking-symbol
)))
601 (goto-char (point-min))
603 (let ((ol (make-overlay (point-min) (point-max) nil t nil
)))
604 (set (make-local-variable 'diary-selective-display
) t
)
605 (overlay-put ol
'invisible
'diary
)
606 (overlay-put ol
'evaporate t
)))
607 (dotimes (idummy number
)
608 (let ((month (extract-calendar-month date
))
609 (day (extract-calendar-day date
))
610 (year (extract-calendar-year date
))
611 (entry-found (list-sexp-diary-entries date
)))
612 (dolist (date-form diary-date-forms
)
613 (let* ((backup (when (eq (car date-form
) 'backup
)
614 (setq date-form
(cdr date-form
))
617 (format "%s\\|%s\\.?"
618 (calendar-day-name date
)
619 (calendar-day-name date
'abbrev
)))
621 (format "\\*\\|%s\\|%s\\.?"
622 (calendar-month-name month
)
623 (calendar-month-name month
'abbrev
)))
624 (month (concat "\\*\\|0*" (int-to-string month
)))
625 (day (concat "\\*\\|0*" (int-to-string day
)))
628 "\\*\\|0*" (int-to-string year
)
629 (if abbreviated-calendar-year
630 (concat "\\|" (format "%02d" (% year
100)))
635 ;; This must be let* so that date-form
637 (mapconcat 'eval date-form
"\\)\\(?:")
639 (case-fold-search t
))
640 (goto-char (point-min))
641 (while (re-search-forward regexp nil t
)
642 (if backup
(re-search-backward "\\<" nil t
))
643 (if (and (bolp) (not (looking-at "[ \t]")))
644 ;; Diary entry that consists only of date.
646 ;; Found a nonempty diary entry--make it
647 ;; visible and add it to the list.
649 (if (looking-at "[ \t]*\n[ \t]") (forward-line 1))
650 (let ((entry-start (point))
654 (if (and (bolp) (> number
1)) -
1 0)))
656 (while (looking-at "[ \t]")
658 (unless (and (eobp) (not (bolp)))
661 (remove-overlays date-start
(point)
663 (setq temp
(diary-pull-attrs
664 (buffer-substring entry-start
(point))
670 (1+ date-start
) (1- entry-start
))
671 (copy-marker entry-start
) (nth 1 temp
)))))))
673 (not diary-list-include-blanks
)
674 (add-to-diary-list date
"" "" "" ""))
676 (calendar-gregorian-from-absolute
677 (1+ (calendar-absolute-from-gregorian date
))))
678 (setq entry-found nil
)))))
679 (goto-char (point-min))
680 (run-hooks 'nongregorian-diary-listing-hook
681 'list-diary-entries-hook
)
683 (if diary-display-hook
684 (run-hooks 'diary-display-hook
)
685 (simple-diary-display)))
686 (run-hooks 'diary-hook
)
687 diary-entries-list
))))))
689 (defun diary-unhide-everything ()
690 "Show all invisible text in the diary."
691 (kill-local-variable 'diary-selective-display
)
692 (remove-overlays (point-min) (point-max) 'invisible
'diary
)
693 (kill-local-variable 'mode-line-format
))
695 (defvar original-date
) ; bound in diary-list-entries
698 (defun include-other-diary-files ()
699 "Include the diary entries from other diary files with those of `diary-file'.
700 This function is suitable for use in `list-diary-entries-hook';
701 it enables you to use shared diary files together with your own.
702 The files included are specified in the `diary-file' by lines of this form:
703 #include \"filename\"
704 This is recursive; that is, #include directives in diary files thus included
705 are obeyed. You can change the `#include' to some other string by
706 changing the variable `diary-include-string'."
707 (goto-char (point-min))
708 (while (re-search-forward
711 (regexp-quote diary-include-string
)
714 (let ((diary-file (substitute-in-file-name
715 (match-string-no-properties 1)))
716 (diary-list-include-blanks nil
)
717 (list-diary-entries-hook 'include-other-diary-files
)
718 (diary-display-hook 'ignore
)
720 (if (file-exists-p diary-file
)
721 (if (file-readable-p diary-file
)
723 (setq diary-entries-list
724 (append diary-entries-list
725 (diary-list-entries original-date number
)))
726 (with-current-buffer (find-buffer-visiting diary-file
)
727 (diary-unhide-everything)))
729 (message "Can't read included diary file %s" diary-file
)
732 (message "Can't find included diary file %s" diary-file
)
734 (goto-char (point-min)))
736 ;; Bound in diary-list-entries.
738 (defvar diary-saved-point
)
740 (defun simple-diary-display ()
741 "Display the diary buffer if there are any relevant entries or holidays."
742 (let* ((holiday-list (if holidays-in-diary-buffer
743 (calendar-check-holidays original-date
)))
744 (hol-string (format "%s%s%s"
746 (if holiday-list
": " "")
747 (mapconcat 'identity holiday-list
"; ")))
748 (msg (format "No diary entries for %s" hol-string
))
749 ;; If selected window is dedicated (to the calendar),
750 ;; need a new one to display the diary.
751 (pop-up-frames (or pop-up-frames
752 (window-dedicated-p (selected-window)))))
753 (calendar-set-mode-line (format "Diary for %s" hol-string
))
754 (if (or (not diary-entries-list
)
755 (and (not (cdr diary-entries-list
))
756 (string-equal (car (cdr (car diary-entries-list
))) "")))
757 (if (< (length msg
) (frame-width))
759 (set-buffer (get-buffer-create holiday-buffer
))
760 (setq buffer-read-only nil
)
761 (calendar-set-mode-line date-string
)
763 (insert (mapconcat 'identity holiday-list
"\n"))
764 (goto-char (point-min))
765 (set-buffer-modified-p nil
)
766 (setq buffer-read-only t
)
767 (display-buffer holiday-buffer
)
768 (message "No diary entries for %s" date-string
))
770 (find-buffer-visiting (substitute-in-file-name diary-file
))
771 (let ((window (display-buffer (current-buffer))))
772 ;; d-s-p is passed from diary-list-entries.
773 (set-window-point window diary-saved-point
)
774 (set-window-start window
(point-min))))
775 (message "Preparing diary...done"))))
777 (defface diary-button
'((((type pc
) (class color
))
778 (:foreground
"lightblue")))
779 "Default face used for buttons."
782 ;; Backward-compatibility alias. FIXME make obsolete.
783 (put 'diary-button-face
'face-alias
'diary-button
)
785 (define-button-type 'diary-entry
786 'action
#'diary-goto-entry
789 (defun diary-goto-entry (button)
790 "Jump to the diary entry for the button at point."
791 (let* ((locator (button-get button
'locator
))
792 (marker (car locator
))
794 ;; If marker pointing to diary location is valid, use that.
795 (if (and marker
(setq markbuf
(marker-buffer marker
)))
797 (pop-to-buffer markbuf
)
798 (goto-char (marker-position marker
)))
799 ;; Marker is invalid (eg buffer has been killed).
800 (or (and (setq file
(cadr locator
))
802 (find-file-other-window file
)
804 (when (eq major-mode default-major-mode
) (diary-mode))
805 (goto-char (point-min))
806 (if (re-search-forward (format "%s.*\\(%s\\)"
807 (regexp-quote (nth 2 locator
))
808 (regexp-quote (nth 3 locator
)))
810 (goto-char (match-beginning 1)))))
811 (message "Unable to locate this diary entry")))))
813 (defun fancy-diary-display ()
814 "Prepare a diary buffer with relevant entries in a fancy, noneditable form.
815 This function is provided for optional use as the `diary-display-hook'."
816 ;; Turn off selective-display in the diary file's buffer.
818 (find-buffer-visiting (substitute-in-file-name diary-file
))
819 (diary-unhide-everything))
820 (if (or (not diary-entries-list
)
821 (and (not (cdr diary-entries-list
))
822 (string-equal (car (cdr (car diary-entries-list
))) "")))
823 (let* ((holiday-list (if holidays-in-diary-buffer
824 (calendar-check-holidays original-date
)))
825 (msg (format "No diary entries for %s %s"
826 (concat date-string
(if holiday-list
":" ""))
827 (mapconcat 'identity holiday-list
"; "))))
828 (if (<= (length msg
) (frame-width))
830 (set-buffer (get-buffer-create holiday-buffer
))
831 (setq buffer-read-only nil
)
833 (insert (mapconcat 'identity holiday-list
"\n"))
834 (goto-char (point-min))
835 (set-buffer-modified-p nil
)
836 (setq buffer-read-only t
)
837 (display-buffer holiday-buffer
)
838 (message "No diary entries for %s" date-string
)))
839 ;; Prepare the fancy diary buffer.
841 (make-fancy-diary-buffer)
842 (setq buffer-read-only nil
)
843 (let ((entry-list diary-entries-list
)
845 (holiday-list-last-month 1)
846 (holiday-list-last-year 1)
848 (dolist (entry entry-list
)
849 (if (not (calendar-date-equal date
(car entry
)))
851 (setq date
(car entry
))
852 (and holidays-in-diary-buffer
853 (calendar-date-compare
854 (list (list holiday-list-last-month
855 (calendar-last-day-of-month
856 holiday-list-last-month
857 holiday-list-last-year
)
858 holiday-list-last-year
))
860 ;; We need to get the holidays for the next 3 months.
861 (setq holiday-list-last-month
862 (extract-calendar-month date
)
863 holiday-list-last-year
864 (extract-calendar-year date
))
866 (increment-calendar-month
867 holiday-list-last-month holiday-list-last-year
1)
870 (let ((displayed-month holiday-list-last-month
)
871 (displayed-year holiday-list-last-year
))
872 (calendar-holiday-list)))
873 (increment-calendar-month
874 holiday-list-last-month holiday-list-last-year
1))
875 (let (date-holiday-list)
876 ;; Make a list of all holidays for date.
877 (dolist (h holiday-list
)
878 (if (calendar-date-equal date
(car h
))
879 (setq date-holiday-list
(append date-holiday-list
881 (insert (if (bobp) "" ?
\n) (calendar-date-string date
))
882 (if date-holiday-list
(insert ": "))
883 (let ((l (current-column))
885 (insert (mapconcat (lambda (x)
886 (if (< longest
(length x
))
887 (setq longest
(length x
)))
890 (concat "\n" (make-string l ?
))))
891 (insert ?
\n (make-string (+ l longest
) ?
=) ?
\n)))))
892 (let ((this-entry (cadr entry
))
894 (unless (zerop (length this-entry
))
895 (if (setq this-loc
(nth 3 entry
))
896 (insert-button (concat this-entry
"\n")
897 ;; (MARKER FILENAME SPECIFIER LITERAL)
898 'locator
(list (car this-loc
)
904 (insert this-entry ?
\n))
906 (let* ((marks (nth 4 entry
))
910 (setq temp-face
(make-symbol
913 (mapcar (lambda (sym)
918 (make-face temp-face
)
919 ;; Remove :face info from the marks,
920 ;; copy the face info into temp-face
921 (while (setq faceinfo
(memq :face faceinfo
))
922 (copy-face (read (nth 1 faceinfo
)) temp-face
)
923 (setcar faceinfo nil
)
924 (setcar (cdr faceinfo
) nil
))
925 (setq marks
(delq nil marks
))
926 ;; Apply the font aspects.
927 (apply 'set-face-attribute temp-face nil marks
)
928 (search-backward this-entry
)
930 (make-overlay (match-beginning 0) (match-end 0))
931 'face temp-face
))))))))
932 (set-buffer-modified-p nil
)
933 (goto-char (point-min))
934 (setq buffer-read-only t
)
935 (display-buffer fancy-diary-buffer
)
936 (fancy-diary-display-mode)
937 (calendar-set-mode-line date-string
)
938 (message "Preparing diary...done"))))
940 (defun make-fancy-diary-buffer ()
941 "Create and return the initial fancy diary buffer."
942 (with-current-buffer (get-buffer-create fancy-diary-buffer
)
943 (setq buffer-read-only nil
)
944 (calendar-set-mode-line "Diary Entries")
946 (set-buffer-modified-p nil
)
947 (setq buffer-read-only t
)
948 (get-buffer fancy-diary-buffer
)))
950 (defun print-diary-entries ()
951 "Print a hard copy of the diary display.
953 If the simple diary display is being used, prepare a temp buffer with the
954 visible lines of the diary buffer, add a heading line composed from the mode
955 line, print the temp buffer, and destroy it.
957 If the fancy diary display is being used, just print the buffer.
959 The hooks given by the variable `print-diary-entries-hook' are called to do
960 the actual printing."
962 (if (bufferp (get-buffer fancy-diary-buffer
))
963 (with-current-buffer (get-buffer fancy-diary-buffer
)
964 (run-hooks 'print-diary-entries-hook
))
966 (find-buffer-visiting (substitute-in-file-name diary-file
))))
968 (let ((temp-buffer (get-buffer-create " *Printable Diary Entries*"))
970 (with-current-buffer diary-buffer
972 (if (not (stringp mode-line-format
))
974 (string-match "^-*\\([^-].*[^-]\\)-*$" mode-line-format
)
975 (match-string 1 mode-line-format
)))
976 (let ((start (point-min))
980 (setq end
(next-single-char-property-change
982 (if (get-char-property start
'invisible
)
984 (with-current-buffer temp-buffer
985 (insert-buffer-substring diary-buffer
986 start
(or end
(point-max)))))
988 (and end
(< end
(point-max))))))
989 (set-buffer temp-buffer
)
990 (goto-char (point-min))
992 (make-string (length heading
) ?
=) "\n")
993 (run-hooks 'print-diary-entries-hook
)
994 (kill-buffer temp-buffer
)))
995 (error "You don't have a diary buffer!")))))
997 (define-obsolete-function-alias 'show-all-diary-entries
'diary-show-all-entries
)
999 (defun diary-show-all-entries ()
1000 "Show all of the diary entries in the diary file.
1001 This function gets rid of the selective display of the diary file so that
1002 all entries, not just some, are visible. If there is no diary buffer, one
1005 (let ((d-file (diary-check-diary-file))
1006 (pop-up-frames (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)))))
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 You can call `diary-mail-entries' every night using an at/cron job.
1020 For example, this script will run the program at 2am daily. Since
1021 `emacs -batch' does not load your `.emacs' file, you must ensure that
1022 all relevant variables are set, as done here.
1025 # diary-rem.sh -- repeatedly run the Emacs diary-reminder
1027 -eval \"(setq diary-mail-days 3 \\
1028 diary-file \\\"/path/to/diary.file\\\" \\
1029 european-calendar-style t \\
1030 diary-mail-addr \\\"user@host.name\\\" )\" \\
1031 -l diary-lib -f diary-mail-entries
1032 at -f diary-rem.sh 0200 tomorrow
1034 You may have to tweak the syntax of the `at' command to suit your
1035 system. Alternatively, you can specify a cron entry:
1036 0 1 * * * diary-rem.sh
1037 to run it every morning at 1am."
1039 (if (string-equal diary-mail-addr
"")
1040 (error "You must set `diary-mail-addr' to use this command")
1041 (let ((diary-display-hook 'fancy-diary-display
))
1042 (diary-list-entries (calendar-current-date) (or ndays diary-mail-days
)))
1043 (compose-mail diary-mail-addr
1044 (concat "Diary entries generated "
1045 (calendar-date-string (calendar-current-date))))
1047 (if (get-buffer fancy-diary-buffer
)
1048 (with-current-buffer fancy-diary-buffer
(buffer-string))
1049 "No entries found"))
1050 (call-interactively (get mail-user-agent
'sendfunc
))))
1052 (defun diary-name-pattern (string-array &optional abbrev-array paren
)
1053 "Return a regexp matching the strings in the array STRING-ARRAY.
1054 If the optional argument ABBREV-ARRAY is present, then the function
1055 `calendar-abbrev-construct' is used to construct abbreviations from the
1056 two supplied arrays. The returned regexp will then also match these
1057 abbreviations, with or without final `.' characters. If the optional
1058 argument PAREN is non-nil, the regexp is surrounded by parentheses."
1059 (regexp-opt (append string-array
1061 (calendar-abbrev-construct abbrev-array
1064 (calendar-abbrev-construct abbrev-array
1070 (defvar marking-diary-entries nil
1071 "True during the marking of diary entries, nil otherwise.")
1073 (defvar marking-diary-entry nil
1074 "True during the marking of diary entries, if current entry is marking.")
1077 (defun mark-diary-entries (&optional redraw
)
1078 "Mark days in the calendar window that have diary entries.
1079 Each entry in the diary file visible in the calendar window is
1080 marked. After the entries are marked, the hooks
1081 `nongregorian-diary-marking-hook' and `mark-diary-entries-hook'
1082 are run. If the optional argument REDRAW is non-nil (which is
1083 the case interactively, for example) then any existing diary
1084 marks are first removed. This is intended to deal with deleted
1087 ;; To remove any deleted diary entries. Do not redraw when:
1088 ;; i) processing #include diary files (else only get the marks from
1089 ;; the last #include file processed).
1090 ;; ii) called via calendar-redraw (since calendar has already been
1092 ;; Use of REDRAW handles both of these cases.
1093 (when (and redraw mark-diary-entries-in-calendar
)
1094 (setq mark-diary-entries-in-calendar nil
)
1096 (let ((marking-diary-entries t
)
1097 file-glob-attrs marks
)
1098 (with-current-buffer (find-file-noselect (diary-check-diary-file) t
)
1100 (when (eq major-mode default-major-mode
) (diary-mode))
1101 (setq mark-diary-entries-in-calendar t
)
1102 (message "Marking diary entries...")
1103 (setq file-glob-attrs
(nth 1 (diary-pull-attrs nil
'())))
1104 (with-syntax-table diary-syntax-table
1105 (dolist (date-form diary-date-forms
)
1106 (if (eq (car date-form
) 'backup
)
1107 (setq date-form
(cdr date-form
))) ; ignore 'backup directive
1109 (diary-name-pattern calendar-day-name-array
1110 calendar-day-abbrev-array
))
1113 (diary-name-pattern calendar-month-name-array
1114 calendar-month-abbrev-array
)))
1115 (month "[0-9]+\\|\\*")
1116 (day "[0-9]+\\|\\*")
1117 (year "[0-9]+\\|\\*")
1118 (l (length date-form
))
1119 (d-name-pos (- l
(length (memq 'dayname date-form
))))
1120 (d-name-pos (if (/= l d-name-pos
) (+ 1 d-name-pos
)))
1121 (m-name-pos (- l
(length (memq 'monthname date-form
))))
1122 (m-name-pos (if (/= l m-name-pos
) (+ 1 m-name-pos
)))
1123 (d-pos (- l
(length (memq 'day date-form
))))
1124 (d-pos (if (/= l d-pos
) (+ 1 d-pos
)))
1125 (m-pos (- l
(length (memq 'month date-form
))))
1126 (m-pos (if (/= l m-pos
) (+ 1 m-pos
)))
1127 (y-pos (- l
(length (memq 'year date-form
))))
1128 (y-pos (if (/= l y-pos
) (+ 1 y-pos
)))
1132 (mapconcat 'eval date-form
"\\)\\(")
1134 (case-fold-search t
))
1135 (goto-char (point-min))
1136 (while (re-search-forward regexp nil t
)
1139 (match-string-no-properties d-name-pos
)))
1142 (match-string-no-properties m-name-pos
)))
1143 (mm (string-to-number
1145 (match-string-no-properties m-pos
)
1147 (dd (string-to-number
1149 (match-string-no-properties d-pos
)
1152 (match-string-no-properties y-pos
)))
1155 (if (and (= (length y-str
) 2)
1156 abbreviated-calendar-year
)
1158 (extract-calendar-year
1159 (calendar-current-date)))
1160 (y (+ (string-to-number y-str
)
1162 (/ current-y
100)))))
1163 (if (> (- y current-y
) 50)
1165 (if (> (- current-y y
) 50)
1168 (string-to-number y-str
)))))
1171 (buffer-substring-no-properties
1172 (point) (line-end-position))
1175 (mark-calendar-days-named
1178 (calendar-make-alist
1179 calendar-day-name-array
1180 0 nil calendar-day-abbrev-array
) t
)) marks
)
1183 (if (string-equal mm-name
"*") 0
1186 (calendar-make-alist
1187 calendar-month-name-array
1188 1 nil calendar-month-abbrev-array
) t
)))))
1189 (mark-calendar-date-pattern mm dd yy marks
))))))
1190 (mark-sexp-diary-entries)
1191 (run-hooks 'nongregorian-diary-marking-hook
1192 'mark-diary-entries-hook
))
1193 (message "Marking diary entries...done")))))
1195 (defvar displayed-year
) ; bound in generate-calendar
1196 (defvar displayed-month
)
1198 (defun mark-sexp-diary-entries ()
1199 "Mark days in the calendar window that have sexp diary entries.
1200 Each entry in the diary file (or included files) visible in the calendar window
1201 is marked. See the documentation for the function `list-sexp-diary-entries'."
1202 (let* ((sexp-mark (regexp-quote sexp-diary-entry-symbol
))
1203 (s-entry (concat "^\\("
1204 sexp-mark
"(\\)\\|\\("
1205 (regexp-quote diary-nonmarking-symbol
)
1206 sexp-mark
"(diary-remind\\)"))
1207 (file-glob-attrs (nth 1 (diary-pull-attrs nil
'())))
1208 m y first-date last-date mark file-glob-attrs
)
1209 (with-current-buffer calendar-buffer
1210 (setq m displayed-month
)
1211 (setq y displayed-year
))
1212 (increment-calendar-month m y -
1)
1214 (calendar-absolute-from-gregorian (list m
1 y
)))
1215 (increment-calendar-month m y
2)
1217 (calendar-absolute-from-gregorian
1218 (list m
(calendar-last-day-of-month m y
) y
)))
1219 (goto-char (point-min))
1220 (while (re-search-forward s-entry nil t
)
1221 (setq marking-diary-entry
(char-equal (preceding-char) ?\
())
1222 (re-search-backward "(")
1223 (let ((sexp-start (point))
1224 sexp entry entry-start marks
)
1226 (setq sexp
(buffer-substring-no-properties sexp-start
(point)))
1228 (if (and (bolp) (not (looking-at "[ \t]")))
1229 ;; Diary entry consists only of the sexp.
1233 (setq entry-start
(point))
1234 ;; Find end of entry.
1236 (while (looking-at "[ \t]")
1238 (if (bolp) (backward-char 1))
1239 (setq entry
(buffer-substring-no-properties entry-start
(point))))
1240 (calendar-for-loop date from first-date to last-date do
1241 (if (setq mark
(diary-sexp-entry sexp entry
1242 (calendar-gregorian-from-absolute date
)))
1244 (setq marks
(diary-pull-attrs entry file-glob-attrs
)
1245 marks
(nth 1 (diary-pull-attrs entry file-glob-attrs
)))
1246 (mark-visible-calendar-date
1247 (calendar-gregorian-from-absolute date
)
1248 (if (< 0 (length marks
))
1251 (car mark
)))))))))))
1253 (defun mark-included-diary-files ()
1254 "Mark the diary entries from other diary files with those of the diary file.
1255 This function is suitable for use as the `mark-diary-entries-hook'; it enables
1256 you to use shared diary files together with your own. The files included are
1257 specified in the `diary-file' by lines of this form:
1258 #include \"filename\"
1259 This is recursive; that is, #include directives in diary files thus included
1260 are obeyed. You can change the `#include' to some other string by
1261 changing the variable `diary-include-string'."
1262 (goto-char (point-min))
1263 (while (re-search-forward
1266 (regexp-quote diary-include-string
)
1267 " \"\\([^\"]*\\)\"")
1269 (let* ((diary-file (substitute-in-file-name
1270 (match-string-no-properties 1)))
1271 (mark-diary-entries-hook 'mark-included-diary-files
)
1272 (dbuff (find-buffer-visiting diary-file
)))
1273 (if (file-exists-p diary-file
)
1274 (if (file-readable-p diary-file
)
1276 (mark-diary-entries)
1278 (kill-buffer (find-buffer-visiting diary-file
))))
1280 (message "Can't read included diary file %s" diary-file
)
1283 (message "Can't find included diary file %s" diary-file
)
1285 (goto-char (point-min)))
1287 (defun mark-calendar-days-named (dayname &optional color
)
1288 "Mark all dates in the calendar window that are day DAYNAME of the week.
1289 0 means all Sundays, 1 means all Mondays, and so on.
1290 Optional argument COLOR is passed to `mark-visible-calendar-date' as MARK."
1291 (with-current-buffer calendar-buffer
1292 (let ((prev-month displayed-month
)
1293 (prev-year displayed-year
)
1294 (succ-month displayed-month
)
1295 (succ-year displayed-year
)
1298 (increment-calendar-month succ-month succ-year
1)
1299 (increment-calendar-month prev-month prev-year -
1)
1300 (setq day
(calendar-absolute-from-gregorian
1301 (calendar-nth-named-day 1 dayname prev-month prev-year
))
1302 last-day
(calendar-absolute-from-gregorian
1303 (calendar-nth-named-day -
1 dayname succ-month succ-year
)))
1304 (while (<= day last-day
)
1305 (mark-visible-calendar-date (calendar-gregorian-from-absolute day
)
1307 (setq day
(+ day
7))))))
1309 (defun mark-calendar-date-pattern (month day year
&optional color
)
1310 "Mark all dates in the calendar window that conform to MONTH/DAY/YEAR.
1311 A value of 0 in any position is a wildcard.
1312 Optional argument COLOR is passed to `mark-visible-calendar-date' as MARK."
1313 (with-current-buffer calendar-buffer
1314 (let ((m displayed-month
)
1316 (increment-calendar-month m y -
1)
1318 (mark-calendar-month m y month day year color
)
1319 (increment-calendar-month m y
1)))))
1321 (defun mark-calendar-month (month year p-month p-day p-year
&optional color
)
1322 "Mark dates in the MONTH/YEAR that conform to pattern P-MONTH/P-DAY/P-YEAR.
1323 A value of 0 in any position of the pattern is a wildcard.
1324 Optional argument COLOR is passed to `mark-visible-calendar-date' as MARK."
1325 (if (or (and (= month p-month
)
1326 (or (zerop p-year
) (= year p-year
)))
1327 (and (zerop p-month
)
1328 (or (zerop p-year
) (= year p-year
))))
1331 i from
1 to
(calendar-last-day-of-month month year
) do
1332 (mark-visible-calendar-date (list month i year
) color
))
1333 (mark-visible-calendar-date (list month p-day year
) color
))))
1335 (defun sort-diary-entries ()
1336 "Sort the list of diary entries by time of day."
1337 (setq diary-entries-list
(sort diary-entries-list
'diary-entry-compare
)))
1339 (defun diary-entry-compare (e1 e2
)
1340 "Return t if E1 is earlier than E2."
1341 (or (calendar-date-compare e1 e2
)
1342 (and (calendar-date-equal (car e1
) (car e2
))
1343 (let* ((ts1 (cadr e1
)) (t1 (diary-entry-time ts1
))
1344 (ts2 (cadr e2
)) (t2 (diary-entry-time ts2
)))
1347 (string-lessp ts1 ts2
)))))))
1349 (defun diary-entry-time (s)
1350 "Return time at the beginning of the string S as a military-style integer.
1351 For example, returns 1325 for 1:25pm.
1353 Returns `diary-unknown-time' (default value -9999) if no time is recognized.
1354 The recognized forms are XXXX, X:XX, or XX:XX (military time), and XXam,
1355 XXAM, XXpm, XXPM, XX:XXam, XX:XXAM XX:XXpm, or XX:XXPM. A period (.) can
1356 be used instead of a colon (:) to separate the hour and minute parts."
1357 (let ((case-fold-search nil
))
1358 (cond ((string-match ; military time
1359 "\\`[ \t\n]*\\([0-9]?[0-9]\\)[:.]?\\([0-9][0-9]\\)\\(\\>\\|[^ap]\\)"
1361 (+ (* 100 (string-to-number (match-string 1 s
)))
1362 (string-to-number (match-string 2 s
))))
1363 ((string-match ; hour only (XXam or XXpm)
1364 "\\`[ \t\n]*\\([0-9]?[0-9]\\)\\([ap]\\)m\\>" s
)
1365 (+ (* 100 (%
(string-to-number (match-string 1 s
)) 12))
1366 (if (equal ?a
(downcase (aref s
(match-beginning 2))))
1368 ((string-match ; hour and minute (XX:XXam or XX:XXpm)
1369 "\\`[ \t\n]*\\([0-9]?[0-9]\\)[:.]\\([0-9][0-9]\\)\\([ap]\\)m\\>" s
)
1370 (+ (* 100 (%
(string-to-number (match-string 1 s
)) 12))
1371 (string-to-number (match-string 2 s
))
1372 (if (equal ?a
(downcase (aref s
(match-beginning 3))))
1374 (t diary-unknown-time
)))) ; unrecognizable
1376 (defun list-sexp-diary-entries (date)
1377 "Add sexp entries for DATE from the diary file to `diary-entries-list'.
1378 Also, make them visible in the diary file. Returns t if any entries were
1381 Sexp diary entries must be prefaced by a `sexp-diary-entry-symbol' (normally
1382 `%%'). The form of a sexp diary entry is
1386 Both ENTRY and DATE are globally available when the SEXP is evaluated. If the
1387 SEXP yields the value nil, the diary entry does not apply. If it yields a
1388 non-nil value, ENTRY will be taken to apply to DATE; if the non-nil value is a
1389 string, that string will be the diary entry in the fancy diary display.
1391 For example, the following diary entry will apply to the 21st of the month
1392 if it is a weekday and the Friday before if the 21st is on a weekend:
1394 &%%(let ((dayname (calendar-day-of-week date))
1395 (day (extract-calendar-day date)))
1397 (and (= day 21) (memq dayname '(1 2 3 4 5)))
1398 (and (memq day '(19 20)) (= dayname 5)))
1399 ) UIUC pay checks deposited
1401 A number of built-in functions are available for this type of diary entry:
1403 %%(diary-date MONTH DAY YEAR &optional MARK) text
1404 Entry applies if date is MONTH, DAY, YEAR if
1405 `european-calendar-style' is nil, and DAY, MONTH, YEAR if
1406 `european-calendar-style' is t. DAY, MONTH, and YEAR
1407 can be lists of integers, the constant t, or an integer.
1408 The constant t means all values. An optional parameter
1409 MARK specifies a face or single-character string to use
1410 when highlighting the day in the calendar.
1412 %%(diary-float MONTH DAYNAME N &optional DAY MARK) text
1413 Entry will appear on the Nth DAYNAME of MONTH.
1414 (DAYNAME=0 means Sunday, 1 means Monday, and so on;
1415 if N is negative it counts backward from the end of
1416 the month. MONTH can be a list of months, a single
1417 month, or t to specify all months. Optional DAY means
1418 Nth DAYNAME of MONTH on or after/before DAY. DAY defaults
1419 to 1 if N>0 and the last day of the month if N<0. An
1420 optional parameter MARK specifies a face or single-character
1421 string to use when highlighting the day in the calendar.
1423 %%(diary-block M1 D1 Y1 M2 D2 Y2 &optional MARK) text
1424 Entry will appear on dates between M1/D1/Y1 and M2/D2/Y2,
1425 inclusive. (If `european-calendar-style' is t, the
1426 order of the parameters should be changed to D1, M1, Y1,
1427 D2, M2, Y2.) An optional parameter MARK specifies a face
1428 or single-character string to use when highlighting the
1429 day in the calendar.
1431 %%(diary-anniversary MONTH DAY YEAR &optional MARK) text
1432 Entry will appear on anniversary dates of MONTH DAY, YEAR.
1433 (If `european-calendar-style' is t, the order of the
1434 parameters should be changed to DAY, MONTH, YEAR.) Text
1435 can contain %d or %d%s; %d will be replaced by the number
1436 of years since the MONTH DAY, YEAR and %s will be replaced
1437 by the ordinal ending of that number (that is, `st', `nd',
1438 `rd' or `th', as appropriate. The anniversary of February
1439 29 is considered to be March 1 in a non-leap year. An
1440 optional parameter MARK specifies a face or single-character
1441 string to use when highlighting the day in the calendar.
1443 %%(diary-cyclic N MONTH DAY YEAR &optional MARK) text
1444 Entry will appear every N days, starting MONTH DAY, YEAR.
1445 (If `european-calendar-style' is t, the order of the
1446 parameters should be changed to N, DAY, MONTH, YEAR.) Text
1447 can contain %d or %d%s; %d will be replaced by the number
1448 of repetitions since the MONTH DAY, YEAR and %s will
1449 be replaced by the ordinal ending of that number (that is,
1450 `st', `nd', `rd' or `th', as appropriate. An optional
1451 parameter MARK specifies a face or single-character string
1452 to use when highlighting the day in the calendar.
1454 %%(diary-remind SEXP DAYS &optional MARKING) text
1455 Entry is a reminder for diary sexp SEXP. DAYS is either a
1456 single number or a list of numbers indicating the number(s)
1457 of days before the event that the warning(s) should occur.
1458 If the current date is (one of) DAYS before the event
1459 indicated by EXPR, then a suitable message (as specified
1460 by `diary-remind-message') appears. In addition to the
1461 reminders beforehand, the diary entry also appears on
1462 the date itself. If optional MARKING is non-nil then the
1463 *reminders* are marked on the calendar. Marking of
1464 reminders is independent of whether the entry *itself* is
1465 a marking or nonmarking one.
1467 %%(diary-day-of-year)
1468 Diary entries giving the day of the year and the number of
1469 days remaining in the year will be made every day. Note
1470 that since there is no text, it makes sense only if the
1471 fancy diary display is used.
1474 Diary entries giving the corresponding ISO commercial date
1475 will be made every day. Note that since there is no text,
1476 it makes sense only if the fancy diary display is used.
1478 %%(diary-french-date)
1479 Diary entries giving the corresponding French Revolutionary
1480 date will be made every day. Note that since there is no
1481 text, it makes sense only if the fancy diary display is used.
1483 %%(diary-islamic-date)
1484 Diary entries giving the corresponding Islamic date will be
1485 made every day. Note that since there is no text, it
1486 makes sense only if the fancy diary display is used.
1488 %%(diary-hebrew-date)
1489 Diary entries giving the corresponding Hebrew date will be
1490 made every day. Note that since there is no text, it
1491 makes sense only if the fancy diary display is used.
1493 %%(diary-astro-day-number) Diary entries giving the corresponding
1494 astronomical (Julian) day number will be made every day.
1495 Note that since there is no text, it makes sense only if the
1496 fancy diary display is used.
1498 %%(diary-julian-date) Diary entries giving the corresponding
1499 Julian date will be made every day. Note that since
1500 there is no text, it makes sense only if the fancy diary
1503 %%(diary-sunrise-sunset)
1504 Diary entries giving the local times of sunrise and sunset
1505 will be made every day. Note that since there is no text,
1506 it makes sense only if the fancy diary display is used.
1507 Floating point required.
1509 %%(diary-phases-of-moon)
1510 Diary entries giving the times of the phases of the moon
1511 will be when appropriate. Note that since there is no text,
1512 it makes sense only if the fancy diary display is used.
1513 Floating point required.
1515 %%(diary-yahrzeit MONTH DAY YEAR) text
1516 Text is assumed to be the name of the person; the date is
1517 the date of death on the *civil* calendar. The diary entry
1518 will appear on the proper Hebrew-date anniversary and on the
1519 day before. (If `european-calendar-style' is t, the order
1520 of the parameters should be changed to DAY, MONTH, YEAR.)
1522 %%(diary-rosh-hodesh)
1523 Diary entries will be made on the dates of Rosh Hodesh on
1524 the Hebrew calendar. Note that since there is no text, it
1525 makes sense only if the fancy diary display is used.
1528 Diary entries giving the weekly parasha will be made on
1529 every Saturday. Note that since there is no text, it
1530 makes sense only if the fancy diary display is used.
1533 Diary entries giving the omer count will be made every day
1534 from Passover to Shavuot. Note that since there is no text,
1535 it makes sense only if the fancy diary display is used.
1537 Marking these entries is *extremely* time consuming, so these entries are
1538 best if they are nonmarking."
1539 (let ((s-entry (concat "^"
1540 (regexp-quote diary-nonmarking-symbol
)
1542 (regexp-quote sexp-diary-entry-symbol
)
1544 entry-found file-glob-attrs marks
)
1545 (goto-char (point-min))
1547 (setq file-glob-attrs
(nth 1 (diary-pull-attrs nil
'()))))
1548 (while (re-search-forward s-entry nil t
)
1550 (let ((sexp-start (point))
1551 sexp entry specifier entry-start line-start
)
1553 (setq sexp
(buffer-substring-no-properties sexp-start
(point)))
1554 (setq line-start
(line-end-position 0))
1556 (buffer-substring-no-properties (1+ line-start
) (point))
1557 entry-start
(1+ line-start
))
1559 (if (and (bolp) (not (looking-at "[ \t]")))
1560 ;; Diary entry consists only of the sexp.
1564 (setq entry-start
(point))
1566 (while (looking-at "[ \t]")
1569 (setq entry
(buffer-substring-no-properties entry-start
(point))))
1570 (let ((diary-entry (diary-sexp-entry sexp entry date
))
1572 (setq literal entry
; before evaluation
1573 entry
(if (consp diary-entry
)
1578 (remove-overlays line-start
(point) 'invisible
'diary
)
1579 (if (< 0 (length entry
))
1580 (setq temp
(diary-pull-attrs entry file-glob-attrs
)
1582 marks
(nth 1 temp
)))))
1583 (add-to-diary-list date
1586 (if entry-start
(copy-marker entry-start
)
1590 (setq entry-found
(or entry-found diary-entry
)))))
1593 (defun diary-sexp-entry (sexp entry date
)
1594 "Process a SEXP diary ENTRY for DATE."
1595 (let ((result (if calendar-debug-sexp
1596 (let ((stack-trace-on-error t
))
1597 (eval (car (read-from-string sexp
))))
1599 (eval (car (read-from-string sexp
)))
1602 (message "Bad sexp at line %d in %s: %s"
1603 (count-lines (point-min) (point))
1606 (cond ((stringp result
) result
)
1607 ((and (consp result
)
1608 (stringp (cdr result
))) result
)
1615 ;; To be called from diary-sexp-entry, where DATE, ENTRY are bound.
1616 (defun diary-date (month day year
&optional mark
)
1617 "Specific date(s) diary entry.
1618 Entry applies if date is MONTH, DAY, YEAR if `european-calendar-style' is nil,
1619 and DAY, MONTH, YEAR if `european-calendar-style' is t. DAY, MONTH, and YEAR
1620 can be lists of integers, the constant t, or an integer. The constant t means
1623 An optional parameter MARK specifies a face or single-character string to
1624 use when highlighting the day in the calendar."
1625 (let ((dd (if european-calendar-style
1628 (mm (if european-calendar-style
1631 (m (extract-calendar-month date
))
1632 (y (extract-calendar-year date
))
1633 (d (extract-calendar-day date
)))
1635 (or (and (listp dd
) (memq d dd
))
1638 (or (and (listp mm
) (memq m mm
))
1641 (or (and (listp year
) (memq y year
))
1644 (cons mark entry
))))
1646 ;; To be called from diary-sexp-entry, where DATE, ENTRY are bound.
1647 (defun diary-block (m1 d1 y1 m2 d2 y2
&optional mark
)
1649 Entry applies if date is between, or on one of, two dates.
1650 The order of the parameters is
1651 M1, D1, Y1, M2, D2, Y2 if `european-calendar-style' is nil, and
1652 D1, M1, Y1, D2, M2, Y2 if `european-calendar-style' is t.
1654 An optional parameter MARK specifies a face or single-character string to
1655 use when highlighting the day in the calendar."
1657 (let ((date1 (calendar-absolute-from-gregorian
1658 (if european-calendar-style
1661 (date2 (calendar-absolute-from-gregorian
1662 (if european-calendar-style
1665 (d (calendar-absolute-from-gregorian date
)))
1666 (if (and (<= date1 d
) (<= d date2
))
1667 (cons mark entry
))))
1669 ;; To be called from diary-sexp-entry, where DATE, ENTRY are bound.
1670 (defun diary-float (month dayname n
&optional day mark
)
1671 "Floating diary entry--entry applies if date is the nth dayname of month.
1672 Parameters are MONTH, DAYNAME, N. MONTH can be a list of months, the constant
1673 t, or an integer. The constant t means all months. If N is negative, count
1674 backward from the end of the month.
1676 An optional parameter DAY means the Nth DAYNAME on or after/before MONTH DAY.
1677 Optional MARK specifies a face or single-character string to use when
1678 highlighting the day in the calendar."
1679 ;; This is messy because the diary entry may apply, but the date on which it
1680 ;; is based can be in a different month/year. For example, asking for the
1681 ;; first Monday after December 30. For large values of |n| the problem is
1683 (and (= dayname
(calendar-day-of-week date
))
1684 (let* ((m (extract-calendar-month date
))
1685 (d (extract-calendar-day date
))
1686 (y (extract-calendar-year date
))
1687 ;; Last (n>0) or first (n<0) possible base date for entry.
1689 (calendar-nth-named-absday (- n
) dayname m y d
))
1690 (last-abs (if (> n
0) limit
(+ limit
6)))
1691 (first-abs (if (> n
0) (- limit
6) limit
))
1692 (last (calendar-gregorian-from-absolute last-abs
))
1693 (first (calendar-gregorian-from-absolute first-abs
))
1694 ;; m1, d1 is first possible base date.
1695 (m1 (extract-calendar-month first
))
1696 (d1 (extract-calendar-day first
))
1697 (y1 (extract-calendar-year first
))
1698 ;; m2, d2 is last possible base date.
1699 (m2 (extract-calendar-month last
))
1700 (d2 (extract-calendar-day last
))
1701 (y2 (extract-calendar-year last
)))
1702 (if (or (and (= m1 m2
) ; only possible base dates in one month
1707 (let ((d (or day
(if (> n
0)
1709 (calendar-last-day-of-month m1 y1
)))))
1710 (and (<= d1 d
) (<= d d2
))))
1711 ;; Only possible base dates straddle two months.
1713 (and (= y1 y2
) (< m1 m2
)))
1715 ;; m1, d1 works as a base date.
1721 (<= d1
(or day
(if (> n
0)
1723 (calendar-last-day-of-month m1 y1
)))))
1724 ;; m2, d2 works as a base date.
1725 (and (or (eq month t
)
1729 (<= (or day
(if (> n
0)
1731 (calendar-last-day-of-month m2 y2
)))
1733 (cons mark entry
)))))
1735 ;; To be called from diary-sexp-entry, where DATE, ENTRY are bound.
1736 (defun diary-anniversary (month day
&optional year mark
)
1737 "Anniversary diary entry.
1738 Entry applies if date is the anniversary of MONTH, DAY, YEAR if
1739 `european-calendar-style' is nil, and DAY, MONTH, YEAR if
1740 `european-calendar-style' is t. Diary entry can contain `%d' or `%d%s'; the
1741 %d will be replaced by the number of years since the MONTH DAY, YEAR and the
1742 %s will be replaced by the ordinal ending of that number (that is, `st', `nd',
1743 `rd' or `th', as appropriate. The anniversary of February 29 is considered
1744 to be March 1 in non-leap years.
1746 An optional parameter MARK specifies a face or single-character string to
1747 use when highlighting the day in the calendar."
1748 (let* ((d (if european-calendar-style
1751 (m (if european-calendar-style
1754 (y (extract-calendar-year date
))
1755 (diff (if year
(- y year
) 100)))
1756 (if (and (= m
2) (= d
29) (not (calendar-leap-year-p y
)))
1759 (if (and (> diff
0) (calendar-date-equal (list m d y
) date
))
1760 (cons mark
(format entry diff
(diary-ordinal-suffix diff
))))))
1762 ;; To be called from diary-sexp-entry, where DATE, ENTRY are bound.
1763 (defun diary-cyclic (n month day year
&optional mark
)
1764 "Cycle diary entry--entry applies every N days starting at MONTH, DAY, YEAR.
1765 If `european-calendar-style' is t, parameters are N, DAY, MONTH, YEAR.
1766 ENTRY can contain `%d' or `%d%s'; the %d will be replaced by the number of
1767 repetitions since the MONTH DAY, YEAR and %s will be replaced by the
1768 ordinal ending of that number (that is, `st', `nd', `rd' or `th', as
1771 An optional parameter MARK specifies a face or single-character string to
1772 use when highlighting the day in the calendar."
1773 (let* ((d (if european-calendar-style
1776 (m (if european-calendar-style
1779 (diff (- (calendar-absolute-from-gregorian date
)
1780 (calendar-absolute-from-gregorian
1783 (if (and (>= diff
0) (zerop (% diff n
)))
1784 (cons mark
(format entry cycle
(diary-ordinal-suffix cycle
))))))
1786 (defun diary-ordinal-suffix (n)
1787 "Ordinal suffix for N. (That is, `st', `nd', `rd', or `th', as appropriate.)"
1788 (if (or (memq (% n
100) '(11 12 13))
1791 (aref ["th" "st" "nd" "rd"] (% n
10))))
1793 (defun diary-day-of-year ()
1794 "Day of year and number of days remaining in the year of date diary entry."
1795 (calendar-day-of-year-string date
))
1797 (defun diary-remind (sexp days
&optional marking
)
1798 "Provide a reminder of a diary entry.
1799 SEXP is a diary-sexp. DAYS is either a single number or a list of numbers
1800 indicating the number(s) of days before the event that the warning(s) should
1801 occur on. If the current date is (one of) DAYS before the event indicated by
1802 SEXP, then a suitable message (as specified by `diary-remind-message' is
1805 In addition to the reminders beforehand, the diary entry also appears on the
1808 A `diary-nonmarking-symbol' at the beginning of the line of the `diary-remind'
1809 entry specifies that the diary entry (not the reminder) is non-marking.
1810 Marking of reminders is independent of whether the entry itself is a marking
1811 or nonmarking; if optional parameter MARKING is non-nil then the reminders are
1812 marked on the calendar."
1813 (let ((diary-entry (eval sexp
)))
1815 ;; Diary entry applies on date.
1817 (or (not marking-diary-entries
) marking-diary-entry
))
1819 ;; Diary entry may apply to `days' before date.
1820 ((and (integerp days
)
1821 (not diary-entry
) ; diary entry does not apply to date
1822 (or (not marking-diary-entries
) marking
))
1823 (let ((date (calendar-gregorian-from-absolute
1824 (+ (calendar-absolute-from-gregorian date
) days
))))
1825 (when (setq diary-entry
(eval sexp
)) ; re-evaluate with adjusted date
1826 ;; Discard any mark portion from diary-anniversary, etc.
1827 (if (consp diary-entry
) (setq diary-entry
(cdr diary-entry
)))
1828 (mapconcat 'eval diary-remind-message
""))))
1829 ;; Diary entry may apply to one of a list of days before date.
1830 ((and (listp days
) days
)
1831 (or (diary-remind sexp
(car days
) marking
)
1832 (diary-remind sexp
(cdr days
) marking
))))))
1834 (defun diary-redraw-calendar ()
1835 "If `calendar-buffer' is live and diary entries are marked, redraw it."
1836 (and mark-diary-entries-in-calendar
1839 ;; Return value suitable for `write-contents-functions'.
1843 (defun make-diary-entry (string &optional nonmarking file
)
1844 "Insert a diary entry STRING which may be NONMARKING in FILE.
1845 If omitted, NONMARKING defaults to nil and FILE defaults to
1847 (let ((pop-up-frames (window-dedicated-p (selected-window))))
1848 (find-file-other-window (substitute-in-file-name (or file diary-file
))))
1849 (when (eq major-mode default-major-mode
) (diary-mode))
1851 (diary-unhide-everything)
1852 (goto-char (point-max))
1853 (when (let ((case-fold-search t
))
1854 (search-backward "Local Variables:"
1855 (max (- (point-max) 3000) (point-min))
1862 (if nonmarking diary-nonmarking-symbol
"")
1866 (defun insert-diary-entry (arg)
1867 "Insert a diary entry for the date indicated by point.
1868 Prefix argument ARG makes the entry nonmarking."
1870 (make-diary-entry (calendar-date-string (calendar-cursor-to-date t
) t t
)
1874 (defun insert-weekly-diary-entry (arg)
1875 "Insert a weekly diary entry for the day of the week indicated by point.
1876 Prefix argument ARG makes the entry nonmarking."
1878 (make-diary-entry (calendar-day-name (calendar-cursor-to-date t
))
1882 (defun insert-monthly-diary-entry (arg)
1883 "Insert a monthly diary entry for the day of the month indicated by point.
1884 Prefix argument ARG makes the entry nonmarking."
1886 (let ((calendar-date-display-form
1887 (if european-calendar-style
1890 (make-diary-entry (calendar-date-string (calendar-cursor-to-date t
) t
)
1894 (defun insert-yearly-diary-entry (arg)
1895 "Insert an annual diary entry for the day of the year indicated by point.
1896 Prefix argument ARG makes the entry nonmarking."
1898 (let ((calendar-date-display-form
1899 (if european-calendar-style
1900 '(day " " monthname
)
1901 '(monthname " " day
))))
1902 (make-diary-entry (calendar-date-string (calendar-cursor-to-date t
) t
)
1906 (defun insert-anniversary-diary-entry (arg)
1907 "Insert an anniversary diary entry for the date given by point.
1908 Prefix argument ARG makes the entry nonmarking."
1910 (let ((calendar-date-display-form
1911 (if european-calendar-style
1912 '(day " " month
" " year
)
1913 '(month " " day
" " year
))))
1915 (format "%s(diary-anniversary %s)"
1916 sexp-diary-entry-symbol
1917 (calendar-date-string (calendar-cursor-to-date t
) nil t
))
1921 (defun insert-block-diary-entry (arg)
1922 "Insert a block diary entry for the days between the point and marked date.
1923 Prefix argument ARG makes the entry nonmarking."
1925 (let ((calendar-date-display-form
1926 (if european-calendar-style
1927 '(day " " month
" " year
)
1928 '(month " " day
" " year
)))
1929 (cursor (calendar-cursor-to-date t
))
1930 (mark (or (car calendar-mark-ring
)
1931 (error "No mark set in this buffer")))
1933 (if (< (calendar-absolute-from-gregorian mark
)
1934 (calendar-absolute-from-gregorian cursor
))
1940 (format "%s(diary-block %s %s)"
1941 sexp-diary-entry-symbol
1942 (calendar-date-string start nil t
)
1943 (calendar-date-string end nil t
))
1947 (defun insert-cyclic-diary-entry (arg)
1948 "Insert a cyclic diary entry starting at the date given by point.
1949 Prefix argument ARG makes the entry nonmarking."
1951 (let ((calendar-date-display-form
1952 (if european-calendar-style
1953 '(day " " month
" " year
)
1954 '(month " " day
" " year
))))
1956 (format "%s(diary-cyclic %d %s)"
1957 sexp-diary-entry-symbol
1958 (calendar-read "Repeat every how many days: "
1959 (lambda (x) (> x
0)))
1960 (calendar-date-string (calendar-cursor-to-date t
) nil t
))
1963 (defvar diary-mode-map
1964 (let ((map (make-sparse-keymap)))
1965 (define-key map
"\C-c\C-s" 'diary-show-all-entries
)
1966 (define-key map
"\C-c\C-q" 'quit-window
)
1968 "Keymap for `diary-mode'.")
1971 (define-derived-mode diary-mode fundamental-mode
"Diary"
1972 "Major mode for editing the diary file."
1973 (set (make-local-variable 'font-lock-defaults
)
1974 '(diary-font-lock-keywords t
))
1975 (add-to-invisibility-spec '(diary . nil
))
1976 (add-hook 'after-save-hook
'diary-redraw-calendar nil t
)
1977 (if diary-header-line-flag
1978 (setq header-line-format diary-header-line-format
)))
1981 (defvar diary-fancy-date-pattern
1983 (let ((dayname (diary-name-pattern calendar-day-name-array nil t
))
1984 (monthname (diary-name-pattern calendar-month-name-array nil t
))
1988 (mapconcat 'eval calendar-date-display-form
""))
1989 ;; Optional ": holiday name" after the date.
1991 "Regular expression matching a date header in Fancy Diary.")
1993 (defconst diary-time-regexp
1994 ;; Accepted formats: 10:00 10.00 10h00 10h 10am 10:00am 10.00am
1995 ;; Use of "." as a separator annoyingly matches numbers, eg "123.45".
1996 ;; Hence often prefix this with "\\(^\\|\\s-\\)."
1997 (concat "[0-9]?[0-9]\\([AaPp][mM]\\|\\("
1998 "[Hh]\\([0-9][0-9]\\)?\\|[:.][0-9][0-9]"
1999 "\\)\\([AaPp][Mm]\\)?\\)")
2000 "Regular expression matching a time of day.")
2002 (defface diary-anniversary
'((t :inherit font-lock-keyword-face
))
2003 "Face used for anniversaries in the diary."
2007 (defface diary-time
'((t :inherit font-lock-variable-name-face
))
2008 "Face used for times of day in the diary."
2012 (defvar fancy-diary-font-lock-keywords
2015 ;; Any number of " other holiday name" lines, followed by "==" line.
2016 (concat diary-fancy-date-pattern
"\\(\n +.*\\)*\n=+$")
2017 '(0 (progn (put-text-property (match-beginning 0) (match-end 0)
2018 'font-lock-multiline t
)
2020 '("^.*\\([aA]nniversary\\|[bB]irthday\\).*$" .
'diary-anniversary
)
2021 '("^.*Yahrzeit.*$" . font-lock-reference-face
)
2022 '("^\\(Erev \\)?Rosh Hodesh.*" . font-lock-function-name-face
)
2023 '("^Day.*omer.*$" . font-lock-builtin-face
)
2024 '("^Parashat.*$" . font-lock-comment-face
)
2025 `(,(format "\\(^\\|\\s-\\)%s\\(-%s\\)?" diary-time-regexp
2026 diary-time-regexp
) .
'diary-time
))
2027 "Keywords to highlight in fancy diary display.")
2029 ;; If region looks like it might start or end in the middle of a
2030 ;; multiline pattern, extend the region to encompass the whole pattern.
2031 (defun diary-fancy-font-lock-fontify-region-function (beg end
&optional verbose
)
2032 "Function to use for `font-lock-fontify-region-function' in Fancy Diary.
2033 Needed to handle multiline keyword in `fancy-diary-font-lock-keywords'."
2036 (if (looking-at "=+$") (forward-line -
1))
2037 (while (and (looking-at " +[^ ]")
2038 (zerop (forward-line -
1))))
2039 ;; This check not essential.
2040 (if (looking-at diary-fancy-date-pattern
)
2041 (setq beg
(line-beginning-position)))
2044 (while (and (looking-at " +[^ ]")
2045 (zerop (forward-line 1))))
2046 (if (looking-at "=+$")
2047 (setq end
(line-beginning-position 2)))
2048 (font-lock-default-fontify-region beg end verbose
))
2050 (define-derived-mode fancy-diary-display-mode fundamental-mode
2052 "Major mode used while displaying diary entries using Fancy Display."
2053 (set (make-local-variable 'font-lock-defaults
)
2054 '(fancy-diary-font-lock-keywords
2056 (font-lock-fontify-region-function
2057 . diary-fancy-font-lock-fontify-region-function
)))
2058 (local-set-key "q" 'quit-window
))
2061 (defun diary-font-lock-sexps (limit)
2062 "Recognize sexp diary entry up to LIMIT for font-locking."
2063 (if (re-search-forward
2064 (concat "^" (regexp-quote diary-nonmarking-symbol
)
2065 "?\\(" (regexp-quote sexp-diary-entry-symbol
) "\\)")
2069 (narrow-to-region (point-min) limit
)
2070 (let ((start (point)))
2072 (store-match-data (list start
(point)))
2076 (defun diary-font-lock-date-forms (month-array &optional symbol abbrev-array
)
2077 "Create font-lock patterns for `diary-date-forms' using MONTH-ARRAY.
2078 If given, optional SYMBOL must be a prefix to entries.
2079 If optional ABBREV-ARRAY is present, the abbreviations constructed
2080 from this array by the function `calendar-abbrev-construct' are
2081 matched (with or without a final `.'), in addition to the full month
2083 (let ((dayname (diary-name-pattern calendar-day-name-array
2084 calendar-day-abbrev-array t
))
2085 (monthname (format "\\(%s\\|\\*\\)"
2086 (diary-name-pattern month-array abbrev-array
)))
2087 (month "\\([0-9]+\\|\\*\\)")
2088 (day "\\([0-9]+\\|\\*\\)")
2089 (year "-?\\([0-9]+\\|\\*\\)"))
2092 (concat "^" (regexp-quote diary-nonmarking-symbol
) "?"
2093 (if symbol
(regexp-quote symbol
) "") "\\("
2095 ;; If backup, omit first item (backup)
2096 ;; and last item (not part of date).
2097 (if (equal (car x
) 'backup
)
2098 (nreverse (cdr (reverse (cdr x
))))
2101 ;; With backup, last item is not part of date.
2102 (if (equal (car x
) 'backup
)
2103 (concat "\\)" (eval (car (reverse x
))))
2108 (defvar calendar-hebrew-month-name-array-leap-year
)
2109 (defvar calendar-islamic-month-name-array
)
2110 (defvar calendar-bahai-month-name-array
)
2113 (defun diary-font-lock-keywords ()
2114 "Return a value for the variable `diary-font-lock-keywords'."
2116 (diary-font-lock-date-forms calendar-month-name-array
2117 nil calendar-month-abbrev-array
)
2118 (when (or (memq 'mark-hebrew-diary-entries
2119 nongregorian-diary-marking-hook
)
2120 (memq 'list-hebrew-diary-entries
2121 nongregorian-diary-listing-hook
))
2122 (require 'cal-hebrew
)
2123 (diary-font-lock-date-forms
2124 calendar-hebrew-month-name-array-leap-year hebrew-diary-entry-symbol
))
2125 (when (or (memq 'mark-islamic-diary-entries
2126 nongregorian-diary-marking-hook
)
2127 (memq 'list-islamic-diary-entries
2128 nongregorian-diary-listing-hook
))
2129 (require 'cal-islam
)
2130 (diary-font-lock-date-forms
2131 calendar-islamic-month-name-array islamic-diary-entry-symbol
))
2132 (when (or (memq 'diary-bahai-mark-entries
2133 nongregorian-diary-marking-hook
)
2134 (memq 'diary-bahai-list-entries
2135 nongregorian-diary-marking-hook
))
2136 (require 'cal-bahai
)
2137 (diary-font-lock-date-forms
2138 calendar-bahai-month-name-array bahai-diary-entry-symbol
))
2141 (format "^%s.*$" (regexp-quote diary-include-string
))
2142 'font-lock-keyword-face
)
2144 (format "^%s?\\(%s\\)" (regexp-quote diary-nonmarking-symbol
)
2145 (regexp-quote sexp-diary-entry-symbol
))
2146 '(1 font-lock-reference-face
))
2148 (format "^%s" (regexp-quote diary-nonmarking-symbol
))
2149 'font-lock-reference-face
)
2151 (format "^%s?%s" (regexp-quote diary-nonmarking-symbol
)
2152 (regexp-opt (mapcar 'regexp-quote
2153 (list hebrew-diary-entry-symbol
2154 islamic-diary-entry-symbol
2155 bahai-diary-entry-symbol
))
2157 '(1 font-lock-reference-face
))
2158 '(diary-font-lock-sexps . font-lock-keyword-face
)
2159 `(,(format "\\(^\\|\\s-\\)%s\\(-%s\\)?" diary-time-regexp
2163 (defvar diary-font-lock-keywords
(diary-font-lock-keywords)
2164 "Forms to highlight in `diary-mode'.")
2166 ;; Following code from Dave Love <fx@gnu.org>.
2167 ;; Import Outlook-format appointments from mail messages in Gnus or
2168 ;; Rmail using command `diary-from-outlook'. This, or the specialized
2169 ;; functions `diary-from-outlook-gnus' and `diary-from-outlook-rmail',
2170 ;; could be run from hooks to notice appointments automatically (in
2171 ;; which case they will prompt about adding to the diary). The
2172 ;; message formats recognized are customizable through
2173 ;; `diary-outlook-formats'.
2175 ;; Dynamically bound.
2178 (defun diary-from-outlook-internal (&optional test-only
)
2179 "Snarf a diary entry from a message assumed to be from MS Outlook.
2180 Assumes `body' is bound to a string comprising the body of the message and
2181 `subject' is bound to a string comprising its subject.
2182 Arg TEST-ONLY non-nil means return non-nil if and only if the
2183 message contains an appointment, don't make a diary entry."
2185 (let (format-string)
2186 (dotimes (i (length diary-outlook-formats
))
2187 (when (eq 0 (string-match (car (nth i diary-outlook-formats
))
2190 (setq format-string
(cdr (nth i diary-outlook-formats
)))
2192 (save-window-excursion
2193 ;; Fixme: References to optional fields in the format
2194 ;; are treated literally, not replaced by the empty
2195 ;; string. I think this is an Emacs bug.
2197 (format (replace-match (if (functionp format-string
)
2198 (funcall format-string body
)
2200 t nil
(match-string 0 body
))
2203 (throw 'finished t
))))
2206 (defun diary-from-outlook (&optional noconfirm
)
2207 "Maybe snarf diary entry from current Outlook-generated message.
2208 Currently knows about Gnus and Rmail modes. Unless the optional
2209 argument NOCONFIRM is non-nil (which is the case when this
2210 function is called interactively), then if an entry is found the
2211 user is asked to confirm its addition."
2214 ((eq major-mode
'rmail-mode
)
2215 #'diary-from-outlook-rmail
)
2216 ((memq major-mode
'(gnus-summary-mode gnus-article-mode
))
2217 #'diary-from-outlook-gnus
)
2218 (t (error "Don't know how to snarf in `%s'" major-mode
)))))
2219 (funcall func noconfirm
)))
2222 (defvar gnus-article-mime-handles
)
2223 (defvar gnus-article-buffer
)
2225 (autoload 'gnus-fetch-field
"gnus-util")
2226 (autoload 'gnus-narrow-to-body
"gnus")
2227 (autoload 'mm-get-part
"mm-decode")
2229 (defun diary-from-outlook-gnus (&optional noconfirm
)
2230 "Maybe snarf diary entry from Outlook-generated message in Gnus.
2231 Unless the optional argument NOCONFIRM is non-nil (which is the case when
2232 this function is called interactively), then if an entry is found the
2233 user is asked to confirm its addition.
2234 Add this function to `gnus-article-prepare-hook' to notice appointments
2237 (with-current-buffer gnus-article-buffer
2238 (let ((subject (gnus-fetch-field "subject"))
2239 (body (if gnus-article-mime-handles
2240 ;; We're multipart. Don't get confused by part
2241 ;; buttons &c. Assume info is in first part.
2242 (mm-get-part (nth 1 gnus-article-mime-handles
))
2244 (gnus-narrow-to-body)
2246 (when (diary-from-outlook-internal t
)
2247 (when (or noconfirm
(y-or-n-p "Snarf diary entry? "))
2248 (diary-from-outlook-internal)
2249 (message "Diary entry added"))))))
2251 (custom-add-option 'gnus-article-prepare-hook
'diary-from-outlook-gnus
)
2254 (defvar rmail-buffer
)
2256 (defun diary-from-outlook-rmail (&optional noconfirm
)
2257 "Maybe snarf diary entry from Outlook-generated message in Rmail.
2258 Unless the optional argument NOCONFIRM is non-nil (which is the case when
2259 this function is called interactively), then if an entry is found the
2260 user is asked to confirm its addition."
2262 (with-current-buffer rmail-buffer
2263 (let ((subject (mail-fetch-field "subject"))
2264 (body (buffer-substring (save-excursion
2268 (when (diary-from-outlook-internal t
)
2269 (when (or noconfirm
(y-or-n-p "Snarf diary entry? "))
2270 (diary-from-outlook-internal)
2271 (message "Diary entry added"))))))
2274 (provide 'diary-lib
)
2276 ;; arch-tag: 22dd506e-2e33-410d-9ae1-095a0c1b2010
2277 ;;; diary-lib.el ends here