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