(number, original-date, add-to-diary-list)
[emacs.git] / lisp / calendar / cal-islam.el
blob05b629f3c323c7c863859dd21c2acab3bd178e14
1 ;;; cal-islam.el --- calendar functions for the Islamic calendar
3 ;; Copyright (C) 1995, 1997, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 ;; 2008 Free Software Foundation, Inc.
6 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
7 ;; Maintainer: Glenn Morris <rgm@gnu.org>
8 ;; Keywords: calendar
9 ;; Human-Keywords: Islamic calendar, calendar, diary
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 3, or (at your option)
16 ;; any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
28 ;;; Commentary:
30 ;; This collection of functions implements the features of calendar.el and
31 ;; diary.el that deal with the Islamic calendar.
33 ;; Technical details of all the calendrical calculations can be found in
34 ;; ``Calendrical Calculations: The Millennium Edition'' by Edward M. Reingold
35 ;; and Nachum Dershowitz, Cambridge University Press (2001).
37 ;;; Code:
39 (require 'cal-julian)
41 (defconst calendar-islamic-month-name-array
42 ["Muharram" "Safar" "Rabi I" "Rabi II" "Jumada I" "Jumada II"
43 "Rajab" "Sha'ban" "Ramadan" "Shawwal" "Dhu al-Qada" "Dhu al-Hijjah"]
44 "Array of strings giving the names of the Islamic months.")
46 (defconst calendar-islamic-epoch (calendar-absolute-from-julian '(7 16 622))
47 "Absolute date of start of Islamic calendar = August 29, 284 AD (Julian).")
49 (defun islamic-calendar-leap-year-p (year)
50 "Return t if YEAR is a leap year on the Islamic calendar."
51 (memq (% year 30)
52 (list 2 5 7 10 13 16 18 21 24 26 29)))
54 (defun islamic-calendar-last-day-of-month (month year)
55 "The last day in MONTH during YEAR on the Islamic calendar."
56 (cond
57 ((memq month (list 1 3 5 7 9 11)) 30)
58 ((memq month (list 2 4 6 8 10)) 29)
59 (t (if (islamic-calendar-leap-year-p year) 30 29))))
61 (defun islamic-calendar-day-number (date)
62 "Return the day number within the year of the Islamic date DATE."
63 (let ((month (extract-calendar-month date)))
64 (+ (* 30 (/ month 2))
65 (* 29 (/ (1- month) 2))
66 (extract-calendar-day date))))
68 (defun calendar-absolute-from-islamic (date)
69 "Absolute date of Islamic DATE.
70 The absolute date is the number of days elapsed since the (imaginary)
71 Gregorian date Sunday, December 31, 1 BC."
72 (let* ((month (extract-calendar-month date))
73 (day (extract-calendar-day date))
74 (year (extract-calendar-year date))
75 (y (% year 30))
76 (leap-years-in-cycle
77 (cond ((< y 3) 0)
78 ((< y 6) 1)
79 ((< y 8) 2)
80 ((< y 11) 3)
81 ((< y 14) 4)
82 ((< y 17) 5)
83 ((< y 19) 6)
84 ((< y 22) 7)
85 ((< y 25) 8)
86 ((< y 27) 9)
87 (t 10))))
88 (+ (islamic-calendar-day-number date) ; days so far this year
89 (* (1- year) 354) ; days in all non-leap years
90 (* 11 (/ year 30)) ; leap days in complete cycles
91 leap-years-in-cycle ; leap days this cycle
92 (1- calendar-islamic-epoch)))) ; days before start of calendar
94 (defun calendar-islamic-from-absolute (date)
95 "Compute the Islamic date (month day year) corresponding to absolute DATE.
96 The absolute date is the number of days elapsed since the (imaginary)
97 Gregorian date Sunday, December 31, 1 BC."
98 (if (< date calendar-islamic-epoch)
99 (list 0 0 0) ; pre-Islamic date
100 (let* ((approx (/ (- date calendar-islamic-epoch)
101 355)) ; approximation from below
102 (year ; search forward from the approximation
103 (+ approx
104 (calendar-sum y approx
105 (>= date (calendar-absolute-from-islamic
106 (list 1 1 (1+ y))))
107 1)))
108 (month ; search forward from Muharram
109 (1+ (calendar-sum m 1
110 (> date
111 (calendar-absolute-from-islamic
112 (list m
113 (islamic-calendar-last-day-of-month
114 m year)
115 year)))
116 1)))
117 (day ; calculate the day by subtraction
118 (- date
119 (1- (calendar-absolute-from-islamic (list month 1 year))))))
120 (list month day year))))
122 ;;;###cal-autoload
123 (defun calendar-islamic-date-string (&optional date)
124 "String of Islamic date before sunset of Gregorian DATE.
125 Returns the empty string if DATE is pre-Islamic.
126 Defaults to today's date if DATE is not given.
127 Driven by the variable `calendar-date-display-form'."
128 (let ((calendar-month-name-array calendar-islamic-month-name-array)
129 (islamic-date (calendar-islamic-from-absolute
130 (calendar-absolute-from-gregorian
131 (or date (calendar-current-date))))))
132 (if (< (extract-calendar-year islamic-date) 1)
134 (calendar-date-string islamic-date nil t))))
136 ;;;###cal-autoload
137 (defun calendar-print-islamic-date ()
138 "Show the Islamic calendar equivalent of the date under the cursor."
139 (interactive)
140 (let ((i (calendar-islamic-date-string (calendar-cursor-to-date t))))
141 (if (string-equal i "")
142 (message "Date is pre-Islamic")
143 (message "Islamic date (until sunset): %s" i))))
145 ;;;###cal-autoload
146 (defun calendar-goto-islamic-date (date &optional noecho)
147 "Move cursor to Islamic DATE; echo Islamic date unless NOECHO is non-nil."
148 (interactive
149 (let* ((today (calendar-current-date))
150 (year (calendar-read
151 "Islamic calendar year (>0): "
152 (lambda (x) (> x 0))
153 (int-to-string
154 (extract-calendar-year
155 (calendar-islamic-from-absolute
156 (calendar-absolute-from-gregorian today))))))
157 (month-array calendar-islamic-month-name-array)
158 (completion-ignore-case t)
159 (month (cdr (assoc-string
160 (completing-read
161 "Islamic calendar month name: "
162 (mapcar 'list (append month-array nil))
163 nil t)
164 (calendar-make-alist month-array 1) t)))
165 (last (islamic-calendar-last-day-of-month month year))
166 (day (calendar-read
167 (format "Islamic calendar day (1-%d): " last)
168 (lambda (x) (and (< 0 x) (<= x last))))))
169 (list (list month day year))))
170 (calendar-goto-date (calendar-gregorian-from-absolute
171 (calendar-absolute-from-islamic date)))
172 (or noecho (calendar-print-islamic-date)))
174 (defvar displayed-month) ; from generate-calendar
175 (defvar displayed-year)
177 ;;;###holiday-autoload
178 (defun holiday-islamic (month day string)
179 "Holiday on MONTH, DAY (Islamic) called STRING.
180 If MONTH, DAY (Islamic) is visible, the value returned is corresponding
181 Gregorian date in the form of the list (((month day year) STRING)). Returns
182 nil if it is not visible in the current calendar window."
183 (let* ((islamic-date (calendar-islamic-from-absolute
184 (calendar-absolute-from-gregorian
185 (list displayed-month 15 displayed-year))))
186 (m (extract-calendar-month islamic-date))
187 (y (extract-calendar-year islamic-date))
188 (date))
189 (unless (< m 1) ; Islamic calendar doesn't apply
190 (increment-calendar-month m y (- 10 month))
191 (if (> m 7) ; Islamic date might be visible
192 (let ((date (calendar-gregorian-from-absolute
193 (calendar-absolute-from-islamic (list month day y)))))
194 (if (calendar-date-is-visible-p date)
195 (list (list date string))))))))
197 (autoload 'diary-list-entries-1 "diary-lib")
199 ;;;###diary-autoload
200 (defun list-islamic-diary-entries ()
201 "Add any Islamic date entries from the diary file to `diary-entries-list'.
202 Islamic date diary entries must be prefaced by `islamic-diary-entry-symbol'
203 \(normally an `I'). The same `diary-date-forms' govern the style
204 of the Islamic calendar entries, except that the Islamic month
205 names must be spelled in full. The Islamic months are numbered
206 from 1 to 12 with Muharram being 1 and 12 being Dhu al-Hijjah.
207 If an Islamic date diary entry begins with `diary-nonmarking-symbol',
208 the entry will appear in the diary listing, but will not be
209 marked in the calendar. This function is provided for use with
210 `nongregorian-diary-listing-hook'."
211 (diary-list-entries-1 calendar-islamic-month-name-array
212 islamic-diary-entry-symbol
213 'calendar-islamic-from-absolute))
215 ;;;###diary-autoload
216 (defun mark-islamic-calendar-date-pattern (month day year)
217 "Mark dates in calendar window that conform to Islamic date MONTH/DAY/YEAR.
218 A value of 0 in any position is a wildcard."
219 (save-excursion
220 (set-buffer calendar-buffer)
221 (if (and (not (zerop month)) (not (zerop day)))
222 (if (not (zerop year))
223 ;; Fully specified Islamic date.
224 (let ((date (calendar-gregorian-from-absolute
225 (calendar-absolute-from-islamic
226 (list month day year)))))
227 (if (calendar-date-is-visible-p date)
228 (mark-visible-calendar-date date)))
229 ;; Month and day in any year--this taken from the holiday stuff.
230 (let* ((islamic-date (calendar-islamic-from-absolute
231 (calendar-absolute-from-gregorian
232 (list displayed-month 15 displayed-year))))
233 (m (extract-calendar-month islamic-date))
234 (y (extract-calendar-year islamic-date))
235 (date))
236 (unless (< m 1) ; Islamic calendar doesn't apply
237 (increment-calendar-month m y (- 10 month))
238 (if (> m 7) ; Islamic date might be visible
239 (let ((date (calendar-gregorian-from-absolute
240 (calendar-absolute-from-islamic
241 (list month day y)))))
242 (if (calendar-date-is-visible-p date)
243 (mark-visible-calendar-date date)))))))
244 ;; Not one of the simple cases--check all visible dates for match.
245 ;; Actually, the following code takes care of ALL of the cases, but
246 ;; it's much too slow to be used for the simple (common) cases.
247 (let ((m displayed-month)
248 (y displayed-year)
249 (first-date)
250 (last-date))
251 (increment-calendar-month m y -1)
252 (setq first-date
253 (calendar-absolute-from-gregorian
254 (list m 1 y)))
255 (increment-calendar-month m y 2)
256 (setq last-date
257 (calendar-absolute-from-gregorian
258 (list m (calendar-last-day-of-month m y) y)))
259 (calendar-for-loop date from first-date to last-date do
260 (let* ((i-date (calendar-islamic-from-absolute date))
261 (i-month (extract-calendar-month i-date))
262 (i-day (extract-calendar-day i-date))
263 (i-year (extract-calendar-year i-date)))
264 (and (or (zerop month)
265 (= month i-month))
266 (or (zerop day)
267 (= day i-day))
268 (or (zerop year)
269 (= year i-year))
270 (mark-visible-calendar-date
271 (calendar-gregorian-from-absolute date)))))))))
273 (autoload 'diary-mark-entries-1 "diary-lib")
275 ;;;###diary-autoload
276 (defun mark-islamic-diary-entries ()
277 "Mark days in the calendar window that have Islamic date diary entries.
278 Marks each entry in `diary-file' (or included files) visible in the calendar
279 window. See `list-islamic-diary-entries' for more information."
280 (diary-mark-entries-1 calendar-islamic-month-name-array
281 islamic-diary-entry-symbol
282 'calendar-islamic-from-absolute
283 'mark-islamic-calendar-date-pattern))
285 ;;;###cal-autoload
286 (defun insert-islamic-diary-entry (arg)
287 "Insert a diary entry.
288 For the Islamic date corresponding to the date indicated by point.
289 Prefix argument ARG makes the entry nonmarking."
290 (interactive "P")
291 (let ((calendar-month-name-array calendar-islamic-month-name-array))
292 (make-diary-entry
293 (concat islamic-diary-entry-symbol
294 (calendar-date-string
295 (calendar-islamic-from-absolute
296 (calendar-absolute-from-gregorian (calendar-cursor-to-date t)))
297 nil t))
298 arg)))
300 ;;;###cal-autoload
301 (defun insert-monthly-islamic-diary-entry (arg)
302 "Insert a monthly diary entry.
303 For the day of the Islamic month corresponding to the date indicated by point.
304 Prefix argument ARG makes the entry nonmarking."
305 (interactive "P")
306 (let ((calendar-date-display-form (if european-calendar-style
307 '(day " * ")
308 '("* " day )))
309 (calendar-month-name-array calendar-islamic-month-name-array))
310 (make-diary-entry
311 (concat islamic-diary-entry-symbol
312 (calendar-date-string
313 (calendar-islamic-from-absolute
314 (calendar-absolute-from-gregorian (calendar-cursor-to-date t)))))
315 arg)))
317 ;;;###cal-autoload
318 (defun insert-yearly-islamic-diary-entry (arg)
319 "Insert an annual diary entry.
320 For the day of the Islamic year corresponding to the date indicated by point.
321 Prefix argument ARG makes the entry nonmarking."
322 (interactive "P")
323 (let ((calendar-date-display-form (if european-calendar-style
324 '(day " " monthname)
325 '(monthname " " day)))
326 (calendar-month-name-array calendar-islamic-month-name-array))
327 (make-diary-entry
328 (concat islamic-diary-entry-symbol
329 (calendar-date-string
330 (calendar-islamic-from-absolute
331 (calendar-absolute-from-gregorian (calendar-cursor-to-date t)))))
332 arg)))
334 (defvar date)
336 ;; To be called from diary-sexp-entry, where DATE, ENTRY are bound.
337 ;;;###diary-autoload
338 (defun diary-islamic-date ()
339 "Islamic calendar equivalent of date diary entry."
340 (let ((i (calendar-islamic-date-string date)))
341 (if (string-equal i "")
342 "Date is pre-Islamic"
343 (format "Islamic date (until sunset): %s" i))))
345 (provide 'cal-islam)
347 ;; arch-tag: a951b6c1-6f47-48d5-bac3-1b505cd719f7
348 ;;; cal-islam.el ends here