(latexenc-find-file-coding-system): Don't inherit the EOL part of the
[emacs.git] / lisp / calendar / holidays.el
blob6596657d454e14483cc973d785e6487f41ffb58d
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: 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 ;; 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 (&optional n string)
404 "Date of Nth day after advent (named STRING), if visible in calendar window.
405 Negative values of N are interpreted as days before advent.
406 STRING is used purely for display purposes. The return value has
407 the form ((MONTH DAY YEAR) STRING), where the date is that of the
408 Nth day before or after advent.
410 For backwards compatibility, if this function is called with no
411 arguments, then it returns the value appropriate for advent itself."
412 ;; Backwards compatibility layer.
413 (if (not n)
414 (holiday-advent 0 "Advent")
415 (let ((year displayed-year)
416 (month displayed-month))
417 (increment-calendar-month month year -1)
418 (let ((advent (calendar-gregorian-from-absolute
419 (+ n
420 (calendar-dayname-on-or-before
422 (calendar-absolute-from-gregorian
423 (list 12 3 year)))))))
424 (if (calendar-date-is-visible-p advent)
425 (list (list advent string)))))))
427 (defun holiday-easter-etc (&optional n string)
428 "Date of Nth day after Easter (named STRING), if visible in calendar window.
429 Negative values of N are interpreted as days before Easter.
430 STRING is used purely for display purposes. The return value has
431 the form ((MONTH DAY YEAR) STRING), where the date is that of the
432 Nth day before or after Easter.
434 For backwards compatibility, if this function is called with no
435 arguments, then it returns a list of \"standard\" Easter-related
436 holidays (with more entries if `all-christian-calendar-holidays'
437 is non-nil)."
438 ;; Backwards compatibility layer.
439 (if (not n)
440 (let (res-list res)
441 (dolist (elem (append
442 (if all-christian-calendar-holidays
443 '((-63 . "Septuagesima Sunday")
444 (-56 . "Sexagesima Sunday")
445 (-49 . "Shrove Sunday")
446 (-48 . "Shrove Monday")
447 (-47 . "Shrove Tuesday")
448 (-14 . "Passion Sunday")
449 (-7 . "Palm Sunday")
450 (-3 . "Maundy Thursday")
451 (35 . "Rogation Sunday")
452 (39 . "Ascension Day")
453 (49 . "Pentecost (Whitsunday)")
454 (50 . "Whitmonday")
455 (56 . "Trinity Sunday")
456 (60 . "Corpus Christi")))
457 '((0 . "Easter Sunday")
458 (-2 . "Good Friday")
459 (-46 . "Ash Wednesday")))
460 res-list)
461 ;; Filter out nil (not visible) values.
462 (if (setq res (holiday-easter-etc (car elem) (cdr elem)))
463 (setq res-list (append res res-list)))))
464 (let* ((century (1+ (/ displayed-year 100)))
465 (shifted-epact ;; Age of moon for April 5...
466 (% (+ 14 (* 11 (% displayed-year 19)) ;; ...by Nicaean rule
467 (- ;; ...corrected for the Gregorian century rule
468 (/ (* 3 century) 4))
469 (/ ;; ...corrected for Metonic cycle inaccuracy.
470 (+ 5 (* 8 century)) 25)
471 (* 30 century)) ;; Keeps value positive.
472 30))
473 (adjusted-epact ;; Adjust for 29.5 day month.
474 (if (or (zerop shifted-epact)
475 (and (= shifted-epact 1) (< 10 (% displayed-year 19))))
476 (1+ shifted-epact)
477 shifted-epact))
478 (paschal-moon ;; Day after the full moon on or after March 21.
479 (- (calendar-absolute-from-gregorian (list 4 19 displayed-year))
480 adjusted-epact))
481 (abs-easter (calendar-dayname-on-or-before 0 (+ paschal-moon 7))))
482 (filter-visible-calendar-holidays
483 (list (list (calendar-gregorian-from-absolute (+ abs-easter n))
484 string))))))
486 (defun holiday-greek-orthodox-easter ()
487 "Date of Easter according to the rule of the Council of Nicaea."
488 (let ((m displayed-month)
489 (y displayed-year))
490 (increment-calendar-month m y 1)
491 (let* ((julian-year
492 (extract-calendar-year
493 (calendar-julian-from-absolute
494 (calendar-absolute-from-gregorian
495 (list m (calendar-last-day-of-month m y) y)))))
496 (shifted-epact ;; Age of moon for April 5.
497 (% (+ 14
498 (* 11 (% julian-year 19)))
499 30))
500 (paschal-moon ;; Day after full moon on or after March 21.
501 (- (calendar-absolute-from-julian (list 4 19 julian-year))
502 shifted-epact))
503 (nicaean-easter;; Sunday following the Paschal moon
504 (calendar-gregorian-from-absolute
505 (calendar-dayname-on-or-before 0 (+ paschal-moon 7)))))
506 (if (calendar-date-is-visible-p nicaean-easter)
507 (list (list nicaean-easter "Pascha (Greek Orthodox Easter)"))))))
509 (defun filter-visible-calendar-holidays (l)
510 "Return a list of all visible holidays of those on L."
511 (let ((visible)
512 (p l))
513 (while p
514 (and (car (car p))
515 (calendar-date-is-visible-p (car (car p)))
516 (setq visible (append (list (car p)) visible)))
517 (setq p (cdr p)))
518 visible))
520 (provide 'holidays)
522 ;;; arch-tag: 48eb3117-75a7-4dbe-8fd9-873c3cbb0d37
523 ;;; holidays.el ends here