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