*** empty log message ***
[bpt/emacs.git] / lisp / calendar / cal-bahai.el
1 ;;; cal-bahai.el --- calendar functions for the Bahá'í calendar. -*- coding: utf-8 -*-
2
3 ;; Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 ;; Free Software Foundation, Inc.
5
6 ;; Author: John Wiegley <johnw@gnu.org>
7 ;; Keywords: calendar
8 ;; Human-Keywords: Bahá'í calendar, Bahá'í, Baha'i, Bahai, calendar, diary
9
10 ;; This file is part of GNU Emacs.
11
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.
16
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.
21
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.
26
27 ;;; Commentary:
28
29 ;; This collection of functions implements the features of calendar.el
30 ;; and diary.el that deal with the Bahá'í calendar.
31
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).
40
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".
46
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.
51
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.
55
56 ;;; Code:
57
58 (defvar date)
59 (defvar displayed-month)
60 (defvar displayed-year)
61 (defvar number)
62 (defvar original-date)
63
64 (require 'cal-julian)
65
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á"])
70
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).")
73
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)))
77
78 (defconst calendar-bahai-leap-base
79 (+ (/ 1844 4) (- (/ 1844 100)) (/ 1844 400)))
80
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))
91 (/ prior-years 400))
92 calendar-bahai-leap-base)))
93 (+ (1- calendar-bahai-epoch) ; Days before epoch
94 (* 365 (1- year)) ; Days in prior years.
95 leap-days
96 (calendar-sum m 1 (< m month) 19)
97 (if (= month 19) 4 0)
98 day))) ; Days so far this month.
99
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)))
109 1 0)))
110 (month ;; Search forward from Baha.
111 (1+ (calendar-sum m 1
112 (> date
113 (calendar-absolute-from-bahai
114 (list m 19 year)))
115 1)))
116 (day ;; Calculate the day by subtraction.
117 (- date
118 (1- (calendar-absolute-from-bahai (list month 1 year))))))
119 (list month day year))))
120
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)))
130 (let ((monthname
131 (if (and (= m 19)
132 (<= d 0))
133 "Ayyám-i-Há"
134 (aref calendar-bahai-month-name-array (1- m))))
135 (day (int-to-string
136 (if (<= d 0)
137 (if (calendar-bahai-leap-year-p y)
138 (+ d 5)
139 (+ d 4))
140 d)))
141 (dayname nil)
142 (month (int-to-string m))
143 (year (int-to-string y)))
144 (mapconcat 'eval calendar-date-display-form ""))))
145
146 (defun calendar-bahai-print-date ()
147 "Show the Bahá'í calendar equivalent of the selected date."
148 (interactive)
149 (message "Bahá'í date: %s"
150 (calendar-bahai-date-string (calendar-cursor-to-date t))))
151
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)))
159
160 (defun calendar-bahai-prompt-for-date ()
161 "Ask for a Bahá'í date."
162 (let* ((today (calendar-current-date))
163 (year (calendar-read
164 "Bahá'í calendar year (not 0): "
165 '(lambda (x) (/= x 0))
166 (int-to-string
167 (extract-calendar-year
168 (calendar-bahai-from-absolute
169 (calendar-absolute-from-gregorian today))))))
170 (completion-ignore-case t)
171 (month (cdr (assoc
172 (completing-read
173 "Bahá'í calendar month name: "
174 (mapcar 'list
175 (append calendar-bahai-month-name-array nil))
176 nil t)
177 (calendar-make-alist calendar-bahai-month-name-array
178 1))))
179 (day (calendar-read "Bahá'í calendar day (1-19): "
180 '(lambda (x) (and (< 0 x) (<= x 19))))))
181 (list (list month day year))))
182
183 (defun diary-bahai-date ()
184 "Bahá'í calendar equivalent of date diary entry."
185 (format "Bahá'í date: %s" (calendar-bahai-date-string date)))
186
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))
197 (date))
198 (if (< m 1)
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))))))))
206
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))
210
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'."
222 (if (< 0 number)
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)))
234 (while d
235 (let*
236 ((date-form (if (equal (car (car d)) 'backup)
237 (cdr (car d))
238 (car d)))
239 (backup (equal (car (car d)) 'backup))
240 (dayname
241 (concat
242 (calendar-day-name gdate) "\\|"
243 (substring (calendar-day-name gdate) 0 3) ".?"))
244 (calendar-month-name-array
245 calendar-bahai-month-name-array)
246 (monthname
247 (concat
248 "\\*\\|"
249 (calendar-month-name month)))
250 (month (concat "\\*\\|0*" (int-to-string month)))
251 (day (concat "\\*\\|0*" (int-to-string day)))
252 (year
253 (concat
254 "\\*\\|0*" (int-to-string year)
255 (if abbreviated-calendar-year
256 (concat "\\|" (int-to-string (% year 100)))
257 "")))
258 (regexp
259 (concat
260 "\\(\\`\\|\^M\\|\n\\)" mark "?"
261 (regexp-quote bahai-diary-entry-symbol)
262 "\\("
263 (mapconcat 'eval date-form "\\)\\(")
264 "\\)"))
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.
273 (backward-char 1)
274 ;; Found a nonempty diary entry--make it visible and
275 ;; add it to the list.
276 (let ((entry-start (point))
277 (date-start))
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))
283 (backward-char 1)
284 (subst-char-in-region date-start (point) ?\^M ?\n t)
285 (add-to-diary-list
286 gdate
287 (buffer-substring-no-properties entry-start (point))
288 (buffer-substring-no-properties
289 (1+ date-start) (1- entry-start)))))))
290 (setq d (cdr d))))
291 (setq gdate
292 (calendar-gregorian-from-absolute
293 (1+ (calendar-absolute-from-gregorian gdate)))))
294 (set-buffer-modified-p diary-modified))
295 (goto-char (point-min))))
296
297 (declare-function diary-name-pattern "diary-lib"
298 (string-array &optional abbrev-array paren))
299
300 (declare-function mark-calendar-days-named "diary-lib"
301 (dayname &optional color))
302
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))
316 (while d
317 (let*
318 ((date-form (if (equal (car (car d)) 'backup)
319 (cdr (car d))
320 (car d)));; ignore 'backup directive
321 (dayname (diary-name-pattern calendar-day-name-array))
322 (monthname
323 (concat
324 (diary-name-pattern calendar-bahai-month-name-array t)
325 "\\|\\*"))
326 (month "[0-9]+\\|\\*")
327 (day "[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)))
340 (regexp
341 (concat
342 "\\(\\`\\|\^M\\|\n\\)"
343 (regexp-quote bahai-diary-entry-symbol)
344 "\\("
345 (mapconcat 'eval date-form "\\)\\(")
346 "\\)"))
347 (case-fold-search t))
348 (goto-char (point-min))
349 (while (re-search-forward regexp nil t)
350 (let* ((dd-name
351 (if d-name-pos
352 (buffer-substring
353 (match-beginning d-name-pos)
354 (match-end d-name-pos))))
355 (mm-name
356 (if m-name-pos
357 (buffer-substring
358 (match-beginning m-name-pos)
359 (match-end m-name-pos))))
360 (mm (string-to-number
361 (if m-pos
362 (buffer-substring
363 (match-beginning m-pos)
364 (match-end m-pos))
365 "")))
366 (dd (string-to-number
367 (if d-pos
368 (buffer-substring
369 (match-beginning d-pos)
370 (match-end d-pos))
371 "")))
372 (y-str (if y-pos
373 (buffer-substring
374 (match-beginning y-pos)
375 (match-end y-pos))))
376 (yy (if (not y-str)
377 0
378 (if (and (= (length y-str) 2)
379 abbreviated-calendar-year)
380 (let* ((current-y
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)
388 (- y 100)
389 (if (> (- current-y y) 50)
390 (+ y 100)
391 y)))
392 (string-to-number y-str)))))
393 (if dd-name
394 (mark-calendar-days-named
395 (cdr (assoc-string (substring dd-name 0 3)
396 (calendar-make-alist
397 calendar-day-name-array
398 0
399 '(lambda (x) (substring x 0 3)))
400 t)))
401 (if mm-name
402 (if (string-equal mm-name "*")
403 (setq mm 0)
404 (setq mm
405 (cdr (assoc-string
406 mm-name
407 (calendar-make-alist
408 calendar-bahai-month-name-array)
409 t)))))
410 (calendar-bahai-mark-date-pattern mm dd yy)))))
411 (setq d (cdr d)))))
412
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."
416 (save-excursion
417 (set-buffer calendar-buffer)
418 (if (and (/= 0 month) (/= 0 day))
419 (if (/= 0 year)
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))
432 (date))
433 (if (< m 1)
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)
446 (y displayed-year)
447 (first-date)
448 (last-date))
449 (increment-calendar-month m y -1)
450 (setq first-date
451 (calendar-absolute-from-gregorian
452 (list m 1 y)))
453 (increment-calendar-month m y 2)
454 (setq last-date
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)
463 (= month i-month))
464 (or (zerop day)
465 (= day i-day))
466 (or (zerop year)
467 (= year i-year))
468 (mark-visible-calendar-date
469 (calendar-gregorian-from-absolute date)))))))))
470
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."
475 (interactive "P")
476 (let* ((calendar-month-name-array calendar-bahai-month-name-array))
477 (make-diary-entry
478 (concat
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)))
484 nil t))
485 arg)))
486
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."
491 (interactive "P")
492 (let* ((calendar-date-display-form
493 (if european-calendar-style '(day " * ") '("* " day )))
494 (calendar-month-name-array calendar-bahai-month-name-array))
495 (make-diary-entry
496 (concat
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)))))
502 arg)))
503
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."
508 (interactive "P")
509 (let* ((calendar-date-display-form
510 (if european-calendar-style
511 '(day " " monthname)
512 '(monthname " " day)))
513 (calendar-month-name-array calendar-bahai-month-name-array))
514 (make-diary-entry
515 (concat
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)))))
521 arg)))
522
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")
540
541 (provide 'cal-bahai)
542
543 ;; arch-tag: c1cb1d67-862a-4264-a01c-41cb4df01f14
544 ;;; cal-bahai.el ends here