(TAGS, tags): Include new lisp subdirectories.
[emacs.git] / lisp / calendar / holidays.el
blobeeae46b940144a99ff9c58628577af35605ccd3f
1 ;;; holidays.el --- holiday functions for the calendar package
3 ;; Copyright (C) 1989, 1990, 1992, 1993, 1994 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)
13 ;; any later version.
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.
25 ;;; Commentary:
27 ;; This collection of functions implements the holiday features as described
28 ;; in calendar.el.
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),
39 ;; pages 899-928. ``Calendrical Calculations, Part II: Three Historical
40 ;; Calendars'' by E. M. Reingold, N. Dershowitz, and S. M. Clamen,
41 ;; Software--Practice and Experience, Volume 23, Number 4 (April, 1993),
42 ;; pages 383-404.
44 ;; Hard copies of these two papers can be obtained by sending email to
45 ;; reingold@cs.uiuc.edu with the SUBJECT "send-paper-cal" (no quotes) and
46 ;; the message BODY containing your mailing address (snail).
48 ;;; Code:
50 (require 'calendar)
52 (autoload 'holiday-julian "cal-julian"
53 "Holiday on MONTH, DAY (Julian) called STRING."
56 (autoload 'holiday-hebrew "cal-hebrew"
57 "Holiday on MONTH, DAY (Hebrew) called STRING."
60 (autoload 'holiday-rosh-hashanah-etc "cal-hebrew"
61 "List of dates related to Rosh Hashanah, as visible in calendar window."
64 (autoload 'holiday-hanukkah "cal-hebrew"
65 "List of dates related to Hanukkah, as visible in calendar window."
68 (autoload 'holiday-passover-etc "cal-hebrew"
69 "List of dates related to Passover, as visible in calendar window."
72 (autoload 'holiday-tisha-b-av-etc "cal-hebrew"
73 "List of dates around Tisha B'Av, as visible in calendar window."
76 (autoload 'holiday-islamic "cal-islam"
77 "Holiday on MONTH, DAY (Islamic) called STRING."
80 (autoload 'holiday-chinese-new-year "cal-china"
81 "Date of Chinese New Year."
84 (autoload 'solar-equinoxes-solstices "solar"
85 "Date and time of equinoxes and solstices, if visible in the calendar window.
86 Requires floating point."
89 (defun holidays (&optional arg)
90 "Display the holidays for last month, this month, and next month.
91 If called with an optional prefix argument, prompts for month and year.
93 This function is suitable for execution in a .emacs file."
94 (interactive "P")
95 (save-excursion
96 (let* ((completion-ignore-case t)
97 (date (if arg
98 (calendar-read-date t)
99 (calendar-current-date)))
100 (displayed-month (extract-calendar-month date))
101 (displayed-year (extract-calendar-year date)))
102 (list-calendar-holidays))))
104 ;;;###autoload
105 (defun list-holidays (y1 y2 &optional l label)
106 "Display holidays for years Y1 to Y2 (inclusive).
108 The optional list of holidays L defaults to `calendar-holidays'. See the
109 documentation for that variable for a description of holiday lists.
111 The optional LABEL is used to label the buffer created."
112 (interactive
113 (let* ((start-year (calendar-read
114 "Starting year of holidays (>0): "
115 '(lambda (x) (> x 0))
116 (int-to-string (extract-calendar-year
117 (calendar-current-date)))))
118 (end-year (calendar-read
119 (format "Ending year (inclusive) of holidays (>=%s): "
120 start-year)
121 '(lambda (x) (>= x start-year))
122 (int-to-string start-year)))
123 (completion-ignore-case t)
124 (lists
125 (list
126 (cons "All" calendar-holidays)
127 (if (fboundp 'atan)
128 (cons "Equinoxes/Solstices"
129 (list (list 'solar-equinoxes-solstices))))
130 (if general-holidays (cons "General" general-holidays))
131 (if local-holidays (cons "Local" local-holidays))
132 (if other-holidays (cons "Other" other-holidays))
133 (if christian-holidays (cons "Christian" christian-holidays))
134 (if hebrew-holidays (cons "Hebrew" hebrew-holidays))
135 (if islamic-holidays (cons "Islamic" islamic-holidays))
136 (if oriental-holidays (cons "Oriental" oriental-holidays))
137 (if solar-holidays (cons "Solar" solar-holidays))
138 (cons "Ask" nil)))
139 (choice (capitalize
140 (completing-read "List (TAB for choices): " lists nil t)))
141 (which (if (string-equal choice "Ask")
142 (eval (read-variable "Enter list name: "))
143 (cdr (assoc choice lists))))
144 (name (if (string-equal choice "Equinoxes/Solstices")
145 choice
146 (if (member choice '("Ask" ""))
147 "Holidays"
148 (format "%s Holidays" choice)))))
149 (list start-year end-year which name)))
150 (message "Computing holidays...")
151 (let* ((holiday-buffer "*Holidays*")
152 (calendar-holidays (if l l calendar-holidays))
153 (title (or label "Holidays"))
154 (holiday-list nil)
155 (s (calendar-absolute-from-gregorian (list 2 1 y1)))
156 (e (calendar-absolute-from-gregorian (list 11 1 y2)))
157 (d s)
158 (never t)
159 (displayed-month 2)
160 (displayed-year y1))
161 (while (or never (<= d e))
162 (setq holiday-list (append holiday-list (calendar-holiday-list)))
163 (setq never nil)
164 (increment-calendar-month displayed-month displayed-year 3)
165 (setq d (calendar-absolute-from-gregorian
166 (list displayed-month 1 displayed-year))))
167 (save-excursion
168 (set-buffer (get-buffer-create holiday-buffer))
169 (setq buffer-read-only nil)
170 (calendar-set-mode-line
171 (if (= y1 y2)
172 (format "%s for %s" title y1)
173 (format "%s for %s-%s" title y1 y2)))
174 (erase-buffer)
175 (goto-char (point-min))
176 (insert
177 (mapconcat
178 '(lambda (x) (concat (calendar-date-string (car x))
179 ": " (car (cdr x))))
180 holiday-list "\n"))
181 (goto-char (point-min))
182 (set-buffer-modified-p nil)
183 (setq buffer-read-only t)
184 (display-buffer holiday-buffer)
185 (message "Computing holidays...done"))))
188 (defun check-calendar-holidays (date)
189 "Check the list of holidays for any that occur on DATE.
190 The value returned is a list of strings of relevant holiday descriptions.
191 The holidays are those in the list calendar-holidays."
192 (let* ((displayed-month (extract-calendar-month date))
193 (displayed-year (extract-calendar-year date))
194 (h (calendar-holiday-list))
195 (holiday-list))
196 (while h
197 (if (calendar-date-equal date (car (car h)))
198 (setq holiday-list (append holiday-list (cdr (car h)))))
199 (setq h (cdr h)))
200 holiday-list))
202 (defun calendar-cursor-holidays ()
203 "Find holidays for the date specified by the cursor in the calendar window."
204 (interactive)
205 (message "Checking holidays...")
206 (let* ((date (calendar-cursor-to-date t))
207 (date-string (calendar-date-string date))
208 (holiday-list (check-calendar-holidays date))
209 (holiday-string (mapconcat 'identity holiday-list "; "))
210 (msg (format "%s: %s" date-string holiday-string)))
211 (if (not holiday-list)
212 (message "No holidays known for %s" date-string)
213 (if (<= (length msg) (frame-width))
214 (message "%s" msg)
215 (set-buffer (get-buffer-create holiday-buffer))
216 (setq buffer-read-only nil)
217 (calendar-set-mode-line date-string)
218 (erase-buffer)
219 (insert (mapconcat 'identity holiday-list "\n"))
220 (goto-char (point-min))
221 (set-buffer-modified-p nil)
222 (setq buffer-read-only t)
223 (display-buffer holiday-buffer)
224 (message "Checking holidays...done")))))
226 (defun mark-calendar-holidays ()
227 "Mark notable days in the calendar window."
228 (interactive)
229 (setq mark-holidays-in-calendar t)
230 (message "Marking holidays...")
231 (let ((holiday-list (calendar-holiday-list)))
232 (while holiday-list
233 (mark-visible-calendar-date
234 (car (car holiday-list)) calendar-holiday-marker)
235 (setq holiday-list (cdr holiday-list))))
236 (message "Marking holidays...done"))
238 (defun list-calendar-holidays ()
239 "Create a buffer containing the holidays for the current calendar window.
240 The holidays are those in the list calendar-notable-days. Returns t if any
241 holidays are found, nil if not."
242 (interactive)
243 (message "Looking up holidays...")
244 (let ((holiday-list (calendar-holiday-list))
245 (m1 displayed-month)
246 (y1 displayed-year)
247 (m2 displayed-month)
248 (y2 displayed-year))
249 (if (not holiday-list)
250 (progn
251 (message "Looking up holidays...none found")
252 nil)
253 (set-buffer (get-buffer-create holiday-buffer))
254 (setq buffer-read-only nil)
255 (increment-calendar-month m1 y1 -1)
256 (increment-calendar-month m2 y2 1)
257 (calendar-set-mode-line
258 (if (= y1 y2)
259 (format "Notable Dates from %s to %s, %d%%-"
260 (calendar-month-name m1) (calendar-month-name m2) y2)
261 (format "Notable Dates from %s, %d to %s, %d%%-"
262 (calendar-month-name m1) y1 (calendar-month-name m2) y2)))
263 (erase-buffer)
264 (insert
265 (mapconcat
266 '(lambda (x) (concat (calendar-date-string (car x))
267 ": " (car (cdr x))))
268 holiday-list "\n"))
269 (goto-char (point-min))
270 (set-buffer-modified-p nil)
271 (setq buffer-read-only t)
272 (display-buffer holiday-buffer)
273 (message "Looking up holidays...done")
274 t)))
276 (defun calendar-holiday-list ()
277 "Form the list of holidays that occur on dates in the calendar window.
278 The holidays are those in the list calendar-holidays."
279 (let ((p calendar-holidays)
280 (holiday-list))
281 (while p
282 (let* ((holidays
283 (if calendar-debug-sexp
284 (let ((stack-trace-on-error t))
285 (eval (car p)))
286 (condition-case nil
287 (eval (car p))
288 (error (beep)
289 (message "Bad holiday list item: %s" (car p))
290 (sleep-for 2))))))
291 (if holidays
292 (setq holiday-list (append holidays holiday-list))))
293 (setq p (cdr p)))
294 (setq holiday-list (sort holiday-list 'calendar-date-compare))))
296 ;; Below are the functions that calculate the dates of holidays; these
297 ;; are eval'ed in the function calendar-holiday-list. If you
298 ;; write other such functions, be sure to imitate the style used below.
299 ;; Remember that each function must return a list of items of the form
300 ;; ((month day year) string) of VISIBLE dates in the calendar window.
302 (defun holiday-fixed (month day string)
303 "Holiday on MONTH, DAY (Gregorian) called STRING.
304 If MONTH, DAY is visible, the value returned is the list (((MONTH DAY year)
305 STRING)). Returns nil if it is not visible in the current calendar window."
306 (let ((m displayed-month)
307 (y displayed-year))
308 (increment-calendar-month m y (- 11 month))
309 (if (> m 9)
310 (list (list (list month day y) string)))))
312 (defun holiday-float (month dayname n string &optional day)
313 "Holiday on MONTH, DAYNAME (Nth occurrence) called STRING.
314 If the Nth DAYNAME in MONTH is visible, the value returned is the list
315 \(((MONTH DAY year) STRING)).
317 If N<0, count backward from the end of MONTH.
319 An optional parameter DAY means the Nth DAYNAME on or after/before MONTH DAY.
321 Returns nil if it is not visible in the current calendar window."
322 ;; This is messy because the holiday may be visible, while the date on which
323 ;; it is based is not. For example, the first Monday after December 30 may be
324 ;; visible when January is not. For large values of |n| the problem is more
325 ;; grotesque. If we didn't have to worry about such cases, we could just use
327 ;; (let ((m displayed-month)
328 ;; (y displayed-year))
329 ;; (increment-calendar-month m y (- 11 month))
330 ;; (if (> m 9); month in year y is visible
331 ;; (list (list (calendar-nth-named-day n dayname month y day) string)))))
333 ;; which is the way the function was originally written.
335 (let* ((m1 displayed-month)
336 (y1 displayed-year)
337 (m2 m1)
338 (y2 y1))
339 (increment-calendar-month m1 y1 -1)
340 (increment-calendar-month m2 y2 1)
341 (let* ((d1; first possible base date for holiday
342 (+ (calendar-nth-named-absday 1 dayname m1 y1)
343 (* -7 n)
344 (if (> n 0) 1 -7)))
345 (d2; last possible base date for holiday
346 (+ (calendar-nth-named-absday -1 dayname m2 y2)
347 (* -7 n)
348 (if (> n 0) 7 -1)))
349 (y1 (extract-calendar-year (calendar-gregorian-from-absolute d1)))
350 (y2 (extract-calendar-year (calendar-gregorian-from-absolute d2)))
351 (y; year of base date
352 (if (or (= y1 y2) (> month 9))
354 y2))
355 (d; day of base date
356 (or day (if (> n 0)
358 (calendar-last-day-of-month month y))))
359 (date; base date for holiday
360 (calendar-absolute-from-gregorian (list month d y))))
361 (if (and (<= d1 date) (<= date d2))
362 (list (list (calendar-nth-named-day n dayname month y d)
363 string))))))
365 (defun holiday-sexp (sexp string)
366 "Sexp holiday for dates in the calendar window.
367 SEXP is an expression in variable `year' evaluates to `date'.
369 STRING is an expression in `date' that evaluates to the holiday description
370 of `date'.
372 If `date' is visible in the calendar window, the holiday STRING is on that
373 date. If date is nil, or if the date is not visible, there is no holiday."
374 (let ((m displayed-month)
375 (y displayed-year))
376 (increment-calendar-month m y -1)
377 (filter-visible-calendar-holidays
378 (append
379 (let* ((year y)
380 (date (eval sexp))
381 (string (if date (eval string))))
382 (list (list date string)))
383 (let* ((year (1+ y))
384 (date (eval sexp))
385 (string (if date (eval string))))
386 (list (list date string)))))))
388 (defun holiday-advent ()
389 "Date of Advent, if visible in calendar window."
390 (let ((year displayed-year)
391 (month displayed-month))
392 (increment-calendar-month month year -1)
393 (let ((advent (calendar-gregorian-from-absolute
394 (calendar-dayname-on-or-before 0
395 (calendar-absolute-from-gregorian
396 (list 12 3 year))))))
397 (if (calendar-date-is-visible-p advent)
398 (list (list advent "Advent"))))))
400 (defun holiday-easter-etc ()
401 "List of dates related to Easter, as visible in calendar window."
402 (if (and (> displayed-month 5) (not all-christian-calendar-holidays))
403 nil;; Ash Wednesday, Good Friday, and Easter are not visible.
404 (let* ((century (1+ (/ displayed-year 100)))
405 (shifted-epact ;; Age of moon for April 5...
406 (% (+ 14 (* 11 (% displayed-year 19));; ...by Nicaean rule
407 (- ;; ...corrected for the Gregorian century rule
408 (/ (* 3 century) 4))
409 (/ ;; ...corrected for Metonic cycle inaccuracy.
410 (+ 5 (* 8 century)) 25)
411 (* 30 century));; Keeps value positive.
412 30))
413 (adjusted-epact ;; Adjust for 29.5 day month.
414 (if (or (= shifted-epact 0)
415 (and (= shifted-epact 1) (< 10 (% displayed-year 19))))
416 (1+ shifted-epact)
417 shifted-epact))
418 (paschal-moon ;; Day after the full moon on or after March 21.
419 (- (calendar-absolute-from-gregorian (list 4 19 displayed-year))
420 adjusted-epact))
421 (abs-easter (calendar-dayname-on-or-before 0 (+ paschal-moon 7)))
422 (mandatory
423 (list
424 (list (calendar-gregorian-from-absolute abs-easter)
425 "Easter Sunday")
426 (list (calendar-gregorian-from-absolute (- abs-easter 2))
427 "Good Friday")
428 (list (calendar-gregorian-from-absolute (- abs-easter 46))
429 "Ash Wednesday")))
430 (optional
431 (list
432 (list (calendar-gregorian-from-absolute (- abs-easter 63))
433 "Septuagesima Sunday")
434 (list (calendar-gregorian-from-absolute (- abs-easter 56))
435 "Sexagesima Sunday")
436 (list (calendar-gregorian-from-absolute (- abs-easter 49))
437 "Shrove Sunday")
438 (list (calendar-gregorian-from-absolute (- abs-easter 48))
439 "Shrove Monday")
440 (list (calendar-gregorian-from-absolute (- abs-easter 47))
441 "Shrove Tuesday")
442 (list (calendar-gregorian-from-absolute (- abs-easter 14))
443 "Passion Sunday")
444 (list (calendar-gregorian-from-absolute (- abs-easter 7))
445 "Palm Sunday")
446 (list (calendar-gregorian-from-absolute (- abs-easter 3))
447 "Maundy Thursday")
448 (list (calendar-gregorian-from-absolute (+ abs-easter 35))
449 "Rogation Sunday")
450 (list (calendar-gregorian-from-absolute (+ abs-easter 39))
451 "Ascension Day")
452 (list (calendar-gregorian-from-absolute (+ abs-easter 49))
453 "Pentecost (Whitsunday)")
454 (list (calendar-gregorian-from-absolute (+ abs-easter 50))
455 "Whitmonday")
456 (list (calendar-gregorian-from-absolute (+ abs-easter 56))
457 "Trinity Sunday")
458 (list (calendar-gregorian-from-absolute (+ abs-easter 60))
459 "Corpus Christi")))
460 (output-list
461 (filter-visible-calendar-holidays mandatory)))
462 (if all-christian-calendar-holidays
463 (setq output-list
464 (append
465 (filter-visible-calendar-holidays optional)
466 output-list)))
467 output-list)))
469 (defun holiday-greek-orthodox-easter ()
470 "Date of Easter according to the rule of the Council of Nicaea."
471 (let ((m displayed-month)
472 (y displayed-year))
473 (increment-calendar-month m y 1)
474 (let* ((julian-year
475 (extract-calendar-year
476 (calendar-julian-from-absolute
477 (calendar-absolute-from-gregorian
478 (list m (calendar-last-day-of-month m y) y)))))
479 (shifted-epact ;; Age of moon for April 5.
480 (% (+ 14
481 (* 11 (% julian-year 19)))
482 30))
483 (paschal-moon ;; Day after full moon on or after March 21.
484 (- (calendar-absolute-from-julian (list 4 19 julian-year))
485 shifted-epact))
486 (nicaean-easter;; Sunday following the Paschal moon
487 (calendar-gregorian-from-absolute
488 (calendar-dayname-on-or-before 0 (+ paschal-moon 7)))))
489 (if (calendar-date-is-visible-p nicaean-easter)
490 (list (list nicaean-easter "Pascha (Greek Orthodox Easter)"))))))
492 (defun filter-visible-calendar-holidays (l)
493 "Return a list of all visible holidays of those on L."
494 (let ((visible)
495 (p l))
496 (while p
497 (and (car (car p))
498 (calendar-date-is-visible-p (car (car p)))
499 (setq visible (append (list (car p)) visible)))
500 (setq p (cdr p)))
501 visible))
503 (provide 'holidays)
505 ;;; holidays.el ends here