1 ;;; cal-bahai.el --- calendar functions for the Baha'i calendar.
3 ;; Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006
4 ;; Free Software Foundation, Inc.
6 ;; Author: John Wiegley <johnw@gnu.org>
8 ;; Human-Keywords: Baha'i calendar, 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 2, 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 Baha'i calendar.
32 ;; The Baha'i (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 Ayyam-i-Ha (days of Ha), 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 (Ha
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 ;; Vahids. A cycle of 19 Vahids (361 years) is called a Kullu-Shay,
45 ;; which means "all things".
47 ;; The calendar was named the "Badi calendar" by its author, the Bab.
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 Ayyam-i-Ha 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 Ayyam-i-Ha, in the year 157 BE.
59 (defvar displayed-month
)
60 (defvar displayed-year
)
62 (defvar original-date
)
66 (defvar bahai-calendar-month-name-array
67 ["Baha" "Jalal" "Jamal" "`Azamat" "Nur" "Rahmat" "Kalimat" "Kamal"
68 "Asma" "`Izzat" "Mashiyyat" "`Ilm" "Qudrat" "Qawl" "Masa'il"
69 "Sharaf" "Sultan" "Mulk" "`Ala"])
71 (defvar calendar-bahai-epoch
(calendar-absolute-from-gregorian '(3 21 1844))
72 "Absolute date of start of Baha'i calendar = March 19, 622 A.D. (Julian).")
74 (defun bahai-calendar-leap-year-p (year)
75 "True if YEAR is a leap year on the Baha'i calendar."
76 (calendar-leap-year-p (+ year
1844)))
78 (defvar bahai-calendar-leap-base
79 (+ (/ 1844 4) (- (/ 1844 100)) (/ 1844 400)))
81 (defun calendar-absolute-from-bahai (date)
82 "Compute absolute date from Baha'i 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 bahai-calendar-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 "Baha'i year corresponding to the absolute DATE."
102 (if (< date calendar-bahai-epoch
)
103 (list 0 0 0) ;; pre-Baha'i 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
))))
121 (defun calendar-bahai-date-string (&optional date
)
122 "String of Baha'i date of Gregorian DATE.
123 Defaults to today's date if DATE is not given."
124 (let* ((bahai-date (calendar-bahai-from-absolute
125 (calendar-absolute-from-gregorian
126 (or date
(calendar-current-date)))))
127 (y (extract-calendar-year bahai-date
))
128 (m (extract-calendar-month bahai-date
))
129 (d (extract-calendar-day bahai-date
)))
134 (aref bahai-calendar-month-name-array
(1- m
))))
137 (if (bahai-calendar-leap-year-p y
)
142 (month (int-to-string m
))
143 (year (int-to-string y
)))
144 (mapconcat 'eval calendar-date-display-form
""))))
146 (defun calendar-print-bahai-date ()
147 "Show the Baha'i calendar equivalent of the selected date."
149 (message "Baha'i date: %s"
150 (calendar-bahai-date-string (calendar-cursor-to-date t
))))
152 (defun calendar-goto-bahai-date (date &optional noecho
)
153 "Move cursor to Baha'i date DATE.
154 Echo Baha'i date unless NOECHO is t."
155 (interactive (bahai-prompt-for-date))
156 (calendar-goto-date (calendar-gregorian-from-absolute
157 (calendar-absolute-from-bahai date
)))
158 (or noecho
(calendar-print-bahai-date)))
160 (defun bahai-prompt-for-date ()
161 "Ask for a Baha'i date."
162 (let* ((today (calendar-current-date))
164 "Baha'i calendar year (not 0): "
165 '(lambda (x) (/= x
0))
167 (extract-calendar-year
168 (calendar-bahai-from-absolute
169 (calendar-absolute-from-gregorian today
))))))
170 (completion-ignore-case t
)
173 "Baha'i calendar month name: "
175 (append bahai-calendar-month-name-array nil
))
177 (calendar-make-alist bahai-calendar-month-name-array
179 (day (calendar-read "Baha'i calendar day (1-19): "
180 '(lambda (x) (and (< 0 x
) (<= x
19))))))
181 (list (list month day year
))))
183 (defun diary-bahai-date ()
184 "Baha'i calendar equivalent of date diary entry."
185 (format "Baha'i date: %s" (calendar-bahai-date-string date
)))
187 (defun holiday-bahai (month day string
)
188 "Holiday on MONTH, DAY (Baha'i) called STRING.
189 If MONTH, DAY (Baha'i) is visible, the value returned is corresponding
190 Gregorian date in the form of the list (((month day year) STRING)). Returns
191 nil if it is not visible in the current calendar window."
192 (let* ((bahai-date (calendar-bahai-from-absolute
193 (calendar-absolute-from-gregorian
194 (list displayed-month
15 displayed-year
))))
195 (m (extract-calendar-month bahai-date
))
196 (y (extract-calendar-year bahai-date
))
199 nil
;; Baha'i calendar doesn't apply.
200 (increment-calendar-month m y
(- 10 month
))
201 (if (> m
7) ;; Baha'i date might be visible
202 (let ((date (calendar-gregorian-from-absolute
203 (calendar-absolute-from-bahai (list month day y
)))))
204 (if (calendar-date-is-visible-p date
)
205 (list (list date string
))))))))
207 (defun list-bahai-diary-entries ()
208 "Add any Baha'i date entries from the diary file to `diary-entries-list'.
209 Baha'i date diary entries must be prefaced by an
210 `bahai-diary-entry-symbol' (normally a `B'). The same diary date
211 forms govern the style of the Baha'i calendar entries, except that the
212 Baha'i month names must be given numerically. The Baha'i months are
213 numbered from 1 to 19 with Baha being 1 and 19 being `Ala. If a
214 Baha'i date diary entry begins with a `diary-nonmarking-symbol', the
215 entry will appear in the diary listing, but will not be marked in the
216 calendar. This function is provided for use with the
217 `nongregorian-diary-listing-hook'."
219 (let ((buffer-read-only nil
)
220 (diary-modified (buffer-modified-p))
221 (gdate original-date
)
222 (mark (regexp-quote diary-nonmarking-symbol
)))
223 (calendar-for-loop i from
1 to number do
224 (let* ((d diary-date-forms
)
225 (bdate (calendar-bahai-from-absolute
226 (calendar-absolute-from-gregorian gdate
)))
227 (month (extract-calendar-month bdate
))
228 (day (extract-calendar-day bdate
))
229 (year (extract-calendar-year bdate
)))
232 ((date-form (if (equal (car (car d
)) 'backup
)
235 (backup (equal (car (car d
)) 'backup
))
238 (calendar-day-name gdate
) "\\|"
239 (substring (calendar-day-name gdate
) 0 3) ".?"))
240 (calendar-month-name-array
241 bahai-calendar-month-name-array
)
245 (calendar-month-name month
)))
246 (month (concat "\\*\\|0*" (int-to-string month
)))
247 (day (concat "\\*\\|0*" (int-to-string day
)))
250 "\\*\\|0*" (int-to-string year
)
251 (if abbreviated-calendar-year
252 (concat "\\|" (int-to-string (% year
100)))
256 "\\(\\`\\|\^M\\|\n\\)" mark
"?"
257 (regexp-quote bahai-diary-entry-symbol
)
259 (mapconcat 'eval date-form
"\\)\\(")
261 (case-fold-search t
))
262 (goto-char (point-min))
263 (while (re-search-forward regexp nil t
)
264 (if backup
(re-search-backward "\\<" nil t
))
265 (if (and (or (char-equal (preceding-char) ?\^M
)
266 (char-equal (preceding-char) ?
\n))
267 (not (looking-at " \\|\^I")))
268 ;; Diary entry that consists only of date.
270 ;; Found a nonempty diary entry--make it visible and
271 ;; add it to the list.
272 (let ((entry-start (point))
274 (re-search-backward "\^M\\|\n\\|\\`")
275 (setq date-start
(point))
276 (re-search-forward "\^M\\|\n" nil t
2)
277 (while (looking-at " \\|\^I")
278 (re-search-forward "\^M\\|\n" nil t
))
280 (subst-char-in-region date-start
(point) ?\^M ?
\n t
)
283 (buffer-substring-no-properties entry-start
(point))
284 (buffer-substring-no-properties
285 (1+ date-start
) (1- entry-start
)))))))
288 (calendar-gregorian-from-absolute
289 (1+ (calendar-absolute-from-gregorian gdate
)))))
290 (set-buffer-modified-p diary-modified
))
291 (goto-char (point-min))))
293 (defun mark-bahai-diary-entries ()
294 "Mark days in the calendar window that have Baha'i date diary entries.
295 Each entry in diary-file (or included files) visible in the calendar
296 window is marked. Baha'i date entries are prefaced by a
297 bahai-diary-entry-symbol \(normally a B`I'). The same
298 diary-date-forms govern the style of the Baha'i calendar entries,
299 except that the Baha'i month names must be spelled in full. The
300 Baha'i months are numbered from 1 to 12 with Baha being 1 and 12 being
301 `Ala. Baha'i date diary entries that begin with a
302 diary-nonmarking-symbol will not be marked in the calendar. This
303 function is provided for use as part of the
304 nongregorian-diary-marking-hook."
305 (let ((d diary-date-forms
))
308 ((date-form (if (equal (car (car d
)) 'backup
)
310 (car d
)));; ignore 'backup directive
311 (dayname (diary-name-pattern calendar-day-name-array
))
314 (diary-name-pattern bahai-calendar-month-name-array t
)
316 (month "[0-9]+\\|\\*")
318 (year "[0-9]+\\|\\*")
319 (l (length date-form
))
320 (d-name-pos (- l
(length (memq 'dayname date-form
))))
321 (d-name-pos (if (/= l d-name-pos
) (+ 2 d-name-pos
)))
322 (m-name-pos (- l
(length (memq 'monthname date-form
))))
323 (m-name-pos (if (/= l m-name-pos
) (+ 2 m-name-pos
)))
324 (d-pos (- l
(length (memq 'day date-form
))))
325 (d-pos (if (/= l d-pos
) (+ 2 d-pos
)))
326 (m-pos (- l
(length (memq 'month date-form
))))
327 (m-pos (if (/= l m-pos
) (+ 2 m-pos
)))
328 (y-pos (- l
(length (memq 'year date-form
))))
329 (y-pos (if (/= l y-pos
) (+ 2 y-pos
)))
332 "\\(\\`\\|\^M\\|\n\\)"
333 (regexp-quote bahai-diary-entry-symbol
)
335 (mapconcat 'eval date-form
"\\)\\(")
337 (case-fold-search t
))
338 (goto-char (point-min))
339 (while (re-search-forward regexp nil t
)
343 (match-beginning d-name-pos
)
344 (match-end d-name-pos
))))
348 (match-beginning m-name-pos
)
349 (match-end m-name-pos
))))
350 (mm (string-to-number
353 (match-beginning m-pos
)
356 (dd (string-to-number
359 (match-beginning d-pos
)
364 (match-beginning y-pos
)
368 (if (and (= (length y-str
) 2)
369 abbreviated-calendar-year
)
371 (extract-calendar-year
372 (calendar-bahai-from-absolute
373 (calendar-absolute-from-gregorian
374 (calendar-current-date)))))
375 (y (+ (string-to-number y-str
)
376 (* 100 (/ current-y
100)))))
377 (if (> (- y current-y
) 50)
379 (if (> (- current-y y
) 50)
382 (string-to-number y-str
)))))
384 (mark-calendar-days-named
385 (cdr (assoc-string (substring dd-name
0 3)
387 calendar-day-name-array
389 '(lambda (x) (substring x
0 3)))
392 (if (string-equal mm-name
"*")
398 bahai-calendar-month-name-array
)
400 (mark-bahai-calendar-date-pattern mm dd yy
)))))
403 (defun mark-bahai-calendar-date-pattern (month day year
)
404 "Mark dates in calendar window that conform to Baha'i date MONTH/DAY/YEAR.
405 A value of 0 in any position is a wildcard."
407 (set-buffer calendar-buffer
)
408 (if (and (/= 0 month
) (/= 0 day
))
410 ;; Fully specified Baha'i date.
411 (let ((date (calendar-gregorian-from-absolute
412 (calendar-absolute-from-bahai
413 (list month day year
)))))
414 (if (calendar-date-is-visible-p date
)
415 (mark-visible-calendar-date date
)))
416 ;; Month and day in any year--this taken from the holiday stuff.
417 (let* ((bahai-date (calendar-bahai-from-absolute
418 (calendar-absolute-from-gregorian
419 (list displayed-month
15 displayed-year
))))
420 (m (extract-calendar-month bahai-date
))
421 (y (extract-calendar-year bahai-date
))
424 nil
;; Baha'i calendar doesn't apply.
425 (increment-calendar-month m y
(- 10 month
))
426 (if (> m
7);; Baha'i date might be visible
427 (let ((date (calendar-gregorian-from-absolute
428 (calendar-absolute-from-bahai
429 (list month day y
)))))
430 (if (calendar-date-is-visible-p date
)
431 (mark-visible-calendar-date date
)))))))
432 ;; Not one of the simple cases--check all visible dates for match.
433 ;; Actually, the following code takes care of ALL of the cases, but
434 ;; it's much too slow to be used for the simple (common) cases.
435 (let ((m displayed-month
)
439 (increment-calendar-month m y -
1)
441 (calendar-absolute-from-gregorian
443 (increment-calendar-month m y
2)
445 (calendar-absolute-from-gregorian
446 (list m
(calendar-last-day-of-month m y
) y
)))
447 (calendar-for-loop date from first-date to last-date do
448 (let* ((b-date (calendar-bahai-from-absolute date
))
449 (i-month (extract-calendar-month b-date
))
450 (i-day (extract-calendar-day b-date
))
451 (i-year (extract-calendar-year b-date
)))
452 (and (or (zerop month
)
458 (mark-visible-calendar-date
459 (calendar-gregorian-from-absolute date
)))))))))
461 (defun insert-bahai-diary-entry (arg)
462 "Insert a diary entry.
463 For the Baha'i date corresponding to the date indicated by point.
464 Prefix arg will make the entry nonmarking."
466 (let* ((calendar-month-name-array bahai-calendar-month-name-array
))
469 bahai-diary-entry-symbol
470 (calendar-date-string
471 (calendar-bahai-from-absolute
472 (calendar-absolute-from-gregorian
473 (calendar-cursor-to-date t
)))
477 (defun insert-monthly-bahai-diary-entry (arg)
478 "Insert a monthly diary entry.
479 For the day of the Baha'i month corresponding to the date indicated by point.
480 Prefix arg will make the entry nonmarking."
482 (let* ((calendar-date-display-form
483 (if european-calendar-style
'(day " * ") '("* " day
)))
484 (calendar-month-name-array bahai-calendar-month-name-array
))
487 bahai-diary-entry-symbol
488 (calendar-date-string
489 (calendar-bahai-from-absolute
490 (calendar-absolute-from-gregorian
491 (calendar-cursor-to-date t
)))))
494 (defun insert-yearly-bahai-diary-entry (arg)
495 "Insert an annual diary entry.
496 For the day of the Baha'i year corresponding to the date indicated by point.
497 Prefix arg will make the entry nonmarking."
499 (let* ((calendar-date-display-form
500 (if european-calendar-style
502 '(monthname " " day
)))
503 (calendar-month-name-array bahai-calendar-month-name-array
))
506 bahai-diary-entry-symbol
507 (calendar-date-string
508 (calendar-bahai-from-absolute
509 (calendar-absolute-from-gregorian
510 (calendar-cursor-to-date t
)))))
515 ;;; arch-tag: c1cb1d67-862a-4264-a01c-41cb4df01f14
516 ;;; cal-bahai.el ends here