(calendar-mark-1): Autoload it.
[emacs.git] / lisp / calendar / cal-bahai.el
blobd92cab52c1e5c2d15a8e6708d27a80f0284244c4
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 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))
87 (/ prior-years 400))
88 calendar-bahai-leap-base)))
89 (+ (1- calendar-bahai-epoch) ; days before epoch
90 (* 365 (1- year)) ; days in prior years
91 leap-days
92 (calendar-sum m 1 (< m month) 19)
93 (if (= month 19) 4 0)
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)))
105 1 0)))
106 (month ; search forward from Baha
107 (1+ (calendar-sum m 1
108 (> date
109 (calendar-absolute-from-bahai
110 (list m 19 year)))
111 1)))
112 (day ; calculate the day by subtraction
113 (- date
114 (1- (calendar-absolute-from-bahai (list month 1 year))))))
115 (list month day year))))
117 ;;;###cal-autoload
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)
128 (<= d 0))
129 "Ayyám-i-Há"
130 (aref calendar-bahai-month-name-array (1- m))))
131 (day (int-to-string
132 (if (<= d 0)
133 (if (calendar-bahai-leap-year-p y)
134 (+ d 5)
135 (+ d 4))
136 d)))
137 (year (int-to-string y))
138 (month (int-to-string m))
139 dayname)
140 (mapconcat 'eval calendar-date-display-form "")))
142 ;;;###cal-autoload
143 (defun calendar-bahai-print-date ()
144 "Show the Bahá'í calendar equivalent of the selected date."
145 (interactive)
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))
155 (year (calendar-read
156 "Bahá'í calendar year (not 0): "
157 (lambda (x) (not (zerop x)))
158 (int-to-string
159 (extract-calendar-year
160 (calendar-bahai-from-absolute
161 (calendar-absolute-from-gregorian today))))))
162 (completion-ignore-case t)
163 (month (cdr (assoc
164 (completing-read
165 "Bahá'í calendar month name: "
166 (mapcar 'list
167 (append calendar-bahai-month-name-array nil))
168 nil t)
169 (calendar-make-alist calendar-bahai-month-name-array
170 1))))
171 (day (calendar-read "Bahá'í calendar day (1-19): "
172 (lambda (x) (and (< 0 x) (<= x 19))))))
173 (list (list month day year))))
175 ;;;###cal-autoload
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))
200 (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.
212 ;;;###diary-autoload
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")
232 ;;;###diary-autoload
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")
246 ;;;###diary-autoload
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")
259 ;;;###cal-autoload
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."
264 (interactive "P")
265 (let ((calendar-month-name-array calendar-bahai-month-name-array))
266 (make-diary-entry
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)))
271 nil t))
272 arg)))
274 (define-obsolete-function-alias
275 'insert-bahai-diary-entry 'diary-bahai-insert-entry "23.1")
277 ;;;###cal-autoload
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."
282 (interactive "P")
283 (let ((calendar-date-display-form (if european-calendar-style
284 '(day " * ")
285 '("* " day )))
286 (calendar-month-name-array calendar-bahai-month-name-array))
287 (make-diary-entry
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)))))
292 arg)))
294 (define-obsolete-function-alias
295 'insert-monthly-bahai-diary-entry 'diary-bahai-insert-monthly-entry "23.1")
297 ;;;###cal-autoload
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."
302 (interactive "P")
303 (let ((calendar-date-display-form (if european-calendar-style
304 '(day " " monthname)
305 '(monthname " " day)))
306 (calendar-month-name-array calendar-bahai-month-name-array))
307 (make-diary-entry
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)))))
312 arg)))
314 (define-obsolete-function-alias
315 'insert-yearly-bahai-diary-entry 'diary-bahai-insert-yearly-entry "23.1")
317 (defvar date)
319 ;; To be called from list-sexp-diary-entries, where DATE is bound.
320 ;;;###diary-autoload
321 (defun diary-bahai-date ()
322 "Bahá'í calendar equivalent of date diary entry."
323 (format "Bahá'í date: %s" (calendar-bahai-date-string date)))
326 (provide 'cal-bahai)
328 ;; Local Variables:
329 ;; coding: utf-8
330 ;; End:
332 ;; arch-tag: c1cb1d67-862a-4264-a01c-41cb4df01f14
333 ;;; cal-bahai.el ends here