]> code.delx.au - gnu-emacs/blob - lisp/calendar/holidays.el
Update copyright year to 2016
[gnu-emacs] / lisp / calendar / holidays.el
1 ;;; holidays.el --- holiday functions for the calendar package
2
3 ;; Copyright (C) 1989-1990, 1992-1994, 1997, 2001-2016 Free Software
4 ;; Foundation, Inc.
5
6 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
7 ;; Maintainer: Glenn Morris <rgm@gnu.org>
8 ;; Keywords: holidays, calendar
9 ;; Package: calendar
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25
26 ;;; Commentary:
27
28 ;; See calendar.el.
29
30 ;;; Code:
31
32 (require 'calendar)
33 (load "hol-loaddefs" nil t)
34
35 (defgroup holidays nil
36 "Holidays support in calendar."
37 :group 'calendar
38 :prefix "holidays-"
39 :group 'local)
40
41 ;; The various holiday variables are autoloaded because people
42 ;; are used to using them to set calendar-holidays without having to
43 ;; explicitly load this file.
44
45 ;;;###autoload
46 (defcustom holiday-general-holidays
47 (mapcar 'purecopy
48 '((holiday-fixed 1 1 "New Year's Day")
49 (holiday-float 1 1 3 "Martin Luther King Day")
50 (holiday-fixed 2 2 "Groundhog Day")
51 (holiday-fixed 2 14 "Valentine's Day")
52 (holiday-float 2 1 3 "President's Day")
53 (holiday-fixed 3 17 "St. Patrick's Day")
54 (holiday-fixed 4 1 "April Fools' Day")
55 (holiday-float 5 0 2 "Mother's Day")
56 (holiday-float 5 1 -1 "Memorial Day")
57 (holiday-fixed 6 14 "Flag Day")
58 (holiday-float 6 0 3 "Father's Day")
59 (holiday-fixed 7 4 "Independence Day")
60 (holiday-float 9 1 1 "Labor Day")
61 (holiday-float 10 1 2 "Columbus Day")
62 (holiday-fixed 10 31 "Halloween")
63 (holiday-fixed 11 11 "Veteran's Day")
64 (holiday-float 11 4 4 "Thanksgiving")))
65 "General holidays. Default value is for the United States.
66 See the documentation for `calendar-holidays' for details."
67 :type 'sexp
68 :group 'holidays)
69 ;;;###autoload
70 (put 'holiday-general-holidays 'risky-local-variable t)
71
72 ;;;###autoload
73 (defcustom holiday-oriental-holidays
74 (mapcar 'purecopy
75 '((holiday-chinese-new-year)
76 (if calendar-chinese-all-holidays-flag
77 (append
78 (holiday-chinese 1 15 "Lantern Festival")
79 (holiday-chinese-qingming)
80 (holiday-chinese 5 5 "Dragon Boat Festival")
81 (holiday-chinese 7 7 "Double Seventh Festival")
82 (holiday-chinese 8 15 "Mid-Autumn Festival")
83 (holiday-chinese 9 9 "Double Ninth Festival")
84 (holiday-chinese-winter-solstice)
85 ))))
86 "Oriental holidays.
87 See the documentation for `calendar-holidays' for details."
88 :version "23.1" ; added more holidays
89 :type 'sexp
90 :group 'holidays)
91 ;;;###autoload
92 (put 'holiday-oriental-holidays 'risky-local-variable t)
93
94 ;;;###autoload
95 (defcustom holiday-local-holidays nil
96 "Local holidays.
97 See the documentation for `calendar-holidays' for details."
98 :type 'sexp
99 :group 'holidays)
100 ;;;###autoload
101 (put 'holiday-local-holidays 'risky-local-variable t)
102
103 ;;;###autoload
104 (defcustom holiday-other-holidays nil
105 "User defined holidays.
106 See the documentation for `calendar-holidays' for details."
107 :type 'sexp
108 :group 'holidays)
109 ;;;###autoload
110 (put 'holiday-other-holidays 'risky-local-variable t)
111
112 ;;;###autoload
113 (defcustom holiday-hebrew-holidays
114 (mapcar 'purecopy
115 '((holiday-hebrew-passover)
116 (holiday-hebrew-rosh-hashanah)
117 (holiday-hebrew-hanukkah)
118 (if calendar-hebrew-all-holidays-flag
119 (append
120 (holiday-hebrew-tisha-b-av)
121 (holiday-hebrew-misc)))))
122 "Jewish holidays.
123 See the documentation for `calendar-holidays' for details."
124 :type 'sexp
125 :version "23.1" ; removed dependency on hebrew-holidays-N
126 :group 'holidays)
127 ;;;###autoload
128 (put 'holiday-hebrew-holidays 'risky-local-variable t)
129
130 ;;;###autoload
131 (defcustom holiday-christian-holidays
132 (mapcar 'purecopy
133 '((holiday-easter-etc) ; respects calendar-christian-all-holidays-flag
134 (holiday-fixed 12 25 "Christmas")
135 (if calendar-christian-all-holidays-flag
136 (append
137 (holiday-fixed 1 6 "Epiphany")
138 (holiday-julian 12 25 "Christmas (Julian calendar)")
139 (holiday-greek-orthodox-easter)
140 (holiday-fixed 8 15 "Assumption")
141 (holiday-advent 0 "Advent")))))
142 "Christian holidays.
143 See the documentation for `calendar-holidays' for details."
144 :type 'sexp
145 :group 'holidays)
146 ;;;###autoload
147 (put 'holiday-christian-holidays 'risky-local-variable t)
148
149 ;;;###autoload
150 (defcustom holiday-islamic-holidays
151 (mapcar 'purecopy
152 '((holiday-islamic-new-year)
153 (holiday-islamic 9 1 "Ramadan Begins")
154 (if calendar-islamic-all-holidays-flag
155 (append
156 (holiday-islamic 1 10 "Ashura")
157 (holiday-islamic 3 12 "Mulad-al-Nabi")
158 (holiday-islamic 7 26 "Shab-e-Mi'raj")
159 (holiday-islamic 8 15 "Shab-e-Bara't")
160 (holiday-islamic 9 27 "Shab-e Qadr")
161 (holiday-islamic 10 1 "Id-al-Fitr")
162 (holiday-islamic 12 10 "Id-al-Adha")))))
163 "Islamic holidays.
164 See the documentation for `calendar-holidays' for details."
165 :type 'sexp
166 :group 'holidays)
167 ;;;###autoload
168 (put 'holiday-islamic-holidays 'risky-local-variable t)
169
170 ;;;###autoload
171 (defcustom holiday-bahai-holidays
172 (mapcar 'purecopy
173 '((holiday-bahai-new-year)
174 (holiday-bahai-ridvan) ; respects calendar-bahai-all-holidays-flag
175 (holiday-fixed 5 23 "Declaration of the Báb")
176 (holiday-fixed 5 29 "Ascension of Bahá’u’lláh")
177 (holiday-fixed 7 9 "Martyrdom of the Báb")
178 (holiday-fixed 10 20 "Birth of the Báb")
179 (holiday-fixed 11 12 "Birth of Bahá’u’lláh")
180 (if calendar-bahai-all-holidays-flag
181 (append
182 (holiday-fixed 11 26 "Day of the Covenant")
183 (holiday-fixed 11 28 "Ascension of `Abdu’l-Bahá")))))
184 "Bahá’í holidays.
185 See the documentation for `calendar-holidays' for details."
186 :type 'sexp
187 :group 'holidays)
188 ;;;###autoload
189 (put 'holiday-bahai-holidays 'risky-local-variable t)
190
191 ;;;###autoload
192 (defcustom holiday-solar-holidays
193 (mapcar 'purecopy
194 '((solar-equinoxes-solstices)
195 (holiday-sexp calendar-daylight-savings-starts
196 (format "Daylight Saving Time Begins %s"
197 (solar-time-string
198 (/ calendar-daylight-savings-starts-time (float 60))
199 calendar-standard-time-zone-name)))
200 (holiday-sexp calendar-daylight-savings-ends
201 (format "Daylight Saving Time Ends %s"
202 (solar-time-string
203 (/ calendar-daylight-savings-ends-time (float 60))
204 calendar-daylight-time-zone-name)))))
205 "Sun-related holidays.
206 See the documentation for `calendar-holidays' for details."
207 :type 'sexp
208 :group 'holidays)
209 ;;;###autoload
210 (put 'holiday-solar-holidays 'risky-local-variable t)
211
212 ;; This one should not be autoloaded, else .emacs changes of
213 ;; holiday-general-holidays etc have no effect.
214 ;; FIXME should have some :set-after.
215 (defcustom calendar-holidays
216 (append holiday-general-holidays holiday-local-holidays
217 holiday-other-holidays holiday-christian-holidays
218 holiday-hebrew-holidays holiday-islamic-holidays
219 holiday-bahai-holidays holiday-oriental-holidays
220 holiday-solar-holidays)
221 "List of notable days for the command \\[holidays].
222
223 Additional holidays are easy to add to the list, just put them in the
224 list `holiday-other-holidays' in your init file. Similarly, by setting
225 any of `holiday-general-holidays', `holiday-local-holidays',
226 `holiday-christian-holidays', `holiday-hebrew-holidays',
227 `holiday-islamic-holidays', `holiday-bahai-holidays',
228 `holiday-oriental-holidays', or `holiday-solar-holidays' to nil in your
229 init file, you can eliminate unwanted categories of holidays.
230
231 The aforementioned variables control the holiday choices offered
232 by the function `holiday-list' when it is called interactively.
233
234 They also initialize the default value of `calendar-holidays',
235 which is the default list of holidays used by the function
236 `holiday-list' in the non-interactive case. Note that these
237 variables have no effect on `calendar-holidays' after it has been
238 set (e.g. after the calendar is loaded). In that case, customize
239 `calendar-holidays' directly.
240
241 The intention is that (in the US) `holiday-local-holidays' be set in
242 site-init.el and `holiday-other-holidays' be set by the user.
243
244 Entries on the list are expressions that return (possibly empty) lists of
245 items of the form ((month day year) string) of a holiday in the
246 three-month period centered around `displayed-month' of `displayed-year'.
247 Several basic functions are provided for this purpose:
248
249 (holiday-fixed MONTH DAY STRING) is a fixed date on the Gregorian calendar
250 (holiday-float MONTH DAYNAME K STRING &optional DAY) is the Kth DAYNAME
251 (0 for Sunday, etc.) after/before Gregorian
252 MONTH DAY. K<0 means count back from the end
253 of the month. Optional DAY defaults to 1 if
254 K>0, and MONTH's last day otherwise.
255 (holiday-hebrew MONTH DAY STRING) a fixed date on the Hebrew calendar
256 (holiday-islamic MONTH DAY STRING) a fixed date on the Islamic calendar
257 (holiday-bahai MONTH DAY STRING) a fixed date on the Bahá’í calendar
258 (holiday-julian MONTH DAY STRING) a fixed date on the Julian calendar
259 (holiday-sexp SEXP STRING) SEXP is a Gregorian-date-valued expression
260 in the variable `year'; if it evaluates to
261 a visible date, that's the holiday; if it
262 evaluates to nil, there's no holiday. STRING
263 is an expression in the variable `date'.
264
265 For example, to add Bastille Day, celebrated in France on July 14, add
266
267 (holiday-fixed 7 14 \"Bastille Day\")
268
269 to the list. To add Hurricane Supplication Day, celebrated in the Virgin
270 Islands on the fourth Monday in July, add
271
272 (holiday-float 7 1 4 \"Hurricane Supplication Day\")
273
274 to the list (the last Monday would be specified with `-1' instead of `4').
275 To add the last day of Hanukkah to the list, use
276
277 (holiday-hebrew 10 2 \"Last day of Hanukkah\")
278
279 since the Hebrew months are numbered with 1 starting from Nisan.
280 To add the Islamic feast celebrating Mohammed's birthday, use
281
282 (holiday-islamic 3 12 \"Mohammed's Birthday\")
283
284 since the Islamic months are numbered from 1 starting with Muharram.
285 To add an entry for the Bahá’í festival of Ridvan, use
286
287 (holiday-bahai 2 13 \"Festival of Ridvan\")
288
289 since the Bahá’í months are numbered from 1 starting with Bahá.
290 To add Thomas Jefferson's birthday, April 2, 1743 (Julian), use
291
292 (holiday-julian 4 2 \"Jefferson's Birthday\")
293
294 To include a holiday conditionally, use the sexp form or a conditional. For
295 example, to include American presidential elections, which occur on the first
296 Tuesday after the first Monday in November of years divisible by 4, add
297
298 (holiday-sexp
299 \\='(if (zerop (% year 4))
300 (calendar-gregorian-from-absolute
301 (1+ (calendar-dayname-on-or-before
302 1 (+ 6 (calendar-absolute-from-gregorian
303 (list 11 1 year)))))))
304 \"US Presidential Election\")
305
306 or
307
308 (if (zerop (% displayed-year 4))
309 (holiday-fixed 11
310 (calendar-extract-day
311 (calendar-gregorian-from-absolute
312 (1+ (calendar-dayname-on-or-before
313 1 (+ 6 (calendar-absolute-from-gregorian
314 (list 11 1 displayed-year)))))))
315 \"US Presidential Election\"))
316
317 to the list. To include the phases of the moon, add
318
319 (lunar-phases)
320
321 to the holiday list, where `lunar-phases' is an Emacs-Lisp function that
322 you've written to return a (possibly empty) list of the relevant VISIBLE dates
323 with descriptive strings such as
324
325 (((2 6 1989) \"New Moon\") ((2 12 1989) \"First Quarter Moon\") ... )."
326 :type 'sexp
327 :group 'holidays)
328 ;;;###autoload
329 (put 'calendar-holidays 'risky-local-variable t)
330
331 ;;; End of user options.
332
333
334 ;; FIXME name that makes sense
335 ;;;###diary-autoload
336 (defun calendar-holiday-list ()
337 "Form the list of holidays that occur on dates in the calendar window.
338 The holidays are those in the list `calendar-holidays'."
339 (let (res h err)
340 (sort
341 (dolist (p calendar-holidays res)
342 (if (setq h (if calendar-debug-sexp
343 (let ((debug-on-error t))
344 (eval p))
345 (condition-case err
346 (eval p)
347 (error
348 (display-warning
349 'holidays
350 (format "Bad holiday list item: %s\nError: %s\n"
351 p err)
352 :error)
353 nil))))
354 (setq res (append h res))))
355 'calendar-date-compare)))
356
357 (defvar displayed-month) ; from calendar-generate
358 (defvar displayed-year)
359
360 ;; FIXME name that makes sense
361 ;;;###cal-autoload
362 (defun calendar-list-holidays (&optional event)
363 "Create a buffer containing the holidays for the current calendar window.
364 The holidays are those in the list `calendar-notable-days'.
365 Returns non-nil if any holidays are found.
366 If EVENT is non-nil, it's an event indicating the buffer position to
367 use instead of point."
368 (interactive (list last-nonmenu-event))
369 ;; If called from a menu, with the calendar window not selected.
370 (with-current-buffer
371 (if event (window-buffer (posn-window (event-start event)))
372 (current-buffer))
373 (message "Looking up holidays...")
374 (let ((holiday-list (calendar-holiday-list))
375 (m1 displayed-month)
376 (y1 displayed-year)
377 (m2 displayed-month)
378 (y2 displayed-year))
379 (if (not holiday-list)
380 (message "Looking up holidays...none found")
381 (calendar-in-read-only-buffer holiday-buffer
382 (calendar-increment-month m1 y1 -1)
383 (calendar-increment-month m2 y2 1)
384 (calendar-set-mode-line
385 (if (= y1 y2)
386 (format "Notable Dates from %s to %s, %d%%-"
387 (calendar-month-name m1) (calendar-month-name m2) y2)
388 (format "Notable Dates from %s, %d to %s, %d%%-"
389 (calendar-month-name m1) y1 (calendar-month-name m2) y2)))
390 (insert
391 (mapconcat
392 (lambda (x) (concat (calendar-date-string (car x))
393 ": " (cadr x)))
394 holiday-list "\n")))
395 (message "Looking up holidays...done"))
396 holiday-list)))
397
398 ;;;###autoload
399 (defun holidays (&optional arg)
400 "Display the holidays for last month, this month, and next month.
401 If called with an optional prefix argument ARG, prompts for month and year.
402 This function is suitable for execution in a init file."
403 (interactive "P")
404 (save-excursion
405 (let* ((completion-ignore-case t)
406 (date (if arg (calendar-read-date t)
407 (calendar-current-date)))
408 (displayed-month (calendar-extract-month date))
409 (displayed-year (calendar-extract-year date)))
410 (calendar-list-holidays))))
411
412 ;; rms: "Emacs commands to display a list of something generally start
413 ;; with `list-'. Please make `list-holidays' the principal name."
414 ;;;###autoload
415 (defun list-holidays (y1 &optional y2 l label)
416 "Display holidays for years Y1 to Y2 (inclusive).
417 Y2 defaults to Y1. The optional list of holidays L defaults to
418 `calendar-holidays'. If you want to control what holidays are
419 displayed, use a different list. For example,
420
421 (list-holidays 2006 2006
422 (append holiday-general-holidays holiday-local-holidays))
423
424 will display holidays for the year 2006 defined in the two
425 mentioned lists, and nothing else.
426
427 When called interactively, this command offers a choice of
428 holidays, based on the variables `holiday-solar-holidays' etc. See the
429 documentation of `calendar-holidays' for a list of the variables
430 that control the choices, as well as a description of the format
431 of a holiday list.
432
433 The optional LABEL is used to label the buffer created."
434 (interactive
435 (let* ((start-year (calendar-read
436 "Starting year of holidays (>0): "
437 (lambda (x) (> x 0))
438 (number-to-string (calendar-extract-year
439 (calendar-current-date)))))
440 (end-year (calendar-read
441 (format "Ending year (inclusive) of holidays (>=%s): "
442 start-year)
443 (lambda (x) (>= x start-year))
444 (number-to-string start-year)))
445 (completion-ignore-case t)
446 (lists
447 (list
448 (cons "All" calendar-holidays)
449 (cons "Equinoxes/Solstices"
450 (list (list 'solar-equinoxes-solstices)))
451 (if holiday-general-holidays
452 (cons "General" holiday-general-holidays))
453 (if holiday-local-holidays
454 (cons "Local" holiday-local-holidays))
455 (if holiday-other-holidays
456 (cons "Other" holiday-other-holidays))
457 (if holiday-christian-holidays
458 (cons "Christian" holiday-christian-holidays))
459 (if holiday-hebrew-holidays
460 (cons "Hebrew" holiday-hebrew-holidays))
461 (if holiday-islamic-holidays
462 (cons "Islamic" holiday-islamic-holidays))
463 (if holiday-bahai-holidays
464 (cons "Bahá’í" holiday-bahai-holidays))
465 (if holiday-oriental-holidays
466 (cons "Oriental" holiday-oriental-holidays))
467 (if holiday-solar-holidays
468 (cons "Solar" holiday-solar-holidays))
469 (cons "Ask" nil)))
470 (choice (capitalize
471 (completing-read "List (TAB for choices): " lists nil t)))
472 (which (if (string-equal choice "Ask")
473 (eval (read-variable "Enter list name: "))
474 (cdr (assoc choice lists))))
475 (name (if (string-equal choice "Equinoxes/Solstices")
476 choice
477 (if (member choice '("Ask" ""))
478 "Holidays"
479 (format "%s Holidays" choice)))))
480 (list start-year end-year which name)))
481 (unless y2 (setq y2 y1))
482 (message "Computing holidays...")
483 (let ((calendar-holidays (or l calendar-holidays))
484 (title (or label "Holidays"))
485 (s (calendar-absolute-from-gregorian (list 2 1 y1)))
486 (e (calendar-absolute-from-gregorian (list 11 1 y2)))
487 (displayed-month 2)
488 (displayed-year y1)
489 holiday-list)
490 (while (<= s e)
491 (setq holiday-list (append holiday-list (calendar-holiday-list)))
492 (calendar-increment-month displayed-month displayed-year 3)
493 (setq s (calendar-absolute-from-gregorian
494 (list displayed-month 1 displayed-year))))
495 (save-excursion
496 (calendar-in-read-only-buffer holiday-buffer
497 (calendar-set-mode-line
498 (if (= y1 y2)
499 (format "%s for %s" title y1)
500 (format "%s for %s-%s" title y1 y2)))
501 (insert
502 (mapconcat
503 (lambda (x) (concat (calendar-date-string (car x))
504 ": " (cadr x)))
505 holiday-list "\n")))
506 (message "Computing holidays...done"))))
507
508 ;;;###autoload
509 (defalias 'holiday-list 'list-holidays)
510
511 ;;;###diary-autoload
512 (defun calendar-check-holidays (date)
513 "Check the list of holidays for any that occur on DATE.
514 DATE is a list (month day year). This function considers the
515 holidays from the list `calendar-holidays', and returns a list of
516 strings describing those holidays that apply on DATE, or nil if none do."
517 (let ((displayed-month (calendar-extract-month date))
518 (displayed-year (calendar-extract-year date))
519 holiday-list)
520 (dolist (h (calendar-holiday-list) holiday-list)
521 (if (calendar-date-equal date (car h))
522 (setq holiday-list (append holiday-list (cdr h)))))))
523
524
525 ;; Formerly cal-tex-list-holidays.
526 (defun holiday-in-range (d1 d2)
527 "Generate a list of all holidays in range from absolute date D1 to D2."
528 (let* ((start (calendar-gregorian-from-absolute d1))
529 (displayed-month (calendar-extract-month start))
530 (displayed-year (calendar-extract-year start))
531 (end (calendar-gregorian-from-absolute d2))
532 (end-month (calendar-extract-month end))
533 (end-year (calendar-extract-year end))
534 (number-of-intervals
535 (1+ (/ (calendar-interval displayed-month displayed-year
536 end-month end-year)
537 3)))
538 holidays in-range a)
539 (calendar-increment-month displayed-month displayed-year 1)
540 (dotimes (_idummy number-of-intervals)
541 (setq holidays (append holidays (calendar-holiday-list)))
542 (calendar-increment-month displayed-month displayed-year 3))
543 (dolist (hol holidays)
544 (and (car hol)
545 (setq a (calendar-absolute-from-gregorian (car hol)))
546 (and (<= d1 a) (<= a d2))
547 (setq in-range (append (list hol) in-range))))
548 in-range))
549
550
551 (declare-function x-popup-menu "menu.c" (position menu))
552
553 ;;;###cal-autoload
554 (defun calendar-cursor-holidays (&optional date event)
555 "Find holidays for the date specified by the cursor in the calendar window.
556 Optional DATE is a list (month day year) to use instead of the
557 cursor position. EVENT specifies a buffer position to use for a date."
558 (interactive (list nil last-nonmenu-event))
559 (message "Checking holidays...")
560 (or date (setq date (calendar-cursor-to-date t event)))
561 (let ((date-string (calendar-date-string date))
562 (holiday-list (calendar-check-holidays date))
563 selection msg)
564 (if (mouse-event-p event)
565 (and (setq selection (cal-menu-x-popup-menu event
566 (format "Holidays for %s" date-string)
567 (if holiday-list
568 (mapcar 'list holiday-list)
569 '("None"))))
570 (call-interactively selection))
571 (if (not holiday-list)
572 (message "No holidays known for %s" date-string)
573 (if (<= (length (setq msg
574 (format "%s: %s" date-string
575 (mapconcat 'identity holiday-list "; "))))
576 (frame-width))
577 (message "%s" msg)
578 (calendar-in-read-only-buffer holiday-buffer
579 (calendar-set-mode-line date-string)
580 (insert (mapconcat 'identity holiday-list "\n")))
581 (message "Checking holidays...done"))))))
582
583 ;; FIXME move to calendar?
584 ;;;###cal-autoload
585 (defun calendar-mark-holidays (&optional event)
586 "Mark notable days in the calendar window.
587 If EVENT is non-nil, it's an event indicating the buffer position to
588 use instead of point."
589 (interactive (list last-nonmenu-event))
590 ;; If called from a menu, with the calendar window not selected.
591 (with-current-buffer
592 (if event (window-buffer (posn-window (event-start event)))
593 (current-buffer))
594 (setq calendar-mark-holidays-flag t)
595 (message "Marking holidays...")
596 (dolist (holiday (calendar-holiday-list))
597 (calendar-mark-visible-date (car holiday) calendar-holiday-marker))
598 (message "Marking holidays...done")))
599
600 ;; Below are the functions that calculate the dates of holidays; these
601 ;; are eval'ed in the function calendar-holiday-list. If you
602 ;; write other such functions, be sure to imitate the style used below.
603 ;; Remember that each function must return a list of items of the form
604 ;; ((month day year) string) of VISIBLE dates in the calendar window.
605
606 (defun holiday-fixed (month day string)
607 "Holiday on MONTH, DAY (Gregorian) called STRING.
608 If MONTH, DAY is visible, the value returned is the list (((MONTH DAY year)
609 STRING)). Returns nil if it is not visible in the current calendar window."
610 ;; This determines whether a given month is visible in the calendar.
611 ;; cf calendar-date-is-visible-p (which also checks the year part).
612 ;; The day is irrelevant since only full months are displayed.
613 ;; Since the calendar displays three months at a time, month N
614 ;; is visible if displayed-month = N-1, N, N+1.
615 ;; In particular, November is visible if d-m = 10, 11, 12.
616 ;; This is useful, because we can do a one-sided test:
617 ;; November is visible if d-m > 9. (Similarly, February is visible if
618 ;; d-m < 4.)
619 ;; To determine if December is visible, we can shift the calendar
620 ;; back a month and ask if November is visible; to determine if
621 ;; October is visible, we can shift it forward a month and ask if
622 ;; November is visible; etc.
623 (let ((m displayed-month)
624 (y displayed-year))
625 (calendar-increment-month m y (- 11 month))
626 (if (> m 9) ; Is November visible?
627 (list (list (list month day y) string)))))
628
629 (defun holiday-float (month dayname n string &optional day)
630 "Holiday called STRING on the Nth DAYNAME after/before MONTH DAY.
631 DAYNAME=0 means Sunday, DAYNAME=1 means Monday, and so on.
632 If N>0, use the Nth DAYNAME after MONTH DAY.
633 If N<0, use the Nth DAYNAME before MONTH DAY.
634 DAY defaults to 1 if N>0, and MONTH's last day otherwise.
635 If the holiday is visible in the calendar window, returns a
636 list (((month day year) STRING)). Otherwise returns nil."
637 ;; This is messy because the holiday may be visible, while the date
638 ;; on which it is based is not. For example, the first Monday after
639 ;; December 30 may be visible when January is not. For large values
640 ;; of |n| the problem is more grotesque. If we didn't have to worry
641 ;; about such cases, we could just use the original version of this
642 ;; function:
643 ;; (let ((m displayed-month)
644 ;; (y displayed-year))
645 ;; (calendar-increment-month m y (- 11 month))
646 ;; (if (> m 9); month in year y is visible
647 ;; (list (list (calendar-nth-named-day n dayname month y day) string)))))
648 (let* ((m1 displayed-month)
649 (y1 displayed-year)
650 (m2 displayed-month)
651 (y2 displayed-year)
652 (d1 (progn ; first possible base date for holiday
653 (calendar-increment-month m1 y1 -1)
654 (+ (calendar-nth-named-absday 1 dayname m1 y1)
655 (* -7 n)
656 (if (> n 0) 1 -7))))
657 (d2 ; last possible base date for holiday
658 (progn
659 (calendar-increment-month m2 y2 1)
660 (+ (calendar-nth-named-absday -1 dayname m2 y2)
661 (* -7 n)
662 (if (> n 0) 7 -1))))
663 (y1 (calendar-extract-year (calendar-gregorian-from-absolute d1)))
664 (y2 (calendar-extract-year (calendar-gregorian-from-absolute d2)))
665 (y ; year of base date
666 (if (or (= y1 y2) (> month 9))
667 y1
668 y2))
669 (d ; day of base date
670 (or day (if (> n 0)
671 1
672 (calendar-last-day-of-month month y))))
673 (date ; base date for holiday
674 (calendar-absolute-from-gregorian (list month d y))))
675 (and (<= d1 date) (<= date d2)
676 (list (list (calendar-nth-named-day n dayname month y d)
677 string)))))
678
679 (defun holiday-filter-visible-calendar (hlist)
680 "Filter list of holidays HLIST, and return only the visible ones.
681 HLIST is a list of elements of the form (DATE) TEXT."
682 (delq nil (mapcar (lambda (p)
683 (and (car p) (calendar-date-is-visible-p (car p)) p))
684 hlist)))
685
686 (defun holiday-sexp (sexp string)
687 "Sexp holiday for dates in the calendar window.
688 SEXP is an expression in variable `year' that is evaluated to
689 give `date'. STRING is an expression in `date' that evaluates to
690 the holiday description of `date'. If `date' is visible in the
691 calendar window, the holiday STRING is on that date. If date is
692 nil, or if the date is not visible, there is no holiday."
693 (let ((m displayed-month)
694 (y displayed-year)
695 year date)
696 (calendar-increment-month m y -1)
697 (holiday-filter-visible-calendar
698 (list
699 (progn
700 (setq year y
701 date (eval sexp))
702 (list date (if date (eval string))))
703 (progn
704 (setq year (1+ y)
705 date (eval sexp))
706 (list date (if date (eval string))))))))
707
708
709 (defun holiday-advent (&optional n string)
710 "Date of Nth day after advent (named STRING), if visible in calendar window.
711 Negative values of N are interpreted as days before advent.
712 STRING is used purely for display purposes. The return value has
713 the form ((MONTH DAY YEAR) STRING), where the date is that of the
714 Nth day before or after advent.
715
716 For backwards compatibility, if this function is called with no
717 arguments, then it returns the value appropriate for advent itself."
718 ;; Backwards compatibility layer.
719 (if (not n)
720 (holiday-advent 0 "Advent")
721 (let* ((year displayed-year)
722 (month displayed-month)
723 (advent (progn
724 (calendar-increment-month month year -1)
725 (calendar-gregorian-from-absolute
726 (+ n
727 (calendar-dayname-on-or-before
728 0
729 (calendar-absolute-from-gregorian
730 (list 12 3 year))))))))
731 (if (calendar-date-is-visible-p advent)
732 (list (list advent string))))))
733
734 (defun holiday-easter-etc (&optional n string)
735 "Date of Nth day after Easter (named STRING), if visible in calendar window.
736 Negative values of N are interpreted as days before Easter.
737 STRING is used purely for display purposes. The return value has
738 the form ((MONTH DAY YEAR) STRING), where the date is that of the
739 Nth day before or after Easter.
740
741 For backwards compatibility, if this function is called with no
742 arguments, then it returns a list of \"standard\" Easter-related
743 holidays (with more entries if `calendar-christian-all-holidays-flag'
744 is non-nil)."
745 ;; Backwards compatibility layer.
746 (if (not n)
747 (apply 'append
748 (mapcar (lambda (e)
749 (apply 'holiday-easter-etc e))
750 ;; The combined list is not in order.
751 (append
752 (if calendar-christian-all-holidays-flag
753 '((-63 "Septuagesima Sunday")
754 (-56 "Sexagesima Sunday")
755 (-49 "Shrove Sunday")
756 (-48 "Shrove Monday")
757 (-47 "Shrove Tuesday")
758 (-14 "Passion Sunday")
759 (-7 "Palm Sunday")
760 (-3 "Maundy Thursday")
761 (35 "Rogation Sunday")
762 (39 "Ascension Day")
763 (49 "Pentecost (Whitsunday)")
764 (50 "Whitmonday")
765 (56 "Trinity Sunday")
766 (60 "Corpus Christi")))
767 '((-46 "Ash Wednesday")
768 (-2 "Good Friday")
769 (0 "Easter Sunday")))))
770 (let* ((century (1+ (/ displayed-year 100)))
771 (shifted-epact ; age of moon for April 5...
772 (% (+ 14 (* 11 (% displayed-year 19)) ; ...by Nicaean rule
773 (- ; ...corrected for the Gregorian century rule
774 (/ (* 3 century) 4))
775 (/ ; ...corrected for Metonic cycle inaccuracy
776 (+ 5 (* 8 century)) 25)
777 (* 30 century)) ; keeps value positive
778 30))
779 (adjusted-epact ; adjust for 29.5 day month
780 (if (or (zerop shifted-epact)
781 (and (= shifted-epact 1) (< 10 (% displayed-year 19))))
782 (1+ shifted-epact)
783 shifted-epact))
784 (paschal-moon ; day after the full moon on or after March 21
785 (- (calendar-absolute-from-gregorian (list 4 19 displayed-year))
786 adjusted-epact))
787 (abs-easter (calendar-dayname-on-or-before 0 (+ paschal-moon 7)))
788 (greg (calendar-gregorian-from-absolute (+ abs-easter n))))
789 (if (calendar-date-is-visible-p greg)
790 (list (list greg string))))))
791
792 ;; Prior call to calendar-julian-from-absolute will autoload cal-julian.
793 (declare-function calendar-julian-to-absolute "cal-julian" (date))
794
795 (defun holiday-greek-orthodox-easter (&optional n string)
796 "Date of Nth day after Easter (named STRING), if visible in calendar window.
797 It is calculated according to the rule of the Council of Nicaea.
798 Negative values of N are interpreted as days before Easter.
799 STRING is used purely for display purposes. The return value has
800 the form ((MONTH DAY YEAR) STRING), where the date is that of the
801 Nth day before or after Easter.
802
803 For backwards compatibility, if this function is called with no
804 arguments, it returns the date of Pascha (Greek Orthodox Easter)."
805 (let* ((m displayed-month)
806 (y displayed-year)
807 (julian-year (progn
808 (calendar-increment-month m y 1)
809 (calendar-extract-year
810 (calendar-julian-from-absolute
811 (calendar-absolute-from-gregorian
812 (list m (calendar-last-day-of-month m y) y))))))
813 (shifted-epact ; age of moon for April 5
814 (% (+ 14
815 (* 11 (% julian-year 19)))
816 30))
817 (paschal-moon ; day after full moon on or after March 21
818 (- (calendar-julian-to-absolute (list 4 19 julian-year))
819 shifted-epact))
820 (abs-easter (calendar-dayname-on-or-before 0 (+ paschal-moon 7)))
821 (greg (calendar-gregorian-from-absolute (+ abs-easter (or n 0)))))
822 (if (calendar-date-is-visible-p greg)
823 (list (list greg (or string "Pascha (Greek Orthodox Easter)"))))))
824
825 (provide 'holidays)
826
827 ;;; holidays.el ends here