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