(calendar-list-holidays, list-holidays, calendar-cursor-holidays):
[emacs.git] / lisp / calendar / holidays.el
blobe591ac82328f4257acc0804563faa5e792ce6787
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 y2 &optional 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 (if (fboundp 'atan)
166 (cons "Equinoxes/Solstices"
167 (list (list 'solar-equinoxes-solstices))))
168 (if general-holidays (cons "General" general-holidays))
169 (if local-holidays (cons "Local" local-holidays))
170 (if other-holidays (cons "Other" other-holidays))
171 (if christian-holidays (cons "Christian" christian-holidays))
172 (if hebrew-holidays (cons "Hebrew" hebrew-holidays))
173 (if islamic-holidays (cons "Islamic" islamic-holidays))
174 (if bahai-holidays (cons "Baha'i" bahai-holidays))
175 (if oriental-holidays (cons "Oriental" oriental-holidays))
176 (if solar-holidays (cons "Solar" solar-holidays))
177 (cons "Ask" nil)))
178 (choice (capitalize
179 (completing-read "List (TAB for choices): " lists nil t)))
180 (which (if (string-equal choice "Ask")
181 (eval (read-variable "Enter list name: "))
182 (cdr (assoc choice lists))))
183 (name (if (string-equal choice "Equinoxes/Solstices")
184 choice
185 (if (member choice '("Ask" ""))
186 "Holidays"
187 (format "%s Holidays" choice)))))
188 (list start-year end-year which name)))
189 (message "Computing holidays...")
190 (let* ((holiday-buffer "*Holidays*")
191 (calendar-holidays (if l l calendar-holidays))
192 (title (or label "Holidays"))
193 (holiday-list nil)
194 (s (calendar-absolute-from-gregorian (list 2 1 y1)))
195 (e (calendar-absolute-from-gregorian (list 11 1 y2)))
196 (d s)
197 (never t)
198 (displayed-month 2)
199 (displayed-year y1))
200 (while (or never (<= d e))
201 (setq holiday-list (append holiday-list (calendar-holiday-list))
202 never nil)
203 (increment-calendar-month displayed-month displayed-year 3)
204 (setq d (calendar-absolute-from-gregorian
205 (list displayed-month 1 displayed-year))))
206 (save-excursion
207 (calendar-in-read-only-buffer holiday-buffer
208 (calendar-set-mode-line
209 (if (= y1 y2)
210 (format "%s for %s" title y1)
211 (format "%s for %s-%s" title y1 y2)))
212 (insert
213 (mapconcat
214 (lambda (x) (concat (calendar-date-string (car x))
215 ": " (cadr x)))
216 holiday-list "\n")))
217 (message "Computing holidays...done"))))
219 ;;;###autoload
220 (defalias 'holiday-list 'list-holidays)
222 ;;;###diary-autoload
223 (defun calendar-check-holidays (date)
224 "Check the list of holidays for any that occur on DATE.
225 The value returned is a list of strings of relevant holiday descriptions.
226 The holidays are those in the list `calendar-holidays'."
227 (let ((displayed-month (extract-calendar-month date))
228 (displayed-year (extract-calendar-year date))
229 (holiday-list))
230 (dolist (h (calendar-holiday-list))
231 (if (calendar-date-equal date (car h))
232 (setq holiday-list (append holiday-list (cdr h)))))
233 holiday-list))
235 (define-obsolete-function-alias
236 'check-calendar-holidays 'calendar-check-holidays "23.1")
238 ;;;###cal-autoload
239 (defun calendar-cursor-holidays ()
240 "Find holidays for the date specified by the cursor in the calendar window."
241 (interactive)
242 (message "Checking holidays...")
243 (let* ((date (calendar-cursor-to-date t))
244 (date-string (calendar-date-string date))
245 (holiday-list (calendar-check-holidays date))
246 (holiday-string (mapconcat 'identity holiday-list "; "))
247 (msg (format "%s: %s" date-string holiday-string)))
248 (if (not holiday-list)
249 (message "No holidays known for %s" date-string)
250 (if (<= (length msg) (frame-width))
251 (message "%s" msg)
252 (calendar-in-read-only-buffer holiday-buffer
253 (calendar-set-mode-line date-string)
254 (insert (mapconcat 'identity holiday-list "\n")))
255 (message "Checking holidays...done")))))
257 ;;;###cal-autoload
258 (defun calendar-mark-holidays ()
259 "Mark notable days in the calendar window."
260 (interactive)
261 (setq mark-holidays-in-calendar t)
262 (message "Marking holidays...")
263 (dolist (holiday (calendar-holiday-list))
264 (mark-visible-calendar-date (car holiday) calendar-holiday-marker))
265 (message "Marking holidays...done"))
267 (define-obsolete-function-alias
268 'mark-calendar-holidays 'calendar-mark-holidays "23.1")
270 ;; Below are the functions that calculate the dates of holidays; these
271 ;; are eval'ed in the function calendar-holiday-list. If you
272 ;; write other such functions, be sure to imitate the style used below.
273 ;; Remember that each function must return a list of items of the form
274 ;; ((month day year) string) of VISIBLE dates in the calendar window.
276 (defun holiday-fixed (month day string)
277 "Holiday on MONTH, DAY (Gregorian) called STRING.
278 If MONTH, DAY is visible, the value returned is the list (((MONTH DAY year)
279 STRING)). Returns nil if it is not visible in the current calendar window."
280 (let ((m displayed-month)
281 (y displayed-year))
282 (increment-calendar-month m y (- 11 month))
283 (if (> m 9)
284 (list (list (list month day y) string)))))
286 (defun holiday-float (month dayname n string &optional day)
287 "Holiday on MONTH, DAYNAME (Nth occurrence) called STRING.
288 If the Nth DAYNAME in MONTH is visible, the value returned is the list
289 \(((MONTH DAY year) STRING)).
291 If N<0, count backward from the end of MONTH.
293 An optional parameter DAY means the Nth DAYNAME on or after/before MONTH DAY.
295 Returns nil if it is not visible in the current calendar window."
296 ;; This is messy because the holiday may be visible, while the date on which
297 ;; it is based is not. For example, the first Monday after December 30 may be
298 ;; visible when January is not. For large values of |n| the problem is more
299 ;; grotesque. If we didn't have to worry about such cases, we could just use
301 ;; (let ((m displayed-month)
302 ;; (y displayed-year))
303 ;; (increment-calendar-month m y (- 11 month))
304 ;; (if (> m 9); month in year y is visible
305 ;; (list (list (calendar-nth-named-day n dayname month y day) string)))))
307 ;; which is the way the function was originally written.
309 (let* ((m1 displayed-month)
310 (y1 displayed-year)
311 (m2 m1)
312 (y2 y1))
313 (increment-calendar-month m1 y1 -1)
314 (increment-calendar-month m2 y2 1)
315 (let* ((d1 ; first possible base date for holiday
316 (+ (calendar-nth-named-absday 1 dayname m1 y1)
317 (* -7 n)
318 (if (> n 0) 1 -7)))
319 (d2 ; last possible base date for holiday
320 (+ (calendar-nth-named-absday -1 dayname m2 y2)
321 (* -7 n)
322 (if (> n 0) 7 -1)))
323 (y1 (extract-calendar-year (calendar-gregorian-from-absolute d1)))
324 (y2 (extract-calendar-year (calendar-gregorian-from-absolute d2)))
325 (y ; year of base date
326 (if (or (= y1 y2) (> month 9))
328 y2))
329 (d ; day of base date
330 (or day (if (> n 0)
332 (calendar-last-day-of-month month y))))
333 (date ; base date for holiday
334 (calendar-absolute-from-gregorian (list month d y))))
335 (if (and (<= d1 date) (<= date d2))
336 (list (list (calendar-nth-named-day n dayname month y d)
337 string))))))
339 (defun holiday-filter-visible-calendar (l)
340 "Return a list of all visible holidays of those on L."
341 (let ((visible ()))
342 (dolist (p l)
343 (and (car p)
344 (calendar-date-is-visible-p (car p))
345 (push p visible)))
346 visible))
348 (define-obsolete-function-alias
349 'filter-visible-calendar-holidays 'holiday-filter-visible-calendar "23.1")
351 (defun holiday-sexp (sexp string)
352 "Sexp holiday for dates in the calendar window.
353 SEXP is an expression in variable `year' evaluates to `date'.
355 STRING is an expression in `date' that evaluates to the holiday description
356 of `date'.
358 If `date' is visible in the calendar window, the holiday STRING is on that
359 date. If date is nil, or if the date is not visible, there is no holiday."
360 (let ((m displayed-month)
361 (y displayed-year))
362 (increment-calendar-month m y -1)
363 (holiday-filter-visible-calendar
364 (list
365 (let* ((year y)
366 (date (eval sexp))
367 (string (if date (eval string))))
368 (list date string))
369 (let* ((year (1+ y))
370 (date (eval sexp))
371 (string (if date (eval string))))
372 (list date string))))))
374 (defun holiday-advent (&optional n string)
375 "Date of Nth day after advent (named STRING), if visible in calendar window.
376 Negative values of N are interpreted as days before advent.
377 STRING is used purely for display purposes. The return value has
378 the form ((MONTH DAY YEAR) STRING), where the date is that of the
379 Nth day before or after advent.
381 For backwards compatibility, if this function is called with no
382 arguments, then it returns the value appropriate for advent itself."
383 ;; Backwards compatibility layer.
384 (if (not n)
385 (holiday-advent 0 "Advent")
386 (let ((year displayed-year)
387 (month displayed-month))
388 (increment-calendar-month month year -1)
389 (let ((advent (calendar-gregorian-from-absolute
390 (+ n
391 (calendar-dayname-on-or-before
393 (calendar-absolute-from-gregorian
394 (list 12 3 year)))))))
395 (if (calendar-date-is-visible-p advent)
396 (list (list advent string)))))))
398 (defun holiday-easter-etc (&optional n string)
399 "Date of Nth day after Easter (named STRING), if visible in calendar window.
400 Negative values of N are interpreted as days before Easter.
401 STRING is used purely for display purposes. The return value has
402 the form ((MONTH DAY YEAR) STRING), where the date is that of the
403 Nth day before or after Easter.
405 For backwards compatibility, if this function is called with no
406 arguments, then it returns a list of \"standard\" Easter-related
407 holidays (with more entries if `all-christian-calendar-holidays'
408 is non-nil)."
409 ;; Backwards compatibility layer.
410 (if (not n)
411 (let (res-list res)
412 (dolist (elem (append
413 (if all-christian-calendar-holidays
414 '((-63 . "Septuagesima Sunday")
415 (-56 . "Sexagesima Sunday")
416 (-49 . "Shrove Sunday")
417 (-48 . "Shrove Monday")
418 (-47 . "Shrove Tuesday")
419 (-14 . "Passion Sunday")
420 (-7 . "Palm Sunday")
421 (-3 . "Maundy Thursday")
422 (35 . "Rogation Sunday")
423 (39 . "Ascension Day")
424 (49 . "Pentecost (Whitsunday)")
425 (50 . "Whitmonday")
426 (56 . "Trinity Sunday")
427 (60 . "Corpus Christi")))
428 '((0 . "Easter Sunday")
429 (-2 . "Good Friday")
430 (-46 . "Ash Wednesday")))
431 res-list)
432 ;; Filter out nil (not visible) values.
433 (if (setq res (holiday-easter-etc (car elem) (cdr elem)))
434 (setq res-list (append res res-list)))))
435 (let* ((century (1+ (/ displayed-year 100)))
436 (shifted-epact ; age of moon for April 5...
437 (% (+ 14 (* 11 (% displayed-year 19)) ; ...by Nicaean rule
438 (- ; ...corrected for the Gregorian century rule
439 (/ (* 3 century) 4))
440 (/ ; ...corrected for Metonic cycle inaccuracy
441 (+ 5 (* 8 century)) 25)
442 (* 30 century)) ; keeps value positive
443 30))
444 (adjusted-epact ; adjust for 29.5 day month
445 (if (or (zerop shifted-epact)
446 (and (= shifted-epact 1) (< 10 (% displayed-year 19))))
447 (1+ shifted-epact)
448 shifted-epact))
449 (paschal-moon ; day after the full moon on or after March 21
450 (- (calendar-absolute-from-gregorian (list 4 19 displayed-year))
451 adjusted-epact))
452 (abs-easter (calendar-dayname-on-or-before 0 (+ paschal-moon 7))))
453 (holiday-filter-visible-calendar
454 (list (list (calendar-gregorian-from-absolute (+ abs-easter n))
455 string))))))
457 ;; Prior call to calendar-julian-from-absolute will autoload cal-julian.
458 (declare-function calendar-absolute-from-julian "cal-julian" (date))
460 (defun holiday-greek-orthodox-easter ()
461 "Date of Easter according to the rule of the Council of Nicaea."
462 (let ((m displayed-month)
463 (y displayed-year))
464 (increment-calendar-month m y 1)
465 (let* ((julian-year
466 (extract-calendar-year
467 (calendar-julian-from-absolute
468 (calendar-absolute-from-gregorian
469 (list m (calendar-last-day-of-month m y) y)))))
470 (shifted-epact ; age of moon for April 5
471 (% (+ 14
472 (* 11 (% julian-year 19)))
473 30))
474 (paschal-moon ; day after full moon on or after March 21
475 (- (calendar-absolute-from-julian (list 4 19 julian-year))
476 shifted-epact))
477 (nicaean-easter ; Sunday following the Paschal moon
478 (calendar-gregorian-from-absolute
479 (calendar-dayname-on-or-before 0 (+ paschal-moon 7)))))
480 (if (calendar-date-is-visible-p nicaean-easter)
481 (list (list nicaean-easter "Pascha (Greek Orthodox Easter)"))))))
483 (provide 'holidays)
485 ;; arch-tag: 48eb3117-75a7-4dbe-8fd9-873c3cbb0d37
486 ;;; holidays.el ends here