(calendar-holidays): Doc fix.
[emacs.git] / lisp / calendar / calendar.el
blob63c67a4545ee6f3614870115aafc818cba2edb46
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. Times of sunrise/sunset can
52 ;; be displayed, as can the phases of the moon. Appointment
53 ;; notification for diary entries is available. Calendar printing via
54 ;; LaTeX is available.
56 ;; The following files are part of the calendar/diary code:
58 ;; appt.el Appointment notification
59 ;; cal-bahai.el Baha'i calendar
60 ;; cal-china.el Chinese calendar
61 ;; cal-coptic.el Coptic/Ethiopic calendars
62 ;; cal-dst.el Daylight saving time rules
63 ;; cal-french.el French revolutionary calendar
64 ;; cal-hebrew.el Hebrew calendar
65 ;; cal-html.el Calendars in HTML
66 ;; cal-islam.el Islamic calendar
67 ;; cal-iso.el ISO calendar
68 ;; cal-julian.el Julian/astronomical calendars
69 ;; cal-mayan.el Mayan calendars
70 ;; cal-menu.el Menu support
71 ;; cal-move.el Movement in the calendar
72 ;; cal-persia.el Persian calendar
73 ;; cal-tex.el Calendars in LaTeX
74 ;; cal-x.el Dedicated frame functions
75 ;; calendar.el This file
76 ;; diary-lib.el Diary functions
77 ;; holidays.el Holiday functions
78 ;; lunar.el Phases of the moon
79 ;; solar.el Sunrise/sunset, equinoxes/solstices
81 ;; Technical details of all the calendrical calculations can be found in
82 ;; ``Calendrical Calculations: The Millennium Edition'' by Edward M. Reingold
83 ;; and Nachum Dershowitz, Cambridge University Press (2001).
85 ;; An earlier version of the technical details appeared in
86 ;; ``Calendrical Calculations'' by Nachum Dershowitz and Edward M. Reingold,
87 ;; Software--Practice and Experience, Volume 20, Number 9 (September, 1990),
88 ;; pages 899-928, and in ``Calendrical Calculations, Part II: Three Historical
89 ;; Calendars'' by E. M. Reingold, N. Dershowitz, and S. M. Clamen,
90 ;; Software--Practice and Experience, Volume 23, Number 4 (April, 1993),
91 ;; pages 383-404.
93 ;; Hard copies of these two papers can be obtained by sending email to
94 ;; reingold@cs.uiuc.edu with the SUBJECT "send-paper-cal" (no quotes) and
95 ;; the message BODY containing your mailing address (snail).
98 ;; A note on free variables:
100 ;; The calendar passes around a few dynamically bound variables, which
101 ;; unfortunately have rather common names. They are meant to be
102 ;; available for external functions, so the names can't be changed.
104 ;; displayed-month, displayed-year: bound in generate-calendar, the
105 ;; central month of the 3 month calendar window
106 ;; original-date, number: bound in diary-list-entries, the arguments
107 ;; with which that function was called.
108 ;; date, entry: bound in list-sexp-diary-entries (qv)
110 ;; Bound in diary-list-entries:
111 ;; diary-entries-list: use in d-l, appt.el, and by add-to-diary-list
112 ;; diary-saved-point: only used in diary-lib.el, passed to the display func
113 ;; date-string: only used in diary-lib.el
114 ;; list-only: don't modify the diary-buffer, just return a list of entries
115 ;; file-glob-attrs: yuck
117 ;;; Code:
119 ;; (elisp) Eval During Compile: "Effectively `require' is
120 ;; automatically `eval-and-compile'" [but `load' is not]
121 (eval-and-compile
122 (load "cal-loaddefs" nil 'quiet))
124 ;; Avoid recursive load of calendar when loading cal-menu.
125 (provide 'calendar)
126 (require 'cal-menu)
129 (defgroup calendar nil
130 "Calendar and time management support."
131 :group 'applications)
133 (defgroup calendar-hooks nil
134 "Calendar hooks."
135 :prefix "calendar-"
136 :group 'calendar)
138 (defgroup diary nil
139 "Emacs diary."
140 :group 'calendar)
142 (defgroup holidays nil
143 "Holidays support in calendar."
144 :group 'calendar
145 :prefix "calendar-"
146 :group 'local)
149 (defcustom calendar-offset 0
150 "The offset of the principal month from the center of the calendar window.
151 0 means the principal month is in the center (default), -1 means on the left,
152 +1 means on the right. Larger (or smaller) values push the principal month off
153 the screen."
154 :type 'integer
155 :group 'calendar)
157 (defcustom calendar-setup nil
158 "The frame setup of the calendar.
159 The choices are: `one-frame' (calendar and diary together in one separate,
160 dedicated frame); `two-frames' (calendar and diary in separate, dedicated
161 frames); `calendar-only' (calendar in a separate, dedicated frame); with
162 any other value the current frame is used. Using any of the first
163 three options overrides the value of `view-diary-entries-initially'."
164 :type '(choice
165 (const :tag "calendar and diary in separate frame" one-frame)
166 (const :tag "calendar and diary each in own frame" two-frames)
167 (const :tag "calendar in separate frame" calendar-only)
168 (const :tag "use current frame" nil))
169 :group 'calendar)
171 (defcustom calendar-minimum-window-height 8
172 "Minimum height `generate-calendar-window' should use for calendar window."
173 :type 'integer
174 :version "22.1"
175 :group 'calendar)
177 (defcustom calendar-week-start-day 0
178 "The day of the week on which a week in the calendar begins.
179 0 means Sunday (default), 1 means Monday, and so on.
181 If you change this variable directly (without using customize)
182 after starting `calendar', you should call `redraw-calendar' to
183 update the calendar display to reflect the change, otherwise
184 movement commands will not work correctly."
185 :type 'integer
186 ;; Change the initialize so that if you reload calendar.el, it will not
187 ;; cause a redraw (which may fail, e.g. with "invalid byte-code in
188 ;; calendar.elc" because of the "byte-compile-dynamic").
189 :initialize 'custom-initialize-default
190 :set (lambda (sym val)
191 (set sym val)
192 (redraw-calendar))
193 :group 'calendar)
195 (defcustom view-diary-entries-initially nil
196 "Non-nil means display current date's diary entries on entry to calendar.
197 The diary is displayed in another window when the calendar is first displayed,
198 if the current date is visible. The number of days of diary entries displayed
199 is governed by the variable `number-of-diary-entries'. This variable can
200 be overridden by the value of `calendar-setup'."
201 :type 'boolean
202 :group 'diary)
204 (defcustom mark-diary-entries-in-calendar nil
205 "Non-nil means mark dates with diary entries, in the calendar window.
206 The marking symbol is specified by the variable `diary-entry-marker'."
207 :type 'boolean
208 :group 'diary)
210 (defcustom calendar-remove-frame-by-deleting t
211 "Determine how the calendar mode removes a frame no longer needed.
212 If nil, make an icon of the frame. If non-nil, delete the frame."
213 :type 'boolean
214 :version "23.1" ; changed from nil to t
215 :group 'view
216 :group 'calendar)
218 (defface calendar-today
219 '((t (:underline t)))
220 "Face for indicating today's date."
221 :group 'diary)
222 ;; Backward-compatibility alias. FIXME make obsolete.
223 (put 'calendar-today-face 'face-alias 'calendar-today)
225 (defface diary
226 '((((min-colors 88) (class color) (background light))
227 :foreground "red1")
228 (((class color) (background light))
229 :foreground "red")
230 (((min-colors 88) (class color) (background dark))
231 :foreground "yellow1")
232 (((class color) (background dark))
233 :foreground "yellow")
235 :weight bold))
236 "Face for highlighting diary entries."
237 :group 'diary)
238 ;; Backward-compatibility alias. FIXME make obsolete.
239 (put 'diary-face 'face-alias 'diary)
241 (defface holiday
242 '((((class color) (background light))
243 :background "pink")
244 (((class color) (background dark))
245 :background "chocolate4")
247 :inverse-video t))
248 "Face for indicating dates that have holidays."
249 :group 'diary)
250 ;; Backward-compatibility alias. FIXME make obsolete.
251 (put 'holiday-face 'face-alias 'holiday)
253 ;; These don't respect changes in font-lock-mode after loading.
254 (defcustom diary-entry-marker (if (and font-lock-mode (display-color-p))
255 'diary
256 "+")
257 "How to mark dates that have diary entries.
258 The value can be either a single-character string or a face."
259 :type '(choice string face)
260 :group 'diary)
262 (defcustom calendar-today-marker (if (and font-lock-mode (display-color-p))
263 'calendar-today
264 "=")
265 "How to mark today's date in the calendar.
266 The value can be either a single-character string or a face.
267 Used by `calendar-mark-today'."
268 :type '(choice string face)
269 :group 'calendar)
271 (defcustom calendar-holiday-marker (if (and font-lock-mode (display-color-p))
272 'holiday
273 "*")
274 "How to mark notable dates in the calendar.
275 The value can be either a single-character string or a face."
276 :type '(choice string face)
277 :group 'calendar)
279 (defcustom view-calendar-holidays-initially nil
280 "Non-nil means display holidays for current three month period on entry.
281 The holidays are displayed in another window when the calendar is first
282 displayed."
283 :type 'boolean
284 :group 'holidays)
286 (defcustom mark-holidays-in-calendar nil
287 "Non-nil means mark dates of holidays in the calendar window.
288 The marking symbol is specified by the variable `calendar-holiday-marker'."
289 :type 'boolean
290 :group 'holidays)
292 (defcustom calendar-mode-hook nil
293 "Hook run when entering `calendar-mode'."
294 :type 'hook
295 :group 'calendar-hooks)
297 (defcustom calendar-load-hook nil
298 "List of functions to be called after the calendar is first loaded.
299 This is the place to add key bindings to `calendar-mode-map'."
300 :type 'hook
301 :group 'calendar-hooks)
303 (defcustom initial-calendar-window-hook nil
304 "List of functions to be called when the calendar window is created.
305 Quitting the calendar and re-entering it will cause these functions
306 to be called again."
307 :type 'hook
308 :group 'calendar-hooks)
310 (defcustom today-visible-calendar-hook nil
311 "List of functions called whenever the current date is visible.
312 To mark today's date, add the function `calendar-mark-today'.
313 To replace the date with asterisks, add the function `calendar-star-date'.
314 See also `today-invisible-calendar-hook'.
316 In general, be careful about changing characters in the calendar buffer,
317 since it may cause the movement commands to fail."
318 :type 'hook
319 :options '(calendar-mark-today calendar-star-date)
320 :group 'calendar-hooks)
322 (defcustom today-invisible-calendar-hook nil
323 "List of functions called whenever the current date is not visible.
324 See also `today-visible-calendar-hook'."
325 :type 'hook
326 :group 'calendar-hooks)
328 (defcustom calendar-move-hook nil
329 "List of functions called whenever the cursor moves in the calendar.
330 For example,
332 (add-hook 'calendar-move-hook (lambda () (diary-view-entries 1)))
334 redisplays the diary for whatever date the cursor is moved to."
335 :type 'hook
336 :group 'calendar-hooks)
338 ;;;###autoload
339 (defcustom diary-file "~/diary"
340 "Name of the file in which one's personal diary of dates is kept.
342 The file's entries are lines beginning with any of the forms
343 specified by the variable `diary-date-forms', which by default
344 uses the forms of `american-date-diary-pattern':
346 MONTH/DAY
347 MONTH/DAY/YEAR
348 MONTHNAME DAY
349 MONTHNAME DAY, YEAR
350 DAYNAME
352 with the remainder of the line being the diary entry string for
353 that date. MONTH and DAY are one or two digit numbers, YEAR is a
354 number and may be written in full or abbreviated to the final two
355 digits (if `abbreviated-calendar-year' is non-nil). MONTHNAME
356 and DAYNAME can be spelled in full (as specified by the variables
357 `calendar-month-name-array' and `calendar-day-name-array'), or
358 abbreviated (as specified by `calendar-month-abbrev-array' and
359 `calendar-day-abbrev-array') with or without a period. Case is
360 ignored. Any of DAY, MONTH, or MONTHNAME, YEAR can be `*' which
361 matches any day, month, or year, respectively. If the date does
362 not contain a year, it is generic and applies to any year. A
363 DAYNAME entry applies to the appropriate day of the week in every week.
365 You can customize `diary-date-forms' to your preferred format.
366 Three default styles are provided: `american-date-diary-pattern',
367 `european-date-diary-pattern', and `iso-date-diary-pattern'.
368 You can choose between these by setting `calendar-date-style' in your
369 .emacs file, or by using `calendar-set-date-style' when in the calendar.
371 A diary entry can be preceded by the character `diary-nonmarking-symbol'
372 \(ordinarily `&') to make that entry nonmarking--that is, it will not be
373 marked on dates in the calendar window but will appear in a diary window.
375 Multiline diary entries are made by indenting lines after the first with
376 either a TAB or one or more spaces.
378 Lines not in one the above formats are ignored. Here are some sample diary
379 entries (in the default American style):
381 12/22/1988 Twentieth wedding anniversary!!
382 &1/1. Happy New Year!
383 10/22 Ruth's birthday.
384 21: Payday
385 Tuesday--weekly meeting with grad students at 10am
386 Supowit, Shen, Bitner, and Kapoor to attend.
387 1/13/89 Friday the thirteenth!!
388 &thu 4pm squash game with Lloyd.
389 mar 16 Dad's birthday
390 April 15, 1989 Income tax due.
391 &* 15 time cards due.
393 If the first line of a diary entry consists only of the date or day name with
394 no trailing blanks or punctuation, then that line is not displayed in the
395 diary window; only the continuation lines is shown. For example, the
396 single diary entry
398 02/11/1989
399 Bill Blattner visits Princeton today
400 2pm Cognitive Studies Committee meeting
401 2:30-5:30 Lizzie at Lawrenceville for `Group Initiative'
402 4:00pm Jamie Tappenden
403 7:30pm Dinner at George and Ed's for Alan Ryan
404 7:30-10:00pm dance at Stewart Country Day School
406 will appear in the diary window without the date line at the beginning. This
407 facility allows the diary window to look neater, but can cause confusion if
408 used with more than one day's entries displayed.
410 Diary entries can be based on Lisp sexps. For example, the diary entry
412 %%(diary-block 11 1 1990 11 10 1990) Vacation
414 causes the diary entry \"Vacation\" to appear from November 1 through
415 November 10, 1990. See the documentation for the function
416 `list-sexp-diary-entries' for more details.
418 Diary entries based on the Hebrew, the Islamic and/or the Baha'i
419 calendar are also possible, but because these are somewhat slow, they
420 are ignored unless you set the `nongregorian-diary-listing-hook' and
421 the `nongregorian-diary-marking-hook' appropriately. See the
422 documentation of these hooks for details.
424 Diary files can contain directives to include the contents of other files; for
425 details, see the documentation for the variable `list-diary-entries-hook'."
426 :type 'file
427 :group 'diary)
429 ;; FIXME do these have to be single characters?
430 (defcustom diary-nonmarking-symbol "&"
431 "Symbol indicating that a diary entry is not to be marked in the calendar."
432 :type 'string
433 :group 'diary)
435 (defcustom hebrew-diary-entry-symbol "H"
436 "Symbol indicating a diary entry according to the Hebrew calendar."
437 :type 'string
438 :group 'diary)
440 (defcustom islamic-diary-entry-symbol "I"
441 "Symbol indicating a diary entry according to the Islamic calendar."
442 :type 'string
443 :group 'diary)
445 (defcustom bahai-diary-entry-symbol "B"
446 "Symbol indicating a diary entry according to the Baha'i calendar."
447 :type 'string
448 :group 'diary)
450 ;;;###autoload
451 (defcustom european-calendar-style nil
452 "Non-nil means use the European style of dates in the diary and display.
453 In this case, a date like 1/2/1990 would be interpreted as
454 February 1, 1990. See `european-date-diary-pattern' for the
455 default European diary date styles.
457 Setting this variable directly does not take effect (if the
458 calendar package is already loaded). Rather, use either
459 \\[customize] or the function `calendar-set-date-style'."
460 :type 'boolean
461 ;; Without :initialize (require 'calendar) throws an error because
462 ;; calendar-set-date-style is undefined at this point.
463 :initialize 'custom-initialize-default
464 :set (lambda (symbol value)
465 (if value
466 (calendar-set-date-style 'european)
467 (calendar-set-date-style 'american)))
468 :group 'diary)
470 ;;;###autoload
471 (make-obsolete-variable 'european-calendar-style 'calendar-date-style "23.1")
473 ;; Used by various other packages.
474 ;;;###autoload
475 (defcustom calendar-date-style (if european-calendar-style 'european
476 'american)
477 "Your preferred style for writing dates.
478 The options are:
479 `american' - month/day/year
480 `european' - day/month/year
481 `iso' - year/month/day
482 This affects how dates written in your diary are interpreted.
483 It also affects date display, as well as those calendar and diary
484 functions that take a date as an argument, e.g. `diary-date', by
485 changing the order in which the arguments are interpreted.
487 Setting this variable directly does not take effect (if the
488 calendar package is already loaded). Rather, use either
489 \\[customize] or the function `calendar-set-date-style'."
490 :version "23.1"
491 :type '(choice (const american :tag "Month/Day/Year")
492 (const european :tag "Day/Month/Year")
493 (const iso :tag "Year/Month/Day"))
494 :initialize 'custom-initialize-default
495 :set (lambda (symbol value)
496 (calendar-set-date-style value))
497 :group 'calendar)
499 ;; Next three are provided to aid in setting diary-date-forms.
500 (defcustom iso-date-diary-pattern
501 '((month "[-/]" day "[^-/0-9]")
502 (year "[-/]" month "[-/]" day "[^0-9]")
503 (monthname "-" day "[^-0-9]")
504 (year "-" monthname "-" day "[^0-9]")
505 (dayname "\\W"))
506 "List of pseudo-patterns describing the ISO style of dates.
507 The defaults are: MONTH[-/]DAY; YEAR[-/]MONTH[-/]DAY; MONTHNAME-DAY;
508 YEAR-MONTHNAME-DAY; DAYNAME. Normally you should not customize this,
509 but `diary-date-forms' (which see)."
510 :version "23.1"
511 :type '(repeat (choice (cons :tag "Backup"
512 :value (backup . nil)
513 (const backup)
514 (repeat (list :inline t :format "%v"
515 (symbol :tag "Keyword")
516 (choice symbol regexp))))
517 (repeat (list :inline t :format "%v"
518 (symbol :tag "Keyword")
519 (choice symbol regexp)))))
520 :group 'diary)
522 (defcustom american-date-diary-pattern
523 '((month "/" day "[^/0-9]")
524 (month "/" day "/" year "[^0-9]")
525 (monthname " *" day "[^,0-9]")
526 (monthname " *" day ", *" year "[^0-9]")
527 (dayname "\\W"))
528 "List of pseudo-patterns describing the American style of dates.
529 The defaults are: MONTH/DAY; MONTH/DAY/YEAR; MONTHNAME DAY;
530 MONTHNAME DAY, YEAR; DAYNAME. Normally you should not customize this,
531 but `diary-date-forms' (which see)."
532 :type '(repeat (choice (cons :tag "Backup"
533 :value (backup . nil)
534 (const backup)
535 (repeat (list :inline t :format "%v"
536 (symbol :tag "Keyword")
537 (choice symbol regexp))))
538 (repeat (list :inline t :format "%v"
539 (symbol :tag "Keyword")
540 (choice symbol regexp)))))
541 :group 'diary)
543 (defcustom european-date-diary-pattern
544 '((day "/" month "[^/0-9]")
545 (day "/" month "/" year "[^0-9]")
546 (backup day " *" monthname "\\W+\\<\\([^*0-9]\\|\\([0-9]+[:aApP]\\)\\)")
547 (day " *" monthname " *" year "[^0-9]")
548 (dayname "\\W"))
549 "List of pseudo-patterns describing the European style of dates.
550 The defaults are: DAY/MONTH; DAY/MONTH/YEAR; DAY MONTHNAME;
551 DAY MONTHNAME YEAR; DAYNAME. Normally you should not customize this, but
552 `diary-date-forms' (which see)."
553 :type '(repeat (choice (cons :tag "Backup"
554 :value (backup . nil)
555 (const backup)
556 (repeat (list :inline t :format "%v"
557 (symbol :tag "Keyword")
558 (choice symbol regexp))))
559 (repeat (list :inline t :format "%v"
560 (symbol :tag "Keyword")
561 (choice symbol regexp)))))
562 :group 'diary)
564 (defvar diary-font-lock-keywords)
566 (defcustom diary-date-forms (cond ((eq calendar-date-style 'iso)
567 iso-date-diary-pattern)
568 ((eq calendar-date-style 'european)
569 european-date-diary-pattern)
570 (t american-date-diary-pattern))
571 "List of pseudo-patterns describing the forms of date used in the diary.
572 The patterns on the list must be MUTUALLY EXCLUSIVE and should not match
573 any portion of the diary entry itself, just the date component.
575 A pseudo-pattern is a list of regular expressions and the keywords `month',
576 `day', `year', `monthname', and `dayname'. The keyword `monthname' will
577 match the name of the month (see `calendar-month-name-array'), capitalized
578 or not, or its user-specified abbreviation (see `calendar-month-abbrev-array'),
579 followed by a period or not; it will also match `*'. Similarly, `dayname'
580 will match the name of the day (see `calendar-day-name-array'), capitalized or
581 not, or its user-specified abbreviation (see `calendar-day-abbrev-array'),
582 followed by a period or not. The keywords `month', `day', and `year' will
583 match those numerical values, preceded by arbitrarily many zeros; they will
584 also match `*'.
586 The matching of the diary entries with the date forms is done with the
587 standard syntax table from Fundamental mode, but with the `*' changed so
588 that it is a word constituent.
590 If, to be mutually exclusive, a pseudo-pattern must match a portion of the
591 diary entry itself, the first element of the pattern MUST be `backup'. This
592 directive causes the date recognizer to back up to the beginning of the
593 current word of the diary entry, so in no case can the pattern match more than
594 a portion of the first word of the diary entry.
596 For examples of three common styles, see `american-date-diary-pattern',
597 `european-date-diary-pattern', and `iso-date-diary-pattern'."
598 :type '(repeat (choice (cons :tag "Backup"
599 :value (backup . nil)
600 (const backup)
601 (repeat (list :inline t :format "%v"
602 (symbol :tag "Keyword")
603 (choice symbol regexp))))
604 (repeat (list :inline t :format "%v"
605 (symbol :tag "Keyword")
606 (choice symbol regexp)))))
607 :initialize 'custom-initialize-default
608 :set (lambda (symbol value)
609 (unless (equal value (eval symbol))
610 (custom-set-default symbol value)
611 (setq diary-font-lock-keywords (diary-font-lock-keywords))
612 ;; Need to redraw not just to get new font-locking, but also
613 ;; to pick up any newly recognized entries.
614 (and (diary-live-p)
615 (diary))))
616 :group 'diary)
618 ;; Next three are provided to aid in setting calendar-date-display-form.
619 (defcustom iso-calendar-display-form '((format "%s-%.2d-%.2d" year
620 (string-to-number month)
621 (string-to-number day)))
622 "Pseudo-pattern governing the way a date appears in the ISO style.
623 Normally you should not customize this, but `calendar-date-display-form'
624 \(which see)."
625 :type 'sexp
626 :version "23.1"
627 :group 'calendar)
629 (defcustom european-calendar-display-form
630 '((if dayname (concat dayname ", ")) day " " monthname " " year)
631 "Pseudo-pattern governing the way a date appears in the European style.
632 Normally you should not customize this, but `calendar-date-display-form'
633 \(which see)."
634 :type 'sexp
635 :group 'calendar)
637 (defcustom american-calendar-display-form
638 '((if dayname (concat dayname ", ")) monthname " " day ", " year)
639 "Pseudo-pattern governing the way a date appears in the American style.
640 Normally you should not customize this, but `calendar-date-display-form'
641 \(which see)."
642 :type 'sexp
643 :group 'calendar)
645 (defcustom calendar-date-display-form (cond ((eq calendar-date-style 'iso)
646 iso-calendar-display-form)
647 ((eq calendar-date-style 'european)
648 european-calendar-display-form)
649 (t american-calendar-display-form))
650 "Pseudo-pattern governing the way a calendar date appears.
651 Used by the function `calendar-date-string' (which see), a pseudo-pattern
652 is a list of expressions that can involve the keywords `month', `day',
653 and `year' (all numbers in string form), and `monthname' and `dayname'
654 \(both alphabetic strings). For example, a typical American form would be
656 '(month \"/\" day \"/\" (substring year -2))
658 whereas
660 '((format \"%9s, %9s %2s, %4s\" dayname monthname day year))
662 would give the usual American style in fixed-length fields. The variables
663 `iso-calendar-display-form', `european-calendar-display-form', and
664 `american-calendar-display-form' provide some defaults for three common
665 styles."
666 :type 'sexp
667 :group 'calendar)
669 (defun calendar-set-date-style (style)
670 "Set the style of calendar and diary dates to STYLE (a symbol).
671 The valid styles are described in the documentation of `calendar-date-style'."
672 (interactive (list (intern
673 (completing-read "Date style: "
674 '("american" "european" "iso") nil t
675 nil nil "american"))))
676 (or (memq style '(american european iso))
677 (setq style 'american))
678 (setq calendar-date-style style
679 calendar-date-display-form
680 (symbol-value (intern-soft (format "%s-calendar-display-form" style)))
681 diary-date-forms
682 (symbol-value (intern-soft (format "%s-date-diary-pattern" style))))
683 (update-calendar-mode-line))
685 (defun european-calendar ()
686 "Set the interpretation and display of dates to the European style."
687 (interactive)
688 (calendar-set-date-style 'european))
690 (make-obsolete 'european-calendar 'calendar-set-date-style "23.1")
692 (defun american-calendar ()
693 "Set the interpretation and display of dates to the American style."
694 (interactive)
695 (calendar-set-date-style 'american))
697 (make-obsolete 'american-calendar 'calendar-set-date-style "23.1")
699 ;; FIXME move to diary-lib and adjust appt.
700 ;; Add appt-make-list as an option?
701 (defcustom diary-hook nil
702 "List of functions called after the display of the diary.
703 Can be used for appointment notification."
704 :type 'hook
705 :group 'diary)
707 (defcustom diary-display-hook nil
708 "List of functions that handle the display of the diary.
709 If nil (the default), `simple-diary-display' is used. Use
710 `ignore' for no diary display.
712 Ordinarily, this just displays the diary buffer (with holidays
713 indicated in the mode line), if there are any relevant entries.
714 At the time these functions are called, the variable
715 `diary-entries-list' is a list, in order by date, of all relevant
716 diary entries in the form of ((MONTH DAY YEAR) STRING), where
717 string is the diary entry for the given date. This can be used,
718 for example, a different buffer for display (perhaps combined
719 with holidays), or produce hard copy output.
721 A function `fancy-diary-display' is provided for use with this
722 hook; this function prepares a special noneditable diary buffer
723 with the relevant diary entries that has neat day-by-day
724 arrangement with headings. The fancy diary buffer will show the
725 holidays unless the variable `holidays-in-diary-buffer' is set to
726 nil. Ordinarily, the fancy diary buffer will not show days for
727 which there are no diary entries, even if that day is a holiday;
728 if you want such days to be shown in the fancy diary buffer, set
729 the variable `diary-list-include-blanks' non-nil."
730 :type 'hook
731 :options '(fancy-diary-display)
732 :initialize 'custom-initialize-default
733 :set 'diary-set-maybe-redraw
734 :group 'diary)
736 (defcustom holidays-in-diary-buffer t
737 "Non-nil means include holidays in the diary display.
738 The holidays appear in the mode line of the diary buffer, or in the
739 fancy diary buffer next to the date. This slows down the diary functions
740 somewhat; setting it to nil makes the diary display faster."
741 :type 'boolean
742 :group 'holidays)
744 (defcustom calendar-debug-sexp nil
745 "Turn debugging on when evaluating a sexp in the diary or holiday list."
746 :type 'boolean
747 :group 'calendar)
749 ;; The various holiday variables are autoloaded because people
750 ;; are used to using them to set calendar-holidays without having to
751 ;; explicitly load this file.
753 ;;;###autoload
754 (defcustom general-holidays
755 '((holiday-fixed 1 1 "New Year's Day")
756 (holiday-float 1 1 3 "Martin Luther King Day")
757 (holiday-fixed 2 2 "Groundhog Day")
758 (holiday-fixed 2 14 "Valentine's Day")
759 (holiday-float 2 1 3 "President's Day")
760 (holiday-fixed 3 17 "St. Patrick's Day")
761 (holiday-fixed 4 1 "April Fools' Day")
762 (holiday-float 5 0 2 "Mother's Day")
763 (holiday-float 5 1 -1 "Memorial Day")
764 (holiday-fixed 6 14 "Flag Day")
765 (holiday-float 6 0 3 "Father's Day")
766 (holiday-fixed 7 4 "Independence Day")
767 (holiday-float 9 1 1 "Labor Day")
768 (holiday-float 10 1 2 "Columbus Day")
769 (holiday-fixed 10 31 "Halloween")
770 (holiday-fixed 11 11 "Veteran's Day")
771 (holiday-float 11 4 4 "Thanksgiving"))
772 "General holidays. Default value is for the United States.
773 See the documentation for `calendar-holidays' for details."
774 :type 'sexp
775 :group 'holidays)
776 ;;;###autoload
777 (put 'general-holidays 'risky-local-variable t)
779 ;;;###autoload
780 (defcustom oriental-holidays
781 '((holiday-chinese-new-year))
782 "Oriental holidays.
783 See the documentation for `calendar-holidays' for details."
784 :type 'sexp
785 :group 'holidays)
786 ;;;###autoload
787 (put 'oriental-holidays 'risky-local-variable t)
789 ;;;###autoload
790 (defcustom local-holidays nil
791 "Local holidays.
792 See the documentation for `calendar-holidays' for details."
793 :type 'sexp
794 :group 'holidays)
795 ;;;###autoload
796 (put 'local-holidays 'risky-local-variable t)
798 ;;;###autoload
799 (defcustom other-holidays nil
800 "User defined holidays.
801 See the documentation for `calendar-holidays' for details."
802 :type 'sexp
803 :group 'holidays)
804 ;;;###autoload
805 (put 'other-holidays 'risky-local-variable t)
807 (defcustom all-hebrew-calendar-holidays nil
808 "If nil, show only major holidays from the Hebrew calendar.
809 This means only those Jewish holidays that appear on secular calendars.
810 Otherwise, show all the holidays that would appear in a complete Hebrew
811 calendar."
812 :type 'boolean
813 :group 'holidays)
815 ;;;###autoload
816 (defvar hebrew-holidays-1
817 '((holiday-rosh-hashanah-etc)
818 (if all-hebrew-calendar-holidays
819 (holiday-julian
821 (let ((m displayed-month)
822 (y displayed-year)
823 year)
824 (increment-calendar-month m y -1)
825 (setq year (extract-calendar-year
826 (calendar-julian-from-absolute
827 (calendar-absolute-from-gregorian (list m 1 y)))))
828 (if (zerop (% (1+ year) 4))
830 21)) "\"Tal Umatar\" (evening)")))
831 "Component of the default value of `hebrew-holidays'.")
832 ;;;###autoload
833 (put 'hebrew-holidays-1 'risky-local-variable t)
834 ;;;###autoload
835 (make-obsolete-variable 'hebrew-holidays-1 'hebrew-holidays "23.1")
837 ;;;###autoload
838 (defvar hebrew-holidays-2
839 '((holiday-hanukkah) ; respects all-hebrew-calendar-holidays
840 (if all-hebrew-calendar-holidays
841 (holiday-hebrew
843 (let ((h-year (extract-calendar-year
844 (calendar-hebrew-from-absolute
845 (calendar-absolute-from-gregorian
846 (list displayed-month 28 displayed-year))))))
847 (if (= 6 (% (calendar-absolute-from-hebrew (list 10 10 h-year))
849 11 10))
850 "Tzom Teveth"))
851 (if all-hebrew-calendar-holidays
852 (holiday-hebrew 11 15 "Tu B'Shevat")))
853 "Component of the default value of `hebrew-holidays'.")
854 ;;;###autoload
855 (put 'hebrew-holidays-2 'risky-local-variable t)
856 ;;;###autoload
857 (make-obsolete-variable 'hebrew-holidays-2 'hebrew-holidays "23.1")
859 ;;;###autoload
860 (defvar hebrew-holidays-3
861 '((if all-hebrew-calendar-holidays
862 (holiday-hebrew
864 (let* ((m displayed-month)
865 (y displayed-year)
866 (h-year (progn
867 (increment-calendar-month m y 1)
868 (extract-calendar-year
869 (calendar-hebrew-from-absolute
870 (calendar-absolute-from-gregorian
871 (list m (calendar-last-day-of-month m y) y))))))
872 (s-s
873 (calendar-hebrew-from-absolute
874 (if (= 6
875 (% (calendar-absolute-from-hebrew
876 (list 7 1 h-year))
878 (calendar-dayname-on-or-before
879 6 (calendar-absolute-from-hebrew
880 (list 11 17 h-year)))
881 (calendar-dayname-on-or-before
882 6 (calendar-absolute-from-hebrew
883 (list 11 16 h-year))))))
884 (day (extract-calendar-day s-s)))
885 day)
886 "Shabbat Shirah")))
887 "Component of the default value of `hebrew-holidays'.")
888 ;;;###autoload
889 (put 'hebrew-holidays-3 'risky-local-variable t)
890 ;;;###autoload
891 (make-obsolete-variable 'hebrew-holidays-3 'hebrew-holidays "23.1")
893 ;;;###autoload
894 (defvar hebrew-holidays-4
895 '((holiday-passover-etc)
896 (and all-hebrew-calendar-holidays
897 (let* ((m displayed-month)
898 (y displayed-year)
899 (year (progn
900 (increment-calendar-month m y -1)
901 (extract-calendar-year
902 (calendar-julian-from-absolute
903 (calendar-absolute-from-gregorian (list m 1 y)))))))
904 (= 21 (% year 28)))
905 (holiday-julian 3 26 "Kiddush HaHamah"))
906 (if all-hebrew-calendar-holidays
907 (holiday-tisha-b-av-etc)))
908 "Component of the default value of `hebrew-holidays'.")
909 ;;;###autoload
910 (put 'hebrew-holidays-4 'risky-local-variable t)
911 ;;;###autoload
912 (make-obsolete-variable 'hebrew-holidays-4 'hebrew-holidays "23.1")
914 ;;;###autoload
915 (defcustom hebrew-holidays
916 '((holiday-passover-etc)
917 (holiday-rosh-hashanah-etc)
918 (holiday-hanukkah)
919 (if all-hebrew-calendar-holidays
920 (append
921 (holiday-tisha-b-av-etc)
922 (holiday-hebrew-misc))))
923 "Jewish holidays.
924 See the documentation for `calendar-holidays' for details."
925 :type 'sexp
926 :version "23.1" ; removed dependency on hebrew-holidays-N
927 :group 'holidays)
928 ;;;###autoload
929 (put 'hebrew-holidays 'risky-local-variable t)
931 (defcustom all-christian-calendar-holidays nil
932 "If nil, show only major holidays from the Christian calendar.
933 This means only those Christian holidays that appear on secular calendars.
934 Otherwise, show all the holidays that would appear in a complete Christian
935 calendar."
936 :type 'boolean
937 :group 'holidays)
939 ;;;###autoload
940 (defcustom christian-holidays
941 '((holiday-easter-etc) ; respects all-christian-calendar-holidays
942 (holiday-fixed 12 25 "Christmas")
943 (if all-christian-calendar-holidays
944 (append
945 (holiday-fixed 1 6 "Epiphany")
946 (holiday-julian 12 25 "Eastern Orthodox Christmas")
947 (holiday-greek-orthodox-easter)
948 (holiday-fixed 8 15 "Assumption")
949 (holiday-advent 0 "Advent"))))
950 "Christian holidays.
951 See the documentation for `calendar-holidays' for details."
952 :type 'sexp
953 :group 'holidays)
954 ;;;###autoload
955 (put 'christian-holidays 'risky-local-variable t)
957 (defcustom all-islamic-calendar-holidays nil
958 "If nil, show only major holidays from the Islamic calendar.
959 This means only those Islamic holidays that appear on secular calendars.
960 Otherwise, show all the holidays that would appear in a complete Islamic
961 calendar."
962 :type 'boolean
963 :group 'holidays)
965 ;;;###autoload
966 (defcustom islamic-holidays
967 '((holiday-islamic-new-year)
968 (holiday-islamic 9 1 "Ramadan Begins")
969 (if all-islamic-calendar-holidays
970 (append
971 (holiday-islamic 1 10 "Ashura")
972 (holiday-islamic 3 12 "Mulad-al-Nabi")
973 (holiday-islamic 7 26 "Shab-e-Mi'raj")
974 (holiday-islamic 8 15 "Shab-e-Bara't")
975 (holiday-islamic 9 27 "Shab-e Qadr")
976 (holiday-islamic 10 1 "Id-al-Fitr")
977 (holiday-islamic 12 10 "Id-al-Adha"))))
978 "Islamic holidays.
979 See the documentation for `calendar-holidays' for details."
980 :type 'sexp
981 :group 'holidays)
982 ;;;###autoload
983 (put 'islamic-holidays 'risky-local-variable t)
985 (defcustom all-bahai-calendar-holidays nil
986 "If nil, show only major holidays from the Baha'i calendar.
987 These are the days on which work and school must be suspended.
988 Otherwise, show all the holidays that would appear in a complete Baha'i
989 calendar."
990 :type 'boolean
991 :group 'holidays)
993 ;;;###autoload
994 (defcustom bahai-holidays
995 '((holiday-bahai-new-year)
996 (holiday-bahai-ridvan) ; respects all-bahai-calendar-holidays
997 (if all-bahai-calendar-holidays
998 (append
999 (holiday-fixed 11 26 "Day of the Covenant")
1000 (holiday-fixed 11 28 "Ascension of `Abdu'l-Baha")))
1001 (holiday-fixed 5 23 "Declaration of the Bab")
1002 (holiday-fixed 5 29 "Ascension of Baha'u'llah")
1003 (holiday-fixed 7 9 "Martyrdom of the Bab")
1004 (holiday-fixed 10 20 "Birth of the Bab")
1005 (holiday-fixed 11 12 "Birth of Baha'u'llah"))
1006 "Baha'i holidays.
1007 See the documentation for `calendar-holidays' for details."
1008 :type 'sexp
1009 :group 'holidays)
1010 ;;;###autoload
1011 (put 'bahai-holidays 'risky-local-variable t)
1013 ;;;###autoload
1014 (defcustom solar-holidays
1015 '((solar-equinoxes-solstices)
1016 (holiday-sexp calendar-daylight-savings-starts
1017 (format "Daylight Saving Time Begins %s"
1018 (solar-time-string
1019 (/ calendar-daylight-savings-starts-time (float 60))
1020 calendar-standard-time-zone-name)))
1021 (holiday-sexp calendar-daylight-savings-ends
1022 (format "Daylight Saving Time Ends %s"
1023 (solar-time-string
1024 (/ calendar-daylight-savings-ends-time (float 60))
1025 calendar-daylight-time-zone-name))))
1026 "Sun-related holidays.
1027 See the documentation for `calendar-holidays' for details."
1028 :type 'sexp
1029 :group 'holidays)
1030 ;;;###autoload
1031 (put 'solar-holidays 'risky-local-variable t)
1033 ;;;###autoload
1034 (defcustom calendar-holidays
1035 (append general-holidays local-holidays other-holidays
1036 christian-holidays hebrew-holidays islamic-holidays
1037 bahai-holidays oriental-holidays solar-holidays)
1038 "List of notable days for the command \\[holidays].
1040 Additional holidays are easy to add to the list, just put them in the
1041 list `other-holidays' in your .emacs file. Similarly, by setting any
1042 of `general-holidays', `local-holidays' `christian-holidays',
1043 `hebrew-holidays', `islamic-holidays', `bahai-holidays',
1044 `oriental-holidays', or `solar-holidays' to nil in your .emacs file,
1045 you can eliminate unwanted categories of holidays.
1047 The aforementioned variables control the holiday choices offered
1048 by the function `holiday-list' when it is called interactively.
1050 They also initialize the default value of `calendar-holidays',
1051 which is the default list of holidays used by the function
1052 `holiday-list' in the non-interactive case. Note that these
1053 variables have no effect on `calendar-holidays' after it has been
1054 set (e.g. after the calendar is loaded). In that case, customize
1055 `calendar-holidays' directly.
1057 The intention is that (in the US) `local-holidays' be set in
1058 site-init.el and `other-holidays' be set by the user.
1060 Entries on the list are expressions that return (possibly empty) lists of
1061 items of the form ((month day year) string) of a holiday in the
1062 three-month period centered around `displayed-month' of `displayed-year'.
1063 Several basic functions are provided for this purpose:
1065 (holiday-fixed MONTH DAY STRING) is a fixed date on the Gregorian calendar
1066 (holiday-float MONTH DAYNAME K STRING &optional day) is the Kth DAYNAME in
1067 MONTH on the Gregorian calendar (0 for Sunday,
1068 etc.); K<0 means count back from the end of the
1069 month. An optional parameter DAY means the Kth
1070 DAYNAME after/before MONTH DAY.
1071 (holiday-hebrew MONTH DAY STRING) a fixed date on the Hebrew calendar
1072 (holiday-islamic MONTH DAY STRING) a fixed date on the Islamic calendar
1073 (holiday-bahai MONTH DAY STRING) a fixed date on the Baha'i calendar
1074 (holiday-julian MONTH DAY STRING) a fixed date on the Julian calendar
1075 (holiday-sexp SEXP STRING) SEXP is a Gregorian-date-valued expression
1076 in the variable `year'; if it evaluates to
1077 a visible date, that's the holiday; if it
1078 evaluates to nil, there's no holiday. STRING
1079 is an expression in the variable `date'.
1081 For example, to add Bastille Day, celebrated in France on July 14, add
1083 (holiday-fixed 7 14 \"Bastille Day\")
1085 to the list. To add Hurricane Supplication Day, celebrated in the Virgin
1086 Islands on the fourth Monday in August, add
1088 (holiday-float 8 1 4 \"Hurricane Supplication Day\")
1090 to the list (the last Monday would be specified with `-1' instead of `4').
1091 To add the last day of Hanukkah to the list, use
1093 (holiday-hebrew 10 2 \"Last day of Hanukkah\")
1095 since the Hebrew months are numbered with 1 starting from Nisan.
1096 To add the Islamic feast celebrating Mohammed's birthday, use
1098 (holiday-islamic 3 12 \"Mohammed's Birthday\")
1100 since the Islamic months are numbered from 1 starting with Muharram.
1101 To add an entry for the Baha'i festival of Ridvan, use
1103 (holiday-bahai 2 13 \"Festival of Ridvan\")
1105 since the Baha'i months are numbered from 1 starting with Baha.
1106 To add Thomas Jefferson's birthday, April 2, 1743 (Julian), use
1108 (holiday-julian 4 2 \"Jefferson's Birthday\")
1110 To include a holiday conditionally, use the sexp form or a conditional. For
1111 example, to include American presidential elections, which occur on the first
1112 Tuesday after the first Monday in November of years divisible by 4, add
1114 (holiday-sexp
1115 '(if (zerop (% year 4))
1116 (calendar-gregorian-from-absolute
1117 (1+ (calendar-dayname-on-or-before
1118 1 (+ 6 (calendar-absolute-from-gregorian
1119 (list 11 1 year)))))))
1120 \"US Presidential Election\")
1124 (if (zerop (% displayed-year 4))
1125 (holiday-fixed 11
1126 (extract-calendar-day
1127 (calendar-gregorian-from-absolute
1128 (1+ (calendar-dayname-on-or-before
1129 1 (+ 6 (calendar-absolute-from-gregorian
1130 (list 11 1 displayed-year)))))))
1131 \"US Presidential Election\"))
1133 to the list. To include the phases of the moon, add
1135 (lunar-phases)
1137 to the holiday list, where `lunar-phases' is an Emacs-Lisp function that
1138 you've written to return a (possibly empty) list of the relevant VISIBLE dates
1139 with descriptive strings such as
1141 (((2 6 1989) \"New Moon\") ((2 12 1989) \"First Quarter Moon\") ... )."
1142 :type 'sexp
1143 :group 'holidays)
1144 ;;;###autoload
1145 (put 'calendar-holidays 'risky-local-variable t)
1147 ;;; End of user options.
1149 (defconst calendar-buffer "*Calendar*"
1150 "Name of the buffer used for the calendar.")
1152 (defconst holiday-buffer "*Holidays*"
1153 "Name of the buffer used for the displaying the holidays.")
1155 (defconst fancy-diary-buffer "*Fancy Diary Entries*"
1156 "Name of the buffer used for the optional fancy display of the diary.")
1158 (defconst other-calendars-buffer "*Other Calendars*"
1159 "Name of the buffer used for the display of date on other calendars.")
1161 (defconst lunar-phases-buffer "*Phases of Moon*"
1162 "Name of the buffer used for the lunar phases.")
1164 (defconst cal-hebrew-yahrzeit-buffer "*Yahrzeits*"
1165 "Name of the buffer used by `list-yahrzeit-dates'.")
1167 (defmacro increment-calendar-month (mon yr n &optional nmonths)
1168 "Increment the variables MON and YR by N months.
1169 Forward if N is positive or backward if N is negative.
1170 A negative YR is interpreted as BC; -1 being 1 BC, and so on.
1171 Optional NMONTHS is the number of months per year (default 12)."
1172 ;; Can view this as a form of base-nmonths arithmetic, in which "a
1173 ;; year" = "ten", and we never bother to use hundreds.
1174 `(let ((nmonths (or ,nmonths 12))
1175 macro-y)
1176 (if (< ,yr 0) (setq ,yr (1+ ,yr))) ; -1 BC -> 0 AD, etc
1177 (setq macro-y (+ (* ,yr nmonths) ,mon -1 ,n)
1178 ,mon (1+ (mod macro-y nmonths))
1179 ,yr (/ macro-y nmonths))
1180 ;; Alternative:
1181 ;;; (setq macro-y (+ (* ,yr nmonths) ,mon -1 ,n)
1182 ;;; ,yr (/ macro-y nmonths)
1183 ;;; ,mon (- macro-y (* ,yr nmonths)))
1184 (and (< macro-y 0) (> ,mon 1) (setq ,yr (1- ,yr)))
1185 (if (< ,yr 1) (setq ,yr (1- ,yr))))) ; 0 AD -> -1 BC, etc
1187 (defvar displayed-month)
1188 (defvar displayed-year)
1190 (defun calendar-increment-month (n &optional mon yr)
1191 "Return the Nth month after MON/YR.
1192 The return value is a pair (MONTH . YEAR).
1193 MON defaults to `displayed-month'. YR defaults to `displayed-year'."
1194 (unless mon (setq mon displayed-month))
1195 (unless yr (setq yr displayed-year))
1196 (increment-calendar-month mon yr n)
1197 (cons mon yr))
1199 (defmacro calendar-for-loop (var from init to final do &rest body)
1200 "Execute a for loop.
1201 Evaluate BODY with VAR bound to successive integers from INIT to FINAL,
1202 inclusive. The standard macro `dotimes' is preferable in most cases."
1203 (declare (debug (symbolp "from" form "to" form "do" body))
1204 (indent defun))
1205 `(let ((,var (1- ,init)))
1206 (while (>= ,final (setq ,var (1+ ,var)))
1207 ,@body)))
1209 (make-obsolete 'calendar-for-loop "use `dotimes' or `while' instead." "23.1")
1211 (defmacro calendar-sum (index initial condition expression)
1212 "For INDEX = INITIAL, +1, ... (as long as CONDITION holds), sum EXPRESSION."
1213 (declare (debug (symbolp form form form)))
1214 `(let ((,index ,initial)
1215 (sum 0))
1216 (while ,condition
1217 (setq sum (+ sum ,expression)
1218 ,index (1+ ,index)))
1219 sum))
1221 ;; FIXME bind q to bury-buffer?
1222 (defmacro calendar-in-read-only-buffer (buffer &rest body)
1223 "Switch to BUFFER and executes the forms in BODY.
1224 First creates or erases BUFFER as needed. Leaves BUFFER read-only,
1225 with disabled undo. Leaves point at point-min, displays BUFFER."
1226 (declare (indent 1) (debug t))
1227 `(progn
1228 (set-buffer (get-buffer-create ,buffer))
1229 (setq buffer-read-only nil
1230 buffer-undo-list t)
1231 (erase-buffer)
1232 ,@body
1233 (goto-char (point-min))
1234 (set-buffer-modified-p nil)
1235 (setq buffer-read-only t)
1236 (display-buffer ,buffer)))
1238 ;; The following are in-line for speed; they can be called thousands of times
1239 ;; when looking up holidays or processing the diary. Here, for example, are
1240 ;; the numbers of calls to calendar/diary/holiday functions in preparing the
1241 ;; fancy diary display, for a moderately complex diary file, with functions
1242 ;; used instead of macros. There were a total of 10000 such calls:
1244 ;; 1934 extract-calendar-month
1245 ;; 1852 extract-calendar-year
1246 ;; 1819 extract-calendar-day
1247 ;; 845 calendar-leap-year-p
1248 ;; 837 calendar-day-number
1249 ;; 775 calendar-absolute-from-gregorian
1250 ;; 346 calendar-last-day-of-month
1251 ;; 286 hebrew-calendar-last-day-of-month
1252 ;; 188 hebrew-calendar-leap-year-p
1253 ;; 180 hebrew-calendar-elapsed-days
1254 ;; 163 hebrew-calendar-last-month-of-year
1255 ;; 66 calendar-date-compare
1256 ;; 65 hebrew-calendar-days-in-year
1257 ;; 60 calendar-absolute-from-julian
1258 ;; 50 calendar-absolute-from-hebrew
1259 ;; 43 calendar-date-equal
1260 ;; 38 calendar-gregorian-from-absolute
1261 ;; .
1263 ;; The use of these seven macros eliminates the overhead of 92% of the function
1264 ;; calls; it's faster this way.
1266 (defsubst extract-calendar-month (date)
1267 "Extract the month part of DATE which has the form (month day year)."
1268 (car date))
1270 ;; Note gives wrong answer for result of (calendar-read-date 'noday),
1271 ;; but that is only used by `calendar-other-month'.
1272 (defsubst extract-calendar-day (date)
1273 "Extract the day part of DATE which has the form (month day year)."
1274 (cadr date))
1276 (defsubst extract-calendar-year (date)
1277 "Extract the year part of DATE which has the form (month day year)."
1278 (nth 2 date))
1280 (defsubst calendar-leap-year-p (year)
1281 "Return t if YEAR is a Gregorian leap year.
1282 A negative year is interpreted as BC; -1 being 1 BC, and so on."
1283 ;; 1 BC = 0 AD, 2 BC acts like 1 AD, etc.
1284 (if (< year 0) (setq year (1- (abs year))))
1285 (and (zerop (% year 4))
1286 (or (not (zerop (% year 100)))
1287 (zerop (% year 400)))))
1289 ;; The foregoing is a bit faster, but not as clear as the following:
1291 ;;(defsubst calendar-leap-year-p (year)
1292 ;; "Return t if YEAR is a Gregorian leap year."
1293 ;; (or
1294 ;; (and (zerop (% year 4))
1295 ;; (not (zerop (% year 100))))
1296 ;; (zerop (% year 400)))
1298 (defsubst calendar-last-day-of-month (month year)
1299 "The last day in MONTH during YEAR."
1300 (if (and (= month 2) (calendar-leap-year-p year))
1302 (aref [31 28 31 30 31 30 31 31 30 31 30 31] (1- month))))
1304 ;; An explanation of the calculation can be found in PascAlgorithms by
1305 ;; Edward and Ruth Reingold, Scott-Foresman/Little, Brown, 1988.
1307 (defsubst calendar-day-number (date)
1308 "Return the day number within the year of the date DATE.
1309 For example, (calendar-day-number '(1 1 1987)) returns the value 1,
1310 while (calendar-day-number '(12 31 1980)) returns 366."
1311 (let* ((month (extract-calendar-month date))
1312 (day (extract-calendar-day date))
1313 (year (extract-calendar-year date))
1314 (day-of-year (+ day (* 31 (1- month)))))
1315 (when (> month 2)
1316 (setq day-of-year (- day-of-year (/ (+ 23 (* 4 month)) 10)))
1317 (if (calendar-leap-year-p year)
1318 (setq day-of-year (1+ day-of-year))))
1319 day-of-year))
1321 (defsubst calendar-absolute-from-gregorian (date)
1322 "The number of days elapsed between the Gregorian date 12/31/1 BC and DATE.
1323 The Gregorian date Sunday, December 31, 1 BC is imaginary.
1324 DATE is a list of the form (month day year). A negative year is
1325 interpreted as BC; -1 being 1 BC, and so on. Dates before 12/31/1 BC
1326 return negative results."
1327 (let ((year (extract-calendar-year date))
1328 offset-years)
1329 (cond ((zerop year)
1330 (error "There was no year zero"))
1331 ((> year 0)
1332 (setq offset-years (1- year))
1333 (+ (calendar-day-number date) ; days this year
1334 (* 365 offset-years) ; + days in prior years
1335 (/ offset-years 4) ; + Julian leap years
1336 (- (/ offset-years 100)) ; - century years
1337 (/ offset-years 400))) ; + Gregorian leap years
1339 ;; Years between date and 1 BC, excluding 1 BC (1 for 2 BC, etc).
1340 (setq offset-years (abs (1+ year)))
1341 (- (calendar-day-number date)
1342 (* 365 offset-years)
1343 (/ offset-years 4)
1344 (- (/ offset-years 100))
1345 (/ offset-years 400)
1346 (calendar-day-number '(12 31 -1))))))) ; days in year 1 BC
1348 ;;;###autoload
1349 (defun calendar (&optional arg)
1350 "Display a three-month Gregorian calendar.
1351 The three months appear side by side, with the current month in
1352 the middle surrounded by the previous and next months. The
1353 cursor is put on today's date. If optional prefix argument ARG
1354 is non-nil, prompts for the central month and year.
1356 Once in the calendar window, future or past months can be moved
1357 into view. Arbitrary months can be displayed, or the calendar
1358 can be scrolled forward or backward. The cursor can be moved
1359 forward or backward by one day, one week, one month, or one year.
1360 All of these commands take prefix arguments which, when negative,
1361 cause movement in the opposite direction. For convenience, the
1362 digit keys and the minus sign are automatically prefixes. Use
1363 \\[describe-mode] for details of the key bindings in the calendar
1364 window.
1366 Displays the calendar in a separate window, or optionally in a
1367 separate frame, depending on the value of `calendar-setup'.
1369 If `view-diary-entries-initially' is non-nil, also displays the
1370 diary entries for the current date (or however many days
1371 `number-of-diary-entries' specifies). This variable can be
1372 overridden by `calendar-setup'. As well as being displayed,
1373 diary entries can also be marked on the calendar (see
1374 `mark-diary-entries-in-calendar').
1376 Runs the following hooks:
1378 `calendar-load-hook' - after loading calendar.el
1379 `today-visible-calendar-hook', `today-invisible-calendar-hook' - after
1380 generating a calendar, if today's date is visible or not, respectively
1381 `initial-calendar-window-hook' - after first creating a calendar
1383 This function is suitable for execution in a .emacs file."
1384 (interactive "P")
1385 ;; Avoid loading cal-x unless it will be used.
1386 (if (and (memq calendar-setup '(one-frame two-frames calendar-only))
1387 (display-multi-frame-p))
1388 (calendar-frame-setup calendar-setup arg)
1389 (calendar-basic-setup arg)))
1391 (defun calendar-basic-setup (&optional arg nodisplay)
1392 "Create a three-month calendar.
1393 If optional prefix argument ARG is non-nil, prompts for the month
1394 and year, else uses the current date. If NODISPLAY is non-nil, don't
1395 display the generated calendar."
1396 (interactive "P")
1397 (set-buffer (get-buffer-create calendar-buffer))
1398 (calendar-mode)
1399 (let* ((pop-up-windows t)
1400 (split-height-threshold 1000)
1401 (date (if arg (calendar-read-date t)
1402 (calendar-current-date)))
1403 (month (extract-calendar-month date))
1404 (year (extract-calendar-year date)))
1405 (increment-calendar-month month year (- calendar-offset))
1406 ;; Display the buffer before calling generate-calendar-window so that it
1407 ;; can get a chance to adjust the window sizes to the frame size.
1408 (or nodisplay (pop-to-buffer calendar-buffer))
1409 (generate-calendar-window month year)
1410 (if (and view-diary-entries-initially (calendar-date-is-visible-p date))
1411 (diary-view-entries)))
1412 (if view-calendar-holidays-initially
1413 (let* ((diary-buffer (get-file-buffer diary-file))
1414 (diary-window (if diary-buffer (get-buffer-window diary-buffer)))
1415 (split-height-threshold (if diary-window 2 1000)))
1416 ;; FIXME display buffer?
1417 (calendar-list-holidays)))
1418 (run-hooks 'initial-calendar-window-hook))
1420 (defun generate-calendar-window (&optional mon yr)
1421 "Generate the calendar window for the current date.
1422 Optional integers MON and YR are used instead of today's date."
1423 (let* ((inhibit-read-only t)
1424 (today (calendar-current-date))
1425 (month (extract-calendar-month today))
1426 (day (extract-calendar-day today))
1427 (year (extract-calendar-year today))
1428 (today-visible (or (not mon)
1429 (<= (abs (calendar-interval mon yr month year)) 1)))
1430 (day-in-week (calendar-day-of-week today))
1431 (in-calendar-window (eq (window-buffer (selected-window))
1432 (get-buffer calendar-buffer))))
1433 (generate-calendar (or mon month) (or yr year))
1434 (update-calendar-mode-line)
1435 (calendar-cursor-to-visible-date
1436 (if today-visible today (list displayed-month 1 displayed-year)))
1437 (set-buffer-modified-p nil)
1438 ;; Don't do any window-related stuff if we weren't called from a
1439 ;; window displaying the calendar.
1440 (when in-calendar-window
1441 (if (or (one-window-p t) (not (window-full-width-p)))
1442 ;; Don't mess with the window size, but ensure that the first
1443 ;; line is fully visible.
1444 (set-window-vscroll nil 0)
1445 ;; Adjust the window to exactly fit the displayed calendar.
1446 (fit-window-to-buffer nil nil calendar-minimum-window-height))
1447 (sit-for 0))
1448 (and (bound-and-true-p font-lock-mode)
1449 (font-lock-fontify-buffer))
1450 (and mark-holidays-in-calendar
1451 ;;; (calendar-date-is-valid-p today) ; useful for BC dates
1452 (calendar-mark-holidays)
1453 (and in-calendar-window (sit-for 0)))
1454 (unwind-protect
1455 (if mark-diary-entries-in-calendar (mark-diary-entries))
1456 (if today-visible
1457 (run-hooks 'today-visible-calendar-hook)
1458 (run-hooks 'today-invisible-calendar-hook)))))
1460 (defun generate-calendar (month year)
1461 "Generate a three-month Gregorian calendar centered around MONTH, YEAR."
1462 ;; A negative YEAR is interpreted as BC; -1 being 1 BC, and so on.
1463 ;; Note that while calendars for years BC could be displayed as it
1464 ;; stands, almost all other calendar functions (eg holidays) would
1465 ;; at best have unpredictable results for such dates.
1466 (if (< (+ month (* 12 (1- year))) 2)
1467 (error "Months before January, 1 AD cannot be displayed"))
1468 (setq displayed-month month
1469 displayed-year year)
1470 (erase-buffer)
1471 (increment-calendar-month month year -1)
1472 (dotimes (i 3)
1473 (generate-calendar-month month year (+ 5 (* 25 i)))
1474 (increment-calendar-month month year 1)))
1476 (defun generate-calendar-month (month year indent)
1477 "Produce a calendar for MONTH, YEAR on the Gregorian calendar.
1478 The calendar is inserted at the top of the buffer in which point is currently
1479 located, but indented INDENT spaces. The indentation is done from the first
1480 character on the line and does not disturb the first INDENT characters on the
1481 line."
1482 (let ((blank-days ; at start of month
1483 (mod
1484 (- (calendar-day-of-week (list month 1 year))
1485 calendar-week-start-day)
1487 (last (calendar-last-day-of-month month year))
1488 string)
1489 (goto-char (point-min))
1490 (calendar-insert-indented
1491 (calendar-string-spread
1492 (list (format "%s %d" (calendar-month-name month) year)) ? 20)
1493 indent t)
1494 (calendar-insert-indented "" indent) ; go to proper spot
1495 ;; Use the first two characters of each day to head the columns.
1496 (dotimes (i 7)
1497 (insert
1498 (progn
1499 (setq 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" 'calendar-mark-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 (defun calendar-mode ()
1793 "A major mode for the calendar window.
1795 For a complete description, type \
1796 \\<calendar-mode-map>\\[calendar-goto-info-node] from within the calendar.
1798 \\<calendar-mode-map>\\{calendar-mode-map}"
1799 (kill-all-local-variables)
1800 (setq major-mode 'calendar-mode
1801 mode-name "Calendar"
1802 buffer-read-only t
1803 buffer-undo-list t
1804 indent-tabs-mode nil)
1805 (use-local-map calendar-mode-map)
1806 (update-calendar-mode-line)
1807 (make-local-variable 'calendar-mark-ring)
1808 (make-local-variable 'displayed-month) ; month in middle of window
1809 (make-local-variable 'displayed-year) ; year in middle of window
1810 ;; Most functions only work if displayed-month and displayed-year are set,
1811 ;; so let's make sure they're always set. Most likely, this will be reset
1812 ;; soon in generate-calendar, but better safe than sorry.
1813 (unless (boundp 'displayed-month) (setq displayed-month 1))
1814 (unless (boundp 'displayed-year) (setq displayed-year 2001))
1815 (set (make-local-variable 'font-lock-defaults)
1816 '(calendar-font-lock-keywords t))
1817 (run-mode-hooks 'calendar-mode-hook))
1819 (defun calendar-string-spread (strings char length)
1820 "Concatenate list of STRINGS separated with copies of CHAR to fill LENGTH.
1821 The effect is like mapconcat but the separating pieces are as balanced as
1822 possible. Each item of STRINGS is evaluated before concatenation so it can
1823 actually be an expression that evaluates to a string. If LENGTH is too short,
1824 the STRINGS are just concatenated and the result truncated."
1825 ;; The algorithm is based on equation (3.25) on page 85 of Concrete
1826 ;; Mathematics by Ronald L. Graham, Donald E. Knuth, and Oren Patashnik,
1827 ;; Addison-Wesley, Reading, MA, 1989.
1828 (let* ((strings (mapcar 'eval
1829 (if (< (length strings) 2)
1830 (append (list "") strings (list ""))
1831 strings)))
1832 (n (- length (length (apply 'concat strings))))
1833 (m (1- (length strings)))
1834 (s (car strings))
1835 (strings (cdr strings))
1836 (i 0))
1837 (dolist (string strings)
1838 (setq s (concat s
1839 (make-string (max 0 (/ (+ n i) m)) char)
1840 string)
1841 i (1+ i)))
1842 (substring s 0 length)))
1844 (defun update-calendar-mode-line ()
1845 "Update the calendar mode line with the current date and date style."
1846 (if (bufferp (get-buffer calendar-buffer))
1847 (with-current-buffer calendar-buffer
1848 (setq mode-line-format
1849 (calendar-string-spread
1850 (let ((date (condition-case nil
1851 (calendar-cursor-to-nearest-date)
1852 (error (calendar-current-date)))))
1853 (mapcar 'eval calendar-mode-line-format))
1854 ? (frame-width)))
1855 (force-mode-line-update))))
1857 (defun calendar-window-list ()
1858 "List of all calendar-related windows."
1859 (let ((calendar-buffers (calendar-buffer-list))
1860 list)
1861 (walk-windows (lambda (w)
1862 (if (memq (window-buffer w) calendar-buffers)
1863 (push w list)))
1864 nil t)
1865 list))
1867 (defun calendar-buffer-list ()
1868 "List of all calendar-related buffers (as buffers, not strings)."
1869 (let (buffs)
1870 (dolist (b (list cal-hebrew-yahrzeit-buffer lunar-phases-buffer
1871 holiday-buffer fancy-diary-buffer
1872 (get-file-buffer diary-file)
1873 calendar-buffer other-calendars-buffer))
1874 (and b (setq b (get-buffer b))
1875 (push b buffs)))
1876 buffs))
1878 (defun exit-calendar ()
1879 "Get out of the calendar window and hide it and related buffers."
1880 (interactive)
1881 (let ((diary-buffer (get-file-buffer diary-file)))
1882 (if (or (not diary-buffer)
1883 (not (buffer-modified-p diary-buffer))
1884 (yes-or-no-p
1885 "Diary modified; do you really want to exit the calendar? "))
1886 ;; Need to do this multiple times because one time can replace some
1887 ;; calendar-related buffers with other calendar-related buffers.
1888 (mapc (lambda (x)
1889 (mapc 'calendar-hide-window (calendar-window-list)))
1890 (calendar-window-list)))))
1892 (defun calendar-hide-window (window)
1893 "Hide WINDOW if it is calendar-related."
1894 (let ((buffer (if (window-live-p window) (window-buffer window))))
1895 (if (memq buffer (calendar-buffer-list))
1896 (cond
1897 ((and (display-multi-frame-p)
1898 (eq 'icon (cdr (assoc 'visibility
1899 (frame-parameters
1900 (window-frame window))))))
1901 nil)
1902 ((and (display-multi-frame-p) (window-dedicated-p window))
1903 (if calendar-remove-frame-by-deleting
1904 (delete-frame (window-frame window))
1905 (iconify-frame (window-frame window))))
1906 ((not (and (select-window window) (one-window-p window)))
1907 (delete-window window))
1908 (t (set-buffer buffer)
1909 (bury-buffer))))))
1911 (defun calendar-current-date ()
1912 "Return the current date in a list (month day year)."
1913 (let ((now (decode-time)))
1914 (list (nth 4 now) (nth 3 now) (nth 5 now))))
1916 (defun calendar-cursor-to-date (&optional error)
1917 "Return a list (month day year) of current cursor position.
1918 If cursor is not on a specific date, signals an error if optional parameter
1919 ERROR is non-nil, otherwise just returns nil."
1920 (let* ((segment (/ (current-column) 25))
1921 (month (% (+ displayed-month segment -1) 12))
1922 (month (if (zerop month) 12 month))
1923 (year
1924 (cond
1925 ((and (= 12 month) (zerop segment)) (1- displayed-year))
1926 ((and (= 1 month) (= segment 2)) (1+ displayed-year))
1927 (t displayed-year))))
1928 (if (and (looking-at "[ 0-9]?[0-9][^0-9]")
1929 (< 2 (count-lines (point-min) (point))))
1930 (save-excursion
1931 (if (not (looking-at " "))
1932 (re-search-backward "[^0-9]"))
1933 (list month
1934 (string-to-number (buffer-substring (1+ (point)) (+ 4 (point))))
1935 year))
1936 (if error (error "Not on a date!")))))
1938 (add-to-list 'debug-ignored-errors "Not on a date!")
1940 ;; The following version of calendar-gregorian-from-absolute is preferred for
1941 ;; reasons of clarity, BUT it's much slower than the version that follows it.
1943 ;;(defun calendar-gregorian-from-absolute (date)
1944 ;; "Compute the list (month day year) corresponding to the absolute DATE.
1945 ;;The absolute date is the number of days elapsed since the (imaginary)
1946 ;;Gregorian date Sunday, December 31, 1 BC."
1947 ;; (let* ((approx (/ date 366)) ; approximation from below
1948 ;; (year ; search forward from the approximation
1949 ;; (+ approx
1950 ;; (calendar-sum y approx
1951 ;; (>= date (calendar-absolute-from-gregorian (list 1 1 (1+ y))))
1952 ;; 1)))
1953 ;; (month ; search forward from January
1954 ;; (1+ (calendar-sum m 1
1955 ;; (> date
1956 ;; (calendar-absolute-from-gregorian
1957 ;; (list m (calendar-last-day-of-month m year) year)))
1958 ;; 1)))
1959 ;; (day ; calculate the day by subtraction
1960 ;; (- date
1961 ;; (1- (calendar-absolute-from-gregorian (list month 1 year))))))
1962 ;; (list month day year)))
1964 (defun calendar-gregorian-from-absolute (date)
1965 "Compute the list (month day year) corresponding to the absolute DATE.
1966 The absolute date is the number of days elapsed since the (imaginary)
1967 Gregorian date Sunday, December 31, 1 BC. This function does not
1968 handle dates in years BC."
1969 ;; See the footnote on page 384 of ``Calendrical Calculations, Part II:
1970 ;; Three Historical Calendars'' by E. M. Reingold, N. Dershowitz, and S. M.
1971 ;; Clamen, Software--Practice and Experience, Volume 23, Number 4
1972 ;; (April, 1993), pages 383-404 for an explanation.
1973 (let* ((d0 (1- date))
1974 (n400 (/ d0 146097))
1975 (d1 (% d0 146097))
1976 (n100 (/ d1 36524))
1977 (d2 (% d1 36524))
1978 (n4 (/ d2 1461))
1979 (d3 (% d2 1461))
1980 (n1 (/ d3 365))
1981 (day (1+ (% d3 365)))
1982 (year (+ (* 400 n400) (* 100 n100) (* n4 4) n1))
1983 (month 1)
1984 mdays)
1985 (if (or (= n100 4) (= n1 4))
1986 (list 12 31 year)
1987 (setq year (1+ year))
1988 (while (< (setq mdays (calendar-last-day-of-month month year)) day)
1989 (setq day (- day mdays)
1990 month (1+ month)))
1991 (list month day year))))
1993 (defun calendar-other-month (month year)
1994 "Display a three-month calendar centered around MONTH and YEAR."
1995 (interactive (calendar-read-date 'noday))
1996 (unless (and (= month displayed-month)
1997 (= year displayed-year))
1998 (let ((old-date (calendar-cursor-to-date))
1999 (today (calendar-current-date)))
2000 (generate-calendar-window month year)
2001 (calendar-cursor-to-visible-date
2002 (cond
2003 ((calendar-date-is-visible-p old-date) old-date)
2004 ((calendar-date-is-visible-p today) today)
2005 (t (list month 1 year)))))))
2007 (defun calendar-set-mark (arg)
2008 "Mark the date under the cursor, or jump to marked date.
2009 With no prefix argument, push current date onto marked date ring.
2010 With argument ARG, jump to mark, pop it, and put point at end of ring."
2011 (interactive "P")
2012 (let ((date (calendar-cursor-to-date t)))
2013 (if arg
2014 (if (null calendar-mark-ring)
2015 (error "No mark set in this buffer")
2016 (calendar-goto-date (car calendar-mark-ring))
2017 (setq calendar-mark-ring
2018 (cdr (nconc calendar-mark-ring (list date)))))
2019 (push date calendar-mark-ring)
2020 ;; Since the top of the mark ring is the marked date in the
2021 ;; calendar, the mark ring in the calendar is one longer than
2022 ;; in other buffers to get the same effect.
2023 (if (> (length calendar-mark-ring) (1+ mark-ring-max))
2024 (setcdr (nthcdr mark-ring-max calendar-mark-ring) nil))
2025 (message "Mark set"))))
2027 (defun calendar-exchange-point-and-mark ()
2028 "Exchange the current cursor position with the marked date."
2029 (interactive)
2030 (let ((mark (car calendar-mark-ring))
2031 (date (calendar-cursor-to-date t)))
2032 (if (null mark)
2033 (error "No mark set in this buffer")
2034 (setq calendar-mark-ring (cons date (cdr calendar-mark-ring)))
2035 (calendar-goto-date mark))))
2037 (defun calendar-count-days-region ()
2038 "Count the number of days (inclusive) between point and the mark."
2039 (interactive)
2040 (let* ((days (- (calendar-absolute-from-gregorian
2041 (calendar-cursor-to-date t))
2042 (calendar-absolute-from-gregorian
2043 (or (car calendar-mark-ring)
2044 (error "No mark set in this buffer")))))
2045 (days (1+ (if (> days 0) days (- days)))))
2046 (message "Region has %d day%s (inclusive)"
2047 days (if (> days 1) "s" ""))))
2049 (defun calendar-not-implemented ()
2050 "Not implemented."
2051 (interactive)
2052 (error "%s not available in the calendar"
2053 (global-key-binding (this-command-keys))))
2055 (defun calendar-read (prompt acceptable &optional initial-contents)
2056 "Return an object read from the minibuffer.
2057 Prompt with the string PROMPT and use the function ACCEPTABLE to decide if
2058 entered item is acceptable. If non-nil, optional third arg INITIAL-CONTENTS
2059 is a string to insert in the minibuffer before reading."
2060 (let ((value (read-minibuffer prompt initial-contents)))
2061 (while (not (funcall acceptable value))
2062 (setq value (read-minibuffer prompt initial-contents)))
2063 value))
2066 (defvar calendar-abbrev-length 3
2067 "*Length of abbreviations to be used for day and month names.
2068 See also `calendar-day-abbrev-array' and `calendar-month-abbrev-array'.")
2070 ;; FIXME does it have to start from Sunday?
2071 (defcustom calendar-day-name-array
2072 ["Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday"]
2073 "Array of capitalized strings giving, in order, the day names.
2074 The first two characters of each string will be used to head the
2075 day columns in the calendar. See also the variable
2076 `calendar-day-abbrev-array'."
2077 :group 'calendar
2078 :type '(vector (string :tag "Sunday")
2079 (string :tag "Monday")
2080 (string :tag "Tuesday")
2081 (string :tag "Wednesday")
2082 (string :tag "Thursday")
2083 (string :tag "Friday")
2084 (string :tag "Saturday")))
2086 (defvar calendar-day-abbrev-array
2087 [nil nil nil nil nil nil nil]
2088 "*Array of capitalized strings giving the abbreviated day names.
2089 The order should be the same as that of the full names specified
2090 in `calendar-day-name-array'. These abbreviations may be used
2091 instead of the full names in the diary file. Do not include a
2092 trailing `.' in the strings specified in this variable, though
2093 you may use such in the diary file. If any element of this array
2094 is nil, then the abbreviation will be constructed as the first
2095 `calendar-abbrev-length' characters of the corresponding full name.")
2097 (defcustom calendar-month-name-array
2098 ["January" "February" "March" "April" "May" "June"
2099 "July" "August" "September" "October" "November" "December"]
2100 "Array of capitalized strings giving, in order, the month names.
2101 See also the variable `calendar-month-abbrev-array'."
2102 :group 'calendar
2103 :type '(vector (string :tag "January")
2104 (string :tag "February")
2105 (string :tag "March")
2106 (string :tag "April")
2107 (string :tag "May")
2108 (string :tag "June")
2109 (string :tag "July")
2110 (string :tag "August")
2111 (string :tag "September")
2112 (string :tag "October")
2113 (string :tag "November")
2114 (string :tag "December")))
2116 (defvar calendar-month-abbrev-array
2117 [nil nil nil nil nil nil nil nil nil nil nil nil]
2118 "*Array of capitalized strings giving the abbreviated month names.
2119 The order should be the same as that of the full names specified
2120 in `calendar-month-name-array'. These abbreviations are used in
2121 the calendar menu entries, and can also be used in the diary
2122 file. Do not include a trailing `.' in the strings specified in
2123 this variable, though you may use such in the diary file. If any
2124 element of this array is nil, then the abbreviation will be
2125 constructed as the first `calendar-abbrev-length' characters of the
2126 corresponding full name.")
2128 (defun calendar-make-alist (sequence &optional start-index filter abbrevs)
2129 "Make an assoc list corresponding to SEQUENCE.
2130 Each element of sequence will be associated with an integer, starting
2131 from 1, or from START-INDEX if that is non-nil. If a sequence ABBREVS
2132 is supplied, the function `calendar-abbrev-construct' is used to
2133 construct abbreviations corresponding to the elements in SEQUENCE.
2134 Each abbreviation is entered into the alist with the same
2135 association index as the full name it represents.
2136 If FILTER is provided, apply it to each key in the alist."
2137 (let ((index 0)
2138 (offset (or start-index 1))
2139 (aseq (if abbrevs (calendar-abbrev-construct abbrevs sequence)))
2140 (aseqp (if abbrevs (calendar-abbrev-construct abbrevs sequence
2141 'period)))
2142 alist elem)
2143 (dotimes (i (length sequence) (reverse alist))
2144 (setq index (+ i offset)
2145 elem (elt sequence i)
2146 alist
2147 (cons (cons (if filter (funcall filter elem) elem) index) alist))
2148 (if aseq
2149 (setq elem (elt aseq i)
2150 alist (cons (cons (if filter (funcall filter elem) elem)
2151 index) alist)))
2152 (if aseqp
2153 (setq elem (elt aseqp i)
2154 alist (cons (cons (if filter (funcall filter elem) elem)
2155 index) alist))))))
2157 (defun calendar-read-date (&optional noday)
2158 "Prompt for Gregorian date. Return a list (month day year).
2159 If optional NODAY is t, does not ask for day, but just returns
2160 \(month 1 year); if NODAY is any other non-nil value the value returned is
2161 \(month year)"
2162 (let* ((year (calendar-read
2163 "Year (>0): "
2164 (lambda (x) (> x 0))
2165 (int-to-string (extract-calendar-year
2166 (calendar-current-date)))))
2167 (month-array calendar-month-name-array)
2168 (completion-ignore-case t)
2169 (month (cdr (assoc-string
2170 (completing-read
2171 "Month name: "
2172 (mapcar 'list (append month-array nil))
2173 nil t)
2174 (calendar-make-alist month-array 1) t)))
2175 (last (calendar-last-day-of-month month year)))
2176 (if noday
2177 (if (eq noday t)
2178 (list month 1 year)
2179 (list month year))
2180 (list month
2181 (calendar-read (format "Day (1-%d): " last)
2182 (lambda (x) (and (< 0 x) (<= x last))))
2183 year))))
2185 (defun calendar-interval (mon1 yr1 mon2 yr2)
2186 "The number of months difference between MON1, YR1 and MON2, YR2.
2187 The result is positive if the second date is later than the first.
2188 Negative years are interpreted as years BC; -1 being 1 BC, and so on."
2189 (if (< yr1 0) (setq yr1 (1+ yr1))) ; -1 BC -> 0 AD, etc
2190 (if (< yr2 0) (setq yr2 (1+ yr2)))
2191 (+ (* 12 (- yr2 yr1))
2192 (- mon2 mon1)))
2194 (defun calendar-abbrev-construct (abbrev full &optional period)
2195 "Internal calendar function to return a complete abbreviation array.
2196 ABBREV is an array of abbreviations, FULL the corresponding array
2197 of full names. The return value is the ABBREV array, with any nil
2198 elements replaced by the first three characters taken from the
2199 corresponding element of FULL. If optional argument PERIOD is non-nil,
2200 each element returned has a final `.' character."
2201 (let (elem array name)
2202 (dotimes (i (length full))
2203 (setq name (aref full i)
2204 elem (or (aref abbrev i)
2205 (substring name 0
2206 (min calendar-abbrev-length (length name))))
2207 elem (format "%s%s" elem (if period "." ""))
2208 array (append array (list elem))))
2209 (vconcat array)))
2211 (defvar calendar-font-lock-keywords
2212 `((,(concat (regexp-opt (mapcar 'identity calendar-month-name-array) t)
2213 " -?[0-9]+")
2214 . font-lock-function-name-face) ; month and year
2215 (,(regexp-opt
2216 (list (substring (aref calendar-day-name-array 6) 0 2)
2217 (substring (aref calendar-day-name-array 0) 0 2)))
2218 ;; Saturdays and Sundays are highlighted differently.
2219 . font-lock-comment-face)
2220 ;; First two chars of each day are used in the calendar.
2221 (,(regexp-opt (mapcar (lambda (x) (substring x 0 2))
2222 calendar-day-name-array))
2223 . font-lock-reference-face))
2224 "Default keywords to highlight in Calendar mode.")
2226 (defun calendar-day-name (date &optional abbrev absolute)
2227 "Return a string with the name of the day of the week of DATE.
2228 DATE should be a list in the format (MONTH DAY YEAR), unless the
2229 optional argument ABSOLUTE is non-nil, in which case DATE should
2230 be an integer in the range 0 to 6 corresponding to the day of the
2231 week. Day names are taken from the variable `calendar-day-name-array',
2232 unless the optional argument ABBREV is non-nil, in which case
2233 the variable `calendar-day-abbrev-array' is used."
2234 (aref (if abbrev
2235 (calendar-abbrev-construct calendar-day-abbrev-array
2236 calendar-day-name-array)
2237 calendar-day-name-array)
2238 (if absolute date (calendar-day-of-week date))))
2240 (defun calendar-month-name (month &optional abbrev)
2241 "Return a string with the name of month number MONTH.
2242 Months are numbered from one. Month names are taken from the
2243 variable `calendar-month-name-array', unless the optional
2244 argument ABBREV is non-nil, in which case
2245 `calendar-month-abbrev-array' is used."
2246 (aref (if abbrev
2247 (calendar-abbrev-construct calendar-month-abbrev-array
2248 calendar-month-name-array)
2249 calendar-month-name-array)
2250 (1- month)))
2252 (defun calendar-day-of-week (date)
2253 "Return the day-of-the-week index of DATE, 0 for Sunday, 1 for Monday, etc.
2254 DATE is a list of the form (month day year). A negative year is
2255 interpreted as BC; -1 being 1 BC, and so on."
2256 (mod (calendar-absolute-from-gregorian date) 7))
2258 (defun calendar-unmark ()
2259 "Delete all diary/holiday marks/highlighting from the calendar."
2260 (interactive)
2261 (setq mark-holidays-in-calendar nil
2262 mark-diary-entries-in-calendar nil)
2263 (with-current-buffer calendar-buffer
2264 (mapc 'delete-overlay (overlays-in (point-min) (point-max)))))
2266 (defun calendar-date-is-visible-p (date)
2267 "Return non-nil if DATE is valid and is visible in the calendar window."
2268 (and (calendar-date-is-valid-p date)
2269 (< (abs (calendar-interval
2270 displayed-month displayed-year
2271 (extract-calendar-month date) (extract-calendar-year date)))
2272 2)))
2274 (defun calendar-nongregorian-visible-p (month day toabs fromabs switch)
2275 "Return non-nil if MONTH, DAY is visible in the calendar window.
2276 MONTH and DAY are in some non-Gregorian calendar system. The
2277 functions TOABS and FROMABS convert that system to and from
2278 absolute, respectively. SWITCH is a function that takes a single
2279 argument (a local month number). It applies when the local year
2280 changes across the calendar window, and returns non-nil if the
2281 specified month should be associated with the higher year.
2282 Returns the corresponding Gregorian date."
2283 ;; We need to choose the local year associated with month and day
2284 ;; that might make them visible.
2285 (let* ((m1 displayed-month)
2286 (y1 displayed-year)
2287 (m2 displayed-month)
2288 (y2 displayed-year)
2289 ;; Absolute date of first/last dates in calendar window.
2290 (start-date (progn
2291 (increment-calendar-month m1 y1 -1)
2292 (calendar-absolute-from-gregorian (list m1 1 y1))))
2293 (end-date (progn
2294 (increment-calendar-month m2 y2 1)
2295 (calendar-absolute-from-gregorian
2296 (list m2 (calendar-last-day-of-month m2 y2) y2))))
2297 ;; Local date of first/last date in calendar window.
2298 (local-start (funcall fromabs start-date))
2299 (local-end (funcall fromabs end-date))
2300 ;; Local year of first/last dates.
2301 ;; Can only differ if displayed-month = 12, 1, 2.
2302 (local-y1 (extract-calendar-year local-start))
2303 (local-y2 (extract-calendar-year local-end))
2304 ;; Choose which year might be visible in the window.
2305 ;; Obviously it only matters when y1 and y2 differ, ie
2306 ;; when the _local_ new year is visible.
2307 (year (if (funcall switch month) local-y2 local-y1))
2308 (date (calendar-gregorian-from-absolute
2309 (funcall toabs (list month day year)))))
2310 (if (calendar-date-is-visible-p date)
2311 date)))
2313 (defun calendar-date-is-valid-p (date)
2314 "Return t if DATE is a valid date."
2315 (let ((month (extract-calendar-month date))
2316 (day (extract-calendar-day date))
2317 (year (extract-calendar-year date)))
2318 (and (<= 1 month) (<= month 12)
2319 ;; (calendar-read-date t) used to return a date with day = nil.
2320 ;; Should not be valid (?), since many funcs prob assume integer.
2321 ;; (calendar-read-date 'noday) returns (month year), which
2322 ;; currently results in extract-calendar-year returning nil.
2323 day year (<= 1 day) (<= day (calendar-last-day-of-month month year))
2324 ;; BC dates left as non-valid, to suppress errors from
2325 ;; complex holiday algorithms not suitable for years BC.
2326 ;; Note there are side effects on calendar navigation.
2327 (<= 1 year))))
2329 (define-obsolete-function-alias 'calendar-date-is-legal-p
2330 'calendar-date-is-valid-p "23.1")
2332 (defun calendar-date-equal (date1 date2)
2333 "Return t if the DATE1 and DATE2 are the same."
2334 (and
2335 (= (extract-calendar-month date1) (extract-calendar-month date2))
2336 (= (extract-calendar-day date1) (extract-calendar-day date2))
2337 (= (extract-calendar-year date1) (extract-calendar-year date2))))
2339 (defun calendar-make-temp-face (attrlist)
2340 "Return a temporary face based on the attributes in ATTRLIST.
2341 ATTRLIST is a list with elements of the form :face face :foreground color."
2342 (let ((attrs attrlist)
2343 faceinfo face temp-face)
2344 ;; Separate :face from the other attributes. Use the last :face
2345 ;; if there are more than one. FIXME is merging meaningful?
2346 (while attrs
2347 (if (eq (car attrs) :face)
2348 (setq face (intern-soft (cadr attrs))
2349 attrs (cddr attrs))
2350 (push (car attrs) faceinfo)
2351 (setq attrs (cdr attrs))))
2352 (or (facep face) (setq face 'default))
2353 (if (not faceinfo)
2354 ;; No attributes to apply, so just use an existing-face.
2355 face
2356 ;; FIXME should we be using numbered temp-faces, re-using where poss?
2357 (setq temp-face
2358 (make-symbol
2359 (concat ":caltemp"
2360 (mapconcat (lambda (sym)
2361 (cond
2362 ((symbolp sym) (symbol-name sym))
2363 ((numberp sym) (number-to-string sym))
2364 (t sym)))
2365 attrlist ""))))
2366 (make-face temp-face)
2367 (copy-face face temp-face)
2368 ;; Apply the font aspects.
2369 (apply 'set-face-attribute temp-face nil (nreverse faceinfo))
2370 temp-face)))
2372 (defun mark-visible-calendar-date (date &optional mark)
2373 "Mark DATE in the calendar window with MARK.
2374 MARK is a single-character string, a list of face attributes/values, or a face.
2375 MARK defaults to `diary-entry-marker'."
2376 (if (calendar-date-is-valid-p date)
2377 (with-current-buffer calendar-buffer
2378 (save-excursion
2379 (calendar-cursor-to-visible-date date)
2380 (setq mark
2381 (or (and (stringp mark) (= (length mark) 1) mark) ; single-char
2382 (and font-lock-mode
2384 (and (listp mark) (> (length mark) 0) mark) ; attrs
2385 (and (facep mark) mark))) ; face-name
2386 diary-entry-marker))
2387 (cond
2388 ;; Face or an attr-list that contained a face.
2389 ((facep mark)
2390 (overlay-put
2391 (make-overlay (1- (point)) (1+ (point))) 'face mark))
2392 ;; Single-character mark, goes after the date.
2393 ((and (stringp mark) (= (length mark) 1))
2394 (overlay-put
2395 (make-overlay (1+ (point)) (+ 2 (point))) 'display mark))
2396 (t ; attr list
2397 (overlay-put
2398 (make-overlay (1- (point)) (1+ (point))) 'face
2399 (calendar-make-temp-face mark))))))))
2401 (defun calendar-star-date ()
2402 "Replace the date under the cursor in the calendar window with asterisks.
2403 You might want to add this function to `today-visible-calendar-hook'."
2404 (unless (catch 'found
2405 (dolist (ol (overlays-at (point)))
2406 (and (overlay-get ol 'calendar-star)
2407 (throw 'found t))))
2408 (let ((ol (make-overlay (1- (point)) (point))))
2409 (overlay-put ol 'display "*")
2410 (overlay-put ol 'calendar-star t)
2411 ;; Use copy-sequence to avoid merging of identical 'display props.
2412 ;; Use two overlays so as not to mess up
2413 ;; calendar-cursor-to-nearest-date (and calendar-forward-day).
2414 (overlay-put (setq ol (make-overlay (point) (1+ (point))))
2415 'display (copy-sequence "*"))
2416 (overlay-put ol 'calendar-star t))))
2418 (defun calendar-mark-today ()
2419 "Mark the date under the cursor in the calendar window.
2420 The date is marked with `calendar-today-marker'. You might want to add
2421 this function to `today-visible-calendar-hook'."
2422 (mark-visible-calendar-date (calendar-cursor-to-date) calendar-today-marker))
2424 (defun calendar-date-compare (date1 date2)
2425 "Return t if DATE1 is before DATE2, nil otherwise.
2426 The actual dates are in the car of DATE1 and DATE2."
2427 (< (calendar-absolute-from-gregorian (car date1))
2428 (calendar-absolute-from-gregorian (car date2))))
2430 (defun calendar-date-string (date &optional abbreviate nodayname)
2431 "A string form of DATE, driven by the variable `calendar-date-display-form'.
2432 An optional parameter ABBREVIATE, when non-nil, causes the month
2433 and day names to be abbreviated as specified by
2434 `calendar-month-abbrev-array' and `calendar-day-abbrev-array',
2435 respectively. An optional parameter NODAYNAME, when t, omits the
2436 name of the day of the week."
2437 (let* ((dayname (unless nodayname (calendar-day-name date abbreviate)))
2438 (month (extract-calendar-month date))
2439 (monthname (calendar-month-name month abbreviate))
2440 (day (int-to-string (extract-calendar-day date)))
2441 (month (int-to-string month))
2442 (year (int-to-string (extract-calendar-year date))))
2443 (mapconcat 'eval calendar-date-display-form "")))
2445 (defun calendar-dayname-on-or-before (dayname date)
2446 "Return the absolute date of the DAYNAME on or before absolute DATE.
2447 DAYNAME=0 means Sunday, DAYNAME=1 means Monday, and so on.
2449 Note: Applying this function to d+6 gives us the DAYNAME on or after an
2450 absolute day d. Similarly, applying it to d+3 gives the DAYNAME nearest to
2451 absolute date d, applying it to d-1 gives the DAYNAME previous to absolute
2452 date d, and applying it to d+7 gives the DAYNAME following absolute date d."
2453 (- date (% (- date dayname) 7)))
2455 (defun calendar-nth-named-absday (n dayname month year &optional day)
2456 "The absolute date of Nth DAYNAME in MONTH, YEAR before/after optional DAY.
2457 A DAYNAME of 0 means Sunday, 1 means Monday, and so on. If N<0,
2458 return the Nth DAYNAME before MONTH DAY, YEAR (inclusive).
2459 If N>0, return the Nth DAYNAME after MONTH DAY, YEAR (inclusive).
2461 If DAY is omitted, it defaults to 1 if N>0, and MONTH's last day otherwise."
2462 (if (> n 0)
2463 (+ (* 7 (1- n))
2464 (calendar-dayname-on-or-before
2465 dayname
2466 (+ 6 (calendar-absolute-from-gregorian
2467 (list month (or day 1) year)))))
2468 (+ (* 7 (1+ n))
2469 (calendar-dayname-on-or-before
2470 dayname
2471 (calendar-absolute-from-gregorian
2472 (list month
2473 (or day (calendar-last-day-of-month month year))
2474 year))))))
2476 (defun calendar-nth-named-day (n dayname month year &optional day)
2477 "The date of Nth DAYNAME in MONTH, YEAR before/after optional DAY.
2478 Like `calendar-nth-named-absday', but returns a Gregorian date."
2479 (calendar-gregorian-from-absolute
2480 (calendar-nth-named-absday n dayname month year day)))
2482 (defun calendar-day-of-year-string (&optional date)
2483 "String of day number of year of Gregorian DATE.
2484 Defaults to today's date if DATE is not given."
2485 (let* ((d (or date (calendar-current-date)))
2486 (year (extract-calendar-year d))
2487 (day (calendar-day-number d))
2488 (days-remaining (- (calendar-day-number (list 12 31 year)) day)))
2489 (format "Day %d of %d; %d day%s remaining in the year"
2490 day year days-remaining (if (= days-remaining 1) "" "s"))))
2492 (defun calendar-other-dates (date)
2493 "Return a list of strings giving Gregorian DATE in other calendars.
2494 DATE is (month day year). Calendars that do not apply are omitted."
2495 (let (odate)
2496 (delq nil
2497 (list
2498 (calendar-day-of-year-string date)
2499 (format "ISO date: %s" (calendar-iso-date-string date))
2500 (format "Julian date: %s"
2501 (calendar-julian-date-string date))
2502 (format "Astronomical (Julian) day number (at noon UTC): %s.0"
2503 (calendar-astro-date-string date))
2504 (format "Fixed (RD) date: %s"
2505 (calendar-absolute-from-gregorian date))
2506 (format "Hebrew date (before sunset): %s"
2507 (calendar-hebrew-date-string date))
2508 (format "Persian date: %s"
2509 (calendar-persian-date-string date))
2510 (unless (string-equal
2511 (setq odate (calendar-islamic-date-string date))
2513 (format "Islamic date (before sunset): %s" odate))
2514 (unless (string-equal
2515 (setq odate (calendar-bahai-date-string date))
2517 (format "Baha'i date: %s" odate))
2518 (format "Chinese date: %s"
2519 (calendar-chinese-date-string date))
2520 (unless (string-equal
2521 (setq odate (calendar-coptic-date-string date))
2523 (format "Coptic date: %s" odate))
2524 (unless (string-equal
2525 (setq odate (calendar-ethiopic-date-string date))
2527 (format "Ethiopic date: %s" odate))
2528 (unless (string-equal
2529 (setq odate (calendar-french-date-string date))
2531 (format "French Revolutionary date: %s" odate))
2532 (format "Mayan date: %s"
2533 (calendar-mayan-date-string date))))))
2535 (defun calendar-print-other-dates ()
2536 "Show dates on other calendars for date under the cursor."
2537 (interactive)
2538 (let ((date (calendar-cursor-to-date t)))
2539 (calendar-in-read-only-buffer other-calendars-buffer
2540 (calendar-set-mode-line (format "%s (Gregorian)"
2541 (calendar-date-string date)))
2542 (insert (mapconcat 'identity (calendar-other-dates date) "\n")))))
2544 (defun calendar-print-day-of-year ()
2545 "Show day number in year/days remaining in year for date under the cursor."
2546 (interactive)
2547 (message "%s" (calendar-day-of-year-string (calendar-cursor-to-date t))))
2549 (defun calendar-set-mode-line (str)
2550 "Set mode line to STR, centered, surrounded by dashes."
2551 (let* ((edges (window-edges))
2552 ;; As per doc of window-width, total visible mode-line length.
2553 (width (- (nth 2 edges) (car edges))))
2554 (setq mode-line-format
2555 (if buffer-file-name
2556 `("-" mode-line-modified
2557 ,(calendar-string-spread (list str) ?- (- width 6))
2558 "---")
2559 (calendar-string-spread (list str) ?- width)))))
2561 (defun calendar-version ()
2562 "Display the Calendar version."
2563 (interactive)
2564 (message "GNU Emacs %s" emacs-version))
2566 (make-obsolete 'calendar-version 'emacs-version "23.1")
2569 (run-hooks 'calendar-load-hook)
2571 (provide 'calendar)
2573 ;; Local variables:
2574 ;; byte-compile-dynamic: t
2575 ;; End:
2577 ;; arch-tag: 19c61596-c8fb-4c69-bcf1-7dd739919cd8
2578 ;;; calendar.el ends here