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