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