*** empty log message ***
[emacs.git] / lisp / calendar / calendar.el
blob6eca77808c2dcd4dec9f3d8483934465341266a0
1 ;;; calendar.el --- Calendar functions.
2 ;;; Copyright (C) 1988, 1989, 1990, 1991 Free Software Foundation, Inc.
4 ;; This file is part of GNU Emacs.
6 ;; GNU Emacs is distributed in the hope that it will be useful,
7 ;; but WITHOUT ANY WARRANTY. No author or distributor
8 ;; accepts responsibility to anyone for the consequences of using it
9 ;; or for whether it serves any particular purpose or works at all,
10 ;; unless he says so in writing. Refer to the GNU Emacs General Public
11 ;; License for full details.
13 ;; Everyone is granted permission to copy, modify and redistribute
14 ;; GNU Emacs, but only under the conditions described in the
15 ;; GNU Emacs General Public License. A copy of this license is
16 ;; supposed to have been given to you along with GNU Emacs so you
17 ;; can know your rights and responsibilities. It should be in a
18 ;; file named COPYING. Among other things, the copyright notice
19 ;; and this notice must be preserved on all copies.
21 ;; This collection of functions implements a calendar window. It generates
22 ;; generates a calendar for the current month, together with the previous and
23 ;; coming months, or for any other three-month period. The calendar can be
24 ;; scrolled forward and backward in the window to show months in the past or
25 ;; future; the cursor can move forward and backward by days, weeks, or months,
26 ;; making it possible, for instance, to jump to the date a specified number of
27 ;; days, weeks, or months from the date under the cursor. The user can
28 ;; display a list of holidays and other notable days for the period shown; the
29 ;; notable days can be marked on the calendar, if desired. The user can also
30 ;; specify that dates having corresponding diary entries (in a file that the
31 ;; user specifies) be marked; the diary entries for any date can be viewed in
32 ;; a separate window. The diary and the notable days can be viewed
33 ;; independently of the calendar. Dates can be translated from the (usual)
34 ;; Gregorian calendar to the day of the year/days remaining in year, to the
35 ;; ISO commercial calendar, to the Julian (old style) calendar, to the Hebrew
36 ;; calendar, to the Islamic calendar, and to the French Revolutionary calendar.
38 ;; The diary related functions are in diary.el; the holiday related functions
39 ;; are in holiday.el
41 ;; Comments, corrections, and improvements should be sent to
42 ;; Edward M. Reingold Department of Computer Science
43 ;; (217) 333-6733 University of Illinois at Urbana-Champaign
44 ;; reingold@cs.uiuc.edu 1304 West Springfield Avenue
45 ;; Urbana, Illinois 61801
47 ;; GNU Emacs users too numerous to list pointed out a variety of problems
48 ;; with earlier forms of the `infinite' sliding calendar and suggested some
49 ;; of the features included in this package. Especially significant in this
50 ;; regard was the suggestion of mark-diary-entries and view-diary-entries,
51 ;; together ideas for their implementation, by
52 ;; Michael S. Littman Cognitive Science Research Group
53 ;; (201) 829-5155 Bell Communications Research
54 ;; mlittman@wind.bellcore.com 445 South St. Box 1961 (2L-331)
55 ;; Morristown, NJ 07960
57 ;; The algorithms for the Hebrew calendar are those of the Rambam (Rabbi Moses
58 ;; Maimonides), from his Mishneh Torah, as implemented by
59 ;; Nachum Dershowitz Department of Computer Science
60 ;; (217) 333-4219 University of Illinois at Urbana-Champaign
61 ;; nachum@cs.uiuc.edu 1304 West Springfield Avenue
62 ;; Urbana, Illinois 61801
64 ;; Technical details of all the calendrical calculations can be found in
65 ;; ``Calendrical Calculations'' by Nachum Dershowitz and Edward M. Reingold,
66 ;; Software--Practice and Experience, Volume 20, Number 9 (September, 1990),
67 ;; pages 899-928.
69 (defconst calendar-version "Version 4.02, released June 14, 1992")
71 (defvar view-diary-entries-initially nil
72 "*If T, the diary entries for the current date will be displayed on entry.
73 The diary is displayed in another window when the calendar is first displayed,
74 if the current date is visible. The number of days of diary entries displayed
75 is governed by the variable `number-of-diary-entries'.")
77 (defvar number-of-diary-entries 1
78 "*Specifies how many days of diary entries are to be displayed initially.
79 This variable affects the diary display when the command M-x diary is used,
80 or if the value of the variable `view-diary-entries-initially' is t. For
81 example, if the default value 1 is used, then only the current day's diary
82 entries will be displayed. If the value 2 is used, then both the current
83 day's and the next day's entries will be displayed. The value can also be
84 a vector such as [0 2 2 2 2 4 1]; this value will cause no diary entries to
85 be displayed on Sunday, the current date's and the next day's diary entries
86 to be displayed Monday through Thursday, Friday through Monday's entries to
87 be displayed on Friday, and only Saturday's entries to be displayed on
88 Saturday. This variable does not affect the diary display with the `d'
89 command from the calendar; in that case, the prefix argument controls the
90 number of days of diary entries displayed.")
92 (defvar mark-diary-entries-in-calendar nil
93 "*If t, dates with diary entries will be marked in the calendar window.
94 The marking symbol is specified by the variable `diary-entry-marker'.")
96 (defvar diary-entry-marker "+"
97 "*The symbol used to mark dates that have diary entries.")
99 (defvar view-calendar-holidays-initially nil
100 "*If t, the holidays for the current three month period will be displayed
101 on entry. The holidays are displayed in another window when the calendar is
102 first displayed.")
104 ;;;###autoload
105 (defvar mark-holidays-in-calendar nil
106 "*If t, dates of holidays will be marked in the calendar window.
107 The marking symbol is specified by the variable `calendar-holiday-marker'.")
109 (defvar calendar-holiday-marker "*"
110 "*The symbol used to mark notable dates in the calendar.")
112 ;;;###autoload
113 (defvar all-hebrew-calendar-holidays nil
114 "*If nil, the holidays from the Hebrew calendar that are shown will
115 include only those days of such major interest as to appear on secular
116 calendars. If t, the holidays shown in the calendar will include all
117 special days that would be shown on a complete Hebrew calendar.")
119 ;;;###autoload
120 (defvar all-christian-calendar-holidays nil
121 "*If nil, the holidays from the Christian calendar that are shown will
122 include only those days of such major interest as to appear on secular
123 calendars. If t, the holidays shown in the calendar will include all
124 special days that would be shown on a complete Christian calendar.")
126 ;;;###autoload
127 (defvar all-islamic-calendar-holidays nil
128 "*If nil, the holidays from the Islamic calendar that are shown will
129 include only those days of such major interest as to appear on secular
130 calendars. If t, the holidays shown in the calendar will include all
131 special days that would be shown on a complete Islamic calendar.")
133 (defvar initial-calendar-window-hook nil
134 "*List of functions to be called when the calendar window is first opened.
135 The functions invoked are called after the calendar window is opened, but
136 once opened is never called again. Leaving the calendar with the `q' command
137 and reentering it will cause these functions to be called again.")
139 (defvar today-visible-calendar-hook nil
140 "*List of functions called whenever the current date is visible.
141 This can be used, for example, to replace today's date with asterisks; a
142 function `calendar-star-date' is included for this purpose:
143 (setq today-visible-calendar-hook 'calendar-star-date)
144 It could also be used to mark the current date with `='; a function is also
145 provided for this:
146 (setq today-visible-calendar-hook 'calendar-mark-today)
148 The corresponding variable `today-invisible-calendar-hook' is the list of
149 functions called when the calendar function was called when the current
150 date is not visible in the window.
152 Other than the use of the provided functions, the changing of any
153 characters in the calendar buffer by the hooks may cause the failure of the
154 functions that move by days and weeks.")
156 (defvar today-invisible-calendar-hook nil
157 "*List of functions called whenever the current date is not visible.
159 The corresponding variable `today-visible-calendar-hook' is the list of
160 functions called when the calendar function was called when the current
161 date is visible in the window.
163 Other than the use of the provided functions, the changing of any
164 characters in the calendar buffer by the hooks may cause the failure of the
165 functions that move by days and weeks.")
167 (defvar diary-file "~/diary"
168 "*Name of the file in which one's personal diary of dates is kept.
170 The file's entries are lines in any of the forms
172 MONTH/DAY
173 MONTH/DAY/YEAR
174 MONTHNAME DAY
175 MONTHNAME DAY, YEAR
176 DAYNAME
178 at the beginning of the line; the remainder of the line is the diary entry
179 string for that date. MONTH and DAY are one or two digit numbers, YEAR is
180 a number and may be written in full or abbreviated to the final two digits.
181 If the date does not contain a year, it is generic and applies to any year.
182 DAYNAME entries apply to any date on which is on that day of the week.
183 MONTHNAME and DAYNAME can be spelled in full, abbreviated to three
184 characters (with or without a period), capitalized or not. Any of DAY,
185 MONTH, or MONTHNAME, YEAR can be `*' which matches any day, month, or year,
186 respectively.
188 The European style (in which the day precedes the month) can be used
189 instead, if you execute `european-calendar' when in the calendar, or set
190 `european-calendar-style' to t in your .emacs file. The European forms are
192 DAY/MONTH
193 DAY/MONTH/YEAR
194 DAY MONTHNAME
195 DAY MONTHNAME YEAR
196 DAYNAME
198 To revert to the default American style from the European style, execute
199 `american-calendar' in the calendar.
201 A diary entry can be preceded by a diary-nonmarking-symbol (ordinarily `&')
202 to make that entry nonmarking--that is, it will not be marked on dates in
203 the calendar window but will appear in a diary window.
205 Multiline diary entries are made by indenting lines after the first with
206 either a TAB or one or more spaces.
208 Lines not in one the above formats are ignored. Here are some sample diary
209 entries (in the default American style):
211 12/22/1988 Twentieth wedding anniversary!!
212 &1/1. Happy New Year!
213 10/22 Ruth's birthday.
214 21: Payday
215 Tuesday--weekly meeting with grad students at 10am
216 Supowit, Shen, Bitner, and Kapoor to attend.
217 1/13/89 Friday the thirteenth!!
218 &thu 4pm squash game with Lloyd.
219 mar 16 Dad's birthday
220 April 15, 1989 Income tax due.
221 &* 15 time cards due.
223 If the first line of a diary entry consists only of the date or day name with
224 no trailing blanks or punctuation, then that line will not be displayed in the
225 diary window; only the continuation lines will be shown. For example, the
226 single diary entry
228 02/11/1989
229 Bill Blattner visits Princeton today
230 2pm Cognitive Studies Committee meeting
231 2:30-5:30 Lizzie at Lawrenceville for `Group Initiative'
232 4:00pm Jamie Tappenden
233 7:30pm Dinner at George and Ed's for Alan Ryan
234 7:30-10:00pm dance at Stewart Country Day School
236 will appear in the diary window without the date line at the beginning. This
237 facility allows the diary window to look neater, but can cause confusion if
238 used with more than one day's entries displayed.
240 Diary entries can be based on Lisp sexps. For example, the diary entry
242 %%(diary-block 11 1 1990 11 10 1990) Vacation
244 causes the diary entry \"Vacation\" to appear from November 1 through November
245 10, 1990. Other functions available are `diary-float', `diary-anniversary',
246 `diary-cyclic', `day-of-year', `iso-date', `commercial-date', `french-date',
247 `hebrew-date', `islamic-date', `parasha', `omer', and `rosh-hodesh'. See the
248 documentation for the function `list-sexp-diary-entries' for more details.
250 Diary entries based on the Hebrew and/or the Islamic calendar are also
251 possible, but because these are somewhat slow, they are ignored
252 unless you set the `nongregorian-diary-listing-hook' and the
253 `nongregorian-diary-marking-hook' appropriately. See the documentation
254 for these functions for details.
256 Diary files can contain directives to include the contents of other files; for
257 details, see the documentation for the variable `list-diary-entries-hook'.")
259 (defvar diary-nonmarking-symbol "&"
260 "*The symbol used to indicate that a diary entry is not to be marked in the
261 calendar window.")
263 (defvar hebrew-diary-entry-symbol "H"
264 "*The symbol used to indicate that a diary entry is according to the
265 Hebrew calendar.")
267 (defvar islamic-diary-entry-symbol "I"
268 "*The symbol used to indicate that a diary entry is according to the
269 Islamic calendar.")
271 (defvar diary-include-string "#include"
272 "*The string used to indicate the inclusion of another file of diary entries
273 in diary-file. See the documentation for the function
274 `include-other-diary-files'.")
276 (defvar sexp-diary-entry-symbol "%%"
277 "*The string used to indicate a sexp diary entry in diary-file.
278 See the documentation for the function `list-sexp-diary-entries'.")
280 (defvar abbreviated-calendar-year t
281 "*Interpret a two-digit year DD in a diary entry as being either 19DD or
282 20DD, as appropriate, for the Gregorian calendar; similarly for the Hebrew and
283 Islamic calendars. If this variable is nil, years must be written in full.")
285 (defvar european-calendar-style nil
286 "*Use the European style of dates in the diary and in any displays. If this
287 variable is t, a date 1/2/1990 would be interpreted as February 1, 1990.
288 The accepted European date styles are
290 DAY/MONTH
291 DAY/MONTH/YEAR
292 DAY MONTHNAME
293 DAY MONTHNAME YEAR
294 DAYNAME
296 Names can be capitalized or not, written in full, or abbreviated to three
297 characters with or without a period.")
299 (defvar american-date-diary-pattern
300 '((month "/" day "[^/0-9]")
301 (month "/" day "/" year "[^0-9]")
302 (monthname " *" day "[^,0-9]")
303 (monthname " *" day ", *" year "[^0-9]")
304 (dayname "\\W"))
305 "*List of pseudo-patterns describing the American patterns of date used.
306 See the documentation of diary-date-forms for an explanantion.")
308 (defvar european-date-diary-pattern
309 '((day "/" month "[^/0-9]")
310 (day "/" month "/" year "[^0-9]")
311 (backup day " *" monthname "\\W+\\<[^*0-9]")
312 (day " *" monthname " *" year "[^0-9]")
313 (dayname "\\W"))
314 "*List of pseudo-patterns describing the European patterns of date used.
315 See the documentation of diary-date-forms for an explanantion.")
317 (defvar diary-date-forms
318 (if european-calendar-style
319 european-date-diary-pattern
320 american-date-diary-pattern)
321 "*List of pseudo-patterns describing the forms of date used in the diary.
322 The patterns on the list must be MUTUALLY EXCLUSIVE and must should not match
323 any portion of the diary entry itself, just the date component.
325 A pseudo-pattern is a list of regular expressions and the keywords `month',
326 `day', `year', `monthname', and `dayname'. The keyword `monthname' will
327 match the name of the month, capitalized or not, or its three-letter
328 abbreviation, followed by a period or not; it will also match `*'.
329 Similarly, `dayname' will match the name of the day, capitalized or not, or
330 its three-letter abbreviation, followed by a period or not. The keywords
331 `month', `day', and `year' will match those numerical values, preceded by
332 arbitrarily many zeros; they will also match `*'.
334 The matching of the diary entries with the date forms is done with the
335 standard syntax table from Fundamental mode, but with the `*' changed so
336 that it is a word constituent.
338 If, to be mutually exclusive, a pseudo-pattern must match a portion of the
339 diary entry itself, the first element of the pattern MUST be `backup'. This
340 directive causes the the date recognizer to back up to the beginning of the
341 current word of the diary entry, so in no case can the pattern match more
342 than a portion of the first word of the diary entry.")
344 (defvar european-calendar-display-form
345 '(dayname ", " day " " monthname " " year)
346 "*The pseudo-pattern that governs the way a Gregorian date is formatted
347 in the European style. See the documentation of calendar-date-display-forms
348 for an explanantion.")
350 (defvar american-calendar-display-form
351 '(dayname ", " monthname " " day ", " year)
352 "*The pseudo-pattern that governs the way a Gregorian date is formatted
353 in the American style. See the documentation of calendar-date-display-forms
354 for an explanantion.")
356 (defvar calendar-date-display-form
357 (if european-calendar-style
358 european-calendar-display-form
359 american-calendar-display-form)
360 "*The pseudo-pattern that governs the way a Gregorian date is formatted
361 as a string by the function `calendar-date-string'. A pseudo-pattern is a
362 list of expressions that can involve the keywords `month', `day', and
363 `year', all numbers in string form, and `monthname' and `dayname', both
364 alphabetic strings. For example, the ISO standard would use the pseudo-
365 pattern
367 '(year \"-\" month \"-\" day)
369 while a typical American form would be
371 '(month \"/\" day \"/\" (substring year -2))
375 '((format \"%9s, %9s %2s, %4s\" dayname monthname day year))
377 would give the usual American style in fixed-length fields.
379 See the documentation of the function `calendar-date-string'.")
381 (defun european-calendar ()
382 "Set the interpretation and display of dates to the European style."
383 (interactive)
384 (setq european-calendar-style t)
385 (setq calendar-date-display-form european-calendar-display-form)
386 (setq diary-date-forms european-date-diary-pattern)
387 (update-calendar-mode-line))
389 (defun american-calendar ()
390 "Set the interpretation and display of dates to the American style."
391 (interactive)
392 (setq european-calendar-style nil)
393 (setq calendar-date-display-form american-calendar-display-form)
394 (setq diary-date-forms american-date-diary-pattern)
395 (update-calendar-mode-line))
397 (defvar print-diary-entries-hook
398 '(add-diary-heading lpr-buffer (lambda nil (kill-buffer temp-buffer)))
399 "*List of functions to be called after a temporary buffer is prepared
400 with the diary entries currently visible in the diary buffer. The default
401 value adds a heading (formed from the information in the mode line of the
402 diary buffer), does the printing, and kills the buffer. Other uses might
403 include, for example, rearranging the lines into order by day and time,
404 saving the buffer instead of deleting it, or changing the function used to
405 do the printing.")
407 ;;;###autoload
408 (defvar list-diary-entries-hook nil
409 "*List of functions to be called after the diary file is culled for
410 relevant entries. It is to be used for diary entries that are not found in
411 the diary file.
413 A function `include-other-diary-files' is provided for use as the value of
414 this hook. This function enables you to use shared diary files together
415 with your own. The files included are specified in the diary-file by lines
416 of the form
418 #include \"filename\"
420 This is recursive; that is, #include directives in files thus included are
421 obeyed. You can change the \"#include\" to some other string by changing
422 the variable `diary-include-string'. When you use `include-other-diary-files'
423 as part of the list-diary-entries-hook, you will probably also want to use the
424 function `mark-included-diary-files' as part of the mark-diary-entries-hook.
426 For example, you could use
428 (setq list-diary-entries-hook
429 '(include-other-diary-files
430 (lambda nil
431 (setq diary-entries-list
432 (sort diary-entries-list 'diary-entry-compare)))))
433 (setq diary-display-hook 'fancy-diary-display)
435 in your .emacs file to cause the fancy diary buffer to be displayed with
436 diary entries from various included files, each day's entries sorted into
437 lexicographic order.")
439 ;;;###autoload
440 (defvar diary-display-hook 'simple-diary-display
441 "*List of functions that handle the display of the diary.
443 Ordinarily, this just displays the diary buffer (with holidays indicated in
444 the mode line), if there are any relevant entries. At the time these
445 functions are called, the variable `diary-entries-list' is a list, in order
446 by date, of all relevant diary entries in the form of ((MONTH DAY YEAR)
447 STRING), where string is the diary entry for the given date. This can be
448 used, for example, to handle appointment notification, prepare a different
449 buffer for display (perhaps combined with holidays), or produce hard copy
450 output.
452 A function `fancy-diary-display' is provided as an alternative
453 choice for this hook; this function prepares a special noneditable diary
454 buffer with the relevant diary entries that has neat day-by-day arrangement
455 with headings. The fancy diary buffer will show the holidays unless the
456 variable `holidays-in-diary-buffer' is set to nil. Ordinarily, the fancy
457 diary buffer will not show days for which there are no diary entries, even
458 if that day is a holiday; if you want such days to be shown in the fancy
459 diary buffer, set the variable `diary-list-include-blanks' to t.")
461 ;;;###autoload
462 (defvar nongregorian-diary-listing-hook nil
463 "*List of functions to be called for the diary file and included files as
464 they are processed for listing diary entries. You can use any or all of
465 `list-hebrew-diary-entries', `yahrzeit-diary-entry', and
466 `list-islamic-diary-entries'. The documentation for these functions
467 describes the style of such diary entries.")
469 (defvar mark-diary-entries-hook nil
470 "*List of functions called after marking diary entries in the calendar.
472 A function `mark-included-diary-files' is also provided for use as the
473 mark-diary-entries-hook; it enables you to use shared diary files together
474 with your own. The files included are specified in the diary-file by lines
475 of the form
476 #include \"filename\"
477 This is recursive; that is, #include directives in files thus included are
478 obeyed. You can change the \"#include\" to some other string by changing the
479 variable `diary-include-string'. When you use `mark-included-diary-files' as
480 part of the mark-diary-entries-hook, you will probably also want to use the
481 function `include-other-diary-files' as part of the list-diary-entries-hook.")
483 ;;;###autoload
484 (defvar nongregorian-diary-marking-hook nil
485 "*List of functions to be called as the diary file and included files are
486 processed for marking diary entries. You can use either or both of
487 mark-hebrew-diary-entries and mark-islamic-diary-entries. The documentation
488 for these functions describes the style of such diary entries.")
490 ;;;###autoload
491 (defvar diary-list-include-blanks nil
492 "*If nil, do not include days with no diary entry in the list of diary
493 entries. Such days will then not be shown in the the fancy diary buffer,
494 even if they are holidays.")
496 (defvar holidays-in-diary-buffer t
497 "*If t, the holidays will be indicated in the mode line of the diary buffer
498 (or in the fancy diary buffer next to the date). This slows down the diary
499 functions somewhat; setting it to nil will make the diary display faster.")
501 (defvar calendar-holidays
503 ;; General Holidays (American)
504 (fixed 1 1 "New Year's Day")
505 (float 1 1 3 "Martin Luther King Day")
506 (fixed 2 2 "Ground Hog Day")
507 (fixed 2 14 "Valentine's Day")
508 (float 2 1 3 "President's Day")
509 (fixed 3 17 "St. Patrick's Day")
510 (fixed 4 1 "April Fool's Day")
511 (float 4 0 1 "Daylight Savings Time Begins")
512 (float 5 0 2 "Mother's Day")
513 (float 5 1 -1 "Memorial Day")
514 (fixed 6 14 "Flag Day")
515 (float 6 0 3 "Father's Day")
516 (fixed 7 4 "Independence Day")
517 (float 9 1 1 "Labor Day")
518 (float 10 1 2 "Columbus Day")
519 (float 10 0 -1 "Daylight Savings Time Ends")
520 (fixed 10 31 "Halloween")
521 (fixed 11 11 "Veteran's Day")
522 (float 11 4 4 "Thanksgiving")
524 ;; Christian Holidays
525 (if all-christian-calendar-holidays
526 (fixed 1 6 "Epiphany"))
527 (easter-etc)
528 (if all-christian-calendar-holidays
529 (fixed 8 15 "Assumption"))
530 (if all-christian-calendar-holidays
531 (advent))
532 (fixed 12 25 "Christmas")
533 (if all-christian-calendar-holidays
534 (julian 12 25 "Eastern Orthodox Christmas"))
536 ;; Jewish Holidays
537 (rosh-hashanah-etc)
538 (if all-hebrew-calendar-holidays
539 (julian 11
540 (let* ((m displayed-month)
541 (y displayed-year)
542 (year))
543 (increment-calendar-month m y -1)
544 (let ((year (extract-calendar-year
545 (calendar-julian-from-absolute
546 (calendar-absolute-from-gregorian
547 (list m 1 y))))))
548 (if (zerop (% (1+ year) 4))
550 21))) "\"Tal Umatar\" (evening)"))
551 (if all-hebrew-calendar-holidays
552 (hanukkah)
553 (hebrew 9 25 "Hanukkah"))
554 (if all-hebrew-calendar-holidays
555 (hebrew 10
556 (let ((h-year (extract-calendar-year
557 (calendar-hebrew-from-absolute
558 (calendar-absolute-from-gregorian
559 (list displayed-month 28 displayed-year))))))
560 (if (= (% (calendar-absolute-from-hebrew (list 10 10 h-year))
563 11 10))
564 "Tzom Teveth"))
565 (if all-hebrew-calendar-holidays
566 (hebrew 11 15 "Tu B'Shevat"))
567 (if all-hebrew-calendar-holidays
568 (hebrew
570 (let ((m displayed-month)
571 (y displayed-year))
572 (increment-calendar-month m y 1)
573 (let* ((h-year (extract-calendar-year
574 (calendar-hebrew-from-absolute
575 (calendar-absolute-from-gregorian
576 (list m
577 (calendar-last-day-of-month m y)
578 y)))))
579 (s-s
580 (calendar-hebrew-from-absolute
581 (if (=
582 (% (calendar-absolute-from-hebrew
583 (list 7 1 h-year))
586 (calendar-dayname-on-or-before
587 6 (calendar-absolute-from-hebrew
588 (list 11 17 h-year)))
589 (calendar-dayname-on-or-before
590 6 (calendar-absolute-from-hebrew
591 (list 11 16 h-year))))))
592 (day (extract-calendar-day s-s)))
593 day))
594 "Shabbat Shirah"))
595 (passover-etc)
596 (if (and all-hebrew-calendar-holidays
597 (let* ((m displayed-month)
598 (y displayed-year)
599 (year))
600 (increment-calendar-month m y -1)
601 (let ((year (extract-calendar-year
602 (calendar-julian-from-absolute
603 (calendar-absolute-from-gregorian
604 (list m 1 y))))))
605 (= 21 (% year 28)))))
606 (julian 3 26 "Kiddush HaHamah"))
607 (if all-hebrew-calendar-holidays
608 (tisha-b-av-etc))
610 ;; Islamic Holidays
611 (islamic 1 1 (format "Islamic New Year %d"
612 (let ((m displayed-month)
613 (y displayed-year))
614 (increment-calendar-month m y 1)
615 (extract-calendar-year
616 (calendar-islamic-from-absolute
617 (calendar-absolute-from-gregorian
618 (list m (calendar-last-day-of-month m y) y)))))))
619 (if all-islamic-calendar-holidays
620 (islamic 1 10 "Ashura"))
621 (if all-islamic-calendar-holidays
622 (islamic 3 12 "Mulad-al-Nabi"))
623 (if all-islamic-calendar-holidays
624 (islamic 7 26 "Shab-e-Mi'raj"))
625 (if all-islamic-calendar-holidays
626 (islamic 8 15 "Shab-e-Bara't"))
627 (islamic 9 1 "Ramadan Begins")
628 (if all-islamic-calendar-holidays
629 (islamic 9 27 "Shab-e Qadr"))
630 (if all-islamic-calendar-holidays
631 (islamic 10 1 "Id-al-Fitr"))
632 (if all-islamic-calendar-holidays
633 (islamic 12 10 "Id-al-Adha")))
634 "List of notable days for the command M-x holidays.
635 Additional holidays are easy to add to the list. The possible holiday-forms
636 are as follows:
638 (fixed MONTH DAY STRING) a fixed date on the Gregorian calendar
639 (float MONTH DAYNAME K STRING) the Kth DAYNAME in MONTH on the Gregorian
640 calendar (0 for Sunday, etc.); K<0 means
641 count back from the end of the month
642 (hebrew MONTH DAY STRING) a fixed date on the Hebrew calendar
643 (islamic MONTH DAY STRING) a fixed date on the Islamic calendar
644 (julian MONTH DAY STRING) a fixed date on the Julian calendar
645 (if BOOLEAN HOLIDAY-FORM &optional HOLIDAY-FORM) gives a choice between
646 two holidays based on the value of BOOLEAN
647 (FUNCTION &optional ARGS) dates requiring special computation; ARGS,
648 if any, are passed in a list to the function
649 `calendar-holiday-function-FUNCTION'
651 For example, to add Bastille Day, celebrated in France on July 14, add
653 (fixed 7 14 \"Bastille Day\")
655 to the list. To add Hurricane Supplication Day, celebrated in the Virgin
656 Islands on the fourth Monday in August, add
658 (float 8 1 4 \"Hurricane Supplication Day\")
660 to the list (the last Monday would be specified with `-1' instead of `4').
661 To add the last day of Hanukah to the list, use
663 (hebrew 10 2 \"Last day of Hanukah\")
665 since the Hebrew months are numbered with 1 starting from Nisan, while to
666 add the Islamic feast celebrating Mohammed's birthday use
668 (islamic 3 12 \"Mohammed's Birthday\")
670 since the Islamic months are numbered from 1 starting with Muharram. To
671 add Thomas Jefferson's birthday, April 2, 1743 (Julian), use
673 (julian 4 2 \"Jefferson's Birthday\")
675 To include a holiday conditionally, use the if form. For example, to
676 include American presidential elections, which occur on the first Tuesday
677 after the first Monday in November of years divisble by 4, add
679 (if (zerop (% displayed-year 4))
680 (fixed 11
681 (extract-calendar-day
682 (calendar-gregorian-from-absolute
683 (1+ (calendar-dayname-on-or-before
684 1 (+ 6 (calendar-absolute-from-gregorian
685 (list 11 1 displayed-year)))))))
686 \"US Presidential Election\"))
688 to the list. To include the phases of the moon, add
690 (lunar-phases)
692 to the holiday list, where `calendar-holiday-function-lunar-phases' is an
693 Emacs-Lisp function that you've written to return a (possibly empty) list of
694 the relevant VISIBLE dates with descriptive strings such as
696 (((2 6 1989) \"New Moon\") ((2 12 1989) \"First Quarter Moon\") ... )
698 The fixed, float, hebrew, islamic, julian and if forms are implemented by
699 the inclusion of the functions `calendar-holiday-function-fixed',
700 `calendar-holiday-function-float', `calendar-holiday-function-hebrew',
701 `calendar-holiday-function-islamic', `calendar-holiday-function-julian',
702 and `calendar-holiday-function-if', respectively.")
704 (defconst calendar-buffer "*Calendar*"
705 "Name of the buffer used for the calendar.")
707 (defconst holiday-buffer "*Holidays*"
708 "Name of the buffer used for the displaying the holidays.")
710 (defconst fancy-diary-buffer "*Fancy Diary Entries*"
711 "Name of the buffer used for the optional fancy display of the diary.")
713 (defmacro increment-calendar-month (mon yr n)
714 "Move the variables MON and YR to the month and year N months forward
715 if N is positive or backward if N is negative."
716 (` (let (( macro-y (+ (* (, yr) 12) (, mon) -1 (, n) )))
717 (setq (, mon) (1+ (% macro-y 12) ))
718 (setq (, yr) (/ macro-y 12)))))
720 (defmacro calendar-for-loop (var from init to final do &rest body)
721 "Execute a for loop."
722 (` (let (( (, var) (1- (, init)) ))
723 (while (>= (, final) (setq (, var) (1+ (, var))))
724 (,@ body)))))
726 (defmacro calendar-sum (index initial condition expression)
727 "For INDEX = INITIAL and successive integers, as long as CONDITION holds,
728 sum EXPRESSION."
729 (` (let (( (, index) (, initial))
730 (sum 0))
731 (while (, condition)
732 (setq sum (+ sum (, expression) ))
733 (setq (, index) (1+ (, index))))
734 sum)))
736 ;; The following macros are for speed; the code would be clearer if they
737 ;; were functions, but they can be called thousands of times when
738 ;; looking up holidays or processing the diary. Here, for example, are the
739 ;; numbers of calls to calendar/diary/holiday functions in preparing the
740 ;; fancy diary display, for a moderately complex diary file, with functions
741 ;; used instead of macros. There were a total of 10000 such calls:
743 ;; 1934 extract-calendar-month
744 ;; 1852 extract-calendar-year
745 ;; 1819 extract-calendar-day
746 ;; 845 calendar-leap-year-p
747 ;; 837 calendar-day-number
748 ;; 775 calendar-absolute-from-gregorian
749 ;; 346 calendar-last-day-of-month
750 ;; 286 hebrew-calendar-last-day-of-month
751 ;; 188 hebrew-calendar-leap-year-p
752 ;; 180 hebrew-calendar-elapsed-days
753 ;; 163 hebrew-calendar-last-month-of-year
754 ;; 66 calendar-date-compare
755 ;; 65 hebrew-calendar-days-in-year
756 ;; 60 calendar-absolute-from-julian
757 ;; 50 calendar-absolute-from-hebrew
758 ;; 43 calendar-date-equal
759 ;; 38 calendar-gregorian-from-absolute
760 ;; .
761 ;; .
762 ;; .
764 ;; The use of these seven macros eliminates the overhead of 92% of the function
765 ;; calls; it's faster this way.
767 (defmacro extract-calendar-month (date)
768 "Extract the month part of DATE which has the form (month day year)."
769 (` (car (, date))))
771 (defmacro extract-calendar-day (date)
772 "Extract the day part of DATE which has the form (month day year)."
773 (` (car (cdr (, date)))))
775 (defmacro extract-calendar-year (date)
776 "Extract the year part of DATE which has the form (month day year)."
777 (` (car (cdr (cdr (, date))))))
779 (defmacro calendar-leap-year-p (year)
780 "Returns t if YEAR is a Gregorian leap year."
781 (` (or
782 (and (= (% (, year) 4) 0)
783 (/= (% (, year) 100) 0))
784 (= (% (, year) 400) 0))))
786 (defmacro calendar-last-day-of-month (month year)
787 "The last day in MONTH during YEAR."
788 (` (if (and
789 (, (macroexpand (` (calendar-leap-year-p (, year)))))
790 (= (, month) 2))
792 (aref [31 28 31 30 31 30 31 31 30 31 30 31] (1- (, month))))))
794 (defmacro calendar-day-number (date)
795 "Return the day number within the year of the date DATE.
796 For example, (calendar-day-number '(1 1 1987)) returns the value 1,
797 while (calendar-day-number '(12 31 1980)) returns 366."
799 ;; An explanation of the calculation can be found in PascAlgorithms by
800 ;; Edward and Ruth Reingold, Scott-Foresman/Little, Brown, 1988.
802 (` (let* ((month (, (macroexpand (` (extract-calendar-month (, date))))))
803 (day (, (macroexpand (` (extract-calendar-day (, date))))))
804 (year (, (macroexpand (` (extract-calendar-year (, date))))))
805 (day-of-year (+ day (* 31 (1- month)))))
806 (if (> month 2)
807 (progn
808 (setq day-of-year (- day-of-year (/ (+ 23 (* 4 month)) 10)))
809 (if (, (macroexpand (` (calendar-leap-year-p year))))
810 (setq day-of-year (1+ day-of-year)))))
811 day-of-year)))
813 (defmacro calendar-absolute-from-gregorian (date)
814 "The number of days elapsed between the Gregorian date 12/31/1 BC and DATE.
815 The Gregorian date Sunday, December 31, 1 BC is imaginary."
816 (` (let ((month (, (macroexpand (` (extract-calendar-month (, date))))))
817 (day (, (macroexpand (` (extract-calendar-day (, date))))))
818 (year (, (macroexpand (` (extract-calendar-year (, date)))))))
819 (+ (, (macroexpand (` (calendar-day-number (, date)))));; Days this year
820 (* 365 (1- year));; + Days in prior years
821 (/ (1- year) 4);; + Julian leap years
822 (- (/ (1- year) 100));; - century years
823 (/ (1- year) 400)))));; + Gregorian leap years
825 ;;;###autoload
826 (defun calendar (&optional arg)
827 "Display a three-month calendar in another window.
828 The three months appear side by side, with the current month in the middle
829 surrounded by the previous and next months. The cursor is put on today's date.
831 This function is suitable for execution in a .emacs file; appropriate setting
832 of the variable `view-diary-entries-initially' will cause the diary entries for
833 the current date to be displayed in another window. The value of the variable
834 `number-of-diary-entries' controls the number of days of diary entries
835 displayed upon initial display of the calendar.
837 An optional prefix argument ARG causes the calendar displayed to be ARG
838 months in the future if ARG is positive or in the past if ARG is negative;
839 in this case the cursor goes on the first day of the month.
841 Once in the calendar window, future or past months can be moved into view.
842 Arbitrary months can be displayed, or the calendar can be scrolled forward
843 or backward.
845 The cursor can be moved forward or backward by one day, one week, one month,
846 or one year. All of these commands take prefix arguments which, when negative,
847 cause movement in the opposite direction. For convenience, the digit keys
848 and the minus sign are automatically prefixes. The window is replotted as
849 necessary to display the desired date.
851 Diary entries can be marked on the calendar or displayed in another window.
853 Use M-x describe-mode for details of the key bindings in the calendar window.
855 The Gregorian calendar is assumed.
857 After preparing the calendar window initially, the hooks given by the variable
858 `initial-calendar-window-hook' are run.
860 The hooks given by the variable `today-visible-calendar-hook' are run
861 everytime the calendar window gets scrolled, if the current date is visible
862 in the window. If it is not visible, the hooks given by the variable
863 `today-invisible-calendar-hook' are run. Thus, for example, setting
864 `today-visible-calendar-hook' to 'calendar-star-date will cause today's date
865 to be replaced by asterisks to highlight it whenever it is in the window."
866 (interactive "P")
867 (setq arg (if arg (prefix-numeric-value arg) 0))
868 (set-buffer (get-buffer-create calendar-buffer))
869 (calendar-mode)
870 (setq calendar-window-configuration (current-window-configuration))
871 (let ((pop-up-windows t)
872 (split-height-threshold 1000))
873 (pop-to-buffer calendar-buffer)
874 (regenerate-calendar-window arg)
875 (let ((date (list current-month current-day current-year)))
876 (if (and view-diary-entries-initially (calendar-date-is-visible-p date))
877 (view-diary-entries
878 (if (vectorp number-of-diary-entries)
879 (aref number-of-diary-entries (calendar-day-of-week date))
880 number-of-diary-entries))))
881 (let* ((diary-buffer (get-file-buffer diary-file))
882 (diary-window (if diary-buffer (get-buffer-window diary-buffer)))
883 (split-height-threshold (if diary-window 2 1000)))
884 (if view-calendar-holidays-initially
885 (list-calendar-holidays))))
886 (run-hooks 'initial-calendar-window-hook))
888 (autoload 'view-diary-entries "diary"
889 "Prepare and display a buffer with diary entries.
890 Searches the file diary-file for entries that match ARG days starting with
891 the date indicated by the cursor position in the displayed three-month
892 calendar."
895 (autoload 'show-all-diary-entries "diary"
896 "Show all of the diary entries in the diary-file.
897 This function gets rid of the selective display of the diary-file so that
898 all entries, not just some, are visible. If there is no diary buffer, one
899 is created."
902 (autoload 'mark-diary-entries "diary"
903 "Mark days in the calendar window that have diary entries.
904 Each entry in diary-file visible in the calendar window is marked."
907 (autoload 'insert-diary-entry "diary"
908 "Insert a diary entry for the date indicated by point."
911 (autoload 'insert-weekly-diary-entry "diary"
912 "Insert a weekly diary entry for the day of the week indicated by point."
915 (autoload 'insert-monthly-diary-entry "diary"
916 "Insert a monthly diary entry for the day of the month indicated by point."
919 (autoload 'insert-yearly-diary-entry "diary"
920 "Insert an annual diary entry for the day of the year indicated by point."
923 (autoload 'insert-anniversary-diary-entry "diary"
924 "Insert an anniversary diary entry for the date indicated by point."
927 (autoload 'insert-block-diary-entry "diary"
928 "Insert a block diary entry for the dates indicated by point and mark."
931 (autoload 'insert-cyclic-diary-entry "diary"
932 "Insert a cyclic diary entry starting at the date indicated by point."
935 (autoload 'insert-hebrew-diary-entry "diary"
936 "Insert a diary entry for the Hebrew date corresponding to the date
937 indicated by point."
940 (autoload 'insert-monthly-hebrew-diary-entry "diary"
941 "Insert a monthly diary entry for the day of the Hebrew month corresponding
942 to the date indicated by point."
945 (autoload 'insert-yearly-hebrew-diary-entry "diary"
946 "Insert an annual diary entry for the day of the Hebrew year corresponding
947 to the date indicated by point."
950 (autoload 'insert-islamic-diary-entry "diary"
951 "Insert a diary entry for the Islamic date corresponding to the date
952 indicated by point."
955 (autoload 'insert-monthly-islamic-diary-entry "diary"
956 "Insert a monthly diary entry for the day of the Islamic month corresponding
957 to the date indicated by point."
960 (autoload 'insert-yearly-islamic-diary-entry "diary"
961 "Insert an annual diary entry for the day of the Islamic year corresponding
962 to the date indicated by point."
965 (autoload 'list-calendar-holidays "holidays"
966 "Create a buffer containing the holidays for the current calendar window.
967 The holidays are those in the list `calendar-notable-days'. Returns t if any
968 holidays are found, nil if not."
971 (autoload 'mark-calendar-holidays "holidays"
972 "Mark notable days in the calendar window."
975 (autoload 'calendar-cursor-holidays "holidays"
976 "Find holidays for the date specified by the cursor in the calendar window."
979 (defun regenerate-calendar-window (&optional arg)
980 "Generate the calendar window, offset from the current date by ARG months."
981 (if (not arg) (setq arg 0))
982 (let* ((buffer-read-only nil)
983 (today-visible (and (<= arg 1) (>= arg -1)))
984 (today (calendar-current-date))
985 (month (extract-calendar-month today))
986 (day (extract-calendar-day today))
987 (year (extract-calendar-year today))
988 (day-in-week (calendar-day-of-week today)))
989 (update-calendar-mode-line)
990 (setq current-month month)
991 (setq current-day day)
992 (setq current-year year)
993 (increment-calendar-month month year arg)
994 (generate-calendar month year)
995 (calendar-cursor-to-visible-date
996 (if today-visible today (list displayed-month 1 displayed-year)))
997 (set-buffer-modified-p nil)
998 (or (one-window-p t)
999 (/= (screen-width) (window-width))
1000 (shrink-window (- (window-height) 9)))
1001 (sit-for 0)
1002 (and mark-holidays-in-calendar
1003 (mark-calendar-holidays)
1004 (sit-for 0))
1005 (unwind-protect
1006 (if mark-diary-entries-in-calendar (mark-diary-entries))
1007 (if today-visible
1008 (run-hooks 'today-visible-calendar-hook)
1009 (run-hooks 'today-invisible-calendar-hook)))))
1011 (defun generate-calendar (month year)
1012 "Generate a three-month Gregorian calendar centered around MONTH, YEAR."
1013 (if (< (+ month (* 12 (1- year))) 2)
1014 (error "Months before February, 1 AD are not available."))
1015 (setq displayed-month month)
1016 (setq displayed-year year)
1017 (erase-buffer)
1018 (increment-calendar-month month year -1)
1019 (calendar-for-loop i from 0 to 2 do
1020 (generate-calendar-month month year (+ 5 (* 25 i)))
1021 (increment-calendar-month month year 1)))
1023 (defun generate-calendar-month (month year indent)
1024 "Produce a calendar for MONTH, YEAR on the Gregorian calendar.
1025 The calendar is inserted in the buffer starting at the line on which point
1026 is currently located, but indented INDENT spaces. The indentation is done
1027 from the first character on the line and does not disturb the first INDENT
1028 characters on the line."
1029 (let* ((first-day-of-month (calendar-day-of-week (list month 1 year)))
1030 (first-saturday (- 7 first-day-of-month))
1031 (last (calendar-last-day-of-month month year))
1032 (heading (format "%s %d" (calendar-month-name month) year)))
1033 (goto-char (point-min))
1034 (calendar-insert-indented
1035 heading (+ indent (/ (- 20 (length heading)) 2)) t)
1036 (calendar-insert-indented " S M Tu W Th F S" indent t)
1037 (calendar-insert-indented "" indent);; Move to appropriate spot on line
1038 ;; Add blank days before the first of the month
1039 (calendar-for-loop i from 1 to first-day-of-month do
1040 (insert " "))
1041 ;; Put in the days of the month
1042 (calendar-for-loop i from 1 to last do
1043 (insert (format "%2d " i))
1044 (and (= (% i 7) (% first-saturday 7))
1045 (/= i last)
1046 (calendar-insert-indented "" 0 t) ;; Force onto following line
1047 (calendar-insert-indented "" indent)))));; Go to proper spot
1049 (defun calendar-insert-indented (string indent &optional newline)
1050 "Insert STRING at column INDENT.
1051 If the optional parameter NEWLINE is t, leave point at start of next line,
1052 inserting a newline if there was no next line; otherwise, leave point after
1053 the inserted text. Value is always t."
1054 ;; Try to move to that column.
1055 (move-to-column indent)
1056 ;; If line is too short, indent out to that column.
1057 (if (< (current-column) indent)
1058 (indent-to indent))
1059 (insert string)
1060 ;; Advance to next line, if requested.
1061 (if newline
1062 (progn
1063 (end-of-line)
1064 (if (eobp)
1065 (newline)
1066 (forward-line 1))))
1069 (defun redraw-calendar ()
1070 "Redraw the calendar display."
1071 (interactive)
1072 (let ((cursor-date (calendar-cursor-to-date)))
1073 (regenerate-calendar-window
1074 (calendar-interval current-month current-year
1075 displayed-month displayed-year))
1076 (calendar-cursor-to-visible-date cursor-date)))
1078 (defvar calendar-mode-map nil)
1079 (if calendar-mode-map
1081 (setq calendar-mode-map (make-sparse-keymap))
1082 (calendar-for-loop i from 0 to 9 do
1083 (define-key calendar-mode-map (int-to-string i) 'digit-argument))
1084 (let ((l (list 'narrow-to-region 'mark-word 'mark-sexp 'mark-paragraph
1085 'mark-defun 'mark-whole-buffer 'mark-page 'kill-region
1086 'copy-region-as-kill 'downcase-region 'upcase-region
1087 'capitalize-region 'write-region)))
1088 (while (car l)
1089 (let ((k (where-is-internal (car l))))
1090 (while (car k)
1091 (define-key calendar-mode-map (car k) 'calendar-not-implemented)
1092 (setq k (cdr k)))
1093 (setq l (cdr l)))))
1094 (define-key calendar-mode-map "-" 'negative-argument)
1095 (define-key calendar-mode-map "\C-x>" 'scroll-calendar-right)
1096 (define-key calendar-mode-map "\ev" 'scroll-calendar-right-three-months)
1097 (define-key calendar-mode-map "\C-x<" 'scroll-calendar-left)
1098 (define-key calendar-mode-map "\C-v" 'scroll-calendar-left-three-months)
1099 (define-key calendar-mode-map "\C-b" 'calendar-backward-day)
1100 (define-key calendar-mode-map "\C-p" 'calendar-backward-week)
1101 (define-key calendar-mode-map "\e[" 'calendar-backward-month)
1102 (define-key calendar-mode-map "\C-x[" 'calendar-backward-year)
1103 (define-key calendar-mode-map "\C-f" 'calendar-forward-day)
1104 (define-key calendar-mode-map "\C-n" 'calendar-forward-week)
1105 (define-key calendar-mode-map "\e]" 'calendar-forward-month)
1106 (define-key calendar-mode-map "\C-x]" 'calendar-forward-year)
1107 (define-key calendar-mode-map "\C-a" 'calendar-beginning-of-week)
1108 (define-key calendar-mode-map "\C-e" 'calendar-end-of-week)
1109 (define-key calendar-mode-map "\ea" 'calendar-beginning-of-month)
1110 (define-key calendar-mode-map "\ee" 'calendar-end-of-month)
1111 (define-key calendar-mode-map "\e<" 'calendar-beginning-of-year)
1112 (define-key calendar-mode-map "\e>" 'calendar-end-of-year)
1113 (define-key calendar-mode-map "\C-@" 'calendar-set-mark)
1114 (define-key calendar-mode-map "\C-x\C-x" 'calendar-exchange-point-and-mark)
1115 (define-key calendar-mode-map "\e=" 'calendar-count-days-region)
1116 (define-key calendar-mode-map "gd" 'calendar-goto-date)
1117 (define-key calendar-mode-map "gJ" 'calendar-goto-julian-date)
1118 (define-key calendar-mode-map "gH" 'calendar-goto-hebrew-date)
1119 (define-key calendar-mode-map "gI" 'calendar-goto-islamic-date)
1120 (define-key calendar-mode-map "gC" 'calendar-goto-iso-date)
1121 (define-key calendar-mode-map " " 'scroll-other-window)
1122 (define-key calendar-mode-map "\C-c\C-l" 'redraw-calendar)
1123 (define-key calendar-mode-map "c" 'calendar-current-month)
1124 (define-key calendar-mode-map "o" 'calendar-other-month)
1125 (define-key calendar-mode-map "q" 'exit-calendar)
1126 (define-key calendar-mode-map "a" 'list-calendar-holidays)
1127 (define-key calendar-mode-map "h" 'calendar-cursor-holidays)
1128 (define-key calendar-mode-map "x" 'mark-calendar-holidays)
1129 (define-key calendar-mode-map "u" 'calendar-unmark)
1130 (define-key calendar-mode-map "m" 'mark-diary-entries)
1131 (define-key calendar-mode-map "d" 'view-diary-entries)
1132 (define-key calendar-mode-map "s" 'show-all-diary-entries)
1133 (define-key calendar-mode-map "D" 'cursor-to-calendar-day-of-year)
1134 (define-key calendar-mode-map "C" 'cursor-to-iso-calendar-date)
1135 (define-key calendar-mode-map "J" 'cursor-to-julian-calendar-date)
1136 (define-key calendar-mode-map "H" 'cursor-to-hebrew-calendar-date)
1137 (define-key calendar-mode-map "I" 'cursor-to-islamic-calendar-date)
1138 (define-key calendar-mode-map "F" 'cursor-to-french-calendar-date)
1139 (define-key calendar-mode-map "\C-cd" 'insert-diary-entry)
1140 (define-key calendar-mode-map "\C-cw" 'insert-weekly-diary-entry)
1141 (define-key calendar-mode-map "\C-cm" 'insert-monthly-diary-entry)
1142 (define-key calendar-mode-map "\C-cy" 'insert-yearly-diary-entry)
1143 (define-key calendar-mode-map "\C-ca" 'insert-anniversary-diary-entry)
1144 (define-key calendar-mode-map "\C-cb" 'insert-block-diary-entry)
1145 (define-key calendar-mode-map "\C-cc" 'insert-cyclic-diary-entry)
1146 (define-key calendar-mode-map "\C-cHd" 'insert-hebrew-diary-entry)
1147 (define-key calendar-mode-map "\C-cHm" 'insert-monthly-hebrew-diary-entry)
1148 (define-key calendar-mode-map "\C-cHy" 'insert-yearly-hebrew-diary-entry)
1149 (define-key calendar-mode-map "\C-cId" 'insert-islamic-diary-entry)
1150 (define-key calendar-mode-map "\C-cIm" 'insert-monthly-islamic-diary-entry)
1151 (define-key calendar-mode-map "\C-cIy" 'insert-yearly-islamic-diary-entry)
1152 (define-key calendar-mode-map "?" 'describe-calendar-mode))
1154 (defun describe-calendar-mode ()
1155 "Create a help buffer with a brief description of the calendar-mode."
1156 (interactive)
1157 (with-output-to-temp-buffer "*Help*"
1158 (princ
1159 (format
1160 "Calendar Mode:\nFor a complete description, type %s\n%s\n"
1161 (substitute-command-keys
1162 "\\<calendar-mode-map>\\[describe-mode] from within the calendar")
1163 (substitute-command-keys "\\{calendar-mode-map}")))
1164 (print-help-return-message)))
1166 ;; Calendar mode is suitable only for specially formatted data.
1167 (put 'calendar-mode 'mode-class 'special)
1169 (defvar calendar-mode-line-format
1170 (substitute-command-keys
1171 "\\<calendar-mode-map>\\[scroll-calendar-left] Calendar \\[describe-calendar-mode] help/\\[calendar-other-month] other/\\[calendar-current-month] current %17s \\[scroll-calendar-right]")
1172 "The mode line of the calendar buffer.")
1174 (defun calendar-mode ()
1175 "A major mode for the sliding calendar window and diary.
1177 The commands for cursor movement are:\\<calendar-mode-map>
1179 \\[calendar-forward-day] one day forward \\[calendar-backward-day] one day backward
1180 \\[calendar-forward-week] one week forward \\[calendar-backward-week] one week backward
1181 \\[calendar-forward-month] one month forward \\[calendar-backward-month] one month backward
1182 \\[calendar-forward-year] one year forward \\[calendar-backward-year] one year backward
1183 \\[calendar-beginning-of-week] beginning of week \\[calendar-end-of-week] end of week
1184 \\[calendar-beginning-of-month] beginning of month \\[calendar-end-of-month] end of month
1185 \\[calendar-beginning-of-year] beginning of year \\[calendar-end-of-year] end of year
1186 \\[calendar-goto-date] go to date \\[calendar-goto-julian-date] go to Julian date
1187 \\[calendar-goto-hebrew-date] go to Hebrew date \\[calendar-goto-islamic-date] go to Islamic date
1188 \\[calendar-goto-iso-date] go to ISO date
1190 You can mark a date in the calendar and switch the point and mark:
1191 \\[calendar-set-mark] mark date \\[calendar-exchange-point-and-mark] exchange point and mark
1192 You can determine the number of days (inclusive) between the point and mark by
1193 \\[calendar-count-days-region] count days in the region
1195 The commands for calendar movement are:
1197 \\[scroll-calendar-right] scroll one month right \\[scroll-calendar-left] scroll one month left
1198 \\[scroll-calendar-right-three-months] scroll 3 months right \\[scroll-calendar-left-three-months] scroll 3 months left
1199 \\[calendar-current-month] display current month \\[calendar-other-month] display another month
1201 Whenever it makes sense, the above commands take prefix arguments that
1202 multiply their affect. For convenience, the digit keys and the minus sign
1203 are bound to digit-argument, so they need not be prefixed with ESC.
1205 If the calendar window somehow becomes corrupted, it can be regenerated with
1207 \\[redraw-calendar] redraw the calendar
1209 The following commands deal with holidays and other notable days:
1211 \\[calendar-cursor-holidays] give holidays for the date specified by the cursor
1212 \\[mark-calendar-holidays] mark notable days
1213 \\[calendar-unmark] unmark dates
1214 \\[list-calendar-holidays] display notable days
1216 The command M-x holidays causes the notable dates for the current month, and
1217 the preceding and succeeding months, to be displayed, independently of the
1218 calendar.
1220 The following commands control the diary:
1222 \\[mark-diary-entries] mark diary entries \\[calendar-unmark] unmark dates
1223 \\[view-diary-entries] display diary entries \\[show-all-diary-entries] show all diary entries
1224 \\[print-diary-entries] print diary entries
1226 Displaying the diary entries causes the diary entries from the diary-file
1227 (for the date indicated by the cursor in the calendar window) to be
1228 displayed in another window. This function takes an integer argument that
1229 specifies the number of days of calendar entries to be displayed, starting
1230 with the date indicated by the cursor.
1232 The command \\[print-diary-entries] prints the diary buffer (as it appears)
1233 on the line printer.
1235 The command M-x diary causes the diary entries for the current date to be
1236 displayed, independently of the calendar. The number of days of entries is
1237 governed by number-of-diary-entries.
1239 The format of the entries in the diary file is described in the
1240 documentation string for the variable diary-file.
1242 When diary entries are in view in the window, they can be edited. It is
1243 important to keep in mind that the buffer displayed contains the entire
1244 diary file, but with portions of it concealed from view. This means, for
1245 instance, that the forward-char command can put the cursor at what appears
1246 to be the end of the line, but what is in reality the middle of some
1247 concealed line. BE CAREFUL WHEN EDITING THE DIARY ENTRIES! (Inserting
1248 additional lines or adding/deleting characters in the middle of a visible
1249 line will not cause problems; watch out for end-of-line, however--it may
1250 put you at the end of a concealed line far from where the cursor appears to
1251 be!) BEFORE EDITING THE DIARY IT IS BEST TO DISPLAY THE ENTIRE FILE WITH
1252 show-all-diary-entries. BE SURE TO WRITE THE FILE BEFORE EXITING FROM THE
1253 CALENDAR.
1255 The following commands assist in making diary entries:
1257 \\[insert-diary-entry] insert a diary entry for the selected date
1258 \\[insert-weekly-diary-entry] insert a diary entry for the selected day of the week
1259 \\[insert-monthly-diary-entry] insert a diary entry for the selected day of the month
1260 \\[insert-yearly-diary-entry] insert a diary entry for the selected day of the year
1261 \\[insert-block-diary-entry] insert a diary entry for the block days between point and mark
1262 \\[insert-anniversary-diary-entry] insert an anniversary diary entry for the selected date
1263 \\[insert-cyclic-diary-entry] insert a cyclic diary entry
1265 There are corresponding commands to assist in making Hebrew- or Islamic-date
1266 diary entries:
1268 \\[insert-hebrew-diary-entry] insert a diary entry for the Hebrew date corresponding
1269 to the selected date
1270 \\[insert-monthly-hebrew-diary-entry] insert a diary entry for the day of the Hebrew month
1271 corresponding to the selected day
1272 \\[insert-yearly-hebrew-diary-entry] insert a diary entry for the day of the Hebrew year
1273 corresponding to the selected day
1274 \\[insert-islamic-diary-entry] insert a diary entry for the Islamic date corresponding
1275 to the selected date
1276 \\[insert-monthly-islamic-diary-entry] insert a diary entry for the day of the Islamic month
1277 corresponding to the selected day
1278 \\[insert-yearly-islamic-diary-entry] insert a diary entry for the day of the Islamic year
1279 corresponding to the selected day
1281 All of the diary entry commands make nonmarking entries when given a prefix
1282 argument; with no prefix argument, the diary entries are marking.
1284 The day number in the year and the number of days remaining in the year can be
1285 determined by
1287 \\[cursor-to-calendar-day-of-year] show day number and the number of days remaining in the year
1289 Equivalent dates on the ISO commercial, Julian, Hebrew, Islamic and French
1290 Revolutionary calendars can be determined by
1292 \\[cursor-to-iso-calendar-date] show equivalent date on the ISO commercial calendar
1293 \\[cursor-to-julian-calendar-date] show equivalent date on the Julian calendar
1294 \\[cursor-to-hebrew-calendar-date] show equivalent date on the Hebrew calendar
1295 \\[cursor-to-islamic-calendar-date] show equivalent date on the Islamic calendar
1296 \\[cursor-to-french-calendar-date] show equivalent date on the French Revolutionary calendar
1298 To exit from the calendar use
1300 \\[exit-calendar] exit from calendar
1302 The variable `view-diary-entries-initially', whose default is nil, can be
1303 set to to t cause diary entries for the current date will be displayed in
1304 another window when the calendar is first displayed, if the current date is
1305 visible. The variable `number-of-diary-entries' controls number of days of
1306 diary entries that will be displayed initially or with the command M-x
1307 diary. For example, if the default value 1 is used, then only the current
1308 day's diary entries will be displayed. If the value 2 is used, both the
1309 current day's and the next day's entries will be displayed. The value can
1310 also be a vector: If the value is [0 2 2 2 2 4 1] then no diary entries
1311 will be displayed on Sunday, the current date's and the next day's diary
1312 entries will be displayed Monday through Thursday, Friday through Monday's
1313 entries will be displayed on Friday, while on Saturday only that day's
1314 entries will be displayed.
1316 The variable `view-calendar-holidays-initially' can be set to t to cause
1317 the holidays for the current three month period will be displayed on entry
1318 to the calendar. The holidays are displayed in another window.
1320 The variable `mark-diary-entries-in-calendar' can be set to t to cause any
1321 dates visible with calendar entries to be marked with the symbol specified
1322 by the variable `diary-entry-marker', normally a plus sign.
1324 The variable `initial-calendar-window-hook', whose default value is nil,
1325 is list of functions to be called when the calendar window is first opened.
1326 The functions invoked are called after the calendar window is opened, but
1327 once opened is never called again. Leaving the calendar with the `q' command
1328 and reentering it will cause these functions to be called again.
1330 The variable `today-visible-calendar-hook', whose default value is nil,
1331 is the list of functions called after the calendar buffer has been prepared
1332 with the calendar when the current date is visible in the window.
1333 This can be used, for example, to replace today's date with asterisks; a
1334 function calendar-star-date is included for this purpose:
1335 (setq today-visible-calendar-hook 'calendar-star-date)
1336 It could also be used to mark the current date with `*'; a function is also
1337 provided for this:
1338 (setq today-visible-calendar-hook 'calendar-mark-today)
1340 The variable `today-invisible-calendar-hook', whose default value is nil,
1341 is the list of functions called after the calendar buffer has been prepared
1342 with the calendar when the current date is not visible in the window.
1344 The variable `diary-display-hook' is the list of functions called
1345 after the diary buffer is prepared. The default value simply displays the
1346 diary file using selective-display to conceal irrelevant diary entries. An
1347 alternative function `fancy-diary-display' is provided that, when
1348 used as the `diary-display-hook', causes a noneditable buffer to be
1349 prepared with a neatly organized day-by-day listing of relevant diary
1350 entries, together with any known holidays. The inclusion of the holidays
1351 slows this fancy display of the diary; to speed it up, set the variable
1352 `holidays-in-diary-buffer' to nil.
1354 The variable `print-diary-entries-hook' is the list of functions called
1355 after a temporary buffer is prepared with the diary entries currently
1356 visible in the diary buffer. The default value of this hook adds a heading
1357 (composed from the diary buffer's mode line), does the printing with the
1358 command lpr-buffer, and kills the temporary buffer. Other uses might
1359 include, for example, rearranging the lines into order by day and time.
1361 The Gregorian calendar is assumed."
1363 (kill-all-local-variables)
1364 (setq major-mode 'calendar-mode)
1365 (setq mode-name "Calendar")
1366 (use-local-map calendar-mode-map)
1367 (setq buffer-read-only t)
1368 (setq indent-tabs-mode nil)
1369 (make-local-variable 'calendar-window-configuration);; Windows on entry.
1370 (make-local-variable 'calendar-mark-ring)
1371 (make-local-variable 'current-month) ;; Current month.
1372 (make-local-variable 'current-day) ;; Current day.
1373 (make-local-variable 'current-year) ;; Current year.
1374 (make-local-variable 'displayed-month);; Month in middle of window.
1375 (make-local-variable 'displayed-year));; Year in middle of window.
1377 (defun update-calendar-mode-line ()
1378 "Update the calendar mode line with the current date and date style."
1379 (if (bufferp (get-buffer calendar-buffer))
1380 (save-excursion
1381 (set-buffer calendar-buffer)
1382 (setq mode-line-format
1383 (format calendar-mode-line-format
1384 (calendar-date-string (calendar-current-date) t))))))
1386 (defun exit-calendar ()
1387 "Get out of the calendar window and destroy it and related buffers."
1388 (interactive)
1389 (let ((diary-buffer (get-file-buffer diary-file))
1390 (d-buffer (get-buffer fancy-diary-buffer))
1391 (h-buffer (get-buffer holiday-buffer)))
1392 (if (not diary-buffer)
1393 (progn
1394 (set-window-configuration calendar-window-configuration)
1395 (kill-buffer calendar-buffer)
1396 (if d-buffer (kill-buffer d-buffer))
1397 (if h-buffer (kill-buffer h-buffer)))
1398 (if (or (not (buffer-modified-p diary-buffer))
1399 (yes-or-no-p "Diary modified; do you really want to exit the calendar? "))
1400 (progn
1401 (set-window-configuration calendar-window-configuration)
1402 (kill-buffer calendar-buffer)
1403 (if d-buffer (kill-buffer d-buffer))
1404 (if h-buffer (kill-buffer h-buffer))
1405 (set-buffer diary-buffer)
1406 (set-buffer-modified-p nil)
1407 (kill-buffer diary-buffer))))))
1409 (defun calendar-current-month ()
1410 "Reposition the calendar window so the current date is visible."
1411 (interactive)
1412 (let ((today (calendar-current-date)));; The date might have changed.
1413 (if (not (calendar-date-is-visible-p today))
1414 (regenerate-calendar-window)
1415 (update-calendar-mode-line)
1416 (calendar-cursor-to-visible-date today))))
1418 (defun calendar-forward-month (arg)
1419 "Move the cursor forward ARG months.
1420 Movement is backward if ARG is negative."
1421 (interactive "p")
1422 (calendar-cursor-to-nearest-date)
1423 (let* ((cursor-date (or (calendar-cursor-to-date)
1424 (error "Cursor is not on a date!")))
1425 (month (extract-calendar-month cursor-date))
1426 (day (extract-calendar-day cursor-date))
1427 (year (extract-calendar-year cursor-date)))
1428 (increment-calendar-month month year arg)
1429 (let ((last (calendar-last-day-of-month month year)))
1430 (if (< last day)
1431 (setq day last)))
1432 ;; Put the new month on the screen, if needed, and go to the new date.
1433 (let ((new-cursor-date (list month day year)))
1434 (if (not (calendar-date-is-visible-p new-cursor-date))
1435 (calendar-other-month month year))
1436 (calendar-cursor-to-visible-date new-cursor-date))))
1438 (defun calendar-forward-year (arg)
1439 "Move the cursor forward by ARG years.
1440 Movement is backward if ARG is negative."
1441 (interactive "p")
1442 (calendar-forward-month (* 12 arg)))
1444 (defun calendar-backward-month (arg)
1445 "Move the cursor backward by ARG months.
1446 Movement is forward if ARG is negative."
1447 (interactive "p")
1448 (calendar-forward-month (- arg)))
1450 (defun calendar-backward-year (arg)
1451 "Move the cursor backward ARG years.
1452 Movement is forward is ARG is negative."
1453 (interactive "p")
1454 (calendar-forward-month (* -12 arg)))
1456 (defun scroll-calendar-left (arg)
1457 "Scroll the displayed calendar left by ARG months.
1458 If ARG is negative the calendar is scrolled right. Maintains the relative
1459 position of the cursor with respect to the calendar as well as possible."
1460 (interactive "p")
1461 (calendar-cursor-to-nearest-date)
1462 (let ((old-date (calendar-cursor-to-date))
1463 (today (calendar-current-date)))
1464 (if (/= arg 0)
1465 (progn
1466 (regenerate-calendar-window
1467 (+ arg (calendar-interval current-month current-year
1468 displayed-month displayed-year)))
1469 (calendar-cursor-to-visible-date
1470 (cond
1471 ((calendar-date-is-visible-p old-date) old-date)
1472 ((calendar-date-is-visible-p today) today)
1473 (t (list displayed-month 1 displayed-year))))))))
1475 (defun scroll-calendar-right (arg)
1476 "Scroll the displayed calendar window right by ARG months.
1477 If ARG is negative the calendar is scrolled left. Maintains the relative
1478 position of the cursor with respect to the calendar as well as possible."
1479 (interactive "p")
1480 (scroll-calendar-left (- arg)))
1482 (defun scroll-calendar-left-three-months (arg)
1483 "Scroll the displayed calendar window left by 3*ARG months.
1484 If ARG is negative the calendar is scrolled right. Maintains the relative
1485 position of the cursor with respect to the calendar as well as possible."
1486 (interactive "p")
1487 (scroll-calendar-left (* 3 arg)))
1489 (defun scroll-calendar-right-three-months (arg)
1490 "Scroll the displayed calendar window right by 3*ARG months.
1491 If ARG is negative the calendar is scrolled left. Maintains the relative
1492 position of the cursor with respect to the calendar as well as possible."
1493 (interactive "p")
1494 (scroll-calendar-left (* -3 arg)))
1496 (defun calendar-current-date ()
1497 "Returns the current date in a list (month day year).
1498 If in the calendar buffer, also sets the current date local variables."
1499 (let* ((date (current-time-string))
1500 (garbage
1501 (string-match
1502 "^\\([A-Z][a-z]*\\) *\\([A-Z][a-z]*\\) *\\([0-9]*\\) .* \\([0-9]*\\)$"
1503 date))
1504 (month
1505 (cdr (assoc
1506 (substring date (match-beginning 2) (match-end 2))
1507 (calendar-make-alist
1508 calendar-month-name-array
1510 '(lambda (x) (substring x 0 3))))))
1511 (day
1512 (string-to-int (substring date (match-beginning 3) (match-end 3))))
1513 (year
1514 (string-to-int (substring date (match-beginning 4) (match-end 4)))))
1515 (if (equal (current-buffer) (get-buffer calendar-buffer))
1516 (progn
1517 (setq current-month month)
1518 (setq current-day day)
1519 (setq current-year year)))
1520 (list month day year)))
1522 (defun calendar-cursor-to-date ()
1523 "Returns a list of the month, day, and year of current cursor position.
1524 Returns nil if the cursor is not on a specific day."
1525 (if (and (looking-at "[*0-9]")
1526 (< 2 (count-lines (point-min) (point))))
1527 (save-excursion
1528 (re-search-backward "[^0-9]")
1529 (forward-char 1)
1530 (let*
1531 ((day (string-to-int (buffer-substring (point) (+ 3 (point)))))
1532 (day (if (= 0 day) current-day day));; Starred date.
1533 (segment (/ (current-column) 25))
1534 (month (% (+ displayed-month segment -1) 12))
1535 (month (if (= 0 month) 12 month))
1536 (year
1537 (cond
1538 ((and (= 12 month) (= segment 0)) (1- displayed-year))
1539 ((and (= 1 month) (= segment 2)) (1+ displayed-year))
1540 (t displayed-year))))
1541 (list month day year)))))
1543 (defun calendar-cursor-to-nearest-date ()
1544 "Move the cursor to the closest date.
1545 The position of the cursor is unchanged if it is already on a date.
1546 Returns the list (month day year) giving the cursor position."
1547 (let ((date (calendar-cursor-to-date))
1548 (column (current-column)))
1549 (if date
1550 date
1551 (if (> 3 (count-lines (point-min) (point)))
1552 (progn
1553 (goto-line 3)
1554 (move-to-column column)))
1555 (if (not (looking-at "[0-9]"))
1556 (if (and (not (looking-at " *$"))
1557 (or (< column 25)
1558 (and (> column 27)
1559 (< column 50))
1560 (and (> column 52)
1561 (< column 75))))
1562 (progn
1563 (re-search-forward "[0-9]" nil t)
1564 (backward-char 1))
1565 (re-search-backward "[0-9]" nil t)))
1566 (calendar-cursor-to-date))))
1568 (defun calendar-forward-day (arg)
1569 "Move the cursor forward ARG days.
1570 Moves backward if ARG is negative."
1571 (interactive "p")
1572 (if (/= 0 arg)
1573 (let*
1574 ((cursor-date (calendar-cursor-to-date))
1575 (cursor-date (if cursor-date
1576 cursor-date
1577 (if (> arg 0) (setq arg (1- arg)))
1578 (calendar-cursor-to-nearest-date)))
1579 (new-cursor-date
1580 (calendar-gregorian-from-absolute
1581 (+ (calendar-absolute-from-gregorian cursor-date) arg)))
1582 (new-display-month (extract-calendar-month new-cursor-date))
1583 (new-display-year (extract-calendar-year new-cursor-date)))
1584 ;; Put the new month on the screen, if needed, and go to the new date.
1585 (if (not (calendar-date-is-visible-p new-cursor-date))
1586 (calendar-other-month new-display-month new-display-year))
1587 (calendar-cursor-to-visible-date new-cursor-date))))
1589 (defun calendar-backward-day (arg)
1590 "Move the cursor back ARG days.
1591 Moves forward if ARG is negative."
1592 (interactive "p")
1593 (calendar-forward-day (- arg)))
1595 (defun calendar-forward-week (arg)
1596 "Move the cursor forward ARG weeks.
1597 Moves backward if ARG is negative."
1598 (interactive "p")
1599 (calendar-forward-day (* arg 7)))
1601 (defun calendar-backward-week (arg)
1602 "Move the cursor back ARG weeks.
1603 Moves forward if ARG is negative."
1604 (interactive "p")
1605 (calendar-forward-day (* arg -7)))
1607 (defun calendar-beginning-of-week (arg)
1608 "Move the cursor back ARG Sundays."
1609 (interactive "p")
1610 (calendar-cursor-to-nearest-date)
1611 (let ((day (calendar-day-of-week (calendar-cursor-to-date))))
1612 (calendar-backward-day
1613 (if (= day 0) (* 7 arg) (+ day (* 7 (1- arg)))))))
1615 (defun calendar-end-of-week (arg)
1616 "Move the cursor forward ARG Saturdays."
1617 (interactive "p")
1618 (calendar-cursor-to-nearest-date)
1619 (let ((day (calendar-day-of-week (calendar-cursor-to-date))))
1620 (calendar-forward-day
1621 (if (= day 6) (* 7 arg) (+ (- 6 day) (* 7 (1- arg)))))))
1623 (defun calendar-beginning-of-month (arg)
1624 "Move the cursor backward ARG month beginnings."
1625 (interactive "p")
1626 (calendar-cursor-to-nearest-date)
1627 (let* ((date (calendar-cursor-to-date))
1628 (month (extract-calendar-month date))
1629 (day (extract-calendar-day date))
1630 (year (extract-calendar-year date)))
1631 (if (= day 1)
1632 (calendar-backward-month arg)
1633 (calendar-cursor-to-visible-date (list month 1 year))
1634 (calendar-backward-month (1- arg)))))
1636 (defun calendar-end-of-month (arg)
1637 "Move the cursor forward ARG month ends."
1638 (interactive "p")
1639 (calendar-cursor-to-nearest-date)
1640 (let* ((date (calendar-cursor-to-date))
1641 (month (extract-calendar-month date))
1642 (day (extract-calendar-day date))
1643 (year (extract-calendar-year date))
1644 (last-day (calendar-last-day-of-month month year)))
1645 (if (/= day last-day)
1646 (progn
1647 (calendar-cursor-to-visible-date (list month last-day year))
1648 (setq arg (1- arg))))
1649 (increment-calendar-month month year arg)
1650 (let ((last-day (list
1651 month
1652 (calendar-last-day-of-month month year)
1653 year)))
1654 (if (not (calendar-date-is-visible-p last-day))
1655 (calendar-other-month month year)
1656 (calendar-cursor-to-visible-date last-day)))))
1658 (defun calendar-beginning-of-year (arg)
1659 "Move the cursor backward ARG year beginnings."
1660 (interactive "p")
1661 (calendar-cursor-to-nearest-date)
1662 (let* ((date (calendar-cursor-to-date))
1663 (month (extract-calendar-month date))
1664 (day (extract-calendar-day date))
1665 (year (extract-calendar-year date))
1666 (jan-first (list 1 1 year)))
1667 (if (and (= day 1) (= 1 month))
1668 (calendar-backward-month (* 12 arg))
1669 (if (and (= arg 1)
1670 (calendar-date-is-visible-p jan-first))
1671 (calendar-cursor-to-visible-date jan-first)
1672 (calendar-other-month 1 (- year (1- arg)))))))
1674 (defun calendar-end-of-year (arg)
1675 "Move the cursor forward ARG year beginnings."
1676 (interactive "p")
1677 (calendar-cursor-to-nearest-date)
1678 (let* ((date (calendar-cursor-to-date))
1679 (month (extract-calendar-month date))
1680 (day (extract-calendar-day date))
1681 (year (extract-calendar-year date))
1682 (dec-31 (list 12 31 year)))
1683 (if (and (= day 31) (= 12 month))
1684 (calendar-forward-month (* 12 arg))
1685 (if (and (= arg 1)
1686 (calendar-date-is-visible-p dec-31))
1687 (calendar-cursor-to-visible-date dec-31)
1688 (calendar-other-month 12 (- year (1- arg)))
1689 (calendar-cursor-to-visible-date (list 12 31 displayed-year))))))
1691 (defun calendar-gregorian-from-absolute (date)
1692 "Compute the list (month day year) corresponding to the absolute DATE.
1693 The absolute date is the number of days elapsed since the (imaginary)
1694 Gregorian date Sunday, December 31, 1 BC."
1695 (let* ((approx (/ date 366));; Approximation from below.
1696 (year ;; Search forward from the approximation.
1697 (+ approx
1698 (calendar-sum y approx
1699 (>= date (calendar-absolute-from-gregorian (list 1 1 (1+ y))))
1700 1)))
1701 (month ;; Search forward from January.
1702 (1+ (calendar-sum m 1
1703 (> date
1704 (calendar-absolute-from-gregorian
1705 (list m (calendar-last-day-of-month m year) year)))
1706 1)))
1707 (day ;; Calculate the day by subtraction.
1708 (- date
1709 (1- (calendar-absolute-from-gregorian (list month 1 year))))))
1710 (list month day year)))
1712 (defun calendar-cursor-to-visible-date (date)
1713 "Move the cursor to DATE that is on the screen."
1714 (let ((month (extract-calendar-month date))
1715 (day (extract-calendar-day date))
1716 (year (extract-calendar-year date)))
1717 (goto-line (+ 3
1718 (/ (+ day -1
1719 (calendar-day-of-week (list month 1 year)))
1720 7)))
1721 (move-to-column (+ 6
1722 (* 25
1723 (1+ (calendar-interval
1724 displayed-month displayed-year month year)))
1725 (* 3 (calendar-day-of-week date))))))
1727 (defun calendar-other-month (month year)
1728 "Display a three-month calendar centered around MONTH and YEAR."
1729 (interactive
1730 (let* ((completion-ignore-case t)
1731 (month (cdr (assoc
1732 (capitalize
1733 (completing-read
1734 "Month name: "
1735 (mapcar 'list (append calendar-month-name-array nil))
1736 nil t))
1737 (calendar-make-alist calendar-month-name-array))))
1738 (year (calendar-read
1739 "Year (>0): "
1740 '(lambda (x) (> x 0))
1741 (int-to-string current-year))))
1742 (list month year)))
1743 (if (and (= month displayed-month)
1744 (= year displayed-year))
1746 (let ((old-date (calendar-cursor-to-date))
1747 (today (calendar-current-date)))
1748 (regenerate-calendar-window
1749 (calendar-interval current-month current-year month year))
1750 (calendar-cursor-to-visible-date
1751 (cond
1752 ((calendar-date-is-visible-p old-date) old-date)
1753 ((calendar-date-is-visible-p today) today)
1754 (t (list month 1 year)))))))
1756 (defun calendar-set-mark (arg)
1757 "Mark the date under the cursor, or jump to marked date.
1758 With no prefix argument, push current date onto marked date ring.
1759 With argument, jump to mark, pop it, and put point at end of ring."
1760 (interactive "P")
1761 (let ((date (or (calendar-cursor-to-date)
1762 (error "Cursor is not on a date!"))))
1763 (if (null arg)
1764 (progn
1765 (setq calendar-mark-ring (cons date calendar-mark-ring))
1766 ;; Since the top of the mark ring is the marked date in the
1767 ;; calendar, the mark ring in the calendar is one longer than
1768 ;; in other buffers to get the same effect.
1769 (if (> (length calendar-mark-ring) (1+ mark-ring-max))
1770 (setcdr (nthcdr mark-ring-max calendar-mark-ring) nil))
1771 (message "Mark set"))
1772 (if (null calendar-mark-ring)
1773 (error "No mark set in this buffer")
1774 (calendar-goto-date (car calendar-mark-ring))
1775 (setq calendar-mark-ring
1776 (cdr (nconc calendar-mark-ring (list date))))))))
1778 (defun calendar-exchange-point-and-mark ()
1779 "Exchange the current cursor position with the marked date."
1780 (interactive)
1781 (let ((mark (car calendar-mark-ring))
1782 (date (or (calendar-cursor-to-date)
1783 (error "Cursor is not on a date!"))))
1784 (if (null mark)
1785 (error "No mark set in this buffer")
1786 (setq calendar-mark-ring (cons date (cdr calendar-mark-ring)))
1787 (calendar-goto-date mark))))
1789 (defun calendar-count-days-region ()
1790 "Count the number of days (inclusive) between point and the mark."
1791 (interactive)
1792 (let* ((days (- (calendar-absolute-from-gregorian
1793 (or (calendar-cursor-to-date)
1794 (error "Cursor is not on a date!")))
1795 (calendar-absolute-from-gregorian
1796 (or (car calendar-mark-ring)
1797 (error "No mark set in this buffer")))))
1798 (days (1+ (if (> days 0) days (- days)))))
1799 (message "Region has %d day%s (inclusive)"
1800 days (if (> days 1) "s" ""))))
1802 (defun calendar-not-implemented ()
1803 "Not implemented."
1804 (interactive)
1805 (error "%s not available in the calendar"
1806 (global-key-binding (this-command-keys))))
1808 (defun calendar-read (prompt acceptable &optional initial-contents)
1809 "Return an object read from the minibuffer.
1810 Prompt with the string PROMPT and use the function ACCEPTABLE to decide if
1811 entered item is acceptable. If non-nil, optional third arg INITIAL-CONTENTS
1812 is a string to insert in the minibuffer before reading."
1813 (let ((value (read-minibuffer prompt initial-contents)))
1814 (while (not (funcall acceptable value))
1815 (setq value (read-minibuffer prompt initial-contents)))
1816 value))
1818 (defun calendar-goto-date (date)
1819 "Move cursor to DATE."
1820 (interactive
1821 (let* ((year (calendar-read
1822 "Year (>0): "
1823 '(lambda (x) (> x 0))
1824 (int-to-string current-year)))
1825 (month-array calendar-month-name-array)
1826 (completion-ignore-case t)
1827 (month (cdr (assoc
1828 (capitalize
1829 (completing-read
1830 "Month name: "
1831 (mapcar 'list (append month-array nil))
1832 nil t))
1833 (calendar-make-alist month-array 1 'capitalize))))
1834 (last (calendar-last-day-of-month month year))
1835 (day (calendar-read
1836 (format "Day (1-%d): " last)
1837 '(lambda (x) (and (< 0 x) (<= x last))))))
1838 (list (list month day year))))
1839 (let ((month (extract-calendar-month date))
1840 (year (extract-calendar-year date)))
1841 (if (not (calendar-date-is-visible-p date))
1842 (calendar-other-month
1843 (if (and (= month 1) (= year 1))
1845 month)
1846 year)))
1847 (calendar-cursor-to-visible-date date))
1849 (defun calendar-goto-julian-date (date &optional noecho)
1850 "Move cursor to Julian DATE; echo Julian date unless NOECHO is t."
1851 (interactive
1852 (let* ((year (calendar-read
1853 "Julian calendar year (>0): "
1854 '(lambda (x) (> x 0))
1855 (int-to-string
1856 (extract-calendar-year
1857 (calendar-julian-from-absolute
1858 (calendar-absolute-from-gregorian
1859 (list current-month 1 current-year)))))))
1860 (month-array calendar-month-name-array)
1861 (completion-ignore-case t)
1862 (month (cdr (assoc
1863 (capitalize
1864 (completing-read
1865 "Julian calendar month name: "
1866 (mapcar 'list (append month-array nil))
1867 nil t))
1868 (calendar-make-alist month-array 1 'capitalize))))
1869 (last
1870 (if (and (zerop (% year 4)) (= month 2))
1872 (aref [31 28 31 30 31 30 31 31 30 31 30 31] (1- month))))
1873 (day (calendar-read
1874 (format "Julian calendar day (%d-%d): "
1875 (if (and (= year 1) (= month 1)) 3 1) last)
1876 '(lambda (x)
1877 (and (< (if (and (= year 1) (= month 1)) 2 0) x)
1878 (<= x last))))))
1879 (list (list month day year))))
1880 (calendar-goto-date (calendar-gregorian-from-absolute
1881 (calendar-absolute-from-julian date)))
1882 (or noecho (cursor-to-julian-calendar-date)))
1884 (defun calendar-goto-hebrew-date (date &optional noecho)
1885 "Move cursor to Hebrew DATE; echo Hebrew date unless NOECHO is t."
1886 (interactive
1887 (let* ((year (calendar-read
1888 "Hebrew calendar year (>3760): "
1889 '(lambda (x) (> x 3760))
1890 (int-to-string
1891 (extract-calendar-year
1892 (calendar-hebrew-from-absolute
1893 (calendar-absolute-from-gregorian
1894 (list current-month 1 current-year)))))))
1895 (month-array (if (hebrew-calendar-leap-year-p year)
1896 calendar-hebrew-month-name-array-leap-year
1897 calendar-hebrew-month-name-array-common-year))
1898 (completion-ignore-case t)
1899 (month (cdr (assoc
1900 (capitalize
1901 (completing-read
1902 "Hebrew calendar month name: "
1903 (mapcar 'list (append month-array nil))
1904 (if (= year 3761)
1905 '(lambda (x)
1906 (let ((m (cdr
1907 (assoc
1908 (car x)
1909 (calendar-make-alist
1910 month-array)))))
1911 (< 0
1912 (calendar-absolute-from-hebrew
1913 (list m
1914 (hebrew-calendar-last-day-of-month
1915 m year)
1916 year))))))
1919 (calendar-make-alist month-array 1 'capitalize))))
1920 (last (hebrew-calendar-last-day-of-month month year))
1921 (first (if (and (= year 3761) (= month 10))
1922 18 1))
1923 (day (calendar-read
1924 (format "Hebrew calendar day (%d-%d): "
1925 first last)
1926 '(lambda (x) (and (<= first x) (<= x last))))))
1927 (list (list month day year))))
1928 (calendar-goto-date (calendar-gregorian-from-absolute
1929 (calendar-absolute-from-hebrew date)))
1930 (or noecho (cursor-to-hebrew-calendar-date)))
1932 (defun calendar-goto-islamic-date (date &optional noecho)
1933 "Move cursor to Islamic DATE; echo Islamic date unless NOECHO is t."
1934 (interactive
1935 (let* ((year (calendar-read
1936 "Islamic calendar year (>0): "
1937 '(lambda (x) (> x 0))
1938 (int-to-string
1939 (extract-calendar-year
1940 (calendar-islamic-from-absolute
1941 (calendar-absolute-from-gregorian
1942 (list current-month 1 current-year)))))))
1943 (month-array calendar-islamic-month-name-array)
1944 (completion-ignore-case t)
1945 (month (cdr (assoc
1946 (capitalize
1947 (completing-read
1948 "Islamic calendar month name: "
1949 (mapcar 'list (append month-array nil))
1950 nil t))
1951 (calendar-make-alist month-array 1 'capitalize))))
1952 (last (islamic-calendar-last-day-of-month month year))
1953 (day (calendar-read
1954 (format "Islamic calendar day (1-%d): " last)
1955 '(lambda (x) (and (< 0 x) (<= x last))))))
1956 (list (list month day year))))
1957 (calendar-goto-date (calendar-gregorian-from-absolute
1958 (calendar-absolute-from-islamic date)))
1959 (or noecho (cursor-to-islamic-calendar-date)))
1961 (defun calendar-goto-iso-date (date &optional noecho)
1962 "Move cursor to ISO DATE; echo ISO date unless NOECHO is t."
1963 (interactive
1964 (let* ((year (calendar-read
1965 "ISO calendar year (>0): "
1966 '(lambda (x) (> x 0))
1967 (int-to-string current-year)))
1968 (no-weeks (extract-calendar-month
1969 (calendar-iso-from-absolute
1971 (calendar-dayname-on-or-before
1972 1 (calendar-absolute-from-gregorian
1973 (list 1 4 (1+ year))))))))
1974 (week (calendar-read
1975 (format "ISO calendar week (1-%d): " no-weeks)
1976 '(lambda (x) (and (> x 0) (<= x no-weeks)))))
1977 (day (calendar-read
1978 "ISO day (1-7): "
1979 '(lambda (x) (and (<= 1 x) (<= x 7))))))
1980 (list (list week day year))))
1981 (calendar-goto-date (calendar-gregorian-from-absolute
1982 (calendar-absolute-from-iso date)))
1983 (or noecho (cursor-to-iso-calendar-date)))
1985 (defun calendar-interval (mon1 yr1 mon2 yr2)
1986 "The number of months difference between the two specified months."
1987 (+ (* 12 (- yr2 yr1))
1988 (- mon2 mon1)))
1990 (defun calendar-day-name (date)
1991 "Returns a string with the name of the day of the week of DATE."
1992 (aref calendar-day-name-array (calendar-day-of-week date)))
1994 (defconst calendar-day-name-array
1995 ["Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday"])
1997 (defconst calendar-month-name-array
1998 ["January" "February" "March" "April" "May" "June"
1999 "July" "August" "September" "October" "November" "December"])
2001 (defun calendar-make-alist (sequence &optional start-index filter)
2002 "Make an assoc list corresponding to SEQUENCE.
2003 Start at index 1, unless optional START-INDEX is provided.
2004 If FILTER is provided, apply it to each item in the list."
2005 (let ((index (if start-index (1- start-index) 0)))
2006 (mapcar
2007 '(lambda (x)
2008 (setq index (1+ index))
2009 (cons (if filter (funcall filter x) x)
2010 index))
2011 (append sequence nil))))
2013 (defun calendar-month-name (month)
2014 "The name of MONTH."
2015 (aref calendar-month-name-array (1- month)))
2017 (defun calendar-day-of-week (date)
2018 "Returns the day-of-the-week index of DATE, 0 for Sunday, 1 for Monday, etc."
2019 (% (calendar-absolute-from-gregorian date) 7))
2021 (defun calendar-unmark ()
2022 "Delete the diary and holiday marks from the calendar."
2023 (interactive)
2024 (setq mark-diary-entries-in-calendar nil)
2025 (setq mark-holidays-in-calendar nil)
2026 (save-excursion
2027 (goto-line 3)
2028 (beginning-of-line)
2029 (let ((buffer-read-only nil)
2030 (start (point))
2031 (star-date (search-forward "**" nil t))
2032 (star-point (point)))
2033 (if star-date
2034 (progn ;; Don't delete today as left by calendar-star-date
2035 (subst-char-in-region start (- star-point 2)
2036 (string-to-char diary-entry-marker) ? t)
2037 (subst-char-in-region start (- star-point 2)
2038 (string-to-char calendar-holiday-marker) ? t)
2039 (subst-char-in-region star-point (point-max)
2040 (string-to-char diary-entry-marker) ? t)
2041 (subst-char-in-region star-point (point-max)
2042 (string-to-char calendar-holiday-marker) ? t))
2043 (subst-char-in-region start (point-max)
2044 (string-to-char diary-entry-marker) ? t)
2045 (subst-char-in-region start (point-max)
2046 (string-to-char calendar-holiday-marker) ? t))
2047 (set-buffer-modified-p nil))))
2049 (defun calendar-date-is-visible-p (date)
2050 "Returns t if DATE is legal and is visible in the calendar window."
2051 (let ((gap (calendar-interval
2052 displayed-month displayed-year
2053 (extract-calendar-month date) (extract-calendar-year date))))
2054 (and (calendar-date-is-legal-p date) (> 2 gap) (< -2 gap))))
2056 (defun calendar-date-is-legal-p (date)
2057 "Returns t if DATE is a legal date."
2058 (let ((month (extract-calendar-month date))
2059 (day (extract-calendar-day date))
2060 (year (extract-calendar-year date)))
2061 (and (<= 1 month) (<= month 12)
2062 (<= 1 day) (<= day (calendar-last-day-of-month month year))
2063 (<= 1 year))))
2065 (defun calendar-date-equal (date1 date2)
2066 "Returns t if the DATE1 and DATE2 are the same."
2067 (and
2068 (= (extract-calendar-month date1) (extract-calendar-month date2))
2069 (= (extract-calendar-day date1) (extract-calendar-day date2))
2070 (= (extract-calendar-year date1) (extract-calendar-year date2))))
2072 (defun mark-visible-calendar-date (date &optional mark)
2073 "Leave mark DATE with MARK. MARK defaults to diary-entry-marker."
2074 (if (calendar-date-is-legal-p date)
2075 (save-excursion
2076 (set-buffer calendar-buffer)
2077 (calendar-cursor-to-visible-date date)
2078 (forward-char 1)
2079 (let ((buffer-read-only nil))
2080 (delete-char 1)
2081 (insert (if mark mark diary-entry-marker))
2082 (forward-char -2))
2083 (set-buffer-modified-p nil))))
2085 (defun calendar-star-date ()
2086 "Replace the date under the cursor in the calendar window with asterisks.
2087 This function can be used with the today-visible-calendar-hook run after the
2088 calendar window has been prepared."
2089 (let ((buffer-read-only nil))
2090 (forward-char 1)
2091 (delete-char -2)
2092 (insert "**")
2093 (backward-char 1)
2094 (set-buffer-modified-p nil)))
2096 (defun calendar-mark-today ()
2097 "Mark the date under the cursor in the calendar window with an equal sign.
2098 This function can be used with the today-visible-calendar-hook run after the
2099 calendar window has been prepared."
2100 (let ((buffer-read-only nil))
2101 (forward-char 1)
2102 (delete-char 1)
2103 (insert "=")
2104 (backward-char 2)
2105 (set-buffer-modified-p nil)))
2107 (defun calendar-date-compare (date1 date2)
2108 "Returns t if DATE1 is before DATE2, nil otherwise.
2109 The actual dates are in the car of DATE1 and DATE2."
2110 (< (calendar-absolute-from-gregorian (car date1))
2111 (calendar-absolute-from-gregorian (car date2))))
2113 (defun calendar-date-string (date &optional abbreviate nodayname)
2114 "A string form of DATE, driven by the variable `calendar-date-display-form'.
2115 An optional parameter ABBREVIATE, when t, causes the month and day names to be
2116 abbreviated to three characters. An optional parameter NODAYNAME, when t,
2117 omits the name of the day of the week."
2118 (let* ((dayname
2119 (if nodayname
2121 (if abbreviate
2122 (substring (calendar-day-name date) 0 3)
2123 (calendar-day-name date))))
2124 (month (extract-calendar-month date))
2125 (monthname
2126 (if abbreviate
2127 (substring
2128 (calendar-month-name month) 0 3)
2129 (calendar-month-name month)))
2130 (day (int-to-string (extract-calendar-day date)))
2131 (month (int-to-string month))
2132 (year (int-to-string (extract-calendar-year date))))
2133 (mapconcat 'eval calendar-date-display-form "")))
2135 (defun calendar-dayname-on-or-before (dayname date)
2136 "Returns the absolute date of the DAYNAME on or before absolute DATE.
2137 DAYNAME=0 means Sunday, DAYNAME=1 means Monday, and so on.
2139 Note: Applying this function to d+6 gives us the DAYNAME on or after an
2140 absolute day d. Similarly, applying it to d+3 gives the DAYNAME nearest to
2141 absolute date d, applying it to d-1 gives the DAYNAME previous to absolute
2142 date d, and applying it to d+7 gives the DAYNAME following absolute date d."
2143 (- date (% (- date dayname) 7)))
2145 (defun calendar-nth-named-day (n dayname month year)
2146 "Returns the date of the Nth DAYNAME in MONTH, YEAR.
2147 A DAYNAME of 0 means Sunday, 1 means Monday, and so on. If N<0, the
2148 date returned is the Nth DAYNAME from the end of MONTH, YEAR (that is, -1 is
2149 the last DAYNAME, -2 is the penultimate DAYNAME, and so on."
2150 (calendar-gregorian-from-absolute
2151 (if (> n 0)
2152 (+ (calendar-dayname-on-or-before
2153 dayname (calendar-absolute-from-gregorian (list month 7 year)))
2154 (* 7 (1- n)))
2155 (+ (calendar-dayname-on-or-before
2156 dayname
2157 (calendar-absolute-from-gregorian
2158 (list month (calendar-last-day-of-month month year) year)))
2159 (* 7 (1+ n))))))
2161 (defun cursor-to-calendar-day-of-year ()
2162 "Show the day number in the year and the number of days remaining in the
2163 year for the date under the cursor."
2164 (interactive)
2165 (let* ((date (or (calendar-cursor-to-date)
2166 (error "Cursor is not on a date!")))
2167 (year (extract-calendar-year date))
2168 (day (calendar-day-number date))
2169 (days-remaining (- (calendar-day-number (list 12 31 year)) day)))
2170 (message "Day %d of %d; %d day%s remaining in the year"
2171 day year days-remaining (if (= days-remaining 1) "" "s"))))
2173 (defun calendar-absolute-from-iso (date)
2174 "The number of days elapsed between the Gregorian date 12/31/1 BC and
2175 DATE. The `ISO year' corresponds approximately to the Gregorian year, but
2176 weeks start on Monday and end on Sunday. The first week of the ISO year is
2177 the first such week in which at least 4 days are in a year. The ISO
2178 commercial DATE has the form (week day year) in which week is in the range
2179 1..52 and day is in the range 0..6 (1 = Monday, 2 = Tuesday, ..., 0 =
2180 Sunday). The The Gregorian date Sunday, December 31, 1 BC is imaginary."
2181 (let* ((week (extract-calendar-month date))
2182 (day (extract-calendar-day date))
2183 (year (extract-calendar-year date)))
2184 (+ (calendar-dayname-on-or-before
2185 1 (+ 3 (calendar-absolute-from-gregorian (list 1 1 year))))
2186 (* 7 (1- week))
2187 (if (= day 0) 6 (1- day)))))
2189 (defun calendar-iso-from-absolute (date)
2190 "Compute the `ISO commercial date' corresponding to the absolute DATE.
2191 The ISO year corresponds approximately to the Gregorian year, but weeks
2192 start on Monday and end on Sunday. The first week of the ISO year is the
2193 first such week in which at least 4 days are in a year. The ISO commercial
2194 date has the form (week day year) in which week is in the range 1..52 and
2195 day is in the range 0..6 (1 = Monday, 2 = Tuesday, ..., 0 = Sunday). The
2196 absolute date is the number of days elapsed since the (imaginary) Gregorian
2197 date Sunday, December 31, 1 BC."
2198 (let* ((approx (extract-calendar-year
2199 (calendar-gregorian-from-absolute (- date 3))))
2200 (year (+ approx
2201 (calendar-sum y approx
2202 (>= date (calendar-absolute-from-iso (list 1 1 (1+ y))))
2203 1))))
2204 (list
2205 (1+ (/ (- date (calendar-absolute-from-iso (list 1 1 year))) 7))
2206 (% date 7)
2207 year)))
2209 (defun cursor-to-iso-calendar-date ()
2210 "Show the equivalent date on the `ISO commercial calendar' for the date
2211 under the cursor."
2212 (interactive)
2213 (let* ((greg-date
2214 (or (calendar-cursor-to-date)
2215 (error "Cursor is not on a date!")))
2216 (day (% (calendar-absolute-from-gregorian greg-date) 7))
2217 (iso-date (calendar-iso-from-absolute
2218 (calendar-absolute-from-gregorian greg-date))))
2219 (message "ISO date: Day %s of week %d of %d."
2220 (if (zerop day) 7 day)
2221 (extract-calendar-month iso-date)
2222 (extract-calendar-year iso-date))))
2224 (defun calendar-julian-from-absolute (date)
2225 "Compute the Julian (month day year) corresponding to the absolute DATE.
2226 The absolute date is the number of days elapsed since the (imaginary)
2227 Gregorian date Sunday, December 31, 1 BC."
2228 (let* ((approx (/ (+ date 2) 366));; Approximation from below.
2229 (year ;; Search forward from the approximation.
2230 (+ approx
2231 (calendar-sum y approx
2232 (>= date (calendar-absolute-from-julian (list 1 1 (1+ y))))
2233 1)))
2234 (month ;; Search forward from January.
2235 (1+ (calendar-sum m 1
2236 (> date
2237 (calendar-absolute-from-julian
2238 (list m
2239 (if (and (= m 2) (= (% year 4) 0))
2241 (aref [31 28 31 30 31 30 31 31 30 31 30 31]
2242 (1- m)))
2243 year)))
2244 1)))
2245 (day ;; Calculate the day by subtraction.
2246 (- date (1- (calendar-absolute-from-julian (list month 1 year))))))
2247 (list month day year)))
2249 (defun calendar-absolute-from-julian (date)
2250 "The number of days elapsed between the Gregorian date 12/31/1 BC and DATE.
2251 The Gregorian date Sunday, December 31, 1 BC is imaginary."
2252 (let ((month (extract-calendar-month date))
2253 (day (extract-calendar-day date))
2254 (year (extract-calendar-year date)))
2255 (+ (calendar-day-number date)
2256 (if (and (= (% year 100) 0)
2257 (/= (% year 400) 0)
2258 (> month 2))
2259 1 0);; Correct for Julian but not Gregorian leap year.
2260 (* 365 (1- year))
2261 (/ (1- year) 4)
2262 -2)))
2264 (defun cursor-to-julian-calendar-date ()
2265 "Show the Julian calendar equivalent of the date under the cursor."
2266 (interactive)
2267 (let ((calendar-date-display-form
2268 (if european-calendar-style
2269 '(day " " monthname " " year)
2270 '(monthname " " day ", " year))))
2271 (message "Julian date: %s"
2272 (calendar-date-string
2273 (calendar-julian-from-absolute
2274 (calendar-absolute-from-gregorian
2275 (or (calendar-cursor-to-date)
2276 (error "Cursor is not on a date!"))))))))
2278 (defun islamic-calendar-leap-year-p (year)
2279 "Returns t if YEAR is a leap year on the Islamic calendar."
2280 (memq (% year 30)
2281 (list 2 5 7 10 13 16 18 21 24 26 29)))
2283 (defun islamic-calendar-last-day-of-month (month year)
2284 "The last day in MONTH during YEAR on the Islamic calendar."
2285 (cond
2286 ((memq month (list 1 3 5 7 9 11)) 30)
2287 ((memq month (list 2 4 6 8 10)) 29)
2288 (t (if (islamic-calendar-leap-year-p year) 30 29))))
2290 (defun islamic-calendar-day-number (date)
2291 "Return the day number within the year of the Islamic date DATE."
2292 (let* ((month (extract-calendar-month date))
2293 (day (extract-calendar-day date)))
2294 (+ (* 30 (/ month 2))
2295 (* 29 (/ (1- month) 2))
2296 day)))
2298 (defun calendar-absolute-from-islamic (date)
2299 "Absolute date of Islamic DATE.
2300 The absolute date is the number of days elapsed since the (imaginary)
2301 Gregorian date Sunday, December 31, 1 BC."
2302 (let* ((month (extract-calendar-month date))
2303 (day (extract-calendar-day date))
2304 (year (extract-calendar-year date))
2305 (y (% year 30))
2306 (leap-years-in-cycle
2307 (cond
2308 ((< y 3) 0) ((< y 6) 1) ((< y 8) 2) ((< y 11) 3) ((< y 14) 4)
2309 ((< y 17) 5) ((< y 19) 6) ((< y 22) 7) ((< y 25) 8) ((< y 27) 9)
2310 (t 10))))
2311 (+ (islamic-calendar-day-number date);; days so far this year
2312 (* (1- year) 354) ;; days in all non-leap years
2313 (* 11 (/ year 30)) ;; leap days in complete cycles
2314 leap-years-in-cycle ;; leap days this cycle
2315 227014))) ;; days before start of calendar
2317 (defun calendar-islamic-from-absolute (date)
2318 "Compute the Islamic date (month day year) corresponding to absolute DATE.
2319 The absolute date is the number of days elapsed since the (imaginary)
2320 Gregorian date Sunday, December 31, 1 BC."
2321 (if (< date 227015)
2322 (list 0 0 0);; pre-Islamic date
2323 (let* ((approx (/ (- date 227014) 355));; Approximation from below.
2324 (year ;; Search forward from the approximation.
2325 (+ approx
2326 (calendar-sum y approx
2327 (>= date (calendar-absolute-from-islamic
2328 (list 1 1 (1+ y))))
2329 1)))
2330 (month ;; Search forward from Muharram.
2331 (1+ (calendar-sum m 1
2332 (> date
2333 (calendar-absolute-from-islamic
2334 (list m
2335 (islamic-calendar-last-day-of-month
2336 m year)
2337 year)))
2338 1)))
2339 (day ;; Calculate the day by subtraction.
2340 (- date
2341 (1- (calendar-absolute-from-islamic (list month 1 year))))))
2342 (list month day year))))
2344 (defconst calendar-islamic-month-name-array
2345 ["Muharram" "Safar" "Rabi I" "Rabi II" "Jumada I" "Jumada II"
2346 "Rajab" "Sha'ban" "Ramadan" "Shawwal" "Dhu al-Qada" "Dhu al-Hijjah"])
2348 (defun cursor-to-islamic-calendar-date ()
2349 "Show the Islamic calendar equivalent of the date under the cursor."
2350 (interactive)
2351 (let ((calendar-date-display-form
2352 (if european-calendar-style
2353 '(day " " monthname " " year)
2354 '(monthname " " day ", " year)))
2355 (calendar-month-name-array calendar-islamic-month-name-array)
2356 (islamic-date (calendar-islamic-from-absolute
2357 (calendar-absolute-from-gregorian
2358 (or (calendar-cursor-to-date)
2359 (error "Cursor is not on a date!"))))))
2360 (if (< (extract-calendar-year islamic-date) 1)
2361 (message "Date is pre-Islamic")
2362 (message "Islamic date: %s" (calendar-date-string islamic-date nil t)))))
2364 (defun calendar-hebrew-from-absolute (date)
2365 "Compute the Hebrew date (month day year) corresponding to absolute DATE.
2366 The absolute date is the number of days elapsed since the (imaginary)
2367 Gregorian date Sunday, December 31, 1 BC."
2368 (let* ((greg-date (calendar-gregorian-from-absolute date))
2369 (month (aref [9 10 11 12 1 2 3 4 7 7 7 8]
2370 (1- (extract-calendar-month greg-date))))
2371 (day)
2372 (year (+ 3760 (extract-calendar-year greg-date))))
2373 (while (>= date (calendar-absolute-from-hebrew (list 7 1 (1+ year))))
2374 (setq year (1+ year)))
2375 (let ((length (hebrew-calendar-last-month-of-year year)))
2376 (while (> date
2377 (calendar-absolute-from-hebrew
2378 (list month
2379 (hebrew-calendar-last-day-of-month month year)
2380 year)))
2381 (setq month (1+ (% month length)))))
2382 (setq day (1+
2383 (- date (calendar-absolute-from-hebrew (list month 1 year)))))
2384 (list month day year)))
2386 (defun hebrew-calendar-leap-year-p (year)
2387 "t if YEAR is a Hebrew calendar leap year."
2388 (< (% (1+ (* 7 year)) 19) 7))
2390 (defun hebrew-calendar-last-month-of-year (year)
2391 "The last month of the Hebrew calendar YEAR."
2392 (if (hebrew-calendar-leap-year-p year)
2394 12))
2396 (defun hebrew-calendar-last-day-of-month (month year)
2397 "The last day of MONTH in YEAR."
2398 (if (or (memq month (list 2 4 6 10 13))
2399 (and (= month 12) (not (hebrew-calendar-leap-year-p year)))
2400 (and (= month 8) (not (hebrew-calendar-long-heshvan-p year)))
2401 (and (= month 9) (hebrew-calendar-short-kislev-p year)))
2403 30))
2405 (defun hebrew-calendar-elapsed-days (year)
2406 "Number of days elapsed from the Sunday prior to the start of the Hebrew
2407 calendar to the mean conjunction of Tishri of Hebrew YEAR."
2408 (let* ((months-elapsed
2409 (+ (* 235 (/ (1- year) 19));; Months in complete cycles so far.
2410 (* 12 (% (1- year) 19)) ;; Regular months in this cycle
2411 (/ (1+ (* 7 (% (1- year) 19))) 19)));; Leap months this cycle
2412 (parts-elapsed (+ 204 (* 793 (% months-elapsed 1080))))
2413 (hours-elapsed (+ 5
2414 (* 12 months-elapsed)
2415 (* 793 (/ months-elapsed 1080))
2416 (/ parts-elapsed 1080)))
2417 (parts ;; Conjunction parts
2418 (+ (* 1080 (% hours-elapsed 24)) (% parts-elapsed 1080)))
2419 (day ;; Conjunction day
2420 (+ 1 (* 29 months-elapsed) (/ hours-elapsed 24)))
2421 (alternative-day
2422 (if (or (>= parts 19440) ;; If the new moon is at or after midday,
2423 (and (= (% day 7) 2);; ...or is on a Tuesday...
2424 (>= parts 9924) ;; at 9 hours, 204 parts or later...
2425 (not (hebrew-calendar-leap-year-p year)));; of a
2426 ;; common year,
2427 (and (= (% day 7) 1);; ...or is on a Monday...
2428 (>= parts 16789) ;; at 15 hours, 589 parts or later...
2429 (hebrew-calendar-leap-year-p (1- year))));; at the end
2430 ;; of a leap year
2431 ;; Then postpone Rosh HaShanah one day
2432 (1+ day)
2433 ;; Else
2434 day)))
2435 (if ;; If Rosh HaShanah would occur on Sunday, Wednesday, or Friday
2436 (memq (% alternative-day 7) (list 0 3 5))
2437 ;; Then postpone it one (more) day and return
2438 (1+ alternative-day)
2439 ;; Else return
2440 alternative-day)))
2442 (defun hebrew-calendar-days-in-year (year)
2443 "Number of days in Hebrew YEAR."
2444 (- (hebrew-calendar-elapsed-days (1+ year))
2445 (hebrew-calendar-elapsed-days year)))
2447 (defun hebrew-calendar-long-heshvan-p (year)
2448 "t if Heshvan is long in Hebrew YEAR."
2449 (= (% (hebrew-calendar-days-in-year year) 10) 5))
2451 (defun hebrew-calendar-short-kislev-p (year)
2452 "t if Kislev is short in Hebrew YEAR."
2453 (= (% (hebrew-calendar-days-in-year year) 10) 3))
2455 (defun calendar-absolute-from-hebrew (date)
2456 "Absolute date of Hebrew DATE.
2457 The absolute date is the number of days elapsed since the (imaginary)
2458 Gregorian date Sunday, December 31, 1 BC."
2459 (let* ((month (extract-calendar-month date))
2460 (day (extract-calendar-day date))
2461 (year (extract-calendar-year date)))
2462 (+ day ;; Days so far this month.
2463 (if (< month 7);; before Tishri
2464 ;; Then add days in prior months this year before and after Nisan
2465 (+ (calendar-sum
2466 m 7 (<= m (hebrew-calendar-last-month-of-year year))
2467 (hebrew-calendar-last-day-of-month m year))
2468 (calendar-sum
2469 m 1 (< m month)
2470 (hebrew-calendar-last-day-of-month m year)))
2471 ;; Else add days in prior months this year
2472 (calendar-sum
2473 m 7 (< m month)
2474 (hebrew-calendar-last-day-of-month m year)))
2475 (hebrew-calendar-elapsed-days year);; Days in prior years.
2476 -1373429))) ;; Days elapsed before absolute date 1.
2478 (defconst calendar-hebrew-month-name-array-common-year
2479 ["Nisan" "Iyar" "Sivan" "Tammuz" "Av" "Elul" "Tishri"
2480 "Heshvan" "Kislev" "Teveth" "Shevat" "Adar"])
2482 (defconst calendar-hebrew-month-name-array-leap-year
2483 ["Nisan" "Iyar" "Sivan" "Tammuz" "Av" "Elul" "Tishri"
2484 "Heshvan" "Kislev" "Teveth" "Shevat" "Adar I" "Adar II"])
2486 (defun cursor-to-hebrew-calendar-date ()
2487 "Show the Hebrew calendar equivalent of the date under the cursor."
2488 (interactive)
2489 (let* ((calendar-date-display-form
2490 (if european-calendar-style
2491 '(day " " monthname " " year)
2492 '(monthname " " day ", " year)))
2493 (hebrew-date (calendar-hebrew-from-absolute
2494 (calendar-absolute-from-gregorian
2495 (or (calendar-cursor-to-date)
2496 (error "Cursor is not on a date!")))))
2497 (calendar-month-name-array
2498 (if (hebrew-calendar-leap-year-p (extract-calendar-year hebrew-date))
2499 calendar-hebrew-month-name-array-leap-year
2500 calendar-hebrew-month-name-array-common-year)))
2501 (message "Hebrew date: %s" (calendar-date-string hebrew-date nil t))))
2503 (defun hebrew-calendar-yahrzeit (death-date year)
2504 "Absolute date of the anniversary of Hebrew DEATH-DATE in Hebrew YEAR."
2505 (let* ((death-day (extract-calendar-day death-date))
2506 (death-month (extract-calendar-month death-date))
2507 (death-year (extract-calendar-year death-date)))
2508 (cond
2509 ;; If it's Heshvan 30 it depends on the first anniversary; if
2510 ;; that was not Heshvan 30, use the day before Kislev 1.
2511 ((and (= death-month 8)
2512 (= death-day 30)
2513 (not (hebrew-calendar-long-heshvan-p (1+ death-year))))
2514 (1- (calendar-absolute-from-hebrew (list 9 1 year))))
2515 ;; If it's Kislev 30 it depends on the first anniversary; if
2516 ;; that was not Kislev 30, use the day before Teveth 1.
2517 ((and (= death-month 9)
2518 (= death-day 30)
2519 (hebrew-calendar-short-kislev-p (1+ death-year)))
2520 (1- (calendar-absolute-from-hebrew (list 10 1 year))))
2521 ;; If it's Adar II, use the same day in last month of
2522 ;; year (Adar or Adar II).
2523 ((= death-month 13)
2524 (calendar-absolute-from-hebrew
2525 (list (last-month-of-hebrew-year year) death-day year)))
2526 ;; If it's the 30th in Adar I and $year$ is not a leap year
2527 ;; (so Adar has only 29 days), use the last day in Shevat.
2528 ((and (= death-day 30)
2529 (= death-month 12)
2530 (not (hebrew-calendar-leap-year-p death-year)))
2531 (calendar-absolute-from-hebrew (list 11 30 year)))
2532 ;; In all other cases, use the normal anniversary of the date of death.
2533 (t (calendar-absolute-from-hebrew
2534 (list death-month death-day year))))))
2536 (defun list-yahrzeit-dates (death-date start-year end-year)
2537 "List of Yahrzeit dates for *Gregorian* DEATH-DATE
2538 from START-YEAR to END-YEAR. When called interactively
2539 the date of death is taken from the cursor in the calendar window."
2540 (interactive
2541 (let* ((death-date (calendar-cursor-to-date))
2542 (death-year (extract-calendar-year death-date))
2543 (start-year (calendar-read
2544 (format "Starting year of Yahrzeit table (>%d): "
2545 death-year)
2546 '(lambda (x) (> x death-year))
2547 (int-to-string (1+ death-year))))
2548 (end-year (calendar-read
2549 (format "Ending year of Yahrzeit table (>=%d): "
2550 start-year)
2551 '(lambda (x) (>= x start-year)))))
2552 (list death-date start-year end-year)))
2553 (message "Computing yahrzeits...")
2554 (let* ((yahrzeit-buffer "*Yahrzeits*")
2555 (h-date (calendar-hebrew-from-absolute
2556 (calendar-absolute-from-gregorian death-date)))
2557 (h-month (extract-calendar-month h-date))
2558 (h-day (extract-calendar-day h-date))
2559 (h-year (extract-calendar-year h-date)))
2560 (set-buffer (get-buffer-create yahrzeit-buffer))
2561 (setq buffer-read-only nil)
2562 (setq mode-line-format
2563 (format "------Yahrzeit dates for %s = %s%%-"
2564 (calendar-date-string death-date)
2565 (let ((calendar-month-name-array
2566 (if (hebrew-calendar-leap-year-p h-year)
2567 calendar-hebrew-month-name-array-leap-year
2568 calendar-hebrew-month-name-array-common-year))
2569 (calendar-date-display-form
2570 (if european-calendar-style
2571 '(day " " monthname " " year)
2572 '(monthname " " day ", " year))))
2573 (calendar-date-string h-date nil t))))
2574 (erase-buffer)
2575 (goto-char (point-min))
2576 (calendar-for-loop i from start-year to end-year do
2577 (insert
2578 (calendar-date-string
2579 (calendar-gregorian-from-absolute
2580 (hebrew-calendar-yahrzeit
2581 h-date
2582 (extract-calendar-year
2583 (calendar-hebrew-from-absolute
2584 (calendar-absolute-from-gregorian (list 1 1 i))))))) "\n"))
2585 (goto-char (point-min))
2586 (set-buffer-modified-p nil)
2587 (setq buffer-read-only t)
2588 (display-buffer yahrzeit-buffer)
2589 (message "Computing yahrzeits...done")))
2591 (defun french-calendar-leap-year-p (year)
2592 "True if YEAR is a leap year on the French Revolutionary calendar.
2593 For Gregorian years 1793 to 1805, the years of actual operation of the
2594 calendar, uses historical practice based on equinoxes is followed (years 3, 7,
2595 and 11 were leap years; 15 and 20 would have been leap years). For later
2596 years uses the proposed rule of Romme (never adopted)--leap years fall every
2597 four years except century years not divisible 400 and century years that are
2598 multiples of 4000."
2599 (or (memq year '(3 7 11));; Actual practice--based on equinoxes
2600 (memq year '(15 20)) ;; Anticipated practice--based on equinoxes
2601 (and (> year 20) ;; Romme's proposal--never adopted
2602 (zerop (% year 4))
2603 (not (memq (% year 400) '(100 200 300)))
2604 (not (zerop (% year 4000))))))
2606 (defun french-calendar-last-day-of-month (month year)
2607 "Last day of MONTH, YEAR on the French Revolutionary calendar.
2608 The 13th month is not really a month, but the 5 (6 in leap years) day period of
2609 `sansculottides' at the end of the year."
2610 (if (< month 13)
2612 (if (french-calendar-leap-year-p year)
2614 5)))
2616 (defun calendar-absolute-from-french (date)
2617 "Absolute date of French Revolutionary DATE.
2618 The absolute date is the number of days elapsed since the (imaginary)
2619 Gregorian date Sunday, December 31, 1 BC."
2620 (let ((month (extract-calendar-month date))
2621 (day (extract-calendar-day date))
2622 (year (extract-calendar-year date)))
2623 (+ (* 365 (1- year));; Days in prior years
2624 ;; Leap days in prior years
2625 (if (< year 20)
2626 (/ year 4);; Actual and anticipated practice (years 3, 7, 11, 15)
2627 ;; Romme's proposed rule (using the Principle of Inclusion/Exclusion)
2628 (+ (/ (1- year) 4);; Luckily, there were 4 leap years before year 20
2629 (- (/ (1- year) 100))
2630 (/ (1- year) 400)
2631 (- (/ (1- year) 4000))))
2632 (* 30 (1- month));; Days in prior months this year
2633 day;; Days so far this month
2634 654414)));; Days before start of calendar (September 22, 1792).
2636 (defun calendar-french-from-absolute (date)
2637 "Compute the French Revolutionary date (month day year) corresponding to
2638 absolute DATE. The absolute date is the number of days elapsed since the
2639 (imaginary) Gregorian date Sunday, December 31, 1 BC."
2640 (if (< date 654415)
2641 (list 0 0 0);; pre-French Revolutionary date
2642 (let* ((approx (/ (- date 654414) 366));; Approximation from below.
2643 (year ;; Search forward from the approximation.
2644 (+ approx
2645 (calendar-sum y approx
2646 (>= date (calendar-absolute-from-french (list 1 1 (1+ y))))
2647 1)))
2648 (month ;; Search forward from Vendemiaire.
2649 (1+ (calendar-sum m 1
2650 (> date
2651 (calendar-absolute-from-french
2652 (list m
2653 (french-calendar-last-day-of-month m year)
2654 year)))
2655 1)))
2656 (day ;; Calculate the day by subtraction.
2657 (- date
2658 (1- (calendar-absolute-from-french (list month 1 year))))))
2659 (list month day year))))
2661 (defun cursor-to-french-calendar-date ()
2662 "Show the French Revolutionary calendar equivalent of the date under the
2663 cursor."
2664 (interactive)
2665 (let* ((french-date (calendar-french-from-absolute
2666 (calendar-absolute-from-gregorian
2667 (or (calendar-cursor-to-date)
2668 (error "Cursor is not on a date!")))))
2669 (y (extract-calendar-year french-date))
2670 (m (extract-calendar-month french-date))
2671 (d (extract-calendar-day french-date)))
2672 (if (< y 1)
2673 (message "Date is pre-French Revolution")
2674 (if (= m 13)
2675 (message "Jour %s de l'Annee %d de la Revolution"
2676 (aref french-calendar-special-days-array (1- d))
2678 (message "Decade %s, %s de %s de l'Annee %d de la Revolution"
2679 (make-string (1+ (/ (1- d) 10)) ?I)
2680 (aref french-calendar-day-name-array (% (1- d) 10))
2681 (aref french-calendar-month-name-array (1- m))
2682 y)))))
2684 (defconst french-calendar-month-name-array
2685 ["Vendemiaire" "Brumaire" "Frimaire" "Nivose" "Pluviose" "Ventose" "Germinal"
2686 "Floreal" "Prairial" "Messidor" "Thermidor" "Fructidor"])
2687 ;; Very loosely translated as
2688 ;; Slippy, Nippy, Drippy, Freezy, Wheezy, Sneezy,
2689 ;; Showery, Flowery, Bowery, Heaty, Wheaty, Sweety.
2691 (defconst french-calendar-day-name-array
2692 ["Primidi" "Duodi" "Tridi" "Quartidi" "Quintidi" "Sextidi" "Septidi"
2693 "Octidi" "Nonidi" "Decadi"])
2695 (defconst french-calendar-special-days-array
2696 ["de la Vertu" "du Genie" "du Labour" "de la Raison" "de la Recompense"
2697 "de la Revolution"])
2699 (provide 'calendar)
2701 ;;; calendar.el ends here