Merge from trunk
[emacs.git] / lisp / calendar / calendar.el
blob2be030e4cfbb938a373b34c9d28981cf390a3c82
1 ;;; calendar.el --- calendar functions
3 ;; Copyright (C) 1988-1995, 1997, 2000-2011 Free Software Foundation, Inc.
5 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
6 ;; Maintainer: Glenn Morris <rgm@gnu.org>
7 ;; Keywords: calendar
8 ;; Human-Keywords: calendar, Gregorian calendar, diary, holidays
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; This collection of functions implements a calendar window. It
28 ;; generates a calendar for the current month, together with the
29 ;; previous and coming months, or for any other three-month period.
30 ;; The calendar can be scrolled forward and backward in the window to
31 ;; show months in the past or future; the cursor can move forward and
32 ;; backward by days, weeks, or months, making it possible, for
33 ;; instance, to jump to the date a specified number of days, weeks, or
34 ;; months from the date under the cursor. The user can display a list
35 ;; of holidays and other notable days for the period shown; the
36 ;; notable days can be marked on the calendar, if desired. The user
37 ;; can also specify that dates having corresponding diary entries (in
38 ;; a file that the user specifies) be marked; the diary entries for
39 ;; any date can be viewed in a separate window. The diary and the
40 ;; notable days can be viewed independently of the calendar. Dates
41 ;; can be translated from the (usual) Gregorian calendar to the day of
42 ;; the year/days remaining in year, to the ISO commercial calendar, to
43 ;; the Julian (old style) calendar, to the Hebrew calendar, to the
44 ;; Islamic calendar, to the Baha'i calendar, to the French
45 ;; Revolutionary calendar, to the Mayan calendar, to the Chinese
46 ;; calendar, to the Coptic calendar, to the Ethiopic calendar, and to
47 ;; the astronomical (Julian) day number. Times of sunrise/sunset can
48 ;; be displayed, as can the phases of the moon. Appointment
49 ;; notification for diary entries is available. Calendar printing via
50 ;; LaTeX is available.
52 ;; The following files are part of the calendar/diary code:
54 ;; appt.el Appointment notification
55 ;; cal-bahai.el Baha'i calendar
56 ;; cal-china.el Chinese calendar
57 ;; cal-coptic.el Coptic/Ethiopic calendars
58 ;; cal-dst.el Daylight saving time rules
59 ;; cal-french.el French revolutionary calendar
60 ;; cal-hebrew.el Hebrew calendar
61 ;; cal-html.el Calendars in HTML
62 ;; cal-islam.el Islamic calendar
63 ;; cal-iso.el ISO calendar
64 ;; cal-julian.el Julian/astronomical calendars
65 ;; cal-mayan.el Mayan calendars
66 ;; cal-menu.el Menu support
67 ;; cal-move.el Movement in the calendar
68 ;; cal-persia.el Persian calendar
69 ;; cal-tex.el Calendars in LaTeX
70 ;; cal-x.el Dedicated frame functions
71 ;; calendar.el This file
72 ;; diary-lib.el Diary functions
73 ;; holidays.el Holiday functions
74 ;; lunar.el Phases of the moon
75 ;; solar.el Sunrise/sunset, equinoxes/solstices
77 ;; Technical details of all the calendrical calculations can be found in
78 ;; ``Calendrical Calculations: The Millennium Edition'' by Edward M. Reingold
79 ;; and Nachum Dershowitz, Cambridge University Press (2001).
81 ;; An earlier version of the technical details appeared in
82 ;; ``Calendrical Calculations'' by Nachum Dershowitz and Edward M. Reingold,
83 ;; Software--Practice and Experience, Volume 20, Number 9 (September, 1990),
84 ;; pages 899-928, and in ``Calendrical Calculations, Part II: Three Historical
85 ;; Calendars'' by E. M. Reingold, N. Dershowitz, and S. M. Clamen,
86 ;; Software--Practice and Experience, Volume 23, Number 4 (April, 1993),
87 ;; pages 383-404.
89 ;; Hard copies of these two papers can be obtained by sending email to
90 ;; reingold@cs.uiuc.edu with the SUBJECT "send-paper-cal" (no quotes) and
91 ;; the message BODY containing your mailing address (snail).
94 ;; A note on free variables:
96 ;; The calendar passes around a few dynamically bound variables, which
97 ;; unfortunately have rather common names. They are meant to be
98 ;; available for external functions, so the names can't be changed.
100 ;; displayed-month, displayed-year: bound in calendar-generate, the
101 ;; central month of the 3 month calendar window
102 ;; original-date, number: bound in diary-list-entries, the arguments
103 ;; with which that function was called.
104 ;; date, entry: bound in diary-list-sexp-entries (qv)
106 ;; Bound in diary-list-entries:
107 ;; diary-entries-list: use in d-l, appt.el, and by diary-add-to-list
108 ;; diary-saved-point: only used in diary-lib.el, passed to the display func
109 ;; date-string: only used in diary-lib.el
110 ;; list-only: don't modify the diary-buffer, just return a list of entries
111 ;; file-glob-attrs: yuck
113 ;;; Code:
115 (load "cal-loaddefs" nil t)
117 ;; Avoid recursive load of calendar when loading cal-menu. Yuck.
118 (provide 'calendar)
119 (require 'cal-menu)
121 (defgroup calendar nil
122 "Calendar and time management support."
123 :prefix "calendar-"
124 :group 'applications)
126 (defgroup calendar-hooks nil
127 "Calendar hooks."
128 :prefix "calendar-"
129 :group 'calendar)
131 (defgroup calendar-faces nil
132 "Calendar faces."
133 :prefix "calendar-"
134 :group 'calendar)
136 (defcustom calendar-offset 0
137 "The offset of the principal month from the center of the calendar window.
138 0 means the principal month is in the center (default), -1 means on the left,
139 +1 means on the right. Larger (or smaller) values push the principal month off
140 the screen."
141 :type 'integer
142 :group 'calendar)
144 (defcustom calendar-setup nil
145 "The frame setup of the calendar.
146 The choices are: `one-frame' (calendar and diary together in one separate,
147 dedicated frame); `two-frames' (calendar and diary in separate, dedicated
148 frames); `calendar-only' (calendar in a separate, dedicated frame); with
149 any other value the current frame is used. Using any of the first
150 three options overrides the value of `calendar-view-diary-initially-flag'."
151 :type '(choice
152 (const :tag "calendar and diary in separate frame" one-frame)
153 (const :tag "calendar and diary each in own frame" two-frames)
154 (const :tag "calendar in separate frame" calendar-only)
155 (const :tag "use current frame" nil))
156 :group 'calendar)
158 (defcustom calendar-minimum-window-height 8
159 "Minimum height `calendar-generate-window' should use for calendar window."
160 :type 'integer
161 :version "22.1"
162 :group 'calendar)
164 ;; See discussion in bug#1806.
165 (defcustom calendar-split-width-threshold nil
166 "Value to use for `split-width-threshold' when creating a calendar.
167 This only affects frames wider than the default value of
168 `split-width-threshold'."
169 :type '(choice (const nil)
170 (integer))
171 :version "23.2"
172 :group 'calendar)
173 (make-obsolete-variable
174 'calendar-split-width-threshold
175 "customize `display-buffer-alist' instead." "24.1")
177 (defcustom calendar-week-start-day 0
178 "The day of the week on which a week in the calendar begins.
179 0 means Sunday (default), 1 means Monday, and so on.
181 If you change this variable directly (without using customize)
182 after starting `calendar', you should call `calendar-redraw' to
183 update the calendar display to reflect the change, otherwise
184 movement commands will not work correctly."
185 :type 'integer
186 ;; Change the initialize so that if you reload calendar.el, it will not
187 ;; cause a redraw (which may fail, e.g. with "invalid byte-code in
188 ;; calendar.elc" because of the "byte-compile-dynamic").
189 :initialize 'custom-initialize-default
190 :set (lambda (sym val)
191 (set sym val)
192 (calendar-redraw))
193 :group 'calendar)
195 (define-obsolete-variable-alias 'view-diary-entries-initially
196 'calendar-view-diary-initially-flag "23.1")
198 (defcustom calendar-view-diary-initially-flag nil
199 "Non-nil means display current date's diary entries on entry to calendar.
200 The diary is displayed in another window when the calendar is first displayed,
201 if the current date is visible. The number of days of diary entries displayed
202 is governed by the variable `diary-number-of-entries'. This variable can
203 be overridden by the value of `calendar-setup'."
204 :type 'boolean
205 :group 'diary)
207 (define-obsolete-variable-alias 'mark-diary-entries-in-calendar
208 'calendar-mark-diary-entries-flag "23.1")
210 ;; FIXME :set
211 (defcustom calendar-mark-diary-entries-flag nil
212 "Non-nil means mark dates with diary entries, in the calendar window.
213 The marking symbol is specified by the variable `diary-entry-marker'."
214 :type 'boolean
215 :group 'diary)
217 (defcustom calendar-remove-frame-by-deleting t
218 "Determine how the calendar mode removes a frame no longer needed.
219 If nil, make an icon of the frame. If non-nil, delete the frame."
220 :type 'boolean
221 :version "23.1" ; changed from nil to t
222 :group 'view
223 :group 'calendar)
225 (defface calendar-today
226 '((t (:underline t)))
227 "Face for indicating today's date in the calendar.
228 See the variable `calendar-today-marker'."
229 :group 'calendar-faces)
231 (define-obsolete-face-alias 'calendar-today-face 'calendar-today "22.1")
233 (defface diary
234 '((((min-colors 88) (class color) (background light))
235 :foreground "red1")
236 (((class color) (background light))
237 :foreground "red")
238 (((min-colors 88) (class color) (background dark))
239 :foreground "yellow1")
240 (((class color) (background dark))
241 :foreground "yellow")
243 :weight bold))
244 "Face for highlighting diary entries.
245 Used to mark diary entries in the calendar (see `diary-entry-marker'),
246 and to highlight the date header in the fancy diary."
247 :group 'calendar-faces)
249 (define-obsolete-face-alias 'diary-face 'diary "22.1")
251 (defface holiday
252 '((((class color) (background light))
253 :background "pink")
254 (((class color) (background dark))
255 :background "chocolate4")
257 :inverse-video t))
258 "Face for indicating in the calendar dates that have holidays.
259 See `calendar-holiday-marker'."
260 :group 'calendar-faces)
262 (define-obsolete-face-alias 'holiday-face 'holiday "22.1")
264 ;; These briefly checked font-lock-mode, but that is broken, since it
265 ;; is a buffer-local variable, and which buffer happens to be current
266 ;; when this file is loaded shouldn't make a difference. One could
267 ;; perhaps check global-font-lock-mode, or font-lock-global-modes; but
268 ;; this feature doesn't use font-lock, so there's no real reason it
269 ;; should respect those either. See bug#2199.
270 ;; They also used to check display-color-p, but that is a problem if
271 ;; loaded from --daemon. Since BW displays are rare now, this was
272 ;; also taken out. The way to keep it would be to have nil mean do a
273 ;; runtime check whenever this variable is used.
274 (defcustom diary-entry-marker 'diary
275 "How to mark dates that have diary entries.
276 The value can be either a single-character string (e.g. \"+\") or a face."
277 :type '(choice (string :tag "Single character string") face)
278 :group 'diary
279 :version "23.1")
281 (defcustom calendar-today-marker 'calendar-today
282 "How to mark today's date in the calendar.
283 The value can be either a single-character string (e.g. \"=\") or a face.
284 Used by `calendar-mark-today'."
285 :type '(choice (string :tag "Single character string") face)
286 :group 'calendar
287 :version "23.1")
289 (defcustom calendar-holiday-marker 'holiday
290 "How to mark notable dates in the calendar.
291 The value can be either a single-character string (e.g. \"*\") or a face."
292 :type '(choice (string :tag "Single character string") face)
293 :group 'holidays
294 :version "23.1")
296 (define-obsolete-variable-alias 'view-calendar-holidays-initially
297 'calendar-view-holidays-initially-flag "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 (define-obsolete-variable-alias 'mark-holidays-in-calendar
307 'calendar-mark-holidays-flag "23.1")
309 ;; FIXME :set
310 (defcustom calendar-mark-holidays-flag nil
311 "Non-nil means mark dates of holidays in the calendar window.
312 The marking symbol is specified by the variable `calendar-holiday-marker'."
313 :type 'boolean
314 :group 'holidays)
316 (defcustom calendar-mode-hook nil
317 "Hook run when entering `calendar-mode'."
318 :type 'hook
319 :group 'calendar-hooks)
321 (defcustom calendar-load-hook nil
322 "List of functions to be called after the calendar is first loaded.
323 This is the place to add key bindings to `calendar-mode-map'."
324 :type 'hook
325 :group 'calendar-hooks)
327 (define-obsolete-variable-alias 'initial-calendar-window-hook
328 'calendar-initial-window-hook "23.1")
330 (defcustom calendar-initial-window-hook nil
331 "List of functions to be called when the calendar window is created.
332 Quitting the calendar and re-entering it will cause these functions
333 to be called again."
334 :type 'hook
335 :group 'calendar-hooks)
337 (define-obsolete-variable-alias 'today-visible-calendar-hook
338 'calendar-today-visible-hook "23.1")
340 (defcustom calendar-today-visible-hook nil
341 "List of functions called whenever the current date is visible.
342 To mark today's date, add the function `calendar-mark-today'.
343 To replace the date with asterisks, add the function `calendar-star-date'.
344 See also `calendar-today-invisible-hook'.
346 In general, be careful about changing characters in the calendar buffer,
347 since it may cause the movement commands to fail."
348 :type 'hook
349 :options '(calendar-mark-today calendar-star-date)
350 :group 'calendar-hooks)
352 (define-obsolete-variable-alias 'today-invisible-calendar-hook
353 'calendar-today-invisible-hook "23.1")
355 (defcustom calendar-today-invisible-hook nil
356 "List of functions called whenever the current date is not visible.
357 See also `calendar-today-visible-hook'."
358 :type 'hook
359 :group 'calendar-hooks)
361 (defcustom calendar-move-hook nil
362 "List of functions called whenever the cursor moves in the calendar.
363 For example,
365 (add-hook 'calendar-move-hook (lambda () (diary-view-entries 1)))
367 redisplays the diary for whatever date the cursor is moved to."
368 :type 'hook
369 :options '(calendar-update-mode-line)
370 :group 'calendar-hooks)
372 (defcustom calendar-date-echo-text
373 "mouse-2: general menu\nmouse-3: menu for this date"
374 "String displayed when the cursor is over a date in the calendar.
375 Can be either a fixed string, or a lisp expression that returns one.
376 When this expression is evaluated, DAY, MONTH, and YEAR are
377 integers appropriate to the relevant date. For example, to
378 display the ISO date:
380 (setq calendar-date-echo-text '(format \"ISO date: %s\"
381 (calendar-iso-date-string
382 (list month day year))))
383 Changing this variable without using customize has no effect on
384 pre-existing calendar windows."
385 :group 'calendar
386 :initialize 'custom-initialize-default
387 :risky t
388 :set (lambda (sym val)
389 (set sym val)
390 (calendar-redraw))
391 :type '(choice (string :tag "Fixed string")
392 (sexp :value
393 (format "ISO date: %s"
394 (calendar-iso-date-string
395 (list month day year)))))
396 :version "23.1")
399 (defvar calendar-month-digit-width nil
400 "Width of the region with numbers in each month in the calendar.")
402 (defvar calendar-month-width nil
403 "Full width of each month in the calendar.")
405 (defvar calendar-right-margin nil
406 "Right margin of the calendar.")
408 (defvar calendar-month-edges nil
409 "Alist of month edge columns.
410 Each element has the form (N LEFT FIRST LAST RIGHT), where
411 LEFT is the leftmost column associated with month segment N,
412 FIRST and LAST are the first and last columns with day digits in,
413 and LAST is the rightmost column.")
415 (defun calendar-month-edges (segment)
416 "Compute the month edge columns for month SEGMENT.
417 Returns a list (LEFT FIRST LAST RIGHT), where LEFT is the
418 leftmost column associated with a month, FIRST and LAST are the
419 first and last columns with day digits in, and LAST is the
420 rightmost column."
421 ;; The leftmost column with a digit in it in this month segment.
422 (let* ((first (+ calendar-left-margin
423 (* segment calendar-month-width)))
424 ;; The rightmost column with a digit in it in this month segment.
425 (last (+ first (1- calendar-month-digit-width)))
426 (left (if (eq segment 0)
428 (+ calendar-left-margin
429 (* segment calendar-month-width)
430 (- (/ calendar-intermonth-spacing 2)))))
431 ;; The rightmost edge of this month segment, dividing the
432 ;; space between months in two.
433 (right (+ calendar-left-margin
434 (* (1+ segment) calendar-month-width)
435 (- (/ calendar-intermonth-spacing 2)))))
436 (list left first last right)))
438 (defun calendar-recompute-layout-variables ()
439 "Recompute some layout-related calendar \"constants\"."
440 (setq calendar-month-digit-width (+ (* 6 calendar-column-width)
441 calendar-day-digit-width)
442 calendar-month-width (+ (* 7 calendar-column-width)
443 calendar-intermonth-spacing)
444 calendar-right-margin (+ calendar-left-margin
445 (* 3 (* 7 calendar-column-width))
446 (* 2 calendar-intermonth-spacing))
447 calendar-month-edges nil)
448 (dotimes (i 3)
449 (push (cons i (calendar-month-edges i)) calendar-month-edges))
450 (setq calendar-month-edges (reverse calendar-month-edges)))
452 ;; FIXME add font-lock-keywords.
453 (defun calendar-set-layout-variable (symbol value &optional minmax)
454 "Set SYMBOL's value to VALUE, an integer.
455 A positive/negative MINMAX enforces a minimum/maximum value.
456 Then redraw the calendar, if necessary."
457 (let ((oldvalue (symbol-value symbol)))
458 (custom-set-default symbol (if minmax
459 (if (< minmax 0)
460 (min value (- minmax))
461 (max value minmax))
462 value))
463 (unless (equal value oldvalue)
464 (calendar-recompute-layout-variables)
465 (calendar-redraw))))
467 (defcustom calendar-left-margin 5
468 "Empty space to the left of the first month in the calendar."
469 :group 'calendar
470 :initialize 'custom-initialize-default
471 :set 'calendar-set-layout-variable
472 :type 'integer
473 :version "23.1")
475 ;; Or you can view it as columns of width 2, with 1 space, no space
476 ;; after the last column, and a 5 space gap between month.
477 ;; FIXME check things work if this is odd.
478 (defcustom calendar-intermonth-spacing 4
479 "Space between months in the calendar. Minimum value is 1."
480 :group 'calendar
481 :initialize 'custom-initialize-default
482 :set (lambda (sym val)
483 (calendar-set-layout-variable sym val 1))
484 :type 'integer
485 :version "23.1")
487 ;; FIXME calendar-month-column-width?
488 (defcustom calendar-column-width 3
489 "Width of each day column in the calendar. Minimum value is 3."
490 :initialize 'custom-initialize-default
491 :set (lambda (sym val)
492 (calendar-set-layout-variable sym val 3))
493 :type 'integer
494 :version "23.1")
496 (defcustom calendar-day-header-width 2
497 "Width of the day column headers in the calendar.
498 Must be at least one less than `calendar-column-width'."
499 :group 'calendar
500 :initialize 'custom-initialize-default
501 :set (lambda (sym 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 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 "~/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 .emacs 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 Baha'i
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 :type 'file
657 :group 'diary)
659 ;; FIXME do these have to be single characters?
660 (defcustom diary-nonmarking-symbol "&"
661 "Symbol indicating that a diary entry is not to be marked in the calendar."
662 :type 'string
663 :group 'diary)
665 (define-obsolete-variable-alias 'hebrew-diary-entry-symbol
666 'diary-hebrew-entry-symbol "23.1")
668 (defcustom diary-hebrew-entry-symbol "H"
669 "Symbol indicating a diary entry according to the Hebrew calendar."
670 :type 'string
671 :group 'diary)
673 (define-obsolete-variable-alias 'islamic-diary-entry-symbol
674 'diary-islamic-entry-symbol "23.1")
676 (defcustom diary-islamic-entry-symbol "I"
677 "Symbol indicating a diary entry according to the Islamic calendar."
678 :type 'string
679 :group 'diary)
681 (define-obsolete-variable-alias 'bahai-diary-entry-symbol
682 'diary-bahai-entry-symbol "23.1")
684 (defcustom diary-bahai-entry-symbol "B"
685 "Symbol indicating a diary entry according to the Baha'i calendar."
686 :type 'string
687 :group 'diary)
689 (defcustom european-calendar-style nil
690 "Non-nil means use the European style of dates in the diary and display.
691 In this case, a date like 1/2/1990 would be interpreted as
692 February 1, 1990. See `diary-european-date-forms' for the
693 default European diary date styles.
695 Setting this variable directly does not take effect (if the
696 calendar package is already loaded). Rather, use either
697 \\[customize] or the function `calendar-set-date-style'."
698 :type 'boolean
699 ;; Without :initialize (require 'calendar) throws an error because
700 ;; calendar-set-date-style is undefined at this point.
701 :initialize 'custom-initialize-default
702 :set (lambda (symbol value)
703 (if value
704 (calendar-set-date-style 'european)
705 (calendar-set-date-style 'american)))
706 :group 'calendar)
708 (make-obsolete-variable 'european-calendar-style 'calendar-date-style "23.1")
710 ;; If this is autoloaded, c-d-s gets set before any customization of e-c-s.
711 (defcustom calendar-date-style (if european-calendar-style 'european
712 'american)
713 "Your preferred style for writing dates.
714 The options are:
715 `american' - month/day/year
716 `european' - day/month/year
717 `iso' - year/month/day
718 This affects how dates written in your diary are interpreted.
719 It also affects date display, as well as those calendar and diary
720 functions that take a date as an argument, e.g. `diary-date', by
721 changing the order in which the arguments are interpreted.
723 Setting this variable directly does not take effect (if the
724 calendar package is already loaded). Rather, use either
725 \\[customize] or the function `calendar-set-date-style'."
726 :version "23.1"
727 :type '(choice (const american :tag "Month/Day/Year")
728 (const european :tag "Day/Month/Year")
729 (const iso :tag "Year/Month/Day"))
730 :initialize 'custom-initialize-default
731 :set (lambda (symbol value)
732 (calendar-set-date-style value))
733 :group 'calendar)
735 ;; Next three are provided to aid in setting diary-date-forms.
736 ;; FIXME move to diary-lib?
737 (defcustom diary-iso-date-forms
738 '((month "[-/]" day "[^-/0-9]")
739 (year "[-/]" month "[-/]" day "[^0-9]")
740 ;; Cannot allow [-/] as separators here, since it would also match
741 ;; the first element (bug#7377).
742 (monthname " *" day "[^-0-9]")
743 (year " *" monthname " *" day "[^0-9]")
744 (dayname "\\W"))
745 "List of pseudo-patterns describing the ISO style of dates.
746 The defaults are: MONTH[-/]DAY; YEAR[-/]MONTH[-/]DAY; MONTHNAME DAY;
747 YEAR MONTHNAME DAY; DAYNAME. Normally you should not customize this,
748 but `diary-date-forms' (which see)."
749 :version "23.3" ; bug#7377
750 :type '(repeat (choice (cons :tag "Backup"
751 :value (backup . nil)
752 (const backup)
753 (repeat (list :inline t :format "%v"
754 (symbol :tag "Keyword")
755 (choice symbol regexp))))
756 (repeat (list :inline t :format "%v"
757 (symbol :tag "Keyword")
758 (choice symbol regexp)))))
759 :group 'diary)
761 (define-obsolete-variable-alias 'american-date-diary-pattern
762 'diary-american-date-forms "23.1")
764 (defcustom diary-american-date-forms
765 '((month "/" day "[^/0-9]")
766 (month "/" day "/" year "[^0-9]")
767 (monthname " *" day "[^,0-9]")
768 (monthname " *" day ", *" year "[^0-9]")
769 (dayname "\\W"))
770 "List of pseudo-patterns describing the American style of dates.
771 The defaults are: MONTH/DAY; MONTH/DAY/YEAR; MONTHNAME DAY;
772 MONTHNAME DAY, YEAR; DAYNAME. Normally you should not customize this,
773 but `diary-date-forms' (which see)."
774 :type '(repeat (choice (cons :tag "Backup"
775 :value (backup . nil)
776 (const backup)
777 (repeat (list :inline t :format "%v"
778 (symbol :tag "Keyword")
779 (choice symbol regexp))))
780 (repeat (list :inline t :format "%v"
781 (symbol :tag "Keyword")
782 (choice symbol regexp)))))
783 :group 'diary)
785 (define-obsolete-variable-alias 'european-date-diary-pattern
786 'diary-european-date-forms "23.1")
788 (defcustom diary-european-date-forms
789 '((day "/" month "[^/0-9]")
790 (day "/" month "/" year "[^0-9]")
791 (backup day " *" monthname "\\W+\\<\\([^*0-9]\\|\\([0-9]+[:aApP]\\)\\)")
792 (day " *" monthname " *" year "[^0-9]")
793 (dayname "\\W"))
794 "List of pseudo-patterns describing the European style of dates.
795 The defaults are: DAY/MONTH; DAY/MONTH/YEAR; DAY MONTHNAME;
796 DAY MONTHNAME YEAR; DAYNAME. Normally you should not customize this, but
797 `diary-date-forms' (which see)."
798 :type '(repeat (choice (cons :tag "Backup"
799 :value (backup . nil)
800 (const backup)
801 (repeat (list :inline t :format "%v"
802 (symbol :tag "Keyword")
803 (choice symbol regexp))))
804 (repeat (list :inline t :format "%v"
805 (symbol :tag "Keyword")
806 (choice symbol regexp)))))
807 :group 'diary)
809 (defvar diary-font-lock-keywords)
811 (defcustom diary-date-forms (cond ((eq calendar-date-style 'iso)
812 diary-iso-date-forms)
813 ((eq calendar-date-style 'european)
814 diary-european-date-forms)
815 (t diary-american-date-forms))
816 "List of pseudo-patterns describing the forms of date used in the diary.
817 The patterns on the list must be MUTUALLY EXCLUSIVE and should not match
818 any portion of the diary entry itself, just the date component.
820 A pseudo-pattern is a list of regular expressions and the keywords `month',
821 `day', `year', `monthname', and `dayname'. The keyword `monthname' will
822 match the name of the month (see `calendar-month-name-array'), capitalized
823 or not, or its user-specified abbreviation (see `calendar-month-abbrev-array'),
824 followed by a period or not; it will also match `*'. Similarly, `dayname'
825 will match the name of the day (see `calendar-day-name-array'), capitalized or
826 not, or its user-specified abbreviation (see `calendar-day-abbrev-array'),
827 followed by a period or not. The keywords `month', `day', and `year' will
828 match those numerical values, preceded by arbitrarily many zeros; they will
829 also match `*'.
831 The matching of the diary entries with the date forms is done with the
832 standard syntax table from Fundamental mode, but with the `*' changed so
833 that it is a word constituent.
835 If, to be mutually exclusive, a pseudo-pattern must match a portion of the
836 diary entry itself, the first element of the pattern MUST be `backup'. This
837 directive causes the date recognizer to back up to the beginning of the
838 current word of the diary entry, so in no case can the pattern match more than
839 a portion of the first word of the diary entry.
841 For examples of three common styles, see `diary-american-date-forms',
842 `diary-european-date-forms', and `diary-iso-date-forms'."
843 :type '(repeat (choice (cons :tag "Backup"
844 :value (backup . nil)
845 (const backup)
846 (repeat (list :inline t :format "%v"
847 (symbol :tag "Keyword")
848 (choice symbol regexp))))
849 (repeat (list :inline t :format "%v"
850 (symbol :tag "Keyword")
851 (choice symbol regexp)))))
852 :set-after '(calendar-date-style diary-iso-date-forms
853 diary-european-date-forms
854 diary-american-date-forms)
855 :initialize 'custom-initialize-default
856 :set (lambda (symbol value)
857 (unless (equal value (eval symbol))
858 (custom-set-default symbol value)
859 (setq diary-font-lock-keywords (diary-font-lock-keywords))
860 ;; Need to redraw not just to get new font-locking, but also
861 ;; to pick up any newly recognized entries.
862 (and (diary-live-p)
863 (diary))))
864 :group 'diary)
866 ;; Next three are provided to aid in setting calendar-date-display-form.
867 (defcustom calendar-iso-date-display-form '((format "%s-%.2d-%.2d" year
868 (string-to-number month)
869 (string-to-number day)))
870 "Pseudo-pattern governing the way a date appears in the ISO style.
871 Normally you should not customize this, but `calendar-date-display-form'
872 \(which see)."
873 :type 'sexp
874 :version "23.1"
875 :group 'calendar)
877 (define-obsolete-variable-alias 'european-calendar-display-form
878 'calendar-european-date-display-form "23.1")
880 (defcustom calendar-european-date-display-form
881 '((if dayname (concat dayname ", ")) day " " monthname " " year)
882 "Pseudo-pattern governing the way a date appears in the European style.
883 Normally you should not customize this, but `calendar-date-display-form'
884 \(which see)."
885 :type 'sexp
886 :group 'calendar)
888 (define-obsolete-variable-alias 'american-calendar-display-form
889 'calendar-american-date-display-form "23.1")
891 (defcustom calendar-american-date-display-form
892 '((if dayname (concat dayname ", ")) monthname " " day ", " year)
893 "Pseudo-pattern governing the way a date appears in the American style.
894 Normally you should not customize this, but `calendar-date-display-form'
895 \(which see)."
896 :type 'sexp
897 :group 'calendar)
899 (defcustom calendar-date-display-form
900 (cond ((eq calendar-date-style 'iso)
901 calendar-iso-date-display-form)
902 ((eq calendar-date-style 'european)
903 calendar-european-date-display-form)
904 (t calendar-american-date-display-form))
905 "Pseudo-pattern governing the way a calendar date appears.
906 Used by the function `calendar-date-string' (which see), a pseudo-pattern
907 is a list of expressions that can involve the keywords `month', `day',
908 and `year' (all numbers in string form), and `monthname' and `dayname'
909 \(both alphabetic strings). For example, a typical American form would be
911 '(month \"/\" day \"/\" (substring year -2))
913 whereas
915 '((format \"%9s, %9s %2s, %4s\" dayname monthname day year))
917 would give the usual American style in fixed-length fields. The variables
918 `calendar-iso-date-display-form', `calendar-european-date-display-form', and
919 `calendar-american-date-display-form' provide some defaults for three common
920 styles."
921 :type 'sexp
922 :set-after '(calendar-date-style calendar-iso-date-display-form
923 calendar-european-date-display-form
924 calendar-american-date-display-form)
925 :group 'calendar)
927 (defun calendar-set-date-style (style)
928 "Set the style of calendar and diary dates to STYLE (a symbol).
929 The valid styles are described in the documentation of `calendar-date-style'."
930 (interactive (list (intern
931 (completing-read "Date style: "
932 '("american" "european" "iso") nil t
933 nil nil "american"))))
934 (or (memq style '(american european iso))
935 (setq style 'american))
936 (setq calendar-date-style style
937 calendar-date-display-form
938 (symbol-value (intern-soft
939 (format "calendar-%s-date-display-form" style)))
940 diary-date-forms
941 (symbol-value (intern-soft (format "diary-%s-date-forms" style))))
942 (calendar-update-mode-line))
944 (defun european-calendar ()
945 "Set the interpretation and display of dates to the European style."
946 (interactive)
947 (calendar-set-date-style 'european))
949 (make-obsolete 'european-calendar 'calendar-set-date-style "23.1")
951 (defun american-calendar ()
952 "Set the interpretation and display of dates to the American style."
953 (interactive)
954 (calendar-set-date-style 'american))
956 (make-obsolete 'american-calendar 'calendar-set-date-style "23.1")
958 (define-obsolete-variable-alias 'holidays-in-diary-buffer
959 'diary-show-holidays-flag "23.1")
961 (defcustom diary-show-holidays-flag t
962 "Non-nil means include holidays in the diary display.
963 The holidays appear in the mode line of the diary buffer, or in the
964 fancy diary buffer next to the date. This slows down the diary functions
965 somewhat; setting it to nil makes the diary display faster."
966 :type 'boolean
967 :group 'holidays)
969 (defcustom calendar-debug-sexp nil
970 "Turn debugging on when evaluating a sexp in the diary or holiday list."
971 :type 'boolean
972 :group 'calendar)
974 (define-obsolete-variable-alias 'all-hebrew-calendar-holidays
975 'calendar-hebrew-all-holidays-flag "23.1")
977 (defcustom calendar-hebrew-all-holidays-flag nil
978 "If nil, show only major holidays from the Hebrew calendar.
979 This means only those Jewish holidays that appear on secular calendars.
980 Otherwise, show all the holidays that would appear in a complete Hebrew
981 calendar."
982 :type 'boolean
983 :group 'holidays)
985 (define-obsolete-variable-alias 'all-christian-calendar-holidays
986 'calendar-christian-all-holidays-flag "23.1")
988 (defcustom calendar-christian-all-holidays-flag nil
989 "If nil, show only major holidays from the Christian calendar.
990 This means only those Christian holidays that appear on secular calendars.
991 Otherwise, show all the holidays that would appear in a complete Christian
992 calendar."
993 :type 'boolean
994 :group 'holidays)
996 (define-obsolete-variable-alias 'all-islamic-calendar-holidays
997 'calendar-islamic-all-holidays-flag "23.1")
999 (defcustom calendar-islamic-all-holidays-flag nil
1000 "If nil, show only major holidays from the Islamic calendar.
1001 This means only those Islamic holidays that appear on secular calendars.
1002 Otherwise, show all the holidays that would appear in a complete Islamic
1003 calendar."
1004 :type 'boolean
1005 :group 'holidays)
1007 (define-obsolete-variable-alias 'all-bahai-calendar-holidays
1008 'calendar-bahai-all-holidays-flag "23.1")
1010 (defcustom calendar-bahai-all-holidays-flag nil
1011 "If nil, show only major holidays from the Baha'i calendar.
1012 These are the days on which work and school must be suspended.
1013 Otherwise, show all the holidays that would appear in a complete Baha'i
1014 calendar."
1015 :type 'boolean
1016 :group 'holidays)
1018 (defcustom calendar-chinese-all-holidays-flag nil
1019 "If nil, show only the major holidays from the Chinese calendar."
1020 :version "23.1"
1021 :type 'boolean
1022 :group 'holidays)
1024 ;;; End of user options.
1026 (calendar-recompute-layout-variables)
1028 (defconst calendar-first-date-row 3
1029 "First row in the calendar with actual dates.")
1031 (defconst calendar-buffer "*Calendar*"
1032 "Name of the buffer used for the calendar.")
1034 (defconst holiday-buffer "*Holidays*"
1035 "Name of the buffer used for the displaying the holidays.")
1037 (defconst diary-fancy-buffer "*Fancy Diary Entries*"
1038 "Name of the buffer used for the optional fancy display of the diary.")
1040 (define-obsolete-variable-alias 'fancy-diary-buffer 'diary-fancy-buffer "23.1")
1042 (defconst calendar-other-calendars-buffer "*Other Calendars*"
1043 "Name of the buffer used for the display of date on other calendars.")
1045 (defconst lunar-phases-buffer "*Phases of Moon*"
1046 "Name of the buffer used for the lunar phases.")
1048 (defconst solar-sunrises-buffer "*Sunrise/Sunset Times*"
1049 "Name of buffer used for sunrise/sunset times.")
1051 (defconst calendar-hebrew-yahrzeit-buffer "*Yahrzeits*"
1052 "Name of the buffer used by `list-yahrzeit-dates'.")
1054 (defmacro calendar-increment-month (mon yr n &optional nmonths)
1055 "Increment the variables MON and YR by N months.
1056 Forward if N is positive or backward if N is negative.
1057 A negative YR is interpreted as BC; -1 being 1 BC, and so on.
1058 Optional NMONTHS is the number of months per year (default 12)."
1059 ;; Can view this as a form of base-nmonths arithmetic, in which "a
1060 ;; year" = "ten", and we never bother to use hundreds.
1061 `(let ((nmonths (or ,nmonths 12))
1062 macro-y)
1063 (if (< ,yr 0) (setq ,yr (1+ ,yr))) ; -1 BC -> 0 AD, etc
1064 (setq macro-y (+ (* ,yr nmonths) ,mon -1 ,n)
1065 ,mon (1+ (mod macro-y nmonths))
1066 ,yr (/ macro-y nmonths))
1067 ;; Alternative:
1068 ;;; (setq macro-y (+ (* ,yr nmonths) ,mon -1 ,n)
1069 ;;; ,yr (/ macro-y nmonths)
1070 ;;; ,mon (- macro-y (* ,yr nmonths)))
1071 (and (< macro-y 0) (> ,mon 1) (setq ,yr (1- ,yr)))
1072 (if (< ,yr 1) (setq ,yr (1- ,yr))))) ; 0 AD -> -1 BC, etc
1074 (define-obsolete-function-alias 'increment-calendar-month
1075 'calendar-increment-month "23.1")
1077 (defvar displayed-month)
1078 (defvar displayed-year)
1080 (defun calendar-increment-month-cons (n &optional mon yr)
1081 "Return the Nth month after MON/YR.
1082 The return value is a pair (MONTH . YEAR).
1083 MON defaults to `displayed-month'. YR defaults to `displayed-year'."
1084 (unless mon (setq mon displayed-month))
1085 (unless yr (setq yr displayed-year))
1086 (calendar-increment-month mon yr n)
1087 (cons mon yr))
1089 (defmacro calendar-for-loop (var from init to final do &rest body)
1090 "Execute a for loop.
1091 Evaluate BODY with VAR bound to successive integers from INIT to FINAL,
1092 inclusive. The standard macro `dotimes' is preferable in most cases."
1093 (declare (debug (symbolp "from" form "to" form "do" body))
1094 (indent defun))
1095 `(let ((,var (1- ,init)))
1096 (while (>= ,final (setq ,var (1+ ,var)))
1097 ,@body)))
1099 (make-obsolete 'calendar-for-loop "use `dotimes' or `while' instead." "23.1")
1101 (defmacro calendar-sum (index initial condition expression)
1102 "For INDEX = INITIAL, +1, ... (as long as CONDITION holds), sum EXPRESSION."
1103 (declare (debug (symbolp form form form)))
1104 `(let ((,index ,initial)
1105 (sum 0))
1106 (while ,condition
1107 (setq sum (+ sum ,expression)
1108 ,index (1+ ,index)))
1109 sum))
1111 ;; FIXME bind q to bury-buffer?
1112 (defmacro calendar-in-read-only-buffer (buffer &rest body)
1113 "Switch to BUFFER and executes the forms in BODY.
1114 First creates or erases BUFFER as needed. Leaves BUFFER read-only,
1115 with disabled undo. Leaves point at point-min, displays BUFFER."
1116 (declare (indent 1) (debug t))
1117 `(progn
1118 (set-buffer (get-buffer-create ,buffer))
1119 (setq buffer-read-only nil
1120 buffer-undo-list t)
1121 (erase-buffer)
1122 ,@body
1123 (goto-char (point-min))
1124 (set-buffer-modified-p nil)
1125 (setq buffer-read-only t)
1126 (display-buffer ,buffer)))
1128 ;; The following are in-line for speed; they can be called thousands of times
1129 ;; when looking up holidays or processing the diary. Here, for example, are
1130 ;; the numbers of calls to calendar/diary/holiday functions in preparing the
1131 ;; fancy diary display, for a moderately complex diary file, with functions
1132 ;; used instead of macros. There were a total of 10000 such calls:
1134 ;; 1934 calendar-extract-month
1135 ;; 1852 calendar-extract-year
1136 ;; 1819 calendar-extract-day
1137 ;; 845 calendar-leap-year-p
1138 ;; 837 calendar-day-number
1139 ;; 775 calendar-absolute-from-gregorian
1140 ;; 346 calendar-last-day-of-month
1141 ;; 286 calendar-hebrew-last-day-of-month
1142 ;; 188 calendar-hebrew-leap-year-p
1143 ;; 180 calendar-hebrew-elapsed-days
1144 ;; 163 calendar-hebrew-last-month-of-year
1145 ;; 66 calendar-date-compare
1146 ;; 65 calendar-hebrew-days-in-year
1147 ;; 60 calendar-julian-to-absolute
1148 ;; 50 calendar-hebrew-to-absolute
1149 ;; 43 calendar-date-equal
1150 ;; 38 calendar-gregorian-from-absolute
1151 ;; .
1153 ;; The use of these seven macros eliminates the overhead of 92% of the function
1154 ;; calls; it's faster this way.
1156 (defsubst calendar-extract-month (date)
1157 "Extract the month part of DATE which has the form (month day year)."
1158 (car date))
1160 (define-obsolete-function-alias 'extract-calendar-month
1161 'calendar-extract-month "23.1")
1163 ;; Note gives wrong answer for result of (calendar-read-date 'noday),
1164 ;; but that is only used by `calendar-other-month'.
1165 (defsubst calendar-extract-day (date)
1166 "Extract the day part of DATE which has the form (month day year)."
1167 (cadr date))
1169 (define-obsolete-function-alias 'extract-calendar-day
1170 'calendar-extract-day "23.1")
1172 (defsubst calendar-extract-year (date)
1173 "Extract the year part of DATE which has the form (month day year)."
1174 (nth 2 date))
1176 (define-obsolete-function-alias 'extract-calendar-year
1177 'calendar-extract-year "23.1")
1179 (defsubst calendar-leap-year-p (year)
1180 "Return t if YEAR is a Gregorian leap year.
1181 A negative year is interpreted as BC; -1 being 1 BC, and so on."
1182 ;; 1 BC = 0 AD, 2 BC acts like 1 AD, etc.
1183 (if (< year 0) (setq year (1- (abs year))))
1184 (and (zerop (% year 4))
1185 (or (not (zerop (% year 100)))
1186 (zerop (% year 400)))))
1188 ;; The foregoing is a bit faster, but not as clear as the following:
1190 ;;(defsubst calendar-leap-year-p (year)
1191 ;; "Return t if YEAR is a Gregorian leap year."
1192 ;; (or
1193 ;; (and (zerop (% year 4))
1194 ;; (not (zerop (% year 100))))
1195 ;; (zerop (% year 400)))
1197 (defsubst calendar-last-day-of-month (month year)
1198 "The last day in MONTH during YEAR."
1199 (if (and (= month 2) (calendar-leap-year-p year))
1201 (aref [31 28 31 30 31 30 31 31 30 31 30 31] (1- month))))
1203 ;; An explanation of the calculation can be found in PascAlgorithms by
1204 ;; Edward and Ruth Reingold, Scott-Foresman/Little, Brown, 1988.
1206 (defsubst calendar-day-number (date)
1207 "Return the day number within the year of the date DATE.
1208 For example, (calendar-day-number '(1 1 1987)) returns the value 1,
1209 while (calendar-day-number '(12 31 1980)) returns 366."
1210 (let* ((month (calendar-extract-month date))
1211 (day (calendar-extract-day date))
1212 (year (calendar-extract-year date))
1213 (day-of-year (+ day (* 31 (1- month)))))
1214 (when (> month 2)
1215 (setq day-of-year (- day-of-year (/ (+ 23 (* 4 month)) 10)))
1216 (if (calendar-leap-year-p year)
1217 (setq day-of-year (1+ day-of-year))))
1218 day-of-year))
1220 (defsubst calendar-absolute-from-gregorian (date)
1221 "The number of days elapsed between the Gregorian date 12/31/1 BC and DATE.
1222 The Gregorian date Sunday, December 31, 1 BC is imaginary.
1223 DATE is a list of the form (month day year). A negative year is
1224 interpreted as BC; -1 being 1 BC, and so on. Dates before 12/31/1 BC
1225 return negative results."
1226 (let ((year (calendar-extract-year date))
1227 offset-years)
1228 (cond ((zerop year)
1229 (error "There was no year zero"))
1230 ((> year 0)
1231 (setq offset-years (1- year))
1232 (+ (calendar-day-number date) ; days this year
1233 (* 365 offset-years) ; + days in prior years
1234 (/ offset-years 4) ; + Julian leap years
1235 (- (/ offset-years 100)) ; - century years
1236 (/ offset-years 400))) ; + Gregorian leap years
1238 ;; Years between date and 1 BC, excluding 1 BC (1 for 2 BC, etc).
1239 (setq offset-years (abs (1+ year)))
1240 (- (calendar-day-number date)
1241 (* 365 offset-years)
1242 (/ offset-years 4)
1243 (- (/ offset-years 100))
1244 (/ offset-years 400)
1245 (calendar-day-number '(12 31 -1))))))) ; days in year 1 BC
1247 ;;;###autoload
1248 (defun calendar (&optional arg)
1249 "Display a three-month Gregorian calendar.
1250 The three months appear side by side, with the current month in
1251 the middle surrounded by the previous and next months. The
1252 cursor is put on today's date. If optional prefix argument ARG
1253 is non-nil, prompts for the central month and year.
1255 Once in the calendar window, future or past months can be moved
1256 into view. Arbitrary months can be displayed, or the calendar
1257 can be scrolled forward or backward. The cursor can be moved
1258 forward or backward by one day, one week, one month, or one year.
1259 All of these commands take prefix arguments which, when negative,
1260 cause movement in the opposite direction. For convenience, the
1261 digit keys and the minus sign are automatically prefixes. Use
1262 \\[describe-mode] for details of the key bindings in the calendar
1263 window.
1265 Displays the calendar in a separate window, or optionally in a
1266 separate frame, depending on the value of `calendar-setup'.
1268 If `calendar-view-diary-initially-flag' is non-nil, also displays the
1269 diary entries for the current date (or however many days
1270 `diary-number-of-entries' specifies). This variable can be
1271 overridden by `calendar-setup'. As well as being displayed,
1272 diary entries can also be marked on the calendar (see
1273 `calendar-mark-diary-entries-flag').
1275 Runs the following hooks:
1277 `calendar-load-hook' - after loading calendar.el
1278 `calendar-today-visible-hook', `calendar-today-invisible-hook' - after
1279 generating a calendar, if today's date is visible or not, respectively
1280 `calendar-initial-window-hook' - after first creating a calendar
1282 This function is suitable for execution in a .emacs file."
1283 (interactive "P")
1284 ;; Avoid loading cal-x unless it will be used.
1285 (if (and (memq calendar-setup '(one-frame two-frames calendar-only))
1286 (display-multi-frame-p))
1287 (calendar-frame-setup calendar-setup arg)
1288 (calendar-basic-setup arg)))
1290 (defun calendar-basic-setup (&optional arg nodisplay)
1291 "Create a three-month calendar.
1292 If optional prefix argument ARG is non-nil, prompts for the month
1293 and year, else uses the current date. If NODISPLAY is non-nil, don't
1294 display the generated calendar."
1295 (interactive "P")
1296 (let ((buff (current-buffer)))
1297 (set-buffer (get-buffer-create calendar-buffer))
1298 (calendar-mode)
1299 (let* ((date (if arg (calendar-read-date t)
1300 (calendar-current-date)))
1301 (month (calendar-extract-month date))
1302 (year (calendar-extract-year date)))
1303 (calendar-increment-month month year (- calendar-offset))
1304 ;; Display the buffer before calling calendar-generate-window so that it
1305 ;; can get a chance to adjust the window sizes to the frame size.
1306 (unless nodisplay
1307 ;; We want a window configuration that looks something like
1308 ;; X X | Y
1309 ;; - -----
1310 ;; C Z | C
1311 ;; where C is the calendar, and the LHS is the traditional,
1312 ;; non-wide frame, and the RHS is the wide frame case.
1313 ;; We should end up in the same state regardless of whether the
1314 ;; windows were initially split or not.
1315 ;; Previously, we only thought about the non-wide case.
1316 ;; We could just set split-height-threshold to 1000, relying on
1317 ;; the fact that the window splitting treated a single window as
1318 ;; a special case and would always split it (vertically). The
1319 ;; same thing does not work in the wide-frame case, so now we do
1320 ;; the splitting by hand.
1321 ;; See discussion in bug#1806.
1322 ;; Actually, this still does not do quite the right thing in the
1323 ;; wide frame case if started from a configuration like the LHS.
1324 ;; Eg if you start with a non-wide frame, call calendar, then
1325 ;; make the frame wider. This one is problematic because you
1326 ;; might need to split a totally unrelated window. Oh well, it
1327 ;; seems unlikely, and perhaps respecting the original layout is
1328 ;; the right thing in that case.
1330 ;; Is this a wide frame? If so, split it horizontally.
1332 ;; Try to make the new window at the bottom of the frame. If
1333 ;; there's real urgent need, we could also try to split the last
1334 ;; full width window on the frame (provided we write a function
1335 ;; to do that) and split it into two side-by-side windows with
1336 ;; the calendar in the right window. Hopefully, users will
1337 ;; eventually start using the rightmost bottom side window for
1338 ;; the calendar. Using (pop-up-window (bottom . 1000)), for
1339 ;; example, in `display-buffer-alist', with (override . t),
1340 ;; should do the job.
1341 (pop-to-buffer calendar-buffer '((pop-up-window (root . below)))))
1342 (calendar-generate-window month year)
1343 (if (and calendar-view-diary-initially-flag
1344 (calendar-date-is-visible-p date))
1345 (diary-view-entries))))
1346 (if calendar-view-holidays-initially-flag
1347 (let* ((diary-buffer (get-file-buffer diary-file))
1348 (diary-window (if diary-buffer (get-buffer-window diary-buffer))))
1349 ;; This used to bind `split-height-threshold'. Since that
1350 ;; variable is no longer supported we have to find a different
1351 ;; solution if needed.
1352 (calendar-list-holidays)))
1353 (run-hooks 'calendar-initial-window-hook))
1355 (defun calendar-generate-window (&optional mon yr)
1356 "Generate the calendar window for the current date.
1357 Optional integers MON and YR are used instead of today's date."
1358 (let* ((inhibit-read-only t)
1359 (today (calendar-current-date))
1360 (month (calendar-extract-month today))
1361 (day (calendar-extract-day today))
1362 (year (calendar-extract-year today))
1363 (today-visible (or (not mon)
1364 (<= (abs (calendar-interval mon yr month year)) 1)))
1365 (in-calendar-window (eq (window-buffer (selected-window))
1366 (get-buffer calendar-buffer))))
1367 (calendar-generate (or mon month) (or yr year))
1368 (calendar-update-mode-line)
1369 (calendar-cursor-to-visible-date
1370 (if today-visible today (list displayed-month 1 displayed-year)))
1371 (set-buffer-modified-p nil)
1372 ;; Don't do any window-related stuff if we weren't called from a
1373 ;; window displaying the calendar.
1374 (when in-calendar-window
1375 (if (window-iso-combined-p)
1376 ;; Adjust the window to exactly fit the displayed calendar.
1377 (fit-window-to-buffer nil nil calendar-minimum-window-height)
1378 ;; For a full height window or a window that is horizontally
1379 ;; combined don't fit height to that of its buffer.
1380 (set-window-vscroll nil 0))
1381 (sit-for 0))
1382 (and (bound-and-true-p font-lock-mode)
1383 (font-lock-fontify-buffer))
1384 (and calendar-mark-holidays-flag
1385 ;;; (calendar-date-is-valid-p today) ; useful for BC dates
1386 (calendar-mark-holidays)
1387 (and in-calendar-window (sit-for 0)))
1388 (unwind-protect
1389 (if calendar-mark-diary-entries-flag (diary-mark-entries))
1390 (if today-visible
1391 (run-hooks 'calendar-today-visible-hook)
1392 (run-hooks 'calendar-today-invisible-hook)))))
1394 (defun calendar-generate (month year)
1395 "Generate a three-month Gregorian calendar centered around MONTH, YEAR."
1396 ;; A negative YEAR is interpreted as BC; -1 being 1 BC, and so on.
1397 ;; Note that while calendars for years BC could be displayed as it
1398 ;; stands, almost all other calendar functions (eg holidays) would
1399 ;; at best have unpredictable results for such dates.
1400 (if (< (+ month (* 12 (1- year))) 2)
1401 (error "Months before January, 1 AD cannot be displayed"))
1402 (setq displayed-month month
1403 displayed-year year)
1404 (erase-buffer)
1405 (calendar-increment-month month year -1)
1406 (dotimes (i 3)
1407 (calendar-generate-month month year
1408 (+ calendar-left-margin
1409 (* calendar-month-width i)))
1410 (calendar-increment-month month year 1)))
1412 (defun calendar-move-to-column (indent)
1413 "Like `move-to-column', but indents if the line is too short."
1414 (if (< (move-to-column indent) indent)
1415 (indent-to indent)))
1417 (defun calendar-ensure-newline ()
1418 "Move to the next line, adding a newline if necessary."
1419 (or (zerop (forward-line 1))
1420 (insert "\n")))
1422 (defun calendar-insert-at-column (indent string truncate)
1423 "Move to column INDENT, adding spaces as needed.
1424 Inserts STRING so that it ends at INDENT. STRING is either a
1425 literal string, or a sexp to evaluate to return such. Truncates
1426 STRING to length TRUNCATE, ensure a trailing space."
1427 (if (not (ignore-errors (stringp (setq string (eval string)))))
1428 (calendar-move-to-column indent)
1429 (if (> (length string) truncate)
1430 (setq string (substring string 0 truncate)))
1431 (or (string-match " $" string)
1432 (if (= (length string) truncate)
1433 (aset string (1- truncate) ?\s)
1434 (setq string (concat string " "))))
1435 (calendar-move-to-column (- indent (length string)))
1436 (insert string)))
1438 (defun calendar-generate-month (month year indent)
1439 "Produce a calendar for MONTH, YEAR on the Gregorian calendar.
1440 The calendar is inserted at the top of the buffer in which point is currently
1441 located, but indented INDENT spaces. The indentation is done from the first
1442 character on the line and does not disturb the first INDENT characters on the
1443 line."
1444 (let ((blank-days ; at start of month
1445 (mod
1446 (- (calendar-day-of-week (list month 1 year))
1447 calendar-week-start-day)
1449 (last (calendar-last-day-of-month month year))
1450 (trunc (min calendar-intermonth-spacing
1451 (1- calendar-left-margin)))
1452 (day 1)
1453 string)
1454 (goto-char (point-min))
1455 (calendar-move-to-column indent)
1456 (insert
1457 (calendar-string-spread
1458 (list (format "%s %d" (calendar-month-name month) year))
1459 ?\s calendar-month-digit-width))
1460 (calendar-ensure-newline)
1461 (calendar-insert-at-column indent calendar-intermonth-header trunc)
1462 ;; Use the first two characters of each day to head the columns.
1463 (dotimes (i 7)
1464 (insert
1465 (progn
1466 (setq string
1467 (calendar-day-name (mod (+ calendar-week-start-day i) 7) nil t))
1468 (if enable-multibyte-characters
1469 (truncate-string-to-width string calendar-day-header-width)
1470 (substring string 0 calendar-day-header-width)))
1471 (make-string (- calendar-column-width calendar-day-header-width) ?\s)))
1472 (calendar-ensure-newline)
1473 (calendar-insert-at-column indent calendar-intermonth-text trunc)
1474 ;; Add blank days before the first of the month.
1475 (insert (make-string (* blank-days calendar-column-width) ?\s))
1476 ;; Put in the days of the month.
1477 (dotimes (i last)
1478 (setq day (1+ i))
1479 ;; TODO should numbers be left-justified, centered...?
1480 (insert (format (format "%%%dd%%s" calendar-day-digit-width) day
1481 (make-string
1482 (- calendar-column-width calendar-day-digit-width) ?\s)))
1483 ;; 'date property prevents intermonth text confusing re-searches.
1484 ;; (Tried intangible, it did not really work.)
1485 (set-text-properties
1486 (- (point) (1+ calendar-day-digit-width)) (1- (point))
1487 `(mouse-face highlight help-echo ,(eval calendar-date-echo-text)
1488 date t))
1489 (when (and (zerop (mod (+ day blank-days) 7))
1490 (/= day last))
1491 (calendar-ensure-newline)
1492 (setq day (1+ day)) ; first day of next week
1493 (calendar-insert-at-column indent calendar-intermonth-text trunc)))))
1495 (defun calendar-redraw ()
1496 "Redraw the calendar display, if `calendar-buffer' is live."
1497 (interactive)
1498 (if (get-buffer calendar-buffer)
1499 (with-current-buffer calendar-buffer
1500 (let ((cursor-date (calendar-cursor-to-nearest-date)))
1501 (calendar-generate-window displayed-month displayed-year)
1502 (calendar-cursor-to-visible-date cursor-date)))))
1504 (defvar calendar-mode-map
1505 (let ((map (make-keymap)))
1506 (suppress-keymap map)
1507 (dolist (c '(narrow-to-region mark-word mark-sexp mark-paragraph
1508 mark-defun mark-whole-buffer mark-page
1509 downcase-region upcase-region kill-region
1510 copy-region-as-kill capitalize-region write-region))
1511 (define-key map (vector 'remap c) 'calendar-not-implemented))
1512 (define-key map "<" 'calendar-scroll-right)
1513 (define-key map "\C-x<" 'calendar-scroll-right)
1514 (define-key map [prior] 'calendar-scroll-right-three-months)
1515 (define-key map "\ev" 'calendar-scroll-right-three-months)
1516 (define-key map ">" 'calendar-scroll-left)
1517 (define-key map "\C-x>" 'calendar-scroll-left)
1518 (define-key map [next] 'calendar-scroll-left-three-months)
1519 (define-key map "\C-v" 'calendar-scroll-left-three-months)
1520 (define-key map "\C-b" 'calendar-backward-day)
1521 (define-key map "\C-p" 'calendar-backward-week)
1522 (define-key map "\e{" 'calendar-backward-month)
1523 (define-key map "\C-x[" 'calendar-backward-year)
1524 (define-key map "\C-f" 'calendar-forward-day)
1525 (define-key map "\C-n" 'calendar-forward-week)
1526 (define-key map [left] 'calendar-backward-day)
1527 (define-key map [up] 'calendar-backward-week)
1528 (define-key map [right] 'calendar-forward-day)
1529 (define-key map [down] 'calendar-forward-week)
1530 (define-key map "\e}" 'calendar-forward-month)
1531 (define-key map "\C-x]" 'calendar-forward-year)
1532 (define-key map "\C-a" 'calendar-beginning-of-week)
1533 (define-key map "\C-e" 'calendar-end-of-week)
1534 (define-key map "\ea" 'calendar-beginning-of-month)
1535 (define-key map "\ee" 'calendar-end-of-month)
1536 (define-key map "\e<" 'calendar-beginning-of-year)
1537 (define-key map "\e>" 'calendar-end-of-year)
1538 (define-key map "\C-@" 'calendar-set-mark)
1539 ;; Many people are used to typing C-SPC and getting C-@.
1540 (define-key map [?\C-\s] 'calendar-set-mark)
1541 (define-key map "\C-x\C-x" 'calendar-exchange-point-and-mark)
1542 (define-key map "\e=" 'calendar-count-days-region)
1543 (define-key map "gd" 'calendar-goto-date)
1544 (define-key map "gD" 'calendar-goto-day-of-year)
1545 (define-key map "gj" 'calendar-julian-goto-date)
1546 (define-key map "ga" 'calendar-astro-goto-day-number)
1547 (define-key map "gh" 'calendar-hebrew-goto-date)
1548 (define-key map "gi" 'calendar-islamic-goto-date)
1549 (define-key map "gb" 'calendar-bahai-goto-date)
1550 (define-key map "gC" 'calendar-chinese-goto-date)
1551 (define-key map "gk" 'calendar-coptic-goto-date)
1552 (define-key map "ge" 'calendar-ethiopic-goto-date)
1553 (define-key map "gp" 'calendar-persian-goto-date)
1554 (define-key map "gc" 'calendar-iso-goto-date)
1555 (define-key map "gw" 'calendar-iso-goto-week)
1556 (define-key map "gf" 'calendar-french-goto-date)
1557 (define-key map "gml" 'calendar-mayan-goto-long-count-date)
1558 (define-key map "gmpc" 'calendar-mayan-previous-round-date)
1559 (define-key map "gmnc" 'calendar-mayan-next-round-date)
1560 (define-key map "gmph" 'calendar-mayan-previous-haab-date)
1561 (define-key map "gmnh" 'calendar-mayan-next-haab-date)
1562 (define-key map "gmpt" 'calendar-mayan-previous-tzolkin-date)
1563 (define-key map "gmnt" 'calendar-mayan-next-tzolkin-date)
1564 (define-key map "Aa" 'appt-add)
1565 (define-key map "Ad" 'appt-delete)
1566 (define-key map "S" 'calendar-sunrise-sunset)
1567 (define-key map "M" 'calendar-lunar-phases)
1568 (define-key map " " 'scroll-other-window)
1569 (define-key map "\d" 'scroll-other-window-down)
1570 (define-key map "\C-c\C-l" 'calendar-redraw)
1571 (define-key map "." 'calendar-goto-today)
1572 (define-key map "o" 'calendar-other-month)
1573 (define-key map "q" 'calendar-exit)
1574 (define-key map "a" 'calendar-list-holidays)
1575 (define-key map "h" 'calendar-cursor-holidays)
1576 (define-key map "x" 'calendar-mark-holidays)
1577 (define-key map "u" 'calendar-unmark)
1578 (define-key map "m" 'diary-mark-entries)
1579 (define-key map "d" 'diary-view-entries)
1580 (define-key map "D" 'diary-view-other-diary-entries)
1581 (define-key map "s" 'diary-show-all-entries)
1582 (define-key map "pd" 'calendar-print-day-of-year)
1583 (define-key map "pC" 'calendar-chinese-print-date)
1584 (define-key map "pk" 'calendar-coptic-print-date)
1585 (define-key map "pe" 'calendar-ethiopic-print-date)
1586 (define-key map "pp" 'calendar-persian-print-date)
1587 (define-key map "pc" 'calendar-iso-print-date)
1588 (define-key map "pj" 'calendar-julian-print-date)
1589 (define-key map "pa" 'calendar-astro-print-day-number)
1590 (define-key map "ph" 'calendar-hebrew-print-date)
1591 (define-key map "pi" 'calendar-islamic-print-date)
1592 (define-key map "pb" 'calendar-bahai-print-date)
1593 (define-key map "pf" 'calendar-french-print-date)
1594 (define-key map "pm" 'calendar-mayan-print-date)
1595 (define-key map "po" 'calendar-print-other-dates)
1596 (define-key map "id" 'diary-insert-entry)
1597 (define-key map "iw" 'diary-insert-weekly-entry)
1598 (define-key map "im" 'diary-insert-monthly-entry)
1599 (define-key map "iy" 'diary-insert-yearly-entry)
1600 (define-key map "ia" 'diary-insert-anniversary-entry)
1601 (define-key map "ib" 'diary-insert-block-entry)
1602 (define-key map "ic" 'diary-insert-cyclic-entry)
1603 (define-key map "ihd" 'diary-hebrew-insert-entry)
1604 (define-key map "ihm" 'diary-hebrew-insert-monthly-entry)
1605 (define-key map "ihy" 'diary-hebrew-insert-yearly-entry)
1606 (define-key map "iid" 'diary-islamic-insert-entry)
1607 (define-key map "iim" 'diary-islamic-insert-monthly-entry)
1608 (define-key map "iiy" 'diary-islamic-insert-yearly-entry)
1609 (define-key map "iBd" 'diary-bahai-insert-entry)
1610 (define-key map "iBm" 'diary-bahai-insert-monthly-entry)
1611 (define-key map "iBy" 'diary-bahai-insert-yearly-entry)
1612 (define-key map "?" 'calendar-goto-info-node)
1613 (define-key map "Hm" 'cal-html-cursor-month)
1614 (define-key map "Hy" 'cal-html-cursor-year)
1615 (define-key map "tm" 'cal-tex-cursor-month)
1616 (define-key map "tM" 'cal-tex-cursor-month-landscape)
1617 (define-key map "td" 'cal-tex-cursor-day)
1618 (define-key map "tw1" 'cal-tex-cursor-week)
1619 (define-key map "tw2" 'cal-tex-cursor-week2)
1620 (define-key map "tw3" 'cal-tex-cursor-week-iso)
1621 (define-key map "tw4" 'cal-tex-cursor-week-monday)
1622 (define-key map "tfd" 'cal-tex-cursor-filofax-daily)
1623 (define-key map "tfw" 'cal-tex-cursor-filofax-2week)
1624 (define-key map "tfW" 'cal-tex-cursor-filofax-week)
1625 (define-key map "tfy" 'cal-tex-cursor-filofax-year)
1626 (define-key map "ty" 'cal-tex-cursor-year)
1627 (define-key map "tY" 'cal-tex-cursor-year-landscape)
1629 (define-key map [menu-bar edit] 'undefined)
1630 (define-key map [menu-bar search] 'undefined)
1632 (easy-menu-define nil map nil cal-menu-sunmoon-menu)
1633 (easy-menu-define nil map nil cal-menu-diary-menu)
1634 (easy-menu-define nil map nil cal-menu-holidays-menu)
1635 (easy-menu-define nil map nil cal-menu-goto-menu)
1636 (easy-menu-define nil map nil cal-menu-scroll-menu)
1638 ;; These are referenced in the default calendar-date-echo-text.
1639 (define-key map [down-mouse-3]
1640 (easy-menu-binding cal-menu-context-mouse-menu))
1641 (define-key map [down-mouse-2]
1642 (easy-menu-binding cal-menu-global-mouse-menu))
1644 ;; cf scroll-bar.el.
1645 (if (and (boundp 'x-toolkit-scroll-bars) x-toolkit-scroll-bars)
1646 (define-key map [vertical-scroll-bar mouse-1]
1647 'calendar-scroll-toolkit-scroll)
1648 ;; Left-click moves us forward in time, right-click backwards.
1649 (define-key map [vertical-scroll-bar mouse-1] 'calendar-scroll-left)
1650 (define-key map [vertical-scroll-bar drag-mouse-1] 'calendar-scroll-left)
1651 ;; down-mouse-2 stays as scroll-bar-drag.
1652 (define-key map [vertical-scroll-bar mouse-3] 'calendar-scroll-right)
1653 (define-key map [vertical-scroll-bar drag-mouse-3]
1654 'calendar-scroll-right))
1655 map)
1656 "Keymap for `calendar-mode'.")
1658 ;; Calendar mode is suitable only for specially formatted data.
1659 (put 'calendar-mode 'mode-class 'special)
1661 (defun calendar-mode-line-entry (command echo &optional key string)
1662 "Return a propertized string for `calendar-mode-line-format'.
1663 COMMAND is a command to run, ECHO is the help-echo text, KEY
1664 is COMMAND's keybinding, STRING describes the binding."
1665 (propertize (or key
1666 (substitute-command-keys
1667 (format "\\<calendar-mode-map>\\[%s] %s" command string)))
1668 'help-echo (format "mouse-1: %s" echo)
1669 'mouse-face 'mode-line-highlight
1670 'keymap (make-mode-line-mouse-map 'mouse-1 command)))
1672 ;; After calendar-mode-map.
1673 (defcustom calendar-mode-line-format
1674 (list
1675 (calendar-mode-line-entry 'calendar-scroll-right "previous month" "<")
1676 "Calendar"
1677 (concat
1678 (calendar-mode-line-entry 'calendar-goto-info-node "read Info on Calendar"
1679 nil "info")
1680 " / "
1681 (calendar-mode-line-entry 'calendar-other-month "choose another month"
1682 nil "other")
1683 " / "
1684 (calendar-mode-line-entry 'calendar-goto-today "go to today's date"
1685 nil "today"))
1686 '(calendar-date-string (calendar-current-date) t)
1687 (calendar-mode-line-entry 'calendar-scroll-left "next month" ">"))
1688 "The mode line of the calendar buffer.
1689 This is a list of items that evaluate to strings. The elements
1690 are evaluated and concatenated, evenly separated by blanks.
1691 During evaluation, the variable `date' is available as the date
1692 nearest the cursor (or today's date if that fails). To update
1693 the mode-line as the cursor moves, add `calendar-update-mode-line'
1694 to `calendar-move-hook'. Here is an example that has the Hebrew date,
1695 the day number/days remaining in the year, and the ISO week/year numbers:
1697 (list
1698 \"\"
1699 '(calendar-hebrew-date-string date)
1700 '(let* ((year (calendar-extract-year date))
1701 (d (calendar-day-number date))
1702 (days-remaining
1703 (- (calendar-day-number (list 12 31 year)) d)))
1704 (format \"%d/%d\" d days-remaining))
1705 '(let* ((d (calendar-absolute-from-gregorian date))
1706 (iso-date (calendar-iso-from-absolute d)))
1707 (format \"ISO week %d of %d\"
1708 (calendar-extract-month iso-date)
1709 (calendar-extract-year iso-date)))
1710 \"\"))"
1711 :risky t
1712 :type 'sexp
1713 :group 'calendar)
1715 (defun calendar-goto-info-node ()
1716 "Go to the info node for the calendar."
1717 (interactive)
1718 (info "(emacs)Calendar/Diary")
1719 (fit-window-to-buffer))
1721 (defvar calendar-mark-ring nil
1722 "Used by `calendar-set-mark'.")
1724 (define-derived-mode calendar-mode nil "Calendar"
1725 "A major mode for the calendar window.
1726 For a complete description, see the info node `Calendar/Diary'.
1728 \\<calendar-mode-map>\\{calendar-mode-map}"
1729 (setq buffer-read-only t
1730 buffer-undo-list t
1731 indent-tabs-mode nil)
1732 (calendar-update-mode-line)
1733 (make-local-variable 'calendar-mark-ring)
1734 (make-local-variable 'displayed-month) ; month in middle of window
1735 (make-local-variable 'displayed-year) ; year in middle of window
1736 ;; Most functions only work if displayed-month and displayed-year are set,
1737 ;; so let's make sure they're always set. Most likely, this will be reset
1738 ;; soon in calendar-generate, but better safe than sorry.
1739 (unless (boundp 'displayed-month) (setq displayed-month 1))
1740 (unless (boundp 'displayed-year) (setq displayed-year 2001))
1741 (set (make-local-variable 'font-lock-defaults)
1742 '(calendar-font-lock-keywords t)))
1744 (defun calendar-string-spread (strings char length)
1745 "Concatenate list of STRINGS separated with copies of CHAR to fill LENGTH.
1746 The effect is like mapconcat but the separating pieces are as balanced as
1747 possible. Each item of STRINGS is evaluated before concatenation so it can
1748 actually be an expression that evaluates to a string. If LENGTH is too short,
1749 the STRINGS are just concatenated and the result truncated."
1750 ;; The algorithm is based on equation (3.25) on page 85 of Concrete
1751 ;; Mathematics by Ronald L. Graham, Donald E. Knuth, and Oren Patashnik,
1752 ;; Addison-Wesley, Reading, MA, 1989.
1753 (let* ((strings (mapcar 'eval
1754 (if (< (length strings) 2)
1755 (append (list "") strings (list ""))
1756 strings)))
1757 (n (- length (length (apply 'concat strings))))
1758 (m (1- (length strings)))
1759 (s (car strings))
1760 (strings (cdr strings))
1761 (i 0))
1762 (dolist (string strings)
1763 (setq s (concat s
1764 (make-string (max 0 (/ (+ n i) m)) char)
1765 string)
1766 i (1+ i)))
1767 (substring s 0 length)))
1769 (defun calendar-update-mode-line ()
1770 "Update the calendar mode line with the current date and date style."
1771 (if (bufferp (get-buffer calendar-buffer))
1772 (with-current-buffer calendar-buffer
1773 (let ((start (- calendar-left-margin 2))
1774 (date (condition-case nil
1775 (calendar-cursor-to-nearest-date)
1776 (error (calendar-current-date)))))
1777 (setq mode-line-format
1778 (concat (make-string (max 0 (+ start
1779 (- (car (window-inside-edges))
1780 (car (window-edges))))) ?\s)
1781 (calendar-string-spread
1782 (mapcar 'eval calendar-mode-line-format)
1783 ?\s (- calendar-right-margin (1- start))))))
1784 (force-mode-line-update))))
1786 (defun calendar-window-list ()
1787 "List of all calendar-related windows."
1788 (let ((calendar-buffers (calendar-buffer-list))
1789 list)
1790 ;; Using 0 rather than t for last argument - see bug#2199.
1791 ;; This is only used with calendar-hide-window, which ignores
1792 ;; iconified frames anyway, so could use 'visible rather than 0.
1793 (walk-windows (lambda (w)
1794 (if (memq (window-buffer w) calendar-buffers)
1795 (push w list)))
1796 nil 0)
1797 list))
1799 (defun calendar-buffer-list ()
1800 "List of all calendar-related buffers (as buffers, not strings)."
1801 (let (buffs)
1802 (dolist (b (list calendar-hebrew-yahrzeit-buffer lunar-phases-buffer
1803 holiday-buffer diary-fancy-buffer solar-sunrises-buffer
1804 (get-file-buffer diary-file)
1805 calendar-buffer calendar-other-calendars-buffer))
1806 (and b (setq b (get-buffer b))
1807 (push b buffs)))
1808 buffs))
1810 (defun calendar-exit ()
1811 "Get out of the calendar window and hide it and related buffers."
1812 (interactive)
1813 (let ((diary-buffer (get-file-buffer diary-file)))
1814 (if (or (not diary-buffer)
1815 (not (buffer-modified-p diary-buffer))
1816 (yes-or-no-p
1817 "Diary modified; do you really want to exit the calendar? "))
1818 ;; Need to do this multiple times because one time can replace some
1819 ;; calendar-related buffers with other calendar-related buffers.
1820 (mapc (lambda (x)
1821 (mapc 'calendar-hide-window (calendar-window-list)))
1822 (calendar-window-list)))))
1824 (define-obsolete-function-alias 'exit-calendar 'calendar-exit "23.1")
1826 (defun calendar-hide-window (window)
1827 "Hide WINDOW if it is calendar-related."
1828 (let ((buffer (if (window-live-p window) (window-buffer window))))
1829 (if (memq buffer (calendar-buffer-list))
1830 (cond
1831 ((and (display-multi-frame-p)
1832 (eq 'icon (cdr (assoc 'visibility
1833 (frame-parameters
1834 (window-frame window))))))
1835 nil)
1836 ((and (display-multi-frame-p) (window-dedicated-p window))
1837 (if calendar-remove-frame-by-deleting
1838 (delete-frame (window-frame window))
1839 (iconify-frame (window-frame window))))
1840 ((not (and (select-window window) (one-window-p window)))
1841 (delete-window window))
1842 (t (set-buffer buffer)
1843 (bury-buffer))))))
1845 (defun calendar-current-date (&optional offset)
1846 "Return the current date in a list (month day year).
1847 Optional integer OFFSET is a number of days from the current date."
1848 (let* ((now (decode-time))
1849 (now (list (nth 4 now) (nth 3 now) (nth 5 now))))
1850 (if (zerop (or offset 0))
1852 (calendar-gregorian-from-absolute
1853 (+ offset (calendar-absolute-from-gregorian now))))))
1855 (defun calendar-column-to-segment ()
1856 "Convert current column to calendar month \"segment\".
1857 The left-most month returns 0, the next right 1, and so on."
1858 (let ((col (max 0 (+ (current-column)
1859 (/ calendar-intermonth-spacing 2)
1860 (- calendar-left-margin)))))
1861 (/ col (+ (* 7 calendar-column-width) calendar-intermonth-spacing))))
1863 (defun calendar-cursor-to-date (&optional error event)
1864 "Return a list (month day year) of current cursor position.
1865 If cursor is not on a specific date, signals an error if optional parameter
1866 ERROR is non-nil, otherwise just returns nil.
1867 If EVENT is non-nil, it's an event indicating the buffer position to
1868 use instead of point."
1869 (with-current-buffer
1870 (if event (window-buffer (posn-window (event-start event)))
1871 (current-buffer))
1872 (save-excursion
1873 (and event (setq event (event-start event))
1874 (goto-char (posn-point event)))
1875 (let* ((segment (calendar-column-to-segment))
1876 (month (% (+ displayed-month (1- segment)) 12)))
1877 ;; Call with point on either of the two digits in a 2-digit date,
1878 ;; or on or before the digit of a 1-digit date.
1879 (if (not (and (looking-at "[ 0-9]?[0-9][^0-9]")
1880 (get-text-property (point) 'date)))
1881 (if error (error "Not on a date!"))
1882 ;; Convert segment to real month and year.
1883 (if (zerop month) (setq month 12))
1884 ;; Go back to before the first date digit.
1885 (or (looking-at " ")
1886 (re-search-backward "[^0-9]"))
1887 (list month
1888 (string-to-number
1889 (buffer-substring (1+ (point))
1890 (+ 1 calendar-day-digit-width (point))))
1891 (cond
1892 ((and (= 12 month) (zerop segment)) (1- displayed-year))
1893 ((and (= 1 month) (= segment 2)) (1+ displayed-year))
1894 (t displayed-year))))))))
1896 (add-to-list 'debug-ignored-errors "Not on a date!")
1898 ;; The following version of calendar-gregorian-from-absolute is preferred for
1899 ;; reasons of clarity, BUT it's much slower than the version that follows it.
1901 ;;(defun calendar-gregorian-from-absolute (date)
1902 ;; "Compute the list (month day year) corresponding to the absolute DATE.
1903 ;;The absolute date is the number of days elapsed since the (imaginary)
1904 ;;Gregorian date Sunday, December 31, 1 BC."
1905 ;; (let* ((approx (/ date 366)) ; approximation from below
1906 ;; (year ; search forward from the approximation
1907 ;; (+ approx
1908 ;; (calendar-sum y approx
1909 ;; (>= date (calendar-absolute-from-gregorian (list 1 1 (1+ y))))
1910 ;; 1)))
1911 ;; (month ; search forward from January
1912 ;; (1+ (calendar-sum m 1
1913 ;; (> date
1914 ;; (calendar-absolute-from-gregorian
1915 ;; (list m (calendar-last-day-of-month m year) year)))
1916 ;; 1)))
1917 ;; (day ; calculate the day by subtraction
1918 ;; (- date
1919 ;; (1- (calendar-absolute-from-gregorian (list month 1 year))))))
1920 ;; (list month day year)))
1922 (defun calendar-gregorian-from-absolute (date)
1923 "Compute the list (month day year) corresponding to the absolute DATE.
1924 The absolute date is the number of days elapsed since the (imaginary)
1925 Gregorian date Sunday, December 31, 1 BC. This function does not
1926 handle dates in years BC."
1927 ;; See the footnote on page 384 of ``Calendrical Calculations, Part II:
1928 ;; Three Historical Calendars'' by E. M. Reingold, N. Dershowitz, and S. M.
1929 ;; Clamen, Software--Practice and Experience, Volume 23, Number 4
1930 ;; (April, 1993), pages 383-404 for an explanation.
1931 (let* ((d0 (1- date))
1932 (n400 (/ d0 146097))
1933 (d1 (% d0 146097))
1934 (n100 (/ d1 36524))
1935 (d2 (% d1 36524))
1936 (n4 (/ d2 1461))
1937 (d3 (% d2 1461))
1938 (n1 (/ d3 365))
1939 (day (1+ (% d3 365)))
1940 (year (+ (* 400 n400) (* 100 n100) (* n4 4) n1))
1941 (month 1)
1942 mdays)
1943 (if (or (= n100 4) (= n1 4))
1944 (list 12 31 year)
1945 (setq year (1+ year))
1946 (while (< (setq mdays (calendar-last-day-of-month month year)) day)
1947 (setq day (- day mdays)
1948 month (1+ month)))
1949 (list month day year))))
1951 (defun calendar-other-month (month year &optional event)
1952 "Display a three-month calendar centered around MONTH and YEAR.
1953 EVENT is an event like `last-nonmenu-event'."
1954 (interactive (let ((event (list last-nonmenu-event)))
1955 (append (calendar-read-date 'noday) event)))
1956 (save-selected-window
1957 (and event
1958 (setq event (event-start event))
1959 (select-window (posn-window event)))
1960 (unless (and (= month displayed-month)
1961 (= year displayed-year))
1962 (let ((old-date (calendar-cursor-to-date))
1963 (today (calendar-current-date)))
1964 (calendar-generate-window month year)
1965 (calendar-cursor-to-visible-date
1966 (cond
1967 ((calendar-date-is-visible-p old-date) old-date)
1968 ((calendar-date-is-visible-p today) today)
1969 (t (list month 1 year))))))))
1971 (defun calendar-set-mark (arg &optional event)
1972 "Mark the date under the cursor, or jump to marked date.
1973 With no prefix argument, push current date onto marked date ring.
1974 With argument ARG, jump to mark, pop it, and put point at end of ring."
1975 (interactive
1976 (list current-prefix-arg last-nonmenu-event))
1977 (let ((date (calendar-cursor-to-date t event)))
1978 (if arg
1979 (if (null calendar-mark-ring)
1980 (error "No mark set in this buffer")
1981 (calendar-goto-date (car calendar-mark-ring))
1982 (setq calendar-mark-ring
1983 (cdr (nconc calendar-mark-ring (list date)))))
1984 (push date calendar-mark-ring)
1985 ;; Since the top of the mark ring is the marked date in the
1986 ;; calendar, the mark ring in the calendar is one longer than
1987 ;; in other buffers to get the same effect.
1988 (if (> (length calendar-mark-ring) (1+ mark-ring-max))
1989 (setcdr (nthcdr mark-ring-max calendar-mark-ring) nil))
1990 (message "Mark set"))))
1992 (defun calendar-exchange-point-and-mark ()
1993 "Exchange the current cursor position with the marked date."
1994 (interactive)
1995 (let ((mark (car calendar-mark-ring))
1996 (date (calendar-cursor-to-date t)))
1997 (if (null mark)
1998 (error "No mark set in this buffer")
1999 (setq calendar-mark-ring (cons date (cdr calendar-mark-ring)))
2000 (calendar-goto-date mark))))
2002 (defun calendar-count-days-region ()
2003 "Count the number of days (inclusive) between point and the mark."
2004 (interactive)
2005 (let* ((days (- (calendar-absolute-from-gregorian
2006 (calendar-cursor-to-date t))
2007 (calendar-absolute-from-gregorian
2008 (or (car calendar-mark-ring)
2009 (error "No mark set in this buffer")))))
2010 (days (1+ (if (> days 0) days (- days)))))
2011 (message "Region has %d day%s (inclusive)"
2012 days (if (> days 1) "s" ""))))
2014 (defun calendar-not-implemented ()
2015 "Not implemented."
2016 (interactive)
2017 (error "%s not available in the calendar"
2018 (global-key-binding (this-command-keys))))
2020 (defun calendar-read (prompt acceptable &optional initial-contents)
2021 "Return an object read from the minibuffer.
2022 Prompt with the string PROMPT and use the function ACCEPTABLE to decide if
2023 entered item is acceptable. If non-nil, optional third arg INITIAL-CONTENTS
2024 is a string to insert in the minibuffer before reading."
2025 (let ((value (read-minibuffer prompt initial-contents)))
2026 (while (not (funcall acceptable value))
2027 (setq value (read-minibuffer prompt initial-contents)))
2028 value))
2031 (defvar calendar-abbrev-length 3
2032 "*Length of abbreviations to be used for day and month names.
2033 See also `calendar-day-abbrev-array' and `calendar-month-abbrev-array'.")
2035 ;; FIXME does it have to start from Sunday?
2036 (defcustom calendar-day-name-array
2037 ["Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday"]
2038 "Array of capitalized strings giving, in order, the day names.
2039 The first two characters of each string will be used to head the
2040 day columns in the calendar. See also the variable
2041 `calendar-day-abbrev-array'."
2042 :group 'calendar
2043 :type '(vector (string :tag "Sunday")
2044 (string :tag "Monday")
2045 (string :tag "Tuesday")
2046 (string :tag "Wednesday")
2047 (string :tag "Thursday")
2048 (string :tag "Friday")
2049 (string :tag "Saturday")))
2051 (defvar calendar-day-abbrev-array
2052 [nil nil nil nil nil nil nil]
2053 "*Array of capitalized strings giving the abbreviated day names.
2054 The order should be the same as that of the full names specified
2055 in `calendar-day-name-array'. These abbreviations may be used
2056 instead of the full names in the diary file. Do not include a
2057 trailing `.' in the strings specified in this variable, though
2058 you may use such in the diary file. If any element of this array
2059 is nil, then the abbreviation will be constructed as the first
2060 `calendar-abbrev-length' characters of the corresponding full name.")
2062 (defcustom calendar-month-name-array
2063 ["January" "February" "March" "April" "May" "June"
2064 "July" "August" "September" "October" "November" "December"]
2065 "Array of capitalized strings giving, in order, the month names.
2066 See also the variable `calendar-month-abbrev-array'."
2067 :group 'calendar
2068 :type '(vector (string :tag "January")
2069 (string :tag "February")
2070 (string :tag "March")
2071 (string :tag "April")
2072 (string :tag "May")
2073 (string :tag "June")
2074 (string :tag "July")
2075 (string :tag "August")
2076 (string :tag "September")
2077 (string :tag "October")
2078 (string :tag "November")
2079 (string :tag "December")))
2081 (defvar calendar-month-abbrev-array
2082 [nil nil nil nil nil nil nil nil nil nil nil nil]
2083 "*Array of capitalized strings giving the abbreviated month names.
2084 The order should be the same as that of the full names specified
2085 in `calendar-month-name-array'. These abbreviations are used in
2086 the calendar menu entries, and can also be used in the diary
2087 file. Do not include a trailing `.' in the strings specified in
2088 this variable, though you may use such in the diary file. If any
2089 element of this array is nil, then the abbreviation will be
2090 constructed as the first `calendar-abbrev-length' characters of the
2091 corresponding full name.")
2093 (defun calendar-make-alist (sequence &optional start-index filter abbrevs)
2094 "Make an assoc list corresponding to SEQUENCE.
2095 Each element of sequence will be associated with an integer, starting
2096 from 1, or from START-INDEX if that is non-nil. If a sequence ABBREVS
2097 is supplied, the function `calendar-abbrev-construct' is used to
2098 construct abbreviations corresponding to the elements in SEQUENCE.
2099 Each abbreviation is entered into the alist with the same
2100 association index as the full name it represents.
2101 If FILTER is provided, apply it to each key in the alist."
2102 (let ((index 0)
2103 (offset (or start-index 1))
2104 (aseq (if abbrevs (calendar-abbrev-construct abbrevs sequence)))
2105 (aseqp (if abbrevs (calendar-abbrev-construct abbrevs sequence
2106 'period)))
2107 alist elem)
2108 (dotimes (i (length sequence) (reverse alist))
2109 (setq index (+ i offset)
2110 elem (elt sequence i)
2111 alist
2112 (cons (cons (if filter (funcall filter elem) elem) index) alist))
2113 (if aseq
2114 (setq elem (elt aseq i)
2115 alist (cons (cons (if filter (funcall filter elem) elem)
2116 index) alist)))
2117 (if aseqp
2118 (setq elem (elt aseqp i)
2119 alist (cons (cons (if filter (funcall filter elem) elem)
2120 index) alist))))))
2122 (defun calendar-read-date (&optional noday)
2123 "Prompt for Gregorian date. Return a list (month day year).
2124 If optional NODAY is t, does not ask for day, but just returns
2125 \(month 1 year); if NODAY is any other non-nil value the value returned is
2126 \(month year)"
2127 (let* ((year (calendar-read
2128 "Year (>0): "
2129 (lambda (x) (> x 0))
2130 (number-to-string (calendar-extract-year
2131 (calendar-current-date)))))
2132 (month-array calendar-month-name-array)
2133 (completion-ignore-case t)
2134 (month (cdr (assoc-string
2135 (completing-read
2136 "Month name: "
2137 (mapcar 'list (append month-array nil))
2138 nil t)
2139 (calendar-make-alist month-array 1) t)))
2140 (last (calendar-last-day-of-month month year)))
2141 (if noday
2142 (if (eq noday t)
2143 (list month 1 year)
2144 (list month year))
2145 (list month
2146 (calendar-read (format "Day (1-%d): " last)
2147 (lambda (x) (and (< 0 x) (<= x last))))
2148 year))))
2150 (defun calendar-interval (mon1 yr1 mon2 yr2)
2151 "The number of months difference between MON1, YR1 and MON2, YR2.
2152 The result is positive if the second date is later than the first.
2153 Negative years are interpreted as years BC; -1 being 1 BC, and so on."
2154 (if (< yr1 0) (setq yr1 (1+ yr1))) ; -1 BC -> 0 AD, etc
2155 (if (< yr2 0) (setq yr2 (1+ yr2)))
2156 (+ (* 12 (- yr2 yr1))
2157 (- mon2 mon1)))
2159 (defun calendar-abbrev-construct (abbrev full &optional period)
2160 "Internal calendar function to return a complete abbreviation array.
2161 ABBREV is an array of abbreviations, FULL the corresponding array
2162 of full names. The return value is the ABBREV array, with any nil
2163 elements replaced by the first three characters taken from the
2164 corresponding element of FULL. If optional argument PERIOD is non-nil,
2165 each element returned has a final `.' character."
2166 (let (elem array name)
2167 (dotimes (i (length full))
2168 (setq name (aref full i)
2169 elem (or (aref abbrev i)
2170 (substring name 0
2171 (min calendar-abbrev-length (length name))))
2172 elem (format "%s%s" elem (if period "." ""))
2173 array (append array (list elem))))
2174 (vconcat array)))
2176 (defvar calendar-font-lock-keywords
2177 `((,(concat (regexp-opt (mapcar 'identity calendar-month-name-array) t)
2178 " -?[0-9]+")
2179 . font-lock-function-name-face) ; month and year
2180 (,(regexp-opt
2181 (list (substring (aref calendar-day-name-array 6)
2182 0 calendar-day-header-width)
2183 (substring (aref calendar-day-name-array 0)
2184 0 calendar-day-header-width)))
2185 ;; Saturdays and Sundays are highlighted differently.
2186 . font-lock-comment-face)
2187 ;; First two chars of each day are used in the calendar.
2188 (,(regexp-opt (mapcar (lambda (x) (substring x 0 calendar-day-header-width))
2189 calendar-day-name-array))
2190 . font-lock-reference-face))
2191 "Default keywords to highlight in Calendar mode.")
2193 (defun calendar-day-name (date &optional abbrev absolute)
2194 "Return a string with the name of the day of the week of DATE.
2195 DATE should be a list in the format (MONTH DAY YEAR), unless the
2196 optional argument ABSOLUTE is non-nil, in which case DATE should
2197 be an integer in the range 0 to 6 corresponding to the day of the
2198 week. Day names are taken from the variable `calendar-day-name-array',
2199 unless the optional argument ABBREV is non-nil, in which case
2200 the variable `calendar-day-abbrev-array' is used."
2201 (aref (if abbrev
2202 (calendar-abbrev-construct calendar-day-abbrev-array
2203 calendar-day-name-array)
2204 calendar-day-name-array)
2205 (if absolute date (calendar-day-of-week date))))
2207 (defun calendar-month-name (month &optional abbrev)
2208 "Return a string with the name of month number MONTH.
2209 Months are numbered from one. Month names are taken from the
2210 variable `calendar-month-name-array', unless the optional
2211 argument ABBREV is non-nil, in which case
2212 `calendar-month-abbrev-array' is used."
2213 (aref (if abbrev
2214 (calendar-abbrev-construct calendar-month-abbrev-array
2215 calendar-month-name-array)
2216 calendar-month-name-array)
2217 (1- month)))
2219 (defun calendar-day-of-week (date)
2220 "Return the day-of-the-week index of DATE, 0 for Sunday, 1 for Monday, etc.
2221 DATE is a list of the form (month day year). A negative year is
2222 interpreted as BC; -1 being 1 BC, and so on."
2223 (mod (calendar-absolute-from-gregorian date) 7))
2225 (defun calendar-week-end-day ()
2226 "Return the index (0 for Sunday, etc.) of the last day of the week."
2227 (mod (+ calendar-week-start-day 6) 7))
2229 (defun calendar-unmark ()
2230 "Delete all diary/holiday marks/highlighting from the calendar."
2231 (interactive)
2232 (setq calendar-mark-holidays-flag nil
2233 calendar-mark-diary-entries-flag nil)
2234 (with-current-buffer calendar-buffer
2235 (mapc 'delete-overlay (overlays-in (point-min) (point-max)))))
2237 (defun calendar-date-is-visible-p (date)
2238 "Return non-nil if DATE is valid and is visible in the calendar window."
2239 (and (calendar-date-is-valid-p date)
2240 (< (abs (calendar-interval
2241 displayed-month displayed-year
2242 (calendar-extract-month date) (calendar-extract-year date)))
2243 2)))
2245 ;; FIXME can this be generalized for holiday-chinese?
2246 (defun calendar-nongregorian-visible-p (month day toabs fromabs switch)
2247 "Return non-nil if MONTH, DAY is visible in the calendar window.
2248 MONTH and DAY are in some non-Gregorian calendar system. The
2249 functions TOABS and FROMABS convert that system to and from
2250 absolute, respectively. SWITCH is a function that takes a single
2251 argument (a local month number). It applies when the local year
2252 changes across the calendar window, and returns non-nil if the
2253 specified month should be associated with the higher year.
2254 Returns the corresponding Gregorian date."
2255 ;; We need to choose the local year associated with month and day
2256 ;; that might make them visible.
2257 (let* ((m1 displayed-month)
2258 (y1 displayed-year)
2259 (m2 displayed-month)
2260 (y2 displayed-year)
2261 ;; Absolute date of first/last dates in calendar window.
2262 (start-date (progn
2263 (calendar-increment-month m1 y1 -1)
2264 (calendar-absolute-from-gregorian (list m1 1 y1))))
2265 (end-date (progn
2266 (calendar-increment-month m2 y2 1)
2267 (calendar-absolute-from-gregorian
2268 (list m2 (calendar-last-day-of-month m2 y2) y2))))
2269 ;; Local date of first/last date in calendar window.
2270 (local-start (funcall fromabs start-date))
2271 (local-end (funcall fromabs end-date))
2272 ;; Local year of first/last dates.
2273 ;; Can only differ if displayed-month = 12, 1, 2.
2274 (local-y1 (calendar-extract-year local-start))
2275 (local-y2 (calendar-extract-year local-end))
2276 ;; Choose which year might be visible in the window.
2277 ;; Obviously it only matters when y1 and y2 differ, ie
2278 ;; when the _local_ new year is visible.
2279 (year (if (funcall switch month) local-y2 local-y1))
2280 (date (calendar-gregorian-from-absolute
2281 (funcall toabs (list month day year)))))
2282 (if (calendar-date-is-visible-p date)
2283 date)))
2285 (defun calendar-date-is-valid-p (date)
2286 "Return t if DATE is a valid date."
2287 (let ((month (calendar-extract-month date))
2288 (day (calendar-extract-day date))
2289 (year (calendar-extract-year date)))
2290 (and (<= 1 month) (<= month 12)
2291 ;; (calendar-read-date t) used to return a date with day = nil.
2292 ;; Should not be valid (?), since many funcs prob assume integer.
2293 ;; (calendar-read-date 'noday) returns (month year), which
2294 ;; currently results in calendar-extract-year returning nil.
2295 day year (<= 1 day) (<= day (calendar-last-day-of-month month year))
2296 ;; BC dates left as non-valid, to suppress errors from
2297 ;; complex holiday algorithms not suitable for years BC.
2298 ;; Note there are side effects on calendar navigation.
2299 (<= 1 year))))
2301 (define-obsolete-function-alias 'calendar-date-is-legal-p
2302 'calendar-date-is-valid-p "23.1")
2304 (defun calendar-date-equal (date1 date2)
2305 "Return t if the DATE1 and DATE2 are the same."
2306 (and
2307 (= (calendar-extract-month date1) (calendar-extract-month date2))
2308 (= (calendar-extract-day date1) (calendar-extract-day date2))
2309 (= (calendar-extract-year date1) (calendar-extract-year date2))))
2311 (defun calendar-make-temp-face (attrlist)
2312 "Return a temporary face based on the attributes in ATTRLIST.
2313 ATTRLIST is a list with elements of the form :face face :foreground color."
2314 (let ((attrs attrlist)
2315 faceinfo face temp-face)
2316 ;; Separate :face from the other attributes. Use the last :face
2317 ;; if there are more than one. FIXME is merging meaningful?
2318 (while attrs
2319 (if (eq (car attrs) :face)
2320 (setq face (intern-soft (cadr attrs))
2321 attrs (cddr attrs))
2322 (push (car attrs) faceinfo)
2323 (setq attrs (cdr attrs))))
2324 (or (facep face) (setq face 'default))
2325 (if (not faceinfo)
2326 ;; No attributes to apply, so just use an existing-face.
2327 face
2328 ;; FIXME should we be using numbered temp-faces, re-using where poss?
2329 (setq temp-face
2330 (make-symbol
2331 (concat ":caltemp"
2332 (mapconcat (lambda (sym)
2333 (cond
2334 ((symbolp sym) (symbol-name sym))
2335 ((numberp sym) (number-to-string sym))
2336 (t sym)))
2337 attrlist ""))))
2338 (make-face temp-face)
2339 (copy-face face temp-face)
2340 ;; Apply the font aspects.
2341 (apply 'set-face-attribute temp-face nil (nreverse faceinfo))
2342 temp-face)))
2344 (defun calendar-mark-visible-date (date &optional mark)
2345 "Mark DATE in the calendar window with MARK.
2346 MARK is a single-character string, a list of face attributes/values, or a face.
2347 MARK defaults to `diary-entry-marker'."
2348 (if (calendar-date-is-valid-p date)
2349 (with-current-buffer calendar-buffer
2350 (save-excursion
2351 (calendar-cursor-to-visible-date date)
2352 (setq mark
2353 (or (and (stringp mark) (= (length mark) 1) mark) ; single-char
2354 ;; The next two use to also check font-lock-mode.
2355 ;; See comments above diary-entry-marker for why
2356 ;; this was dropped.
2357 ;;; (and font-lock-mode
2358 ;;; (or
2359 (and (listp mark) (> (length mark) 0) mark) ; attrs
2360 (and (facep mark) mark) ; )) face-name
2361 diary-entry-marker))
2362 (cond
2363 ;; Face or an attr-list that contained a face.
2364 ((facep mark)
2365 (overlay-put
2366 (make-overlay (1- (point)) (1+ (point))) 'face mark))
2367 ;; Single-character mark, goes after the date.
2368 ((and (stringp mark) (= (length mark) 1))
2369 (overlay-put
2370 (make-overlay (1+ (point)) (+ 2 (point))) 'display mark))
2371 (t ; attr list
2372 (overlay-put
2373 (make-overlay (1- (point)) (1+ (point))) 'face
2374 (calendar-make-temp-face mark))))))))
2376 (define-obsolete-function-alias 'mark-visible-calendar-date
2377 'calendar-mark-visible-date "23.1")
2379 (defun calendar-star-date ()
2380 "Replace the date under the cursor in the calendar window with asterisks.
2381 You might want to add this function to `calendar-today-visible-hook'."
2382 (unless (catch 'found
2383 (dolist (ol (overlays-at (point)))
2384 (and (overlay-get ol 'calendar-star)
2385 (throw 'found t))))
2386 (let ((ol (make-overlay (1- (point)) (point))))
2387 (overlay-put ol 'display "*")
2388 (overlay-put ol 'calendar-star t)
2389 ;; Use copy-sequence to avoid merging of identical 'display props.
2390 ;; Use two overlays so as not to mess up
2391 ;; calendar-cursor-to-nearest-date (and calendar-forward-day).
2392 (overlay-put (setq ol (make-overlay (point) (1+ (point))))
2393 'display (copy-sequence "*"))
2394 (overlay-put ol 'calendar-star t))))
2396 (defun calendar-mark-today ()
2397 "Mark the date under the cursor in the calendar window.
2398 The date is marked with `calendar-today-marker'. You might want to add
2399 this function to `calendar-today-visible-hook'."
2400 (calendar-mark-visible-date (calendar-cursor-to-date) calendar-today-marker))
2402 ;; FIXME why the car? Almost every usage calls list on the args.
2403 (defun calendar-date-compare (date1 date2)
2404 "Return t if DATE1 is before DATE2, nil otherwise.
2405 The actual dates are in the car of DATE1 and DATE2."
2406 (< (calendar-absolute-from-gregorian (car date1))
2407 (calendar-absolute-from-gregorian (car date2))))
2409 (defun calendar-date-string (date &optional abbreviate nodayname)
2410 "A string form of DATE, driven by the variable `calendar-date-display-form'.
2411 An optional parameter ABBREVIATE, when non-nil, causes the month
2412 and day names to be abbreviated as specified by
2413 `calendar-month-abbrev-array' and `calendar-day-abbrev-array',
2414 respectively. An optional parameter NODAYNAME, when t, omits the
2415 name of the day of the week."
2416 (let* ((dayname (unless nodayname (calendar-day-name date abbreviate)))
2417 (month (calendar-extract-month date))
2418 (monthname (calendar-month-name month abbreviate))
2419 (day (number-to-string (calendar-extract-day date)))
2420 (month (number-to-string month))
2421 (year (number-to-string (calendar-extract-year date))))
2422 (mapconcat 'eval calendar-date-display-form "")))
2424 (defun calendar-dayname-on-or-before (dayname date)
2425 "Return the absolute date of the DAYNAME on or before absolute DATE.
2426 DAYNAME=0 means Sunday, DAYNAME=1 means Monday, and so on.
2428 Note: Applying this function to d+6 gives us the DAYNAME on or after an
2429 absolute day d. Similarly, applying it to d+3 gives the DAYNAME nearest to
2430 absolute date d, applying it to d-1 gives the DAYNAME previous to absolute
2431 date d, and applying it to d+7 gives the DAYNAME following absolute date d."
2432 (- date (% (- date dayname) 7)))
2434 (defun calendar-nth-named-absday (n dayname month year &optional day)
2435 "Absolute date of the Nth DAYNAME after/before MONTH YEAR DAY.
2436 A DAYNAME of 0 means Sunday, 1 means Monday, and so on.
2437 If N>0, return the Nth DAYNAME after MONTH DAY, YEAR (inclusive).
2438 If N<0, return the Nth DAYNAME before MONTH DAY, YEAR (inclusive).
2439 DAY defaults to 1 if N>0, and MONTH's last day otherwise."
2440 (if (> n 0)
2441 (+ (* 7 (1- n))
2442 (calendar-dayname-on-or-before
2443 dayname
2444 (+ 6 (calendar-absolute-from-gregorian
2445 (list month (or day 1) year)))))
2446 (+ (* 7 (1+ n))
2447 (calendar-dayname-on-or-before
2448 dayname
2449 (calendar-absolute-from-gregorian
2450 (list month
2451 (or day (calendar-last-day-of-month month year))
2452 year))))))
2454 (defun calendar-nth-named-day (n dayname month year &optional day)
2455 "Date of the Nth DAYNAME after/before MONTH YEAR DAY.
2456 Like `calendar-nth-named-absday', but returns a Gregorian date."
2457 (calendar-gregorian-from-absolute
2458 (calendar-nth-named-absday n dayname month year day)))
2460 (defun calendar-day-of-year-string (&optional date)
2461 "String of day number of year of Gregorian DATE.
2462 Defaults to today's date if DATE is not given."
2463 (let* ((d (or date (calendar-current-date)))
2464 (year (calendar-extract-year d))
2465 (day (calendar-day-number d))
2466 (days-remaining (- (calendar-day-number (list 12 31 year)) day)))
2467 (format "Day %d of %d; %d day%s remaining in the year"
2468 day year days-remaining (if (= days-remaining 1) "" "s"))))
2470 (defun calendar-other-dates (date)
2471 "Return a list of strings giving Gregorian DATE in other calendars.
2472 DATE is (month day year). Calendars that do not apply are omitted."
2473 (let (odate)
2474 (delq nil
2475 (list
2476 (calendar-day-of-year-string date)
2477 (format "ISO date: %s" (calendar-iso-date-string date))
2478 (format "Julian date: %s"
2479 (calendar-julian-date-string date))
2480 (format "Astronomical (Julian) day number (at noon UTC): %s.0"
2481 (calendar-astro-date-string date))
2482 (format "Fixed (RD) date: %s"
2483 (calendar-absolute-from-gregorian date))
2484 (format "Hebrew date (before sunset): %s"
2485 (calendar-hebrew-date-string date))
2486 (format "Persian date: %s"
2487 (calendar-persian-date-string date))
2488 (unless (string-equal
2489 (setq odate (calendar-islamic-date-string date))
2491 (format "Islamic date (before sunset): %s" odate))
2492 (unless (string-equal
2493 (setq odate (calendar-bahai-date-string date))
2495 (format "Baha'i date: %s" odate))
2496 (format "Chinese date: %s"
2497 (calendar-chinese-date-string date))
2498 (unless (string-equal
2499 (setq odate (calendar-coptic-date-string date))
2501 (format "Coptic date: %s" odate))
2502 (unless (string-equal
2503 (setq odate (calendar-ethiopic-date-string date))
2505 (format "Ethiopic date: %s" odate))
2506 (unless (string-equal
2507 (setq odate (calendar-french-date-string date))
2509 (format "French Revolutionary date: %s" odate))
2510 (format "Mayan date: %s"
2511 (calendar-mayan-date-string date))))))
2513 (declare-function x-popup-menu "menu.c" (position menu))
2515 (defun calendar-print-other-dates (&optional event)
2516 "Show dates on other calendars for date under the cursor.
2517 If called by a mouse-event, pops up a menu with the result."
2518 (interactive (list last-nonmenu-event))
2519 (let* ((date (calendar-cursor-to-date t event))
2520 (title (format "%s (Gregorian)" (calendar-date-string date)))
2521 (others (calendar-other-dates date))
2522 selection)
2523 (if (mouse-event-p event)
2524 (and (setq selection (cal-menu-x-popup-menu event title
2525 (mapcar 'list others)))
2526 (call-interactively selection))
2527 (calendar-in-read-only-buffer calendar-other-calendars-buffer
2528 (calendar-set-mode-line title)
2529 (insert (mapconcat 'identity others "\n"))))))
2531 (defun calendar-print-day-of-year ()
2532 "Show day number in year/days remaining in year for date under the cursor."
2533 (interactive)
2534 (message "%s" (calendar-day-of-year-string (calendar-cursor-to-date t))))
2536 (defun calendar-set-mode-line (str)
2537 "Set mode line to STR, centered, surrounded by dashes."
2538 (let* ((edges (window-edges))
2539 ;; As per doc of window-width, total visible mode-line length.
2540 (width (- (nth 2 edges) (car edges))))
2541 ;; Hack for --daemon. See bug #2199.
2542 ;; If no frame exists yet, we have no idea what width to use.
2543 (and (= width 10)
2544 (not window-system)
2545 (setq width (or (getenv "COLUMNS") 80)))
2546 (setq mode-line-format
2547 (if buffer-file-name
2548 `("-" mode-line-modified
2549 ,(calendar-string-spread (list str) ?- (- width 6))
2550 "---")
2551 (calendar-string-spread (list str) ?- width)))))
2553 (defun calendar-version ()
2554 "Display the Calendar version."
2555 (interactive)
2556 (message "GNU Emacs %s" emacs-version))
2558 (make-obsolete 'calendar-version 'emacs-version "23.1")
2561 (run-hooks 'calendar-load-hook)
2563 (provide 'calendar)
2565 ;; Local variables:
2566 ;; byte-compile-dynamic: t
2567 ;; End:
2569 ;;; calendar.el ends here