1 ;;; cal-bahai.el --- calendar functions for the Bahá'í calendar. -*- coding: utf-8 -*-
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
))))
121 (defun calendar-bahai-date-string (&optional date
)
122 "String of Bahá'í 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 calendar-bahai-month-name-array
(1- m
))))
137 (if (calendar-bahai-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-bahai-print-date ()
147 "Show the Bahá'í calendar equivalent of the selected date."
149 (message "Bahá'í date: %s"
150 (calendar-bahai-date-string (calendar-cursor-to-date t
))))
152 (defun calendar-bahai-goto-date (date &optional noecho
)
153 "Move cursor to Bahá'í date DATE.
154 Echo Bahá'í date unless NOECHO is t."
155 (interactive (calendar-bahai-prompt-for-date))
156 (calendar-goto-date (calendar-gregorian-from-absolute
157 (calendar-absolute-from-bahai date
)))
158 (or noecho
(calendar-bahai-print-date)))
160 (defun calendar-bahai-prompt-for-date ()
161 "Ask for a Bahá'í date."
162 (let* ((today (calendar-current-date))
164 "Bahá'í 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 "Bahá'í calendar month name: "
175 (append calendar-bahai-month-name-array nil
))
177 (calendar-make-alist calendar-bahai-month-name-array
179 (day (calendar-read "Bahá'í calendar day (1-19): "
180 '(lambda (x) (and (< 0 x
) (<= x
19))))))
181 (list (list month day year
))))
183 (defun diary-bahai-date ()
184 "Bahá'í calendar equivalent of date diary entry."
185 (format "Bahá'í date: %s" (calendar-bahai-date-string date
)))
187 (defun holiday-bahai (month day string
)
188 "Holiday on MONTH, DAY (Bahá'í) called STRING.
189 If MONTH, DAY (Bahá'í) 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
;; Bahá'í calendar doesn't apply.
200 (increment-calendar-month m y
(- 10 month
))
201 (if (> m
7) ;; Bahá'í 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 ;; 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
))
211 (defun diary-bahai-list-entries ()
212 "Add any Bahá'í date entries from the diary file to `diary-entries-list'.
213 Bahá'í date diary entries must be prefaced by an
214 `bahai-diary-entry-symbol' (normally a `B'). The same diary date
215 forms govern the style of the Bahá'í calendar entries, except that the
216 Bahá'í month names must be given numerically. The Bahá'í months are
217 numbered from 1 to 19 with Bahá being 1 and 19 being `Alá. If a
218 Bahá'í date diary entry begins with a `diary-nonmarking-symbol', the
219 entry will appear in the diary listing, but will not be marked in the
220 calendar. This function is provided for use with the
221 `nongregorian-diary-listing-hook'."
223 (let ((buffer-read-only nil
)
224 (diary-modified (buffer-modified-p))
225 (gdate original-date
)
226 (mark (regexp-quote diary-nonmarking-symbol
)))
227 (dotimes (idummy number
)
228 (let* ((d diary-date-forms
)
229 (bdate (calendar-bahai-from-absolute
230 (calendar-absolute-from-gregorian gdate
)))
231 (month (extract-calendar-month bdate
))
232 (day (extract-calendar-day bdate
))
233 (year (extract-calendar-year bdate
)))
236 ((date-form (if (equal (car (car d
)) 'backup
)
239 (backup (equal (car (car d
)) 'backup
))
242 (calendar-day-name gdate
) "\\|"
243 (substring (calendar-day-name gdate
) 0 3) ".?"))
244 (calendar-month-name-array
245 calendar-bahai-month-name-array
)
249 (calendar-month-name month
)))
250 (month (concat "\\*\\|0*" (int-to-string month
)))
251 (day (concat "\\*\\|0*" (int-to-string day
)))
254 "\\*\\|0*" (int-to-string year
)
255 (if abbreviated-calendar-year
256 (concat "\\|" (int-to-string (% year
100)))
260 "\\(\\`\\|\^M\\|\n\\)" mark
"?"
261 (regexp-quote bahai-diary-entry-symbol
)
263 (mapconcat 'eval date-form
"\\)\\(")
265 (case-fold-search t
))
266 (goto-char (point-min))
267 (while (re-search-forward regexp nil t
)
268 (if backup
(re-search-backward "\\<" nil t
))
269 (if (and (or (char-equal (preceding-char) ?\^M
)
270 (char-equal (preceding-char) ?
\n))
271 (not (looking-at " \\|\^I")))
272 ;; Diary entry that consists only of date.
274 ;; Found a nonempty diary entry--make it visible and
275 ;; add it to the list.
276 (let ((entry-start (point))
278 (re-search-backward "\^M\\|\n\\|\\`")
279 (setq date-start
(point))
280 (re-search-forward "\^M\\|\n" nil t
2)
281 (while (looking-at " \\|\^I")
282 (re-search-forward "\^M\\|\n" nil t
))
284 (subst-char-in-region date-start
(point) ?\^M ?
\n t
)
287 (buffer-substring-no-properties entry-start
(point))
288 (buffer-substring-no-properties
289 (1+ date-start
) (1- entry-start
)))))))
292 (calendar-gregorian-from-absolute
293 (1+ (calendar-absolute-from-gregorian gdate
)))))
294 (set-buffer-modified-p diary-modified
))
295 (goto-char (point-min))))
297 (declare-function diary-name-pattern
"diary-lib"
298 (string-array &optional abbrev-array paren
))
300 (declare-function mark-calendar-days-named
"diary-lib"
301 (dayname &optional color
))
303 (defun diary-bahai-mark-entries ()
304 "Mark days in the calendar window that have Bahá'í date diary entries.
305 Each entry in diary-file (or included files) visible in the calendar
306 window is marked. Bahá'í date entries are prefaced by a
307 bahai-diary-entry-symbol \(normally a B`I'). The same
308 diary-date-forms govern the style of the Bahá'í calendar entries,
309 except that the Bahá'í month names must be spelled in full. The
310 Bahá'í months are numbered from 1 to 12 with Bahá being 1 and 12 being
311 `Alá. Bahá'í date diary entries that begin with a
312 diary-nonmarking-symbol will not be marked in the calendar. This
313 function is provided for use as part of the
314 nongregorian-diary-marking-hook."
315 (let ((d diary-date-forms
))
318 ((date-form (if (equal (car (car d
)) 'backup
)
320 (car d
)));; ignore 'backup directive
321 (dayname (diary-name-pattern calendar-day-name-array
))
324 (diary-name-pattern calendar-bahai-month-name-array t
)
326 (month "[0-9]+\\|\\*")
328 (year "[0-9]+\\|\\*")
329 (l (length date-form
))
330 (d-name-pos (- l
(length (memq 'dayname date-form
))))
331 (d-name-pos (if (/= l d-name-pos
) (+ 2 d-name-pos
)))
332 (m-name-pos (- l
(length (memq 'monthname date-form
))))
333 (m-name-pos (if (/= l m-name-pos
) (+ 2 m-name-pos
)))
334 (d-pos (- l
(length (memq 'day date-form
))))
335 (d-pos (if (/= l d-pos
) (+ 2 d-pos
)))
336 (m-pos (- l
(length (memq 'month date-form
))))
337 (m-pos (if (/= l m-pos
) (+ 2 m-pos
)))
338 (y-pos (- l
(length (memq 'year date-form
))))
339 (y-pos (if (/= l y-pos
) (+ 2 y-pos
)))
342 "\\(\\`\\|\^M\\|\n\\)"
343 (regexp-quote bahai-diary-entry-symbol
)
345 (mapconcat 'eval date-form
"\\)\\(")
347 (case-fold-search t
))
348 (goto-char (point-min))
349 (while (re-search-forward regexp nil t
)
353 (match-beginning d-name-pos
)
354 (match-end d-name-pos
))))
358 (match-beginning m-name-pos
)
359 (match-end m-name-pos
))))
360 (mm (string-to-number
363 (match-beginning m-pos
)
366 (dd (string-to-number
369 (match-beginning d-pos
)
374 (match-beginning y-pos
)
378 (if (and (= (length y-str
) 2)
379 abbreviated-calendar-year
)
381 (extract-calendar-year
382 (calendar-bahai-from-absolute
383 (calendar-absolute-from-gregorian
384 (calendar-current-date)))))
385 (y (+ (string-to-number y-str
)
386 (* 100 (/ current-y
100)))))
387 (if (> (- y current-y
) 50)
389 (if (> (- current-y y
) 50)
392 (string-to-number y-str
)))))
394 (mark-calendar-days-named
395 (cdr (assoc-string (substring dd-name
0 3)
397 calendar-day-name-array
399 '(lambda (x) (substring x
0 3)))
402 (if (string-equal mm-name
"*")
408 calendar-bahai-month-name-array
)
410 (calendar-bahai-mark-date-pattern mm dd yy
)))))
413 (defun calendar-bahai-mark-date-pattern (month day year
)
414 "Mark dates in calendar window that conform to Bahá'í date MONTH/DAY/YEAR.
415 A value of 0 in any position is a wildcard."
417 (set-buffer calendar-buffer
)
418 (if (and (/= 0 month
) (/= 0 day
))
420 ;; Fully specified Bahá'í date.
421 (let ((date (calendar-gregorian-from-absolute
422 (calendar-absolute-from-bahai
423 (list month day year
)))))
424 (if (calendar-date-is-visible-p date
)
425 (mark-visible-calendar-date date
)))
426 ;; Month and day in any year--this taken from the holiday stuff.
427 (let* ((bahai-date (calendar-bahai-from-absolute
428 (calendar-absolute-from-gregorian
429 (list displayed-month
15 displayed-year
))))
430 (m (extract-calendar-month bahai-date
))
431 (y (extract-calendar-year bahai-date
))
434 nil
;; Bahá'í calendar doesn't apply.
435 (increment-calendar-month m y
(- 10 month
))
436 (if (> m
7);; Bahá'í date might be visible
437 (let ((date (calendar-gregorian-from-absolute
438 (calendar-absolute-from-bahai
439 (list month day y
)))))
440 (if (calendar-date-is-visible-p date
)
441 (mark-visible-calendar-date date
)))))))
442 ;; Not one of the simple cases--check all visible dates for match.
443 ;; Actually, the following code takes care of ALL of the cases, but
444 ;; it's much too slow to be used for the simple (common) cases.
445 (let ((m displayed-month
)
449 (increment-calendar-month m y -
1)
451 (calendar-absolute-from-gregorian
453 (increment-calendar-month m y
2)
455 (calendar-absolute-from-gregorian
456 (list m
(calendar-last-day-of-month m y
) y
)))
457 (calendar-for-loop date from first-date to last-date do
458 (let* ((b-date (calendar-bahai-from-absolute date
))
459 (i-month (extract-calendar-month b-date
))
460 (i-day (extract-calendar-day b-date
))
461 (i-year (extract-calendar-year b-date
)))
462 (and (or (zerop month
)
468 (mark-visible-calendar-date
469 (calendar-gregorian-from-absolute date
)))))))))
471 (defun diary-bahai-insert-entry (arg)
472 "Insert a diary entry.
473 For the Bahá'í date corresponding to the date indicated by point.
474 Prefix arg will make the entry nonmarking."
476 (let* ((calendar-month-name-array calendar-bahai-month-name-array
))
479 bahai-diary-entry-symbol
480 (calendar-date-string
481 (calendar-bahai-from-absolute
482 (calendar-absolute-from-gregorian
483 (calendar-cursor-to-date t
)))
487 (defun diary-bahai-insert-monthly-entry (arg)
488 "Insert a monthly diary entry.
489 For the day of the Bahá'í month corresponding to the date indicated by point.
490 Prefix arg will make the entry nonmarking."
492 (let* ((calendar-date-display-form
493 (if european-calendar-style
'(day " * ") '("* " day
)))
494 (calendar-month-name-array calendar-bahai-month-name-array
))
497 bahai-diary-entry-symbol
498 (calendar-date-string
499 (calendar-bahai-from-absolute
500 (calendar-absolute-from-gregorian
501 (calendar-cursor-to-date t
)))))
504 (defun diary-bahai-insert-yearly-entry (arg)
505 "Insert an annual diary entry.
506 For the day of the Bahá'í year corresponding to the date indicated by point.
507 Prefix arg will make the entry nonmarking."
509 (let* ((calendar-date-display-form
510 (if european-calendar-style
512 '(monthname " " day
)))
513 (calendar-month-name-array calendar-bahai-month-name-array
))
516 bahai-diary-entry-symbol
517 (calendar-date-string
518 (calendar-bahai-from-absolute
519 (calendar-absolute-from-gregorian
520 (calendar-cursor-to-date t
)))))
523 ;; Backward compatibility.
524 (define-obsolete-function-alias
525 'list-bahai-diary-entries
'diary-bahai-list-entries
"23.1")
526 (define-obsolete-function-alias
527 'mark-bahai-diary-entries
'diary-bahai-mark-entries
"23.1")
528 (define-obsolete-function-alias
529 'insert-bahai-diary-entry
'diary-bahai-insert-entry
"23.1")
530 (define-obsolete-function-alias
531 'insert-monthly-bahai-diary-entry
'diary-bahai-insert-monthly-entry
"23.1")
532 (define-obsolete-function-alias
533 'insert-yearly-bahai-diary-entry
'diary-bahai-insert-yearly-entry
"23.1")
534 (define-obsolete-function-alias
535 'mark-bahai-calendar-date-pattern
'calendar-bahai-mark-date-pattern
"23.1")
536 (define-obsolete-function-alias
537 'calendar-goto-bahai-date
'calendar-bahai-goto-date
"23.1")
538 (define-obsolete-function-alias
539 'calendar-print-bahai-date
'calendar-bahai-print-date
"23.1")
543 ;; arch-tag: c1cb1d67-862a-4264-a01c-41cb4df01f14
544 ;;; cal-bahai.el ends here