*** empty log message ***
[emacs.git] / lisp / diary-lib.el
blob83e22d52aa4815fd4f85c4f1cc59df970fcfa0b0
1 ;;; diary.el --- diary functions.
3 ;; Copyright (C) 1989, 1990, 1991 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is distributed in the hope that it will be useful,
8 ;; but WITHOUT ANY WARRANTY. No author or distributor
9 ;; accepts responsibility to anyone for the consequences of using it
10 ;; or for whether it serves any particular purpose or works at all,
11 ;; unless he says so in writing. Refer to the GNU Emacs General Public
12 ;; License for full details.
14 ;; Everyone is granted permission to copy, modify and redistribute
15 ;; GNU Emacs, but only under the conditions described in the
16 ;; GNU Emacs General Public License. A copy of this license is
17 ;; supposed to have been given to you along with GNU Emacs so you
18 ;; can know your rights and responsibilities. It should be in a
19 ;; file named COPYING. Among other things, the copyright notice
20 ;; and this notice must be preserved on all copies.
22 ;; This collection of functions implements the diary features as described
23 ;; in calendar.el.
25 ;; Comments, corrections, and improvements should be sent to
26 ;; Edward M. Reingold Department of Computer Science
27 ;; (217) 333-6733 University of Illinois at Urbana-Champaign
28 ;; reingold@cs.uiuc.edu 1304 West Springfield Avenue
29 ;; Urbana, Illinois 61801
31 (require 'calendar)
32 (defun diary (&optional arg)
33 "Generate the diary window for ARG days starting with the current date.
34 If no argument is provided, the number of days of diary entries is governed
35 by the variable `number-of-diary-entries'. This function is suitable for
36 execution in a .emacs file."
37 (interactive "P")
38 (let ((d-file (substitute-in-file-name diary-file))
39 (date (calendar-current-date)))
40 (if (and d-file (file-exists-p d-file))
41 (if (file-readable-p d-file)
42 (list-diary-entries
43 date
44 (cond
45 (arg (prefix-numeric-value arg))
46 ((vectorp number-of-diary-entries)
47 (aref number-of-diary-entries (calendar-day-of-week date)))
48 (t number-of-diary-entries)))
49 (error "Your diary file is not readable!"))
50 (error "You don't have a diary file!"))))
52 (defun view-diary-entries (arg)
53 "Prepare and display a buffer with diary entries.
54 Searches the file diary-file for entries that match ARG days starting with
55 the date indicated by the cursor position in the displayed three-month
56 calendar."
57 (interactive "p")
58 (let ((d-file (substitute-in-file-name diary-file)))
59 (if (and d-file (file-exists-p d-file))
60 (if (file-readable-p d-file)
61 (list-diary-entries (or (calendar-cursor-to-date)
62 (error "Cursor is not on a date!"))
63 arg)
64 (error "Your diary file is not readable!"))
65 (error "You don't have a diary file!"))))
67 (autoload 'check-calendar-holidays "holidays"
68 "Check the list of holidays for any that occur on DATE.
69 The value returned is a list of strings of relevant holiday descriptions.
70 The holidays are those in the list calendar-holidays.")
72 (autoload 'calendar-holiday-list "holidays"
73 "Form the list of holidays that occur on dates in the calendar window.
74 The holidays are those in the list calendar-holidays.")
76 (defvar diary-syntax-table
77 (standard-syntax-table)
78 "The syntax table used when parsing dates in the diary file.
79 It is the standard syntax table used in Fundamental mode, but with the
80 syntax of `*' changed to be a word constituent.")
82 (modify-syntax-entry ?* "w" diary-syntax-table)
84 (defun list-diary-entries (date number)
85 "Create and display a buffer containing the relevant lines in diary-file.
86 All lines that apply to DATE and the next NUMBER-1 days are included.
88 Makes all diary entries in the diary file invisible (using selective display),
89 *except* those that are relevant.
91 Returns a list of all relevant diary entries found, if any, in order by date.
92 The list entries have the form ((month day year) string). If the variable
93 `diary-list-include-blanks' is t, this list will include a dummy diary entry
94 \(consisting of the empty string\) for a date with no diary entries.
96 After the list is prepared, the hooks `nongregorian-diary-listing-hook',
97 `list-diary-entries-hook', and `diary-display-hook' are run. These hooks
98 have the following distinct roles:
100 `nongregorian-diary-listing-hook' can cull dates from the diary
101 and each included file. Usually used for Hebrew or Islamic
102 diary entries in files. Applied to *each* file.
104 `list-diary-entries-hook' adds or manipulates diary entries from
105 external sources. Used, for example, to include diary entries
106 from other files or to sort the diary entries. Invoked *once* only.
108 `diary-display-hook' does the actual display of information. Could be
109 used also for an appointment notification function."
111 (if (< 0 number)
112 (let* ((original-date date);; save for possible use in the hooks
113 (old-diary-syntax-table)
114 (diary-entries-list)
115 (date-string (calendar-date-string date))
116 (d-file (substitute-in-file-name diary-file)))
117 (message "Preparing diary...")
118 (save-excursion
119 (let ((diary-buffer (get-file-buffer d-file)))
120 (set-buffer (if diary-buffer
121 diary-buffer
122 (find-file-noselect d-file t))))
123 (setq selective-display t)
124 (setq selective-display-ellipses nil)
125 (setq old-diary-syntax-table (syntax-table))
126 (set-syntax-table diary-syntax-table)
127 (unwind-protect
128 (let ((buffer-read-only nil)
129 (diary-modified (buffer-modified-p))
130 (mark (regexp-quote diary-nonmarking-symbol)))
131 (goto-char (1- (point-max)))
132 (if (not (looking-at "\^M\\|\n"))
133 (progn
134 (forward-char 1)
135 (insert-string "\^M")))
136 (goto-char (point-min))
137 (if (not (looking-at "\^M\\|\n"))
138 (insert-string "\^M"))
139 (subst-char-in-region (point-min) (point-max) ?\n ?\^M t)
140 (calendar-for-loop i from 1 to number do
141 (let ((d diary-date-forms)
142 (month (extract-calendar-month date))
143 (day (extract-calendar-day date))
144 (year (extract-calendar-year date))
145 (entry-found (list-sexp-diary-entries date)))
146 (while d
147 (let*
148 ((date-form (if (equal (car (car d)) 'backup)
149 (cdr (car d))
150 (car d)))
151 (backup (equal (car (car d)) 'backup))
152 (dayname
153 (concat
154 (calendar-day-name date) "\\|"
155 (substring (calendar-day-name date) 0 3) ".?"))
156 (monthname
157 (concat
158 "\\*\\|"
159 (calendar-month-name month) "\\|"
160 (substring (calendar-month-name month) 0 3) ".?"))
161 (month (concat "\\*\\|0*" (int-to-string month)))
162 (day (concat "\\*\\|0*" (int-to-string day)))
163 (year
164 (concat
165 "\\*\\|0*" (int-to-string year)
166 (if abbreviated-calendar-year
167 (concat "\\|" (int-to-string (% year 100)))
168 "")))
169 (regexp
170 (concat
171 "\\(\\`\\|\^M\\|\n\\)" mark "?\\("
172 (mapconcat 'eval date-form "\\)\\(")
173 "\\)"))
174 (case-fold-search t))
175 (goto-char (point-min))
176 (while (re-search-forward regexp nil t)
177 (if backup (re-search-backward "\\<" nil t))
178 (if (and (or (char-equal (preceding-char) ?\^M)
179 (char-equal (preceding-char) ?\n))
180 (not (looking-at " \\|\^I")))
181 ;; Diary entry that consists only of date.
182 (backward-char 1)
183 ;; Found a nonempty diary entry--make it visible and
184 ;; add it to the list.
185 (setq entry-found t)
186 (let ((entry-start (point))
187 (date-start))
188 (re-search-backward "\^M\\|\n\\|\\`")
189 (setq date-start (point))
190 (re-search-forward "\^M\\|\n" nil t 2)
191 (while (looking-at " \\|\^I")
192 (re-search-forward "\^M\\|\n" nil t))
193 (backward-char 1)
194 (subst-char-in-region date-start
195 (point) ?\^M ?\n t)
196 (add-to-diary-list
197 date (buffer-substring entry-start (point)))))))
198 (setq d (cdr d)))
199 (or entry-found
200 (not diary-list-include-blanks)
201 (setq diary-entries-list
202 (append diary-entries-list
203 (list (list date "")))))
204 (setq date
205 (calendar-gregorian-from-absolute
206 (1+ (calendar-absolute-from-gregorian date))))
207 (setq entry-found nil)))
208 (set-buffer-modified-p diary-modified))
209 (set-syntax-table old-diary-syntax-table))
210 (goto-char (point-min))
211 (run-hooks 'nongregorian-diary-listing-hook
212 'list-diary-entries-hook
213 'diary-display-hook)
214 diary-entries-list))))
216 (defun include-other-diary-files ()
217 "Include the diary entries from other diary files with those of diary-file.
218 This function is suitable for use just before fancy-diary-display as the
219 list-diary-entries-hook; it enables you to use shared diary files together
220 with your own. The files included are specified in the diary-file by lines of
221 the form
222 #include \"filename\"
223 This is recursive; that is, #include directives in diary files thus included
224 are obeyed. You can change the \"#include\" to some other string by
225 changing the variable `diary-include-string'."
226 (goto-char (point-min))
227 (while (re-search-forward
228 (concat
229 "\\(\\`\\|\^M\\|\n\\)"
230 (regexp-quote diary-include-string)
231 " \"\\([^\"]*\\)\"")
232 nil t)
233 (let ((diary-file (substitute-in-file-name
234 (buffer-substring (match-beginning 2) (match-end 2))))
235 (diary-list-include-blanks nil)
236 (list-diary-entries-hook 'include-other-diary-files)
237 (diary-display-hook nil))
238 (if (file-exists-p diary-file)
239 (if (file-readable-p diary-file)
240 (unwind-protect
241 (setq diary-entries-list
242 (append diary-entries-list
243 (list-diary-entries original-date number)))
244 (kill-buffer (get-file-buffer diary-file)))
245 (beep)
246 (message "Can't read included diary file %s" diary-file)
247 (sleep-for 2))
248 (beep)
249 (message "Can't find included diary file %s" diary-file)
250 (sleep-for 2))))
251 (goto-char (point-min)))
253 (defun simple-diary-display ()
254 "Display the diary buffer if there are any relevant entries or holidays."
255 (let* ((holiday-list (if holidays-in-diary-buffer
256 (check-calendar-holidays original-date)))
257 (msg (format "No diary entries for %s %s"
258 (concat date-string (if holiday-list ":" ""))
259 (mapconcat 'identity holiday-list "; "))))
260 (if (or (not diary-entries-list)
261 (and (not (cdr diary-entries-list))
262 (string-equal (car (cdr (car diary-entries-list))) "")))
263 (if (<= (length msg) (screen-width))
264 (message msg)
265 (set-buffer (get-buffer-create holiday-buffer))
266 (setq buffer-read-only nil)
267 (setq mode-line-format
268 (format "--------------------------%s%%-" date-string))
269 (erase-buffer)
270 (insert (mapconcat 'identity holiday-list "\n"))
271 (goto-char (point-min))
272 (set-buffer-modified-p nil)
273 (setq buffer-read-only t)
274 (display-buffer holiday-buffer)
275 (message "No diary entries for %s" date-string))
276 (setq mode-line-format
277 (format "%%*--%sDiary %s %s%s%s%%-"
278 (if holiday-list "" "---------------")
279 (if holiday-list "for" "entries for")
280 date-string
281 (if holiday-list ": " "")
282 (mapconcat 'identity holiday-list "; ")))
283 (display-buffer (get-file-buffer d-file))
284 (message "Preparing diary...done"))))
286 (defun fancy-diary-display ()
287 "Prepare a diary buffer with relevant entries in a fancy, noneditable form.
288 This function is provided for optional use as the `list-diary-entries-hook'."
289 (if (or (not diary-entries-list)
290 (and (not (cdr diary-entries-list))
291 (string-equal (car (cdr (car diary-entries-list))) "")))
292 (let* ((holiday-list (if holidays-in-diary-buffer
293 (check-calendar-holidays original-date)))
294 (msg (format "No diary entries for %s %s"
295 (concat date-string (if holiday-list ":" ""))
296 (mapconcat 'identity holiday-list "; "))))
297 (if (<= (length msg) (screen-width))
298 (message msg)
299 (set-buffer (get-buffer-create holiday-buffer))
300 (setq buffer-read-only nil)
301 (setq mode-line-format
302 (format "--------------------------%s%%-" date-string))
303 (erase-buffer)
304 (insert (mapconcat 'identity holiday-list "\n"))
305 (goto-char (point-min))
306 (set-buffer-modified-p nil)
307 (setq buffer-read-only t)
308 (display-buffer holiday-buffer)
309 (message "No diary entries for %s" date-string)))
310 (save-excursion;; Turn off selective-display in the diary file's buffer.
311 (set-buffer (get-file-buffer (substitute-in-file-name diary-file)))
312 (let ((diary-modified (buffer-modified-p)))
313 (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)
314 (setq selective-display nil)
315 (kill-local-variable 'mode-line-format)
316 (set-buffer-modified-p diary-modified)))
317 (save-excursion;; Prepare the fancy diary buffer.
318 (set-buffer (get-buffer-create fancy-diary-buffer))
319 (setq buffer-read-only nil)
320 (make-local-variable 'mode-line-format)
321 (setq mode-line-format "---------------------------Diary Entries%-")
322 (erase-buffer)
323 (let ((entry-list diary-entries-list)
324 (holiday-list)
325 (holiday-list-last-month 1)
326 (holiday-list-last-year 1)
327 (date (list 0 0 0)))
328 (while entry-list
329 (if (not (calendar-date-equal date (car (car entry-list))))
330 (progn
331 (setq date (car (car entry-list)))
332 (and holidays-in-diary-buffer
333 (calendar-date-compare
334 (list (list holiday-list-last-month
335 (calendar-last-day-of-month
336 holiday-list-last-month
337 holiday-list-last-year)
338 holiday-list-last-year))
339 (list date))
340 ;; We need to get the holidays for the next 3 months.
341 (setq holiday-list-last-month
342 (extract-calendar-month date))
343 (setq holiday-list-last-year
344 (extract-calendar-year date))
345 (increment-calendar-month
346 holiday-list-last-month holiday-list-last-year 1)
347 (setq holiday-list
348 (let ((displayed-month holiday-list-last-month)
349 (displayed-year holiday-list-last-year))
350 (calendar-holiday-list)))
351 (increment-calendar-month
352 holiday-list-last-month holiday-list-last-year 1))
353 (let* ((date-string (calendar-date-string date))
354 (date-holiday-list
355 (let ((h holiday-list)
356 (d))
357 ;; Make a list of all holidays for date.
358 (while h
359 (if (calendar-date-equal date (car (car h)))
360 (setq d (append d (cdr (car h)))))
361 (setq h (cdr h)))
362 d)))
363 (insert (if (= (point) (point-min)) "" ?\n) date-string)
364 (if date-holiday-list (insert ": "))
365 (let ((l (current-column)))
366 (insert (mapconcat 'identity date-holiday-list
367 (concat "\n" (make-string l ? )))))
368 (let ((l (current-column)))
369 (insert ?\n (make-string l ?=) ?\n)))))
370 (if (< 0 (length (car (cdr (car entry-list)))))
371 (insert (car (cdr (car entry-list))) ?\n))
372 (setq entry-list (cdr entry-list))))
373 (set-buffer-modified-p nil)
374 (goto-char (point-min))
375 (setq buffer-read-only t)
376 (display-buffer fancy-diary-buffer)
377 (message "Preparing diary...done"))))
379 (defun print-diary-entries ()
380 "Print a hard copy of the entries visible in the diary window.
381 The hooks given by the variable `print-diary-entries-hook' are called after
382 the temporary buffer of visible diary entries is prepared; it is the hooks
383 that do the actual printing and kill the buffer."
384 (interactive)
385 (let ((diary-buffer (get-file-buffer (substitute-in-file-name diary-file))))
386 (if diary-buffer
387 (let ((temp-buffer (get-buffer-create "*Printable Diary Entries*")))
388 (save-excursion
389 (set-buffer diary-buffer)
390 (copy-to-buffer temp-buffer (point-min) (point-max))
391 (set-buffer temp-buffer)
392 (while (re-search-forward "\^M.*$" nil t)
393 (replace-match ""))
394 (run-hooks 'print-diary-entries-hook)))
395 (error "You don't have a diary buffer!"))))
397 (defun add-diary-heading ()
398 "Add a heading to the diary entries for printing.
399 The heading is formed from the mode line of the diary buffer. This function
400 is used in the default value of the variable `print-diary-entry-hooks'."
401 (save-excursion
402 (let ((heading))
403 (set-buffer diary-buffer)
404 (setq heading mode-line-format)
405 (string-match "%\\*-*\\([^-].*\\)%-$" heading)
406 (setq heading
407 (substring heading (match-beginning 1) (match-end 1)))
408 (set-buffer temp-buffer)
409 (goto-char (point-min))
410 (insert heading "\n"
411 (make-string (length heading) ?=) "\n"))))
413 (defun show-all-diary-entries ()
414 "Show all of the diary entries in the diary-file.
415 This function gets rid of the selective display of the diary-file so that
416 all entries, not just some, are visible. If there is no diary buffer, one
417 is created."
418 (interactive)
419 (let ((d-file (substitute-in-file-name diary-file)))
420 (if (and d-file (file-exists-p d-file))
421 (if (file-readable-p d-file)
422 (save-excursion
423 (let ((diary-buffer (get-file-buffer d-file)))
424 (set-buffer (if diary-buffer
425 diary-buffer
426 (find-file-noselect d-file t)))
427 (let ((buffer-read-only nil)
428 (diary-modified (buffer-modified-p)))
429 (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)
430 (setq selective-display nil)
431 (make-local-variable 'mode-line-format)
432 (setq mode-line-format
433 "%*---------------------------All Diary Entries%-")
434 (display-buffer (current-buffer))
435 (set-buffer-modified-p diary-modified))))
436 (error "Your diary file is not readable!"))
437 (error "You don't have a diary file!"))))
439 (defun diary-name-pattern (string-array &optional fullname)
440 "Convert an STRING-ARRAY, an array of strings to a pattern.
441 The pattern will match any of the strings, either entirely or abbreviated
442 to three characters. An abbreviated form will match with or without a period;
443 If the optional FULLNAME is t, abbreviations will not match, just the full
444 name."
445 (let ((pattern ""))
446 (calendar-for-loop i from 0 to (1- (length string-array)) do
447 (setq pattern
448 (concat
449 pattern
450 (if (string-equal pattern "") "" "\\|")
451 (aref string-array i)
452 (if fullname
454 (concat
455 "\\|"
456 (substring (aref string-array i) 0 3) ".?")))))
457 pattern))
459 (defun mark-diary-entries ()
460 "Mark days in the calendar window that have diary entries.
461 Each entry in diary-file visible in the calendar window is marked. After the
462 entries are marked, the hooks `nongregorian-diary-marking-hook' and
463 `mark-diary-entries-hook' are run."
464 (interactive)
465 (setq mark-diary-entries-in-calendar t)
466 (let ((d-file (substitute-in-file-name diary-file)))
467 (if (and d-file (file-exists-p d-file))
468 (if (file-readable-p d-file)
469 (save-excursion
470 (message "Marking diary entries...")
471 (set-buffer (find-file-noselect d-file t))
472 (let ((d diary-date-forms)
473 (old-diary-syntax-table))
474 (setq old-diary-syntax-table (syntax-table))
475 (set-syntax-table diary-syntax-table)
476 (while d
477 (let*
478 ((date-form (if (equal (car (car d)) 'backup)
479 (cdr (car d))
480 (car d)));; ignore 'backup directive
481 (dayname (diary-name-pattern calendar-day-name-array))
482 (monthname
483 (concat
484 (diary-name-pattern calendar-month-name-array)
485 "\\|\\*"))
486 (month "[0-9]+\\|\\*")
487 (day "[0-9]+\\|\\*")
488 (year "[0-9]+\\|\\*")
489 (l (length date-form))
490 (d-name-pos (- l (length (memq 'dayname date-form))))
491 (d-name-pos (if (/= l d-name-pos) (+ 2 d-name-pos)))
492 (m-name-pos (- l (length (memq 'monthname date-form))))
493 (m-name-pos (if (/= l m-name-pos) (+ 2 m-name-pos)))
494 (d-pos (- l (length (memq 'day date-form))))
495 (d-pos (if (/= l d-pos) (+ 2 d-pos)))
496 (m-pos (- l (length (memq 'month date-form))))
497 (m-pos (if (/= l m-pos) (+ 2 m-pos)))
498 (y-pos (- l (length (memq 'year date-form))))
499 (y-pos (if (/= l y-pos) (+ 2 y-pos)))
500 (regexp
501 (concat
502 "\\(\\`\\|\^M\\|\n\\)\\("
503 (mapconcat 'eval date-form "\\)\\(")
504 "\\)"))
505 (case-fold-search t))
506 (goto-char (point-min))
507 (while (re-search-forward regexp nil t)
508 (let* ((dd-name
509 (if d-name-pos
510 (buffer-substring
511 (match-beginning d-name-pos)
512 (match-end d-name-pos))))
513 (mm-name
514 (if m-name-pos
515 (buffer-substring
516 (match-beginning m-name-pos)
517 (match-end m-name-pos))))
518 (mm (string-to-int
519 (if m-pos
520 (buffer-substring
521 (match-beginning m-pos)
522 (match-end m-pos))
523 "")))
524 (dd (string-to-int
525 (if d-pos
526 (buffer-substring
527 (match-beginning d-pos)
528 (match-end d-pos))
529 "")))
530 (y-str (if y-pos
531 (buffer-substring
532 (match-beginning y-pos)
533 (match-end y-pos))))
534 (yy (if (not y-str)
536 (if (and (= (length y-str) 2)
537 abbreviated-calendar-year)
538 (let* ((current-y
539 (extract-calendar-year
540 (calendar-current-date)))
541 (y (+ (string-to-int y-str)
542 (* 100
543 (/ current-y 100)))))
544 (if (> (- y current-y) 50)
545 (- y 100)
546 (if (> (- current-y y) 50)
547 (+ y 100)
548 y)))
549 (string-to-int y-str)))))
550 (if dd-name
551 (mark-calendar-days-named
552 (cdr (assoc (capitalize (substring dd-name 0 3))
553 (calendar-make-alist
554 calendar-day-name-array
556 '(lambda (x) (substring x 0 3))))))
557 (if mm-name
558 (if (string-equal mm-name "*")
559 (setq mm 0)
560 (setq mm
561 (cdr (assoc
562 (capitalize
563 (substring mm-name 0 3))
564 (calendar-make-alist
565 calendar-month-name-array
567 '(lambda (x) (substring x 0 3)))
568 )))))
569 (mark-calendar-date-pattern mm dd yy))))
570 (setq d (cdr d))))
571 (mark-sexp-diary-entries)
572 (run-hooks 'nongregorian-diary-marking-hook
573 'mark-diary-entries-hook)
574 (set-syntax-table old-diary-syntax-table)
575 (message "Marking diary entries...done")))
576 (error "Your diary file is not readable!"))
577 (error "You don't have a diary file!"))))
579 (defun mark-sexp-diary-entries ()
580 "Mark days in the calendar window that have sexp diary entries.
581 Each entry in diary-file (or included files) visible in the calendar window
582 is marked. See the documentation for the function `list-sexp-diary-entries'."
583 (let* ((sexp-mark (regexp-quote sexp-diary-entry-symbol))
584 (s-entry (concat "\\(\\`\\|\^M\\|\n\\)" sexp-mark "("))
587 (first-date)
588 (last-date))
589 (save-excursion
590 (set-buffer calendar-buffer)
591 (setq m displayed-month)
592 (setq y displayed-year))
593 (increment-calendar-month m y -1)
594 (setq first-date
595 (calendar-absolute-from-gregorian (list m 1 y)))
596 (increment-calendar-month m y 2)
597 (setq last-date
598 (calendar-absolute-from-gregorian
599 (list m (calendar-last-day-of-month m y) y)))
600 (goto-char (point-min))
601 (while (re-search-forward s-entry nil t)
602 (backward-char 1)
603 (let ((sexp-start (point))
604 (sexp)
605 (entry)
606 (entry-start)
607 (line-start))
608 (forward-sexp)
609 (setq sexp (buffer-substring sexp-start (point)))
610 (save-excursion
611 (re-search-backward "\^M\\|\n\\|\\`")
612 (setq line-start (point)))
613 (forward-char 1)
614 (if (and (or (char-equal (preceding-char) ?\^M)
615 (char-equal (preceding-char) ?\n))
616 (not (looking-at " \\|\^I")))
617 (progn;; Diary entry consists only of the sexp
618 (backward-char 1)
619 (setq entry ""))
620 (setq entry-start (point))
621 (re-search-forward "\^M\\|\n" nil t)
622 (while (looking-at " \\|\^I")
623 (re-search-forward "\^M\\|\n" nil t))
624 (backward-char 1)
625 (setq entry (buffer-substring entry-start (point)))
626 (while (string-match "[\^M]" entry)
627 (aset entry (match-beginning 0) ?\n )))
628 (calendar-for-loop date from first-date to last-date do
629 (if (diary-sexp-entry sexp entry
630 (calendar-gregorian-from-absolute date))
631 (mark-visible-calendar-date
632 (calendar-gregorian-from-absolute date))))))))
634 (defun mark-included-diary-files ()
635 "Mark the diary entries from other diary files with those of diary-file.
636 This function is suitable for use as the mark-diary-entries-hook; it enables
637 you to use shared diary files together with your own. The files included are
638 specified in the diary-file by lines of the form
639 #include \"filename\"
640 This is recursive; that is, #include directives in diary files thus included
641 are obeyed. You can change the \"#include\" to some other string by
642 changing the variable `diary-include-string'."
643 (goto-char (point-min))
644 (while (re-search-forward
645 (concat
646 "\\(\\`\\|\^M\\|\n\\)"
647 (regexp-quote diary-include-string)
648 " \"\\([^\"]*\\)\"")
649 nil t)
650 (let ((diary-file (substitute-in-file-name
651 (buffer-substring (match-beginning 2) (match-end 2))))
652 (mark-diary-entries-hook 'mark-included-diary-files))
653 (if (file-exists-p diary-file)
654 (if (file-readable-p diary-file)
655 (progn
656 (mark-diary-entries)
657 (kill-buffer (get-file-buffer diary-file)))
658 (beep)
659 (message "Can't read included diary file %s" diary-file)
660 (sleep-for 2))
661 (beep)
662 (message "Can't find included diary file %s" diary-file)
663 (sleep-for 2))))
664 (goto-char (point-min)))
666 (defun mark-calendar-days-named (dayname)
667 "Mark all dates in the calendar window that are day DAYNAME of the week.
668 0 means all Sundays, 1 means all Mondays, and so on."
669 (save-excursion
670 (set-buffer calendar-buffer)
671 (let ((prev-month displayed-month)
672 (prev-year displayed-year)
673 (succ-month displayed-month)
674 (succ-year displayed-year)
675 (last-day)
676 (day))
677 (increment-calendar-month succ-month succ-year 1)
678 (increment-calendar-month prev-month prev-year -1)
679 (setq day (calendar-absolute-from-gregorian
680 (calendar-nth-named-day 1 dayname prev-month prev-year)))
681 (setq last-day (calendar-absolute-from-gregorian
682 (calendar-nth-named-day -1 dayname succ-month succ-year)))
683 (while (<= day last-day)
684 (mark-visible-calendar-date (calendar-gregorian-from-absolute day))
685 (setq day (+ day 7))))))
687 (defun mark-calendar-date-pattern (month day year)
688 "Mark all dates in the calendar window that conform to MONTH/DAY/YEAR.
689 A value of 0 in any position is a wild-card."
690 (save-excursion
691 (set-buffer calendar-buffer)
692 (let ((m displayed-month)
693 (y displayed-year))
694 (increment-calendar-month m y -1)
695 (calendar-for-loop i from 0 to 2 do
696 (mark-calendar-month m y month day year)
697 (increment-calendar-month m y 1)))))
699 (defun mark-calendar-month (month year p-month p-day p-year)
700 "Mark dates in the MONTH/YEAR that conform to pattern P-MONTH/P_DAY/P-YEAR.
701 A value of 0 in any position of the pattern is a wild-card."
702 (if (or (and (= month p-month)
703 (or (= p-year 0) (= year p-year)))
704 (and (= p-month 0)
705 (or (= p-year 0) (= year p-year))))
706 (if (= p-day 0)
707 (calendar-for-loop
708 i from 1 to (calendar-last-day-of-month month year) do
709 (mark-visible-calendar-date (list month i year)))
710 (mark-visible-calendar-date (list month p-day year)))))
712 (defun diary-entry-compare (e1 e2)
713 "Returns t if E1 is earlier than E2."
714 (or (calendar-date-compare e1 e2)
715 (and (calendar-date-equal (car e1) (car e2))
716 (< (diary-entry-time (car (cdr e1)))
717 (diary-entry-time (car (cdr e2)))))))
719 (defun diary-entry-time (s)
720 "Time at the beginning of the string S in a military-style integer.
721 For example, returns 1325 for 1:25pm. Returns -9999 if no time is recognized.
722 The recognized forms are XXXX or X:XX or XX:XX (military time), XXam or XXpm,
723 and XX:XXam or XX:XXpm."
724 (cond ((string-match;; Military time
725 "^ *\\([0-9]?[0-9]\\):?\\([0-9][0-9]\\)\\(\\>\\|[^ap]\\)" s)
726 (+ (* 100 (string-to-int
727 (substring s (match-beginning 1) (match-end 1))))
728 (string-to-int (substring s (match-beginning 2) (match-end 2)))))
729 ((string-match;; Hour only XXam or XXpm
730 "^ *\\([0-9]?[0-9]\\)\\([ap]\\)m\\>" s)
731 (+ (* 100 (% (string-to-int
732 (substring s (match-beginning 1) (match-end 1)))
733 12))
734 (if (string-equal "a"
735 (substring s (match-beginning 2) (match-end 2)))
736 0 1200)))
737 ((string-match;; Hour and minute XX:XXam or XX:XXpm
738 "^ *\\([0-9]?[0-9]\\):\\([0-9][0-9]\\)\\([ap]\\)m\\>" s)
739 (+ (* 100 (% (string-to-int
740 (substring s (match-beginning 1) (match-end 1)))
741 12))
742 (string-to-int (substring s (match-beginning 2) (match-end 2)))
743 (if (string-equal "a"
744 (substring s (match-beginning 3) (match-end 3)))
745 0 1200)))
746 (t -9999)));; Unrecognizable
748 (defun list-hebrew-diary-entries ()
749 "Add any Hebrew date entries from the diary-file to diary-entries-list.
750 Hebrew date diary entries must be prefaced by a hebrew-diary-entry-symbol
751 \(normally an `H'\). The same diary-date-forms govern the style of the Hebrew
752 calendar entries, except that the Hebrew month names must be spelled in full.
753 The Hebrew months are numbered from 1 to 13 with Nisan being 1, 12 being
754 Adar I and 13 being Adar II; you must use `Adar I' if you want Adar of a
755 common Hebrew year. If a Hebrew date diary entry begins with a
756 diary-nonmarking-symbol the entry will appear in the diary listing, but will
757 not be marked in the calendar. This function is provided for use with the
758 nongregorian-diary-listing-hook."
759 (if (< 0 number)
760 (let ((buffer-read-only nil)
761 (diary-modified (buffer-modified-p))
762 (gdate original-date)
763 (mark (regexp-quote diary-nonmarking-symbol)))
764 (calendar-for-loop i from 1 to number do
765 (let* ((d diary-date-forms)
766 (hdate (calendar-hebrew-from-absolute
767 (calendar-absolute-from-gregorian gdate)))
768 (month (extract-calendar-month hdate))
769 (day (extract-calendar-day hdate))
770 (year (extract-calendar-year hdate)))
771 (while d
772 (let*
773 ((date-form (if (equal (car (car d)) 'backup)
774 (cdr (car d))
775 (car d)))
776 (backup (equal (car (car d)) 'backup))
777 (dayname
778 (concat
779 (calendar-day-name gdate) "\\|"
780 (substring (calendar-day-name gdate) 0 3) ".?"))
781 (calendar-month-name-array
782 calendar-hebrew-month-name-array-leap-year)
783 (monthname
784 (concat
785 "\\*\\|"
786 (calendar-month-name month)))
787 (month (concat "\\*\\|0*" (int-to-string month)))
788 (day (concat "\\*\\|0*" (int-to-string day)))
789 (year
790 (concat
791 "\\*\\|0*" (int-to-string year)
792 (if abbreviated-calendar-year
793 (concat "\\|" (int-to-string (% year 100)))
794 "")))
795 (regexp
796 (concat
797 "\\(\\`\\|\^M\\|\n\\)" mark "?"
798 (regexp-quote hebrew-diary-entry-symbol)
799 "\\("
800 (mapconcat 'eval date-form "\\)\\(")
801 "\\)"))
802 (case-fold-search t))
803 (goto-char (point-min))
804 (while (re-search-forward regexp nil t)
805 (if backup (re-search-backward "\\<" nil t))
806 (if (and (or (char-equal (preceding-char) ?\^M)
807 (char-equal (preceding-char) ?\n))
808 (not (looking-at " \\|\^I")))
809 ;; Diary entry that consists only of date.
810 (backward-char 1)
811 ;; Found a nonempty diary entry--make it visible and
812 ;; add it to the list.
813 (let ((entry-start (point))
814 (date-start))
815 (re-search-backward "\^M\\|\n\\|\\`")
816 (setq date-start (point))
817 (re-search-forward "\^M\\|\n" nil t 2)
818 (while (looking-at " \\|\^I")
819 (re-search-forward "\^M\\|\n" nil t))
820 (backward-char 1)
821 (subst-char-in-region date-start (point) ?\^M ?\n t)
822 (add-to-diary-list
823 gdate (buffer-substring entry-start (point)))))))
824 (setq d (cdr d))))
825 (setq gdate
826 (calendar-gregorian-from-absolute
827 (1+ (calendar-absolute-from-gregorian gdate)))))
828 (set-buffer-modified-p diary-modified))
829 (goto-char (point-min))))
831 (defun mark-hebrew-diary-entries ()
832 "Mark days in the calendar window that have Hebrew date diary entries.
833 Each entry in diary-file (or included files) visible in the calendar window
834 is marked. Hebrew date entries are prefaced by a hebrew-diary-entry-symbol
835 \(normally an `H'\). The same diary-date-forms govern the style of the Hebrew
836 calendar entries, except that the Hebrew month names must be spelled in full.
837 The Hebrew months are numbered from 1 to 13 with Nisan being 1, 12 being
838 Adar I and 13 being Adar II; you must use `Adar I' if you want Adar of a
839 common Hebrew year. Hebrew date diary entries that begin with a
840 diary-nonmarking symbol will not be marked in the calendar. This function
841 is provided for use as part of the nongregorian-diary-marking-hook."
842 (let ((d diary-date-forms))
843 (while d
844 (let*
845 ((date-form (if (equal (car (car d)) 'backup)
846 (cdr (car d))
847 (car d)));; ignore 'backup directive
848 (dayname (diary-name-pattern calendar-day-name-array))
849 (monthname
850 (concat
851 (diary-name-pattern calendar-hebrew-month-name-array-leap-year t)
852 "\\|\\*"))
853 (month "[0-9]+\\|\\*")
854 (day "[0-9]+\\|\\*")
855 (year "[0-9]+\\|\\*")
856 (l (length date-form))
857 (d-name-pos (- l (length (memq 'dayname date-form))))
858 (d-name-pos (if (/= l d-name-pos) (+ 2 d-name-pos)))
859 (m-name-pos (- l (length (memq 'monthname date-form))))
860 (m-name-pos (if (/= l m-name-pos) (+ 2 m-name-pos)))
861 (d-pos (- l (length (memq 'day date-form))))
862 (d-pos (if (/= l d-pos) (+ 2 d-pos)))
863 (m-pos (- l (length (memq 'month date-form))))
864 (m-pos (if (/= l m-pos) (+ 2 m-pos)))
865 (y-pos (- l (length (memq 'year date-form))))
866 (y-pos (if (/= l y-pos) (+ 2 y-pos)))
867 (regexp
868 (concat
869 "\\(\\`\\|\^M\\|\n\\)"
870 (regexp-quote hebrew-diary-entry-symbol)
871 "\\("
872 (mapconcat 'eval date-form "\\)\\(")
873 "\\)"))
874 (case-fold-search t))
875 (goto-char (point-min))
876 (while (re-search-forward regexp nil t)
877 (let* ((dd-name
878 (if d-name-pos
879 (buffer-substring
880 (match-beginning d-name-pos)
881 (match-end d-name-pos))))
882 (mm-name
883 (if m-name-pos
884 (buffer-substring
885 (match-beginning m-name-pos)
886 (match-end m-name-pos))))
887 (mm (string-to-int
888 (if m-pos
889 (buffer-substring
890 (match-beginning m-pos)
891 (match-end m-pos))
892 "")))
893 (dd (string-to-int
894 (if d-pos
895 (buffer-substring
896 (match-beginning d-pos)
897 (match-end d-pos))
898 "")))
899 (y-str (if y-pos
900 (buffer-substring
901 (match-beginning y-pos)
902 (match-end y-pos))))
903 (yy (if (not y-str)
905 (if (and (= (length y-str) 2)
906 abbreviated-calendar-year)
907 (let* ((current-y
908 (extract-calendar-year
909 (calendar-hebrew-from-absolute
910 (calendar-absolute-from-gregorian
911 (calendar-current-date)))))
912 (y (+ (string-to-int y-str)
913 (* 100 (/ current-y 100)))))
914 (if (> (- y current-y) 50)
915 (- y 100)
916 (if (> (- current-y y) 50)
917 (+ y 100)
918 y)))
919 (string-to-int y-str)))))
920 (if dd-name
921 (mark-calendar-days-named
922 (cdr (assoc (capitalize (substring dd-name 0 3))
923 (calendar-make-alist
924 calendar-day-name-array
926 '(lambda (x) (substring x 0 3))))))
927 (if mm-name
928 (if (string-equal mm-name "*")
929 (setq mm 0)
930 (setq
932 (cdr
933 (assoc
934 (capitalize mm-name)
935 (calendar-make-alist
936 calendar-hebrew-month-name-array-leap-year))))))
937 (mark-hebrew-calendar-date-pattern mm dd yy)))))
938 (setq d (cdr d)))))
940 (defun mark-hebrew-calendar-date-pattern (month day year)
941 "Mark all dates in the calendar window that conform to the Hebrew date
942 MONTH/DAY/YEAR. A value of 0 in any position is a wild-card."
943 (save-excursion
944 (set-buffer calendar-buffer)
945 (if (and (/= 0 month) (/= 0 day))
946 (if (/= 0 year)
947 ;; Fully specified Hebrew date.
948 (let ((date (calendar-gregorian-from-absolute
949 (calendar-absolute-from-hebrew
950 (list month day year)))))
951 (if (calendar-date-is-visible-p date)
952 (mark-visible-calendar-date date)))
953 ;; Month and day in any year--this taken from the holiday stuff.
954 (if (memq displayed-month;; This test is only to speed things up a
955 (list ;; bit; it works fine without the test too.
956 (if (< 11 month) (- month 11) (+ month 1))
957 (if (< 10 month) (- month 10) (+ month 2))
958 (if (< 9 month) (- month 9) (+ month 3))
959 (if (< 8 month) (- month 8) (+ month 4))
960 (if (< 7 month) (- month 7) (+ month 5))))
961 (let ((m1 displayed-month)
962 (y1 displayed-year)
963 (m2 displayed-month)
964 (y2 displayed-year)
965 (year))
966 (increment-calendar-month m1 y1 -1)
967 (increment-calendar-month m2 y2 1)
968 (let* ((start-date (calendar-absolute-from-gregorian
969 (list m1 1 y1)))
970 (end-date (calendar-absolute-from-gregorian
971 (list m2
972 (calendar-last-day-of-month m2 y2)
973 y2)))
974 (hebrew-start
975 (calendar-hebrew-from-absolute start-date))
976 (hebrew-end (calendar-hebrew-from-absolute end-date))
977 (hebrew-y1 (extract-calendar-year hebrew-start))
978 (hebrew-y2 (extract-calendar-year hebrew-end)))
979 (setq year (if (< 6 month) hebrew-y2 hebrew-y1))
980 (let ((date (calendar-gregorian-from-absolute
981 (calendar-absolute-from-hebrew
982 (list month day year)))))
983 (if (calendar-date-is-visible-p date)
984 (mark-visible-calendar-date date)))))))
985 ;; Not one of the simple cases--check all visible dates for match.
986 ;; Actually, the following code takes care of ALL of the cases, but
987 ;; it's much too slow to be used for the simple (common) cases.
988 (let ((m displayed-month)
989 (y displayed-year)
990 (first-date)
991 (last-date))
992 (increment-calendar-month m y -1)
993 (setq first-date
994 (calendar-absolute-from-gregorian
995 (list m 1 y)))
996 (increment-calendar-month m y 2)
997 (setq last-date
998 (calendar-absolute-from-gregorian
999 (list m (calendar-last-day-of-month m y) y)))
1000 (calendar-for-loop date from first-date to last-date do
1001 (let* ((h-date (calendar-hebrew-from-absolute date))
1002 (h-month (extract-calendar-month h-date))
1003 (h-day (extract-calendar-day h-date))
1004 (h-year (extract-calendar-year h-date)))
1005 (and (or (zerop month)
1006 (= month h-month))
1007 (or (zerop day)
1008 (= day h-day))
1009 (or (zerop year)
1010 (= year h-year))
1011 (mark-visible-calendar-date
1012 (calendar-gregorian-from-absolute date)))))))))
1014 (defun list-sexp-diary-entries (date)
1015 "Add any sexp entries for DATE from the diary-file to diary-entries-list
1016 and make them visible in the diary file. Returns t if any entries were found.
1018 Sexp diary entries must be prefaced by a sexp-diary-entry-symbol (normally
1019 `%%'). The form of a sexp diary entry is
1021 %%(SEXP) ENTRY
1023 Both ENTRY and DATE are globally available when the SEXP is evaluated. If the
1024 SEXP yields the value nil, the diary entry does not apply. If it yields a
1025 non-nil value, ENTRY will be taken to apply to DATE; if the non-nil value is a
1026 string, that string will be the diary entry in the fancy diary display.
1028 For example, the following diary entry will apply to the 21st of the month
1029 if it is a weekday and the Friday before if the 21st is on a weekend:
1031 &%%(let ((dayname (calendar-day-of-week date))
1032 (day (extract-calendar-day date)))
1034 (and (= day 21) (memq dayname '(1 2 3 4 5)))
1035 (and (memq day '(19 20)) (= dayname 5)))
1036 ) UIUC pay checks deposited
1038 A number of built-in functions are available for this type of diary entry:
1040 %%(diary-float MONTH DAYNAME N) text
1041 Entry will appear on the Nth DAYNAME of MONTH.
1042 (DAYNAME=0 means Sunday, 1 means Monday, and so on;
1043 if N is negative it counts backward from the end of
1044 the month. MONTH can be a list of months, a single
1045 month, or t to specify all months.
1047 %%(diary-block M1 D1 Y1 M2 D2 Y2) text
1048 Entry will appear on dates between M1/D1/Y1 and M2/D2/Y2,
1049 inclusive. (If `european-calendar-style' is t, the
1050 order of the parameters should be changed to D1, M1, Y1,
1051 D2, M2, Y2.)
1053 %%(diary-anniversary MONTH DAY YEAR) text
1054 Entry will appear on anniversary dates of MONTH DAY, YEAR.
1055 (If `european-calendar-style' is t, the order of the
1056 parameters should be changed to DAY, MONTH, YEAR.) Text
1057 can contain %d or %d%s; %d will be replaced by the number
1058 of years since the MONTH DAY, YEAR and %s will be replaced
1059 by the ordinal ending of that number (that is, `st', `nd',
1060 `rd' or `th', as appropriate. The anniversary of February
1061 29 is considered to be March 1 in a non-leap year.
1063 %%(diary-cyclic N MONTH DAY YEAR) text
1064 Entry will appear every N days, starting MONTH DAY, YEAR.
1065 (If `european-calendar-style' is t, the order of the
1066 parameters should be changed to N, DAY, MONTH, YEAR.) Text
1067 can contain %d or %d%s; %d will be replaced by the number
1068 of repetitions since the MONTH DAY, YEAR and %s will
1069 be replaced by the ordinal ending of that number (that is,
1070 `st', `nd', `rd' or `th', as appropriate.
1072 %%(diary-day-of-year)
1073 Diary entries giving the day of the year and the number of
1074 days remaining in the year will be made every day. Note
1075 that since there is no text, it makes sense only if the
1076 fancy diary display is used.
1078 %%(diary-iso-date)
1079 Diary entries giving the corresponding ISO commercial date
1080 will be made every day. Note that since there is no text,
1081 it makes sense only if the fancy diary display is used.
1083 %%(diary-french-date)
1084 Diary entries giving the corresponding French Revolutionary
1085 date will be made every day. Note that since there is no
1086 text, it makes sense only if the fancy diary display is used.
1088 %%(diary-islamic-date)
1089 Diary entries giving the corresponding Islamic date will be
1090 made every day. Note that since there is no text, it
1091 makes sense only if the fancy diary display is used.
1093 %%(diary-hebrew-date)
1094 Diary entries giving the corresponding Hebrew date will be
1095 made every day. Note that since there is no text, it
1096 makes sense only if the fancy diary display is used.
1098 %%(diary-yahrzeit MONTH DAY YEAR) text
1099 Text is assumed to be the name of the person; the date is
1100 the date of death on the *civil* calendar. The diary entry
1101 will appear on the proper Hebrew-date anniversary and on the
1102 day before. (If `european-calendar-style' is t, the order
1103 of the parameters should be changed to DAY, MONTH, YEAR.)
1105 %%(diary-rosh-hodesh)
1106 Diary entries will be made on the dates of Rosh Hodesh on
1107 the Hebrew calendar. Note that since there is no text, it
1108 makes sense only if the fancy diary display is used.
1110 %%(diary-parasha)
1111 Diary entries giving the weekly parasha will be made on
1112 every Saturday. Note that since there is no text, it
1113 makes sense only if the fancy diary display is used.
1115 %%(diary-omer)
1116 Diary entries giving the omer count will be made every day
1117 from Passover to Shavuoth. Note that since there is no text,
1118 it makes sense only if the fancy diary display is used.
1120 Marking these entries is *extremely* time consuming, so these entries are
1121 best if they are nonmarking."
1122 (let* ((mark (regexp-quote diary-nonmarking-symbol))
1123 (sexp-mark (regexp-quote sexp-diary-entry-symbol))
1124 (s-entry (concat "\\(\\`\\|\^M\\|\n\\)" mark "?" sexp-mark "("))
1125 (entry-found))
1126 (goto-char (point-min))
1127 (while (re-search-forward s-entry nil t)
1128 (backward-char 1)
1129 (let ((sexp-start (point))
1130 (sexp)
1131 (entry)
1132 (entry-start)
1133 (line-start))
1134 (forward-sexp)
1135 (setq sexp (buffer-substring sexp-start (point)))
1136 (save-excursion
1137 (re-search-backward "\^M\\|\n\\|\\`")
1138 (setq line-start (point)))
1139 (forward-char 1)
1140 (if (and (or (char-equal (preceding-char) ?\^M)
1141 (char-equal (preceding-char) ?\n))
1142 (not (looking-at " \\|\^I")))
1143 (progn;; Diary entry consists only of the sexp
1144 (backward-char 1)
1145 (setq entry ""))
1146 (setq entry-start (point))
1147 (re-search-forward "\^M\\|\n" nil t)
1148 (while (looking-at " \\|\^I")
1149 (re-search-forward "\^M\\|\n" nil t))
1150 (backward-char 1)
1151 (setq entry (buffer-substring entry-start (point)))
1152 (while (string-match "[\^M]" entry)
1153 (aset entry (match-beginning 0) ?\n )))
1154 (let ((diary-entry (diary-sexp-entry sexp entry date)))
1155 (if diary-entry
1156 (subst-char-in-region line-start (point) ?\^M ?\n t))
1157 (add-to-diary-list date diary-entry)
1158 (setq entry-found (or entry-found diary-entry)))))
1159 entry-found))
1161 (defun diary-sexp-entry (sexp entry date)
1162 "Process a SEXP diary ENTRY for DATE."
1163 (let ((result (condition-case nil
1164 (eval (car (read-from-string sexp)))
1165 (error
1166 (beep)
1167 (message "Bad sexp at line %d in %s: %s"
1168 (save-excursion
1169 (save-restriction
1170 (narrow-to-region 1 (point))
1171 (goto-char (point-min))
1172 (let ((lines 1))
1173 (while (re-search-forward "\n\\|\^M" nil t)
1174 (setq lines (1+ lines)))
1175 lines)))
1176 diary-file sexp)
1177 (sleep-for 2)))))
1178 (if (stringp result)
1179 result
1180 (if result
1181 entry
1182 nil))))
1184 (defun diary-block (m1 d1 y1 m2 d2 y2)
1185 "Block diary entry--entry applies if date is between two dates. Order of
1186 the parameters is M1, D1, Y1, M2, D2, Y2 `european-calendar-style' is nil, and
1187 D1, M1, Y1, D2, M2, Y2 if `european-calendar-style' is t."
1188 (let ((date1 (calendar-absolute-from-gregorian
1189 (if european-calendar-style
1190 (list d1 m1 y1)
1191 (list m1 d1 y1))))
1192 (date2 (calendar-absolute-from-gregorian
1193 (if european-calendar-style
1194 (list d2 m2 y2)
1195 (list m2 d2 y2))))
1196 (d (calendar-absolute-from-gregorian date)))
1197 (if (and (<= date1 d) (<= d date2))
1198 entry)))
1200 (defun diary-float (month dayname n)
1201 "Floating diary entry--entry applies if date is the nth dayname of month.
1202 Parameters are MONTH, DAYNAME, N. MONTH can be a list of months, the constant
1203 t, or an integer. The constant t means all months. If N is negative, count
1204 backward from the end of the month."
1205 (let ((m (extract-calendar-month date))
1206 (y (extract-calendar-year date)))
1207 (if (and
1208 (or (and (listp month) (memq m month))
1209 (equal m month)
1210 (eq month t))
1211 (calendar-date-equal date (calendar-nth-named-day n dayname m y)))
1212 entry)))
1214 (defun diary-anniversary (month day year)
1215 "Anniversary diary entry--entry applies if date is the anniversary of
1216 MONTH, DAY, YEAR if `european-calendar-style' is nil, and DAY, MONTH, YEAR
1217 if `european-calendar-style' is t. Diary entry can contain `%d' or `%d%s'; the
1218 %d will be replaced by the number of years since the MONTH DAY, YEAR and the
1219 %s will be replaced by the ordinal ending of that number (that is, `st', `nd',
1220 `rd' or `th', as appropriate. The anniversary of February 29 is considered
1221 to be March 1 in non-leap years."
1222 (let* ((d (if european-calendar-style
1223 month
1224 day))
1225 (m (if european-calendar-style
1227 month))
1228 (y (extract-calendar-year date))
1229 (diff (- y year)))
1230 (if (and (= m 2) (= d 29) (not (calendar-leap-year-p y)))
1231 (setq m 3
1232 d 1))
1233 (if (and (> diff 0) (calendar-date-equal (list m d y) date))
1234 (format entry diff (diary-ordinal-suffix diff)))))
1236 (defun diary-cyclic (n month day year)
1237 "Cycle diary entry--entry applies every N days starting at MONTH, DAY, YEAR.
1238 If `european-calendar-style' is t, parameters are N, DAY, MONTH, YEAR.
1239 ENTRY can contain `%d' or `%d%s'; the %d will be replaced by the number of
1240 years since the MONTH DAY, YEAR and the %s will be replaced by the ordinal
1241 ending of that number (that is, `st', `nd', `rd' or `th', as appropriate."
1242 (let* ((d (if european-calendar-style
1243 month
1244 day))
1245 (m (if european-calendar-style
1247 month))
1248 (diff (- (calendar-absolute-from-gregorian date)
1249 (calendar-absolute-from-gregorian
1250 (list m d year))))
1251 (cycle (/ diff n)))
1252 (if (and (>= diff 0) (zerop (% diff n)))
1253 (format entry cycle (diary-ordinal-suffix cycle)))))
1255 (defun diary-ordinal-suffix (n)
1256 "Ordinal suffix for N. (That is, `st', `nd', `rd', or `th', as appropriate.)"
1257 (if (or (and (< 9 n) (< n 20))
1258 (memq (% n 10) '(4 5 6 7 8 9 0)))
1259 "th"
1260 (aref ["th" "st" "nd" "rd"] (% n 10))))
1262 (defun diary-day-of-year ()
1263 "Day of year and number of days remaining in the year of date diary entry."
1264 (let* ((year (extract-calendar-year date))
1265 (day (calendar-day-number date))
1266 (days-remaining (- (calendar-day-number (list 12 31 year)) day)))
1267 (format "Day %d of %d; %d day%s remaining in the year"
1268 day year days-remaining (if (= days-remaining 1) "" "s"))))
1270 (defun diary-iso-date ()
1271 "ISO calendar equivalent of date diary entry."
1272 (let ((day (% (calendar-absolute-from-gregorian date) 7))
1273 (iso-date (calendar-iso-from-absolute
1274 (calendar-absolute-from-gregorian date))))
1275 (format "ISO date: Day %s of week %d of %d."
1276 (if (zerop day) 7 day)
1277 (extract-calendar-month iso-date)
1278 (extract-calendar-year iso-date))))
1280 (defun diary-islamic-date ()
1281 "Islamic calendar equivalent of date diary entry."
1282 (let* ((calendar-date-display-form
1283 (if european-calendar-style
1284 '(day " " monthname " " year)
1285 '(monthname " " day ", " year)))
1286 (i-date (calendar-islamic-from-absolute
1287 (calendar-absolute-from-gregorian date)))
1288 (calendar-month-name-array calendar-islamic-month-name-array))
1289 (if (>= (extract-calendar-year i-date) 1)
1290 (format "Islamic date: %s" (calendar-date-string i-date)))))
1292 (defun diary-hebrew-date ()
1293 "Hebrew calendar equivalent of date diary entry."
1294 (let* ((calendar-date-display-form
1295 (if european-calendar-style
1296 '(day " " monthname " " year)
1297 '(monthname " " day ", " year)))
1298 (h-date (calendar-hebrew-from-absolute
1299 (calendar-absolute-from-gregorian date)))
1300 (calendar-month-name-array
1301 (if (hebrew-calendar-leap-year-p
1302 (extract-calendar-year h-date))
1303 calendar-hebrew-month-name-array-leap-year
1304 calendar-hebrew-month-name-array-common-year)))
1305 (format "Hebrew date: %s" (calendar-date-string h-date))))
1307 (defun diary-french-date ()
1308 "French calendar equivalent of date diary entry."
1309 (let* ((french-date (calendar-french-from-absolute
1310 (calendar-absolute-from-gregorian date)))
1311 (y (extract-calendar-year french-date))
1312 (m (extract-calendar-month french-date))
1313 (d (extract-calendar-day french-date)))
1314 (if (> y 0)
1315 (if (= m 13)
1316 (format "Jour %s de l'Annee %d de la Revolution"
1317 (aref french-calendar-special-days-array (1- d))
1319 (format "Decade %s, %s de %s de l'Annee %d de la Revolution"
1320 (make-string (1+ (/ (1- d) 10)) ?I)
1321 (aref french-calendar-day-name-array (% (1- d) 10))
1322 (aref french-calendar-month-name-array (1- m))
1323 y)))))
1325 (defun diary-omer ()
1326 "Omer count diary entry--entry applies if date is within 50 days after
1327 Passover."
1328 (let* ((passover
1329 (calendar-absolute-from-hebrew
1330 (list 1 15 (+ (extract-calendar-year date) 3760))))
1331 (omer (- (calendar-absolute-from-gregorian date) passover))
1332 (week (/ omer 7))
1333 (day (% omer 7)))
1334 (if (and (> omer 0) (< omer 50))
1335 (format "Day %d%s of the omer (until sunset)"
1336 omer
1337 (if (zerop week)
1339 (format ", that is, %d week%s%s"
1340 week
1341 (if (= week 1) "" "s")
1342 (if (zerop day)
1344 (format " and %d day%s"
1345 day (if (= day 1) "" "s")))))))))
1347 (defun diary-yahrzeit (death-month death-day death-year)
1348 "Yahrzeit diary entry--entry applies if date is yahrzeit or the day before.
1349 Parameters are DEATH-MONTH, DEATH-DAY, DEATH-YEAR; the diary entry is assumed
1350 to be the name of the person. Date of death is on the *civil* calendar;
1351 although the date of death is specified by the civil calendar, the proper
1352 Hebrew calendar yahrzeit is determined. If european-calendar-style is t, the
1353 order of the parameters is changed to DEATH-DAY, DEATH-MONTH, DEATH-YEAR."
1354 (let* ((h-date (calendar-hebrew-from-absolute
1355 (calendar-absolute-from-gregorian
1356 (if european-calendar-style
1357 (list death-day death-month death-year)
1358 (list death-month death-day death-year)))))
1359 (h-month (extract-calendar-month h-date))
1360 (h-day (extract-calendar-day h-date))
1361 (h-year (extract-calendar-year h-date))
1362 (d (calendar-absolute-from-gregorian date))
1363 (yr (extract-calendar-year (calendar-hebrew-from-absolute d)))
1364 (diff (- yr h-year))
1365 (y (hebrew-calendar-yahrzeit h-date yr)))
1366 (if (and (> diff 0) (or (= y d) (= y (1+ d))))
1367 (format "Yahrzeit of %s%s: %d%s anniversary"
1368 entry
1369 (if (= y d) "" " (evening)")
1370 diff
1371 (cond ((= (% diff 10) 1) "st")
1372 ((= (% diff 10) 2) "nd")
1373 ((= (% diff 10) 3) "rd")
1374 (t "th"))))))
1376 (defun diary-rosh-hodesh ()
1377 "Rosh Hodesh diary entry--entry applies if date is Rosh Hodesh or the
1378 Saturday before."
1379 (let* ((d (calendar-absolute-from-gregorian date))
1380 (h-date (calendar-hebrew-from-absolute d))
1381 (h-month (extract-calendar-month h-date))
1382 (h-day (extract-calendar-day h-date))
1383 (h-year (extract-calendar-year h-date))
1384 (leap-year (hebrew-calendar-leap-year-p h-year))
1385 (last-day (hebrew-calendar-last-day-of-month h-month h-year))
1386 (h-month-names
1387 (if leap-year
1388 calendar-hebrew-month-name-array-leap-year
1389 calendar-hebrew-month-name-array-common-year))
1390 (this-month (aref h-month-names (1- h-month)))
1391 (h-yesterday (extract-calendar-day
1392 (calendar-hebrew-from-absolute (1- d)))))
1393 (if (or (= h-day 30) (and (= h-day 1) (/= h-month 7)))
1394 (format
1395 "Rosh Hodesh %s"
1396 (if (= h-day 30)
1397 (format
1398 "%s (first day)"
1399 ;; next month must be in the same year since this
1400 ;; month can't be the last month of the year since
1401 ;; it has 30 days
1402 (aref h-month-names h-month))
1403 (if (= h-yesterday 30)
1404 (format "%s (second day)" this-month)
1405 this-month)))
1406 (if (= (mod d 7) 6);; Saturday--check for Shabbat Mevarhim
1407 (cond ((and (> h-day 22) (/= h-month 6) (= 29 last-day))
1408 (format "Mevarhim Rosh Hodesh %s (%s)"
1409 (aref h-month-names
1410 (if (= h-month
1411 (hebrew-calendar-last-month-of-year
1412 h-year))
1413 0 h-month))
1414 (aref calendar-day-name-array (- 29 h-day))))
1415 ((and (< h-day 30) (> h-day 22) (= 30 last-day))
1416 (format "Mevarhim Rosh Hodesh %s (%s-%s)"
1417 (aref h-month-names h-month)
1418 (aref calendar-day-name-array (- 29 h-day))
1419 (aref calendar-day-name-array
1420 (mod (- 30 h-day) 7)))))))))
1422 (defun diary-parasha ()
1423 "Parasha diary entry--entry applies if date is a Saturday."
1424 (let ((d (calendar-absolute-from-gregorian date)))
1425 (if (= (% d 7) 6);; Saturday
1426 (let*
1427 ((h-year (extract-calendar-year
1428 (calendar-hebrew-from-absolute d)))
1429 (rosh-hashannah
1430 (calendar-absolute-from-hebrew (list 7 1 h-year)))
1431 (passover
1432 (calendar-absolute-from-hebrew (list 1 15 h-year)))
1433 (rosh-hashannah-day
1434 (aref calendar-day-name-array (% rosh-hashannah 7)))
1435 (passover-day
1436 (aref calendar-day-name-array (% passover 7)))
1437 (long-h (hebrew-calendar-long-heshvan-p h-year))
1438 (short-k (hebrew-calendar-short-kislev-p h-year))
1439 (type (cond ((and long-h (not short-k)) "complete")
1440 ((and (not long-h) short-k) "incomplete")
1441 (t "regular")))
1442 (year-format
1443 (symbol-value
1444 (intern (format "hebrew-calendar-year-%s-%s-%s";; keviah
1445 rosh-hashannah-day type passover-day))))
1446 (first-saturday;; of Hebrew year
1447 (calendar-dayname-on-or-before 6 (+ 6 rosh-hashannah)))
1448 (saturday;; which Saturday of the Hebrew year
1449 (/ (- d first-saturday) 7))
1450 (parasha (aref year-format saturday)))
1451 (if parasha
1452 (format
1453 "Parashat %s"
1454 (if (listp parasha);; Israel differs from diaspora
1455 (if (car parasha)
1456 (format "%s (diaspora), %s (Israel)"
1457 (hebrew-calendar-parasha-name (car parasha))
1458 (hebrew-calendar-parasha-name (cdr parasha)))
1459 (format "%s (Israel)"
1460 (hebrew-calendar-parasha-name (cdr parasha))))
1461 (hebrew-calendar-parasha-name parasha))))))))
1463 (defun add-to-diary-list (date string)
1464 "Add the entry (DATE STRING) to the diary-entries-list.
1465 Do nothing if DATE or STRING is nil."
1466 (and date string
1467 (setq diary-entries-list
1468 (append diary-entries-list (list (list date string))))))
1470 (defconst hebrew-calendar-parashiot-names
1471 ["Bereshith" "Noah" "Lech L'cha" "Vayera" "Hayei Sarah" "Toledoth"
1472 "Vayetze" "Vayishlah" "Vayeshev" "Mikketz" "Vayiggash" "Vayhi"
1473 "Shemoth" "Vaera" "Bo" "Beshallah" "Yithro" "Mishpatim"
1474 "Terumah" "Tetzavveh" "Ki Tissa" "Vayakhel" "Pekudei" "Vayikra"
1475 "Tzav" "Shemini" "Tazria" "Metzora" "Aharei Moth" "Kedoshim"
1476 "Emor" "Behar" "Behukkotai" "Bemidbar" "Naso" "Behaalot'cha"
1477 "Shelah L'cha" "Korah" "Hukkath" "Balak" "Pinhas" "Mattoth"
1478 "Masei" "Devarim" "Vaethanan" "Ekev" "Reeh" "Shofetim"
1479 "Ki Tetze" "Ki Tavo" "Nitzavim" "Vayelech" "Haazinu"]
1480 "The names of the parashiot in the Torah.")
1482 ;; The seven ordinary year types (keviot)
1484 (defconst hebrew-calendar-year-Saturday-incomplete-Sunday
1485 [nil 52 nil nil 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 [21 22]
1486 23 24 nil 25 [26 27] [28 29] 30 [31 32] 33 34 35 36 37 38 39 40 [41 42]
1487 43 44 45 46 47 48 49 50]
1488 "The structure of the parashiot in a Hebrew year that starts on Saturday,
1489 is `incomplete' (Heshvan and Kislev each have 29 days), and has Passover
1490 start on Sunday.")
1492 (defconst hebrew-calendar-year-Saturday-complete-Tuesday
1493 [nil 52 nil nil 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 [21 22]
1494 23 24 nil 25 [26 27] [28 29] 30 [31 32] 33 34 35 36 37 38 39 40 [41 42]
1495 43 44 45 46 47 48 49 [50 51]]
1496 "The structure of the parashiot in a Hebrew year that starts on Saturday,
1497 is `complete' (Heshvan and Kislev each have 30 days), and has Passover
1498 start on Tuesday.")
1500 (defconst hebrew-calendar-year-Monday-incomplete-Tuesday
1501 [51 52 nil 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 [21 22]
1502 23 24 nil 25 [26 27] [28 29] 30 [31 32] 33 34 35 36 37 38 39 40 [41 42]
1503 43 44 45 46 47 48 49 [50 51]]
1504 "The structure of the parashiot in a Hebrew year that starts on Monday,
1505 is `incomplete' (Heshvan and Kislev each have 29 days), and has Passover
1506 start on Tuesday.")
1508 (defconst hebrew-calendar-year-Monday-complete-Thursday
1509 [51 52 nil 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 [21 22]
1510 23 24 nil 25 [26 27] [28 29] 30 [31 32] 33 (nil . 34) (34.35) (35.36)
1511 (36.37) (37.38) ([38 39].39) 40 [41 42] 43 44 45 46 47 48 49 [50 51]]
1512 "The structure of the parashiot in a Hebrew year that starts on Monday,
1513 is `complete' (Heshvan and Kislev each have 30 days), and has Passover
1514 start on Thursday.")
1516 (defconst hebrew-calendar-year-Tuesday-regular-Thursday
1517 [51 52 nil 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 [21 22]
1518 23 24 nil 25 [26 27] [28 29] 30 [31 32] 33 (nil . 34) (34.35) (35.36)
1519 (36.37) (37.38) ([38 39].39) 40 [41 42] 43 44 45 46 47 48 49 [50 51]]
1520 "The structure of the parashiot in a Hebrew year that starts on Tuesday,
1521 is `regular' (Heshvan has 29 days and Kislev has 30 days), and has Passover
1522 start on Thursday.")
1524 (defconst hebrew-calendar-year-Thursday-regular-Saturday
1525 [52 nil nil 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 [21 22]
1526 23 24 nil (nil . 25) (25.[26 27]) ([26 27].[28 29]) ([28 29].30) (30.31)
1527 ([31 32].32) 33 34 35 36 37 38 39 40 [41 42]
1528 43 44 45 46 47 48 49 50]
1529 "The structure of the parashiot in a Hebrew year that starts on Thursday,
1530 is `regular' (Heshvan has 29 days and Kislev has 30 days), and has Passover
1531 start on Saturday.")
1533 (defconst hebrew-calendar-year-Thursday-complete-Sunday
1534 [52 nil nil 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
1535 23 24 nil 25 [26 27] [28 29] 30 [31 32] 33 34 35 36 37 38 39 40 [41 42]
1536 43 44 45 46 47 48 49 50]
1537 "The structure of the parashiot in a Hebrew year that starts on Thursday,
1538 is `complete' (Heshvan and Kislev each have 30 days), and has Passover
1539 start on Sunday.")
1541 ;; The seven leap year types (keviot)
1543 (defconst hebrew-calendar-year-Saturday-incomplete-Tuesday
1544 [nil 52 nil nil 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
1545 23 24 25 26 27 nil 28 29 30 31 32 33 34 35 36 37 38 39 40 [41 42]
1546 43 44 45 46 47 48 49 [50 51]]
1547 "The structure of the parashiot in a Hebrew year that starts on Saturday,
1548 is `incomplete' (Heshvan and Kislev each have 29 days), and has Passover
1549 start on Tuesday.")
1551 (defconst hebrew-calendar-year-Saturday-complete-Thursday
1552 [nil 52 nil nil 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
1553 23 24 25 26 27 nil 28 29 30 31 32 33 (nil . 34) (34.35) (35.36) (36.37)
1554 (37.38) ([38 39].39) 40 [41 42] 43 44 45 46 47 48 49 [50 51]]
1555 "The structure of the parashiot in a Hebrew year that starts on Saturday,
1556 is `complete' (Heshvan and Kislev each have 30 days), and has Passover
1557 start on Thursday.")
1559 (defconst hebrew-calendar-year-Monday-incomplete-Thursday
1560 [51 52 nil 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
1561 23 24 25 26 27 nil 28 29 30 31 32 33 (nil . 34) (34.35) (35.36) (36.37)
1562 (37.38) ([38 39].39) 40 [41 42] 43 44 45 46 47 48 49 [50 51]]
1563 "The structure of the parashiot in a Hebrew year that starts on Monday,
1564 is `incomplete' (Heshvan and Kislev each have 29 days), and has Passover
1565 start on Thursday.")
1567 (defconst hebrew-calendar-year-Monday-complete-Saturday
1568 [51 52 nil 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
1569 23 24 25 26 27 nil (nil . 28) (28.29) (29.30) (30.31) (31.32) (32.33)
1570 (33.34) (34.35) (35.36) (36.37) (37.38) (38.39) (39.40) (40.41) ([41 42].42)
1571 43 44 45 46 47 48 49 50]
1572 "The structure of the parashiot in a Hebrew year that starts on Monday,
1573 is `complete' (Heshvan and Kislev each have 30 days), and has Passover
1574 start on Saturday.")
1576 (defconst hebrew-calendar-year-Tuesday-regular-Saturday
1577 [51 52 nil 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
1578 23 24 25 26 27 nil (nil . 28) (28.29) (29.30) (30.31) (31.32) (32.33)
1579 (33.34) (34.35) (35.36) (36.37) (37.38) (38.39) (39.40) (40.41) ([41 42].42)
1580 43 44 45 46 47 48 49 50]
1581 "The structure of the parashiot in a Hebrew year that starts on Tuesday,
1582 is `regular' (Heshvan has 29 days and Kislev has 30 days), and has Passover
1583 start on Saturday.")
1585 (defconst hebrew-calendar-year-Thursday-incomplete-Sunday
1586 [52 nil nil 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
1587 23 24 25 26 27 28 nil 29 30 31 32 33 34 35 36 37 38 39 40 41 42
1588 43 44 45 46 47 48 49 50]
1589 "The structure of the parashiot in a Hebrew year that starts on Thursday,
1590 is `incomplete' (Heshvan and Kislev both have 29 days), and has Passover
1591 start on Sunday.")
1593 (defconst hebrew-calendar-year-Thursday-complete-Tuesday
1594 [52 nil nil 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
1595 23 24 25 26 27 28 nil 29 30 31 32 33 34 35 36 37 38 39 40 41 42
1596 43 44 45 46 47 48 49 [50 51]]
1597 "The structure of the parashiot in a Hebrew year that starts on Thursday,
1598 is `complete' (Heshvan and Kislev both have 30 days), and has Passover
1599 start on Tuesday.")
1601 (defun hebrew-calendar-parasha-name (p)
1602 "Name(s) corresponding to parasha P."
1603 (if (arrayp p);; combined parasha
1604 (format "%s/%s"
1605 (aref hebrew-calendar-parashiot-names (aref p 0))
1606 (aref hebrew-calendar-parashiot-names (aref p 1)))
1607 (aref hebrew-calendar-parashiot-names p)))
1609 (defun hebrew-calendar-yahrzeit (death-date year)
1610 "Absolute date of the anniversary of Hebrew DEATH-DATE in Hebrew YEAR."
1611 (let* ((death-day (extract-calendar-day death-date))
1612 (death-month (extract-calendar-month death-date))
1613 (death-year (extract-calendar-year death-date)))
1614 (cond
1615 ;; If it's Heshvan 30 it depends on the first anniversary; if
1616 ;; that was not Heshvan 30, use the day before Kislev 1.
1617 ((and (= death-month 8)
1618 (= death-day 30)
1619 (not (hebrew-calendar-long-heshvan-p (1+ death-year))))
1620 (1- (calendar-absolute-from-hebrew (list 9 1 year))))
1621 ;; If it's Kislev 30 it depends on the first anniversary; if
1622 ;; that was not Kislev 30, use the day before Teveth 1.
1623 ((and (= death-month 9)
1624 (= death-day 30)
1625 (hebrew-calendar-short-kislev-p (1+ death-year)))
1626 (1- (calendar-absolute-from-hebrew (list 10 1 year))))
1627 ;; If it's Adar II, use the same day in last month of
1628 ;; year (Adar or Adar II).
1629 ((= death-month 13)
1630 (calendar-absolute-from-hebrew
1631 (list (last-month-of-hebrew-year year) death-day year)))
1632 ;; If it's the 30th in Adar I and $year$ is not a leap year
1633 ;; (so Adar has only 29 days), use the last day in Shevat.
1634 ((and (= death-day 30)
1635 (= death-month 12)
1636 (not (hebrew-calendar-leap-year-p death-year)))
1637 (calendar-absolute-from-hebrew (list 11 30 year)))
1638 ;; In all other cases, use the normal anniversary of the date of death.
1639 (t (calendar-absolute-from-hebrew
1640 (list death-month death-day year))))))
1642 (defun list-islamic-diary-entries ()
1643 "Add any Islamic date entries from the diary-file to diary-entries-list.
1644 Islamic date diary entries must be prefaced by an islamic-diary-entry-symbol
1645 \(normally an `I'\). The same diary-date-forms govern the style of the Islamic
1646 calendar entries, except that the Islamic month names must be spelled in full.
1647 The Islamic months are numbered from 1 to 12 with Muharram being 1 and 12 being
1648 Dhu al-Hijjah. If an Islamic date diary entry begins with a
1649 diary-nonmarking-symbol the entry will appear in the diary listing, but will
1650 not be marked in the calendar. This function is provided for use with the
1651 nongregorian-diary-listing-hook."
1652 (if (< 0 number)
1653 (let ((buffer-read-only nil)
1654 (diary-modified (buffer-modified-p))
1655 (gdate original-date)
1656 (mark (regexp-quote diary-nonmarking-symbol)))
1657 (calendar-for-loop i from 1 to number do
1658 (let* ((d diary-date-forms)
1659 (idate (calendar-islamic-from-absolute
1660 (calendar-absolute-from-gregorian gdate)))
1661 (month (extract-calendar-month idate))
1662 (day (extract-calendar-day idate))
1663 (year (extract-calendar-year idate)))
1664 (while d
1665 (let*
1666 ((date-form (if (equal (car (car d)) 'backup)
1667 (cdr (car d))
1668 (car d)))
1669 (backup (equal (car (car d)) 'backup))
1670 (dayname
1671 (concat
1672 (calendar-day-name gdate) "\\|"
1673 (substring (calendar-day-name gdate) 0 3) ".?"))
1674 (calendar-month-name-array
1675 calendar-islamic-month-name-array)
1676 (monthname
1677 (concat
1678 "\\*\\|"
1679 (calendar-month-name month)))
1680 (month (concat "\\*\\|0*" (int-to-string month)))
1681 (day (concat "\\*\\|0*" (int-to-string day)))
1682 (year
1683 (concat
1684 "\\*\\|0*" (int-to-string year)
1685 (if abbreviated-calendar-year
1686 (concat "\\|" (int-to-string (% year 100)))
1687 "")))
1688 (regexp
1689 (concat
1690 "\\(\\`\\|\^M\\|\n\\)" mark "?"
1691 (regexp-quote islamic-diary-entry-symbol)
1692 "\\("
1693 (mapconcat 'eval date-form "\\)\\(")
1694 "\\)"))
1695 (case-fold-search t))
1696 (goto-char (point-min))
1697 (while (re-search-forward regexp nil t)
1698 (if backup (re-search-backward "\\<" nil t))
1699 (if (and (or (char-equal (preceding-char) ?\^M)
1700 (char-equal (preceding-char) ?\n))
1701 (not (looking-at " \\|\^I")))
1702 ;; Diary entry that consists only of date.
1703 (backward-char 1)
1704 ;; Found a nonempty diary entry--make it visible and
1705 ;; add it to the list.
1706 (let ((entry-start (point))
1707 (date-start))
1708 (re-search-backward "\^M\\|\n\\|\\`")
1709 (setq date-start (point))
1710 (re-search-forward "\^M\\|\n" nil t 2)
1711 (while (looking-at " \\|\^I")
1712 (re-search-forward "\^M\\|\n" nil t))
1713 (backward-char 1)
1714 (subst-char-in-region date-start (point) ?\^M ?\n t)
1715 (add-to-diary-list
1716 gdate (buffer-substring entry-start (point)))))))
1717 (setq d (cdr d))))
1718 (setq gdate
1719 (calendar-gregorian-from-absolute
1720 (1+ (calendar-absolute-from-gregorian gdate)))))
1721 (set-buffer-modified-p diary-modified))
1722 (goto-char (point-min))))
1724 (defun mark-islamic-diary-entries ()
1725 "Mark days in the calendar window that have Islamic date diary entries.
1726 Each entry in diary-file (or included files) visible in the calendar window
1727 is marked. Islamic date entries are prefaced by a islamic-diary-entry-symbol
1728 \(normally an `I'\). The same diary-date-forms govern the style of the Islamic
1729 calendar entries, except that the Islamic month names must be spelled in full.
1730 The Islamic months are numbered from 1 to 12 with Muharram being 1 and 12 being
1731 Dhu al-Hijjah. Islamic date diary entries that begin with a
1732 diary-nonmarking-symbol will not be marked in the calendar. This function is
1733 provided for use as part of the nongregorian-diary-marking-hook."
1734 (let ((d diary-date-forms))
1735 (while d
1736 (let*
1737 ((date-form (if (equal (car (car d)) 'backup)
1738 (cdr (car d))
1739 (car d)));; ignore 'backup directive
1740 (dayname (diary-name-pattern calendar-day-name-array))
1741 (monthname
1742 (concat
1743 (diary-name-pattern calendar-islamic-month-name-array t)
1744 "\\|\\*"))
1745 (month "[0-9]+\\|\\*")
1746 (day "[0-9]+\\|\\*")
1747 (year "[0-9]+\\|\\*")
1748 (l (length date-form))
1749 (d-name-pos (- l (length (memq 'dayname date-form))))
1750 (d-name-pos (if (/= l d-name-pos) (+ 2 d-name-pos)))
1751 (m-name-pos (- l (length (memq 'monthname date-form))))
1752 (m-name-pos (if (/= l m-name-pos) (+ 2 m-name-pos)))
1753 (d-pos (- l (length (memq 'day date-form))))
1754 (d-pos (if (/= l d-pos) (+ 2 d-pos)))
1755 (m-pos (- l (length (memq 'month date-form))))
1756 (m-pos (if (/= l m-pos) (+ 2 m-pos)))
1757 (y-pos (- l (length (memq 'year date-form))))
1758 (y-pos (if (/= l y-pos) (+ 2 y-pos)))
1759 (regexp
1760 (concat
1761 "\\(\\`\\|\^M\\|\n\\)"
1762 (regexp-quote islamic-diary-entry-symbol)
1763 "\\("
1764 (mapconcat 'eval date-form "\\)\\(")
1765 "\\)"))
1766 (case-fold-search t))
1767 (goto-char (point-min))
1768 (while (re-search-forward regexp nil t)
1769 (let* ((dd-name
1770 (if d-name-pos
1771 (buffer-substring
1772 (match-beginning d-name-pos)
1773 (match-end d-name-pos))))
1774 (mm-name
1775 (if m-name-pos
1776 (buffer-substring
1777 (match-beginning m-name-pos)
1778 (match-end m-name-pos))))
1779 (mm (string-to-int
1780 (if m-pos
1781 (buffer-substring
1782 (match-beginning m-pos)
1783 (match-end m-pos))
1784 "")))
1785 (dd (string-to-int
1786 (if d-pos
1787 (buffer-substring
1788 (match-beginning d-pos)
1789 (match-end d-pos))
1790 "")))
1791 (y-str (if y-pos
1792 (buffer-substring
1793 (match-beginning y-pos)
1794 (match-end y-pos))))
1795 (yy (if (not y-str)
1797 (if (and (= (length y-str) 2)
1798 abbreviated-calendar-year)
1799 (let* ((current-y
1800 (extract-calendar-year
1801 (calendar-islamic-from-absolute
1802 (calendar-absolute-from-gregorian
1803 (calendar-current-date)))))
1804 (y (+ (string-to-int y-str)
1805 (* 100 (/ current-y 100)))))
1806 (if (> (- y current-y) 50)
1807 (- y 100)
1808 (if (> (- current-y y) 50)
1809 (+ y 100)
1810 y)))
1811 (string-to-int y-str)))))
1812 (if dd-name
1813 (mark-calendar-days-named
1814 (cdr (assoc (capitalize (substring dd-name 0 3))
1815 (calendar-make-alist
1816 calendar-day-name-array
1818 '(lambda (x) (substring x 0 3))))))
1819 (if mm-name
1820 (if (string-equal mm-name "*")
1821 (setq mm 0)
1822 (setq mm
1823 (cdr (assoc
1824 (capitalize mm-name)
1825 (calendar-make-alist
1826 calendar-islamic-month-name-array))))))
1827 (mark-islamic-calendar-date-pattern mm dd yy)))))
1828 (setq d (cdr d)))))
1830 (defun mark-islamic-calendar-date-pattern (month day year)
1831 "Mark all dates in the calendar window that conform to the Islamic date
1832 MONTH/DAY/YEAR. A value of 0 in any position is a wild-card."
1833 (save-excursion
1834 (set-buffer calendar-buffer)
1835 (if (and (/= 0 month) (/= 0 day))
1836 (if (/= 0 year)
1837 ;; Fully specified Islamic date.
1838 (let ((date (calendar-gregorian-from-absolute
1839 (calendar-absolute-from-islamic
1840 (list month day year)))))
1841 (if (calendar-date-is-visible-p date)
1842 (mark-visible-calendar-date date)))
1843 ;; Month and day in any year--this taken from the holiday stuff.
1844 (let* ((islamic-date (calendar-islamic-from-absolute
1845 (calendar-absolute-from-gregorian
1846 (list displayed-month 15 displayed-year))))
1847 (m (extract-calendar-month islamic-date))
1848 (y (extract-calendar-year islamic-date))
1849 (date))
1850 (if (< m 1)
1851 nil;; Islamic calendar doesn't apply.
1852 (increment-calendar-month m y (- 10 month))
1853 (if (> m 7);; Islamic date might be visible
1854 (let ((date (calendar-gregorian-from-absolute
1855 (calendar-absolute-from-islamic
1856 (list month day y)))))
1857 (if (calendar-date-is-visible-p date)
1858 (mark-visible-calendar-date date)))))))
1859 ;; Not one of the simple cases--check all visible dates for match.
1860 ;; Actually, the following code takes care of ALL of the cases, but
1861 ;; it's much too slow to be used for the simple (common) cases.
1862 (let ((m displayed-month)
1863 (y displayed-year)
1864 (first-date)
1865 (last-date))
1866 (increment-calendar-month m y -1)
1867 (setq first-date
1868 (calendar-absolute-from-gregorian
1869 (list m 1 y)))
1870 (increment-calendar-month m y 2)
1871 (setq last-date
1872 (calendar-absolute-from-gregorian
1873 (list m (calendar-last-day-of-month m y) y)))
1874 (calendar-for-loop date from first-date to last-date do
1875 (let* ((i-date (calendar-islamic-from-absolute date))
1876 (i-month (extract-calendar-month i-date))
1877 (i-day (extract-calendar-day i-date))
1878 (i-year (extract-calendar-year i-date)))
1879 (and (or (zerop month)
1880 (= month i-month))
1881 (or (zerop day)
1882 (= day i-day))
1883 (or (zerop year)
1884 (= year i-year))
1885 (mark-visible-calendar-date
1886 (calendar-gregorian-from-absolute date)))))))))
1888 (defun make-diary-entry (string &optional nonmarking file)
1889 "Insert a diary entry STRING which may be NONMARKING in FILE.
1890 If omitted, NONMARKING defaults to nil and FILE defaults to diary-file."
1891 (find-file-other-window
1892 (substitute-in-file-name (if file file diary-file)))
1893 (goto-char (point-max))
1894 (insert
1895 (if (bolp) "" "\n")
1896 (if nonmarking diary-nonmarking-symbol "")
1897 string " "))
1899 (defun insert-diary-entry (arg)
1900 "Insert a diary entry for the date indicated by point.
1901 Prefix arg will make the entry nonmarking."
1902 (interactive "P")
1903 (let* ((calendar-date-display-form
1904 (if european-calendar-style
1905 '(day " " monthname " " year)
1906 '(monthname " " day ", " year))))
1907 (make-diary-entry
1908 (calendar-date-string
1909 (or (calendar-cursor-to-date)
1910 (error "Cursor is not on a date!"))
1912 arg)))
1914 (defun insert-weekly-diary-entry (arg)
1915 "Insert a weekly diary entry for the day of the week indicated by point.
1916 Prefix arg will make the entry nonmarking."
1917 (interactive "P")
1918 (make-diary-entry
1919 (calendar-day-name
1920 (or (calendar-cursor-to-date)
1921 (error "Cursor is not on a date!")))
1922 arg))
1924 (defun insert-monthly-diary-entry (arg)
1925 "Insert a monthly diary entry for the day of the month indicated by point.
1926 Prefix arg will make the entry nonmarking."
1927 (interactive "P")
1928 (let* ((calendar-date-display-form
1929 (if european-calendar-style
1930 '(day " * ")
1931 '("* " day))))
1932 (make-diary-entry
1933 (calendar-date-string
1934 (or (calendar-cursor-to-date)
1935 (error "Cursor is not on a date!"))
1937 arg)))
1939 (defun insert-yearly-diary-entry (arg)
1940 "Insert an annual diary entry for the day of the year indicated by point.
1941 Prefix arg will make the entry nonmarking."
1942 (interactive "P")
1943 (let* ((calendar-date-display-form
1944 (if european-calendar-style
1945 '(day " " monthname)
1946 '(monthname " " day))))
1947 (make-diary-entry
1948 (calendar-date-string
1949 (or (calendar-cursor-to-date)
1950 (error "Cursor is not on a date!"))
1952 arg)))
1954 (defun insert-anniversary-diary-entry (arg)
1955 "Insert an anniversary diary entry for the date given by point.
1956 Prefix arg will make the entry nonmarking."
1957 (interactive "P")
1958 (let* ((calendar-date-display-form
1959 (if european-calendar-style
1960 '(day " " month " " year)
1961 '(month " " day " " year))))
1962 (make-diary-entry
1963 (format "%s(diary-anniversary %s)"
1964 sexp-diary-entry-symbol
1965 (calendar-date-string
1966 (or (calendar-cursor-to-date)
1967 (error "Cursor is not on a date!"))))
1968 arg)))
1970 (defun insert-block-diary-entry (arg)
1971 "Insert a block diary entry for the days between the point and marked date.
1972 Prefix arg will make the entry nonmarking."
1973 (interactive "P")
1974 (let* ((calendar-date-display-form
1975 (if european-calendar-style
1976 '(day " " month " " year)
1977 '(month " " day " " year)))
1978 (cursor (or (calendar-cursor-to-date)
1979 (error "Cursor is not on a date!")))
1980 (mark (or (car calendar-mark-ring)
1981 (error "No mark set in this buffer")))
1982 (start)
1983 (end))
1984 (if (< (calendar-absolute-from-gregorian mark)
1985 (calendar-absolute-from-gregorian cursor))
1986 (setq start mark
1987 end cursor)
1988 (setq start cursor
1989 end mark))
1990 (make-diary-entry
1991 (format "%s(diary-block %s %s)"
1992 sexp-diary-entry-symbol
1993 (calendar-date-string start)
1994 (calendar-date-string end))
1995 arg)))
1997 (defun insert-cyclic-diary-entry (arg)
1998 "Insert a cyclic diary entry starting at the date given by point.
1999 Prefix arg will make the entry nonmarking."
2000 (interactive "P")
2001 (let* ((calendar-date-display-form
2002 (if european-calendar-style
2003 '(day " " month " " year)
2004 '(month " " day " " year))))
2005 (make-diary-entry
2006 (format "%s(diary-cyclic %d %s)"
2007 sexp-diary-entry-symbol
2008 (calendar-read "Repeat every how many days: "
2009 '(lambda (x) (> x 0)))
2010 (calendar-date-string
2011 (or (calendar-cursor-to-date)
2012 (error "Cursor is not on a date!"))))
2013 arg)))
2015 (defun insert-hebrew-diary-entry (arg)
2016 "Insert a diary entry for the Hebrew date corresponding to the date
2017 indicated by point. Prefix arg will make the entry nonmarking."
2018 (interactive "P")
2019 (let* ((calendar-date-display-form
2020 (if european-calendar-style
2021 '(day " " monthname " " year)
2022 '(monthname " " day ", " year)))
2023 (calendar-month-name-array
2024 calendar-hebrew-month-name-array-leap-year))
2025 (make-diary-entry
2026 (concat
2027 hebrew-diary-entry-symbol
2028 (calendar-date-string
2029 (calendar-hebrew-from-absolute
2030 (calendar-absolute-from-gregorian
2031 (or (calendar-cursor-to-date)
2032 (error "Cursor is not on a date!"))))))
2033 arg)))
2035 (defun insert-monthly-hebrew-diary-entry (arg)
2036 "Insert a monthly diary entry for the day of the Hebrew month corresponding
2037 to the date indicated by point. Prefix arg will make the entry nonmarking."
2038 (interactive "P")
2039 (let* ((calendar-date-display-form
2040 (if european-calendar-style '(day " * ") '("* " day )))
2041 (calendar-month-name-array
2042 calendar-hebrew-month-name-array-leap-year))
2043 (make-diary-entry
2044 (concat
2045 hebrew-diary-entry-symbol
2046 (calendar-date-string
2047 (calendar-hebrew-from-absolute
2048 (calendar-absolute-from-gregorian
2049 (or (calendar-cursor-to-date)
2050 (error "Cursor is not on a date!"))))))
2051 arg)))
2053 (defun insert-yearly-hebrew-diary-entry (arg)
2054 "Insert an annual diary entry for the day of the Hebrew year corresponding
2055 to the date indicated by point. Prefix arg will make the entry nonmarking."
2056 (interactive "P")
2057 (let* ((calendar-date-display-form
2058 (if european-calendar-style
2059 '(day " " monthname)
2060 '(monthname " " day)))
2061 (calendar-month-name-array
2062 calendar-hebrew-month-name-array-leap-year))
2063 (make-diary-entry
2064 (concat
2065 hebrew-diary-entry-symbol
2066 (calendar-date-string
2067 (calendar-hebrew-from-absolute
2068 (calendar-absolute-from-gregorian
2069 (or (calendar-cursor-to-date)
2070 (error "Cursor is not on a date!"))))))
2071 arg)))
2073 (defun insert-islamic-diary-entry (arg)
2074 "Insert a diary entry for the Islamic date corresponding to the date
2075 indicated by point. Prefix arg will make the entry nonmarking."
2076 (interactive "P")
2077 (let* ((calendar-date-display-form
2078 (if european-calendar-style
2079 '(day " " monthname " " year)
2080 '(monthname " " day ", " year)))
2081 (calendar-month-name-array calendar-islamic-month-name-array))
2082 (make-diary-entry
2083 (concat
2084 islamic-diary-entry-symbol
2085 (calendar-date-string
2086 (calendar-islamic-from-absolute
2087 (calendar-absolute-from-gregorian
2088 (or (calendar-cursor-to-date)
2089 (error "Cursor is not on a date!"))))))
2090 arg)))
2092 (defun insert-monthly-islamic-diary-entry (arg)
2093 "Insert a monthly diary entry for the day of the Islamic month corresponding
2094 to the date indicated by point. Prefix arg will make the entry nonmarking."
2095 (interactive "P")
2096 (let* ((calendar-date-display-form
2097 (if european-calendar-style '(day " * ") '("* " day )))
2098 (calendar-month-name-array calendar-islamic-month-name-array))
2099 (make-diary-entry
2100 (concat
2101 islamic-diary-entry-symbol
2102 (calendar-date-string
2103 (calendar-islamic-from-absolute
2104 (calendar-absolute-from-gregorian
2105 (or (calendar-cursor-to-date)
2106 (error "Cursor is not on a date!"))))))
2107 arg)))
2109 (defun insert-yearly-islamic-diary-entry (arg)
2110 "Insert an annual diary entry for the day of the Islamic year corresponding
2111 to the date indicated by point. Prefix arg will make the entry nonmarking."
2112 (interactive "P")
2113 (let* ((calendar-date-display-form
2114 (if european-calendar-style
2115 '(day " " monthname)
2116 '(monthname " " day)))
2117 (calendar-month-name-array calendar-islamic-month-name-array))
2118 (make-diary-entry
2119 (concat
2120 islamic-diary-entry-symbol
2121 (calendar-date-string
2122 (calendar-islamic-from-absolute
2123 (calendar-absolute-from-gregorian
2124 (or (calendar-cursor-to-date)
2125 (error "Cursor is not on a date!"))))))
2126 arg)))
2128 (provide 'diary)
2130 ;;; diary.el ends here