cal-hebrew.el 54.7 KB
Newer Older
1
;;; cal-hebrew.el --- calendar functions for the Hebrew calendar
Edward M. Reingold's avatar
Edward M. Reingold committed
2

3 4
;; Copyright (C) 1995, 1997, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
;;   2008  Free Software Foundation, Inc.
Edward M. Reingold's avatar
Edward M. Reingold committed
5

Edward M. Reingold's avatar
Edward M. Reingold committed
6
;; Author: Nachum Dershowitz <nachum@cs.uiuc.edu>
Edward M. Reingold's avatar
Edward M. Reingold committed
7
;;      Edward M. Reingold <reingold@cs.uiuc.edu>
8
;; Maintainer: Glenn Morris <rgm@gnu.org>
Edward M. Reingold's avatar
Edward M. Reingold committed
9 10 11 12 13 14 15
;; Keywords: calendar
;; Human-Keywords: Hebrew calendar, calendar, diary

;; This file is part of GNU Emacs.

;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
16
;; the Free Software Foundation; either version 3, or (at your option)
Edward M. Reingold's avatar
Edward M. Reingold committed
17 18 19 20 21 22 23 24
;; any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
Erik Naggum's avatar
Erik Naggum committed
25
;; along with GNU Emacs; see the file COPYING.  If not, write to the
Lute Kamstra's avatar
Lute Kamstra committed
26 27
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
Edward M. Reingold's avatar
Edward M. Reingold committed
28 29 30 31 32 33

;;; Commentary:

;; This collection of functions implements the features of calendar.el and
;; diary.el that deal with the Hebrew calendar.

34
;; Technical details of all the calendrical calculations can be found in
35 36
;; ``Calendrical Calculations: The Millennium Edition'' by Edward M. Reingold
;; and Nachum Dershowitz, Cambridge University Press (2001).
37

Edward M. Reingold's avatar
Edward M. Reingold committed
38 39
;;; Code:

40
(defvar date)
41 42
(defvar displayed-month)
(defvar displayed-year)
43 44 45
(defvar entry)
(defvar number)
(defvar original-date)
Edward M. Reingold's avatar
Edward M. Reingold committed
46

47
(require 'calendar)
Edward M. Reingold's avatar
Edward M. Reingold committed
48 49

(defun hebrew-calendar-leap-year-p (year)
Glenn Morris's avatar
Glenn Morris committed
50
  "Non-nil if YEAR is a Hebrew calendar leap year."
Edward M. Reingold's avatar
Edward M. Reingold committed
51 52 53 54 55 56 57 58 59
  (< (% (1+ (* 7 year)) 19) 7))

(defun hebrew-calendar-last-month-of-year (year)
  "The last month of the Hebrew calendar YEAR."
  (if (hebrew-calendar-leap-year-p year)
      13
    12))

(defun hebrew-calendar-elapsed-days (year)
Glenn Morris's avatar
Glenn Morris committed
60
  "Days from Sunday before start of Hebrew calendar to mean conjunction of Tishri of Hebrew YEAR."
Edward M. Reingold's avatar
Edward M. Reingold committed
61
  (let* ((months-elapsed
Glenn Morris's avatar
Glenn Morris committed
62 63 64
          (+ (* 235 (/ (1- year) 19)) ; months in complete cycles so far
             (* 12 (% (1- year) 19))  ; regular months in this cycle
             (/ (1+ (* 7 (% (1- year) 19))) 19))) ; leap months this cycle
Edward M. Reingold's avatar
Edward M. Reingold committed
65 66 67 68 69
         (parts-elapsed (+ 204 (* 793 (% months-elapsed 1080))))
         (hours-elapsed (+ 5
                           (* 12 months-elapsed)
                           (* 793 (/ months-elapsed 1080))
                           (/ parts-elapsed 1080)))
Glenn Morris's avatar
Glenn Morris committed
70
         (parts				; conjunction parts
Edward M. Reingold's avatar
Edward M. Reingold committed
71
          (+ (* 1080 (% hours-elapsed 24)) (% parts-elapsed 1080)))
Glenn Morris's avatar
Glenn Morris committed
72
         (day				; conjunction day
Edward M. Reingold's avatar
Edward M. Reingold committed
73 74
          (+ 1 (* 29 months-elapsed) (/ hours-elapsed 24)))
         (alternative-day
Glenn Morris's avatar
Glenn Morris committed
75 76 77 78 79 80 81 82 83 84
          (if (or (>= parts 19440) ; if the new moon is at or after midday
                  (and (= (% day 7) 2)	; ...or is on a Tuesday...
                       (>= parts 9924) ; at 9 hours, 204 parts or later...
		       ;; of a common year...
                       (not (hebrew-calendar-leap-year-p year)))
                  (and (= (% day 7) 1)	; ...or is on a Monday...
                       (>= parts 16789) ; at 15 hours, 589 parts or later...
		       ;; at the end of a leap year.
                       (hebrew-calendar-leap-year-p (1- year))))
	      ;; Then postpone Rosh HaShanah one day.
Edward M. Reingold's avatar
Edward M. Reingold committed
85
              (1+ day)
Glenn Morris's avatar
Glenn Morris committed
86
	    ;; Else:
Edward M. Reingold's avatar
Edward M. Reingold committed
87
            day)))
Glenn Morris's avatar
Glenn Morris committed
88 89 90
    ;; If Rosh HaShanah would occur on Sunday, Wednesday, or Friday
    (if (memq (% alternative-day 7) (list 0 3 5))
	;; Then postpone it one (more) day and return.
Edward M. Reingold's avatar
Edward M. Reingold committed
91
        (1+ alternative-day)
Glenn Morris's avatar
Glenn Morris committed
92
      ;; Else return.
Edward M. Reingold's avatar
Edward M. Reingold committed
93 94 95 96 97 98 99 100
      alternative-day)))

(defun hebrew-calendar-days-in-year (year)
  "Number of days in Hebrew YEAR."
  (- (hebrew-calendar-elapsed-days (1+ year))
     (hebrew-calendar-elapsed-days year)))

(defun hebrew-calendar-long-heshvan-p (year)
Glenn Morris's avatar
Glenn Morris committed
101
  "Non-nil if Heshvan is long in Hebrew YEAR."
Edward M. Reingold's avatar
Edward M. Reingold committed
102 103 104
  (= (% (hebrew-calendar-days-in-year year) 10) 5))

(defun hebrew-calendar-short-kislev-p (year)
Glenn Morris's avatar
Glenn Morris committed
105
  "Non-nil if Kislev is short in Hebrew YEAR."
Edward M. Reingold's avatar
Edward M. Reingold committed
106 107
  (= (% (hebrew-calendar-days-in-year year) 10) 3))

108 109 110 111 112 113 114 115 116
(defun hebrew-calendar-last-day-of-month (month year)
  "The last day of MONTH in YEAR."
  (if (or (memq month (list 2 4 6 10 13))
          (and (= month 12) (not (hebrew-calendar-leap-year-p year)))
          (and (= month 8) (not (hebrew-calendar-long-heshvan-p year)))
          (and (= month 9) (hebrew-calendar-short-kislev-p year)))
      29
    30))

Edward M. Reingold's avatar
Edward M. Reingold committed
117 118 119 120 121 122 123
(defun calendar-absolute-from-hebrew (date)
  "Absolute date of Hebrew DATE.
The absolute date is the number of days elapsed since the (imaginary)
Gregorian date Sunday, December 31, 1 BC."
  (let* ((month (extract-calendar-month date))
         (day (extract-calendar-day date))
         (year (extract-calendar-year date)))
Glenn Morris's avatar
Glenn Morris committed
124 125 126
    (+ day				; days so far this month
       (if (< month 7)			; before Tishri
	   ;; Then add days in prior months this year before and after Nisan.
Edward M. Reingold's avatar
Edward M. Reingold committed
127 128 129 130 131 132
           (+ (calendar-sum
               m 7 (<= m (hebrew-calendar-last-month-of-year year))
               (hebrew-calendar-last-day-of-month m year))
              (calendar-sum
               m 1 (< m month)
               (hebrew-calendar-last-day-of-month m year)))
Glenn Morris's avatar
Glenn Morris committed
133
	 ;; Else add days in prior months this year.
Edward M. Reingold's avatar
Edward M. Reingold committed
134 135 136
         (calendar-sum
          m 7 (< m month)
          (hebrew-calendar-last-day-of-month m year)))
Glenn Morris's avatar
Glenn Morris committed
137 138
    (hebrew-calendar-elapsed-days year)	; days in prior years
    -1373429)))			; days elapsed before absolute date 1
Edward M. Reingold's avatar
Edward M. Reingold committed
139

140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
(defun calendar-hebrew-from-absolute (date)
  "Compute the Hebrew date (month day year) corresponding to absolute DATE.
The absolute date is the number of days elapsed since the (imaginary)
Gregorian date Sunday, December 31, 1 BC."
  (let* ((greg-date (calendar-gregorian-from-absolute date))
         (month (aref [9 10 11 12 1 2 3 4 7 7 7 8]
                 (1- (extract-calendar-month greg-date))))
         (day)
         (year (+ 3760 (extract-calendar-year greg-date))))
    (while (>= date (calendar-absolute-from-hebrew (list 7 1 (1+ year))))
        (setq year (1+ year)))
    (let ((length (hebrew-calendar-last-month-of-year year)))
      (while (> date
                (calendar-absolute-from-hebrew
                 (list month
                       (hebrew-calendar-last-day-of-month month year)
                       year)))
        (setq month (1+ (% month length)))))
    (setq day (1+
               (- date (calendar-absolute-from-hebrew (list month 1 year)))))
    (list month day year)))

Edward M. Reingold's avatar
Edward M. Reingold committed
162 163
(defvar calendar-hebrew-month-name-array-common-year
  ["Nisan" "Iyar" "Sivan" "Tammuz" "Av" "Elul" "Tishri"
164 165
   "Heshvan" "Kislev" "Teveth" "Shevat" "Adar"]
"Array of strings giving the names of the Hebrew months in a common year.")
Edward M. Reingold's avatar
Edward M. Reingold committed
166 167 168

(defvar calendar-hebrew-month-name-array-leap-year
  ["Nisan" "Iyar" "Sivan" "Tammuz" "Av" "Elul" "Tishri"
169 170
   "Heshvan" "Kislev" "Teveth" "Shevat" "Adar I" "Adar II"]
"Array of strings giving the names of the Hebrew months in a leap year.")
Edward M. Reingold's avatar
Edward M. Reingold committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202

(defun calendar-hebrew-date-string (&optional date)
  "String of Hebrew date before sunset of Gregorian DATE.
Defaults to today's date if DATE is not given.
Driven by the variable `calendar-date-display-form'."
  (let* ((hebrew-date (calendar-hebrew-from-absolute
                       (calendar-absolute-from-gregorian
                        (or date (calendar-current-date)))))
         (calendar-month-name-array
          (if (hebrew-calendar-leap-year-p (extract-calendar-year hebrew-date))
              calendar-hebrew-month-name-array-leap-year
            calendar-hebrew-month-name-array-common-year)))
    (calendar-date-string hebrew-date nil t)))

(defun calendar-print-hebrew-date ()
  "Show the Hebrew calendar equivalent of the date under the cursor."
  (interactive)
  (message "Hebrew date (until sunset): %s"
           (calendar-hebrew-date-string (calendar-cursor-to-date t))))

(defun hebrew-calendar-yahrzeit (death-date year)
  "Absolute date of the anniversary of Hebrew DEATH-DATE in Hebrew YEAR."
  (let* ((death-day (extract-calendar-day death-date))
         (death-month (extract-calendar-month death-date))
         (death-year (extract-calendar-year death-date)))
    (cond
     ;; If it's Heshvan 30 it depends on the first anniversary; if
     ;; that was not Heshvan 30, use the day before Kislev 1.
     ((and (= death-month 8)
           (= death-day 30)
           (not (hebrew-calendar-long-heshvan-p (1+ death-year))))
      (1- (calendar-absolute-from-hebrew (list 9 1 year))))
Glenn Morris's avatar
Glenn Morris committed
203 204
     ;; If it's Kislev 30 it depends on the first anniversary; if that
     ;; was not Kislev 30, use the day before Teveth 1.
Edward M. Reingold's avatar
Edward M. Reingold committed
205 206 207 208
     ((and (= death-month 9)
           (= death-day 30)
           (hebrew-calendar-short-kislev-p (1+ death-year)))
      (1- (calendar-absolute-from-hebrew (list 10 1 year))))
Glenn Morris's avatar
Glenn Morris committed
209 210
     ;; If it's Adar II, use the same day in last month of year (Adar
     ;; or Adar II).
Edward M. Reingold's avatar
Edward M. Reingold committed
211 212 213
     ((= death-month 13)
      (calendar-absolute-from-hebrew
       (list (hebrew-calendar-last-month-of-year year) death-day year)))
Glenn Morris's avatar
Glenn Morris committed
214 215
     ;; If it's the 30th in Adar I and year is not a leap year (so
     ;; Adar has only 29 days), use the last day in Shevat.
Edward M. Reingold's avatar
Edward M. Reingold committed
216 217 218 219 220 221 222 223 224 225 226 227 228 229
     ((and (= death-day 30)
           (= death-month 12)
           (not (hebrew-calendar-leap-year-p year)))
      (calendar-absolute-from-hebrew (list 11 30 year)))
     ;; In all other cases, use the normal anniversary of the date of death.
     (t (calendar-absolute-from-hebrew
         (list death-month death-day year))))))

(defun calendar-goto-hebrew-date (date &optional noecho)
  "Move cursor to Hebrew DATE; echo Hebrew date unless NOECHO is t."
  (interactive
   (let* ((today (calendar-current-date))
          (year (calendar-read
                 "Hebrew calendar year (>3760): "
Glenn Morris's avatar
Glenn Morris committed
230
                 (lambda (x) (> x 3760))
Edward M. Reingold's avatar
Edward M. Reingold committed
231 232 233 234 235 236 237 238
                 (int-to-string
                  (extract-calendar-year
                   (calendar-hebrew-from-absolute
                    (calendar-absolute-from-gregorian today))))))
          (month-array (if (hebrew-calendar-leap-year-p year)
                           calendar-hebrew-month-name-array-leap-year
                         calendar-hebrew-month-name-array-common-year))
          (completion-ignore-case t)
239
          (month (cdr (assoc-string
240 241 242 243
                       (completing-read
                        "Hebrew calendar month name: "
                        (mapcar 'list (append month-array nil))
                        (if (= year 3761)
Glenn Morris's avatar
Glenn Morris committed
244
                            (lambda (x)
245
                               (let ((m (cdr
246
                                         (assoc-string
247
                                          (car x)
248 249
                                          (calendar-make-alist month-array)
                                          t))))
250 251 252 253 254 255 256
                                 (< 0
                                    (calendar-absolute-from-hebrew
                                     (list m
                                           (hebrew-calendar-last-day-of-month
                                            m year)
                                           year))))))
                        t)
257
                       (calendar-make-alist month-array 1) t)))
Edward M. Reingold's avatar
Edward M. Reingold committed
258 259 260 261 262 263
          (last (hebrew-calendar-last-day-of-month month year))
          (first (if (and (= year 3761) (= month 10))
                     18 1))
          (day (calendar-read
                (format "Hebrew calendar day (%d-%d): "
                        first last)
Glenn Morris's avatar
Glenn Morris committed
264
                (lambda (x) (and (<= first x) (<= x last))))))
Edward M. Reingold's avatar
Edward M. Reingold committed
265 266 267 268 269 270 271 272 273 274
     (list (list month day year))))
  (calendar-goto-date (calendar-gregorian-from-absolute
                       (calendar-absolute-from-hebrew date)))
  (or noecho (calendar-print-hebrew-date)))

(defun holiday-hebrew (month day string)
  "Holiday on MONTH, DAY (Hebrew) called STRING.
If MONTH, DAY (Hebrew) is visible, the value returned is corresponding
Gregorian date in the form of the list (((month day year) STRING)).  Returns
nil if it is not visible in the current calendar window."
Glenn Morris's avatar
Glenn Morris committed
275 276 277
  ;; This test is only to speed things up a bit; it works fine without it.
  (if (memq displayed-month
            (list
Edward M. Reingold's avatar
Edward M. Reingold committed
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
             (if (< 11 month) (- month 11) (+ month 1))
             (if (< 10 month) (- month 10) (+ month 2))
             (if (<  9 month) (- month  9) (+ month 3))
             (if (<  8 month) (- month  8) (+ month 4))
             (if (<  7 month) (- month  7) (+ month 5))))
      (let ((m1 displayed-month)
            (y1 displayed-year)
            (m2 displayed-month)
            (y2 displayed-year)
            (year))
        (increment-calendar-month m1 y1 -1)
        (increment-calendar-month m2 y2 1)
        (let* ((start-date (calendar-absolute-from-gregorian
                            (list m1 1 y1)))
               (end-date (calendar-absolute-from-gregorian
                          (list m2 (calendar-last-day-of-month m2 y2) y2)))
               (hebrew-start (calendar-hebrew-from-absolute start-date))
               (hebrew-end (calendar-hebrew-from-absolute end-date))
               (hebrew-y1 (extract-calendar-year hebrew-start))
               (hebrew-y2 (extract-calendar-year hebrew-end)))
          (setq year (if (< 6 month) hebrew-y2 hebrew-y1))
          (let ((date (calendar-gregorian-from-absolute
                       (calendar-absolute-from-hebrew
                        (list month day year)))))
            (if (calendar-date-is-visible-p date)
                (list (list date string))))))))

305 306 307
;; h-r-h-e should be called from holidays code.
(declare-function holiday-filter-visible-calendar "holidays" (l))

Edward M. Reingold's avatar
Edward M. Reingold committed
308 309 310 311
(defun holiday-rosh-hashanah-etc ()
  "List of dates related to Rosh Hashanah, as visible in calendar window."
  (if (or (< displayed-month 8)
          (> displayed-month 11))
Glenn Morris's avatar
Glenn Morris committed
312
      nil				; none of the dates is visible
Edward M. Reingold's avatar
Edward M. Reingold committed
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
    (let* ((abs-r-h (calendar-absolute-from-hebrew
                      (list 7 1 (+ displayed-year 3761))))
            (mandatory
             (list
              (list (calendar-gregorian-from-absolute abs-r-h)
                    (format "Rosh HaShanah %d" (+ 3761 displayed-year)))
              (list (calendar-gregorian-from-absolute (+ abs-r-h 9))
                    "Yom Kippur")
              (list (calendar-gregorian-from-absolute (+ abs-r-h 14))
                    "Sukkot")
              (list (calendar-gregorian-from-absolute (+ abs-r-h 21))
                    "Shemini Atzeret")
              (list (calendar-gregorian-from-absolute (+ abs-r-h 22))
                    "Simchat Torah")))
           (optional
328
            (list
Edward M. Reingold's avatar
Edward M. Reingold committed
329 330 331 332
             (list (calendar-gregorian-from-absolute
                    (calendar-dayname-on-or-before 6 (- abs-r-h 4)))
                   "Selichot (night)")
             (list (calendar-gregorian-from-absolute (1- abs-r-h))
333
                   "Erev Rosh HaShanah")
Edward M. Reingold's avatar
Edward M. Reingold committed
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
             (list (calendar-gregorian-from-absolute (1+ abs-r-h))
                   "Rosh HaShanah (second day)")
             (list (calendar-gregorian-from-absolute
                    (if (= (% abs-r-h 7) 4) (+ abs-r-h 3) (+ abs-r-h 2)))
                   "Tzom Gedaliah")
             (list (calendar-gregorian-from-absolute
                    (calendar-dayname-on-or-before 6 (+ 7 abs-r-h)))
                   "Shabbat Shuvah")
             (list (calendar-gregorian-from-absolute (+ abs-r-h 8))
                   "Erev Yom Kippur")
             (list (calendar-gregorian-from-absolute (+ abs-r-h 13))
                   "Erev Sukkot")
             (list (calendar-gregorian-from-absolute (+ abs-r-h 15))
                   "Sukkot (second day)")
             (list (calendar-gregorian-from-absolute (+ abs-r-h 16))
                   "Hol Hamoed Sukkot (first day)")
             (list (calendar-gregorian-from-absolute (+ abs-r-h 17))
                   "Hol Hamoed Sukkot (second day)")
             (list (calendar-gregorian-from-absolute (+ abs-r-h 18))
                   "Hol Hamoed Sukkot (third day)")
             (list (calendar-gregorian-from-absolute (+ abs-r-h 19))
                   "Hol Hamoed Sukkot (fourth day)")
             (list (calendar-gregorian-from-absolute (+ abs-r-h 20))
357
                   "Hoshanah Rabbah")))
Edward M. Reingold's avatar
Edward M. Reingold committed
358
            (output-list
359
             (holiday-filter-visible-calendar mandatory)))
Edward M. Reingold's avatar
Edward M. Reingold committed
360 361
      (if all-hebrew-calendar-holidays
          (setq output-list
362
                (append
363
                 (holiday-filter-visible-calendar optional)
Edward M. Reingold's avatar
Edward M. Reingold committed
364 365 366 367 368
                 output-list)))
      output-list)))

(defun holiday-hanukkah ()
  "List of dates related to Hanukkah, as visible in calendar window."
Glenn Morris's avatar
Glenn Morris committed
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
  ;; This test is only to speed things up a bit, it works fine without it.
  (if (memq displayed-month
	    '(10 11 12 1 2))
      (let ((m displayed-month)
	    (y displayed-year))
	(increment-calendar-month m y 1)
	(let* ((h-y (extract-calendar-year
		     (calendar-hebrew-from-absolute
		      (calendar-absolute-from-gregorian
		       (list m (calendar-last-day-of-month m y) y)))))
	       (abs-h (calendar-absolute-from-hebrew (list 9 25 h-y))))
	  (holiday-filter-visible-calendar
	   (list
	    (list (calendar-gregorian-from-absolute (1- abs-h))
		  "Erev Hanukkah")
	    (list (calendar-gregorian-from-absolute abs-h)
		  "Hanukkah (first day)")
	    (list (calendar-gregorian-from-absolute (1+ abs-h))
		  "Hanukkah (second day)")
	    (list (calendar-gregorian-from-absolute (+ abs-h 2))
		  "Hanukkah (third day)")
	    (list (calendar-gregorian-from-absolute (+ abs-h 3))
		  "Hanukkah (fourth day)")
	    (list (calendar-gregorian-from-absolute (+ abs-h 4))
		  "Hanukkah (fifth day)")
	    (list (calendar-gregorian-from-absolute (+ abs-h 5))
		  "Hanukkah (sixth day)")
	    (list (calendar-gregorian-from-absolute (+ abs-h 6))
		  "Hanukkah (seventh day)")
	    (list (calendar-gregorian-from-absolute (+ abs-h 7))
		  "Hanukkah (eighth day)")))))))
Edward M. Reingold's avatar
Edward M. Reingold committed
400 401 402 403

(defun holiday-passover-etc ()
  "List of dates related to Passover, as visible in calendar window."
 (if (< 7 displayed-month)
Glenn Morris's avatar
Glenn Morris committed
404
      nil				; none of the dates is visible
Edward M. Reingold's avatar
Edward M. Reingold committed
405 406 407 408 409 410 411 412 413
    (let* ((abs-p (calendar-absolute-from-hebrew
                      (list 1 15 (+ displayed-year 3760))))
           (mandatory
            (list
             (list (calendar-gregorian-from-absolute abs-p)
                   "Passover")
             (list (calendar-gregorian-from-absolute (+ abs-p 50))
                   "Shavuot")))
           (optional
414
            (list
Edward M. Reingold's avatar
Edward M. Reingold committed
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
             (list (calendar-gregorian-from-absolute
                    (calendar-dayname-on-or-before 6 (- abs-p 43)))
                   "Shabbat Shekalim")
             (list (calendar-gregorian-from-absolute
                    (calendar-dayname-on-or-before 6 (- abs-p 30)))
                   "Shabbat Zachor")
             (list (calendar-gregorian-from-absolute
                    (if (= (% abs-p 7) 2) (- abs-p 33) (- abs-p 31)))
                   "Fast of Esther")
             (list (calendar-gregorian-from-absolute (- abs-p 31))
                   "Erev Purim")
             (list (calendar-gregorian-from-absolute (- abs-p 30))
                   "Purim")
             (list (calendar-gregorian-from-absolute
                    (if (zerop (% abs-p 7)) (- abs-p 28) (- abs-p 29)))
                   "Shushan Purim")
             (list (calendar-gregorian-from-absolute
                    (- (calendar-dayname-on-or-before 6 (- abs-p 14)) 7))
                   "Shabbat Parah")
             (list (calendar-gregorian-from-absolute
                    (calendar-dayname-on-or-before 6 (- abs-p 14)))
                   "Shabbat HaHodesh")
             (list (calendar-gregorian-from-absolute
                    (calendar-dayname-on-or-before 6 (1- abs-p)))
                   "Shabbat HaGadol")
             (list (calendar-gregorian-from-absolute (1- abs-p))
                   "Erev Passover")
             (list (calendar-gregorian-from-absolute (1+ abs-p))
                   "Passover (second day)")
             (list (calendar-gregorian-from-absolute (+ abs-p 2))
                   "Hol Hamoed Passover (first day)")
             (list (calendar-gregorian-from-absolute (+ abs-p 3))
                   "Hol Hamoed Passover (second day)")
             (list (calendar-gregorian-from-absolute (+ abs-p 4))
                   "Hol Hamoed Passover (third day)")
             (list (calendar-gregorian-from-absolute (+ abs-p 5))
                   "Hol Hamoed Passover (fourth day)")
             (list (calendar-gregorian-from-absolute (+ abs-p 6))
                   "Passover (seventh day)")
             (list (calendar-gregorian-from-absolute (+ abs-p 7))
                   "Passover (eighth day)")
456 457 458 459
             (list (calendar-gregorian-from-absolute
                    (if (zerop (% (+ abs-p 12) 7))
                        (+ abs-p 13)
                      (+ abs-p 12)))
Edward M. Reingold's avatar
Edward M. Reingold committed
460 461 462 463 464 465 466 467 468 469 470
                   "Yom HaShoah")
             (list (calendar-gregorian-from-absolute
                    (if (zerop (% abs-p 7))
                        (+ abs-p 18)
                      (if (= (% abs-p 7) 6)
                          (+ abs-p 19)
                        (+ abs-p 20))))
                   "Yom HaAtzma'ut")
             (list (calendar-gregorian-from-absolute (+ abs-p 33))
                   "Lag BaOmer")
             (list (calendar-gregorian-from-absolute (+ abs-p 43))
Edward M. Reingold's avatar
Edward M. Reingold committed
471
                   "Yom Yerushalaim")
Edward M. Reingold's avatar
Edward M. Reingold committed
472 473 474 475 476
             (list (calendar-gregorian-from-absolute (+ abs-p 49))
                   "Erev Shavuot")
             (list (calendar-gregorian-from-absolute (+ abs-p 51))
                   "Shavuot (second day)")))
           (output-list
477
             (holiday-filter-visible-calendar mandatory)))
Edward M. Reingold's avatar
Edward M. Reingold committed
478 479
      (if all-hebrew-calendar-holidays
          (setq output-list
480
                (append
481
                 (holiday-filter-visible-calendar optional)
Edward M. Reingold's avatar
Edward M. Reingold committed
482 483 484 485 486 487 488
                 output-list)))
      output-list)))

(defun holiday-tisha-b-av-etc ()
  "List of dates around Tisha B'Av, as visible in calendar window."
  (if (or (< displayed-month 5)
          (> displayed-month 9))
Glenn Morris's avatar
Glenn Morris committed
489
      nil				; none of the dates is visible
Edward M. Reingold's avatar
Edward M. Reingold committed
490 491 492
    (let* ((abs-t-a (calendar-absolute-from-hebrew
                      (list 5 9 (+ displayed-year 3760)))))

493
      (holiday-filter-visible-calendar
494
       (list
Edward M. Reingold's avatar
Edward M. Reingold committed
495 496 497 498 499 500 501 502 503 504 505 506 507
        (list (calendar-gregorian-from-absolute
               (if (= (% abs-t-a 7) 6) (- abs-t-a 20) (- abs-t-a 21)))
              "Tzom Tammuz")
        (list (calendar-gregorian-from-absolute
               (calendar-dayname-on-or-before 6 abs-t-a))
              "Shabbat Hazon")
        (list (calendar-gregorian-from-absolute
               (if (= (% abs-t-a 7) 6) (1+ abs-t-a) abs-t-a))
              "Tisha B'Av")
        (list (calendar-gregorian-from-absolute
               (calendar-dayname-on-or-before 6 (+ abs-t-a 7)))
              "Shabbat Nahamu"))))))

508 509 510 511
;; l-h-d-e should be called from diary code.
(declare-function add-to-diary-list "diary-lib"
                  (date string specifier &optional marker globcolor literal))

Edward M. Reingold's avatar
Edward M. Reingold committed
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
(defun list-hebrew-diary-entries ()
  "Add any Hebrew date entries from the diary file to `diary-entries-list'.
Hebrew date diary entries must be prefaced by `hebrew-diary-entry-symbol'
\(normally an `H').  The same diary date forms govern the style of the Hebrew
calendar entries, except that the Hebrew month names must be spelled in full.
The Hebrew months are numbered from 1 to 13 with Nisan being 1, 12 being
Adar I and 13 being Adar II; you must use `Adar I' if you want Adar of a
common Hebrew year.  If a Hebrew date diary entry begins with a
`diary-nonmarking-symbol', the entry will appear in the diary listing, but will
not be marked in the calendar.  This function is provided for use with the
`nongregorian-diary-listing-hook'."
  (if (< 0 number)
      (let ((buffer-read-only nil)
            (diary-modified (buffer-modified-p))
            (gdate original-date)
            (mark (regexp-quote diary-nonmarking-symbol)))
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
        (dotimes (idummy number)
          (let* ((d diary-date-forms)
                 (hdate (calendar-hebrew-from-absolute
                         (calendar-absolute-from-gregorian gdate)))
                 (month (extract-calendar-month hdate))
                 (day (extract-calendar-day hdate))
                 (year (extract-calendar-year hdate)))
            (while d
              (let*
                  ((date-form (if (equal (car (car d)) 'backup)
                                  (cdr (car d))
                                (car d)))
                   (backup (equal (car (car d)) 'backup))
                   (dayname
                    (format "%s\\|%s\\.?"
                            (calendar-day-name gdate)
                            (calendar-day-name gdate 'abbrev)))
                   (calendar-month-name-array
                    calendar-hebrew-month-name-array-leap-year)
                   (monthname
                    (concat
                     "\\*\\|"
                     (calendar-month-name month)))
                   (month (concat "\\*\\|0*" (int-to-string month)))
                   (day (concat "\\*\\|0*" (int-to-string day)))
                   (year
                    (concat
                     "\\*\\|0*" (int-to-string year)
                     (if abbreviated-calendar-year
                         (concat "\\|" (int-to-string (% year 100)))
                       "")))
                   (regexp
                    (concat
                     "\\(\\`\\|\^M\\|\n\\)" mark "?"
                     (regexp-quote hebrew-diary-entry-symbol)
                     "\\("
                     (mapconcat 'eval date-form "\\)\\(")
                     "\\)"))
                   (case-fold-search t))
                (goto-char (point-min))
                (while (re-search-forward regexp nil t)
                  (if backup (re-search-backward "\\<" nil t))
                  (if (and (or (char-equal (preceding-char) ?\^M)
                               (char-equal (preceding-char) ?\n))
                           (not (looking-at " \\|\^I")))
Glenn Morris's avatar
Glenn Morris committed
573
                      ;; Diary entry that consists only of date.
574
                      (backward-char 1)
Glenn Morris's avatar
Glenn Morris committed
575 576
                    ;; Found a nonempty diary entry--make it visible and
                    ;; add it to the list.
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
                    (let ((entry-start (point))
                          (date-start))
                      (re-search-backward "\^M\\|\n\\|\\`")
                      (setq date-start (point))
                      (re-search-forward "\^M\\|\n" nil t 2)
                      (while (looking-at " \\|\^I")
                        (re-search-forward "\^M\\|\n" nil t))
                      (backward-char 1)
                      (subst-char-in-region date-start (point) ?\^M ?\n t)
                      (add-to-diary-list
                       gdate
                       (buffer-substring-no-properties entry-start (point))
                       (buffer-substring-no-properties
                        (1+ date-start) (1- entry-start))
                       (copy-marker entry-start))))))
              (setq d (cdr d))))
          (setq gdate
                (calendar-gregorian-from-absolute
                 (1+ (calendar-absolute-from-gregorian gdate)))))
        (set-buffer-modified-p diary-modified))
    (goto-char (point-min))))
Edward M. Reingold's avatar
Edward M. Reingold committed
598

599 600 601 602 603
(defun mark-hebrew-calendar-date-pattern (month day year)
  "Mark dates in calendar window that conform to Hebrew date MONTH/DAY/YEAR.
A value of 0 in any position is a wildcard."
  (save-excursion
    (set-buffer calendar-buffer)
604 605
    (if (and (not (zerop month)) (not (zerop day)))
        (if (not (zerop year))
606 607 608 609 610 611 612
            ;; Fully specified Hebrew date.
            (let ((date (calendar-gregorian-from-absolute
                         (calendar-absolute-from-hebrew
                          (list month day year)))))
              (if (calendar-date-is-visible-p date)
                  (mark-visible-calendar-date date)))
          ;; Month and day in any year--this taken from the holiday stuff.
Glenn Morris's avatar
Glenn Morris committed
613 614 615 616
	  ;; This test is only to speed things up a bit, it works
	  ;; fine without it.
          (if (memq displayed-month
                    (list
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
                     (if (< 11 month) (- month 11) (+ month 1))
                     (if (< 10 month) (- month 10) (+ month 2))
                     (if (<  9 month) (- month  9) (+ month 3))
                     (if (<  8 month) (- month  8) (+ month 4))
                     (if (<  7 month) (- month  7) (+ month 5))))
              (let ((m1 displayed-month)
                    (y1 displayed-year)
                    (m2 displayed-month)
                    (y2 displayed-year)
                    (year))
                (increment-calendar-month m1 y1 -1)
                (increment-calendar-month m2 y2 1)
                (let* ((start-date (calendar-absolute-from-gregorian
                                    (list m1 1 y1)))
                       (end-date (calendar-absolute-from-gregorian
                                  (list m2
                                        (calendar-last-day-of-month m2 y2)
                                        y2)))
                       (hebrew-start
                        (calendar-hebrew-from-absolute start-date))
                       (hebrew-end (calendar-hebrew-from-absolute end-date))
                       (hebrew-y1 (extract-calendar-year hebrew-start))
                       (hebrew-y2 (extract-calendar-year hebrew-end)))
                  (setq year (if (< 6 month) hebrew-y2 hebrew-y1))
                  (let ((date (calendar-gregorian-from-absolute
                               (calendar-absolute-from-hebrew
                                (list month day year)))))
                    (if (calendar-date-is-visible-p date)
                        (mark-visible-calendar-date date)))))))
      ;; Not one of the simple cases--check all visible dates for match.
      ;; Actually, the following code takes care of ALL of the cases, but
      ;; it's much too slow to be used for the simple (common) cases.
      (let ((m displayed-month)
            (y displayed-year)
            (first-date)
            (last-date))
        (increment-calendar-month m y -1)
        (setq first-date
              (calendar-absolute-from-gregorian
               (list m 1 y)))
        (increment-calendar-month m y 2)
        (setq last-date
              (calendar-absolute-from-gregorian
               (list m (calendar-last-day-of-month m y) y)))
        (calendar-for-loop date from first-date to last-date do
          (let* ((h-date (calendar-hebrew-from-absolute date))
                 (h-month (extract-calendar-month h-date))
                 (h-day (extract-calendar-day h-date))
                 (h-year (extract-calendar-year h-date)))
            (and (or (zerop month)
                     (= month h-month))
                 (or (zerop day)
                     (= day h-day))
                 (or (zerop year)
                     (= year h-year))
                 (mark-visible-calendar-date
                  (calendar-gregorian-from-absolute date)))))))))

675 676 677 678 679 680
(declare-function diary-name-pattern "diary-lib"
                  (string-array &optional abbrev-array paren))

(declare-function mark-calendar-days-named "diary-lib"
                  (dayname &optional color))

Edward M. Reingold's avatar
Edward M. Reingold committed
681 682
(defun mark-hebrew-diary-entries ()
  "Mark days in the calendar window that have Hebrew date diary entries.
Glenn Morris's avatar
Glenn Morris committed
683 684 685
Each entry in `diary-file' (or included files) visible in the calendar window
is marked.  Hebrew date entries are prefaced by `hebrew-diary-entry-symbol'
\(normally an `H').  The same `diary-date-forms' govern the style of the Hebrew
Edward M. Reingold's avatar
Edward M. Reingold committed
686 687 688
calendar entries, except that the Hebrew month names must be spelled in full.
The Hebrew months are numbered from 1 to 13 with Nisan being 1, 12 being
Adar I and 13 being Adar II; you must use `Adar I' if you want Adar of a
Glenn Morris's avatar
Glenn Morris committed
689 690 691
common Hebrew year.  Hebrew date diary entries that begin with
`diary-nonmarking-symbol' will not be marked in the calendar.  This function
is provided for use as part of `nongregorian-diary-marking-hook'."
Edward M. Reingold's avatar
Edward M. Reingold committed
692 693 694 695 696
  (let ((d diary-date-forms))
    (while d
      (let*
          ((date-form (if (equal (car (car d)) 'backup)
                          (cdr (car d))
Glenn Morris's avatar
Glenn Morris committed
697
                        (car d)))	; ignore 'backup directive
698 699
           (dayname (diary-name-pattern calendar-day-name-array
                                        calendar-day-abbrev-array))
Edward M. Reingold's avatar
Edward M. Reingold committed
700
           (monthname
701 702 703
            (format "%s\\|\\*"
                    (diary-name-pattern
                     calendar-hebrew-month-name-array-leap-year)))
Edward M. Reingold's avatar
Edward M. Reingold committed
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
           (month "[0-9]+\\|\\*")
           (day "[0-9]+\\|\\*")
           (year "[0-9]+\\|\\*")
           (l (length date-form))
           (d-name-pos (- l (length (memq 'dayname date-form))))
           (d-name-pos (if (/= l d-name-pos) (+ 2 d-name-pos)))
           (m-name-pos (- l (length (memq 'monthname date-form))))
           (m-name-pos (if (/= l m-name-pos) (+ 2 m-name-pos)))
           (d-pos (- l (length (memq 'day date-form))))
           (d-pos (if (/= l d-pos) (+ 2 d-pos)))
           (m-pos (- l (length (memq 'month date-form))))
           (m-pos (if (/= l m-pos) (+ 2 m-pos)))
           (y-pos (- l (length (memq 'year date-form))))
           (y-pos (if (/= l y-pos) (+ 2 y-pos)))
           (regexp
            (concat
             "\\(\\`\\|\^M\\|\n\\)"
             (regexp-quote hebrew-diary-entry-symbol)
             "\\("
             (mapconcat 'eval date-form "\\)\\(")
             "\\)"))
           (case-fold-search t))
        (goto-char (point-min))
        (while (re-search-forward regexp nil t)
          (let* ((dd-name
                  (if d-name-pos
                      (buffer-substring
                       (match-beginning d-name-pos)
                       (match-end d-name-pos))))
                 (mm-name
                  (if m-name-pos
                      (buffer-substring
                       (match-beginning m-name-pos)
                       (match-end m-name-pos))))
738
                 (mm (string-to-number
Edward M. Reingold's avatar
Edward M. Reingold committed
739 740 741 742 743
                      (if m-pos
                          (buffer-substring
                           (match-beginning m-pos)
                           (match-end m-pos))
                        "")))
744
                 (dd (string-to-number
Edward M. Reingold's avatar
Edward M. Reingold committed
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
                      (if d-pos
                          (buffer-substring
                           (match-beginning d-pos)
                           (match-end d-pos))
                        "")))
                 (y-str (if y-pos
                            (buffer-substring
                             (match-beginning y-pos)
                             (match-end y-pos))))
                 (yy (if (not y-str)
                         0
                       (if (and (= (length y-str) 2)
                                abbreviated-calendar-year)
                           (let* ((current-y
                                   (extract-calendar-year
                                    (calendar-hebrew-from-absolute
                                     (calendar-absolute-from-gregorian
                                      (calendar-current-date)))))
763
                                  (y (+ (string-to-number y-str)
Edward M. Reingold's avatar
Edward M. Reingold committed
764 765 766 767 768 769
                                        (* 100 (/ current-y 100)))))
                             (if (> (- y current-y) 50)
                                 (- y 100)
                               (if (> (- current-y y) 50)
                                   (+ y 100)
                                 y)))
770
                         (string-to-number y-str)))))
Edward M. Reingold's avatar
Edward M. Reingold committed
771 772
            (if dd-name
                (mark-calendar-days-named
773
                 (cdr (assoc-string dd-name
774 775
                                         (calendar-make-alist
                                          calendar-day-name-array
776
                                          0 nil calendar-day-abbrev-array) t)))
Edward M. Reingold's avatar
Edward M. Reingold committed
777
              (if mm-name
778 779 780
                  (setq mm
                        (if (string-equal mm-name "*") 0
                          (cdr
781
                           (assoc-string
782 783
                            mm-name
                            (calendar-make-alist
784
                             calendar-hebrew-month-name-array-leap-year) t)))))
Edward M. Reingold's avatar
Edward M. Reingold committed
785 786 787 788 789 790
              (mark-hebrew-calendar-date-pattern mm dd yy)))))
      (setq d (cdr d)))))

(defun insert-hebrew-diary-entry (arg)
  "Insert a diary entry.
For the Hebrew date corresponding to the date indicated by point.
Glenn Morris's avatar
Glenn Morris committed
791
Prefix argument ARG makes the entry nonmarking."
Edward M. Reingold's avatar
Edward M. Reingold committed
792 793 794 795 796 797
  (interactive "P")
  (let* ((calendar-month-name-array
          calendar-hebrew-month-name-array-leap-year))
    (make-diary-entry
     (concat
      hebrew-diary-entry-symbol
798
      (calendar-date-string
Edward M. Reingold's avatar
Edward M. Reingold committed
799 800 801 802 803 804 805 806 807
       (calendar-hebrew-from-absolute
        (calendar-absolute-from-gregorian
         (calendar-cursor-to-date t)))
       nil t))
     arg)))

(defun insert-monthly-hebrew-diary-entry (arg)
  "Insert a monthly diary entry.
For the day of the Hebrew month corresponding to the date indicated by point.
Glenn Morris's avatar
Glenn Morris committed
808
Prefix argument ARG makes the entry nonmarking."
Edward M. Reingold's avatar
Edward M. Reingold committed
809 810 811 812 813 814 815 816
  (interactive "P")
  (let* ((calendar-date-display-form
          (if european-calendar-style '(day " * ") '("* " day )))
         (calendar-month-name-array
          calendar-hebrew-month-name-array-leap-year))
    (make-diary-entry
     (concat
      hebrew-diary-entry-symbol
817
      (calendar-date-string
Edward M. Reingold's avatar
Edward M. Reingold committed
818 819 820 821 822 823 824 825
       (calendar-hebrew-from-absolute
        (calendar-absolute-from-gregorian
         (calendar-cursor-to-date t)))))
     arg)))

(defun insert-yearly-hebrew-diary-entry (arg)
  "Insert an annual diary entry.
For the day of the Hebrew year corresponding to the date indicated by point.
Glenn Morris's avatar
Glenn Morris committed
826
Prefix argument ARG makes the entry nonmarking."
Edward M. Reingold's avatar
Edward M. Reingold committed
827 828 829 830 831 832 833 834 835 836
  (interactive "P")
  (let* ((calendar-date-display-form
          (if european-calendar-style
              '(day " " monthname)
            '(monthname " " day)))
         (calendar-month-name-array
          calendar-hebrew-month-name-array-leap-year))
    (make-diary-entry
     (concat
      hebrew-diary-entry-symbol
837
      (calendar-date-string
Edward M. Reingold's avatar
Edward M. Reingold committed
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
       (calendar-hebrew-from-absolute
        (calendar-absolute-from-gregorian
         (calendar-cursor-to-date t)))))
     arg)))

;;;###autoload
(defun list-yahrzeit-dates (death-date start-year end-year)
  "List Yahrzeit dates for *Gregorian* DEATH-DATE from START-YEAR to END-YEAR.
When called interactively from the calendar window, the date of death is taken
from the cursor position."
  (interactive
   (let* ((death-date
           (if (equal (current-buffer) (get-buffer calendar-buffer))
               (calendar-cursor-to-date)
             (let* ((today (calendar-current-date))
                    (year (calendar-read
                           "Year of death (>0): "
Glenn Morris's avatar
Glenn Morris committed
855
                           (lambda (x) (> x 0))
Edward M. Reingold's avatar
Edward M. Reingold committed
856 857 858
                           (int-to-string (extract-calendar-year today))))
                    (month-array calendar-month-name-array)
                    (completion-ignore-case t)
859
                    (month (cdr (assoc-string
860 861 862 863
                                 (completing-read
                                  "Month of death (name): "
                                  (mapcar 'list (append month-array nil))
                                  nil t)
864
                                 (calendar-make-alist month-array 1) t)))
Edward M. Reingold's avatar
Edward M. Reingold committed
865 866 867
                    (last (calendar-last-day-of-month month year))
                    (day (calendar-read
                          (format "Day of death (1-%d): " last)
Glenn Morris's avatar
Glenn Morris committed
868
                          (lambda (x) (and (< 0 x) (<= x last))))))
Edward M. Reingold's avatar
Edward M. Reingold committed
869 870 871 872 873
               (list month day year))))
          (death-year (extract-calendar-year death-date))
          (start-year (calendar-read
                       (format "Starting year of Yahrzeit table (>%d): "
                               death-year)
Glenn Morris's avatar
Glenn Morris committed
874
                       (lambda (x) (> x death-year))
Edward M. Reingold's avatar
Edward M. Reingold committed
875 876 877 878
                       (int-to-string (1+ death-year))))
          (end-year (calendar-read
                     (format "Ending year of Yahrzeit table (>=%d): "
                             start-year)
Glenn Morris's avatar
Glenn Morris committed
879
                       (lambda (x) (>= x start-year)))))
Edward M. Reingold's avatar
Edward M. Reingold committed
880
   (list death-date start-year end-year)))
Glenn Morris's avatar
Glenn Morris committed
881
  (message "Computing Yahrzeits...")
Edward M. Reingold's avatar
Edward M. Reingold committed
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
  (let* ((yahrzeit-buffer "*Yahrzeits*")
         (h-date (calendar-hebrew-from-absolute
                  (calendar-absolute-from-gregorian death-date)))
         (h-month (extract-calendar-month h-date))
         (h-day (extract-calendar-day h-date))
         (h-year (extract-calendar-year h-date)))
    (set-buffer (get-buffer-create yahrzeit-buffer))
    (setq buffer-read-only nil)
    (calendar-set-mode-line
     (format "Yahrzeit dates for %s = %s"
             (calendar-date-string death-date)
             (let ((calendar-month-name-array
                    (if (hebrew-calendar-leap-year-p h-year)
                        calendar-hebrew-month-name-array-leap-year
                      calendar-hebrew-month-name-array-common-year)))
               (calendar-date-string h-date nil t))))
    (erase-buffer)
    (goto-char (point-min))
    (calendar-for-loop i from start-year to end-year do
        (insert
         (calendar-date-string
          (calendar-gregorian-from-absolute
           (hebrew-calendar-yahrzeit
            h-date
            (extract-calendar-year
             (calendar-hebrew-from-absolute
              (calendar-absolute-from-gregorian (list 1 1 i))))))) "\n"))
    (goto-char (point-min))
    (set-buffer-modified-p nil)
    (setq buffer-read-only t)
    (display-buffer yahrzeit-buffer)
Glenn Morris's avatar
Glenn Morris committed
913
    (message "Computing Yahrzeits...done")))
Edward M. Reingold's avatar
Edward M. Reingold committed
914 915 916 917 918

(defun diary-hebrew-date ()
  "Hebrew calendar equivalent of date diary entry."
  (format "Hebrew date (until sunset): %s" (calendar-hebrew-date-string date)))

919
(defun diary-omer (&optional mark)
Edward M. Reingold's avatar
Edward M. Reingold committed
920
  "Omer count diary entry.
921 922
Entry applies if date is within 50 days after Passover.

923
An optional parameter MARK specifies a face or single-character string to
924
use when highlighting the day in the calendar."
Edward M. Reingold's avatar
Edward M. Reingold committed
925 926 927 928 929 930 931
  (let* ((passover
          (calendar-absolute-from-hebrew
           (list 1 15 (+ (extract-calendar-year date) 3760))))
         (omer (- (calendar-absolute-from-gregorian date) passover))
         (week (/ omer 7))
         (day (% omer 7)))
    (if (and (> omer 0) (< omer 50))
932
        (cons mark
933 934 935 936 937 938 939 940 941 942 943
	      (format "Day %d%s of the omer (until sunset)"
		      omer
		      (if (zerop week)
			  ""
			(format ", that is, %d week%s%s"
				week
				(if (= week 1) "" "s")
				(if (zerop day)
				    ""
				  (format " and %d day%s"
					  day (if (= day 1) "" "s"))))))))))
Edward M. Reingold's avatar
Edward M. Reingold committed
944

945
(defun diary-yahrzeit (death-month death-day death-year &optional mark)
Glenn Morris's avatar
Glenn Morris committed
946
  "Yahrzeit diary entry--entry applies if date is Yahrzeit or the day before.
Edward M. Reingold's avatar
Edward M. Reingold committed
947 948 949
Parameters are DEATH-MONTH, DEATH-DAY, DEATH-YEAR; the diary entry is assumed
to be the name of the person.  Date of death is on the *civil* calendar;
although the date of death is specified by the civil calendar, the proper
Glenn Morris's avatar
Glenn Morris committed
950
Hebrew calendar Yahrzeit is determined.  If `european-calendar-style' is t, the
951 952
order of the parameters is changed to DEATH-DAY, DEATH-MONTH, DEATH-YEAR.

953
An optional parameter MARK specifies a face or single-character string to
954
use when highlighting the day in the calendar."
Edward M. Reingold's avatar
Edward M. Reingold committed
955 956 957 958
  (let* ((h-date (calendar-hebrew-from-absolute
                  (calendar-absolute-from-gregorian
                   (if european-calendar-style
                       (list death-day death-month death-year)
959
		     (list death-month death-day death-year)))))
Edward M. Reingold's avatar
Edward M. Reingold committed
960 961 962 963 964 965 966 967
         (h-month (extract-calendar-month h-date))
         (h-day (extract-calendar-day h-date))
         (h-year (extract-calendar-year h-date))
         (d (calendar-absolute-from-gregorian date))
         (yr (extract-calendar-year (calendar-hebrew-from-absolute d)))
         (diff (- yr h-year))
         (y (hebrew-calendar-yahrzeit h-date yr)))
    (if (and (> diff 0) (or (= y d) (= y (1+ d))))
968 969 970 971 972 973 974 975 976
        (cons mark
	      (format "Yahrzeit of %s%s: %d%s anniversary"
		      entry
		      (if (= y d) "" " (evening)")
		      diff
		      (cond ((= (% diff 10) 1) "st")
			    ((= (% diff 10) 2) "nd")
			    ((= (% diff 10) 3) "rd")
			    (t "th")))))))
Edward M. Reingold's avatar
Edward M. Reingold committed
977

978
(defun diary-rosh-hodesh (&optional mark)
Edward M. Reingold's avatar
Edward M. Reingold committed
979
  "Rosh Hodesh diary entry.
980 981
Entry applies if date is Rosh Hodesh, the day before, or the Saturday before.

982
An optional parameter MARK specifies a face or single-character string to
983
use when highlighting the day in the calendar."
Edward M. Reingold's avatar
Edward M. Reingold committed
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998
  (let* ((d (calendar-absolute-from-gregorian date))
         (h-date (calendar-hebrew-from-absolute d))
         (h-month (extract-calendar-month h-date))
         (h-day (extract-calendar-day h-date))
         (h-year (extract-calendar-year h-date))
         (leap-year (hebrew-calendar-leap-year-p h-year))
         (last-day (hebrew-calendar-last-day-of-month h-month h-year))
         (h-month-names
          (if leap-year
              calendar-hebrew-month-name-array-leap-year
            calendar-hebrew-month-name-array-common-year))
         (this-month (aref h-month-names (1- h-month)))
         (h-yesterday (extract-calendar-day
                       (calendar-hebrew-from-absolute (1- d)))))
    (if (or (= h-day 30) (and (= h-day 1) (/= h-month 7)))
999
        (cons mark
1000 1001 1002 1003 1004
	      (format
	       "Rosh Hodesh %s"
	       (if (= h-day 30)
		   (format
		    "%s (first day)"
Glenn Morris's avatar
Glenn Morris committed
1005
		    ;; Next month must be in the same year since this
1006 1007 1008 1009 1010 1011
		    ;; month can't be the last month of the year since
		    ;; it has 30 days
		    (aref h-month-names h-month))
		 (if (= h-yesterday 30)
		     (format "%s (second day)" this-month)
		   this-month))))
Glenn Morris's avatar
Glenn Morris committed
1012
      (if (= (% d 7) 6)	       ; Saturday--check for Shabbat Mevarchim
1013
          (cons mark
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
		(cond ((and (> h-day 22) (/= h-month 6) (= 29 last-day))
		       (format "Mevarchim Rosh Hodesh %s (%s)"
			       (aref h-month-names
				     (if (= h-month
					    (hebrew-calendar-last-month-of-year
					     h-year))
					 0 h-month))
			       (aref calendar-day-name-array (- 29 h-day))))
		      ((and (< h-day 30) (> h-day 22) (= 30 last-day))
		       (format "Mevarchim Rosh Hodesh %s (%s-%s)"
			       (aref h-month-names h-month)
			       (if (= h-day 29)
				   "tomorrow"
				 (aref calendar-day-name-array (- 29 h-day)))
			       (aref calendar-day-name-array
				     (% (- 30 h-day) 7))))))
Edward M. Reingold's avatar
Edward M. Reingold committed
1030
        (if (and (= h-day 29) (/= h-month 6))
1031 1032
            (cons mark
		  (format "Erev Rosh Hodesh %s"
1033 1034 1035 1036 1037 1038
			  (aref h-month-names
				(if (= h-month
				       (hebrew-calendar-last-month-of-year
					h-year))
				    0 h-month)))))))))

1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
(defvar hebrew-calendar-parashiot-names
["Bereshith"   "Noah"      "Lech L'cha" "Vayera"    "Hayei Sarah" "Toledoth"
 "Vayetze"     "Vayishlah" "Vayeshev"   "Mikketz"   "Vayiggash"   "Vayhi"
 "Shemoth"     "Vaera"     "Bo"         "Beshallah" "Yithro"      "Mishpatim"
 "Terumah"     "Tetzavveh" "Ki Tissa"   "Vayakhel"  "Pekudei"     "Vayikra"
 "Tzav"        "Shemini"   "Tazria"     "Metzora"   "Aharei Moth" "Kedoshim"
 "Emor"        "Behar"     "Behukkotai" "Bemidbar"  "Naso"       "Behaalot'cha"
 "Shelah L'cha" "Korah"    "Hukkath"    "Balak"     "Pinhas"      "Mattoth"
 "Masei"       "Devarim"   "Vaethanan"  "Ekev"      "Reeh"        "Shofetim"
 "Ki Tetze"    "Ki Tavo"   "Nitzavim"   "Vayelech"  "Haazinu"]
  "The names of the parashiot in the Torah.")

(defun hebrew-calendar-parasha-name (p)
  "Name(s) corresponding to parasha P."
Glenn Morris's avatar
Glenn Morris committed
1053
  (if (arrayp p)			; combined parasha
1054 1055 1056 1057 1058
      (format "%s/%s"
              (aref hebrew-calendar-parashiot-names (aref p 0))
              (aref hebrew-calendar-parashiot-names (aref p 1)))
    (aref hebrew-calendar-parashiot-names p)))

1059 1060
(defun diary-parasha (&optional mark)
  "Parasha diary entry--entry applies if date is a Saturday.
1061
An optional parameter MARK specifies a face or single-character string to
1062
use when highlighting the day in the calendar."
Edward M. Reingold's avatar
Edward M. Reingold committed
1063
  (let ((d (calendar-absolute-from-gregorian date)))
Glenn Morris's avatar
Glenn Morris committed
1064
    (if (= (% d 7) 6)			;  Saturday
Edward M. Reingold's avatar
Edward M. Reingold committed
1065 1066 1067
        (let*
            ((h-year (extract-calendar-year
                      (calendar-hebrew-from-absolute d)))
1068
             (rosh-hashanah
Edward M. Reingold's avatar
Edward M. Reingold committed
1069 1070 1071
              (calendar-absolute-from-hebrew (list 7 1 h-year)))
             (passover
              (calendar-absolute-from-hebrew (list 1 15 h-year)))
1072 1073
             (rosh-hashanah-day
              (aref calendar-day-name-array (% rosh-hashanah 7)))
Edward M. Reingold's avatar
Edward M. Reingold committed
1074 1075 1076 1077 1078 1079 1080 1081 1082
             (passover-day
              (aref calendar-day-name-array (% passover 7)))
             (long-h (hebrew-calendar-long-heshvan-p h-year))
             (short-k (hebrew-calendar-short-kislev-p h-year))
             (type (cond ((and long-h (not short-k)) "complete")
                         ((and (not long-h) short-k) "incomplete")
                         (t "regular")))
             (year-format
              (symbol-value
Glenn Morris's avatar
Glenn Morris committed
1083
               (intern (format "hebrew-calendar-year-%s-%s-%s" ; keviah
1084
                               rosh-hashanah-day type passover-day))))
Glenn Morris's avatar
Glenn Morris committed
1085
             (first-saturday		; of Hebrew year
1086
              (calendar-dayname-on-or-before 6 (+ 6 rosh-hashanah)))
Glenn Morris's avatar
Glenn Morris committed
1087
             (saturday		   ; which Saturday of the Hebrew year
Edward M. Reingold's avatar
Edward M. Reingold committed
1088 1089 1090
              (/ (- d first-saturday) 7))
             (parasha (aref year-format saturday)))
          (if parasha
1091 1092 1093
              (cons mark
		    (format
		     "Parashat %s"
Glenn Morris's avatar
Glenn Morris committed
1094
		     (if (listp parasha) ; Israel differs from diaspora
1095 1096
			 (if (car parasha)
			     (format "%s (diaspora), %s (Israel)"
Glenn Morris's avatar
Glenn Morris committed
1097 1098 1099 1100
				     (hebrew-calendar-parasha-name
				      (car parasha))
				     (hebrew-calendar-parasha-name
				      (cdr parasha)))
1101
			   (format "%s (Israel)"
Glenn Morris's avatar
Glenn Morris committed
1102 1103
				   (hebrew-calendar-parasha-name
				    (cdr parasha))))
1104
		       (hebrew-calendar-parasha-name parasha)))))))))
Edward M. Reingold's avatar
Edward M. Reingold committed
1105

Glenn Morris's avatar
Glenn Morris committed
1106
;; The seven ordinary year types (keviot).
Edward M. Reingold's avatar
Edward M. Reingold committed
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
(defconst hebrew-calendar-year-Saturday-incomplete-Sunday
  [nil 52 nil nil 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 [21 22]
    23 24 nil 25 [26 27] [28 29] 30 [31 32] 33 34 35 36 37 38 39 40 [41 42]
    43 44 45 46 47 48 49 50]
  "The structure of the parashiot.
Hebrew year starts on Saturday, is `incomplete' (Heshvan and Kislev each have
29 days), and has Passover start on Sunday.")

(defconst hebrew-calendar-year-Saturday-complete-Tuesday
  [nil 52 nil nil 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 [21 22]
    23 24 nil 25 [26 27] [28 29] 30 [31 32] 33 34 35 36 37 38 39 40 [41 42]
    43 44 45 46 47 48 49 [50 51]]
  "The structure of the parashiot.
Hebrew year that starts on Saturday, is `complete' (Heshvan and Kislev each
have 30 days), and has Passover start on Tuesday.")

(defconst hebrew-calendar-year-Monday-incomplete-Tuesday
  [51 52 nil 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 [21 22]
    23 24 nil 25 [26 27] [28 29] 30 [31 32] 33 34 35 36 37 38 39 40 [41 42]
    43 44 45 46 47 48 49 [50 51]]
  "The structure of the parashiot.
Hebrew year that starts on Monday, is `incomplete' (Heshvan and Kislev each
have 29 days), and has Passover start on Tuesday.")

(defconst hebrew-calendar-year-Monday-complete-Thursday
  [51 52 nil 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 [21 22]
   23 24 nil 25 [26 27] [28 29] 30 [31 32] 33 (nil . 34) (34 . 35) (35 . 36)
   (36 . 37) (37 . 38) ([38 39] . 39) 40 [41 42] 43 44 45 46 47 48 49 [50 51]]
  "The structure of the parashiot.
Hebrew year that starts on Monday, is `complete' (Heshvan and Kislev each have
30 days), and has Passover start on Thursday.")

(defconst hebrew-calendar-year-Tuesday-regular-Thursday
  [51 52 nil 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 [21 22]
   23 24 nil 25 [26 27] [28 29] 30 [31 32] 33 (nil . 34) (34 . 35) (35 . 36)
   (36 . 37) (37 . 38) ([38 39] . 39) 40 [41 42] 43 44 45 46 47 48 49 [50 51]]
  "The structure of the parashiot.
Hebrew year that starts on Tuesday, is `regular' (Heshvan has 29 days and
Kislev has 30 days), and has Passover start on Thursday.")

(defconst hebrew-calendar-year-Thursday-regular-Saturday
  [52 nil nil 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 [21 22] 23
   24 nil (nil . 25) (25 . [26 27]) ([26 27] . [28 29]) ([28 29] . 30)
   (30 . 31) ([31 32] . 32) 33 34 35 36 37 38 39 40 [41 42] 43 44 45 46 47 48
   49 50]
  "The structure of the parashiot.
Hebrew year that starts on Thursday, is `regular' (Heshvan has 29 days and
Kislev has 30 days), and has Passover start on Saturday.")

(defconst hebrew-calendar-year-Thursday-complete-Sunday
  [52 nil nil 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
    23 24 nil 25 [26 27] [28 29] 30 [31 32] 33 34 35 36 37 38 39 40 [41 42]
    43 44 45 46 47 48 49 50]
  "The structure of the parashiot.
Hebrew year that starts on Thursday, is `complete' (Heshvan and Kislev each
have 30 days), and has Passover start on Sunday.")

Glenn Morris's avatar
Glenn Morris committed
1164
;; The seven leap year types (keviot).
Edward M. Reingold's avatar
Edward M. Reingold committed
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
(defconst hebrew-calendar-year-Saturday-incomplete-Tuesday
  [nil 52 nil nil 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
    23 24 25 26 27 nil 28 29 30 31 32 33 34 35 36 37 38 39 40 [41 42]
    43 44 45 46 47 48 49 [50 51]]
  "The structure of the parashiot.
Hebrew year that starts on Saturday, is `incomplete' (Heshvan and Kislev each
have 29 days), and has Passover start on Tuesday.")

(defconst hebrew-calendar-year-Saturday-complete-Thursday
  [nil 52 nil nil 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
   23 24 25 26 27 nil 28 29 30 31 32 33 (nil . 34) (34 . 35) (35 . 36)
   (36 . 37) (37 . 38) ([38 39] . 39) 40 [41 42] 43 44 45 46 47 48 49 [50 51]]
  "The structure of the parashiot.
Hebrew year that starts on Saturday, is `complete' (Heshvan and Kislev each
have 30 days), and has Passover start on Thursday.")

(defconst hebrew-calendar-year-Monday-incomplete-Thursday
  [51 52 nil 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
   23 24 25 26 27 nil 28 29 30 31 32 33 (nil . 34) (34 . 35) (35 . 36)
   (36 . 37) (37 . 38) ([38 39] . 39) 40 [41 42] 43 44 45 46 47 48 49 [50 51]]
  "The structure of the parashiot.
Hebrew year that starts on Monday, is `incomplete' (Heshvan and Kislev each
have 29 days), and has Passover start on Thursday.")

(defconst hebrew-calendar-year-Monday-complete-Saturday
  [51 52 nil 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
   23 24 25 26 27 nil (nil . 28) (28 . 29) (29 . 30) (30 . 31) (31 . 32)
   (32 . 33) (33 . 34) (34 . 35) (35 . 36) (36 . 37) (37 . 38) (38 . 39)
   (39 . 40) (40 . 41) ([41 42] . 42) 43 44 45 46 47 48 49 50]
  "The structure of the parashiot.
Hebrew year that starts on Monday, is `complete' (Heshvan and Kislev each have
30 days), and has Passover start on Saturday.")