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