Autoload diary-insert-entry-1.
[emacs.git] / lisp / calendar / cal-bahai.el
blob21978cacbf485b5fa956a3a6b1f0c2642a54d589
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-absolute-from-bahai'.")
78 (defun calendar-absolute-from-bahai (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 (defun calendar-bahai-from-absolute (date)
100 "Bahá'í date (month day year) corresponding to the absolute DATE."
101 (if (< date calendar-bahai-epoch)
102 (list 0 0 0) ; pre-Bahá'í date
103 (let* ((greg (calendar-gregorian-from-absolute date))
104 (gmonth (extract-calendar-month greg))
105 (year (+ (- (extract-calendar-year greg) 1844)
106 (if (or (> gmonth 3)
107 (and (= gmonth 3)
108 (>= (extract-calendar-day greg) 21)))
109 1 0)))
110 (month ; search forward from Baha
111 (1+ (calendar-sum m 1
112 (> date (calendar-absolute-from-bahai (list m 19 year)))
113 1)))
114 (day ; calculate the day by subtraction
115 (- date
116 (1- (calendar-absolute-from-bahai (list month 1 year))))))
117 (list month day year))))
119 ;;;###cal-autoload
120 (defun calendar-bahai-date-string (&optional date)
121 "String of Bahá'í date of Gregorian DATE.
122 Defaults to today's date if DATE is not given."
123 (let* ((bahai-date (calendar-bahai-from-absolute
124 (calendar-absolute-from-gregorian
125 (or date (calendar-current-date)))))
126 (y (extract-calendar-year bahai-date))
127 (m (extract-calendar-month bahai-date))
128 (d (extract-calendar-day bahai-date))
129 (monthname (if (and (= m 19)
130 (<= d 0))
131 "Ayyám-i-Há"
132 (aref calendar-bahai-month-name-array (1- m))))
133 (day (int-to-string
134 (if (<= d 0)
135 (+ d (if (calendar-bahai-leap-year-p y) 5 4))
136 d)))
137 (year (int-to-string y))
138 (month (int-to-string m))
139 dayname)
140 (if (< y 1)
142 ;; Can't call calendar-date-string because of monthname oddity.
143 (mapconcat 'eval calendar-date-display-form ""))))
145 ;;;###cal-autoload
146 (defun calendar-bahai-print-date ()
147 "Show the Bahá'í calendar equivalent of the selected date."
148 (interactive)
149 (let ((s (calendar-bahai-date-string (calendar-cursor-to-date t))))
150 (if (string-equal s "")
151 (message "Date is pre-Bahá'í")
152 (message "Bahá'í date: %s" s))))
154 (define-obsolete-function-alias
155 'calendar-print-bahai-date 'calendar-bahai-print-date "23.1")
157 (defun calendar-bahai-read-date ()
158 "Interactively read the arguments for a Bahá'í date command.
159 Reads a year, month and day."
160 (let* ((today (calendar-current-date))
161 (year (calendar-read
162 "Bahá'í calendar year (not 0): "
163 (lambda (x) (not (zerop x)))
164 (int-to-string
165 (extract-calendar-year
166 (calendar-bahai-from-absolute
167 (calendar-absolute-from-gregorian today))))))
168 (completion-ignore-case t)
169 (month (cdr (assoc
170 (completing-read
171 "Bahá'í calendar month name: "
172 (mapcar 'list
173 (append calendar-bahai-month-name-array nil))
174 nil t)
175 (calendar-make-alist calendar-bahai-month-name-array
176 1))))
177 (day (calendar-read "Bahá'í calendar day (1-19): "
178 (lambda (x) (and (< 0 x) (<= x 19))))))
179 (list (list month day year))))
181 (define-obsolete-function-alias
182 'calendar-bahai-prompt-for-date 'calendar-bahai-read-date "23.1")
184 ;;;###cal-autoload
185 (defun calendar-bahai-goto-date (date &optional noecho)
186 "Move cursor to Bahá'í date DATE; echo Bahá'í date unless NOECHO is non-nil."
187 (interactive (calendar-bahai-read-date))
188 (calendar-goto-date (calendar-gregorian-from-absolute
189 (calendar-absolute-from-bahai date)))
190 (or noecho (calendar-bahai-print-date)))
192 (define-obsolete-function-alias
193 'calendar-goto-bahai-date 'calendar-bahai-goto-date "23.1")
195 (defvar displayed-month)
196 (defvar displayed-year)
198 ;;;###holiday-autoload
199 (defun holiday-bahai (month day string)
200 "Holiday on MONTH, DAY (Bahá'í) called STRING.
201 If MONTH, DAY (Bahá'í) is visible, the value returned is corresponding
202 Gregorian date in the form of the list (((month day year) STRING)). Returns
203 nil if it is not visible in the current calendar window."
204 (let* ((bahai-date (calendar-bahai-from-absolute
205 (calendar-absolute-from-gregorian
206 (list displayed-month 15 displayed-year))))
207 (m (extract-calendar-month bahai-date))
208 (y (extract-calendar-year bahai-date))
209 date)
210 (unless (< m 1) ; Bahá'í calendar doesn't apply
211 (increment-calendar-month m y (- 10 month))
212 (if (> m 7) ; Bahá'í date might be visible
213 (let ((date (calendar-gregorian-from-absolute
214 (calendar-absolute-from-bahai (list month day y)))))
215 (if (calendar-date-is-visible-p date)
216 (list (list date string))))))))
218 (autoload 'diary-list-entries-1 "diary-lib")
220 ;;;###diary-autoload
221 (defun diary-bahai-list-entries ()
222 "Add any Bahá'í date entries from the diary file to `diary-entries-list'.
223 Bahá'í date diary entries must be prefaced by `bahai-diary-entry-symbol'
224 \(normally a `B'). The same diary date forms govern the style of the
225 Bahá'í calendar entries, except that the Bahá'í month names cannot be
226 abbreviated. The Bahá'í months are numbered from 1 to 19 with Bahá being
227 1 and 19 being `Alá. If a Bahá'í date diary entry begins with
228 `diary-nonmarking-symbol', the entry will appear in the diary listing, but
229 will not be marked in the calendar. This function is provided for use with
230 `nongregorian-diary-listing-hook'."
231 (diary-list-entries-1 calendar-bahai-month-name-array
232 bahai-diary-entry-symbol
233 'calendar-bahai-from-absolute))
234 (define-obsolete-function-alias
235 'list-bahai-diary-entries 'diary-bahai-list-entries "23.1")
238 (autoload 'calendar-mark-1 "diary-lib")
240 ;;;###diary-autoload
241 (defun calendar-bahai-mark-date-pattern (month day year &optional color)
242 "Mark dates in calendar window that conform to Bahá'í date MONTH/DAY/YEAR.
243 A value of 0 in any position is a wildcard. Optional argument COLOR is
244 passed to `mark-visible-calendar-date' as MARK."
245 (calendar-mark-1 month day year 'calendar-bahai-from-absolute
246 'calendar-absolute-from-bahai color))
248 (define-obsolete-function-alias
249 'mark-bahai-calendar-date-pattern 'calendar-bahai-mark-date-pattern "23.1")
252 (autoload 'diary-mark-entries-1 "diary-lib")
254 ;;;###diary-autoload
255 (defun diary-bahai-mark-entries ()
256 "Mark days in the calendar window that have Bahá'í date diary entries.
257 Marks each entry in `diary-file' (or included files) visible in the calendar
258 window. See `diary-bahai-list-entries' for more information."
259 (diary-mark-entries-1 calendar-bahai-month-name-array
260 bahai-diary-entry-symbol
261 'calendar-bahai-from-absolute
262 'calendar-bahai-mark-date-pattern))
264 (define-obsolete-function-alias
265 'mark-bahai-diary-entries 'diary-bahai-mark-entries "23.1")
268 (autoload 'diary-insert-entry-1 "diary-lib")
270 ;;;###cal-autoload
271 (defun diary-bahai-insert-entry (arg)
272 "Insert a diary entry.
273 For the Bahá'í date corresponding to the date indicated by point.
274 Prefix argument ARG makes the entry nonmarking."
275 (interactive "P")
276 (diary-insert-entry-1 nil arg calendar-bahai-month-name-array
277 bahai-diary-entry-symbol
278 'calendar-bahai-from-absolute))
280 (define-obsolete-function-alias
281 'insert-bahai-diary-entry 'diary-bahai-insert-entry "23.1")
283 ;;;###cal-autoload
284 (defun diary-bahai-insert-monthly-entry (arg)
285 "Insert a monthly diary entry.
286 For the day of the Bahá'í month corresponding to the date indicated by point.
287 Prefix argument ARG makes the entry nonmarking."
288 (interactive "P")
289 (diary-insert-entry-1 'monthly arg calendar-bahai-month-name-array
290 bahai-diary-entry-symbol
291 'calendar-bahai-from-absolute))
293 (define-obsolete-function-alias
294 'insert-monthly-bahai-diary-entry 'diary-bahai-insert-monthly-entry "23.1")
296 ;;;###cal-autoload
297 (defun diary-bahai-insert-yearly-entry (arg)
298 "Insert an annual diary entry.
299 For the day of the Bahá'í year corresponding to the date indicated by point.
300 Prefix argument ARG will make the entry nonmarking."
301 (interactive "P")
302 (diary-insert-entry-1 'yearly arg calendar-bahai-month-name-array
303 bahai-diary-entry-symbol
304 'calendar-bahai-from-absolute))
306 (define-obsolete-function-alias
307 'insert-yearly-bahai-diary-entry 'diary-bahai-insert-yearly-entry "23.1")
309 (defvar date)
311 ;; To be called from list-sexp-diary-entries, where DATE is bound.
312 ;;;###diary-autoload
313 (defun diary-bahai-date ()
314 "Bahá'í calendar equivalent of date diary entry."
315 (format "Bahá'í date: %s" (calendar-bahai-date-string date)))
318 (provide 'cal-bahai)
320 ;; Local Variables:
321 ;; coding: utf-8
322 ;; End:
324 ;; arch-tag: c1cb1d67-862a-4264-a01c-41cb4df01f14
325 ;;; cal-bahai.el ends here