* lisp/progmodes/cc-defs.el (c--macroexpand-all): New function.
[emacs/old-mirror.git] / lisp / calendar / calendar.el
blob3fb984525790f9a04e5f4a9021aeed2a7c1ff67a
1 ;;; calendar.el --- calendar functions
3 ;; Copyright (C) 1988-1995, 1997, 2000-2014 Free Software Foundation, Inc.
5 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
6 ;; Maintainer: Glenn Morris <rgm@gnu.org>
7 ;; Keywords: calendar
8 ;; Human-Keywords: calendar, Gregorian calendar, diary, holidays
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; This collection of functions implements a calendar window. It
28 ;; generates a calendar for the current month, together with the
29 ;; previous and coming months, or for any other three-month period.
30 ;; The calendar can be scrolled forward and backward in the window to
31 ;; show months in the past or future; the cursor can move forward and
32 ;; backward by days, weeks, or months, making it possible, for
33 ;; instance, to jump to the date a specified number of days, weeks, or
34 ;; months from the date under the cursor. The user can display a list
35 ;; of holidays and other notable days for the period shown; the
36 ;; notable days can be marked on the calendar, if desired. The user
37 ;; can also specify that dates having corresponding diary entries (in
38 ;; a file that the user specifies) be marked; the diary entries for
39 ;; any date can be viewed in a separate window. The diary and the
40 ;; notable days can be viewed independently of the calendar. Dates
41 ;; can be translated from the (usual) Gregorian calendar to the day of
42 ;; the year/days remaining in year, to the ISO commercial calendar, to
43 ;; the Julian (old style) calendar, to the Hebrew calendar, to the
44 ;; Islamic calendar, to the Bahá'í calendar, to the French
45 ;; Revolutionary calendar, to the Mayan calendar, to the Chinese
46 ;; calendar, to the Coptic calendar, to the Ethiopic calendar, and to
47 ;; the astronomical (Julian) day number. Times of sunrise/sunset can
48 ;; be displayed, as can the phases of the moon. Appointment
49 ;; notification for diary entries is available. Calendar printing via
50 ;; LaTeX is available.
52 ;; The following files are part of the calendar/diary code:
54 ;; appt.el Appointment notification
55 ;; cal-bahai.el Bahá'í calendar
56 ;; cal-china.el Chinese calendar
57 ;; cal-coptic.el Coptic/Ethiopic calendars
58 ;; cal-dst.el Daylight saving time rules
59 ;; cal-french.el French revolutionary calendar
60 ;; cal-hebrew.el Hebrew calendar
61 ;; cal-html.el Calendars in HTML
62 ;; cal-islam.el Islamic calendar
63 ;; cal-iso.el ISO calendar
64 ;; cal-julian.el Julian/astronomical calendars
65 ;; cal-mayan.el Mayan calendars
66 ;; cal-menu.el Menu support
67 ;; cal-move.el Movement in the calendar
68 ;; cal-persia.el Persian calendar
69 ;; cal-tex.el Calendars in LaTeX
70 ;; cal-x.el Dedicated frame functions
71 ;; calendar.el This file
72 ;; diary-lib.el Diary functions
73 ;; holidays.el Holiday functions
74 ;; lunar.el Phases of the moon
75 ;; solar.el Sunrise/sunset, equinoxes/solstices
77 ;; Technical details of all the calendrical calculations can be found in
78 ;; ``Calendrical Calculations: The Millennium Edition'' by Edward M. Reingold
79 ;; and Nachum Dershowitz, Cambridge University Press (2001).
81 ;; An earlier version of the technical details appeared in
82 ;; ``Calendrical Calculations'' by Nachum Dershowitz and Edward M. Reingold,
83 ;; Software--Practice and Experience, Volume 20, Number 9 (September, 1990),
84 ;; pages 899-928, and in ``Calendrical Calculations, Part II: Three Historical
85 ;; Calendars'' by E. M. Reingold, N. Dershowitz, and S. M. Clamen,
86 ;; Software--Practice and Experience, Volume 23, Number 4 (April, 1993),
87 ;; pages 383-404.
89 ;; Hard copies of these two papers can be obtained by sending email to
90 ;; reingold@cs.uiuc.edu with the SUBJECT "send-paper-cal" (no quotes) and
91 ;; the message BODY containing your mailing address (snail).
94 ;; A note on free variables:
96 ;; The calendar passes around a few dynamically bound variables, which
97 ;; unfortunately have rather common names. They are meant to be
98 ;; available for external functions, so the names can't be changed.
100 ;; displayed-month, displayed-year: bound in calendar-generate, the
101 ;; central month of the 3 month calendar window
102 ;; original-date, number: bound in diary-list-entries, the arguments
103 ;; with which that function was called.
104 ;; date, entry: bound in diary-list-sexp-entries (qv)
106 ;; Bound in diary-list-entries:
107 ;; diary-entries-list: use in d-l, appt.el, and by diary-add-to-list
108 ;; diary-saved-point: only used in diary-lib.el, passed to the display func
109 ;; date-string: only used in diary-lib.el
110 ;; list-only: don't modify the diary-buffer, just return a list of entries
111 ;; file-glob-attrs: yuck
113 ;;; Code:
115 (load "cal-loaddefs" nil t)
117 ;; Avoid recursive load of calendar when loading cal-menu. Yuck.
118 (provide 'calendar)
119 (require 'cal-menu)
121 (defgroup calendar nil
122 "Calendar and time management support."
123 :prefix "calendar-"
124 :group 'applications)
126 (defgroup calendar-hooks nil
127 "Calendar hooks."
128 :prefix "calendar-"
129 :group 'calendar)
131 (defgroup calendar-faces nil
132 "Calendar faces."
133 :prefix "calendar-"
134 :group 'calendar)
136 (defcustom calendar-offset 0
137 "The offset of the principal month from the center of the calendar window.
138 0 means the principal month is in the center (default), -1 means on the left,
139 +1 means on the right. Larger (or smaller) values push the principal month off
140 the screen."
141 :type 'integer
142 :group 'calendar)
144 (defcustom calendar-setup nil
145 "The frame setup of the calendar.
146 The choices are: `one-frame' (calendar and diary together in one separate,
147 dedicated frame); `two-frames' (calendar and diary in separate, dedicated
148 frames); `calendar-only' (calendar in a separate, dedicated frame); with
149 any other value the current frame is used. Using any of the first
150 three options overrides the value of `calendar-view-diary-initially-flag'."
151 :type '(choice
152 (const :tag "calendar and diary in separate frame" one-frame)
153 (const :tag "calendar and diary each in own frame" two-frames)
154 (const :tag "calendar in separate frame" calendar-only)
155 (const :tag "use current frame" nil))
156 :group 'calendar)
158 (defcustom calendar-minimum-window-height 8
159 "Minimum height `calendar-generate-window' should use for calendar window."
160 :type 'integer
161 :version "22.1"
162 :group 'calendar)
164 ;; See discussion in bug#1806.
165 (defcustom calendar-split-width-threshold nil
166 "Value to use for `split-width-threshold' when creating a calendar.
167 This only affects frames wider than the default value of
168 `split-width-threshold'."
169 :type '(choice (const nil)
170 (integer))
171 :version "23.2"
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 `calendar-redraw' 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 (calendar-redraw))
190 :group 'calendar)
192 (define-obsolete-variable-alias 'view-diary-entries-initially
193 'calendar-view-diary-initially-flag "23.1")
195 (defcustom calendar-view-diary-initially-flag nil
196 "Non-nil means display current date's diary entries on entry to calendar.
197 The diary is displayed in another window when the calendar is first displayed,
198 if the current date is visible. The number of days of diary entries displayed
199 is governed by the variable `diary-number-of-entries'. This variable can
200 be overridden by the value of `calendar-setup'."
201 :type 'boolean
202 :group 'diary)
204 (define-obsolete-variable-alias 'mark-diary-entries-in-calendar
205 'calendar-mark-diary-entries-flag "23.1")
207 ;; FIXME :set
208 (defcustom calendar-mark-diary-entries-flag nil
209 "Non-nil means mark dates with diary entries, in the calendar window.
210 The marking symbol is specified by the variable `diary-entry-marker'."
211 :type 'boolean
212 :group 'diary)
214 (defcustom calendar-remove-frame-by-deleting t
215 "Determine how the calendar mode removes a frame no longer needed.
216 If nil, make an icon of the frame. If non-nil, delete the frame."
217 :type 'boolean
218 :version "23.1" ; changed from nil to t
219 :group 'view
220 :group 'calendar)
222 (defface calendar-today
223 '((t (:underline t)))
224 "Face for indicating today's date in the calendar.
225 See the variable `calendar-today-marker'."
226 :group 'calendar-faces)
228 (define-obsolete-face-alias 'calendar-today-face 'calendar-today "22.1")
230 (defface diary
231 '((((min-colors 88) (class color) (background light))
232 :foreground "red1")
233 (((class color) (background light))
234 :foreground "red")
235 (((min-colors 88) (class color) (background dark))
236 :foreground "yellow1")
237 (((class color) (background dark))
238 :foreground "yellow")
240 :weight bold))
241 "Face for highlighting diary entries.
242 Used to mark diary entries in the calendar (see `diary-entry-marker'),
243 and to highlight the date header in the fancy diary."
244 :group 'calendar-faces)
246 (define-obsolete-face-alias 'diary-face 'diary "22.1")
248 (defface holiday
249 '((((class color) (background light))
250 :background "pink")
251 (((class color) (background dark))
252 :background "chocolate4")
254 :inverse-video t))
255 "Face for indicating in the calendar dates that have holidays.
256 See `calendar-holiday-marker'."
257 :group 'calendar-faces)
259 (define-obsolete-face-alias 'holiday-face 'holiday "22.1")
261 (defface calendar-weekday-header '((t :inherit font-lock-constant-face))
262 "Face used for weekday column headers in the calendar.
263 See also the face `calendar-weekend-header'."
264 :version "24.4"
265 :group 'calendar-faces)
267 (defface calendar-weekend-header '((t :inherit font-lock-comment-face))
268 "Face used for weekend column headers in the calendar.
269 See also the face `calendar-weekday-header'."
270 :version "24.4"
271 :group 'calendar-faces)
273 (defface calendar-month-header '((t :inherit font-lock-function-name-face))
274 "Face used for month headers in the calendar."
275 :version "24.4"
276 :group 'calendar-faces)
278 ;; These briefly checked font-lock-mode, but that is broken, since it
279 ;; is a buffer-local variable, and which buffer happens to be current
280 ;; when this file is loaded shouldn't make a difference. One could
281 ;; perhaps check global-font-lock-mode, or font-lock-global-modes; but
282 ;; this feature doesn't use font-lock, so there's no real reason it
283 ;; should respect those either. See bug#2199.
284 ;; They also used to check display-color-p, but that is a problem if
285 ;; loaded from --daemon. Since BW displays are rare now, this was
286 ;; also taken out. The way to keep it would be to have nil mean do a
287 ;; runtime check whenever this variable is used.
288 (defcustom diary-entry-marker 'diary
289 "How to mark dates that have diary entries.
290 The value can be either a single-character string (e.g. \"+\") or a face."
291 :type '(choice (string :tag "Single character string") face)
292 :group 'diary
293 :version "23.1")
295 (defcustom calendar-today-marker 'calendar-today
296 "How to mark today's date in the calendar.
297 The value can be either a single-character string (e.g. \"=\") or a face.
298 Used by `calendar-mark-today'."
299 :type '(choice (string :tag "Single character string") face)
300 :group 'calendar
301 :version "23.1")
303 (defcustom calendar-holiday-marker 'holiday
304 "How to mark notable dates in the calendar.
305 The value can be either a single-character string (e.g. \"*\") or a face."
306 :type '(choice (string :tag "Single character string") face)
307 :group 'holidays
308 :version "23.1")
310 (define-obsolete-variable-alias 'view-calendar-holidays-initially
311 'calendar-view-holidays-initially-flag "23.1")
313 (defcustom calendar-view-holidays-initially-flag nil
314 "Non-nil means display holidays for current three month period on entry.
315 The holidays are displayed in another window when the calendar is first
316 displayed."
317 :type 'boolean
318 :group 'holidays)
320 (define-obsolete-variable-alias 'mark-holidays-in-calendar
321 'calendar-mark-holidays-flag "23.1")
323 ;; FIXME :set
324 (defcustom calendar-mark-holidays-flag nil
325 "Non-nil means mark dates of holidays in the calendar window.
326 The marking symbol is specified by the variable `calendar-holiday-marker'."
327 :type 'boolean
328 :group 'holidays)
330 (defcustom calendar-mode-hook nil
331 "Hook run when entering `calendar-mode'."
332 :type 'hook
333 :group 'calendar-hooks)
335 (defcustom calendar-load-hook nil
336 "List of functions to be called after the calendar is first loaded.
337 This is the place to add key bindings to `calendar-mode-map'."
338 :type 'hook
339 :group 'calendar-hooks)
341 (define-obsolete-variable-alias 'initial-calendar-window-hook
342 'calendar-initial-window-hook "23.1")
344 (defcustom calendar-initial-window-hook nil
345 "List of functions to be called when the calendar window is created.
346 Quitting the calendar and re-entering it will cause these functions
347 to be called again."
348 :type 'hook
349 :group 'calendar-hooks)
351 (define-obsolete-variable-alias 'today-visible-calendar-hook
352 'calendar-today-visible-hook "23.1")
354 (defcustom calendar-today-visible-hook nil
355 "List of functions called whenever the current date is visible.
356 To mark today's date, add the function `calendar-mark-today'.
357 To replace the date with asterisks, add the function `calendar-star-date'.
358 See also `calendar-today-invisible-hook'.
360 In general, be careful about changing characters in the calendar buffer,
361 since it may cause the movement commands to fail."
362 :type 'hook
363 :options '(calendar-mark-today calendar-star-date)
364 :group 'calendar-hooks)
366 (define-obsolete-variable-alias 'today-invisible-calendar-hook
367 'calendar-today-invisible-hook "23.1")
369 (defcustom calendar-today-invisible-hook nil
370 "List of functions called whenever the current date is not visible.
371 See also `calendar-today-visible-hook'."
372 :type 'hook
373 :group 'calendar-hooks)
375 (defcustom calendar-move-hook nil
376 "List of functions called whenever the cursor moves in the calendar.
377 For example,
379 (add-hook 'calendar-move-hook (lambda () (diary-view-entries 1)))
381 redisplays the diary for whatever date the cursor is moved to."
382 :type 'hook
383 :options '(calendar-update-mode-line)
384 :group 'calendar-hooks)
386 (defcustom calendar-date-echo-text
387 "mouse-2: general menu\nmouse-3: menu for this date"
388 "String displayed when the cursor is over a date in the calendar.
389 Can be either a fixed string, or a lisp expression that returns one.
390 When this expression is evaluated, DAY, MONTH, and YEAR are
391 integers appropriate to the relevant date. For example, to
392 display the ISO date:
394 (setq calendar-date-echo-text '(format \"ISO date: %s\"
395 (calendar-iso-date-string
396 (list month day year))))
397 Changing this variable without using customize has no effect on
398 pre-existing calendar windows."
399 :group 'calendar
400 :initialize 'custom-initialize-default
401 :risky t
402 :set (lambda (sym val)
403 (set sym val)
404 (calendar-redraw))
405 :type '(choice (string :tag "Fixed string")
406 (sexp :value
407 (format "ISO date: %s"
408 (calendar-iso-date-string
409 (list month day year)))))
410 :version "23.1")
413 (defvar calendar-month-digit-width nil
414 "Width of the region with numbers in each month in the calendar.")
416 (defvar calendar-month-width nil
417 "Full width of each month in the calendar.")
419 (defvar calendar-right-margin nil
420 "Right margin of the calendar.")
422 (defvar calendar-month-edges nil
423 "Alist of month edge columns.
424 Each element has the form (N LEFT FIRST LAST RIGHT), where
425 LEFT is the leftmost column associated with month segment N,
426 FIRST and LAST are the first and last columns with day digits in,
427 and LAST is the rightmost column.")
429 (defun calendar-month-edges (segment)
430 "Compute the month edge columns for month SEGMENT.
431 Returns a list (LEFT FIRST LAST RIGHT), where LEFT is the
432 leftmost column associated with a month, FIRST and LAST are the
433 first and last columns with day digits in, and LAST is the
434 rightmost column."
435 ;; The leftmost column with a digit in it in this month segment.
436 (let* ((first (+ calendar-left-margin
437 (* segment calendar-month-width)))
438 ;; The rightmost column with a digit in it in this month segment.
439 (last (+ first (1- calendar-month-digit-width)))
440 (left (if (eq segment 0)
442 (+ calendar-left-margin
443 (* segment calendar-month-width)
444 (- (/ calendar-intermonth-spacing 2)))))
445 ;; The rightmost edge of this month segment, dividing the
446 ;; space between months in two.
447 (right (+ calendar-left-margin
448 (* (1+ segment) calendar-month-width)
449 (- (/ calendar-intermonth-spacing 2)))))
450 (list left first last right)))
452 (defun calendar-recompute-layout-variables ()
453 "Recompute some layout-related calendar \"constants\"."
454 (setq calendar-month-digit-width (+ (* 6 calendar-column-width)
455 calendar-day-digit-width)
456 calendar-month-width (+ (* 7 calendar-column-width)
457 calendar-intermonth-spacing)
458 calendar-right-margin (+ calendar-left-margin
459 (* 3 (* 7 calendar-column-width))
460 (* 2 calendar-intermonth-spacing))
461 calendar-month-edges nil)
462 (dotimes (i 3)
463 (push (cons i (calendar-month-edges i)) calendar-month-edges))
464 (setq calendar-month-edges (reverse calendar-month-edges)))
466 (defun calendar-set-layout-variable (symbol value &optional minmax)
467 "Set SYMBOL's value to VALUE, an integer.
468 A positive/negative MINMAX enforces a minimum/maximum value.
469 Then redraw the calendar, if necessary."
470 (let ((oldvalue (symbol-value symbol)))
471 (custom-set-default symbol (if minmax
472 (if (< minmax 0)
473 (min value (- minmax))
474 (max value minmax))
475 value))
476 (unless (equal value oldvalue)
477 (calendar-recompute-layout-variables)
478 (calendar-redraw))))
480 (defcustom calendar-left-margin 5
481 "Empty space to the left of the first month in the calendar."
482 :group 'calendar
483 :initialize 'custom-initialize-default
484 :set 'calendar-set-layout-variable
485 :type 'integer
486 :version "23.1")
488 ;; Or you can view it as columns of width 2, with 1 space, no space
489 ;; after the last column, and a 5 space gap between month.
490 ;; FIXME check things work if this is odd.
491 (defcustom calendar-intermonth-spacing 4
492 "Space between months in the calendar. Minimum value is 1."
493 :group 'calendar
494 :initialize 'custom-initialize-default
495 :set (lambda (sym val)
496 (calendar-set-layout-variable sym val 1))
497 :type 'integer
498 :version "23.1")
500 ;; FIXME calendar-month-column-width?
501 (defcustom calendar-column-width 3
502 "Width of each day column in the calendar. Minimum value is 3."
503 :initialize 'custom-initialize-default
504 :set (lambda (sym val)
505 (calendar-set-layout-variable sym val 3))
506 :type 'integer
507 :version "23.1")
509 (defun calendar-day-header-construct (&optional width)
510 "Return the default value for `calendar-day-header-array'.
511 WIDTH defaults to `calendar-day-header-width'."
512 (or width (setq width calendar-day-header-width))
513 (calendar-abbrev-construct (if (<= width calendar-abbrev-length)
514 calendar-day-abbrev-array
515 calendar-day-name-array)
516 width))
518 ;; FIXME better to use a format spec?
519 (defcustom calendar-day-header-width 2
520 "Width of the day column headers in the calendar.
521 Must be at least one less than `calendar-column-width'."
522 :group 'calendar
523 :initialize 'custom-initialize-default
524 :set (lambda (sym val)
525 (or (calendar-customized-p 'calendar-day-header-array)
526 (setq calendar-day-header-array
527 (calendar-day-header-construct val)))
528 (calendar-set-layout-variable sym val (- 1 calendar-column-width)))
529 :type 'integer
530 :version "23.1")
532 ;; FIXME a format specifier instead?
533 (defcustom calendar-day-digit-width 2
534 "Width of the day digits in the calendar. Minimum value is 2."
535 :group 'calendar
536 :initialize 'custom-initialize-default
537 :set (lambda (sym val)
538 (calendar-set-layout-variable sym val 2))
539 :type 'integer
540 :version "23.1")
542 (defcustom calendar-intermonth-header nil
543 "Header text to display in the space to the left of each calendar month.
544 See `calendar-intermonth-text'."
545 :group 'calendar
546 :initialize 'custom-initialize-default
547 :risky t
548 :set (lambda (sym val)
549 (set sym val)
550 (calendar-redraw))
551 :type '(choice (const nil :tag "Nothing")
552 (string :tag "Fixed string")
553 (sexp :value
554 (propertize "WK" 'font-lock-face
555 'font-lock-function-name-face)))
556 :version "23.1")
558 (defcustom calendar-intermonth-text nil
559 "Text to display in the space to the left of each calendar month.
560 Can be nil, a fixed string, or a lisp expression that returns a string.
561 When the expression is evaluated, the variables DAY, MONTH and YEAR
562 are integers appropriate for the first day in each week.
563 Will be truncated to the smaller of `calendar-left-margin' and
564 `calendar-intermonth-spacing'. The last character is forced to be a space.
565 For example, to display the ISO week numbers:
567 (setq calendar-week-start-day 1
568 calendar-intermonth-text
569 '(propertize
570 (format \"%2d\"
571 (car
572 (calendar-iso-from-absolute
573 (calendar-absolute-from-gregorian (list month day year)))))
574 'font-lock-face 'font-lock-function-name-face))
576 See also `calendar-intermonth-header'."
577 :group 'calendar
578 :initialize 'custom-initialize-default
579 :risky t
580 :set (lambda (sym val)
581 (set sym val)
582 (calendar-redraw))
583 :type '(choice (const nil :tag "Nothing")
584 (string :tag "Fixed string")
585 (sexp :value
586 (propertize
587 (format "%2d"
588 (car
589 (calendar-iso-from-absolute
590 (calendar-absolute-from-gregorian
591 (list month day year)))))
592 'font-lock-face 'font-lock-function-name-face)))
593 :version "23.1")
595 (defcustom diary-file "~/diary"
596 "Name of the file in which one's personal diary of dates is kept.
598 The file's entries are lines beginning with any of the forms
599 specified by the variable `diary-date-forms', which by default
600 uses the forms of `diary-american-date-forms':
602 MONTH/DAY
603 MONTH/DAY/YEAR
604 MONTHNAME DAY
605 MONTHNAME DAY, YEAR
606 DAYNAME
608 with the remainder of the line being the diary entry string for
609 that date. MONTH and DAY are one or two digit numbers, YEAR is a
610 number and may be written in full or abbreviated to the final two
611 digits (if `diary-abbreviated-year-flag' is non-nil). MONTHNAME
612 and DAYNAME can be spelled in full (as specified by the variables
613 `calendar-month-name-array' and `calendar-day-name-array'), or
614 abbreviated (as specified by `calendar-month-abbrev-array' and
615 `calendar-day-abbrev-array') with or without a period. Case is
616 ignored. Any of DAY, MONTH, or MONTHNAME, YEAR can be `*' which
617 matches any day, month, or year, respectively. If the date does
618 not contain a year, it is generic and applies to any year. A
619 DAYNAME entry applies to the appropriate day of the week in every week.
621 You can customize `diary-date-forms' to your preferred format.
622 Three default styles are provided: `diary-american-date-forms',
623 `diary-european-date-forms', and `diary-iso-date-forms'.
624 You can choose between these by setting `calendar-date-style' in your
625 init file, or by using `calendar-set-date-style' when in the calendar.
627 A diary entry can be preceded by the character `diary-nonmarking-symbol'
628 \(ordinarily `&') to make that entry nonmarking--that is, it will not be
629 marked on dates in the calendar window but will appear in a diary window.
631 Multiline diary entries are made by indenting lines after the first with
632 either a TAB or one or more spaces.
634 Lines not in one the above formats are ignored. Here are some sample diary
635 entries (in the default American style):
637 12/22/1988 Twentieth wedding anniversary!!
638 &1/1. Happy New Year!
639 10/22 Ruth's birthday.
640 21: Payday
641 Tuesday--weekly meeting with grad students at 10am
642 Supowit, Shen, Bitner, and Kapoor to attend.
643 1/13/89 Friday the thirteenth!!
644 &thu 4pm squash game with Lloyd.
645 mar 16 Dad's birthday
646 April 15, 1989 Income tax due.
647 &* 15 time cards due.
649 If the first line of a diary entry consists only of the date or day name with
650 no trailing blanks or punctuation, then that line is not displayed in the
651 diary window; only the continuation lines is shown. For example, the
652 single diary entry
654 02/11/1989
655 Bill Blattner visits Princeton today
656 2pm Cognitive Studies Committee meeting
657 2:30-5:30 Lizzie at Lawrenceville for `Group Initiative'
658 4:00pm Jamie Tappenden
659 7:30pm Dinner at George and Ed's for Alan Ryan
660 7:30-10:00pm dance at Stewart Country Day School
662 will appear in the diary window without the date line at the beginning. This
663 facility allows the diary window to look neater, but can cause confusion if
664 used with more than one day's entries displayed.
666 Diary entries can be based on Lisp sexps. For example, the diary entry
668 %%(diary-block 11 1 1990 11 10 1990) Vacation
670 causes the diary entry \"Vacation\" to appear from November 1 through
671 November 10, 1990. See the documentation for the function
672 `diary-list-sexp-entries' for more details.
674 Diary entries based on the Hebrew, the Islamic and/or the Bahá'í
675 calendar are also possible, but because these are somewhat slow, they
676 are ignored unless you set the `diary-nongregorian-listing-hook' and
677 the `diary-nongregorian-marking-hook' appropriately. See the
678 documentation of these hooks for details.
680 Diary files can contain directives to include the contents of other files; for
681 details, see the documentation for the variable `diary-list-entries-hook'."
682 :type 'file
683 :group 'diary)
685 ;; FIXME do these have to be single characters?
686 (defcustom diary-nonmarking-symbol "&"
687 "Symbol indicating that a diary entry is not to be marked in the calendar."
688 :type 'string
689 :group 'diary)
691 (define-obsolete-variable-alias 'hebrew-diary-entry-symbol
692 'diary-hebrew-entry-symbol "23.1")
694 (defcustom diary-hebrew-entry-symbol "H"
695 "Symbol indicating a diary entry according to the Hebrew calendar."
696 :type 'string
697 :group 'diary)
699 (define-obsolete-variable-alias 'islamic-diary-entry-symbol
700 'diary-islamic-entry-symbol "23.1")
702 (defcustom diary-islamic-entry-symbol "I"
703 "Symbol indicating a diary entry according to the Islamic calendar."
704 :type 'string
705 :group 'diary)
707 (define-obsolete-variable-alias 'bahai-diary-entry-symbol
708 'diary-bahai-entry-symbol "23.1")
710 (defcustom diary-bahai-entry-symbol "B"
711 "Symbol indicating a diary entry according to the Bahá'í calendar."
712 :type 'string
713 :group 'diary)
715 (defcustom european-calendar-style nil
716 "Non-nil means use the European style of dates in the diary and display.
717 In this case, a date like 1/2/1990 would be interpreted as
718 February 1, 1990. See `diary-european-date-forms' for the
719 default European diary date styles.
721 Setting this variable directly does not take effect (if the
722 calendar package is already loaded). Rather, use either
723 \\[customize] or the function `calendar-set-date-style'."
724 :type 'boolean
725 ;; Without :initialize (require 'calendar) throws an error because
726 ;; calendar-set-date-style is undefined at this point.
727 :initialize 'custom-initialize-default
728 :set (lambda (symbol value)
729 (if value
730 (calendar-set-date-style 'european)
731 (calendar-set-date-style 'american)))
732 :group 'calendar)
734 (make-obsolete-variable 'european-calendar-style 'calendar-date-style "23.1")
736 ;; If this is autoloaded, c-d-s gets set before any customization of e-c-s.
737 (defcustom calendar-date-style (if european-calendar-style 'european
738 'american)
739 "Your preferred style for writing dates.
740 The options are:
741 `american' - month/day/year
742 `european' - day/month/year
743 `iso' - year/month/day
744 This affects how dates written in your diary are interpreted.
745 It also affects date display, as well as those calendar and diary
746 functions that take a date as an argument, e.g. `diary-date', by
747 changing the order in which the arguments are interpreted.
749 Setting this variable directly does not take effect (if the
750 calendar package is already loaded). Rather, use either
751 \\[customize] or the function `calendar-set-date-style'."
752 :version "23.1"
753 :type '(choice (const american :tag "Month/Day/Year")
754 (const european :tag "Day/Month/Year")
755 (const iso :tag "Year/Month/Day"))
756 :initialize 'custom-initialize-default
757 :set (lambda (symbol value)
758 (calendar-set-date-style value))
759 :group 'calendar)
761 ;; Next three are provided to aid in setting diary-date-forms.
762 ;; FIXME move to diary-lib?
763 (defcustom diary-iso-date-forms
764 '((month "[-/]" day "[^-/0-9]")
765 (year "[-/]" month "[-/]" day "[^0-9]")
766 ;; Cannot allow [-/] as separators here, since it would also match
767 ;; the first element (bug#7377).
768 (monthname " *" day "[^-0-9]")
769 (year " *" monthname " *" day "[^0-9]")
770 (dayname "\\W"))
771 "List of pseudo-patterns describing the ISO style of dates.
772 The defaults are: MONTH[-/]DAY; YEAR[-/]MONTH[-/]DAY; MONTHNAME DAY;
773 YEAR MONTHNAME DAY; DAYNAME. Normally you should not customize this,
774 but `diary-date-forms' (which see)."
775 :version "23.3" ; bug#7377
776 :type '(repeat (choice (cons :tag "Backup"
777 :value (backup . nil)
778 (const backup)
779 (repeat (list :inline t :format "%v"
780 (symbol :tag "Keyword")
781 (choice symbol regexp))))
782 (repeat (list :inline t :format "%v"
783 (symbol :tag "Keyword")
784 (choice symbol regexp)))))
785 :group 'diary)
787 (define-obsolete-variable-alias 'american-date-diary-pattern
788 'diary-american-date-forms "23.1")
790 (defcustom diary-american-date-forms
791 '((month "/" day "[^/0-9]")
792 (month "/" day "/" year "[^0-9]")
793 (monthname " *" day "[^,0-9]")
794 (monthname " *" day ", *" year "[^0-9]")
795 (dayname "\\W"))
796 "List of pseudo-patterns describing the American style of dates.
797 The defaults are: MONTH/DAY; MONTH/DAY/YEAR; MONTHNAME DAY;
798 MONTHNAME DAY, YEAR; DAYNAME. Normally you should not customize this,
799 but `diary-date-forms' (which see)."
800 :type '(repeat (choice (cons :tag "Backup"
801 :value (backup . nil)
802 (const backup)
803 (repeat (list :inline t :format "%v"
804 (symbol :tag "Keyword")
805 (choice symbol regexp))))
806 (repeat (list :inline t :format "%v"
807 (symbol :tag "Keyword")
808 (choice symbol regexp)))))
809 :group 'diary)
811 (define-obsolete-variable-alias 'european-date-diary-pattern
812 'diary-european-date-forms "23.1")
814 (defcustom diary-european-date-forms
815 '((day "/" month "[^/0-9]")
816 (day "/" month "/" year "[^0-9]")
817 (backup day " *" monthname "\\W+\\<\\([^*0-9]\\|\\([0-9]+[:aApP]\\)\\)")
818 (day " *" monthname " *" year "[^0-9]")
819 (dayname "\\W"))
820 "List of pseudo-patterns describing the European style of dates.
821 The defaults are: DAY/MONTH; DAY/MONTH/YEAR; DAY MONTHNAME;
822 DAY MONTHNAME YEAR; DAYNAME. Normally you should not customize this, but
823 `diary-date-forms' (which see)."
824 :type '(repeat (choice (cons :tag "Backup"
825 :value (backup . nil)
826 (const backup)
827 (repeat (list :inline t :format "%v"
828 (symbol :tag "Keyword")
829 (choice symbol regexp))))
830 (repeat (list :inline t :format "%v"
831 (symbol :tag "Keyword")
832 (choice symbol regexp)))))
833 :group 'diary)
835 (defvar diary-font-lock-keywords)
837 (defcustom diary-date-forms (cond ((eq calendar-date-style 'iso)
838 diary-iso-date-forms)
839 ((eq calendar-date-style 'european)
840 diary-european-date-forms)
841 (t diary-american-date-forms))
842 "List of pseudo-patterns describing the forms of date used in the diary.
843 The patterns on the list must be MUTUALLY EXCLUSIVE and should not match
844 any portion of the diary entry itself, just the date component.
846 A pseudo-pattern is a list of regular expressions and the keywords `month',
847 `day', `year', `monthname', and `dayname'. The keyword `monthname' will
848 match the name of the month (see `calendar-month-name-array'), capitalized
849 or not, or its user-specified abbreviation (see `calendar-month-abbrev-array'),
850 followed by a period or not; it will also match `*'. Similarly, `dayname'
851 will match the name of the day (see `calendar-day-name-array'), capitalized or
852 not, or its user-specified abbreviation (see `calendar-day-abbrev-array'),
853 followed by a period or not. The keywords `month', `day', and `year' will
854 match those numerical values, preceded by arbitrarily many zeros; they will
855 also match `*'.
857 The matching of the diary entries with the date forms is done with the
858 standard syntax table from Fundamental mode, but with the `*' changed so
859 that it is a word constituent.
861 If, to be mutually exclusive, a pseudo-pattern must match a portion of the
862 diary entry itself, the first element of the pattern MUST be `backup'. This
863 directive causes the date recognizer to back up to the beginning of the
864 current word of the diary entry, so in no case can the pattern match more than
865 a portion of the first word of the diary entry.
867 For examples of three common styles, see `diary-american-date-forms',
868 `diary-european-date-forms', and `diary-iso-date-forms'."
869 :type '(repeat (choice (cons :tag "Backup"
870 :value (backup . nil)
871 (const backup)
872 (repeat (list :inline t :format "%v"
873 (symbol :tag "Keyword")
874 (choice symbol regexp))))
875 (repeat (list :inline t :format "%v"
876 (symbol :tag "Keyword")
877 (choice symbol regexp)))))
878 :set-after '(calendar-date-style diary-iso-date-forms
879 diary-european-date-forms
880 diary-american-date-forms)
881 :initialize 'custom-initialize-default
882 :set (lambda (symbol value)
883 (unless (equal value (eval symbol))
884 (custom-set-default symbol value)
885 (setq diary-font-lock-keywords (diary-font-lock-keywords))
886 ;; Need to redraw not just to get new font-locking, but also
887 ;; to pick up any newly recognized entries.
888 (and (diary-live-p)
889 (diary))))
890 :group 'diary)
892 ;; Next three are provided to aid in setting calendar-date-display-form.
893 (defcustom calendar-iso-date-display-form '((format "%s-%.2d-%.2d" year
894 (string-to-number month)
895 (string-to-number day)))
896 "Pseudo-pattern governing the way a date appears in the ISO style.
897 Normally you should not customize this, but `calendar-date-display-form'
898 \(which see)."
899 :type 'sexp
900 :version "23.1"
901 :group 'calendar)
903 (define-obsolete-variable-alias 'european-calendar-display-form
904 'calendar-european-date-display-form "23.1")
906 (defcustom calendar-european-date-display-form
907 '((if dayname (concat dayname ", ")) day " " monthname " " year)
908 "Pseudo-pattern governing the way a date appears in the European style.
909 Normally you should not customize this, but `calendar-date-display-form'
910 \(which see)."
911 :type 'sexp
912 :group 'calendar)
914 (define-obsolete-variable-alias 'american-calendar-display-form
915 'calendar-american-date-display-form "23.1")
917 (defcustom calendar-american-date-display-form
918 '((if dayname (concat dayname ", ")) monthname " " day ", " year)
919 "Pseudo-pattern governing the way a date appears in the American style.
920 Normally you should not customize this, but `calendar-date-display-form'
921 \(which see)."
922 :type 'sexp
923 :group 'calendar)
925 (defcustom calendar-date-display-form
926 (cond ((eq calendar-date-style 'iso)
927 calendar-iso-date-display-form)
928 ((eq calendar-date-style 'european)
929 calendar-european-date-display-form)
930 (t calendar-american-date-display-form))
931 "Pseudo-pattern governing the way a calendar date appears.
932 Used by the function `calendar-date-string' (which see), a pseudo-pattern
933 is a list of expressions that can involve the keywords `month', `day',
934 and `year' (all numbers in string form), and `monthname' and `dayname'
935 \(both alphabetic strings). For example, a typical American form would be
937 '(month \"/\" day \"/\" (substring year -2))
939 whereas
941 '((format \"%9s, %9s %2s, %4s\" dayname monthname day year))
943 would give the usual American style in fixed-length fields. The variables
944 `calendar-iso-date-display-form', `calendar-european-date-display-form', and
945 `calendar-american-date-display-form' provide some defaults for three common
946 styles."
947 :type 'sexp
948 :set-after '(calendar-date-style calendar-iso-date-display-form
949 calendar-european-date-display-form
950 calendar-american-date-display-form)
951 :group 'calendar)
953 (defcustom calendar-american-month-header
954 '(propertize (format "%s %d" (calendar-month-name month) year)
955 'font-lock-face 'calendar-month-header)
956 "Default format for calendar month headings with the American date style.
957 Normally you should not customize this, but `calender-month-header'."
958 :group 'calendar
959 :risky t
960 :type 'sexp
961 :version "24.4") ; font-lock-function-name-face -> calendar-month-header
963 (defcustom calendar-european-month-header
964 '(propertize (format "%s %d" (calendar-month-name month) year)
965 'font-lock-face 'calendar-month-header)
966 "Default format for calendar month headings with the European date style.
967 Normally you should not customize this, but `calender-month-header'."
968 :group 'calendar
969 :risky t
970 :type 'sexp
971 :version "24.4") ; font-lock-function-name-face -> calendar-month-header
973 (defcustom calendar-iso-month-header
974 '(propertize (format "%d %s" year (calendar-month-name month))
975 'font-lock-face 'calendar-month-header)
976 "Default format for calendar month headings with the ISO date style.
977 Normally you should not customize this, but `calender-month-header'."
978 :group 'calendar
979 :risky t
980 :type 'sexp
981 :version "24.4") ; font-lock-function-name-face -> calendar-month-header
983 (defcustom calendar-month-header
984 (cond ((eq calendar-date-style 'iso)
985 calendar-iso-month-header)
986 ((eq calendar-date-style 'european)
987 calendar-european-month-header)
988 (t calendar-american-month-header))
989 "Expression to evaluate to return the calendar month headings.
990 When this expression is evaluated, the variables MONTH and YEAR are
991 integers appropriate to the relevant month. The result is padded
992 to the width of `calendar-month-digit-width'.
994 For examples of three common styles, see `calendar-american-month-header',
995 `calendar-european-month-header', and `calendar-iso-month-header'.
997 Changing this variable without using customize has no effect on
998 pre-existing calendar windows."
999 :group 'calendar
1000 :initialize 'custom-initialize-default
1001 :risky t
1002 :set (lambda (sym val)
1003 (set sym val)
1004 (calendar-redraw))
1005 :set-after '(calendar-date-style calendar-american-month-header
1006 calendar-european-month-header
1007 calendar-iso-month-header)
1008 :type 'sexp
1009 :version "24.3")
1011 (defun calendar-set-date-style (style)
1012 "Set the style of calendar and diary dates to STYLE (a symbol).
1013 The valid styles are described in the documentation of `calendar-date-style'."
1014 (interactive (list (intern
1015 (completing-read "Date style: "
1016 '("american" "european" "iso") nil t
1017 nil nil "american"))))
1018 (or (memq style '(american european iso))
1019 (setq style 'american))
1020 (setq calendar-date-style style
1021 calendar-date-display-form
1022 (symbol-value (intern-soft
1023 (format "calendar-%s-date-display-form" style)))
1024 calendar-month-header
1025 (symbol-value (intern-soft (format "calendar-%s-month-header" style)))
1026 diary-date-forms
1027 (symbol-value (intern-soft (format "diary-%s-date-forms" style))))
1028 (calendar-redraw)
1029 (calendar-update-mode-line))
1031 (defun european-calendar ()
1032 "Set the interpretation and display of dates to the European style."
1033 (declare (obsolete calendar-set-date-style "23.1"))
1034 (interactive)
1035 (calendar-set-date-style 'european))
1037 (defun american-calendar ()
1038 "Set the interpretation and display of dates to the American style."
1039 (declare (obsolete calendar-set-date-style "23.1"))
1040 (interactive)
1041 (calendar-set-date-style 'american))
1043 (define-obsolete-variable-alias 'holidays-in-diary-buffer
1044 'diary-show-holidays-flag "23.1")
1046 (defcustom diary-show-holidays-flag t
1047 "Non-nil means include holidays in the diary display.
1048 The holidays appear in the mode line of the diary buffer, or in the
1049 fancy diary buffer next to the date. This slows down the diary functions
1050 somewhat; setting it to nil makes the diary display faster."
1051 :type 'boolean
1052 :group 'holidays)
1054 (defcustom calendar-debug-sexp nil
1055 "Turn debugging on when evaluating a sexp in the diary or holiday list."
1056 :type 'boolean
1057 :group 'calendar)
1059 (define-obsolete-variable-alias 'all-hebrew-calendar-holidays
1060 'calendar-hebrew-all-holidays-flag "23.1")
1062 (defcustom calendar-hebrew-all-holidays-flag nil
1063 "If nil, show only major holidays from the Hebrew calendar.
1064 This means only those Jewish holidays that appear on secular calendars.
1065 Otherwise, show all the holidays that would appear in a complete Hebrew
1066 calendar."
1067 :type 'boolean
1068 :group 'holidays)
1070 (define-obsolete-variable-alias 'all-christian-calendar-holidays
1071 'calendar-christian-all-holidays-flag "23.1")
1073 (defcustom calendar-christian-all-holidays-flag nil
1074 "If nil, show only major holidays from the Christian calendar.
1075 This means only those Christian holidays that appear on secular calendars.
1076 Otherwise, show all the holidays that would appear in a complete Christian
1077 calendar."
1078 :type 'boolean
1079 :group 'holidays)
1081 (define-obsolete-variable-alias 'all-islamic-calendar-holidays
1082 'calendar-islamic-all-holidays-flag "23.1")
1084 (defcustom calendar-islamic-all-holidays-flag nil
1085 "If nil, show only major holidays from the Islamic calendar.
1086 This means only those Islamic holidays that appear on secular calendars.
1087 Otherwise, show all the holidays that would appear in a complete Islamic
1088 calendar."
1089 :type 'boolean
1090 :group 'holidays)
1092 (define-obsolete-variable-alias 'all-bahai-calendar-holidays
1093 'calendar-bahai-all-holidays-flag "23.1")
1095 (defcustom calendar-bahai-all-holidays-flag nil
1096 "If nil, show only major holidays from the Bahá'í calendar.
1097 These are the days on which work and school must be suspended.
1098 Otherwise, show all the holidays that would appear in a complete Bahá'í
1099 calendar."
1100 :type 'boolean
1101 :group 'holidays)
1103 (defcustom calendar-chinese-all-holidays-flag nil
1104 "If nil, show only the major holidays from the Chinese calendar."
1105 :version "23.1"
1106 :type 'boolean
1107 :group 'holidays)
1109 ;;; End of user options.
1111 (calendar-recompute-layout-variables)
1113 (defconst calendar-first-date-row 3
1114 "First row in the calendar with actual dates.")
1116 (defconst calendar-buffer "*Calendar*"
1117 "Name of the buffer used for the calendar.")
1119 (defconst holiday-buffer "*Holidays*"
1120 "Name of the buffer used for the displaying the holidays.")
1122 (defconst diary-fancy-buffer "*Fancy Diary Entries*"
1123 "Name of the buffer used for the optional fancy display of the diary.")
1125 (define-obsolete-variable-alias 'fancy-diary-buffer 'diary-fancy-buffer "23.1")
1127 (defconst calendar-other-calendars-buffer "*Other Calendars*"
1128 "Name of the buffer used for the display of date on other calendars.")
1130 (defconst lunar-phases-buffer "*Phases of Moon*"
1131 "Name of the buffer used for the lunar phases.")
1133 (defconst solar-sunrises-buffer "*Sunrise/Sunset Times*"
1134 "Name of buffer used for sunrise/sunset times.")
1136 (defconst calendar-hebrew-yahrzeit-buffer "*Yahrzeits*"
1137 "Name of the buffer used by `list-yahrzeit-dates'.")
1139 (defmacro calendar-increment-month (mon yr n &optional nmonths)
1140 "Increment the variables MON and YR by N months.
1141 Forward if N is positive or backward if N is negative.
1142 A negative YR is interpreted as BC; -1 being 1 BC, and so on.
1143 Optional NMONTHS is the number of months per year (default 12)."
1144 ;; Can view this as a form of base-nmonths arithmetic, in which "a
1145 ;; year" = "ten", and we never bother to use hundreds.
1146 `(let ((nmonths (or ,nmonths 12))
1147 macro-y)
1148 (if (< ,yr 0) (setq ,yr (1+ ,yr))) ; -1 BC -> 0 AD, etc
1149 (setq macro-y (+ (* ,yr nmonths) ,mon -1 ,n)
1150 ,mon (1+ (mod macro-y nmonths))
1151 ,yr (/ macro-y nmonths))
1152 ;; Alternative:
1153 ;;; (setq macro-y (+ (* ,yr nmonths) ,mon -1 ,n)
1154 ;;; ,yr (/ macro-y nmonths)
1155 ;;; ,mon (- macro-y (* ,yr nmonths)))
1156 (and (< macro-y 0) (> ,mon 1) (setq ,yr (1- ,yr)))
1157 (if (< ,yr 1) (setq ,yr (1- ,yr))))) ; 0 AD -> -1 BC, etc
1159 (define-obsolete-function-alias 'increment-calendar-month
1160 'calendar-increment-month "23.1")
1162 (defvar displayed-month)
1163 (defvar displayed-year)
1165 (defun calendar-increment-month-cons (n &optional mon yr)
1166 "Return the Nth month after MON/YR.
1167 The return value is a pair (MONTH . YEAR).
1168 MON defaults to `displayed-month'. YR defaults to `displayed-year'."
1169 (unless mon (setq mon displayed-month))
1170 (unless yr (setq yr displayed-year))
1171 (calendar-increment-month mon yr n)
1172 (cons mon yr))
1174 (defmacro calendar-for-loop (var from init to final do &rest body)
1175 "Execute a for loop.
1176 Evaluate BODY with VAR bound to successive integers from INIT to FINAL,
1177 inclusive. The standard macro `dotimes' is preferable in most cases."
1178 (declare (obsolete "use `dotimes' or `while' instead." "23.1")
1179 (debug (symbolp "from" form "to" form "do" body))
1180 (indent defun))
1181 `(let ((,var (1- ,init)))
1182 (while (>= ,final (setq ,var (1+ ,var)))
1183 ,@body)))
1185 (defmacro calendar-sum (index initial condition expression)
1186 "For INDEX = INITIAL, +1, ... (as long as CONDITION holds), sum EXPRESSION."
1187 (declare (debug (symbolp form form form)))
1188 `(let ((,index ,initial)
1189 (sum 0))
1190 (while ,condition
1191 (setq sum (+ sum ,expression)
1192 ,index (1+ ,index)))
1193 sum))
1195 (defmacro calendar-in-read-only-buffer (buffer &rest body)
1196 "Switch to BUFFER and execute the forms in BODY.
1197 First creates or erases BUFFER as needed. Leaves BUFFER read-only,
1198 with disabled undo. Leaves point at point-min, displays BUFFER."
1199 (declare (indent 1) (debug t))
1200 `(progn
1201 (set-buffer (get-buffer-create ,buffer))
1202 (or (derived-mode-p 'special-mode) (special-mode))
1203 (setq buffer-read-only nil
1204 buffer-undo-list t)
1205 (erase-buffer)
1206 ,@body
1207 (goto-char (point-min))
1208 (set-buffer-modified-p nil)
1209 (setq buffer-read-only t)
1210 (display-buffer ,buffer)))
1212 ;; The following are in-line for speed; they can be called thousands of times
1213 ;; when looking up holidays or processing the diary. Here, for example, are
1214 ;; the numbers of calls to calendar/diary/holiday functions in preparing the
1215 ;; fancy diary display, for a moderately complex diary file, with functions
1216 ;; used instead of macros. There were a total of 10000 such calls:
1218 ;; 1934 calendar-extract-month
1219 ;; 1852 calendar-extract-year
1220 ;; 1819 calendar-extract-day
1221 ;; 845 calendar-leap-year-p
1222 ;; 837 calendar-day-number
1223 ;; 775 calendar-absolute-from-gregorian
1224 ;; 346 calendar-last-day-of-month
1225 ;; 286 calendar-hebrew-last-day-of-month
1226 ;; 188 calendar-hebrew-leap-year-p
1227 ;; 180 calendar-hebrew-elapsed-days
1228 ;; 163 calendar-hebrew-last-month-of-year
1229 ;; 66 calendar-date-compare
1230 ;; 65 calendar-hebrew-days-in-year
1231 ;; 60 calendar-julian-to-absolute
1232 ;; 50 calendar-hebrew-to-absolute
1233 ;; 43 calendar-date-equal
1234 ;; 38 calendar-gregorian-from-absolute
1235 ;; .
1237 ;; The use of these seven macros eliminates the overhead of 92% of the function
1238 ;; calls; it's faster this way.
1240 (defsubst calendar-extract-month (date)
1241 "Extract the month part of DATE which has the form (month day year)."
1242 (car date))
1244 (define-obsolete-function-alias 'extract-calendar-month
1245 'calendar-extract-month "23.1")
1247 ;; Note gives wrong answer for result of (calendar-read-date 'noday),
1248 ;; but that is only used by `calendar-other-month'.
1249 (defsubst calendar-extract-day (date)
1250 "Extract the day part of DATE which has the form (month day year)."
1251 (cadr date))
1253 (define-obsolete-function-alias 'extract-calendar-day
1254 'calendar-extract-day "23.1")
1256 (defsubst calendar-extract-year (date)
1257 "Extract the year part of DATE which has the form (month day year)."
1258 (nth 2 date))
1260 (define-obsolete-function-alias 'extract-calendar-year
1261 'calendar-extract-year "23.1")
1263 (defsubst calendar-leap-year-p (year)
1264 "Return t if YEAR is a Gregorian leap year.
1265 A negative year is interpreted as BC; -1 being 1 BC, and so on."
1266 ;; 1 BC = 0 AD, 2 BC acts like 1 AD, etc.
1267 (if (< year 0) (setq year (1- (abs year))))
1268 (and (zerop (% year 4))
1269 (or (not (zerop (% year 100)))
1270 (zerop (% year 400)))))
1272 ;; The foregoing is a bit faster, but not as clear as the following:
1274 ;;(defsubst calendar-leap-year-p (year)
1275 ;; "Return t if YEAR is a Gregorian leap year."
1276 ;; (or
1277 ;; (and (zerop (% year 4))
1278 ;; (not (zerop (% year 100))))
1279 ;; (zerop (% year 400)))
1281 (defsubst calendar-last-day-of-month (month year)
1282 "The last day in MONTH during YEAR."
1283 (if (and (= month 2) (calendar-leap-year-p year))
1285 (aref [31 28 31 30 31 30 31 31 30 31 30 31] (1- month))))
1287 ;; An explanation of the calculation can be found in PascAlgorithms by
1288 ;; Edward and Ruth Reingold, Scott-Foresman/Little, Brown, 1988.
1290 (defsubst calendar-day-number (date)
1291 "Return the day number within the year of the date DATE.
1292 For example, (calendar-day-number '(1 1 1987)) returns the value 1,
1293 while (calendar-day-number '(12 31 1980)) returns 366."
1294 (let* ((month (calendar-extract-month date))
1295 (day (calendar-extract-day date))
1296 (year (calendar-extract-year date))
1297 (day-of-year (+ day (* 31 (1- month)))))
1298 (when (> month 2)
1299 (setq day-of-year (- day-of-year (/ (+ 23 (* 4 month)) 10)))
1300 (if (calendar-leap-year-p year)
1301 (setq day-of-year (1+ day-of-year))))
1302 day-of-year))
1304 (defsubst calendar-absolute-from-gregorian (date)
1305 "The number of days elapsed between the Gregorian date 12/31/1 BC and DATE.
1306 The Gregorian date Sunday, December 31, 1 BC is imaginary.
1307 DATE is a list of the form (month day year). A negative year is
1308 interpreted as BC; -1 being 1 BC, and so on. Dates before 12/31/1 BC
1309 return negative results."
1310 (let ((year (calendar-extract-year date))
1311 offset-years)
1312 (cond ((zerop year)
1313 (error "There was no year zero"))
1314 ((> year 0)
1315 (setq offset-years (1- year))
1316 (+ (calendar-day-number date) ; days this year
1317 (* 365 offset-years) ; + days in prior years
1318 (/ offset-years 4) ; + Julian leap years
1319 (- (/ offset-years 100)) ; - century years
1320 (/ offset-years 400))) ; + Gregorian leap years
1322 ;; Years between date and 1 BC, excluding 1 BC (1 for 2 BC, etc).
1323 (setq offset-years (abs (1+ year)))
1324 (- (calendar-day-number date)
1325 (* 365 offset-years)
1326 (/ offset-years 4)
1327 (- (/ offset-years 100))
1328 (/ offset-years 400)
1329 (calendar-day-number '(12 31 -1))))))) ; days in year 1 BC
1331 ;;;###autoload
1332 (defun calendar (&optional arg)
1333 "Display a three-month Gregorian calendar.
1334 The three months appear side by side, with the current month in
1335 the middle surrounded by the previous and next months. The
1336 cursor is put on today's date. If optional prefix argument ARG
1337 is non-nil, prompts for the central month and year.
1339 Once in the calendar window, future or past months can be moved
1340 into view. Arbitrary months can be displayed, or the calendar
1341 can be scrolled forward or backward. The cursor can be moved
1342 forward or backward by one day, one week, one month, or one year.
1343 All of these commands take prefix arguments which, when negative,
1344 cause movement in the opposite direction. For convenience, the
1345 digit keys and the minus sign are automatically prefixes. Use
1346 \\[describe-mode] for details of the key bindings in the calendar
1347 window.
1349 Displays the calendar in a separate window, or optionally in a
1350 separate frame, depending on the value of `calendar-setup'.
1352 If `calendar-view-diary-initially-flag' is non-nil, also displays the
1353 diary entries for the current date (or however many days
1354 `diary-number-of-entries' specifies). This variable can be
1355 overridden by `calendar-setup'. As well as being displayed,
1356 diary entries can also be marked on the calendar (see
1357 `calendar-mark-diary-entries-flag').
1359 Runs the following hooks:
1361 `calendar-load-hook' - after loading calendar.el
1362 `calendar-today-visible-hook', `calendar-today-invisible-hook' - after
1363 generating a calendar, if today's date is visible or not, respectively
1364 `calendar-initial-window-hook' - after first creating a calendar
1366 This function is suitable for execution in an init file."
1367 (interactive "P")
1368 ;; Avoid loading cal-x unless it will be used.
1369 (if (and (memq calendar-setup '(one-frame two-frames calendar-only))
1370 (display-multi-frame-p))
1371 (calendar-frame-setup calendar-setup arg)
1372 (calendar-basic-setup arg)))
1374 (defun calendar-basic-setup (&optional arg nodisplay)
1375 "Create a three-month calendar.
1376 If optional prefix argument ARG is non-nil, prompts for the month
1377 and year, else uses the current date. If NODISPLAY is non-nil, don't
1378 display the generated calendar."
1379 (interactive "P")
1380 (let ((buff (current-buffer)))
1381 (set-buffer (get-buffer-create calendar-buffer))
1382 (calendar-mode)
1383 (let* ((pop-up-windows t)
1384 ;; Not really needed now, but means we use exactly the same
1385 ;; behavior as before in the non-wide case (see below).
1386 (split-height-threshold 1000)
1387 (split-width-threshold calendar-split-width-threshold)
1388 (date (if arg (calendar-read-date t)
1389 (calendar-current-date)))
1390 (month (calendar-extract-month date))
1391 (year (calendar-extract-year date)))
1392 (calendar-increment-month month year (- calendar-offset))
1393 ;; Display the buffer before calling calendar-generate-window so that it
1394 ;; can get a chance to adjust the window sizes to the frame size.
1395 (unless nodisplay
1396 ;; We want a window configuration that looks something like
1397 ;; X X | Y
1398 ;; - -----
1399 ;; C Z | C
1400 ;; where C is the calendar, and the LHS is the traditional,
1401 ;; non-wide frame, and the RHS is the wide frame case.
1402 ;; We should end up in the same state regardless of whether the
1403 ;; windows were initially split or not.
1404 ;; Previously, we only thought about the non-wide case.
1405 ;; We could just set split-height-threshold to 1000, relying on
1406 ;; the fact that the window splitting treated a single window as
1407 ;; a special case and would always split it (vertically). The
1408 ;; same thing does not work in the wide-frame case, so now we do
1409 ;; the splitting by hand.
1410 ;; See discussion in bug#1806.
1411 ;; Actually, this still does not do quite the right thing in the
1412 ;; wide frame case if started from a configuration like the LHS.
1413 ;; Eg if you start with a non-wide frame, call calendar, then
1414 ;; make the frame wider. This one is problematic because you
1415 ;; might need to split a totally unrelated window. Oh well, it
1416 ;; seems unlikely, and perhaps respecting the original layout is
1417 ;; the right thing in that case.
1419 ;; Is this a wide frame? If so, split it horizontally.
1420 (if (window-splittable-p t) (split-window-right))
1421 (pop-to-buffer calendar-buffer)
1422 ;; Has the window already been split vertically?
1423 (when (and (not (window-dedicated-p))
1424 (window-full-height-p))
1425 (let ((win (split-window-below)))
1426 ;; In the upper window, show whatever was visible before.
1427 ;; This looks better than using other-buffer.
1428 (switch-to-buffer buff)
1429 ;; Switch to the lower window with the calendar buffer.
1430 (select-window win))))
1431 (calendar-generate-window month year)
1432 (if (and calendar-view-diary-initially-flag
1433 (calendar-date-is-visible-p date))
1434 ;; Do not clobber the calendar with the diary, if the diary
1435 ;; has previously been shown in the window that now shows the
1436 ;; calendar (bug#18381).
1437 (let ((display-buffer-overriding-action
1438 '(nil . ((inhibit-same-window . t)))))
1439 (diary-view-entries)))))
1440 (if calendar-view-holidays-initially-flag
1441 (let* ((diary-buffer (get-file-buffer diary-file))
1442 (diary-window (if diary-buffer (get-buffer-window diary-buffer)))
1443 (split-height-threshold (if diary-window 2 1000)))
1444 ;; FIXME display buffer?
1445 (calendar-list-holidays)))
1446 (run-hooks 'calendar-initial-window-hook))
1448 (defun calendar-generate-window (&optional mon yr)
1449 "Generate the calendar window for the current date.
1450 Optional integers MON and YR are used instead of today's date."
1451 (let* ((inhibit-read-only t)
1452 (today (calendar-current-date))
1453 (month (calendar-extract-month today))
1454 (day (calendar-extract-day today))
1455 (year (calendar-extract-year today))
1456 (today-visible (or (not mon)
1457 (<= (abs (calendar-interval mon yr month year)) 1)))
1458 (in-calendar-window (eq (window-buffer)
1459 (get-buffer calendar-buffer))))
1460 (calendar-generate (or mon month) (or yr year))
1461 (calendar-update-mode-line)
1462 (calendar-cursor-to-visible-date
1463 (if today-visible today (list displayed-month 1 displayed-year)))
1464 (set-buffer-modified-p nil)
1465 ;; Don't do any window-related stuff if we weren't called from a
1466 ;; window displaying the calendar.
1467 (when in-calendar-window
1468 (if (window-combined-p)
1469 ;; Adjust the window to exactly fit the displayed calendar.
1470 (fit-window-to-buffer nil nil calendar-minimum-window-height)
1471 ;; For a full height window or a window that is horizontally
1472 ;; combined don't fit height to that of its buffer.
1473 (set-window-vscroll nil 0))
1474 (sit-for 0))
1475 (and (bound-and-true-p font-lock-mode)
1476 (font-lock-fontify-buffer))
1477 (and calendar-mark-holidays-flag
1478 ;;; (calendar-date-is-valid-p today) ; useful for BC dates
1479 (calendar-mark-holidays)
1480 (and in-calendar-window (sit-for 0)))
1481 (unwind-protect
1482 (if calendar-mark-diary-entries-flag (diary-mark-entries))
1483 (if today-visible
1484 (run-hooks 'calendar-today-visible-hook)
1485 (run-hooks 'calendar-today-invisible-hook)))))
1487 (defun calendar-generate (month year)
1488 "Generate a three-month Gregorian calendar centered around MONTH, YEAR."
1489 ;; A negative YEAR is interpreted as BC; -1 being 1 BC, and so on.
1490 ;; Note that while calendars for years BC could be displayed as it
1491 ;; stands, almost all other calendar functions (eg holidays) would
1492 ;; at best have unpredictable results for such dates.
1493 (if (< (+ month (* 12 (1- year))) 2)
1494 (error "Months before January, 1 AD cannot be displayed"))
1495 (setq displayed-month month
1496 displayed-year year)
1497 (erase-buffer)
1498 (calendar-increment-month month year -1)
1499 (dotimes (i 3)
1500 (calendar-generate-month month year
1501 (+ calendar-left-margin
1502 (* calendar-month-width i)))
1503 (calendar-increment-month month year 1)))
1505 (defun calendar-move-to-column (indent)
1506 "Like `move-to-column', but indents if the line is too short."
1507 (if (< (move-to-column indent) indent)
1508 (indent-to indent)))
1510 (defun calendar-ensure-newline ()
1511 "Move to the next line, adding a newline if necessary."
1512 (or (zerop (forward-line 1))
1513 (insert "\n")))
1515 (defun calendar-insert-at-column (indent string truncate)
1516 "Move to column INDENT, adding spaces as needed.
1517 Inserts STRING so that it ends at INDENT. STRING is either a
1518 literal string, or a sexp to evaluate to return such. Truncates
1519 STRING to length TRUNCATE, and ensures a trailing space."
1520 (if (not (ignore-errors (stringp (setq string (eval string)))))
1521 (calendar-move-to-column indent)
1522 (if (> (string-width string) truncate)
1523 (setq string (truncate-string-to-width string truncate)))
1524 (or (string-match " $" string)
1525 (setq string (concat (if (= (string-width string) truncate)
1526 (substring string 0 -1)
1527 string)
1528 ;; Avoid inserting text properties unless
1529 ;; we have to (ie, non-unit-width chars).
1530 ;; This is by no means essential.
1531 (if (= (string-width string) (length string))
1533 ;; Cribbed from buff-menu.el.
1534 (propertize
1535 " " 'display `(space :align-to ,indent))))))
1536 (calendar-move-to-column (- indent (string-width string)))
1537 (insert string)))
1539 (defun calendar-generate-month (month year indent)
1540 "Produce a calendar for MONTH, YEAR on the Gregorian calendar.
1541 The calendar is inserted at the top of the buffer in which point is currently
1542 located, but indented INDENT spaces. The indentation is done from the first
1543 character on the line and does not disturb the first INDENT characters on the
1544 line."
1545 (let ((blank-days ; at start of month
1546 (mod
1547 (- (calendar-day-of-week (list month 1 year))
1548 calendar-week-start-day)
1550 (last (calendar-last-day-of-month month year))
1551 (trunc (min calendar-intermonth-spacing
1552 (1- calendar-left-margin)))
1553 (day 1)
1555 (goto-char (point-min))
1556 (calendar-move-to-column indent)
1557 (insert
1558 (calendar-string-spread (list calendar-month-header)
1559 ?\s calendar-month-digit-width))
1560 (calendar-ensure-newline)
1561 (calendar-insert-at-column indent calendar-intermonth-header trunc)
1562 ;; Use the first N characters of each day to head the columns.
1563 (dotimes (i 7)
1564 (setq j (mod (+ calendar-week-start-day i) 7))
1565 (insert
1566 (truncate-string-to-width
1567 (propertize (calendar-day-name j 'header t)
1568 'font-lock-face (if (memq j '(0 6))
1569 'calendar-weekend-header
1570 'calendar-weekday-header))
1571 calendar-day-header-width nil ?\s)
1572 (make-string (- calendar-column-width calendar-day-header-width) ?\s)))
1573 (calendar-ensure-newline)
1574 (calendar-insert-at-column indent calendar-intermonth-text trunc)
1575 ;; Add blank days before the first of the month.
1576 (insert (make-string (* blank-days calendar-column-width) ?\s))
1577 ;; Put in the days of the month.
1578 (dotimes (i last)
1579 (setq day (1+ i))
1580 ;; TODO should numbers be left-justified, centered...?
1581 (insert (propertize
1582 (format (format "%%%dd" calendar-day-digit-width) day)
1583 'mouse-face 'highlight
1584 'help-echo (eval calendar-date-echo-text)
1585 ;; 'date property prevents intermonth text confusing re-searches.
1586 ;; (Tried intangible, it did not really work.)
1587 'date t)
1588 (make-string
1589 (- calendar-column-width calendar-day-digit-width) ?\s))
1590 (when (and (zerop (mod (+ day blank-days) 7))
1591 (/= day last))
1592 (calendar-ensure-newline)
1593 (setq day (1+ day)) ; first day of next week
1594 (calendar-insert-at-column indent calendar-intermonth-text trunc)))))
1596 (defun calendar-redraw ()
1597 "Redraw the calendar display, if `calendar-buffer' is live."
1598 (interactive)
1599 (when (get-buffer calendar-buffer)
1600 (with-current-buffer calendar-buffer
1601 (let ((cursor-date (calendar-cursor-to-nearest-date)))
1602 (calendar-generate-window displayed-month displayed-year)
1603 (calendar-cursor-to-visible-date cursor-date))
1604 (when (window-live-p (get-buffer-window))
1605 (set-window-point (get-buffer-window) (point))))))
1607 (defvar calendar-mode-map
1608 (let ((map (make-keymap)))
1609 (suppress-keymap map)
1610 (dolist (c '(narrow-to-region mark-word mark-sexp mark-paragraph
1611 mark-defun mark-whole-buffer mark-page
1612 downcase-region upcase-region kill-region
1613 copy-region-as-kill capitalize-region write-region))
1614 (define-key map (vector 'remap c) 'calendar-not-implemented))
1615 (define-key map "<" 'calendar-scroll-right)
1616 (define-key map "\C-x<" 'calendar-scroll-right)
1617 (define-key map [prior] 'calendar-scroll-right-three-months)
1618 (define-key map "\ev" 'calendar-scroll-right-three-months)
1619 (define-key map ">" 'calendar-scroll-left)
1620 (define-key map "\C-x>" 'calendar-scroll-left)
1621 (define-key map [next] 'calendar-scroll-left-three-months)
1622 (define-key map "\C-v" 'calendar-scroll-left-three-months)
1623 (define-key map "\C-b" 'calendar-backward-day)
1624 (define-key map "\C-p" 'calendar-backward-week)
1625 (define-key map "\e{" 'calendar-backward-month)
1626 (define-key map "\C-x[" 'calendar-backward-year)
1627 (define-key map "\C-f" 'calendar-forward-day)
1628 (define-key map "\C-n" 'calendar-forward-week)
1629 (define-key map [left] 'calendar-backward-day)
1630 (define-key map [up] 'calendar-backward-week)
1631 (define-key map [right] 'calendar-forward-day)
1632 (define-key map [down] 'calendar-forward-week)
1633 (define-key map "\e}" 'calendar-forward-month)
1634 (define-key map "\C-x]" 'calendar-forward-year)
1635 (define-key map "\C-a" 'calendar-beginning-of-week)
1636 (define-key map "\C-e" 'calendar-end-of-week)
1637 (define-key map "\ea" 'calendar-beginning-of-month)
1638 (define-key map "\ee" 'calendar-end-of-month)
1639 (define-key map "\e<" 'calendar-beginning-of-year)
1640 (define-key map "\e>" 'calendar-end-of-year)
1641 (define-key map "\C-@" 'calendar-set-mark)
1642 ;; Many people are used to typing C-SPC and getting C-@.
1643 (define-key map [?\C-\s] 'calendar-set-mark)
1644 (define-key map "\C-x\C-x" 'calendar-exchange-point-and-mark)
1645 (define-key map "\e=" 'calendar-count-days-region)
1646 (define-key map "gd" 'calendar-goto-date)
1647 (define-key map "gD" 'calendar-goto-day-of-year)
1648 (define-key map "gj" 'calendar-julian-goto-date)
1649 (define-key map "ga" 'calendar-astro-goto-day-number)
1650 (define-key map "gh" 'calendar-hebrew-goto-date)
1651 (define-key map "gi" 'calendar-islamic-goto-date)
1652 (define-key map "gb" 'calendar-bahai-goto-date)
1653 (define-key map "gC" 'calendar-chinese-goto-date)
1654 (define-key map "gk" 'calendar-coptic-goto-date)
1655 (define-key map "ge" 'calendar-ethiopic-goto-date)
1656 (define-key map "gp" 'calendar-persian-goto-date)
1657 (define-key map "gc" 'calendar-iso-goto-date)
1658 (define-key map "gw" 'calendar-iso-goto-week)
1659 (define-key map "gf" 'calendar-french-goto-date)
1660 (define-key map "gml" 'calendar-mayan-goto-long-count-date)
1661 (define-key map "gmpc" 'calendar-mayan-previous-round-date)
1662 (define-key map "gmnc" 'calendar-mayan-next-round-date)
1663 (define-key map "gmph" 'calendar-mayan-previous-haab-date)
1664 (define-key map "gmnh" 'calendar-mayan-next-haab-date)
1665 (define-key map "gmpt" 'calendar-mayan-previous-tzolkin-date)
1666 (define-key map "gmnt" 'calendar-mayan-next-tzolkin-date)
1667 (define-key map "Aa" 'appt-add)
1668 (define-key map "Ad" 'appt-delete)
1669 (define-key map "S" 'calendar-sunrise-sunset)
1670 (define-key map "M" 'calendar-lunar-phases)
1671 (define-key map " " 'scroll-other-window)
1672 (define-key map [?\S-\ ] 'scroll-other-window-down)
1673 (define-key map "\d" 'scroll-other-window-down)
1674 (define-key map "\C-c\C-l" 'calendar-redraw)
1675 (define-key map "." 'calendar-goto-today)
1676 (define-key map "o" 'calendar-other-month)
1677 (define-key map "q" 'calendar-exit)
1678 (define-key map "a" 'calendar-list-holidays)
1679 (define-key map "h" 'calendar-cursor-holidays)
1680 (define-key map "x" 'calendar-mark-holidays)
1681 (define-key map "u" 'calendar-unmark)
1682 (define-key map "m" 'diary-mark-entries)
1683 (define-key map "d" 'diary-view-entries)
1684 (define-key map "D" 'diary-view-other-diary-entries)
1685 (define-key map "s" 'diary-show-all-entries)
1686 (define-key map "pd" 'calendar-print-day-of-year)
1687 (define-key map "pC" 'calendar-chinese-print-date)
1688 (define-key map "pk" 'calendar-coptic-print-date)
1689 (define-key map "pe" 'calendar-ethiopic-print-date)
1690 (define-key map "pp" 'calendar-persian-print-date)
1691 (define-key map "pc" 'calendar-iso-print-date)
1692 (define-key map "pj" 'calendar-julian-print-date)
1693 (define-key map "pa" 'calendar-astro-print-day-number)
1694 (define-key map "ph" 'calendar-hebrew-print-date)
1695 (define-key map "pi" 'calendar-islamic-print-date)
1696 (define-key map "pb" 'calendar-bahai-print-date)
1697 (define-key map "pf" 'calendar-french-print-date)
1698 (define-key map "pm" 'calendar-mayan-print-date)
1699 (define-key map "po" 'calendar-print-other-dates)
1700 (define-key map "id" 'diary-insert-entry)
1701 (define-key map "iw" 'diary-insert-weekly-entry)
1702 (define-key map "im" 'diary-insert-monthly-entry)
1703 (define-key map "iy" 'diary-insert-yearly-entry)
1704 (define-key map "ia" 'diary-insert-anniversary-entry)
1705 (define-key map "ib" 'diary-insert-block-entry)
1706 (define-key map "ic" 'diary-insert-cyclic-entry)
1707 (define-key map "ihd" 'diary-hebrew-insert-entry)
1708 (define-key map "ihm" 'diary-hebrew-insert-monthly-entry)
1709 (define-key map "ihy" 'diary-hebrew-insert-yearly-entry)
1710 (define-key map "iid" 'diary-islamic-insert-entry)
1711 (define-key map "iim" 'diary-islamic-insert-monthly-entry)
1712 (define-key map "iiy" 'diary-islamic-insert-yearly-entry)
1713 (define-key map "iBd" 'diary-bahai-insert-entry)
1714 (define-key map "iBm" 'diary-bahai-insert-monthly-entry)
1715 (define-key map "iBy" 'diary-bahai-insert-yearly-entry)
1716 (define-key map "?" 'calendar-goto-info-node)
1717 (define-key map "Hm" 'cal-html-cursor-month)
1718 (define-key map "Hy" 'cal-html-cursor-year)
1719 (define-key map "tm" 'cal-tex-cursor-month)
1720 (define-key map "tM" 'cal-tex-cursor-month-landscape)
1721 (define-key map "td" 'cal-tex-cursor-day)
1722 (define-key map "tw1" 'cal-tex-cursor-week)
1723 (define-key map "tw2" 'cal-tex-cursor-week2)
1724 (define-key map "tw3" 'cal-tex-cursor-week-iso) ; FIXME twi ?
1725 (define-key map "tw4" 'cal-tex-cursor-week-monday) ; twm ?
1726 (define-key map "twW" 'cal-tex-cursor-week2-summary)
1727 (define-key map "tfd" 'cal-tex-cursor-filofax-daily)
1728 (define-key map "tfw" 'cal-tex-cursor-filofax-2week)
1729 (define-key map "tfW" 'cal-tex-cursor-filofax-week)
1730 (define-key map "tfy" 'cal-tex-cursor-filofax-year)
1731 (define-key map "ty" 'cal-tex-cursor-year)
1732 (define-key map "tY" 'cal-tex-cursor-year-landscape)
1734 (define-key map [menu-bar edit] 'undefined)
1735 (define-key map [menu-bar search] 'undefined)
1737 (easy-menu-define nil map nil cal-menu-sunmoon-menu)
1738 (easy-menu-define nil map nil cal-menu-diary-menu)
1739 (easy-menu-define nil map nil cal-menu-holidays-menu)
1740 (easy-menu-define nil map nil cal-menu-goto-menu)
1741 (easy-menu-define nil map nil cal-menu-scroll-menu)
1743 ;; These are referenced in the default calendar-date-echo-text.
1744 (define-key map [down-mouse-3]
1745 (easy-menu-binding cal-menu-context-mouse-menu))
1746 (define-key map [down-mouse-2]
1747 (easy-menu-binding cal-menu-global-mouse-menu))
1749 ;; cf scroll-bar.el.
1750 (if (and (boundp 'x-toolkit-scroll-bars) x-toolkit-scroll-bars)
1751 (define-key map [vertical-scroll-bar mouse-1]
1752 'calendar-scroll-toolkit-scroll)
1753 ;; Left-click moves us forward in time, right-click backwards.
1754 (define-key map [vertical-scroll-bar mouse-1] 'calendar-scroll-left)
1755 (define-key map [vertical-scroll-bar drag-mouse-1] 'calendar-scroll-left)
1756 ;; down-mouse-2 stays as scroll-bar-drag.
1757 (define-key map [vertical-scroll-bar mouse-3] 'calendar-scroll-right)
1758 (define-key map [vertical-scroll-bar drag-mouse-3]
1759 'calendar-scroll-right))
1760 map)
1761 "Keymap for `calendar-mode'.")
1763 ;; Calendar mode is suitable only for specially formatted data.
1764 (put 'calendar-mode 'mode-class 'special)
1766 (defun calendar-mode-line-entry (command echo &optional key string)
1767 "Return a propertized string for `calendar-mode-line-format'.
1768 COMMAND is a command to run, ECHO is the help-echo text, KEY
1769 is COMMAND's keybinding, STRING describes the binding."
1770 (propertize (or key
1771 (substitute-command-keys
1772 (format "\\<calendar-mode-map>\\[%s] %s" command string)))
1773 'help-echo (format "mouse-1: %s" echo)
1774 'mouse-face 'mode-line-highlight
1775 'keymap (make-mode-line-mouse-map 'mouse-1 command)))
1777 ;; After calendar-mode-map.
1778 (defcustom calendar-mode-line-format
1779 (list
1780 (calendar-mode-line-entry 'calendar-scroll-right "previous month" "<")
1781 "Calendar"
1782 (concat
1783 (calendar-mode-line-entry 'calendar-goto-info-node "read Info on Calendar"
1784 nil "info")
1785 " / "
1786 (calendar-mode-line-entry 'calendar-other-month "choose another month"
1787 nil "other")
1788 " / "
1789 (calendar-mode-line-entry 'calendar-goto-today "go to today's date"
1790 nil "today"))
1791 '(calendar-date-string (calendar-current-date) t)
1792 (calendar-mode-line-entry 'calendar-scroll-left "next month" ">"))
1793 "The mode line of the calendar buffer.
1794 This is a list of items that evaluate to strings. The elements
1795 are evaluated and concatenated, evenly separated by blanks.
1796 During evaluation, the variable `date' is available as the date
1797 nearest the cursor (or today's date if that fails). To update
1798 the mode-line as the cursor moves, add `calendar-update-mode-line'
1799 to `calendar-move-hook'. Here is an example that has the Hebrew date,
1800 the day number/days remaining in the year, and the ISO week/year numbers:
1802 (list
1803 \"\"
1804 '(calendar-hebrew-date-string date)
1805 '(let* ((year (calendar-extract-year date))
1806 (d (calendar-day-number date))
1807 (days-remaining
1808 (- (calendar-day-number (list 12 31 year)) d)))
1809 (format \"%d/%d\" d days-remaining))
1810 '(let* ((d (calendar-absolute-from-gregorian date))
1811 (iso-date (calendar-iso-from-absolute d)))
1812 (format \"ISO week %d of %d\"
1813 (calendar-extract-month iso-date)
1814 (calendar-extract-year iso-date)))
1815 \"\"))"
1816 :risky t
1817 :type 'sexp
1818 :group 'calendar)
1820 (defun calendar-goto-info-node ()
1821 "Go to the info node for the calendar."
1822 (interactive)
1823 (info "(emacs)Calendar/Diary")
1824 (fit-window-to-buffer))
1826 (defvar calendar-mark-ring nil
1827 "Used by `calendar-set-mark'.")
1829 (define-derived-mode calendar-mode nil "Calendar"
1830 "A major mode for the calendar window.
1831 For a complete description, see the info node `Calendar/Diary'.
1833 \\<calendar-mode-map>\\{calendar-mode-map}"
1834 (setq buffer-read-only t
1835 buffer-undo-list t
1836 indent-tabs-mode nil)
1837 (set (make-local-variable 'scroll-margin) 0) ; bug#10379
1838 (calendar-update-mode-line)
1839 (make-local-variable 'calendar-mark-ring)
1840 (make-local-variable 'displayed-month) ; month in middle of window
1841 (make-local-variable 'displayed-year) ; year in middle of window
1842 ;; Most functions only work if displayed-month and displayed-year are set,
1843 ;; so let's make sure they're always set. Most likely, this will be reset
1844 ;; soon in calendar-generate, but better safe than sorry.
1845 (unless (boundp 'displayed-month) (setq displayed-month 1))
1846 (unless (boundp 'displayed-year) (setq displayed-year 2001))
1847 (if (bound-and-true-p calendar-font-lock-keywords)
1848 (set (make-local-variable 'font-lock-defaults)
1849 '(calendar-font-lock-keywords t))))
1851 (defun calendar-string-spread (strings char length)
1852 "Concatenate list of STRINGS separated with copies of CHAR to fill LENGTH.
1853 The effect is like mapconcat but the separating pieces are as balanced as
1854 possible. Each item of STRINGS is evaluated before concatenation so it can
1855 actually be an expression that evaluates to a string. If LENGTH is too short,
1856 the STRINGS are just concatenated and the result truncated."
1857 ;; The algorithm is based on equation (3.25) on page 85 of Concrete
1858 ;; Mathematics by Ronald L. Graham, Donald E. Knuth, and Oren Patashnik,
1859 ;; Addison-Wesley, Reading, MA, 1989.
1860 (let* ((strings (mapcar 'eval
1861 (if (< (length strings) 2)
1862 (append (list "") strings (list ""))
1863 strings)))
1864 (n (- length (string-width (apply 'concat strings))))
1865 (m (* (1- (length strings)) (char-width char)))
1866 (s (car strings))
1867 (strings (cdr strings))
1868 (i 0))
1869 (dolist (string strings)
1870 (setq s (concat s
1871 (make-string (max 0 (/ (+ n i) m)) char)
1872 string)
1873 i (1+ i)))
1874 (truncate-string-to-width s length)))
1876 (defun calendar-update-mode-line ()
1877 "Update the calendar mode line with the current date and date style."
1878 (if (bufferp (get-buffer calendar-buffer))
1879 (with-current-buffer calendar-buffer
1880 (let ((start (- calendar-left-margin 2))
1881 (date (condition-case nil
1882 (calendar-cursor-to-nearest-date)
1883 (error (calendar-current-date)))))
1884 (setq mode-line-format
1885 (concat (make-string (max 0 (+ start
1886 (- (car (window-inside-edges))
1887 (car (window-edges))))) ?\s)
1888 (calendar-string-spread
1889 (mapcar 'eval calendar-mode-line-format)
1890 ?\s (- calendar-right-margin (1- start))))))
1891 (force-mode-line-update))))
1893 (defun calendar-buffer-list ()
1894 "List of all calendar-related buffers (as buffers, not strings)."
1895 (let (buffs)
1896 (dolist (b (list calendar-hebrew-yahrzeit-buffer lunar-phases-buffer
1897 holiday-buffer diary-fancy-buffer solar-sunrises-buffer
1898 (get-file-buffer diary-file)
1899 calendar-buffer calendar-other-calendars-buffer))
1900 (and b (setq b (get-buffer b))
1901 (push b buffs)))
1902 buffs))
1904 (defun calendar-exit (&optional kill)
1905 "Get out of the calendar window and hide it and related buffers."
1906 (interactive "P")
1907 (let ((diary-buffer (get-file-buffer diary-file))
1908 (calendar-buffers (calendar-buffer-list)))
1909 (when (or (not diary-buffer)
1910 (not (buffer-modified-p diary-buffer))
1911 (yes-or-no-p
1912 "Diary modified; do you really want to exit the calendar? "))
1913 (if (and calendar-setup (display-multi-frame-p))
1914 ;; FIXME: replace this cruft with the `quit-restore' window property
1915 (dolist (w (window-list-1 nil nil t))
1916 (if (and (memq (window-buffer w) calendar-buffers)
1917 (window-dedicated-p w))
1918 (if (eq (window-deletable-p w) 'frame)
1919 (if calendar-remove-frame-by-deleting
1920 (delete-frame (window-frame w))
1921 (iconify-frame (window-frame w)))
1922 (quit-window kill w))))
1923 (dolist (b calendar-buffers)
1924 (quit-windows-on b kill))))))
1926 (define-obsolete-function-alias 'exit-calendar 'calendar-exit "23.1")
1928 (defun calendar-current-date (&optional offset)
1929 "Return the current date in a list (month day year).
1930 Optional integer OFFSET is a number of days from the current date."
1931 (let* ((now (decode-time))
1932 (now (list (nth 4 now) (nth 3 now) (nth 5 now))))
1933 (if (zerop (or offset 0))
1935 (calendar-gregorian-from-absolute
1936 (+ offset (calendar-absolute-from-gregorian now))))))
1938 (defun calendar-column-to-segment ()
1939 "Convert current column to calendar month \"segment\".
1940 The left-most month returns 0, the next right 1, and so on."
1941 (let ((col (max 0 (+ (current-column)
1942 (/ calendar-intermonth-spacing 2)
1943 (- calendar-left-margin)))))
1944 (/ col (+ (* 7 calendar-column-width) calendar-intermonth-spacing))))
1946 (defun calendar-cursor-to-date (&optional error event)
1947 "Return a list (month day year) of current cursor position.
1948 If cursor is not on a specific date, signals an error if optional parameter
1949 ERROR is non-nil, otherwise just returns nil.
1950 If EVENT is non-nil, it's an event indicating the buffer position to
1951 use instead of point."
1952 (with-current-buffer
1953 (if event (window-buffer (posn-window (event-start event)))
1954 (current-buffer))
1955 (save-excursion
1956 (and event (setq event (event-start event))
1957 (goto-char (posn-point event)))
1958 (let* ((segment (calendar-column-to-segment))
1959 (month (% (+ displayed-month (1- segment)) 12)))
1960 ;; Call with point on either of the two digits in a 2-digit date,
1961 ;; or on or before the digit of a 1-digit date.
1962 (if (not (and (looking-at "[ 0-9]?[0-9][^0-9]")
1963 (get-text-property (point) 'date)))
1964 (if error (user-error "Not on a date!"))
1965 ;; Convert segment to real month and year.
1966 (if (zerop month) (setq month 12))
1967 ;; Go back to before the first date digit.
1968 (or (looking-at " ")
1969 (re-search-backward "[^0-9]"))
1970 (list month
1971 (string-to-number
1972 (buffer-substring (1+ (point))
1973 (+ 1 calendar-day-digit-width (point))))
1974 (cond
1975 ((and (= 12 month) (zerop segment)) (1- displayed-year))
1976 ((and (= 1 month) (= segment 2)) (1+ displayed-year))
1977 (t displayed-year))))))))
1979 ;; The following version of calendar-gregorian-from-absolute is preferred for
1980 ;; reasons of clarity, BUT it's much slower than the version that follows it.
1982 ;;(defun calendar-gregorian-from-absolute (date)
1983 ;; "Compute the list (month day year) corresponding to the absolute DATE.
1984 ;;The absolute date is the number of days elapsed since the (imaginary)
1985 ;;Gregorian date Sunday, December 31, 1 BC."
1986 ;; (let* ((approx (/ date 366)) ; approximation from below
1987 ;; (year ; search forward from the approximation
1988 ;; (+ approx
1989 ;; (calendar-sum y approx
1990 ;; (>= date (calendar-absolute-from-gregorian (list 1 1 (1+ y))))
1991 ;; 1)))
1992 ;; (month ; search forward from January
1993 ;; (1+ (calendar-sum m 1
1994 ;; (> date
1995 ;; (calendar-absolute-from-gregorian
1996 ;; (list m (calendar-last-day-of-month m year) year)))
1997 ;; 1)))
1998 ;; (day ; calculate the day by subtraction
1999 ;; (- date
2000 ;; (1- (calendar-absolute-from-gregorian (list month 1 year))))))
2001 ;; (list month day year)))
2003 (defun calendar-gregorian-from-absolute (date)
2004 "Compute the list (month day year) corresponding to the absolute DATE.
2005 The absolute date is the number of days elapsed since the (imaginary)
2006 Gregorian date Sunday, December 31, 1 BC. This function does not
2007 handle dates in years BC."
2008 ;; See the footnote on page 384 of ``Calendrical Calculations, Part II:
2009 ;; Three Historical Calendars'' by E. M. Reingold, N. Dershowitz, and S. M.
2010 ;; Clamen, Software--Practice and Experience, Volume 23, Number 4
2011 ;; (April, 1993), pages 383-404 for an explanation.
2012 (let* ((d0 (1- date))
2013 (n400 (/ d0 146097))
2014 (d1 (% d0 146097))
2015 (n100 (/ d1 36524))
2016 (d2 (% d1 36524))
2017 (n4 (/ d2 1461))
2018 (d3 (% d2 1461))
2019 (n1 (/ d3 365))
2020 (day (1+ (% d3 365)))
2021 (year (+ (* 400 n400) (* 100 n100) (* n4 4) n1))
2022 (month 1)
2023 mdays)
2024 (if (or (= n100 4) (= n1 4))
2025 (list 12 31 year)
2026 (setq year (1+ year))
2027 (while (< (setq mdays (calendar-last-day-of-month month year)) day)
2028 (setq day (- day mdays)
2029 month (1+ month)))
2030 (list month day year))))
2032 (defun calendar-other-month (month year &optional event)
2033 "Display a three-month calendar centered around MONTH and YEAR.
2034 EVENT is an event like `last-nonmenu-event'."
2035 (interactive (let ((event (list last-nonmenu-event)))
2036 (append (calendar-read-date 'noday) event)))
2037 (save-selected-window
2038 (and event
2039 (setq event (event-start event))
2040 (select-window (posn-window event)))
2041 (unless (and (= month displayed-month)
2042 (= year displayed-year))
2043 (let ((old-date (calendar-cursor-to-date))
2044 (today (calendar-current-date)))
2045 (calendar-generate-window month year)
2046 (calendar-cursor-to-visible-date
2047 (cond
2048 ((calendar-date-is-visible-p old-date) old-date)
2049 ((calendar-date-is-visible-p today) today)
2050 (t (list month 1 year))))))))
2052 (defun calendar-set-mark (arg &optional event)
2053 "Mark the date under the cursor, or jump to marked date.
2054 With no prefix argument, push current date onto marked date ring.
2055 With argument ARG, jump to mark, pop it, and put point at end of ring."
2056 (interactive
2057 (list current-prefix-arg last-nonmenu-event))
2058 (let ((date (calendar-cursor-to-date t event)))
2059 (if arg
2060 (if (null calendar-mark-ring)
2061 (error "No mark set in this buffer")
2062 (calendar-goto-date (car calendar-mark-ring))
2063 (setq calendar-mark-ring
2064 (cdr (nconc calendar-mark-ring (list date)))))
2065 (push date calendar-mark-ring)
2066 ;; Since the top of the mark ring is the marked date in the
2067 ;; calendar, the mark ring in the calendar is one longer than
2068 ;; in other buffers to get the same effect.
2069 (if (> (length calendar-mark-ring) (1+ mark-ring-max))
2070 (setcdr (nthcdr mark-ring-max calendar-mark-ring) nil))
2071 (message "Mark set"))))
2073 (defun calendar-exchange-point-and-mark ()
2074 "Exchange the current cursor position with the marked date."
2075 (interactive)
2076 (let ((mark (car calendar-mark-ring))
2077 (date (calendar-cursor-to-date t)))
2078 (if (null mark)
2079 (error "No mark set in this buffer")
2080 (setq calendar-mark-ring (cons date (cdr calendar-mark-ring)))
2081 (calendar-goto-date mark))))
2083 (defun calendar-count-days-region ()
2084 "Count the number of days (inclusive) between point and the mark."
2085 (interactive)
2086 (let* ((days (- (calendar-absolute-from-gregorian
2087 (calendar-cursor-to-date t))
2088 (calendar-absolute-from-gregorian
2089 (or (car calendar-mark-ring)
2090 (error "No mark set in this buffer")))))
2091 (days (1+ (if (> days 0) days (- days)))))
2092 (message "Region has %d day%s (inclusive)"
2093 days (if (> days 1) "s" ""))))
2095 (defun calendar-not-implemented ()
2096 "Not implemented."
2097 (interactive)
2098 (error "%s not available in the calendar"
2099 (global-key-binding (this-command-keys))))
2101 (defun calendar-read (prompt acceptable &optional initial-contents)
2102 "Return an object read from the minibuffer.
2103 Prompt with the string PROMPT and use the function ACCEPTABLE to decide if
2104 entered item is acceptable. If non-nil, optional third arg INITIAL-CONTENTS
2105 is a string to insert in the minibuffer before reading."
2106 (let ((value (read-minibuffer prompt initial-contents)))
2107 (while (not (funcall acceptable value))
2108 (setq value (read-minibuffer prompt initial-contents)))
2109 value))
2112 (defun calendar-customized-p (symbol)
2113 "Return non-nil if SYMBOL has been customized."
2114 (and (default-boundp symbol)
2115 (let ((standard (get symbol 'standard-value)))
2116 (and standard
2117 (not (equal (eval (car standard)) (default-value symbol)))))))
2119 (defun calendar-abbrev-construct (full &optional maxlen)
2120 "From sequence FULL, return a vector of abbreviations.
2121 Each abbreviation is no longer than MAXLEN (default `calendar-abbrev-length')
2122 characters."
2123 (or maxlen (setq maxlen calendar-abbrev-length))
2124 (apply 'vector (mapcar
2125 (lambda (f)
2126 ;; TODO? truncate-string-to-width?
2127 (substring f 0 (min maxlen (length f))))
2128 full)))
2130 (defcustom calendar-day-name-array
2131 ["Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday"]
2132 "Array of capitalized strings giving, in order from Sunday, the day names.
2133 If you change this without using customize after the calendar has loaded,
2134 then you may also want to change `calendar-day-abbrev-array'
2135 and `calendar-day-header-array'."
2136 :group 'calendar
2137 :initialize 'custom-initialize-default
2138 :set (lambda (symbol value)
2139 (let ((dcustomized (calendar-customized-p 'calendar-day-abbrev-array))
2140 (hcustomized (calendar-customized-p 'cal-html-day-abbrev-array))
2141 (ccustomized (calendar-customized-p 'calendar-day-header-array)))
2142 (set symbol value)
2143 (or dcustomized
2144 (setq calendar-day-abbrev-array
2145 (calendar-abbrev-construct calendar-day-name-array)))
2146 (and (not hcustomized)
2147 (boundp 'cal-html-day-abbrev-array)
2148 (setq cal-html-day-abbrev-array calendar-day-abbrev-array))
2149 (or ccustomized
2150 (equal calendar-day-header-array
2151 (setq calendar-day-header-array
2152 (calendar-day-header-construct)))
2153 (calendar-redraw))))
2154 :type '(vector (string :tag "Sunday")
2155 (string :tag "Monday")
2156 (string :tag "Tuesday")
2157 (string :tag "Wednesday")
2158 (string :tag "Thursday")
2159 (string :tag "Friday")
2160 (string :tag "Saturday")))
2162 (defcustom calendar-abbrev-length 3
2163 "Default length of abbreviations to use for day and month names.
2164 If you change this without using customize after the calendar has loaded,
2165 then you may also want to change `calendar-day-abbrev-array' and
2166 `calendar-month-abbrev-array'."
2167 :group 'calendar
2168 :initialize 'custom-initialize-default
2169 :set (lambda (symbol value)
2170 (let ((dcustomized (calendar-customized-p 'calendar-day-abbrev-array))
2171 (mcustomized (calendar-customized-p
2172 'calendar-month-abbrev-array))
2173 (hcustomized (calendar-customized-p 'cal-html-day-abbrev-array))
2174 (ccustomized (calendar-customized-p 'calendar-day-header-array)))
2175 (set symbol value)
2176 (or dcustomized
2177 (setq calendar-day-abbrev-array
2178 (calendar-abbrev-construct calendar-day-name-array)))
2179 (or mcustomized
2180 (setq calendar-month-abbrev-array
2181 (calendar-abbrev-construct calendar-month-name-array)))
2182 (and (not hcustomized)
2183 (boundp 'cal-html-day-abbrev-array)
2184 (setq cal-html-day-abbrev-array calendar-day-abbrev-array))
2185 (or ccustomized
2186 (equal calendar-day-header-array
2187 (setq calendar-day-header-array
2188 (calendar-day-header-construct)))
2189 (calendar-redraw))))
2190 :type 'integer)
2192 (defcustom calendar-day-abbrev-array
2193 (calendar-abbrev-construct calendar-day-name-array)
2194 "Array of capitalized strings giving the abbreviated day names.
2195 The order should be the same as that of the full names specified
2196 in `calendar-day-name-array'. These abbreviations may be used
2197 instead of the full names in the diary file. Do not include a
2198 trailing `.' in the strings specified in this variable, though
2199 you may use such in the diary file. By default, each string is
2200 the first `calendar-abbrev-length' characters of the corresponding
2201 full name."
2202 :group 'calendar
2203 :initialize 'custom-initialize-default
2204 :set-after '(calendar-abbrev-length calendar-day-name-array)
2205 :set (lambda (symbol value)
2206 (let ((hcustomized (calendar-customized-p 'cal-html-day-abbrev-array))
2207 (ccustomized (calendar-customized-p 'calendar-day-header-array)))
2208 (set symbol value)
2209 (and (not hcustomized)
2210 (boundp 'cal-html-day-abbrev-array)
2211 (setq cal-html-day-abbrev-array calendar-day-abbrev-array))
2212 (or ccustomized
2213 (equal calendar-day-header-array
2214 (setq calendar-day-header-array
2215 (calendar-day-header-construct)))
2216 (calendar-redraw))))
2217 :type '(vector (string :tag "Sun")
2218 (string :tag "Mon")
2219 (string :tag "Tue")
2220 (string :tag "Wed")
2221 (string :tag "Thu")
2222 (string :tag "Fri")
2223 (string :tag "Sat"))
2224 ;; Made defcustom, changed defaults from nil nil...
2225 :version "24.1")
2227 (defcustom calendar-day-header-array (calendar-day-header-construct)
2228 "Array of strings to use for the headers of the calendar's day columns.
2229 The order should be the same as in `calendar-day-name-array'.
2230 In use, the calendar truncates elements to no more than
2231 `calendar-day-header-width' columns wide.
2232 Emacs constructs the default from either `calendar-day-name-array'
2233 \(if `calendar-day-header-width' is more than `calendar-abbrev-length'),
2234 or from `calendar-day-abbrev-array' (assuming that the abbreviated
2235 name are more likely to be unique when truncated)."
2236 :group 'calendar
2237 :initialize 'custom-initialize-default
2238 :set-after '(calendar-day-header-width
2239 calendar-abbrev-length calendar-day-name-array
2240 calendar-day-abbrev-array)
2241 :set (lambda (symbol value)
2242 (or (equal calendar-day-header-array
2243 (set symbol value))
2244 (calendar-redraw)))
2245 :type '(vector (string :tag "Su")
2246 (string :tag "Mo")
2247 (string :tag "Tu")
2248 (string :tag "We")
2249 (string :tag "Th")
2250 (string :tag "Fr")
2251 (string :tag "Sa"))
2252 :version "24.4")
2254 (defcustom calendar-month-name-array
2255 ["January" "February" "March" "April" "May" "June"
2256 "July" "August" "September" "October" "November" "December"]
2257 "Array of capitalized strings giving, in order, the month names.
2258 If you change this without using customize after the calendar has loaded,
2259 then you may also want to change `calendar-month-abbrev-array'."
2260 :group 'calendar
2261 :initialize 'custom-initialize-default
2262 :set (lambda (symbol value)
2263 (let ((mcustomized (calendar-customized-p
2264 'calendar-month-abbrev-array)))
2265 (set symbol value)
2266 (or mcustomized
2267 (setq calendar-month-abbrev-array
2268 (calendar-abbrev-construct calendar-month-name-array)))))
2269 :type '(vector (string :tag "January")
2270 (string :tag "February")
2271 (string :tag "March")
2272 (string :tag "April")
2273 (string :tag "May")
2274 (string :tag "June")
2275 (string :tag "July")
2276 (string :tag "August")
2277 (string :tag "September")
2278 (string :tag "October")
2279 (string :tag "November")
2280 (string :tag "December")))
2282 (defcustom calendar-month-abbrev-array
2283 (calendar-abbrev-construct calendar-month-name-array)
2284 "Array of capitalized strings giving the abbreviated month names.
2285 The order should be the same as that of the full names specified
2286 in `calendar-month-name-array'. These abbreviations are used in
2287 the calendar menu entries, and can also be used in the diary
2288 file. Do not include a trailing `.' in the strings specified in
2289 this variable, though you may use such in the diary file. By
2290 default, each string is the first ``calendar-abbrev-length'
2291 characters of the corresponding full name."
2292 :group 'calendar
2293 :set-after '(calendar-abbrev-length calendar-month-name-array)
2294 :type '(vector (string :tag "Jan")
2295 (string :tag "Feb")
2296 (string :tag "Mar")
2297 (string :tag "Apr")
2298 (string :tag "May")
2299 (string :tag "Jun")
2300 (string :tag "Jul")
2301 (string :tag "Aug")
2302 (string :tag "Sep")
2303 (string :tag "Oct")
2304 (string :tag "Nov")
2305 (string :tag "Dec"))
2306 ;; Made defcustom, changed defaults from nil nil...
2307 :version "24.1")
2309 (defun calendar-make-alist (sequence &optional start-index filter
2310 &rest sequences)
2311 "Return an association list corresponding to SEQUENCE.
2312 Associates each element of SEQUENCE with an incremented integer,
2313 starting from START-INDEX (default 1). Applies the function FILTER,
2314 if provided, to each key in the alist. Repeats the process, with
2315 indices starting from START-INDEX each time, for any remaining
2316 arguments SEQUENCES."
2317 (or start-index (setq start-index 1))
2318 (let (index alist)
2319 (mapc (lambda (seq)
2320 (setq index start-index)
2321 (mapc (lambda (elem)
2322 (setq alist (cons
2323 (cons (if filter (funcall filter elem) elem)
2324 index)
2325 alist)
2326 index (1+ index)))
2327 seq))
2328 (append (list sequence) sequences))
2329 (reverse alist)))
2331 (defun calendar-read-date (&optional noday)
2332 "Prompt for Gregorian date. Return a list (month day year).
2333 If optional NODAY is t, does not ask for day, but just returns
2334 \(month 1 year); if NODAY is any other non-nil value the value returned is
2335 \(month year)"
2336 (let* ((year (calendar-read
2337 "Year (>0): "
2338 (lambda (x) (> x 0))
2339 (number-to-string (calendar-extract-year
2340 (calendar-current-date)))))
2341 (month-array calendar-month-name-array)
2342 (completion-ignore-case t)
2343 (month (cdr (assoc-string
2344 (completing-read
2345 "Month name: "
2346 (mapcar 'list (append month-array nil))
2347 nil t)
2348 (calendar-make-alist month-array 1) t)))
2349 (last (calendar-last-day-of-month month year)))
2350 (if noday
2351 (if (eq noday t)
2352 (list month 1 year)
2353 (list month year))
2354 (list month
2355 (calendar-read (format "Day (1-%d): " last)
2356 (lambda (x) (and (< 0 x) (<= x last))))
2357 year))))
2359 (defun calendar-interval (mon1 yr1 mon2 yr2)
2360 "The number of months difference between MON1, YR1 and MON2, YR2.
2361 The result is positive if the second date is later than the first.
2362 Negative years are interpreted as years BC; -1 being 1 BC, and so on."
2363 (if (< yr1 0) (setq yr1 (1+ yr1))) ; -1 BC -> 0 AD, etc
2364 (if (< yr2 0) (setq yr2 (1+ yr2)))
2365 (+ (* 12 (- yr2 yr1))
2366 (- mon2 mon1)))
2368 (defvar calendar-font-lock-keywords nil
2369 "Default keywords to highlight in Calendar mode.")
2371 (make-obsolete-variable 'calendar-font-lock-keywords
2372 "set font-lock keywords in `calendar-mode-hook', \
2373 or customize calendar faces." "24.4")
2375 (defun calendar-day-name (date &optional abbrev absolute)
2376 "Return a string with the name of the day of the week of DATE.
2377 DATE should be a list in the format (MONTH DAY YEAR), unless the
2378 optional argument ABSOLUTE is non-nil, in which case DATE should
2379 be an integer in the range 0 to 6 corresponding to the day of the
2380 week. Day names are taken from the variable `calendar-day-name-array',
2381 unless the optional argument ABBREV is non-nil:
2382 `header' means to use `calendar-day-header-array';
2383 t to use `calendar-day-abbrev-array'."
2384 (aref (cond ((eq abbrev 'header) calendar-day-header-array)
2385 (abbrev calendar-day-abbrev-array)
2386 (t calendar-day-name-array))
2387 (if absolute date (calendar-day-of-week date))))
2389 (defun calendar-month-name (month &optional abbrev)
2390 "Return a string with the name of month number MONTH.
2391 Months are numbered from one. Month names are taken from the
2392 variable `calendar-month-name-array', unless the optional
2393 argument ABBREV is non-nil, in which case
2394 `calendar-month-abbrev-array' is used."
2395 (aref (if abbrev calendar-month-abbrev-array calendar-month-name-array)
2396 (1- month)))
2398 (defun calendar-day-of-week (date)
2399 "Return the day-of-the-week index of DATE, 0 for Sunday, 1 for Monday, etc.
2400 DATE is a list of the form (month day year). A negative year is
2401 interpreted as BC; -1 being 1 BC, and so on."
2402 (mod (calendar-absolute-from-gregorian date) 7))
2404 (defun calendar-week-end-day ()
2405 "Return the index (0 for Sunday, etc.) of the last day of the week."
2406 (mod (+ calendar-week-start-day 6) 7))
2408 (defun calendar-unmark ()
2409 "Delete all diary/holiday marks/highlighting from the calendar."
2410 (interactive)
2411 (setq calendar-mark-holidays-flag nil
2412 calendar-mark-diary-entries-flag nil)
2413 (with-current-buffer calendar-buffer
2414 (mapc 'delete-overlay (overlays-in (point-min) (point-max)))))
2416 (defun calendar-date-is-visible-p (date)
2417 "Return non-nil if DATE is valid and is visible in the calendar window."
2418 (and (calendar-date-is-valid-p date)
2419 (< (abs (calendar-interval
2420 displayed-month displayed-year
2421 (calendar-extract-month date) (calendar-extract-year date)))
2422 2)))
2424 ;; FIXME can this be generalized for holiday-chinese?
2425 (defun calendar-nongregorian-visible-p (month day toabs fromabs switch)
2426 "Return non-nil if MONTH, DAY is visible in the calendar window.
2427 MONTH and DAY are in some non-Gregorian calendar system. The
2428 functions TOABS and FROMABS convert that system to and from
2429 absolute, respectively. SWITCH is a function that takes a single
2430 argument (a local month number). It applies when the local year
2431 changes across the calendar window, and returns non-nil if the
2432 specified month should be associated with the higher year.
2433 Returns the corresponding Gregorian date."
2434 ;; We need to choose the local year associated with month and day
2435 ;; that might make them visible.
2436 (let* ((m1 displayed-month)
2437 (y1 displayed-year)
2438 (m2 displayed-month)
2439 (y2 displayed-year)
2440 ;; Absolute date of first/last dates in calendar window.
2441 (start-date (progn
2442 (calendar-increment-month m1 y1 -1)
2443 (calendar-absolute-from-gregorian (list m1 1 y1))))
2444 (end-date (progn
2445 (calendar-increment-month m2 y2 1)
2446 (calendar-absolute-from-gregorian
2447 (list m2 (calendar-last-day-of-month m2 y2) y2))))
2448 ;; Local date of first/last date in calendar window.
2449 (local-start (funcall fromabs start-date))
2450 (local-end (funcall fromabs end-date))
2451 ;; Local year of first/last dates.
2452 ;; Can only differ if displayed-month = 12, 1, 2.
2453 (local-y1 (calendar-extract-year local-start))
2454 (local-y2 (calendar-extract-year local-end))
2455 ;; Choose which year might be visible in the window.
2456 ;; Obviously it only matters when y1 and y2 differ, ie
2457 ;; when the _local_ new year is visible.
2458 (year (if (funcall switch month) local-y2 local-y1))
2459 (date (calendar-gregorian-from-absolute
2460 (funcall toabs (list month day year)))))
2461 (if (calendar-date-is-visible-p date)
2462 date)))
2464 (defun calendar-date-is-valid-p (date)
2465 "Return t if DATE is a valid date."
2466 (let ((month (calendar-extract-month date))
2467 (day (calendar-extract-day date))
2468 (year (calendar-extract-year date)))
2469 (and (<= 1 month) (<= month 12)
2470 ;; (calendar-read-date t) used to return a date with day = nil.
2471 ;; Should not be valid (?), since many funcs prob assume integer.
2472 ;; (calendar-read-date 'noday) returns (month year), which
2473 ;; currently results in calendar-extract-year returning nil.
2474 day year (<= 1 day) (<= day (calendar-last-day-of-month month year))
2475 ;; BC dates left as non-valid, to suppress errors from
2476 ;; complex holiday algorithms not suitable for years BC.
2477 ;; Note there are side effects on calendar navigation.
2478 (<= 1 year))))
2480 (define-obsolete-function-alias 'calendar-date-is-legal-p
2481 'calendar-date-is-valid-p "23.1")
2483 (defun calendar-date-equal (date1 date2)
2484 "Return t if the DATE1 and DATE2 are the same."
2485 (and
2486 (= (calendar-extract-month date1) (calendar-extract-month date2))
2487 (= (calendar-extract-day date1) (calendar-extract-day date2))
2488 (= (calendar-extract-year date1) (calendar-extract-year date2))))
2490 (defun calendar-make-temp-face (attrlist)
2491 "Return a temporary face based on the attributes in ATTRLIST.
2492 ATTRLIST is a list with elements of the form :face face :foreground color."
2493 (let ((attrs attrlist)
2494 faceinfo face temp-face)
2495 ;; Separate :face from the other attributes. Use the last :face
2496 ;; if there are more than one. FIXME is merging meaningful?
2497 (while attrs
2498 (if (eq (car attrs) :face)
2499 (setq face (intern-soft (cadr attrs))
2500 attrs (cddr attrs))
2501 (push (car attrs) faceinfo)
2502 (setq attrs (cdr attrs))))
2503 (or (facep face) (setq face 'default))
2504 (if (not faceinfo)
2505 ;; No attributes to apply, so just use an existing-face.
2506 face
2507 ;; FIXME should we be using numbered temp-faces, re-using where poss?
2508 (setq temp-face
2509 (make-symbol
2510 (concat ":caltemp"
2511 (mapconcat (lambda (sym)
2512 (cond
2513 ((symbolp sym) (symbol-name sym))
2514 ((numberp sym) (number-to-string sym))
2515 (t sym)))
2516 attrlist ""))))
2517 (make-face temp-face)
2518 (copy-face face temp-face)
2519 ;; Apply the font aspects.
2520 (apply 'set-face-attribute temp-face nil (nreverse faceinfo))
2521 temp-face)))
2523 (defun calendar-mark-visible-date (date &optional mark)
2524 "Mark DATE in the calendar window with MARK.
2525 MARK is a single-character string, a list of face attributes/values, or a face.
2526 MARK defaults to `diary-entry-marker'."
2527 (if (calendar-date-is-valid-p date)
2528 (with-current-buffer calendar-buffer
2529 (save-excursion
2530 (calendar-cursor-to-visible-date date)
2531 (setq mark
2532 (or (and (stringp mark) (= (length mark) 1) mark) ; single-char
2533 ;; The next two use to also check font-lock-mode.
2534 ;; See comments above diary-entry-marker for why
2535 ;; this was dropped.
2536 ;;; (and font-lock-mode
2537 ;;; (or
2538 (and (listp mark) (> (length mark) 0) mark) ; attrs
2539 (and (facep mark) mark) ; )) face-name
2540 diary-entry-marker))
2541 (cond
2542 ;; Face or an attr-list that contained a face.
2543 ((facep mark)
2544 (overlay-put
2545 (make-overlay (1- (point)) (1+ (point))) 'face mark))
2546 ;; Single-character mark, goes after the date.
2547 ((and (stringp mark) (= (length mark) 1))
2548 (overlay-put
2549 (make-overlay (1+ (point)) (+ 2 (point))) 'display mark))
2550 (t ; attr list
2551 (overlay-put
2552 (make-overlay (1- (point)) (1+ (point))) 'face
2553 (calendar-make-temp-face mark))))))))
2555 (define-obsolete-function-alias 'mark-visible-calendar-date
2556 'calendar-mark-visible-date "23.1")
2558 (defun calendar-star-date ()
2559 "Replace the date under the cursor in the calendar window with asterisks.
2560 You might want to add this function to `calendar-today-visible-hook'."
2561 (unless (catch 'found
2562 (dolist (ol (overlays-at (point)))
2563 (and (overlay-get ol 'calendar-star)
2564 (throw 'found t))))
2565 (let ((ol (make-overlay (1- (point)) (point))))
2566 (overlay-put ol 'display "*")
2567 (overlay-put ol 'calendar-star t)
2568 ;; Use copy-sequence to avoid merging of identical 'display props.
2569 ;; Use two overlays so as not to mess up
2570 ;; calendar-cursor-to-nearest-date (and calendar-forward-day).
2571 (overlay-put (setq ol (make-overlay (point) (1+ (point))))
2572 'display (copy-sequence "*"))
2573 (overlay-put ol 'calendar-star t))))
2575 (defun calendar-mark-today ()
2576 "Mark the date under the cursor in the calendar window.
2577 The date is marked with `calendar-today-marker'. You might want to add
2578 this function to `calendar-today-visible-hook'."
2579 (calendar-mark-visible-date (calendar-cursor-to-date) calendar-today-marker))
2581 ;; FIXME why the car? Almost every usage calls list on the args.
2582 (defun calendar-date-compare (date1 date2)
2583 "Return t if DATE1 is before DATE2, nil otherwise.
2584 The actual dates are in the car of DATE1 and DATE2."
2585 (< (calendar-absolute-from-gregorian (car date1))
2586 (calendar-absolute-from-gregorian (car date2))))
2588 (defun calendar-date-string (date &optional abbreviate nodayname)
2589 "A string form of DATE, driven by the variable `calendar-date-display-form'.
2590 An optional parameter ABBREVIATE, when non-nil, causes the month
2591 and day names to be abbreviated as specified by
2592 `calendar-month-abbrev-array' and `calendar-day-abbrev-array',
2593 respectively. An optional parameter NODAYNAME, when t, omits the
2594 name of the day of the week."
2595 (let* ((dayname (unless nodayname (calendar-day-name date abbreviate)))
2596 (month (calendar-extract-month date))
2597 (monthname (calendar-month-name month abbreviate))
2598 (day (number-to-string (calendar-extract-day date)))
2599 (month (number-to-string month))
2600 (year (number-to-string (calendar-extract-year date))))
2601 (mapconcat 'eval calendar-date-display-form "")))
2603 (defun calendar-dayname-on-or-before (dayname date)
2604 "Return the absolute date of the DAYNAME on or before absolute DATE.
2605 DAYNAME=0 means Sunday, DAYNAME=1 means Monday, and so on.
2607 Note: Applying this function to d+6 gives us the DAYNAME on or after an
2608 absolute day d. Similarly, applying it to d+3 gives the DAYNAME nearest to
2609 absolute date d, applying it to d-1 gives the DAYNAME previous to absolute
2610 date d, and applying it to d+7 gives the DAYNAME following absolute date d."
2611 (- date (% (- date dayname) 7)))
2613 (defun calendar-nth-named-absday (n dayname month year &optional day)
2614 "Absolute date of the Nth DAYNAME after/before MONTH YEAR DAY.
2615 A DAYNAME of 0 means Sunday, 1 means Monday, and so on.
2616 If N>0, return the Nth DAYNAME after MONTH DAY, YEAR (inclusive).
2617 If N<0, return the Nth DAYNAME before MONTH DAY, YEAR (inclusive).
2618 DAY defaults to 1 if N>0, and MONTH's last day otherwise."
2619 (if (> n 0)
2620 (+ (* 7 (1- n))
2621 (calendar-dayname-on-or-before
2622 dayname
2623 (+ 6 (calendar-absolute-from-gregorian
2624 (list month (or day 1) year)))))
2625 (+ (* 7 (1+ n))
2626 (calendar-dayname-on-or-before
2627 dayname
2628 (calendar-absolute-from-gregorian
2629 (list month
2630 (or day (calendar-last-day-of-month month year))
2631 year))))))
2633 (defun calendar-nth-named-day (n dayname month year &optional day)
2634 "Date of the Nth DAYNAME after/before MONTH YEAR DAY.
2635 Like `calendar-nth-named-absday', but returns a Gregorian date."
2636 (calendar-gregorian-from-absolute
2637 (calendar-nth-named-absday n dayname month year day)))
2639 (defun calendar-day-of-year-string (&optional date)
2640 "String of day number of year of Gregorian DATE.
2641 Defaults to today's date if DATE is not given."
2642 (let* ((d (or date (calendar-current-date)))
2643 (year (calendar-extract-year d))
2644 (day (calendar-day-number d))
2645 (days-remaining (- (calendar-day-number (list 12 31 year)) day)))
2646 (format "Day %d of %d; %d day%s remaining in the year"
2647 day year days-remaining (if (= days-remaining 1) "" "s"))))
2649 (defun calendar-other-dates (date)
2650 "Return a list of strings giving Gregorian DATE in other calendars.
2651 DATE is (month day year). Calendars that do not apply are omitted."
2652 (let (odate)
2653 (delq nil
2654 (list
2655 (calendar-day-of-year-string date)
2656 (format "ISO date: %s" (calendar-iso-date-string date))
2657 (format "Julian date: %s"
2658 (calendar-julian-date-string date))
2659 (format "Astronomical (Julian) day number (at noon UTC): %s.0"
2660 (calendar-astro-date-string date))
2661 (format "Fixed (RD) date: %s"
2662 (calendar-absolute-from-gregorian date))
2663 (format "Hebrew date (before sunset): %s"
2664 (calendar-hebrew-date-string date))
2665 (format "Persian date: %s"
2666 (calendar-persian-date-string date))
2667 (unless (string-equal
2668 (setq odate (calendar-islamic-date-string date))
2670 (format "Islamic date (before sunset): %s" odate))
2671 (unless (string-equal
2672 (setq odate (calendar-bahai-date-string date))
2674 (format "Bahá'í date: %s" odate))
2675 (format "Chinese date: %s"
2676 (calendar-chinese-date-string date))
2677 (unless (string-equal
2678 (setq odate (calendar-coptic-date-string date))
2680 (format "Coptic date: %s" odate))
2681 (unless (string-equal
2682 (setq odate (calendar-ethiopic-date-string date))
2684 (format "Ethiopic date: %s" odate))
2685 (unless (string-equal
2686 (setq odate (calendar-french-date-string date))
2688 (format "French Revolutionary date: %s" odate))
2689 (format "Mayan date: %s"
2690 (calendar-mayan-date-string date))))))
2692 (declare-function x-popup-menu "menu.c" (position menu))
2694 (defun calendar-print-other-dates (&optional event)
2695 "Show dates on other calendars for date under the cursor.
2696 If called by a mouse-event, pops up a menu with the result."
2697 (interactive (list last-nonmenu-event))
2698 (let* ((date (calendar-cursor-to-date t event))
2699 (title (format "%s (Gregorian)" (calendar-date-string date)))
2700 (others (calendar-other-dates date))
2701 selection)
2702 (if (mouse-event-p event)
2703 (and (setq selection (cal-menu-x-popup-menu event title
2704 (mapcar 'list others)))
2705 (call-interactively selection))
2706 (calendar-in-read-only-buffer calendar-other-calendars-buffer
2707 (calendar-set-mode-line title)
2708 (insert (mapconcat 'identity others "\n"))))))
2710 (defun calendar-print-day-of-year ()
2711 "Show day number in year/days remaining in year for date under the cursor."
2712 (interactive)
2713 (message "%s" (calendar-day-of-year-string (calendar-cursor-to-date t))))
2715 (defun calendar-set-mode-line (str)
2716 "Set mode line to STR, centered, surrounded by dashes."
2717 (let* ((edges (window-edges))
2718 ;; As per doc of window-width, total visible mode-line length.
2719 (width (- (nth 2 edges) (car edges))))
2720 ;; Hack for --daemon. See bug #2199.
2721 ;; If no frame exists yet, we have no idea what width to use.
2722 (and (= width 10)
2723 (not window-system)
2724 (setq width (string-to-number (or (getenv "COLUMNS") "80"))))
2725 (setq mode-line-format
2726 (if buffer-file-name
2727 `("-" mode-line-modified
2728 ,(calendar-string-spread (list str) ?- (- width 6))
2729 "---")
2730 (calendar-string-spread (list str) ?- width)))))
2732 (define-obsolete-function-alias 'calendar-version 'emacs-version "23.1")
2734 (run-hooks 'calendar-load-hook)
2736 (provide 'calendar)
2738 ;; Local variables:
2739 ;; byte-compile-dynamic: t
2740 ;; coding: utf-8
2741 ;; End:
2743 ;;; calendar.el ends here