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