(Commentary): No longer maintained by original author, bug reports as
[bpt/emacs.git] / lisp / calendar / holidays.el
1 ;;; holidays.el --- holiday functions for the calendar package
2
3 ;; Copyright (C) 1989, 1990, 1992, 1993, 1994, 1997, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
5
6 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
7 ;; Maintainer: Glenn Morris <rgm@gnu.org>
8 ;; Keywords: holidays, calendar
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
26
27 ;;; Commentary:
28
29 ;; This collection of functions implements the holiday features as described
30 ;; in calendar.el.
31
32 ;; Technical details of all the calendrical calculations can be found in
33 ;; ``Calendrical Calculations: The Millennium Edition'' by Edward M. Reingold
34 ;; and Nachum Dershowitz, Cambridge University Press (2001).
35
36 ;; An earlier version of the technical details appeared 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.
43
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).
47
48 ;;; Code:
49
50 (defvar displayed-month)
51 (defvar displayed-year)
52
53 (require 'calendar)
54
55 (autoload 'holiday-julian "cal-julian"
56 "Holiday on MONTH, DAY (Julian) called STRING."
57 t)
58
59 (autoload 'holiday-hebrew "cal-hebrew"
60 "Holiday on MONTH, DAY (Hebrew) called STRING."
61 t)
62
63 (autoload 'holiday-rosh-hashanah-etc "cal-hebrew"
64 "List of dates related to Rosh Hashanah, as visible in calendar window."
65 t)
66
67 (autoload 'holiday-hanukkah "cal-hebrew"
68 "List of dates related to Hanukkah, as visible in calendar window."
69 t)
70
71 (autoload 'holiday-passover-etc "cal-hebrew"
72 "List of dates related to Passover, as visible in calendar window."
73 t)
74
75 (autoload 'holiday-tisha-b-av-etc "cal-hebrew"
76 "List of dates around Tisha B'Av, as visible in calendar window."
77 t)
78
79 (autoload 'holiday-islamic "cal-islam"
80 "Holiday on MONTH, DAY (Islamic) called STRING."
81 t)
82
83 (autoload 'holiday-bahai "cal-bahai"
84 "Holiday on MONTH, DAY (Baha'i) called STRING."
85 t)
86
87 (autoload 'holiday-chinese-new-year "cal-china"
88 "Date of Chinese New Year."
89 t)
90
91 (autoload 'solar-equinoxes-solstices "solar"
92 "Date and time of equinoxes and solstices, if visible in the calendar window.
93 Requires floating point."
94 t)
95
96 ;;;###autoload
97 (defun holidays (&optional arg)
98 "Display the holidays for last month, this month, and next month.
99 If called with an optional prefix argument, prompts for month and year.
100
101 This function is suitable for execution in a .emacs file."
102 (interactive "P")
103 (save-excursion
104 (let* ((completion-ignore-case t)
105 (date (if arg
106 (calendar-read-date t)
107 (calendar-current-date)))
108 (displayed-month (extract-calendar-month date))
109 (displayed-year (extract-calendar-year date)))
110 (list-calendar-holidays))))
111
112 ;;;###autoload
113 (defun list-holidays (y1 y2 &optional l label)
114 "Display holidays for years Y1 to Y2 (inclusive).
115
116 The optional list of holidays L defaults to `calendar-holidays'.
117 If you want to control what holidays are displayed, use a
118 different list. For example,
119
120 (list-holidays 2006 2006
121 (append general-holidays local-holidays other-holidays))
122
123 will display holidays for the year 2006 defined in the 3
124 mentioned lists, and nothing else.
125
126 When called interactively, this command offers a choice of
127 holidays, based on the variables `solar-holidays' etc. See the
128 documentation of `calendar-holidays' for a list of the variables
129 that control the choices, as well as a description of the format
130 of a holiday list.
131
132 The optional LABEL is used to label the buffer created."
133 (interactive
134 (let* ((start-year (calendar-read
135 "Starting year of holidays (>0): "
136 '(lambda (x) (> x 0))
137 (int-to-string (extract-calendar-year
138 (calendar-current-date)))))
139 (end-year (calendar-read
140 (format "Ending year (inclusive) of holidays (>=%s): "
141 start-year)
142 '(lambda (x) (>= x start-year))
143 (int-to-string start-year)))
144 (completion-ignore-case t)
145 (lists
146 (list
147 (cons "All" calendar-holidays)
148 (if (fboundp 'atan)
149 (cons "Equinoxes/Solstices"
150 (list (list 'solar-equinoxes-solstices))))
151 (if general-holidays (cons "General" general-holidays))
152 (if local-holidays (cons "Local" local-holidays))
153 (if other-holidays (cons "Other" other-holidays))
154 (if christian-holidays (cons "Christian" christian-holidays))
155 (if hebrew-holidays (cons "Hebrew" hebrew-holidays))
156 (if islamic-holidays (cons "Islamic" islamic-holidays))
157 (if bahai-holidays (cons "Baha'i" bahai-holidays))
158 (if oriental-holidays (cons "Oriental" oriental-holidays))
159 (if solar-holidays (cons "Solar" solar-holidays))
160 (cons "Ask" nil)))
161 (choice (capitalize
162 (completing-read "List (TAB for choices): " lists nil t)))
163 (which (if (string-equal choice "Ask")
164 (eval (read-variable "Enter list name: "))
165 (cdr (assoc choice lists))))
166 (name (if (string-equal choice "Equinoxes/Solstices")
167 choice
168 (if (member choice '("Ask" ""))
169 "Holidays"
170 (format "%s Holidays" choice)))))
171 (list start-year end-year which name)))
172 (message "Computing holidays...")
173 (let* ((holiday-buffer "*Holidays*")
174 (calendar-holidays (if l l calendar-holidays))
175 (title (or label "Holidays"))
176 (holiday-list nil)
177 (s (calendar-absolute-from-gregorian (list 2 1 y1)))
178 (e (calendar-absolute-from-gregorian (list 11 1 y2)))
179 (d s)
180 (never t)
181 (displayed-month 2)
182 (displayed-year y1))
183 (while (or never (<= d e))
184 (setq holiday-list (append holiday-list (calendar-holiday-list)))
185 (setq never nil)
186 (increment-calendar-month displayed-month displayed-year 3)
187 (setq d (calendar-absolute-from-gregorian
188 (list displayed-month 1 displayed-year))))
189 (save-excursion
190 (set-buffer (get-buffer-create holiday-buffer))
191 (setq buffer-read-only nil)
192 (calendar-set-mode-line
193 (if (= y1 y2)
194 (format "%s for %s" title y1)
195 (format "%s for %s-%s" title y1 y2)))
196 (erase-buffer)
197 (goto-char (point-min))
198 (insert
199 (mapconcat
200 '(lambda (x) (concat (calendar-date-string (car x))
201 ": " (car (cdr x))))
202 holiday-list "\n"))
203 (goto-char (point-min))
204 (set-buffer-modified-p nil)
205 (setq buffer-read-only t)
206 (display-buffer holiday-buffer)
207 (message "Computing holidays...done"))))
208
209
210 (defun check-calendar-holidays (date)
211 "Check the list of holidays for any that occur on DATE.
212 The value returned is a list of strings of relevant holiday descriptions.
213 The holidays are those in the list calendar-holidays."
214 (let* ((displayed-month (extract-calendar-month date))
215 (displayed-year (extract-calendar-year date))
216 (h (calendar-holiday-list))
217 (holiday-list))
218 (while h
219 (if (calendar-date-equal date (car (car h)))
220 (setq holiday-list (append holiday-list (cdr (car h)))))
221 (setq h (cdr h)))
222 holiday-list))
223
224 (defun calendar-cursor-holidays ()
225 "Find holidays for the date specified by the cursor in the calendar window."
226 (interactive)
227 (message "Checking holidays...")
228 (let* ((date (calendar-cursor-to-date t))
229 (date-string (calendar-date-string date))
230 (holiday-list (check-calendar-holidays date))
231 (holiday-string (mapconcat 'identity holiday-list "; "))
232 (msg (format "%s: %s" date-string holiday-string)))
233 (if (not holiday-list)
234 (message "No holidays known for %s" date-string)
235 (if (<= (length msg) (frame-width))
236 (message "%s" msg)
237 (set-buffer (get-buffer-create holiday-buffer))
238 (setq buffer-read-only nil)
239 (calendar-set-mode-line date-string)
240 (erase-buffer)
241 (insert (mapconcat 'identity holiday-list "\n"))
242 (goto-char (point-min))
243 (set-buffer-modified-p nil)
244 (setq buffer-read-only t)
245 (display-buffer holiday-buffer)
246 (message "Checking holidays...done")))))
247
248 (defun mark-calendar-holidays ()
249 "Mark notable days in the calendar window."
250 (interactive)
251 (setq mark-holidays-in-calendar t)
252 (message "Marking holidays...")
253 (let ((holiday-list (calendar-holiday-list)))
254 (while holiday-list
255 (mark-visible-calendar-date
256 (car (car holiday-list)) calendar-holiday-marker)
257 (setq holiday-list (cdr holiday-list))))
258 (message "Marking holidays...done"))
259
260 (defun list-calendar-holidays ()
261 "Create a buffer containing the holidays for the current calendar window.
262 The holidays are those in the list calendar-notable-days. Returns t if any
263 holidays are found, nil if not."
264 (interactive)
265 (message "Looking up holidays...")
266 (let ((holiday-list (calendar-holiday-list))
267 (m1 displayed-month)
268 (y1 displayed-year)
269 (m2 displayed-month)
270 (y2 displayed-year))
271 (if (not holiday-list)
272 (progn
273 (message "Looking up holidays...none found")
274 nil)
275 (set-buffer (get-buffer-create holiday-buffer))
276 (setq buffer-read-only nil)
277 (increment-calendar-month m1 y1 -1)
278 (increment-calendar-month m2 y2 1)
279 (calendar-set-mode-line
280 (if (= y1 y2)
281 (format "Notable Dates from %s to %s, %d%%-"
282 (calendar-month-name m1) (calendar-month-name m2) y2)
283 (format "Notable Dates from %s, %d to %s, %d%%-"
284 (calendar-month-name m1) y1 (calendar-month-name m2) y2)))
285 (erase-buffer)
286 (insert
287 (mapconcat
288 '(lambda (x) (concat (calendar-date-string (car x))
289 ": " (car (cdr x))))
290 holiday-list "\n"))
291 (goto-char (point-min))
292 (set-buffer-modified-p nil)
293 (setq buffer-read-only t)
294 (display-buffer holiday-buffer)
295 (message "Looking up holidays...done")
296 t)))
297
298 (defun calendar-holiday-list ()
299 "Form the list of holidays that occur on dates in the calendar window.
300 The holidays are those in the list calendar-holidays."
301 (let ((p calendar-holidays)
302 (holiday-list))
303 (while p
304 (let* ((holidays
305 (if calendar-debug-sexp
306 (let ((stack-trace-on-error t))
307 (eval (car p)))
308 (condition-case nil
309 (eval (car p))
310 (error (beep)
311 (message "Bad holiday list item: %s" (car p))
312 (sleep-for 2))))))
313 (if holidays
314 (setq holiday-list (append holidays holiday-list))))
315 (setq p (cdr p)))
316 (setq holiday-list (sort holiday-list 'calendar-date-compare))))
317
318 ;; Below are the functions that calculate the dates of holidays; these
319 ;; are eval'ed in the function calendar-holiday-list. If you
320 ;; write other such functions, be sure to imitate the style used below.
321 ;; Remember that each function must return a list of items of the form
322 ;; ((month day year) string) of VISIBLE dates in the calendar window.
323
324 (defun holiday-fixed (month day string)
325 "Holiday on MONTH, DAY (Gregorian) called STRING.
326 If MONTH, DAY is visible, the value returned is the list (((MONTH DAY year)
327 STRING)). Returns nil if it is not visible in the current calendar window."
328 (let ((m displayed-month)
329 (y displayed-year))
330 (increment-calendar-month m y (- 11 month))
331 (if (> m 9)
332 (list (list (list month day y) string)))))
333
334 (defun holiday-float (month dayname n string &optional day)
335 "Holiday on MONTH, DAYNAME (Nth occurrence) called STRING.
336 If the Nth DAYNAME in MONTH is visible, the value returned is the list
337 \(((MONTH DAY year) STRING)).
338
339 If N<0, count backward from the end of MONTH.
340
341 An optional parameter DAY means the Nth DAYNAME on or after/before MONTH DAY.
342
343 Returns nil if it is not visible in the current calendar window."
344 ;; This is messy because the holiday may be visible, while the date on which
345 ;; it is based is not. For example, the first Monday after December 30 may be
346 ;; visible when January is not. For large values of |n| the problem is more
347 ;; grotesque. If we didn't have to worry about such cases, we could just use
348
349 ;; (let ((m displayed-month)
350 ;; (y displayed-year))
351 ;; (increment-calendar-month m y (- 11 month))
352 ;; (if (> m 9); month in year y is visible
353 ;; (list (list (calendar-nth-named-day n dayname month y day) string)))))
354
355 ;; which is the way the function was originally written.
356
357 (let* ((m1 displayed-month)
358 (y1 displayed-year)
359 (m2 m1)
360 (y2 y1))
361 (increment-calendar-month m1 y1 -1)
362 (increment-calendar-month m2 y2 1)
363 (let* ((d1; first possible base date for holiday
364 (+ (calendar-nth-named-absday 1 dayname m1 y1)
365 (* -7 n)
366 (if (> n 0) 1 -7)))
367 (d2; last possible base date for holiday
368 (+ (calendar-nth-named-absday -1 dayname m2 y2)
369 (* -7 n)
370 (if (> n 0) 7 -1)))
371 (y1 (extract-calendar-year (calendar-gregorian-from-absolute d1)))
372 (y2 (extract-calendar-year (calendar-gregorian-from-absolute d2)))
373 (y; year of base date
374 (if (or (= y1 y2) (> month 9))
375 y1
376 y2))
377 (d; day of base date
378 (or day (if (> n 0)
379 1
380 (calendar-last-day-of-month month y))))
381 (date; base date for holiday
382 (calendar-absolute-from-gregorian (list month d y))))
383 (if (and (<= d1 date) (<= date d2))
384 (list (list (calendar-nth-named-day n dayname month y d)
385 string))))))
386
387 (defun holiday-sexp (sexp string)
388 "Sexp holiday for dates in the calendar window.
389 SEXP is an expression in variable `year' evaluates to `date'.
390
391 STRING is an expression in `date' that evaluates to the holiday description
392 of `date'.
393
394 If `date' is visible in the calendar window, the holiday STRING is on that
395 date. If date is nil, or if the date is not visible, there is no holiday."
396 (let ((m displayed-month)
397 (y displayed-year))
398 (increment-calendar-month m y -1)
399 (filter-visible-calendar-holidays
400 (append
401 (let* ((year y)
402 (date (eval sexp))
403 (string (if date (eval string))))
404 (list (list date string)))
405 (let* ((year (1+ y))
406 (date (eval sexp))
407 (string (if date (eval string))))
408 (list (list date string)))))))
409
410 (defun holiday-advent (&optional n string)
411 "Date of Nth day after advent (named STRING), if visible in calendar window.
412 Negative values of N are interpreted as days before advent.
413 STRING is used purely for display purposes. The return value has
414 the form ((MONTH DAY YEAR) STRING), where the date is that of the
415 Nth day before or after advent.
416
417 For backwards compatibility, if this function is called with no
418 arguments, then it returns the value appropriate for advent itself."
419 ;; Backwards compatibility layer.
420 (if (not n)
421 (holiday-advent 0 "Advent")
422 (let ((year displayed-year)
423 (month displayed-month))
424 (increment-calendar-month month year -1)
425 (let ((advent (calendar-gregorian-from-absolute
426 (+ n
427 (calendar-dayname-on-or-before
428 0
429 (calendar-absolute-from-gregorian
430 (list 12 3 year)))))))
431 (if (calendar-date-is-visible-p advent)
432 (list (list advent string)))))))
433
434 (defun holiday-easter-etc (&optional n string)
435 "Date of Nth day after Easter (named STRING), if visible in calendar window.
436 Negative values of N are interpreted as days before Easter.
437 STRING is used purely for display purposes. The return value has
438 the form ((MONTH DAY YEAR) STRING), where the date is that of the
439 Nth day before or after Easter.
440
441 For backwards compatibility, if this function is called with no
442 arguments, then it returns a list of \"standard\" Easter-related
443 holidays (with more entries if `all-christian-calendar-holidays'
444 is non-nil)."
445 ;; Backwards compatibility layer.
446 (if (not n)
447 (let (res-list res)
448 (dolist (elem (append
449 (if all-christian-calendar-holidays
450 '((-63 . "Septuagesima Sunday")
451 (-56 . "Sexagesima Sunday")
452 (-49 . "Shrove Sunday")
453 (-48 . "Shrove Monday")
454 (-47 . "Shrove Tuesday")
455 (-14 . "Passion Sunday")
456 (-7 . "Palm Sunday")
457 (-3 . "Maundy Thursday")
458 (35 . "Rogation Sunday")
459 (39 . "Ascension Day")
460 (49 . "Pentecost (Whitsunday)")
461 (50 . "Whitmonday")
462 (56 . "Trinity Sunday")
463 (60 . "Corpus Christi")))
464 '((0 . "Easter Sunday")
465 (-2 . "Good Friday")
466 (-46 . "Ash Wednesday")))
467 res-list)
468 ;; Filter out nil (not visible) values.
469 (if (setq res (holiday-easter-etc (car elem) (cdr elem)))
470 (setq res-list (append res res-list)))))
471 (let* ((century (1+ (/ displayed-year 100)))
472 (shifted-epact ;; Age of moon for April 5...
473 (% (+ 14 (* 11 (% displayed-year 19)) ;; ...by Nicaean rule
474 (- ;; ...corrected for the Gregorian century rule
475 (/ (* 3 century) 4))
476 (/ ;; ...corrected for Metonic cycle inaccuracy.
477 (+ 5 (* 8 century)) 25)
478 (* 30 century)) ;; Keeps value positive.
479 30))
480 (adjusted-epact ;; Adjust for 29.5 day month.
481 (if (or (zerop shifted-epact)
482 (and (= shifted-epact 1) (< 10 (% displayed-year 19))))
483 (1+ shifted-epact)
484 shifted-epact))
485 (paschal-moon ;; Day after the full moon on or after March 21.
486 (- (calendar-absolute-from-gregorian (list 4 19 displayed-year))
487 adjusted-epact))
488 (abs-easter (calendar-dayname-on-or-before 0 (+ paschal-moon 7))))
489 (filter-visible-calendar-holidays
490 (list (list (calendar-gregorian-from-absolute (+ abs-easter n))
491 string))))))
492
493 (defun holiday-greek-orthodox-easter ()
494 "Date of Easter according to the rule of the Council of Nicaea."
495 (let ((m displayed-month)
496 (y displayed-year))
497 (increment-calendar-month m y 1)
498 (let* ((julian-year
499 (extract-calendar-year
500 (calendar-julian-from-absolute
501 (calendar-absolute-from-gregorian
502 (list m (calendar-last-day-of-month m y) y)))))
503 (shifted-epact ;; Age of moon for April 5.
504 (% (+ 14
505 (* 11 (% julian-year 19)))
506 30))
507 (paschal-moon ;; Day after full moon on or after March 21.
508 (- (calendar-absolute-from-julian (list 4 19 julian-year))
509 shifted-epact))
510 (nicaean-easter;; Sunday following the Paschal moon
511 (calendar-gregorian-from-absolute
512 (calendar-dayname-on-or-before 0 (+ paschal-moon 7)))))
513 (if (calendar-date-is-visible-p nicaean-easter)
514 (list (list nicaean-easter "Pascha (Greek Orthodox Easter)"))))))
515
516 (defun filter-visible-calendar-holidays (l)
517 "Return a list of all visible holidays of those on L."
518 (let ((visible)
519 (p l))
520 (while p
521 (and (car (car p))
522 (calendar-date-is-visible-p (car (car p)))
523 (setq visible (append (list (car p)) visible)))
524 (setq p (cdr p)))
525 visible))
526
527 (provide 'holidays)
528
529 ;;; arch-tag: 48eb3117-75a7-4dbe-8fd9-873c3cbb0d37
530 ;;; holidays.el ends here