1 ;;; holidays.el --- holiday functions for the calendar package
3 ;;; Copyright (C) 1989, 1990 Free Software Foundation, Inc.
5 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY. No author or distributor
12 ;; accepts responsibility to anyone for the consequences of using it
13 ;; or for whether it serves any particular purpose or works at all,
14 ;; unless he says so in writing. Refer to the GNU Emacs General Public
15 ;; License for full details.
17 ;; Everyone is granted permission to copy, modify and redistribute
18 ;; GNU Emacs, but only under the conditions described in the
19 ;; GNU Emacs General Public License. A copy of this license is
20 ;; supposed to have been given to you along with GNU Emacs so you
21 ;; can know your rights and responsibilities. It should be in a
22 ;; file named COPYING. Among other things, the copyright notice
23 ;; and this notice must be preserved on all copies.
27 ;; This collection of functions implements the holiday features as described
30 ;; Comments, corrections, and improvements should be sent to
31 ;; Edward M. Reingold Department of Computer Science
32 ;; (217) 333-6733 University of Illinois at Urbana-Champaign
33 ;; reingold@cs.uiuc.edu 1304 West Springfield Avenue
34 ;; Urbana, Illinois 61801
36 ;; Technical details of all the calendrical calculations can be found in
37 ;; ``Calendrical Calculations'' by Nachum Dershowitz and Edward M. Reingold,
38 ;; Software--Practice and Experience, Volume 20, Number 9 (September, 1990),
47 "Display the holidays for last month, this month, and next month.
48 This function is suitable for execution in a .emacs file."
51 (let* ((date (calendar-current-date))
52 (displayed-month (extract-calendar-month date
))
53 (displayed-year (extract-calendar-year date
)))
54 (list-calendar-holidays))))
56 (defun check-calendar-holidays (date)
57 "Check the list of holidays for any that occur on DATE.
58 The value returned is a list of strings of relevant holiday descriptions.
59 The holidays are those in the list calendar-holidays."
60 (let* ((displayed-month (extract-calendar-month date
))
61 (displayed-year (extract-calendar-year date
))
62 (h (calendar-holiday-list))
65 (if (calendar-date-equal date
(car (car h
)))
66 (setq holiday-list
(append holiday-list
(cdr (car h
)))))
70 (defun calendar-cursor-holidays ()
71 "Find holidays for the date specified by the cursor in the calendar window."
73 (message "Checking holidays...")
74 (let* ((date (or (calendar-cursor-to-date)
75 (error "Cursor is not on a date!")))
76 (date-string (calendar-date-string date
))
77 (holiday-list (check-calendar-holidays date
))
78 (holiday-string (mapconcat 'identity holiday-list
"; "))
79 (msg (format "%s: %s" date-string holiday-string
)))
80 (if (not holiday-list
)
81 (message "No holidays known for %s" date-string
)
82 (if (<= (length msg
) (frame-width))
84 (set-buffer (get-buffer-create holiday-buffer
))
85 (setq buffer-read-only nil
)
86 (setq mode-line-format
87 (format "--------------------------%s%%-"
90 (insert (mapconcat 'identity holiday-list
"\n"))
91 (goto-char (point-min))
92 (set-buffer-modified-p nil
)
93 (setq buffer-read-only t
)
94 (display-buffer holiday-buffer
)
95 (message "Checking holidays...done")))))
97 (defun mark-calendar-holidays ()
98 "Mark notable days in the calendar window."
100 (setq mark-holidays-in-calendar t
)
101 (message "Marking holidays...")
102 (let ((holiday-list (calendar-holiday-list)))
104 (mark-visible-calendar-date
105 (car (car holiday-list
)) calendar-holiday-marker
)
106 (setq holiday-list
(cdr holiday-list
))))
107 (message "Marking holidays...done"))
109 (defun list-calendar-holidays ()
110 "Create a buffer containing the holidays for the current calendar window.
111 The holidays are those in the list calendar-notable-days. Returns t if any
112 holidays are found, nil if not."
114 (message "Looking up holidays...")
115 (let ((holiday-list (calendar-holiday-list))
120 (if (not holiday-list
)
122 (message "Looking up holidays...none found")
124 (set-buffer (get-buffer-create holiday-buffer
))
125 (setq buffer-read-only nil
)
126 (increment-calendar-month m1 y1 -
1)
127 (increment-calendar-month m2 y2
1)
128 (setq mode-line-format
129 (format "-------------Notable Dates from %s, %d to %s, %d%%-"
130 (calendar-month-name m1
) y1
(calendar-month-name m2
) y2
))
134 '(lambda (x) (concat (calendar-date-string (car x
))
137 (goto-char (point-min))
138 (set-buffer-modified-p nil
)
139 (setq buffer-read-only t
)
140 (display-buffer holiday-buffer
)
141 (message "Looking up holidays...done")
144 (defun calendar-holiday-list ()
145 "Form the list of holidays that occur on dates in the calendar window.
146 The holidays are those in the list calendar-holidays."
147 (let ((p calendar-holidays
)
150 (let* ((function-name
151 (intern (format "calendar-holiday-function-%s" (car (car p
)))))
153 (if (cdr (car p
));; optional arguments
154 (funcall function-name
(cdr (car p
)))
155 (funcall function-name
))))
157 (setq holiday-list
(append holidays holiday-list
))))
159 (setq holiday-list
(sort holiday-list
'calendar-date-compare
))))
161 ;; Below are the functions that calculate the dates of holidays; these
162 ;; are called by the funcall in the function calendar-holiday-list. If you
163 ;; write other such functions, be sure to imitate the style used below,
164 ;; including the evaluation of each element in the list that constitutes
165 ;; the argument to the function. If you don't do this evaluation, the
166 ;; list calendar-holidays cannot contain expressions (as, for example, in
167 ;; the entry for the Islamic new year. Also remember that each function
168 ;; must return a list of items of the form ((month day year) string);
169 ;; the date (month day year) should be visible in the calendar window.
171 (defun calendar-holiday-function-fixed (x)
172 "Returns the corresponding Gregorian date, if visible in the window, to
173 month, year where month is (car X) and year is (car (cdr X)). If it is
174 visible, the value returned is the list (((month day year) string)) where
175 string is (car (nthcdr 2 X)). Returns nil if it is not visible in the
176 current calendar window."
177 (let* ((month (eval (car x
)))
178 (day (eval (car (cdr x
))))
179 (string (eval (car (nthcdr 2 x
))))
182 (increment-calendar-month m y
(- 11 month
))
184 (list (list (list month day y
) string
)))))
186 (defun calendar-holiday-function-float (x)
187 "Returns the corresponding Gregorian date, if visible in the window, to the
188 n-th occurrence (negative counts from the end of the month) of dayname in
189 month, year where month is (car X), year is (car (cdr X)), n is
190 \(car \(nthcdr 2 X\)\). If it is visible, the value returned is the list
191 \(\(\(month day year)\ string\)\) where string is (car (nthcdr 3 X)).
192 Returns nil if it is not visible in the current calendar window."
193 (let* ((month (eval (car x
)))
194 (dayname (eval (car (cdr x
))))
195 (n (eval (car (nthcdr 2 x
))))
196 (string (eval (car (nthcdr 3 x
))))
199 (increment-calendar-month m y
(- 11 month
))
201 (list (list (calendar-nth-named-day n dayname month y
) string
)))))
203 (defun calendar-holiday-function-julian (x)
204 "Returns the corresponding Gregorian date, if visible in the window, to the
205 Julian date month, year where month is (car X) and year is (car (cdr X)).
206 If it is visible, the value returned is the list (((month day year) string))
207 where string is (car (nthcdr 2 X)). Returns nil if it is not visible in the
208 current calendar window."
209 (let* ((month (eval (car x
)))
210 (day (eval (car (cdr x
))))
211 (string (eval (car (nthcdr 2 x
))))
217 (increment-calendar-month m1 y1 -
1)
218 (increment-calendar-month m2 y2
1)
219 (let* ((start-date (calendar-absolute-from-gregorian
221 (end-date (calendar-absolute-from-gregorian
222 (list m2
(calendar-last-day-of-month m2 y2
) y2
)))
223 (julian-start (calendar-julian-from-absolute start-date
))
224 (julian-end (calendar-julian-from-absolute end-date
))
225 (julian-y1 (extract-calendar-year julian-start
))
226 (julian-y2 (extract-calendar-year julian-end
)))
227 (setq year
(if (< 10 month
) julian-y1 julian-y2
))
228 (let ((date (calendar-gregorian-from-absolute
229 (calendar-absolute-from-julian
230 (list month day year
)))))
231 (if (calendar-date-is-visible-p date
)
232 (list (list date string
)))))))
234 (defun calendar-holiday-function-islamic (x)
235 "Returns the corresponding Gregorian date, if visible in the window, to the
236 Islamic date month, day where month is (car X) and day is (car (cdr X)).
237 If it is visible, the value returned is the list (((month day year) string))
238 where string is (car (nthcdr 2 X)). Returns nil if it is not visible in
239 the current calendar window."
240 (let* ((month (eval (car x
)))
241 (day (eval (car (cdr x
))))
242 (string (eval (car (nthcdr 2 x
))))
243 (islamic-date (calendar-islamic-from-absolute
244 (calendar-absolute-from-gregorian
245 (list displayed-month
15 displayed-year
))))
246 (m (extract-calendar-month islamic-date
))
247 (y (extract-calendar-year islamic-date
))
250 nil
;; Islamic calendar doesn't apply.
251 (increment-calendar-month m y
(- 10 month
))
252 (if (> m
7);; Islamic date might be visible
253 (let ((date (calendar-gregorian-from-absolute
254 (calendar-absolute-from-islamic (list month day y
)))))
255 (if (calendar-date-is-visible-p date
)
256 (list (list date string
))))))))
258 (defun calendar-holiday-function-hebrew (x)
259 "Returns the corresponding Gregorian date, if visible in the window, to the
260 Hebrew date month, day where month is (car X) and day is (car (cdr X)).
261 If it is visible, the value returned is the list (((month day year) string))
262 where string is (car (nthcdr 2 X)). Returns nil if it is not visible in
263 the current calendar window."
264 (let* ((month (eval (car x
)))
265 (day (eval (car (cdr x
))))
266 (string (eval (car (nthcdr 2 x
)))))
267 (if (memq displayed-month
;; This test is only to speed things up a bit;
268 (list ;; it works fine without the test too.
269 (if (< 11 month
) (- month
11) (+ month
1))
270 (if (< 10 month
) (- month
10) (+ month
2))
271 (if (< 9 month
) (- month
9) (+ month
3))
272 (if (< 8 month
) (- month
8) (+ month
4))
273 (if (< 7 month
) (- month
7) (+ month
5))))
274 (let ((m1 displayed-month
)
279 (increment-calendar-month m1 y1 -
1)
280 (increment-calendar-month m2 y2
1)
281 (let* ((start-date (calendar-absolute-from-gregorian
283 (end-date (calendar-absolute-from-gregorian
284 (list m2
(calendar-last-day-of-month m2 y2
) y2
)))
285 (hebrew-start (calendar-hebrew-from-absolute start-date
))
286 (hebrew-end (calendar-hebrew-from-absolute end-date
))
287 (hebrew-y1 (extract-calendar-year hebrew-start
))
288 (hebrew-y2 (extract-calendar-year hebrew-end
)))
289 (setq year
(if (< 6 month
) hebrew-y2 hebrew-y1
))
290 (let ((date (calendar-gregorian-from-absolute
291 (calendar-absolute-from-hebrew
292 (list month day year
)))))
293 (if (calendar-date-is-visible-p date
)
294 (list (list date string
)))))))))
296 (defun calendar-holiday-function-if (x)
297 "Conditional holiday for dates in the calendar window.
298 The boolean condition is (car X). If t, the holiday (car (cdr X)) is
299 checked. If nil, the holiday (car (cdr (cdr X))), if there, is checked."
300 (let* ((boolean (eval (car x
)))
301 (h (if boolean
(car (cdr x
)) (car (cdr (cdr x
))))))
303 (let* ((function-name
304 (intern (format "calendar-holiday-function-%s" (car h
))))
306 (if (cdr h
);; optional arguments
307 (funcall function-name
(cdr h
))
308 (funcall function-name
))))
311 (defun calendar-holiday-function-advent ()
312 "Date of Advent, if visible in calendar window."
313 (let ((year displayed-year
)
314 (month displayed-month
))
315 (increment-calendar-month month year -
1)
316 (let ((advent (calendar-gregorian-from-absolute
317 (calendar-dayname-on-or-before 0
318 (calendar-absolute-from-gregorian
319 (list 12 3 year
))))))
320 (if (calendar-date-is-visible-p advent
)
321 (list (list advent
"Advent"))))))
323 (defun calendar-holiday-function-easter-etc ()
324 "List of dates related to Easter, as visible in calendar window."
325 (if (and (> displayed-month
5) (not all-christian-calendar-holidays
))
326 nil
;; Ash Wednesday, Good Friday, and Easter are not visible.
327 (let* ((century (1+ (/ displayed-year
100)))
328 (shifted-epact ;; Age of moon for April 5...
329 (%
(+ 14 (* 11 (% displayed-year
19));; ...by Nicaean rule
330 (- ;; ...corrected for the Gregorian century rule
332 (/ ;; ...corrected for Metonic cycle inaccuracy.
333 (+ 5 (* 8 century
)) 25)
334 (* 30 century
));; Keeps value positive.
336 (adjusted-epact ;; Adjust for 29.5 day month.
337 (if (or (= shifted-epact
0)
338 (and (= shifted-epact
1) (< 10 (% displayed-year
19))))
341 (paschal-moon ;; Day after the full moon on or after March 21.
342 (- (calendar-absolute-from-gregorian (list 4 19 displayed-year
))
344 (abs-easter (calendar-dayname-on-or-before 0 (+ paschal-moon
7)))
347 (list (calendar-gregorian-from-absolute abs-easter
)
349 (list (calendar-gregorian-from-absolute (- abs-easter
2))
351 (list (calendar-gregorian-from-absolute (- abs-easter
46))
355 (list (calendar-gregorian-from-absolute (- abs-easter
63))
356 "Septuagesima Sunday")
357 (list (calendar-gregorian-from-absolute (- abs-easter
56))
359 (list (calendar-gregorian-from-absolute (- abs-easter
49))
361 (list (calendar-gregorian-from-absolute (- abs-easter
48))
363 (list (calendar-gregorian-from-absolute (- abs-easter
47))
365 (list (calendar-gregorian-from-absolute (- abs-easter
14))
367 (list (calendar-gregorian-from-absolute (- abs-easter
7))
369 (list (calendar-gregorian-from-absolute (- abs-easter
3))
371 (list (calendar-gregorian-from-absolute (+ abs-easter
35))
373 (list (calendar-gregorian-from-absolute (+ abs-easter
39))
375 (list (calendar-gregorian-from-absolute (+ abs-easter
49))
376 "Pentecost (Whitsunday)")
377 (list (calendar-gregorian-from-absolute (+ abs-easter
50))
379 (list (calendar-gregorian-from-absolute (+ abs-easter
56))
381 (list (calendar-gregorian-from-absolute (+ abs-easter
60))
384 (filter-visible-calendar-holidays mandatory
)))
385 (if all-christian-calendar-holidays
388 (filter-visible-calendar-holidays optional
)
392 (defun calendar-holiday-function-rosh-hashanah-etc ()
393 "List of dates related to Rosh Hashanah, as visible in calendar window."
394 (if (or (< displayed-month
8)
395 (> displayed-month
11))
396 nil
;; None of the dates is visible
397 (let* ((abs-r-h (calendar-absolute-from-hebrew
398 (list 7 1 (+ displayed-year
3761))))
401 (list (calendar-gregorian-from-absolute abs-r-h
)
402 (format "Rosh HaShanah %d" (+ 3761 displayed-year
)))
403 (list (calendar-gregorian-from-absolute (+ abs-r-h
9))
405 (list (calendar-gregorian-from-absolute (+ abs-r-h
14))
407 (list (calendar-gregorian-from-absolute (+ abs-r-h
21))
409 (list (calendar-gregorian-from-absolute (+ abs-r-h
22))
413 (list (calendar-gregorian-from-absolute
414 (calendar-dayname-on-or-before 6 (- abs-r-h
4)))
416 (list (calendar-gregorian-from-absolute (1- abs-r-h
))
417 "Erev Rosh HaShannah")
418 (list (calendar-gregorian-from-absolute (1+ abs-r-h
))
419 "Rosh HaShanah (second day)")
420 (list (calendar-gregorian-from-absolute
421 (if (= (% abs-r-h
7) 4) (+ abs-r-h
3) (+ abs-r-h
2)))
423 (list (calendar-gregorian-from-absolute
424 (calendar-dayname-on-or-before 6 (+ 7 abs-r-h
)))
426 (list (calendar-gregorian-from-absolute (+ abs-r-h
8))
428 (list (calendar-gregorian-from-absolute (+ abs-r-h
13))
430 (list (calendar-gregorian-from-absolute (+ abs-r-h
15))
431 "Sukkot (second day)")
432 (list (calendar-gregorian-from-absolute (+ abs-r-h
16))
433 "Hol Hamoed Sukkot (first day)")
434 (list (calendar-gregorian-from-absolute (+ abs-r-h
17))
435 "Hol Hamoed Sukkot (second day)")
436 (list (calendar-gregorian-from-absolute (+ abs-r-h
18))
437 "Hol Hamoed Sukkot (third day)")
438 (list (calendar-gregorian-from-absolute (+ abs-r-h
19))
439 "Hol Hamoed Sukkot (fourth day)")
440 (list (calendar-gregorian-from-absolute (+ abs-r-h
20))
441 "Hoshannah Rabbah")))
443 (filter-visible-calendar-holidays mandatory
)))
444 (if all-hebrew-calendar-holidays
447 (filter-visible-calendar-holidays optional
)
451 (defun calendar-holiday-function-hanukkah ()
452 "List of dates related to Hanukkah, as visible in calendar window."
453 (if (memq displayed-month
;; This test is only to speed things up a bit;
454 '(10 11 12 1 2));; it works fine without the test too.
455 (let ((m displayed-month
)
457 (increment-calendar-month m y
1)
458 (let* ((h-y (extract-calendar-year
459 (calendar-hebrew-from-absolute
460 (calendar-absolute-from-gregorian
461 (list m
(calendar-last-day-of-month m y
) y
)))))
462 (abs-h (calendar-absolute-from-hebrew (list 9 25 h-y
))))
463 (filter-visible-calendar-holidays
465 (list (calendar-gregorian-from-absolute (1- abs-h
))
467 (list (calendar-gregorian-from-absolute abs-h
)
468 "Hanukkah (first day)")
469 (list (calendar-gregorian-from-absolute (1+ abs-h
))
470 "Hanukkah (second day)")
471 (list (calendar-gregorian-from-absolute (+ abs-h
2))
472 "Hanukkah (third day)")
473 (list (calendar-gregorian-from-absolute (+ abs-h
3))
474 "Hanukkah (fourth day)")
475 (list (calendar-gregorian-from-absolute (+ abs-h
4))
476 "Hanukkah (fifth day)")
477 (list (calendar-gregorian-from-absolute (+ abs-h
5))
478 "Hanukkah (sixth day)")
479 (list (calendar-gregorian-from-absolute (+ abs-h
6))
480 "Hanukkah (seventh day)")
481 (list (calendar-gregorian-from-absolute (+ abs-h
7))
482 "Hanukkah (eighth day)")))))))
484 (defun calendar-holiday-function-passover-etc ()
485 "List of dates related to Passover, as visible in calendar window."
486 (if (< 7 displayed-month
)
487 nil
;; None of the dates is visible
488 (let* ((abs-p (calendar-absolute-from-hebrew
489 (list 1 15 (+ displayed-year
3760))))
492 (list (calendar-gregorian-from-absolute abs-p
)
494 (list (calendar-gregorian-from-absolute (+ abs-p
50))
498 (list (calendar-gregorian-from-absolute
499 (calendar-dayname-on-or-before 6 (- abs-p
43)))
501 (list (calendar-gregorian-from-absolute
502 (calendar-dayname-on-or-before 6 (- abs-p
30)))
504 (list (calendar-gregorian-from-absolute
505 (if (= (% abs-p
7) 2) (- abs-p
33) (- abs-p
31)))
507 (list (calendar-gregorian-from-absolute (- abs-p
31))
509 (list (calendar-gregorian-from-absolute (- abs-p
30))
511 (list (calendar-gregorian-from-absolute
512 (if (zerop (% abs-p
7)) (- abs-p
28) (- abs-p
29)))
514 (list (calendar-gregorian-from-absolute
515 (- (calendar-dayname-on-or-before 6 (- abs-p
14)) 7))
517 (list (calendar-gregorian-from-absolute
518 (calendar-dayname-on-or-before 6 (- abs-p
14)))
520 (list (calendar-gregorian-from-absolute
521 (calendar-dayname-on-or-before 6 (1- abs-p
)))
523 (list (calendar-gregorian-from-absolute (1- abs-p
))
525 (list (calendar-gregorian-from-absolute (1+ abs-p
))
526 "Passover (second day)")
527 (list (calendar-gregorian-from-absolute (+ abs-p
2))
528 "Hol Hamoed Passover (first day)")
529 (list (calendar-gregorian-from-absolute (+ abs-p
3))
530 "Hol Hamoed Passover (second day)")
531 (list (calendar-gregorian-from-absolute (+ abs-p
4))
532 "Hol Hamoed Passover (third day)")
533 (list (calendar-gregorian-from-absolute (+ abs-p
5))
534 "Hol Hamoed Passover (fourth day)")
535 (list (calendar-gregorian-from-absolute (+ abs-p
6))
536 "Passover (seventh day)")
537 (list (calendar-gregorian-from-absolute (+ abs-p
7))
538 "Passover (eighth day)")
539 (list (calendar-gregorian-from-absolute (+ abs-p
12))
541 (list (calendar-gregorian-from-absolute
542 (if (zerop (% abs-p
7))
544 (if (= (% abs-p
7) 6)
548 (list (calendar-gregorian-from-absolute (+ abs-p
33))
550 (list (calendar-gregorian-from-absolute (+ abs-p
43))
552 (list (calendar-gregorian-from-absolute (+ abs-p
49))
554 (list (calendar-gregorian-from-absolute (+ abs-p
51))
555 "Shavuot (second day)")))
557 (filter-visible-calendar-holidays mandatory
)))
558 (if all-hebrew-calendar-holidays
561 (filter-visible-calendar-holidays optional
)
565 (defun calendar-holiday-function-tisha-b-av-etc ()
566 "List of dates around Tisha B'Av, as visible in calendar window."
567 (if (or (< displayed-month
5)
568 (> displayed-month
9))
569 nil
;; None of the dates is visible
570 (let* ((abs-t-a (calendar-absolute-from-hebrew
571 (list 5 9 (+ displayed-year
3760)))))
573 (filter-visible-calendar-holidays
575 (list (calendar-gregorian-from-absolute
576 (if (= (% abs-t-a
7) 6) (- abs-t-a
20) (- abs-t-a
21)))
578 (list (calendar-gregorian-from-absolute
579 (calendar-dayname-on-or-before 6 abs-t-a
))
581 (list (calendar-gregorian-from-absolute
582 (if (= (% abs-t-a
7) 6) (1+ abs-t-a
) abs-t-a
))
584 (list (calendar-gregorian-from-absolute
585 (calendar-dayname-on-or-before 6 (+ abs-t-a
7)))
586 "Shabbat Nahamu"))))))
588 (defun filter-visible-calendar-holidays (l)
589 "Return a list of all visible holidays of those on L."
593 (if (calendar-date-is-visible-p (car (car p
)))
594 (setq visible
(append (list (car p
)) visible
)))
600 ;;; holidays.el ends here