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)
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.
29 ;; This collection of functions implements the holiday features as described
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),
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).
50 (defvar displayed-month
)
51 (defvar displayed-year
)
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."
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."
104 (let* ((completion-ignore-case t
)
106 (calendar-read-date t
)
107 (calendar-current-date)))
108 (displayed-month (extract-calendar-month date
))
109 (displayed-year (extract-calendar-year date
)))
110 (list-calendar-holidays))))
113 (defun list-holidays (y1 y2
&optional l label
)
114 "Display holidays for years Y1 to Y2 (inclusive).
116 The optional list of holidays L defaults to `calendar-holidays'.
117 If you want to control what holidays are displayed, use a
118 different list. For example,
120 (list-holidays 2006 2006
121 (append general-holidays local-holidays other-holidays))
123 will display holidays for the year 2006 defined in the 3
124 mentioned lists, and nothing else.
126 When called interactively, this command offers a choice of
127 holidays, based on the variables `solar-holidays' etc. See the
128 documentation of `calendar-holidays' for a list of the variables
129 that control the choices, as well as a description of the format
132 The optional LABEL is used to label the buffer created."
134 (let* ((start-year (calendar-read
135 "Starting year of holidays (>0): "
136 '(lambda (x) (> x
0))
137 (int-to-string (extract-calendar-year
138 (calendar-current-date)))))
139 (end-year (calendar-read
140 (format "Ending year (inclusive) of holidays (>=%s): "
142 '(lambda (x) (>= x start-year
))
143 (int-to-string start-year
)))
144 (completion-ignore-case t
)
147 (cons "All" calendar-holidays
)
149 (cons "Equinoxes/Solstices"
150 (list (list 'solar-equinoxes-solstices
))))
151 (if general-holidays
(cons "General" general-holidays
))
152 (if local-holidays
(cons "Local" local-holidays
))
153 (if other-holidays
(cons "Other" other-holidays
))
154 (if christian-holidays
(cons "Christian" christian-holidays
))
155 (if hebrew-holidays
(cons "Hebrew" hebrew-holidays
))
156 (if islamic-holidays
(cons "Islamic" islamic-holidays
))
157 (if bahai-holidays
(cons "Baha'i" bahai-holidays
))
158 (if oriental-holidays
(cons "Oriental" oriental-holidays
))
159 (if solar-holidays
(cons "Solar" solar-holidays
))
162 (completing-read "List (TAB for choices): " lists nil t
)))
163 (which (if (string-equal choice
"Ask")
164 (eval (read-variable "Enter list name: "))
165 (cdr (assoc choice lists
))))
166 (name (if (string-equal choice
"Equinoxes/Solstices")
168 (if (member choice
'("Ask" ""))
170 (format "%s Holidays" choice
)))))
171 (list start-year end-year which name
)))
172 (message "Computing holidays...")
173 (let* ((holiday-buffer "*Holidays*")
174 (calendar-holidays (if l l calendar-holidays
))
175 (title (or label
"Holidays"))
177 (s (calendar-absolute-from-gregorian (list 2 1 y1
)))
178 (e (calendar-absolute-from-gregorian (list 11 1 y2
)))
183 (while (or never
(<= d e
))
184 (setq holiday-list
(append holiday-list
(calendar-holiday-list)))
186 (increment-calendar-month displayed-month displayed-year
3)
187 (setq d
(calendar-absolute-from-gregorian
188 (list displayed-month
1 displayed-year
))))
190 (set-buffer (get-buffer-create holiday-buffer
))
191 (setq buffer-read-only nil
)
192 (calendar-set-mode-line
194 (format "%s for %s" title y1
)
195 (format "%s for %s-%s" title y1 y2
)))
197 (goto-char (point-min))
200 '(lambda (x) (concat (calendar-date-string (car x
))
203 (goto-char (point-min))
204 (set-buffer-modified-p nil
)
205 (setq buffer-read-only t
)
206 (display-buffer holiday-buffer
)
207 (message "Computing holidays...done"))))
210 (defun check-calendar-holidays (date)
211 "Check the list of holidays for any that occur on DATE.
212 The value returned is a list of strings of relevant holiday descriptions.
213 The holidays are those in the list calendar-holidays."
214 (let* ((displayed-month (extract-calendar-month date
))
215 (displayed-year (extract-calendar-year date
))
216 (h (calendar-holiday-list))
219 (if (calendar-date-equal date
(car (car h
)))
220 (setq holiday-list
(append holiday-list
(cdr (car h
)))))
224 (defun calendar-cursor-holidays ()
225 "Find holidays for the date specified by the cursor in the calendar window."
227 (message "Checking holidays...")
228 (let* ((date (calendar-cursor-to-date t
))
229 (date-string (calendar-date-string date
))
230 (holiday-list (check-calendar-holidays date
))
231 (holiday-string (mapconcat 'identity holiday-list
"; "))
232 (msg (format "%s: %s" date-string holiday-string
)))
233 (if (not holiday-list
)
234 (message "No holidays known for %s" date-string
)
235 (if (<= (length msg
) (frame-width))
237 (set-buffer (get-buffer-create holiday-buffer
))
238 (setq buffer-read-only nil
)
239 (calendar-set-mode-line date-string
)
241 (insert (mapconcat 'identity holiday-list
"\n"))
242 (goto-char (point-min))
243 (set-buffer-modified-p nil
)
244 (setq buffer-read-only t
)
245 (display-buffer holiday-buffer
)
246 (message "Checking holidays...done")))))
248 (defun mark-calendar-holidays ()
249 "Mark notable days in the calendar window."
251 (setq mark-holidays-in-calendar t
)
252 (message "Marking holidays...")
253 (let ((holiday-list (calendar-holiday-list)))
255 (mark-visible-calendar-date
256 (car (car holiday-list
)) calendar-holiday-marker
)
257 (setq holiday-list
(cdr holiday-list
))))
258 (message "Marking holidays...done"))
260 (defun list-calendar-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."
265 (message "Looking up holidays...")
266 (let ((holiday-list (calendar-holiday-list))
271 (if (not holiday-list
)
273 (message "Looking up holidays...none found")
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
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
)))
288 '(lambda (x) (concat (calendar-date-string (car x
))
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")
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 ((p calendar-holidays
)
305 (if calendar-debug-sexp
306 (let ((stack-trace-on-error t
))
311 (message "Bad holiday list item: %s" (car p
))
314 (setq holiday-list
(append holidays holiday-list
))))
316 (setq holiday-list
(sort holiday-list
'calendar-date-compare
))))
318 ;; Below are the functions that calculate the dates of holidays; these
319 ;; are eval'ed in the function calendar-holiday-list. If you
320 ;; write other such functions, be sure to imitate the style used below.
321 ;; Remember that each function must return a list of items of the form
322 ;; ((month day year) string) of VISIBLE dates in the calendar window.
324 (defun holiday-fixed (month day string
)
325 "Holiday on MONTH, DAY (Gregorian) called STRING.
326 If MONTH, DAY is visible, the value returned is the list (((MONTH DAY year)
327 STRING)). Returns nil if it is not visible in the current calendar window."
328 (let ((m displayed-month
)
330 (increment-calendar-month m y
(- 11 month
))
332 (list (list (list month day y
) string
)))))
334 (defun holiday-float (month dayname n string
&optional day
)
335 "Holiday on MONTH, DAYNAME (Nth occurrence) called STRING.
336 If the Nth DAYNAME in MONTH is visible, the value returned is the list
337 \(((MONTH DAY year) STRING)).
339 If N<0, count backward from the end of MONTH.
341 An optional parameter DAY means the Nth DAYNAME on or after/before MONTH DAY.
343 Returns nil if it is not visible in the current calendar window."
344 ;; This is messy because the holiday may be visible, while the date on which
345 ;; it is based is not. For example, the first Monday after December 30 may be
346 ;; visible when January is not. For large values of |n| the problem is more
347 ;; grotesque. If we didn't have to worry about such cases, we could just use
349 ;; (let ((m displayed-month)
350 ;; (y displayed-year))
351 ;; (increment-calendar-month m y (- 11 month))
352 ;; (if (> m 9); month in year y is visible
353 ;; (list (list (calendar-nth-named-day n dayname month y day) string)))))
355 ;; which is the way the function was originally written.
357 (let* ((m1 displayed-month
)
361 (increment-calendar-month m1 y1 -
1)
362 (increment-calendar-month m2 y2
1)
363 (let* ((d1; first possible base date for holiday
364 (+ (calendar-nth-named-absday 1 dayname m1 y1
)
367 (d2; last possible base date for holiday
368 (+ (calendar-nth-named-absday -
1 dayname m2 y2
)
371 (y1 (extract-calendar-year (calendar-gregorian-from-absolute d1
)))
372 (y2 (extract-calendar-year (calendar-gregorian-from-absolute d2
)))
373 (y; year of base date
374 (if (or (= y1 y2
) (> month
9))
380 (calendar-last-day-of-month month y
))))
381 (date; base date for holiday
382 (calendar-absolute-from-gregorian (list month d y
))))
383 (if (and (<= d1 date
) (<= date d2
))
384 (list (list (calendar-nth-named-day n dayname month y d
)
387 (defun holiday-sexp (sexp string
)
388 "Sexp holiday for dates in the calendar window.
389 SEXP is an expression in variable `year' evaluates to `date'.
391 STRING is an expression in `date' that evaluates to the holiday description
394 If `date' is visible in the calendar window, the holiday STRING is on that
395 date. If date is nil, or if the date is not visible, there is no holiday."
396 (let ((m displayed-month
)
398 (increment-calendar-month m y -
1)
399 (filter-visible-calendar-holidays
403 (string (if date
(eval string
))))
404 (list (list date string
)))
407 (string (if date
(eval string
))))
408 (list (list date string
)))))))
410 (defun holiday-advent (&optional n string
)
411 "Date of Nth day after advent (named STRING), if visible in calendar window.
412 Negative values of N are interpreted as days before advent.
413 STRING is used purely for display purposes. The return value has
414 the form ((MONTH DAY YEAR) STRING), where the date is that of the
415 Nth day before or after advent.
417 For backwards compatibility, if this function is called with no
418 arguments, then it returns the value appropriate for advent itself."
419 ;; Backwards compatibility layer.
421 (holiday-advent 0 "Advent")
422 (let ((year displayed-year
)
423 (month displayed-month
))
424 (increment-calendar-month month year -
1)
425 (let ((advent (calendar-gregorian-from-absolute
427 (calendar-dayname-on-or-before
429 (calendar-absolute-from-gregorian
430 (list 12 3 year
)))))))
431 (if (calendar-date-is-visible-p advent
)
432 (list (list advent string
)))))))
434 (defun holiday-easter-etc (&optional n string
)
435 "Date of Nth day after Easter (named STRING), if visible in calendar window.
436 Negative values of N are interpreted as days before Easter.
437 STRING is used purely for display purposes. The return value has
438 the form ((MONTH DAY YEAR) STRING), where the date is that of the
439 Nth day before or after Easter.
441 For backwards compatibility, if this function is called with no
442 arguments, then it returns a list of \"standard\" Easter-related
443 holidays (with more entries if `all-christian-calendar-holidays'
445 ;; Backwards compatibility layer.
448 (dolist (elem (append
449 (if all-christian-calendar-holidays
450 '((-63 .
"Septuagesima Sunday")
451 (-56 .
"Sexagesima Sunday")
452 (-49 .
"Shrove Sunday")
453 (-48 .
"Shrove Monday")
454 (-47 .
"Shrove Tuesday")
455 (-14 .
"Passion Sunday")
457 (-3 .
"Maundy Thursday")
458 (35 .
"Rogation Sunday")
459 (39 .
"Ascension Day")
460 (49 .
"Pentecost (Whitsunday)")
462 (56 .
"Trinity Sunday")
463 (60 .
"Corpus Christi")))
464 '((0 .
"Easter Sunday")
466 (-46 .
"Ash Wednesday")))
468 ;; Filter out nil (not visible) values.
469 (if (setq res
(holiday-easter-etc (car elem
) (cdr elem
)))
470 (setq res-list
(append res res-list
)))))
471 (let* ((century (1+ (/ displayed-year
100)))
472 (shifted-epact ;; Age of moon for April 5...
473 (%
(+ 14 (* 11 (% displayed-year
19)) ;; ...by Nicaean rule
474 (- ;; ...corrected for the Gregorian century rule
476 (/ ;; ...corrected for Metonic cycle inaccuracy.
477 (+ 5 (* 8 century
)) 25)
478 (* 30 century
)) ;; Keeps value positive.
480 (adjusted-epact ;; Adjust for 29.5 day month.
481 (if (or (zerop shifted-epact
)
482 (and (= shifted-epact
1) (< 10 (% displayed-year
19))))
485 (paschal-moon ;; Day after the full moon on or after March 21.
486 (- (calendar-absolute-from-gregorian (list 4 19 displayed-year
))
488 (abs-easter (calendar-dayname-on-or-before 0 (+ paschal-moon
7))))
489 (filter-visible-calendar-holidays
490 (list (list (calendar-gregorian-from-absolute (+ abs-easter n
))
493 (defun holiday-greek-orthodox-easter ()
494 "Date of Easter according to the rule of the Council of Nicaea."
495 (let ((m displayed-month
)
497 (increment-calendar-month m y
1)
499 (extract-calendar-year
500 (calendar-julian-from-absolute
501 (calendar-absolute-from-gregorian
502 (list m
(calendar-last-day-of-month m y
) y
)))))
503 (shifted-epact ;; Age of moon for April 5.
505 (* 11 (% julian-year
19)))
507 (paschal-moon ;; Day after full moon on or after March 21.
508 (- (calendar-absolute-from-julian (list 4 19 julian-year
))
510 (nicaean-easter;; Sunday following the Paschal moon
511 (calendar-gregorian-from-absolute
512 (calendar-dayname-on-or-before 0 (+ paschal-moon
7)))))
513 (if (calendar-date-is-visible-p nicaean-easter
)
514 (list (list nicaean-easter
"Pascha (Greek Orthodox Easter)"))))))
516 (defun filter-visible-calendar-holidays (l)
517 "Return a list of all visible holidays of those on L."
522 (calendar-date-is-visible-p (car (car p
)))
523 (setq visible
(append (list (car p
)) visible
)))
529 ;;; arch-tag: 48eb3117-75a7-4dbe-8fd9-873c3cbb0d37
530 ;;; holidays.el ends here