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