(list-holidays): Make Y2 optional.
[emacs.git] / lisp / calendar / cal-bahai.el
blob57ff7bad89aad9c76c752f688301ea60bb80b293
1 ;;; cal-bahai.el --- calendar functions for the Bahá'í calendar.
3 ;; Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 ;; Free Software Foundation, Inc.
6 ;; Author: John Wiegley <johnw@gnu.org>
7 ;; Keywords: calendar
8 ;; Human-Keywords: Bahá'í calendar, Bahá'í, Baha'i, Bahai, calendar, diary
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 ;; This collection of functions implements the features of calendar.el
30 ;; and diary.el that deal with the Bahá'í calendar.
32 ;; The Bahá'í (http://www.bahai.org) calendar system is based on a
33 ;; solar cycle of 19 months with 19 days each. The four remaining
34 ;; "intercalary" days are called the Ayyám-i-Há (days of Há), and are
35 ;; placed between the 18th and 19th months. They are meant as a time
36 ;; of festivals preceding the 19th month, which is the month of
37 ;; fasting. In Gregorian leap years, there are 5 of these days (Há
38 ;; has the numerical value of 5 in the arabic abjad, or
39 ;; letter-to-number, reckoning).
41 ;; Each month is named after an attribute of God, as are the 19 days
42 ;; -- which have the same names as the months. There is also a name
43 ;; for each year in every 19 year cycle. These cycles are called
44 ;; Váhids. A cycle of 19 Váhids (361 years) is called a Kullu-Shay,
45 ;; which means "all things".
47 ;; The calendar was named the "Badí` calendar" by its author, the Báb.
48 ;; It uses a week of seven days, corresponding to the Gregorian week,
49 ;; each of which has its own name, again patterned after the
50 ;; attributes of God.
52 ;; Note: The days of Ayyám-i-Há are encoded as zero and negative
53 ;; offsets from the first day of the final month. So, (19 -3 157) is
54 ;; the first day of Ayyám-i-Há, in the year 157 BE.
56 ;;; Code:
58 (require 'calendar)
60 (defconst calendar-bahai-month-name-array
61 ["Bahá" "Jalál" "Jamál" "`Azamat" "Núr" "Rahmat" "Kalimát" "Kamál"
62 "Asmá" "`Izzat" "Mashiyyat" "`Ilm" "Qudrat" "Qawl" "Masá'il"
63 "Sharaf" "Sultán" "Mulk" "`Alá"]
64 "Array of the month names in the Bahá'í calendar.")
66 (defconst calendar-bahai-epoch (calendar-absolute-from-gregorian '(3 21 1844))
67 "Absolute date of start of Bahá'í calendar = March 21, 1844 AD.")
69 (defun calendar-bahai-leap-year-p (year)
70 "True if Bahá'í YEAR is a leap year in the Bahá'í calendar."
71 (calendar-leap-year-p (+ year 1844)))
73 (defconst calendar-bahai-leap-base
74 (+ (/ 1844 4) (- (/ 1844 100)) (/ 1844 400))
75 "Number of leap years between 1 and 1844 AD, inclusive.
76 Used by `calendar-absolute-from-bahai'.")
78 (defun calendar-absolute-from-bahai (date)
79 "Compute absolute date from Bahá'í date DATE.
80 The absolute date is the number of days elapsed since the (imaginary)
81 Gregorian date Sunday, December 31, 1 BC."
82 (let* ((month (extract-calendar-month date))
83 (day (extract-calendar-day date))
84 (year (extract-calendar-year date))
85 (prior-years (+ (1- year) 1844))
86 (leap-days (- (+ (/ prior-years 4) ; leap days in prior years
87 (- (/ prior-years 100))
88 (/ prior-years 400))
89 calendar-bahai-leap-base)))
90 (+ (1- calendar-bahai-epoch) ; days before epoch
91 (* 365 (1- year)) ; days in prior years
92 leap-days
93 (calendar-sum m 1 (< m month) 19)
94 (if (= month 19)
95 (if (calendar-bahai-leap-year-p year) 5 4)
97 day))) ; days so far this month
99 (defun calendar-bahai-from-absolute (date)
100 "Bahá'í date (month day year) corresponding to the absolute DATE."
101 (if (< date calendar-bahai-epoch)
102 (list 0 0 0) ; pre-Bahá'í date
103 (let* ((greg (calendar-gregorian-from-absolute date))
104 (gmonth (extract-calendar-month greg))
105 (year (+ (- (extract-calendar-year greg) 1844)
106 (if (or (> gmonth 3)
107 (and (= gmonth 3)
108 (>= (extract-calendar-day greg) 21)))
109 1 0)))
110 (month ; search forward from Baha
111 (1+ (calendar-sum m 1
112 (> date (calendar-absolute-from-bahai (list m 19 year)))
113 1)))
114 (day ; calculate the day by subtraction
115 (- date
116 (1- (calendar-absolute-from-bahai (list month 1 year))))))
117 (list month day year))))
119 ;;;###cal-autoload
120 (defun calendar-bahai-date-string (&optional date)
121 "String of Bahá'í date of Gregorian DATE.
122 Defaults to today's date if DATE is not given."
123 (let* ((bahai-date (calendar-bahai-from-absolute
124 (calendar-absolute-from-gregorian
125 (or date (calendar-current-date)))))
126 (y (extract-calendar-year bahai-date))
127 (m (extract-calendar-month bahai-date))
128 (d (extract-calendar-day bahai-date))
129 (monthname (if (and (= m 19)
130 (<= d 0))
131 "Ayyám-i-Há"
132 (aref calendar-bahai-month-name-array (1- m))))
133 (day (int-to-string
134 (if (<= d 0)
135 (+ d (if (calendar-bahai-leap-year-p y) 5 4))
136 d)))
137 (year (int-to-string y))
138 (month (int-to-string m))
139 dayname)
140 (if (< y 1)
142 ;; Can't call calendar-date-string because of monthname oddity.
143 (mapconcat 'eval calendar-date-display-form ""))))
145 ;;;###cal-autoload
146 (defun calendar-bahai-print-date ()
147 "Show the Bahá'í calendar equivalent of the selected date."
148 (interactive)
149 (let ((s (calendar-bahai-date-string (calendar-cursor-to-date t))))
150 (if (string-equal s "")
151 (message "Date is pre-Bahá'í")
152 (message "Bahá'í date: %s" s))))
154 (define-obsolete-function-alias
155 'calendar-print-bahai-date 'calendar-bahai-print-date "23.1")
157 (defun calendar-bahai-read-date ()
158 "Interactively read the arguments for a Bahá'í date command.
159 Reads a year, month and day."
160 (let* ((today (calendar-current-date))
161 (year (calendar-read
162 "Bahá'í calendar year (not 0): "
163 (lambda (x) (not (zerop x)))
164 (int-to-string
165 (extract-calendar-year
166 (calendar-bahai-from-absolute
167 (calendar-absolute-from-gregorian today))))))
168 (completion-ignore-case t)
169 (month (cdr (assoc
170 (completing-read
171 "Bahá'í calendar month name: "
172 (mapcar 'list
173 (append calendar-bahai-month-name-array nil))
174 nil t)
175 (calendar-make-alist calendar-bahai-month-name-array
176 1))))
177 (day (calendar-read "Bahá'í calendar day (1-19): "
178 (lambda (x) (and (< 0 x) (<= x 19))))))
179 (list (list month day year))))
181 (define-obsolete-function-alias
182 'calendar-bahai-prompt-for-date 'calendar-bahai-read-date "23.1")
184 ;;;###cal-autoload
185 (defun calendar-bahai-goto-date (date &optional noecho)
186 "Move cursor to Bahá'í date DATE; echo Bahá'í date unless NOECHO is non-nil."
187 (interactive (calendar-bahai-read-date))
188 (calendar-goto-date (calendar-gregorian-from-absolute
189 (calendar-absolute-from-bahai date)))
190 (or noecho (calendar-bahai-print-date)))
192 (define-obsolete-function-alias
193 'calendar-goto-bahai-date 'calendar-bahai-goto-date "23.1")
195 (defvar displayed-month)
196 (defvar displayed-year)
198 ;;;###holiday-autoload
199 (defun holiday-bahai (month day string)
200 "Holiday on MONTH, DAY (Bahá'í) called STRING.
201 If MONTH, DAY (Bahá'í) is visible, the value returned is corresponding
202 Gregorian date in the form of the list (((month day year) STRING)). Returns
203 nil if it is not visible in the current calendar window."
204 (let* ((bahai-date (calendar-bahai-from-absolute
205 (calendar-absolute-from-gregorian
206 (list displayed-month 15 displayed-year))))
207 (m (extract-calendar-month bahai-date))
208 (y (extract-calendar-year bahai-date))
209 (date))
210 (unless (< m 1) ; Bahá'í calendar doesn't apply
211 (increment-calendar-month m y (- 10 month))
212 (if (> m 7) ; Bahá'í date might be visible
213 (let ((date (calendar-gregorian-from-absolute
214 (calendar-absolute-from-bahai (list month day y)))))
215 (if (calendar-date-is-visible-p date)
216 (list (list date string))))))))
218 (autoload 'diary-list-entries-1 "diary-lib")
220 ;; FIXME diary-bahai-mark-entries said the names could be spelled in full.
221 ;;;###diary-autoload
222 (defun diary-bahai-list-entries ()
223 "Add any Bahá'í date entries from the diary file to `diary-entries-list'.
224 Bahá'í date diary entries must be prefaced by `bahai-diary-entry-symbol'
225 \(normally a `B'). The same diary date forms govern the style of the
226 Bahá'í calendar entries, except that the Bahá'í month names must be given
227 numerically. The Bahá'í months are numbered from 1 to 19 with Bahá being
228 1 and 19 being `Alá. If a Bahá'í date diary entry begins with
229 `diary-nonmarking-symbol', the entry will appear in the diary listing, but
230 will not be marked in the calendar. This function is provided for use with
231 `nongregorian-diary-listing-hook'."
232 (diary-list-entries-1 calendar-bahai-month-name-array
233 bahai-diary-entry-symbol
234 'calendar-bahai-from-absolute))
235 (define-obsolete-function-alias
236 'list-bahai-diary-entries 'diary-bahai-list-entries "23.1")
239 (autoload 'calendar-mark-1 "diary-lib")
241 ;;;###diary-autoload
242 (defun calendar-bahai-mark-date-pattern (month day year &optional color)
243 "Mark dates in calendar window that conform to Bahá'í date MONTH/DAY/YEAR.
244 A value of 0 in any position is a wildcard. Optional argument COLOR is
245 passed to `mark-visible-calendar-date' as MARK."
246 (calendar-mark-1 month day year 'calendar-bahai-from-absolute
247 'calendar-absolute-from-bahai color))
249 (define-obsolete-function-alias
250 'mark-bahai-calendar-date-pattern 'calendar-bahai-mark-date-pattern "23.1")
253 (autoload 'diary-mark-entries-1 "diary-lib")
255 ;;;###diary-autoload
256 (defun diary-bahai-mark-entries ()
257 "Mark days in the calendar window that have Bahá'í date diary entries.
258 Marks each entry in `diary-file' (or included files) visible in the calendar
259 window. See `diary-bahai-list-entries' for more information."
260 (diary-mark-entries-1 calendar-bahai-month-name-array
261 bahai-diary-entry-symbol
262 'calendar-bahai-from-absolute
263 'calendar-bahai-mark-date-pattern))
265 (define-obsolete-function-alias
266 'mark-bahai-diary-entries 'diary-bahai-mark-entries "23.1")
268 ;;;###cal-autoload
269 (defun diary-bahai-insert-entry (arg)
270 "Insert a diary entry.
271 For the Bahá'í date corresponding to the date indicated by point.
272 Prefix argument ARG makes the entry nonmarking."
273 (interactive "P")
274 (let ((calendar-month-name-array calendar-bahai-month-name-array))
275 (make-diary-entry
276 (concat bahai-diary-entry-symbol
277 (calendar-date-string
278 (calendar-bahai-from-absolute
279 (calendar-absolute-from-gregorian (calendar-cursor-to-date t)))
280 nil t))
281 arg)))
283 (define-obsolete-function-alias
284 'insert-bahai-diary-entry 'diary-bahai-insert-entry "23.1")
286 ;;;###cal-autoload
287 (defun diary-bahai-insert-monthly-entry (arg)
288 "Insert a monthly diary entry.
289 For the day of the Bahá'í month corresponding to the date indicated by point.
290 Prefix argument ARG makes the entry nonmarking."
291 (interactive "P")
292 (let ((calendar-date-display-form (if european-calendar-style
293 '(day " * ")
294 '("* " day )))
295 (calendar-month-name-array calendar-bahai-month-name-array))
296 (make-diary-entry
297 (concat bahai-diary-entry-symbol
298 (calendar-date-string
299 (calendar-bahai-from-absolute
300 (calendar-absolute-from-gregorian (calendar-cursor-to-date t)))))
301 arg)))
303 (define-obsolete-function-alias
304 'insert-monthly-bahai-diary-entry 'diary-bahai-insert-monthly-entry "23.1")
306 ;;;###cal-autoload
307 (defun diary-bahai-insert-yearly-entry (arg)
308 "Insert an annual diary entry.
309 For the day of the Bahá'í year corresponding to the date indicated by point.
310 Prefix argument ARG will make the entry nonmarking."
311 (interactive "P")
312 (let ((calendar-date-display-form (if european-calendar-style
313 '(day " " monthname)
314 '(monthname " " day)))
315 (calendar-month-name-array calendar-bahai-month-name-array))
316 (make-diary-entry
317 (concat bahai-diary-entry-symbol
318 (calendar-date-string
319 (calendar-bahai-from-absolute
320 (calendar-absolute-from-gregorian (calendar-cursor-to-date t)))))
321 arg)))
323 (define-obsolete-function-alias
324 'insert-yearly-bahai-diary-entry 'diary-bahai-insert-yearly-entry "23.1")
326 (defvar date)
328 ;; To be called from list-sexp-diary-entries, where DATE is bound.
329 ;;;###diary-autoload
330 (defun diary-bahai-date ()
331 "Bahá'í calendar equivalent of date diary entry."
332 (format "Bahá'í date: %s" (calendar-bahai-date-string date)))
335 (provide 'cal-bahai)
337 ;; Local Variables:
338 ;; coding: utf-8
339 ;; End:
341 ;; arch-tag: c1cb1d67-862a-4264-a01c-41cb4df01f14
342 ;;; cal-bahai.el ends here