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