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