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