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
))
127 (monthname (if (and (= m
19)
130 (aref calendar-bahai-month-name-array
(1- m
))))
133 (if (calendar-bahai-leap-year-p y
)
137 (year (int-to-string y
))
138 (month (int-to-string m
))
140 (mapconcat 'eval calendar-date-display-form
"")))
143 (defun calendar-bahai-print-date ()
144 "Show the Bahá'í calendar equivalent of the selected date."
146 (message "Bahá'í date: %s"
147 (calendar-bahai-date-string (calendar-cursor-to-date t
))))
149 (define-obsolete-function-alias
150 'calendar-print-bahai-date
'calendar-bahai-print-date
"23.1")
152 (defun calendar-bahai-prompt-for-date ()
153 "Ask for a Bahá'í date."
154 (let* ((today (calendar-current-date))
156 "Bahá'í calendar year (not 0): "
157 (lambda (x) (not (zerop x
)))
159 (extract-calendar-year
160 (calendar-bahai-from-absolute
161 (calendar-absolute-from-gregorian today
))))))
162 (completion-ignore-case t
)
165 "Bahá'í calendar month name: "
167 (append calendar-bahai-month-name-array nil
))
169 (calendar-make-alist calendar-bahai-month-name-array
171 (day (calendar-read "Bahá'í calendar day (1-19): "
172 (lambda (x) (and (< 0 x
) (<= x
19))))))
173 (list (list month day year
))))
176 (defun calendar-bahai-goto-date (date &optional noecho
)
177 "Move cursor to Bahá'í date DATE; echo Bahá'í date unless NOECHO is non-nil."
178 (interactive (calendar-bahai-prompt-for-date))
179 (calendar-goto-date (calendar-gregorian-from-absolute
180 (calendar-absolute-from-bahai date
)))
181 (or noecho
(calendar-bahai-print-date)))
183 (define-obsolete-function-alias
184 'calendar-goto-bahai-date
'calendar-bahai-goto-date
"23.1")
186 (defvar displayed-month
)
187 (defvar displayed-year
)
189 ;;;###holiday-autoload
190 (defun holiday-bahai (month day string
)
191 "Holiday on MONTH, DAY (Bahá'í) called STRING.
192 If MONTH, DAY (Bahá'í) is visible, the value returned is corresponding
193 Gregorian date in the form of the list (((month day year) STRING)). Returns
194 nil if it is not visible in the current calendar window."
195 (let* ((bahai-date (calendar-bahai-from-absolute
196 (calendar-absolute-from-gregorian
197 (list displayed-month
15 displayed-year
))))
198 (m (extract-calendar-month bahai-date
))
199 (y (extract-calendar-year bahai-date
))
201 (unless (< m
1) ; Bahá'í calendar doesn't apply
202 (increment-calendar-month m y
(- 10 month
))
203 (if (> m
7) ; Bahá'í date might be visible
204 (let ((date (calendar-gregorian-from-absolute
205 (calendar-absolute-from-bahai (list month day y
)))))
206 (if (calendar-date-is-visible-p date
)
207 (list (list date string
))))))))
209 (autoload 'diary-list-entries-1
"diary-lib")
211 ;; FIXME diary-bahai-mark-entries said the names could be spelled in full.
213 (defun diary-bahai-list-entries ()
214 "Add any Bahá'í date entries from the diary file to `diary-entries-list'.
215 Bahá'í date diary entries must be prefaced by `bahai-diary-entry-symbol'
216 \(normally a `B'). The same diary date forms govern the style of the
217 Bahá'í calendar entries, except that the Bahá'í month names must be given
218 numerically. The Bahá'í months are numbered from 1 to 19 with Bahá being
219 1 and 19 being `Alá. If a Bahá'í date diary entry begins with
220 `diary-nonmarking-symbol', the entry will appear in the diary listing, but
221 will not be marked in the calendar. This function is provided for use with
222 `nongregorian-diary-listing-hook'."
223 (diary-list-entries-1 calendar-bahai-month-name-array
224 bahai-diary-entry-symbol
225 'calendar-bahai-from-absolute
))
226 (define-obsolete-function-alias
227 'list-bahai-diary-entries
'diary-bahai-list-entries
"23.1")
230 (autoload 'calendar-mark-1
"diary-lib")
233 (defun calendar-bahai-mark-date-pattern (month day year
&optional color
)
234 "Mark dates in calendar window that conform to Bahá'í date MONTH/DAY/YEAR.
235 A value of 0 in any position is a wildcard. Optional argument COLOR is
236 passed to `mark-visible-calendar-date' as MARK."
237 (calendar-mark-1 month day year
'calendar-bahai-from-absolute
238 'calendar-absolute-from-bahai color
))
240 (define-obsolete-function-alias
241 'mark-bahai-calendar-date-pattern
'calendar-bahai-mark-date-pattern
"23.1")
244 (autoload 'diary-mark-entries-1
"diary-lib")
247 (defun diary-bahai-mark-entries ()
248 "Mark days in the calendar window that have Bahá'í date diary entries.
249 Marks each entry in `diary-file' (or included files) visible in the calendar
250 window. See `diary-bahai-list-entries' for more information."
251 (diary-mark-entries-1 calendar-bahai-month-name-array
252 bahai-diary-entry-symbol
253 'calendar-bahai-from-absolute
254 'calendar-bahai-mark-date-pattern
))
256 (define-obsolete-function-alias
257 'mark-bahai-diary-entries
'diary-bahai-mark-entries
"23.1")
260 (defun diary-bahai-insert-entry (arg)
261 "Insert a diary entry.
262 For the Bahá'í date corresponding to the date indicated by point.
263 Prefix argument ARG makes the entry nonmarking."
265 (let ((calendar-month-name-array calendar-bahai-month-name-array
))
267 (concat bahai-diary-entry-symbol
268 (calendar-date-string
269 (calendar-bahai-from-absolute
270 (calendar-absolute-from-gregorian (calendar-cursor-to-date t
)))
274 (define-obsolete-function-alias
275 'insert-bahai-diary-entry
'diary-bahai-insert-entry
"23.1")
278 (defun diary-bahai-insert-monthly-entry (arg)
279 "Insert a monthly diary entry.
280 For the day of the Bahá'í month corresponding to the date indicated by point.
281 Prefix argument ARG makes the entry nonmarking."
283 (let ((calendar-date-display-form (if european-calendar-style
286 (calendar-month-name-array calendar-bahai-month-name-array
))
288 (concat bahai-diary-entry-symbol
289 (calendar-date-string
290 (calendar-bahai-from-absolute
291 (calendar-absolute-from-gregorian (calendar-cursor-to-date t
)))))
294 (define-obsolete-function-alias
295 'insert-monthly-bahai-diary-entry
'diary-bahai-insert-monthly-entry
"23.1")
298 (defun diary-bahai-insert-yearly-entry (arg)
299 "Insert an annual diary entry.
300 For the day of the Bahá'í year corresponding to the date indicated by point.
301 Prefix argument ARG will make the entry nonmarking."
303 (let ((calendar-date-display-form (if european-calendar-style
305 '(monthname " " day
)))
306 (calendar-month-name-array calendar-bahai-month-name-array
))
308 (concat bahai-diary-entry-symbol
309 (calendar-date-string
310 (calendar-bahai-from-absolute
311 (calendar-absolute-from-gregorian (calendar-cursor-to-date t
)))))
314 (define-obsolete-function-alias
315 'insert-yearly-bahai-diary-entry
'diary-bahai-insert-yearly-entry
"23.1")
319 ;; To be called from list-sexp-diary-entries, where DATE is bound.
321 (defun diary-bahai-date ()
322 "Bahá'í calendar equivalent of date diary entry."
323 (format "Bahá'í date: %s" (calendar-bahai-date-string date
)))
332 ;; arch-tag: c1cb1d67-862a-4264-a01c-41cb4df01f14
333 ;;; cal-bahai.el ends here