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