Update maintainer email address.
[emacs.git] / lisp / calendar / diary-lib.el
blobae42fa986610317340d86c38c9589284b5ec012e
1 ;;; diary-lib.el --- diary functions
3 ;; Copyright (C) 1989, 1990, 1992, 1993, 1994, 1995, 2003, 2004, 2005
4 ;; Free Software Foundation, Inc.
6 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
7 ;; Maintainer: Glenn Morris <rgm@gnu.org>
8 ;; Keywords: calendar
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; 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.
27 ;;; Commentary:
29 ;; This collection of functions implements the diary features as described
30 ;; in calendar.el.
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
38 ;;; Code:
40 (require 'calendar)
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))))
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)))))
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))
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 (view-diary-entries arg)))
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'.")
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'.")
93 (autoload 'diary-french-date "cal-french"
94 "French calendar equivalent of date diary entry.")
96 (autoload 'diary-mayan-date "cal-mayan"
97 "Mayan calendar equivalent of date diary entry.")
99 (autoload 'diary-iso-date "cal-iso"
100 "ISO calendar equivalent of date diary entry.")
102 (autoload 'diary-julian-date "cal-julian"
103 "Julian calendar equivalent of date diary entry.")
105 (autoload 'diary-astro-day-number "cal-julian"
106 "Astronomical (Julian) day number diary entry.")
108 (autoload 'diary-chinese-date "cal-china"
109 "Chinese calendar equivalent of date diary entry.")
111 (autoload 'diary-islamic-date "cal-islam"
112 "Islamic calendar equivalent of date diary entry.")
114 (autoload 'list-islamic-diary-entries "cal-islam"
115 "Add any Islamic date entries from the diary file to `diary-entries-list'.")
117 (autoload 'mark-islamic-diary-entries "cal-islam"
118 "Mark days in the calendar window that have Islamic date diary entries.")
120 (autoload 'mark-islamic-calendar-date-pattern "cal-islam"
121 "Mark dates in calendar window that conform to Islamic date MONTH/DAY/YEAR.")
123 (autoload 'diary-bahai-date "cal-bahai"
124 "Baha'i calendar equivalent of date diary entry."
127 (autoload 'list-bahai-diary-entries "cal-bahai"
128 "Add any Baha'i date entries from the diary file to `diary-entries-list'."
131 (autoload 'mark-bahai-diary-entries "cal-bahai"
132 "Mark days in the calendar window that have Baha'i date diary entries."
135 (autoload 'mark-bahai-calendar-date-pattern "cal-bahai"
136 "Mark dates in calendar window that conform to Baha'i date MONTH/DAY/YEAR."
139 (autoload 'diary-hebrew-date "cal-hebrew"
140 "Hebrew calendar equivalent of date diary entry.")
142 (autoload 'diary-omer "cal-hebrew"
143 "Omer count diary entry.")
145 (autoload 'diary-yahrzeit "cal-hebrew"
146 "Yahrzeit diary entry--entry applies if date is yahrzeit or the day before.")
148 (autoload 'diary-parasha "cal-hebrew"
149 "Parasha diary entry--entry applies if date is a Saturday.")
151 (autoload 'diary-rosh-hodesh "cal-hebrew"
152 "Rosh Hodesh diary entry.")
154 (autoload 'list-hebrew-diary-entries "cal-hebrew"
155 "Add any Hebrew date entries from the diary file to `diary-entries-list'.")
157 (autoload 'mark-hebrew-diary-entries "cal-hebrew"
158 "Mark days in the calendar window that have Hebrew date diary entries.")
160 (autoload 'mark-hebrew-calendar-date-pattern "cal-hebrew"
161 "Mark dates in calendar window that conform to Hebrew date MONTH/DAY/YEAR.")
163 (autoload 'diary-coptic-date "cal-coptic"
164 "Coptic calendar equivalent of date diary entry.")
166 (autoload 'diary-ethiopic-date "cal-coptic"
167 "Ethiopic calendar equivalent of date diary entry.")
169 (autoload 'diary-persian-date "cal-persia"
170 "Persian calendar equivalent of date diary entry.")
172 (autoload 'diary-phases-of-moon "lunar" "Moon phases diary entry.")
174 (autoload 'diary-sunrise-sunset "solar"
175 "Local time of sunrise and sunset as a diary entry.")
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.")
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)
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.")
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)
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))
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))))
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")
280 (defvar diary-selective-display nil)
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")
295 (defvar diary-saved-point) ; internal
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.
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.
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)
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.
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.
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:
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.
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.
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.
358 `diary-hook' is run last. This can be used for an appointment
359 notification function.
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 'fundamental-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))
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 (re-search-forward "\^M\\|\n" nil t 2)
443 (while (looking-at " \\|\^I")
444 (re-search-forward "\^M\\|\n" nil t))
445 (backward-char 1)
446 (unless list-only
447 (remove-overlays date-start (point)
448 'invisible 'diary))
449 (setq entry (buffer-substring entry-start (point))
450 temp (diary-pull-attrs entry file-glob-attrs)
451 entry (nth 0 temp))
452 (add-to-diary-list
453 date
454 entry
455 (buffer-substring
456 (1+ date-start) (1- entry-start))
457 (copy-marker entry-start) (nth 1 temp)))))))
458 (or entry-found
459 (not diary-list-include-blanks)
460 (setq diary-entries-list
461 (append diary-entries-list
462 (list (list date "" "" "" "")))))
463 (setq date
464 (calendar-gregorian-from-absolute
465 (1+ (calendar-absolute-from-gregorian date))))
466 (setq entry-found nil)))))
467 (goto-char (point-min))
468 (run-hooks 'nongregorian-diary-listing-hook
469 'list-diary-entries-hook)
470 (unless list-only
471 (if diary-display-hook
472 (run-hooks 'diary-display-hook)
473 (simple-diary-display)))
474 (run-hooks 'diary-hook)
475 diary-entries-list))))))
477 (defun diary-unhide-everything ()
478 (kill-local-variable 'diary-selective-display)
479 (remove-overlays (point-min) (point-max) 'invisible 'diary)
480 (kill-local-variable 'mode-line-format))
482 (defun include-other-diary-files ()
483 "Include the diary entries from other diary files with those of diary-file.
484 This function is suitable for use in `list-diary-entries-hook';
485 it enables you to use shared diary files together with your own.
486 The files included are specified in the diaryfile by lines of this form:
487 #include \"filename\"
488 This is recursive; that is, #include directives in diary files thus included
489 are obeyed. You can change the `#include' to some other string by
490 changing the variable `diary-include-string'."
491 (goto-char (point-min))
492 (while (re-search-forward
493 (concat
494 "\\(?:\\`\\|\^M\\|\n\\)"
495 (regexp-quote diary-include-string)
496 " \"\\([^\"]*\\)\"")
497 nil t)
498 (let* ((diary-file (substitute-in-file-name
499 (match-string-no-properties 1)))
500 (diary-list-include-blanks nil)
501 (list-diary-entries-hook 'include-other-diary-files)
502 (diary-display-hook 'ignore)
503 (diary-hook nil))
504 (if (file-exists-p diary-file)
505 (if (file-readable-p diary-file)
506 (unwind-protect
507 (setq diary-entries-list
508 (append diary-entries-list
509 (list-diary-entries original-date number)))
510 (with-current-buffer (find-buffer-visiting diary-file)
511 (diary-unhide-everything)))
512 (beep)
513 (message "Can't read included diary file %s" diary-file)
514 (sleep-for 2))
515 (beep)
516 (message "Can't find included diary file %s" diary-file)
517 (sleep-for 2))))
518 (goto-char (point-min)))
520 (defun simple-diary-display ()
521 "Display the diary buffer if there are any relevant entries or holidays."
522 (let* ((holiday-list (if holidays-in-diary-buffer
523 (check-calendar-holidays original-date)))
524 (hol-string (format "%s%s%s"
525 date-string
526 (if holiday-list ": " "")
527 (mapconcat 'identity holiday-list "; ")))
528 (msg (format "No diary entries for %s" hol-string))
529 ;; If selected window is dedicated (to the calendar),
530 ;; need a new one to display the diary.
531 (pop-up-frames (window-dedicated-p (selected-window))))
532 (calendar-set-mode-line (format "Diary for %s" hol-string))
533 (if (or (not diary-entries-list)
534 (and (not (cdr diary-entries-list))
535 (string-equal (car (cdr (car diary-entries-list))) "")))
536 (if (< (length msg) (frame-width))
537 (message "%s" msg)
538 (set-buffer (get-buffer-create holiday-buffer))
539 (setq buffer-read-only nil)
540 (calendar-set-mode-line date-string)
541 (erase-buffer)
542 (insert (mapconcat 'identity holiday-list "\n"))
543 (goto-char (point-min))
544 (set-buffer-modified-p nil)
545 (setq buffer-read-only t)
546 (display-buffer holiday-buffer)
547 (message "No diary entries for %s" date-string))
548 (with-current-buffer
549 (find-buffer-visiting (substitute-in-file-name diary-file))
550 (let ((window (display-buffer (current-buffer))))
551 ;; d-s-p is passed from list-diary-entries.
552 (set-window-point window diary-saved-point)
553 (set-window-start window (point-min))))
554 (message "Preparing diary...done"))))
556 (defface diary-button '((((type pc) (class color))
557 (:foreground "lightblue")))
558 "Default face used for buttons."
559 :version "22.1"
560 :group 'diary)
561 ;; backward-compatibility alias
562 (put 'diary-button-face 'face-alias 'diary-button)
564 (define-button-type 'diary-entry
565 'action #'diary-goto-entry
566 'face 'diary-button)
568 (defun diary-goto-entry (button)
569 (let ((marker (button-get button 'marker)))
570 (when marker
571 (pop-to-buffer (marker-buffer marker))
572 (goto-char (marker-position marker)))))
574 (defun fancy-diary-display ()
575 "Prepare a diary buffer with relevant entries in a fancy, noneditable form.
576 This function is provided for optional use as the `diary-display-hook'."
577 (with-current-buffer ;; Turn off selective-display in the diary file's buffer.
578 (find-buffer-visiting (substitute-in-file-name diary-file))
579 (diary-unhide-everything))
580 (if (or (not diary-entries-list)
581 (and (not (cdr diary-entries-list))
582 (string-equal (car (cdr (car diary-entries-list))) "")))
583 (let* ((holiday-list (if holidays-in-diary-buffer
584 (check-calendar-holidays original-date)))
585 (msg (format "No diary entries for %s %s"
586 (concat date-string (if holiday-list ":" ""))
587 (mapconcat 'identity holiday-list "; "))))
588 (if (<= (length msg) (frame-width))
589 (message "%s" msg)
590 (set-buffer (get-buffer-create holiday-buffer))
591 (setq buffer-read-only nil)
592 (erase-buffer)
593 (insert (mapconcat 'identity holiday-list "\n"))
594 (goto-char (point-min))
595 (set-buffer-modified-p nil)
596 (setq buffer-read-only t)
597 (display-buffer holiday-buffer)
598 (message "No diary entries for %s" date-string)))
599 (with-current-buffer;; Prepare the fancy diary buffer.
600 (make-fancy-diary-buffer)
601 (setq buffer-read-only nil)
602 (let ((entry-list diary-entries-list)
603 (holiday-list)
604 (holiday-list-last-month 1)
605 (holiday-list-last-year 1)
606 (date (list 0 0 0)))
607 (while entry-list
608 (if (not (calendar-date-equal date (car (car entry-list))))
609 (progn
610 (setq date (car (car entry-list)))
611 (and holidays-in-diary-buffer
612 (calendar-date-compare
613 (list (list holiday-list-last-month
614 (calendar-last-day-of-month
615 holiday-list-last-month
616 holiday-list-last-year)
617 holiday-list-last-year))
618 (list date))
619 ;; We need to get the holidays for the next 3 months.
620 (setq holiday-list-last-month
621 (extract-calendar-month date))
622 (setq holiday-list-last-year
623 (extract-calendar-year date))
624 (progn
625 (increment-calendar-month
626 holiday-list-last-month holiday-list-last-year 1)
628 (setq holiday-list
629 (let ((displayed-month holiday-list-last-month)
630 (displayed-year holiday-list-last-year))
631 (calendar-holiday-list)))
632 (increment-calendar-month
633 holiday-list-last-month holiday-list-last-year 1))
634 (let* ((date-string (calendar-date-string date))
635 (date-holiday-list
636 (let ((h holiday-list)
637 (d))
638 ;; Make a list of all holidays for date.
639 (while h
640 (if (calendar-date-equal date (car (car h)))
641 (setq d (append d (cdr (car h)))))
642 (setq h (cdr h)))
643 d)))
644 (insert (if (= (point) (point-min)) "" ?\n) date-string)
645 (if date-holiday-list (insert ": "))
646 (let* ((l (current-column))
647 (longest 0))
648 (insert (mapconcat (lambda (x)
649 (if (< longest (length x))
650 (setq longest (length x)))
652 date-holiday-list
653 (concat "\n" (make-string l ? ))))
654 (insert ?\n (make-string (+ l longest) ?=) ?\n)))))
656 (setq entry (car (cdr (car entry-list))))
657 (if (< 0 (length entry))
658 (progn
659 (if (nth 3 (car entry-list))
660 (insert-button (concat entry "\n")
661 'marker (nth 3 (car entry-list))
662 :type 'diary-entry)
663 (insert entry ?\n))
664 (save-excursion
665 (let* ((marks (nth 4 (car entry-list)))
666 (temp-face (make-symbol
667 (apply
668 'concat "temp-face-"
669 (mapcar (lambda (sym)
670 (if (stringp sym)
672 (symbol-name sym)))
673 marks))))
674 (faceinfo marks))
675 (make-face temp-face)
676 ;; Remove :face info from the marks,
677 ;; copy the face info into temp-face
678 (while (setq faceinfo (memq :face faceinfo))
679 (copy-face (read (nth 1 faceinfo)) temp-face)
680 (setcar faceinfo nil)
681 (setcar (cdr faceinfo) nil))
682 (setq marks (delq nil marks))
683 ;; Apply the font aspects.
684 (apply 'set-face-attribute temp-face nil marks)
685 (search-backward entry)
686 (overlay-put
687 (make-overlay (match-beginning 0) (match-end 0))
688 'face temp-face)))))
689 (setq entry-list (cdr entry-list))))
690 (set-buffer-modified-p nil)
691 (goto-char (point-min))
692 (setq buffer-read-only t)
693 (display-buffer fancy-diary-buffer)
694 (fancy-diary-display-mode)
695 (calendar-set-mode-line date-string)
696 (message "Preparing diary...done"))))
698 (defun make-fancy-diary-buffer ()
699 "Create and return the initial fancy diary buffer."
700 (with-current-buffer (get-buffer-create fancy-diary-buffer)
701 (setq buffer-read-only nil)
702 (calendar-set-mode-line "Diary Entries")
703 (erase-buffer)
704 (set-buffer-modified-p nil)
705 (setq buffer-read-only t)
706 (get-buffer fancy-diary-buffer)))
708 (defun print-diary-entries ()
709 "Print a hard copy of the diary display.
711 If the simple diary display is being used, prepare a temp buffer with the
712 visible lines of the diary buffer, add a heading line composed from the mode
713 line, print the temp buffer, and destroy it.
715 If the fancy diary display is being used, just print the buffer.
717 The hooks given by the variable `print-diary-entries-hook' are called to do
718 the actual printing."
719 (interactive)
720 (if (bufferp (get-buffer fancy-diary-buffer))
721 (with-current-buffer (get-buffer fancy-diary-buffer)
722 (run-hooks 'print-diary-entries-hook))
723 (let ((diary-buffer
724 (find-buffer-visiting (substitute-in-file-name diary-file))))
725 (if diary-buffer
726 (let ((temp-buffer (get-buffer-create " *Printable Diary Entries*"))
727 (heading))
728 (with-current-buffer diary-buffer
729 (setq heading
730 (if (not (stringp mode-line-format))
731 "All Diary Entries"
732 (string-match "^-*\\([^-].*[^-]\\)-*$" mode-line-format)
733 (match-string 1 mode-line-format)))
734 (let ((start (point-min))
735 end)
736 (while
737 (progn
738 (setq end (next-single-char-property-change
739 start 'invisible))
740 (if (get-char-property start 'invisible)
742 (with-current-buffer temp-buffer
743 (insert-buffer-substring diary-buffer
744 start (or end (point-max)))))
745 (setq start end)
746 (and end (< end (point-max))))))
747 (set-buffer temp-buffer)
748 (goto-char (point-min))
749 (insert heading "\n"
750 (make-string (length heading) ?=) "\n")
751 (run-hooks 'print-diary-entries-hook)
752 (kill-buffer temp-buffer)))
753 (error "You don't have a diary buffer!")))))
755 (define-obsolete-function-alias 'show-all-diary-entries 'diary-show-all-entries)
756 (defun diary-show-all-entries ()
757 "Show all of the diary entries in the diary file.
758 This function gets rid of the selective display of the diary file so that
759 all entries, not just some, are visible. If there is no diary buffer, one
760 is created."
761 (interactive)
762 (let ((d-file (diary-check-diary-file))
763 (pop-up-frames (window-dedicated-p (selected-window))))
764 (with-current-buffer (or (find-buffer-visiting d-file)
765 (find-file-noselect d-file t))
766 (when (eq major-mode 'fundamental-mode) (diary-mode))
767 (diary-unhide-everything)
768 (display-buffer (current-buffer)))))
770 (defcustom diary-mail-addr
771 (if (boundp 'user-mail-address) user-mail-address "")
772 "Email address that `diary-mail-entries' will send email to."
773 :group 'diary
774 :type 'string
775 :version "20.3")
777 (defcustom diary-mail-days 7
778 "Default number of days for `diary-mail-entries' to check."
779 :group 'diary
780 :type 'integer
781 :version "20.3")
783 ;;;###autoload
784 (defun diary-mail-entries (&optional ndays)
785 "Send a mail message showing diary entries for next NDAYS days.
786 If no prefix argument is given, NDAYS is set to `diary-mail-days'.
787 Mail is sent to the address specified by `diary-mail-addr'.
789 You can call `diary-mail-entries' every night using an at/cron job.
790 For example, this script will run the program at 2am daily. Since
791 `emacs -batch' does not load your `.emacs' file, you must ensure that
792 all relevant variables are set, as done here.
794 #!/bin/sh
795 # diary-rem.sh -- repeatedly run the Emacs diary-reminder
796 emacs -batch \\
797 -eval \"(setq diary-mail-days 3 \\
798 diary-file \\\"/path/to/diary.file\\\" \\
799 european-calendar-style t \\
800 diary-mail-addr \\\"user@host.name\\\" )\" \\
801 -l diary-lib -f diary-mail-entries
802 at -f diary-rem.sh 0200 tomorrow
804 You may have to tweak the syntax of the `at' command to suit your
805 system. Alternatively, you can specify a cron entry:
806 0 1 * * * diary-rem.sh
807 to run it every morning at 1am."
808 (interactive "P")
809 (if (string-equal diary-mail-addr "")
810 (error "You must set `diary-mail-addr' to use this command")
811 (let ((diary-display-hook 'fancy-diary-display))
812 (list-diary-entries (calendar-current-date) (or ndays diary-mail-days)))
813 (compose-mail diary-mail-addr
814 (concat "Diary entries generated "
815 (calendar-date-string (calendar-current-date))))
816 (insert
817 (if (get-buffer fancy-diary-buffer)
818 (with-current-buffer fancy-diary-buffer (buffer-string))
819 "No entries found"))
820 (call-interactively (get mail-user-agent 'sendfunc))))
822 (defun diary-name-pattern (string-array &optional abbrev-array paren)
823 "Return a regexp matching the strings in the array STRING-ARRAY.
824 If the optional argument ABBREV-ARRAY is present, then the function
825 `calendar-abbrev-construct' is used to construct abbreviations from the
826 two supplied arrays. The returned regexp will then also match these
827 abbreviations, with or without final `.' characters. If the optional
828 argument PAREN is non-nil, the regexp is surrounded by parentheses."
829 (regexp-opt (append string-array
830 (if abbrev-array
831 (calendar-abbrev-construct abbrev-array
832 string-array))
833 (if abbrev-array
834 (calendar-abbrev-construct abbrev-array
835 string-array
836 'period))
837 nil)
838 paren))
840 (defvar marking-diary-entries nil
841 "True during the marking of diary entries, nil otherwise.")
843 (defvar marking-diary-entry nil
844 "True during the marking of diary entries, if current entry is marking.")
846 (defun mark-diary-entries (&optional redraw)
847 "Mark days in the calendar window that have diary entries.
848 Each entry in the diary file visible in the calendar window is
849 marked. After the entries are marked, the hooks
850 `nongregorian-diary-marking-hook' and `mark-diary-entries-hook'
851 are run. If the optional argument REDRAW is non-nil (which is
852 the case interactively, for example) then any existing diary
853 marks are first removed. This is intended to deal with deleted
854 diary entries."
855 (interactive "p")
856 ;; To remove any deleted diary entries. Do not redraw when:
857 ;; i) processing #include diary files (else only get the marks from
858 ;; the last #include file processed).
859 ;; ii) called via calendar-redraw (since calendar has already been
860 ;; erased).
861 ;; Use of REDRAW handles both of these cases.
862 (when (and redraw mark-diary-entries-in-calendar)
863 (setq mark-diary-entries-in-calendar nil)
864 (redraw-calendar))
865 (let ((marking-diary-entries t)
866 file-glob-attrs marks)
867 (with-current-buffer (find-file-noselect (diary-check-diary-file) t)
868 (save-excursion
869 (when (eq major-mode 'fundamental-mode) (diary-mode))
870 (setq mark-diary-entries-in-calendar t)
871 (message "Marking diary entries...")
872 (setq file-glob-attrs (nth 1 (diary-pull-attrs nil '())))
873 (with-syntax-table diary-syntax-table
874 (dolist (date-form diary-date-forms)
875 (if (eq (car date-form) 'backup)
876 (setq date-form (cdr date-form))) ;; ignore 'backup directive
877 (let* ((dayname
878 (diary-name-pattern calendar-day-name-array
879 calendar-day-abbrev-array))
880 (monthname
881 (format "%s\\|\\*"
882 (diary-name-pattern calendar-month-name-array
883 calendar-month-abbrev-array)))
884 (month "[0-9]+\\|\\*")
885 (day "[0-9]+\\|\\*")
886 (year "[0-9]+\\|\\*")
887 (l (length date-form))
888 (d-name-pos (- l (length (memq 'dayname date-form))))
889 (d-name-pos (if (/= l d-name-pos) (+ 2 d-name-pos)))
890 (m-name-pos (- l (length (memq 'monthname date-form))))
891 (m-name-pos (if (/= l m-name-pos) (+ 2 m-name-pos)))
892 (d-pos (- l (length (memq 'day date-form))))
893 (d-pos (if (/= l d-pos) (+ 2 d-pos)))
894 (m-pos (- l (length (memq 'month date-form))))
895 (m-pos (if (/= l m-pos) (+ 2 m-pos)))
896 (y-pos (- l (length (memq 'year date-form))))
897 (y-pos (if (/= l y-pos) (+ 2 y-pos)))
898 (regexp
899 (concat
900 "\\(\\`\\|\^M\\|\n\\)\\("
901 (mapconcat 'eval date-form "\\)\\(")
902 "\\)"))
903 (case-fold-search t))
904 (goto-char (point-min))
905 (while (re-search-forward regexp nil t)
906 (let* ((dd-name
907 (if d-name-pos
908 (match-string-no-properties d-name-pos)))
909 (mm-name
910 (if m-name-pos
911 (match-string-no-properties m-name-pos)))
912 (mm (string-to-number
913 (if m-pos
914 (match-string-no-properties m-pos)
915 "")))
916 (dd (string-to-number
917 (if d-pos
918 (match-string-no-properties d-pos)
919 "")))
920 (y-str (if y-pos
921 (match-string-no-properties y-pos)))
922 (yy (if (not y-str)
924 (if (and (= (length y-str) 2)
925 abbreviated-calendar-year)
926 (let* ((current-y
927 (extract-calendar-year
928 (calendar-current-date)))
929 (y (+ (string-to-number y-str)
930 (* 100
931 (/ current-y 100)))))
932 (if (> (- y current-y) 50)
933 (- y 100)
934 (if (> (- current-y y) 50)
935 (+ y 100)
936 y)))
937 (string-to-number y-str)))))
938 (let ((tmp (diary-pull-attrs (buffer-substring-no-properties
939 (point) (line-end-position))
940 file-glob-attrs)))
941 (setq entry (nth 0 tmp)
942 marks (nth 1 tmp)))
943 (if dd-name
944 (mark-calendar-days-named
945 (cdr (assoc-string
946 dd-name
947 (calendar-make-alist
948 calendar-day-name-array
949 0 nil calendar-day-abbrev-array) t)) marks)
950 (if mm-name
951 (setq mm
952 (if (string-equal mm-name "*") 0
953 (cdr (assoc-string
954 mm-name
955 (calendar-make-alist
956 calendar-month-name-array
957 1 nil calendar-month-abbrev-array) t)))))
958 (mark-calendar-date-pattern mm dd yy marks))))))
959 (mark-sexp-diary-entries)
960 (run-hooks 'nongregorian-diary-marking-hook
961 'mark-diary-entries-hook))
962 (message "Marking diary entries...done")))))
964 (defun mark-sexp-diary-entries ()
965 "Mark days in the calendar window that have sexp diary entries.
966 Each entry in the diary file (or included files) visible in the calendar window
967 is marked. See the documentation for the function `list-sexp-diary-entries'."
968 (let* ((sexp-mark (regexp-quote sexp-diary-entry-symbol))
969 (s-entry (concat "\\(\\`\\|\^M\\|\n\\)\\("
970 sexp-mark "(\\)\\|\\("
971 (regexp-quote diary-nonmarking-symbol)
972 sexp-mark "(diary-remind\\)"))
973 (file-glob-attrs (nth 1 (diary-pull-attrs nil '())))
974 m y first-date last-date mark file-glob-attrs)
975 (with-current-buffer calendar-buffer
976 (setq m displayed-month)
977 (setq y displayed-year))
978 (increment-calendar-month m y -1)
979 (setq first-date
980 (calendar-absolute-from-gregorian (list m 1 y)))
981 (increment-calendar-month m y 2)
982 (setq last-date
983 (calendar-absolute-from-gregorian
984 (list m (calendar-last-day-of-month m y) y)))
985 (goto-char (point-min))
986 (while (re-search-forward s-entry nil t)
987 (setq marking-diary-entry (char-equal (preceding-char) ?\())
988 (re-search-backward "(")
989 (let ((sexp-start (point))
990 sexp entry entry-start line-start marks)
991 (forward-sexp)
992 (setq sexp (buffer-substring-no-properties sexp-start (point)))
993 (save-excursion
994 (re-search-backward "\^M\\|\n\\|\\`")
995 (setq line-start (point)))
996 (forward-char 1)
997 (if (and (or (char-equal (preceding-char) ?\^M)
998 (char-equal (preceding-char) ?\n))
999 (not (looking-at " \\|\^I")))
1000 (progn;; Diary entry consists only of the sexp
1001 (backward-char 1)
1002 (setq entry ""))
1003 (setq entry-start (point))
1004 ;; Find end of entry
1005 (re-search-forward "\^M\\|\n" nil t)
1006 (while (looking-at " \\|\^I")
1007 (or (re-search-forward "\^M\\|\n" nil t)
1008 (re-search-forward "$" nil t)))
1009 (if (or (char-equal (preceding-char) ?\^M)
1010 (char-equal (preceding-char) ?\n))
1011 (backward-char 1))
1012 (setq entry (buffer-substring-no-properties entry-start (point)))
1013 (while (string-match "[\^M]" entry)
1014 (aset entry (match-beginning 0) ?\n )))
1015 (calendar-for-loop date from first-date to last-date do
1016 (if (setq mark (diary-sexp-entry sexp entry
1017 (calendar-gregorian-from-absolute date)))
1018 (progn
1019 (setq marks (diary-pull-attrs entry file-glob-attrs)
1020 marks (nth 1 (diary-pull-attrs entry file-glob-attrs)))
1021 (mark-visible-calendar-date
1022 (calendar-gregorian-from-absolute date)
1023 (if (< 0 (length marks))
1024 marks
1025 (if (consp mark)
1026 (car mark)))))))))))
1028 (defun mark-included-diary-files ()
1029 "Mark the diary entries from other diary files with those of the diary file.
1030 This function is suitable for use as the `mark-diary-entries-hook'; it enables
1031 you to use shared diary files together with your own. The files included are
1032 specified in the diary-file by lines of this form:
1033 #include \"filename\"
1034 This is recursive; that is, #include directives in diary files thus included
1035 are obeyed. You can change the `#include' to some other string by
1036 changing the variable `diary-include-string'."
1037 (goto-char (point-min))
1038 (while (re-search-forward
1039 (concat
1040 "\\(?:\\`\\|\^M\\|\n\\)"
1041 (regexp-quote diary-include-string)
1042 " \"\\([^\"]*\\)\"")
1043 nil t)
1044 (let* ((diary-file (substitute-in-file-name
1045 (match-string-no-properties 1)))
1046 (mark-diary-entries-hook 'mark-included-diary-files)
1047 (dbuff (find-buffer-visiting diary-file)))
1048 (if (file-exists-p diary-file)
1049 (if (file-readable-p diary-file)
1050 (progn
1051 (mark-diary-entries)
1052 (unless dbuff
1053 (kill-buffer (find-buffer-visiting diary-file))))
1054 (beep)
1055 (message "Can't read included diary file %s" diary-file)
1056 (sleep-for 2))
1057 (beep)
1058 (message "Can't find included diary file %s" diary-file)
1059 (sleep-for 2))))
1060 (goto-char (point-min)))
1062 (defun mark-calendar-days-named (dayname &optional color)
1063 "Mark all dates in the calendar window that are day DAYNAME of the week.
1064 0 means all Sundays, 1 means all Mondays, and so on."
1065 (with-current-buffer calendar-buffer
1066 (let ((prev-month displayed-month)
1067 (prev-year displayed-year)
1068 (succ-month displayed-month)
1069 (succ-year displayed-year)
1070 (last-day)
1071 (day))
1072 (increment-calendar-month succ-month succ-year 1)
1073 (increment-calendar-month prev-month prev-year -1)
1074 (setq day (calendar-absolute-from-gregorian
1075 (calendar-nth-named-day 1 dayname prev-month prev-year)))
1076 (setq last-day (calendar-absolute-from-gregorian
1077 (calendar-nth-named-day -1 dayname succ-month succ-year)))
1078 (while (<= day last-day)
1079 (mark-visible-calendar-date (calendar-gregorian-from-absolute day) color)
1080 (setq day (+ day 7))))))
1082 (defun mark-calendar-date-pattern (month day year &optional color)
1083 "Mark all dates in the calendar window that conform to MONTH/DAY/YEAR.
1084 A value of 0 in any position is a wildcard."
1085 (with-current-buffer calendar-buffer
1086 (let ((m displayed-month)
1087 (y displayed-year))
1088 (increment-calendar-month m y -1)
1089 (calendar-for-loop i from 0 to 2 do
1090 (mark-calendar-month m y month day year color)
1091 (increment-calendar-month m y 1)))))
1093 (defun mark-calendar-month (month year p-month p-day p-year &optional color)
1094 "Mark dates in the MONTH/YEAR that conform to pattern P-MONTH/P_DAY/P-YEAR.
1095 A value of 0 in any position of the pattern is a wildcard."
1096 (if (or (and (= month p-month)
1097 (or (= p-year 0) (= year p-year)))
1098 (and (= p-month 0)
1099 (or (= p-year 0) (= year p-year))))
1100 (if (= p-day 0)
1101 (calendar-for-loop
1102 i from 1 to (calendar-last-day-of-month month year) do
1103 (mark-visible-calendar-date (list month i year) color))
1104 (mark-visible-calendar-date (list month p-day year) color))))
1106 (defun sort-diary-entries ()
1107 "Sort the list of diary entries by time of day."
1108 (setq diary-entries-list (sort diary-entries-list 'diary-entry-compare)))
1110 (defun diary-entry-compare (e1 e2)
1111 "Returns t if E1 is earlier than E2."
1112 (or (calendar-date-compare e1 e2)
1113 (and (calendar-date-equal (car e1) (car e2))
1114 (let* ((ts1 (cadr e1)) (t1 (diary-entry-time ts1))
1115 (ts2 (cadr e2)) (t2 (diary-entry-time ts2)))
1116 (or (< t1 t2)
1117 (and (= t1 t2)
1118 (string-lessp ts1 ts2)))))))
1120 (defcustom diary-unknown-time
1121 -9999
1122 "Value returned by diary-entry-time when no time is found.
1123 The default value -9999 causes entries with no recognizable time to be placed
1124 before those with times; 9999 would place entries with no recognizable time
1125 after those with times."
1126 :type 'integer
1127 :group 'diary
1128 :version "20.3")
1130 (defun diary-entry-time (s)
1131 "Return time at the beginning of the string S as a military-style integer.
1132 For example, returns 1325 for 1:25pm.
1134 Returns `diary-unknown-time' (default value -9999) if no time is recognized.
1135 The recognized forms are XXXX, X:XX, or XX:XX (military time), and XXam,
1136 XXAM, XXpm, XXPM, XX:XXam, XX:XXAM XX:XXpm, or XX:XXPM. A period (.) can
1137 be used instead of a colon (:) to separate the hour and minute parts."
1138 (let ((case-fold-search nil))
1139 (cond ((string-match ; Military time
1140 "\\`[ \t\n\\^M]*\\([0-9]?[0-9]\\)[:.]?\\([0-9][0-9]\\)\\(\\>\\|[^ap]\\)"
1142 (+ (* 100 (string-to-number (match-string 1 s)))
1143 (string-to-number (match-string 2 s))))
1144 ((string-match ; Hour only XXam or XXpm
1145 "\\`[ \t\n\\^M]*\\([0-9]?[0-9]\\)\\([ap]\\)m\\>" s)
1146 (+ (* 100 (% (string-to-number (match-string 1 s)) 12))
1147 (if (equal ?a (downcase (aref s (match-beginning 2))))
1148 0 1200)))
1149 ((string-match ; Hour and minute XX:XXam or XX:XXpm
1150 "\\`[ \t\n\\^M]*\\([0-9]?[0-9]\\)[:.]\\([0-9][0-9]\\)\\([ap]\\)m\\>" s)
1151 (+ (* 100 (% (string-to-number (match-string 1 s)) 12))
1152 (string-to-number (match-string 2 s))
1153 (if (equal ?a (downcase (aref s (match-beginning 3))))
1154 0 1200)))
1155 (t diary-unknown-time)))) ; Unrecognizable
1157 ;; Unrecognizable
1159 (defun list-sexp-diary-entries (date)
1160 "Add sexp entries for DATE from the diary file to `diary-entries-list'.
1161 Also, Make them visible in the diary file. Returns t if any entries were
1162 found.
1164 Sexp diary entries must be prefaced by a `sexp-diary-entry-symbol' (normally
1165 `%%'). The form of a sexp diary entry is
1167 %%(SEXP) ENTRY
1169 Both ENTRY and DATE are globally available when the SEXP is evaluated. If the
1170 SEXP yields the value nil, the diary entry does not apply. If it yields a
1171 non-nil value, ENTRY will be taken to apply to DATE; if the non-nil value is a
1172 string, that string will be the diary entry in the fancy diary display.
1174 For example, the following diary entry will apply to the 21st of the month
1175 if it is a weekday and the Friday before if the 21st is on a weekend:
1177 &%%(let ((dayname (calendar-day-of-week date))
1178 (day (extract-calendar-day date)))
1180 (and (= day 21) (memq dayname '(1 2 3 4 5)))
1181 (and (memq day '(19 20)) (= dayname 5)))
1182 ) UIUC pay checks deposited
1184 A number of built-in functions are available for this type of diary entry:
1186 %%(diary-date MONTH DAY YEAR &optional MARK) text
1187 Entry applies if date is MONTH, DAY, YEAR if
1188 `european-calendar-style' is nil, and DAY, MONTH, YEAR if
1189 `european-calendar-style' is t. DAY, MONTH, and YEAR
1190 can be lists of integers, the constant t, or an integer.
1191 The constant t means all values. An optional parameter
1192 MARK specifies a face or single-character string to use
1193 when highlighting the day in the calendar.
1195 %%(diary-float MONTH DAYNAME N &optional DAY MARK) text
1196 Entry will appear on the Nth DAYNAME of MONTH.
1197 (DAYNAME=0 means Sunday, 1 means Monday, and so on;
1198 if N is negative it counts backward from the end of
1199 the month. MONTH can be a list of months, a single
1200 month, or t to specify all months. Optional DAY means
1201 Nth DAYNAME of MONTH on or after/before DAY. DAY defaults
1202 to 1 if N>0 and the last day of the month if N<0. An
1203 optional parameter MARK specifies a face or single-character
1204 string to use when highlighting the day in the calendar.
1206 %%(diary-block M1 D1 Y1 M2 D2 Y2 &optional MARK) text
1207 Entry will appear on dates between M1/D1/Y1 and M2/D2/Y2,
1208 inclusive. (If `european-calendar-style' is t, the
1209 order of the parameters should be changed to D1, M1, Y1,
1210 D2, M2, Y2.) An optional parameter MARK specifies a face
1211 or single-character string to use when highlighting the
1212 day in the calendar.
1214 %%(diary-anniversary MONTH DAY YEAR &optional MARK) text
1215 Entry will appear on anniversary dates of MONTH DAY, YEAR.
1216 (If `european-calendar-style' is t, the order of the
1217 parameters should be changed to DAY, MONTH, YEAR.) Text
1218 can contain %d or %d%s; %d will be replaced by the number
1219 of years since the MONTH DAY, YEAR and %s will be replaced
1220 by the ordinal ending of that number (that is, `st', `nd',
1221 `rd' or `th', as appropriate. The anniversary of February
1222 29 is considered to be March 1 in a non-leap year. An
1223 optional parameter MARK specifies a face or single-character
1224 string to use when highlighting the day in the calendar.
1226 %%(diary-cyclic N MONTH DAY YEAR &optional MARK) text
1227 Entry will appear every N days, starting MONTH DAY, YEAR.
1228 (If `european-calendar-style' is t, the order of the
1229 parameters should be changed to N, DAY, MONTH, YEAR.) Text
1230 can contain %d or %d%s; %d will be replaced by the number
1231 of repetitions since the MONTH DAY, YEAR and %s will
1232 be replaced by the ordinal ending of that number (that is,
1233 `st', `nd', `rd' or `th', as appropriate. An optional
1234 parameter MARK specifies a face or single-character string
1235 to use when highlighting the day in the calendar.
1237 %%(diary-remind SEXP DAYS &optional MARKING) text
1238 Entry is a reminder for diary sexp SEXP. DAYS is either a
1239 single number or a list of numbers indicating the number(s)
1240 of days before the event that the warning(s) should occur.
1241 If the current date is (one of) DAYS before the event
1242 indicated by EXPR, then a suitable message (as specified
1243 by `diary-remind-message') appears. In addition to the
1244 reminders beforehand, the diary entry also appears on
1245 the date itself. If optional MARKING is non-nil then the
1246 *reminders* are marked on the calendar. Marking of
1247 reminders is independent of whether the entry *itself* is
1248 a marking or nonmarking one.
1250 %%(diary-day-of-year)
1251 Diary entries giving the day of the year and the number of
1252 days remaining in the year will be made every day. Note
1253 that since there is no text, it makes sense only if the
1254 fancy diary display is used.
1256 %%(diary-iso-date)
1257 Diary entries giving the corresponding ISO commercial date
1258 will be made every day. Note that since there is no text,
1259 it makes sense only if the fancy diary display is used.
1261 %%(diary-french-date)
1262 Diary entries giving the corresponding French Revolutionary
1263 date will be made every day. Note that since there is no
1264 text, it makes sense only if the fancy diary display is used.
1266 %%(diary-islamic-date)
1267 Diary entries giving the corresponding Islamic date will be
1268 made every day. Note that since there is no text, it
1269 makes sense only if the fancy diary display is used.
1271 %%(diary-hebrew-date)
1272 Diary entries giving the corresponding Hebrew date will be
1273 made every day. Note that since there is no text, it
1274 makes sense only if the fancy diary display is used.
1276 %%(diary-astro-day-number) Diary entries giving the corresponding
1277 astronomical (Julian) day number will be made every day.
1278 Note that since there is no text, it makes sense only if the
1279 fancy diary display is used.
1281 %%(diary-julian-date) Diary entries giving the corresponding
1282 Julian date will be made every day. Note that since
1283 there is no text, it makes sense only if the fancy diary
1284 display is used.
1286 %%(diary-sunrise-sunset)
1287 Diary entries giving the local times of sunrise and sunset
1288 will be made every day. Note that since there is no text,
1289 it makes sense only if the fancy diary display is used.
1290 Floating point required.
1292 %%(diary-phases-of-moon)
1293 Diary entries giving the times of the phases of the moon
1294 will be when appropriate. Note that since there is no text,
1295 it makes sense only if the fancy diary display is used.
1296 Floating point required.
1298 %%(diary-yahrzeit MONTH DAY YEAR) text
1299 Text is assumed to be the name of the person; the date is
1300 the date of death on the *civil* calendar. The diary entry
1301 will appear on the proper Hebrew-date anniversary and on the
1302 day before. (If `european-calendar-style' is t, the order
1303 of the parameters should be changed to DAY, MONTH, YEAR.)
1305 %%(diary-rosh-hodesh)
1306 Diary entries will be made on the dates of Rosh Hodesh on
1307 the Hebrew calendar. Note that since there is no text, it
1308 makes sense only if the fancy diary display is used.
1310 %%(diary-parasha)
1311 Diary entries giving the weekly parasha will be made on
1312 every Saturday. Note that since there is no text, it
1313 makes sense only if the fancy diary display is used.
1315 %%(diary-omer)
1316 Diary entries giving the omer count will be made every day
1317 from Passover to Shavuot. Note that since there is no text,
1318 it makes sense only if the fancy diary display is used.
1320 Marking these entries is *extremely* time consuming, so these entries are
1321 best if they are nonmarking."
1322 (let ((s-entry (concat "\\(\\`\\|\^M\\|\n\\)"
1323 (regexp-quote diary-nonmarking-symbol)
1325 (regexp-quote sexp-diary-entry-symbol)
1326 "("))
1327 entry-found file-glob-attrs marks)
1328 (goto-char (point-min))
1329 (save-excursion
1330 (setq file-glob-attrs (nth 1 (diary-pull-attrs nil '()))))
1331 (while (re-search-forward s-entry nil t)
1332 (backward-char 1)
1333 (let ((sexp-start (point))
1334 sexp entry specifier entry-start line-start)
1335 (forward-sexp)
1336 (setq sexp (buffer-substring-no-properties sexp-start (point)))
1337 (save-excursion
1338 (re-search-backward "\^M\\|\n\\|\\`")
1339 (setq line-start (point)))
1340 (setq specifier
1341 (buffer-substring-no-properties (1+ line-start) (point))
1342 entry-start (1+ line-start))
1343 (forward-char 1)
1344 (if (and (or (char-equal (preceding-char) ?\^M)
1345 (char-equal (preceding-char) ?\n))
1346 (not (looking-at " \\|\^I")))
1347 (progn;; Diary entry consists only of the sexp
1348 (backward-char 1)
1349 (setq entry ""))
1350 (setq entry-start (point))
1351 (re-search-forward "\^M\\|\n" nil t)
1352 (while (looking-at " \\|\^I")
1353 (re-search-forward "\^M\\|\n" nil t))
1354 (backward-char 1)
1355 (setq entry (buffer-substring-no-properties entry-start (point)))
1356 (while (string-match "[\^M]" entry)
1357 (aset entry (match-beginning 0) ?\n )))
1358 (let ((diary-entry (diary-sexp-entry sexp entry date))
1359 temp)
1360 (setq entry (if (consp diary-entry)
1361 (cdr diary-entry)
1362 diary-entry))
1363 (if diary-entry
1364 (progn
1365 (remove-overlays line-start (point) 'invisible 'diary)
1366 (if (< 0 (length entry))
1367 (setq temp (diary-pull-attrs entry file-glob-attrs)
1368 entry (nth 0 temp)
1369 marks (nth 1 temp)))))
1370 (add-to-diary-list date
1371 entry
1372 specifier
1373 (if entry-start (copy-marker entry-start)
1374 nil)
1375 marks)
1376 (setq entry-found (or entry-found diary-entry)))))
1377 entry-found))
1379 (defun diary-sexp-entry (sexp entry date)
1380 "Process a SEXP diary ENTRY for DATE."
1381 (let ((result (if calendar-debug-sexp
1382 (let ((stack-trace-on-error t))
1383 (eval (car (read-from-string sexp))))
1384 (condition-case nil
1385 (eval (car (read-from-string sexp)))
1386 (error
1387 (beep)
1388 (message "Bad sexp at line %d in %s: %s"
1389 (count-lines (point-min) (point))
1390 diary-file sexp)
1391 (sleep-for 2))))))
1392 (cond ((stringp result) result)
1393 ((and (consp result)
1394 (stringp (cdr result))) result)
1395 (result entry)
1396 (t nil))))
1398 (defun diary-date (month day year &optional mark)
1399 "Specific date(s) diary entry.
1400 Entry applies if date is MONTH, DAY, YEAR if `european-calendar-style' is nil,
1401 and DAY, MONTH, YEAR if `european-calendar-style' is t. DAY, MONTH, and YEAR
1402 can be lists of integers, the constant t, or an integer. The constant t means
1403 all values.
1405 An optional parameter MARK specifies a face or single-character string to
1406 use when highlighting the day in the calendar."
1407 (let ((dd (if european-calendar-style
1408 month
1409 day))
1410 (mm (if european-calendar-style
1412 month))
1413 (m (extract-calendar-month date))
1414 (y (extract-calendar-year date))
1415 (d (extract-calendar-day date)))
1416 (if (and
1417 (or (and (listp dd) (memq d dd))
1418 (equal d dd)
1419 (eq dd t))
1420 (or (and (listp mm) (memq m mm))
1421 (equal m mm)
1422 (eq mm t))
1423 (or (and (listp year) (memq y year))
1424 (equal y year)
1425 (eq year t)))
1426 (cons mark entry))))
1428 (defun diary-block (m1 d1 y1 m2 d2 y2 &optional mark)
1429 "Block diary entry.
1430 Entry applies if date is between, or on one of, two dates.
1431 The order of the parameters is
1432 M1, D1, Y1, M2, D2, Y2 if `european-calendar-style' is nil, and
1433 D1, M1, Y1, D2, M2, Y2 if `european-calendar-style' is t.
1435 An optional parameter MARK specifies a face or single-character string to
1436 use when highlighting the day in the calendar."
1438 (let ((date1 (calendar-absolute-from-gregorian
1439 (if european-calendar-style
1440 (list d1 m1 y1)
1441 (list m1 d1 y1))))
1442 (date2 (calendar-absolute-from-gregorian
1443 (if european-calendar-style
1444 (list d2 m2 y2)
1445 (list m2 d2 y2))))
1446 (d (calendar-absolute-from-gregorian date)))
1447 (if (and (<= date1 d) (<= d date2))
1448 (cons mark entry))))
1450 (defun diary-float (month dayname n &optional day mark)
1451 "Floating diary entry--entry applies if date is the nth dayname of month.
1452 Parameters are MONTH, DAYNAME, N. MONTH can be a list of months, the constant
1453 t, or an integer. The constant t means all months. If N is negative, count
1454 backward from the end of the month.
1456 An optional parameter DAY means the Nth DAYNAME on or after/before MONTH DAY.
1457 Optional MARK specifies a face or single-character string to use when
1458 highlighting the day in the calendar."
1459 ;; This is messy because the diary entry may apply, but the date on which it
1460 ;; is based can be in a different month/year. For example, asking for the
1461 ;; first Monday after December 30. For large values of |n| the problem is
1462 ;; more grotesque.
1463 (and (= dayname (calendar-day-of-week date))
1464 (let* ((m (extract-calendar-month date))
1465 (d (extract-calendar-day date))
1466 (y (extract-calendar-year date))
1467 (limit; last (n>0) or first (n<0) possible base date for entry
1468 (calendar-nth-named-absday (- n) dayname m y d))
1469 (last-abs (if (> n 0) limit (+ limit 6)))
1470 (first-abs (if (> n 0) (- limit 6) limit))
1471 (last (calendar-gregorian-from-absolute last-abs))
1472 (first (calendar-gregorian-from-absolute first-abs))
1473 ; m1, d1 is first possible base date
1474 (m1 (extract-calendar-month first))
1475 (d1 (extract-calendar-day first))
1476 (y1 (extract-calendar-year first))
1477 ; m2, d2 is last possible base date
1478 (m2 (extract-calendar-month last))
1479 (d2 (extract-calendar-day last))
1480 (y2 (extract-calendar-year last)))
1481 (if (or (and (= m1 m2) ; only possible base dates in one month
1482 (or (eq month t)
1483 (if (listp month)
1484 (memq m1 month)
1485 (= m1 month)))
1486 (let ((d (or day (if (> n 0)
1488 (calendar-last-day-of-month m1 y1)))))
1489 (and (<= d1 d) (<= d d2))))
1490 ;; only possible base dates straddle two months
1491 (and (or (< y1 y2)
1492 (and (= y1 y2) (< m1 m2)))
1494 ;; m1, d1 works as a base date
1495 (and
1496 (or (eq month t)
1497 (if (listp month)
1498 (memq m1 month)
1499 (= m1 month)))
1500 (<= d1 (or day (if (> n 0)
1502 (calendar-last-day-of-month m1 y1)))))
1503 ;; m2, d2 works as a base date
1504 (and (or (eq month t)
1505 (if (listp month)
1506 (memq m2 month)
1507 (= m2 month)))
1508 (<= (or day (if (> n 0)
1510 (calendar-last-day-of-month m2 y2)))
1511 d2)))))
1512 (cons mark entry)))))
1515 (defun diary-anniversary (month day &optional year mark)
1516 "Anniversary diary entry.
1517 Entry applies if date is the anniversary of MONTH, DAY, YEAR if
1518 `european-calendar-style' is nil, and DAY, MONTH, YEAR if
1519 `european-calendar-style' is t. Diary entry can contain `%d' or `%d%s'; the
1520 %d will be replaced by the number of years since the MONTH DAY, YEAR and the
1521 %s will be replaced by the ordinal ending of that number (that is, `st', `nd',
1522 `rd' or `th', as appropriate. The anniversary of February 29 is considered
1523 to be March 1 in non-leap years.
1525 An optional parameter MARK specifies a face or single-character string to
1526 use when highlighting the day in the calendar."
1527 (let* ((d (if european-calendar-style
1528 month
1529 day))
1530 (m (if european-calendar-style
1532 month))
1533 (y (extract-calendar-year date))
1534 (diff (if year (- y year) 100)))
1535 (if (and (= m 2) (= d 29) (not (calendar-leap-year-p y)))
1536 (setq m 3
1537 d 1))
1538 (if (and (> diff 0) (calendar-date-equal (list m d y) date))
1539 (cons mark (format entry diff (diary-ordinal-suffix diff))))))
1541 (defun diary-cyclic (n month day year &optional mark)
1542 "Cycle diary entry--entry applies every N days starting at MONTH, DAY, YEAR.
1543 If `european-calendar-style' is t, parameters are N, DAY, MONTH, YEAR.
1544 ENTRY can contain `%d' or `%d%s'; the %d will be replaced by the number of
1545 repetitions since the MONTH DAY, YEAR and %s will be replaced by the
1546 ordinal ending of that number (that is, `st', `nd', `rd' or `th', as
1547 appropriate.
1549 An optional parameter MARK specifies a face or single-character string to
1550 use when highlighting the day in the calendar."
1551 (let* ((d (if european-calendar-style
1552 month
1553 day))
1554 (m (if european-calendar-style
1556 month))
1557 (diff (- (calendar-absolute-from-gregorian date)
1558 (calendar-absolute-from-gregorian
1559 (list m d year))))
1560 (cycle (/ diff n)))
1561 (if (and (>= diff 0) (zerop (% diff n)))
1562 (cons mark (format entry cycle (diary-ordinal-suffix cycle))))))
1564 (defun diary-ordinal-suffix (n)
1565 "Ordinal suffix for N. (That is, `st', `nd', `rd', or `th', as appropriate.)"
1566 (if (or (memq (% n 100) '(11 12 13))
1567 (< 3 (% n 10)))
1568 "th"
1569 (aref ["th" "st" "nd" "rd"] (% n 10))))
1571 (defun diary-day-of-year ()
1572 "Day of year and number of days remaining in the year of date diary entry."
1573 (calendar-day-of-year-string date))
1575 (defcustom diary-remind-message
1576 '("Reminder: Only "
1577 (if (= 0 (% days 7))
1578 (concat (int-to-string (/ days 7)) (if (= 7 days) " week" " weeks"))
1579 (concat (int-to-string days) (if (= 1 days) " day" " days")))
1580 " until "
1581 diary-entry)
1582 "Pseudo-pattern giving form of reminder messages in the fancy diary
1583 display.
1585 Used by the function `diary-remind', a pseudo-pattern is a list of
1586 expressions that can involve the keywords `days' (a number), `date' (a list of
1587 month, day, year), and `diary-entry' (a string)."
1588 :type 'sexp
1589 :group 'diary)
1591 (defun diary-remind (sexp days &optional marking)
1592 "Provide a reminder of a diary entry.
1593 SEXP is a diary-sexp. DAYS is either a single number or a list of numbers
1594 indicating the number(s) of days before the event that the warning(s) should
1595 occur on. If the current date is (one of) DAYS before the event indicated by
1596 SEXP, then a suitable message (as specified by `diary-remind-message' is
1597 returned.
1599 In addition to the reminders beforehand, the diary entry also appears on the
1600 date itself.
1602 A `diary-nonmarking-symbol' at the beginning of the line of the diary-remind
1603 entry specifies that the diary entry (not the reminder) is non-marking.
1604 Marking of reminders is independent of whether the entry itself is a marking
1605 or nonmarking; if optional parameter MARKING is non-nil then the reminders are
1606 marked on the calendar."
1607 (let ((diary-entry (eval sexp)))
1608 (cond
1609 ;; Diary entry applies on date
1610 ((and diary-entry
1611 (or (not marking-diary-entries) marking-diary-entry))
1612 diary-entry)
1613 ;; Diary entry may apply to `days' before date
1614 ((and (integerp days)
1615 (not diary-entry); Diary entry does not apply to date
1616 (or (not marking-diary-entries) marking))
1617 (let ((date (calendar-gregorian-from-absolute
1618 (+ (calendar-absolute-from-gregorian date) days))))
1619 (when (setq diary-entry (eval sexp)) ; re-evaluate with adjusted date
1620 ;; Discard any mark portion from diary-anniversary, etc.
1621 (if (consp diary-entry) (setq diary-entry (cdr diary-entry)))
1622 (mapconcat 'eval diary-remind-message ""))))
1623 ;; Diary entry may apply to one of a list of days before date
1624 ((and (listp days) days)
1625 (or (diary-remind sexp (car days) marking)
1626 (diary-remind sexp (cdr days) marking))))))
1628 (defvar diary-modify-entry-list-string-function nil
1629 "Function applied to entry string before putting it into the entries list.
1630 Can be used by programs integrating a diary list into other buffers (e.g.
1631 org.el and planner.el) to modify the string or add properties to it.
1632 The function takes a string argument and must return a string.")
1634 (defun add-to-diary-list (date string specifier &optional marker globcolor)
1635 "Add the entry (DATE STRING SPECIFIER MARKER GLOBCOLOR) to `diary-entries-list'.
1636 Do nothing if DATE or STRING is nil."
1637 (when (and date string)
1638 (if diary-file-name-prefix
1639 (let ((prefix (funcall diary-file-name-prefix-function
1640 (buffer-file-name))))
1641 (or (string= prefix "")
1642 (setq string (format "[%s] %s" prefix string)))))
1643 (and diary-modify-entry-list-string-function
1644 (setq string (funcall diary-modify-entry-list-string-function
1645 string)))
1646 (setq diary-entries-list
1647 (append diary-entries-list
1648 (list (list date string specifier marker globcolor))))))
1650 (defun diary-redraw-calendar ()
1651 "If `calendar-buffer' is live and diary entries are marked, redraw it."
1652 (and mark-diary-entries-in-calendar
1653 (save-excursion
1654 (redraw-calendar)))
1655 ;; Return value suitable for `write-contents-functions'.
1656 nil)
1658 (defun make-diary-entry (string &optional nonmarking file)
1659 "Insert a diary entry STRING which may be NONMARKING in FILE.
1660 If omitted, NONMARKING defaults to nil and FILE defaults to
1661 `diary-file'."
1662 (let ((pop-up-frames (window-dedicated-p (selected-window))))
1663 (find-file-other-window (substitute-in-file-name (or file diary-file))))
1664 (when (eq major-mode 'fundamental-mode) (diary-mode))
1665 (widen)
1666 (diary-unhide-everything)
1667 (goto-char (point-max))
1668 (when (let ((case-fold-search t))
1669 (search-backward "Local Variables:"
1670 (max (- (point-max) 3000) (point-min))
1672 (beginning-of-line)
1673 (insert "\n")
1674 (forward-line -1))
1675 (insert
1676 (if (bolp) "" "\n")
1677 (if nonmarking diary-nonmarking-symbol "")
1678 string " "))
1680 (defun insert-diary-entry (arg)
1681 "Insert a diary entry for the date indicated by point.
1682 Prefix arg will make the entry nonmarking."
1683 (interactive "P")
1684 (make-diary-entry (calendar-date-string (calendar-cursor-to-date t) t t)
1685 arg))
1687 (defun insert-weekly-diary-entry (arg)
1688 "Insert a weekly diary entry for the day of the week indicated by point.
1689 Prefix arg will make the entry nonmarking."
1690 (interactive "P")
1691 (make-diary-entry (calendar-day-name (calendar-cursor-to-date t))
1692 arg))
1694 (defun insert-monthly-diary-entry (arg)
1695 "Insert a monthly diary entry for the day of the month indicated by point.
1696 Prefix arg will make the entry nonmarking."
1697 (interactive "P")
1698 (let ((calendar-date-display-form
1699 (if european-calendar-style
1700 '(day " * ")
1701 '("* " day))))
1702 (make-diary-entry (calendar-date-string (calendar-cursor-to-date t) t)
1703 arg)))
1705 (defun insert-yearly-diary-entry (arg)
1706 "Insert an annual diary entry for the day of the year 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 " " monthname)
1712 '(monthname " " day))))
1713 (make-diary-entry (calendar-date-string (calendar-cursor-to-date t) t)
1714 arg)))
1716 (defun insert-anniversary-diary-entry (arg)
1717 "Insert an anniversary diary entry for the date given 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 " " month " " year)
1723 '(month " " day " " year))))
1724 (make-diary-entry
1725 (format "%s(diary-anniversary %s)"
1726 sexp-diary-entry-symbol
1727 (calendar-date-string (calendar-cursor-to-date t) nil t))
1728 arg)))
1730 (defun insert-block-diary-entry (arg)
1731 "Insert a block diary entry for the days between the point and marked date.
1732 Prefix arg will make the entry nonmarking."
1733 (interactive "P")
1734 (let ((calendar-date-display-form
1735 (if european-calendar-style
1736 '(day " " month " " year)
1737 '(month " " day " " year)))
1738 (cursor (calendar-cursor-to-date t))
1739 (mark (or (car calendar-mark-ring)
1740 (error "No mark set in this buffer")))
1741 start end)
1742 (if (< (calendar-absolute-from-gregorian mark)
1743 (calendar-absolute-from-gregorian cursor))
1744 (setq start mark
1745 end cursor)
1746 (setq start cursor
1747 end mark))
1748 (make-diary-entry
1749 (format "%s(diary-block %s %s)"
1750 sexp-diary-entry-symbol
1751 (calendar-date-string start nil t)
1752 (calendar-date-string end nil t))
1753 arg)))
1755 (defun insert-cyclic-diary-entry (arg)
1756 "Insert a cyclic diary entry starting at the date given by point.
1757 Prefix arg will make the entry nonmarking."
1758 (interactive "P")
1759 (let ((calendar-date-display-form
1760 (if european-calendar-style
1761 '(day " " month " " year)
1762 '(month " " day " " year))))
1763 (make-diary-entry
1764 (format "%s(diary-cyclic %d %s)"
1765 sexp-diary-entry-symbol
1766 (calendar-read "Repeat every how many days: "
1767 (lambda (x) (> x 0)))
1768 (calendar-date-string (calendar-cursor-to-date t) nil t))
1769 arg)))
1771 (defvar diary-mode-map
1772 (let ((map (make-sparse-keymap)))
1773 (define-key map "\C-c\C-s" 'diary-show-all-entries)
1774 (define-key map "\C-c\C-q" 'quit-window)
1775 map)
1776 "Keymap for `diary-mode'.")
1778 ;;;###autoload
1779 (define-derived-mode diary-mode fundamental-mode "Diary"
1780 "Major mode for editing the diary file."
1781 (set (make-local-variable 'font-lock-defaults)
1782 '(diary-font-lock-keywords t))
1783 (add-to-invisibility-spec '(diary . nil))
1784 (add-hook 'after-save-hook 'diary-redraw-calendar nil t)
1785 (if diary-header-line-flag
1786 (setq header-line-format diary-header-line-format)))
1788 (define-derived-mode fancy-diary-display-mode fundamental-mode
1789 "Diary"
1790 "Major mode used while displaying diary entries using Fancy Display."
1791 (set (make-local-variable 'font-lock-defaults)
1792 '(fancy-diary-font-lock-keywords t))
1793 (local-set-key "q" 'quit-window))
1796 (defvar fancy-diary-font-lock-keywords
1797 (list
1798 (cons
1799 (concat
1800 (let ((dayname (diary-name-pattern calendar-day-name-array nil t))
1801 (monthname (diary-name-pattern calendar-month-name-array nil t))
1802 (day "[0-9]+")
1803 (month "[0-9]+")
1804 (year "-?[0-9]+"))
1805 (mapconcat 'eval calendar-date-display-form ""))
1806 "\\(\\(: .*\\)\\|\\(\n +.*\\)\\)*\n=+$")
1807 'diary-face)
1808 '("^.*anniversary.*$" . font-lock-keyword-face)
1809 '("^.*birthday.*$" . font-lock-keyword-face)
1810 '("^.*Yahrzeit.*$" . font-lock-reference-face)
1811 '("^\\(Erev \\)?Rosh Hodesh.*" . font-lock-function-name-face)
1812 '("^Day.*omer.*$" . font-lock-builtin-face)
1813 '("^Parashat.*$" . font-lock-comment-face)
1814 '("^[ \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\\)?\\)?"
1815 . font-lock-variable-name-face))
1816 "Keywords to highlight in fancy diary display")
1819 (defun diary-font-lock-sexps (limit)
1820 "Recognize sexp diary entry for font-locking."
1821 (if (re-search-forward
1822 (concat "^" (regexp-quote diary-nonmarking-symbol)
1823 "?\\(" (regexp-quote sexp-diary-entry-symbol) "\\)")
1824 limit t)
1825 (condition-case nil
1826 (save-restriction
1827 (narrow-to-region (point-min) limit)
1828 (let ((start (point)))
1829 (forward-sexp 1)
1830 (store-match-data (list start (point)))
1832 (error t))))
1834 (defun diary-font-lock-date-forms (month-array &optional symbol abbrev-array)
1835 "Create font-lock patterns for `diary-date-forms' using MONTH-ARRAY.
1836 If given, optional SYMBOL must be a prefix to entries.
1837 If optional ABBREV-ARRAY is present, the abbreviations constructed
1838 from this array by the function `calendar-abbrev-construct' are
1839 matched (with or without a final `.'), in addition to the full month
1840 names."
1841 (let ((dayname (diary-name-pattern calendar-day-name-array
1842 calendar-day-abbrev-array t))
1843 (monthname (format "\\(%s\\|\\*\\)"
1844 (diary-name-pattern month-array abbrev-array)))
1845 (month "\\([0-9]+\\|\\*\\)")
1846 (day "\\([0-9]+\\|\\*\\)")
1847 (year "-?\\([0-9]+\\|\\*\\)"))
1848 (mapcar (lambda (x)
1849 (cons
1850 (concat "^" (regexp-quote diary-nonmarking-symbol) "?"
1851 (if symbol (regexp-quote symbol) "") "\\("
1852 (mapconcat 'eval
1853 ;; If backup, omit first item (backup)
1854 ;; and last item (not part of date)
1855 (if (equal (car x) 'backup)
1856 (nreverse (cdr (reverse (cdr x))))
1859 ;; With backup, last item is not part of date
1860 (if (equal (car x) 'backup)
1861 (concat "\\)" (eval (car (reverse x))))
1862 "\\)"))
1863 '(1 diary-face)))
1864 diary-date-forms)))
1866 (eval-when-compile (require 'cal-hebrew)
1867 (require 'cal-islam))
1869 (defconst diary-time-regexp
1870 ;; Formats that should be accepted:
1871 ;; 10:00 10.00 10h00 10h 10am 10:00am 10.00am
1872 (concat "[0-9]?[0-9]\\([AaPp][mM]\\|\\("
1873 "[Hh]\\([0-9][0-9]\\)?\\|[:.][0-9][0-9]"
1874 "\\)\\([AaPp][Mm]\\)?\\)"))
1876 (defvar diary-font-lock-keywords
1877 (append
1878 (diary-font-lock-date-forms calendar-month-name-array
1879 nil calendar-month-abbrev-array)
1880 (when (or (memq 'mark-hebrew-diary-entries
1881 nongregorian-diary-marking-hook)
1882 (memq 'list-hebrew-diary-entries
1883 nongregorian-diary-listing-hook))
1884 (require 'cal-hebrew)
1885 (diary-font-lock-date-forms
1886 calendar-hebrew-month-name-array-leap-year
1887 hebrew-diary-entry-symbol))
1888 (when (or (memq 'mark-islamic-diary-entries
1889 nongregorian-diary-marking-hook)
1890 (memq 'list-islamic-diary-entries
1891 nongregorian-diary-listing-hook))
1892 (require 'cal-islam)
1893 (diary-font-lock-date-forms
1894 calendar-islamic-month-name-array
1895 islamic-diary-entry-symbol))
1896 (list
1897 (cons
1898 (concat "^" (regexp-quote diary-include-string) ".*$")
1899 'font-lock-keyword-face)
1900 (cons
1901 (concat "^" (regexp-quote diary-nonmarking-symbol)
1902 "?\\(" (regexp-quote sexp-diary-entry-symbol) "\\)")
1903 '(1 font-lock-reference-face))
1904 (cons
1905 (concat "^" (regexp-quote diary-nonmarking-symbol))
1906 'font-lock-reference-face)
1907 (cons
1908 (concat "^" (regexp-quote diary-nonmarking-symbol)
1909 "?\\(" (regexp-quote hebrew-diary-entry-symbol) "\\)")
1910 '(1 font-lock-reference-face))
1911 (cons
1912 (concat "^" (regexp-quote diary-nonmarking-symbol)
1913 "?\\(" (regexp-quote islamic-diary-entry-symbol) "\\)")
1914 '(1 font-lock-reference-face))
1915 '(diary-font-lock-sexps . font-lock-keyword-face)
1916 (cons
1917 (concat ;; "^[ \t]+"
1918 diary-time-regexp "\\(-" diary-time-regexp "\\)?")
1919 'font-lock-function-name-face)))
1920 "Forms to highlight in `diary-mode'.")
1923 ;; Following code from Dave Love <fx@gnu.org>.
1924 ;; Import Outlook-format appointments from mail messages in Gnus or
1925 ;; Rmail using command `diary-from-outlook'. This, or the specialized
1926 ;; functions `diary-from-outlook-gnus' and `diary-from-outlook-rmail',
1927 ;; could be run from hooks to notice appointments automatically (in
1928 ;; which case they will prompt about adding to the diary). The
1929 ;; message formats recognized are customizable through
1930 ;; `diary-outlook-formats'.
1932 (defcustom diary-outlook-formats
1934 ;; When: 11 October 2001 12:00-14:00 (GMT) Greenwich Mean Time : Dublin, ...
1935 ;; [Current UK format? The timezone is meaningless. Sometimes the
1936 ;; Where is missing.]
1937 ("When: \\([0-9]+ [[:alpha:]]+ [0-9]+\\) \
1938 \\([^ ]+\\) [^\n]+
1939 \[^\n]+
1940 \\(?:Where: \\([^\n]+\\)\n+\\)?
1941 \\*~\\*~\\*~\\*~\\*~\\*~\\*~\\*~\\*~\\*"
1942 . "\\1\n \\2 %s, \\3")
1943 ;; When: Tuesday, April 30, 2002 03:00 PM-03:30 PM (GMT) Greenwich Mean ...
1944 ;; [Old UK format?]
1945 ("^When: [[:alpha:]]+, \\([[:alpha:]]+\\) \\([0-9][0-9]*\\), \\([0-9]\\{4\\}\\) \
1946 \\([^ ]+\\) [^\n]+
1947 \[^\n]+
1948 \\(?:Where: \\([^\n]+\\)\\)?\n+"
1949 . "\\2 \\1 \\3\n \\4 %s, \\5")
1951 ;; German format, apparently.
1952 "^Zeit: [^ ]+, +\\([0-9]+\\)\. +\\([[:upper:]][[:lower:]][[:lower:]]\\)[^ ]* +\\([0-9]+\\) +\\([^ ]+\\).*$"
1953 . "\\1 \\2 \\3\n \\4 %s"))
1954 "Alist of regexps matching message text and replacement text.
1956 The regexp must match the start of the message text containing an
1957 appointment, but need not include a leading `^'. If it matches the
1958 current message, a diary entry is made from the corresponding
1959 template. If the template is a string, it should be suitable for
1960 passing to `replace-match', and so will have occurrences of `\\D' to
1961 substitute the match for the Dth subexpression. It must also contain
1962 a single `%s' which will be replaced with the text of the message's
1963 Subject field. Any other `%' characters must be doubled, so that the
1964 template can be passed to `format'.
1966 If the template is actually a function, it is called with the message
1967 body text as argument, and may use `match-string' etc. to make a
1968 template following the rules above."
1969 :type '(alist :key-type (regexp :tag "Regexp matching time/place")
1970 :value-type (choice
1971 (string :tag "Template for entry")
1972 (function :tag "Unary function providing template")))
1973 :version "22.1"
1974 :group 'diary)
1977 ;; Dynamically bound.
1978 (defvar body)
1979 (defvar subject)
1981 (defun diary-from-outlook-internal (&optional test-only)
1982 "Snarf a diary entry from a message assumed to be from MS Outlook.
1983 Assumes `body' is bound to a string comprising the body of the message and
1984 `subject' is bound to a string comprising its subject.
1985 Arg TEST-ONLY non-nil means return non-nil if and only if the
1986 message contains an appointment, don't make a diary entry."
1987 (catch 'finished
1988 (let (format-string)
1989 (dotimes (i (length diary-outlook-formats))
1990 (when (eq 0 (string-match (car (nth i diary-outlook-formats))
1991 body))
1992 (unless test-only
1993 (setq format-string (cdr (nth i diary-outlook-formats)))
1994 (save-excursion
1995 (save-window-excursion
1996 ;; Fixme: References to optional fields in the format
1997 ;; are treated literally, not replaced by the empty
1998 ;; string. I think this is an Emacs bug.
1999 (make-diary-entry
2000 (format (replace-match (if (functionp format-string)
2001 (funcall format-string body)
2002 format-string)
2003 t nil (match-string 0 body))
2004 subject))
2005 (save-buffer))))
2006 (throw 'finished t))))
2007 nil))
2009 (defun diary-from-outlook (&optional noconfirm)
2010 "Maybe snarf diary entry from current Outlook-generated message.
2011 Currently knows about Gnus and Rmail modes. Unless the optional
2012 argument NOCONFIRM is non-nil (which is the case when this
2013 function is called interactively), then if an entry is found the
2014 user is asked to confirm its addition."
2015 (interactive "p")
2016 (let ((func (cond
2017 ((eq major-mode 'rmail-mode)
2018 #'diary-from-outlook-rmail)
2019 ((memq major-mode '(gnus-summary-mode gnus-article-mode))
2020 #'diary-from-outlook-gnus)
2021 (t (error "Don't know how to snarf in `%s'" major-mode)))))
2022 (funcall func noconfirm)))
2025 (defvar gnus-article-mime-handles)
2026 (defvar gnus-article-buffer)
2028 (autoload 'gnus-fetch-field "gnus-util")
2029 (autoload 'gnus-narrow-to-body "gnus")
2030 (autoload 'mm-get-part "mm-decode")
2032 (defun diary-from-outlook-gnus (&optional noconfirm)
2033 "Maybe snarf diary entry from Outlook-generated message in Gnus.
2034 Unless the optional argument NOCONFIRM is non-nil (which is the case when
2035 this function is called interactively), then if an entry is found the
2036 user is asked to confirm its addition.
2037 Add this function to `gnus-article-prepare-hook' to notice appointments
2038 automatically."
2039 (interactive "p")
2040 (with-current-buffer gnus-article-buffer
2041 (let ((subject (gnus-fetch-field "subject"))
2042 (body (if gnus-article-mime-handles
2043 ;; We're multipart. Don't get confused by part
2044 ;; buttons &c. Assume info is in first part.
2045 (mm-get-part (nth 1 gnus-article-mime-handles))
2046 (save-restriction
2047 (gnus-narrow-to-body)
2048 (buffer-string)))))
2049 (when (diary-from-outlook-internal t)
2050 (when (or noconfirm (y-or-n-p "Snarf diary entry? "))
2051 (diary-from-outlook-internal)
2052 (message "Diary entry added"))))))
2054 (custom-add-option 'gnus-article-prepare-hook 'diary-from-outlook-gnus)
2057 (defvar rmail-buffer)
2059 (defun diary-from-outlook-rmail (&optional noconfirm)
2060 "Maybe snarf diary entry from Outlook-generated message in Rmail.
2061 Unless the optional argument NOCONFIRM is non-nil (which is the case when
2062 this function is called interactively), then if an entry is found the
2063 user is asked to confirm its addition."
2064 (interactive "p")
2065 (with-current-buffer rmail-buffer
2066 (let ((subject (mail-fetch-field "subject"))
2067 (body (buffer-substring (save-excursion
2068 (rfc822-goto-eoh)
2069 (point))
2070 (point-max))))
2071 (when (diary-from-outlook-internal t)
2072 (when (or noconfirm (y-or-n-p "Snarf diary entry? "))
2073 (diary-from-outlook-internal)
2074 (message "Diary entry added"))))))
2077 (provide 'diary-lib)
2079 ;; arch-tag: 22dd506e-2e33-410d-9ae1-095a0c1b2010
2080 ;;; diary-lib.el ends here