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>
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)
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.
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
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.
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 YEAR is a leap year on the Bahá'í calendar."
71 (calendar-leap-year-p (+ year
1844)))
73 (defconst calendar-bahai-leap-base
74 (+ (/ 1844 4) (- (/ 1844 100)) (/ 1844 400))
75 "Used by `calendar-absolute-from-bahai'.")
77 (defun calendar-absolute-from-bahai (date)
78 "Compute absolute date from Bahá'í date DATE.
79 The absolute date is the number of days elapsed since the (imaginary)
80 Gregorian date Sunday, December 31, 1 BC."
81 (let* ((month (extract-calendar-month date
))
82 (day (extract-calendar-day date
))
83 (year (extract-calendar-year date
))
84 (prior-years (+ (1- year
) 1844))
85 (leap-days (- (+ (/ prior-years
4) ; leap days in prior years
86 (- (/ prior-years
100))
88 calendar-bahai-leap-base
)))
89 (+ (1- calendar-bahai-epoch
) ; days before epoch
90 (* 365 (1- year
)) ; days in prior years
92 (calendar-sum m
1 (< m month
) 19)
94 day
))) ; days so far this month
96 (defun calendar-bahai-from-absolute (date)
97 "Bahá'í year corresponding to the absolute DATE."
98 (if (< date calendar-bahai-epoch
)
99 (list 0 0 0) ; pre-Bahá'í date
100 (let* ((greg (calendar-gregorian-from-absolute date
))
101 (year (+ (- (extract-calendar-year greg
) 1844)
102 (if (or (> (extract-calendar-month greg
) 3)
103 (and (= (extract-calendar-month greg
) 3)
104 (>= (extract-calendar-day greg
) 21)))
106 (month ; search forward from Baha
107 (1+ (calendar-sum m
1
109 (calendar-absolute-from-bahai
112 (day ; calculate the day by subtraction
114 (1- (calendar-absolute-from-bahai (list month
1 year
))))))
115 (list month day year
))))
118 (defun calendar-bahai-date-string (&optional date
)
119 "String of Bahá'í date of Gregorian DATE.
120 Defaults to today's date if DATE is not given."
121 (let* ((bahai-date (calendar-bahai-from-absolute
122 (calendar-absolute-from-gregorian
123 (or date
(calendar-current-date)))))
124 (y (extract-calendar-year bahai-date
))
125 (m (extract-calendar-month bahai-date
))
126 (d (extract-calendar-day bahai-date
)))
131 (aref calendar-bahai-month-name-array
(1- m
))))
134 (if (calendar-bahai-leap-year-p y
)
139 (month (int-to-string m
))
140 (year (int-to-string y
)))
141 (mapconcat 'eval calendar-date-display-form
""))))
144 (defun calendar-bahai-print-date ()
145 "Show the Bahá'í calendar equivalent of the selected date."
147 (message "Bahá'í date: %s"
148 (calendar-bahai-date-string (calendar-cursor-to-date t
))))
150 (defun calendar-bahai-prompt-for-date ()
151 "Ask for a Bahá'í date."
152 (let* ((today (calendar-current-date))
154 "Bahá'í calendar year (not 0): "
155 (lambda (x) (not (zerop x
)))
157 (extract-calendar-year
158 (calendar-bahai-from-absolute
159 (calendar-absolute-from-gregorian today
))))))
160 (completion-ignore-case t
)
163 "Bahá'í calendar month name: "
165 (append calendar-bahai-month-name-array nil
))
167 (calendar-make-alist calendar-bahai-month-name-array
169 (day (calendar-read "Bahá'í calendar day (1-19): "
170 (lambda (x) (and (< 0 x
) (<= x
19))))))
171 (list (list month day year
))))
174 (defun calendar-bahai-goto-date (date &optional noecho
)
175 "Move cursor to Bahá'í date DATE.
176 Echo Bahá'í date unless NOECHO is non-nil."
177 (interactive (calendar-bahai-prompt-for-date))
178 (calendar-goto-date (calendar-gregorian-from-absolute
179 (calendar-absolute-from-bahai date
)))
180 (or noecho
(calendar-bahai-print-date)))
182 (defvar displayed-month
)
183 (defvar displayed-year
)
185 ;;;###holiday-autoload
186 (defun holiday-bahai (month day string
)
187 "Holiday on MONTH, DAY (Bahá'í) called STRING.
188 If MONTH, DAY (Bahá'í) is visible, the value returned is corresponding
189 Gregorian date in the form of the list (((month day year) STRING)). Returns
190 nil if it is not visible in the current calendar window."
191 (let* ((bahai-date (calendar-bahai-from-absolute
192 (calendar-absolute-from-gregorian
193 (list displayed-month
15 displayed-year
))))
194 (m (extract-calendar-month bahai-date
))
195 (y (extract-calendar-year bahai-date
))
197 (unless (< m
1) ; Bahá'í calendar doesn't apply
198 (increment-calendar-month m y
(- 10 month
))
199 (if (> m
7) ; Bahá'í date might be visible
200 (let ((date (calendar-gregorian-from-absolute
201 (calendar-absolute-from-bahai (list month day y
)))))
202 (if (calendar-date-is-visible-p date
)
203 (list (list date string
))))))))
205 (autoload 'diary-list-entries-1
"diary-lib")
207 ;; FIXME diary-bahai-mark-entries said the names could be spelled in full.
209 (defun diary-bahai-list-entries ()
210 "Add any Bahá'í date entries from the diary file to `diary-entries-list'.
211 Bahá'í date diary entries must be prefaced by `bahai-diary-entry-symbol'
212 \(normally a `B'). The same diary date forms govern the style of the
213 Bahá'í calendar entries, except that the Bahá'í month names must be given
214 numerically. The Bahá'í months are numbered from 1 to 19 with Bahá being
215 1 and 19 being `Alá. If a Bahá'í date diary entry begins with
216 `diary-nonmarking-symbol', the entry will appear in the diary listing, but
217 will not be marked in the calendar. This function is provided for use with
218 `nongregorian-diary-listing-hook'."
219 (diary-list-entries-1 calendar-bahai-month-name-array
220 bahai-diary-entry-symbol
221 'calendar-bahai-from-absolute
))
224 (defun calendar-bahai-mark-date-pattern (month day year
)
225 "Mark dates in calendar window that conform to Bahá'í date MONTH/DAY/YEAR.
226 A value of 0 in any position is a wildcard."
228 (set-buffer calendar-buffer
)
229 (if (and (not (zerop month
)) (not (zerop day
)))
230 (if (not (zerop year
))
231 ;; Fully specified Bahá'í date.
232 (let ((date (calendar-gregorian-from-absolute
233 (calendar-absolute-from-bahai
234 (list month day year
)))))
235 (if (calendar-date-is-visible-p date
)
236 (mark-visible-calendar-date date
)))
237 ;; Month and day in any year--this taken from the holiday stuff.
238 (let* ((bahai-date (calendar-bahai-from-absolute
239 (calendar-absolute-from-gregorian
240 (list displayed-month
15 displayed-year
))))
241 (m (extract-calendar-month bahai-date
))
242 (y (extract-calendar-year bahai-date
))
244 (unless (< m
1) ; Bahá'í calendar doesn't apply
245 (increment-calendar-month m y
(- 10 month
))
246 (if (> m
7) ; Bahá'í date might be visible
247 (let ((date (calendar-gregorian-from-absolute
248 (calendar-absolute-from-bahai
249 (list month day y
)))))
250 (if (calendar-date-is-visible-p date
)
251 (mark-visible-calendar-date date
)))))))
252 ;; Not one of the simple cases--check all visible dates for match.
253 ;; Actually, the following code takes care of ALL of the cases, but
254 ;; it's much too slow to be used for the simple (common) cases.
255 (let ((m displayed-month
)
259 (increment-calendar-month m y -
1)
261 (calendar-absolute-from-gregorian
263 (increment-calendar-month m y
2)
265 (calendar-absolute-from-gregorian
266 (list m
(calendar-last-day-of-month m y
) y
)))
267 (calendar-for-loop date from first-date to last-date do
268 (let* ((b-date (calendar-bahai-from-absolute date
))
269 (i-month (extract-calendar-month b-date
))
270 (i-day (extract-calendar-day b-date
))
271 (i-year (extract-calendar-year b-date
)))
272 (and (or (zerop month
)
278 (mark-visible-calendar-date
279 (calendar-gregorian-from-absolute
282 (autoload 'diary-mark-entries-1
"diary-lib")
285 (defun diary-bahai-mark-entries ()
286 "Mark days in the calendar window that have Bahá'í date diary entries.
287 Marks each entry in `diary-file' (or included files) visible in the calendar
288 window. See `diary-bahai-list-entries' for more information."
289 (diary-mark-entries-1 calendar-bahai-month-name-array
290 bahai-diary-entry-symbol
291 'calendar-bahai-from-absolute
292 'calendar-bahai-mark-date-pattern
))
295 (defun diary-bahai-insert-entry (arg)
296 "Insert a diary entry.
297 For the Bahá'í date corresponding to the date indicated by point.
298 Prefix argument ARG makes the entry nonmarking."
300 (let* ((calendar-month-name-array calendar-bahai-month-name-array
))
302 (concat bahai-diary-entry-symbol
303 (calendar-date-string
304 (calendar-bahai-from-absolute
305 (calendar-absolute-from-gregorian (calendar-cursor-to-date t
)))
310 (defun diary-bahai-insert-monthly-entry (arg)
311 "Insert a monthly diary entry.
312 For the day of the Bahá'í month corresponding to the date indicated by point.
313 Prefix argument ARG makes the entry nonmarking."
315 (let* ((calendar-date-display-form (if european-calendar-style
318 (calendar-month-name-array calendar-bahai-month-name-array
))
320 (concat bahai-diary-entry-symbol
321 (calendar-date-string
322 (calendar-bahai-from-absolute
323 (calendar-absolute-from-gregorian (calendar-cursor-to-date t
)))))
327 (defun diary-bahai-insert-yearly-entry (arg)
328 "Insert an annual diary entry.
329 For the day of the Bahá'í year corresponding to the date indicated by point.
330 Prefix argument ARG will make the entry nonmarking."
332 (let* ((calendar-date-display-form (if european-calendar-style
334 '(monthname " " day
)))
335 (calendar-month-name-array calendar-bahai-month-name-array
))
337 (concat bahai-diary-entry-symbol
338 (calendar-date-string
339 (calendar-bahai-from-absolute
340 (calendar-absolute-from-gregorian (calendar-cursor-to-date t
)))))
345 ;; To be called from list-sexp-diary-entries, where DATE is bound.
347 (defun diary-bahai-date ()
348 "Bahá'í calendar equivalent of date diary entry."
349 (format "Bahá'í date: %s" (calendar-bahai-date-string date
)))
352 ;; Backward compatibility.
353 (define-obsolete-function-alias
354 'list-bahai-diary-entries
'diary-bahai-list-entries
"23.1")
355 (define-obsolete-function-alias
356 'mark-bahai-diary-entries
'diary-bahai-mark-entries
"23.1")
357 (define-obsolete-function-alias
358 'insert-bahai-diary-entry
'diary-bahai-insert-entry
"23.1")
359 (define-obsolete-function-alias
360 'insert-monthly-bahai-diary-entry
'diary-bahai-insert-monthly-entry
"23.1")
361 (define-obsolete-function-alias
362 'insert-yearly-bahai-diary-entry
'diary-bahai-insert-yearly-entry
"23.1")
363 (define-obsolete-function-alias
364 'mark-bahai-calendar-date-pattern
'calendar-bahai-mark-date-pattern
"23.1")
365 (define-obsolete-function-alias
366 'calendar-goto-bahai-date
'calendar-bahai-goto-date
"23.1")
367 (define-obsolete-function-alias
368 'calendar-print-bahai-date
'calendar-bahai-print-date
"23.1")
376 ;; arch-tag: c1cb1d67-862a-4264-a01c-41cb4df01f14
377 ;;; cal-bahai.el ends here