(calendar-bahai-leap-year-p)
[emacs.git] / lisp / calendar / cal-bahai.el
blob8a24ffae108897fc63014f2d7fbce47cf8acc617
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 'cal-julian)
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 19, 622 AD (Julian).")
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-prompt-for-date ()
158 "Ask for a Bahá'í date."
159 (let* ((today (calendar-current-date))
160 (year (calendar-read
161 "Bahá'í calendar year (not 0): "
162 (lambda (x) (not (zerop x)))
163 (int-to-string
164 (extract-calendar-year
165 (calendar-bahai-from-absolute
166 (calendar-absolute-from-gregorian today))))))
167 (completion-ignore-case t)
168 (month (cdr (assoc
169 (completing-read
170 "Bahá'í calendar month name: "
171 (mapcar 'list
172 (append calendar-bahai-month-name-array nil))
173 nil t)
174 (calendar-make-alist calendar-bahai-month-name-array
175 1))))
176 (day (calendar-read "Bahá'í calendar day (1-19): "
177 (lambda (x) (and (< 0 x) (<= x 19))))))
178 (list (list month day year))))
180 ;;;###cal-autoload
181 (defun calendar-bahai-goto-date (date &optional noecho)
182 "Move cursor to Bahá'í date DATE; echo Bahá'í date unless NOECHO is non-nil."
183 (interactive (calendar-bahai-prompt-for-date))
184 (calendar-goto-date (calendar-gregorian-from-absolute
185 (calendar-absolute-from-bahai date)))
186 (or noecho (calendar-bahai-print-date)))
188 (define-obsolete-function-alias
189 'calendar-goto-bahai-date 'calendar-bahai-goto-date "23.1")
191 (defvar displayed-month)
192 (defvar displayed-year)
194 ;;;###holiday-autoload
195 (defun holiday-bahai (month day string)
196 "Holiday on MONTH, DAY (Bahá'í) called STRING.
197 If MONTH, DAY (Bahá'í) is visible, the value returned is corresponding
198 Gregorian date in the form of the list (((month day year) STRING)). Returns
199 nil if it is not visible in the current calendar window."
200 (let* ((bahai-date (calendar-bahai-from-absolute
201 (calendar-absolute-from-gregorian
202 (list displayed-month 15 displayed-year))))
203 (m (extract-calendar-month bahai-date))
204 (y (extract-calendar-year bahai-date))
205 (date))
206 (unless (< m 1) ; Bahá'í calendar doesn't apply
207 (increment-calendar-month m y (- 10 month))
208 (if (> m 7) ; Bahá'í date might be visible
209 (let ((date (calendar-gregorian-from-absolute
210 (calendar-absolute-from-bahai (list month day y)))))
211 (if (calendar-date-is-visible-p date)
212 (list (list date string))))))))
214 (autoload 'diary-list-entries-1 "diary-lib")
216 ;; FIXME diary-bahai-mark-entries said the names could be spelled in full.
217 ;;;###diary-autoload
218 (defun diary-bahai-list-entries ()
219 "Add any Bahá'í date entries from the diary file to `diary-entries-list'.
220 Bahá'í date diary entries must be prefaced by `bahai-diary-entry-symbol'
221 \(normally a `B'). The same diary date forms govern the style of the
222 Bahá'í calendar entries, except that the Bahá'í month names must be given
223 numerically. The Bahá'í months are numbered from 1 to 19 with Bahá being
224 1 and 19 being `Alá. If a Bahá'í date diary entry begins with
225 `diary-nonmarking-symbol', the entry will appear in the diary listing, but
226 will not be marked in the calendar. This function is provided for use with
227 `nongregorian-diary-listing-hook'."
228 (diary-list-entries-1 calendar-bahai-month-name-array
229 bahai-diary-entry-symbol
230 'calendar-bahai-from-absolute))
231 (define-obsolete-function-alias
232 'list-bahai-diary-entries 'diary-bahai-list-entries "23.1")
235 (autoload 'calendar-mark-1 "diary-lib")
237 ;;;###diary-autoload
238 (defun calendar-bahai-mark-date-pattern (month day year &optional color)
239 "Mark dates in calendar window that conform to Bahá'í date MONTH/DAY/YEAR.
240 A value of 0 in any position is a wildcard. Optional argument COLOR is
241 passed to `mark-visible-calendar-date' as MARK."
242 (calendar-mark-1 month day year 'calendar-bahai-from-absolute
243 'calendar-absolute-from-bahai color))
245 (define-obsolete-function-alias
246 'mark-bahai-calendar-date-pattern 'calendar-bahai-mark-date-pattern "23.1")
249 (autoload 'diary-mark-entries-1 "diary-lib")
251 ;;;###diary-autoload
252 (defun diary-bahai-mark-entries ()
253 "Mark days in the calendar window that have Bahá'í date diary entries.
254 Marks each entry in `diary-file' (or included files) visible in the calendar
255 window. See `diary-bahai-list-entries' for more information."
256 (diary-mark-entries-1 calendar-bahai-month-name-array
257 bahai-diary-entry-symbol
258 'calendar-bahai-from-absolute
259 'calendar-bahai-mark-date-pattern))
261 (define-obsolete-function-alias
262 'mark-bahai-diary-entries 'diary-bahai-mark-entries "23.1")
264 ;;;###cal-autoload
265 (defun diary-bahai-insert-entry (arg)
266 "Insert a diary entry.
267 For the Bahá'í date corresponding to the date indicated by point.
268 Prefix argument ARG makes the entry nonmarking."
269 (interactive "P")
270 (let ((calendar-month-name-array calendar-bahai-month-name-array))
271 (make-diary-entry
272 (concat bahai-diary-entry-symbol
273 (calendar-date-string
274 (calendar-bahai-from-absolute
275 (calendar-absolute-from-gregorian (calendar-cursor-to-date t)))
276 nil t))
277 arg)))
279 (define-obsolete-function-alias
280 'insert-bahai-diary-entry 'diary-bahai-insert-entry "23.1")
282 ;;;###cal-autoload
283 (defun diary-bahai-insert-monthly-entry (arg)
284 "Insert a monthly diary entry.
285 For the day of the Bahá'í month corresponding to the date indicated by point.
286 Prefix argument ARG makes the entry nonmarking."
287 (interactive "P")
288 (let ((calendar-date-display-form (if european-calendar-style
289 '(day " * ")
290 '("* " day )))
291 (calendar-month-name-array calendar-bahai-month-name-array))
292 (make-diary-entry
293 (concat bahai-diary-entry-symbol
294 (calendar-date-string
295 (calendar-bahai-from-absolute
296 (calendar-absolute-from-gregorian (calendar-cursor-to-date t)))))
297 arg)))
299 (define-obsolete-function-alias
300 'insert-monthly-bahai-diary-entry 'diary-bahai-insert-monthly-entry "23.1")
302 ;;;###cal-autoload
303 (defun diary-bahai-insert-yearly-entry (arg)
304 "Insert an annual diary entry.
305 For the day of the Bahá'í year corresponding to the date indicated by point.
306 Prefix argument ARG will make the entry nonmarking."
307 (interactive "P")
308 (let ((calendar-date-display-form (if european-calendar-style
309 '(day " " monthname)
310 '(monthname " " day)))
311 (calendar-month-name-array calendar-bahai-month-name-array))
312 (make-diary-entry
313 (concat bahai-diary-entry-symbol
314 (calendar-date-string
315 (calendar-bahai-from-absolute
316 (calendar-absolute-from-gregorian (calendar-cursor-to-date t)))))
317 arg)))
319 (define-obsolete-function-alias
320 'insert-yearly-bahai-diary-entry 'diary-bahai-insert-yearly-entry "23.1")
322 (defvar date)
324 ;; To be called from list-sexp-diary-entries, where DATE is bound.
325 ;;;###diary-autoload
326 (defun diary-bahai-date ()
327 "Bahá'í calendar equivalent of date diary entry."
328 (format "Bahá'í date: %s" (calendar-bahai-date-string date)))
331 (provide 'cal-bahai)
333 ;; Local Variables:
334 ;; coding: utf-8
335 ;; End:
337 ;; arch-tag: c1cb1d67-862a-4264-a01c-41cb4df01f14
338 ;;; cal-bahai.el ends here