1 ;;; holidays.el --- holiday functions for the calendar package
3 ;; Copyright (C) 1989, 90, 92, 93, 94, 1997 Free Software Foundation, Inc.
5 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
6 ;; Keywords: holidays, calendar
8 ;; This file is part of GNU Emacs.
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)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
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.
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 the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; This collection of functions implements the holiday features as described
30 ;; Technical details of all the calendrical calculations can be found in
31 ;; ``Calendrical Calculations'' by Nachum Dershowitz and Edward M. Reingold,
32 ;; Cambridge University Press (1997).
34 ;; An earlier version of the technical details appeared in
35 ;; ``Calendrical Calculations'' by Nachum Dershowitz and Edward M. Reingold,
36 ;; Software--Practice and Experience, Volume 20, Number 9 (September, 1990),
37 ;; pages 899-928. ``Calendrical Calculations, Part II: Three Historical
38 ;; Calendars'' by E. M. Reingold, N. Dershowitz, and S. M. Clamen,
39 ;; Software--Practice and Experience, Volume 23, Number 4 (April, 1993),
42 ;; Hard copies of these two papers can be obtained by sending email to
43 ;; reingold@cs.uiuc.edu with the SUBJECT "send-paper-cal" (no quotes) and
44 ;; the message BODY containing your mailing address (snail).
46 ;; Comments, corrections, and improvements should be sent to
47 ;; Edward M. Reingold Department of Computer Science
48 ;; (217) 333-6733 University of Illinois at Urbana-Champaign
49 ;; reingold@cs.uiuc.edu 1304 West Springfield Avenue
50 ;; Urbana, Illinois 61801
54 (defvar displayed-month
)
55 (defvar displayed-year
)
59 (autoload 'holiday-julian
"cal-julian"
60 "Holiday on MONTH, DAY (Julian) called STRING."
63 (autoload 'holiday-hebrew
"cal-hebrew"
64 "Holiday on MONTH, DAY (Hebrew) called STRING."
67 (autoload 'holiday-rosh-hashanah-etc
"cal-hebrew"
68 "List of dates related to Rosh Hashanah, as visible in calendar window."
71 (autoload 'holiday-hanukkah
"cal-hebrew"
72 "List of dates related to Hanukkah, as visible in calendar window."
75 (autoload 'holiday-passover-etc
"cal-hebrew"
76 "List of dates related to Passover, as visible in calendar window."
79 (autoload 'holiday-tisha-b-av-etc
"cal-hebrew"
80 "List of dates around Tisha B'Av, as visible in calendar window."
83 (autoload 'holiday-islamic
"cal-islam"
84 "Holiday on MONTH, DAY (Islamic) called STRING."
87 (autoload 'holiday-bahai
"cal-bahai"
88 "Holiday on MONTH, DAY (Baha'i) called STRING."
91 (autoload 'holiday-chinese-new-year
"cal-china"
92 "Date of Chinese New Year."
95 (autoload 'solar-equinoxes-solstices
"solar"
96 "Date and time of equinoxes and solstices, if visible in the calendar window.
97 Requires floating point."
101 (defun holidays (&optional arg
)
102 "Display the holidays for last month, this month, and next month.
103 If called with an optional prefix argument, prompts for month and year.
105 This function is suitable for execution in a .emacs file."
108 (let* ((completion-ignore-case t
)
110 (calendar-read-date t
)
111 (calendar-current-date)))
112 (displayed-month (extract-calendar-month date
))
113 (displayed-year (extract-calendar-year date
)))
114 (list-calendar-holidays))))
117 (defun list-holidays (y1 y2
&optional l label
)
118 "Display holidays for years Y1 to Y2 (inclusive).
120 The optional list of holidays L defaults to `calendar-holidays'. See the
121 documentation for that variable for a description of holiday lists.
123 The optional LABEL is used to label the buffer created."
125 (let* ((start-year (calendar-read
126 "Starting year of holidays (>0): "
127 '(lambda (x) (> x
0))
128 (int-to-string (extract-calendar-year
129 (calendar-current-date)))))
130 (end-year (calendar-read
131 (format "Ending year (inclusive) of holidays (>=%s): "
133 '(lambda (x) (>= x start-year
))
134 (int-to-string start-year
)))
135 (completion-ignore-case t
)
138 (cons "All" calendar-holidays
)
140 (cons "Equinoxes/Solstices"
141 (list (list 'solar-equinoxes-solstices
))))
142 (if general-holidays
(cons "General" general-holidays
))
143 (if local-holidays
(cons "Local" local-holidays
))
144 (if other-holidays
(cons "Other" other-holidays
))
145 (if christian-holidays
(cons "Christian" christian-holidays
))
146 (if hebrew-holidays
(cons "Hebrew" hebrew-holidays
))
147 (if islamic-holidays
(cons "Islamic" islamic-holidays
))
148 (if bahai-holidays
(cons "Baha'i" bahai-holidays
))
149 (if oriental-holidays
(cons "Oriental" oriental-holidays
))
150 (if solar-holidays
(cons "Solar" solar-holidays
))
153 (completing-read "List (TAB for choices): " lists nil t
)))
154 (which (if (string-equal choice
"Ask")
155 (eval (read-variable "Enter list name: "))
156 (cdr (assoc choice lists
))))
157 (name (if (string-equal choice
"Equinoxes/Solstices")
159 (if (member choice
'("Ask" ""))
161 (format "%s Holidays" choice
)))))
162 (list start-year end-year which name
)))
163 (message "Computing holidays...")
164 (let* ((holiday-buffer "*Holidays*")
165 (calendar-holidays (if l l calendar-holidays
))
166 (title (or label
"Holidays"))
168 (s (calendar-absolute-from-gregorian (list 2 1 y1
)))
169 (e (calendar-absolute-from-gregorian (list 11 1 y2
)))
174 (while (or never
(<= d e
))
175 (setq holiday-list
(append holiday-list
(calendar-holiday-list)))
177 (increment-calendar-month displayed-month displayed-year
3)
178 (setq d
(calendar-absolute-from-gregorian
179 (list displayed-month
1 displayed-year
))))
181 (set-buffer (get-buffer-create holiday-buffer
))
182 (setq buffer-read-only nil
)
183 (calendar-set-mode-line
185 (format "%s for %s" title y1
)
186 (format "%s for %s-%s" title y1 y2
)))
188 (goto-char (point-min))
191 '(lambda (x) (concat (calendar-date-string (car x
))
194 (goto-char (point-min))
195 (set-buffer-modified-p nil
)
196 (setq buffer-read-only t
)
197 (display-buffer holiday-buffer
)
198 (message "Computing holidays...done"))))
201 (defun check-calendar-holidays (date)
202 "Check the list of holidays for any that occur on DATE.
203 The value returned is a list of strings of relevant holiday descriptions.
204 The holidays are those in the list calendar-holidays."
205 (let* ((displayed-month (extract-calendar-month date
))
206 (displayed-year (extract-calendar-year date
))
207 (h (calendar-holiday-list))
210 (if (calendar-date-equal date
(car (car h
)))
211 (setq holiday-list
(append holiday-list
(cdr (car h
)))))
215 (defun calendar-cursor-holidays ()
216 "Find holidays for the date specified by the cursor in the calendar window."
218 (message "Checking holidays...")
219 (let* ((date (calendar-cursor-to-date t
))
220 (date-string (calendar-date-string date
))
221 (holiday-list (check-calendar-holidays date
))
222 (holiday-string (mapconcat 'identity holiday-list
"; "))
223 (msg (format "%s: %s" date-string holiday-string
)))
224 (if (not holiday-list
)
225 (message "No holidays known for %s" date-string
)
226 (if (<= (length msg
) (frame-width))
228 (set-buffer (get-buffer-create holiday-buffer
))
229 (setq buffer-read-only nil
)
230 (calendar-set-mode-line date-string
)
232 (insert (mapconcat 'identity holiday-list
"\n"))
233 (goto-char (point-min))
234 (set-buffer-modified-p nil
)
235 (setq buffer-read-only t
)
236 (display-buffer holiday-buffer
)
237 (message "Checking holidays...done")))))
239 (defun mark-calendar-holidays ()
240 "Mark notable days in the calendar window."
242 (setq mark-holidays-in-calendar t
)
243 (message "Marking holidays...")
244 (let ((holiday-list (calendar-holiday-list)))
246 (mark-visible-calendar-date
247 (car (car holiday-list
)) calendar-holiday-marker
)
248 (setq holiday-list
(cdr holiday-list
))))
249 (message "Marking holidays...done"))
251 (defun list-calendar-holidays ()
252 "Create a buffer containing the holidays for the current calendar window.
253 The holidays are those in the list calendar-notable-days. Returns t if any
254 holidays are found, nil if not."
256 (message "Looking up holidays...")
257 (let ((holiday-list (calendar-holiday-list))
262 (if (not holiday-list
)
264 (message "Looking up holidays...none found")
266 (set-buffer (get-buffer-create holiday-buffer
))
267 (setq buffer-read-only nil
)
268 (increment-calendar-month m1 y1 -
1)
269 (increment-calendar-month m2 y2
1)
270 (calendar-set-mode-line
272 (format "Notable Dates from %s to %s, %d%%-"
273 (calendar-month-name m1
) (calendar-month-name m2
) y2
)
274 (format "Notable Dates from %s, %d to %s, %d%%-"
275 (calendar-month-name m1
) y1
(calendar-month-name m2
) y2
)))
279 '(lambda (x) (concat (calendar-date-string (car x
))
282 (goto-char (point-min))
283 (set-buffer-modified-p nil
)
284 (setq buffer-read-only t
)
285 (display-buffer holiday-buffer
)
286 (message "Looking up holidays...done")
289 (defun calendar-holiday-list ()
290 "Form the list of holidays that occur on dates in the calendar window.
291 The holidays are those in the list calendar-holidays."
292 (let ((p calendar-holidays
)
296 (if calendar-debug-sexp
297 (let ((stack-trace-on-error t
))
302 (message "Bad holiday list item: %s" (car p
))
305 (setq holiday-list
(append holidays holiday-list
))))
307 (setq holiday-list
(sort holiday-list
'calendar-date-compare
))))
309 ;; Below are the functions that calculate the dates of holidays; these
310 ;; are eval'ed in the function calendar-holiday-list. If you
311 ;; write other such functions, be sure to imitate the style used below.
312 ;; Remember that each function must return a list of items of the form
313 ;; ((month day year) string) of VISIBLE dates in the calendar window.
315 (defun holiday-fixed (month day string
)
316 "Holiday on MONTH, DAY (Gregorian) called STRING.
317 If MONTH, DAY is visible, the value returned is the list (((MONTH DAY year)
318 STRING)). Returns nil if it is not visible in the current calendar window."
319 (let ((m displayed-month
)
321 (increment-calendar-month m y
(- 11 month
))
323 (list (list (list month day y
) string
)))))
325 (defun holiday-float (month dayname n string
&optional day
)
326 "Holiday on MONTH, DAYNAME (Nth occurrence) called STRING.
327 If the Nth DAYNAME in MONTH is visible, the value returned is the list
328 \(((MONTH DAY year) STRING)).
330 If N<0, count backward from the end of MONTH.
332 An optional parameter DAY means the Nth DAYNAME on or after/before MONTH DAY.
334 Returns nil if it is not visible in the current calendar window."
335 ;; This is messy because the holiday may be visible, while the date on which
336 ;; it is based is not. For example, the first Monday after December 30 may be
337 ;; visible when January is not. For large values of |n| the problem is more
338 ;; grotesque. If we didn't have to worry about such cases, we could just use
340 ;; (let ((m displayed-month)
341 ;; (y displayed-year))
342 ;; (increment-calendar-month m y (- 11 month))
343 ;; (if (> m 9); month in year y is visible
344 ;; (list (list (calendar-nth-named-day n dayname month y day) string)))))
346 ;; which is the way the function was originally written.
348 (let* ((m1 displayed-month
)
352 (increment-calendar-month m1 y1 -
1)
353 (increment-calendar-month m2 y2
1)
354 (let* ((d1; first possible base date for holiday
355 (+ (calendar-nth-named-absday 1 dayname m1 y1
)
358 (d2; last possible base date for holiday
359 (+ (calendar-nth-named-absday -
1 dayname m2 y2
)
362 (y1 (extract-calendar-year (calendar-gregorian-from-absolute d1
)))
363 (y2 (extract-calendar-year (calendar-gregorian-from-absolute d2
)))
364 (y; year of base date
365 (if (or (= y1 y2
) (> month
9))
371 (calendar-last-day-of-month month y
))))
372 (date; base date for holiday
373 (calendar-absolute-from-gregorian (list month d y
))))
374 (if (and (<= d1 date
) (<= date d2
))
375 (list (list (calendar-nth-named-day n dayname month y d
)
378 (defun holiday-sexp (sexp string
)
379 "Sexp holiday for dates in the calendar window.
380 SEXP is an expression in variable `year' evaluates to `date'.
382 STRING is an expression in `date' that evaluates to the holiday description
385 If `date' is visible in the calendar window, the holiday STRING is on that
386 date. If date is nil, or if the date is not visible, there is no holiday."
387 (let ((m displayed-month
)
389 (increment-calendar-month m y -
1)
390 (filter-visible-calendar-holidays
394 (string (if date
(eval string
))))
395 (list (list date string
)))
398 (string (if date
(eval string
))))
399 (list (list date string
)))))))
401 (defun holiday-advent ()
402 "Date of Advent, if visible in calendar window."
403 (let ((year displayed-year
)
404 (month displayed-month
))
405 (increment-calendar-month month year -
1)
406 (let ((advent (calendar-gregorian-from-absolute
407 (calendar-dayname-on-or-before 0
408 (calendar-absolute-from-gregorian
409 (list 12 3 year
))))))
410 (if (calendar-date-is-visible-p advent
)
411 (list (list advent
"Advent"))))))
413 (defun holiday-easter-etc ()
414 "List of dates related to Easter, as visible in calendar window."
415 (if (and (> displayed-month
5) (not all-christian-calendar-holidays
))
416 nil
;; Ash Wednesday, Good Friday, and Easter are not visible.
417 (let* ((century (1+ (/ displayed-year
100)))
418 (shifted-epact ;; Age of moon for April 5...
419 (%
(+ 14 (* 11 (% displayed-year
19));; ...by Nicaean rule
420 (- ;; ...corrected for the Gregorian century rule
422 (/ ;; ...corrected for Metonic cycle inaccuracy.
423 (+ 5 (* 8 century
)) 25)
424 (* 30 century
));; Keeps value positive.
426 (adjusted-epact ;; Adjust for 29.5 day month.
427 (if (or (= shifted-epact
0)
428 (and (= shifted-epact
1) (< 10 (% displayed-year
19))))
431 (paschal-moon ;; Day after the full moon on or after March 21.
432 (- (calendar-absolute-from-gregorian (list 4 19 displayed-year
))
434 (abs-easter (calendar-dayname-on-or-before 0 (+ paschal-moon
7)))
437 (list (calendar-gregorian-from-absolute abs-easter
)
439 (list (calendar-gregorian-from-absolute (- abs-easter
2))
441 (list (calendar-gregorian-from-absolute (- abs-easter
46))
445 (list (calendar-gregorian-from-absolute (- abs-easter
63))
446 "Septuagesima Sunday")
447 (list (calendar-gregorian-from-absolute (- abs-easter
56))
449 (list (calendar-gregorian-from-absolute (- abs-easter
49))
451 (list (calendar-gregorian-from-absolute (- abs-easter
48))
453 (list (calendar-gregorian-from-absolute (- abs-easter
47))
455 (list (calendar-gregorian-from-absolute (- abs-easter
14))
457 (list (calendar-gregorian-from-absolute (- abs-easter
7))
459 (list (calendar-gregorian-from-absolute (- abs-easter
3))
461 (list (calendar-gregorian-from-absolute (+ abs-easter
35))
463 (list (calendar-gregorian-from-absolute (+ abs-easter
39))
465 (list (calendar-gregorian-from-absolute (+ abs-easter
49))
466 "Pentecost (Whitsunday)")
467 (list (calendar-gregorian-from-absolute (+ abs-easter
50))
469 (list (calendar-gregorian-from-absolute (+ abs-easter
56))
471 (list (calendar-gregorian-from-absolute (+ abs-easter
60))
474 (filter-visible-calendar-holidays mandatory
)))
475 (if all-christian-calendar-holidays
478 (filter-visible-calendar-holidays optional
)
482 (defun holiday-greek-orthodox-easter ()
483 "Date of Easter according to the rule of the Council of Nicaea."
484 (let ((m displayed-month
)
486 (increment-calendar-month m y
1)
488 (extract-calendar-year
489 (calendar-julian-from-absolute
490 (calendar-absolute-from-gregorian
491 (list m
(calendar-last-day-of-month m y
) y
)))))
492 (shifted-epact ;; Age of moon for April 5.
494 (* 11 (% julian-year
19)))
496 (paschal-moon ;; Day after full moon on or after March 21.
497 (- (calendar-absolute-from-julian (list 4 19 julian-year
))
499 (nicaean-easter;; Sunday following the Paschal moon
500 (calendar-gregorian-from-absolute
501 (calendar-dayname-on-or-before 0 (+ paschal-moon
7)))))
502 (if (calendar-date-is-visible-p nicaean-easter
)
503 (list (list nicaean-easter
"Pascha (Greek Orthodox Easter)"))))))
505 (defun filter-visible-calendar-holidays (l)
506 "Return a list of all visible holidays of those on L."
511 (calendar-date-is-visible-p (car (car p
)))
512 (setq visible
(append (list (car p
)) visible
)))
518 ;;; arch-tag: 48eb3117-75a7-4dbe-8fd9-873c3cbb0d37
519 ;;; holidays.el ends here