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