Update for diary-lib name changes.
[emacs.git] / lisp / calendar / cal-bahai.el
blobf49c29cb29c581813a7b302fce643f65658686fe
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-bahai-to-absolute'.")
78 (defun calendar-bahai-to-absolute (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 (define-obsolete-function-alias 'calendar-absolute-from-bahai
100 'calendar-bahai-to-absolute "23.1")
102 (defun calendar-bahai-from-absolute (date)
103 "Bahá'í date (month day year) corresponding to the absolute DATE."
104 (if (< date calendar-bahai-epoch)
105 (list 0 0 0) ; pre-Bahá'í date
106 (let* ((greg (calendar-gregorian-from-absolute date))
107 (gmonth (extract-calendar-month greg))
108 (year (+ (- (extract-calendar-year greg) 1844)
109 (if (or (> gmonth 3)
110 (and (= gmonth 3)
111 (>= (extract-calendar-day greg) 21)))
112 1 0)))
113 (month ; search forward from Baha
114 (1+ (calendar-sum m 1
115 (> date (calendar-bahai-to-absolute (list m 19 year)))
116 1)))
117 (day ; calculate the day by subtraction
118 (- date
119 (1- (calendar-bahai-to-absolute (list month 1 year))))))
120 (list month day year))))
122 ;;;###cal-autoload
123 (defun calendar-bahai-date-string (&optional date)
124 "String of Bahá'í date of Gregorian DATE.
125 Defaults to today's date if DATE is not given."
126 (let* ((bahai-date (calendar-bahai-from-absolute
127 (calendar-absolute-from-gregorian
128 (or date (calendar-current-date)))))
129 (y (extract-calendar-year bahai-date)))
130 (if (< y 1)
131 "" ; pre-Bahai
132 (let* ((m (extract-calendar-month bahai-date))
133 (d (extract-calendar-day bahai-date))
134 (monthname (if (and (= m 19)
135 (<= d 0))
136 "Ayyám-i-Há"
137 (aref calendar-bahai-month-name-array (1- m))))
138 (day (int-to-string
139 (if (<= d 0)
140 (+ d (if (calendar-bahai-leap-year-p y) 5 4))
141 d)))
142 (year (int-to-string y))
143 (month (int-to-string m))
144 dayname)
145 ;; Can't call calendar-date-string because of monthname oddity.
146 (mapconcat 'eval calendar-date-display-form "")))))
148 ;;;###cal-autoload
149 (defun calendar-bahai-print-date ()
150 "Show the Bahá'í calendar equivalent of the selected date."
151 (interactive)
152 (let ((s (calendar-bahai-date-string (calendar-cursor-to-date t))))
153 (if (string-equal s "")
154 (message "Date is pre-Bahá'í")
155 (message "Bahá'í date: %s" s))))
157 (define-obsolete-function-alias
158 'calendar-print-bahai-date 'calendar-bahai-print-date "23.1")
160 (defun calendar-bahai-read-date ()
161 "Interactively read the arguments for a Bahá'í date command.
162 Reads a year, month and day."
163 (let* ((today (calendar-current-date))
164 (year (calendar-read
165 "Bahá'í calendar year (not 0): "
166 (lambda (x) (not (zerop x)))
167 (int-to-string
168 (extract-calendar-year
169 (calendar-bahai-from-absolute
170 (calendar-absolute-from-gregorian today))))))
171 (completion-ignore-case t)
172 (month (cdr (assoc
173 (completing-read
174 "Bahá'í calendar month name: "
175 (mapcar 'list
176 (append calendar-bahai-month-name-array nil))
177 nil t)
178 (calendar-make-alist calendar-bahai-month-name-array
179 1))))
180 (day (calendar-read "Bahá'í calendar day (1-19): "
181 (lambda (x) (and (< 0 x) (<= x 19))))))
182 (list (list month day year))))
184 (define-obsolete-function-alias
185 'calendar-bahai-prompt-for-date 'calendar-bahai-read-date "23.1")
187 ;;;###cal-autoload
188 (defun calendar-bahai-goto-date (date &optional noecho)
189 "Move cursor to Bahá'í date DATE; echo Bahá'í date unless NOECHO is non-nil."
190 (interactive (calendar-bahai-read-date))
191 (calendar-goto-date (calendar-gregorian-from-absolute
192 (calendar-bahai-to-absolute date)))
193 (or noecho (calendar-bahai-print-date)))
195 (define-obsolete-function-alias
196 'calendar-goto-bahai-date 'calendar-bahai-goto-date "23.1")
198 (defvar displayed-month)
199 (defvar displayed-year)
201 ;;;###holiday-autoload
202 (defun holiday-bahai (month day string)
203 "Holiday on MONTH, DAY (Bahá'í) called STRING.
204 If MONTH, DAY (Bahá'í) is visible, the value returned is corresponding
205 Gregorian date in the form of the list (((month day year) STRING)). Returns
206 nil if it is not visible in the current calendar window."
207 ;; Since the calendar window shows 3 months at a time, there are
208 ;; approx +/- 45 days either side of the central month.
209 ;; Since the Bahai months have 19 days, this means up to +/- 3 months.
210 (let* ((bahai-date (calendar-bahai-from-absolute
211 (calendar-absolute-from-gregorian
212 (list displayed-month 15 displayed-year))))
213 (m (extract-calendar-month bahai-date))
214 (y (extract-calendar-year bahai-date))
215 date)
216 (unless (< m 1) ; Bahá'í calendar doesn't apply
217 ;; Cf holiday-fixed, holiday-islamic.
218 ;; With a +- 3 month calendar window, and 19 months per year,
219 ;; month 16 is special. When m16 is central is when the
220 ;; end-of-year first appears. When m1 is central, m16 is no
221 ;; longer visible. Hence we can do a one-sided test to see if
222 ;; m16 is visible. m16 is visible when the central month >= 13.
223 ;; To see if other months are visible we can shift the range
224 ;; accordingly.
225 (increment-calendar-month m y (- 16 month) 19)
226 (and (> m 12) ; Bahá'í date might be visible
227 (calendar-date-is-visible-p
228 (setq date (calendar-gregorian-from-absolute
229 (calendar-bahai-to-absolute (list month day y)))))
230 (list (list date string))))))
232 (autoload 'holiday-fixed "holidays")
234 ;;;###holiday-autoload
235 (defun holiday-bahai-new-year ()
236 "Holiday entry for the Bahá'í New Year, if visible in the calendar window."
237 (holiday-fixed 3 21
238 (format "Bahá'í New Year (Naw-Ruz) %d"
239 (- displayed-year (1- 1844)))))
241 ;;;###holiday-autoload
242 (defun holiday-bahai-ridvan (&optional all)
243 "Holidays related to Ridvan, as visible in the calendar window.
244 Only considers the first, ninth, and twelfth days, unless ALL or
245 `calendar-bahai-all-holidays-flag' is non-nil."
246 (let ((ord ["First" "Second" "Third" "Fourth" "Fifth" "Sixth"
247 "Seventh" "Eighth" "Ninth" "Tenth" "Eleventh" "Twelfth"])
248 (show '(0 8 11))
249 rid h)
250 (if (or all calendar-bahai-all-holidays-flag)
251 (setq show (number-sequence 0 11)))
252 ;; More trouble than it was worth...?
253 (dolist (i show (nreverse rid))
254 (if (setq h (holiday-fixed (if (< i 10) 4 5)
255 (+ i (if (< i 10) 21 -9))
256 (format "%s Day of Ridvan" (aref ord i))))
257 (push (car h) rid)))))
259 (autoload 'diary-list-entries-1 "diary-lib")
261 ;;;###diary-autoload
262 (defun diary-bahai-list-entries ()
263 "Add any Bahá'í date entries from the diary file to `diary-entries-list'.
264 Bahá'í date diary entries must be prefaced by `diary-bahai-entry-symbol'
265 \(normally a `B'). The same diary date forms govern the style of the
266 Bahá'í calendar entries, except that the Bahá'í month names cannot be
267 abbreviated. The Bahá'í months are numbered from 1 to 19 with Bahá being
268 1 and 19 being `Alá. If a Bahá'í date diary entry begins with
269 `diary-nonmarking-symbol', the entry will appear in the diary listing, but
270 will not be marked in the calendar. This function is provided for use with
271 `diary-nongregorian-listing-hook'."
272 (diary-list-entries-1 calendar-bahai-month-name-array
273 diary-bahai-entry-symbol
274 'calendar-bahai-from-absolute))
275 (define-obsolete-function-alias
276 'list-bahai-diary-entries 'diary-bahai-list-entries "23.1")
279 (autoload 'calendar-mark-1 "diary-lib")
281 ;;;###diary-autoload
282 (defun calendar-bahai-mark-date-pattern (month day year &optional color)
283 "Mark dates in calendar window that conform to Bahá'í date MONTH/DAY/YEAR.
284 A value of 0 in any position is a wildcard. Optional argument COLOR is
285 passed to `mark-visible-calendar-date' as MARK."
286 (calendar-mark-1 month day year 'calendar-bahai-from-absolute
287 'calendar-bahai-to-absolute color))
289 (define-obsolete-function-alias
290 'mark-bahai-calendar-date-pattern 'calendar-bahai-mark-date-pattern "23.1")
293 (autoload 'diary-mark-entries-1 "diary-lib")
295 ;;;###diary-autoload
296 (defun diary-bahai-mark-entries ()
297 "Mark days in the calendar window that have Bahá'í date diary entries.
298 Marks each entry in `diary-file' (or included files) visible in the calendar
299 window. See `diary-bahai-list-entries' for more information."
300 (diary-mark-entries-1 'calendar-bahai-mark-date-pattern
301 calendar-bahai-month-name-array
302 diary-bahai-entry-symbol
303 'calendar-bahai-from-absolute))
305 (define-obsolete-function-alias
306 'mark-bahai-diary-entries 'diary-bahai-mark-entries "23.1")
309 (autoload 'diary-insert-entry-1 "diary-lib")
311 ;;;###cal-autoload
312 (defun diary-bahai-insert-entry (arg)
313 "Insert a diary entry.
314 For the Bahá'í date corresponding to the date indicated by point.
315 Prefix argument ARG makes the entry nonmarking."
316 (interactive "P")
317 (diary-insert-entry-1 nil arg calendar-bahai-month-name-array
318 diary-bahai-entry-symbol
319 'calendar-bahai-from-absolute))
321 (define-obsolete-function-alias
322 'insert-bahai-diary-entry 'diary-bahai-insert-entry "23.1")
324 ;;;###cal-autoload
325 (defun diary-bahai-insert-monthly-entry (arg)
326 "Insert a monthly diary entry.
327 For the day of the Bahá'í month corresponding to the date indicated by point.
328 Prefix argument ARG makes the entry nonmarking."
329 (interactive "P")
330 (diary-insert-entry-1 'monthly arg calendar-bahai-month-name-array
331 diary-bahai-entry-symbol
332 'calendar-bahai-from-absolute))
334 (define-obsolete-function-alias
335 'insert-monthly-bahai-diary-entry 'diary-bahai-insert-monthly-entry "23.1")
337 ;;;###cal-autoload
338 (defun diary-bahai-insert-yearly-entry (arg)
339 "Insert an annual diary entry.
340 For the day of the Bahá'í year corresponding to the date indicated by point.
341 Prefix argument ARG will make the entry nonmarking."
342 (interactive "P")
343 (diary-insert-entry-1 'yearly arg calendar-bahai-month-name-array
344 diary-bahai-entry-symbol
345 'calendar-bahai-from-absolute))
347 (define-obsolete-function-alias
348 'insert-yearly-bahai-diary-entry 'diary-bahai-insert-yearly-entry "23.1")
350 (defvar date)
352 ;; To be called from diary-list-sexp-entries, where DATE is bound.
353 ;;;###diary-autoload
354 (defun diary-bahai-date ()
355 "Bahá'í calendar equivalent of date diary entry."
356 (format "Bahá'í date: %s" (calendar-bahai-date-string date)))
359 (provide 'cal-bahai)
361 ;; Local Variables:
362 ;; coding: utf-8
363 ;; End:
365 ;; arch-tag: c1cb1d67-862a-4264-a01c-41cb4df01f14
366 ;;; cal-bahai.el ends here