comparison lisp/calendar/calendar.el @ 628:ab209803a494

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