Unquote lambda functions. Add autoload cookies to functions formerly
[emacs.git] / lisp / calendar / cal-bahai.el
blobf18b88528e9f887ca34b4307806a7038955fd27c
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 (defvar date)
59 (defvar displayed-month)
60 (defvar displayed-year)
61 (defvar number)
62 (defvar original-date)
64 (require 'cal-julian)
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))
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.
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))))
121 ;;;###autoload
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)))
131 (let ((monthname
132 (if (and (= m 19)
133 (<= d 0))
134 "Ayyám-i-Há"
135 (aref calendar-bahai-month-name-array (1- m))))
136 (day (int-to-string
137 (if (<= d 0)
138 (if (calendar-bahai-leap-year-p y)
139 (+ d 5)
140 (+ d 4))
141 d)))
142 (dayname nil)
143 (month (int-to-string m))
144 (year (int-to-string y)))
145 (mapconcat 'eval calendar-date-display-form ""))))
147 ;;;###autoload
148 (defun calendar-bahai-print-date ()
149 "Show the Bahá'í calendar equivalent of the selected date."
150 (interactive)
151 (message "Bahá'í date: %s"
152 (calendar-bahai-date-string (calendar-cursor-to-date t))))
154 ;;;###autoload
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))
166 (year (calendar-read
167 "Bahá'í calendar year (not 0): "
168 (lambda (x) (/= x 0))
169 (int-to-string
170 (extract-calendar-year
171 (calendar-bahai-from-absolute
172 (calendar-absolute-from-gregorian today))))))
173 (completion-ignore-case t)
174 (month (cdr (assoc
175 (completing-read
176 "Bahá'í calendar month name: "
177 (mapcar 'list
178 (append calendar-bahai-month-name-array nil))
179 nil t)
180 (calendar-make-alist calendar-bahai-month-name-array
181 1))))
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))
200 (date))
201 (if (< m 1)
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'."
225 (if (< 0 number)
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)))
237 (while d
238 (let*
239 ((date-form (if (equal (car (car d)) 'backup)
240 (cdr (car d))
241 (car d)))
242 (backup (equal (car (car d)) 'backup))
243 (dayname
244 (concat
245 (calendar-day-name gdate) "\\|"
246 (substring (calendar-day-name gdate) 0 3) ".?"))
247 (calendar-month-name-array
248 calendar-bahai-month-name-array)
249 (monthname
250 (concat
251 "\\*\\|"
252 (calendar-month-name month)))
253 (month (concat "\\*\\|0*" (int-to-string month)))
254 (day (concat "\\*\\|0*" (int-to-string day)))
255 (year
256 (concat
257 "\\*\\|0*" (int-to-string year)
258 (if abbreviated-calendar-year
259 (concat "\\|" (int-to-string (% year 100)))
260 "")))
261 (regexp
262 (concat
263 "\\(\\`\\|\^M\\|\n\\)" mark "?"
264 (regexp-quote bahai-diary-entry-symbol)
265 "\\("
266 (mapconcat 'eval date-form "\\)\\(")
267 "\\)"))
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.
276 (backward-char 1)
277 ;; Found a nonempty diary entry--make it visible and
278 ;; add it to the list.
279 (let ((entry-start (point))
280 (date-start))
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))
286 (backward-char 1)
287 (subst-char-in-region date-start (point) ?\^M ?\n t)
288 (add-to-diary-list
289 gdate
290 (buffer-substring-no-properties entry-start (point))
291 (buffer-substring-no-properties
292 (1+ date-start) (1- entry-start)))))))
293 (setq d (cdr d))))
294 (setq gdate
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))
319 (while d
320 (let*
321 ((date-form (if (equal (car (car d)) 'backup)
322 (cdr (car d))
323 (car d)));; ignore 'backup directive
324 (dayname (diary-name-pattern calendar-day-name-array))
325 (monthname
326 (concat
327 (diary-name-pattern calendar-bahai-month-name-array t)
328 "\\|\\*"))
329 (month "[0-9]+\\|\\*")
330 (day "[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)))
343 (regexp
344 (concat
345 "\\(\\`\\|\^M\\|\n\\)"
346 (regexp-quote bahai-diary-entry-symbol)
347 "\\("
348 (mapconcat 'eval date-form "\\)\\(")
349 "\\)"))
350 (case-fold-search t))
351 (goto-char (point-min))
352 (while (re-search-forward regexp nil t)
353 (let* ((dd-name
354 (if d-name-pos
355 (buffer-substring
356 (match-beginning d-name-pos)
357 (match-end d-name-pos))))
358 (mm-name
359 (if m-name-pos
360 (buffer-substring
361 (match-beginning m-name-pos)
362 (match-end m-name-pos))))
363 (mm (string-to-number
364 (if m-pos
365 (buffer-substring
366 (match-beginning m-pos)
367 (match-end m-pos))
368 "")))
369 (dd (string-to-number
370 (if d-pos
371 (buffer-substring
372 (match-beginning d-pos)
373 (match-end d-pos))
374 "")))
375 (y-str (if y-pos
376 (buffer-substring
377 (match-beginning y-pos)
378 (match-end y-pos))))
379 (yy (if (not y-str)
381 (if (and (= (length y-str) 2)
382 abbreviated-calendar-year)
383 (let* ((current-y
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)
391 (- y 100)
392 (if (> (- current-y y) 50)
393 (+ y 100)
394 y)))
395 (string-to-number y-str)))))
396 (if dd-name
397 (mark-calendar-days-named
398 (cdr (assoc-string (substring dd-name 0 3)
399 (calendar-make-alist
400 calendar-day-name-array
402 (lambda (x) (substring x 0 3)))
403 t)))
404 (if mm-name
405 (if (string-equal mm-name "*")
406 (setq mm 0)
407 (setq mm
408 (cdr (assoc-string
409 mm-name
410 (calendar-make-alist
411 calendar-bahai-month-name-array)
412 t)))))
413 (calendar-bahai-mark-date-pattern mm dd yy)))))
414 (setq d (cdr d)))))
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."
419 (save-excursion
420 (set-buffer calendar-buffer)
421 (if (and (/= 0 month) (/= 0 day))
422 (if (/= 0 year)
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))
435 (date))
436 (if (< m 1)
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)
449 (y displayed-year)
450 (first-date)
451 (last-date))
452 (increment-calendar-month m y -1)
453 (setq first-date
454 (calendar-absolute-from-gregorian
455 (list m 1 y)))
456 (increment-calendar-month m y 2)
457 (setq last-date
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)
466 (= month i-month))
467 (or (zerop day)
468 (= day i-day))
469 (or (zerop year)
470 (= year i-year))
471 (mark-visible-calendar-date
472 (calendar-gregorian-from-absolute date)))))))))
474 ;;;###autoload
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."
479 (interactive "P")
480 (let* ((calendar-month-name-array calendar-bahai-month-name-array))
481 (make-diary-entry
482 (concat
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)))
488 nil t))
489 arg)))
491 ;;;###autoload
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."
496 (interactive "P")
497 (let* ((calendar-date-display-form
498 (if european-calendar-style '(day " * ") '("* " day )))
499 (calendar-month-name-array calendar-bahai-month-name-array))
500 (make-diary-entry
501 (concat
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)))))
507 arg)))
509 ;;;###autoload
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."
514 (interactive "P")
515 (let* ((calendar-date-display-form
516 (if european-calendar-style
517 '(day " " monthname)
518 '(monthname " " day)))
519 (calendar-month-name-array calendar-bahai-month-name-array))
520 (make-diary-entry
521 (concat
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)))))
527 arg)))
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")
547 (provide 'cal-bahai)
549 ;; Local Variables:
550 ;; coding: utf-8
551 ;; generated-autoload-file: "cal-loaddefs.el"
552 ;; End:
554 ;; arch-tag: c1cb1d67-862a-4264-a01c-41cb4df01f14
555 ;;; cal-bahai.el ends here