1 ;;; diary-lib.el --- diary functions
3 ;; Copyright (C) 1989-1990, 1992-1995, 2001-2011
4 ;; 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 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
32 (eval-and-compile (load "diary-loaddefs" nil t))
39 (defcustom diary-include-string "#include"
40 "The string indicating inclusion of another file of diary entries.
41 See the documentation for the function `diary-include-other-diary-files'."
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."
55 :group 'calendar-faces)
56 (make-obsolete-variable 'diary-face "customize the face `diary' instead."
59 (defface diary-anniversary '((t :inherit font-lock-keyword-face))
60 "Face used for anniversaries in the fancy diary display."
62 :group 'calendar-faces)
64 (defface diary-time '((t :inherit font-lock-variable-name-face))
65 "Face used for times of day in the fancy diary display."
67 :group 'calendar-faces)
69 (defface diary-button '((((type pc) (class color))
70 (:foreground "lightblue")))
71 "Face used for buttons in the fancy diary display."
73 :group 'calendar-faces)
75 (define-obsolete-face-alias 'diary-button-face 'diary-button "22.1")
77 ;; Face markup of calendar and diary displays: Any entry line that
78 ;; ends with [foo:value] where foo is a face attribute (except :box
79 ;; :stipple) or with [face:blah] tags, will have these values applied
80 ;; to the calendar and fancy diary displays. These attributes "stack"
81 ;; on calendar displays. File-wide attributes can be defined as
82 ;; follows: the first line matching "^# [tag:value]" defines the value
83 ;; for that particular tag.
84 (defcustom diary-face-attrs
85 '((" *\\[foreground:\\([-a-z]+\\)\\]$" 1 :foreground string)
86 (" *\\[background:\\([-a-z]+\\)\\]$" 1 :background string)
87 (" *\\[width:\\([-a-z]+\\)\\]$" 1 :width symbol)
88 (" *\\[height:\\([.0-9]+\\)\\]$" 1 :height int)
89 (" *\\[weight:\\([-a-z]+\\)\\]$" 1 :weight symbol)
90 (" *\\[slant:\\([-a-z]+\\)\\]$" 1 :slant symbol)
91 (" *\\[underline:\\([-a-z]+\\)\\]$" 1 :underline stringtnil)
92 (" *\\[overline:\\([-a-z]+\\)\\]$" 1 :overline stringtnil)
93 (" *\\[strike-through:\\([-a-z]+\\)\\]$" 1 :strike-through stringtnil)
94 (" *\\[inverse-video:\\([-a-z]+\\)\\]$" 1 :inverse-video tnil)
95 (" *\\[face:\\([-0-9a-z]+\\)\\]$" 1 :face string)
96 (" *\\[font:\\([-a-z0-9]+\\)\\]$" 1 :font string)
98 ;;; (" *\\[box:\\([-a-z]+\\)\\]$" 1 :box)
99 ;;; (" *\\[stipple:\\([-a-z]+\\)\\]$" 1 :stipple)
101 "Alist of (REGEXP SUBEXP ATTRIBUTE TYPE) elements.
102 This is used by `diary-pull-attrs' to fontify certain diary
103 elements. REGEXP is a regular expression to for, and SUBEXP is
104 the numbered sub-expression to extract. `diary-glob-file-regexp-prefix'
105 is pre-pended to REGEXP for file-wide specifiers. ATTRIBUTE
106 specifies which face attribute (e.g. `:foreground') to modify, or
107 that this is a face (`:face') to apply. TYPE is the type of
108 attribute being applied. Available TYPES (see `diary-attrtype-convert')
109 are: `string', `symbol', `int', `tnil', `stringtnil.'"
110 :type '(repeat (list (string :tag "Regular expression")
111 (integer :tag "Sub-expression")
112 (symbol :tag "Attribute (e.g. :foreground)")
113 (choice (const string :tag "A string")
114 (const symbol :tag "A symbol")
115 (const int :tag "An integer")
116 (const tnil :tag "`t' or `nil'")
118 :tag "A string, `t', or `nil'"))))
121 (defcustom diary-glob-file-regexp-prefix "^\\#"
122 "Regular expression pre-pended to `diary-face-attrs' for file-wide specifiers."
126 (defcustom diary-file-name-prefix nil
127 "Non-nil means prefix each diary entry with the name of the file defining it."
131 (defcustom diary-file-name-prefix-function 'identity
132 "The function that will take a diary file name and return the desired prefix."
136 (define-obsolete-variable-alias 'sexp-diary-entry-symbol
137 'diary-sexp-entry-symbol "23.1")
139 (defcustom diary-sexp-entry-symbol "%%"
140 "The string used to indicate a sexp diary entry in `diary-file'.
141 See the documentation for the function `diary-list-sexp-entries'."
145 (defcustom diary-comment-start nil
146 "String marking the start of a comment in the diary, or nil.
147 Nil means there are no comments. The diary does not display
148 parts of entries that are inside comments. You can use comments
149 for whatever you like, e.g. for meta-data that packages such as
150 `appt.el' can use. Comments may not span mutliple lines, and there
151 can be only one comment on any line.
152 See also `diary-comment-end'."
154 :type '(choice (const :tag "No comment" nil) string)
157 (defcustom diary-comment-end ""
158 "String marking the end of a comment in the diary.
159 The empty string means comments finish at the end of a line.
160 See also `diary-comment-start'."
165 (defcustom diary-hook nil
166 "List of functions called after the display of the diary.
167 Used for example by the appointment package - see `appt-activate'."
171 (define-obsolete-variable-alias 'diary-display-hook 'diary-display-function
174 (defcustom diary-display-function 'diary-fancy-display
175 "Function used to display the diary.
176 The two standard options are `diary-fancy-display' and `diary-simple-display'.
178 For historical reasons, `nil' is the same as `diary-simple-display'
179 \(so you must use `ignore' for no display). Also for historical
180 reasons, this variable can be a list of functions to run. These
181 uses are not recommended and may be removed at some point.
183 When this function is called, the variable `diary-entries-list'
184 is a list, in order by date, of all relevant diary entries in the
185 form of ((MONTH DAY YEAR) STRING), where string is the diary
186 entry for the given date. This can be used, for example, to
187 produce a different buffer for display (perhaps combined with
188 holidays), or hard copy output."
189 :type '(choice (const diary-fancy-display :tag "Fancy display")
190 (const diary-simple-display :tag "Basic display")
191 (const ignore :tag "No display")
192 (const nil :tag "Obsolete way to choose basic display")
193 (hook :tag "Obsolete form with list of display functions"))
194 :initialize 'custom-initialize-default
195 :set 'diary-set-maybe-redraw
196 :version "23.2" ; simple->fancy
199 (define-obsolete-variable-alias 'list-diary-entries-hook
200 'diary-list-entries-hook "23.1")
202 (defcustom diary-list-entries-hook nil
203 "List of functions called after diary file is culled for relevant entries.
204 You might wish to add `diary-include-other-diary-files', in which case
205 you will probably also want to add `diary-mark-included-diary-files' to
206 `diary-mark-entries-hook'. For example, you could use
208 (setq diary-display-function 'diary-fancy-display)
209 (add-hook 'diary-list-entries-hook 'diary-include-other-diary-files)
210 (add-hook 'diary-list-entries-hook 'diary-sort-entries t)
212 in your `.emacs' file to cause the fancy diary buffer to be displayed with
213 diary entries from various included files, each day's entries sorted into
214 lexicographic order. Note how the sort function is placed last,
215 so that it can sort the entries included from other files."
217 :options '(diary-include-other-diary-files diary-sort-entries)
220 (define-obsolete-variable-alias 'mark-diary-entries-hook
221 'diary-mark-entries-hook "23.1")
223 (defcustom diary-mark-entries-hook nil
224 "List of functions called after marking diary entries in the calendar.
225 You might wish to add `diary-mark-included-diary-files', in which case
226 you will probably also want to add `diary-include-other-diary-files' to
227 `diary-list-entries-hook'."
229 :options '(diary-mark-included-diary-files)
232 (define-obsolete-variable-alias 'nongregorian-diary-listing-hook
233 'diary-nongregorian-listing-hook "23.1")
235 (defcustom diary-nongregorian-listing-hook nil
236 "List of functions called for listing diary file and included files.
237 As the files are processed for diary entries, these functions are used
238 to cull relevant entries. You can use any or all of
239 `diary-bahai-list-entries', `diary-hebrew-list-entries', and
240 `diary-islamic-list-entries'. The documentation for these functions
241 describes the style of such diary entries."
243 :options '(diary-bahai-list-entries
244 diary-hebrew-list-entries
245 diary-islamic-list-entries)
248 (define-obsolete-variable-alias 'nongregorian-diary-marking-hook
249 'diary-nongregorian-marking-hook "23.1")
251 (defcustom diary-nongregorian-marking-hook nil
252 "List of functions called for marking diary file and included files.
253 As the files are processed for diary entries, these functions are used
254 to cull relevant entries. You can use any or all of
255 `diary-bahai-mark-entries', `diary-hebrew-mark-entries' and
256 `diary-islamic-mark-entries'. The documentation for these functions
257 describes the style of such diary entries."
259 :options '(diary-bahai-mark-entries
260 diary-hebrew-mark-entries
261 diary-islamic-mark-entries)
264 (define-obsolete-variable-alias 'print-diary-entries-hook
265 'diary-print-entries-hook "23.1")
267 (defcustom diary-print-entries-hook 'lpr-buffer
268 "Run by `diary-print-entries' after preparing a temporary diary buffer.
269 The buffer shows only the diary entries currently visible in the
270 diary buffer. The default just does the printing. Other uses
271 might include, for example, rearranging the lines into order by
272 day and time, saving the buffer instead of deleting it, or
273 changing the function used to do the printing."
277 (defcustom diary-unknown-time -9999
278 "Value returned by `diary-entry-time' when no time is found.
279 The default value -9999 causes entries with no recognizable time
280 to be placed before those with times; 9999 would place entries
281 with no recognizable time after those with times."
286 (defcustom diary-mail-addr
287 (or (bound-and-true-p user-mail-address) "")
288 "Email address that `diary-mail-entries' will send email to."
293 (defcustom diary-mail-days 7
294 "Default number of days for `diary-mail-entries' to check."
299 (defcustom diary-remind-message
301 (if (zerop (% days 7))
302 (format "%d week%s" (/ days 7) (if (= 7 days) "" "s"))
303 (format "%d day%s" days (if (= 1 days) "" "s")))
306 "Pseudo-pattern giving form of reminder messages in the fancy diary display.
308 Used by the function `diary-remind', a pseudo-pattern is a list of
309 expressions that can involve the keywords `days' (a number), `date'
310 \(a list of month, day, year), and `diary-entry' (a string)."
314 (define-obsolete-variable-alias 'abbreviated-calendar-year
315 'diary-abbreviated-year-flag "23.1")
317 (defcustom diary-abbreviated-year-flag t
318 "Interpret a two-digit year DD in a diary entry as either 19DD or 20DD.
319 This applies to the Gregorian, Hebrew, Islamic, and Baha'i calendars.
320 When the current century is added to a two-digit year, if the result
321 is more than 50 years in the future, the previous century is assumed.
322 If the result is more than 50 years in the past, the next century is assumed.
323 If this variable is nil, years must be written in full."
327 (defun diary-outlook-format-1 (body)
328 "Return a replace-match template for an element of `diary-outlook-formats'.
329 Returns a string using match elements 1-5, where:
330 1 = month name, 2 = day, 3 = year, 4 = time, 5 = location; also uses
331 %s = message subject. BODY is the string from which the matches derive."
332 (let* ((monthname (match-string 1 body))
333 (day (match-string 2 body))
334 (year (match-string 3 body))
337 (dotimes (i (length calendar-month-name-array))
338 (if (string-equal (aref calendar-month-name-array i)
340 (throw 'found (1+ i))))
342 ;; If we could convert the monthname to a numeric month, we can
343 ;; use the standard function calendar-date-string.
345 (calendar-date-string (list month (string-to-number day)
346 (string-to-number year)))
347 (cond ((eq calendar-date-style 'iso) "\\3 \\1 \\2") ; YMD
348 ((eq calendar-date-style 'european) "\\2 \\1 \\3") ; DMY
349 (t "\\1 \\2 \\3"))) ; MDY
351 ;; TODO Sometimes the time is in a different time-zone to the one you
352 ;; are in. Eg in PST, you might still get an email referring to:
353 ;; "7:00 PM-8:00 PM. Greenwich Standard Time".
354 ;; Note that it doesn't use a standard abbreviation for the timezone,
355 ;; or anything helpful like that.
356 ;; Sigh, this could cause the meeting to even be on a different day
357 ;; to that given in the When: string.
358 ;; These things seem to come in a multipart mail with a calendar part,
359 ;; it's probably better to use that rather than this whole thing.
360 ;; So this is unlikely to get improved.
362 ;; TODO Is the format of these messages actually documented anywhere?
363 (defcustom diary-outlook-formats
364 '(;; When: Tuesday, November 9, 2010 7:00 PM-8:00 PM. Greenwich Standard Time
365 ;; Where: Meeting room B
366 ("[ \t\n]*When: [[:alpha:]]+, \\([[:alpha:]]+\\) \\([0-9][0-9]*\\), \
367 \\([0-9]\\{4\\}\\),? \\(.+\\)\n\
368 \\(?:Where: \\(.+\n\\)\\)?" . diary-outlook-format-1))
369 "Alist of regexps matching message text and replacement text.
371 The regexp must match the start of the message text containing an
372 appointment, but need not include a leading `^'. If it matches the
373 current message, a diary entry is made from the corresponding
374 template. If the template is a string, it should be suitable for
375 passing to `replace-match', and so will have occurrences of `\\D' to
376 substitute the match for the Dth subexpression. It must also contain
377 a single `%s' which will be replaced with the text of the message's
378 Subject field. Any other `%' characters must be doubled, so that the
379 template can be passed to `format'.
381 If the template is actually a function, it is called with the message
382 body text as argument, and may use `match-string' etc. to make a
383 template following the rules above."
384 :type '(alist :key-type (regexp :tag "Regexp matching time/place")
386 (string :tag "Template for entry")
388 "Unary function providing template")))
392 (defvar diary-header-line-flag)
393 (defvar diary-header-line-format)
395 (defun diary-set-header (symbol value)
396 "Set SYMBOL's value to VALUE, and redraw the diary header if necessary."
397 (let ((oldvalue (symbol-value symbol))
398 (dbuff (and diary-file (find-buffer-visiting diary-file))))
399 (custom-set-default symbol value)
401 (not (equal value oldvalue))
402 (with-current-buffer dbuff
403 (if (eq major-mode 'diary-mode)
404 (setq header-line-format (and diary-header-line-flag
405 diary-header-line-format)))))))
407 ;; This can be removed once the kill/yank treatment of invisible text
408 ;; (see etc/TODO) is fixed. -- gm
409 (defcustom diary-header-line-flag t
410 "Non-nil means `diary-simple-display' will show a header line.
411 The format of the header is specified by `diary-header-line-format'."
414 :initialize 'custom-initialize-default
415 :set 'diary-set-header
418 (defvar diary-selective-display nil
419 "Internal diary variable; non-nil if some diary text is hidden.")
421 (defcustom diary-header-line-format
422 '(:eval (calendar-string-spread
423 (list (if diary-selective-display
424 "Some text is hidden - press \"s\" in calendar \
428 "Format of the header line displayed by `diary-simple-display'.
429 Only used if `diary-header-line-flag' is non-nil."
432 :initialize 'custom-initialize-default
433 :set 'diary-set-header
434 :version "23.3") ; frame-width -> window-width
436 ;; The first version of this also checked for diary-selective-display
437 ;; in the non-fancy case. This was an attempt to distinguish between
438 ;; displaying the diary and just visiting the diary file. However,
439 ;; when using fancy diary, calling diary when there are no entries to
440 ;; display does not create the fancy buffer, nor does it set
441 ;; diary-selective-display in the diary buffer. This means some
442 ;; customizations will not take effect, eg:
443 ;; http://lists.gnu.org/archive/html/emacs-pretest-bug/2007-03/msg00466.html
444 ;; So the check for diary-selective-display was dropped. This means the
445 ;; diary will be displayed if one customizes a diary variable while
446 ;; just visiting the diary-file. This is i) unlikely, and ii) no great loss.
448 (defun diary-live-p ()
449 "Return non-nil if the diary is being displayed."
450 (or (get-buffer diary-fancy-buffer)
451 (and diary-file (find-buffer-visiting diary-file))))
454 (defun diary-set-maybe-redraw (symbol value)
455 "Set SYMBOL's value to VALUE, and redraw the diary if necessary.
456 Redraws the diary if it is being displayed (note this is not the same as
457 just visiting the `diary-file'), and SYMBOL's value is to be changed."
458 (let ((oldvalue (symbol-value symbol)))
459 (custom-set-default symbol value)
460 (and (not (equal value oldvalue))
462 ;; Note this assumes diary was called without prefix arg.
465 (define-obsolete-variable-alias 'number-of-diary-entries
466 'diary-number-of-entries "23.1")
468 (defcustom diary-number-of-entries 1
469 "Specifies how many days of diary entries are to be displayed initially.
470 This variable affects the diary display when the command \\[diary] is
471 used, or if the value of the variable `calendar-view-diary-initially-flag'
472 is non-nil. For example, if the default value 1 is used, then only the
473 current day's diary entries will be displayed. If the value 2 is used,
474 then both the current day's and the next day's entries will be displayed.
476 The value can also be a vector such as [0 2 2 2 2 4 1]; this value says
477 to display no diary entries on Sunday, the entries for the current date
478 and the day after on Monday through Thursday, Friday through Monday's
479 entries on Friday, and only Saturday's entries on Saturday.
481 This variable does not affect the diary display with the `d' command
482 from the calendar; in that case, the prefix argument controls the number
483 of days of diary entries displayed."
484 :type '(choice (integer :tag "Entries")
485 (vector :value [0 0 0 0 0 0 0]
486 (integer :tag "Sunday")
487 (integer :tag "Monday")
488 (integer :tag "Tuesday")
489 (integer :tag "Wednesday")
490 (integer :tag "Thursday")
491 (integer :tag "Friday")
492 (integer :tag "Saturday")))
493 :initialize 'custom-initialize-default
494 :set 'diary-set-maybe-redraw
497 ;;; More user options in calendar.el, holidays.el.
500 (defun diary-check-diary-file ()
501 "Check that the file specified by `diary-file' exists and is readable.
502 If so, return the expanded file name, otherwise signal an error."
503 (if (and diary-file (file-exists-p diary-file))
504 (if (file-readable-p diary-file)
506 (error "Diary file `%s' is not readable" diary-file))
507 (error "Diary file `%s' does not exist" diary-file)))
510 (defun diary (&optional arg)
511 "Generate the diary window for ARG days starting with the current date.
512 If no argument is provided, the number of days of diary entries is governed
513 by the variable `diary-number-of-entries'. A value of ARG less than 1
514 does nothing. This function is suitable for execution in a `.emacs' file."
516 (diary-check-diary-file)
517 (diary-list-entries (calendar-current-date)
518 (if arg (prefix-numeric-value arg))))
521 (defun diary-view-entries (&optional arg)
522 "Prepare and display a buffer with diary entries.
523 Searches the file named in `diary-file' for entries that match
524 ARG days starting with the date indicated by the cursor position
525 in the displayed three-month calendar."
527 (diary-check-diary-file)
528 (diary-list-entries (calendar-cursor-to-date t) arg))
532 (defun diary-view-other-diary-entries (arg dfile)
533 "Prepare and display buffer of diary entries from an alternative diary file.
534 Searches for entries that match ARG days, starting with the date indicated
535 by the cursor position in the displayed three-month calendar.
536 DFILE specifies the file to use as the diary file."
538 (list (prefix-numeric-value current-prefix-arg)
539 (read-file-name "Enter diary file name: " default-directory nil t)))
540 (let ((diary-file dfile))
541 (diary-view-entries arg)))
544 (define-obsolete-function-alias 'view-other-diary-entries
545 'diary-view-other-diary-entries "23.1")
547 (defvar diary-syntax-table
548 (let ((st (copy-syntax-table (standard-syntax-table))))
549 (modify-syntax-entry ?* "w" st)
550 (modify-syntax-entry ?: "w" st)
552 "The syntax table used when parsing dates in the diary file.
553 It is the standard syntax table used in Fundamental mode, but with the
554 syntax of `*' and `:' changed to be word constituents.")
556 (defun diary-attrtype-convert (attrvalue type)
557 "Convert string ATTRVALUE to TYPE appropriate for a face description.
558 Valid TYPEs are: string, symbol, int, stringtnil, tnil."
559 (cond ((eq type 'string) attrvalue)
560 ((eq type 'symbol) (intern-soft attrvalue))
561 ((eq type 'int) (string-to-number attrvalue))
562 ((eq type 'stringtnil)
563 (cond ((string-equal "t" attrvalue) t)
564 ((string-equal "nil" attrvalue) nil)
566 ((eq type 'tnil) (string-equal "t" attrvalue))))
568 (defun diary-pull-attrs (entry fileglobattrs)
569 "Search for matches for regexps from `diary-face-attrs'.
570 If ENTRY is nil, searches from the start of the current buffer, and
571 prepends all regexps with `diary-glob-file-regexp-prefix'.
572 If ENTRY is a string, search for matches in that string, and remove them.
573 Returns a list of ENTRY followed by (ATTRIBUTE VALUE) pairs.
574 When ENTRY is non-nil, FILEGLOBATTRS forms the start of the (ATTRIBUTE VALUE)
576 (let (regexp regnum attrname attrname attrvalue type ret-attr)
579 (dolist (attr diary-face-attrs)
580 ;; FIXME inefficient searching.
581 (goto-char (point-min))
582 (setq regexp (concat diary-glob-file-regexp-prefix (car attr))
584 attrname (nth 2 attr)
586 attrvalue (if (re-search-forward regexp nil t)
587 (match-string-no-properties regnum)))
589 (setq attrvalue (diary-attrtype-convert attrvalue type))
590 (setq ret-attr (append ret-attr
591 (list attrname attrvalue))))))
592 (setq ret-attr fileglobattrs)
593 (dolist (attr diary-face-attrs)
594 (setq regexp (car attr)
596 attrname (nth 2 attr)
599 ;; If multiple matches, replace all, use the last (which may
600 ;; be the first instance in the line, if the regexp is
602 (while (string-match regexp entry)
603 (setq attrvalue (match-string-no-properties regnum entry)
604 entry (replace-match "" t t entry)))
606 (setq attrvalue (diary-attrtype-convert attrvalue type))
607 (setq ret-attr (append ret-attr (list attrname attrvalue))))))
608 (list entry ret-attr)))
612 (defvar diary-modify-entry-list-string-function nil
613 "Function applied to entry string before putting it into the entries list.
614 Can be used by programs integrating a diary list into other buffers (e.g.
615 org.el and planner.el) to modify the string or add properties to it.
616 The function takes a string argument and must return a string.")
618 (defvar diary-entries-list) ; bound in diary-list-entries
620 (defun diary-add-to-list (date string specifier &optional marker
622 "Add an entry to `diary-entries-list'.
623 Do nothing if DATE or STRING are nil. DATE is the (MONTH DAY
624 YEAR) for which the entry applies; STRING is the text of the
625 entry as it will appear in the diary (i.e. with any format
626 strings such as \"%d\" expanded); SPECIFIER is the date part of
627 the entry as it appears in the diary-file; LITERAL is the entry
628 as it appears in the diary-file (i.e. before expansion).
629 If LITERAL is nil, it is taken to be the same as STRING.
631 The entry is added to the list as (DATE STRING SPECIFIER LOCATOR
632 GLOBCOLOR), where LOCATOR has the form (MARKER FILENAME LITERAL),
633 FILENAME being the file containing the diary entry.
635 Modifies STRING using `diary-modify-entry-list-string-function', if non-nil.
636 Also removes the region between `diary-comment-start' and
637 `diary-comment-end', if the former is non-nil."
638 (when (and date string)
639 ;; b-f-n is nil if we are visiting an include file in a temp-buffer.
640 (let ((dfile (or (buffer-file-name) diary-file))
642 (if diary-file-name-prefix
643 (let ((prefix (funcall diary-file-name-prefix-function dfile)))
644 (or (string-equal prefix "")
645 (setq string (format "[%s] %s" prefix string)))))
646 (and diary-modify-entry-list-string-function
647 (setq string (funcall diary-modify-entry-list-string-function
649 (when (and diary-comment-start
650 (string-match (setq cstart (regexp-quote diary-comment-start))
652 ;; Preserve the value with the comments.
653 (or literal (setq literal string))
654 ;; Handles multiple comments per entry, so long as each is on
655 ;; a single line, and each line has no more than one comment.
656 (setq string (replace-regexp-in-string
657 (format "%s.*%s" cstart (regexp-quote diary-comment-end))
659 (setq diary-entries-list
660 (append diary-entries-list
661 (list (list date string specifier
662 (list marker dfile literal)
665 (define-obsolete-function-alias 'add-to-diary-list 'diary-add-to-list "23.1")
667 (defun diary-list-entries-2 (date mark globattr list-only
668 &optional months symbol gdate)
669 "Internal subroutine of `diary-list-entries'.
670 Find diary entries applying to DATE, by searching from point-min for
671 each element of `diary-date-forms'. MARK indicates an entry is non-marking.
672 GLOBATTR is the list of global file attributes. If LIST-ONLY is
673 non-nil, don't change the buffer, only return a list of entries.
674 Optional array MONTHS replaces `calendar-month-name-array', and
675 means months cannot be abbreviated. Optional string SYMBOL marks diary
676 entries of the desired type. If DATE is not Gregorian, then the
677 Gregorian equivalent should be provided via GDATE. Returns non-nil if
678 any entries were found."
679 (let* ((month (calendar-extract-month date))
680 (day (calendar-extract-day date))
681 (year (calendar-extract-year date))
682 (dayname (format "%s\\|%s\\.?" (calendar-day-name date)
683 (calendar-day-name date 'abbrev)))
684 (calendar-month-name-array (or months calendar-month-name-array))
685 (monthname (format "\\*\\|%s%s" (calendar-month-name month)
688 (calendar-month-name month 'abbrev)))))
689 (month (format "\\*\\|0*%d" month))
690 (day (format "\\*\\|0*%d" day))
691 (year (format "\\*\\|0*%d%s" year
692 (if diary-abbreviated-year-flag
693 (format "\\|%02d" (% year 100))
697 (dolist (date-form diary-date-forms)
698 (let ((backup (when (eq (car date-form) 'backup)
699 (setq date-form (cdr date-form))
701 ;; date-form uses day etc as set above.
702 (regexp (format "^%s?%s\\(%s\\)" (regexp-quote mark)
703 (if symbol (regexp-quote symbol) "")
704 (mapconcat 'eval date-form "\\)\\(?:")))
705 entry-start date-start temp)
706 (goto-char (point-min))
707 (while (re-search-forward regexp nil t)
708 (if backup (re-search-backward "\\<" nil t))
709 ;; regexp moves us past the end of date, onto the next line.
710 ;; Trailing whitespace after date not allowed (see diary-file).
711 (if (and (bolp) (not (looking-at "[ \t]")))
712 ;; Diary entry that consists only of date.
714 ;; Found a nonempty diary entry--make it
715 ;; visible and add it to the list.
716 (setq date-start (line-end-position 0))
717 ;; Actual entry starts on the next-line?
718 (if (looking-at "[ \t]*\n[ \t]") (forward-line 1))
722 (while (looking-at "[ \t]") ; continued entry
724 (unless (and (eobp) (not (bolp)))
727 (remove-overlays date-start (point) 'invisible 'diary))
728 (setq temp (diary-pull-attrs
729 (buffer-substring-no-properties
730 entry-start (point)) globattr))
732 (or gdate date) (car temp)
733 (buffer-substring-no-properties (1+ date-start) (1- entry-start))
734 (copy-marker entry-start) (cadr temp))))))
737 (defvar original-date) ; from diary-list-entries
738 (defvar file-glob-attrs)
742 (defun diary-list-entries-1 (months symbol absfunc)
743 "List diary entries of a certain type.
744 MONTHS is an array of month names. SYMBOL marks diary entries of the type
745 in question. ABSFUNC is a function that converts absolute dates to dates
746 of the appropriate type."
747 (let ((gdate original-date))
748 (dotimes (_idummy number)
749 (diary-list-entries-2
750 (funcall absfunc (calendar-absolute-from-gregorian gdate))
751 diary-nonmarking-symbol file-glob-attrs list-only months symbol gdate)
753 (calendar-gregorian-from-absolute
754 (1+ (calendar-absolute-from-gregorian gdate))))))
755 (goto-char (point-min)))
757 (defvar diary-included-files nil
758 "List of any diary files included in the last call to `diary-list-entries'.")
760 ;; FIXME non-greg and list hooks run same number of times?
761 (defun diary-list-entries (date number &optional list-only)
762 "Create and display a buffer containing the relevant lines in `diary-file'.
763 The arguments are DATE and NUMBER; the entries selected are those
764 for NUMBER days starting with date DATE. The other entries are hidden
765 using overlays. If NUMBER is less than 1, this function does nothing.
767 Returns a list of all relevant diary entries found.
768 The list entries have the form ((MONTH DAY YEAR) STRING SPECIFIER) where
769 \(MONTH DAY YEAR) is the date of the entry, STRING is the entry text, and
770 SPECIFIER is the applicability. If the variable `diary-list-include-blanks'
771 is non-nil, this list includes a dummy diary entry consisting of the empty
772 string for a date with no diary entries.
774 If entries are being produced for multiple dates (i.e., NUMBER > 1),
775 then this function normally returns the entries from any given
776 diary file in date order. The entries for any given day are in
777 the order in which they were found in the file, not necessarily
778 in time-of-day order. Note that any functions present on the
779 hooks (see below) may add entries, or change the order. For
780 example, `diary-include-other-diary-files' adds entries from any
781 include files that it finds to the end of the original list. The
782 entries from each file will be in date order, but the overall
783 list will not be. If you want the entire list to be in time order,
784 add `diary-sort-entries' to the end of `diary-list-entries-hook'.
786 After the initial list is prepared, the following hooks are run:
788 `diary-nongregorian-listing-hook' can cull dates from the diary
789 and each included file, for example to process Islamic diary
790 entries. Applied to *each* file.
792 `diary-list-entries-hook' adds or manipulates diary entries from
793 external sources. Used, for example, to include diary entries
794 from other files or to sort the diary entries. Invoked *once*
795 only, before the display hook is run.
797 `diary-hook' is run last, after the diary is displayed.
798 This is used e.g. by `appt-check'.
800 Functions called by these hooks may use the variables ORIGINAL-DATE
801 and NUMBER, which are the arguments with which this function was called.
802 Note that hook functions should _not_ use DATE, but ORIGINAL-DATE.
803 \(Sexp diary entries may use DATE - see `diary-list-sexp-entries'.)
805 This function displays the list using `diary-display-function', unless
806 LIST-ONLY is non-nil, in which case it just returns the list."
808 (setq number (if (vectorp diary-number-of-entries)
809 (aref diary-number-of-entries (calendar-day-of-week date))
810 diary-number-of-entries)))
812 (let* ((original-date date) ; save for possible use in the hooks
813 (date-string (calendar-date-string date))
814 (diary-buffer (find-buffer-visiting diary-file))
815 ;; Dynamically bound in diary-include-other-diary-files.
816 (d-incp (and (boundp 'diary-including) diary-including))
817 diary-entries-list file-glob-attrs temp-buff)
819 (setq diary-included-files nil)
820 (message "Preparing diary..."))
822 (with-current-buffer (or diary-buffer
824 (setq temp-buff (generate-new-buffer
826 (find-file-noselect diary-file t)))
828 (or (verify-visited-file-modtime diary-buffer)
829 (revert-buffer t t)))
831 ;; If including, caller has already verified it is readable.
832 (insert-file-contents diary-file)
833 ;; Setup things like the header-line-format and invisibility-spec.
834 (if (eq major-mode (default-value 'major-mode))
836 ;; This kludge is to make customizations to
837 ;; diary-header-line-flag after diary has been displayed
838 ;; take effect. Unconditionally calling (diary-mode)
839 ;; clobbers file local variables.
840 ;; http://lists.gnu.org/archive/html/emacs-pretest-bug/2007-03/msg00363.html
841 ;; http://lists.gnu.org/archive/html/emacs-pretest-bug/2007-04/msg00404.html
842 (if (eq major-mode 'diary-mode)
843 (setq header-line-format (and diary-header-line-flag
844 diary-header-line-format)))))
845 ;; d-s-p is passed to the diary display function.
846 (let ((diary-saved-point (point)))
850 (setq file-glob-attrs (cadr (diary-pull-attrs nil "")))
851 (with-syntax-table diary-syntax-table
852 (goto-char (point-min))
854 (let ((ol (make-overlay (point-min) (point-max) nil t nil)))
855 (set (make-local-variable 'diary-selective-display) t)
856 (overlay-put ol 'invisible 'diary)
857 (overlay-put ol 'evaporate t)))
858 (dotimes (_idummy number)
859 (let ((sexp-found (diary-list-sexp-entries date))
860 (entry-found (diary-list-entries-2
861 date diary-nonmarking-symbol
862 file-glob-attrs list-only)))
863 (if diary-list-include-blanks
864 (or sexp-found entry-found
865 (diary-add-to-list date "" "" "" "")))
867 (calendar-gregorian-from-absolute
868 (1+ (calendar-absolute-from-gregorian date)))))))
869 (goto-char (point-min))
870 (run-hooks 'diary-nongregorian-listing-hook
871 'diary-list-entries-hook)
873 (if (and diary-display-function
874 (listp diary-display-function))
875 ;; Backwards compatibility.
876 (run-hooks 'diary-display-function)
877 (funcall (or diary-display-function
878 'diary-simple-display))))
879 (run-hooks 'diary-hook)))))
880 (and temp-buff (buffer-name temp-buff) (kill-buffer temp-buff)))
881 (or d-incp (message "Preparing diary...done"))
882 diary-entries-list)))
884 (defun diary-unhide-everything ()
885 "Show all invisible text in the diary."
886 (kill-local-variable 'diary-selective-display)
887 (save-restriction ; bug#5477
889 (remove-overlays (point-min) (point-max) 'invisible 'diary))
890 (kill-local-variable 'mode-line-format))
892 (defvar original-date) ; bound in diary-list-entries
893 ;(defvar number) ; already declared above
895 (defun diary-include-other-diary-files ()
896 "Include the diary entries from other diary files with those of `diary-file'.
897 This function is suitable for use with `diary-list-entries-hook';
898 it enables you to use shared diary files together with your own.
899 The files included are specified in the `diary-file' by lines of this form:
900 #include \"filename\"
901 This is recursive; that is, #include directives in diary files thus included
902 are obeyed. You can change the `#include' to some other string by changing
903 the variable `diary-include-string'."
904 (goto-char (point-min))
905 (while (re-search-forward
906 (format "^%s \"\\([^\"]*\\)\"" (regexp-quote diary-include-string))
908 (let ((diary-file (match-string-no-properties 1))
909 (diary-list-entries-hook 'diary-include-other-diary-files)
911 diary-hook diary-list-include-blanks efile)
912 (if (file-exists-p diary-file)
913 (if (file-readable-p diary-file)
914 (if (member (setq efile (expand-file-name diary-file))
915 diary-included-files)
916 (error "Recursive diary include for %s" diary-file)
917 (setq diary-included-files
918 (append diary-included-files (list efile))
920 (append diary-entries-list
921 (diary-list-entries original-date number t))))
923 (message "Can't read included diary file %s" diary-file)
926 (message "Can't find included diary file %s" diary-file)
928 (goto-char (point-min)))
930 (define-obsolete-function-alias 'include-other-diary-files
931 'diary-include-other-diary-files "23.1")
933 (defvar date-string) ; bound in diary-list-entries
935 (defun diary-display-no-entries ()
936 "Common subroutine of `diary-simple-display' and `diary-fancy-display'.
937 Handles the case where there are no diary entries.
938 Returns a cons (NOENTRIES . HOLIDAY-STRING)."
939 (let* ((holiday-list (if diary-show-holidays-flag
940 (calendar-check-holidays original-date)))
941 (hol-string (format "%s%s%s"
943 (if holiday-list ": " "")
944 (mapconcat 'identity holiday-list "; ")))
945 (msg (format "No diary entries for %s" hol-string))
946 ;; Empty list, or single item with no text.
947 ;; FIXME multiple items with no text?
948 (noentries (or (not diary-entries-list)
949 (and (not (cdr diary-entries-list))
950 (string-equal "" (cadr
951 (car diary-entries-list)))))))
952 ;; Inconsistency: whether or not the holidays are displayed in a
953 ;; separate buffer depends on if there are diary entries.
955 (if (or (< (length msg) (frame-width))
958 ;; holiday-list which is too wide for a message gets a buffer.
959 (calendar-in-read-only-buffer holiday-buffer
960 (calendar-set-mode-line (format "Holidays for %s" date-string))
961 (insert (mapconcat 'identity holiday-list "\n")))
962 (message "No diary entries for %s" date-string)))
963 (cons noentries hol-string)))
966 (defvar diary-saved-point) ; bound in diary-list-entries
968 (defun diary-simple-display ()
969 "Display the diary buffer if there are any relevant entries or holidays.
970 Entries that do not apply are made invisible. Holidays are shown
971 in the mode line. This is an option for `diary-display-function'."
972 ;; If selected window is dedicated (to the calendar), need a new one
973 ;; to display the diary.
974 (let* ((pop-up-frames (or pop-up-frames
975 (window-dedicated-p (selected-window))))
976 (dbuff (find-buffer-visiting diary-file))
977 (empty (diary-display-no-entries)))
978 ;; This may be too wide, but when simple diary is used there is
979 ;; nowhere else for the holidays to go. Also, it is documented in
980 ;; diary-show-holidays-flag that the holidays go in the mode-line.
981 ;; FIXME however if there are no diary entries a separate buffer
982 ;; is displayed - this is inconsistent.
983 (with-current-buffer dbuff
984 (calendar-set-mode-line (format "Diary for %s" (cdr empty))))
985 (unless (car empty) ; no entries
986 (with-current-buffer dbuff
987 (let ((window (display-buffer (current-buffer))))
988 ;; d-s-p is passed from diary-list-entries.
989 (set-window-point window diary-saved-point)
990 (set-window-start window (point-min)))))))
992 (define-obsolete-function-alias 'simple-diary-display
993 'diary-simple-display "23.1")
995 (define-button-type 'diary-entry 'action #'diary-goto-entry
996 'face 'diary-button 'help-echo "Find this diary entry"
999 (defun diary-goto-entry (button)
1000 "Jump to the diary entry for the BUTTON at point."
1001 (let* ((locator (button-get button 'locator))
1002 (marker (car locator))
1004 ;; If marker pointing to diary location is valid, use that.
1005 (if (and marker (setq markbuf (marker-buffer marker)))
1007 (pop-to-buffer markbuf)
1008 (goto-char (marker-position marker)))
1009 ;; Marker is invalid (eg buffer has been killed).
1010 (or (and (setq file (cadr locator))
1011 (file-exists-p file)
1012 (find-file-other-window file)
1014 (when (eq major-mode (default-value 'major-mode)) (diary-mode))
1015 (goto-char (point-min))
1016 (if (re-search-forward (format "%s.*\\(%s\\)"
1017 (regexp-quote (nth 2 locator))
1018 (regexp-quote (nth 3 locator)))
1020 (goto-char (match-beginning 1)))))
1021 (message "Unable to locate this diary entry")))))
1023 (defun diary-fancy-display ()
1024 "Prepare a diary buffer with relevant entries in a fancy, noneditable form.
1025 Holidays are shown unless `diary-show-holidays-flag' is nil.
1026 Days with no diary entries are not shown (even if that day is a
1027 holiday), unless `diary-list-include-blanks' is non-nil.
1029 This is an option for `diary-display-function'."
1030 ;; Turn off selective-display in the diary file's buffer.
1031 (with-current-buffer (find-buffer-visiting diary-file)
1032 (diary-unhide-everything))
1033 (unless (car (diary-display-no-entries)) ; no entries
1034 ;; Prepare the fancy diary buffer.
1035 (calendar-in-read-only-buffer diary-fancy-buffer
1036 (calendar-set-mode-line "Diary Entries")
1037 (let ((holiday-list-last-month 1)
1038 (holiday-list-last-year 1)
1041 (dolist (entry diary-entries-list)
1042 (unless (calendar-date-equal date (car entry))
1043 (setq date (car entry))
1044 (and diary-show-holidays-flag
1045 (calendar-date-compare
1046 (list (list holiday-list-last-month
1047 (calendar-last-day-of-month
1048 holiday-list-last-month
1049 holiday-list-last-year)
1050 holiday-list-last-year))
1052 ;; We need to get the holidays for the next 3 months.
1053 (setq holiday-list-last-month
1054 (calendar-extract-month date)
1055 holiday-list-last-year
1056 (calendar-extract-year date))
1058 (calendar-increment-month
1059 holiday-list-last-month holiday-list-last-year 1)
1062 (let ((displayed-month holiday-list-last-month)
1063 (displayed-year holiday-list-last-year))
1064 (calendar-holiday-list)))
1065 (calendar-increment-month
1066 holiday-list-last-month holiday-list-last-year 1))
1068 date-holiday-list cc)
1069 ;; Make a list of all holidays for date.
1070 (dolist (h holiday-list)
1071 (if (calendar-date-equal date (car h))
1072 (setq date-holiday-list (append date-holiday-list
1074 (insert (if (bobp) "" ?\n) (calendar-date-string date))
1075 (if date-holiday-list (insert ": "))
1076 (setq cc (current-column))
1077 (insert (mapconcat (lambda (x)
1078 (setq longest (max longest (length x)))
1081 (concat "\n" (make-string cc ?\s))))
1082 (insert ?\n (make-string (+ cc longest) ?=) ?\n)))
1083 (let ((this-entry (cadr entry))
1084 this-loc marks temp-face)
1085 (unless (zerop (length this-entry))
1086 (if (setq this-loc (nth 3 entry))
1087 (insert-button this-entry
1088 ;; (MARKER FILENAME SPECIFIER LITERAL)
1089 'locator (list (car this-loc)
1092 (or (nth 2 this-loc)
1095 (insert this-entry))
1097 ;; Doesn't make sense to check font-lock-mode - see
1098 ;; comments above diary-entry-marker in calendar.el.
1099 (and ; font-lock-mode
1100 (setq marks (nth 4 entry))
1102 (setq temp-face (calendar-make-temp-face marks))
1103 (search-backward this-entry)
1105 (make-overlay (match-beginning 0) (match-end 0))
1106 'face temp-face)))))))
1107 ;; FIXME can't remember what this check was for.
1108 ;; To prevent something looping, or a minor optimization?
1109 (if (eq major-mode 'diary-fancy-display-mode)
1110 (run-hooks 'diary-fancy-display-mode-hook)
1111 (diary-fancy-display-mode))
1112 (calendar-set-mode-line date-string))))
1114 (define-obsolete-function-alias 'fancy-diary-display
1115 'diary-fancy-display "23.1")
1118 (defun diary-print-entries ()
1119 "Print a hard copy of the diary display.
1121 If the simple diary display is being used, prepare a temp buffer with the
1122 visible lines of the diary buffer, add a heading line composed from the mode
1123 line, print the temp buffer, and destroy it.
1125 If the fancy diary display is being used, just print the buffer.
1127 The hooks given by the variable `diary-print-entries-hook' are called to do
1128 the actual printing."
1130 (let ((diary-buffer (get-buffer diary-fancy-buffer))
1131 temp-buffer heading start end)
1133 (with-current-buffer diary-buffer
1134 (run-hooks 'diary-print-entries-hook))
1135 (or (setq diary-buffer (find-buffer-visiting diary-file))
1136 (error "You don't have a diary buffer!"))
1137 ;; Name affects printing?
1138 (setq temp-buffer (get-buffer-create " *Printable Diary Entries*"))
1139 (with-current-buffer diary-buffer
1141 (if (not (stringp mode-line-format))
1143 (string-match "^-*\\([^-].*[^-]\\)-*$" mode-line-format)
1144 (match-string 1 mode-line-format))
1148 (setq end (next-single-char-property-change start 'invisible))
1149 (unless (get-char-property start 'invisible)
1150 (with-current-buffer temp-buffer
1151 (insert-buffer-substring diary-buffer start end)))
1153 (and end (< end (point-max))))))
1154 (set-buffer temp-buffer)
1155 (goto-char (point-min))
1156 (insert heading "\n"
1157 (make-string (length heading) ?=) "\n")
1158 (run-hooks 'diary-print-entries-hook)
1159 (kill-buffer temp-buffer))))
1161 (define-obsolete-function-alias 'print-diary-entries
1162 'diary-print-entries "23.1")
1165 (defun diary-show-all-entries ()
1166 "Show all of the diary entries in the diary file.
1167 This function gets rid of the selective display of the diary file so that
1168 all entries, not just some, are visible. If there is no diary buffer, one
1171 (let* ((d-file (diary-check-diary-file))
1172 (pop-up-frames (or pop-up-frames
1173 (window-dedicated-p (selected-window))))
1174 (win (selected-window))
1175 (height (window-height)))
1176 (with-current-buffer (or (find-buffer-visiting d-file)
1177 (find-file-noselect d-file t))
1178 (when (eq major-mode (default-value 'major-mode)) (diary-mode))
1179 (diary-unhide-everything)
1180 (display-buffer (current-buffer))
1181 (when (and (/= height (window-height win))
1182 (with-current-buffer (window-buffer win)
1183 (derived-mode-p 'calendar-mode)))
1184 (fit-window-to-buffer win)))))
1187 (defun diary-mail-entries (&optional ndays)
1188 "Send a mail message showing diary entries for next NDAYS days.
1189 If no prefix argument is given, NDAYS is set to `diary-mail-days'.
1190 Mail is sent to the address specified by `diary-mail-addr'.
1192 Here is an example of a script to call `diary-mail-entries',
1193 suitable for regular scheduling using cron (or at). Note that
1194 since `emacs -script' does not load your `.emacs' file, you
1195 should ensure that all relevant variables are set.
1197 #!/usr/bin/emacs -script
1198 ;; diary-rem.el - run the Emacs diary-reminder
1200 \(setq diary-mail-days 3
1201 diary-file \"/path/to/diary.file\"
1202 calendar-date-style 'european
1203 diary-mail-addr \"user@host.name\")
1205 \(diary-mail-entries)
1207 # diary-rem.el ends here
1210 (if (string-equal diary-mail-addr "")
1211 (error "You must set `diary-mail-addr' to use this command")
1212 (let ((diary-display-function 'diary-fancy-display))
1213 (diary-list-entries (calendar-current-date) (or ndays diary-mail-days)))
1214 (compose-mail diary-mail-addr
1215 (concat "Diary entries generated "
1216 (calendar-date-string (calendar-current-date))))
1218 (if (get-buffer diary-fancy-buffer)
1219 (with-current-buffer diary-fancy-buffer (buffer-string))
1220 "No entries found"))
1221 (call-interactively (get mail-user-agent 'sendfunc))))
1223 (defun diary-name-pattern (string-array &optional abbrev-array paren)
1224 "Return a regexp matching the strings in the array STRING-ARRAY.
1225 If the optional argument ABBREV-ARRAY is present, then the function
1226 `calendar-abbrev-construct' is used to construct abbreviations from the
1227 two supplied arrays. The returned regexp will then also match these
1228 abbreviations, with or without final `.' characters. If the optional
1229 argument PAREN is non-nil, the regexp is surrounded by parentheses."
1230 (regexp-opt (append string-array
1232 (calendar-abbrev-construct abbrev-array
1235 (calendar-abbrev-construct abbrev-array
1241 (defvar diary-marking-entries-flag nil
1242 "True during the marking of diary entries, nil otherwise.")
1244 (defvar diary-marking-entry-flag nil
1245 "True during the marking of diary entries, if current entry is marking.")
1247 ;; file-glob-attrs bound in diary-mark-entries.
1248 (defun diary-mark-entries-1 (markfunc &optional months symbol absfunc)
1249 "Mark diary entries of a certain type.
1250 MARKFUNC is a function that marks entries of the appropriate type
1251 matching a given date pattern. MONTHS is an array of month names.
1252 SYMBOL marks diary entries of the type in question. ABSFUNC is a
1253 function that converts absolute dates to dates of the appropriate type. "
1254 (let ((dayname (diary-name-pattern calendar-day-name-array
1255 calendar-day-abbrev-array))
1256 (monthname (format "%s\\|\\*"
1258 (diary-name-pattern months)
1259 (diary-name-pattern calendar-month-name-array
1260 calendar-month-abbrev-array))))
1261 (month "[0-9]+\\|\\*")
1262 (day "[0-9]+\\|\\*")
1263 (year "[0-9]+\\|\\*")
1264 (case-fold-search t)
1266 (dolist (date-form diary-date-forms)
1267 (if (eq (car date-form) 'backup) ; ignore 'backup directive
1268 (setq date-form (cdr date-form)))
1269 (let* ((l (length date-form))
1270 (d-name-pos (- l (length (memq 'dayname date-form))))
1271 (d-name-pos (if (/= l d-name-pos) (1+ d-name-pos)))
1272 (m-name-pos (- l (length (memq 'monthname date-form))))
1273 (m-name-pos (if (/= l m-name-pos) (1+ m-name-pos)))
1274 (d-pos (- l (length (memq 'day date-form))))
1275 (d-pos (if (/= l d-pos) (1+ d-pos)))
1276 (m-pos (- l (length (memq 'month date-form))))
1277 (m-pos (if (/= l m-pos) (1+ m-pos)))
1278 (y-pos (- l (length (memq 'year date-form))))
1279 (y-pos (if (/= l y-pos) (1+ y-pos)))
1280 (regexp (format "^%s\\(%s\\)"
1281 (if symbol (regexp-quote symbol) "")
1282 (mapconcat 'eval date-form "\\)\\("))))
1283 (goto-char (point-min))
1284 (while (re-search-forward regexp nil t)
1287 (match-string-no-properties d-name-pos)))
1290 (match-string-no-properties m-name-pos)))
1291 (mm (string-to-number
1293 (match-string-no-properties m-pos)
1295 (dd (string-to-number
1297 (match-string-no-properties d-pos)
1300 (match-string-no-properties y-pos)))
1303 (if (and (= (length y-str) 2)
1304 diary-abbreviated-year-flag)
1306 (calendar-extract-year
1310 (calendar-absolute-from-gregorian
1311 (calendar-current-date)))
1312 (calendar-current-date))))
1313 (y (+ (string-to-number y-str)
1314 ;; Current century, eg 2000.
1315 (* 100 (/ current-y 100))))
1316 (offset (- y current-y)))
1317 ;; Add 2-digit year to current century.
1318 ;; If more than 50 years in the future,
1319 ;; assume last century. If more than 50
1320 ;; years in the past, assume next century.
1326 (string-to-number y-str)))))
1327 (setq marks (cadr (diary-pull-attrs
1328 (buffer-substring-no-properties
1329 (point) (line-end-position))
1331 ;; Only mark all days of a given name if the pattern
1332 ;; contains no more specific elements.
1333 (if (and dd-name (not (or d-pos m-pos y-pos)))
1334 (calendar-mark-days-named
1335 (cdr (assoc-string dd-name
1336 (calendar-make-alist
1337 calendar-day-name-array
1338 0 nil calendar-day-abbrev-array) t)) marks)
1341 (if (string-equal mm-name "*") 0
1344 (if months (calendar-make-alist months)
1345 (calendar-make-alist
1346 calendar-month-name-array
1347 1 nil calendar-month-abbrev-array)) t)))))
1348 (funcall markfunc mm dd yy marks))))))))
1351 (defun diary-mark-entries (&optional redraw)
1352 "Mark days in the calendar window that have diary entries.
1353 Each entry in the diary file visible in the calendar window is
1354 marked. After the entries are marked, the hooks
1355 `diary-nongregorian-marking-hook' and `diary-mark-entries-hook'
1356 are run. If the optional argument REDRAW is non-nil (which is
1357 the case interactively, for example) then any existing diary
1358 marks are first removed. This is intended to deal with deleted
1361 ;; To remove any deleted diary entries. Do not redraw when:
1362 ;; i) processing #include diary files (else only get the marks from
1363 ;; the last #include file processed).
1364 ;; ii) called via calendar-redraw (since calendar has already been
1366 ;; Use of REDRAW handles both of these cases.
1367 (when (and redraw calendar-mark-diary-entries-flag)
1368 (setq calendar-mark-diary-entries-flag nil)
1370 (let ((diary-marking-entries-flag t)
1372 (with-current-buffer (find-file-noselect (diary-check-diary-file) t)
1374 (when (eq major-mode (default-value 'major-mode)) (diary-mode))
1375 (setq calendar-mark-diary-entries-flag t)
1376 (message "Marking diary entries...")
1377 (setq file-glob-attrs (nth 1 (diary-pull-attrs nil '())))
1378 (with-syntax-table diary-syntax-table
1379 (diary-mark-entries-1 'calendar-mark-date-pattern)
1380 (diary-mark-sexp-entries)
1381 (run-hooks 'diary-nongregorian-marking-hook
1382 'diary-mark-entries-hook))
1383 (message "Marking diary entries...done")))))
1386 (define-obsolete-function-alias 'mark-diary-entries 'diary-mark-entries "23.1")
1388 (defun diary-sexp-entry (sexp entry date)
1389 "Process a SEXP diary ENTRY for DATE."
1390 (let ((result (if calendar-debug-sexp
1391 (let ((debug-on-error t))
1392 (eval (car (read-from-string sexp))))
1394 (eval (car (read-from-string sexp)))
1397 (message "Bad sexp at line %d in %s: %s"
1398 (count-lines (point-min) (point))
1401 (cond ((stringp result) result)
1402 ((and (consp result)
1403 (stringp (cdr result))) result)
1407 (defvar displayed-year) ; bound in calendar-generate
1408 (defvar displayed-month)
1410 (defun diary-mark-sexp-entries ()
1411 "Mark days in the calendar window that have sexp diary entries.
1412 Each entry in the diary file (or included files) visible in the calendar window
1413 is marked. See the documentation for the function `diary-list-sexp-entries'."
1414 (let* ((sexp-mark (regexp-quote diary-sexp-entry-symbol))
1415 (s-entry (format "^\\(%s(\\)\\|\\(%s%s(diary-remind\\)" sexp-mark
1416 (regexp-quote diary-nonmarking-symbol)
1418 (file-glob-attrs (nth 1 (diary-pull-attrs nil '())))
1419 m y first-date last-date date mark file-glob-attrs
1420 sexp-start sexp entry entry-start)
1421 (with-current-buffer calendar-buffer
1422 (setq m displayed-month
1424 (calendar-increment-month m y -1)
1425 (setq first-date (calendar-absolute-from-gregorian (list m 1 y)))
1426 (calendar-increment-month m y 2)
1428 (calendar-absolute-from-gregorian
1429 (list m (calendar-last-day-of-month m y) y)))
1430 (goto-char (point-min))
1431 (while (re-search-forward s-entry nil t)
1432 (setq diary-marking-entry-flag (char-equal (preceding-char) ?\())
1433 (re-search-backward "(")
1434 (setq sexp-start (point))
1436 (setq sexp (buffer-substring-no-properties sexp-start (point)))
1438 (if (and (bolp) (not (looking-at "[ \t]")))
1439 ;; Diary entry consists only of the sexp.
1443 (setq entry-start (point))
1444 ;; Find end of entry.
1446 (while (looking-at "[ \t]")
1448 (if (bolp) (backward-char 1))
1449 (setq entry (buffer-substring-no-properties entry-start (point))))
1450 (setq date (1- first-date))
1451 ;; FIXME this loops over all visible dates.
1452 ;; Could be optimized in many cases. Depends on whether t or * present.
1453 (while (<= (setq date (1+ date)) last-date)
1454 (when (setq mark (diary-sexp-entry
1456 (calendar-gregorian-from-absolute date)))
1457 (calendar-mark-visible-date
1458 (calendar-gregorian-from-absolute date)
1459 (or (cadr (diary-pull-attrs entry file-glob-attrs))
1460 (if (consp mark) (car mark)))))))))
1462 (define-obsolete-function-alias 'mark-sexp-diary-entries
1463 'diary-mark-sexp-entries "23.1")
1465 (defun diary-mark-included-diary-files ()
1466 "Mark the diary entries from other diary files with those of the diary file.
1467 This function is suitable for use with `diary-mark-entries-hook'; it enables
1468 you to use shared diary files together with your own. The files included are
1469 specified in the `diary-file' by lines of this form:
1470 #include \"filename\"
1471 This is recursive; that is, #include directives in diary files thus included
1472 are obeyed. You can change the `#include' to some other string by changing
1473 the variable `diary-include-string'."
1474 (goto-char (point-min))
1475 (while (re-search-forward
1476 (format "^%s \"\\([^\"]*\\)\"" (regexp-quote diary-include-string))
1478 (let* ((diary-file (match-string-no-properties 1))
1479 (diary-mark-entries-hook 'diary-mark-included-diary-files)
1480 (dbuff (find-buffer-visiting diary-file)))
1481 (if (file-exists-p diary-file)
1482 (if (file-readable-p diary-file)
1484 (diary-mark-entries)
1486 (kill-buffer (find-buffer-visiting diary-file))))
1488 (message "Can't read included diary file %s" diary-file)
1491 (message "Can't find included diary file %s" diary-file)
1493 (goto-char (point-min)))
1495 (define-obsolete-function-alias 'mark-included-diary-files
1496 'diary-mark-included-diary-files "23.1")
1498 (defun calendar-mark-days-named (dayname &optional color)
1499 "Mark all dates in the calendar window that are day DAYNAME of the week.
1500 0 means all Sundays, 1 means all Mondays, and so on.
1501 Optional argument COLOR is passed to `calendar-mark-visible-date' as MARK."
1502 (with-current-buffer calendar-buffer
1503 (let ((prev-month displayed-month)
1504 (prev-year displayed-year)
1505 (succ-month displayed-month)
1506 (succ-year displayed-year)
1509 (calendar-increment-month succ-month succ-year 1)
1510 (calendar-increment-month prev-month prev-year -1)
1511 (setq day (calendar-absolute-from-gregorian
1512 (calendar-nth-named-day 1 dayname prev-month prev-year))
1513 last-day (calendar-absolute-from-gregorian
1514 (calendar-nth-named-day -1 dayname succ-month succ-year)))
1515 (while (<= day last-day)
1516 (calendar-mark-visible-date (calendar-gregorian-from-absolute day)
1518 (setq day (+ day 7))))))
1520 (define-obsolete-function-alias 'mark-calendar-days-named
1521 'calendar-mark-days-named "23.1")
1523 (defun calendar-mark-month (month year p-month p-day p-year &optional color)
1524 "Mark dates in the MONTH/YEAR that conform to pattern P-MONTH/P-DAY/P-YEAR.
1525 A value of 0 in any position of the pattern is a wildcard.
1526 Optional argument COLOR is passed to `calendar-mark-visible-date' as MARK."
1527 (if (or (and (= month p-month)
1528 (or (zerop p-year) (= year p-year)))
1529 (and (zerop p-month)
1530 (or (zerop p-year) (= year p-year))))
1532 (dotimes (i (calendar-last-day-of-month month year))
1533 (calendar-mark-visible-date (list month (1+ i) year) color))
1534 (calendar-mark-visible-date (list month p-day year) color))))
1536 (define-obsolete-function-alias 'mark-calendar-month
1537 'calendar-mark-month "23.1")
1539 (defun calendar-mark-date-pattern (month day year &optional color)
1540 "Mark all dates in the calendar window that conform to MONTH/DAY/YEAR.
1541 A value of 0 in any position is a wildcard. Optional argument COLOR is
1542 passed to `calendar-mark-visible-date' as MARK."
1543 (with-current-buffer calendar-buffer
1544 (let ((m displayed-month)
1546 (calendar-increment-month m y -1)
1547 (dotimes (_idummy 3)
1548 (calendar-mark-month m y month day year color)
1549 (calendar-increment-month m y 1)))))
1551 (define-obsolete-function-alias 'mark-calendar-date-pattern
1552 'calendar-mark-date-pattern "23.1")
1554 ;; Bahai, Hebrew, Islamic.
1555 (defun calendar-mark-complex (month day year fromabs &optional color)
1556 "Mark dates in the calendar conforming to MONTH DAY YEAR of some system.
1557 The function FROMABS converts absolute dates to the appropriate date system.
1558 Optional argument COLOR is passed to `calendar-mark-visible-date' as MARK."
1559 ;; Not one of the simple cases--check all visible dates for match.
1560 ;; Actually, the following code takes care of ALL of the cases, but
1561 ;; it's much too slow to be used for the simple (common) cases.
1562 (let* ((m displayed-month)
1565 (calendar-increment-month m y -1)
1566 (calendar-absolute-from-gregorian (list m 1 y))))
1568 (calendar-increment-month m y 2)
1569 (calendar-absolute-from-gregorian
1570 (list m (calendar-last-day-of-month m y) y))))
1571 (date (1- first-date))
1573 (while (<= (setq date (1+ date)) last-date)
1574 (setq local-date (funcall fromabs date))
1575 (and (or (zerop month)
1576 (= month (calendar-extract-month local-date)))
1578 (= day (calendar-extract-day local-date)))
1580 (= year (calendar-extract-year local-date)))
1581 (calendar-mark-visible-date
1582 (calendar-gregorian-from-absolute date) color)))))
1585 (defun calendar-mark-1 (month day year fromabs toabs &optional color)
1586 "Mark dates in the calendar conforming to MONTH DAY YEAR of some system.
1587 The function FROMABS converts absolute dates to the appropriate date system.
1588 The function TOABS carries out the inverse operation. Optional argument
1589 COLOR is passed to `calendar-mark-visible-date' as MARK."
1590 (with-current-buffer calendar-buffer
1591 (if (and (not (zerop month)) (not (zerop day)))
1592 (if (not (zerop year))
1593 ;; Fully specified date.
1594 (let ((date (calendar-gregorian-from-absolute
1595 (funcall toabs (list month day year)))))
1596 (if (calendar-date-is-visible-p date)
1597 (calendar-mark-visible-date date color)))
1598 ;; Month and day in any year--this taken from the holiday stuff.
1599 (let* ((i-date (funcall fromabs
1600 (calendar-absolute-from-gregorian
1601 (list displayed-month 15 displayed-year))))
1602 (m (calendar-extract-month i-date))
1603 (y (calendar-extract-year i-date))
1605 (unless (< m 1) ; calendar doesn't apply
1606 (calendar-increment-month m y (- 10 month))
1607 (and (> m 7) ; date might be visible
1608 (calendar-date-is-visible-p
1609 (setq date (calendar-gregorian-from-absolute
1610 (funcall toabs (list month day y)))))
1611 (calendar-mark-visible-date date color)))))
1612 (calendar-mark-complex month day year
1613 'calendar-bahai-from-absolute color))))
1616 (defun diary-entry-time (s)
1617 "Return time at the beginning of the string S as a military-style integer.
1618 For example, returns 1325 for 1:25pm.
1620 Returns `diary-unknown-time' (default value -9999) if no time is recognized.
1621 The recognized forms are XXXX, X:XX, or XX:XX (military time), and XXam,
1622 XXAM, XXpm, XXPM, XX:XXam, XX:XXAM, XX:XXpm, or XX:XXPM. A period (.) can
1623 be used instead of a colon (:) to separate the hour and minute parts."
1624 (let (case-fold-search)
1625 (cond ((string-match ; military time
1626 "\\`[ \t\n]*\\([0-9]?[0-9]\\)[:.]?\\([0-9][0-9]\\)\\(\\>\\|[^ap]\\)"
1628 (+ (* 100 (string-to-number (match-string 1 s)))
1629 (string-to-number (match-string 2 s))))
1630 ((string-match ; hour only (XXam or XXpm)
1631 "\\`[ \t\n]*\\([0-9]?[0-9]\\)\\([ap]\\)m\\>" s)
1632 (+ (* 100 (% (string-to-number (match-string 1 s)) 12))
1633 (if (equal ?a (downcase (aref s (match-beginning 2))))
1635 ((string-match ; hour and minute (XX:XXam or XX:XXpm)
1636 "\\`[ \t\n]*\\([0-9]?[0-9]\\)[:.]\\([0-9][0-9]\\)\\([ap]\\)m\\>" s)
1637 (+ (* 100 (% (string-to-number (match-string 1 s)) 12))
1638 (string-to-number (match-string 2 s))
1639 (if (equal ?a (downcase (aref s (match-beginning 3))))
1641 (t diary-unknown-time)))) ; unrecognizable
1643 (defun diary-entry-compare (e1 e2)
1644 "Return t if E1 is earlier than E2."
1645 (or (calendar-date-compare e1 e2)
1646 (and (calendar-date-equal (car e1) (car e2))
1647 (let* ((ts1 (cadr e1)) (t1 (diary-entry-time ts1))
1648 (ts2 (cadr e2)) (t2 (diary-entry-time ts2)))
1651 (string-lessp ts1 ts2)))))))
1653 (defun diary-sort-entries ()
1654 "Sort the list of diary entries by time of day.
1655 If you add this function to `diary-list-entries-hook', it should
1656 be the last item in the hook, in case earlier items add diary
1657 entries, or change the order."
1658 (setq diary-entries-list (sort diary-entries-list 'diary-entry-compare)))
1660 (define-obsolete-function-alias 'sort-diary-entries 'diary-sort-entries "23.1")
1663 (defun diary-list-sexp-entries (date)
1664 "Add sexp entries for DATE from the diary file to `diary-entries-list'.
1665 Also, make them visible in the diary. Returns t if any entries are found.
1667 Sexp diary entries must be prefaced by a `diary-sexp-entry-symbol'
1668 \(normally `%%'). The form of a sexp diary entry is
1672 Both ENTRY and DATE are available when the SEXP is evaluated. If
1673 the SEXP returns nil, the diary entry does not apply. If it
1674 returns a non-nil value, ENTRY will be taken to apply to DATE; if
1675 the value is a string, that string will be the diary entry in the
1676 fancy diary display.
1678 For example, the following diary entry will apply to the 21st of
1679 the month if it is a weekday and the Friday before if the 21st is
1682 &%%(let ((dayname (calendar-day-of-week date))
1683 (day (calendar-extract-day date)))
1685 (and (= day 21) (memq dayname '(1 2 3 4 5)))
1686 (and (memq day '(19 20)) (= dayname 5)))
1687 ) UIUC pay checks deposited
1689 A number of built-in functions are available for this type of
1690 diary entry. In the following, the optional parameter MARK
1691 specifies a face or single-character string to use when
1692 highlighting the day in the calendar. For those functions that
1693 take MONTH, DAY, and YEAR as arguments, the order of the input
1694 parameters changes according to `calendar-date-style' (e.g. to
1695 DAY MONTH YEAR in the European style).
1697 %%(diary-date MONTH DAY YEAR &optional MARK) text
1698 Entry applies if date is MONTH, DAY, YEAR. DAY, MONTH, and YEAR can
1699 be a list of integers, `t' (meaning all values), or an integer.
1701 %%(diary-float MONTH DAYNAME N &optional DAY MARK) text
1702 Entry will appear on the Nth DAYNAME after/before MONTH DAY.
1703 DAYNAME=0 means Sunday, DAYNAME=1 means Monday, and so on.
1704 If N>0, use the Nth DAYNAME after MONTH DAY.
1705 If N<0, use the Nth DAYNAME before MONTH DAY.
1706 DAY defaults to 1 if N>0, and MONTH's last day otherwise.
1707 MONTH can be a list of months, a single month, or `t' to
1710 %%(diary-block M1 D1 Y1 M2 D2 Y2 &optional MARK) text
1711 Entry will appear on dates between M1/D1/Y1 and M2/D2/Y2,
1714 %%(diary-anniversary MONTH DAY YEAR &optional MARK) text
1715 Entry will appear on anniversary dates of MONTH DAY, YEAR.
1716 Text can contain `%d' or `%d%s'; `%d' will be replaced by the
1717 number of years since the MONTH DAY, YEAR, and `%s' by the
1718 ordinal ending of that number (i.e. `st', `nd', `rd' or `th',
1719 as appropriate). The anniversary of February 29 is
1720 considered to be March 1 in a non-leap year.
1722 %%(diary-cyclic N MONTH DAY YEAR &optional MARK) text
1723 Entry will appear every N days, starting MONTH DAY, YEAR.
1724 Text can contain `%d' or `%d%s'; `%d' will be replaced by the
1725 number of repetitions since the MONTH DAY, YEAR and `%s' by
1726 the ordinal ending of that number (i.e. `st', `nd', `rd' or
1727 `th', as appropriate).
1729 %%(diary-remind SEXP DAYS &optional MARKING) text
1730 Entry is a reminder for diary sexp SEXP. DAYS is either a
1731 single number or a list of numbers indicating the number(s)
1732 of days before the event that the warning(s) should occur.
1733 A negative number -DAYS has the same meaning as a list (1 2 ... DAYS).
1734 If the current date is (one of) DAYS before the event indicated
1735 by EXPR, then a suitable message (as specified by
1736 `diary-remind-message') appears. In addition to the
1737 reminders beforehand, the diary entry also appears on the
1738 date itself. If optional MARKING is non-nil then the
1739 *reminders* are marked on the calendar. Marking of reminders
1740 is independent of whether the entry *itself* is a marking or
1743 %%(diary-hebrew-yahrzeit MONTH DAY YEAR) text
1744 Text is assumed to be the name of the person; the date is the
1745 date of death on the *civil* calendar. The diary entry will
1746 appear on the proper Hebrew-date anniversary and on the day
1749 All the remaining functions do not accept any text, and so only
1750 make sense with `diary-fancy-display'. Most produce output every day.
1752 `diary-day-of-year' - day of year and number of days remaining
1753 `diary-iso-date' - ISO commercial date
1754 `diary-astro-day-number' - astronomical (Julian) day number
1755 `diary-sunrise-sunset' - local times of sunrise and sunset
1757 These functions give the date in alternative calendrical systems:
1759 `diary-bahai-date', `diary-chinese-date', `diary-coptic-date',
1760 `diary-ethiopic-date', `diary-french-date', `diary-hebrew-date',
1761 `diary-islamic-date', `diary-julian-date', `diary-mayan-date',
1762 `diary-persian-date'
1764 Theses functions only produce output on certain dates:
1766 `diary-lunar-phases' - phases of moon (on the appropriate days)
1767 `diary-hebrew-omer' - Omer count, within 50 days after Passover
1768 `diary-hebrew-parasha' - weekly parasha, every Saturday
1769 `diary-hebrew-rosh-hodesh' - Rosh Hodesh, or the day or Saturday before
1770 `diary-hebrew-sabbath-candles' - local time of candle lighting, on Fridays
1773 Marking these entries is *extremely* time consuming, so it is
1774 best if they are non-marking."
1775 (let ((s-entry (format "^%s?%s(" (regexp-quote diary-nonmarking-symbol)
1776 (regexp-quote diary-sexp-entry-symbol)))
1777 entry-found file-glob-attrs marks
1778 sexp-start sexp entry specifier entry-start line-start
1779 diary-entry temp literal)
1780 (goto-char (point-min))
1781 (setq file-glob-attrs (nth 1 (diary-pull-attrs nil '())))
1782 (while (re-search-forward s-entry nil t)
1784 (setq sexp-start (point))
1786 (setq sexp (buffer-substring-no-properties sexp-start (point))
1787 line-start (line-end-position 0)
1789 (buffer-substring-no-properties (1+ line-start) (point))
1790 entry-start (1+ line-start))
1792 (if (and (bolp) (not (looking-at "[ \t]")))
1793 ;; Diary entry consists only of the sexp.
1797 (setq entry-start (point))
1799 (while (looking-at "[ \t]")
1801 (if (bolp) (backward-char 1))
1802 (setq entry (buffer-substring-no-properties entry-start (point))))
1803 (setq diary-entry (diary-sexp-entry sexp entry date)
1804 literal entry ; before evaluation
1805 entry (if (consp diary-entry)
1809 (remove-overlays line-start (point) 'invisible 'diary)
1810 (if (< 0 (length entry))
1811 (setq temp (diary-pull-attrs entry file-glob-attrs)
1813 marks (nth 1 temp))))
1814 (diary-add-to-list date entry specifier
1815 (if entry-start (copy-marker entry-start))
1817 (setq entry-found (or entry-found diary-entry)))
1820 (define-obsolete-function-alias 'list-sexp-diary-entries
1821 'diary-list-sexp-entries "23.1")
1823 (defun diary-make-date (a b c)
1824 "Convert A B C into the internal calendar date form.
1825 The expected order of the inputs depends on `calendar-date-style',
1826 e.g. in the European case, A = day, B = month, C = year. Returns
1827 a list (MONTH DAY YEAR), i.e. the American style, which is the
1828 form used internally by the calendar and diary."
1829 (cond ((eq calendar-date-style 'iso) ; YMD
1831 ((eq calendar-date-style 'european) ; DMY
1836 ;;; Sexp diary functions.
1841 ;; To be called from diary-sexp-entry, where DATE, ENTRY are bound.
1842 (defun diary-date (month day year &optional mark)
1843 "Specific date(s) diary entry.
1844 Entry applies if date is MONTH, DAY, YEAR. Each parameter can be a
1845 list of integers, `t' (meaning all values), or an integer. The order
1846 of the input parameters changes according to `calendar-date-style'
1847 \(e.g. to DAY MONTH YEAR in the European style).
1849 An optional parameter MARK specifies a face or single-character string
1850 to use when highlighting the day in the calendar."
1851 (let* ((ddate (diary-make-date month day year))
1852 (dd (calendar-extract-day ddate))
1853 (mm (calendar-extract-month ddate))
1854 (yy (calendar-extract-year ddate))
1855 (m (calendar-extract-month date))
1856 (y (calendar-extract-year date))
1857 (d (calendar-extract-day date)))
1859 (or (and (listp dd) (memq d dd))
1862 (or (and (listp mm) (memq m mm))
1865 (or (and (listp yy) (memq y yy))
1868 (cons mark entry))))
1870 ;; To be called from diary-sexp-entry, where DATE, ENTRY are bound.
1871 (defun diary-block (m1 d1 y1 m2 d2 y2 &optional mark)
1873 Entry applies if date is between, or on one of, two dates. The order
1874 of the input parameters changes according to `calendar-date-style'
1875 \(e.g. to D1, M1, Y1, D2, M2, Y2 in the European style).
1877 An optional parameter MARK specifies a face or single-character string
1878 to use when highlighting the day in the calendar."
1879 (let ((date1 (calendar-absolute-from-gregorian
1880 (diary-make-date m1 d1 y1)))
1881 (date2 (calendar-absolute-from-gregorian
1882 (diary-make-date m2 d2 y2)))
1883 (d (calendar-absolute-from-gregorian date)))
1884 (and (<= date1 d) (<= d date2)
1885 (cons mark entry))))
1887 ;; To be called from diary-sexp-entry, where DATE, ENTRY are bound.
1888 (defun diary-float (month dayname n &optional day mark)
1889 "Diary entry for the Nth DAYNAME after/before MONTH DAY.
1890 DAYNAME=0 means Sunday, DAYNAME=1 means Monday, and so on.
1891 If N>0, use the Nth DAYNAME after MONTH DAY.
1892 If N<0, use the Nth DAYNAME before MONTH DAY.
1893 DAY defaults to 1 if N>0, and MONTH's last day otherwise.
1894 MONTH can be a list of months, an integer, or `t' (meaning all months).
1895 Optional MARK specifies a face or single-character string to use when
1896 highlighting the day in the calendar."
1897 ;; This is messy because the diary entry may apply, but the date on which it
1898 ;; is based can be in a different month/year. For example, asking for the
1899 ;; first Monday after December 30. For large values of |n| the problem is
1901 (and (= dayname (calendar-day-of-week date))
1902 (let* ((m (calendar-extract-month date))
1903 (d (calendar-extract-day date))
1904 (y (calendar-extract-year date))
1905 ;; Last (n>0) or first (n<0) possible base date for entry.
1907 (calendar-nth-named-absday (- n) dayname m y d))
1908 (last-abs (if (> n 0) limit (+ limit 6)))
1909 (first-abs (if (> n 0) (- limit 6) limit))
1910 (last (calendar-gregorian-from-absolute last-abs))
1911 (first (calendar-gregorian-from-absolute first-abs))
1912 ;; m1, d1 is first possible base date.
1913 (m1 (calendar-extract-month first))
1914 (d1 (calendar-extract-day first))
1915 (y1 (calendar-extract-year first))
1916 ;; m2, d2 is last possible base date.
1917 (m2 (calendar-extract-month last))
1918 (d2 (calendar-extract-day last))
1919 (y2 (calendar-extract-year last)))
1920 (if (or (and (= m1 m2) ; only possible base dates in one month
1925 (let ((d (or day (if (> n 0)
1927 (calendar-last-day-of-month m1 y1)))))
1928 (and (<= d1 d) (<= d d2))))
1929 ;; Only possible base dates straddle two months.
1931 (and (= y1 y2) (< m1 m2)))
1933 ;; m1, d1 works as a base date.
1939 (<= d1 (or day (if (> n 0)
1941 (calendar-last-day-of-month m1 y1)))))
1942 ;; m2, d2 works as a base date.
1943 (and (or (eq month t)
1947 (<= (or day (if (> n 0)
1949 (calendar-last-day-of-month m2 y2)))
1951 (cons mark entry)))))
1953 (defun diary-ordinal-suffix (n)
1954 "Ordinal suffix for N. (That is, `st', `nd', `rd', or `th', as appropriate.)"
1955 (if (or (memq (% n 100) '(11 12 13))
1958 (aref ["th" "st" "nd" "rd"] (% n 10))))
1960 ;; To be called from diary-sexp-entry, where DATE, ENTRY are bound.
1961 (defun diary-anniversary (month day &optional year mark)
1962 "Anniversary diary entry.
1963 Entry applies if date is the anniversary of MONTH, DAY, YEAR.
1964 The order of the input parameters changes according to
1965 `calendar-date-style' (e.g. to DAY MONTH YEAR in the European style).
1967 The diary entry can contain `%d' or `%d%s'; the %d will be replaced
1968 by the number of years since the MONTH, DAY, YEAR, and the %s will
1969 be replaced by the ordinal ending of that number (that is, `st',
1970 `nd', `rd' or `th', as appropriate). The anniversary of February 29
1971 is considered to be March 1 in non-leap years.
1973 An optional parameter MARK specifies a face or single-character
1974 string to use when highlighting the day in the calendar."
1975 (let* ((ddate (diary-make-date month day year))
1976 (dd (calendar-extract-day ddate))
1977 (mm (calendar-extract-month ddate))
1978 (yy (calendar-extract-year ddate))
1979 (y (calendar-extract-year date))
1980 (diff (if yy (- y yy) 100)))
1981 (and (= mm 2) (= dd 29) (not (calendar-leap-year-p y))
1984 (and (> diff 0) (calendar-date-equal (list mm dd y) date)
1985 (cons mark (format entry diff (diary-ordinal-suffix diff))))))
1987 ;; To be called from diary-sexp-entry, where DATE, ENTRY are bound.
1988 (defun diary-cyclic (n month day year &optional mark)
1989 "Cycle diary entry--entry applies every N days starting at MONTH, DAY, YEAR.
1990 The order of the input parameters changes according to
1991 `calendar-date-style' (e.g. to N DAY MONTH YEAR in the European
1992 style). The entry can contain `%d' or `%d%s'; the %d will be
1993 replaced by the number of repetitions since the MONTH DAY YEAR,
1994 and %s by the ordinal ending of that number (that is, `st', `nd',
1995 `rd' or `th', as appropriate).
1997 An optional parameter MARK specifies a face or single-character
1998 string to use when highlighting the day in the calendar."
2000 (error "Day count must be positive"))
2001 (let* ((diff (- (calendar-absolute-from-gregorian date)
2002 (calendar-absolute-from-gregorian
2003 (diary-make-date month day year))))
2005 (and (>= diff 0) (zerop (% diff n))
2006 (cons mark (format entry cycle (diary-ordinal-suffix cycle))))))
2008 (defun diary-day-of-year ()
2009 "Day of year and number of days remaining in the year of date diary entry."
2010 (calendar-day-of-year-string date))
2012 (defun diary-remind (sexp days &optional marking)
2013 "Provide a reminder of a diary entry.
2014 SEXP is a diary-sexp. DAYS is either a single number or a list
2015 of numbers indicating the number(s) of days before the event that
2016 the warning(s) should occur on. A negative number -DAYS has the
2017 same meaning as a list (1 2 ... DAYS). If the current date
2018 is (one of) DAYS before the event indicated by SEXP, then this function
2019 returns a suitable message (as specified by `diary-remind-message').
2021 In addition to the reminders beforehand, the diary entry also
2022 appears on the date itself.
2024 A `diary-nonmarking-symbol' at the beginning of the line of the
2025 `diary-remind' entry specifies that the diary entry (not the
2026 reminder) is non-marking. Marking of reminders is independent of
2027 whether the entry itself is a marking or nonmarking; if optional
2028 parameter MARKING is non-nil then the reminders are marked on the
2030 ;; `date' has a value at this point, from diary-sexp-entry.
2031 ;; Convert a negative number to a list of days.
2032 (and (integerp days)
2034 (setq days (number-sequence 1 (- days))))
2035 (let ((diary-entry (eval sexp)))
2037 ;; Diary entry applies on date.
2039 (or (not diary-marking-entries-flag) diary-marking-entry-flag))
2041 ;; Diary entry may apply to `days' before date.
2042 ((and (integerp days)
2043 (not diary-entry) ; diary entry does not apply to date
2044 (or (not diary-marking-entries-flag) marking))
2045 ;; Adjust date, and re-evaluate.
2046 (let ((date (calendar-gregorian-from-absolute
2047 (+ (calendar-absolute-from-gregorian date) days))))
2048 (when (setq diary-entry (eval sexp))
2049 ;; Discard any mark portion from diary-anniversary, etc.
2050 (if (consp diary-entry) (setq diary-entry (cdr diary-entry)))
2051 (mapconcat 'eval diary-remind-message ""))))
2052 ;; Diary entry may apply to one of a list of days before date.
2053 ((and (listp days) days)
2054 (or (diary-remind sexp (car days) marking)
2055 (diary-remind sexp (cdr days) marking))))))
2058 ;;; Diary insertion functions.
2061 (defun diary-make-entry (string &optional nonmarking file)
2062 "Insert a diary entry STRING which may be NONMARKING in FILE.
2063 If omitted, NONMARKING defaults to nil and FILE defaults to
2065 (let ((pop-up-frames (or pop-up-frames
2066 (window-dedicated-p (selected-window)))))
2067 (find-file-other-window (or file diary-file)))
2068 (when (eq major-mode (default-value 'major-mode)) (diary-mode))
2070 (diary-unhide-everything)
2071 (goto-char (point-max))
2072 (when (let ((case-fold-search t))
2073 (search-backward "Local Variables:"
2074 (max (- (point-max) 3000) (point-min))
2081 (if nonmarking diary-nonmarking-symbol "")
2085 (define-obsolete-function-alias 'make-diary-entry 'diary-make-entry "23.1")
2088 (defun diary-insert-entry (arg &optional event)
2089 "Insert a diary entry for the date indicated by point.
2090 Prefix argument ARG makes the entry nonmarking."
2092 (list current-prefix-arg last-nonmenu-event))
2093 (diary-make-entry (calendar-date-string (calendar-cursor-to-date t event) t t)
2097 (define-obsolete-function-alias 'insert-diary-entry 'diary-insert-entry "23.1")
2100 (defun diary-insert-weekly-entry (arg)
2101 "Insert a weekly diary entry for the day of the week indicated by point.
2102 Prefix argument ARG makes the entry nonmarking."
2104 (diary-make-entry (calendar-day-name (calendar-cursor-to-date t))
2108 (define-obsolete-function-alias 'insert-weekly-diary-entry
2109 'diary-insert-weekly-entry "23.1")
2111 (defun diary-date-display-form (&optional type)
2112 "Return value for `calendar-date-display-form' using `calendar-date-style'.
2113 Optional symbol TYPE is either `monthly' or `yearly'."
2114 (cond ((eq type 'monthly) (cond ((eq calendar-date-style 'iso)
2115 '((format "*-*-%.2d"
2116 (string-to-number day))))
2117 ((eq calendar-date-style 'european)
2120 ((eq type 'yearly) (cond ((eq calendar-date-style 'iso)
2121 '((format "*-%.2d-%.2d"
2122 (string-to-number month)
2123 (string-to-number day))))
2124 ((eq calendar-date-style 'european)
2125 '(day " " monthname))
2126 (t '(monthname " " day))))
2127 ;; Iso cannot contain "-", because this form used eg by
2128 ;; diary-insert-anniversary-entry.
2129 (t (cond ((eq calendar-date-style 'iso)
2130 '((format "%s %.2d %.2d" year
2131 (string-to-number month) (string-to-number day))))
2132 ((eq calendar-date-style 'european)
2133 '(day " " month " " year))
2134 (t '(month " " day " " year))))))
2136 (defun diary-insert-entry-1 (&optional type nomark months symbol absfunc)
2137 "Subroutine to insert a diary entry related to the date at point.
2138 TYPE is the type of entry (`monthly' or `yearly'). NOMARK non-nil
2139 means make the entry non-marking. Array MONTHS is used in place
2140 of `calendar-month-name-array'. String SYMBOL marks the type of
2141 diary entry. Function ABSFUNC converts absolute dates to dates of
2142 the appropriate type."
2143 (let ((calendar-date-display-form (if type
2144 (diary-date-display-form type)
2145 calendar-date-display-form))
2146 (calendar-month-name-array (or months calendar-month-name-array))
2147 (date (calendar-cursor-to-date t)))
2149 (format "%s%s" (or symbol "")
2150 (calendar-date-string
2152 (funcall absfunc (calendar-absolute-from-gregorian date))
2159 (defun diary-insert-monthly-entry (arg)
2160 "Insert a monthly diary entry for the day of the month indicated by point.
2161 Prefix argument ARG makes the entry nonmarking."
2163 (diary-insert-entry-1 'monthly arg))
2166 (define-obsolete-function-alias 'insert-monthly-diary-entry
2167 'diary-insert-monthly-entry "23.1")
2170 (defun diary-insert-yearly-entry (arg)
2171 "Insert an annual diary entry for the day of the year indicated by point.
2172 Prefix argument ARG makes the entry nonmarking."
2174 (diary-insert-entry-1 'yearly arg))
2177 (define-obsolete-function-alias 'insert-yearly-diary-entry
2178 'diary-insert-yearly-entry "23.1")
2181 (defun diary-insert-anniversary-entry (arg)
2182 "Insert an anniversary diary entry for the date given by point.
2183 Prefix argument ARG makes the entry nonmarking."
2185 (let ((calendar-date-display-form (diary-date-display-form)))
2187 (format "%s(diary-anniversary %s)"
2188 diary-sexp-entry-symbol
2189 (calendar-date-string (calendar-cursor-to-date t) nil t))
2193 (define-obsolete-function-alias 'insert-anniversary-diary-entry
2194 'diary-insert-anniversary-entry "23.1")
2197 (defun diary-insert-block-entry (arg)
2198 "Insert a block diary entry for the days between the point and marked date.
2199 Prefix argument ARG makes the entry nonmarking."
2201 (let ((calendar-date-display-form (diary-date-display-form))
2202 (cursor (calendar-cursor-to-date t))
2203 (mark (or (car calendar-mark-ring)
2204 (error "No mark set in this buffer")))
2206 (if (< (calendar-absolute-from-gregorian mark)
2207 (calendar-absolute-from-gregorian cursor))
2213 (format "%s(diary-block %s %s)"
2214 diary-sexp-entry-symbol
2215 (calendar-date-string start nil t)
2216 (calendar-date-string end nil t))
2220 (define-obsolete-function-alias 'insert-block-diary-entry
2221 'diary-insert-block-entry "23.1")
2224 (defun diary-insert-cyclic-entry (arg)
2225 "Insert a cyclic diary entry starting at the date given by point.
2226 Prefix argument ARG makes the entry nonmarking."
2228 (let ((calendar-date-display-form (diary-date-display-form)))
2230 (format "%s(diary-cyclic %d %s)"
2231 diary-sexp-entry-symbol
2232 (calendar-read "Repeat every how many days: "
2233 (lambda (x) (> x 0)))
2234 (calendar-date-string (calendar-cursor-to-date t) nil t))
2238 (define-obsolete-function-alias 'insert-cyclic-diary-entry
2239 'diary-insert-cyclic-entry "23.1")
2243 (defun diary-redraw-calendar ()
2244 "If `calendar-buffer' is live and diary entries are marked, redraw it."
2245 (and calendar-mark-diary-entries-flag
2248 ;; Return value suitable for `write-contents-functions'.
2251 (defvar diary-mode-map
2252 (let ((map (make-sparse-keymap)))
2253 (define-key map "\C-c\C-s" 'diary-show-all-entries)
2254 (define-key map "\C-c\C-q" 'quit-window)
2256 "Keymap for `diary-mode'.")
2258 (defun diary-font-lock-sexps (limit)
2259 "Recognize sexp diary entry up to LIMIT for font-locking."
2260 (if (re-search-forward
2261 (format "^%s?\\(%s\\)" (regexp-quote diary-nonmarking-symbol)
2262 (regexp-quote diary-sexp-entry-symbol))
2266 (narrow-to-region (point-min) limit)
2267 (let ((start (point)))
2269 (store-match-data (list start (point)))
2273 (defun diary-font-lock-date-forms (month-array &optional symbol abbrev-array)
2274 "Create font-lock patterns for `diary-date-forms' using MONTH-ARRAY.
2275 If given, optional SYMBOL must be a prefix to entries.
2276 If optional ABBREV-ARRAY is present, the abbreviations constructed
2277 from this array by the function `calendar-abbrev-construct' are
2278 matched (with or without a final `.'), in addition to the full month
2280 (let ((dayname (diary-name-pattern calendar-day-name-array
2281 calendar-day-abbrev-array t))
2282 (monthname (format "\\(%s\\|\\*\\)"
2283 (diary-name-pattern month-array abbrev-array)))
2284 (month "\\([0-9]+\\|\\*\\)")
2285 (day "\\([0-9]+\\|\\*\\)")
2286 (year "-?\\([0-9]+\\|\\*\\)"))
2289 (concat "^" (regexp-quote diary-nonmarking-symbol) "?"
2290 (if symbol (regexp-quote symbol) "") "\\("
2292 ;; If backup, omit first item (backup)
2293 ;; and last item (not part of date).
2294 (if (equal (car x) 'backup)
2295 (nreverse (cdr (reverse (cdr x))))
2298 ;; With backup, last item is not part of date.
2299 (if (equal (car x) 'backup)
2300 (concat "\\)" (eval (car (reverse x))))
2305 (defmacro diary-font-lock-keywords-1 (markfunc listfunc feature months symbol)
2306 "Subroutine of the function `diary-font-lock-keywords'.
2307 If MARKFUNC is a member of `diary-nongregorian-marking-hook', or
2308 LISTFUNC of `diary-nongregorian-listing-hook', then require FEATURE and
2309 return a font-lock pattern matching array of MONTHS and marking SYMBOL."
2310 `(when (or (memq ',markfunc diary-nongregorian-marking-hook)
2311 (memq ',listfunc diary-nongregorian-listing-hook))
2313 (diary-font-lock-date-forms ,months ,symbol)))
2315 (defconst diary-time-regexp
2316 ;; Accepted formats: 10:00 10.00 10h00 10h 10am 10:00am 10.00am
2317 ;; Use of "." as a separator annoyingly matches numbers, eg "123.45".
2318 ;; Hence often prefix this with "\\(^\\|\\s-\\)."
2319 (concat "[0-9]?[0-9]\\([AaPp][mM]\\|\\("
2320 "[Hh]\\([0-9][0-9]\\)?\\|[:.][0-9][0-9]"
2321 "\\)\\([AaPp][Mm]\\)?\\)")
2322 "Regular expression matching a time of day.")
2324 (defvar calendar-hebrew-month-name-array-leap-year)
2325 (defvar calendar-islamic-month-name-array)
2326 (defvar calendar-bahai-month-name-array)
2329 (defun diary-font-lock-keywords ()
2330 "Return a value for the variable `diary-font-lock-keywords'."
2332 (diary-font-lock-date-forms calendar-month-name-array
2333 nil calendar-month-abbrev-array)
2334 (diary-font-lock-keywords-1 diary-hebrew-mark-entries
2335 diary-hebrew-list-entries
2337 calendar-hebrew-month-name-array-leap-year
2338 diary-hebrew-entry-symbol)
2339 (diary-font-lock-keywords-1 diary-islamic-mark-entries
2340 diary-islamic-list-entries
2342 calendar-islamic-month-name-array
2343 diary-islamic-entry-symbol)
2344 (diary-font-lock-keywords-1 diary-bahai-mark-entries
2345 diary-bahai-list-entries
2347 calendar-bahai-month-name-array
2348 diary-bahai-entry-symbol)
2351 (format "^%s.*$" (regexp-quote diary-include-string))
2352 'font-lock-keyword-face)
2354 (format "^%s?\\(%s\\)" (regexp-quote diary-nonmarking-symbol)
2355 (regexp-quote diary-sexp-entry-symbol))
2356 '(1 font-lock-reference-face))
2358 (format "^%s" (regexp-quote diary-nonmarking-symbol))
2359 'font-lock-reference-face)
2361 (format "^%s?%s" (regexp-quote diary-nonmarking-symbol)
2362 (regexp-opt (mapcar 'regexp-quote
2363 (list diary-hebrew-entry-symbol
2364 diary-islamic-entry-symbol
2365 diary-bahai-entry-symbol))
2367 '(1 font-lock-reference-face))
2368 '(diary-font-lock-sexps . font-lock-keyword-face)
2369 ;; Don't need to worry about space around "-" because the first
2370 ;; match takes care of that. It does mean the "-" itself may or
2371 ;; may not be fontified though.
2372 ;; diary-date-forms often include a final character that is not
2373 ;; part of the date (eg a non-digit to mark the end of the year).
2374 ;; This can use up the only space char between a date and time (b#7891).
2375 ;; Hence we use OVERRIDE, which can only override whitespace.
2376 ;; FIXME it's probably better to tighten up the diary-time-regexp
2377 ;; and drop the whitespace requirement below.
2378 `(,(format "\\(^\\|\\s-\\)%s\\(-%s\\)?" diary-time-regexp
2380 . (0 'diary-time t)))))
2383 (defvar diary-font-lock-keywords (diary-font-lock-keywords)
2384 "Forms to highlight in `diary-mode'.")
2387 (define-derived-mode diary-mode fundamental-mode "Diary"
2388 "Major mode for editing the diary file."
2389 (set (make-local-variable 'font-lock-defaults)
2390 '(diary-font-lock-keywords t))
2391 (set (make-local-variable 'comment-start) diary-comment-start)
2392 (set (make-local-variable 'comment-end) diary-comment-end)
2393 (add-to-invisibility-spec '(diary . nil))
2394 (add-hook 'after-save-hook 'diary-redraw-calendar nil t)
2395 ;; In case the file was modified externally, refresh the calendar
2396 ;; after refreshing the diary buffer.
2397 (add-hook 'after-revert-hook 'diary-redraw-calendar nil t)
2398 (if diary-header-line-flag
2399 (setq header-line-format diary-header-line-format)))
2402 ;;; Fancy Diary Mode.
2404 (defun diary-fancy-date-pattern ()
2405 "Return a regexp matching the first line of a fancy diary date header.
2406 This depends on the calendar date style."
2408 (let ((dayname (diary-name-pattern calendar-day-name-array nil t))
2409 (monthname (diary-name-pattern calendar-month-name-array nil t))
2412 ;; FIXME? This used to be "-?[0-9]+" - what was the "-?" for?
2414 ;; This is ugly. c-d-d-form expects `day' etc to be "numbers in
2415 ;; string form"; eg the iso version calls string-to-number on some.
2416 ;; Therefore we cannot eg just let day = "[0-9]+". (Bug#8583).
2417 ;; Assumes no integers in c-day/month-name-array.
2418 (replace-regexp-in-string "[0-9]+" "[0-9]+"
2419 (mapconcat 'eval calendar-date-display-form "")
2421 ;; Optional ": holiday name" after the date.
2424 (defun diary-fancy-date-matcher (limit)
2425 "Search for a fancy diary data header, up to LIMIT."
2426 ;; Any number of " other holiday name" lines, followed by "==" line.
2427 (when (re-search-forward
2428 (format "%s\\(\n +.*\\)*\n=+$" (diary-fancy-date-pattern)) limit t)
2429 (put-text-property (match-beginning 0) (match-end 0) 'font-lock-multiline t)
2432 (define-obsolete-variable-alias 'fancy-diary-font-lock-keywords
2433 'diary-fancy-font-lock-keywords "23.1")
2435 (defvar diary-fancy-font-lock-keywords
2436 `((diary-fancy-date-matcher . diary-face)
2437 ("^.*\\([aA]nniversary\\|[bB]irthday\\).*$" . 'diary-anniversary)
2438 ("^.*Yahrzeit.*$" . font-lock-reference-face)
2439 ("^\\(Erev \\)?Rosh Hodesh.*" . font-lock-function-name-face)
2440 ("^Day.*omer.*$" . font-lock-builtin-face)
2441 ("^Parashat.*$" . font-lock-comment-face)
2442 (,(format "\\(^\\|\\s-\\)%s\\(-%s\\)?" diary-time-regexp
2443 diary-time-regexp) . 'diary-time))
2444 "Keywords to highlight in fancy diary display.")
2446 ;; If region looks like it might start or end in the middle of a
2447 ;; multiline pattern, extend the region to encompass the whole pattern.
2448 (defun diary-fancy-font-lock-fontify-region-function (beg end &optional verbose)
2449 "Function to use for `font-lock-fontify-region-function' in Fancy Diary.
2450 Needed to handle multiline keyword in `diary-fancy-font-lock-keywords'.
2451 Fontify the region between BEG and END, quietly unless VERBOSE is non-nil."
2454 (if (looking-at "=+$") (forward-line -1))
2455 (while (and (looking-at " +[^ ]")
2456 (zerop (forward-line -1))))
2457 ;; This check not essential.
2458 (if (looking-at (diary-fancy-date-pattern))
2459 (setq beg (line-beginning-position)))
2462 (while (and (looking-at " +[^ ]")
2463 (zerop (forward-line 1))))
2464 (if (looking-at "=+$")
2465 (setq end (line-beginning-position 2)))
2466 (font-lock-default-fontify-region beg end verbose))
2468 (defvar diary-fancy-overriding-map (make-sparse-keymap)
2469 "Keymap overriding minor-mode maps in `diary-fancy-display-mode'.")
2471 (define-derived-mode diary-fancy-display-mode special-mode
2473 "Major mode used while displaying diary entries using Fancy Display."
2474 (set (make-local-variable 'font-lock-defaults)
2475 '(diary-fancy-font-lock-keywords
2477 (font-lock-fontify-region-function
2478 . diary-fancy-font-lock-fontify-region-function)))
2479 (set (make-local-variable 'minor-mode-overriding-map-alist)
2480 (list (cons t diary-fancy-overriding-map)))
2483 (define-obsolete-function-alias 'fancy-diary-display-mode
2484 'diary-fancy-display-mode "23.1")
2486 ;; Following code from Dave Love <fx@gnu.org>.
2487 ;; Import Outlook-format appointments from mail messages in Gnus or
2488 ;; Rmail using command `diary-from-outlook'. This, or the specialized
2489 ;; functions `diary-from-outlook-gnus' and `diary-from-outlook-rmail',
2490 ;; could be run from hooks to notice appointments automatically (in
2491 ;; which case they will prompt about adding to the diary). The
2492 ;; message formats recognized are customizable through `diary-outlook-formats'.
2494 (defun diary-from-outlook-internal (subject body &optional test-only)
2495 "Snarf a diary entry from a message assumed to be from MS Outlook.
2496 SUBJECT and BODY are strings giving the message subject and body.
2497 Arg TEST-ONLY non-nil means return non-nil if and only if the
2498 message contains an appointment, don't make a diary entry."
2500 (let (format-string)
2501 (dolist (fmt diary-outlook-formats)
2502 (when (eq 0 (string-match (car fmt) body))
2504 (setq format-string (cdr fmt))
2506 (save-window-excursion
2508 (format (replace-match (if (functionp format-string)
2509 (funcall format-string body)
2511 t nil (match-string 0 body))
2513 (throw 'finished t))))
2516 (defvar gnus-article-mime-handles)
2517 (defvar gnus-article-buffer)
2519 (autoload 'gnus-fetch-field "gnus-util")
2520 (autoload 'gnus-narrow-to-body "gnus")
2521 (autoload 'mm-get-part "mm-decode")
2523 (defun diary-from-outlook-gnus (&optional noconfirm)
2524 "Maybe snarf diary entry from Outlook-generated message in Gnus.
2525 Unless the optional argument NOCONFIRM is non-nil (which is the case when
2526 this function is called interactively), then if an entry is found the
2527 user is asked to confirm its addition.
2528 Add this function to `gnus-article-prepare-hook' to notice appointments
2531 (with-current-buffer gnus-article-buffer
2532 (let ((subject (gnus-fetch-field "subject"))
2533 (body (if gnus-article-mime-handles
2534 ;; We're multipart. Don't get confused by part
2535 ;; buttons &c. Assume info is in first part.
2536 (mm-get-part (nth 1 gnus-article-mime-handles))
2538 (gnus-narrow-to-body)
2540 (when (diary-from-outlook-internal subject body t)
2541 (when (or noconfirm (y-or-n-p "Snarf diary entry? "))
2542 (diary-from-outlook-internal subject body)
2543 (message "Diary entry added"))))))
2545 (custom-add-option 'gnus-article-prepare-hook 'diary-from-outlook-gnus)
2547 (defvar rmail-buffer)
2549 (defun diary-from-outlook-rmail (&optional noconfirm)
2550 "Maybe snarf diary entry from Outlook-generated message in Rmail.
2551 Unless the optional argument NOCONFIRM is non-nil (which is the case when
2552 this function is called interactively), then if an entry is found the
2553 user is asked to confirm its addition."
2555 ;; FIXME maybe the body needs rmail-mm decoding, in which case
2556 ;; there is no single buffer with both body and subject, sigh.
2557 (with-current-buffer rmail-buffer
2558 (let ((subject (mail-fetch-field "subject"))
2559 (body (buffer-substring (save-excursion
2563 (when (diary-from-outlook-internal subject body t)
2564 (when (or noconfirm (y-or-n-p "Snarf diary entry? "))
2565 (diary-from-outlook-internal subject body)
2566 (message "Diary entry added"))))))
2568 (defun diary-from-outlook (&optional noconfirm)
2569 "Maybe snarf diary entry from current Outlook-generated message.
2570 Currently knows about Gnus and Rmail modes. Unless the optional
2571 argument NOCONFIRM is non-nil (which is the case when this
2572 function is called interactively), then if an entry is found the
2573 user is asked to confirm its addition."
2576 ((eq major-mode 'rmail-mode)
2577 #'diary-from-outlook-rmail)
2578 ((memq major-mode '(gnus-summary-mode gnus-article-mode))
2579 #'diary-from-outlook-gnus)
2580 (t (error "Don't know how to snarf in `%s'" major-mode)))))
2581 (funcall func noconfirm)))
2583 (provide 'diary-lib)
2585 ;;; diary-lib.el ends here