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