cal-islam.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. ;;; cal-islam.el --- calendar functions for the Islamic calendar
  2. ;; Copyright (C) 1995, 1997, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
  4. ;; Maintainer: Glenn Morris <rgm@gnu.org>
  5. ;; Keywords: calendar
  6. ;; Human-Keywords: Islamic calendar, calendar, diary
  7. ;; Package: calendar
  8. ;; This file is part of GNU Emacs.
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;; See calendar.el.
  21. ;;; Code:
  22. (require 'calendar)
  23. (defconst calendar-islamic-month-name-array
  24. ["Muharram" "Safar" "Rabi I" "Rabi II" "Jumada I" "Jumada II"
  25. "Rajab" "Sha'ban" "Ramadan" "Shawwal" "Dhu al-Qada" "Dhu al-Hijjah"]
  26. "Array of strings giving the names of the Islamic months.")
  27. (eval-and-compile
  28. (autoload 'calendar-julian-to-absolute "cal-julian"))
  29. (defconst calendar-islamic-epoch
  30. (eval-when-compile (calendar-julian-to-absolute '(7 16 622)))
  31. "Absolute date of start of Islamic calendar = July 16, 622 AD (Julian).")
  32. (defun calendar-islamic-leap-year-p (year)
  33. "Return t if YEAR is a leap year on the Islamic calendar."
  34. (memq (% year 30)
  35. (list 2 5 7 10 13 16 18 21 24 26 29)))
  36. (defun calendar-islamic-last-day-of-month (month year)
  37. "The last day in MONTH during YEAR on the Islamic calendar."
  38. (cond
  39. ((memq month (list 1 3 5 7 9 11)) 30)
  40. ((memq month (list 2 4 6 8 10)) 29)
  41. (t (if (calendar-islamic-leap-year-p year) 30 29))))
  42. (defun calendar-islamic-day-number (date)
  43. "Return the day number within the year of the Islamic date DATE."
  44. (let ((month (calendar-extract-month date)))
  45. (+ (* 30 (/ month 2))
  46. (* 29 (/ (1- month) 2))
  47. (calendar-extract-day date))))
  48. (defun calendar-islamic-to-absolute (date)
  49. "Absolute date of Islamic DATE.
  50. The absolute date is the number of days elapsed since the (imaginary)
  51. Gregorian date Sunday, December 31, 1 BC."
  52. (let* ((month (calendar-extract-month date))
  53. (day (calendar-extract-day date))
  54. (year (calendar-extract-year date))
  55. (y (% year 30))
  56. (leap-years-in-cycle (cond ((< y 3) 0)
  57. ((< y 6) 1)
  58. ((< y 8) 2)
  59. ((< y 11) 3)
  60. ((< y 14) 4)
  61. ((< y 17) 5)
  62. ((< y 19) 6)
  63. ((< y 22) 7)
  64. ((< y 25) 8)
  65. ((< y 27) 9)
  66. (t 10))))
  67. (+ (calendar-islamic-day-number date) ; days so far this year
  68. (* (1- year) 354) ; days in all non-leap years
  69. (* 11 (/ year 30)) ; leap days in complete cycles
  70. leap-years-in-cycle ; leap days this cycle
  71. (1- calendar-islamic-epoch)))) ; days before start of calendar
  72. (define-obsolete-function-alias 'calendar-absolute-from-islamic
  73. 'calendar-islamic-to-absolute "23.1")
  74. (defun calendar-islamic-from-absolute (date)
  75. "Compute the Islamic date (month day year) corresponding to absolute DATE.
  76. The absolute date is the number of days elapsed since the (imaginary)
  77. Gregorian date Sunday, December 31, 1 BC."
  78. (if (< date calendar-islamic-epoch)
  79. (list 0 0 0) ; pre-Islamic date
  80. (let* ((approx (/ (- date calendar-islamic-epoch)
  81. 355)) ; approximation from below
  82. (year ; search forward from the approximation
  83. (+ approx
  84. (calendar-sum y approx
  85. (>= date (calendar-islamic-to-absolute
  86. (list 1 1 (1+ y))))
  87. 1)))
  88. (month ; search forward from Muharram
  89. (1+ (calendar-sum m 1
  90. (> date
  91. (calendar-islamic-to-absolute
  92. (list m
  93. (calendar-islamic-last-day-of-month
  94. m year)
  95. year)))
  96. 1)))
  97. (day ; calculate the day by subtraction
  98. (- date
  99. (1- (calendar-islamic-to-absolute (list month 1 year))))))
  100. (list month day year))))
  101. ;;;###cal-autoload
  102. (defun calendar-islamic-date-string (&optional date)
  103. "String of Islamic date before sunset of Gregorian DATE.
  104. Returns the empty string if DATE is pre-Islamic.
  105. Defaults to today's date if DATE is not given.
  106. Driven by the variable `calendar-date-display-form'."
  107. (let ((calendar-month-name-array calendar-islamic-month-name-array)
  108. (islamic-date (calendar-islamic-from-absolute
  109. (calendar-absolute-from-gregorian
  110. (or date (calendar-current-date))))))
  111. (if (< (calendar-extract-year islamic-date) 1)
  112. ""
  113. (calendar-date-string islamic-date nil t))))
  114. ;;;###cal-autoload
  115. (defun calendar-islamic-print-date ()
  116. "Show the Islamic calendar equivalent of the date under the cursor."
  117. (interactive)
  118. (let ((i (calendar-islamic-date-string (calendar-cursor-to-date t))))
  119. (if (string-equal i "")
  120. (message "Date is pre-Islamic")
  121. (message "Islamic date (until sunset): %s" i))))
  122. (define-obsolete-function-alias 'calendar-print-islamic-date
  123. 'calendar-islamic-print-date "23.1")
  124. (defun calendar-islamic-read-date ()
  125. "Interactively read the arguments for an Islamic date command.
  126. Reads a year, month, and day."
  127. (let* ((today (calendar-current-date))
  128. (year (calendar-read
  129. "Islamic calendar year (>0): "
  130. (lambda (x) (> x 0))
  131. (number-to-string
  132. (calendar-extract-year
  133. (calendar-islamic-from-absolute
  134. (calendar-absolute-from-gregorian today))))))
  135. (month-array calendar-islamic-month-name-array)
  136. (completion-ignore-case t)
  137. (month (cdr (assoc-string
  138. (completing-read
  139. "Islamic calendar month name: "
  140. (mapcar 'list (append month-array nil))
  141. nil t)
  142. (calendar-make-alist month-array 1) t)))
  143. (last (calendar-islamic-last-day-of-month month year))
  144. (day (calendar-read
  145. (format "Islamic calendar day (1-%d): " last)
  146. (lambda (x) (and (< 0 x) (<= x last))))))
  147. (list (list month day year))))
  148. ;;;###cal-autoload
  149. (defun calendar-islamic-goto-date (date &optional noecho)
  150. "Move cursor to Islamic DATE; echo Islamic date unless NOECHO is non-nil."
  151. (interactive (calendar-islamic-read-date))
  152. (calendar-goto-date (calendar-gregorian-from-absolute
  153. (calendar-islamic-to-absolute date)))
  154. (or noecho (calendar-islamic-print-date)))
  155. (define-obsolete-function-alias 'calendar-goto-islamic-date
  156. 'calendar-islamic-goto-date "23.1")
  157. (defvar displayed-month) ; from calendar-generate
  158. (defvar displayed-year)
  159. ;;;###holiday-autoload
  160. (defun holiday-islamic (month day string)
  161. "Holiday on MONTH, DAY (Islamic) called STRING.
  162. If MONTH, DAY (Islamic) is visible, returns the corresponding
  163. Gregorian date as the list (((month day year) STRING)).
  164. Returns nil if it is not visible in the current calendar window."
  165. ;; Islamic date corresponding to the center of the calendar window.
  166. ;; Since the calendar displays 3 months at a time, there are approx
  167. ;; 45 visible days either side of this date. Given the length of
  168. ;; the Islamic months, this means up to two different months are
  169. ;; visible either side of the central date.
  170. (let* ((islamic-date (calendar-islamic-from-absolute
  171. (calendar-absolute-from-gregorian
  172. (list displayed-month 15 displayed-year))))
  173. (m (calendar-extract-month islamic-date))
  174. (y (calendar-extract-year islamic-date))
  175. date)
  176. (unless (< m 1) ; Islamic calendar doesn't apply
  177. ;; Since converting to absolute dates can be a complex
  178. ;; operation, we try to speed things up by excluding those date
  179. ;; ranges that can't possibly be visible.
  180. ;; We can view the situation (see above) as if we had a calendar
  181. ;; window displaying 5 months at a time. When month m is
  182. ;; central, months m-2:m+2 (modulo 12) might be visible.
  183. ;; Recall from holiday-fixed that with a 3 month calendar
  184. ;; window, November is special, because we can do a one-sided
  185. ;; inclusion test. When November is central is when the end of
  186. ;; year first appears on the calendar. Similarly, with a 5
  187. ;; month window, October is special. When October is central is
  188. ;; when the end of year first appears, and when January is
  189. ;; central, October is no longer visible. October is visible
  190. ;; when the central month is >= 8.
  191. ;; Hence to test if any given month might be visible, we can
  192. ;; shift things and ask about October.
  193. ;; At the same time, we work out the appropriate year y to use.
  194. (calendar-increment-month m y (- 10 month))
  195. (and (> m 7) ; Islamic date might be visible
  196. (calendar-date-is-visible-p
  197. (setq date (calendar-gregorian-from-absolute
  198. (calendar-islamic-to-absolute (list month day y)))))
  199. (list (list date string))))))
  200. ;;;###holiday-autoload
  201. (defun holiday-islamic-new-year ()
  202. "Holiday entry for the Islamic New Year, if visible in the calendar window."
  203. (let ((date (caar (holiday-islamic 1 1 "")))
  204. (m displayed-month)
  205. (y displayed-year))
  206. (and date
  207. (list (list date
  208. (format "Islamic New Year %d"
  209. (progn
  210. (calendar-increment-month m y 1)
  211. (calendar-extract-year
  212. (calendar-islamic-from-absolute
  213. (calendar-absolute-from-gregorian
  214. (list m (calendar-last-day-of-month m y) y)
  215. ))))))))))
  216. (autoload 'diary-list-entries-1 "diary-lib")
  217. ;;;###diary-autoload
  218. (defun diary-islamic-list-entries ()
  219. "Add any Islamic date entries from the diary file to `diary-entries-list'.
  220. Islamic date diary entries must be prefaced by `diary-islamic-entry-symbol'
  221. \(normally an `I'). The same `diary-date-forms' govern the style
  222. of the Islamic calendar entries, except that the Islamic month
  223. names cannot be abbreviated. The Islamic months are numbered
  224. from 1 to 12 with Muharram being 1 and 12 being Dhu al-Hijjah.
  225. If an Islamic date diary entry begins with `diary-nonmarking-symbol',
  226. the entry will appear in the diary listing, but will not be
  227. marked in the calendar. This function is provided for use with
  228. `diary-nongregorian-listing-hook'."
  229. (diary-list-entries-1 calendar-islamic-month-name-array
  230. diary-islamic-entry-symbol
  231. 'calendar-islamic-from-absolute))
  232. (define-obsolete-function-alias 'list-islamic-diary-entries
  233. 'diary-islamic-list-entries "23.1")
  234. (autoload 'calendar-mark-1 "diary-lib")
  235. ;;;###diary-autoload
  236. (defun calendar-islamic-mark-date-pattern (month day year &optional color)
  237. "Mark dates in calendar window that conform to Islamic date MONTH/DAY/YEAR.
  238. A value of 0 in any position is a wildcard. Optional argument COLOR is
  239. passed to `calendar-mark-visible-date' as MARK."
  240. (calendar-mark-1 month day year 'calendar-islamic-from-absolute
  241. 'calendar-islamic-to-absolute color))
  242. (define-obsolete-function-alias 'mark-islamic-calendar-date-pattern
  243. 'calendar-islamic-mark-date-pattern "23.1")
  244. (autoload 'diary-mark-entries-1 "diary-lib")
  245. ;;;###diary-autoload
  246. (defun diary-islamic-mark-entries ()
  247. "Mark days in the calendar window that have Islamic date diary entries.
  248. Marks each entry in `diary-file' (or included files) visible in the calendar
  249. window. See `diary-islamic-list-entries' for more information."
  250. (diary-mark-entries-1 'calendar-islamic-mark-date-pattern
  251. calendar-islamic-month-name-array
  252. diary-islamic-entry-symbol
  253. 'calendar-islamic-from-absolute))
  254. (define-obsolete-function-alias
  255. 'mark-islamic-diary-entries 'diary-islamic-mark-entries "23.1")
  256. (autoload 'diary-insert-entry-1 "diary-lib")
  257. ;;;###cal-autoload
  258. (defun diary-islamic-insert-entry (arg)
  259. "Insert a diary entry.
  260. For the Islamic date corresponding to the date indicated by point.
  261. Prefix argument ARG makes the entry nonmarking."
  262. (interactive "P")
  263. (diary-insert-entry-1 nil arg calendar-islamic-month-name-array
  264. diary-islamic-entry-symbol
  265. 'calendar-islamic-from-absolute))
  266. (define-obsolete-function-alias 'insert-islamic-diary-entry
  267. 'diary-islamic-insert-entry "23.1")
  268. ;;;###cal-autoload
  269. (defun diary-islamic-insert-monthly-entry (arg)
  270. "Insert a monthly diary entry.
  271. For the day of the Islamic month corresponding to the date indicated by point.
  272. Prefix argument ARG makes the entry nonmarking."
  273. (interactive "P")
  274. (diary-insert-entry-1 'monthly arg calendar-islamic-month-name-array
  275. diary-islamic-entry-symbol
  276. 'calendar-islamic-from-absolute))
  277. (define-obsolete-function-alias 'insert-monthly-islamic-diary-entry
  278. 'diary-islamic-insert-monthly-entry "23.1")
  279. ;;;###cal-autoload
  280. (defun diary-islamic-insert-yearly-entry (arg)
  281. "Insert an annual diary entry.
  282. For the day of the Islamic year corresponding to the date indicated by point.
  283. Prefix argument ARG makes the entry nonmarking."
  284. (interactive "P")
  285. (diary-insert-entry-1 'yearly arg calendar-islamic-month-name-array
  286. diary-islamic-entry-symbol
  287. 'calendar-islamic-from-absolute))
  288. (define-obsolete-function-alias
  289. 'insert-yearly-islamic-diary-entry 'diary-islamic-insert-yearly-entry "23.1")
  290. (defvar date)
  291. ;; To be called from diary-sexp-entry, where DATE, ENTRY are bound.
  292. ;;;###diary-autoload
  293. (defun diary-islamic-date ()
  294. "Islamic calendar equivalent of date diary entry."
  295. (let ((i (calendar-islamic-date-string date)))
  296. (if (string-equal i "")
  297. "Date is pre-Islamic"
  298. (format "Islamic date (until sunset): %s" i))))
  299. (provide 'cal-islam)
  300. ;;; cal-islam.el ends here