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