1 ;;; holidays.el --- holiday functions for the calendar package
2 ;;; Copyright (C) 1989, 1990 Free Software Foundation, Inc.
4 ;; This file is part of GNU Emacs.
6 ;; GNU Emacs is distributed in the hope that it will be useful,
7 ;; but WITHOUT ANY WARRANTY. No author or distributor
8 ;; accepts responsibility to anyone for the consequences of using it
9 ;; or for whether it serves any particular purpose or works at all,
10 ;; unless he says so in writing. Refer to the GNU Emacs General Public
11 ;; License for full details.
13 ;; Everyone is granted permission to copy, modify and redistribute
14 ;; GNU Emacs, but only under the conditions described in the
15 ;; GNU Emacs General Public License. A copy of this license is
16 ;; supposed to have been given to you along with GNU Emacs so you
17 ;; can know your rights and responsibilities. It should be in a
18 ;; file named COPYING. Among other things, the copyright notice
19 ;; and this notice must be preserved on all copies.
21 ;; This collection of functions implements the holiday features as described
24 ;; Comments, corrections, and improvements should be sent to
25 ;; Edward M. Reingold Department of Computer Science
26 ;; (217) 333-6733 University of Illinois at Urbana-Champaign
27 ;; reingold@cs.uiuc.edu 1304 West Springfield Avenue
28 ;; Urbana, Illinois 61801
30 ;; Technical details of all the calendrical calculations can be found in
31 ;; ``Calendrical Calculations'' by Nachum Dershowitz and Edward M. Reingold,
32 ;; Software--Practice and Experience, Volume 20, Number 9 (September, 1990),
39 "Display the holidays for last month, this month, and next month.
40 This function is suitable for execution in a .emacs file."
43 (let* ((date (calendar-current-date))
44 (displayed-month (extract-calendar-month date
))
45 (displayed-year (extract-calendar-year date
)))
46 (list-calendar-holidays))))
48 (defun check-calendar-holidays (date)
49 "Check the list of holidays for any that occur on DATE.
50 The value returned is a list of strings of relevant holiday descriptions.
51 The holidays are those in the list calendar-holidays."
52 (let* ((displayed-month (extract-calendar-month date
))
53 (displayed-year (extract-calendar-year date
))
54 (h (calendar-holiday-list))
57 (if (calendar-date-equal date
(car (car h
)))
58 (setq holiday-list
(append holiday-list
(cdr (car h
)))))
62 (defun calendar-cursor-holidays ()
63 "Find holidays for the date specified by the cursor in the calendar window."
65 (message "Checking holidays...")
66 (let* ((date (or (calendar-cursor-to-date)
67 (error "Cursor is not on a date!")))
68 (date-string (calendar-date-string date
))
69 (holiday-list (check-calendar-holidays date
))
70 (holiday-string (mapconcat 'identity holiday-list
"; "))
71 (msg (format "%s: %s" date-string holiday-string
)))
72 (if (not holiday-list
)
73 (message "No holidays known for %s" date-string
)
74 (if (<= (length msg
) (screen-width))
76 (set-buffer (get-buffer-create holiday-buffer
))
77 (setq buffer-read-only nil
)
78 (setq mode-line-format
79 (format "--------------------------%s%%-"
82 (insert (mapconcat 'identity holiday-list
"\n"))
83 (goto-char (point-min))
84 (set-buffer-modified-p nil
)
85 (setq buffer-read-only t
)
86 (display-buffer holiday-buffer
)
87 (message "Checking holidays...done")))))
89 (defun mark-calendar-holidays ()
90 "Mark notable days in the calendar window."
92 (setq mark-holidays-in-calendar t
)
93 (message "Marking holidays...")
94 (let ((holiday-list (calendar-holiday-list)))
96 (mark-visible-calendar-date
97 (car (car holiday-list
)) calendar-holiday-marker
)
98 (setq holiday-list
(cdr holiday-list
))))
99 (message "Marking holidays...done"))
101 (defun list-calendar-holidays ()
102 "Create a buffer containing the holidays for the current calendar window.
103 The holidays are those in the list calendar-notable-days. Returns t if any
104 holidays are found, nil if not."
106 (message "Looking up holidays...")
107 (let ((holiday-list (calendar-holiday-list))
112 (if (not holiday-list
)
114 (message "Looking up holidays...none found")
116 (set-buffer (get-buffer-create holiday-buffer
))
117 (setq buffer-read-only nil
)
118 (increment-calendar-month m1 y1 -
1)
119 (increment-calendar-month m2 y2
1)
120 (setq mode-line-format
121 (format "-------------Notable Dates from %s, %d to %s, %d%%-"
122 (calendar-month-name m1
) y1
(calendar-month-name m2
) y2
))
126 '(lambda (x) (concat (calendar-date-string (car x
))
129 (goto-char (point-min))
130 (set-buffer-modified-p nil
)
131 (setq buffer-read-only t
)
132 (display-buffer holiday-buffer
)
133 (message "Looking up holidays...done")
136 (defun calendar-holiday-list ()
137 "Form the list of holidays that occur on dates in the calendar window.
138 The holidays are those in the list calendar-holidays."
139 (let ((p calendar-holidays
)
142 (let* ((function-name
143 (intern (format "calendar-holiday-function-%s" (car (car p
)))))
145 (if (cdr (car p
));; optional arguments
146 (funcall function-name
(cdr (car p
)))
147 (funcall function-name
))))
149 (setq holiday-list
(append holidays holiday-list
))))
151 (setq holiday-list
(sort holiday-list
'calendar-date-compare
))))
153 ;; Below are the functions that calculate the dates of holidays; these
154 ;; are called by the funcall in the function calendar-holiday-list. If you
155 ;; write other such functions, be sure to imitate the style used below,
156 ;; including the evaluation of each element in the list that constitutes
157 ;; the argument to the function. If you don't do this evaluation, the
158 ;; list calendar-holidays cannot contain expressions (as, for example, in
159 ;; the entry for the Islamic new year. Also remember that each function
160 ;; must return a list of items of the form ((month day year) string);
161 ;; the date (month day year) should be visible in the calendar window.
163 (defun calendar-holiday-function-fixed (x)
164 "Returns the corresponding Gregorian date, if visible in the window, to
165 month, year where month is (car X) and year is (car (cdr X)). If it is
166 visible, the value returned is the list (((month day year) string)) where
167 string is (car (nthcdr 2 X)). Returns nil if it is not visible in the
168 current calendar window."
169 (let* ((month (eval (car x
)))
170 (day (eval (car (cdr x
))))
171 (string (eval (car (nthcdr 2 x
))))
174 (increment-calendar-month m y
(- 11 month
))
176 (list (list (list month day y
) string
)))))
178 (defun calendar-holiday-function-float (x)
179 "Returns the corresponding Gregorian date, if visible in the window, to the
180 n-th occurrence (negative counts from the end of the month) of dayname in
181 month, year where month is (car X), year is (car (cdr X)), n is
182 \(car \(nthcdr 2 X\)\). If it is visible, the value returned is the list
183 \(\(\(month day year)\ string\)\) where string is (car (nthcdr 3 X)).
184 Returns nil if it is not visible in the current calendar window."
185 (let* ((month (eval (car x
)))
186 (dayname (eval (car (cdr x
))))
187 (n (eval (car (nthcdr 2 x
))))
188 (string (eval (car (nthcdr 3 x
))))
191 (increment-calendar-month m y
(- 11 month
))
193 (list (list (calendar-nth-named-day n dayname month y
) string
)))))
195 (defun calendar-holiday-function-julian (x)
196 "Returns the corresponding Gregorian date, if visible in the window, to the
197 Julian date month, year where month is (car X) and year is (car (cdr X)).
198 If it is visible, the value returned is the list (((month day year) string))
199 where string is (car (nthcdr 2 X)). Returns nil if it is not visible in the
200 current calendar window."
201 (let* ((month (eval (car x
)))
202 (day (eval (car (cdr x
))))
203 (string (eval (car (nthcdr 2 x
))))
209 (increment-calendar-month m1 y1 -
1)
210 (increment-calendar-month m2 y2
1)
211 (let* ((start-date (calendar-absolute-from-gregorian
213 (end-date (calendar-absolute-from-gregorian
214 (list m2
(calendar-last-day-of-month m2 y2
) y2
)))
215 (julian-start (calendar-julian-from-absolute start-date
))
216 (julian-end (calendar-julian-from-absolute end-date
))
217 (julian-y1 (extract-calendar-year julian-start
))
218 (julian-y2 (extract-calendar-year julian-end
)))
219 (setq year
(if (< 10 month
) julian-y1 julian-y2
))
220 (let ((date (calendar-gregorian-from-absolute
221 (calendar-absolute-from-julian
222 (list month day year
)))))
223 (if (calendar-date-is-visible-p date
)
224 (list (list date string
)))))))
226 (defun calendar-holiday-function-islamic (x)
227 "Returns the corresponding Gregorian date, if visible in the window, to the
228 Islamic date month, day where month is (car X) and day is (car (cdr X)).
229 If it is visible, the value returned is the list (((month day year) string))
230 where string is (car (nthcdr 2 X)). Returns nil if it is not visible in
231 the current calendar window."
232 (let* ((month (eval (car x
)))
233 (day (eval (car (cdr x
))))
234 (string (eval (car (nthcdr 2 x
))))
235 (islamic-date (calendar-islamic-from-absolute
236 (calendar-absolute-from-gregorian
237 (list displayed-month
15 displayed-year
))))
238 (m (extract-calendar-month islamic-date
))
239 (y (extract-calendar-year islamic-date
))
242 nil
;; Islamic calendar doesn't apply.
243 (increment-calendar-month m y
(- 10 month
))
244 (if (> m
7);; Islamic date might be visible
245 (let ((date (calendar-gregorian-from-absolute
246 (calendar-absolute-from-islamic (list month day y
)))))
247 (if (calendar-date-is-visible-p date
)
248 (list (list date string
))))))))
250 (defun calendar-holiday-function-hebrew (x)
251 "Returns the corresponding Gregorian date, if visible in the window, to the
252 Hebrew date month, day where month is (car X) and day is (car (cdr X)).
253 If it is visible, the value returned is the list (((month day year) string))
254 where string is (car (nthcdr 2 X)). Returns nil if it is not visible in
255 the current calendar window."
256 (let* ((month (eval (car x
)))
257 (day (eval (car (cdr x
))))
258 (string (eval (car (nthcdr 2 x
)))))
259 (if (memq displayed-month
;; This test is only to speed things up a bit;
260 (list ;; it works fine without the test too.
261 (if (< 11 month
) (- month
11) (+ month
1))
262 (if (< 10 month
) (- month
10) (+ month
2))
263 (if (< 9 month
) (- month
9) (+ month
3))
264 (if (< 8 month
) (- month
8) (+ month
4))
265 (if (< 7 month
) (- month
7) (+ month
5))))
266 (let ((m1 displayed-month
)
271 (increment-calendar-month m1 y1 -
1)
272 (increment-calendar-month m2 y2
1)
273 (let* ((start-date (calendar-absolute-from-gregorian
275 (end-date (calendar-absolute-from-gregorian
276 (list m2
(calendar-last-day-of-month m2 y2
) y2
)))
277 (hebrew-start (calendar-hebrew-from-absolute start-date
))
278 (hebrew-end (calendar-hebrew-from-absolute end-date
))
279 (hebrew-y1 (extract-calendar-year hebrew-start
))
280 (hebrew-y2 (extract-calendar-year hebrew-end
)))
281 (setq year
(if (< 6 month
) hebrew-y2 hebrew-y1
))
282 (let ((date (calendar-gregorian-from-absolute
283 (calendar-absolute-from-hebrew
284 (list month day year
)))))
285 (if (calendar-date-is-visible-p date
)
286 (list (list date string
)))))))))
288 (defun calendar-holiday-function-if (x)
289 "Conditional holiday for dates in the calendar window.
290 The boolean condition is (car X). If t, the holiday (car (cdr X)) is
291 checked. If nil, the holiday (car (cdr (cdr X))), if there, is checked."
292 (let* ((boolean (eval (car x
)))
293 (h (if boolean
(car (cdr x
)) (car (cdr (cdr x
))))))
295 (let* ((function-name
296 (intern (format "calendar-holiday-function-%s" (car h
))))
298 (if (cdr h
);; optional arguments
299 (funcall function-name
(cdr h
))
300 (funcall function-name
))))
303 (defun calendar-holiday-function-advent ()
304 "Date of Advent, if visible in calendar window."
305 (let ((year displayed-year
)
306 (month displayed-month
))
307 (increment-calendar-month month year -
1)
308 (let ((advent (calendar-gregorian-from-absolute
309 (calendar-dayname-on-or-before 0
310 (calendar-absolute-from-gregorian
311 (list 12 3 year
))))))
312 (if (calendar-date-is-visible-p advent
)
313 (list (list advent
"Advent"))))))
315 (defun calendar-holiday-function-easter-etc ()
316 "List of dates related to Easter, as visible in calendar window."
317 (if (and (> displayed-month
5) (not all-christian-calendar-holidays
))
318 nil
;; Ash Wednesday, Good Friday, and Easter are not visible.
319 (let* ((century (1+ (/ displayed-year
100)))
320 (shifted-epact ;; Age of moon for April 5...
321 (%
(+ 14 (* 11 (% displayed-year
19));; ...by Nicaean rule
322 (- ;; ...corrected for the Gregorian century rule
324 (/ ;; ...corrected for Metonic cycle inaccuracy.
325 (+ 5 (* 8 century
)) 25)
326 (* 30 century
));; Keeps value positive.
328 (adjusted-epact ;; Adjust for 29.5 day month.
329 (if (or (= shifted-epact
0)
330 (and (= shifted-epact
1) (< 10 (% displayed-year
19))))
333 (paschal-moon ;; Day after the full moon on or after March 21.
334 (- (calendar-absolute-from-gregorian (list 4 19 displayed-year
))
336 (abs-easter (calendar-dayname-on-or-before 0 (+ paschal-moon
7)))
339 (list (calendar-gregorian-from-absolute abs-easter
)
341 (list (calendar-gregorian-from-absolute (- abs-easter
2))
343 (list (calendar-gregorian-from-absolute (- abs-easter
46))
347 (list (calendar-gregorian-from-absolute (- abs-easter
63))
348 "Septuagesima Sunday")
349 (list (calendar-gregorian-from-absolute (- abs-easter
56))
351 (list (calendar-gregorian-from-absolute (- abs-easter
49))
353 (list (calendar-gregorian-from-absolute (- abs-easter
48))
355 (list (calendar-gregorian-from-absolute (- abs-easter
47))
357 (list (calendar-gregorian-from-absolute (- abs-easter
14))
359 (list (calendar-gregorian-from-absolute (- abs-easter
7))
361 (list (calendar-gregorian-from-absolute (- abs-easter
3))
363 (list (calendar-gregorian-from-absolute (+ abs-easter
35))
365 (list (calendar-gregorian-from-absolute (+ abs-easter
39))
367 (list (calendar-gregorian-from-absolute (+ abs-easter
49))
368 "Pentecost (Whitsunday)")
369 (list (calendar-gregorian-from-absolute (+ abs-easter
50))
371 (list (calendar-gregorian-from-absolute (+ abs-easter
56))
373 (list (calendar-gregorian-from-absolute (+ abs-easter
60))
376 (filter-visible-calendar-holidays mandatory
)))
377 (if all-christian-calendar-holidays
380 (filter-visible-calendar-holidays optional
)
384 (defun calendar-holiday-function-rosh-hashanah-etc ()
385 "List of dates related to Rosh Hashanah, as visible in calendar window."
386 (if (or (< displayed-month
8)
387 (> displayed-month
11))
388 nil
;; None of the dates is visible
389 (let* ((abs-r-h (calendar-absolute-from-hebrew
390 (list 7 1 (+ displayed-year
3761))))
393 (list (calendar-gregorian-from-absolute abs-r-h
)
394 (format "Rosh HaShanah %d" (+ 3761 displayed-year
)))
395 (list (calendar-gregorian-from-absolute (+ abs-r-h
9))
397 (list (calendar-gregorian-from-absolute (+ abs-r-h
14))
399 (list (calendar-gregorian-from-absolute (+ abs-r-h
21))
401 (list (calendar-gregorian-from-absolute (+ abs-r-h
22))
405 (list (calendar-gregorian-from-absolute
406 (calendar-dayname-on-or-before 6 (- abs-r-h
4)))
408 (list (calendar-gregorian-from-absolute (1- abs-r-h
))
409 "Erev Rosh HaShannah")
410 (list (calendar-gregorian-from-absolute (1+ abs-r-h
))
411 "Rosh HaShanah (second day)")
412 (list (calendar-gregorian-from-absolute
413 (if (= (% abs-r-h
7) 4) (+ abs-r-h
3) (+ abs-r-h
2)))
415 (list (calendar-gregorian-from-absolute
416 (calendar-dayname-on-or-before 6 (+ 7 abs-r-h
)))
418 (list (calendar-gregorian-from-absolute (+ abs-r-h
8))
420 (list (calendar-gregorian-from-absolute (+ abs-r-h
13))
422 (list (calendar-gregorian-from-absolute (+ abs-r-h
15))
423 "Sukkot (second day)")
424 (list (calendar-gregorian-from-absolute (+ abs-r-h
16))
425 "Hol Hamoed Sukkot (first day)")
426 (list (calendar-gregorian-from-absolute (+ abs-r-h
17))
427 "Hol Hamoed Sukkot (second day)")
428 (list (calendar-gregorian-from-absolute (+ abs-r-h
18))
429 "Hol Hamoed Sukkot (third day)")
430 (list (calendar-gregorian-from-absolute (+ abs-r-h
19))
431 "Hol Hamoed Sukkot (fourth day)")
432 (list (calendar-gregorian-from-absolute (+ abs-r-h
20))
433 "Hoshannah Rabbah")))
435 (filter-visible-calendar-holidays mandatory
)))
436 (if all-hebrew-calendar-holidays
439 (filter-visible-calendar-holidays optional
)
443 (defun calendar-holiday-function-hanukkah ()
444 "List of dates related to Hanukkah, as visible in calendar window."
445 (if (memq displayed-month
;; This test is only to speed things up a bit;
446 '(10 11 12 1 2));; it works fine without the test too.
447 (let ((m displayed-month
)
449 (increment-calendar-month m y
1)
450 (let* ((h-y (extract-calendar-year
451 (calendar-hebrew-from-absolute
452 (calendar-absolute-from-gregorian
453 (list m
(calendar-last-day-of-month m y
) y
)))))
454 (abs-h (calendar-absolute-from-hebrew (list 9 25 h-y
))))
455 (filter-visible-calendar-holidays
457 (list (calendar-gregorian-from-absolute (1- abs-h
))
459 (list (calendar-gregorian-from-absolute abs-h
)
460 "Hanukkah (first day)")
461 (list (calendar-gregorian-from-absolute (1+ abs-h
))
462 "Hanukkah (second day)")
463 (list (calendar-gregorian-from-absolute (+ abs-h
2))
464 "Hanukkah (third day)")
465 (list (calendar-gregorian-from-absolute (+ abs-h
3))
466 "Hanukkah (fourth day)")
467 (list (calendar-gregorian-from-absolute (+ abs-h
4))
468 "Hanukkah (fifth day)")
469 (list (calendar-gregorian-from-absolute (+ abs-h
5))
470 "Hanukkah (sixth day)")
471 (list (calendar-gregorian-from-absolute (+ abs-h
6))
472 "Hanukkah (seventh day)")
473 (list (calendar-gregorian-from-absolute (+ abs-h
7))
474 "Hanukkah (eighth day)")))))))
476 (defun calendar-holiday-function-passover-etc ()
477 "List of dates related to Passover, as visible in calendar window."
478 (if (< 7 displayed-month
)
479 nil
;; None of the dates is visible
480 (let* ((abs-p (calendar-absolute-from-hebrew
481 (list 1 15 (+ displayed-year
3760))))
484 (list (calendar-gregorian-from-absolute abs-p
)
486 (list (calendar-gregorian-from-absolute (+ abs-p
50))
490 (list (calendar-gregorian-from-absolute
491 (calendar-dayname-on-or-before 6 (- abs-p
43)))
493 (list (calendar-gregorian-from-absolute
494 (calendar-dayname-on-or-before 6 (- abs-p
30)))
496 (list (calendar-gregorian-from-absolute
497 (if (= (% abs-p
7) 2) (- abs-p
33) (- abs-p
31)))
499 (list (calendar-gregorian-from-absolute (- abs-p
31))
501 (list (calendar-gregorian-from-absolute (- abs-p
30))
503 (list (calendar-gregorian-from-absolute
504 (if (zerop (% abs-p
7)) (- abs-p
28) (- abs-p
29)))
506 (list (calendar-gregorian-from-absolute
507 (- (calendar-dayname-on-or-before 6 (- abs-p
14)) 7))
509 (list (calendar-gregorian-from-absolute
510 (calendar-dayname-on-or-before 6 (- abs-p
14)))
512 (list (calendar-gregorian-from-absolute
513 (calendar-dayname-on-or-before 6 (1- abs-p
)))
515 (list (calendar-gregorian-from-absolute (1- abs-p
))
517 (list (calendar-gregorian-from-absolute (1+ abs-p
))
518 "Passover (second day)")
519 (list (calendar-gregorian-from-absolute (+ abs-p
2))
520 "Hol Hamoed Passover (first day)")
521 (list (calendar-gregorian-from-absolute (+ abs-p
3))
522 "Hol Hamoed Passover (second day)")
523 (list (calendar-gregorian-from-absolute (+ abs-p
4))
524 "Hol Hamoed Passover (third day)")
525 (list (calendar-gregorian-from-absolute (+ abs-p
5))
526 "Hol Hamoed Passover (fourth day)")
527 (list (calendar-gregorian-from-absolute (+ abs-p
6))
528 "Passover (seventh day)")
529 (list (calendar-gregorian-from-absolute (+ abs-p
7))
530 "Passover (eighth day)")
531 (list (calendar-gregorian-from-absolute (+ abs-p
12))
533 (list (calendar-gregorian-from-absolute
534 (if (zerop (% abs-p
7))
536 (if (= (% abs-p
7) 6)
540 (list (calendar-gregorian-from-absolute (+ abs-p
33))
542 (list (calendar-gregorian-from-absolute (+ abs-p
43))
544 (list (calendar-gregorian-from-absolute (+ abs-p
49))
546 (list (calendar-gregorian-from-absolute (+ abs-p
51))
547 "Shavuot (second day)")))
549 (filter-visible-calendar-holidays mandatory
)))
550 (if all-hebrew-calendar-holidays
553 (filter-visible-calendar-holidays optional
)
557 (defun calendar-holiday-function-tisha-b-av-etc ()
558 "List of dates around Tisha B'Av, as visible in calendar window."
559 (if (or (< displayed-month
5)
560 (> displayed-month
9))
561 nil
;; None of the dates is visible
562 (let* ((abs-t-a (calendar-absolute-from-hebrew
563 (list 5 9 (+ displayed-year
3760)))))
565 (filter-visible-calendar-holidays
567 (list (calendar-gregorian-from-absolute
568 (if (= (% abs-t-a
7) 6) (- abs-t-a
20) (- abs-t-a
21)))
570 (list (calendar-gregorian-from-absolute
571 (calendar-dayname-on-or-before 6 abs-t-a
))
573 (list (calendar-gregorian-from-absolute
574 (if (= (% abs-t-a
7) 6) (1+ abs-t-a
) abs-t-a
))
576 (list (calendar-gregorian-from-absolute
577 (calendar-dayname-on-or-before 6 (+ abs-t-a
7)))
578 "Shabbat Nahamu"))))))
580 (defun filter-visible-calendar-holidays (l)
581 "Return a list of all visible holidays of those on L."
585 (if (calendar-date-is-visible-p (car (car p
)))
586 (setq visible
(append (list (car p
)) visible
)))
592 ;;; holidays.el ends here