]> code.delx.au - gnu-emacs/blob - lisp/calendar/calendar.el
Add doc strings CORRECTLY!
[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 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 other-calendars-buffer "*Other Calendars*"
1113 "Name of the buffer used for the display of date on other calendars.")
1114
1115 (defconst lunar-phases-buffer "*Phases of Moon*"
1116 "Name of the buffer used for the lunar phases.")
1117
1118 (defmacro increment-calendar-month (mon yr n)
1119 "Move the variables MON and YR to the month and year by N months.
1120 Forward if N is positive or backward if N is negative."
1121 (` (let (( macro-y (+ (* (, yr) 12) (, mon) -1 (, n) )))
1122 (setq (, mon) (1+ (% macro-y 12) ))
1123 (setq (, yr) (/ macro-y 12)))))
1124
1125 (defmacro calendar-for-loop (var from init to final do &rest body)
1126 "Execute a for loop."
1127 (` (let (( (, var) (1- (, init)) ))
1128 (while (>= (, final) (setq (, var) (1+ (, var))))
1129 (,@ body)))))
1130
1131 (defmacro calendar-sum (index initial condition expression)
1132 "For INDEX = INITIAL et seq, as long as CONDITION holds, sum EXPRESSION."
1133 (` (let (( (, index) (, initial))
1134 (sum 0))
1135 (while (, condition)
1136 (setq sum (+ sum (, expression) ))
1137 (setq (, index) (1+ (, index))))
1138 sum)))
1139
1140 ;; The following are in-line for speed; they can be called thousands of times
1141 ;; when looking up holidays or processing the diary. Here, for example, are
1142 ;; the numbers of calls to calendar/diary/holiday functions in preparing the
1143 ;; fancy diary display, for a moderately complex diary file, with functions
1144 ;; used instead of macros. There were a total of 10000 such calls:
1145 ;;
1146 ;; 1934 extract-calendar-month
1147 ;; 1852 extract-calendar-year
1148 ;; 1819 extract-calendar-day
1149 ;; 845 calendar-leap-year-p
1150 ;; 837 calendar-day-number
1151 ;; 775 calendar-absolute-from-gregorian
1152 ;; 346 calendar-last-day-of-month
1153 ;; 286 hebrew-calendar-last-day-of-month
1154 ;; 188 hebrew-calendar-leap-year-p
1155 ;; 180 hebrew-calendar-elapsed-days
1156 ;; 163 hebrew-calendar-last-month-of-year
1157 ;; 66 calendar-date-compare
1158 ;; 65 hebrew-calendar-days-in-year
1159 ;; 60 calendar-absolute-from-julian
1160 ;; 50 calendar-absolute-from-hebrew
1161 ;; 43 calendar-date-equal
1162 ;; 38 calendar-gregorian-from-absolute
1163 ;; .
1164 ;; .
1165 ;; .
1166 ;;
1167 ;; The use of these seven macros eliminates the overhead of 92% of the function
1168 ;; calls; it's faster this way.
1169
1170 (defsubst extract-calendar-month (date)
1171 "Extract the month part of DATE which has the form (month day year)."
1172 (car date))
1173
1174 (defsubst extract-calendar-day (date)
1175 "Extract the day part of DATE which has the form (month day year)."
1176 (car (cdr date)))
1177
1178 (defsubst extract-calendar-year (date)
1179 "Extract the year part of DATE which has the form (month day year)."
1180 (car (cdr (cdr date))))
1181
1182 (defsubst calendar-leap-year-p (year)
1183 "Returns t if YEAR is a Gregorian leap year."
1184 (and (zerop (% year 4))
1185 (or (not (zerop (% year 100)))
1186 (zerop (% year 400)))))
1187
1188 ;; The foregoing is a bit faster, but not as clear as the following:
1189 ;;
1190 ;;(defsubst calendar-leap-year-p (year)
1191 ;; "Returns t if YEAR is a Gregorian leap year."
1192 ;; (or
1193 ;; (and (= (% year 4) 0)
1194 ;; (/= (% year 100) 0))
1195 ;; (= (% year 400) 0)))
1196
1197 (defsubst calendar-last-day-of-month (month year)
1198 "The last day in MONTH during YEAR."
1199 (if (and (= month 2) (calendar-leap-year-p year))
1200 29
1201 (aref [31 28 31 30 31 30 31 31 30 31 30 31] (1- month))))
1202
1203 ;; An explanation of the calculation can be found in PascAlgorithms by
1204 ;; Edward and Ruth Reingold, Scott-Foresman/Little, Brown, 1988.
1205
1206 (defsubst calendar-day-number (date)
1207 "Return the day number within the year of the date DATE.
1208 For example, (calendar-day-number '(1 1 1987)) returns the value 1,
1209 while (calendar-day-number '(12 31 1980)) returns 366."
1210 (let* ((month (extract-calendar-month date))
1211 (day (extract-calendar-day date))
1212 (year (extract-calendar-year date))
1213 (day-of-year (+ day (* 31 (1- month)))))
1214 (if (> month 2)
1215 (progn
1216 (setq day-of-year (- day-of-year (/ (+ 23 (* 4 month)) 10)))
1217 (if (calendar-leap-year-p year)
1218 (setq day-of-year (1+ day-of-year)))))
1219 day-of-year))
1220
1221 (defsubst calendar-absolute-from-gregorian (date)
1222 "The number of days elapsed between the Gregorian date 12/31/1 BC and DATE.
1223 The Gregorian date Sunday, December 31, 1 BC is imaginary."
1224 (let ((prior-years (1- (extract-calendar-year date))))
1225 (+ (calendar-day-number date);; Days this year
1226 (* 365 prior-years);; + Days in prior years
1227 (/ prior-years 4);; + Julian leap years
1228 (- (/ prior-years 100));; - century years
1229 (/ prior-years 400))));; + Gregorian leap years
1230
1231 (autoload 'calendar-goto-today "cal-move"
1232 "Reposition the calendar window so the current date is visible."
1233 t)
1234
1235 (autoload 'calendar-forward-month "cal-move"
1236 "Move the cursor forward ARG months."
1237 t)
1238
1239 (autoload 'calendar-forward-year "cal-move"
1240 "Move the cursor forward by ARG years."
1241 t)
1242
1243 (autoload 'calendar-backward-month "cal-move"
1244 "Move the cursor backward by ARG months."
1245 t)
1246
1247 (autoload 'calendar-backward-year "cal-move"
1248 "Move the cursor backward ARG years."
1249 t)
1250
1251 (autoload 'scroll-calendar-left "cal-move"
1252 "Scroll the displayed calendar left by ARG months."
1253 t)
1254
1255 (autoload 'scroll-calendar-right "cal-move"
1256 "Scroll the displayed calendar window right by ARG months."
1257 t)
1258
1259 (autoload 'scroll-calendar-left-three-months "cal-move"
1260 "Scroll the displayed calendar window left by 3*ARG months."
1261 t)
1262
1263 (autoload 'scroll-calendar-right-three-months "cal-move"
1264 "Scroll the displayed calendar window right by 3*ARG months."
1265 t)
1266
1267 (autoload 'calendar-cursor-to-nearest-date "cal-move"
1268 "Move the cursor to the closest date."
1269 t)
1270
1271 (autoload 'calendar-forward-day "cal-move"
1272 "Move the cursor forward ARG days."
1273 t)
1274
1275 (autoload 'calendar-backward-day "cal-move"
1276 "Move the cursor back ARG days."
1277 t)
1278
1279 (autoload 'calendar-forward-week "cal-move"
1280 "Move the cursor forward ARG weeks."
1281 t)
1282
1283 (autoload 'calendar-backward-week "cal-move"
1284 "Move the cursor back ARG weeks."
1285 t)
1286
1287 (autoload 'calendar-beginning-of-week "cal-move"
1288 "Move the cursor back ARG calendar-week-start-day's."
1289 t)
1290
1291 (autoload 'calendar-end-of-week "cal-move"
1292 "Move the cursor forward ARG calendar-week-start-day+6's."
1293 t)
1294
1295 (autoload 'calendar-beginning-of-month "cal-move"
1296 "Move the cursor backward ARG month beginnings."
1297 t)
1298
1299 (autoload 'calendar-end-of-month "cal-move"
1300 "Move the cursor forward ARG month ends."
1301 t)
1302
1303 (autoload 'calendar-beginning-of-year "cal-move"
1304 "Move the cursor backward ARG year beginnings."
1305 t)
1306
1307 (autoload 'calendar-end-of-year "cal-move"
1308 "Move the cursor forward ARG year beginnings."
1309 t)
1310
1311 (autoload 'calendar-cursor-to-visible-date "cal-move"
1312 "Move the cursor to DATE that is on the screen."
1313 t)
1314
1315 (autoload 'calendar-goto-date "cal-move"
1316 "Move cursor to DATE."
1317 t)
1318
1319 (autoload 'calendar-one-frame-setup "cal-x"
1320 "Start calendar and display it in a dedicated frame together with the diary.")
1321
1322 (autoload 'calendar-two-frame-setup "cal-x"
1323 "Start calendar and diary in separate, dedicated frames.")
1324
1325 ;;;###autoload
1326 (defvar calendar-setup nil
1327 "The frame set up of the calendar.
1328 The choices are `one-frame' (calendar and diary together in one separate,
1329 dedicated frame) or `two-frames' (calendar and diary in separate, dedicated
1330 frames); with any other value the current frame is used.")
1331
1332 ;;;###autoload
1333 (defun calendar (&optional arg)
1334 "Choose between the one frame, two frame, or basic calendar displays.
1335 The original function `calendar' has been renamed `calendar-basic-setup'."
1336 (interactive "P")
1337 (cond ((equal calendar-setup 'one-frame) (calendar-one-frame-setup arg))
1338 ((equal calendar-setup 'two-frames) (calendar-two-frame-setup arg))
1339 (t (calendar-basic-setup arg))))
1340
1341 (defun calendar-basic-setup (&optional arg)
1342 "Display a three-month calendar in another window.
1343 The three months appear side by side, with the current month in the middle
1344 surrounded by the previous and next months. The cursor is put on today's date.
1345
1346 If called with an optional prefix argument, prompts for month and year.
1347
1348 This function is suitable for execution in a .emacs file; appropriate setting
1349 of the variable `view-diary-entries-initially' will cause the diary entries for
1350 the current date to be displayed in another window. The value of the variable
1351 `number-of-diary-entries' controls the number of days of diary entries
1352 displayed upon initial display of the calendar.
1353
1354 An optional prefix argument ARG causes the calendar displayed to be ARG
1355 months in the future if ARG is positive or in the past if ARG is negative;
1356 in this case the cursor goes on the first day of the month.
1357
1358 Once in the calendar window, future or past months can be moved into view.
1359 Arbitrary months can be displayed, or the calendar can be scrolled forward
1360 or backward.
1361
1362 The cursor can be moved forward or backward by one day, one week, one month,
1363 or one year. All of these commands take prefix arguments which, when negative,
1364 cause movement in the opposite direction. For convenience, the digit keys
1365 and the minus sign are automatically prefixes. The window is replotted as
1366 necessary to display the desired date.
1367
1368 Diary entries can be marked on the calendar or displayed in another window.
1369
1370 Use M-x describe-mode for details of the key bindings in the calendar window.
1371
1372 The Gregorian calendar is assumed.
1373
1374 After loading the calendar, the hooks given by the variable
1375 `calendar-load-hook' are run. This is the place to add key bindings to the
1376 calendar-mode-map.
1377
1378 After preparing the calendar window initially, the hooks given by the variable
1379 `initial-calendar-window-hook' are run.
1380
1381 The hooks given by the variable `today-visible-calendar-hook' are run
1382 every time the calendar window gets scrolled, if the current date is visible
1383 in the window. If it is not visible, the hooks given by the variable
1384 `today-invisible-calendar-hook' are run. Thus, for example, setting
1385 `today-visible-calendar-hook' to 'calendar-star-date will cause today's date
1386 to be replaced by asterisks to highlight it whenever it is in the window."
1387 (interactive "P")
1388 (set-buffer (get-buffer-create calendar-buffer))
1389 (calendar-mode)
1390 (let* ((pop-up-windows t)
1391 (split-height-threshold 1000)
1392 (date (if arg
1393 (calendar-read-date t)
1394 (calendar-current-date)))
1395 (month (extract-calendar-month date))
1396 (year (extract-calendar-year date)))
1397 (pop-to-buffer calendar-buffer)
1398 (increment-calendar-month month year (- calendar-offset))
1399 (generate-calendar-window month year)
1400 (if (and view-diary-entries-initially (calendar-date-is-visible-p date))
1401 (view-diary-entries
1402 (if (vectorp number-of-diary-entries)
1403 (aref number-of-diary-entries (calendar-day-of-week date))
1404 number-of-diary-entries))))
1405 (let* ((diary-buffer (get-file-buffer diary-file))
1406 (diary-window (if diary-buffer (get-buffer-window diary-buffer)))
1407 (split-height-threshold (if diary-window 2 1000)))
1408 (if view-calendar-holidays-initially
1409 (list-calendar-holidays)))
1410 (run-hooks 'initial-calendar-window-hook))
1411
1412 (autoload 'view-diary-entries "diary-lib"
1413 "Prepare and display a buffer with diary entries.
1414 Searches your diary file for entries that match ARG days starting with
1415 the date indicated by the cursor position in the displayed three-month
1416 calendar."
1417 t)
1418
1419 (autoload 'calendar-sunrise-sunset "solar"
1420 "Local time of sunrise and sunset for date under cursor."
1421 t)
1422
1423 (autoload 'calendar-phases-of-moon "lunar"
1424 "Create a buffer of the phases of the moon for the current calendar window."
1425 t)
1426
1427 (autoload 'calendar-print-french-date "cal-french"
1428 "Show the French Revolutionary calendar equivalent of the date under the cursor."
1429 t)
1430
1431 (autoload 'calendar-goto-french-date "cal-french"
1432 "Move cursor to French Revolutionary date."
1433 t)
1434
1435 (autoload 'calendar-french-date-string "cal-french"
1436 "String of French Revolutionary date of Gregorian date."
1437 t)
1438
1439 (autoload 'calendar-mayan-date-string "cal-mayan"
1440 "String of Mayan date of Gregorian date."
1441 t)
1442
1443 (autoload 'calendar-print-mayan-date "cal-mayan"
1444 "Show the Mayan long count, Tzolkin, and Haab equivalents of the date under the cursor."
1445 t)
1446
1447 (autoload 'calendar-goto-mayan-long-count-date "cal-mayan"
1448 "Move cursor to Mayan long count date."
1449 t)
1450
1451 (autoload 'calendar-next-haab-date "cal-mayan"
1452 "Move cursor to next instance of Mayan Haab date."
1453 t)
1454
1455 (autoload 'calendar-previous-haab-date "cal-mayan"
1456 "Move cursor to previous instance of Mayan Haab date."
1457 t)
1458
1459 (autoload 'calendar-next-tzolkin-date "cal-mayan"
1460 "Move cursor to next instance of Mayan Tzolkin date."
1461 t)
1462
1463 (autoload 'calendar-previous-tzolkin-date "cal-mayan"
1464 "Move cursor to previous instance of Mayan Tzolkin date."
1465 t)
1466
1467 (autoload 'calendar-next-calendar-round-date "cal-mayan"
1468 "Move cursor to next instance of Mayan Haab/Tzolkin combination."
1469 t)
1470
1471 (autoload 'calendar-previous-calendar-round-date "cal-mayan"
1472 "Move cursor to previous instance of Mayan Haab/Tzolkin combination."
1473 t)
1474
1475 (autoload 'calendar-goto-chinese-date "cal-china"
1476 "Move cursor to Chinese date."
1477 t)
1478
1479 (autoload 'calendar-print-chinese-date "cal-china"
1480 "Show the Chinese date equivalents of date."
1481 t)
1482
1483 (autoload 'calendar-chinese-date-string "cal-china"
1484 "String of Chinese date of Gregorian date."
1485 t)
1486
1487 (autoload 'calendar-absolute-from-astro
1488 "Absolute date of astronomical (Julian) day number D."
1489 "cal-julian")
1490
1491 (autoload 'calendar-astro-from-absolute "cal-julian"
1492 "Astronomical (Julian) day number of absolute date D.")
1493
1494 (autoload 'calendar-astro-date-string "cal-julian"
1495 "String of astronomical (Julian) day number of Gregorian date."
1496 t)
1497
1498 (autoload 'calendar-goto-astro-date "cal-julian"
1499 "Move cursor to astronomical (Julian) day number."
1500 t)
1501
1502 (autoload 'calendar-julian-from-absolute "cal-julian"
1503 "Compute the Julian (month day year) corresponding to the absolute DATE.
1504 The absolute date is the number of days elapsed since the (imaginary)
1505 Gregorian date Sunday, December 31, 1 BC.")
1506
1507 (autoload 'calendar-goto-julian-date "cal-julian"
1508 "Move cursor to Julian DATE; echo Julian date unless NOECHO is t."
1509 t)
1510
1511 (autoload 'calendar-print-julian-date "cal-julian"
1512 "Show the Julian calendar equivalent of the date under the cursor."
1513 t)
1514
1515 (autoload 'calendar-julian-date-string "cal-julian"
1516 "String of Julian date of Gregorian DATE.
1517 Defaults to today's date if DATE is not given.
1518 Driven by the variable `calendar-date-display-form'.")
1519
1520 (autoload 'calendar-goto-iso-date "cal-iso"
1521 "Move cursor to ISO date."
1522 t)
1523
1524 (autoload 'calendar-print-iso-date "cal-iso"
1525 "Show the ISO date equivalents of date."
1526 t)
1527
1528 (autoload 'calendar-iso-date-string "cal-iso"
1529 "String of ISO date of Gregorian date."
1530 t)
1531
1532 (autoload 'calendar-print-islamic-date "cal-islam"
1533 "Show the Islamic date equivalents of date."
1534 t)
1535
1536 (autoload 'calendar-islamic-date-string "cal-islam"
1537 "String of Islamic date of Gregorian date."
1538 t)
1539
1540 (autoload 'calendar-goto-hebrew-date "cal-hebrew"
1541 "Move cursor to Hebrew date date."
1542 t)
1543
1544 (autoload 'calendar-print-hebrew-date "cal-hebrew"
1545 "Show the Hebrew date equivalents of date."
1546 t)
1547
1548 (autoload 'calendar-hebrew-date-string "cal-hebrew"
1549 "String of Hebrew date of Gregorian date."
1550 t)
1551
1552 (autoload 'calendar-goto-coptic-date "cal-coptic"
1553 "Move cursor to Coptic date date."
1554 t)
1555
1556 (autoload 'calendar-print-coptic-date "cal-coptic"
1557 "Show the Coptic date equivalents of date."
1558 t)
1559
1560 (autoload 'calendar-coptic-date-string "cal-coptic"
1561 "String of Coptic date of Gregorian date."
1562 t)
1563
1564 (autoload 'calendar-goto-ethiopic-date "cal-coptic"
1565 "Move cursor to Ethiopic date date."
1566 t)
1567
1568 (autoload 'calendar-print-ethiopic-date "cal-coptic"
1569 "Show the Ethiopic date equivalents of date."
1570 t)
1571
1572 (autoload 'calendar-ethiopic-date-string "cal-coptic"
1573 "String of Ethiopic date of Gregorian date."
1574 t)
1575
1576 (autoload 'calendar-goto-persian-date "cal-persia"
1577 "Move cursor to Persian date date."
1578 t)
1579
1580 (autoload 'calendar-print-persian-date "cal-persia"
1581 "Show the Persian date equivalents of date."
1582 t)
1583
1584 (autoload 'calendar-persian-date-string "cal-persia"
1585 "String of Persian date of Gregorian date."
1586 t)
1587
1588 (autoload 'show-all-diary-entries "diary-lib"
1589 "Show all of the diary entries in the diary file.
1590 This function gets rid of the selective display of the diary file so that
1591 all entries, not just some, are visible. If there is no diary buffer, one
1592 is created."
1593 t)
1594
1595 (autoload 'mark-diary-entries "diary-lib"
1596 "Mark days in the calendar window that have diary entries.
1597 Each entry in diary file visible in the calendar window is marked."
1598 t)
1599
1600 (autoload 'make-diary-entry "diary-lib"
1601 "Insert a diary entry STRING which may be NONMARKING in FILE."
1602 t)
1603
1604 (autoload 'insert-diary-entry "diary-lib"
1605 "Insert a diary entry for the date indicated by point."
1606 t)
1607
1608 (autoload 'insert-weekly-diary-entry "diary-lib"
1609 "Insert a weekly diary entry for the day of the week indicated by point."
1610 t)
1611
1612
1613 (autoload 'insert-monthly-diary-entry "diary-lib"
1614 "Insert a monthly diary entry for the day of the month indicated by point."
1615 t)
1616
1617 (autoload 'insert-yearly-diary-entry "diary-lib"
1618 "Insert an annual diary entry for the day of the year indicated by point."
1619 t)
1620
1621 (autoload 'insert-anniversary-diary-entry "diary-lib"
1622 "Insert an anniversary diary entry for the date indicated by point."
1623 t)
1624
1625 (autoload 'insert-block-diary-entry "diary-lib"
1626 "Insert a block diary entry for the dates indicated by point and mark."
1627 t)
1628
1629 (autoload 'insert-cyclic-diary-entry "diary-lib"
1630 "Insert a cyclic diary entry starting at the date indicated by point."
1631 t)
1632
1633 (autoload 'insert-hebrew-diary-entry "cal-hebrew"
1634 "Insert a diary entry for the Hebrew date corresponding to the date
1635 indicated by point."
1636 t)
1637
1638 (autoload 'insert-monthly-hebrew-diary-entry "cal-hebrew"
1639 "Insert a monthly diary entry for the day of the Hebrew month corresponding
1640 to the date indicated by point."
1641 t)
1642
1643 (autoload 'insert-yearly-hebrew-diary-entry "cal-hebrew"
1644 "Insert an annual diary entry for the day of the Hebrew year corresponding
1645 to the date indicated by point."
1646 t)
1647
1648 (autoload 'insert-islamic-diary-entry "cal-islam"
1649 "Insert a diary entry for the Islamic date corresponding to the date
1650 indicated by point."
1651 t)
1652
1653 (autoload 'insert-monthly-islamic-diary-entry "cal-islam"
1654 "Insert a monthly diary entry for the day of the Islamic month corresponding
1655 to the date indicated by point."
1656 t)
1657
1658 (autoload 'insert-yearly-islamic-diary-entry "cal-islam"
1659 "Insert an annual diary entry for the day of the Islamic year corresponding
1660 to the date indicated by point."
1661 t)
1662
1663 (autoload 'list-calendar-holidays "holidays"
1664 "Create a buffer containing the holidays for the current calendar window.
1665 The holidays are those in the list `calendar-notable-days'. Returns t if any
1666 holidays are found, nil if not."
1667 t)
1668
1669 (autoload 'cal-tex-cursor-month "cal-tex"
1670 "Make a buffer with LaTeX commands for the month cursor is on.
1671 Optional prefix argument specifies number of months to be produced.
1672 Calendar is condensed onto one page.")
1673
1674 (autoload 'cal-tex-cursor-month-landscape "cal-tex"
1675 "Make a buffer with LaTeX commands for the month cursor is on.
1676 Optional prefix argument specifies number of months to be produced.")
1677
1678 (autoload 'cal-tex-cursor-day "cal-tex"
1679 "Make a buffer with LaTeX commands for the day cursor is on.")
1680
1681 (autoload 'cal-tex-cursor-week "cal-tex"
1682 "Make a buffer with LaTeX commands for a two-page one-week calendar.
1683 It applies to the week that point is in.
1684 Optional prefix argument specifies number of weeks.
1685 Holidays are included if `cal-tex-holidays' is t.")
1686
1687 (autoload 'cal-tex-cursor-week2 "cal-tex"
1688 "Make a buffer with LaTeX commands for a two-page one-week calendar.
1689 It applies to the week that point is in.
1690 Optional prefix argument specifies number of weeks.
1691 Holidays are included if `cal-tex-holidays' is t.")
1692
1693 (autoload 'cal-tex-cursor-week-iso "cal-tex"
1694 "Make a buffer with LaTeX commands for a one page ISO-style weekly calendar.
1695 Optional prefix argument specifies number of weeks.
1696 Diary entries are included if `cal-tex-diary' is t.
1697 Holidays are included if `cal-tex-holidays' is t.")
1698
1699 (autoload 'cal-tex-cursor-week-monday "cal-tex"
1700 "Make a buffer with LaTeX commands for a two-page one-week calendar.
1701 It applies to the week that point is in, and starts on Monday.
1702 Optional prefix argument specifies number of weeks.
1703 Holidays are included if `cal-tex-holidays' is t.")
1704
1705 (autoload 'cal-tex-cursor-filofax-2week "cal-tex"
1706 "Two-weeks-at-a-glance Filofax style calendar for week indicated by cursor.
1707 Optional prefix argument specifies number of weeks.
1708 Diary entries are included if cal-tex-diary is t.
1709 Holidays are included if `cal-tex-holidays' is t.")
1710
1711 (autoload 'cal-tex-cursor-filofax-week "cal-tex"
1712 "One-week-at-a-glance Filofax style calendar for week indicated by cursor.
1713 Optional prefix argument specifies number of weeks.
1714 Weeks start on Monday.
1715 Diary entries are included if cal-tex-diary is t.
1716 Holidays are included if `cal-tex-holidays' is t.")
1717
1718 (autoload 'cal-tex-cursor-filofax-daily "cal-tex"
1719 "Day-per-page Filofax style calendar for week indicated by cursor.
1720 Optional prefix argument specifies number of weeks. Weeks start on Monday.
1721 Diary entries are included if `cal-tex-diary' is t.
1722 Holidays are included if `cal-tex-holidays' is t.")
1723
1724 (autoload 'cal-tex-cursor-year "cal-tex"
1725 "Make a buffer with LaTeX commands for a year's calendar.
1726 Optional prefix argument specifies number of years.")
1727
1728 (autoload 'cal-tex-cursor-year-landscape "cal-tex"
1729 "Make a buffer with LaTeX commands for a year's calendar (landscape).
1730 Optional prefix argument specifies number of years.")
1731
1732 (autoload 'cal-tex-cursor-filofax-year "cal-tex"
1733 "Make a buffer with LaTeX commands for a year's calendar (Filofax).
1734 Optional prefix argument specifies number of years.")
1735
1736 (autoload 'mark-calendar-holidays "holidays"
1737 "Mark notable days in the calendar window."
1738 t)
1739
1740 (autoload 'calendar-cursor-holidays "holidays"
1741 "Find holidays for the date specified by the cursor in the calendar window."
1742 t)
1743
1744 (defun generate-calendar-window (&optional mon yr)
1745 "Generate the calendar window for the current date.
1746 Or, for optional MON, YR."
1747 (let* ((buffer-read-only nil)
1748 (today (calendar-current-date))
1749 (month (extract-calendar-month today))
1750 (day (extract-calendar-day today))
1751 (year (extract-calendar-year today))
1752 (today-visible
1753 (or (not mon)
1754 (let ((offset (calendar-interval mon yr month year)))
1755 (and (<= offset 1) (>= offset -1)))))
1756 (day-in-week (calendar-day-of-week today)))
1757 (update-calendar-mode-line)
1758 (if mon
1759 (generate-calendar mon yr)
1760 (generate-calendar month year))
1761 (calendar-cursor-to-visible-date
1762 (if today-visible today (list displayed-month 1 displayed-year)))
1763 (set-buffer-modified-p nil)
1764 (or (one-window-p t)
1765 (/= (frame-width) (window-width))
1766 (shrink-window (- (window-height) 9)))
1767 (sit-for 0)
1768 (and mark-holidays-in-calendar
1769 (mark-calendar-holidays)
1770 (sit-for 0))
1771 (unwind-protect
1772 (if mark-diary-entries-in-calendar (mark-diary-entries))
1773 (if today-visible
1774 (run-hooks 'today-visible-calendar-hook)
1775 (run-hooks 'today-invisible-calendar-hook)))))
1776
1777 (defun generate-calendar (month year)
1778 "Generate a three-month Gregorian calendar centered around MONTH, YEAR."
1779 (if (< (+ month (* 12 (1- year))) 2)
1780 (error "Months before February, 1 AD are not available."))
1781 (setq displayed-month month)
1782 (setq displayed-year year)
1783 (erase-buffer)
1784 (increment-calendar-month month year -1)
1785 (calendar-for-loop i from 0 to 2 do
1786 (generate-calendar-month month year (+ 5 (* 25 i)))
1787 (increment-calendar-month month year 1)))
1788
1789 (defun generate-calendar-month (month year indent)
1790 "Produce a calendar for MONTH, YEAR on the Gregorian calendar.
1791 The calendar is inserted in the buffer starting at the line on which point
1792 is currently located, but indented INDENT spaces. The indentation is done
1793 from the first character on the line and does not disturb the first INDENT
1794 characters on the line."
1795 (let* ((blank-days;; at start of month
1796 (mod
1797 (- (calendar-day-of-week (list month 1 year))
1798 calendar-week-start-day)
1799 7))
1800 (last (calendar-last-day-of-month month year)))
1801 (goto-char (point-min))
1802 (calendar-insert-indented
1803 (calendar-string-spread
1804 (list (format "%s %d" (calendar-month-name month) year)) ? 20)
1805 indent t)
1806 (calendar-insert-indented "" indent);; Go to proper spot
1807 (calendar-for-loop i from 0 to 6 do
1808 (insert (calendar-day-name (mod (+ calendar-week-start-day i) 7)
1809 2 t))
1810 (insert " "))
1811 (calendar-insert-indented "" 0 t);; Force onto following line
1812 (calendar-insert-indented "" indent);; Go to proper spot
1813 ;; Add blank days before the first of the month
1814 (calendar-for-loop i from 1 to blank-days do (insert " "))
1815 ;; Put in the days of the month
1816 (calendar-for-loop i from 1 to last do
1817 (insert (format "%2d " i))
1818 (put-text-property (- (point) 3) (1- (point))
1819 'mouse-face 'highlight)
1820 (and (zerop (mod (+ i blank-days) 7))
1821 (/= i last)
1822 (calendar-insert-indented "" 0 t) ;; Force onto following line
1823 (calendar-insert-indented "" indent)))));; Go to proper spot
1824
1825 (defun calendar-insert-indented (string indent &optional newline)
1826 "Insert STRING at column INDENT.
1827 If the optional parameter NEWLINE is t, leave point at start of next line,
1828 inserting a newline if there was no next line; otherwise, leave point after
1829 the inserted text. Value is always t."
1830 ;; Try to move to that column.
1831 (move-to-column indent)
1832 ;; If line is too short, indent out to that column.
1833 (if (< (current-column) indent)
1834 (indent-to indent))
1835 (insert string)
1836 ;; Advance to next line, if requested.
1837 (if newline
1838 (progn
1839 (end-of-line)
1840 (if (eobp)
1841 (newline)
1842 (forward-line 1))))
1843 t)
1844
1845 (defun redraw-calendar ()
1846 "Redraw the calendar display."
1847 (interactive)
1848 (let ((cursor-date (calendar-cursor-to-date)))
1849 (generate-calendar-window displayed-month displayed-year)
1850 (calendar-cursor-to-visible-date cursor-date)))
1851
1852 (defcustom calendar-debug-sexp nil
1853 "*Turn debugging on when evaluating a sexp in the diary or holiday list."
1854 :type 'boolean
1855 :group 'calendar)
1856
1857 (defvar calendar-mode-map nil)
1858 (if calendar-mode-map
1859 nil
1860 (setq calendar-mode-map (make-sparse-keymap))
1861 (if window-system (require 'cal-menu))
1862 (calendar-for-loop i from 0 to 9 do
1863 (define-key calendar-mode-map (int-to-string i) 'digit-argument))
1864 (let ((l (list 'narrow-to-region 'mark-word 'mark-sexp 'mark-paragraph
1865 'mark-defun 'mark-whole-buffer 'mark-page
1866 'downcase-region 'upcase-region 'kill-region
1867 'copy-region-as-kill 'capitalize-region 'write-region)))
1868 (while l
1869 (substitute-key-definition (car l) 'calendar-not-implemented
1870 calendar-mode-map global-map)
1871 (setq l (cdr l))))
1872 (define-key calendar-mode-map "-" 'negative-argument)
1873 (define-key calendar-mode-map "\C-x>" 'scroll-calendar-right)
1874 (define-key calendar-mode-map [prior] 'scroll-calendar-right-three-months)
1875 (define-key calendar-mode-map "\ev" 'scroll-calendar-right-three-months)
1876 (define-key calendar-mode-map "\C-x<" 'scroll-calendar-left)
1877 (define-key calendar-mode-map [next] 'scroll-calendar-left-three-months)
1878 (define-key calendar-mode-map "\C-v" 'scroll-calendar-left-three-months)
1879 (define-key calendar-mode-map "\C-b" 'calendar-backward-day)
1880 (define-key calendar-mode-map "\C-p" 'calendar-backward-week)
1881 (define-key calendar-mode-map "\e{" 'calendar-backward-month)
1882 (define-key calendar-mode-map "\C-x[" 'calendar-backward-year)
1883 (define-key calendar-mode-map "\C-f" 'calendar-forward-day)
1884 (define-key calendar-mode-map "\C-n" 'calendar-forward-week)
1885 (define-key calendar-mode-map [left] 'calendar-backward-day)
1886 (define-key calendar-mode-map [up] 'calendar-backward-week)
1887 (define-key calendar-mode-map [right] 'calendar-forward-day)
1888 (define-key calendar-mode-map [down] 'calendar-forward-week)
1889 (define-key calendar-mode-map "\e}" 'calendar-forward-month)
1890 (define-key calendar-mode-map "\C-x]" 'calendar-forward-year)
1891 (define-key calendar-mode-map "\C-a" 'calendar-beginning-of-week)
1892 (define-key calendar-mode-map "\C-e" 'calendar-end-of-week)
1893 (define-key calendar-mode-map "\ea" 'calendar-beginning-of-month)
1894 (define-key calendar-mode-map "\ee" 'calendar-end-of-month)
1895 (define-key calendar-mode-map "\e<" 'calendar-beginning-of-year)
1896 (define-key calendar-mode-map "\e>" 'calendar-end-of-year)
1897 (define-key calendar-mode-map "\C-@" 'calendar-set-mark)
1898 ;; Many people are used to typing C-SPC and getting C-@.
1899 (define-key calendar-mode-map [?\C- ] 'calendar-set-mark)
1900 (define-key calendar-mode-map "\C-x\C-x" 'calendar-exchange-point-and-mark)
1901 (define-key calendar-mode-map "\e=" 'calendar-count-days-region)
1902 (define-key calendar-mode-map "gd" 'calendar-goto-date)
1903 (define-key calendar-mode-map "gj" 'calendar-goto-julian-date)
1904 (define-key calendar-mode-map "ga" 'calendar-goto-astro-day-number)
1905 (define-key calendar-mode-map "gh" 'calendar-goto-hebrew-date)
1906 (define-key calendar-mode-map "gi" 'calendar-goto-islamic-date)
1907 (define-key calendar-mode-map "gC" 'calendar-goto-chinese-date)
1908 (define-key calendar-mode-map "gk" 'calendar-goto-coptic-date)
1909 (define-key calendar-mode-map "ge" 'calendar-goto-ethiopic-date)
1910 (define-key calendar-mode-map "gp" 'calendar-goto-persian-date)
1911 (define-key calendar-mode-map "gc" 'calendar-goto-iso-date)
1912 (define-key calendar-mode-map "gf" 'calendar-goto-french-date)
1913 (define-key calendar-mode-map "gml" 'calendar-goto-mayan-long-count-date)
1914 (define-key calendar-mode-map "gmpc" 'calendar-previous-calendar-round-date)
1915 (define-key calendar-mode-map "gmnc" 'calendar-next-calendar-round-date)
1916 (define-key calendar-mode-map "gmph" 'calendar-previous-haab-date)
1917 (define-key calendar-mode-map "gmnh" 'calendar-next-haab-date)
1918 (define-key calendar-mode-map "gmpt" 'calendar-previous-tzolkin-date)
1919 (define-key calendar-mode-map "gmnt" 'calendar-next-tzolkin-date)
1920 (define-key calendar-mode-map "A" 'appt-add)
1921 (define-key calendar-mode-map "D" 'appt-delete)
1922 (define-key calendar-mode-map "S" 'calendar-sunrise-sunset)
1923 (define-key calendar-mode-map "M" 'calendar-phases-of-moon)
1924 (define-key calendar-mode-map " " 'scroll-other-window)
1925 (define-key calendar-mode-map "\C-c\C-l" 'redraw-calendar)
1926 (define-key calendar-mode-map "." 'calendar-goto-today)
1927 (define-key calendar-mode-map "o" 'calendar-other-month)
1928 (define-key calendar-mode-map "q" 'exit-calendar)
1929 (define-key calendar-mode-map "a" 'list-calendar-holidays)
1930 (define-key calendar-mode-map "h" 'calendar-cursor-holidays)
1931 (define-key calendar-mode-map "x" 'mark-calendar-holidays)
1932 (define-key calendar-mode-map "u" 'calendar-unmark)
1933 (define-key calendar-mode-map "m" 'mark-diary-entries)
1934 (define-key calendar-mode-map "d" 'view-diary-entries)
1935 (define-key calendar-mode-map "D" 'view-other-diary-entries)
1936 (define-key calendar-mode-map "s" 'show-all-diary-entries)
1937 (define-key calendar-mode-map "pd" 'calendar-print-day-of-year)
1938 (define-key calendar-mode-map "pC" 'calendar-print-chinese-date)
1939 (define-key calendar-mode-map "pk" 'calendar-print-coptic-date)
1940 (define-key calendar-mode-map "pe" 'calendar-print-ethiopic-date)
1941 (define-key calendar-mode-map "pp" 'calendar-print-persian-date)
1942 (define-key calendar-mode-map "pc" 'calendar-print-iso-date)
1943 (define-key calendar-mode-map "pj" 'calendar-print-julian-date)
1944 (define-key calendar-mode-map "pa" 'calendar-print-astro-day-number)
1945 (define-key calendar-mode-map "ph" 'calendar-print-hebrew-date)
1946 (define-key calendar-mode-map "pi" 'calendar-print-islamic-date)
1947 (define-key calendar-mode-map "pf" 'calendar-print-french-date)
1948 (define-key calendar-mode-map "pm" 'calendar-print-mayan-date)
1949 (define-key calendar-mode-map "po" 'calendar-print-other-dates)
1950 (define-key calendar-mode-map "id" 'insert-diary-entry)
1951 (define-key calendar-mode-map "iw" 'insert-weekly-diary-entry)
1952 (define-key calendar-mode-map "im" 'insert-monthly-diary-entry)
1953 (define-key calendar-mode-map "iy" 'insert-yearly-diary-entry)
1954 (define-key calendar-mode-map "ia" 'insert-anniversary-diary-entry)
1955 (define-key calendar-mode-map "ib" 'insert-block-diary-entry)
1956 (define-key calendar-mode-map "ic" 'insert-cyclic-diary-entry)
1957 (define-key calendar-mode-map "ihd" 'insert-hebrew-diary-entry)
1958 (define-key calendar-mode-map "ihm" 'insert-monthly-hebrew-diary-entry)
1959 (define-key calendar-mode-map "ihy" 'insert-yearly-hebrew-diary-entry)
1960 (define-key calendar-mode-map "iid" 'insert-islamic-diary-entry)
1961 (define-key calendar-mode-map "iim" 'insert-monthly-islamic-diary-entry)
1962 (define-key calendar-mode-map "iiy" 'insert-yearly-islamic-diary-entry)
1963 (define-key calendar-mode-map "?" 'calendar-goto-info-node)
1964 (define-key calendar-mode-map "tm" 'cal-tex-cursor-month)
1965 (define-key calendar-mode-map "tM" 'cal-tex-cursor-month-landscape)
1966 (define-key calendar-mode-map "td" 'cal-tex-cursor-day)
1967 (define-key calendar-mode-map "tw1" 'cal-tex-cursor-week)
1968 (define-key calendar-mode-map "tw2" 'cal-tex-cursor-week2)
1969 (define-key calendar-mode-map "tw3" 'cal-tex-cursor-week-iso)
1970 (define-key calendar-mode-map "tw4" 'cal-tex-cursor-week-monday)
1971 (define-key calendar-mode-map "tfd" 'cal-tex-cursor-filofax-daily)
1972 (define-key calendar-mode-map "tfw" 'cal-tex-cursor-filofax-2week)
1973 (define-key calendar-mode-map "tfW" 'cal-tex-cursor-filofax-week)
1974 (define-key calendar-mode-map "tfy" 'cal-tex-cursor-filofax-year)
1975 (define-key calendar-mode-map "ty" 'cal-tex-cursor-year)
1976 (define-key calendar-mode-map "tY" 'cal-tex-cursor-year-landscape))
1977
1978 (defun describe-calendar-mode ()
1979 "Create a help buffer with a brief description of the calendar-mode."
1980 (interactive)
1981 (with-output-to-temp-buffer "*Help*"
1982 (princ
1983 (format
1984 "Calendar Mode:\nFor a complete description, type %s\n%s\n"
1985 (substitute-command-keys
1986 "\\<calendar-mode-map>\\[describe-mode] from within the calendar")
1987 (substitute-command-keys "\\{calendar-mode-map}")))
1988 (save-excursion
1989 (set-buffer standard-output)
1990 (help-mode))
1991 (print-help-return-message)))
1992
1993 ;; Calendar mode is suitable only for specially formatted data.
1994 (put 'calendar-mode 'mode-class 'special)
1995
1996 (defvar calendar-mode-line-format
1997 (list
1998 (substitute-command-keys "\\<calendar-mode-map>\\[scroll-calendar-left]")
1999 "Calendar"
2000 (substitute-command-keys "\\<calendar-mode-map>\\[calendar-goto-info-node] info/\\[calendar-other-month] other/\\[calendar-goto-today] today")
2001 '(calendar-date-string (calendar-current-date) t)
2002 (substitute-command-keys "\\<calendar-mode-map>\\[scroll-calendar-right]"))
2003 "The mode line of the calendar buffer.")
2004
2005 (defun calendar-goto-info-node ()
2006 "Go to the info node for the calendar."
2007 (interactive)
2008 (require 'info)
2009 (let ((where (save-window-excursion
2010 (Info-find-emacs-command-nodes 'calendar))))
2011 (if (not where)
2012 (error "Couldn't find documentation for the calendar.")
2013 (let (same-window-buffer-names)
2014 (info))
2015 (Info-find-node (car (car where)) (car (cdr (car where)))))))
2016
2017 (defun calendar-mode ()
2018 "A major mode for the calendar window.
2019
2020 For a complete description, type \
2021 \\<calendar-mode-map>\\[calendar-goto-info-node] from within the calendar.
2022
2023 \\<calendar-mode-map>\\{calendar-mode-map}"
2024
2025 (kill-all-local-variables)
2026 (setq major-mode 'calendar-mode)
2027 (setq mode-name "Calendar")
2028 (use-local-map calendar-mode-map)
2029 (setq buffer-read-only t)
2030 (setq indent-tabs-mode nil)
2031 (update-calendar-mode-line)
2032 (if window-system
2033 (progn
2034 (make-local-hook 'activate-menubar-hook)
2035 (add-hook 'activate-menubar-hook 'cal-menu-update nil t)))
2036 (make-local-variable 'calendar-mark-ring)
2037 (make-local-variable 'displayed-month);; Month in middle of window.
2038 (make-local-variable 'displayed-year));; Year in middle of window.
2039
2040 (defun calendar-string-spread (strings char length)
2041 "Concatenate list of STRINGS separated with copies of CHAR to fill LENGTH.
2042 The effect is like mapconcat but the separating pieces are as balanced as
2043 possible. Each item of STRINGS is evaluated before concatenation so it can
2044 actually be an expression that evaluates to a string. If LENGTH is too short,
2045 the STRINGS are just concatenated and the result truncated."
2046 ;; The algorithm is based on equation (3.25) on page 85 of Concrete
2047 ;; Mathematics by Ronald L. Graham, Donald E. Knuth, and Oren Patashnik,
2048 ;; Addison-Wesley, Reading, MA, 1989
2049 (let* ((strings (mapcar 'eval
2050 (if (< (length strings) 2)
2051 (append (list "") strings (list ""))
2052 strings)))
2053 (n (- length (length (apply 'concat strings))))
2054 (m (1- (length strings)))
2055 (s (car strings))
2056 (strings (cdr strings))
2057 (i 0))
2058 (while strings
2059 (setq s (concat s
2060 (make-string (max 0 (/ (+ n i) m)) char)
2061 (car strings)))
2062 (setq i (1+ i))
2063 (setq strings (cdr strings)))
2064 (substring s 0 length)))
2065
2066 (defun update-calendar-mode-line ()
2067 "Update the calendar mode line with the current date and date style."
2068 (if (bufferp (get-buffer calendar-buffer))
2069 (save-excursion
2070 (set-buffer calendar-buffer)
2071 (setq mode-line-format
2072 (calendar-string-spread
2073 calendar-mode-line-format ? (frame-width))))))
2074
2075 (defun calendar-window-list ()
2076 "List of all calendar-related windows."
2077 (let ((calendar-buffers (calendar-buffer-list))
2078 list)
2079 (walk-windows '(lambda (w)
2080 (if (memq (window-buffer w) calendar-buffers)
2081 (setq list (cons w list))))
2082 nil t)
2083 list))
2084
2085 (defun calendar-buffer-list ()
2086 "List of all calendar-related buffers."
2087 (let* ((diary-buffer (get-file-buffer diary-file))
2088 (buffers (list "*Yahrzeits*" lunar-phases-buffer holiday-buffer
2089 fancy-diary-buffer diary-buffer calendar-buffer
2090 other-calendars-buffer))
2091 (buffer-list nil)
2092 b)
2093 (while buffers
2094 (setq b (car buffers))
2095 (setq b (cond ((stringp b) (get-buffer b))
2096 ((bufferp b) b)
2097 (t nil)))
2098 (if b (setq buffer-list (cons b buffer-list)))
2099 (setq buffers (cdr buffers)))
2100 buffer-list))
2101
2102 (defun exit-calendar ()
2103 "Get out of the calendar window and hide it and related buffers."
2104 (interactive)
2105 (let* ((diary-buffer (get-file-buffer diary-file)))
2106 (if (or (not diary-buffer)
2107 (not (buffer-modified-p diary-buffer))
2108 (yes-or-no-p
2109 "Diary modified; do you really want to exit the calendar? "))
2110 ;; Need to do this multiple times because one time can replace some
2111 ;; calendar-related buffers with other calendar-related buffers
2112 (mapcar (lambda (x)
2113 (mapcar 'calendar-hide-window (calendar-window-list)))
2114 (calendar-window-list)))))
2115
2116 (defun calendar-hide-window (window)
2117 "Hide WINDOW if it is calendar-related."
2118 (let ((buffer (if (window-live-p window) (window-buffer window))))
2119 (if (memq buffer (calendar-buffer-list))
2120 (cond
2121 ((and window-system
2122 (eq 'icon (cdr (assoc 'visibility
2123 (frame-parameters
2124 (window-frame window))))))
2125 nil)
2126 ((and window-system (window-dedicated-p window))
2127 (iconify-frame (window-frame window)))
2128 ((not (and (select-window window) (one-window-p window)))
2129 (delete-window window))
2130 (t (set-buffer buffer)
2131 (bury-buffer))))))
2132
2133 (defun calendar-current-date ()
2134 "Returns the current date in a list (month day year)."
2135 (let ((now (decode-time)))
2136 (list (nth 4 now) (nth 3 now) (nth 5 now))))
2137
2138 (defun calendar-cursor-to-date (&optional error)
2139 "Returns a list (month day year) of current cursor position.
2140 If cursor is not on a specific date, signals an error if optional parameter
2141 ERROR is t, otherwise just returns nil."
2142 (let* ((segment (/ (current-column) 25))
2143 (month (% (+ displayed-month segment -1) 12))
2144 (month (if (= 0 month) 12 month))
2145 (year
2146 (cond
2147 ((and (= 12 month) (= segment 0)) (1- displayed-year))
2148 ((and (= 1 month) (= segment 2)) (1+ displayed-year))
2149 (t displayed-year))))
2150 (if (and (looking-at "[ 0-9]?[0-9][^0-9]")
2151 (< 2 (count-lines (point-min) (point))))
2152 (save-excursion
2153 (if (not (looking-at " "))
2154 (re-search-backward "[^0-9]"))
2155 (list month
2156 (string-to-int (buffer-substring (1+ (point)) (+ 4 (point))))
2157 year))
2158 (if (looking-at "\\*")
2159 (save-excursion
2160 (re-search-backward "[^*]")
2161 (if (looking-at ".\\*\\*")
2162 (list month calendar-starred-day year)
2163 (if error (error "Not on a date!"))))
2164 (if error (error "Not on a date!"))))))
2165
2166 ;; The following version of calendar-gregorian-from-absolute is preferred for
2167 ;; reasons of clarity, BUT it's much slower than the version that follows it.
2168
2169 ;;(defun calendar-gregorian-from-absolute (date)
2170 ;; "Compute the list (month day year) corresponding to the absolute DATE.
2171 ;;The absolute date is the number of days elapsed since the (imaginary)
2172 ;;Gregorian date Sunday, December 31, 1 BC."
2173 ;; (let* ((approx (/ date 366));; Approximation from below.
2174 ;; (year ;; Search forward from the approximation.
2175 ;; (+ approx
2176 ;; (calendar-sum y approx
2177 ;; (>= date (calendar-absolute-from-gregorian (list 1 1 (1+ y))))
2178 ;; 1)))
2179 ;; (month ;; Search forward from January.
2180 ;; (1+ (calendar-sum m 1
2181 ;; (> date
2182 ;; (calendar-absolute-from-gregorian
2183 ;; (list m (calendar-last-day-of-month m year) year)))
2184 ;; 1)))
2185 ;; (day ;; Calculate the day by subtraction.
2186 ;; (- date
2187 ;; (1- (calendar-absolute-from-gregorian (list month 1 year))))))
2188 ;; (list month day year)))
2189
2190 (defun calendar-gregorian-from-absolute (date)
2191 "Compute the list (month day year) corresponding to the absolute DATE.
2192 The absolute date is the number of days elapsed since the (imaginary)
2193 Gregorian date Sunday, December 31, 1 BC."
2194 ;; See the footnote on page 384 of ``Calendrical Calculations, Part II:
2195 ;; Three Historical Calendars'' by E. M. Reingold, N. Dershowitz, and S. M.
2196 ;; Clamen, Software--Practice and Experience, Volume 23, Number 4
2197 ;; (April, 1993), pages 383-404 for an explanation.
2198 (let* ((d0 (1- date))
2199 (n400 (/ d0 146097))
2200 (d1 (% d0 146097))
2201 (n100 (/ d1 36524))
2202 (d2 (% d1 36524))
2203 (n4 (/ d2 1461))
2204 (d3 (% d2 1461))
2205 (n1 (/ d3 365))
2206 (day (1+ (% d3 365)))
2207 (year (+ (* 400 n400) (* 100 n100) (* n4 4) n1)))
2208 (if (or (= n100 4) (= n1 4))
2209 (list 12 31 year)
2210 (let ((year (1+ year))
2211 (month 1))
2212 (while (let ((mdays (calendar-last-day-of-month month year)))
2213 (and (< mdays day)
2214 (setq day (- day mdays))))
2215 (setq month (1+ month)))
2216 (list month day year)))))
2217
2218 (defun calendar-other-month (month year)
2219 "Display a three-month calendar centered around MONTH and YEAR."
2220 (interactive (calendar-read-date 'noday))
2221 (if (and (= month displayed-month)
2222 (= year displayed-year))
2223 nil
2224 (let ((old-date (calendar-cursor-to-date))
2225 (today (calendar-current-date)))
2226 (generate-calendar-window month year)
2227 (calendar-cursor-to-visible-date
2228 (cond
2229 ((calendar-date-is-visible-p old-date) old-date)
2230 ((calendar-date-is-visible-p today) today)
2231 (t (list month 1 year)))))))
2232
2233 (defun calendar-set-mark (arg)
2234 "Mark the date under the cursor, or jump to marked date.
2235 With no prefix argument, push current date onto marked date ring.
2236 With argument, jump to mark, pop it, and put point at end of ring."
2237 (interactive "P")
2238 (let ((date (calendar-cursor-to-date t)))
2239 (if (null arg)
2240 (progn
2241 (setq calendar-mark-ring (cons date calendar-mark-ring))
2242 ;; Since the top of the mark ring is the marked date in the
2243 ;; calendar, the mark ring in the calendar is one longer than
2244 ;; in other buffers to get the same effect.
2245 (if (> (length calendar-mark-ring) (1+ mark-ring-max))
2246 (setcdr (nthcdr mark-ring-max calendar-mark-ring) nil))
2247 (message "Mark set"))
2248 (if (null calendar-mark-ring)
2249 (error "No mark set in this buffer")
2250 (calendar-goto-date (car calendar-mark-ring))
2251 (setq calendar-mark-ring
2252 (cdr (nconc calendar-mark-ring (list date))))))))
2253
2254 (defun calendar-exchange-point-and-mark ()
2255 "Exchange the current cursor position with the marked date."
2256 (interactive)
2257 (let ((mark (car calendar-mark-ring))
2258 (date (calendar-cursor-to-date t)))
2259 (if (null mark)
2260 (error "No mark set in this buffer")
2261 (setq calendar-mark-ring (cons date (cdr calendar-mark-ring)))
2262 (calendar-goto-date mark))))
2263
2264 (defun calendar-count-days-region ()
2265 "Count the number of days (inclusive) between point and the mark."
2266 (interactive)
2267 (let* ((days (- (calendar-absolute-from-gregorian
2268 (calendar-cursor-to-date t))
2269 (calendar-absolute-from-gregorian
2270 (or (car calendar-mark-ring)
2271 (error "No mark set in this buffer")))))
2272 (days (1+ (if (> days 0) days (- days)))))
2273 (message "Region has %d day%s (inclusive)"
2274 days (if (> days 1) "s" ""))))
2275
2276 (defun calendar-not-implemented ()
2277 "Not implemented."
2278 (interactive)
2279 (error "%s not available in the calendar"
2280 (global-key-binding (this-command-keys))))
2281
2282 (defun calendar-read (prompt acceptable &optional initial-contents)
2283 "Return an object read from the minibuffer.
2284 Prompt with the string PROMPT and use the function ACCEPTABLE to decide if
2285 entered item is acceptable. If non-nil, optional third arg INITIAL-CONTENTS
2286 is a string to insert in the minibuffer before reading."
2287 (let ((value (read-minibuffer prompt initial-contents)))
2288 (while (not (funcall acceptable value))
2289 (setq value (read-minibuffer prompt initial-contents)))
2290 value))
2291
2292 (defun calendar-read-date (&optional noday)
2293 "Prompt for Gregorian date. Returns a list (month day year).
2294 If optional NODAY is t, does not ask for day, but just returns
2295 \(month nil year); if NODAY is any other non-nil value the value returned is
2296 \(month year) "
2297 (let* ((year (calendar-read
2298 "Year (>0): "
2299 '(lambda (x) (> x 0))
2300 (int-to-string (extract-calendar-year
2301 (calendar-current-date)))))
2302 (month-array calendar-month-name-array)
2303 (completion-ignore-case t)
2304 (month (cdr (assoc
2305 (capitalize
2306 (completing-read
2307 "Month name: "
2308 (mapcar 'list (append month-array nil))
2309 nil t))
2310 (calendar-make-alist month-array 1 'capitalize))))
2311 (last (calendar-last-day-of-month month year)))
2312 (if noday
2313 (if (eq noday t)
2314 (list month nil year)
2315 (list month year))
2316 (list month
2317 (calendar-read (format "Day (1-%d): " last)
2318 '(lambda (x) (and (< 0 x) (<= x last))))
2319 year))))
2320
2321 (defun calendar-interval (mon1 yr1 mon2 yr2)
2322 "The number of months difference between MON1, YR1 and MON2, YR2."
2323 (+ (* 12 (- yr2 yr1))
2324 (- mon2 mon1)))
2325
2326 (defun calendar-day-name (date &optional width absolute)
2327 "Returns a string with the name of the day of the week of DATE.
2328 If WIDTH is non-nil, return just the first WIDTH characters of the name.
2329 If ABSOLUTE is non-nil, then DATE is actually the day-of-the-week
2330 rather than a date."
2331 (let ((string (aref calendar-day-name-array
2332 (if absolute date (calendar-day-of-week date)))))
2333 (cond ((null width) string)
2334 (enable-multibyte-characters (truncate-string-to-width string width))
2335 (t (substring string 0 width)))))
2336
2337 (defvar calendar-day-name-array
2338 ["Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday"]
2339 "Array of capitalized strings giving, in order, the day names.")
2340
2341 (defvar calendar-month-name-array
2342 ["January" "February" "March" "April" "May" "June"
2343 "July" "August" "September" "October" "November" "December"]
2344 "Array of capitalized strings giving, in order, the month names.")
2345
2346 (defun calendar-make-alist (sequence &optional start-index filter)
2347 "Make an assoc list corresponding to SEQUENCE.
2348 Start at index 1, unless optional START-INDEX is provided.
2349 If FILTER is provided, apply it to each item in the list."
2350 (let ((index (if start-index (1- start-index) 0)))
2351 (mapcar
2352 '(lambda (x)
2353 (setq index (1+ index))
2354 (cons (if filter (funcall filter x) x)
2355 index))
2356 (append sequence nil))))
2357
2358 (defun calendar-month-name (month &optional width)
2359 "The name of MONTH.
2360 If WIDTH is non-nil, return just the first WIDTH characters of the name."
2361 (let ((string (aref calendar-month-name-array (1- month))))
2362 (if width
2363 (let ((i 0) (result "") (pos 0))
2364 (while (< i width)
2365 (let ((chartext (char-to-string (sref string pos))))
2366 (setq pos (+ pos (length chartext)))
2367 (setq result (concat result chartext)))
2368 (setq i (1+ i)))
2369 result)
2370 string)))
2371
2372 (defun calendar-day-of-week (date)
2373 "Returns the day-of-the-week index of DATE, 0 for Sunday, 1 for Monday, etc."
2374 (% (calendar-absolute-from-gregorian date) 7))
2375
2376 (defun calendar-unmark ()
2377 "Delete all diary/holiday marks/highlighting from the calendar."
2378 (interactive)
2379 (setq mark-holidays-in-calendar nil)
2380 (setq mark-diary-entries-in-calendar nil)
2381 (redraw-calendar))
2382
2383 (defun calendar-date-is-visible-p (date)
2384 "Returns t if DATE is legal and is visible in the calendar window."
2385 (let ((gap (calendar-interval
2386 displayed-month displayed-year
2387 (extract-calendar-month date) (extract-calendar-year date))))
2388 (and (calendar-date-is-legal-p date) (> 2 gap) (< -2 gap))))
2389
2390 (defun calendar-date-is-legal-p (date)
2391 "Returns t if DATE is a legal date."
2392 (let ((month (extract-calendar-month date))
2393 (day (extract-calendar-day date))
2394 (year (extract-calendar-year date)))
2395 (and (<= 1 month) (<= month 12)
2396 (<= 1 day) (<= day (calendar-last-day-of-month month year))
2397 (<= 1 year))))
2398
2399 (defun calendar-date-equal (date1 date2)
2400 "Returns t if the DATE1 and DATE2 are the same."
2401 (and
2402 (= (extract-calendar-month date1) (extract-calendar-month date2))
2403 (= (extract-calendar-day date1) (extract-calendar-day date2))
2404 (= (extract-calendar-year date1) (extract-calendar-year date2))))
2405
2406 (defun mark-visible-calendar-date (date &optional mark)
2407 "Mark DATE in the calendar window with MARK.
2408 MARK is either a single-character string or a face.
2409 MARK defaults to diary-entry-marker."
2410 (if (calendar-date-is-legal-p date)
2411 (save-excursion
2412 (set-buffer calendar-buffer)
2413 (calendar-cursor-to-visible-date date)
2414 (let ((mark (or mark diary-entry-marker)))
2415 (if (stringp mark)
2416 (let ((buffer-read-only nil))
2417 (forward-char 1)
2418 (delete-char 1)
2419 (insert mark)
2420 (forward-char -2))
2421 (overlay-put
2422 (make-overlay (1- (point)) (1+ (point))) 'face mark))))))
2423
2424 (defun calendar-star-date ()
2425 "Replace the date under the cursor in the calendar window with asterisks.
2426 This function can be used with the today-visible-calendar-hook run after the
2427 calendar window has been prepared."
2428 (let ((buffer-read-only nil))
2429 (make-variable-buffer-local 'calendar-starred-day)
2430 (forward-char 1)
2431 (setq calendar-starred-day
2432 (string-to-int
2433 (buffer-substring (point) (- (point) 2))))
2434 (delete-char -2)
2435 (insert "**")
2436 (backward-char 1)
2437 (set-buffer-modified-p nil)))
2438
2439 (defun calendar-mark-today ()
2440 "Mark the date under the cursor in the calendar window.
2441 The date is marked with calendar-today-marker. This function can be used with
2442 the today-visible-calendar-hook run after the calendar window has been
2443 prepared."
2444 (mark-visible-calendar-date
2445 (calendar-cursor-to-date)
2446 calendar-today-marker))
2447
2448 (defun calendar-date-compare (date1 date2)
2449 "Returns t if DATE1 is before DATE2, nil otherwise.
2450 The actual dates are in the car of DATE1 and DATE2."
2451 (< (calendar-absolute-from-gregorian (car date1))
2452 (calendar-absolute-from-gregorian (car date2))))
2453
2454 (defun calendar-date-string (date &optional abbreviate nodayname)
2455 "A string form of DATE, driven by the variable `calendar-date-display-form'.
2456 An optional parameter ABBREVIATE, when t, causes the month and day names to be
2457 abbreviated to three characters. An optional parameter NODAYNAME, when t,
2458 omits the name of the day of the week."
2459 (let* ((dayname
2460 (if nodayname
2461 nil
2462 (if abbreviate
2463 (calendar-day-name date 3)
2464 (calendar-day-name date))))
2465 (month (extract-calendar-month date))
2466 (monthname
2467 (if abbreviate
2468 (calendar-month-name month 3)
2469 (calendar-month-name month)))
2470 (day (int-to-string (extract-calendar-day date)))
2471 (month (int-to-string month))
2472 (year (int-to-string (extract-calendar-year date))))
2473 (mapconcat 'eval calendar-date-display-form "")))
2474
2475 (defun calendar-dayname-on-or-before (dayname date)
2476 "Returns the absolute date of the DAYNAME on or before absolute DATE.
2477 DAYNAME=0 means Sunday, DAYNAME=1 means Monday, and so on.
2478
2479 Note: Applying this function to d+6 gives us the DAYNAME on or after an
2480 absolute day d. Similarly, applying it to d+3 gives the DAYNAME nearest to
2481 absolute date d, applying it to d-1 gives the DAYNAME previous to absolute
2482 date d, and applying it to d+7 gives the DAYNAME following absolute date d."
2483 (- date (% (- date dayname) 7)))
2484
2485 (defun calendar-nth-named-absday (n dayname month year &optional day)
2486 "The absolute date of Nth DAYNAME in MONTH, YEAR before/after optional DAY.
2487 A DAYNAME of 0 means Sunday, 1 means Monday, and so on. If N<0,
2488 return the Nth DAYNAME before MONTH DAY, YEAR (inclusive).
2489 If N>0, return the Nth DAYNAME after MONTH DAY, YEAR (inclusive).
2490
2491 If DAY is omitted, it defaults to 1 if N>0, and MONTH's last day otherwise."
2492 (if (> n 0)
2493 (+ (* 7 (1- n))
2494 (calendar-dayname-on-or-before
2495 dayname
2496 (+ 6 (calendar-absolute-from-gregorian
2497 (list month (or day 1) year)))))
2498 (+ (* 7 (1+ n))
2499 (calendar-dayname-on-or-before
2500 dayname
2501 (calendar-absolute-from-gregorian
2502 (list month
2503 (or day (calendar-last-day-of-month month year))
2504 year))))))
2505
2506 (defun calendar-nth-named-day (n dayname month year &optional day)
2507 "The date of Nth DAYNAME in MONTH, YEAR before/after optional DAY.
2508 A DAYNAME of 0 means Sunday, 1 means Monday, and so on. If N<0,
2509 return the Nth DAYNAME before MONTH DAY, YEAR (inclusive).
2510 If N>0, return the Nth DAYNAME after MONTH DAY, YEAR (inclusive).
2511
2512 If DAY is omitted, it defaults to 1 if N>0, and MONTH's last day otherwise."
2513 (calendar-gregorian-from-absolute
2514 (calendar-nth-named-absday n dayname month year day)))
2515
2516 (defun calendar-day-of-year-string (&optional date)
2517 "String of day number of year of Gregorian DATE.
2518 Defaults to today's date if DATE is not given."
2519 (let* ((d (or date (calendar-current-date)))
2520 (year (extract-calendar-year d))
2521 (day (calendar-day-number d))
2522 (days-remaining (- (calendar-day-number (list 12 31 year)) day)))
2523 (format "Day %d of %d; %d day%s remaining in the year"
2524 day year days-remaining (if (= days-remaining 1) "" "s"))))
2525
2526 (defun calendar-print-other-dates ()
2527 "Show dates on other calendars for date under the cursor."
2528 (interactive)
2529 (let* ((date (calendar-cursor-to-date t)))
2530 (save-excursion
2531 (set-buffer (get-buffer-create other-calendars-buffer))
2532 (setq buffer-read-only nil)
2533 (calendar-set-mode-line
2534 (concat (calendar-date-string date) " (Gregorian)"))
2535 (erase-buffer)
2536 (insert
2537 (mapconcat 'identity
2538 (list (calendar-day-of-year-string date)
2539 (format "ISO date: %s" (calendar-iso-date-string date))
2540 (format "Julian date: %s"
2541 (calendar-julian-date-string date))
2542 (format
2543 "Astronomical (Julian) day number (at noon UTC): %s.0"
2544 (calendar-astro-date-string date))
2545 (format "Fixed (RD) date: %s"
2546 (calendar-absolute-from-gregorian date))
2547 (format "Hebrew date (before sunset): %s"
2548 (calendar-hebrew-date-string date))
2549 (format "Persian date: %s"
2550 (calendar-persian-date-string date))
2551 (let ((i (calendar-islamic-date-string date)))
2552 (if (not (string-equal i ""))
2553 (format "Islamic date (before sunset): %s" i)))
2554 (format "Chinese date: %s"
2555 (calendar-chinese-date-string date))
2556 (let ((c (calendar-coptic-date-string date)))
2557 (if (not (string-equal c ""))
2558 (format "Coptic date: %s" c)))
2559 (let ((e (calendar-ethiopic-date-string date)))
2560 (if (not (string-equal e ""))
2561 (format "Ethiopic date: %s" e)))
2562 (let ((f (calendar-french-date-string date)))
2563 (if (not (string-equal f ""))
2564 (format "French Revolutionary date: %s" f)))
2565 (format "Mayan date: %s"
2566 (calendar-mayan-date-string date)))
2567 "\n"))
2568 (goto-char (point-min))
2569 (set-buffer-modified-p nil)
2570 (setq buffer-read-only t)
2571 (display-buffer other-calendars-buffer))))
2572
2573 (defun calendar-print-day-of-year ()
2574 "Show day number in year/days remaining in year for date under the cursor."
2575 (interactive)
2576 (message (calendar-day-of-year-string (calendar-cursor-to-date t))))
2577
2578 (defun calendar-set-mode-line (str)
2579 "Set mode line to STR, centered, surrounded by dashes."
2580 (setq mode-line-format
2581 (calendar-string-spread (list str) ?- (frame-width))))
2582
2583 (defun calendar-mod (m n)
2584 "Non-negative remainder of M/N with N instead of 0."
2585 (1+ (mod (1- m) n)))
2586
2587 (run-hooks 'calendar-load-hook)
2588
2589 (provide 'calendar)
2590
2591 ;;; Local variables:
2592 ;;; byte-compile-dynamic: t
2593 ;;; End:
2594
2595 ;;; calendar.el ends here