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)
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.
34 (require 'hol-loaddefs
)
37 (defun calendar-holiday-list ()
38 "Form the list of holidays that occur on dates in the calendar window.
39 The holidays are those in the list `calendar-holidays'."
42 (dolist (p calendar-holidays res
)
43 (if (setq h
(if calendar-debug-sexp
44 (let ((stack-trace-on-error t
))
49 (message "Bad holiday list item: %s" p
)
51 (setq res
(append h res
))))
52 'calendar-date-compare
)))
54 (defvar displayed-month
) ; from calendar-generate
55 (defvar displayed-year
)
58 (defun calendar-list-holidays ()
59 "Create a buffer containing the holidays for the current calendar window.
60 The holidays are those in the list `calendar-notable-days'.
61 Returns non-nil if any holidays are found."
63 (message "Looking up holidays...")
64 (let ((holiday-list (calendar-holiday-list))
69 (if (not holiday-list
)
70 (message "Looking up holidays...none found")
71 (calendar-in-read-only-buffer holiday-buffer
72 (calendar-increment-month m1 y1 -
1)
73 (calendar-increment-month m2 y2
1)
74 (calendar-set-mode-line
76 (format "Notable Dates from %s to %s, %d%%-"
77 (calendar-month-name m1
) (calendar-month-name m2
) y2
)
78 (format "Notable Dates from %s, %d to %s, %d%%-"
79 (calendar-month-name m1
) y1
(calendar-month-name m2
) y2
)))
82 (lambda (x) (concat (calendar-date-string (car x
))
85 (message "Looking up holidays...done"))
88 (define-obsolete-function-alias
89 'list-calendar-holidays
'calendar-list-holidays
"23.1")
92 (defun holidays (&optional arg
)
93 "Display the holidays for last month, this month, and next month.
94 If called with an optional prefix argument ARG, prompts for month and year.
95 This function is suitable for execution in a .emacs file."
98 (let* ((completion-ignore-case t
)
99 (date (if arg
(calendar-read-date t
)
100 (calendar-current-date)))
101 (displayed-month (calendar-extract-month date
))
102 (displayed-year (calendar-extract-year date
)))
103 (calendar-list-holidays))))
105 ;; rms: "Emacs commands to display a list of something generally start
106 ;; with `list-'. Please make `list-holidays' the principal name."
108 (defun list-holidays (y1 &optional y2 l label
)
109 "Display holidays for years Y1 to Y2 (inclusive).
110 Y2 defaults to Y1. The optional list of holidays L defaults to
111 `calendar-holidays'. If you want to control what holidays are
112 displayed, use a different list. For example,
114 (list-holidays 2006 2006
115 (append holiday-general-holidays holiday-local-holidays))
117 will display holidays for the year 2006 defined in the two
118 mentioned lists, and nothing else.
120 When called interactively, this command offers a choice of
121 holidays, based on the variables `holiday-solar-holidays' etc. See the
122 documentation of `calendar-holidays' for a list of the variables
123 that control the choices, as well as a description of the format
126 The optional LABEL is used to label the buffer created."
128 (let* ((start-year (calendar-read
129 "Starting year of holidays (>0): "
131 (int-to-string (calendar-extract-year
132 (calendar-current-date)))))
133 (end-year (calendar-read
134 (format "Ending year (inclusive) of holidays (>=%s): "
136 (lambda (x) (>= x start-year
))
137 (int-to-string start-year
)))
138 (completion-ignore-case t
)
141 (cons "All" calendar-holidays
)
142 (cons "Equinoxes/Solstices"
143 (list (list 'solar-equinoxes-solstices
)))
144 (if holiday-general-holidays
145 (cons "General" holiday-general-holidays
))
146 (if holiday-local-holidays
147 (cons "Local" holiday-local-holidays
))
148 (if holiday-other-holidays
149 (cons "Other" holiday-other-holidays
))
150 (if holiday-christian-holidays
151 (cons "Christian" holiday-christian-holidays
))
152 (if holiday-hebrew-holidays
153 (cons "Hebrew" holiday-hebrew-holidays
))
154 (if holiday-islamic-holidays
155 (cons "Islamic" holiday-islamic-holidays
))
156 (if holiday-bahai-holidays
157 (cons "Baha'i" holiday-bahai-holidays
))
158 (if holiday-oriental-holidays
159 (cons "Oriental" holiday-oriental-holidays
))
160 (if holiday-solar-holidays
161 (cons "Solar" holiday-solar-holidays
))
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")
170 (if (member choice
'("Ask" ""))
172 (format "%s Holidays" choice
)))))
173 (list start-year end-year which name
)))
174 (unless y2
(setq y2 y1
))
175 (message "Computing holidays...")
176 (let ((calendar-holidays (or l calendar-holidays
))
177 (title (or label
"Holidays"))
178 (s (calendar-absolute-from-gregorian (list 2 1 y1
)))
179 (e (calendar-absolute-from-gregorian (list 11 1 y2
)))
184 (setq holiday-list
(append holiday-list
(calendar-holiday-list)))
185 (calendar-increment-month displayed-month displayed-year
3)
186 (setq s
(calendar-absolute-from-gregorian
187 (list displayed-month
1 displayed-year
))))
189 (calendar-in-read-only-buffer holiday-buffer
190 (calendar-set-mode-line
192 (format "%s for %s" title y1
)
193 (format "%s for %s-%s" title y1 y2
)))
196 (lambda (x) (concat (calendar-date-string (car x
))
199 (message "Computing holidays...done"))))
202 (defalias 'holiday-list
'list-holidays
)
205 (defun calendar-check-holidays (date)
206 "Check the list of holidays for any that occur on DATE.
207 The value returned is a list of strings of relevant holiday descriptions.
208 The holidays are those in the list `calendar-holidays'."
209 (let ((displayed-month (calendar-extract-month date
))
210 (displayed-year (calendar-extract-year date
))
212 (dolist (h (calendar-holiday-list) holiday-list
)
213 (if (calendar-date-equal date
(car h
))
214 (setq holiday-list
(append holiday-list
(cdr h
)))))))
216 (define-obsolete-function-alias
217 'check-calendar-holidays
'calendar-check-holidays
"23.1")
220 (defun calendar-cursor-holidays ()
221 "Find holidays for the date specified by the cursor in the calendar window."
223 (message "Checking holidays...")
224 (let* ((date (calendar-cursor-to-date t
))
225 (date-string (calendar-date-string date
))
226 (holiday-list (calendar-check-holidays date
))
227 (holiday-string (mapconcat 'identity holiday-list
"; "))
228 (msg (format "%s: %s" date-string holiday-string
)))
229 (if (not holiday-list
)
230 (message "No holidays known for %s" date-string
)
231 (if (<= (length msg
) (frame-width))
233 (calendar-in-read-only-buffer holiday-buffer
234 (calendar-set-mode-line date-string
)
235 (insert (mapconcat 'identity holiday-list
"\n")))
236 (message "Checking holidays...done")))))
239 (defun calendar-mark-holidays ()
240 "Mark notable days in the calendar window."
242 (setq calendar-mark-holidays-flag t
)
243 (message "Marking holidays...")
244 (dolist (holiday (calendar-holiday-list))
245 (calendar-mark-visible-date (car holiday
) calendar-holiday-marker
))
246 (message "Marking holidays...done"))
248 (define-obsolete-function-alias
249 'mark-calendar-holidays
'calendar-mark-holidays
"23.1")
251 ;; Below are the functions that calculate the dates of holidays; these
252 ;; are eval'ed in the function calendar-holiday-list. If you
253 ;; write other such functions, be sure to imitate the style used below.
254 ;; Remember that each function must return a list of items of the form
255 ;; ((month day year) string) of VISIBLE dates in the calendar window.
257 (defun holiday-fixed (month day string
)
258 "Holiday on MONTH, DAY (Gregorian) called STRING.
259 If MONTH, DAY is visible, the value returned is the list (((MONTH DAY year)
260 STRING)). Returns nil if it is not visible in the current calendar window."
261 ;; This determines whether a given month is visible in the calendar.
262 ;; cf calendar-date-is-visible-p (which also checks the year part).
263 ;; The day is irrelevant since only full months are displayed.
264 ;; Since the calendar displays three months at a time, month N
265 ;; is visible if displayed-month = N-1, N, N+1.
266 ;; In particular, November is visible if d-m = 10, 11, 12.
267 ;; This is useful, because we can do a one-sided test:
268 ;; November is visible if d-m > 9. (Similarly, February is visible if
270 ;; To determine if December is visible, we can shift the calendar
271 ;; back a month and ask if November is visible; to determine if
272 ;; October is visible, we can shift it forward a month and ask if
273 ;; November is visible; etc.
274 (let ((m displayed-month
)
276 (calendar-increment-month m y
(- 11 month
))
277 (if (> m
9) ; is november visible?
278 (list (list (list month day y
) string
)))))
280 (defun holiday-float (month dayname n string
&optional day
)
281 "Holiday on MONTH, DAYNAME (Nth occurrence) called STRING.
282 If the Nth DAYNAME in MONTH is visible, the value returned is the list
283 \(((MONTH DAY year) STRING)).
285 If N<0, count backward from the end of MONTH.
287 An optional parameter DAY means the Nth DAYNAME on or after/before MONTH DAY.
289 Returns nil if it is not visible in the current calendar window."
290 ;; This is messy because the holiday may be visible, while the date
291 ;; on which it is based is not. For example, the first Monday after
292 ;; December 30 may be visible when January is not. For large values
293 ;; of |n| the problem is more grotesque. If we didn't have to worry
294 ;; about such cases, we could just use the original version of this
296 ;; (let ((m displayed-month)
297 ;; (y displayed-year))
298 ;; (calendar-increment-month m y (- 11 month))
299 ;; (if (> m 9); month in year y is visible
300 ;; (list (list (calendar-nth-named-day n dayname month y day) string)))))
301 (let* ((m1 displayed-month
)
305 (d1 (progn ; first possible base date for holiday
306 (calendar-increment-month m1 y1 -
1)
307 (+ (calendar-nth-named-absday 1 dayname m1 y1
)
310 (d2 ; last possible base date for holiday
312 (calendar-increment-month m2 y2
1)
313 (+ (calendar-nth-named-absday -
1 dayname m2 y2
)
316 (y1 (calendar-extract-year (calendar-gregorian-from-absolute d1
)))
317 (y2 (calendar-extract-year (calendar-gregorian-from-absolute d2
)))
318 (y ; year of base date
319 (if (or (= y1 y2
) (> month
9))
322 (d ; day of base date
325 (calendar-last-day-of-month month y
))))
326 (date ; base date for holiday
327 (calendar-absolute-from-gregorian (list month d y
))))
328 (and (<= d1 date
) (<= date d2
)
329 (list (list (calendar-nth-named-day n dayname month y d
)
332 (defun holiday-filter-visible-calendar (hlist)
333 "Filter list of holidays HLIST, and return only the visible ones.
334 HLIST is a list of elements of the form (DATE) TEXT."
335 (delq nil
(mapcar (lambda (p)
336 (and (car p
) (calendar-date-is-visible-p (car p
)) p
))
339 (define-obsolete-function-alias
340 'filter-visible-calendar-holidays
'holiday-filter-visible-calendar
"23.1")
342 (defun holiday-sexp (sexp string
)
343 "Sexp holiday for dates in the calendar window.
344 SEXP is an expression in variable `year' that is evaluated to
345 give `date'. STRING is an expression in `date' that evaluates to
346 the holiday description of `date'. If `date' is visible in the
347 calendar window, the holiday STRING is on that date. If date is
348 nil, or if the date is not visible, there is no holiday."
349 (let ((m displayed-month
)
352 (calendar-increment-month m y -
1)
353 (holiday-filter-visible-calendar
358 (list date
(if date
(eval string
))))
362 (list date
(if date
(eval string
))))))))
365 (defun holiday-advent (&optional n string
)
366 "Date of Nth day after advent (named STRING), if visible in calendar window.
367 Negative values of N are interpreted as days before advent.
368 STRING is used purely for display purposes. The return value has
369 the form ((MONTH DAY YEAR) STRING), where the date is that of the
370 Nth day before or after advent.
372 For backwards compatibility, if this function is called with no
373 arguments, then it returns the value appropriate for advent itself."
374 ;; Backwards compatibility layer.
376 (holiday-advent 0 "Advent")
377 (let* ((year displayed-year
)
378 (month displayed-month
)
380 (calendar-increment-month month year -
1)
381 (calendar-gregorian-from-absolute
383 (calendar-dayname-on-or-before
385 (calendar-absolute-from-gregorian
386 (list 12 3 year
))))))))
387 (if (calendar-date-is-visible-p advent
)
388 (list (list advent string
))))))
390 (defun holiday-easter-etc (&optional n string
)
391 "Date of Nth day after Easter (named STRING), if visible in calendar window.
392 Negative values of N are interpreted as days before Easter.
393 STRING is used purely for display purposes. The return value has
394 the form ((MONTH DAY YEAR) STRING), where the date is that of the
395 Nth day before or after Easter.
397 For backwards compatibility, if this function is called with no
398 arguments, then it returns a list of \"standard\" Easter-related
399 holidays (with more entries if `calendar-christian-all-holidays-flag'
401 ;; Backwards compatibility layer.
405 (apply 'holiday-easter-etc e
))
406 ;; The combined list is not in order.
408 (if calendar-christian-all-holidays-flag
409 '((-63 "Septuagesima Sunday")
410 (-56 "Sexagesima Sunday")
411 (-49 "Shrove Sunday")
412 (-48 "Shrove Monday")
413 (-47 "Shrove Tuesday")
414 (-14 "Passion Sunday")
416 (-3 "Maundy Thursday")
417 (35 "Rogation Sunday")
419 (49 "Pentecost (Whitsunday)")
421 (56 "Trinity Sunday")
422 (60 "Corpus Christi")))
423 '((-46 "Ash Wednesday")
425 (0 "Easter Sunday")))))
426 (let* ((century (1+ (/ displayed-year
100)))
427 (shifted-epact ; age of moon for April 5...
428 (%
(+ 14 (* 11 (% displayed-year
19)) ; ...by Nicaean rule
429 (- ; ...corrected for the Gregorian century rule
431 (/ ; ...corrected for Metonic cycle inaccuracy
432 (+ 5 (* 8 century
)) 25)
433 (* 30 century
)) ; keeps value positive
435 (adjusted-epact ; adjust for 29.5 day month
436 (if (or (zerop shifted-epact
)
437 (and (= shifted-epact
1) (< 10 (% displayed-year
19))))
440 (paschal-moon ; day after the full moon on or after March 21
441 (- (calendar-absolute-from-gregorian (list 4 19 displayed-year
))
443 (abs-easter (calendar-dayname-on-or-before 0 (+ paschal-moon
7)))
444 (greg (calendar-gregorian-from-absolute (+ abs-easter n
))))
445 (if (calendar-date-is-visible-p greg
)
446 (list (list greg string
))))))
448 ;; Prior call to calendar-julian-from-absolute will autoload cal-julian.
449 (declare-function calendar-julian-to-absolute
"cal-julian" (date))
451 (defun holiday-greek-orthodox-easter ()
452 "Date of Easter according to the rule of the Council of Nicaea."
453 (let* ((m displayed-month
)
456 (calendar-increment-month m y
1)
457 (calendar-extract-year
458 (calendar-julian-from-absolute
459 (calendar-absolute-from-gregorian
460 (list m
(calendar-last-day-of-month m y
) y
))))))
461 (shifted-epact ; age of moon for April 5
463 (* 11 (% julian-year
19)))
465 (paschal-moon ; day after full moon on or after March 21
466 (- (calendar-julian-to-absolute (list 4 19 julian-year
))
468 (nicaean-easter ; Sunday following the Paschal moon
469 (calendar-gregorian-from-absolute
470 (calendar-dayname-on-or-before 0 (+ paschal-moon
7)))))
471 (if (calendar-date-is-visible-p nicaean-easter
)
472 (list (list nicaean-easter
"Pascha (Greek Orthodox Easter)")))))
476 ;; arch-tag: 48eb3117-75a7-4dbe-8fd9-873c3cbb0d37
477 ;;; holidays.el ends here