1 ;;; cal-islam.el --- calendar functions for the Islamic calendar.
3 ;; Copyright (C) 1995 Free Software Foundation, Inc.
5 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
7 ;; Human-Keywords: Islamic calendar, calendar, diary
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; This collection of functions implements the features of calendar.el and
29 ;; diary.el that deal with the Islamic calendar.
31 ;; Comments, corrections, and improvements should be sent to
32 ;; Edward M. Reingold Department of Computer Science
33 ;; (217) 333-6733 University of Illinois at Urbana-Champaign
34 ;; reingold@cs.uiuc.edu 1304 West Springfield Avenue
35 ;; Urbana, Illinois 61801
41 (defvar 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"])
45 (defvar calendar-islamic-epoch (calendar-absolute-from-julian '(7 16 622))
46 "Absolute date of start of Islamic calendar = August 29, 284 A.D. (Julian).")
48 (defun islamic-calendar-leap-year-p (year)
49 "Returns t if YEAR is a leap year on the Islamic calendar."
51 (list 2 5 7 10 13 16 18 21 24 26 29)))
53 (defun islamic-calendar-last-day-of-month (month year)
54 "The last day in MONTH during YEAR on the Islamic calendar."
56 ((memq month (list 1 3 5 7 9 11)) 30)
57 ((memq month (list 2 4 6 8 10)) 29)
58 (t (if (islamic-calendar-leap-year-p year) 30 29))))
60 (defun islamic-calendar-day-number (date)
61 "Return the day number within the year of the Islamic date DATE."
62 (let* ((month (extract-calendar-month date))
63 (day (extract-calendar-day date)))
65 (* 29 (/ (1- month) 2))
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))
78 ((< y 3) 0) ((< y 6) 1) ((< y 8) 2) ((< y 11) 3) ((< y 14) 4)
79 ((< y 17) 5) ((< y 19) 6) ((< y 22) 7) ((< y 25) 8) ((< y 27) 9)
81 (+ (islamic-calendar-day-number date);; days so far this year
82 (* (1- year) 354) ;; days in all non-leap years
83 (* 11 (/ year 30)) ;; leap days in complete cycles
84 leap-years-in-cycle ;; leap days this cycle
85 (1- calendar-islamic-epoch)))) ;; days before start of calendar
87 (defun calendar-islamic-from-absolute (date)
88 "Compute the Islamic date (month day year) corresponding to absolute DATE.
89 The absolute date is the number of days elapsed since the (imaginary)
90 Gregorian date Sunday, December 31, 1 BC."
91 (if (< date calendar-islamic-epoch)
92 (list 0 0 0);; pre-Islamic date
93 (let* ((approx (/ (- date calendar-islamic-epoch)
94 355));; Approximation from below.
95 (year ;; Search forward from the approximation.
97 (calendar-sum y approx
98 (>= date (calendar-absolute-from-islamic
101 (month ;; Search forward from Muharram.
102 (1+ (calendar-sum m 1
104 (calendar-absolute-from-islamic
106 (islamic-calendar-last-day-of-month
110 (day ;; Calculate the day by subtraction.
112 (1- (calendar-absolute-from-islamic (list month 1 year))))))
113 (list month day year))))
115 (defun calendar-islamic-date-string (&optional date)
116 "String of Islamic date before sunset of Gregorian DATE.
117 Returns the empty string if DATE is pre-Islamic.
118 Defaults to today's date if DATE is not given.
119 Driven by the variable `calendar-date-display-form'."
120 (let ((calendar-month-name-array calendar-islamic-month-name-array)
121 (islamic-date (calendar-islamic-from-absolute
122 (calendar-absolute-from-gregorian
123 (or date (calendar-current-date))))))
124 (if (< (extract-calendar-year islamic-date) 1)
126 (calendar-date-string islamic-date nil t))))
128 (defun calendar-print-islamic-date ()
129 "Show the Islamic calendar equivalent of the date under the cursor."
131 (let ((i (calendar-islamic-date-string (calendar-cursor-to-date t))))
132 (if (string-equal i "")
133 (message "Date is pre-Islamic")
134 (message "Islamic date (until sunset): %s" i))))
136 (defun calendar-goto-islamic-date (date &optional noecho)
137 "Move cursor to Islamic DATE; echo Islamic date unless NOECHO is t."
139 (let* ((today (calendar-current-date))
141 "Islamic calendar year (>0): "
142 '(lambda (x) (> x 0))
144 (extract-calendar-year
145 (calendar-islamic-from-absolute
146 (calendar-absolute-from-gregorian today))))))
147 (month-array calendar-islamic-month-name-array)
148 (completion-ignore-case t)
152 "Islamic calendar month name: "
153 (mapcar 'list (append month-array nil))
155 (calendar-make-alist month-array 1 'capitalize))))
156 (last (islamic-calendar-last-day-of-month month year))
158 (format "Islamic calendar day (1-%d): " last)
159 '(lambda (x) (and (< 0 x) (<= x last))))))
160 (list (list month day year))))
161 (calendar-goto-date (calendar-gregorian-from-absolute
162 (calendar-absolute-from-islamic date)))
163 (or noecho (calendar-print-islamic-date)))
165 (defun diary-islamic-date ()
166 "Islamic calendar equivalent of date diary entry."
167 (let ((i (calendar-islamic-date-string date)))
168 (if (string-equal i "")
169 "Date is pre-Islamic"
170 (format "Islamic date (until sunset): %s" i))))
172 (defun holiday-islamic (month day string)
173 "Holiday on MONTH, DAY (Islamic) called STRING.
174 If MONTH, DAY (Islamic) is visible, the value returned is corresponding
175 Gregorian date in the form of the list (((month day year) STRING)). Returns
176 nil if it is not visible in the current calendar window."
177 (let* ((islamic-date (calendar-islamic-from-absolute
178 (calendar-absolute-from-gregorian
179 (list displayed-month 15 displayed-year))))
180 (m (extract-calendar-month islamic-date))
181 (y (extract-calendar-year islamic-date))
184 nil;; Islamic calendar doesn't apply.
185 (increment-calendar-month m y (- 10 month))
186 (if (> m 7);; Islamic date might be visible
187 (let ((date (calendar-gregorian-from-absolute
188 (calendar-absolute-from-islamic (list month day y)))))
189 (if (calendar-date-is-visible-p date)
190 (list (list date string))))))))
192 (defun list-islamic-diary-entries ()
193 "Add any Islamic date entries from the diary file to `diary-entries-list'.
194 Islamic date diary entries must be prefaced by an `islamic-diary-entry-symbol'
195 \(normally an `I'). The same diary date forms govern the style of the Islamic
196 calendar entries, except that the Islamic month names must be spelled in full.
197 The Islamic months are numbered from 1 to 12 with Muharram being 1 and 12 being
198 Dhu al-Hijjah. If an Islamic date diary entry begins with a
199 `diary-nonmarking-symbol', the entry will appear in the diary listing, but will
200 not be marked in the calendar. This function is provided for use with the
201 `nongregorian-diary-listing-hook'."
203 (let ((buffer-read-only nil)
204 (diary-modified (buffer-modified-p))
205 (gdate original-date)
206 (mark (regexp-quote diary-nonmarking-symbol)))
207 (calendar-for-loop i from 1 to number do
208 (let* ((d diary-date-forms)
209 (idate (calendar-islamic-from-absolute
210 (calendar-absolute-from-gregorian gdate)))
211 (month (extract-calendar-month idate))
212 (day (extract-calendar-day idate))
213 (year (extract-calendar-year idate)))
216 ((date-form (if (equal (car (car d)) 'backup)
219 (backup (equal (car (car d)) 'backup))
222 (calendar-day-name gdate) "\\|"
223 (substring (calendar-day-name gdate) 0 3) ".?"))
224 (calendar-month-name-array
225 calendar-islamic-month-name-array)
229 (calendar-month-name month)))
230 (month (concat "\\*\\|0*" (int-to-string month)))
231 (day (concat "\\*\\|0*" (int-to-string day)))
234 "\\*\\|0*" (int-to-string year)
235 (if abbreviated-calendar-year
236 (concat "\\|" (int-to-string (% year 100)))
240 "\\(\\`\\|\^M\\|\n\\)" mark "?"
241 (regexp-quote islamic-diary-entry-symbol)
243 (mapconcat 'eval date-form "\\)\\(")
245 (case-fold-search t))
246 (goto-char (point-min))
247 (while (re-search-forward regexp nil t)
248 (if backup (re-search-backward "\\<" nil t))
249 (if (and (or (char-equal (preceding-char) ?\^M)
250 (char-equal (preceding-char) ?\n))
251 (not (looking-at " \\|\^I")))
252 ;; Diary entry that consists only of date.
254 ;; Found a nonempty diary entry--make it visible and
255 ;; add it to the list.
256 (let ((entry-start (point))
258 (re-search-backward "\^M\\|\n\\|\\`")
259 (setq date-start (point))
260 (re-search-forward "\^M\\|\n" nil t 2)
261 (while (looking-at " \\|\^I")
262 (re-search-forward "\^M\\|\n" nil t))
264 (subst-char-in-region date-start (point) ?\^M ?\n t)
267 (buffer-substring-no-properties entry-start (point))
268 (buffer-substring-no-properties
269 (1+ date-start) (1- entry-start)))))))
272 (calendar-gregorian-from-absolute
273 (1+ (calendar-absolute-from-gregorian gdate)))))
274 (set-buffer-modified-p diary-modified))
275 (goto-char (point-min))))
277 (defun mark-islamic-diary-entries ()
278 "Mark days in the calendar window that have Islamic date diary entries.
279 Each entry in diary-file (or included files) visible in the calendar window
280 is marked. Islamic date entries are prefaced by a islamic-diary-entry-symbol
281 \(normally an `I'). The same diary-date-forms govern the style of the Islamic
282 calendar entries, except that the Islamic month names must be spelled in full.
283 The Islamic months are numbered from 1 to 12 with Muharram being 1 and 12 being
284 Dhu al-Hijjah. Islamic date diary entries that begin with a
285 diary-nonmarking-symbol will not be marked in the calendar. This function is
286 provided for use as part of the nongregorian-diary-marking-hook."
287 (let ((d diary-date-forms))
290 ((date-form (if (equal (car (car d)) 'backup)
292 (car d)));; ignore 'backup directive
293 (dayname (diary-name-pattern calendar-day-name-array))
296 (diary-name-pattern calendar-islamic-month-name-array t)
298 (month "[0-9]+\\|\\*")
300 (year "[0-9]+\\|\\*")
301 (l (length date-form))
302 (d-name-pos (- l (length (memq 'dayname date-form))))
303 (d-name-pos (if (/= l d-name-pos) (+ 2 d-name-pos)))
304 (m-name-pos (- l (length (memq 'monthname date-form))))
305 (m-name-pos (if (/= l m-name-pos) (+ 2 m-name-pos)))
306 (d-pos (- l (length (memq 'day date-form))))
307 (d-pos (if (/= l d-pos) (+ 2 d-pos)))
308 (m-pos (- l (length (memq 'month date-form))))
309 (m-pos (if (/= l m-pos) (+ 2 m-pos)))
310 (y-pos (- l (length (memq 'year date-form))))
311 (y-pos (if (/= l y-pos) (+ 2 y-pos)))
314 "\\(\\`\\|\^M\\|\n\\)"
315 (regexp-quote islamic-diary-entry-symbol)
317 (mapconcat 'eval date-form "\\)\\(")
319 (case-fold-search t))
320 (goto-char (point-min))
321 (while (re-search-forward regexp nil t)
325 (match-beginning d-name-pos)
326 (match-end d-name-pos))))
330 (match-beginning m-name-pos)
331 (match-end m-name-pos))))
335 (match-beginning m-pos)
341 (match-beginning d-pos)
346 (match-beginning y-pos)
350 (if (and (= (length y-str) 2)
351 abbreviated-calendar-year)
353 (extract-calendar-year
354 (calendar-islamic-from-absolute
355 (calendar-absolute-from-gregorian
356 (calendar-current-date)))))
357 (y (+ (string-to-int y-str)
358 (* 100 (/ current-y 100)))))
359 (if (> (- y current-y) 50)
361 (if (> (- current-y y) 50)
364 (string-to-int y-str)))))
366 (mark-calendar-days-named
367 (cdr (assoc (capitalize (substring dd-name 0 3))
369 calendar-day-name-array
371 '(lambda (x) (substring x 0 3))))))
373 (if (string-equal mm-name "*")
379 calendar-islamic-month-name-array))))))
380 (mark-islamic-calendar-date-pattern mm dd yy)))))
383 (defun mark-islamic-calendar-date-pattern (month day year)
384 "Mark dates in calendar window that conform to Islamic date MONTH/DAY/YEAR.
385 A value of 0 in any position is a wildcard."
387 (set-buffer calendar-buffer)
388 (if (and (/= 0 month) (/= 0 day))
390 ;; Fully specified Islamic date.
391 (let ((date (calendar-gregorian-from-absolute
392 (calendar-absolute-from-islamic
393 (list month day year)))))
394 (if (calendar-date-is-visible-p date)
395 (mark-visible-calendar-date date)))
396 ;; Month and day in any year--this taken from the holiday stuff.
397 (let* ((islamic-date (calendar-islamic-from-absolute
398 (calendar-absolute-from-gregorian
399 (list displayed-month 15 displayed-year))))
400 (m (extract-calendar-month islamic-date))
401 (y (extract-calendar-year islamic-date))
404 nil;; Islamic calendar doesn't apply.
405 (increment-calendar-month m y (- 10 month))
406 (if (> m 7);; Islamic date might be visible
407 (let ((date (calendar-gregorian-from-absolute
408 (calendar-absolute-from-islamic
409 (list month day y)))))
410 (if (calendar-date-is-visible-p date)
411 (mark-visible-calendar-date date)))))))
412 ;; Not one of the simple cases--check all visible dates for match.
413 ;; Actually, the following code takes care of ALL of the cases, but
414 ;; it's much too slow to be used for the simple (common) cases.
415 (let ((m displayed-month)
419 (increment-calendar-month m y -1)
421 (calendar-absolute-from-gregorian
423 (increment-calendar-month m y 2)
425 (calendar-absolute-from-gregorian
426 (list m (calendar-last-day-of-month m y) y)))
427 (calendar-for-loop date from first-date to last-date do
428 (let* ((i-date (calendar-islamic-from-absolute date))
429 (i-month (extract-calendar-month i-date))
430 (i-day (extract-calendar-day i-date))
431 (i-year (extract-calendar-year i-date)))
432 (and (or (zerop month)
438 (mark-visible-calendar-date
439 (calendar-gregorian-from-absolute date)))))))))
441 (defun insert-islamic-diary-entry (arg)
442 "Insert a diary entry.
443 For the Islamic date corresponding to the date indicated by point.
444 Prefix arg will make the entry nonmarking."
446 (let* ((calendar-month-name-array calendar-islamic-month-name-array))
449 islamic-diary-entry-symbol
450 (calendar-date-string
451 (calendar-islamic-from-absolute
452 (calendar-absolute-from-gregorian
453 (calendar-cursor-to-date t)))
457 (defun insert-monthly-islamic-diary-entry (arg)
458 "Insert a monthly diary entry.
459 For the day of the Islamic month corresponding to the date indicated by point.
460 Prefix arg will make the entry nonmarking."
462 (let* ((calendar-date-display-form
463 (if european-calendar-style '(day " * ") '("* " day )))
464 (calendar-month-name-array calendar-islamic-month-name-array))
467 islamic-diary-entry-symbol
468 (calendar-date-string
469 (calendar-islamic-from-absolute
470 (calendar-absolute-from-gregorian
471 (calendar-cursor-to-date t)))))
474 (defun insert-yearly-islamic-diary-entry (arg)
475 "Insert an annual diary entry.
476 For the day of the Islamic year corresponding to the date indicated by point.
477 Prefix arg will make the entry nonmarking."
479 (let* ((calendar-date-display-form
480 (if european-calendar-style
482 '(monthname " " day)))
483 (calendar-month-name-array calendar-islamic-month-name-array))
486 islamic-diary-entry-symbol
487 (calendar-date-string
488 (calendar-islamic-from-absolute
489 (calendar-absolute-from-gregorian
490 (calendar-cursor-to-date t)))))
495 ;;; cal-islam.el ends here