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