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