(number-chinese-months, calendar-chinese-from-absolute): Use nth, caar.
[emacs.git] / lisp / calendar / cal-bahai.el
blob6628baf224d6e2975b83ad585158f49574b31f41
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 'cal-julian)
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 19, 622 A.D. (Julian).")
69 (defun calendar-bahai-leap-year-p (year)
70 "True if YEAR is a leap year on the Bahá'í calendar."
71 (calendar-leap-year-p (+ year 1844)))
73 (defconst calendar-bahai-leap-base
74 (+ (/ 1844 4) (- (/ 1844 100)) (/ 1844 400))
75 "Used by `calendar-absolute-from-bahai'.")
77 (defun calendar-absolute-from-bahai (date)
78 "Compute absolute date from Bahá'í date DATE.
79 The absolute date is the number of days elapsed since the (imaginary)
80 Gregorian date Sunday, December 31, 1 BC."
81 (let* ((month (extract-calendar-month date))
82 (day (extract-calendar-day date))
83 (year (extract-calendar-year date))
84 (prior-years (+ (1- year) 1844))
85 (leap-days (- (+ (/ prior-years 4) ; leap days in prior years
86 (- (/ prior-years 100))
87 (/ prior-years 400))
88 calendar-bahai-leap-base)))
89 (+ (1- calendar-bahai-epoch) ; days before epoch
90 (* 365 (1- year)) ; days in prior years
91 leap-days
92 (calendar-sum m 1 (< m month) 19)
93 (if (= month 19) 4 0)
94 day))) ; days so far this month
96 (defun calendar-bahai-from-absolute (date)
97 "Bahá'í year corresponding to the absolute DATE."
98 (if (< date calendar-bahai-epoch)
99 (list 0 0 0) ; pre-Bahá'í date
100 (let* ((greg (calendar-gregorian-from-absolute date))
101 (year (+ (- (extract-calendar-year greg) 1844)
102 (if (or (> (extract-calendar-month greg) 3)
103 (and (= (extract-calendar-month greg) 3)
104 (>= (extract-calendar-day greg) 21)))
105 1 0)))
106 (month ; search forward from Baha
107 (1+ (calendar-sum m 1
108 (> date
109 (calendar-absolute-from-bahai
110 (list m 19 year)))
111 1)))
112 (day ; calculate the day by subtraction
113 (- date
114 (1- (calendar-absolute-from-bahai (list month 1 year))))))
115 (list month day year))))
117 ;;;###cal-autoload
118 (defun calendar-bahai-date-string (&optional date)
119 "String of Bahá'í date of Gregorian DATE.
120 Defaults to today's date if DATE is not given."
121 (let* ((bahai-date (calendar-bahai-from-absolute
122 (calendar-absolute-from-gregorian
123 (or date (calendar-current-date)))))
124 (y (extract-calendar-year bahai-date))
125 (m (extract-calendar-month bahai-date))
126 (d (extract-calendar-day bahai-date)))
127 (let ((monthname
128 (if (and (= m 19)
129 (<= d 0))
130 "Ayyám-i-Há"
131 (aref calendar-bahai-month-name-array (1- m))))
132 (day (int-to-string
133 (if (<= d 0)
134 (if (calendar-bahai-leap-year-p y)
135 (+ d 5)
136 (+ d 4))
137 d)))
138 (dayname nil)
139 (month (int-to-string m))
140 (year (int-to-string y)))
141 (mapconcat 'eval calendar-date-display-form ""))))
143 ;;;###cal-autoload
144 (defun calendar-bahai-print-date ()
145 "Show the Bahá'í calendar equivalent of the selected date."
146 (interactive)
147 (message "Bahá'í date: %s"
148 (calendar-bahai-date-string (calendar-cursor-to-date t))))
150 (defun calendar-bahai-prompt-for-date ()
151 "Ask for a Bahá'í date."
152 (let* ((today (calendar-current-date))
153 (year (calendar-read
154 "Bahá'í calendar year (not 0): "
155 (lambda (x) (not (zerop x)))
156 (int-to-string
157 (extract-calendar-year
158 (calendar-bahai-from-absolute
159 (calendar-absolute-from-gregorian today))))))
160 (completion-ignore-case t)
161 (month (cdr (assoc
162 (completing-read
163 "Bahá'í calendar month name: "
164 (mapcar 'list
165 (append calendar-bahai-month-name-array nil))
166 nil t)
167 (calendar-make-alist calendar-bahai-month-name-array
168 1))))
169 (day (calendar-read "Bahá'í calendar day (1-19): "
170 (lambda (x) (and (< 0 x) (<= x 19))))))
171 (list (list month day year))))
173 ;;;###cal-autoload
174 (defun calendar-bahai-goto-date (date &optional noecho)
175 "Move cursor to Bahá'í date DATE.
176 Echo Bahá'í date unless NOECHO is non-nil."
177 (interactive (calendar-bahai-prompt-for-date))
178 (calendar-goto-date (calendar-gregorian-from-absolute
179 (calendar-absolute-from-bahai date)))
180 (or noecho (calendar-bahai-print-date)))
182 (defvar displayed-month)
183 (defvar displayed-year)
185 ;;;###holiday-autoload
186 (defun holiday-bahai (month day string)
187 "Holiday on MONTH, DAY (Bahá'í) called STRING.
188 If MONTH, DAY (Bahá'í) is visible, the value returned is corresponding
189 Gregorian date in the form of the list (((month day year) STRING)). Returns
190 nil if it is not visible in the current calendar window."
191 (let* ((bahai-date (calendar-bahai-from-absolute
192 (calendar-absolute-from-gregorian
193 (list displayed-month 15 displayed-year))))
194 (m (extract-calendar-month bahai-date))
195 (y (extract-calendar-year bahai-date))
196 (date))
197 (unless (< m 1) ; Bahá'í calendar doesn't apply
198 (increment-calendar-month m y (- 10 month))
199 (if (> m 7) ; Bahá'í date might be visible
200 (let ((date (calendar-gregorian-from-absolute
201 (calendar-absolute-from-bahai (list month day y)))))
202 (if (calendar-date-is-visible-p date)
203 (list (list date string))))))))
205 (defvar number)
206 (defvar original-date)
208 ;; d-b-l-e should be called from diary code.
209 (declare-function add-to-diary-list "diary-lib"
210 (date string specifier &optional marker globcolor literal))
212 ;;;###diary-autoload
213 (defun diary-bahai-list-entries ()
214 "Add any Bahá'í date entries from the diary file to `diary-entries-list'.
215 Bahá'í date diary entries must be prefaced by `bahai-diary-entry-symbol'
216 \(normally a `B'). The same diary date forms govern the style of the
217 Bahá'í calendar entries, except that the Bahá'í month names must be given
218 numerically. The Bahá'í months are numbered from 1 to 19 with Bahá being
219 1 and 19 being `Alá. If a Bahá'í date diary entry begins with
220 `diary-nonmarking-symbol', the entry will appear in the diary listing, but
221 will not be marked in the calendar. This function is provided for use with
222 `nongregorian-diary-listing-hook'."
223 (if (< 0 number)
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* ((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))
234 backup)
235 (dolist (date-form diary-date-forms)
236 (if (setq backup (eq (car date-form) 'backup))
237 (setq date-form (cdr date-form)))
238 (let* ((dayname
239 (concat
240 (calendar-day-name gdate) "\\|"
241 (substring (calendar-day-name gdate) 0 3) ".?"))
242 (calendar-month-name-array
243 calendar-bahai-month-name-array)
244 (monthname
245 (concat
246 "\\*\\|"
247 (calendar-month-name month)))
248 (month (concat "\\*\\|0*" (int-to-string month)))
249 (day (concat "\\*\\|0*" (int-to-string day)))
250 (year
251 (concat
252 "\\*\\|0*" (int-to-string year)
253 (if abbreviated-calendar-year
254 (concat "\\|" (int-to-string (% year 100)))
255 "")))
256 ;; FIXME get rid of the ^M stuff.
257 (regexp
258 (concat
259 "\\(\\`\\|\^M\\|\n\\)" mark "?"
260 (regexp-quote bahai-diary-entry-symbol)
261 "\\("
262 (mapconcat 'eval date-form "\\)\\(")
263 "\\)"))
264 (case-fold-search t))
265 (goto-char (point-min))
266 (while (re-search-forward regexp nil t)
267 (if backup (re-search-backward "\\<" nil t))
268 (if (and (or (char-equal (preceding-char) ?\^M)
269 (char-equal (preceding-char) ?\n))
270 (not (looking-at " \\|\^I")))
271 ;; Diary entry that consists only of date.
272 (backward-char 1)
273 ;; Found a nonempty diary entry--make it visible and
274 ;; add it to the list.
275 (let ((entry-start (point))
276 (date-start))
277 (re-search-backward "\^M\\|\n\\|\\`")
278 (setq date-start (point))
279 (re-search-forward "\^M\\|\n" nil t 2)
280 (while (looking-at " \\|\^I")
281 (re-search-forward "\^M\\|\n" nil t))
282 (backward-char 1)
283 (subst-char-in-region date-start (point) ?\^M ?\n t)
284 (add-to-diary-list
285 gdate
286 (buffer-substring-no-properties entry-start (point))
287 (buffer-substring-no-properties
288 (1+ date-start) (1- entry-start)))))))))
289 (setq gdate
290 (calendar-gregorian-from-absolute
291 (1+ (calendar-absolute-from-gregorian gdate)))))
292 (set-buffer-modified-p diary-modified))
293 (goto-char (point-min))))
295 ;;;###diary-autoload
296 (defun calendar-bahai-mark-date-pattern (month day year)
297 "Mark dates in calendar window that conform to Bahá'í date MONTH/DAY/YEAR.
298 A value of 0 in any position is a wildcard."
299 (save-excursion
300 (set-buffer calendar-buffer)
301 (if (and (not (zerop month)) (not (zerop day)))
302 (if (not (zerop year))
303 ;; Fully specified Bahá'í date.
304 (let ((date (calendar-gregorian-from-absolute
305 (calendar-absolute-from-bahai
306 (list month day year)))))
307 (if (calendar-date-is-visible-p date)
308 (mark-visible-calendar-date date)))
309 ;; Month and day in any year--this taken from the holiday stuff.
310 (let* ((bahai-date (calendar-bahai-from-absolute
311 (calendar-absolute-from-gregorian
312 (list displayed-month 15 displayed-year))))
313 (m (extract-calendar-month bahai-date))
314 (y (extract-calendar-year bahai-date))
315 (date))
316 (unless (< m 1) ; Bahá'í calendar doesn't apply
317 (increment-calendar-month m y (- 10 month))
318 (if (> m 7) ; Bahá'í date might be visible
319 (let ((date (calendar-gregorian-from-absolute
320 (calendar-absolute-from-bahai
321 (list month day y)))))
322 (if (calendar-date-is-visible-p date)
323 (mark-visible-calendar-date date)))))))
324 ;; Not one of the simple cases--check all visible dates for match.
325 ;; Actually, the following code takes care of ALL of the cases, but
326 ;; it's much too slow to be used for the simple (common) cases.
327 (let ((m displayed-month)
328 (y displayed-year)
329 (first-date)
330 (last-date))
331 (increment-calendar-month m y -1)
332 (setq first-date
333 (calendar-absolute-from-gregorian
334 (list m 1 y)))
335 (increment-calendar-month m y 2)
336 (setq last-date
337 (calendar-absolute-from-gregorian
338 (list m (calendar-last-day-of-month m y) y)))
339 (calendar-for-loop date from first-date to last-date do
340 (let* ((b-date (calendar-bahai-from-absolute date))
341 (i-month (extract-calendar-month b-date))
342 (i-day (extract-calendar-day b-date))
343 (i-year (extract-calendar-year b-date)))
344 (and (or (zerop month)
345 (= month i-month))
346 (or (zerop day)
347 (= day i-day))
348 (or (zerop year)
349 (= year i-year))
350 (mark-visible-calendar-date
351 (calendar-gregorian-from-absolute
352 date)))))))))
354 (declare-function diary-name-pattern "diary-lib"
355 (string-array &optional abbrev-array paren))
357 (declare-function mark-calendar-days-named "diary-lib"
358 (dayname &optional color))
360 ;;;###diary-autoload
361 (defun diary-bahai-mark-entries ()
362 "Mark days in the calendar window that have Bahá'í date diary entries.
363 Each entry in `diary-file' (or included files) visible in the calendar
364 window is marked. Bahá'í date entries are prefaced by
365 `bahai-diary-entry-symbol' (normally a \"B\"). The same
366 `diary-date-forms' govern the style of the Bahá'í calendar entries,
367 except that the Bahá'í month names must be spelled in full. The
368 Bahá'í months are numbered from 1 to 12 with Bahá being 1 and 12 being
369 `Alá. Bahá'í date diary entries that begin with `diary-nonmarking-symbol'
370 will not be marked in the calendar. This function is provided for use as
371 part of `nongregorian-diary-marking-hook'."
372 (let ((dayname (diary-name-pattern calendar-day-name-array))
373 (monthname
374 (concat
375 (diary-name-pattern calendar-bahai-month-name-array t)
376 "\\|\\*"))
377 (month "[0-9]+\\|\\*")
378 (day "[0-9]+\\|\\*")
379 (year "[0-9]+\\|\\*")
380 (case-fold-search t))
381 (dolist (date-form diary-date-forms)
382 (if (eq (car date-form) 'backup) ; ignore 'backup directive
383 (setq date-form (cdr date-form)))
384 (let* ((l (length date-form))
385 (d-name-pos (- l (length (memq 'dayname date-form))))
386 (d-name-pos (if (/= l d-name-pos) (+ 2 d-name-pos)))
387 (m-name-pos (- l (length (memq 'monthname date-form))))
388 (m-name-pos (if (/= l m-name-pos) (+ 2 m-name-pos)))
389 (d-pos (- l (length (memq 'day date-form))))
390 (d-pos (if (/= l d-pos) (+ 2 d-pos)))
391 (m-pos (- l (length (memq 'month date-form))))
392 (m-pos (if (/= l m-pos) (+ 2 m-pos)))
393 (y-pos (- l (length (memq 'year date-form))))
394 (y-pos (if (/= l y-pos) (+ 2 y-pos)))
395 (regexp
396 (concat
397 "\\(\\`\\|\^M\\|\n\\)"
398 (regexp-quote bahai-diary-entry-symbol)
399 "\\("
400 (mapconcat 'eval date-form "\\)\\(")
401 "\\)")))
402 (goto-char (point-min))
403 (while (re-search-forward regexp nil t)
404 (let* ((dd-name
405 (if d-name-pos
406 (buffer-substring
407 (match-beginning d-name-pos)
408 (match-end d-name-pos))))
409 (mm-name
410 (if m-name-pos
411 (buffer-substring
412 (match-beginning m-name-pos)
413 (match-end m-name-pos))))
414 (mm (string-to-number
415 (if m-pos
416 (buffer-substring
417 (match-beginning m-pos)
418 (match-end m-pos))
419 "")))
420 (dd (string-to-number
421 (if d-pos
422 (buffer-substring
423 (match-beginning d-pos)
424 (match-end d-pos))
425 "")))
426 (y-str (if y-pos
427 (buffer-substring
428 (match-beginning y-pos)
429 (match-end y-pos))))
430 (yy (if (not y-str)
432 (if (and (= (length y-str) 2)
433 abbreviated-calendar-year)
434 (let* ((current-y
435 (extract-calendar-year
436 (calendar-bahai-from-absolute
437 (calendar-absolute-from-gregorian
438 (calendar-current-date)))))
439 (y (+ (string-to-number y-str)
440 (* 100 (/ current-y 100)))))
441 (if (> (- y current-y) 50)
442 (- y 100)
443 (if (> (- current-y y) 50)
444 (+ y 100)
445 y)))
446 (string-to-number y-str)))))
447 (if dd-name
448 (mark-calendar-days-named
449 (cdr (assoc-string (substring dd-name 0 3)
450 (calendar-make-alist
451 calendar-day-name-array
453 (lambda (x) (substring x 0 3)))
454 t)))
455 (if mm-name
456 (if (string-equal mm-name "*")
457 (setq mm 0)
458 (setq mm
459 (cdr (assoc-string
460 mm-name
461 (calendar-make-alist
462 calendar-bahai-month-name-array)
463 t)))))
464 (calendar-bahai-mark-date-pattern mm dd yy))))))))
466 ;;;###cal-autoload
467 (defun diary-bahai-insert-entry (arg)
468 "Insert a diary entry.
469 For the Bahá'í date corresponding to the date indicated by point.
470 Prefix argument ARG makes the entry nonmarking."
471 (interactive "P")
472 (let* ((calendar-month-name-array calendar-bahai-month-name-array))
473 (make-diary-entry
474 (concat
475 bahai-diary-entry-symbol
476 (calendar-date-string
477 (calendar-bahai-from-absolute
478 (calendar-absolute-from-gregorian
479 (calendar-cursor-to-date t)))
480 nil t))
481 arg)))
483 ;;;###cal-autoload
484 (defun diary-bahai-insert-monthly-entry (arg)
485 "Insert a monthly diary entry.
486 For the day of the Bahá'í month corresponding to the date indicated by point.
487 Prefix argument ARG makes the entry nonmarking."
488 (interactive "P")
489 (let* ((calendar-date-display-form
490 (if european-calendar-style '(day " * ") '("* " day )))
491 (calendar-month-name-array calendar-bahai-month-name-array))
492 (make-diary-entry
493 (concat
494 bahai-diary-entry-symbol
495 (calendar-date-string
496 (calendar-bahai-from-absolute
497 (calendar-absolute-from-gregorian
498 (calendar-cursor-to-date t)))))
499 arg)))
501 ;;;###cal-autoload
502 (defun diary-bahai-insert-yearly-entry (arg)
503 "Insert an annual diary entry.
504 For the day of the Bahá'í year corresponding to the date indicated by point.
505 Prefix argument ARG will make the entry nonmarking."
506 (interactive "P")
507 (let* ((calendar-date-display-form
508 (if european-calendar-style
509 '(day " " monthname)
510 '(monthname " " day)))
511 (calendar-month-name-array calendar-bahai-month-name-array))
512 (make-diary-entry
513 (concat
514 bahai-diary-entry-symbol
515 (calendar-date-string
516 (calendar-bahai-from-absolute
517 (calendar-absolute-from-gregorian
518 (calendar-cursor-to-date t)))))
519 arg)))
521 (defvar date)
523 ;; To be called from list-sexp-diary-entries, where DATE is bound.
524 ;;;###diary-autoload
525 (defun diary-bahai-date ()
526 "Bahá'í calendar equivalent of date diary entry."
527 (format "Bahá'í date: %s" (calendar-bahai-date-string date)))
530 ;; Backward compatibility.
531 (define-obsolete-function-alias
532 'list-bahai-diary-entries 'diary-bahai-list-entries "23.1")
533 (define-obsolete-function-alias
534 'mark-bahai-diary-entries 'diary-bahai-mark-entries "23.1")
535 (define-obsolete-function-alias
536 'insert-bahai-diary-entry 'diary-bahai-insert-entry "23.1")
537 (define-obsolete-function-alias
538 'insert-monthly-bahai-diary-entry 'diary-bahai-insert-monthly-entry "23.1")
539 (define-obsolete-function-alias
540 'insert-yearly-bahai-diary-entry 'diary-bahai-insert-yearly-entry "23.1")
541 (define-obsolete-function-alias
542 'mark-bahai-calendar-date-pattern 'calendar-bahai-mark-date-pattern "23.1")
543 (define-obsolete-function-alias
544 'calendar-goto-bahai-date 'calendar-bahai-goto-date "23.1")
545 (define-obsolete-function-alias
546 'calendar-print-bahai-date 'calendar-bahai-print-date "23.1")
548 (provide 'cal-bahai)
550 ;; Local Variables:
551 ;; coding: utf-8
552 ;; End:
554 ;; arch-tag: c1cb1d67-862a-4264-a01c-41cb4df01f14
555 ;;; cal-bahai.el ends here