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