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