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