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á"])
65 (defconst calendar-bahai-epoch
(calendar-absolute-from-gregorian '(3 21 1844))
66 "Absolute date of start of Bahá'í calendar = March 19, 622 A.D. (Julian).")
68 (defun calendar-bahai-leap-year-p (year)
69 "True if YEAR is a leap year on the Bahá'í calendar."
70 (calendar-leap-year-p (+ year
1844)))
72 (defconst calendar-bahai-leap-base
73 (+ (/ 1844 4) (- (/ 1844 100)) (/ 1844 400)))
75 (defun calendar-absolute-from-bahai (date)
76 "Compute absolute date from Bahá'í date DATE.
77 The absolute date is the number of days elapsed since the (imaginary)
78 Gregorian date Sunday, December 31, 1 BC."
79 (let* ((month (extract-calendar-month date
))
80 (day (extract-calendar-day date
))
81 (year (extract-calendar-year date
))
82 (prior-years (+ (1- year
) 1844))
83 (leap-days (- (+ (/ prior-years
4) ; leap days in prior years
84 (- (/ prior-years
100))
86 calendar-bahai-leap-base
)))
87 (+ (1- calendar-bahai-epoch
) ; days before epoch
88 (* 365 (1- year
)) ; days in prior years
90 (calendar-sum m
1 (< m month
) 19)
92 day
))) ; days so far this month
94 (defun calendar-bahai-from-absolute (date)
95 "Bahá'í year corresponding to the absolute DATE."
96 (if (< date calendar-bahai-epoch
)
97 (list 0 0 0) ; pre-Bahá'í date
98 (let* ((greg (calendar-gregorian-from-absolute date
))
99 (year (+ (- (extract-calendar-year greg
) 1844)
100 (if (or (> (extract-calendar-month greg
) 3)
101 (and (= (extract-calendar-month greg
) 3)
102 (>= (extract-calendar-day greg
) 21)))
104 (month ; search forward from Baha
105 (1+ (calendar-sum m
1
107 (calendar-absolute-from-bahai
110 (day ; calculate the day by subtraction
112 (1- (calendar-absolute-from-bahai (list month
1 year
))))))
113 (list month day year
))))
116 (defun calendar-bahai-date-string (&optional date
)
117 "String of Bahá'í date of Gregorian DATE.
118 Defaults to today's date if DATE is not given."
119 (let* ((bahai-date (calendar-bahai-from-absolute
120 (calendar-absolute-from-gregorian
121 (or date
(calendar-current-date)))))
122 (y (extract-calendar-year bahai-date
))
123 (m (extract-calendar-month bahai-date
))
124 (d (extract-calendar-day bahai-date
)))
129 (aref calendar-bahai-month-name-array
(1- m
))))
132 (if (calendar-bahai-leap-year-p y
)
137 (month (int-to-string m
))
138 (year (int-to-string y
)))
139 (mapconcat 'eval calendar-date-display-form
""))))
142 (defun calendar-bahai-print-date ()
143 "Show the Bahá'í calendar equivalent of the selected date."
145 (message "Bahá'í date: %s"
146 (calendar-bahai-date-string (calendar-cursor-to-date t
))))
149 (defun calendar-bahai-goto-date (date &optional noecho
)
150 "Move cursor to Bahá'í date DATE.
151 Echo Bahá'í date unless NOECHO is t."
152 (interactive (calendar-bahai-prompt-for-date))
153 (calendar-goto-date (calendar-gregorian-from-absolute
154 (calendar-absolute-from-bahai date
)))
155 (or noecho
(calendar-bahai-print-date)))
157 (defun calendar-bahai-prompt-for-date ()
158 "Ask for a Bahá'í date."
159 (let* ((today (calendar-current-date))
161 "Bahá'í calendar year (not 0): "
162 (lambda (x) (not (zerop x
)))
164 (extract-calendar-year
165 (calendar-bahai-from-absolute
166 (calendar-absolute-from-gregorian today
))))))
167 (completion-ignore-case t
)
170 "Bahá'í calendar month name: "
172 (append calendar-bahai-month-name-array nil
))
174 (calendar-make-alist calendar-bahai-month-name-array
176 (day (calendar-read "Bahá'í calendar day (1-19): "
177 (lambda (x) (and (< 0 x
) (<= x
19))))))
178 (list (list month day year
))))
180 (defvar displayed-month
)
181 (defvar displayed-year
)
183 ;;;###holiday-autoload
184 (defun holiday-bahai (month day string
)
185 "Holiday on MONTH, DAY (Bahá'í) called STRING.
186 If MONTH, DAY (Bahá'í) is visible, the value returned is corresponding
187 Gregorian date in the form of the list (((month day year) STRING)). Returns
188 nil if it is not visible in the current calendar window."
189 (let* ((bahai-date (calendar-bahai-from-absolute
190 (calendar-absolute-from-gregorian
191 (list displayed-month
15 displayed-year
))))
192 (m (extract-calendar-month bahai-date
))
193 (y (extract-calendar-year bahai-date
))
196 nil
; Bahá'í calendar doesn't apply
197 (increment-calendar-month m y
(- 10 month
))
198 (if (> m
7) ; Bahá'í date might be visible
199 (let ((date (calendar-gregorian-from-absolute
200 (calendar-absolute-from-bahai (list month day y
)))))
201 (if (calendar-date-is-visible-p date
)
202 (list (list date string
))))))))
205 (defvar original-date
)
207 ;; d-b-l-e should be called from diary code.
208 (declare-function add-to-diary-list
"diary-lib"
209 (date string specifier
&optional marker globcolor literal
))
212 (defun diary-bahai-list-entries ()
213 "Add any Bahá'í date entries from the diary file to `diary-entries-list'.
214 Bahá'í date diary entries must be prefaced by an
215 `bahai-diary-entry-symbol' (normally a `B'). The same diary date
216 forms govern the style of the Bahá'í calendar entries, except that the
217 Bahá'í month names must be given numerically. The Bahá'í months are
218 numbered from 1 to 19 with Bahá being 1 and 19 being `Alá. If a
219 Bahá'í date diary entry begins with a `diary-nonmarking-symbol', the
220 entry will appear in the diary listing, but will not be marked in the
221 calendar. This function is provided for use with the
222 `nongregorian-diary-listing-hook'."
224 (let ((buffer-read-only nil
)
225 (diary-modified (buffer-modified-p))
226 (gdate original-date
)
227 (mark (regexp-quote diary-nonmarking-symbol
)))
228 (dotimes (idummy number
)
229 (let* ((d diary-date-forms
)
230 (bdate (calendar-bahai-from-absolute
231 (calendar-absolute-from-gregorian gdate
)))
232 (month (extract-calendar-month bdate
))
233 (day (extract-calendar-day bdate
))
234 (year (extract-calendar-year bdate
)))
237 ((date-form (if (equal (car (car d
)) 'backup
)
240 (backup (equal (car (car d
)) 'backup
))
243 (calendar-day-name gdate
) "\\|"
244 (substring (calendar-day-name gdate
) 0 3) ".?"))
245 (calendar-month-name-array
246 calendar-bahai-month-name-array
)
250 (calendar-month-name month
)))
251 (month (concat "\\*\\|0*" (int-to-string month
)))
252 (day (concat "\\*\\|0*" (int-to-string day
)))
255 "\\*\\|0*" (int-to-string year
)
256 (if abbreviated-calendar-year
257 (concat "\\|" (int-to-string (% year
100)))
261 "\\(\\`\\|\^M\\|\n\\)" mark
"?"
262 (regexp-quote bahai-diary-entry-symbol
)
264 (mapconcat 'eval date-form
"\\)\\(")
266 (case-fold-search t
))
267 (goto-char (point-min))
268 (while (re-search-forward regexp nil t
)
269 (if backup
(re-search-backward "\\<" nil t
))
270 (if (and (or (char-equal (preceding-char) ?\^M
)
271 (char-equal (preceding-char) ?
\n))
272 (not (looking-at " \\|\^I")))
273 ;; Diary entry that consists only of date.
275 ;; Found a nonempty diary entry--make it visible and
276 ;; add it to the list.
277 (let ((entry-start (point))
279 (re-search-backward "\^M\\|\n\\|\\`")
280 (setq date-start
(point))
281 (re-search-forward "\^M\\|\n" nil t
2)
282 (while (looking-at " \\|\^I")
283 (re-search-forward "\^M\\|\n" nil t
))
285 (subst-char-in-region date-start
(point) ?\^M ?
\n t
)
288 (buffer-substring-no-properties entry-start
(point))
289 (buffer-substring-no-properties
290 (1+ date-start
) (1- entry-start
)))))))
293 (calendar-gregorian-from-absolute
294 (1+ (calendar-absolute-from-gregorian gdate
)))))
295 (set-buffer-modified-p diary-modified
))
296 (goto-char (point-min))))
298 (declare-function diary-name-pattern
"diary-lib"
299 (string-array &optional abbrev-array paren
))
301 (declare-function mark-calendar-days-named
"diary-lib"
302 (dayname &optional color
))
305 (defun diary-bahai-mark-entries ()
306 "Mark days in the calendar window that have Bahá'í date diary entries.
307 Each entry in `diary-file' (or included files) visible in the calendar
308 window is marked. Bahá'í date entries are prefaced by
309 `bahai-diary-entry-symbol' (normally a \"B\"). The same
310 `diary-date-forms' govern the style of the Bahá'í calendar entries,
311 except that the Bahá'í month names must be spelled in full. The
312 Bahá'í months are numbered from 1 to 12 with Bahá being 1 and 12 being
313 `Alá. Bahá'í date diary entries that begin with `diary-nonmarking-symbol'
314 will not be marked in the calendar. This function is provided for use as
315 part of `nongregorian-diary-marking-hook'."
316 (let ((d diary-date-forms
))
319 ((date-form (if (equal (car (car d
)) 'backup
)
321 (car d
))) ; ignore 'backup directive
322 (dayname (diary-name-pattern calendar-day-name-array
))
325 (diary-name-pattern calendar-bahai-month-name-array t
)
327 (month "[0-9]+\\|\\*")
329 (year "[0-9]+\\|\\*")
330 (l (length date-form
))
331 (d-name-pos (- l
(length (memq 'dayname date-form
))))
332 (d-name-pos (if (/= l d-name-pos
) (+ 2 d-name-pos
)))
333 (m-name-pos (- l
(length (memq 'monthname date-form
))))
334 (m-name-pos (if (/= l m-name-pos
) (+ 2 m-name-pos
)))
335 (d-pos (- l
(length (memq 'day date-form
))))
336 (d-pos (if (/= l d-pos
) (+ 2 d-pos
)))
337 (m-pos (- l
(length (memq 'month date-form
))))
338 (m-pos (if (/= l m-pos
) (+ 2 m-pos
)))
339 (y-pos (- l
(length (memq 'year date-form
))))
340 (y-pos (if (/= l y-pos
) (+ 2 y-pos
)))
343 "\\(\\`\\|\^M\\|\n\\)"
344 (regexp-quote bahai-diary-entry-symbol
)
346 (mapconcat 'eval date-form
"\\)\\(")
348 (case-fold-search t
))
349 (goto-char (point-min))
350 (while (re-search-forward regexp nil t
)
354 (match-beginning d-name-pos
)
355 (match-end d-name-pos
))))
359 (match-beginning m-name-pos
)
360 (match-end m-name-pos
))))
361 (mm (string-to-number
364 (match-beginning m-pos
)
367 (dd (string-to-number
370 (match-beginning d-pos
)
375 (match-beginning y-pos
)
379 (if (and (= (length y-str
) 2)
380 abbreviated-calendar-year
)
382 (extract-calendar-year
383 (calendar-bahai-from-absolute
384 (calendar-absolute-from-gregorian
385 (calendar-current-date)))))
386 (y (+ (string-to-number y-str
)
387 (* 100 (/ current-y
100)))))
388 (if (> (- y current-y
) 50)
390 (if (> (- current-y y
) 50)
393 (string-to-number y-str
)))))
395 (mark-calendar-days-named
396 (cdr (assoc-string (substring dd-name
0 3)
398 calendar-day-name-array
400 (lambda (x) (substring x
0 3)))
403 (if (string-equal mm-name
"*")
409 calendar-bahai-month-name-array
)
411 (calendar-bahai-mark-date-pattern mm dd yy
)))))
415 (defun calendar-bahai-mark-date-pattern (month day year
)
416 "Mark dates in calendar window that conform to Bahá'í date MONTH/DAY/YEAR.
417 A value of 0 in any position is a wildcard."
419 (set-buffer calendar-buffer
)
420 (if (and (not (zerop month
)) (not (zerop day
)))
421 (if (not (zerop year
))
422 ;; Fully specified Bahá'í date.
423 (let ((date (calendar-gregorian-from-absolute
424 (calendar-absolute-from-bahai
425 (list month day year
)))))
426 (if (calendar-date-is-visible-p date
)
427 (mark-visible-calendar-date date
)))
428 ;; Month and day in any year--this taken from the holiday stuff.
429 (let* ((bahai-date (calendar-bahai-from-absolute
430 (calendar-absolute-from-gregorian
431 (list displayed-month
15 displayed-year
))))
432 (m (extract-calendar-month bahai-date
))
433 (y (extract-calendar-year bahai-date
))
436 nil
; Bahá'í calendar doesn't apply
437 (increment-calendar-month m y
(- 10 month
))
438 (if (> m
7) ; Bahá'í date might be visible
439 (let ((date (calendar-gregorian-from-absolute
440 (calendar-absolute-from-bahai
441 (list month day y
)))))
442 (if (calendar-date-is-visible-p date
)
443 (mark-visible-calendar-date date
)))))))
444 ;; Not one of the simple cases--check all visible dates for match.
445 ;; Actually, the following code takes care of ALL of the cases, but
446 ;; it's much too slow to be used for the simple (common) cases.
447 (let ((m displayed-month
)
451 (increment-calendar-month m y -
1)
453 (calendar-absolute-from-gregorian
455 (increment-calendar-month m y
2)
457 (calendar-absolute-from-gregorian
458 (list m
(calendar-last-day-of-month m y
) y
)))
459 (calendar-for-loop date from first-date to last-date do
460 (let* ((b-date (calendar-bahai-from-absolute date
))
461 (i-month (extract-calendar-month b-date
))
462 (i-day (extract-calendar-day b-date
))
463 (i-year (extract-calendar-year b-date
)))
464 (and (or (zerop month
)
470 (mark-visible-calendar-date
471 (calendar-gregorian-from-absolute
475 (defun diary-bahai-insert-entry (arg)
476 "Insert a diary entry.
477 For the Bahá'í date corresponding to the date indicated by point.
478 Prefix argument ARG makes the entry nonmarking."
480 (let* ((calendar-month-name-array calendar-bahai-month-name-array
))
483 bahai-diary-entry-symbol
484 (calendar-date-string
485 (calendar-bahai-from-absolute
486 (calendar-absolute-from-gregorian
487 (calendar-cursor-to-date t
)))
492 (defun diary-bahai-insert-monthly-entry (arg)
493 "Insert a monthly diary entry.
494 For the day of the Bahá'í month corresponding to the date indicated by point.
495 Prefix argument ARG makes the entry nonmarking."
497 (let* ((calendar-date-display-form
498 (if european-calendar-style
'(day " * ") '("* " day
)))
499 (calendar-month-name-array calendar-bahai-month-name-array
))
502 bahai-diary-entry-symbol
503 (calendar-date-string
504 (calendar-bahai-from-absolute
505 (calendar-absolute-from-gregorian
506 (calendar-cursor-to-date t
)))))
510 (defun diary-bahai-insert-yearly-entry (arg)
511 "Insert an annual diary entry.
512 For the day of the Bahá'í year corresponding to the date indicated by point.
513 Prefix argument ARG will make the entry nonmarking."
515 (let* ((calendar-date-display-form
516 (if european-calendar-style
518 '(monthname " " day
)))
519 (calendar-month-name-array calendar-bahai-month-name-array
))
522 bahai-diary-entry-symbol
523 (calendar-date-string
524 (calendar-bahai-from-absolute
525 (calendar-absolute-from-gregorian
526 (calendar-cursor-to-date t
)))))
531 ;; To be called from list-sexp-diary-entries, where DATE is bound.
533 (defun diary-bahai-date ()
534 "Bahá'í calendar equivalent of date diary entry."
535 (format "Bahá'í date: %s" (calendar-bahai-date-string date
)))
538 ;; Backward compatibility.
539 (define-obsolete-function-alias
540 'list-bahai-diary-entries
'diary-bahai-list-entries
"23.1")
541 (define-obsolete-function-alias
542 'mark-bahai-diary-entries
'diary-bahai-mark-entries
"23.1")
543 (define-obsolete-function-alias
544 'insert-bahai-diary-entry
'diary-bahai-insert-entry
"23.1")
545 (define-obsolete-function-alias
546 'insert-monthly-bahai-diary-entry
'diary-bahai-insert-monthly-entry
"23.1")
547 (define-obsolete-function-alias
548 'insert-yearly-bahai-diary-entry
'diary-bahai-insert-yearly-entry
"23.1")
549 (define-obsolete-function-alias
550 'mark-bahai-calendar-date-pattern
'calendar-bahai-mark-date-pattern
"23.1")
551 (define-obsolete-function-alias
552 'calendar-goto-bahai-date
'calendar-bahai-goto-date
"23.1")
553 (define-obsolete-function-alias
554 'calendar-print-bahai-date
'calendar-bahai-print-date
"23.1")
562 ;; arch-tag: c1cb1d67-862a-4264-a01c-41cb4df01f14
563 ;;; cal-bahai.el ends here