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.
59 (defvar displayed-month
)
60 (defvar displayed-year
)
62 (defvar original-date
)
66 (defconst calendar-bahai-month-name-array
67 ["Bahá" "Jalál" "Jamál" "`Azamat" "Núr" "Rahmat" "Kalimát" "Kamál"
68 "Asmá" "`Izzat" "Mashiyyat" "`Ilm" "Qudrat" "Qawl" "Masá'il"
69 "Sharaf" "Sultán" "Mulk" "`Alá"])
71 (defconst calendar-bahai-epoch
(calendar-absolute-from-gregorian '(3 21 1844))
72 "Absolute date of start of Bahá'í calendar = March 19, 622 A.D. (Julian).")
74 (defun calendar-bahai-leap-year-p (year)
75 "True if YEAR is a leap year on the Bahá'í calendar."
76 (calendar-leap-year-p (+ year
1844)))
78 (defconst calendar-bahai-leap-base
79 (+ (/ 1844 4) (- (/ 1844 100)) (/ 1844 400)))
81 (defun calendar-absolute-from-bahai (date)
82 "Compute absolute date from Bahá'í date DATE.
83 The absolute date is the number of days elapsed since the (imaginary)
84 Gregorian date Sunday, December 31, 1 BC."
85 (let* ((month (extract-calendar-month date
))
86 (day (extract-calendar-day date
))
87 (year (extract-calendar-year date
))
88 (prior-years (+ (1- year
) 1844))
89 (leap-days (- (+ (/ prior-years
4) ; Leap days in prior years.
90 (- (/ prior-years
100))
92 calendar-bahai-leap-base
)))
93 (+ (1- calendar-bahai-epoch
) ; Days before epoch
94 (* 365 (1- year
)) ; Days in prior years.
96 (calendar-sum m
1 (< m month
) 19)
98 day
))) ; Days so far this month.
100 (defun calendar-bahai-from-absolute (date)
101 "Bahá'í year corresponding to the absolute DATE."
102 (if (< date calendar-bahai-epoch
)
103 (list 0 0 0) ;; pre-Bahá'í date
104 (let* ((greg (calendar-gregorian-from-absolute date
))
105 (year (+ (- (extract-calendar-year greg
) 1844)
106 (if (or (> (extract-calendar-month greg
) 3)
107 (and (= (extract-calendar-month greg
) 3)
108 (>= (extract-calendar-day greg
) 21)))
110 (month ;; Search forward from Baha.
111 (1+ (calendar-sum m
1
113 (calendar-absolute-from-bahai
116 (day ;; Calculate the day by subtraction.
118 (1- (calendar-absolute-from-bahai (list month
1 year
))))))
119 (list month day year
))))
122 (defun calendar-bahai-date-string (&optional date
)
123 "String of Bahá'í date of Gregorian DATE.
124 Defaults to today's date if DATE is not given."
125 (let* ((bahai-date (calendar-bahai-from-absolute
126 (calendar-absolute-from-gregorian
127 (or date
(calendar-current-date)))))
128 (y (extract-calendar-year bahai-date
))
129 (m (extract-calendar-month bahai-date
))
130 (d (extract-calendar-day bahai-date
)))
135 (aref calendar-bahai-month-name-array
(1- m
))))
138 (if (calendar-bahai-leap-year-p y
)
143 (month (int-to-string m
))
144 (year (int-to-string y
)))
145 (mapconcat 'eval calendar-date-display-form
""))))
148 (defun calendar-bahai-print-date ()
149 "Show the Bahá'í calendar equivalent of the selected date."
151 (message "Bahá'í date: %s"
152 (calendar-bahai-date-string (calendar-cursor-to-date t
))))
155 (defun calendar-bahai-goto-date (date &optional noecho
)
156 "Move cursor to Bahá'í date DATE.
157 Echo Bahá'í date unless NOECHO is t."
158 (interactive (calendar-bahai-prompt-for-date))
159 (calendar-goto-date (calendar-gregorian-from-absolute
160 (calendar-absolute-from-bahai date
)))
161 (or noecho
(calendar-bahai-print-date)))
163 (defun calendar-bahai-prompt-for-date ()
164 "Ask for a Bahá'í date."
165 (let* ((today (calendar-current-date))
167 "Bahá'í calendar year (not 0): "
168 (lambda (x) (/= x
0))
170 (extract-calendar-year
171 (calendar-bahai-from-absolute
172 (calendar-absolute-from-gregorian today
))))))
173 (completion-ignore-case t
)
176 "Bahá'í calendar month name: "
178 (append calendar-bahai-month-name-array nil
))
180 (calendar-make-alist calendar-bahai-month-name-array
182 (day (calendar-read "Bahá'í calendar day (1-19): "
183 (lambda (x) (and (< 0 x
) (<= x
19))))))
184 (list (list month day year
))))
186 (defun diary-bahai-date ()
187 "Bahá'í calendar equivalent of date diary entry."
188 (format "Bahá'í date: %s" (calendar-bahai-date-string date
)))
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
))
202 nil
;; Bahá'í calendar doesn't apply.
203 (increment-calendar-month m y
(- 10 month
))
204 (if (> m
7) ;; Bahá'í date might be visible
205 (let ((date (calendar-gregorian-from-absolute
206 (calendar-absolute-from-bahai (list month day y
)))))
207 (if (calendar-date-is-visible-p date
)
208 (list (list date string
))))))))
210 ;; d-b-l-e should be called from diary code.
211 (declare-function add-to-diary-list
"diary-lib"
212 (date string specifier
&optional marker globcolor literal
))
214 (defun diary-bahai-list-entries ()
215 "Add any Bahá'í date entries from the diary file to `diary-entries-list'.
216 Bahá'í date diary entries must be prefaced by an
217 `bahai-diary-entry-symbol' (normally a `B'). The same diary date
218 forms govern the style of the Bahá'í calendar entries, except that the
219 Bahá'í month names must be given numerically. The Bahá'í months are
220 numbered from 1 to 19 with Bahá being 1 and 19 being `Alá. If a
221 Bahá'í date diary entry begins with a `diary-nonmarking-symbol', the
222 entry will appear in the diary listing, but will not be marked in the
223 calendar. This function is provided for use with the
224 `nongregorian-diary-listing-hook'."
226 (let ((buffer-read-only nil
)
227 (diary-modified (buffer-modified-p))
228 (gdate original-date
)
229 (mark (regexp-quote diary-nonmarking-symbol
)))
230 (dotimes (idummy number
)
231 (let* ((d diary-date-forms
)
232 (bdate (calendar-bahai-from-absolute
233 (calendar-absolute-from-gregorian gdate
)))
234 (month (extract-calendar-month bdate
))
235 (day (extract-calendar-day bdate
))
236 (year (extract-calendar-year bdate
)))
239 ((date-form (if (equal (car (car d
)) 'backup
)
242 (backup (equal (car (car d
)) 'backup
))
245 (calendar-day-name gdate
) "\\|"
246 (substring (calendar-day-name gdate
) 0 3) ".?"))
247 (calendar-month-name-array
248 calendar-bahai-month-name-array
)
252 (calendar-month-name month
)))
253 (month (concat "\\*\\|0*" (int-to-string month
)))
254 (day (concat "\\*\\|0*" (int-to-string day
)))
257 "\\*\\|0*" (int-to-string year
)
258 (if abbreviated-calendar-year
259 (concat "\\|" (int-to-string (% year
100)))
263 "\\(\\`\\|\^M\\|\n\\)" mark
"?"
264 (regexp-quote bahai-diary-entry-symbol
)
266 (mapconcat 'eval date-form
"\\)\\(")
268 (case-fold-search t
))
269 (goto-char (point-min))
270 (while (re-search-forward regexp nil t
)
271 (if backup
(re-search-backward "\\<" nil t
))
272 (if (and (or (char-equal (preceding-char) ?\^M
)
273 (char-equal (preceding-char) ?
\n))
274 (not (looking-at " \\|\^I")))
275 ;; Diary entry that consists only of date.
277 ;; Found a nonempty diary entry--make it visible and
278 ;; add it to the list.
279 (let ((entry-start (point))
281 (re-search-backward "\^M\\|\n\\|\\`")
282 (setq date-start
(point))
283 (re-search-forward "\^M\\|\n" nil t
2)
284 (while (looking-at " \\|\^I")
285 (re-search-forward "\^M\\|\n" nil t
))
287 (subst-char-in-region date-start
(point) ?\^M ?
\n t
)
290 (buffer-substring-no-properties entry-start
(point))
291 (buffer-substring-no-properties
292 (1+ date-start
) (1- entry-start
)))))))
295 (calendar-gregorian-from-absolute
296 (1+ (calendar-absolute-from-gregorian gdate
)))))
297 (set-buffer-modified-p diary-modified
))
298 (goto-char (point-min))))
300 (declare-function diary-name-pattern
"diary-lib"
301 (string-array &optional abbrev-array paren
))
303 (declare-function mark-calendar-days-named
"diary-lib"
304 (dayname &optional color
))
306 (defun diary-bahai-mark-entries ()
307 "Mark days in the calendar window that have Bahá'í date diary entries.
308 Each entry in diary-file (or included files) visible in the calendar
309 window is marked. Bahá'í date entries are prefaced by a
310 bahai-diary-entry-symbol \(normally a B`I'). The same
311 diary-date-forms govern the style of the Bahá'í calendar entries,
312 except that the Bahá'í month names must be spelled in full. The
313 Bahá'í months are numbered from 1 to 12 with Bahá being 1 and 12 being
314 `Alá. Bahá'í date diary entries that begin with a
315 diary-nonmarking-symbol will not be marked in the calendar. This
316 function is provided for use as part of the
317 nongregorian-diary-marking-hook."
318 (let ((d diary-date-forms
))
321 ((date-form (if (equal (car (car d
)) 'backup
)
323 (car d
)));; ignore 'backup directive
324 (dayname (diary-name-pattern calendar-day-name-array
))
327 (diary-name-pattern calendar-bahai-month-name-array t
)
329 (month "[0-9]+\\|\\*")
331 (year "[0-9]+\\|\\*")
332 (l (length date-form
))
333 (d-name-pos (- l
(length (memq 'dayname date-form
))))
334 (d-name-pos (if (/= l d-name-pos
) (+ 2 d-name-pos
)))
335 (m-name-pos (- l
(length (memq 'monthname date-form
))))
336 (m-name-pos (if (/= l m-name-pos
) (+ 2 m-name-pos
)))
337 (d-pos (- l
(length (memq 'day date-form
))))
338 (d-pos (if (/= l d-pos
) (+ 2 d-pos
)))
339 (m-pos (- l
(length (memq 'month date-form
))))
340 (m-pos (if (/= l m-pos
) (+ 2 m-pos
)))
341 (y-pos (- l
(length (memq 'year date-form
))))
342 (y-pos (if (/= l y-pos
) (+ 2 y-pos
)))
345 "\\(\\`\\|\^M\\|\n\\)"
346 (regexp-quote bahai-diary-entry-symbol
)
348 (mapconcat 'eval date-form
"\\)\\(")
350 (case-fold-search t
))
351 (goto-char (point-min))
352 (while (re-search-forward regexp nil t
)
356 (match-beginning d-name-pos
)
357 (match-end d-name-pos
))))
361 (match-beginning m-name-pos
)
362 (match-end m-name-pos
))))
363 (mm (string-to-number
366 (match-beginning m-pos
)
369 (dd (string-to-number
372 (match-beginning d-pos
)
377 (match-beginning y-pos
)
381 (if (and (= (length y-str
) 2)
382 abbreviated-calendar-year
)
384 (extract-calendar-year
385 (calendar-bahai-from-absolute
386 (calendar-absolute-from-gregorian
387 (calendar-current-date)))))
388 (y (+ (string-to-number y-str
)
389 (* 100 (/ current-y
100)))))
390 (if (> (- y current-y
) 50)
392 (if (> (- current-y y
) 50)
395 (string-to-number y-str
)))))
397 (mark-calendar-days-named
398 (cdr (assoc-string (substring dd-name
0 3)
400 calendar-day-name-array
402 (lambda (x) (substring x
0 3)))
405 (if (string-equal mm-name
"*")
411 calendar-bahai-month-name-array
)
413 (calendar-bahai-mark-date-pattern mm dd yy
)))))
416 (defun calendar-bahai-mark-date-pattern (month day year
)
417 "Mark dates in calendar window that conform to Bahá'í date MONTH/DAY/YEAR.
418 A value of 0 in any position is a wildcard."
420 (set-buffer calendar-buffer
)
421 (if (and (/= 0 month
) (/= 0 day
))
423 ;; Fully specified Bahá'í date.
424 (let ((date (calendar-gregorian-from-absolute
425 (calendar-absolute-from-bahai
426 (list month day year
)))))
427 (if (calendar-date-is-visible-p date
)
428 (mark-visible-calendar-date date
)))
429 ;; Month and day in any year--this taken from the holiday stuff.
430 (let* ((bahai-date (calendar-bahai-from-absolute
431 (calendar-absolute-from-gregorian
432 (list displayed-month
15 displayed-year
))))
433 (m (extract-calendar-month bahai-date
))
434 (y (extract-calendar-year bahai-date
))
437 nil
;; Bahá'í calendar doesn't apply.
438 (increment-calendar-month m y
(- 10 month
))
439 (if (> m
7);; Bahá'í date might be visible
440 (let ((date (calendar-gregorian-from-absolute
441 (calendar-absolute-from-bahai
442 (list month day y
)))))
443 (if (calendar-date-is-visible-p date
)
444 (mark-visible-calendar-date date
)))))))
445 ;; Not one of the simple cases--check all visible dates for match.
446 ;; Actually, the following code takes care of ALL of the cases, but
447 ;; it's much too slow to be used for the simple (common) cases.
448 (let ((m displayed-month
)
452 (increment-calendar-month m y -
1)
454 (calendar-absolute-from-gregorian
456 (increment-calendar-month m y
2)
458 (calendar-absolute-from-gregorian
459 (list m
(calendar-last-day-of-month m y
) y
)))
460 (calendar-for-loop date from first-date to last-date do
461 (let* ((b-date (calendar-bahai-from-absolute date
))
462 (i-month (extract-calendar-month b-date
))
463 (i-day (extract-calendar-day b-date
))
464 (i-year (extract-calendar-year b-date
)))
465 (and (or (zerop month
)
471 (mark-visible-calendar-date
472 (calendar-gregorian-from-absolute date
)))))))))
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 arg will make 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 arg will make 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 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
)))))
529 ;; Backward compatibility.
530 (define-obsolete-function-alias
531 'list-bahai-diary-entries
'diary-bahai-list-entries
"23.1")
532 (define-obsolete-function-alias
533 'mark-bahai-diary-entries
'diary-bahai-mark-entries
"23.1")
534 (define-obsolete-function-alias
535 'insert-bahai-diary-entry
'diary-bahai-insert-entry
"23.1")
536 (define-obsolete-function-alias
537 'insert-monthly-bahai-diary-entry
'diary-bahai-insert-monthly-entry
"23.1")
538 (define-obsolete-function-alias
539 'insert-yearly-bahai-diary-entry
'diary-bahai-insert-yearly-entry
"23.1")
540 (define-obsolete-function-alias
541 'mark-bahai-calendar-date-pattern
'calendar-bahai-mark-date-pattern
"23.1")
542 (define-obsolete-function-alias
543 'calendar-goto-bahai-date
'calendar-bahai-goto-date
"23.1")
544 (define-obsolete-function-alias
545 'calendar-print-bahai-date
'calendar-bahai-print-date
"23.1")
551 ;; generated-autoload-file: "cal-loaddefs.el"
554 ;; arch-tag: c1cb1d67-862a-4264-a01c-41cb4df01f14
555 ;;; cal-bahai.el ends here