]> code.delx.au - gnu-emacs/blob - lisp/calendar/cal-islam.el
Add 2007 to copyright years.
[gnu-emacs] / lisp / calendar / cal-islam.el
1 ;;; cal-islam.el --- calendar functions for the Islamic calendar
2
3 ;; Copyright (C) 1995, 1997, 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 ;; Free Software Foundation, Inc.
5
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
10
11 ;; This file is part of GNU Emacs.
12
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 2, or (at your option)
16 ;; any later version.
17
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.
22
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.
27
28 ;;; Commentary:
29
30 ;; This collection of functions implements the features of calendar.el and
31 ;; diary.el that deal with the Islamic calendar.
32
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).
36
37 ;; Comments, corrections, and improvements should be sent to
38 ;; Edward M. Reingold Department of Computer Science
39 ;; (217) 333-6733 University of Illinois at Urbana-Champaign
40 ;; reingold@cs.uiuc.edu 1304 West Springfield Avenue
41 ;; Urbana, Illinois 61801
42
43 ;;; Code:
44
45 (defvar date)
46 (defvar displayed-month)
47 (defvar displayed-year)
48 (defvar number)
49 (defvar original-date)
50
51 (require 'cal-julian)
52
53 (defvar calendar-islamic-month-name-array
54 ["Muharram" "Safar" "Rabi I" "Rabi II" "Jumada I" "Jumada II"
55 "Rajab" "Sha'ban" "Ramadan" "Shawwal" "Dhu al-Qada" "Dhu al-Hijjah"]
56 "Array of strings giving the names of the Islamic months.")
57
58 (defvar calendar-islamic-epoch (calendar-absolute-from-julian '(7 16 622))
59 "Absolute date of start of Islamic calendar = August 29, 284 A.D. (Julian).")
60
61 (defun islamic-calendar-leap-year-p (year)
62 "Returns t if YEAR is a leap year on the Islamic calendar."
63 (memq (% year 30)
64 (list 2 5 7 10 13 16 18 21 24 26 29)))
65
66 (defun islamic-calendar-last-day-of-month (month year)
67 "The last day in MONTH during YEAR on the Islamic calendar."
68 (cond
69 ((memq month (list 1 3 5 7 9 11)) 30)
70 ((memq month (list 2 4 6 8 10)) 29)
71 (t (if (islamic-calendar-leap-year-p year) 30 29))))
72
73 (defun islamic-calendar-day-number (date)
74 "Return the day number within the year of the Islamic date DATE."
75 (let* ((month (extract-calendar-month date))
76 (day (extract-calendar-day date)))
77 (+ (* 30 (/ month 2))
78 (* 29 (/ (1- month) 2))
79 day)))
80
81 (defun calendar-absolute-from-islamic (date)
82 "Absolute date of Islamic 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 (y (% year 30))
89 (leap-years-in-cycle
90 (cond
91 ((< y 3) 0) ((< y 6) 1) ((< y 8) 2) ((< y 11) 3) ((< y 14) 4)
92 ((< y 17) 5) ((< y 19) 6) ((< y 22) 7) ((< y 25) 8) ((< y 27) 9)
93 (t 10))))
94 (+ (islamic-calendar-day-number date);; days so far this year
95 (* (1- year) 354) ;; days in all non-leap years
96 (* 11 (/ year 30)) ;; leap days in complete cycles
97 leap-years-in-cycle ;; leap days this cycle
98 (1- calendar-islamic-epoch)))) ;; days before start of calendar
99
100 (defun calendar-islamic-from-absolute (date)
101 "Compute the Islamic date (month day year) corresponding to absolute DATE.
102 The absolute date is the number of days elapsed since the (imaginary)
103 Gregorian date Sunday, December 31, 1 BC."
104 (if (< date calendar-islamic-epoch)
105 (list 0 0 0);; pre-Islamic date
106 (let* ((approx (/ (- date calendar-islamic-epoch)
107 355));; Approximation from below.
108 (year ;; Search forward from the approximation.
109 (+ approx
110 (calendar-sum y approx
111 (>= date (calendar-absolute-from-islamic
112 (list 1 1 (1+ y))))
113 1)))
114 (month ;; Search forward from Muharram.
115 (1+ (calendar-sum m 1
116 (> date
117 (calendar-absolute-from-islamic
118 (list m
119 (islamic-calendar-last-day-of-month
120 m year)
121 year)))
122 1)))
123 (day ;; Calculate the day by subtraction.
124 (- date
125 (1- (calendar-absolute-from-islamic (list month 1 year))))))
126 (list month day year))))
127
128 (defun calendar-islamic-date-string (&optional date)
129 "String of Islamic date before sunset of Gregorian DATE.
130 Returns the empty string if DATE is pre-Islamic.
131 Defaults to today's date if DATE is not given.
132 Driven by the variable `calendar-date-display-form'."
133 (let ((calendar-month-name-array calendar-islamic-month-name-array)
134 (islamic-date (calendar-islamic-from-absolute
135 (calendar-absolute-from-gregorian
136 (or date (calendar-current-date))))))
137 (if (< (extract-calendar-year islamic-date) 1)
138 ""
139 (calendar-date-string islamic-date nil t))))
140
141 (defun calendar-print-islamic-date ()
142 "Show the Islamic calendar equivalent of the date under the cursor."
143 (interactive)
144 (let ((i (calendar-islamic-date-string (calendar-cursor-to-date t))))
145 (if (string-equal i "")
146 (message "Date is pre-Islamic")
147 (message "Islamic date (until sunset): %s" i))))
148
149 (defun calendar-goto-islamic-date (date &optional noecho)
150 "Move cursor to Islamic DATE; echo Islamic date unless NOECHO is t."
151 (interactive
152 (let* ((today (calendar-current-date))
153 (year (calendar-read
154 "Islamic calendar year (>0): "
155 '(lambda (x) (> x 0))
156 (int-to-string
157 (extract-calendar-year
158 (calendar-islamic-from-absolute
159 (calendar-absolute-from-gregorian today))))))
160 (month-array calendar-islamic-month-name-array)
161 (completion-ignore-case t)
162 (month (cdr (assoc-string
163 (completing-read
164 "Islamic calendar month name: "
165 (mapcar 'list (append month-array nil))
166 nil t)
167 (calendar-make-alist month-array 1) t)))
168 (last (islamic-calendar-last-day-of-month month year))
169 (day (calendar-read
170 (format "Islamic calendar day (1-%d): " last)
171 '(lambda (x) (and (< 0 x) (<= x last))))))
172 (list (list month day year))))
173 (calendar-goto-date (calendar-gregorian-from-absolute
174 (calendar-absolute-from-islamic date)))
175 (or noecho (calendar-print-islamic-date)))
176
177 (defun diary-islamic-date ()
178 "Islamic calendar equivalent of date diary entry."
179 (let ((i (calendar-islamic-date-string date)))
180 (if (string-equal i "")
181 "Date is pre-Islamic"
182 (format "Islamic date (until sunset): %s" i))))
183
184 (defun holiday-islamic (month day string)
185 "Holiday on MONTH, DAY (Islamic) called STRING.
186 If MONTH, DAY (Islamic) is visible, the value returned is corresponding
187 Gregorian date in the form of the list (((month day year) STRING)). Returns
188 nil if it is not visible in the current calendar window."
189 (let* ((islamic-date (calendar-islamic-from-absolute
190 (calendar-absolute-from-gregorian
191 (list displayed-month 15 displayed-year))))
192 (m (extract-calendar-month islamic-date))
193 (y (extract-calendar-year islamic-date))
194 (date))
195 (if (< m 1)
196 nil;; Islamic calendar doesn't apply.
197 (increment-calendar-month m y (- 10 month))
198 (if (> m 7);; Islamic date might be visible
199 (let ((date (calendar-gregorian-from-absolute
200 (calendar-absolute-from-islamic (list month day y)))))
201 (if (calendar-date-is-visible-p date)
202 (list (list date string))))))))
203
204 (defun list-islamic-diary-entries ()
205 "Add any Islamic date entries from the diary file to `diary-entries-list'.
206 Islamic date diary entries must be prefaced by an `islamic-diary-entry-symbol'
207 \(normally an `I'). The same diary date forms govern the style of the Islamic
208 calendar entries, except that the Islamic month names must be spelled in full.
209 The Islamic months are numbered from 1 to 12 with Muharram being 1 and 12 being
210 Dhu al-Hijjah. If an Islamic date diary entry begins with a
211 `diary-nonmarking-symbol', the entry will appear in the diary listing, but will
212 not be marked in the calendar. This function is provided for use with the
213 `nongregorian-diary-listing-hook'."
214 (if (< 0 number)
215 (let ((buffer-read-only nil)
216 (diary-modified (buffer-modified-p))
217 (gdate original-date)
218 (mark (regexp-quote diary-nonmarking-symbol)))
219 (calendar-for-loop i from 1 to number do
220 (let* ((d diary-date-forms)
221 (idate (calendar-islamic-from-absolute
222 (calendar-absolute-from-gregorian gdate)))
223 (month (extract-calendar-month idate))
224 (day (extract-calendar-day idate))
225 (year (extract-calendar-year idate)))
226 (while d
227 (let*
228 ((date-form (if (equal (car (car d)) 'backup)
229 (cdr (car d))
230 (car d)))
231 (backup (equal (car (car d)) 'backup))
232 (dayname
233 (format "%s\\|%s\\.?"
234 (calendar-day-name gdate)
235 (calendar-day-name gdate 'abbrev)))
236 (calendar-month-name-array
237 calendar-islamic-month-name-array)
238 (monthname
239 (concat
240 "\\*\\|"
241 (calendar-month-name month)))
242 (month (concat "\\*\\|0*" (int-to-string month)))
243 (day (concat "\\*\\|0*" (int-to-string day)))
244 (year
245 (concat
246 "\\*\\|0*" (int-to-string year)
247 (if abbreviated-calendar-year
248 (concat "\\|" (int-to-string (% year 100)))
249 "")))
250 (regexp
251 (concat
252 "\\(\\`\\|\^M\\|\n\\)" mark "?"
253 (regexp-quote islamic-diary-entry-symbol)
254 "\\("
255 (mapconcat 'eval date-form "\\)\\(")
256 "\\)"))
257 (case-fold-search t))
258 (goto-char (point-min))
259 (while (re-search-forward regexp nil t)
260 (if backup (re-search-backward "\\<" nil t))
261 (if (and (or (char-equal (preceding-char) ?\^M)
262 (char-equal (preceding-char) ?\n))
263 (not (looking-at " \\|\^I")))
264 ;; Diary entry that consists only of date.
265 (backward-char 1)
266 ;; Found a nonempty diary entry--make it visible and
267 ;; add it to the list.
268 (let ((entry-start (point))
269 (date-start))
270 (re-search-backward "\^M\\|\n\\|\\`")
271 (setq date-start (point))
272 (re-search-forward "\^M\\|\n" nil t 2)
273 (while (looking-at " \\|\^I")
274 (re-search-forward "\^M\\|\n" nil t))
275 (backward-char 1)
276 (subst-char-in-region date-start (point) ?\^M ?\n t)
277 (add-to-diary-list
278 gdate
279 (buffer-substring-no-properties entry-start (point))
280 (buffer-substring-no-properties
281 (1+ date-start) (1- entry-start))
282 (copy-marker entry-start))))))
283 (setq d (cdr d))))
284 (setq gdate
285 (calendar-gregorian-from-absolute
286 (1+ (calendar-absolute-from-gregorian gdate)))))
287 (set-buffer-modified-p diary-modified))
288 (goto-char (point-min))))
289
290 (defun mark-islamic-diary-entries ()
291 "Mark days in the calendar window that have Islamic date diary entries.
292 Each entry in diary-file (or included files) visible in the calendar window
293 is marked. Islamic date entries are prefaced by a islamic-diary-entry-symbol
294 \(normally an `I'). The same diary-date-forms govern the style of the Islamic
295 calendar entries, except that the Islamic month names must be spelled in full.
296 The Islamic months are numbered from 1 to 12 with Muharram being 1 and 12 being
297 Dhu al-Hijjah. Islamic date diary entries that begin with a
298 diary-nonmarking-symbol will not be marked in the calendar. This function is
299 provided for use as part of the nongregorian-diary-marking-hook."
300 (let ((d diary-date-forms))
301 (while d
302 (let*
303 ((date-form (if (equal (car (car d)) 'backup)
304 (cdr (car d))
305 (car d)));; ignore 'backup directive
306 (dayname (diary-name-pattern calendar-day-name-array
307 calendar-day-abbrev-array))
308 (monthname
309 (format "%s\\|\\*"
310 (diary-name-pattern calendar-islamic-month-name-array)))
311 (month "[0-9]+\\|\\*")
312 (day "[0-9]+\\|\\*")
313 (year "[0-9]+\\|\\*")
314 (l (length date-form))
315 (d-name-pos (- l (length (memq 'dayname date-form))))
316 (d-name-pos (if (/= l d-name-pos) (+ 2 d-name-pos)))
317 (m-name-pos (- l (length (memq 'monthname date-form))))
318 (m-name-pos (if (/= l m-name-pos) (+ 2 m-name-pos)))
319 (d-pos (- l (length (memq 'day date-form))))
320 (d-pos (if (/= l d-pos) (+ 2 d-pos)))
321 (m-pos (- l (length (memq 'month date-form))))
322 (m-pos (if (/= l m-pos) (+ 2 m-pos)))
323 (y-pos (- l (length (memq 'year date-form))))
324 (y-pos (if (/= l y-pos) (+ 2 y-pos)))
325 (regexp
326 (concat
327 "\\(\\`\\|\^M\\|\n\\)"
328 (regexp-quote islamic-diary-entry-symbol)
329 "\\("
330 (mapconcat 'eval date-form "\\)\\(")
331 "\\)"))
332 (case-fold-search t))
333 (goto-char (point-min))
334 (while (re-search-forward regexp nil t)
335 (let* ((dd-name
336 (if d-name-pos
337 (buffer-substring
338 (match-beginning d-name-pos)
339 (match-end d-name-pos))))
340 (mm-name
341 (if m-name-pos
342 (buffer-substring
343 (match-beginning m-name-pos)
344 (match-end m-name-pos))))
345 (mm (string-to-number
346 (if m-pos
347 (buffer-substring
348 (match-beginning m-pos)
349 (match-end m-pos))
350 "")))
351 (dd (string-to-number
352 (if d-pos
353 (buffer-substring
354 (match-beginning d-pos)
355 (match-end d-pos))
356 "")))
357 (y-str (if y-pos
358 (buffer-substring
359 (match-beginning y-pos)
360 (match-end y-pos))))
361 (yy (if (not y-str)
362 0
363 (if (and (= (length y-str) 2)
364 abbreviated-calendar-year)
365 (let* ((current-y
366 (extract-calendar-year
367 (calendar-islamic-from-absolute
368 (calendar-absolute-from-gregorian
369 (calendar-current-date)))))
370 (y (+ (string-to-number y-str)
371 (* 100 (/ current-y 100)))))
372 (if (> (- y current-y) 50)
373 (- y 100)
374 (if (> (- current-y y) 50)
375 (+ y 100)
376 y)))
377 (string-to-number y-str)))))
378 (if dd-name
379 (mark-calendar-days-named
380 (cdr (assoc-string dd-name
381 (calendar-make-alist
382 calendar-day-name-array
383 0 nil calendar-day-abbrev-array) t)))
384 (if mm-name
385 (setq mm (if (string-equal mm-name "*") 0
386 (cdr (assoc-string
387 mm-name
388 (calendar-make-alist
389 calendar-islamic-month-name-array) t)))))
390 (mark-islamic-calendar-date-pattern mm dd yy)))))
391 (setq d (cdr d)))))
392
393 (defun mark-islamic-calendar-date-pattern (month day year)
394 "Mark dates in calendar window that conform to Islamic date MONTH/DAY/YEAR.
395 A value of 0 in any position is a wildcard."
396 (save-excursion
397 (set-buffer calendar-buffer)
398 (if (and (/= 0 month) (/= 0 day))
399 (if (/= 0 year)
400 ;; Fully specified Islamic date.
401 (let ((date (calendar-gregorian-from-absolute
402 (calendar-absolute-from-islamic
403 (list month day year)))))
404 (if (calendar-date-is-visible-p date)
405 (mark-visible-calendar-date date)))
406 ;; Month and day in any year--this taken from the holiday stuff.
407 (let* ((islamic-date (calendar-islamic-from-absolute
408 (calendar-absolute-from-gregorian
409 (list displayed-month 15 displayed-year))))
410 (m (extract-calendar-month islamic-date))
411 (y (extract-calendar-year islamic-date))
412 (date))
413 (if (< m 1)
414 nil;; Islamic calendar doesn't apply.
415 (increment-calendar-month m y (- 10 month))
416 (if (> m 7);; Islamic date might be visible
417 (let ((date (calendar-gregorian-from-absolute
418 (calendar-absolute-from-islamic
419 (list month day y)))))
420 (if (calendar-date-is-visible-p date)
421 (mark-visible-calendar-date date)))))))
422 ;; Not one of the simple cases--check all visible dates for match.
423 ;; Actually, the following code takes care of ALL of the cases, but
424 ;; it's much too slow to be used for the simple (common) cases.
425 (let ((m displayed-month)
426 (y displayed-year)
427 (first-date)
428 (last-date))
429 (increment-calendar-month m y -1)
430 (setq first-date
431 (calendar-absolute-from-gregorian
432 (list m 1 y)))
433 (increment-calendar-month m y 2)
434 (setq last-date
435 (calendar-absolute-from-gregorian
436 (list m (calendar-last-day-of-month m y) y)))
437 (calendar-for-loop date from first-date to last-date do
438 (let* ((i-date (calendar-islamic-from-absolute date))
439 (i-month (extract-calendar-month i-date))
440 (i-day (extract-calendar-day i-date))
441 (i-year (extract-calendar-year i-date)))
442 (and (or (zerop month)
443 (= month i-month))
444 (or (zerop day)
445 (= day i-day))
446 (or (zerop year)
447 (= year i-year))
448 (mark-visible-calendar-date
449 (calendar-gregorian-from-absolute date)))))))))
450
451 (defun insert-islamic-diary-entry (arg)
452 "Insert a diary entry.
453 For the Islamic date corresponding to the date indicated by point.
454 Prefix arg will make the entry nonmarking."
455 (interactive "P")
456 (let* ((calendar-month-name-array calendar-islamic-month-name-array))
457 (make-diary-entry
458 (concat
459 islamic-diary-entry-symbol
460 (calendar-date-string
461 (calendar-islamic-from-absolute
462 (calendar-absolute-from-gregorian
463 (calendar-cursor-to-date t)))
464 nil t))
465 arg)))
466
467 (defun insert-monthly-islamic-diary-entry (arg)
468 "Insert a monthly diary entry.
469 For the day of the Islamic month corresponding to the date indicated by point.
470 Prefix arg will make the entry nonmarking."
471 (interactive "P")
472 (let* ((calendar-date-display-form
473 (if european-calendar-style '(day " * ") '("* " day )))
474 (calendar-month-name-array calendar-islamic-month-name-array))
475 (make-diary-entry
476 (concat
477 islamic-diary-entry-symbol
478 (calendar-date-string
479 (calendar-islamic-from-absolute
480 (calendar-absolute-from-gregorian
481 (calendar-cursor-to-date t)))))
482 arg)))
483
484 (defun insert-yearly-islamic-diary-entry (arg)
485 "Insert an annual diary entry.
486 For the day of the Islamic year corresponding to the date indicated by point.
487 Prefix arg will make the entry nonmarking."
488 (interactive "P")
489 (let* ((calendar-date-display-form
490 (if european-calendar-style
491 '(day " " monthname)
492 '(monthname " " day)))
493 (calendar-month-name-array calendar-islamic-month-name-array))
494 (make-diary-entry
495 (concat
496 islamic-diary-entry-symbol
497 (calendar-date-string
498 (calendar-islamic-from-absolute
499 (calendar-absolute-from-gregorian
500 (calendar-cursor-to-date t)))))
501 arg)))
502
503 (provide 'cal-islam)
504
505 ;;; arch-tag: a951b6c1-6f47-48d5-bac3-1b505cd719f7
506 ;;; cal-islam.el ends here