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