*** empty log message ***
[emacs.git] / lisp / calendar / holidays.el
bloba1adc080b2fa800e8ac81e7aa8687b2b8ec48beb
1 ;;; holidays.el --- holiday functions for the calendar package
3 ;; Copyright (C) 1989, 1990, 1992, 1993, 1994, 1997, 2004 Free
4 ;; Software Foundation, Inc.
6 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
7 ;; Maintainer: Glenn Morris <gmorris@ast.cam.ac.uk>
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 2, 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., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, 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'' by Nachum Dershowitz and Edward M. Reingold,
34 ;; Cambridge University Press (1997).
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 ;; 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
54 ;;; Code:
56 (defvar displayed-month)
57 (defvar displayed-year)
59 (require 'calendar)
61 (autoload 'holiday-julian "cal-julian"
62 "Holiday on MONTH, DAY (Julian) called STRING."
65 (autoload 'holiday-hebrew "cal-hebrew"
66 "Holiday on MONTH, DAY (Hebrew) called STRING."
69 (autoload 'holiday-rosh-hashanah-etc "cal-hebrew"
70 "List of dates related to Rosh Hashanah, as visible in calendar window."
73 (autoload 'holiday-hanukkah "cal-hebrew"
74 "List of dates related to Hanukkah, as visible in calendar window."
77 (autoload 'holiday-passover-etc "cal-hebrew"
78 "List of dates related to Passover, as visible in calendar window."
81 (autoload 'holiday-tisha-b-av-etc "cal-hebrew"
82 "List of dates around Tisha B'Av, as visible in calendar window."
85 (autoload 'holiday-islamic "cal-islam"
86 "Holiday on MONTH, DAY (Islamic) called STRING."
89 (autoload 'holiday-bahai "cal-bahai"
90 "Holiday on MONTH, DAY (Baha'i) called STRING."
93 (autoload 'holiday-chinese-new-year "cal-china"
94 "Date of Chinese New Year."
97 (autoload 'solar-equinoxes-solstices "solar"
98 "Date and time of equinoxes and solstices, if visible in the calendar window.
99 Requires floating point."
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.
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))))
118 ;;;###autoload
119 (defun list-holidays (y1 y2 &optional l label)
120 "Display holidays for years Y1 to Y2 (inclusive).
122 The optional list of holidays L defaults to `calendar-holidays'. See the
123 documentation for that variable for a description of holiday lists.
125 The optional LABEL is used to label the buffer created."
126 (interactive
127 (let* ((start-year (calendar-read
128 "Starting year of holidays (>0): "
129 '(lambda (x) (> x 0))
130 (int-to-string (extract-calendar-year
131 (calendar-current-date)))))
132 (end-year (calendar-read
133 (format "Ending year (inclusive) of holidays (>=%s): "
134 start-year)
135 '(lambda (x) (>= x start-year))
136 (int-to-string start-year)))
137 (completion-ignore-case t)
138 (lists
139 (list
140 (cons "All" calendar-holidays)
141 (if (fboundp 'atan)
142 (cons "Equinoxes/Solstices"
143 (list (list 'solar-equinoxes-solstices))))
144 (if general-holidays (cons "General" general-holidays))
145 (if local-holidays (cons "Local" local-holidays))
146 (if other-holidays (cons "Other" other-holidays))
147 (if christian-holidays (cons "Christian" christian-holidays))
148 (if hebrew-holidays (cons "Hebrew" hebrew-holidays))
149 (if islamic-holidays (cons "Islamic" islamic-holidays))
150 (if bahai-holidays (cons "Baha'i" bahai-holidays))
151 (if oriental-holidays (cons "Oriental" oriental-holidays))
152 (if solar-holidays (cons "Solar" solar-holidays))
153 (cons "Ask" nil)))
154 (choice (capitalize
155 (completing-read "List (TAB for choices): " lists nil t)))
156 (which (if (string-equal choice "Ask")
157 (eval (read-variable "Enter list name: "))
158 (cdr (assoc choice lists))))
159 (name (if (string-equal choice "Equinoxes/Solstices")
160 choice
161 (if (member choice '("Ask" ""))
162 "Holidays"
163 (format "%s Holidays" choice)))))
164 (list start-year end-year which name)))
165 (message "Computing holidays...")
166 (let* ((holiday-buffer "*Holidays*")
167 (calendar-holidays (if l l calendar-holidays))
168 (title (or label "Holidays"))
169 (holiday-list nil)
170 (s (calendar-absolute-from-gregorian (list 2 1 y1)))
171 (e (calendar-absolute-from-gregorian (list 11 1 y2)))
172 (d s)
173 (never t)
174 (displayed-month 2)
175 (displayed-year y1))
176 (while (or never (<= d e))
177 (setq holiday-list (append holiday-list (calendar-holiday-list)))
178 (setq never nil)
179 (increment-calendar-month displayed-month displayed-year 3)
180 (setq d (calendar-absolute-from-gregorian
181 (list displayed-month 1 displayed-year))))
182 (save-excursion
183 (set-buffer (get-buffer-create holiday-buffer))
184 (setq buffer-read-only nil)
185 (calendar-set-mode-line
186 (if (= y1 y2)
187 (format "%s for %s" title y1)
188 (format "%s for %s-%s" title y1 y2)))
189 (erase-buffer)
190 (goto-char (point-min))
191 (insert
192 (mapconcat
193 '(lambda (x) (concat (calendar-date-string (car x))
194 ": " (car (cdr x))))
195 holiday-list "\n"))
196 (goto-char (point-min))
197 (set-buffer-modified-p nil)
198 (setq buffer-read-only t)
199 (display-buffer holiday-buffer)
200 (message "Computing holidays...done"))))
203 (defun check-calendar-holidays (date)
204 "Check the list of holidays for any that occur on DATE.
205 The value returned is a list of strings of relevant holiday descriptions.
206 The holidays are those in the list calendar-holidays."
207 (let* ((displayed-month (extract-calendar-month date))
208 (displayed-year (extract-calendar-year date))
209 (h (calendar-holiday-list))
210 (holiday-list))
211 (while h
212 (if (calendar-date-equal date (car (car h)))
213 (setq holiday-list (append holiday-list (cdr (car h)))))
214 (setq h (cdr h)))
215 holiday-list))
217 (defun calendar-cursor-holidays ()
218 "Find holidays for the date specified by the cursor in the calendar window."
219 (interactive)
220 (message "Checking holidays...")
221 (let* ((date (calendar-cursor-to-date t))
222 (date-string (calendar-date-string date))
223 (holiday-list (check-calendar-holidays date))
224 (holiday-string (mapconcat 'identity holiday-list "; "))
225 (msg (format "%s: %s" date-string holiday-string)))
226 (if (not holiday-list)
227 (message "No holidays known for %s" date-string)
228 (if (<= (length msg) (frame-width))
229 (message "%s" msg)
230 (set-buffer (get-buffer-create holiday-buffer))
231 (setq buffer-read-only nil)
232 (calendar-set-mode-line date-string)
233 (erase-buffer)
234 (insert (mapconcat 'identity holiday-list "\n"))
235 (goto-char (point-min))
236 (set-buffer-modified-p nil)
237 (setq buffer-read-only t)
238 (display-buffer holiday-buffer)
239 (message "Checking holidays...done")))))
241 (defun mark-calendar-holidays ()
242 "Mark notable days in the calendar window."
243 (interactive)
244 (setq mark-holidays-in-calendar t)
245 (message "Marking holidays...")
246 (let ((holiday-list (calendar-holiday-list)))
247 (while holiday-list
248 (mark-visible-calendar-date
249 (car (car holiday-list)) calendar-holiday-marker)
250 (setq holiday-list (cdr holiday-list))))
251 (message "Marking holidays...done"))
253 (defun list-calendar-holidays ()
254 "Create a buffer containing the holidays for the current calendar window.
255 The holidays are those in the list calendar-notable-days. Returns t if any
256 holidays are found, nil if not."
257 (interactive)
258 (message "Looking up holidays...")
259 (let ((holiday-list (calendar-holiday-list))
260 (m1 displayed-month)
261 (y1 displayed-year)
262 (m2 displayed-month)
263 (y2 displayed-year))
264 (if (not holiday-list)
265 (progn
266 (message "Looking up holidays...none found")
267 nil)
268 (set-buffer (get-buffer-create holiday-buffer))
269 (setq buffer-read-only nil)
270 (increment-calendar-month m1 y1 -1)
271 (increment-calendar-month m2 y2 1)
272 (calendar-set-mode-line
273 (if (= y1 y2)
274 (format "Notable Dates from %s to %s, %d%%-"
275 (calendar-month-name m1) (calendar-month-name m2) y2)
276 (format "Notable Dates from %s, %d to %s, %d%%-"
277 (calendar-month-name m1) y1 (calendar-month-name m2) y2)))
278 (erase-buffer)
279 (insert
280 (mapconcat
281 '(lambda (x) (concat (calendar-date-string (car x))
282 ": " (car (cdr x))))
283 holiday-list "\n"))
284 (goto-char (point-min))
285 (set-buffer-modified-p nil)
286 (setq buffer-read-only t)
287 (display-buffer holiday-buffer)
288 (message "Looking up holidays...done")
289 t)))
291 (defun calendar-holiday-list ()
292 "Form the list of holidays that occur on dates in the calendar window.
293 The holidays are those in the list calendar-holidays."
294 (let ((p calendar-holidays)
295 (holiday-list))
296 (while p
297 (let* ((holidays
298 (if calendar-debug-sexp
299 (let ((stack-trace-on-error t))
300 (eval (car p)))
301 (condition-case nil
302 (eval (car p))
303 (error (beep)
304 (message "Bad holiday list item: %s" (car p))
305 (sleep-for 2))))))
306 (if holidays
307 (setq holiday-list (append holidays holiday-list))))
308 (setq p (cdr p)))
309 (setq holiday-list (sort holiday-list 'calendar-date-compare))))
311 ;; Below are the functions that calculate the dates of holidays; these
312 ;; are eval'ed in the function calendar-holiday-list. If you
313 ;; write other such functions, be sure to imitate the style used below.
314 ;; Remember that each function must return a list of items of the form
315 ;; ((month day year) string) of VISIBLE dates in the calendar window.
317 (defun holiday-fixed (month day string)
318 "Holiday on MONTH, DAY (Gregorian) called STRING.
319 If MONTH, DAY is visible, the value returned is the list (((MONTH DAY year)
320 STRING)). Returns nil if it is not visible in the current calendar window."
321 (let ((m displayed-month)
322 (y displayed-year))
323 (increment-calendar-month m y (- 11 month))
324 (if (> m 9)
325 (list (list (list month day y) string)))))
327 (defun holiday-float (month dayname n string &optional day)
328 "Holiday on MONTH, DAYNAME (Nth occurrence) called STRING.
329 If the Nth DAYNAME in MONTH is visible, the value returned is the list
330 \(((MONTH DAY year) STRING)).
332 If N<0, count backward from the end of MONTH.
334 An optional parameter DAY means the Nth DAYNAME on or after/before MONTH DAY.
336 Returns nil if it is not visible in the current calendar window."
337 ;; This is messy because the holiday may be visible, while the date on which
338 ;; it is based is not. For example, the first Monday after December 30 may be
339 ;; visible when January is not. For large values of |n| the problem is more
340 ;; grotesque. If we didn't have to worry about such cases, we could just use
342 ;; (let ((m displayed-month)
343 ;; (y displayed-year))
344 ;; (increment-calendar-month m y (- 11 month))
345 ;; (if (> m 9); month in year y is visible
346 ;; (list (list (calendar-nth-named-day n dayname month y day) string)))))
348 ;; which is the way the function was originally written.
350 (let* ((m1 displayed-month)
351 (y1 displayed-year)
352 (m2 m1)
353 (y2 y1))
354 (increment-calendar-month m1 y1 -1)
355 (increment-calendar-month m2 y2 1)
356 (let* ((d1; first possible base date for holiday
357 (+ (calendar-nth-named-absday 1 dayname m1 y1)
358 (* -7 n)
359 (if (> n 0) 1 -7)))
360 (d2; last possible base date for holiday
361 (+ (calendar-nth-named-absday -1 dayname m2 y2)
362 (* -7 n)
363 (if (> n 0) 7 -1)))
364 (y1 (extract-calendar-year (calendar-gregorian-from-absolute d1)))
365 (y2 (extract-calendar-year (calendar-gregorian-from-absolute d2)))
366 (y; year of base date
367 (if (or (= y1 y2) (> month 9))
369 y2))
370 (d; day of base date
371 (or day (if (> n 0)
373 (calendar-last-day-of-month month y))))
374 (date; base date for holiday
375 (calendar-absolute-from-gregorian (list month d y))))
376 (if (and (<= d1 date) (<= date d2))
377 (list (list (calendar-nth-named-day n dayname month y d)
378 string))))))
380 (defun holiday-sexp (sexp string)
381 "Sexp holiday for dates in the calendar window.
382 SEXP is an expression in variable `year' evaluates to `date'.
384 STRING is an expression in `date' that evaluates to the holiday description
385 of `date'.
387 If `date' is visible in the calendar window, the holiday STRING is on that
388 date. If date is nil, or if the date is not visible, there is no holiday."
389 (let ((m displayed-month)
390 (y displayed-year))
391 (increment-calendar-month m y -1)
392 (filter-visible-calendar-holidays
393 (append
394 (let* ((year y)
395 (date (eval sexp))
396 (string (if date (eval string))))
397 (list (list date string)))
398 (let* ((year (1+ y))
399 (date (eval sexp))
400 (string (if date (eval string))))
401 (list (list date string)))))))
403 (defun holiday-advent (n string)
404 "Date of Nth day after advent (named STRING), if visible in calendar window."
405 (let ((year displayed-year)
406 (month displayed-month))
407 (increment-calendar-month month year -1)
408 (let ((advent (calendar-gregorian-from-absolute
409 (+ n
410 (calendar-dayname-on-or-before
412 (calendar-absolute-from-gregorian
413 (list 12 3 year)))))))
414 (if (calendar-date-is-visible-p advent)
415 (list (list advent string))))))
417 (defun holiday-easter-etc (n string)
418 "Date of Nth day after Easter (named STRING), if visible in calendar window."
419 (let* ((century (1+ (/ displayed-year 100)))
420 (shifted-epact ;; Age of moon for April 5...
421 (% (+ 14 (* 11 (% displayed-year 19));; ...by Nicaean rule
422 (- ;; ...corrected for the Gregorian century rule
423 (/ (* 3 century) 4))
424 (/ ;; ...corrected for Metonic cycle inaccuracy.
425 (+ 5 (* 8 century)) 25)
426 (* 30 century));; Keeps value positive.
427 30))
428 (adjusted-epact ;; Adjust for 29.5 day month.
429 (if (or (= shifted-epact 0)
430 (and (= shifted-epact 1) (< 10 (% displayed-year 19))))
431 (1+ shifted-epact)
432 shifted-epact))
433 (paschal-moon ;; Day after the full moon on or after March 21.
434 (- (calendar-absolute-from-gregorian (list 4 19 displayed-year))
435 adjusted-epact))
436 (abs-easter (calendar-dayname-on-or-before 0 (+ paschal-moon 7)))
437 (mandatory
438 (list
439 (list (calendar-gregorian-from-absolute (+ abs-easter n))
440 string)))
441 (output-list (filter-visible-calendar-holidays mandatory)))
442 output-list))
444 (defun holiday-greek-orthodox-easter ()
445 "Date of Easter according to the rule of the Council of Nicaea."
446 (let ((m displayed-month)
447 (y displayed-year))
448 (increment-calendar-month m y 1)
449 (let* ((julian-year
450 (extract-calendar-year
451 (calendar-julian-from-absolute
452 (calendar-absolute-from-gregorian
453 (list m (calendar-last-day-of-month m y) y)))))
454 (shifted-epact ;; Age of moon for April 5.
455 (% (+ 14
456 (* 11 (% julian-year 19)))
457 30))
458 (paschal-moon ;; Day after full moon on or after March 21.
459 (- (calendar-absolute-from-julian (list 4 19 julian-year))
460 shifted-epact))
461 (nicaean-easter;; Sunday following the Paschal moon
462 (calendar-gregorian-from-absolute
463 (calendar-dayname-on-or-before 0 (+ paschal-moon 7)))))
464 (if (calendar-date-is-visible-p nicaean-easter)
465 (list (list nicaean-easter "Pascha (Greek Orthodox Easter)"))))))
467 (defun filter-visible-calendar-holidays (l)
468 "Return a list of all visible holidays of those on L."
469 (let ((visible)
470 (p l))
471 (while p
472 (and (car (car p))
473 (calendar-date-is-visible-p (car (car p)))
474 (setq visible (append (list (car p)) visible)))
475 (setq p (cdr p)))
476 visible))
478 (provide 'holidays)
480 ;;; arch-tag: 48eb3117-75a7-4dbe-8fd9-873c3cbb0d37
481 ;;; holidays.el ends here