1 ;;; cal-bahai.el --- calendar functions for the Bahá'í calendar.
3 ;; Copyright (C) 2001-2015 Free Software Foundation, Inc.
5 ;; Author: John Wiegley <johnw@gnu.org>
7 ;; 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 of the License, or
15 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
27 ;; This collection of functions implements the features of calendar.el
28 ;; and diary-lib.el that deal with the Bahá'í calendar.
30 ;; The Bahá'í (http://www.bahai.org) calendar system is based on a
31 ;; solar cycle of 19 months with 19 days each. The four remaining
32 ;; "intercalary" days are called the Ayyám-i-Há (days of Há), and are
33 ;; placed between the 18th and 19th months. They are meant as a time
34 ;; of festivals preceding the 19th month, which is the month of
35 ;; fasting. In Gregorian leap years, there are 5 of these days (Há
36 ;; has the numerical value of 5 in the arabic abjad, or
37 ;; letter-to-number, reckoning).
39 ;; Each month is named after an attribute of God, as are the 19 days
40 ;; -- which have the same names as the months. There is also a name
41 ;; for each year in every 19 year cycle. These cycles are called
42 ;; Váhids. A cycle of 19 Váhids (361 years) is called a Kullu-Shay,
43 ;; which means "all things".
45 ;; The calendar was named the "Badí` calendar" by its author, the Báb.
46 ;; It uses a week of seven days, corresponding to the Gregorian week,
47 ;; each of which has its own name, again patterned after the
50 ;; Note: The days of Ayyám-i-Há are encoded as zero and negative
51 ;; offsets from the first day of the final month. So, (19 -3 157) is
52 ;; the first day of Ayyám-i-Há, in the year 157 BE.
58 (defconst calendar-bahai-month-name-array
59 ["Bahá" "Jalál" "Jamál" "`Azamat" "Núr" "Rahmat" "Kalimát" "Kamál"
60 "Asmá" "`Izzat" "Mashiyyat" "`Ilm" "Qudrat" "Qawl" "Masá'il"
61 "Sharaf" "Sultán" "Mulk" "`Alá"]
62 "Array of the month names in the Bahá'í calendar.")
64 (defconst calendar-bahai-epoch
(calendar-absolute-from-gregorian '(3 21 1844))
65 "Absolute date of start of Bahá'í calendar = March 21, 1844 AD.")
67 (defun calendar-bahai-leap-year-p (year)
68 "True if Bahá'í YEAR is a leap year in the Bahá'í calendar."
69 (calendar-leap-year-p (+ year
1844)))
71 (defconst calendar-bahai-leap-base
72 (+ (/ 1844 4) (- (/ 1844 100)) (/ 1844 400))
73 "Number of leap years between 1 and 1844 AD, inclusive.
74 Used by `calendar-bahai-to-absolute'.")
76 (defun calendar-bahai-to-absolute (date)
77 "Compute absolute date from Bahá'í date DATE.
78 The absolute date is the number of days elapsed since the (imaginary)
79 Gregorian date Sunday, December 31, 1 BC."
80 (let* ((month (calendar-extract-month date
))
81 (day (calendar-extract-day date
))
82 (year (calendar-extract-year date
))
83 (prior-years (+ (1- year
) 1844))
84 (leap-days (- (+ (/ prior-years
4) ; leap days in prior years
85 (- (/ prior-years
100))
87 calendar-bahai-leap-base
)))
88 (+ (1- calendar-bahai-epoch
) ; days before epoch
89 (* 365 (1- year
)) ; days in prior years
91 (calendar-sum m
1 (< m month
) 19)
93 (if (calendar-bahai-leap-year-p year
) 5 4)
95 day
))) ; days so far this month
97 (defun calendar-bahai-from-absolute (date)
98 "Bahá'í date (month day year) corresponding to the absolute DATE."
99 (if (< date calendar-bahai-epoch
)
100 (list 0 0 0) ; pre-Bahá'í date
101 (let* ((greg (calendar-gregorian-from-absolute date
))
102 (gmonth (calendar-extract-month greg
))
103 (year (+ (- (calendar-extract-year greg
) 1844)
106 (>= (calendar-extract-day greg
) 21)))
108 (month ; search forward from Baha
109 (1+ (calendar-sum m
1
110 (> date
(calendar-bahai-to-absolute (list m
19 year
)))
112 (day ; calculate the day by subtraction
114 (1- (calendar-bahai-to-absolute (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 (calendar-extract-year bahai-date
)))
127 (let* ((m (calendar-extract-month bahai-date
))
128 (d (calendar-extract-day bahai-date
))
129 (monthname (if (and (= m
19)
132 (aref calendar-bahai-month-name-array
(1- m
))))
133 (day (number-to-string
135 (+ d
(if (calendar-bahai-leap-year-p y
) 5 4))
137 (year (number-to-string y
))
138 (month (number-to-string m
))
140 ;; Can't call calendar-date-string because of monthname oddity.
141 (mapconcat 'eval calendar-date-display-form
"")))))
144 (defun calendar-bahai-print-date ()
145 "Show the Bahá'í calendar equivalent of the selected date."
147 (let ((s (calendar-bahai-date-string (calendar-cursor-to-date t
))))
148 (if (string-equal s
"")
149 (message "Date is pre-Bahá'í")
150 (message "Bahá'í date: %s" s
))))
152 (defun calendar-bahai-read-date ()
153 "Interactively read the arguments for a Bahá'í date command.
154 Reads a year, month and day."
155 (let* ((today (calendar-current-date))
157 "Bahá'í calendar year (not 0): "
158 (lambda (x) (not (zerop x
)))
160 (calendar-extract-year
161 (calendar-bahai-from-absolute
162 (calendar-absolute-from-gregorian today
))))))
163 (completion-ignore-case t
)
166 "Bahá'í calendar month name: "
168 (append calendar-bahai-month-name-array nil
))
170 (calendar-make-alist calendar-bahai-month-name-array
172 (day (calendar-read "Bahá'í calendar day (1-19): "
173 (lambda (x) (and (< 0 x
) (<= x
19))))))
174 (list (list month day year
))))
177 (defun calendar-bahai-goto-date (date &optional noecho
)
178 "Move cursor to Bahá'í date DATE; echo Bahá'í date unless NOECHO is non-nil."
179 (interactive (calendar-bahai-read-date))
180 (calendar-goto-date (calendar-gregorian-from-absolute
181 (calendar-bahai-to-absolute date
)))
182 (or noecho
(calendar-bahai-print-date)))
184 (defvar displayed-month
)
185 (defvar displayed-year
)
187 ;;;###holiday-autoload
188 (defun holiday-bahai (month day string
)
189 "Holiday on MONTH, DAY (Bahá'í) called STRING.
190 If MONTH, DAY (Bahá'í) is visible in the current calendar window,
191 returns the corresponding Gregorian date in the form of the
192 list (((month day year) STRING)). Otherwise, returns nil."
193 ;; Since the calendar window shows 3 months at a time, there are
194 ;; approx +/- 45 days either side of the central month.
195 ;; Since the Bahai months have 19 days, this means up to +/- 3 months.
196 (let* ((bahai-date (calendar-bahai-from-absolute
197 (calendar-absolute-from-gregorian
198 (list displayed-month
15 displayed-year
))))
199 (m (calendar-extract-month bahai-date
))
200 (y (calendar-extract-year bahai-date
))
202 (unless (< m
1) ; Bahá'í calendar doesn't apply
203 ;; Cf holiday-fixed, holiday-islamic.
204 ;; With a +- 3 month calendar window, and 19 months per year,
205 ;; month 16 is special. When m16 is central is when the
206 ;; end-of-year first appears. When m1 is central, m16 is no
207 ;; longer visible. Hence we can do a one-sided test to see if
208 ;; m16 is visible. m16 is visible when the central month >= 13.
209 ;; To see if other months are visible we can shift the range
211 (calendar-increment-month m y
(- 16 month
) 19)
212 (and (> m
12) ; Bahá'í date might be visible
213 (calendar-date-is-visible-p
214 (setq date
(calendar-gregorian-from-absolute
215 (calendar-bahai-to-absolute (list month day y
)))))
216 (list (list date string
))))))
218 (autoload 'holiday-fixed
"holidays")
220 ;;;###holiday-autoload
221 (defun holiday-bahai-new-year ()
222 "Holiday entry for the Bahá'í New Year, if visible in the calendar window."
224 (format "Bahá'í New Year (Naw-Ruz) %d"
225 (- displayed-year
(1- 1844)))))
227 ;;;###holiday-autoload
228 (defun holiday-bahai-ridvan (&optional all
)
229 "Holidays related to Ridvan, as visible in the calendar window.
230 Only considers the first, ninth, and twelfth days, unless ALL or
231 `calendar-bahai-all-holidays-flag' is non-nil."
232 (let ((ord ["First" "Second" "Third" "Fourth" "Fifth" "Sixth"
233 "Seventh" "Eighth" "Ninth" "Tenth" "Eleventh" "Twelfth"])
236 (if (or all calendar-bahai-all-holidays-flag
)
237 (setq show
(number-sequence 0 11)))
238 ;; More trouble than it was worth...?
239 (dolist (i show
(nreverse rid
))
240 (if (setq h
(holiday-fixed (if (< i
10) 4 5)
241 (+ i
(if (< i
10) 21 -
9))
242 (format "%s Day of Ridvan" (aref ord i
))))
243 (push (car h
) rid
)))))
245 (autoload 'diary-list-entries-1
"diary-lib")
248 (defun diary-bahai-list-entries ()
249 "Add any Bahá'í date entries from the diary file to `diary-entries-list'.
250 Bahá'í date diary entries must be prefaced by `diary-bahai-entry-symbol'
251 \(normally a `B'). The same diary date forms govern the style of the
252 Bahá'í calendar entries, except that the Bahá'í month names cannot be
253 abbreviated. The Bahá'í months are numbered from 1 to 19 with Bahá being
254 1 and 19 being `Alá. If a Bahá'í date diary entry begins with
255 `diary-nonmarking-symbol', the entry will appear in the diary listing, but
256 will not be marked in the calendar. This function is provided for use with
257 `diary-nongregorian-listing-hook'."
258 (diary-list-entries-1 calendar-bahai-month-name-array
259 diary-bahai-entry-symbol
260 'calendar-bahai-from-absolute
))
262 (autoload 'calendar-mark-1
"diary-lib")
265 (defun calendar-bahai-mark-date-pattern (month day year
&optional color
)
266 "Mark dates in calendar window that conform to Bahá'í date MONTH/DAY/YEAR.
267 A value of 0 in any position is a wildcard. Optional argument COLOR is
268 passed to `calendar-mark-visible-date' as MARK."
269 (calendar-mark-1 month day year
'calendar-bahai-from-absolute
270 'calendar-bahai-to-absolute color
))
272 (autoload 'diary-mark-entries-1
"diary-lib")
275 (defun diary-bahai-mark-entries ()
276 "Mark days in the calendar window that have Bahá'í date diary entries.
277 Marks each entry in `diary-file' (or included files) visible in the calendar
278 window. See `diary-bahai-list-entries' for more information."
279 (diary-mark-entries-1 'calendar-bahai-mark-date-pattern
280 calendar-bahai-month-name-array
281 diary-bahai-entry-symbol
282 'calendar-bahai-from-absolute
))
284 (autoload 'diary-insert-entry-1
"diary-lib")
287 (defun diary-bahai-insert-entry (arg)
288 "Insert a diary entry.
289 For the Bahá'í date corresponding to the date indicated by point.
290 Prefix argument ARG makes the entry nonmarking."
292 (diary-insert-entry-1 nil arg calendar-bahai-month-name-array
293 diary-bahai-entry-symbol
294 'calendar-bahai-from-absolute
))
297 (defun diary-bahai-insert-monthly-entry (arg)
298 "Insert a monthly diary entry.
299 For the day of the Bahá'í month corresponding to the date indicated by point.
300 Prefix argument ARG makes the entry nonmarking."
302 (diary-insert-entry-1 'monthly arg calendar-bahai-month-name-array
303 diary-bahai-entry-symbol
304 'calendar-bahai-from-absolute
))
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."
312 (diary-insert-entry-1 'yearly arg calendar-bahai-month-name-array
313 diary-bahai-entry-symbol
314 'calendar-bahai-from-absolute
))
318 ;; To be called from diary-list-sexp-entries, where DATE is bound.
320 (defun diary-bahai-date ()
321 "Bahá'í calendar equivalent of date diary entry."
322 (format "Bahá'í date: %s" (calendar-bahai-date-string date
)))
331 ;;; cal-bahai.el ends here