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