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