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