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