(list-holidays): Fix buffer title when list is
[emacs.git] / lisp / calendar / holidays.el
blobe7077e96ecf9cf6809c083ee69c5d4439a7534b7
1 ;;; holidays.el --- holiday functions for the calendar package
3 ;; Copyright (C) 1989, 1990, 1992, 1993, 1994 Free Software Foundation, Inc.
5 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
6 ;; Keywords: holidays, calendar
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
25 ;;; Commentary:
27 ;; This collection of functions implements the holiday features as described
28 ;; in calendar.el.
30 ;; Comments, corrections, and improvements should be sent to
31 ;; Edward M. Reingold Department of Computer Science
32 ;; (217) 333-6733 University of Illinois at Urbana-Champaign
33 ;; reingold@cs.uiuc.edu 1304 West Springfield Avenue
34 ;; Urbana, Illinois 61801
36 ;; Technical details of all the calendrical calculations can be found 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 (autoload 'holiday-julian "cal-julian"
53 "Holiday on MONTH, DAY (Julian) called STRING."
56 (autoload 'holiday-hebrew "cal-hebrew"
57 "Holiday on MONTH, DAY (Hebrew) called STRING."
60 (autoload 'holiday-rosh-hashanah-etc "cal-hebrew"
61 "List of dates related to Rosh Hashanah, as visible in calendar window."
64 (autoload 'holiday-hanukkah "cal-hebrew"
65 "List of dates related to Hanukkah, as visible in calendar window."
68 (autoload 'holiday-passover-etc "cal-hebrew"
69 "List of dates related to Passover, as visible in calendar window."
72 (autoload 'holiday-tisha-b-av-etc "cal-hebrew"
73 "List of dates around Tisha B'Av, as visible in calendar window."
76 (autoload 'holiday-islamic "cal-islam"
77 "Holiday on MONTH, DAY (Islamic) called STRING."
80 (autoload 'holiday-chinese-new-year "cal-china"
81 "Date of Chinese New Year."
84 (autoload 'solar-equinoxes-solstices "solar"
85 "Date and time of equinoxes and solstices, if visible in the calendar window.
86 Requires floating point."
89 (defun holidays (&optional arg)
90 "Display the holidays for last month, this month, and next month.
91 If called with an optional prefix argument, prompts for month and year.
93 This function is suitable for execution in a .emacs file."
94 (interactive "P")
95 (save-excursion
96 (let* ((completion-ignore-case t)
97 (date (if arg
98 (calendar-read-date t)
99 (calendar-current-date)))
100 (displayed-month (extract-calendar-month date))
101 (displayed-year (extract-calendar-year date)))
102 (list-calendar-holidays))))
104 ;;;###autoload
105 (defun list-holidays (y1 y2 &optional l label)
106 "Display holidays for years Y1 to Y2 (inclusive).
108 The optional list of holidays L defaults to `calendar-holidays'. See the
109 documentation for that variable for a description of holiday lists.
111 The optional LABEL is used to label the buffer created."
112 (interactive
113 (let* ((start-year (calendar-read
114 "Starting year of holidays (>0): "
115 '(lambda (x) (> x 0))
116 (int-to-string (extract-calendar-year
117 (calendar-current-date)))))
118 (end-year (calendar-read
119 (format "Ending year (inclusive) of holidays (>=%s): "
120 start-year)
121 '(lambda (x) (>= x start-year))
122 (int-to-string start-year)))
123 (completion-ignore-case t)
124 (lists
125 (list
126 (cons "All" calendar-holidays)
127 (if (fboundp 'atan)
128 (cons "Equinoxes/Solstices"
129 (list (list 'solar-equinoxes-solstices))))
130 (if general-holidays (cons "General" general-holidays))
131 (if local-holidays (cons "Local" local-holidays))
132 (if other-holidays (cons "Other" other-holidays))
133 (if christian-holidays (cons "Christian" christian-holidays))
134 (if hebrew-holidays (cons "Hebrew" hebrew-holidays))
135 (if islamic-holidays (cons "Islamic" islamic-holidays))
136 (if oriental-holidays (cons "Oriental" oriental-holidays))
137 (if solar-holidays (cons "Solar" solar-holidays))
138 (cons "Ask" nil)))
139 (choice (capitalize
140 (completing-read "List (TAB for choices): " lists nil t)))
141 (which (if (string-equal choice "Ask")
142 (eval (read-variable "Enter list name: "))
143 (cdr (assoc choice lists))))
144 (name (if (string-equal choice "Equinoxes/Solstices")
145 choice
146 (if (member choice '("Ask" ""))
147 "Holidays"
148 (format "%s Holidays" choice)))))
149 (list start-year end-year which name)))
150 (message "Computing holidays...")
151 (let* ((holiday-buffer "*Holidays*")
152 (calendar-holidays (if l l calendar-holidays))
153 (title (or label "Holidays"))
154 (holiday-list nil)
155 (s (calendar-absolute-from-gregorian (list 2 1 y1)))
156 (e (calendar-absolute-from-gregorian (list 11 1 y2)))
157 (d s)
158 (never t)
159 (displayed-month 2)
160 (displayed-year y1))
161 (while (or never (<= d e))
162 (setq holiday-list (append holiday-list (calendar-holiday-list)))
163 (setq never nil)
164 (increment-calendar-month displayed-month displayed-year 3)
165 (setq d (calendar-absolute-from-gregorian
166 (list displayed-month 1 displayed-year))))
167 (save-excursion
168 (set-buffer (get-buffer-create holiday-buffer))
169 (setq buffer-read-only nil)
170 (calendar-set-mode-line
171 (if (= y1 y2)
172 (format "%s for %s" title y1)
173 (format "%s for %s-%s" title y1 y2)))
174 (erase-buffer)
175 (goto-char (point-min))
176 (insert
177 (mapconcat
178 '(lambda (x) (concat (calendar-date-string (car x))
179 ": " (car (cdr x))))
180 holiday-list "\n"))
181 (goto-char (point-min))
182 (set-buffer-modified-p nil)
183 (setq buffer-read-only t)
184 (display-buffer holiday-buffer)
185 (message "Computing holidays...done"))))
188 (defun check-calendar-holidays (date)
189 "Check the list of holidays for any that occur on DATE.
190 The value returned is a list of strings of relevant holiday descriptions.
191 The holidays are those in the list calendar-holidays."
192 (let* ((displayed-month (extract-calendar-month date))
193 (displayed-year (extract-calendar-year date))
194 (h (calendar-holiday-list))
195 (holiday-list))
196 (while h
197 (if (calendar-date-equal date (car (car h)))
198 (setq holiday-list (append holiday-list (cdr (car h)))))
199 (setq h (cdr h)))
200 holiday-list))
202 (defun calendar-cursor-holidays ()
203 "Find holidays for the date specified by the cursor in the calendar window."
204 (interactive)
205 (message "Checking holidays...")
206 (let* ((date (calendar-cursor-to-date t))
207 (date-string (calendar-date-string date))
208 (holiday-list (check-calendar-holidays date))
209 (holiday-string (mapconcat 'identity holiday-list "; "))
210 (msg (format "%s: %s" date-string holiday-string)))
211 (if (not holiday-list)
212 (message "No holidays known for %s" date-string)
213 (if (<= (length msg) (frame-width))
214 (message "%s" msg)
215 (set-buffer (get-buffer-create holiday-buffer))
216 (setq buffer-read-only nil)
217 (calendar-set-mode-line date-string)
218 (erase-buffer)
219 (insert (mapconcat 'identity holiday-list "\n"))
220 (goto-char (point-min))
221 (set-buffer-modified-p nil)
222 (setq buffer-read-only t)
223 (display-buffer holiday-buffer)
224 (message "Checking holidays...done")))))
226 (defun mark-calendar-holidays ()
227 "Mark notable days in the calendar window."
228 (interactive)
229 (setq mark-holidays-in-calendar t)
230 (message "Marking holidays...")
231 (let ((holiday-list (calendar-holiday-list)))
232 (while holiday-list
233 (mark-visible-calendar-date
234 (car (car holiday-list)) calendar-holiday-marker)
235 (setq holiday-list (cdr holiday-list))))
236 (message "Marking holidays...done"))
238 (defun list-calendar-holidays ()
239 "Create a buffer containing the holidays for the current calendar window.
240 The holidays are those in the list calendar-notable-days. Returns t if any
241 holidays are found, nil if not."
242 (interactive)
243 (message "Looking up holidays...")
244 (let ((holiday-list (calendar-holiday-list))
245 (m1 displayed-month)
246 (y1 displayed-year)
247 (m2 displayed-month)
248 (y2 displayed-year))
249 (if (not holiday-list)
250 (progn
251 (message "Looking up holidays...none found")
252 nil)
253 (set-buffer (get-buffer-create holiday-buffer))
254 (setq buffer-read-only nil)
255 (increment-calendar-month m1 y1 -1)
256 (increment-calendar-month m2 y2 1)
257 (calendar-set-mode-line
258 (if (= y1 y2)
259 (format "Notable Dates from %s to %s, %d%%-"
260 (calendar-month-name m1) (calendar-month-name m2) y2)
261 (format "Notable Dates from %s, %d to %s, %d%%-"
262 (calendar-month-name m1) y1 (calendar-month-name m2) y2)))
263 (erase-buffer)
264 (insert
265 (mapconcat
266 '(lambda (x) (concat (calendar-date-string (car x))
267 ": " (car (cdr x))))
268 holiday-list "\n"))
269 (goto-char (point-min))
270 (set-buffer-modified-p nil)
271 (setq buffer-read-only t)
272 (display-buffer holiday-buffer)
273 (message "Looking up holidays...done")
274 t)))
276 (defun calendar-holiday-list ()
277 "Form the list of holidays that occur on dates in the calendar window.
278 The holidays are those in the list calendar-holidays."
279 (let ((p calendar-holidays)
280 (holiday-list))
281 (while p
282 (let* ((holidays
283 (if calendar-debug-sexp
284 (let ((stack-trace-on-error t))
285 (eval (car p)))
286 (condition-case nil
287 (eval (car p))
288 (error (beep)
289 (message "Bad holiday list item: %s" (car p))
290 (sleep-for 2))))))
291 (if holidays
292 (setq holiday-list (append holidays holiday-list))))
293 (setq p (cdr p)))
294 (setq holiday-list (sort holiday-list 'calendar-date-compare))))
296 ;; Below are the functions that calculate the dates of holidays; these
297 ;; are eval'ed in the function calendar-holiday-list. If you
298 ;; write other such functions, be sure to imitate the style used below.
299 ;; Remember that each function must return a list of items of the form
300 ;; ((month day year) string) of VISIBLE dates in the calendar window.
302 (defun holiday-fixed (month day string)
303 "Holiday on MONTH, DAY (Gregorian) called STRING.
304 If MONTH, DAY is visible, the value returned is the list (((MONTH DAY year)
305 STRING)). Returns nil if it is not visible in the current calendar window."
306 (let ((m displayed-month)
307 (y displayed-year))
308 (increment-calendar-month m y (- 11 month))
309 (if (> m 9)
310 (list (list (list month day y) string)))))
312 (defun holiday-float (month dayname n string &optional day)
313 "Holiday on MONTH, DAYNAME (Nth occurrence, Gregorian) called STRING.
314 If the Nth DAYNAME in MONTH is visible, the value returned is the list
315 \(((MONTH DAY year) STRING)).
317 If N<0, count backward from the end of MONTH.
319 An optional parameter DAY means the Nth DAYNAME after/before MONTH DAY.
321 Returns nil if it is not visible in the current calendar window."
322 (let ((m displayed-month)
323 (y displayed-year))
324 (increment-calendar-month m y (- 11 month))
325 (if (> m 9)
326 (list (list (calendar-nth-named-day n dayname month y day) string)))))
328 (defun holiday-sexp (sexp string)
329 "Sexp holiday for dates in the calendar window.
330 SEXP is an expression in variable `year' evaluates to `date'.
332 STRING is an expression in `date' that evaluates to the holiday description
333 of `date'.
335 If `date' is visible in the calendar window, the holiday STRING is on that
336 date. If date is nil, or if the date is not visible, there is no holiday."
337 (let ((m displayed-month)
338 (y displayed-year))
339 (increment-calendar-month m y -1)
340 (filter-visible-calendar-holidays
341 (append
342 (let* ((year y)
343 (date (eval sexp))
344 (string (if date (eval string))))
345 (list (list date string)))
346 (let* ((year (1+ y))
347 (date (eval sexp))
348 (string (if date (eval string))))
349 (list (list date string)))))))
351 (defun holiday-advent ()
352 "Date of Advent, if visible in calendar window."
353 (let ((year displayed-year)
354 (month displayed-month))
355 (increment-calendar-month month year -1)
356 (let ((advent (calendar-gregorian-from-absolute
357 (calendar-dayname-on-or-before 0
358 (calendar-absolute-from-gregorian
359 (list 12 3 year))))))
360 (if (calendar-date-is-visible-p advent)
361 (list (list advent "Advent"))))))
363 (defun holiday-easter-etc ()
364 "List of dates related to Easter, as visible in calendar window."
365 (if (and (> displayed-month 5) (not all-christian-calendar-holidays))
366 nil;; Ash Wednesday, Good Friday, and Easter are not visible.
367 (let* ((century (1+ (/ displayed-year 100)))
368 (shifted-epact ;; Age of moon for April 5...
369 (% (+ 14 (* 11 (% displayed-year 19));; ...by Nicaean rule
370 (- ;; ...corrected for the Gregorian century rule
371 (/ (* 3 century) 4))
372 (/ ;; ...corrected for Metonic cycle inaccuracy.
373 (+ 5 (* 8 century)) 25)
374 (* 30 century));; Keeps value positive.
375 30))
376 (adjusted-epact ;; Adjust for 29.5 day month.
377 (if (or (= shifted-epact 0)
378 (and (= shifted-epact 1) (< 10 (% displayed-year 19))))
379 (1+ shifted-epact)
380 shifted-epact))
381 (paschal-moon ;; Day after the full moon on or after March 21.
382 (- (calendar-absolute-from-gregorian (list 4 19 displayed-year))
383 adjusted-epact))
384 (abs-easter (calendar-dayname-on-or-before 0 (+ paschal-moon 7)))
385 (mandatory
386 (list
387 (list (calendar-gregorian-from-absolute abs-easter)
388 "Easter Sunday")
389 (list (calendar-gregorian-from-absolute (- abs-easter 2))
390 "Good Friday")
391 (list (calendar-gregorian-from-absolute (- abs-easter 46))
392 "Ash Wednesday")))
393 (optional
394 (list
395 (list (calendar-gregorian-from-absolute (- abs-easter 63))
396 "Septuagesima Sunday")
397 (list (calendar-gregorian-from-absolute (- abs-easter 56))
398 "Sexagesima Sunday")
399 (list (calendar-gregorian-from-absolute (- abs-easter 49))
400 "Shrove Sunday")
401 (list (calendar-gregorian-from-absolute (- abs-easter 48))
402 "Shrove Monday")
403 (list (calendar-gregorian-from-absolute (- abs-easter 47))
404 "Shrove Tuesday")
405 (list (calendar-gregorian-from-absolute (- abs-easter 14))
406 "Passion Sunday")
407 (list (calendar-gregorian-from-absolute (- abs-easter 7))
408 "Palm Sunday")
409 (list (calendar-gregorian-from-absolute (- abs-easter 3))
410 "Maundy Thursday")
411 (list (calendar-gregorian-from-absolute (+ abs-easter 35))
412 "Rogation Sunday")
413 (list (calendar-gregorian-from-absolute (+ abs-easter 39))
414 "Ascension Day")
415 (list (calendar-gregorian-from-absolute (+ abs-easter 49))
416 "Pentecost (Whitsunday)")
417 (list (calendar-gregorian-from-absolute (+ abs-easter 50))
418 "Whitmonday")
419 (list (calendar-gregorian-from-absolute (+ abs-easter 56))
420 "Trinity Sunday")
421 (list (calendar-gregorian-from-absolute (+ abs-easter 60))
422 "Corpus Christi")))
423 (output-list
424 (filter-visible-calendar-holidays mandatory)))
425 (if all-christian-calendar-holidays
426 (setq output-list
427 (append
428 (filter-visible-calendar-holidays optional)
429 output-list)))
430 output-list)))
432 (defun holiday-greek-orthodox-easter ()
433 "Date of Easter according to the rule of the Council of Nicaea."
434 (let ((m displayed-month)
435 (y displayed-year))
436 (increment-calendar-month m y 1)
437 (let* ((julian-year
438 (extract-calendar-year
439 (calendar-julian-from-absolute
440 (calendar-absolute-from-gregorian
441 (list m (calendar-last-day-of-month m y) y)))))
442 (shifted-epact ;; Age of moon for April 5.
443 (% (+ 14
444 (* 11 (% julian-year 19)))
445 30))
446 (paschal-moon ;; Day after full moon on or after March 21.
447 (- (calendar-absolute-from-julian (list 4 19 julian-year))
448 shifted-epact))
449 (nicaean-easter;; Sunday following the Paschal moon
450 (calendar-gregorian-from-absolute
451 (calendar-dayname-on-or-before 0 (+ paschal-moon 7)))))
452 (if (calendar-date-is-visible-p nicaean-easter)
453 (list (list nicaean-easter "Pascha (Greek Orthodox Easter)"))))))
455 (defun filter-visible-calendar-holidays (l)
456 "Return a list of all visible holidays of those on L."
457 (let ((visible)
458 (p l))
459 (while p
460 (and (car (car p))
461 (calendar-date-is-visible-p (car (car p)))
462 (setq visible (append (list (car p)) visible)))
463 (setq p (cdr p)))
464 visible))
466 (provide 'holidays)
468 ;;; holidays.el ends here