Error if ps-lpr-switches is not a list.
[bpt/emacs.git] / lisp / calendar / holidays.el
1 ;;; holidays.el --- holiday functions for the calendar package
2
3 ;; Copyright (C) 1989, 1990, 1992, 1993, 1994, 1997, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
5
6 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
7 ;; Maintainer: Glenn Morris <rgm@gnu.org>
8 ;; Keywords: holidays, calendar
9
10 ;; This file is part of GNU Emacs.
11
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.
16
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.
21
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.
26
27 ;;; Commentary:
28
29 ;; This collection of functions implements the holiday features as described
30 ;; in calendar.el.
31
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).
35
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.
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).
47
48 ;;; Code:
49
50 (defvar displayed-month)
51 (defvar displayed-year)
52
53 (require 'calendar)
54
55 (autoload 'holiday-julian "cal-julian"
56 "Holiday on MONTH, DAY (Julian) called STRING."
57 t)
58
59 (autoload 'holiday-hebrew "cal-hebrew"
60 "Holiday on MONTH, DAY (Hebrew) called STRING."
61 t)
62
63 (autoload 'holiday-rosh-hashanah-etc "cal-hebrew"
64 "List of dates related to Rosh Hashanah, as visible in calendar window."
65 t)
66
67 (autoload 'holiday-hanukkah "cal-hebrew"
68 "List of dates related to Hanukkah, as visible in calendar window."
69 t)
70
71 (autoload 'holiday-passover-etc "cal-hebrew"
72 "List of dates related to Passover, as visible in calendar window."
73 t)
74
75 (autoload 'holiday-tisha-b-av-etc "cal-hebrew"
76 "List of dates around Tisha B'Av, as visible in calendar window."
77 t)
78
79 (autoload 'holiday-islamic "cal-islam"
80 "Holiday on MONTH, DAY (Islamic) called STRING."
81 t)
82
83 (autoload 'holiday-bahai "cal-bahai"
84 "Holiday on MONTH, DAY (Baha'i) called STRING."
85 t)
86
87 (autoload 'holiday-chinese-new-year "cal-china"
88 "Date of Chinese New Year."
89 t)
90
91 (autoload 'solar-equinoxes-solstices "solar"
92 "Date and time of equinoxes and solstices, if visible in the calendar window.
93 Requires floating point."
94 t)
95
96 ;;;###autoload
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.
100
101 This function is suitable for execution in a .emacs file."
102 (interactive "P")
103 (save-excursion
104 (let* ((completion-ignore-case t)
105 (date (if arg
106 (calendar-read-date t)
107 (calendar-current-date)))
108 (displayed-month (extract-calendar-month date))
109 (displayed-year (extract-calendar-year date)))
110 (calendar-list-holidays))))
111
112 ;; rms: "Emacs commands to display a list of something generally start
113 ;; with `list-'. Please make `list-holidays' the principal name."
114 ;;;###autoload
115 (defun list-holidays (y1 y2 &optional l label)
116 "Display holidays for years Y1 to Y2 (inclusive).
117
118 The optional list of holidays L defaults to `calendar-holidays'.
119 If you want to control what holidays are displayed, use a
120 different list. For example,
121
122 (list-holidays 2006 2006
123 (append general-holidays local-holidays other-holidays))
124
125 will display holidays for the year 2006 defined in the 3
126 mentioned lists, and nothing else.
127
128 When called interactively, this command offers a choice of
129 holidays, based on the variables `solar-holidays' etc. See the
130 documentation of `calendar-holidays' for a list of the variables
131 that control the choices, as well as a description of the format
132 of a holiday list.
133
134 The optional LABEL is used to label the buffer created."
135 (interactive
136 (let* ((start-year (calendar-read
137 "Starting year of holidays (>0): "
138 '(lambda (x) (> x 0))
139 (int-to-string (extract-calendar-year
140 (calendar-current-date)))))
141 (end-year (calendar-read
142 (format "Ending year (inclusive) of holidays (>=%s): "
143 start-year)
144 '(lambda (x) (>= x start-year))
145 (int-to-string start-year)))
146 (completion-ignore-case t)
147 (lists
148 (list
149 (cons "All" calendar-holidays)
150 (if (fboundp 'atan)
151 (cons "Equinoxes/Solstices"
152 (list (list 'solar-equinoxes-solstices))))
153 (if general-holidays (cons "General" general-holidays))
154 (if local-holidays (cons "Local" local-holidays))
155 (if other-holidays (cons "Other" other-holidays))
156 (if christian-holidays (cons "Christian" christian-holidays))
157 (if hebrew-holidays (cons "Hebrew" hebrew-holidays))
158 (if islamic-holidays (cons "Islamic" islamic-holidays))
159 (if bahai-holidays (cons "Baha'i" bahai-holidays))
160 (if oriental-holidays (cons "Oriental" oriental-holidays))
161 (if solar-holidays (cons "Solar" 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 (message "Computing holidays...")
175 (let* ((holiday-buffer "*Holidays*")
176 (calendar-holidays (if l l calendar-holidays))
177 (title (or label "Holidays"))
178 (holiday-list nil)
179 (s (calendar-absolute-from-gregorian (list 2 1 y1)))
180 (e (calendar-absolute-from-gregorian (list 11 1 y2)))
181 (d s)
182 (never t)
183 (displayed-month 2)
184 (displayed-year y1))
185 (while (or never (<= d e))
186 (setq holiday-list (append holiday-list (calendar-holiday-list)))
187 (setq never nil)
188 (increment-calendar-month displayed-month displayed-year 3)
189 (setq d (calendar-absolute-from-gregorian
190 (list displayed-month 1 displayed-year))))
191 (save-excursion
192 (set-buffer (get-buffer-create holiday-buffer))
193 (setq buffer-read-only nil)
194 (calendar-set-mode-line
195 (if (= y1 y2)
196 (format "%s for %s" title y1)
197 (format "%s for %s-%s" title y1 y2)))
198 (erase-buffer)
199 (goto-char (point-min))
200 (insert
201 (mapconcat
202 '(lambda (x) (concat (calendar-date-string (car x))
203 ": " (car (cdr x))))
204 holiday-list "\n"))
205 (goto-char (point-min))
206 (set-buffer-modified-p nil)
207 (setq buffer-read-only t)
208 (display-buffer holiday-buffer)
209 (message "Computing holidays...done"))))
210
211 (defalias 'holiday-list 'list-holidays)
212
213 (defun calendar-check-holidays (date)
214 "Check the list of holidays for any that occur on DATE.
215 The value returned is a list of strings of relevant holiday descriptions.
216 The holidays are those in the list `calendar-holidays'."
217 (let ((displayed-month (extract-calendar-month date))
218 (displayed-year (extract-calendar-year date))
219 (holiday-list))
220 (dolist (h (calendar-holiday-list))
221 (if (calendar-date-equal date (car h))
222 (setq holiday-list (append holiday-list (cdr h)))))
223 holiday-list))
224
225 (defun calendar-cursor-holidays ()
226 "Find holidays for the date specified by the cursor in the calendar window."
227 (interactive)
228 (message "Checking holidays...")
229 (let* ((date (calendar-cursor-to-date t))
230 (date-string (calendar-date-string date))
231 (holiday-list (calendar-check-holidays date))
232 (holiday-string (mapconcat 'identity holiday-list "; "))
233 (msg (format "%s: %s" date-string holiday-string)))
234 (if (not holiday-list)
235 (message "No holidays known for %s" date-string)
236 (if (<= (length msg) (frame-width))
237 (message "%s" msg)
238 (set-buffer (get-buffer-create holiday-buffer))
239 (setq buffer-read-only nil)
240 (calendar-set-mode-line date-string)
241 (erase-buffer)
242 (insert (mapconcat 'identity holiday-list "\n"))
243 (goto-char (point-min))
244 (set-buffer-modified-p nil)
245 (setq buffer-read-only t)
246 (display-buffer holiday-buffer)
247 (message "Checking holidays...done")))))
248
249 (defun calendar-mark-holidays ()
250 "Mark notable days in the calendar window."
251 (interactive)
252 (setq mark-holidays-in-calendar t)
253 (message "Marking holidays...")
254 (dolist (holiday (calendar-holiday-list))
255 (mark-visible-calendar-date
256 (car holiday) calendar-holiday-marker))
257 (message "Marking holidays...done"))
258
259 (defun calendar-list-holidays ()
260 "Create a buffer containing the holidays for the current calendar window.
261 The holidays are those in the list `calendar-notable-days'. Returns t if any
262 holidays are found, nil if not."
263 (interactive)
264 (message "Looking up holidays...")
265 (let ((holiday-list (calendar-holiday-list))
266 (m1 displayed-month)
267 (y1 displayed-year)
268 (m2 displayed-month)
269 (y2 displayed-year))
270 (if (not holiday-list)
271 (progn
272 (message "Looking up holidays...none found")
273 nil)
274 (set-buffer (get-buffer-create holiday-buffer))
275 (setq buffer-read-only nil)
276 (increment-calendar-month m1 y1 -1)
277 (increment-calendar-month m2 y2 1)
278 (calendar-set-mode-line
279 (if (= y1 y2)
280 (format "Notable Dates from %s to %s, %d%%-"
281 (calendar-month-name m1) (calendar-month-name m2) y2)
282 (format "Notable Dates from %s, %d to %s, %d%%-"
283 (calendar-month-name m1) y1 (calendar-month-name m2) y2)))
284 (erase-buffer)
285 (insert
286 (mapconcat
287 '(lambda (x) (concat (calendar-date-string (car x))
288 ": " (car (cdr x))))
289 holiday-list "\n"))
290 (goto-char (point-min))
291 (set-buffer-modified-p nil)
292 (setq buffer-read-only t)
293 (display-buffer holiday-buffer)
294 (message "Looking up holidays...done")
295 t)))
296
297 (defun calendar-holiday-list ()
298 "Form the list of holidays that occur on dates in the calendar window.
299 The holidays are those in the list `calendar-holidays'."
300 (let ((holiday-list ()))
301 (dolist (p calendar-holidays)
302 (let* ((holidays
303 (if calendar-debug-sexp
304 (let ((stack-trace-on-error t))
305 (eval p))
306 (condition-case nil
307 (eval p)
308 (error (beep)
309 (message "Bad holiday list item: %s" p)
310 (sleep-for 2))))))
311 (if holidays
312 (setq holiday-list (append holidays holiday-list)))))
313 (setq holiday-list (sort holiday-list 'calendar-date-compare))))
314
315 ;; Below are the functions that calculate the dates of holidays; these
316 ;; are eval'ed in the function calendar-holiday-list. If you
317 ;; write other such functions, be sure to imitate the style used below.
318 ;; Remember that each function must return a list of items of the form
319 ;; ((month day year) string) of VISIBLE dates in the calendar window.
320
321 (defun holiday-fixed (month day string)
322 "Holiday on MONTH, DAY (Gregorian) called STRING.
323 If MONTH, DAY is visible, the value returned is the list (((MONTH DAY year)
324 STRING)). Returns nil if it is not visible in the current calendar window."
325 (let ((m displayed-month)
326 (y displayed-year))
327 (increment-calendar-month m y (- 11 month))
328 (if (> m 9)
329 (list (list (list month day y) string)))))
330
331 (defun holiday-float (month dayname n string &optional day)
332 "Holiday on MONTH, DAYNAME (Nth occurrence) called STRING.
333 If the Nth DAYNAME in MONTH is visible, the value returned is the list
334 \(((MONTH DAY year) STRING)).
335
336 If N<0, count backward from the end of MONTH.
337
338 An optional parameter DAY means the Nth DAYNAME on or after/before MONTH DAY.
339
340 Returns nil if it is not visible in the current calendar window."
341 ;; This is messy because the holiday may be visible, while the date on which
342 ;; it is based is not. For example, the first Monday after December 30 may be
343 ;; visible when January is not. For large values of |n| the problem is more
344 ;; grotesque. If we didn't have to worry about such cases, we could just use
345
346 ;; (let ((m displayed-month)
347 ;; (y displayed-year))
348 ;; (increment-calendar-month m y (- 11 month))
349 ;; (if (> m 9); month in year y is visible
350 ;; (list (list (calendar-nth-named-day n dayname month y day) string)))))
351
352 ;; which is the way the function was originally written.
353
354 (let* ((m1 displayed-month)
355 (y1 displayed-year)
356 (m2 m1)
357 (y2 y1))
358 (increment-calendar-month m1 y1 -1)
359 (increment-calendar-month m2 y2 1)
360 (let* ((d1; first possible base date for holiday
361 (+ (calendar-nth-named-absday 1 dayname m1 y1)
362 (* -7 n)
363 (if (> n 0) 1 -7)))
364 (d2; last possible base date for holiday
365 (+ (calendar-nth-named-absday -1 dayname m2 y2)
366 (* -7 n)
367 (if (> n 0) 7 -1)))
368 (y1 (extract-calendar-year (calendar-gregorian-from-absolute d1)))
369 (y2 (extract-calendar-year (calendar-gregorian-from-absolute d2)))
370 (y; year of base date
371 (if (or (= y1 y2) (> month 9))
372 y1
373 y2))
374 (d; day of base date
375 (or day (if (> n 0)
376 1
377 (calendar-last-day-of-month month y))))
378 (date; base date for holiday
379 (calendar-absolute-from-gregorian (list month d y))))
380 (if (and (<= d1 date) (<= date d2))
381 (list (list (calendar-nth-named-day n dayname month y d)
382 string))))))
383
384 (defun holiday-sexp (sexp string)
385 "Sexp holiday for dates in the calendar window.
386 SEXP is an expression in variable `year' evaluates to `date'.
387
388 STRING is an expression in `date' that evaluates to the holiday description
389 of `date'.
390
391 If `date' is visible in the calendar window, the holiday STRING is on that
392 date. If date is nil, or if the date is not visible, there is no holiday."
393 (let ((m displayed-month)
394 (y displayed-year))
395 (increment-calendar-month m y -1)
396 (holiday-filter-visible-calendar
397 (list
398 (let* ((year y)
399 (date (eval sexp))
400 (string (if date (eval string))))
401 (list date string))
402 (let* ((year (1+ y))
403 (date (eval sexp))
404 (string (if date (eval string))))
405 (list date string))))))
406
407 (defun holiday-advent (&optional n string)
408 "Date of Nth day after advent (named STRING), if visible in calendar window.
409 Negative values of N are interpreted as days before advent.
410 STRING is used purely for display purposes. The return value has
411 the form ((MONTH DAY YEAR) STRING), where the date is that of the
412 Nth day before or after advent.
413
414 For backwards compatibility, if this function is called with no
415 arguments, then it returns the value appropriate for advent itself."
416 ;; Backwards compatibility layer.
417 (if (not n)
418 (holiday-advent 0 "Advent")
419 (let ((year displayed-year)
420 (month displayed-month))
421 (increment-calendar-month month year -1)
422 (let ((advent (calendar-gregorian-from-absolute
423 (+ n
424 (calendar-dayname-on-or-before
425 0
426 (calendar-absolute-from-gregorian
427 (list 12 3 year)))))))
428 (if (calendar-date-is-visible-p advent)
429 (list (list advent string)))))))
430
431 (defun holiday-easter-etc (&optional n string)
432 "Date of Nth day after Easter (named STRING), if visible in calendar window.
433 Negative values of N are interpreted as days before Easter.
434 STRING is used purely for display purposes. The return value has
435 the form ((MONTH DAY YEAR) STRING), where the date is that of the
436 Nth day before or after Easter.
437
438 For backwards compatibility, if this function is called with no
439 arguments, then it returns a list of \"standard\" Easter-related
440 holidays (with more entries if `all-christian-calendar-holidays'
441 is non-nil)."
442 ;; Backwards compatibility layer.
443 (if (not n)
444 (let (res-list res)
445 (dolist (elem (append
446 (if all-christian-calendar-holidays
447 '((-63 . "Septuagesima Sunday")
448 (-56 . "Sexagesima Sunday")
449 (-49 . "Shrove Sunday")
450 (-48 . "Shrove Monday")
451 (-47 . "Shrove Tuesday")
452 (-14 . "Passion Sunday")
453 (-7 . "Palm Sunday")
454 (-3 . "Maundy Thursday")
455 (35 . "Rogation Sunday")
456 (39 . "Ascension Day")
457 (49 . "Pentecost (Whitsunday)")
458 (50 . "Whitmonday")
459 (56 . "Trinity Sunday")
460 (60 . "Corpus Christi")))
461 '((0 . "Easter Sunday")
462 (-2 . "Good Friday")
463 (-46 . "Ash Wednesday")))
464 res-list)
465 ;; Filter out nil (not visible) values.
466 (if (setq res (holiday-easter-etc (car elem) (cdr elem)))
467 (setq res-list (append res res-list)))))
468 (let* ((century (1+ (/ displayed-year 100)))
469 (shifted-epact ;; Age of moon for April 5...
470 (% (+ 14 (* 11 (% displayed-year 19)) ;; ...by Nicaean rule
471 (- ;; ...corrected for the Gregorian century rule
472 (/ (* 3 century) 4))
473 (/ ;; ...corrected for Metonic cycle inaccuracy.
474 (+ 5 (* 8 century)) 25)
475 (* 30 century)) ;; Keeps value positive.
476 30))
477 (adjusted-epact ;; Adjust for 29.5 day month.
478 (if (or (zerop shifted-epact)
479 (and (= shifted-epact 1) (< 10 (% displayed-year 19))))
480 (1+ shifted-epact)
481 shifted-epact))
482 (paschal-moon ;; Day after the full moon on or after March 21.
483 (- (calendar-absolute-from-gregorian (list 4 19 displayed-year))
484 adjusted-epact))
485 (abs-easter (calendar-dayname-on-or-before 0 (+ paschal-moon 7))))
486 (holiday-filter-visible-calendar
487 (list (list (calendar-gregorian-from-absolute (+ abs-easter n))
488 string))))))
489
490 (defun holiday-greek-orthodox-easter ()
491 "Date of Easter according to the rule of the Council of Nicaea."
492 (let ((m displayed-month)
493 (y displayed-year))
494 (increment-calendar-month m y 1)
495 (let* ((julian-year
496 (extract-calendar-year
497 (calendar-julian-from-absolute
498 (calendar-absolute-from-gregorian
499 (list m (calendar-last-day-of-month m y) y)))))
500 (shifted-epact ;; Age of moon for April 5.
501 (% (+ 14
502 (* 11 (% julian-year 19)))
503 30))
504 (paschal-moon ;; Day after full moon on or after March 21.
505 (- (calendar-absolute-from-julian (list 4 19 julian-year))
506 shifted-epact))
507 (nicaean-easter;; Sunday following the Paschal moon
508 (calendar-gregorian-from-absolute
509 (calendar-dayname-on-or-before 0 (+ paschal-moon 7)))))
510 (if (calendar-date-is-visible-p nicaean-easter)
511 (list (list nicaean-easter "Pascha (Greek Orthodox Easter)"))))))
512
513 (defun holiday-filter-visible-calendar (l)
514 "Return a list of all visible holidays of those on L."
515 (let ((visible ()))
516 (dolist (p l)
517 (and (car p)
518 (calendar-date-is-visible-p (car p))
519 (push p visible)))
520 visible))
521
522 ;; Backward compatibility.
523 (define-obsolete-function-alias
524 'filter-visible-calendar-holidays 'holiday-filter-visible-calendar "23.1")
525 (define-obsolete-function-alias
526 'list-calendar-holidays 'calendar-list-holidays "23.1")
527 (define-obsolete-function-alias
528 'mark-calendar-holidays 'calendar-mark-holidays "23.1")
529 (define-obsolete-function-alias
530 'check-calendar-holidays 'calendar-check-holidays "23.1")
531
532 (provide 'holidays)
533
534 ;; arch-tag: 48eb3117-75a7-4dbe-8fd9-873c3cbb0d37
535 ;;; holidays.el ends here