comparison lisp/calendar/calendar.el @ 0:376386a54a3c r19-14

Import from CVS: tag r19-14
author cvs
date Mon, 13 Aug 2007 08:45:50 +0200
parents
children ac2d302a0011
comparison
equal deleted inserted replaced
-1:000000000000 0:376386a54a3c
1 ;;; calendar.el --- Calendar functions. -*-byte-compile-dynamic: t;-*-
2
3 ;;; Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994 Free Software
4 ;;; Foundation, Inc.
5
6 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
7 ;; Keywords: calendar
8 ;; Human-Keywords: calendar, Gregorian calendar, Julian calendar,
9 ;; Hebrew calendar, Islamic calendar, ISO calendar, Julian day number,
10 ;; diary, holidays
11
12 ;; This file is part of XEmacs.
13
14 ;; XEmacs is free software; you can redistribute it and/or modify it
15 ;; under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; any later version.
18
19 ;; XEmacs is distributed in the hope that it will be useful, but
20 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 ;; General Public License for more details.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with XEmacs; see the file COPYING. If not, write to the Free
26 ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27
28 ;;; Commentary:
29
30 ;; This collection of functions implements a calendar window. It
31 ;; generates a calendar for the current month, together with the previous
32 ;; and coming months, or for any other three-month period. The calendar
33 ;; can be scrolled forward and backward in the window to show months in
34 ;; the past or future; the cursor can move forward and backward by days,
35 ;; weeks, or months, making it possible, for instance, to jump to the
36 ;; date a specified number of days, weeks, or months from the date under
37 ;; the cursor. The user can display a list of holidays and other notable
38 ;; days for the period shown; the notable days can be marked on the
39 ;; calendar, if desired. The user can also specify that dates having
40 ;; corresponding diary entries (in a file that the user specifies) be
41 ;; marked; the diary entries for any date can be viewed in a separate
42 ;; window. The diary and the notable days can be viewed independently of
43 ;; the calendar. Dates can be translated from the (usual) Gregorian
44 ;; calendar to the day of the year/days remaining in year, to the ISO
45 ;; commercial calendar, to the Julian (old style) calendar, to the Hebrew
46 ;; calendar, to the Islamic calendar, to the French Revolutionary calendar,
47 ;; to the Mayan calendar, and to the astronomical (Julian) day number.
48 ;; When floating point is available, times of sunrise/sunset can be displayed,
49 ;; as can the phases of the moon. Appointment notification for diary entries
50 ;; is available.
51
52 ;; The following files are part of the calendar/diary code:
53
54 ;; cal-menu.el Menu support
55 ;; cal-x.el X-windows dedicated frame functions
56 ;; diary-lib.el, diary-ins.el Diary functions
57 ;; holidays.el Holiday functions
58 ;; cal-french.el French Revolutionary calendar
59 ;; cal-mayan.el Mayan calendars
60 ;; cal-dst.el Daylight savings time rules
61 ;; solar.el Sunrise/sunset, equinoxes/solstices
62 ;; lunar.el Phases of the moon
63 ;; appt.el Appointment notification
64
65 ;; Comments, corrections, and improvements should be sent to
66 ;; Edward M. Reingold Department of Computer Science
67 ;; (217) 333-6733 University of Illinois at Urbana-Champaign
68 ;; reingold@cs.uiuc.edu 1304 West Springfield Avenue
69 ;; Urbana, Illinois 61801
70
71 ;; GNU Emacs users too numerous to list pointed out a variety of problems
72 ;; with earlier forms of the `infinite' sliding calendar and suggested some
73 ;; of the features included in this package. Especially significant in this
74 ;; regard was the suggestion of mark-diary-entries and view-diary-entries,
75 ;; together ideas for their implementation, by
76 ;; Michael S. Littman Cognitive Science Research Group
77 ;; (201) 829-5155 Bell Communications Research
78 ;; mlittman@wind.bellcore.com 445 South St. Box 1961 (2L-331)
79 ;; Morristown, NJ 07960
80
81 ;; The algorithms for the Hebrew calendar are those of the Rambam (Rabbi Moses
82 ;; Maimonides), from his Mishneh Torah, as implemented by
83 ;; Nachum Dershowitz Department of Computer Science
84 ;; (217) 333-4219 University of Illinois at Urbana-Champaign
85 ;; nachum@cs.uiuc.edu 1304 West Springfield Avenue
86 ;; Urbana, Illinois 61801
87
88 ;; Technical details of all the calendrical calculations can be found in
89
90 ;; ``Calendrical Calculations'' by Nachum Dershowitz and Edward M. Reingold,
91 ;; Software--Practice and Experience, Volume 20, Number 9 (September, 1990),
92 ;; pages 899-928. ``Calendrical Calculations, Part II: Three Historical
93 ;; Calendars'' by E. M. Reingold, N. Dershowitz, and S. M. Clamen,
94 ;; Software--Practice and Experience, Volume 23, Number 4 (April, 1993),
95 ;; pages 383-404.
96
97 ;; Hard copies of these two papers can be obtained by sending email to
98 ;; reingold@cs.uiuc.edu with the SUBJECT "send-paper-cal" (no quotes) and
99 ;; the message BODY containing your mailing address (snail).
100
101 ;;; Code:
102
103 (defun calendar-version ()
104 (interactive)
105 (message "Version 5.3, January 25, 1994"))
106
107 ;;;###autoload
108 (defvar calendar-week-start-day 0
109 "*The day of the week on which a week in the calendar begins.
110 0 means Sunday (default), 1 means Monday, and so on.")
111
112 ;;;###autoload
113 (defvar calendar-offset 0
114 "*The offset of the principal month from the center of the calendar window.
115 0 means the principal month is in the center (default), -1 means on the left,
116 +1 means on the right. Larger (or smaller) values push the principal month off
117 the screen.")
118
119 ;;;###autoload
120 (defvar view-diary-entries-initially nil
121 "*Non-nil means display current date's diary entries on entry.
122 The diary is displayed in another window when the calendar is first displayed,
123 if the current date is visible. The number of days of diary entries displayed
124 is governed by the variable `number-of-diary-entries'.")
125
126 ;;;###autoload
127 (defvar number-of-diary-entries 1
128 "*Specifies how many days of diary entries are to be displayed initially.
129 This variable affects the diary display when the command M-x diary is used,
130 or if the value of the variable `view-diary-entries-initially' is t. For
131 example, if the default value 1 is used, then only the current day's diary
132 entries will be displayed. If the value 2 is used, then both the current
133 day's and the next day's entries will be displayed.
134
135 The value can also be a vector such as [0 2 2 2 2 4 1]; this value
136 says to display no diary entries on Sunday, the display the entries
137 for the current date and the day after on Monday through Thursday,
138 display Friday through Monday's entries on Friday, and display only
139 Saturday's entries on Saturday.
140
141 This variable does not affect the diary display with the `d' command
142 from the calendar; in that case, the prefix argument controls the
143 number of days of diary entries displayed.")
144
145 ;;;###autoload
146 (defvar mark-diary-entries-in-calendar nil
147 "*Non-nil means mark dates with diary entries, in the calendar window.
148 The marking symbol is specified by the variable `diary-entry-marker'.")
149
150 (defvar diary-entry-marker
151 (progn
152 (make-face 'diary-face)
153 (cond ((face-differs-from-default-p 'diary-face) nil)
154 (t (set-face-foreground 'diary-face "red" 'global '(x color))
155 (set-face-highlight-p 'diary-face t 'global 'tty)
156 ;; avoid a weird problem when byte-compiling appt.el
157 ;; in batch mode.
158 (if (and (not noninteractive) (fboundp 'x-make-font-bold))
159 (let ((bfont (x-make-font-bold
160 (face-font-instance 'default)))
161 (mono-tag (list 'x 'mono))
162 (gray-tag (list 'x 'grayscale)))
163 (if bfont
164 (progn
165 (set-face-font 'diary-face bfont 'global mono-tag)
166 (set-face-font 'diary-face bfont 'global
167 gray-tag)))))))
168 'diary-face)
169 "*Used to mark dates that have diary entries.
170 Can be either a single-character string or a face.")
171
172 (defvar calendar-today-marker
173 (progn
174 (make-face 'calendar-today-face)
175 (if (not (face-differs-from-default-p 'calendar-today-face))
176 (set-face-underline-p 'calendar-today-face t))
177 'calendar-today-face)
178 "*Used to mark today's date.
179 Can be either a single-character string or a face.")
180
181 (defvar calendar-holiday-marker
182 (progn
183 (make-face 'holiday-face)
184 (cond ((face-differs-from-default-p 'holiday-face) nil)
185 (t (let ((color-tag (list 'x 'color))
186 (mono-tag (list 'x 'mono))
187 (gray-tag (list 'x 'grayscale)))
188 (set-face-background 'holiday-face [default foreground] 'global
189 mono-tag)
190 (set-face-foreground 'holiday-face [default background] 'global
191 mono-tag)
192 (set-face-background 'holiday-face [default foreground] 'global
193 gray-tag)
194 (set-face-foreground 'holiday-face [default background] 'global
195 gray-tag)
196 (set-face-background 'holiday-face "pink" 'global color-tag)
197 (set-face-reverse-p 'holiday-face t 'global 'tty))))
198 'holiday-face)
199 "*Used to mark notable dates in the calendar.
200 Can be either a single-character string or a face.")
201
202 ;;;###autoload
203 (defvar view-calendar-holidays-initially nil
204 "*Non-nil means display holidays for current three month period on entry.
205 The holidays are displayed in another window when the calendar is first
206 displayed.")
207
208 ;;;###autoload
209 (defvar mark-holidays-in-calendar nil
210 "*Non-nil means mark dates of holidays in the calendar window.
211 The marking symbol is specified by the variable `calendar-holiday-marker'.")
212
213 ;;;###autoload
214 (defvar all-hebrew-calendar-holidays nil
215 "*If nil, show only major holidays from the Hebrew calendar.
216 This means only those Jewish holidays that appear on secular calendars.
217
218 If t, show all the holidays that would appear in a complete Hebrew calendar.")
219
220 ;;;###autoload
221 (defvar all-christian-calendar-holidays nil
222 "*If nil, show only major holidays from the Christian calendar.
223 This means only those Christian holidays that appear on secular calendars.
224
225 If t, show all the holidays that would appear in a complete Christian
226 calendar.")
227
228 ;;;###autoload
229 (defvar all-islamic-calendar-holidays nil
230 "*If nil, show only major holidays from the Islamic calendar.
231 This means only those Islamic holidays that appear on secular calendars.
232
233 If t, show all the holidays that would appear in a complete Islamic
234 calendar.")
235
236 ;;;###autoload
237 (defvar calendar-load-hook nil
238 "*List of functions to be called after the calendar is first loaded.
239 This is the place to add key bindings to `calendar-mode-map'.")
240
241 ;;;###autoload
242 (defvar initial-calendar-window-hook nil
243 "*List of functions to be called when the calendar window is first opened.
244 The functions invoked are called after the calendar window is opened, but
245 once opened is never called again. Leaving the calendar with the `q' command
246 and reentering it will cause these functions to be called again.")
247
248 ;;;###autoload
249 (defvar today-visible-calendar-hook nil
250 "*List of functions called whenever the current date is visible.
251 This can be used, for example, to replace today's date with asterisks; a
252 function `calendar-star-date' is included for this purpose:
253 (setq today-visible-calendar-hook 'calendar-star-date)
254 It can also be used to mark the current date with `calendar-today-marker';
255 a function is also provided for this:
256 (setq today-visible-calendar-hook 'calendar-mark-today)
257
258 The corresponding variable `today-invisible-calendar-hook' is the list of
259 functions called when the calendar function was called when the current
260 date is not visible in the window.
261
262 Other than the use of the provided functions, the changing of any
263 characters in the calendar buffer by the hooks may cause the failure of the
264 functions that move by days and weeks.")
265
266 ;;;###autoload
267 (defvar today-invisible-calendar-hook nil
268 "*List of functions called whenever the current date is not visible.
269
270 The corresponding variable `today-visible-calendar-hook' is the list of
271 functions called when the calendar function was called when the current
272 date is visible in the window.
273
274 Other than the use of the provided functions, the changing of any
275 characters in the calendar buffer by the hooks may cause the failure of the
276 functions that move by days and weeks.")
277
278 ;;;###autoload
279 (defvar diary-file "~/diary"
280 "*Name of the file in which one's personal diary of dates is kept.
281
282 The file's entries are lines in any of the forms
283
284 MONTH/DAY
285 MONTH/DAY/YEAR
286 MONTHNAME DAY
287 MONTHNAME DAY, YEAR
288 DAYNAME
289
290 at the beginning of the line; the remainder of the line is the diary entry
291 string for that date. MONTH and DAY are one or two digit numbers, YEAR is
292 a number and may be written in full or abbreviated to the final two digits.
293 If the date does not contain a year, it is generic and applies to any year.
294 DAYNAME entries apply to any date on which is on that day of the week.
295 MONTHNAME and DAYNAME can be spelled in full, abbreviated to three
296 characters (with or without a period), capitalized or not. Any of DAY,
297 MONTH, or MONTHNAME, YEAR can be `*' which matches any day, month, or year,
298 respectively.
299
300 The European style (in which the day precedes the month) can be used
301 instead, if you execute `european-calendar' when in the calendar, or set
302 `european-calendar-style' to t in your .emacs file. The European forms are
303
304 DAY/MONTH
305 DAY/MONTH/YEAR
306 DAY MONTHNAME
307 DAY MONTHNAME YEAR
308 DAYNAME
309
310 To revert to the default American style from the European style, execute
311 `american-calendar' in the calendar.
312
313 A diary entry can be preceded by the character
314 `diary-nonmarking-symbol' (ordinarily `&') to make that entry
315 nonmarking--that is, it will not be marked on dates in the calendar
316 window but will appear in a diary window.
317
318 Multiline diary entries are made by indenting lines after the first with
319 either a TAB or one or more spaces.
320
321 Lines not in one the above formats are ignored. Here are some sample diary
322 entries (in the default American style):
323
324 12/22/1988 Twentieth wedding anniversary!!
325 &1/1. Happy New Year!
326 10/22 Ruth's birthday.
327 21: Payday
328 Tuesday--weekly meeting with grad students at 10am
329 Supowit, Shen, Bitner, and Kapoor to attend.
330 1/13/89 Friday the thirteenth!!
331 &thu 4pm squash game with Lloyd.
332 mar 16 Dad's birthday
333 April 15, 1989 Income tax due.
334 &* 15 time cards due.
335
336 If the first line of a diary entry consists only of the date or day name with
337 no trailing blanks or punctuation, then that line is not displayed in the
338 diary window; only the continuation lines is shown. For example, the
339 single diary entry
340
341 02/11/1989
342 Bill Blattner visits Princeton today
343 2pm Cognitive Studies Committee meeting
344 2:30-5:30 Lizzie at Lawrenceville for `Group Initiative'
345 4:00pm Jamie Tappenden
346 7:30pm Dinner at George and Ed's for Alan Ryan
347 7:30-10:00pm dance at Stewart Country Day School
348
349 will appear in the diary window without the date line at the beginning. This
350 facility allows the diary window to look neater, but can cause confusion if
351 used with more than one day's entries displayed.
352
353 Diary entries can be based on Lisp sexps. For example, the diary entry
354
355 %%(diary-block 11 1 1990 11 10 1990) Vacation
356
357 causes the diary entry \"Vacation\" to appear from November 1 through November
358 10, 1990. Other functions available are `diary-float', `diary-anniversary',
359 `diary-cyclic', `diary-day-of-year', `diary-iso-date', `diary-french-date',
360 `diary-hebrew-date', `diary-islamic-date', `diary-mayan-date',
361 `diary-yahrzeit', `diary-sunrise-sunset', `diary-phases-of-moon',
362 `diary-parasha', `diary-omer', `diary-rosh-hodesh', and
363 `diary-sabbath-candles'. See the documentation for the function
364 `list-sexp-diary-entries' for more details.
365
366 Diary entries based on the Hebrew and/or the Islamic calendar are also
367 possible, but because these are somewhat slow, they are ignored
368 unless you set the `nongregorian-diary-listing-hook' and the
369 `nongregorian-diary-marking-hook' appropriately. See the documentation
370 for these functions for details.
371
372 Diary files can contain directives to include the contents of other files; for
373 details, see the documentation for the variable `list-diary-entries-hook'.")
374
375 ;;;###autoload
376 (defvar diary-nonmarking-symbol "&"
377 "*Symbol indicating that a diary entry is not to be marked in the calendar.")
378
379 ;;;###autoload
380 (defvar hebrew-diary-entry-symbol "H"
381 "*Symbol indicating a diary entry according to the Hebrew calendar.")
382
383 ;;;###autoload
384 (defvar islamic-diary-entry-symbol "I"
385 "*Symbol indicating a diary entry according to the Islamic calendar.")
386
387 ;;;###autoload
388 (defvar diary-include-string "#include"
389 "*The string indicating inclusion of another file of diary entries.
390 See the documentation for the function `include-other-diary-files'.")
391
392 ;;;###autoload
393 (defvar sexp-diary-entry-symbol "%%"
394 "*The string used to indicate a sexp diary entry in diary-file.
395 See the documentation for the function `list-sexp-diary-entries'.")
396
397 ;;;###autoload
398 (defvar abbreviated-calendar-year t
399 "*Interpret a two-digit year DD in a diary entry as either 19DD or 20DD.
400 For the Gregorian calendar; similarly for the Hebrew and Islamic calendars.
401 If this variable is nil, years must be written in full.")
402
403 ;;;###autoload
404 (defvar european-calendar-style nil
405 "*Use the European style of dates in the diary and in any displays.
406 If this variable is t, a date 1/2/1990 would be interpreted as February 1,
407 1990. The accepted European date styles are
408
409 DAY/MONTH
410 DAY/MONTH/YEAR
411 DAY MONTHNAME
412 DAY MONTHNAME YEAR
413 DAYNAME
414
415 Names can be capitalized or not, written in full, or abbreviated to three
416 characters with or without a period.")
417
418 ;;;###autoload
419 (defvar american-date-diary-pattern
420 '((month "/" day "[^/0-9]")
421 (month "/" day "/" year "[^0-9]")
422 (monthname " *" day "[^,0-9]")
423 (monthname " *" day ", *" year "[^0-9]")
424 (dayname "\\W"))
425 "*List of pseudo-patterns describing the American patterns of date used.
426 See the documentation of `diary-date-forms' for an explanation.")
427
428 ;;;###autoload
429 (defvar european-date-diary-pattern
430 '((day "/" month "[^/0-9]")
431 (day "/" month "/" year "[^0-9]")
432 (backup day " *" monthname "\\W+\\<[^*0-9]")
433 (day " *" monthname " *" year "[^0-9]")
434 (dayname "\\W"))
435 "*List of pseudo-patterns describing the European patterns of date used.
436 See the documentation of `diary-date-forms' for an explanation.")
437
438 (defvar diary-date-forms
439 (if european-calendar-style
440 european-date-diary-pattern
441 american-date-diary-pattern)
442 "*List of pseudo-patterns describing the forms of date used in the diary.
443 The patterns on the list must be MUTUALLY EXCLUSIVE and must should not match
444 any portion of the diary entry itself, just the date component.
445
446 A pseudo-pattern is a list of regular expressions and the keywords `month',
447 `day', `year', `monthname', and `dayname'. The keyword `monthname' will
448 match the name of the month, capitalized or not, or its three-letter
449 abbreviation, followed by a period or not; it will also match `*'.
450 Similarly, `dayname' will match the name of the day, capitalized or not, or
451 its three-letter abbreviation, followed by a period or not. The keywords
452 `month', `day', and `year' will match those numerical values, preceded by
453 arbitrarily many zeros; they will also match `*'.
454
455 The matching of the diary entries with the date forms is done with the
456 standard syntax table from Fundamental mode, but with the `*' changed so
457 that it is a word constituent.
458
459 If, to be mutually exclusive, a pseudo-pattern must match a portion of the
460 diary entry itself, the first element of the pattern MUST be `backup'. This
461 directive causes the date recognizer to back up to the beginning of the
462 current word of the diary entry, so in no case can the pattern match more than
463 a portion of the first word of the diary entry.")
464
465 ;;;###autoload
466 (defvar european-calendar-display-form
467 '((if dayname (concat dayname ", ")) day " " monthname " " year)
468 "*Pseudo-pattern governing the way a date appears in the European style.
469 See the documentation of calendar-date-display-form for an explanation.")
470
471 ;;;###autoload
472 (defvar american-calendar-display-form
473 '((if dayname (concat dayname ", ")) monthname " " day ", " year)
474 "*Pseudo-pattern governing the way a date appears in the American style.
475 See the documentation of `calendar-date-display-form' for an explanation.")
476
477 (defvar calendar-date-display-form
478 (if european-calendar-style
479 european-calendar-display-form
480 american-calendar-display-form)
481 "*Pseudo-pattern governing the way a date appears.
482
483 Used by the function `calendar-date-string', a pseudo-pattern is a list of
484 expressions that can involve the keywords `month', `day', and `year', all
485 numbers in string form, and `monthname' and `dayname', both alphabetic
486 strings. For example, the ISO standard would use the pseudo- pattern
487
488 '(year \"-\" month \"-\" day)
489
490 while a typical American form would be
491
492 '(month \"/\" day \"/\" (substring year -2))
493
494 and
495
496 '((format \"%9s, %9s %2s, %4s\" dayname monthname day year))
497
498 would give the usual American style in fixed-length fields.
499
500 See the documentation of the function `calendar-date-string'.")
501
502 (defun european-calendar ()
503 "Set the interpretation and display of dates to the European style."
504 (interactive)
505 (setq european-calendar-style t)
506 (setq calendar-date-display-form european-calendar-display-form)
507 (setq diary-date-forms european-date-diary-pattern)
508 (update-calendar-mode-line))
509
510 (defun american-calendar ()
511 "Set the interpretation and display of dates to the American style."
512 (interactive)
513 (setq european-calendar-style nil)
514 (setq calendar-date-display-form american-calendar-display-form)
515 (setq diary-date-forms american-date-diary-pattern)
516 (update-calendar-mode-line))
517
518 ;;;###autoload
519 (defvar print-diary-entries-hook 'lpr-buffer
520 "*List of functions called after a temporary diary buffer is prepared.
521 The buffer shows only the diary entries currently visible in the diary
522 buffer. The default just does the printing. Other uses might include, for
523 example, rearranging the lines into order by day and time, saving the buffer
524 instead of deleting it, or changing the function used to do the printing.")
525
526 ;;;###autoload
527 (defvar list-diary-entries-hook nil
528 "*List of functions called after diary file is culled for relevant entries.
529 It is to be used for diary entries that are not found in the diary file.
530
531 A function `include-other-diary-files' is provided for use as the value of
532 this hook. This function enables you to use shared diary files together
533 with your own. The files included are specified in the diary file by lines
534 of the form
535
536 #include \"filename\"
537
538 This is recursive; that is, #include directives in files thus included are
539 obeyed. You can change the \"#include\" to some other string by changing
540 the variable `diary-include-string'. When you use `include-other-diary-files'
541 as part of the list-diary-entries-hook, you will probably also want to use the
542 function `mark-included-diary-files' as part of `mark-diary-entries-hook'.
543
544 For example, you could use
545
546 (setq list-diary-entries-hook
547 '(include-other-diary-files sort-diary-entries))
548 (setq diary-display-hook 'fancy-diary-display)
549
550 in your `.emacs' file to cause the fancy diary buffer to be displayed with
551 diary entries from various included files, each day's entries sorted into
552 lexicographic order.")
553
554 ;;;###autoload
555 (defvar diary-hook nil
556 "*List of functions called after the display of the diary.
557 Can be used for appointment notification.")
558
559 ;;;###autoload
560 (defvar diary-display-hook nil
561 "*List of functions that handle the display of the diary.
562 If nil (the default), `simple-diary-display' is used. Use `ignore' for no
563 diary display.
564
565 Ordinarily, this just displays the diary buffer (with holidays indicated in
566 the mode line), if there are any relevant entries. At the time these
567 functions are called, the variable `diary-entries-list' is a list, in order
568 by date, of all relevant diary entries in the form of ((MONTH DAY YEAR)
569 STRING), where string is the diary entry for the given date. This can be
570 used, for example, a different buffer for display (perhaps combined with
571 holidays), or produce hard copy output.
572
573 A function `fancy-diary-display' is provided as an alternative
574 choice for this hook; this function prepares a special noneditable diary
575 buffer with the relevant diary entries that has neat day-by-day arrangement
576 with headings. The fancy diary buffer will show the holidays unless the
577 variable `holidays-in-diary-buffer' is set to nil. Ordinarily, the fancy
578 diary buffer will not show days for which there are no diary entries, even
579 if that day is a holiday; if you want such days to be shown in the fancy
580 diary buffer, set the variable `diary-list-include-blanks' to t.")
581
582 ;;;###autoload
583 (defvar nongregorian-diary-listing-hook nil
584 "*List of functions called for listing diary file and included files.
585 As the files are processed for diary entries, these functions are used to cull
586 relevant entries. You can use either or both of `list-hebrew-diary-entries'
587 and `list-islamic-diary-entries'. The documentation for these functions
588 describes the style of such diary entries.")
589
590 ;;;###autoload
591 (defvar mark-diary-entries-hook nil
592 "*List of functions called after marking diary entries in the calendar.
593
594 A function `mark-included-diary-files' is also provided for use as the
595 mark-diary-entries-hook; it enables you to use shared diary files together
596 with your own. The files included are specified in the diary file by lines
597 of the form
598 #include \"filename\"
599 This is recursive; that is, #include directives in files thus included are
600 obeyed. You can change the \"#include\" to some other string by changing the
601 variable `diary-include-string'. When you use `mark-included-diary-files' as
602 part of the mark-diary-entries-hook, you will probably also want to use the
603 function `include-other-diary-files' as part of `list-diary-entries-hook'.")
604
605 ;;;###autoload
606 (defvar nongregorian-diary-marking-hook nil
607 "*List of functions called for marking diary file and included files.
608 As the files are processed for diary entries, these functions are used to cull
609 relevant entries. You can use either or both of `mark-hebrew-diary-entries'
610 and `mark-islamic-diary-entries'. The documentation for these functions
611 describes the style of such diary entries.")
612
613 ;;;###autoload
614 (defvar diary-list-include-blanks nil
615 "*If nil, do not include days with no diary entry in the list of diary entries.
616 Such days will then not be shown in the the fancy diary buffer, even if they
617 are holidays.")
618
619 ;;;###autoload
620 (defvar holidays-in-diary-buffer t
621 "*Non-nil means include holidays in the diary display.
622 The holidays appear in the mode line of the diary buffer, or in the
623 fancy diary buffer next to the date. This slows down the diary functions
624 somewhat; setting it to nil makes the diary display faster.")
625
626 (defvar calendar-mark-ring nil)
627
628 ;;;###autoload
629 (put 'general-holidays 'risky-local-variable t)
630 ;;;###autoload
631 (defvar general-holidays
632 '((holiday-fixed 1 1 "New Year's Day")
633 (holiday-float 1 1 3 "Martin Luther King Day")
634 (holiday-fixed 2 2 "Ground Hog Day")
635 (holiday-fixed 2 14 "Valentine's Day")
636 (holiday-float 2 1 3 "President's Day")
637 (holiday-fixed 3 17 "St. Patrick's Day")
638 (holiday-fixed 4 1 "April Fool's Day")
639 (holiday-float 5 0 2 "Mother's Day")
640 (holiday-float 5 1 -1 "Memorial Day")
641 (holiday-fixed 6 14 "Flag Day")
642 (holiday-float 6 0 3 "Father's Day")
643 (holiday-fixed 7 4 "Independence Day")
644 (holiday-float 9 1 1 "Labor Day")
645 (holiday-float 10 1 2 "Columbus Day")
646 (holiday-fixed 10 31 "Halloween")
647 (holiday-fixed 11 11 "Veteran's Day")
648 (holiday-float 11 4 4 "Thanksgiving"))
649 "*General holidays. Default value is for the United States.
650 See the documentation for `calendar-holidays' for details.")
651
652 ;;;###autoload
653 (put 'local-holidays 'risky-local-variable t)
654 ;;;###autoload
655 (defvar local-holidays nil
656 "*Local holidays.
657 See the documentation for `calendar-holidays' for details.")
658
659 ;;;###autoload
660 (put 'other-holidays 'risky-local-variable t)
661 ;;;###autoload
662 (defvar other-holidays nil
663 "*User defined holidays.
664 See the documentation for `calendar-holidays' for details.")
665
666 ;;;###autoload
667 (put 'hebrew-holidays-1 'risky-local-variable t)
668 ;;;###autoload
669 (defvar hebrew-holidays-1
670 '((holiday-rosh-hashanah-etc)
671 (if all-hebrew-calendar-holidays
672 (holiday-julian
673 11
674 (let* ((m displayed-month)
675 (y displayed-year)
676 (year))
677 (increment-calendar-month m y -1)
678 (let ((year (extract-calendar-year
679 (calendar-julian-from-absolute
680 (calendar-absolute-from-gregorian
681 (list m 1 y))))))
682 (if (zerop (% (1+ year) 4))
683 22
684 21))) "\"Tal Umatar\" (evening)"))))
685
686 ;;;###autoload
687 (put 'hebrew-holidays-2 'risky-local-variable t)
688 ;;;###autoload
689 (defvar hebrew-holidays-2
690 '((if all-hebrew-calendar-holidays
691 (holiday-hanukkah)
692 (holiday-hebrew 9 25 "Hanukkah"))
693 (if all-hebrew-calendar-holidays
694 (holiday-hebrew
695 10
696 (let ((h-year (extract-calendar-year
697 (calendar-hebrew-from-absolute
698 (calendar-absolute-from-gregorian
699 (list displayed-month 28 displayed-year))))))
700 (if (= (% (calendar-absolute-from-hebrew (list 10 10 h-year))
701 7)
702 6)
703 11 10))
704 "Tzom Teveth"))
705 (if all-hebrew-calendar-holidays
706 (holiday-hebrew 11 15 "Tu B'Shevat"))))
707
708 ;;;###autoload
709 (put 'hebrew-holidays-3 'risky-local-variable t)
710 ;;;###autoload
711 (defvar hebrew-holidays-3
712 '((if all-hebrew-calendar-holidays
713 (holiday-hebrew
714 11
715 (let ((m displayed-month)
716 (y displayed-year))
717 (increment-calendar-month m y 1)
718 (let* ((h-year (extract-calendar-year
719 (calendar-hebrew-from-absolute
720 (calendar-absolute-from-gregorian
721 (list m
722 (calendar-last-day-of-month m y)
723 y)))))
724 (s-s
725 (calendar-hebrew-from-absolute
726 (if (=
727 (% (calendar-absolute-from-hebrew
728 (list 7 1 h-year))
729 7)
730 6)
731 (calendar-dayname-on-or-before
732 6 (calendar-absolute-from-hebrew
733 (list 11 17 h-year)))
734 (calendar-dayname-on-or-before
735 6 (calendar-absolute-from-hebrew
736 (list 11 16 h-year))))))
737 (day (extract-calendar-day s-s)))
738 day))
739 "Shabbat Shirah"))))
740
741 ;;;###autoload
742 (put 'hebrew-holidays-4 'risky-local-variable t)
743 ;;;###autoload
744 (defvar hebrew-holidays-4
745 '((holiday-passover-etc)
746 (if (and all-hebrew-calendar-holidays
747 (let* ((m displayed-month)
748 (y displayed-year)
749 (year))
750 (increment-calendar-month m y -1)
751 (let ((year (extract-calendar-year
752 (calendar-julian-from-absolute
753 (calendar-absolute-from-gregorian
754 (list m 1 y))))))
755 (= 21 (% year 28)))))
756 (holiday-julian 3 26 "Kiddush HaHamah"))
757 (if all-hebrew-calendar-holidays
758 (holiday-tisha-b-av-etc))))
759
760 ;;;###autoload
761 (put 'hebrew-holidays 'risky-local-variable t)
762 ;;;###autoload
763 (defvar hebrew-holidays (append hebrew-holidays-1 hebrew-holidays-2
764 hebrew-holidays-3 hebrew-holidays-4)
765 "*Jewish holidays.
766 See the documentation for `calendar-holidays' for details.")
767
768 ;;;###autoload
769 (put 'christian-holidays 'risky-local-variable t)
770 ;;;###autoload
771 (defvar christian-holidays
772 '((if all-christian-calendar-holidays
773 (holiday-fixed 1 6 "Epiphany"))
774 (holiday-easter-etc)
775 (if all-christian-calendar-holidays
776 (holiday-greek-orthodox-easter))
777 (if all-christian-calendar-holidays
778 (holiday-fixed 8 15 "Assumption"))
779 (if all-christian-calendar-holidays
780 (holiday-advent))
781 (holiday-fixed 12 25 "Christmas")
782 (if all-christian-calendar-holidays
783 (holiday-julian 12 25 "Eastern Orthodox Christmas")))
784 "*Christian holidays.
785 See the documentation for `calendar-holidays' for details.")
786
787 ;;;###autoload
788 (put 'islamic-holidays 'risky-local-variable t)
789 ;;;###autoload
790 (defvar islamic-holidays
791 '((holiday-islamic
792 1 1
793 (format "Islamic New Year %d"
794 (let ((m displayed-month)
795 (y displayed-year))
796 (increment-calendar-month m y 1)
797 (extract-calendar-year
798 (calendar-islamic-from-absolute
799 (calendar-absolute-from-gregorian
800 (list
801 m (calendar-last-day-of-month m y) y)))))))
802 (if all-islamic-calendar-holidays
803 (holiday-islamic 1 10 "Ashura"))
804 (if all-islamic-calendar-holidays
805 (holiday-islamic 3 12 "Mulad-al-Nabi"))
806 (if all-islamic-calendar-holidays
807 (holiday-islamic 7 26 "Shab-e-Mi'raj"))
808 (if all-islamic-calendar-holidays
809 (holiday-islamic 8 15 "Shab-e-Bara't"))
810 (holiday-islamic 9 1 "Ramadan Begins")
811 (if all-islamic-calendar-holidays
812 (holiday-islamic 9 27 "Shab-e Qadr"))
813 (if all-islamic-calendar-holidays
814 (holiday-islamic 10 1 "Id-al-Fitr"))
815 (if all-islamic-calendar-holidays
816 (holiday-islamic 12 10 "Id-al-Adha")))
817 "*Islamic holidays.
818 See the documentation for `calendar-holidays' for details.")
819
820 ;;;###autoload
821 (put 'solar-holidays 'risky-local-variable t)
822 ;;;###autoload
823 (defvar solar-holidays
824 '((if (fboundp 'atan)
825 (solar-equinoxes-solstices))
826 (if (progn
827 (require 'cal-dst)
828 t)
829 (funcall
830 'holiday-sexp
831 calendar-daylight-savings-starts
832 '(format "Daylight Savings Time Begins %s"
833 (if (fboundp 'atan)
834 (solar-time-string
835 (/ calendar-daylight-savings-starts-time (float 60))
836 calendar-standard-time-zone-name)
837 ""))))
838 (funcall
839 'holiday-sexp
840 calendar-daylight-savings-ends
841 '(format "Daylight Savings Time Ends %s"
842 (if (fboundp 'atan)
843 (solar-time-string
844 (/ calendar-daylight-savings-ends-time (float 60))
845 calendar-daylight-time-zone-name)
846 ""))))
847 "*Sun-related holidays.
848 See the documentation for `calendar-holidays' for details.")
849
850 ;;;###autoload
851 (put 'calendar-holidays 'risky-local-variable t)
852 (defvar calendar-holidays
853 (append general-holidays local-holidays other-holidays
854 christian-holidays hebrew-holidays islamic-holidays
855 solar-holidays)
856 "*List of notable days for the command M-x holidays.
857
858 Additional holidays are easy to add to the list, just put them in the list
859 `other-holidays' in your .emacs file. Similarly, by setting any of
860 `general-holidays', `local-holidays' `christian-holidays', `hebrew-holidays',
861 `islamic-holidays', or `solar-holidays' to nil in your .emacs file, you can
862 eliminate unwanted categories of holidays. The intention is that (in the US)
863 `local-holidays' be set in site-init.el and `other-holidays' be set by the
864 user.
865
866 Entries on the list are expressions that return (possibly empty) lists of
867 items of the form ((month day year) string) of a holiday in the in the
868 three-month period centered around `displayed-month' of `displayed-year'.
869 Several basic functions are provided for this purpose:
870
871 (holiday-fixed MONTH DAY STRING) is a fixed date on the Gregorian calendar
872 (holiday-float MONTH DAYNAME K STRING &optional day) is the Kth DAYNAME in
873 MONTH on the Gregorian calendar (0 for Sunday,
874 etc.); K<0 means count back from the end of the
875 month. An optional parameter DAY means the Kth
876 DAYNAME after/before MONTH DAY.
877 (holiday-hebrew MONTH DAY STRING) a fixed date on the Hebrew calendar
878 (holiday-islamic MONTH DAY STRING) a fixed date on the Islamic calendar
879 (holiday-julian MONTH DAY STRING) a fixed date on the Julian calendar
880 (holiday-sexp SEXP STRING) SEXP is a Gregorian-date-valued expression
881 in the variable `year'; if it evaluates to
882 a visible date, that's the holiday; if it
883 evaluates to nil, there's no holiday. STRING
884 is an expression in the variable `date'.
885
886 For example, to add Bastille Day, celebrated in France on July 14, add
887
888 (holiday-fixed 7 14 \"Bastille Day\")
889
890 to the list. To add Hurricane Supplication Day, celebrated in the Virgin
891 Islands on the fourth Monday in August, add
892
893 (holiday-float 8 1 4 \"Hurricane Supplication Day\")
894
895 to the list (the last Monday would be specified with `-1' instead of `4').
896 To add the last day of Hanukkah to the list, use
897
898 (holiday-hebrew 10 2 \"Last day of Hanukkah\")
899
900 since the Hebrew months are numbered with 1 starting from Nisan, while to
901 add the Islamic feast celebrating Mohammed's birthday use
902
903 (holiday-islamic 3 12 \"Mohammed's Birthday\")
904
905 since the Islamic months are numbered from 1 starting with Muharram. To
906 add Thomas Jefferson's birthday, April 2, 1743 (Julian), use
907
908 (holiday-julian 4 2 \"Jefferson's Birthday\")
909
910 To include a holiday conditionally, use the sexp form or a conditional. For
911 example, to include American presidential elections, which occur on the first
912 Tuesday after the first Monday in November of years divisible by 4, add
913
914 (holiday-sexp
915 (if (zerop (% year 4))
916 (calendar-gregorian-from-absolute
917 (1+ (calendar-dayname-on-or-before
918 1 (+ 6 (calendar-absolute-from-gregorian
919 (list 11 1 year)))))))
920 \"US Presidential Election\")
921
922 or
923
924 (if (zerop (% displayed-year 4))
925 (holiday-fixed 11
926 (extract-calendar-day
927 (calendar-gregorian-from-absolute
928 (1+ (calendar-dayname-on-or-before
929 1 (+ 6 (calendar-absolute-from-gregorian
930 (list 11 1 displayed-year)))))))
931 \"US Presidential Election\"))
932
933 to the list. To include the phases of the moon, add
934
935 (lunar-phases)
936
937 to the holiday list, where `lunar-phases' is an Emacs-Lisp function that
938 you've written to return a (possibly empty) list of the relevant VISIBLE dates
939 with descriptive strings such as
940
941 (((2 6 1989) \"New Moon\") ((2 12 1989) \"First Quarter Moon\") ... ).")
942
943 (defconst calendar-buffer "*Calendar*"
944 "Name of the buffer used for the calendar.")
945
946 (defconst holiday-buffer "*Holidays*"
947 "Name of the buffer used for the displaying the holidays.")
948
949 (defconst fancy-diary-buffer "*Fancy Diary Entries*"
950 "Name of the buffer used for the optional fancy display of the diary.")
951
952 (defconst lunar-phases-buffer "*Phases of Moon*"
953 "Name of the buffer used for the lunar phases.")
954
955 (defmacro increment-calendar-month (mon yr n)
956 "Move the variables MON and YR to the month and year by N months.
957 Forward if N is positive or backward if N is negative."
958 (` (let (( macro-y (+ (* (, yr) 12) (, mon) -1 (, n) )))
959 (setq (, mon) (1+ (% macro-y 12) ))
960 (setq (, yr) (/ macro-y 12)))))
961
962 (defmacro calendar-for-loop (var from init to final do &rest body)
963 "Execute a for loop."
964 (` (let (( (, var) (1- (, init)) ))
965 (while (>= (, final) (setq (, var) (1+ (, var))))
966 (,@ body)))))
967
968 (defmacro calendar-sum (index initial condition expression)
969 "For INDEX = INITIAL et seq, as long as CONDITION holds, sum EXPRESSION."
970 (` (let (( (, index) (, initial))
971 (sum 0))
972 (while (, condition)
973 (setq sum (+ sum (, expression) ))
974 (setq (, index) (1+ (, index))))
975 sum)))
976
977 ;; The following are in-line for speed; they can be called thousands of times
978 ;; when looking up holidays or processing the diary. Here, for example, are
979 ;; the numbers of calls to calendar/diary/holiday functions in preparing the
980 ;; fancy diary display, for a moderately complex diary file, with functions
981 ;; used instead of macros. There were a total of 10000 such calls:
982 ;;
983 ;; 1934 extract-calendar-month
984 ;; 1852 extract-calendar-year
985 ;; 1819 extract-calendar-day
986 ;; 845 calendar-leap-year-p
987 ;; 837 calendar-day-number
988 ;; 775 calendar-absolute-from-gregorian
989 ;; 346 calendar-last-day-of-month
990 ;; 286 hebrew-calendar-last-day-of-month
991 ;; 188 hebrew-calendar-leap-year-p
992 ;; 180 hebrew-calendar-elapsed-days
993 ;; 163 hebrew-calendar-last-month-of-year
994 ;; 66 calendar-date-compare
995 ;; 65 hebrew-calendar-days-in-year
996 ;; 60 calendar-absolute-from-julian
997 ;; 50 calendar-absolute-from-hebrew
998 ;; 43 calendar-date-equal
999 ;; 38 calendar-gregorian-from-absolute
1000 ;; .
1001 ;; .
1002 ;; .
1003 ;;
1004 ;; The use of these seven macros eliminates the overhead of 92% of the function
1005 ;; calls; it's faster this way.
1006
1007 (defsubst extract-calendar-month (date)
1008 "Extract the month part of DATE which has the form (month day year)."
1009 (car date))
1010
1011 (defsubst extract-calendar-day (date)
1012 "Extract the day part of DATE which has the form (month day year)."
1013 (car (cdr date)))
1014
1015 (defsubst extract-calendar-year (date)
1016 "Extract the year part of DATE which has the form (month day year)."
1017 (car (cdr (cdr date))))
1018
1019 (defsubst calendar-leap-year-p (year)
1020 "Returns t if YEAR is a Gregorian leap year."
1021 (and (zerop (% year 4))
1022 (or (not (zerop (% year 100)))
1023 (zerop (% year 400)))))
1024
1025 ;; The foregoing is a bit faster, but not as clear as the following:
1026 ;;
1027 ;;(defsubst calendar-leap-year-p (year)
1028 ;; "Returns t if YEAR is a Gregorian leap year."
1029 ;; (or
1030 ;; (and (= (% year 4) 0)
1031 ;; (/= (% year 100) 0))
1032 ;; (= (% year 400) 0)))
1033
1034 (defsubst calendar-last-day-of-month (month year)
1035 "The last day in MONTH during YEAR."
1036 (if (and (= month 2) (calendar-leap-year-p year))
1037 29
1038 (aref [31 28 31 30 31 30 31 31 30 31 30 31] (1- month))))
1039
1040 ;; An explanation of the calculation can be found in PascAlgorithms by
1041 ;; Edward and Ruth Reingold, Scott-Foresman/Little, Brown, 1988.
1042
1043 (defsubst calendar-day-number (date)
1044 "Return the day number within the year of the date DATE.
1045 For example, (calendar-day-number '(1 1 1987)) returns the value 1,
1046 while (calendar-day-number '(12 31 1980)) returns 366."
1047 (let* ((month (extract-calendar-month date))
1048 (day (extract-calendar-day date))
1049 (year (extract-calendar-year date))
1050 (day-of-year (+ day (* 31 (1- month)))))
1051 (if (> month 2)
1052 (progn
1053 (setq day-of-year (- day-of-year (/ (+ 23 (* 4 month)) 10)))
1054 (if (calendar-leap-year-p year)
1055 (setq day-of-year (1+ day-of-year)))))
1056 day-of-year))
1057
1058 (defsubst calendar-absolute-from-gregorian (date)
1059 "The number of days elapsed between the Gregorian date 12/31/1 BC and DATE.
1060 The Gregorian date Sunday, December 31, 1 BC is imaginary."
1061 (let ((prior-years (1- (extract-calendar-year date))))
1062 (+ (calendar-day-number date);; Days this year
1063 (* 365 prior-years);; + Days in prior years
1064 (/ prior-years 4);; + Julian leap years
1065 (- (/ prior-years 100));; - century years
1066 (/ prior-years 400))));; + Gregorian leap years
1067
1068 ;;;###autoload
1069 (defun calendar (&optional arg)
1070 "Display a three-month calendar in another window.
1071 The three months appear side by side, with the current month in the middle
1072 surrounded by the previous and next months. The cursor is put on today's date.
1073
1074 If called with an optional prefix argument, prompts for month and year.
1075
1076 This function is suitable for execution in a .emacs file; appropriate setting
1077 of the variable `view-diary-entries-initially' will cause the diary entries for
1078 the current date to be displayed in another window. The value of the variable
1079 `number-of-diary-entries' controls the number of days of diary entries
1080 displayed upon initial display of the calendar.
1081
1082 An optional prefix argument ARG causes the calendar displayed to be ARG
1083 months in the future if ARG is positive or in the past if ARG is negative;
1084 in this case the cursor goes on the first day of the month.
1085
1086 Once in the calendar window, future or past months can be moved into view.
1087 Arbitrary months can be displayed, or the calendar can be scrolled forward
1088 or backward.
1089
1090 The cursor can be moved forward or backward by one day, one week, one month,
1091 or one year. All of these commands take prefix arguments which, when negative,
1092 cause movement in the opposite direction. For convenience, the digit keys
1093 and the minus sign are automatically prefixes. The window is replotted as
1094 necessary to display the desired date.
1095
1096 Diary entries can be marked on the calendar or displayed in another window.
1097
1098 Use M-x describe-mode for details of the key bindings in the calendar window.
1099
1100 The Gregorian calendar is assumed.
1101
1102 After loading the calendar, the hooks given by the variable
1103 `calendar-load-hook' are run. This is the place to add key bindings to the
1104 calendar-mode-map.
1105
1106 After preparing the calendar window initially, the hooks given by the variable
1107 `initial-calendar-window-hook' are run.
1108
1109 The hooks given by the variable `today-visible-calendar-hook' are run
1110 everytime the calendar window gets scrolled, if the current date is visible
1111 in the window. If it is not visible, the hooks given by the variable
1112 `today-invisible-calendar-hook' are run. Thus, for example, setting
1113 `today-visible-calendar-hook' to 'calendar-star-date will cause today's date
1114 to be replaced by asterisks to highlight it whenever it is in the window."
1115 (interactive "P")
1116 (set-buffer (get-buffer-create calendar-buffer))
1117 (calendar-mode)
1118 (let* ((pop-up-windows t)
1119 (split-height-threshold 1000)
1120 (date (if arg
1121 (calendar-read-date t)
1122 (calendar-current-date)))
1123 (month (extract-calendar-month date))
1124 (year (extract-calendar-year date)))
1125 (pop-to-buffer calendar-buffer)
1126 (increment-calendar-month month year (- calendar-offset))
1127 (generate-calendar-window month year)
1128 (if (and view-diary-entries-initially (calendar-date-is-visible-p date))
1129 (view-diary-entries
1130 (if (vectorp number-of-diary-entries)
1131 (aref number-of-diary-entries (calendar-day-of-week date))
1132 number-of-diary-entries))))
1133 (let* ((diary-buffer (get-file-buffer diary-file))
1134 (diary-window (if diary-buffer (get-buffer-window diary-buffer)))
1135 (split-height-threshold (if diary-window 2 1000)))
1136 (if view-calendar-holidays-initially
1137 (list-calendar-holidays)))
1138 (run-hooks 'initial-calendar-window-hook))
1139
1140 (autoload 'view-diary-entries "diary-lib"
1141 "Prepare and display a buffer with diary entries.
1142 Searches your diary file for entries that match ARG days starting with
1143 the date indicated by the cursor position in the displayed three-month
1144 calendar."
1145 t)
1146
1147 (autoload 'calendar-sunrise-sunset "solar"
1148 "Local time of sunrise and sunset for date under cursor."
1149 t)
1150
1151 (autoload 'calendar-phases-of-moon "lunar"
1152 "Create a buffer of the phases of the moon for the current calendar window."
1153 t)
1154
1155 (autoload 'calendar-print-french-date "cal-french"
1156 "Show the French Revolutionary calendar equivalent of the date under the cursor."
1157 t)
1158
1159 (autoload 'calendar-goto-french-date "cal-french"
1160 "Move cursor to French Revolutionary date."
1161 t)
1162
1163 (autoload 'calendar-french-date-string "cal-french"
1164 "String of French Revolutionary date of Gregorian DATE."
1165 t)
1166
1167 (autoload 'calendar-mayan-date-string "cal-mayan"
1168 "String of Mayan date of Gregorian DATE."
1169 t)
1170
1171 (autoload 'calendar-print-mayan-date "cal-mayan"
1172 "Show the Mayan long count, Tzolkin, and Haab equivalents of the date under the cursor."
1173 t)
1174
1175 (autoload 'calendar-goto-mayan-long-count-date "cal-mayan"
1176 "Move cursor to Mayan long count date."
1177 t)
1178
1179 (autoload 'calendar-next-haab-date "cal-mayan"
1180 "Move cursor to next instance of Mayan Haab date."
1181 t)
1182
1183 (autoload 'calendar-previous-haab-date "cal-mayan"
1184 "Move cursor to previous instance of Mayan Haab date."
1185 t)
1186
1187 (autoload 'calendar-next-tzolkin-date "cal-mayan"
1188 "Move cursor to next instance of Mayan Tzolkin date."
1189 t)
1190
1191 (autoload 'calendar-previous-tzolkin-date "cal-mayan"
1192 "Move cursor to previous instance of Mayan Tzolkin date."
1193 t)
1194
1195 (autoload 'calendar-next-calendar-round-date "cal-mayan"
1196 "Move cursor to next instance of Mayan Haab/Tzoklin combination."
1197 t)
1198
1199 (autoload 'calendar-previous-calendar-round-date "cal-mayan"
1200 "Move cursor to previous instance of Mayan Haab/Tzoklin combination."
1201 t)
1202
1203 (autoload 'show-all-diary-entries "diary-lib"
1204 "Show all of the diary entries in the diary file.
1205 This function gets rid of the selective display of the diary file so that
1206 all entries, not just some, are visible. If there is no diary buffer, one
1207 is created."
1208 t)
1209
1210 (autoload 'mark-diary-entries "diary-lib"
1211 "Mark days in the calendar window that have diary entries.
1212 Each entry in diary file visible in the calendar window is marked."
1213 t)
1214
1215 (autoload 'insert-diary-entry "diary-ins"
1216 "Insert a diary entry for the date indicated by point."
1217 t)
1218
1219 (autoload 'insert-weekly-diary-entry "diary-ins"
1220 "Insert a weekly diary entry for the day of the week indicated by point."
1221 t)
1222
1223
1224 (autoload 'insert-monthly-diary-entry "diary-ins"
1225 "Insert a monthly diary entry for the day of the month indicated by point."
1226 t)
1227
1228 (autoload 'insert-yearly-diary-entry "diary-ins"
1229 "Insert an annual diary entry for the day of the year indicated by point."
1230 t)
1231
1232 (autoload 'insert-anniversary-diary-entry "diary-ins"
1233 "Insert an anniversary diary entry for the date indicated by point."
1234 t)
1235
1236 (autoload 'insert-block-diary-entry "diary-ins"
1237 "Insert a block diary entry for the dates indicated by point and mark."
1238 t)
1239
1240 (autoload 'insert-cyclic-diary-entry "diary-ins"
1241 "Insert a cyclic diary entry starting at the date indicated by point."
1242 t)
1243
1244 (autoload 'insert-hebrew-diary-entry "diary-ins"
1245 "Insert a diary entry for the Hebrew date corresponding to the date
1246 indicated by point."
1247 t)
1248
1249 (autoload 'insert-monthly-hebrew-diary-entry "diary-ins"
1250 "Insert a monthly diary entry for the day of the Hebrew month corresponding
1251 to the date indicated by point."
1252 t)
1253
1254 (autoload 'insert-yearly-hebrew-diary-entry "diary-ins"
1255 "Insert an annual diary entry for the day of the Hebrew year corresponding
1256 to the date indicated by point."
1257 t)
1258
1259 (autoload 'insert-islamic-diary-entry "diary-ins"
1260 "Insert a diary entry for the Islamic date corresponding to the date
1261 indicated by point."
1262 t)
1263
1264 (autoload 'insert-monthly-islamic-diary-entry "diary-ins"
1265 "Insert a monthly diary entry for the day of the Islamic month corresponding
1266 to the date indicated by point."
1267 t)
1268
1269 (autoload 'insert-yearly-islamic-diary-entry "diary-ins"
1270 "Insert an annual diary entry for the day of the Islamic year corresponding
1271 to the date indicated by point."
1272 t)
1273
1274 (autoload 'list-calendar-holidays "holidays"
1275 "Create a buffer containing the holidays for the current calendar window.
1276 The holidays are those in the list `calendar-notable-days'. Returns t if any
1277 holidays are found, nil if not."
1278 t)
1279
1280 (autoload 'mark-calendar-holidays "holidays"
1281 "Mark notable days in the calendar window."
1282 t)
1283
1284 (autoload 'calendar-cursor-holidays "holidays"
1285 "Find holidays for the date specified by the cursor in the calendar window."
1286 t)
1287
1288 (defun generate-calendar-window (&optional mon yr)
1289 "Generate the calendar window for the current date.
1290 Or, for optional MON, YR."
1291 (let* ((buffer-read-only nil)
1292 (today (calendar-current-date))
1293 (month (extract-calendar-month today))
1294 (day (extract-calendar-day today))
1295 (year (extract-calendar-year today))
1296 (today-visible
1297 (or (not mon)
1298 (let ((offset (calendar-interval mon yr month year)))
1299 (and (<= offset 1) (>= offset -1)))))
1300 (day-in-week (calendar-day-of-week today)))
1301 (update-calendar-mode-line)
1302 (if mon
1303 (generate-calendar mon yr)
1304 (generate-calendar month year))
1305 (calendar-cursor-to-visible-date
1306 (if today-visible today (list displayed-month 1 displayed-year)))
1307 (set-buffer-modified-p nil)
1308 (or (one-window-p t)
1309 (/= (frame-width) (window-width))
1310 (shrink-window (- (window-height) 9)))
1311 (sit-for 0)
1312 (and mark-holidays-in-calendar
1313 (mark-calendar-holidays)
1314 (sit-for 0))
1315 (unwind-protect
1316 (if mark-diary-entries-in-calendar (mark-diary-entries))
1317 (if today-visible
1318 (run-hooks 'today-visible-calendar-hook)
1319 (run-hooks 'today-invisible-calendar-hook)))))
1320
1321 (defun generate-calendar (month year)
1322 "Generate a three-month Gregorian calendar centered around MONTH, YEAR."
1323 (if (< (+ month (* 12 (1- year))) 2)
1324 (error "Months before February, 1 AD are not available."))
1325 (setq displayed-month month)
1326 (setq displayed-year year)
1327 (erase-buffer)
1328 (increment-calendar-month month year -1)
1329 (calendar-for-loop i from 0 to 2 do
1330 (generate-calendar-month month year (+ 5 (* 25 i)))
1331 (increment-calendar-month month year 1)))
1332
1333 (defun generate-calendar-month (month year indent)
1334 "Produce a calendar for MONTH, YEAR on the Gregorian calendar.
1335 The calendar is inserted in the buffer starting at the line on which point
1336 is currently located, but indented INDENT spaces. The indentation is done
1337 from the first character on the line and does not disturb the first INDENT
1338 characters on the line."
1339 (let* ((blank-days;; at start of month
1340 (mod
1341 (- (calendar-day-of-week (list month 1 year))
1342 calendar-week-start-day)
1343 7))
1344 (last (calendar-last-day-of-month month year)))
1345 (goto-char (point-min))
1346 (calendar-insert-indented
1347 (calendar-string-spread
1348 (list "" (format "%s %d" (calendar-month-name month) year) "") ? 20)
1349 indent t)
1350 (calendar-insert-indented "" indent);; Go to proper spot
1351 (calendar-for-loop i from 0 to 6 do
1352 (insert (substring (aref calendar-day-name-array
1353 (mod (+ calendar-week-start-day i) 7))
1354 0 2))
1355 (insert " "))
1356 (calendar-insert-indented "" 0 t);; Force onto following line
1357 (calendar-insert-indented "" indent);; Go to proper spot
1358 ;; Add blank days before the first of the month
1359 (calendar-for-loop i from 1 to blank-days do (insert " "))
1360 ;; Put in the days of the month
1361 (calendar-for-loop i from 1 to last do
1362 (insert (format "%2d " i))
1363 (put-text-property (- (point) (if (< i 10) 2 3)) (1- (point))
1364 'highlight t)
1365 (and (zerop (mod (+ i blank-days) 7))
1366 (/= i last)
1367 (calendar-insert-indented "" 0 t) ;; Force onto following line
1368 (calendar-insert-indented "" indent)))));; Go to proper spot
1369
1370 (defun calendar-insert-indented (string indent &optional newline)
1371 "Insert STRING at column INDENT.
1372 If the optional parameter NEWLINE is t, leave point at start of next line,
1373 inserting a newline if there was no next line; otherwise, leave point after
1374 the inserted text. Value is always t."
1375 ;; Try to move to that column.
1376 (move-to-column indent)
1377 ;; If line is too short, indent out to that column.
1378 (if (< (current-column) indent)
1379 (indent-to indent))
1380 (insert string)
1381 ;; Advance to next line, if requested.
1382 (if newline
1383 (progn
1384 (end-of-line)
1385 (if (eobp)
1386 (newline)
1387 (forward-line 1))))
1388 t)
1389
1390 (defun redraw-calendar ()
1391 "Redraw the calendar display."
1392 (interactive)
1393 (let ((cursor-date (calendar-cursor-to-date)))
1394 (generate-calendar-window displayed-month displayed-year)
1395 (calendar-cursor-to-visible-date cursor-date)))
1396
1397 (defvar calendar-debug-sexp nil
1398 "*Turn debugging on when evaluating a sexp in the diary or holiday list.")
1399
1400 (defvar calendar-mode-map nil)
1401 (if calendar-mode-map
1402 nil
1403 (setq calendar-mode-map (make-sparse-keymap))
1404 (calendar-for-loop i from 0 to 9 do
1405 (define-key calendar-mode-map (int-to-string i) 'digit-argument))
1406 (let ((l (list 'narrow-to-region 'mark-word 'mark-sexp 'mark-paragraph
1407 'mark-defun 'mark-whole-buffer 'mark-page
1408 'downcase-region 'upcase-region 'kill-region
1409 'copy-region-as-kill 'capitalize-region 'write-region)))
1410 (while l
1411 (substitute-key-definition (car l) 'calendar-not-implemented
1412 calendar-mode-map)
1413 (setq l (cdr l))))
1414 (define-key calendar-mode-map "-" 'negative-argument)
1415 (define-key calendar-mode-map "\C-x>" 'scroll-calendar-right)
1416 (define-key calendar-mode-map '[prior] 'scroll-calendar-right-three-months)
1417 (define-key calendar-mode-map "\M-v" 'scroll-calendar-right-three-months)
1418 (define-key calendar-mode-map "\C-x<" 'scroll-calendar-left)
1419 (define-key calendar-mode-map '[next] 'scroll-calendar-left-three-months)
1420 (define-key calendar-mode-map "\C-v" 'scroll-calendar-left-three-months)
1421 (define-key calendar-mode-map "\C-b" 'calendar-backward-day)
1422 (define-key calendar-mode-map "\C-p" 'calendar-backward-week)
1423 (define-key calendar-mode-map "\M-{" 'calendar-backward-month)
1424 (define-key calendar-mode-map "\C-x[" 'calendar-backward-year)
1425 (define-key calendar-mode-map "\C-f" 'calendar-forward-day)
1426 (define-key calendar-mode-map "\C-n" 'calendar-forward-week)
1427 (define-key calendar-mode-map '[left] 'calendar-backward-day)
1428 (define-key calendar-mode-map '[up] 'calendar-backward-week)
1429 (define-key calendar-mode-map '[right] 'calendar-forward-day)
1430 (define-key calendar-mode-map '[down] 'calendar-forward-week)
1431 (define-key calendar-mode-map "\M-}" 'calendar-forward-month)
1432 (define-key calendar-mode-map "\C-x]" 'calendar-forward-year)
1433 (define-key calendar-mode-map "\C-a" 'calendar-beginning-of-week)
1434 (define-key calendar-mode-map "\C-e" 'calendar-end-of-week)
1435 (define-key calendar-mode-map "\M-a" 'calendar-beginning-of-month)
1436 (define-key calendar-mode-map "\M-e" 'calendar-end-of-month)
1437 (define-key calendar-mode-map "\M-<" 'calendar-beginning-of-year)
1438 (define-key calendar-mode-map "\M->" 'calendar-end-of-year)
1439 (define-key calendar-mode-map "\C-@" 'calendar-set-mark)
1440 ;; Many people are used to typing C-SPC and getting C-@.
1441 (define-key calendar-mode-map "\C- " 'calendar-set-mark)
1442 (define-key calendar-mode-map "\C-x\C-x" 'calendar-exchange-point-and-mark)
1443 (define-key calendar-mode-map "\M-=" 'calendar-count-days-region)
1444 (define-key calendar-mode-map "gd" 'calendar-goto-date)
1445 (define-key calendar-mode-map "gj" 'calendar-goto-julian-date)
1446 (define-key calendar-mode-map "ga" 'calendar-goto-astro-day-number)
1447 (define-key calendar-mode-map "gh" 'calendar-goto-hebrew-date)
1448 (define-key calendar-mode-map "gi" 'calendar-goto-islamic-date)
1449 (define-key calendar-mode-map "gc" 'calendar-goto-iso-date)
1450 (define-key calendar-mode-map "gf" 'calendar-goto-french-date)
1451 (define-key calendar-mode-map "gml" 'calendar-goto-mayan-long-count-date)
1452 (define-key calendar-mode-map "gmpc" 'calendar-previous-calendar-round-date)
1453 (define-key calendar-mode-map "gmnc" 'calendar-next-calendar-round-date)
1454 (define-key calendar-mode-map "gmph" 'calendar-previous-haab-date)
1455 (define-key calendar-mode-map "gmnh" 'calendar-next-haab-date)
1456 (define-key calendar-mode-map "gmpt" 'calendar-previous-tzolkin-date)
1457 (define-key calendar-mode-map "gmnt" 'calendar-next-tzolkin-date)
1458 (define-key calendar-mode-map "S" 'calendar-sunrise-sunset)
1459 (define-key calendar-mode-map "M" 'calendar-phases-of-moon)
1460 (define-key calendar-mode-map " " 'scroll-other-window)
1461 (define-key calendar-mode-map "\C-c\C-l" 'redraw-calendar)
1462 (define-key calendar-mode-map "." 'calendar-goto-today)
1463 (define-key calendar-mode-map "o" 'calendar-other-month)
1464 (define-key calendar-mode-map "q" 'exit-calendar)
1465 (define-key calendar-mode-map "a" 'list-calendar-holidays)
1466 (define-key calendar-mode-map "h" 'calendar-cursor-holidays)
1467 (define-key calendar-mode-map "x" 'mark-calendar-holidays)
1468 (define-key calendar-mode-map "u" 'calendar-unmark)
1469 (define-key calendar-mode-map "m" 'mark-diary-entries)
1470 (define-key calendar-mode-map "d" 'view-diary-entries)
1471 (define-key calendar-mode-map "D" 'view-other-diary-entries)
1472 (define-key calendar-mode-map "s" 'show-all-diary-entries)
1473 (define-key calendar-mode-map "pd" 'calendar-print-day-of-year)
1474 (define-key calendar-mode-map "pc" 'calendar-print-iso-date)
1475 (define-key calendar-mode-map "pj" 'calendar-print-julian-date)
1476 (define-key calendar-mode-map "pa" 'calendar-print-astro-day-number)
1477 (define-key calendar-mode-map "ph" 'calendar-print-hebrew-date)
1478 (define-key calendar-mode-map "pi" 'calendar-print-islamic-date)
1479 (define-key calendar-mode-map "pf" 'calendar-print-french-date)
1480 (define-key calendar-mode-map "pm" 'calendar-print-mayan-date)
1481 (define-key calendar-mode-map "id" 'insert-diary-entry)
1482 (define-key calendar-mode-map "iw" 'insert-weekly-diary-entry)
1483 (define-key calendar-mode-map "im" 'insert-monthly-diary-entry)
1484 (define-key calendar-mode-map "iy" 'insert-yearly-diary-entry)
1485 (define-key calendar-mode-map "ia" 'insert-anniversary-diary-entry)
1486 (define-key calendar-mode-map "ib" 'insert-block-diary-entry)
1487 (define-key calendar-mode-map "ic" 'insert-cyclic-diary-entry)
1488 (define-key calendar-mode-map "ihd" 'insert-hebrew-diary-entry)
1489 (define-key calendar-mode-map "ihm" 'insert-monthly-hebrew-diary-entry)
1490 (define-key calendar-mode-map "ihy" 'insert-yearly-hebrew-diary-entry)
1491 (define-key calendar-mode-map "iid" 'insert-islamic-diary-entry)
1492 (define-key calendar-mode-map "iim" 'insert-monthly-islamic-diary-entry)
1493 (define-key calendar-mode-map "iiy" 'insert-yearly-islamic-diary-entry)
1494 (define-key calendar-mode-map "?" 'calendar-goto-info-node))
1495
1496 (defun describe-calendar-mode ()
1497 "Create a help buffer with a brief description of the calendar-mode."
1498 (interactive)
1499 (with-output-to-temp-buffer "*Help*"
1500 (princ
1501 (format
1502 "Calendar Mode:\nFor a complete description, type %s\n%s\n"
1503 (substitute-command-keys
1504 "\\<calendar-mode-map>\\[describe-mode] from within the calendar")
1505 (substitute-command-keys "\\{calendar-mode-map}")))
1506 (save-excursion
1507 (set-buffer standard-output)
1508 (help-mode))
1509 (print-help-return-message)))
1510
1511 ;; Calendar mode is suitable only for specially formatted data.
1512 (put 'calendar-mode 'mode-class 'special)
1513
1514 (defvar calendar-mode-line-format
1515 (list
1516 (substitute-command-keys "\\<calendar-mode-map>\\[scroll-calendar-left]")
1517 "Calendar"
1518 (substitute-command-keys "\\<calendar-mode-map>\\[calendar-goto-info-node] info/\\[calendar-other-month] other/\\[calendar-goto-today] today")
1519 '(calendar-date-string (calendar-current-date) t)
1520 (substitute-command-keys "\\<calendar-mode-map>\\[scroll-calendar-right]"))
1521 "The mode line of the calendar buffer.")
1522
1523 (defun calendar-goto-info-node ()
1524 "Go to the info node for the calendar."
1525 (interactive)
1526 (require 'info)
1527 (let ((where (Info-find-emacs-command-nodes 'calendar)))
1528 (if (not where)
1529 (error "Couldn't find documentation for the calendar.")
1530 (save-window-excursion (info))
1531 (pop-to-buffer "*info*")
1532 (Info-find-node (car (car where)) (car (cdr (car where)))))))
1533
1534 (defun calendar-mode ()
1535 "A major mode for the calendar window.
1536
1537 For a complete description, type \
1538 \\<calendar-mode-map>\\[calendar-goto-info-node] from within the calendar.
1539
1540 \\<calendar-mode-map>\\{calendar-mode-map}"
1541
1542 (kill-all-local-variables)
1543 (setq major-mode 'calendar-mode)
1544 (setq mode-name "Calendar")
1545 (use-local-map calendar-mode-map)
1546 (setq buffer-read-only t)
1547 (setq indent-tabs-mode nil)
1548 (update-calendar-mode-line)
1549 (if (and (string-match "XEmacs" emacs-version) current-menubar)
1550 (progn
1551 (require 'cal-xemacs)
1552 (calendar-add-menus)
1553 (make-variable-buffer-local 'scroll-on-clipped-lines)
1554 (setq scroll-on-clipped-lines nil)))
1555 (make-local-variable 'calendar-mark-ring)
1556 (make-local-variable 'displayed-month);; Month in middle of window.
1557 (make-local-variable 'displayed-year));; Year in middle of window.
1558
1559 (defun calendar-string-spread (strings char length)
1560 "Concatenate list of STRINGS separated with copies of CHAR to fill LENGTH
1561 There must be at least 2 strings. The effect is like mapconcat but the
1562 separating pieces are as balanced as possible. Each item of STRINGS is
1563 evaluated before concatenation so it can actually be an expression that
1564 evaluates to a string. If LENGTH is too short, the STRINGS are just
1565 concatenated and the result truncated."
1566 ;; The algorithm is based on equation (3.25) on page 85 of Concrete
1567 ;; Mathematics by Ronald L. Graham, Donald E. Knuth, and Oren Patashnik,
1568 ;; Addison-Wesley, Reading, MA, 1989
1569 (let* ((strings (mapcar 'eval strings))
1570 (n (- length (length (apply 'concat strings))))
1571 (m (1- (length strings)))
1572 (s (car strings))
1573 (strings (cdr strings))
1574 (i 0))
1575 (while strings
1576 (setq s (concat s
1577 (make-string (max 0 (/ (+ n i) m)) char)
1578 (car strings)))
1579 (setq i (1+ i))
1580 (setq strings (cdr strings)))
1581 (substring s 0 length)))
1582
1583 (defun update-calendar-mode-line ()
1584 "Update the calendar mode line with the current date and date style."
1585 (if (bufferp (get-buffer calendar-buffer))
1586 (save-excursion
1587 (set-buffer calendar-buffer)
1588 (setq mode-line-format
1589 (calendar-string-spread
1590 calendar-mode-line-format ? (frame-width))))))
1591
1592 (defun calendar-window-list ()
1593 "List of all calendar-related windows."
1594 (let ((calendar-buffers (calendar-buffer-list))
1595 list)
1596 (walk-windows '(lambda (w)
1597 (if (memq (window-buffer w) calendar-buffers)
1598 (setq list (cons w list))))
1599 nil t)
1600 list))
1601
1602 (defun calendar-buffer-list ()
1603 "List of all calendar-related buffers."
1604 (let* ((diary-buffer (get-file-buffer diary-file))
1605 (buffers (list "*Yahrzeits*" lunar-phases-buffer holiday-buffer
1606 fancy-diary-buffer diary-buffer calendar-buffer))
1607 (buffer-list nil)
1608 b)
1609 (while buffers
1610 (setq b (car buffers))
1611 (setq b (cond ((stringp b) (get-buffer b))
1612 ((bufferp b) b)
1613 (t nil)))
1614 (if b (setq buffer-list (cons b buffer-list)))
1615 (setq buffers (cdr buffers)))
1616 buffer-list))
1617
1618 (defun exit-calendar ()
1619 "Get out of the calendar window and hide it and related buffers."
1620 (interactive)
1621 (let* ((diary-buffer (get-file-buffer diary-file)))
1622 (if (and diary-buffer (buffer-modified-p diary-buffer)
1623 (not
1624 (yes-or-no-p
1625 "Diary modified; do you really want to exit the calendar? ")))
1626 (error)
1627 ;; Need to do this multiple times because one time can replace some
1628 ;; calendar-related buffers with other calendar-related buffers
1629 (mapcar (lambda (x)
1630 (mapcar 'calendar-hide-window (calendar-window-list)))
1631 (calendar-window-list)))))
1632
1633 (defun calendar-hide-window (window)
1634 "Hide WINDOW if it is calendar-related."
1635 (let ((buffer (if (window-live-p window) (window-buffer window))))
1636 (if (memq buffer (calendar-buffer-list))
1637 (cond
1638 ((and window-system
1639 (eq 'icon (cdr (assoc 'visibility
1640 (frame-parameters
1641 (window-frame window))))))
1642 nil)
1643 ((and window-system (window-dedicated-p window))
1644 (iconify-frame (window-frame window)))
1645 ((not (and (select-window window) (one-window-p window)))
1646 (delete-window window))
1647 (t (set-buffer buffer)
1648 (bury-buffer))))))
1649
1650 (defun calendar-goto-today ()
1651 "Reposition the calendar window so the current date is visible."
1652 (interactive)
1653 (let ((today (calendar-current-date)));; The date might have changed.
1654 (if (not (calendar-date-is-visible-p today))
1655 (generate-calendar-window)
1656 (update-calendar-mode-line)
1657 (calendar-cursor-to-visible-date today))))
1658
1659 (defun calendar-forward-month (arg)
1660 "Move the cursor forward ARG months.
1661 Movement is backward if ARG is negative."
1662 (interactive "p")
1663 (calendar-cursor-to-nearest-date)
1664 (let* ((cursor-date (calendar-cursor-to-date t))
1665 (month (extract-calendar-month cursor-date))
1666 (day (extract-calendar-day cursor-date))
1667 (year (extract-calendar-year cursor-date)))
1668 (increment-calendar-month month year arg)
1669 (let ((last (calendar-last-day-of-month month year)))
1670 (if (< last day)
1671 (setq day last)))
1672 ;; Put the new month on the screen, if needed, and go to the new date.
1673 (let ((new-cursor-date (list month day year)))
1674 (if (not (calendar-date-is-visible-p new-cursor-date))
1675 (calendar-other-month month year))
1676 (calendar-cursor-to-visible-date new-cursor-date))))
1677
1678 (defun calendar-forward-year (arg)
1679 "Move the cursor forward by ARG years.
1680 Movement is backward if ARG is negative."
1681 (interactive "p")
1682 (calendar-forward-month (* 12 arg)))
1683
1684 (defun calendar-backward-month (arg)
1685 "Move the cursor backward by ARG months.
1686 Movement is forward if ARG is negative."
1687 (interactive "p")
1688 (calendar-forward-month (- arg)))
1689
1690 (defun calendar-backward-year (arg)
1691 "Move the cursor backward ARG years.
1692 Movement is forward is ARG is negative."
1693 (interactive "p")
1694 (calendar-forward-month (* -12 arg)))
1695
1696 (defun scroll-calendar-left (arg)
1697 "Scroll the displayed calendar left by ARG months.
1698 If ARG is negative the calendar is scrolled right. Maintains the relative
1699 position of the cursor with respect to the calendar as well as possible."
1700 (interactive "p")
1701 (calendar-cursor-to-nearest-date)
1702 (let ((old-date (calendar-cursor-to-date))
1703 (today (calendar-current-date)))
1704 (if (/= arg 0)
1705 (progn
1706 (increment-calendar-month displayed-month displayed-year arg)
1707 (generate-calendar-window displayed-month displayed-year)
1708 (calendar-cursor-to-visible-date
1709 (cond
1710 ((calendar-date-is-visible-p old-date) old-date)
1711 ((calendar-date-is-visible-p today) today)
1712 (t (list displayed-month 1 displayed-year))))))))
1713
1714 (defun scroll-calendar-right (arg)
1715 "Scroll the displayed calendar window right by ARG months.
1716 If ARG is negative the calendar is scrolled left. Maintains the relative
1717 position of the cursor with respect to the calendar as well as possible."
1718 (interactive "p")
1719 (scroll-calendar-left (- arg)))
1720
1721 (defun scroll-calendar-left-three-months (arg)
1722 "Scroll the displayed calendar window left by 3*ARG months.
1723 If ARG is negative the calendar is scrolled right. Maintains the relative
1724 position of the cursor with respect to the calendar as well as possible."
1725 (interactive "p")
1726 (scroll-calendar-left (* 3 arg)))
1727
1728 (defun scroll-calendar-right-three-months (arg)
1729 "Scroll the displayed calendar window right by 3*ARG months.
1730 If ARG is negative the calendar is scrolled left. Maintains the relative
1731 position of the cursor with respect to the calendar as well as possible."
1732 (interactive "p")
1733 (scroll-calendar-left (* -3 arg)))
1734
1735 (defun calendar-current-date ()
1736 "Returns the current date in a list (month day year)."
1737 (let ((s (current-time-string)))
1738 (list (length (member (substring s 4 7)
1739 '("Dec" "Nov" "Oct" "Sep" "Aug" "Jul"
1740 "Jun" "May" "Apr" "Mar" "Feb" "Jan")))
1741 (string-to-number (substring s 8 10))
1742 (string-to-number (substring s 20 24)))))
1743
1744 (defun calendar-cursor-to-date (&optional error)
1745 "Returns a list (month day year) of current cursor position.
1746 If cursor is not on a specific date, signals an error if optional parameter
1747 ERROR is t, otherwise just returns nil."
1748 ;; #### This check is to avoid a race condition created by
1749 ;; pop-to-buffer's call to other-window interacting with the 19.13
1750 ;; changes allowing that to be in another frame.
1751 (if (not (number-or-marker-p displayed-month))
1752 nil
1753 (let* ((segment (/ (current-column) 25))
1754 (month (% (+ displayed-month segment -1) 12))
1755 (month (if (= 0 month) 12 month))
1756 (year
1757 (cond
1758 ((and (= 12 month) (= segment 0)) (1- displayed-year))
1759 ((and (= 1 month) (= segment 2)) (1+ displayed-year))
1760 (t displayed-year))))
1761 (if (and (looking-at "[0-9]")
1762 (< 2 (count-lines (point-min) (point))))
1763 (save-excursion
1764 (re-search-backward "[^0-9]")
1765 (list month
1766 (string-to-int (buffer-substring (1+ (point)) (+ 4 (point))))
1767 year))
1768 (if (looking-at "\\*")
1769 (save-excursion
1770 (re-search-backward "[^*]")
1771 (if (looking-at ".\\*\\*")
1772 (list month calendar-starred-day year)
1773 (if error (error "Not on a date!"))))
1774 (if error (error "Not on a date!")))))))
1775
1776 (defun calendar-cursor-to-nearest-date ()
1777 "Move the cursor to the closest date.
1778 The position of the cursor is unchanged if it is already on a date.
1779 Returns the list (month day year) giving the cursor position."
1780 (let ((date (calendar-cursor-to-date))
1781 (column (current-column)))
1782 (if date
1783 date
1784 (if (> 3 (count-lines (point-min) (point)))
1785 (progn
1786 (goto-line 3)
1787 (move-to-column column)))
1788 (if (not (looking-at "[0-9]"))
1789 (if (and (not (looking-at " *$"))
1790 (or (< column 25)
1791 (and (> column 27)
1792 (< column 50))
1793 (and (> column 52)
1794 (< column 75))))
1795 (progn
1796 (re-search-forward "[0-9]" nil t)
1797 (backward-char 1))
1798 (re-search-backward "[0-9]" nil t)))
1799 (calendar-cursor-to-date))))
1800
1801 (defun calendar-forward-day (arg)
1802 "Move the cursor forward ARG days.
1803 Moves backward if ARG is negative."
1804 (interactive "p")
1805 (if (/= 0 arg)
1806 (let*
1807 ((cursor-date (calendar-cursor-to-date))
1808 (cursor-date (if cursor-date
1809 cursor-date
1810 (if (> arg 0) (setq arg (1- arg)))
1811 (calendar-cursor-to-nearest-date)))
1812 (new-cursor-date
1813 (calendar-gregorian-from-absolute
1814 (+ (calendar-absolute-from-gregorian cursor-date) arg)))
1815 (new-display-month (extract-calendar-month new-cursor-date))
1816 (new-display-year (extract-calendar-year new-cursor-date)))
1817 ;; Put the new month on the screen, if needed, and go to the new date.
1818 (if (not (calendar-date-is-visible-p new-cursor-date))
1819 (calendar-other-month new-display-month new-display-year))
1820 (calendar-cursor-to-visible-date new-cursor-date))))
1821
1822 (defun calendar-backward-day (arg)
1823 "Move the cursor back ARG days.
1824 Moves forward if ARG is negative."
1825 (interactive "p")
1826 (calendar-forward-day (- arg)))
1827
1828 (defun calendar-forward-week (arg)
1829 "Move the cursor forward ARG weeks.
1830 Moves backward if ARG is negative."
1831 (interactive "p")
1832 (calendar-forward-day (* arg 7)))
1833
1834 (defun calendar-backward-week (arg)
1835 "Move the cursor back ARG weeks.
1836 Moves forward if ARG is negative."
1837 (interactive "p")
1838 (calendar-forward-day (* arg -7)))
1839
1840 (defun calendar-beginning-of-week (arg)
1841 "Move the cursor back ARG calendar-week-start-day's."
1842 (interactive "p")
1843 (calendar-cursor-to-nearest-date)
1844 (let ((day (calendar-day-of-week (calendar-cursor-to-date))))
1845 (calendar-backward-day
1846 (if (= day calendar-week-start-day)
1847 (* 7 arg)
1848 (+ (mod (- day calendar-week-start-day) 7)
1849 (* 7 (1- arg)))))))
1850
1851 (defun calendar-end-of-week (arg)
1852 "Move the cursor forward ARG calendar-week-start-day+6's."
1853 (interactive "p")
1854 (calendar-cursor-to-nearest-date)
1855 (let ((day (calendar-day-of-week (calendar-cursor-to-date))))
1856 (calendar-forward-day
1857 (if (= day (mod (1- calendar-week-start-day) 7))
1858 (* 7 arg)
1859 (+ (- 6 (mod (- day calendar-week-start-day) 7))
1860 (* 7 (1- arg)))))))
1861
1862 (defun calendar-beginning-of-month (arg)
1863 "Move the cursor backward ARG month beginnings."
1864 (interactive "p")
1865 (calendar-cursor-to-nearest-date)
1866 (let* ((date (calendar-cursor-to-date))
1867 (month (extract-calendar-month date))
1868 (day (extract-calendar-day date))
1869 (year (extract-calendar-year date)))
1870 (if (= day 1)
1871 (calendar-backward-month arg)
1872 (calendar-cursor-to-visible-date (list month 1 year))
1873 (calendar-backward-month (1- arg)))))
1874
1875 (defun calendar-end-of-month (arg)
1876 "Move the cursor forward ARG month ends."
1877 (interactive "p")
1878 (calendar-cursor-to-nearest-date)
1879 (let* ((date (calendar-cursor-to-date))
1880 (month (extract-calendar-month date))
1881 (day (extract-calendar-day date))
1882 (year (extract-calendar-year date))
1883 (last-day (calendar-last-day-of-month month year)))
1884 (if (/= day last-day)
1885 (progn
1886 (calendar-cursor-to-visible-date (list month last-day year))
1887 (setq arg (1- arg))))
1888 (increment-calendar-month month year arg)
1889 (let ((last-day (list
1890 month
1891 (calendar-last-day-of-month month year)
1892 year)))
1893 (if (not (calendar-date-is-visible-p last-day))
1894 (calendar-other-month month year)
1895 (calendar-cursor-to-visible-date last-day)))))
1896
1897 (defun calendar-beginning-of-year (arg)
1898 "Move the cursor backward ARG year beginnings."
1899 (interactive "p")
1900 (calendar-cursor-to-nearest-date)
1901 (let* ((date (calendar-cursor-to-date))
1902 (month (extract-calendar-month date))
1903 (day (extract-calendar-day date))
1904 (year (extract-calendar-year date))
1905 (jan-first (list 1 1 year)))
1906 (if (and (= day 1) (= 1 month))
1907 (calendar-backward-month (* 12 arg))
1908 (if (and (= arg 1)
1909 (calendar-date-is-visible-p jan-first))
1910 (calendar-cursor-to-visible-date jan-first)
1911 (calendar-other-month 1 (- year (1- arg)))))))
1912
1913 (defun calendar-end-of-year (arg)
1914 "Move the cursor forward ARG year beginnings."
1915 (interactive "p")
1916 (calendar-cursor-to-nearest-date)
1917 (let* ((date (calendar-cursor-to-date))
1918 (month (extract-calendar-month date))
1919 (day (extract-calendar-day date))
1920 (year (extract-calendar-year date))
1921 (dec-31 (list 12 31 year)))
1922 (if (and (= day 31) (= 12 month))
1923 (calendar-forward-month (* 12 arg))
1924 (if (and (= arg 1)
1925 (calendar-date-is-visible-p dec-31))
1926 (calendar-cursor-to-visible-date dec-31)
1927 (calendar-other-month 12 (- year (1- arg)))
1928 (calendar-cursor-to-visible-date (list 12 31 displayed-year))))))
1929
1930 ;; The following version of calendar-gregorian-from-absolute is preferred for
1931 ;; reasons of clarity, BUT it's much slower than the version that follows it.
1932
1933 ;;(defun calendar-gregorian-from-absolute (date)
1934 ;; "Compute the list (month day year) corresponding to the absolute DATE.
1935 ;;The absolute date is the number of days elapsed since the (imaginary)
1936 ;;Gregorian date Sunday, December 31, 1 BC."
1937 ;; (let* ((approx (/ date 366));; Approximation from below.
1938 ;; (year ;; Search forward from the approximation.
1939 ;; (+ approx
1940 ;; (calendar-sum y approx
1941 ;; (>= date (calendar-absolute-from-gregorian (list 1 1 (1+ y))))
1942 ;; 1)))
1943 ;; (month ;; Search forward from January.
1944 ;; (1+ (calendar-sum m 1
1945 ;; (> date
1946 ;; (calendar-absolute-from-gregorian
1947 ;; (list m (calendar-last-day-of-month m year) year)))
1948 ;; 1)))
1949 ;; (day ;; Calculate the day by subtraction.
1950 ;; (- date
1951 ;; (1- (calendar-absolute-from-gregorian (list month 1 year))))))
1952 ;; (list month day year)))
1953
1954 (defun calendar-gregorian-from-absolute (date)
1955 "Compute the list (month day year) corresponding to the absolute DATE.
1956 The absolute date is the number of days elapsed since the (imaginary)
1957 Gregorian date Sunday, December 31, 1 BC."
1958 ;; See the footnote on page 384 of ``Calendrical Calculations, Part II:
1959 ;; Three Historical Calendars'' by E. M. Reingold, N. Dershowitz, and S. M.
1960 ;; Clamen, Software--Practice and Experience, Volume 23, Number 4
1961 ;; (April, 1993), pages 383-404 for an explanation.
1962 (let* ((d0 (1- date))
1963 (n400 (/ d0 146097))
1964 (d1 (% d0 146097))
1965 (n100 (/ d1 36524))
1966 (d2 (% d1 36524))
1967 (n4 (/ d2 1461))
1968 (d3 (% d2 1461))
1969 (n1 (/ d3 365))
1970 (day (1+ (% d3 365)))
1971 (year (+ (* 400 n400) (* 100 n100) (* n4 4) n1)))
1972 (if (or (= n100 4) (= n1 4))
1973 (list 12 31 year)
1974 (let ((year (1+ year))
1975 (month 1))
1976 (while (let ((mdays (calendar-last-day-of-month month year)))
1977 (and (< mdays day)
1978 (setq day (- day mdays))))
1979 (setq month (1+ month)))
1980 (list month day year)))))
1981
1982 (defun calendar-cursor-to-visible-date (date)
1983 "Move the cursor to DATE that is on the screen."
1984 (let* ((month (extract-calendar-month date))
1985 (day (extract-calendar-day date))
1986 (year (extract-calendar-year date))
1987 (first-of-month-weekday (calendar-day-of-week (list month 1 year))))
1988 (goto-line (+ 3
1989 (/ (+ day -1
1990 (mod
1991 (- (calendar-day-of-week (list month 1 year))
1992 calendar-week-start-day)
1993 7))
1994 7)))
1995 (move-to-column (+ 6
1996 (* 25
1997 (1+ (calendar-interval
1998 displayed-month displayed-year month year)))
1999 (* 3 (mod
2000 (- (calendar-day-of-week date)
2001 calendar-week-start-day)
2002 7))))))
2003
2004 (defun calendar-other-month (month year)
2005 "Display a three-month calendar centered around MONTH and YEAR."
2006 (interactive (calendar-read-date 'noday))
2007 (if (and (= month displayed-month)
2008 (= year displayed-year))
2009 nil
2010 (let ((old-date (calendar-cursor-to-date))
2011 (today (calendar-current-date)))
2012 (generate-calendar-window month year)
2013 (calendar-cursor-to-visible-date
2014 (cond
2015 ((calendar-date-is-visible-p old-date) old-date)
2016 ((calendar-date-is-visible-p today) today)
2017 (t (list month 1 year)))))))
2018
2019 (defun calendar-set-mark (arg)
2020 "Mark the date under the cursor, or jump to marked date.
2021 With no prefix argument, push current date onto marked date ring.
2022 With argument, jump to mark, pop it, and put point at end of ring."
2023 (interactive "P")
2024 (let ((date (calendar-cursor-to-date t)))
2025 (if (null arg)
2026 (progn
2027 (setq calendar-mark-ring (cons date calendar-mark-ring))
2028 ;; Since the top of the mark ring is the marked date in the
2029 ;; calendar, the mark ring in the calendar is one longer than
2030 ;; in other buffers to get the same effect.
2031 (if (> (length calendar-mark-ring) (1+ mark-ring-max))
2032 (setcdr (nthcdr mark-ring-max calendar-mark-ring) nil))
2033 (message "Mark set"))
2034 (if (null calendar-mark-ring)
2035 (error "No mark set in this buffer")
2036 (calendar-goto-date (car calendar-mark-ring))
2037 (setq calendar-mark-ring
2038 (cdr (nconc calendar-mark-ring (list date))))))))
2039
2040 (defun calendar-exchange-point-and-mark ()
2041 "Exchange the current cursor position with the marked date."
2042 (interactive)
2043 (let ((mark (car calendar-mark-ring))
2044 (date (calendar-cursor-to-date t)))
2045 (if (null mark)
2046 (error "No mark set in this buffer")
2047 (setq calendar-mark-ring (cons date (cdr calendar-mark-ring)))
2048 (calendar-goto-date mark))))
2049
2050 (defun calendar-count-days-region ()
2051 "Count the number of days (inclusive) between point and the mark."
2052 (interactive)
2053 (let* ((days (- (calendar-absolute-from-gregorian
2054 (calendar-cursor-to-date t))
2055 (calendar-absolute-from-gregorian
2056 (or (car calendar-mark-ring)
2057 (error "No mark set in this buffer")))))
2058 (days (1+ (if (> days 0) days (- days)))))
2059 (message "Region has %d day%s (inclusive)"
2060 days (if (> days 1) "s" ""))))
2061
2062 (defun calendar-not-implemented ()
2063 "Not implemented."
2064 (interactive)
2065 (error "%s not available in the calendar"
2066 (global-key-binding (this-command-keys))))
2067
2068 (defun calendar-read (prompt acceptable &optional initial-contents)
2069 "Return an object read from the minibuffer.
2070 Prompt with the string PROMPT and use the function ACCEPTABLE to decide if
2071 entered item is acceptable. If non-nil, optional third arg INITIAL-CONTENTS
2072 is a string to insert in the minibuffer before reading."
2073 (let ((value (read-minibuffer prompt initial-contents)))
2074 (while (not (funcall acceptable value))
2075 (setq value (read-minibuffer prompt initial-contents)))
2076 value))
2077
2078 (defun calendar-read-date (&optional noday)
2079 "Prompt for Gregorian date. Returns a list (month day year).
2080 If optional NODAY is t, does not ask for day, but just returns
2081 (month nil year); if NODAY is any other non-nil value the value returned is
2082 (month year) "
2083 (let* ((year (calendar-read
2084 "Year (>0): "
2085 '(lambda (x) (> x 0))
2086 (int-to-string (extract-calendar-year
2087 (calendar-current-date)))))
2088 (month-array calendar-month-name-array)
2089 (completion-ignore-case t)
2090 (month (cdr (assoc
2091 (capitalize
2092 (completing-read
2093 "Month name: "
2094 (mapcar 'list (append month-array nil))
2095 nil t))
2096 (calendar-make-alist month-array 1 'capitalize))))
2097 (last (calendar-last-day-of-month month year)))
2098 (if noday
2099 (if (eq noday t)
2100 (list month nil year)
2101 (list month year))
2102 (list month
2103 (calendar-read (format "Day (1-%d): " last)
2104 '(lambda (x) (and (< 0 x) (<= x last))))
2105 year))))
2106
2107 (defun calendar-goto-date (date)
2108 "Move cursor to DATE."
2109 (interactive (list (calendar-read-date)))
2110 (let ((month (extract-calendar-month date))
2111 (year (extract-calendar-year date)))
2112 (if (not (calendar-date-is-visible-p date))
2113 (calendar-other-month
2114 (if (and (= month 1) (= year 1))
2115 2
2116 month)
2117 year)))
2118 (calendar-cursor-to-visible-date date))
2119
2120 (defun calendar-goto-julian-date (date &optional noecho)
2121 "Move cursor to Julian DATE; echo Julian date unless NOECHO is t."
2122 (interactive
2123 (let* ((today (calendar-current-date))
2124 (year (calendar-read
2125 "Julian calendar year (>0): "
2126 '(lambda (x) (> x 0))
2127 (int-to-string
2128 (extract-calendar-year
2129 (calendar-julian-from-absolute
2130 (calendar-absolute-from-gregorian
2131 today))))))
2132 (month-array calendar-month-name-array)
2133 (completion-ignore-case t)
2134 (month (cdr (assoc
2135 (capitalize
2136 (completing-read
2137 "Julian calendar month name: "
2138 (mapcar 'list (append month-array nil))
2139 nil t))
2140 (calendar-make-alist month-array 1 'capitalize))))
2141 (last
2142 (if (and (zerop (% year 4)) (= month 2))
2143 29
2144 (aref [31 28 31 30 31 30 31 31 30 31 30 31] (1- month))))
2145 (day (calendar-read
2146 (format "Julian calendar day (%d-%d): "
2147 (if (and (= year 1) (= month 1)) 3 1) last)
2148 '(lambda (x)
2149 (and (< (if (and (= year 1) (= month 1)) 2 0) x)
2150 (<= x last))))))
2151 (list (list month day year))))
2152 (calendar-goto-date (calendar-gregorian-from-absolute
2153 (calendar-absolute-from-julian date)))
2154 (or noecho (calendar-print-julian-date)))
2155
2156 (defun calendar-goto-hebrew-date (date &optional noecho)
2157 "Move cursor to Hebrew DATE; echo Hebrew date unless NOECHO is t."
2158 (interactive
2159 (let* ((today (calendar-current-date))
2160 (year (calendar-read
2161 "Hebrew calendar year (>3760): "
2162 '(lambda (x) (> x 3760))
2163 (int-to-string
2164 (extract-calendar-year
2165 (calendar-hebrew-from-absolute
2166 (calendar-absolute-from-gregorian today))))))
2167 (month-array (if (hebrew-calendar-leap-year-p year)
2168 calendar-hebrew-month-name-array-leap-year
2169 calendar-hebrew-month-name-array-common-year))
2170 (completion-ignore-case t)
2171 (month (cdr (assoc
2172 (capitalize
2173 (completing-read
2174 "Hebrew calendar month name: "
2175 (mapcar 'list (append month-array nil))
2176 (if (= year 3761)
2177 '(lambda (x)
2178 (let ((m (cdr
2179 (assoc
2180 (car x)
2181 (calendar-make-alist
2182 month-array)))))
2183 (< 0
2184 (calendar-absolute-from-hebrew
2185 (list m
2186 (hebrew-calendar-last-day-of-month
2187 m year)
2188 year))))))
2189
2190 t))
2191 (calendar-make-alist month-array 1 'capitalize))))
2192 (last (hebrew-calendar-last-day-of-month month year))
2193 (first (if (and (= year 3761) (= month 10))
2194 18 1))
2195 (day (calendar-read
2196 (format "Hebrew calendar day (%d-%d): "
2197 first last)
2198 '(lambda (x) (and (<= first x) (<= x last))))))
2199 (list (list month day year))))
2200 (calendar-goto-date (calendar-gregorian-from-absolute
2201 (calendar-absolute-from-hebrew date)))
2202 (or noecho (calendar-print-hebrew-date)))
2203
2204 (defun calendar-goto-islamic-date (date &optional noecho)
2205 "Move cursor to Islamic DATE; echo Islamic date unless NOECHO is t."
2206 (interactive
2207 (let* ((today (calendar-current-date))
2208 (year (calendar-read
2209 "Islamic calendar year (>0): "
2210 '(lambda (x) (> x 0))
2211 (int-to-string
2212 (extract-calendar-year
2213 (calendar-islamic-from-absolute
2214 (calendar-absolute-from-gregorian today))))))
2215 (month-array calendar-islamic-month-name-array)
2216 (completion-ignore-case t)
2217 (month (cdr (assoc
2218 (capitalize
2219 (completing-read
2220 "Islamic calendar month name: "
2221 (mapcar 'list (append month-array nil))
2222 nil t))
2223 (calendar-make-alist month-array 1 'capitalize))))
2224 (last (islamic-calendar-last-day-of-month month year))
2225 (day (calendar-read
2226 (format "Islamic calendar day (1-%d): " last)
2227 '(lambda (x) (and (< 0 x) (<= x last))))))
2228 (list (list month day year))))
2229 (calendar-goto-date (calendar-gregorian-from-absolute
2230 (calendar-absolute-from-islamic date)))
2231 (or noecho (calendar-print-islamic-date)))
2232
2233 (defun calendar-goto-iso-date (date &optional noecho)
2234 "Move cursor to ISO DATE; echo ISO date unless NOECHO is t."
2235 (interactive
2236 (let* ((today (calendar-current-date))
2237 (year (calendar-read
2238 "ISO calendar year (>0): "
2239 '(lambda (x) (> x 0))
2240 (int-to-string (extract-calendar-year today))))
2241 (no-weeks (extract-calendar-month
2242 (calendar-iso-from-absolute
2243 (1-
2244 (calendar-dayname-on-or-before
2245 1 (calendar-absolute-from-gregorian
2246 (list 1 4 (1+ year))))))))
2247 (week (calendar-read
2248 (format "ISO calendar week (1-%d): " no-weeks)
2249 '(lambda (x) (and (> x 0) (<= x no-weeks)))))
2250 (day (calendar-read
2251 "ISO day (1-7): "
2252 '(lambda (x) (and (<= 1 x) (<= x 7))))))
2253 (list (list week day year))))
2254 (calendar-goto-date (calendar-gregorian-from-absolute
2255 (calendar-absolute-from-iso date)))
2256 (or noecho (calendar-print-iso-date)))
2257
2258 (defun calendar-interval (mon1 yr1 mon2 yr2)
2259 "The number of months difference between MON1, YR1 and MON2, YR2."
2260 (+ (* 12 (- yr2 yr1))
2261 (- mon2 mon1)))
2262
2263 (defun calendar-day-name (date)
2264 "Returns a string with the name of the day of the week of DATE."
2265 (aref calendar-day-name-array (calendar-day-of-week date)))
2266
2267 (defvar calendar-day-name-array
2268 ["Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday"])
2269
2270 (defvar calendar-month-name-array
2271 ["January" "February" "March" "April" "May" "June"
2272 "July" "August" "September" "October" "November" "December"])
2273
2274 (defun calendar-make-alist (sequence &optional start-index filter)
2275 "Make an assoc list corresponding to SEQUENCE.
2276 Start at index 1, unless optional START-INDEX is provided.
2277 If FILTER is provided, apply it to each item in the list."
2278 (let ((index (if start-index (1- start-index) 0)))
2279 (mapcar
2280 '(lambda (x)
2281 (setq index (1+ index))
2282 (cons (if filter (funcall filter x) x)
2283 index))
2284 (append sequence nil))))
2285
2286 (defun calendar-month-name (month)
2287 "The name of MONTH."
2288 (aref calendar-month-name-array (1- month)))
2289
2290 (defun calendar-day-of-week (date)
2291 "Returns the day-of-the-week index of DATE, 0 for Sunday, 1 for Monday, etc."
2292 (% (calendar-absolute-from-gregorian date) 7))
2293
2294 (defun calendar-unmark ()
2295 "Delete all diary/holiday marks/highlighting from the calendar."
2296 (interactive)
2297 (setq mark-holidays-in-calendar nil)
2298 (setq mark-diary-entries-in-calendar nil)
2299 (redraw-calendar))
2300
2301 (defun calendar-date-is-visible-p (date)
2302 "Returns t if DATE is legal and is visible in the calendar window."
2303 (let ((gap (calendar-interval
2304 displayed-month displayed-year
2305 (extract-calendar-month date) (extract-calendar-year date))))
2306 (and (calendar-date-is-legal-p date) (> 2 gap) (< -2 gap))))
2307
2308 (defun calendar-date-is-legal-p (date)
2309 "Returns t if DATE is a legal date."
2310 (let ((month (extract-calendar-month date))
2311 (day (extract-calendar-day date))
2312 (year (extract-calendar-year date)))
2313 (and (<= 1 month) (<= month 12)
2314 (<= 1 day) (<= day (calendar-last-day-of-month month year))
2315 (<= 1 year))))
2316
2317 (defun calendar-date-equal (date1 date2)
2318 "Returns t if the DATE1 and DATE2 are the same."
2319 (and
2320 (= (extract-calendar-month date1) (extract-calendar-month date2))
2321 (= (extract-calendar-day date1) (extract-calendar-day date2))
2322 (= (extract-calendar-year date1) (extract-calendar-year date2))))
2323
2324 (defun mark-visible-calendar-date (date &optional mark)
2325 "Mark DATE in the calendar window with MARK.
2326 MARK is either a single-character string or a face.
2327 MARK defaults to diary-entry-marker."
2328 (if (calendar-date-is-legal-p date)
2329 (save-excursion
2330 (set-buffer calendar-buffer)
2331 (calendar-cursor-to-visible-date date)
2332 (let ((mark (or mark diary-entry-marker)))
2333 (if (stringp mark)
2334 (let ((buffer-read-only nil))
2335 (forward-char 1)
2336 (delete-char 1)
2337 (insert mark)
2338 (forward-char -2))
2339
2340 (set-extent-property (make-extent (1- (point)) (1+ (point)))
2341 'face mark))))))
2342
2343 (defun calendar-star-date ()
2344 "Replace the date under the cursor in the calendar window with asterisks.
2345 This function can be used with the today-visible-calendar-hook run after the
2346 calendar window has been prepared."
2347 (let ((buffer-read-only nil))
2348 (make-variable-buffer-local 'calendar-starred-day)
2349 (forward-char 1)
2350 (setq calendar-starred-day
2351 (string-to-int
2352 (buffer-substring (point) (- (point) 2))))
2353 (delete-char -2)
2354 (insert "**")
2355 (backward-char 1)
2356 (set-buffer-modified-p nil)))
2357
2358 (defun calendar-mark-today ()
2359 "Mark the date under the cursor in the calendar window.
2360 The date is marked with calendar-today-marker. This function can be used with
2361 the today-visible-calendar-hook run after the calendar window has been
2362 prepared."
2363 (mark-visible-calendar-date
2364 (calendar-cursor-to-date)
2365 calendar-today-marker))
2366
2367 (defun calendar-date-compare (date1 date2)
2368 "Returns t if DATE1 is before DATE2, nil otherwise.
2369 The actual dates are in the car of DATE1 and DATE2."
2370 (< (calendar-absolute-from-gregorian (car date1))
2371 (calendar-absolute-from-gregorian (car date2))))
2372
2373 (defun calendar-date-string (date &optional abbreviate nodayname)
2374 "A string form of DATE, driven by the variable `calendar-date-display-form'.
2375 An optional parameter ABBREVIATE, when t, causes the month and day names to be
2376 abbreviated to three characters. An optional parameter NODAYNAME, when t,
2377 omits the name of the day of the week."
2378 (let* ((dayname
2379 (if nodayname
2380 nil
2381 (if abbreviate
2382 (substring (calendar-day-name date) 0 3)
2383 (calendar-day-name date))))
2384 (month (extract-calendar-month date))
2385 (monthname
2386 (if abbreviate
2387 (substring
2388 (calendar-month-name month) 0 3)
2389 (calendar-month-name month)))
2390 (day (int-to-string (extract-calendar-day date)))
2391 (month (int-to-string month))
2392 (year (int-to-string (extract-calendar-year date))))
2393 (mapconcat 'eval calendar-date-display-form "")))
2394
2395 (defun calendar-dayname-on-or-before (dayname date)
2396 "Returns the absolute date of the DAYNAME on or before absolute DATE.
2397 DAYNAME=0 means Sunday, DAYNAME=1 means Monday, and so on.
2398
2399 Note: Applying this function to d+6 gives us the DAYNAME on or after an
2400 absolute day d. Similarly, applying it to d+3 gives the DAYNAME nearest to
2401 absolute date d, applying it to d-1 gives the DAYNAME previous to absolute
2402 date d, and applying it to d+7 gives the DAYNAME following absolute date d."
2403 (- date (% (- date dayname) 7)))
2404
2405 (defun calendar-nth-named-absday (n dayname month year &optional day)
2406 "The absolute date of Nth DAYNAME in MONTH, YEAR before/after optional DAY.
2407 A DAYNAME of 0 means Sunday, 1 means Monday, and so on. If N<0,
2408 return the Nth DAYNAME before MONTH DAY, YEAR (inclusive).
2409 If N>0, return the Nth DAYNAME after MONTH DAY, YEAR (inclusive).
2410
2411 If DAY is omitted, it defaults to 1 if N>0, and MONTH's last day otherwise."
2412 (if (> n 0)
2413 (+ (* 7 (1- n))
2414 (calendar-dayname-on-or-before
2415 dayname
2416 (+ 6 (calendar-absolute-from-gregorian
2417 (list month (or day 1) year)))))
2418 (+ (* 7 (1+ n))
2419 (calendar-dayname-on-or-before
2420 dayname
2421 (calendar-absolute-from-gregorian
2422 (list month
2423 (or day (calendar-last-day-of-month month year))
2424 year))))))
2425
2426 (defun calendar-nth-named-day (n dayname month year &optional day)
2427 "The date of Nth DAYNAME in MONTH, YEAR before/after optional DAY.
2428 A DAYNAME of 0 means Sunday, 1 means Monday, and so on. If N<0,
2429 return the Nth DAYNAME before MONTH DAY, YEAR (inclusive).
2430 If N>0, return the Nth DAYNAME after MONTH DAY, YEAR (inclusive).
2431
2432 If DAY is omitted, it defaults to 1 if N>0, and MONTH's last day otherwise."
2433 (calendar-gregorian-from-absolute
2434 (calendar-nth-named-absday n dayname month year day)))
2435
2436 (defun calendar-day-of-year-string (&optional date)
2437 "String of day number of year of Gregorian DATE.
2438 Defaults to today's date if DATE is not given."
2439 (let* ((d (or date (calendar-current-date)))
2440 (year (extract-calendar-year d))
2441 (day (calendar-day-number d))
2442 (days-remaining (- (calendar-day-number (list 12 31 year)) day)))
2443 (format "Day %d of %d; %d day%s remaining in the year"
2444 day year days-remaining (if (= days-remaining 1) "" "s"))))
2445
2446 (defun calendar-print-day-of-year ()
2447 "Show day number in year/days remaining in year for date under the cursor."
2448 (interactive)
2449 (message (calendar-day-of-year-string (calendar-cursor-to-date t))))
2450
2451 (defun calendar-absolute-from-iso (date)
2452 "The number of days elapsed between the Gregorian date 12/31/1 BC and DATE.
2453 The `ISO year' corresponds approximately to the Gregorian year, but
2454 weeks start on Monday and end on Sunday. The first week of the ISO year is
2455 the first such week in which at least 4 days are in a year. The ISO
2456 commercial DATE has the form (week day year) in which week is in the range
2457 1..52 and day is in the range 0..6 (1 = Monday, 2 = Tuesday, ..., 0 =
2458 Sunday). The Gregorian date Sunday, December 31, 1 BC is imaginary."
2459 (let* ((week (extract-calendar-month date))
2460 (day (extract-calendar-day date))
2461 (year (extract-calendar-year date)))
2462 (+ (calendar-dayname-on-or-before
2463 1 (+ 3 (calendar-absolute-from-gregorian (list 1 1 year))))
2464 (* 7 (1- week))
2465 (if (= day 0) 6 (1- day)))))
2466
2467 (defun calendar-iso-from-absolute (date)
2468 "Compute the `ISO commercial date' corresponding to the absolute DATE.
2469 The ISO year corresponds approximately to the Gregorian year, but weeks
2470 start on Monday and end on Sunday. The first week of the ISO year is the
2471 first such week in which at least 4 days are in a year. The ISO commercial
2472 date has the form (week day year) in which week is in the range 1..52 and
2473 day is in the range 0..6 (1 = Monday, 2 = Tuesday, ..., 0 = Sunday). The
2474 absolute date is the number of days elapsed since the (imaginary) Gregorian
2475 date Sunday, December 31, 1 BC."
2476 (let* ((approx (extract-calendar-year
2477 (calendar-gregorian-from-absolute (- date 3))))
2478 (year (+ approx
2479 (calendar-sum y approx
2480 (>= date (calendar-absolute-from-iso (list 1 1 (1+ y))))
2481 1))))
2482 (list
2483 (1+ (/ (- date (calendar-absolute-from-iso (list 1 1 year))) 7))
2484 (% date 7)
2485 year)))
2486
2487 (defun calendar-iso-date-string (&optional date)
2488 "String of ISO date of Gregorian DATE.
2489 Defaults to today's date if DATE is not given."
2490 (let* ((d (calendar-absolute-from-gregorian
2491 (or date (calendar-current-date))))
2492 (day (% d 7))
2493 (iso-date (calendar-iso-from-absolute d)))
2494 (format "Day %s of week %d of %d"
2495 (if (zerop day) 7 day)
2496 (extract-calendar-month iso-date)
2497 (extract-calendar-year iso-date))))
2498
2499 (defun calendar-print-iso-date ()
2500 "Show equivalent ISO date for the date under the cursor."
2501 (interactive)
2502 (message "ISO date: %s"
2503 (calendar-iso-date-string (calendar-cursor-to-date t))))
2504
2505 (defun calendar-julian-from-absolute (date)
2506 "Compute the Julian (month day year) corresponding to the absolute DATE.
2507 The absolute date is the number of days elapsed since the (imaginary)
2508 Gregorian date Sunday, December 31, 1 BC."
2509 (let* ((approx (/ (+ date 2) 366));; Approximation from below.
2510 (year ;; Search forward from the approximation.
2511 (+ approx
2512 (calendar-sum y approx
2513 (>= date (calendar-absolute-from-julian (list 1 1 (1+ y))))
2514 1)))
2515 (month ;; Search forward from January.
2516 (1+ (calendar-sum m 1
2517 (> date
2518 (calendar-absolute-from-julian
2519 (list m
2520 (if (and (= m 2) (= (% year 4) 0))
2521 29
2522 (aref [31 28 31 30 31 30 31 31 30 31 30 31]
2523 (1- m)))
2524 year)))
2525 1)))
2526 (day ;; Calculate the day by subtraction.
2527 (- date (1- (calendar-absolute-from-julian (list month 1 year))))))
2528 (list month day year)))
2529
2530 (defun calendar-absolute-from-julian (date)
2531 "The number of days elapsed between the Gregorian date 12/31/1 BC and DATE.
2532 The Gregorian date Sunday, December 31, 1 BC is imaginary."
2533 (let ((month (extract-calendar-month date))
2534 (day (extract-calendar-day date))
2535 (year (extract-calendar-year date)))
2536 (+ (calendar-day-number date)
2537 (if (and (= (% year 100) 0)
2538 (/= (% year 400) 0)
2539 (> month 2))
2540 1 0);; Correct for Julian but not Gregorian leap year.
2541 (* 365 (1- year))
2542 (/ (1- year) 4)
2543 -2)))
2544
2545 (defun calendar-julian-date-string (&optional date)
2546 "String of Julian date of Gregorian DATE.
2547 Defaults to today's date if DATE is not given.
2548 Driven by the variable `calendar-date-display-form'."
2549 (calendar-date-string
2550 (calendar-julian-from-absolute
2551 (calendar-absolute-from-gregorian
2552 (or date (calendar-current-date))))
2553 nil t))
2554
2555 (defun calendar-print-julian-date ()
2556 "Show the Julian calendar equivalent of the date under the cursor."
2557 (interactive)
2558 (message "Julian date: %s"
2559 (calendar-julian-date-string (calendar-cursor-to-date t))))
2560
2561 (defun islamic-calendar-leap-year-p (year)
2562 "Returns t if YEAR is a leap year on the Islamic calendar."
2563 (memq (% year 30)
2564 (list 2 5 7 10 13 16 18 21 24 26 29)))
2565
2566 (defun islamic-calendar-last-day-of-month (month year)
2567 "The last day in MONTH during YEAR on the Islamic calendar."
2568 (cond
2569 ((memq month (list 1 3 5 7 9 11)) 30)
2570 ((memq month (list 2 4 6 8 10)) 29)
2571 (t (if (islamic-calendar-leap-year-p year) 30 29))))
2572
2573 (defun islamic-calendar-day-number (date)
2574 "Return the day number within the year of the Islamic date DATE."
2575 (let* ((month (extract-calendar-month date))
2576 (day (extract-calendar-day date)))
2577 (+ (* 30 (/ month 2))
2578 (* 29 (/ (1- month) 2))
2579 day)))
2580
2581 (defun calendar-absolute-from-islamic (date)
2582 "Absolute date of Islamic DATE.
2583 The absolute date is the number of days elapsed since the (imaginary)
2584 Gregorian date Sunday, December 31, 1 BC."
2585 (let* ((month (extract-calendar-month date))
2586 (day (extract-calendar-day date))
2587 (year (extract-calendar-year date))
2588 (y (% year 30))
2589 (leap-years-in-cycle
2590 (cond
2591 ((< y 3) 0) ((< y 6) 1) ((< y 8) 2) ((< y 11) 3) ((< y 14) 4)
2592 ((< y 17) 5) ((< y 19) 6) ((< y 22) 7) ((< y 25) 8) ((< y 27) 9)
2593 (t 10))))
2594 (+ (islamic-calendar-day-number date);; days so far this year
2595 (* (1- year) 354) ;; days in all non-leap years
2596 (* 11 (/ year 30)) ;; leap days in complete cycles
2597 leap-years-in-cycle ;; leap days this cycle
2598 227014))) ;; days before start of calendar
2599
2600 (defun calendar-islamic-from-absolute (date)
2601 "Compute the Islamic date (month day year) corresponding to absolute DATE.
2602 The absolute date is the number of days elapsed since the (imaginary)
2603 Gregorian date Sunday, December 31, 1 BC."
2604 (if (< date 227015)
2605 (list 0 0 0);; pre-Islamic date
2606 (let* ((approx (/ (- date 227014) 355));; Approximation from below.
2607 (year ;; Search forward from the approximation.
2608 (+ approx
2609 (calendar-sum y approx
2610 (>= date (calendar-absolute-from-islamic
2611 (list 1 1 (1+ y))))
2612 1)))
2613 (month ;; Search forward from Muharram.
2614 (1+ (calendar-sum m 1
2615 (> date
2616 (calendar-absolute-from-islamic
2617 (list m
2618 (islamic-calendar-last-day-of-month
2619 m year)
2620 year)))
2621 1)))
2622 (day ;; Calculate the day by subtraction.
2623 (- date
2624 (1- (calendar-absolute-from-islamic (list month 1 year))))))
2625 (list month day year))))
2626
2627 (defvar calendar-islamic-month-name-array
2628 ["Muharram" "Safar" "Rabi I" "Rabi II" "Jumada I" "Jumada II"
2629 "Rajab" "Sha'ban" "Ramadan" "Shawwal" "Dhu al-Qada" "Dhu al-Hijjah"])
2630
2631 (defun calendar-islamic-date-string (&optional date)
2632 "String of Islamic date before sunset of Gregorian DATE.
2633 Returns the empty string if DATE is pre-Islamic.
2634 Defaults to today's date if DATE is not given.
2635 Driven by the variable `calendar-date-display-form'."
2636 (let ((calendar-month-name-array calendar-islamic-month-name-array)
2637 (islamic-date (calendar-islamic-from-absolute
2638 (calendar-absolute-from-gregorian
2639 (or date (calendar-current-date))))))
2640 (if (< (extract-calendar-year islamic-date) 1)
2641 ""
2642 (calendar-date-string islamic-date nil t))))
2643
2644 (defun calendar-print-islamic-date ()
2645 "Show the Islamic calendar equivalent of the date under the cursor."
2646 (interactive)
2647 (let ((i (calendar-islamic-date-string (calendar-cursor-to-date t))))
2648 (if (string-equal i "")
2649 (message "Date is pre-Islamic")
2650 (message "Islamic date (until sunset): %s" i))))
2651
2652 (defun calendar-hebrew-from-absolute (date)
2653 "Compute the Hebrew date (month day year) corresponding to absolute DATE.
2654 The absolute date is the number of days elapsed since the (imaginary)
2655 Gregorian date Sunday, December 31, 1 BC."
2656 (let* ((greg-date (calendar-gregorian-from-absolute date))
2657 (month (aref [9 10 11 12 1 2 3 4 7 7 7 8]
2658 (1- (extract-calendar-month greg-date))))
2659 (day)
2660 (year (+ 3760 (extract-calendar-year greg-date))))
2661 (while (>= date (calendar-absolute-from-hebrew (list 7 1 (1+ year))))
2662 (setq year (1+ year)))
2663 (let ((length (hebrew-calendar-last-month-of-year year)))
2664 (while (> date
2665 (calendar-absolute-from-hebrew
2666 (list month
2667 (hebrew-calendar-last-day-of-month month year)
2668 year)))
2669 (setq month (1+ (% month length)))))
2670 (setq day (1+
2671 (- date (calendar-absolute-from-hebrew (list month 1 year)))))
2672 (list month day year)))
2673
2674 (defun hebrew-calendar-leap-year-p (year)
2675 "t if YEAR is a Hebrew calendar leap year."
2676 (< (% (1+ (* 7 year)) 19) 7))
2677
2678 (defun hebrew-calendar-last-month-of-year (year)
2679 "The last month of the Hebrew calendar YEAR."
2680 (if (hebrew-calendar-leap-year-p year)
2681 13
2682 12))
2683
2684 (defun hebrew-calendar-last-day-of-month (month year)
2685 "The last day of MONTH in YEAR."
2686 (if (or (memq month (list 2 4 6 10 13))
2687 (and (= month 12) (not (hebrew-calendar-leap-year-p year)))
2688 (and (= month 8) (not (hebrew-calendar-long-heshvan-p year)))
2689 (and (= month 9) (hebrew-calendar-short-kislev-p year)))
2690 29
2691 30))
2692
2693 (defun hebrew-calendar-elapsed-days (year)
2694 "Days from Sun. prior to start of Hebrew calendar to mean conjunction of Tishri of Hebrew YEAR."
2695 (let* ((months-elapsed
2696 (+ (* 235 (/ (1- year) 19));; Months in complete cycles so far.
2697 (* 12 (% (1- year) 19)) ;; Regular months in this cycle
2698 (/ (1+ (* 7 (% (1- year) 19))) 19)));; Leap months this cycle
2699 (parts-elapsed (+ 204 (* 793 (% months-elapsed 1080))))
2700 (hours-elapsed (+ 5
2701 (* 12 months-elapsed)
2702 (* 793 (/ months-elapsed 1080))
2703 (/ parts-elapsed 1080)))
2704 (parts ;; Conjunction parts
2705 (+ (* 1080 (% hours-elapsed 24)) (% parts-elapsed 1080)))
2706 (day ;; Conjunction day
2707 (+ 1 (* 29 months-elapsed) (/ hours-elapsed 24)))
2708 (alternative-day
2709 (if (or (>= parts 19440) ;; If the new moon is at or after midday,
2710 (and (= (% day 7) 2);; ...or is on a Tuesday...
2711 (>= parts 9924) ;; at 9 hours, 204 parts or later...
2712 (not (hebrew-calendar-leap-year-p year)));; of a
2713 ;; common year,
2714 (and (= (% day 7) 1);; ...or is on a Monday...
2715 (>= parts 16789) ;; at 15 hours, 589 parts or later...
2716 (hebrew-calendar-leap-year-p (1- year))));; at the end
2717 ;; of a leap year
2718 ;; Then postpone Rosh HaShanah one day
2719 (1+ day)
2720 ;; Else
2721 day)))
2722 (if ;; If Rosh HaShanah would occur on Sunday, Wednesday, or Friday
2723 (memq (% alternative-day 7) (list 0 3 5))
2724 ;; Then postpone it one (more) day and return
2725 (1+ alternative-day)
2726 ;; Else return
2727 alternative-day)))
2728
2729 (defun hebrew-calendar-days-in-year (year)
2730 "Number of days in Hebrew YEAR."
2731 (- (hebrew-calendar-elapsed-days (1+ year))
2732 (hebrew-calendar-elapsed-days year)))
2733
2734 (defun hebrew-calendar-long-heshvan-p (year)
2735 "t if Heshvan is long in Hebrew YEAR."
2736 (= (% (hebrew-calendar-days-in-year year) 10) 5))
2737
2738 (defun hebrew-calendar-short-kislev-p (year)
2739 "t if Kislev is short in Hebrew YEAR."
2740 (= (% (hebrew-calendar-days-in-year year) 10) 3))
2741
2742 (defun calendar-absolute-from-hebrew (date)
2743 "Absolute date of Hebrew DATE.
2744 The absolute date is the number of days elapsed since the (imaginary)
2745 Gregorian date Sunday, December 31, 1 BC."
2746 (let* ((month (extract-calendar-month date))
2747 (day (extract-calendar-day date))
2748 (year (extract-calendar-year date)))
2749 (+ day ;; Days so far this month.
2750 (if (< month 7);; before Tishri
2751 ;; Then add days in prior months this year before and after Nisan
2752 (+ (calendar-sum
2753 m 7 (<= m (hebrew-calendar-last-month-of-year year))
2754 (hebrew-calendar-last-day-of-month m year))
2755 (calendar-sum
2756 m 1 (< m month)
2757 (hebrew-calendar-last-day-of-month m year)))
2758 ;; Else add days in prior months this year
2759 (calendar-sum
2760 m 7 (< m month)
2761 (hebrew-calendar-last-day-of-month m year)))
2762 (hebrew-calendar-elapsed-days year);; Days in prior years.
2763 -1373429))) ;; Days elapsed before absolute date 1.
2764
2765 (defvar calendar-hebrew-month-name-array-common-year
2766 ["Nisan" "Iyar" "Sivan" "Tammuz" "Av" "Elul" "Tishri"
2767 "Heshvan" "Kislev" "Teveth" "Shevat" "Adar"])
2768
2769 (defvar calendar-hebrew-month-name-array-leap-year
2770 ["Nisan" "Iyar" "Sivan" "Tammuz" "Av" "Elul" "Tishri"
2771 "Heshvan" "Kislev" "Teveth" "Shevat" "Adar I" "Adar II"])
2772
2773 (defun calendar-hebrew-date-string (&optional date)
2774 "String of Hebrew date before sunset of Gregorian DATE.
2775 Defaults to today's date if DATE is not given.
2776 Driven by the variable `calendar-date-display-form'."
2777 (let* ((hebrew-date (calendar-hebrew-from-absolute
2778 (calendar-absolute-from-gregorian
2779 (or date (calendar-current-date)))))
2780 (calendar-month-name-array
2781 (if (hebrew-calendar-leap-year-p (extract-calendar-year hebrew-date))
2782 calendar-hebrew-month-name-array-leap-year
2783 calendar-hebrew-month-name-array-common-year)))
2784 (calendar-date-string hebrew-date nil t)))
2785
2786 (defun calendar-print-hebrew-date ()
2787 "Show the Hebrew calendar equivalent of the date under the cursor."
2788 (interactive)
2789 (message "Hebrew date (until sunset): %s"
2790 (calendar-hebrew-date-string (calendar-cursor-to-date t))))
2791
2792 (defun hebrew-calendar-yahrzeit (death-date year)
2793 "Absolute date of the anniversary of Hebrew DEATH-DATE in Hebrew YEAR."
2794 (let* ((death-day (extract-calendar-day death-date))
2795 (death-month (extract-calendar-month death-date))
2796 (death-year (extract-calendar-year death-date)))
2797 (cond
2798 ;; If it's Heshvan 30 it depends on the first anniversary; if
2799 ;; that was not Heshvan 30, use the day before Kislev 1.
2800 ((and (= death-month 8)
2801 (= death-day 30)
2802 (not (hebrew-calendar-long-heshvan-p (1+ death-year))))
2803 (1- (calendar-absolute-from-hebrew (list 9 1 year))))
2804 ;; If it's Kislev 30 it depends on the first anniversary; if
2805 ;; that was not Kislev 30, use the day before Teveth 1.
2806 ((and (= death-month 9)
2807 (= death-day 30)
2808 (hebrew-calendar-short-kislev-p (1+ death-year)))
2809 (1- (calendar-absolute-from-hebrew (list 10 1 year))))
2810 ;; If it's Adar II, use the same day in last month of
2811 ;; year (Adar or Adar II).
2812 ((= death-month 13)
2813 (calendar-absolute-from-hebrew
2814 (list (hebrew-calendar-last-month-of-year year) death-day year)))
2815 ;; If it's the 30th in Adar I and year is not a leap year
2816 ;; (so Adar has only 29 days), use the last day in Shevat.
2817 ((and (= death-day 30)
2818 (= death-month 12)
2819 (not (hebrew-calendar-leap-year-p year)))
2820 (calendar-absolute-from-hebrew (list 11 30 year)))
2821 ;; In all other cases, use the normal anniversary of the date of death.
2822 (t (calendar-absolute-from-hebrew
2823 (list death-month death-day year))))))
2824
2825 (defun calendar-set-mode-line (str)
2826 "Set mode line to STR, centered, surrounded by dashes."
2827 (setq mode-line-format
2828 (calendar-string-spread (list "" str "") ?- (frame-width))))
2829
2830 ;;;###autoload
2831 (defun list-yahrzeit-dates (death-date start-year end-year)
2832 "List Yahrzeit dates for *Gregorian* DEATH-DATE from START-YEAR to END-YEAR.
2833 When called interactively from the calendar window, the date of death is taken
2834 from the cursor position."
2835 (interactive
2836 (let* ((death-date
2837 (if (equal (current-buffer) (get-buffer calendar-buffer))
2838 (calendar-cursor-to-date)
2839 (let* ((today (calendar-current-date))
2840 (year (calendar-read
2841 "Year of death (>0): "
2842 '(lambda (x) (> x 0))
2843 (int-to-string (extract-calendar-year today))))
2844 (month-array calendar-month-name-array)
2845 (completion-ignore-case t)
2846 (month (cdr (assoc
2847 (capitalize
2848 (completing-read
2849 "Month of death (name): "
2850 (mapcar 'list (append month-array nil))
2851 nil t))
2852 (calendar-make-alist
2853 month-array 1 'capitalize))))
2854 (last (calendar-last-day-of-month month year))
2855 (day (calendar-read
2856 (format "Day of death (1-%d): " last)
2857 '(lambda (x) (and (< 0 x) (<= x last))))))
2858 (list month day year))))
2859 (death-year (extract-calendar-year death-date))
2860 (start-year (calendar-read
2861 (format "Starting year of Yahrzeit table (>%d): "
2862 death-year)
2863 '(lambda (x) (> x death-year))
2864 (int-to-string (1+ death-year))))
2865 (end-year (calendar-read
2866 (format "Ending year of Yahrzeit table (>=%d): "
2867 start-year)
2868 '(lambda (x) (>= x start-year)))))
2869 (list death-date start-year end-year)))
2870 (message "Computing yahrzeits...")
2871 (let* ((yahrzeit-buffer "*Yahrzeits*")
2872 (h-date (calendar-hebrew-from-absolute
2873 (calendar-absolute-from-gregorian death-date)))
2874 (h-month (extract-calendar-month h-date))
2875 (h-day (extract-calendar-day h-date))
2876 (h-year (extract-calendar-year h-date)))
2877 (set-buffer (get-buffer-create yahrzeit-buffer))
2878 (setq buffer-read-only nil)
2879 (calendar-set-mode-line
2880 (format "Yahrzeit dates for %s = %s"
2881 (calendar-date-string death-date)
2882 (let ((calendar-month-name-array
2883 (if (hebrew-calendar-leap-year-p h-year)
2884 calendar-hebrew-month-name-array-leap-year
2885 calendar-hebrew-month-name-array-common-year)))
2886 (calendar-date-string h-date nil t))))
2887 (erase-buffer)
2888 (goto-char (point-min))
2889 (calendar-for-loop i from start-year to end-year do
2890 (insert
2891 (calendar-date-string
2892 (calendar-gregorian-from-absolute
2893 (hebrew-calendar-yahrzeit
2894 h-date
2895 (extract-calendar-year
2896 (calendar-hebrew-from-absolute
2897 (calendar-absolute-from-gregorian (list 1 1 i))))))) "\n"))
2898 (goto-char (point-min))
2899 (set-buffer-modified-p nil)
2900 (setq buffer-read-only t)
2901 (display-buffer yahrzeit-buffer)
2902 (message "Computing yahrzeits...done")))
2903
2904 (defun calendar-astro-date-string (&optional date)
2905 "String of astronomical (Julian) day number of afternoon of Gregorian DATE.
2906 Defaults to today's date if DATE is not given."
2907 (int-to-string
2908 (+ 1721425 (calendar-absolute-from-gregorian
2909 (or date (calendar-current-date))))))
2910
2911 (defun calendar-print-astro-day-number ()
2912 "Show astronomical (Julian) day number of afternoon on date shown by cursor."
2913 (interactive)
2914 (message
2915 "Astronomical (Julian) day number after noon UTC: %s"
2916 (calendar-astro-date-string (calendar-cursor-to-date t))))
2917
2918 (defun calendar-goto-astro-day-number (daynumber &optional noecho)
2919 "Move cursor to astronomical (Julian) DAYNUMBER.
2920 Echo astronomical (Julian) day number unless NOECHO is t."
2921 (interactive (list (calendar-read
2922 "Astronomical (Julian) day number (>1721425): "
2923 '(lambda (x) (> x 1721425)))))
2924 (calendar-goto-date (calendar-gregorian-from-absolute (- daynumber 1721425)))
2925 (or noecho (calendar-print-astro-day-number)))
2926
2927 (run-hooks 'calendar-load-hook)
2928
2929 (provide 'calendar)
2930
2931 ;;; calendar.el ends here