cal-bahai.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. ;;; cal-bahai.el --- calendar functions for the Bahá'í calendar.
  2. ;; Copyright (C) 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: John Wiegley <johnw@gnu.org>
  4. ;; Keywords: calendar
  5. ;; Human-Keywords: Bahá'í calendar, Bahá'í, Baha'i, Bahai, calendar, diary
  6. ;; Package: calendar
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; This collection of functions implements the features of calendar.el
  20. ;; and diary-lib.el that deal with the Bahá'í calendar.
  21. ;; The Bahá'í (http://www.bahai.org) calendar system is based on a
  22. ;; solar cycle of 19 months with 19 days each. The four remaining
  23. ;; "intercalary" days are called the Ayyám-i-Há (days of Há), and are
  24. ;; placed between the 18th and 19th months. They are meant as a time
  25. ;; of festivals preceding the 19th month, which is the month of
  26. ;; fasting. In Gregorian leap years, there are 5 of these days (Há
  27. ;; has the numerical value of 5 in the arabic abjad, or
  28. ;; letter-to-number, reckoning).
  29. ;; Each month is named after an attribute of God, as are the 19 days
  30. ;; -- which have the same names as the months. There is also a name
  31. ;; for each year in every 19 year cycle. These cycles are called
  32. ;; Váhids. A cycle of 19 Váhids (361 years) is called a Kullu-Shay,
  33. ;; which means "all things".
  34. ;; The calendar was named the "Badí` calendar" by its author, the Báb.
  35. ;; It uses a week of seven days, corresponding to the Gregorian week,
  36. ;; each of which has its own name, again patterned after the
  37. ;; attributes of God.
  38. ;; Note: The days of Ayyám-i-Há are encoded as zero and negative
  39. ;; offsets from the first day of the final month. So, (19 -3 157) is
  40. ;; the first day of Ayyám-i-Há, in the year 157 BE.
  41. ;;; Code:
  42. (require 'calendar)
  43. (defconst calendar-bahai-month-name-array
  44. ["Bahá" "Jalál" "Jamál" "`Azamat" "Núr" "Rahmat" "Kalimát" "Kamál"
  45. "Asmá" "`Izzat" "Mashiyyat" "`Ilm" "Qudrat" "Qawl" "Masá'il"
  46. "Sharaf" "Sultán" "Mulk" "`Alá"]
  47. "Array of the month names in the Bahá'í calendar.")
  48. (defconst calendar-bahai-epoch (calendar-absolute-from-gregorian '(3 21 1844))
  49. "Absolute date of start of Bahá'í calendar = March 21, 1844 AD.")
  50. (defun calendar-bahai-leap-year-p (year)
  51. "True if Bahá'í YEAR is a leap year in the Bahá'í calendar."
  52. (calendar-leap-year-p (+ year 1844)))
  53. (defconst calendar-bahai-leap-base
  54. (+ (/ 1844 4) (- (/ 1844 100)) (/ 1844 400))
  55. "Number of leap years between 1 and 1844 AD, inclusive.
  56. Used by `calendar-bahai-to-absolute'.")
  57. (defun calendar-bahai-to-absolute (date)
  58. "Compute absolute date from Bahá'í date DATE.
  59. The absolute date is the number of days elapsed since the (imaginary)
  60. Gregorian date Sunday, December 31, 1 BC."
  61. (let* ((month (calendar-extract-month date))
  62. (day (calendar-extract-day date))
  63. (year (calendar-extract-year date))
  64. (prior-years (+ (1- year) 1844))
  65. (leap-days (- (+ (/ prior-years 4) ; leap days in prior years
  66. (- (/ prior-years 100))
  67. (/ prior-years 400))
  68. calendar-bahai-leap-base)))
  69. (+ (1- calendar-bahai-epoch) ; days before epoch
  70. (* 365 (1- year)) ; days in prior years
  71. leap-days
  72. (calendar-sum m 1 (< m month) 19)
  73. (if (= month 19)
  74. (if (calendar-bahai-leap-year-p year) 5 4)
  75. 0)
  76. day))) ; days so far this month
  77. (define-obsolete-function-alias 'calendar-absolute-from-bahai
  78. 'calendar-bahai-to-absolute "23.1")
  79. (defun calendar-bahai-from-absolute (date)
  80. "Bahá'í date (month day year) corresponding to the absolute DATE."
  81. (if (< date calendar-bahai-epoch)
  82. (list 0 0 0) ; pre-Bahá'í date
  83. (let* ((greg (calendar-gregorian-from-absolute date))
  84. (gmonth (calendar-extract-month greg))
  85. (year (+ (- (calendar-extract-year greg) 1844)
  86. (if (or (> gmonth 3)
  87. (and (= gmonth 3)
  88. (>= (calendar-extract-day greg) 21)))
  89. 1 0)))
  90. (month ; search forward from Baha
  91. (1+ (calendar-sum m 1
  92. (> date (calendar-bahai-to-absolute (list m 19 year)))
  93. 1)))
  94. (day ; calculate the day by subtraction
  95. (- date
  96. (1- (calendar-bahai-to-absolute (list month 1 year))))))
  97. (list month day year))))
  98. ;;;###cal-autoload
  99. (defun calendar-bahai-date-string (&optional date)
  100. "String of Bahá'í date of Gregorian DATE.
  101. Defaults to today's date if DATE is not given."
  102. (let* ((bahai-date (calendar-bahai-from-absolute
  103. (calendar-absolute-from-gregorian
  104. (or date (calendar-current-date)))))
  105. (y (calendar-extract-year bahai-date)))
  106. (if (< y 1)
  107. "" ; pre-Bahai
  108. (let* ((m (calendar-extract-month bahai-date))
  109. (d (calendar-extract-day bahai-date))
  110. (monthname (if (and (= m 19)
  111. (<= d 0))
  112. "Ayyám-i-Há"
  113. (aref calendar-bahai-month-name-array (1- m))))
  114. (day (number-to-string
  115. (if (<= d 0)
  116. (+ d (if (calendar-bahai-leap-year-p y) 5 4))
  117. d)))
  118. (year (number-to-string y))
  119. (month (number-to-string m))
  120. dayname)
  121. ;; Can't call calendar-date-string because of monthname oddity.
  122. (mapconcat 'eval calendar-date-display-form "")))))
  123. ;;;###cal-autoload
  124. (defun calendar-bahai-print-date ()
  125. "Show the Bahá'í calendar equivalent of the selected date."
  126. (interactive)
  127. (let ((s (calendar-bahai-date-string (calendar-cursor-to-date t))))
  128. (if (string-equal s "")
  129. (message "Date is pre-Bahá'í")
  130. (message "Bahá'í date: %s" s))))
  131. (define-obsolete-function-alias
  132. 'calendar-print-bahai-date 'calendar-bahai-print-date "23.1")
  133. (defun calendar-bahai-read-date ()
  134. "Interactively read the arguments for a Bahá'í date command.
  135. Reads a year, month and day."
  136. (let* ((today (calendar-current-date))
  137. (year (calendar-read
  138. "Bahá'í calendar year (not 0): "
  139. (lambda (x) (not (zerop x)))
  140. (number-to-string
  141. (calendar-extract-year
  142. (calendar-bahai-from-absolute
  143. (calendar-absolute-from-gregorian today))))))
  144. (completion-ignore-case t)
  145. (month (cdr (assoc
  146. (completing-read
  147. "Bahá'í calendar month name: "
  148. (mapcar 'list
  149. (append calendar-bahai-month-name-array nil))
  150. nil t)
  151. (calendar-make-alist calendar-bahai-month-name-array
  152. 1))))
  153. (day (calendar-read "Bahá'í calendar day (1-19): "
  154. (lambda (x) (and (< 0 x) (<= x 19))))))
  155. (list (list month day year))))
  156. (define-obsolete-function-alias
  157. 'calendar-bahai-prompt-for-date 'calendar-bahai-read-date "23.1")
  158. ;;;###cal-autoload
  159. (defun calendar-bahai-goto-date (date &optional noecho)
  160. "Move cursor to Bahá'í date DATE; echo Bahá'í date unless NOECHO is non-nil."
  161. (interactive (calendar-bahai-read-date))
  162. (calendar-goto-date (calendar-gregorian-from-absolute
  163. (calendar-bahai-to-absolute date)))
  164. (or noecho (calendar-bahai-print-date)))
  165. (define-obsolete-function-alias
  166. 'calendar-goto-bahai-date 'calendar-bahai-goto-date "23.1")
  167. (defvar displayed-month)
  168. (defvar displayed-year)
  169. ;;;###holiday-autoload
  170. (defun holiday-bahai (month day string)
  171. "Holiday on MONTH, DAY (Bahá'í) called STRING.
  172. If MONTH, DAY (Bahá'í) is visible in the current calendar window,
  173. returns the corresponding Gregorian date in the form of the
  174. list (((month day year) STRING)). Otherwise, returns nil."
  175. ;; Since the calendar window shows 3 months at a time, there are
  176. ;; approx +/- 45 days either side of the central month.
  177. ;; Since the Bahai months have 19 days, this means up to +/- 3 months.
  178. (let* ((bahai-date (calendar-bahai-from-absolute
  179. (calendar-absolute-from-gregorian
  180. (list displayed-month 15 displayed-year))))
  181. (m (calendar-extract-month bahai-date))
  182. (y (calendar-extract-year bahai-date))
  183. date)
  184. (unless (< m 1) ; Bahá'í calendar doesn't apply
  185. ;; Cf holiday-fixed, holiday-islamic.
  186. ;; With a +- 3 month calendar window, and 19 months per year,
  187. ;; month 16 is special. When m16 is central is when the
  188. ;; end-of-year first appears. When m1 is central, m16 is no
  189. ;; longer visible. Hence we can do a one-sided test to see if
  190. ;; m16 is visible. m16 is visible when the central month >= 13.
  191. ;; To see if other months are visible we can shift the range
  192. ;; accordingly.
  193. (calendar-increment-month m y (- 16 month) 19)
  194. (and (> m 12) ; Bahá'í date might be visible
  195. (calendar-date-is-visible-p
  196. (setq date (calendar-gregorian-from-absolute
  197. (calendar-bahai-to-absolute (list month day y)))))
  198. (list (list date string))))))
  199. (autoload 'holiday-fixed "holidays")
  200. ;;;###holiday-autoload
  201. (defun holiday-bahai-new-year ()
  202. "Holiday entry for the Bahá'í New Year, if visible in the calendar window."
  203. (holiday-fixed 3 21
  204. (format "Bahá'í New Year (Naw-Ruz) %d"
  205. (- displayed-year (1- 1844)))))
  206. ;;;###holiday-autoload
  207. (defun holiday-bahai-ridvan (&optional all)
  208. "Holidays related to Ridvan, as visible in the calendar window.
  209. Only considers the first, ninth, and twelfth days, unless ALL or
  210. `calendar-bahai-all-holidays-flag' is non-nil."
  211. (let ((ord ["First" "Second" "Third" "Fourth" "Fifth" "Sixth"
  212. "Seventh" "Eighth" "Ninth" "Tenth" "Eleventh" "Twelfth"])
  213. (show '(0 8 11))
  214. rid h)
  215. (if (or all calendar-bahai-all-holidays-flag)
  216. (setq show (number-sequence 0 11)))
  217. ;; More trouble than it was worth...?
  218. (dolist (i show (nreverse rid))
  219. (if (setq h (holiday-fixed (if (< i 10) 4 5)
  220. (+ i (if (< i 10) 21 -9))
  221. (format "%s Day of Ridvan" (aref ord i))))
  222. (push (car h) rid)))))
  223. (autoload 'diary-list-entries-1 "diary-lib")
  224. ;;;###diary-autoload
  225. (defun diary-bahai-list-entries ()
  226. "Add any Bahá'í date entries from the diary file to `diary-entries-list'.
  227. Bahá'í date diary entries must be prefaced by `diary-bahai-entry-symbol'
  228. \(normally a `B'). The same diary date forms govern the style of the
  229. Bahá'í calendar entries, except that the Bahá'í month names cannot be
  230. abbreviated. The Bahá'í months are numbered from 1 to 19 with Bahá being
  231. 1 and 19 being `Alá. If a Bahá'í date diary entry begins with
  232. `diary-nonmarking-symbol', the entry will appear in the diary listing, but
  233. will not be marked in the calendar. This function is provided for use with
  234. `diary-nongregorian-listing-hook'."
  235. (diary-list-entries-1 calendar-bahai-month-name-array
  236. diary-bahai-entry-symbol
  237. 'calendar-bahai-from-absolute))
  238. (define-obsolete-function-alias
  239. 'list-bahai-diary-entries 'diary-bahai-list-entries "23.1")
  240. (autoload 'calendar-mark-1 "diary-lib")
  241. ;;;###diary-autoload
  242. (defun calendar-bahai-mark-date-pattern (month day year &optional color)
  243. "Mark dates in calendar window that conform to Bahá'í date MONTH/DAY/YEAR.
  244. A value of 0 in any position is a wildcard. Optional argument COLOR is
  245. passed to `calendar-mark-visible-date' as MARK."
  246. (calendar-mark-1 month day year 'calendar-bahai-from-absolute
  247. 'calendar-bahai-to-absolute color))
  248. (define-obsolete-function-alias
  249. 'mark-bahai-calendar-date-pattern 'calendar-bahai-mark-date-pattern "23.1")
  250. (autoload 'diary-mark-entries-1 "diary-lib")
  251. ;;;###diary-autoload
  252. (defun diary-bahai-mark-entries ()
  253. "Mark days in the calendar window that have Bahá'í date diary entries.
  254. Marks each entry in `diary-file' (or included files) visible in the calendar
  255. window. See `diary-bahai-list-entries' for more information."
  256. (diary-mark-entries-1 'calendar-bahai-mark-date-pattern
  257. calendar-bahai-month-name-array
  258. diary-bahai-entry-symbol
  259. 'calendar-bahai-from-absolute))
  260. (define-obsolete-function-alias
  261. 'mark-bahai-diary-entries 'diary-bahai-mark-entries "23.1")
  262. (autoload 'diary-insert-entry-1 "diary-lib")
  263. ;;;###cal-autoload
  264. (defun diary-bahai-insert-entry (arg)
  265. "Insert a diary entry.
  266. For the Bahá'í date corresponding to the date indicated by point.
  267. Prefix argument ARG makes the entry nonmarking."
  268. (interactive "P")
  269. (diary-insert-entry-1 nil arg calendar-bahai-month-name-array
  270. diary-bahai-entry-symbol
  271. 'calendar-bahai-from-absolute))
  272. (define-obsolete-function-alias
  273. 'insert-bahai-diary-entry 'diary-bahai-insert-entry "23.1")
  274. ;;;###cal-autoload
  275. (defun diary-bahai-insert-monthly-entry (arg)
  276. "Insert a monthly diary entry.
  277. For the day of the Bahá'í month corresponding to the date indicated by point.
  278. Prefix argument ARG makes the entry nonmarking."
  279. (interactive "P")
  280. (diary-insert-entry-1 'monthly arg calendar-bahai-month-name-array
  281. diary-bahai-entry-symbol
  282. 'calendar-bahai-from-absolute))
  283. (define-obsolete-function-alias
  284. 'insert-monthly-bahai-diary-entry 'diary-bahai-insert-monthly-entry "23.1")
  285. ;;;###cal-autoload
  286. (defun diary-bahai-insert-yearly-entry (arg)
  287. "Insert an annual diary entry.
  288. For the day of the Bahá'í year corresponding to the date indicated by point.
  289. Prefix argument ARG will make the entry nonmarking."
  290. (interactive "P")
  291. (diary-insert-entry-1 'yearly arg calendar-bahai-month-name-array
  292. diary-bahai-entry-symbol
  293. 'calendar-bahai-from-absolute))
  294. (define-obsolete-function-alias
  295. 'insert-yearly-bahai-diary-entry 'diary-bahai-insert-yearly-entry "23.1")
  296. (defvar date)
  297. ;; To be called from diary-list-sexp-entries, where DATE is bound.
  298. ;;;###diary-autoload
  299. (defun diary-bahai-date ()
  300. "Bahá'í calendar equivalent of date diary entry."
  301. (format "Bahá'í date: %s" (calendar-bahai-date-string date)))
  302. (provide 'cal-bahai)
  303. ;; Local Variables:
  304. ;; coding: utf-8
  305. ;; End:
  306. ;;; cal-bahai.el ends here